# 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:** 2

<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:** [September 6, 2017, 2:34am UTC](https://discuss.pytorch.org/t/how-the-pytorch-freeze-network-in-some-layers-only-the-rest-of-the-training/7088/2 "2017-09-06T02:34:12Z")

</div>

Each parameters of the model have requires\_grad flag:  
[http://pytorch.org/docs/master/notes/autograd.html](http://pytorch.org/docs/master/notes/autograd.html)

For resnet example in the doc, this loop will freeze all layers

```
for param in model.parameters():
    param.requires_grad = False

```

For partially unfreezing some of the last layers, we can identify parameters we want to unfreeze in this loop. setting the flag to True will suffice.

---

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