Custom collate_fn returns a list and I can't use it for neural network input


First I have a dataset that loads a matrix with the input data and the targets.
Input data and targets are loaded like numpy arrays and they have variable size.
The model is very simple. The problem is when I have to put the inputs in the model.

class MusicDataSet(Dataset):
    """CQT dataset."""

    def __init__(self, transform=None):
       ,, self.tam = sd.cargarDatos()  

        self.mean, self.std = self.valoresNormalizacion()            

    def __len__(self):
        return self.tam
    def __getitem__(self, idx):
        inp = ([idx]-self.mean)/self.std
        #A tensor
        inp = torch.from_numpy(inp).float()
        inp = inp.t()
        #to cuda
        inp ='cuda')
        target =[idx]
        #A tensor
        target= torch.from_numpy([idx])
        # LONG
        target = target.long()
        target = target.t()
        target ='cuda')

        return inp, target
    def valoresNormalizacion(self):
        print('Calculando Media y STD...')
        ms =
        total_media = np.zeros(len(ms), dtype=float)
        total_std = np.zeros(len(ms), dtype=float)
        for j in range(len(ms)):
            media = np.zeros(len(ms[j]), dtype=float)
            std = np.zeros(len(ms[j]), dtype = float)
            for i in range(len(ms[j])):    
                media[i] = np.mean(ms[j][i])
                std[i] = np.std(ms[j][i])
            m = np.mean(media)
            s = np.std(std)
            total_media[j] = m
            total_std[j] = s
        media = np.mean(total_media)
        std = np.std(total_std)
        print('Media:', media, '  STD: ', std)
        return media, std

Here is my model:

class MLP(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super(MLP, self).__init__()
        self.input_dim = input_dim
        self.dense1 = nn.Linear(input_dim, hidden_dim[0])      # first hidden layer
        self.dense2 = nn.Linear(hidden_dim[0], hidden_dim[1])  # second hidden layer
        self.dense3 = nn.Linear(hidden_dim[1], output_dim)  # third hidden layer   
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(p=0.5)
    def forward(self, x):
        x = self.relu(self.dense1(x))
        x = self.relu(self.dense2(x))
        x = self.relu(self.dense3(x))
        return x

As the input of the model is variable, I have made a custom collate_fn, since it was impossible for me to go through the dataLoader in any other way.

def music_collate_fn(batch):
    data = [item[0] for item in batch]
    #this doesn't work either
   #data = torch.tensor(data, dtype=torch.float)
    target = [item[1] for item in batch]
    #target = torch.tensor(target, dtype=torch.long)
    return data, target

train_loader =, batch_size=50, shuffle=False, collate_fn=music_collate_fn)
input_dim = 252
hidden_dim = (512,1024,512)
output_dim = 88

mlp = rn.MLP(input_dim, hidden_dim, output_dim).to(device)
optimizer = torch.optim.RMSprop(mlp.parameters(), lr = learning_rate)
criterion = nn.CrossEntropyLoss()

for x,y in train_loader:
    outputs = mlp(x).to(device)

he problem is that now I get the following error when I try to start the model:

Traceback (most recent call last):

  File "<ipython-input-183-cf70272d89f8>", line 1, in <module>
    runfile('C:/Users/Antonio López León/Documents/Universidad/TFG/PROYECTO/BBDD/audioCQT/', wdir='C:/Users/Antonio López León/Documents/Universidad/TFG/PROYECTO/BBDD/audioCQT')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\", line 704, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\", line 108, in execfile
    exec(compile(, filename, 'exec'), namespace)

  File "C:/Users/Antonio López León/Documents/Universidad/TFG/PROYECTO/BBDD/audioCQT/", line 102, in <module>
    outputs = mlp(x).to(device)

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\", line 489, in __call__
    result = self.forward(*input, **kwargs)

  File "C:\Users\Antonio López León\Documents\Universidad\TFG\PROYECTO\BBDD\audioCQT\", line 24, in forward
    x = self.relu(self.dense1(x))

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\", line 489, in __call__
    result = self.forward(*input, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\", line 67, in forward
    return F.linear(input, self.weight, self.bias)

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\", line 1350, in linear
    if input.dim() == 2 and bias is not None:

AttributeError: 'list' object has no attribute 'dim'

Thanks and help!