Hi @ptrblck:
Could you please help me out, I have tow classes 0 and 1 and I am using nn.CrossEntropyLoss() as criterion. (generally I am using the Siamese network in case you feel there is something different, but it is not important for this question as I get the output as required (batch size, number of classes) and the target label of size (batch size).
I followed all other threads and this one and tried all proposed solutions but still get this error. Can you have a look, please! I have been stuck with this for ages.
the shape of all what I have is printed in the output cell:
Code:
`model.train() # prep model for training
        for i, batch in enumerate(train_loader, 0):
            x1, x2, label, _, _ = batch
            x1, x2, label= x1.to(device), x2.to(device), label.to(device)
            # clear the gradients of all optimized variables
            optimizer.zero_grad()
            # forward pass: compute predicted outputs by passing inputs to the model
            output = model.forward(x1, x2)
            # calculate the loss
            print('output', output)
            print('output.data', output.data)
            print('torch.max(label, 1)[1]', torch.max(label, 1)[1])
            print('target label', label)
            print('torch.max(output, 1)[1]', torch.max(output, 1)[1])
            _, predicted= torch.max(output, 1)
            print('predicted', predicted)
            loss = criterion(output.data, label.long())
            # backward pass: compute gradient of the loss with respect to model parameters
            loss.backward()
            # perform a single optimization step (parameter update)
            optimizer.step()
            # record training loss
            train_losses.append(loss.item())
 `

Output:
training has started
output tensor([[-0.0406, -0.0517],
[-0.0390, -0.0647],
[-0.0264, -0.0449],
[-0.0102, -0.0353],
[-0.0430, -0.0276],
[-0.0200, -0.0663],
[-0.0251, -0.0698],
[-0.0400, -0.0760],
[-0.0667, -0.0711],
[-0.0206, -0.0685],
[-0.0214, -0.0880],
[-0.0150, -0.0579],
[ 0.0153, -0.0565],
[-0.0247, -0.0516],
[-0.0076, -0.0543],
[-0.0179, -0.0716]], device=‘cuda:0’, grad_fn=)
output.data tensor([[-0.0406, -0.0517],
[-0.0390, -0.0647],
[-0.0264, -0.0449],
[-0.0102, -0.0353],
[-0.0430, -0.0276],
[-0.0200, -0.0663],
[-0.0251, -0.0698],
[-0.0400, -0.0760],
[-0.0667, -0.0711],
[-0.0206, -0.0685],
[-0.0214, -0.0880],
[-0.0150, -0.0579],
[ 0.0153, -0.0565],
[-0.0247, -0.0516],
[-0.0076, -0.0543],
[-0.0179, -0.0716]], device=‘cuda:0’)
torch.max(label, 1)[1] tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], device=‘cuda:0’)
target label tensor([[1.],
[0.],
[1.],
[1.],
[0.],
[1.],
[0.],
[1.],
[0.],
[0.],
[0.],
[1.],
[1.],
[1.],
[0.],
[1.]], device=‘cuda:0’)
torch.max(output, 1)[1] tensor([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], device=‘cuda:0’)
predicted tensor([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], device=‘cuda:0’)
RuntimeError                              Traceback (most recent call last)
 in ()
2 # early stopping patience; how long to wait after last time validation loss improved.
3 patience = 20
----> 4 model, train_loss, valid_loss = train_model(model, batch_size, patience, n_epochs)
4 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
1869                          .format(input.size(0), target.size(0)))
1870     if dim == 2:
-> 1871         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
1872     elif dim == 4:
1873         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15
I tried without “.data” as well it did not work.
I look forward to your favourable reply!