I think there are two ways to do this.

Only pass the parameters of the earlier layer(s) you want to update to the optimizer

optim = Adam([param for param in layer1.parameters()])


Manually set all gradients of layers you do not want to update to 0 after calling loss.backward()


for param in layer2.parameters():
    param.grad[:] = 0