How to print loss function gradient?

I want to print the gradient calculated after calling backword on the loss function by

loss.backward()
print(loss.grad)

but this order gave me None.

For each weight in your model you can get the gradients with respect to that weight by calling weight.grad.

Trying to get the gradient of the loss with respect to itself has no useful meaning.

3 Likes