Error related to upgrading pytorch 2.0.1 - raise RuntimeError('each element in list of batch should be of equal size') RuntimeError: each element in list of batch should be of equal size

Hello all, I don’t have this error before upgrading pytorch to 2.0.1.


Lib\site-packages\torch\utils\data\_utils\collate.py", line 138, in collate
    raise RuntimeError('each element in list of batch should be of equal size')
RuntimeError: each element in list of batch should be of equal size

I tried to use collate_fn=lambda x: x in my dataloader like this:

    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=args.batch_size, shuffle=True,
        num_workers=args.workers, pin_memory=True, collate_fn=lambda x: x)

but I get an error:

    for i, (inputs, target, _) in enumerate(train_loader):
           ^^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 3)

My task is MLP+GRU, and my model looks like this:

class MLP(nn.Module):
  
  def __init__(self, num_classes, rnn_layers, hidden_size, fc_size):
    super(MLP, self).__init__()
    self.hidden_size = hidden_size
    self.num_classes = num_classes
    self.fc_size = fc_size
    self.apply(self._init_weights)

    self.fc_pre= nn.Sequential(
                            nn.Linear(201, fc_size),
                            
                           nn.ReLU())
    #Defines the number of features that define each element (time-stamp) of the input sequence
    self.rnn = nn.GRU(input_size = fc_size,
                hidden_size = hidden_size,
                num_layers = rnn_layers,
                batch_first = True)
    self.fc = nn.Linear(hidden_size, num_classes)


  def forward(self, inputs, hidden=None, steps=0):
        length = len(inputs)
        # print('inputs size')
        # print(inputs[3].size())
        #Input data: RNN should have 3 dimensions. (Batch Size, Sequence Length and Input Dimension (the number of expected features which is 201)
        fs = torch.zeros(inputs[0].size(0), length, self.rnn.input_size).cuda()

        for i in range(length):
            f = inputs[i]
            #flattens the tensor
            f = f.view(f.size(0), -1)
            #print(f.size())
            f = self.fc_pre(f)

            # stores the tensor f in the sha pe [fs.size(0), fs.size(2)] to each “row” in fs to prepare for learning the sequence in RNN
            fs[:, i, :] = f
            # print('fs')
            # print(fs.size())
       
        #outputs : batch size, seq legnth, hidden size
        outputs, hidden = self.rnn(fs, hidden)
        #print(outputs.size())
        
        #the training code applies crossentropyloss as criterion which also applies softmax to the output so I don't have to use softmax here
        outputs = self.fc(outputs)
        
        #print(outputs)
        #print(outputs.size())
        
        return outputs

Any suggestions/ideas for me? Thank you in advance

I think I should write a custom collate_fn to pad all the elements in my batches in the same size. But I don’t know how to go with it.