Search code examples
image-processingmachine-learningcomputer-visionpytorchgradient-descent

Does the input of model require gradient?


In pytorch, when do we need to set the require_grad of input tensor to be True?


Solution

  • Usually, the inputs are fixed - we do not change the images, we only infer the labels/outputs from the fixed input images.
    Since they are fixed - there is no need to compute a gradient w.r.t the input, only w.r.t trainable parameters.

    Having said that, there are cases where you want to change the inputs. For instance, when you want to visualize features. See, e.g., this nice post.