This error is happening because item() can only be called on a Tensor containing single element for example [1]. If you call item() on [1, 1] it will give this error. Also in PyTorch custom loss functions are suppose to return a scale value. For example below is a simple implementation of mean squared loss function



Custom loss functions


def my_loss(output, target): loss = torch.mean((output - target)**2) return loss