Hello,
Suppose that I have a simple network as follows:
class Net(nn.Module):
    def __init__(self, in_channels):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=3)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=3)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        return x

In training loop, I would like to do (inside a loop)

compute y = Net(x)
lock conv1
loss1 = criterion1(y, target1)
backward()
optimizer.step()
lock conv2, unlock conv1
loss2 = criterion2(y, target2)
backward()
unlock conv2
optimizer.step()

So, to lock and unlock I did
for p in net.conv1.parameters()
    p.requires_grad= False (or True to unlock)

Here, optimizer is created with all parameters of Net.
I expected the second backward() updates weights of conv1 only, but it updates conv2 also.
Do I need to do requires_grad setting of the network just once?
The behaviour is somewhat unpredictable, some layers seem to be frozen while some layers don’t.
Thank you.