How can I get the gradients of the weights of each layer?

Once you’ve called backward to calculate the gradients, you can directly print them using something like this:

model = nn.Sequential(
    nn.Linear(10, 2)
)
...
loss.backward()
print(model[0].weight.grad)

In your case the model definition will look a bit different. So depending how you’ve implemented the model, you might need to index the layers like in my example or call the layers directly with their names:

print(model.my_layer.weight.grad)
22 Likes