Multi-target not supported at /tmp/pip-req-build-58y_cjjl/aten/src/THNN/generic/ClassNLLCriterion.c:22

username: Sarath_Raveendran
cooked: <p>Training set input size: torch.Size([19400, 1, 6, 100])<br>
Training set labels size: torch.Size([19400, 5])<br>
Test set input size: torch.Size([4851, 1, 6, 100])<br>
Test set labels size: torch.Size([4851, 5])<br>
Batch Size: 264<br>
I am using a data loader to load this data to a neural network with an architecture as coded below:<br>
class ConvUIL(nn.Module):</p>
<pre><code>input_size = [6,100]
output_size = 5
input_channels = 1
channels_conv1 = 16
channels_conv2 = 48
kernel_conv1 = [1, 13]
kernel_conv2 = [1, 13]
pool_conv1 = [1, 2]
pool_conv2 = [1, 2]
channels_conv3 = 96
channels_conv4 = 128
channels_conv5 = 264
kernel_conv3 = [1, 11]
kernel_conv4 = [1, 3]
kernel_conv5 = [1, 3]


fcl1_size = 50

def __init__(self):
    super(ConvUIL, self).__init__()
    
    # Define the convolutional layers
    self.conv1 = nn.Conv2d(self.input_channels, self.channels_conv1, self.kernel_conv1)
    self.conv2 = nn.Conv2d(self.channels_conv1, self.channels_conv2, self.kernel_conv2)
    self.conv3 = nn.Conv2d(self.channels_conv2, self.channels_conv3, self.kernel_conv3)
    self.conv4 = nn.Conv2d(self.channels_conv3, self.channels_conv4, self.kernel_conv4)
    self.conv5 = nn.Conv2d(self.channels_conv4, self.channels_conv5, self.kernel_conv5)
    
    # Calculate the convolutional layers output size (stride = 1)
    
    self.conv_out_size = int(264*6*2)
    
    
    # Define the fully connected layers
    self.fcl1 = nn.Linear(self.conv_out_size, self.fcl1_size)
    self.fcl2 = nn.Linear(self.fcl1_size, self.output_size)
    
def forward(self, x):
    # Apply convolution 1 and pooling
    x = self.conv1(x)
    x = F.relu(x)
    x = F.max_pool2d(x, self.pool_conv1)
    
    # Apply convolution 2 and pooling
    x = self.conv2(x)
    x = F.relu(x)
    x = F.max_pool2d(x, self.pool_conv2)
    
    # Apply convolution 3 
    x = self.conv3(x)
    x = F.relu(x)
    
    # Apply convolution 4
    x = self.conv4(x)
    x = F.relu(x)
    
    # Apply convolution 5 
    x = self.conv5(x)
    x = F.relu(x)
    
    # Reshape x to one dimmension to use as input for the fully connected layers
    x = x.view(-1, self.conv_out_size)
    
    # Fully connected layers
    x = self.fcl1(x)
    x = F.relu(x)
    x = self.fcl2(x)
    
    
    return F.log_softmax(x, dim=1)
</code></pre>
<p>criterion = nn.NLLLoss()<br>
optimizer = optim.Adam(model.parameters(), lr=0.01)</p>
<p>from torch.autograd import Variable</p>
<p>epochs = 20</p>
<p>for epoch in range(epochs):<br>
loss = 0<br>
correct = 0<br>
total = wind_test_y.size(0)</p>
<pre><code># Train the model
for i, (observations, labels) in enumerate(train_dataset):
    observations = Variable(observations)
    labels = Variable(labels)
    
    # Forward pass
    optimizer.zero_grad()
    outputs = convUIL(observations)
    print(outputs)
    print(labels)
    
    # Backward pass
    loss = criterion(outputs, labels)
    loss.backward()
    
    # Optimize
    optimizer.step()
    
# Test the model on the validation data
for observations, labels in test_dataset:
    observations = Variable(observations)
    
    # Forward pass
    outputs = convUIL(observations)
    _, predicted = torch.max(outputs.data, 1)
    correct = (predicted == labels).sum()
    
accuracy = correct / total
print('Epoch [%2d/%2d], Accuracy: %.4f' % (epoch + 1, epochs, accuracy))
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/a/5a21b6e84ae6949a769ac40588099be63fcefd6b.png" data-download-href="https://discuss.pytorch.org/uploads/default/5a21b6e84ae6949a769ac40588099be63fcefd6b" title="Screenshot from 2020-09-22 23-34-47"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/a/5a21b6e84ae6949a769ac40588099be63fcefd6b_2_690x441.png" alt="Screenshot from 2020-09-22 23-34-47" data-base62-sha1="cRlcJ79Jcq1M9Okv87W46j9BuuD" width="690" height="441" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/a/5a21b6e84ae6949a769ac40588099be63fcefd6b_2_690x441.png, https://discuss.pytorch.org/uploads/default/original/3X/5/a/5a21b6e84ae6949a769ac40588099be63fcefd6b.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/a/5a21b6e84ae6949a769ac40588099be63fcefd6b.png 2x" data-dominant-color="F5F7F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2020-09-22 23-34-47</span><span class="informations">886×567 82.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p><code>nn.NLLLoss</code> expects the target to have the shape <code>[batch_size]</code> for a multi-class classification and contain the class indices in the range <code>[0, nb_classes-1]</code>.<br>
Your target tensor seems to be one-hot encoded. If that’s the case, you can transform it to the right shape via <code>target = torch.argmax(target, dim=1)</code>.</p>

username: Sarath_Raveendran
cooked: <p>Thanks, it worked. You saved my day <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>