As I mentioned above: You should be careful with your optimizer because even if your gradients are  0 , if you have weight decay or momentum, your weights are going to be updated.
Also with this code, the new values are set in a non differentiable manner. This means that the training will just compute gradient for all the weights. If you want to prevent the update, you can add a hook that will set the unwanted gradients to 0.