If the goal is to update few parameters, then the method mentioned in the top post should be very slow. That’s because when you call backwards() all the gradients are computed anyway so you lose the speed advantage of computing only few gradients.
You can change the forward pass using the zero-weight mask so that only the gradients of the non-zero weights are computed.