When might I want to use requires_grad=True on an input?

The question kind of speaks for itself, but I’ll try to explain a bit further…

As far as I understand, generally you want a Variable to have gradient if it contains some learnable parameters. However, it is possible (and can be found in some PyTorch docs code segments) that you might want the input Variables to track the gradients. Practically speaking, when might you want to do this?


As a very quick answer, you might want to do something like Neural Style Transfer where you are optimizing your input image.

There are many many many other settings that you’d want to do this too.


Unless you want to change the input (for cnns, the input is usually just an image), you do not need to set requires_grad=True for the image Variable.

1 Like