How to freeze a specific layer in pytorch?

I found one post here: How the pytorch freeze network in some layers, only the rest of the training? but it does not answer my question. If I create a layer called conv1 = nn.Conv2d(3,3,2), then how do I freeze this specific layer?

3 Likes

This should do the trick:

for param in conv1.parameters():
    param.requires_grad = False
11 Likes

As defined above, conv1 contains 3 filters of dimensions (3,2,2). What should I do if I wanted to freeze the second filter only?