How to manipulate layer parameters by it’s names?
So how can I set one specific layer’s parameters by the layer name, say “conv3_3” ? In pytorch I get the model  parameters via: <SCODE>params = list(model.parameters())

for p in params:
    print p.size()
<ECODE> But how can I get parameter according to a layer name and then change its values? <SCODE>caffe_params = caffe_model.parameters()

caffe_params['conv3_1'] = np.zeros((64, 128, 3, 3))
<ECODE>
zeakey
smth
<SCODE>optimizer = optim.Adam([param for name, param in model.state_dict().iteritems()
                        if 'foo' in name], lr=args.lr)
<ECODE>
solidor
solidor
Hi Smth, Is there some way I can name a layer using a custom name. Thanks.
Nan_Meng
chsasank
thank you so much !!!
Nan_Meng

nn.Module.named_parameters() does not contain parameters like batch_norm’s running_mean and running_var
 
nn.Module.parameters() does not contain parameter names 
model.state_dict() does not contain parameters that we can update requires_grad property
Emrah_Yigit
<SCODE>for name, param in model.named_parameters():
    param.requires_grad = False```<ECODE>
An_Tran
<SCODE># 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)
<ECODE>
Brando_Miranda
Just to be sure, this will replace the current module with this new module if the name is the same?
whanafy
Just wanted to know, have you found any solution to this problem?
gilbertocunha
For example, to scale a specific layer by a scalar c I did the following: <SCODE>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
<ECODE>
Alon41