How to manipulate layer parameters by it's names?

zeakey: <p>I have a complicated CNN model that contains many layers, I want to copy <strong>some of the layer</strong> parameters from external data, such as a <strong>numpy array</strong>.</p>
<p>So how can I set one specific layer’s parameters by the layer name, say “conv3_3” ?</p>
<p>In pytorch I get the model  parameters via:</p>
<pre><code class="lang-auto">params = list(model.parameters())

for p in params:
    print p.size()
</code></pre>
<p>But how can I get parameter according to a layer name and then change its values?</p>
<hr>
<h2>What I want to do can be described below:</h2>
<pre><code class="lang-auto">caffe_params = caffe_model.parameters()

caffe_params['conv3_1'] = np.zeros((64, 128, 3, 3))
</code></pre>

smth: <p>you can get the params via: <code>params = model.state_dict()</code> and then they will be a dictionary whose name will be similar to <code>conv3_1</code></p>

solidor: <p>Is it possible obtain objects of type <code>Parameter</code> by name? The use case is to do something like:</p>
<pre><code class="lang-python">optimizer = optim.Adam([param for name, param in model.state_dict().iteritems()
                        if 'foo' in name], lr=args.lr)
</code></pre>
<p>but each <code>param</code> here will be a <code>FloatTensor</code> so the optimizer throws a <code>TypeError</code></p>

solidor: <p>Turns out I needed to update my Pytorch version - <code>nn.Module.named_parameters()</code> does the trick</p>

Nan_Meng: <p>Hi Smth,</p>
<p>Is there some way I can name a layer using a custom name.</p>
<p>Thanks.</p>

chsasank: <p>Yep. Just <code>self.add_module('your_name', nn.YourModule(params))</code> in your nn.Module</p>

Nan_Meng: <p>thank you so much !!!</p>

Emrah_Yigit: <ul>
<li>
<code>nn.Module.named_parameters()</code> does not contain parameters like batch_norm’s <strong>running_mean</strong> and <strong>running_var</strong>
</li>
<li>
<code>nn.Module.parameters()</code> does not contain parameter names</li>
<li>
<code>model.state_dict()</code> does not contain parameters that we can update <strong>requires_grad</strong> property</li>
</ul>
<p>Is there any way that we can list <strong>all parameters</strong> allowing us to update <strong>name</strong>, <strong>value</strong> and <strong>requires_grad</strong> properties?</p>

An_Tran: <aside class="quote no-group" data-username="solidor" data-post="3" data-topic="1282">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/dbc845/48.png" class="avatar"> solidor:</div>
<blockquote>
<p>The use case is to do something like</p>
</blockquote>
</aside>
<p>The following code is working on PyTorch 1.0-dev with <code>nn.Module.named_parameters()</code>, I think it is a workable work-around for me.</p>
<pre><code class="lang-auto">for name, param in model.named_parameters():
    param.requires_grad = False```</code></pre>

Brando_Miranda: <p>btw you can also do this (# <a href="https://discuss.pytorch.org/t/how-to-get-the-module-names-of-nn-sequential/39682" class="inline-onebox">How to get the module names of nn.Sequential</a><br>
):</p>
<pre><code class="lang-auto"># https://discuss.pytorch.org/t/how-to-get-the-module-names-of-nn-sequential/39682
# looping through modules but get the one with a specific name

import torch
import torch.nn as nn

from collections import OrderedDict

params = OrderedDict([
    ('fc0', nn.Linear(in_features=4,out_features=4)),
    ('ReLU0', nn.ReLU()),
    ('fc1L:final', nn.Linear(in_features=4,out_features=1))
])
mdl = nn.Sequential(params)

# throws error
# mdl['fc0']

for m in mdl.children():
    print(m)

print()

for m in mdl.modules():
    print(m)

print()

for name, m in mdl.named_modules():
    print(name)
    print(m)

print()

for name, m in mdl.named_children():
    print(name)
    print(m)
</code></pre>

whanafy: <p>Just to be sure, this will replace the current module with this new module if the name is the same?</p>

gilbertocunha: <p>Just wanted to know, have you found any solution to this problem?</p>

Alon41: <p>For example, to scale a specific layer by a scalar c I did the following:</p>
<pre><code class="lang-auto">weight_name = 'fc2.weight'
c = 100
for name, param in model.named_parameters():
    if param.requires_grad:
        if name == weight_name:
            param.data = c*param.data
</code></pre>