Can you change the behaviour to check for if not p.requires_grad: ?
Consider the scenario where you are training a GAN and you have a generator and discriminator.
Now the front of the discriminator is a pretrained CNN (like resnet18(pretrained=True) ) and we don’t want to train this. We only want to train the final Linear layer of the discriminator.
However setting cnn.requires_grad=False will not work since generator output (fake images) requires gradient. The gradient has to flow through the frozen CNN back into the generator in order to train it.
Right now for this scenario you need to manually select parameters from the discriminator and pass it to the optimizer to avoid changing the weights of the pretrained CNN.