RunTimeerror: grad can be implicitly created only for scalar outputs

I tried the multilabel classifier training,but function loss.backwork can not

pass through. It throws the error as follows:

RunTimeerror: grad can be implicitly created only for scalar outputs

some of my code is as follows:

loss=criterion(pred,label)
where pred and label have shapes as: [batchsize,class_number]
criterion = nn.BCEWithLogitsLoss(weight=None, reduce=False)

I read from document that loss must is scalar, should I use only batchsize 1?

Could anybody help me?

I assume you are not summing up / reducing the loss-value in any other (manual) way? You should be able to do

loss = criterion(pred, label) 
loss.backward(torch.ones_like(loss))

Per default a scalar one is propagated back to calculate the gradients but for an arbitrary loss-tensor-size you need to manually specify the tensor which should be propagated back.

2 Likes

Thank you. I have changed the criteron definition as:
criterion = nn.BCEWithLogitsLoss (weight=None, reduce=True),
It’s now can train.

why my multi-label model can not converge, each image have 60 labels and I use 1000 image to train.