# How the pytorch freeze network in some layers, only the rest of the training?

**URL:** <https://discuss.pytorch.org/t/how-the-pytorch-freeze-network-in-some-layers-only-the-rest-of-the-training/7088>\
**Category:** Uncategorized\
**Created:** [September 6, 2017, 2:24am UTC](https://discuss.pytorch.org/t/how-the-pytorch-freeze-network-in-some-layers-only-the-rest-of-the-training/7088 "2017-09-06T02:24:01Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![L0SG](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/l0sg/32/1481_2.png) [@L0SG](https://discuss.pytorch.org/u/L0SG)\
**Post date:** [May 1, 2018, 3:10am UTC](https://discuss.pytorch.org/t/how-the-pytorch-freeze-network-in-some-layers-only-the-rest-of-the-training/7088/14 "2018-05-01T03:10:59Z")

</div>

Yes, it does work when you add the parameters with `requires_grad=True` to the optimizer then setting to `False` after. You can also find out yourself by commenting out

```auto
optimizer = optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=0.1)

```

In the snippet above, since the previous optimizer contains all parameters including the fc2 with the changed `requires_grad` flag.

Note that the above snippet assumed a common “train =\> save =\> load =\> freeze parts” scenario.

---

_[View the full topic](https://discuss.pytorch.org/t/how-the-pytorch-freeze-network-in-some-layers-only-the-rest-of-the-training/7088)._
