I am trying to train a model using multiple data loaders.
The code I use is as follows:
loss_list = list()
    for epoch in range(cfg.start_epoch, cfg.max_epoch):
        batch_time = AverageMeter()
        data_time = AverageMeter()
        losses = AverageMeter()
        losses_exist = AverageMeter()

        print("Train Epoch: {}".format(epoch))

        model.train()

        end = time.time()
        for i in range(len(train_loader_list)):
            train_loader = train_loader_list[i]
            for batch_idx, sample in enumerate(train_loader):
                data_time.update(time.time() - end)

                optimizer.zero_grad()
                model.zero_grad()

                input = torch.autograd.Variable(sample['image'].cuda())

                target = torch.autograd.Variable(sample['label'].cuda())
                target_exist = torch.autograd.Variable(sample['exist'].cuda())

                output, output_exist = model(input)  # output_mid
                loss_seg = criterion(torch.nn.functional.log_softmax(output, dim=1), target)
                loss_exist = criterion_exist(output_exist, target_exist)
                loss = loss_seg + loss_exist * 0.1

                losses.update(loss.data.item(), input.size(0))
                losses_exist.update(loss_exist.item(), input.size(0))

                # optimizer.zero_grad()
                loss.backward(retain_graph=True)
                optimizer.step()

                # scheduler.step()

                # measure elapsed time
                batch_time.update(time.time() - end)
                end = time.time()
               

                if batch_idx == len(train_loader) - 1:
                    loss_list.append(loss)

        for loss_sample in loss_list:
            loss += loss_sample.item()

        loss_tot = loss / len(loss_list)

        optimizer.zero_grad()
        loss_tot.backward()
        optimizer.step()

I don’t know why this doesn’t work.
I get an error in the backward part.
I searched many places but couldn’t find the answer.
Helpme.