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

username: Leon_Lopez
cooked: <p>Hello!</p>
<p>First I have a dataset that loads a matrix with the input data and the targets.<br>
Input data and targets are loaded like numpy arrays and they have variable size.<br>
The model is very simple. The problem is when I have to put the inputs in the model.</p>
<pre><code class="lang-auto">class MusicDataSet(Dataset):
    """CQT dataset."""

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

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

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

        return inp, target
    
    def valoresNormalizacion(self):
        print('Calculando Media y STD...')
        ms = self.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
</code></pre>
<p>Here is my model:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>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.</p>
<pre><code class="lang-auto">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 = torch.utils.data.DataLoader(musicSet, 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)

</code></pre>
<p>he problem is that now I get the following error when I try to start the model:</p>
<pre><code class="lang-auto">Traceback (most recent call last):

  File "&lt;ipython-input-183-cf70272d89f8&gt;", line 1, in &lt;module&gt;
    runfile('C:/Users/Antonio López León/Documents/Universidad/TFG/PROYECTO/BBDD/audioCQT/ProcesamientoNN.py', wdir='C:/Users/Antonio López León/Documents/Universidad/TFG/PROYECTO/BBDD/audioCQT')

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

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

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

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

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

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

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

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

AttributeError: 'list' object has no attribute 'dim'
</code></pre>
<p>Thanks and help!</p>