Update:

I actually found loss.sum().backward() worked.

or can use:

h = loss.size()[1]

w = loss.size()[2]

loss.backward(torch.Tensor(1, h, w))

Btw, is loss.sum().backward() better or worse compared to loss.backward(torch.Tensor(1, h, w))?

Thanks

============================

Hi, I’m using LogSoftmax(dim=1) for 4D input (batch_size, num_classes, height, width), and with NLLLoss, and generated a 3D tensor.

But when calling loss.backward(), I got error:

‘’

RuntimeError: grad can be implicitly created only for scalar outputs

‘’

from the line loss.backward().

Can anyone help?

logsoftmax = nn.LogSoftmax(dim=1).cuda()

criterion = torch.nn.NLLLoss(reduce = False).cuda()

outputs = logsoftmax(outputs)

loss = criterion(outputs, labels)

loss.backward()

Thanks.