How to print model's parameters with its name and `requires_grad value`?

jef: <p>I want to print model’s parameters with its name. I found two ways to print summary. But I want to use both <code>requires_grad</code> and <code>name</code> at same <code>for</code> loop. Can I do this? I want to check gradients during the training.</p>
<pre><code class="lang-auto">for p in model.parameters():
    # p.requires_grad: bool
    # p.data: Tensor

for name, param in model.state_dict().items():
    # name: str
    # param: Tensor

# my fake code
for p in model.parameters():
    if p.requires_grad:
         print(p.name, p.data)
</code></pre>

b4s1cv8vc: <p>You can try this:</p>
<pre><code class="lang-auto">for name, param in model.named_parameters():
    if param.requires_grad:
        print name, param.data
</code></pre>

jef: <aside class="quote no-group" data-username="b4s1cv8vc" data-post="2" data-topic="10778">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/b4s1cv8vc/48/10018_2.png" class="avatar"> b4s1cv8vc:</div>
<blockquote>
<p>for name, param in model.named_parameters():<br>
if param.requires_grad:<br>
print name, param.data</p>
</blockquote>
</aside>
<p>Nice! This is really what I want</p>

sksq96: <p>You can use the package <a href="https://github.com/sksq96/pytorch-summary" rel="nofollow noopener">pytorch-summary</a>.</p>
<p>Example to print all the layer information for VGG:</p>
<pre><code class="lang-python">import torch
from torchvision import models
from torchsummary import summary

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
vgg = models.vgg16().to(device)

summary(vgg, (3, 224, 224))
</code></pre>
<pre><code class="lang-auto">----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 224, 224]           1,792
              ReLU-2         [-1, 64, 224, 224]               0
            Conv2d-3         [-1, 64, 224, 224]          36,928
              ReLU-4         [-1, 64, 224, 224]               0
         MaxPool2d-5         [-1, 64, 112, 112]               0
            Conv2d-6        [-1, 128, 112, 112]          73,856
              ReLU-7        [-1, 128, 112, 112]               0
            Conv2d-8        [-1, 128, 112, 112]         147,584
              ReLU-9        [-1, 128, 112, 112]               0
        MaxPool2d-10          [-1, 128, 56, 56]               0
           Conv2d-11          [-1, 256, 56, 56]         295,168
             ReLU-12          [-1, 256, 56, 56]               0
           Conv2d-13          [-1, 256, 56, 56]         590,080
             ReLU-14          [-1, 256, 56, 56]               0
           Conv2d-15          [-1, 256, 56, 56]         590,080
             ReLU-16          [-1, 256, 56, 56]               0
        MaxPool2d-17          [-1, 256, 28, 28]               0
           Conv2d-18          [-1, 512, 28, 28]       1,180,160
             ReLU-19          [-1, 512, 28, 28]               0
           Conv2d-20          [-1, 512, 28, 28]       2,359,808
             ReLU-21          [-1, 512, 28, 28]               0
           Conv2d-22          [-1, 512, 28, 28]       2,359,808
             ReLU-23          [-1, 512, 28, 28]               0
        MaxPool2d-24          [-1, 512, 14, 14]               0
           Conv2d-25          [-1, 512, 14, 14]       2,359,808
             ReLU-26          [-1, 512, 14, 14]               0
           Conv2d-27          [-1, 512, 14, 14]       2,359,808
             ReLU-28          [-1, 512, 14, 14]               0
           Conv2d-29          [-1, 512, 14, 14]       2,359,808
             ReLU-30          [-1, 512, 14, 14]               0
        MaxPool2d-31            [-1, 512, 7, 7]               0
           Linear-32                 [-1, 4096]     102,764,544
             ReLU-33                 [-1, 4096]               0
          Dropout-34                 [-1, 4096]               0
           Linear-35                 [-1, 4096]      16,781,312
             ReLU-36                 [-1, 4096]               0
          Dropout-37                 [-1, 4096]               0
           Linear-38                 [-1, 1000]       4,097,000
================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 218.59
Params size (MB): 527.79
Estimated Total Size (MB): 746.96
----------------------------------------------------------------
</code></pre>

ginobilinie: <aside class="quote no-group" data-username="b4s1cv8vc" data-post="2" data-topic="10778">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/b4s1cv8vc/48/10018_2.png" class="avatar"> b4s1cv8vc:</div>
<blockquote>
<p>for name, param in model.named_parameters(): if param.requires_grad: print name, param.data</p>
</blockquote>
</aside>
<p>Great, this solves my problem.</p>

TonyYaoMSU: <p>Thx a lot<br>
This makes a lot of sense.</p>

Bill_Collins: <p>This doesn’t work if the model is made of Modules.</p>

palguna_gopireddy: <aside class="quote no-group" data-username="sksq96" data-post="5" data-topic="10778">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sksq96/48/13282_2.png" class="avatar"> sksq96:</div>
<blockquote>
<p><code>summary(vgg, (3, 224, 224))</code></p>
</blockquote>
</aside>
<p>Thank you. It is very helpful</p>