tsuijenk:

But I also read on this forum that I have to set input x with requires_grad=True before passing into the network, since I had previously set it to False.


No, this would only be needed, if you really want to calculate gradients in the input (e.g. for adversarial attacks).



 tsuijenk:

But as for groundtruth, despite we use it in the loss function, do we still need to set requires_grad=True for that? I’ve tried that and still get the same runtime error.


This is also not needed and I don’t know right now a valid use case where the targets need gradients.
Could you explain how this return value is treated?
return torch.mean(torch.FloatTensor(loss))

Are you calling backward() on this averaged loss? If so, this won’t work since you are recreating a new tensor and will thus break the computation graph. Try to use torch.stack or torch.cat instead.