Hi,
You can go through every parameters that you don’t want to update and set them not to require gradients:
for p in layer2.parameters():
    p.requires_grad = False

This means that no gradients will be computed for these parameters.
As further optimization, you can give to your optimizer only the layer you want to optimize such that it won’t check every other parameters that won’t have gradients:
my_opt = torch.optim.SGD(model.layer2.parameters(), other_args)