How to print gradients of network parameters when defining networks with nn.sequential?

In the 60-minute tutorial on pytorch, I learned the following lines to print gradients of network parameters.

print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)

However, when using nn.sequential to define the network, it will not print properly, it report an error ‘print(net.features.0.weight.grad) SyntaxError: invalid syntax’, how can I print the gradient in this case?

Below is an example code

>>> import  torchvision.models as models
>>> net = models.vgg16(pretrained=False)
>>> print(net.state_dict().keys())
odict_keys(['features.0.weight', 'features.0.bias', 'features.2.weight', 'features.2.bias', 'features.5.weight', 'features.5.bias', 'features.7.weight', 'features.7.bias', 'features.10.weight', 'features.10.bias', 'features.12.weight', 'features.12.bias', 'features.14.weight', 'features.14.bias', 'features.17.weight', 'features.17.bias', 'features.19.weight', 'features.19.bias', 'features.21.weight', 'features.21.bias', 'features.24.weight', 'features.24.bias', 'features.26.weight', 'features.26.bias', 'features.28.weight', 'features.28.bias', 'classifier.0.weight', 'classifier.0.bias', 'classifier.3.weight', 'classifier.3.bias', 'classifier.6.weight', 'classifier.6.bias'])

>>> print(net.features.0.weight.grad)
  File "<stdin>", line 1
    print(net.features.0.weight.grad)
                       ^
SyntaxError: invalid syntax

1 Like
print(net.features[0].weight.grad)
1 Like