Solved by ptrblck in post #2 
              
              
                Based on the error message it seems that loss is not a scalar value but a tensor with multiple values. 
You could either reduce it to a scalar (e.g. with torch.mean, if that would fir your use case) or you could pass the gradients to backward e.g. via loss.backward(torch.ones_like(loss)).