# How to manipulate layer parameters by it's names?

**URL:** <https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282>\
**Category:** Uncategorized\
**Created:** [March 23, 2017, 2:04am UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282 "2017-03-23T02:04:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeakey](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zeakey/32/4341_2.png) [@zeakey](https://discuss.pytorch.org/u/zeakey)\
**Post date:** [March 23, 2017, 2:04am UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/1 "2017-03-23T02:04:16Z")

</div>

I have a complicated CNN model that contains many layers, I want to copy **some of the layer** parameters from external data, such as a **numpy array**.

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:

```auto
params = list(model.parameters())

for p in params:
    print p.size()

```

But how can I get parameter according to a layer name and then change its values?

* * *

## What I want to do can be described below:

```auto
caffe_params = caffe_model.parameters()

caffe_params['conv3_1'] = np.zeros((64, 128, 3, 3))

```

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [March 23, 2017, 3:51am UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/2 "2017-03-23T03:51:17Z")

</div>

you can get the params via: `params = model.state_dict()` and then they will be a dictionary whose name will be similar to `conv3_1`

---

<div class="post-metadata">

**Author:** ![solidor](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@solidor](https://discuss.pytorch.org/u/solidor)\
**Post date:** [June 8, 2017, 10:37pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/3 "2017-06-08T22:37:36Z")

</div>

Is it possible obtain objects of type `Parameter` by name? The use case is to do something like:

```python
optimizer = optim.Adam([param for name, param in model.state_dict().iteritems()
                        if 'foo' in name], lr=args.lr)

```

but each `param` here will be a `FloatTensor` so the optimizer throws a `TypeError`

---

<div class="post-metadata">

**Author:** ![solidor](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@solidor](https://discuss.pytorch.org/u/solidor)\
**Post date:** [June 8, 2017, 10:51pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/4 "2017-06-08T22:51:52Z")

</div>

Turns out I needed to update my Pytorch version - `nn.Module.named_parameters()` does the trick

---

<div class="post-metadata">

**Author:** ![Nan\_Meng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nan_meng/32/16502_2.png) [@Nan\_Meng](https://discuss.pytorch.org/u/Nan_Meng)\
**Post date:** [July 10, 2017, 5:38am UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/5 "2017-07-10T05:38:47Z")

</div>

Hi Smth,

Is there some way I can name a layer using a custom name.

Thanks.

---

<div class="post-metadata">

**Author:** ![chsasank](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chsasank/32/340_2.png) [@chsasank](https://discuss.pytorch.org/u/chsasank)\
**Post date:** [July 10, 2017, 8:28am UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/6 "2017-07-10T08:28:40Z")

</div>

Yep. Just `self.add_module('your_name', nn.YourModule(params))` in your nn.Module

---

<div class="post-metadata">

**Author:** ![Nan\_Meng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nan_meng/32/16502_2.png) [@Nan\_Meng](https://discuss.pytorch.org/u/Nan_Meng)\
**Post date:** [July 10, 2017, 5:03pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/7 "2017-07-10T17:03:38Z")

</div>

thank you so much !!!

---

<div class="post-metadata">

**Author:** ![Emrah\_Yigit](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/ba8739/32.png) [@Emrah\_Yigit](https://discuss.pytorch.org/u/Emrah_Yigit)\
**Post date:** [February 11, 2018, 7:14am UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/8 "2018-02-11T07:14:36Z")

</div>

- `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

Is there any way that we can list **all parameters** allowing us to update **name** , **value** and **requires\_grad** properties?

---

<div class="post-metadata">

**Author:** ![An\_Tran](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/an_tran/32/7791_2.png) [@An\_Tran](https://discuss.pytorch.org/u/An_Tran)\
**Post date:** [November 26, 2018, 7:23am UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/9 "2018-11-26T07:23:10Z")

</div>

> [@solidor](#):
>
> The use case is to do something like

The following code is working on PyTorch 1.0-dev with `nn.Module.named_parameters()`, I think it is a workable work-around for me.

````auto
for name, param in model.named_parameters():
    param.requires_grad = False```
````

---

<div class="post-metadata">

**Author:** ![Brando\_Miranda](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/32/14355_2.png) [@Brando\_Miranda](https://discuss.pytorch.org/u/Brando_Miranda)\
**Post date:** [August 7, 2020, 9:51pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/10 "2020-08-07T21:51:02Z")

</div>

btw you can also do this (# [How to get the module names of nn.Sequential](https://discuss.pytorch.org/t/how-to-get-the-module-names-of-nn-sequential/39682)  
):

```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)

```

---

<div class="post-metadata">

**Author:** ![whanafy](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/46a35a/32.png) [@whanafy](https://discuss.pytorch.org/u/whanafy)\
**Post date:** [August 10, 2020, 6:11pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/11 "2020-08-10T18:11:49Z")

</div>

Just to be sure, this will replace the current module with this new module if the name is the same?

---

<div class="post-metadata">

**Author:** ![gilbertocunha](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gilbertocunha/32/27690_2.png) [@gilbertocunha](https://discuss.pytorch.org/u/gilbertocunha)\
**Post date:** [February 3, 2021, 9:24pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/12 "2021-02-03T21:24:44Z")

</div>

Just wanted to know, have you found any solution to this problem?

---

<div class="post-metadata">

**Author:** ![Alon41](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alon41/32/45119_2.png) [@Alon41](https://discuss.pytorch.org/u/Alon41)\
**Post date:** [October 26, 2022, 12:10pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-layer-parameters-by-its-names/1282/13 "2022-10-26T12:10:57Z")

</div>

For example, to scale a specific layer by a scalar c I did the following:

```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

```
