Generally, for any machine learning/deep learning system, we compute a loss, $L = l(x, \theta, y)$ which is a function of the input feature vector $x$ (after activation), model parameters $\theta$ (which actually is encoded in $x$ since $x$ is the final feature vector), and $y$, the label. Now, to learn, we compute $\frac{\partial L}{\partial \theta}$ and update the parameters, $\theta$ using this.

However, pytorch (and other libraries) also provide a way to hook the gradients of the feature vectors as well, (torch.tensor.register_hook()). Let us say I hook this to $x$. What exactly is this gradient? I believe that this is $\frac{\partial L}{\partial x}$. And if so, what is the use of this gradient? What information can this gradient give us? Have there been any works on exploring this gradient and its uses? Any references would be appreciated. Thanks!

  • $\begingroup$ just a quick comment, not all ML algorithms are based on a gradient computation (decision trees for instance) $\endgroup$
    – etiennedm
    Mar 4 at 5:27
  • $\begingroup$ I think the gradient with respect to the raw data doesn't really tell you anything because the raw data cannot be changed. It only tells you that for the current iteration, to obtain the optimal mapping to the output, the input should have differed by this much. This can be seen by following the chain rule for standard neural networks for back propagation, you will encounter the derivative for dL/dz where z is the intermediate results. This will have the exact same interpretation as with dL/dx, just with the intermediate result z rather than the input x. $\endgroup$
    – timmy1691
    Mar 4 at 20:43
  • $\begingroup$ @timmy1691 yeah that was my interpretation, but I still wonder why this feature exists and if there is any use to it, thanks $\endgroup$ Mar 4 at 22:55
  • $\begingroup$ @OlorinIstari this is because with respect to the chain rule, you need it to complete the chain rule, other than this, I don't think there is any use, if you consider using gradient descent for logistic regression or linear regression, that derivative is never calculated. It exists and can be calculated but is never calculated solely for the training of the model. $\endgroup$
    – timmy1691
    Mar 5 at 10:08

1 Answer 1


Think of 𝑥 as a representation of how a change in the input features would affect the loss. Understanding the value provides some utility, such as how sensitive the model is to input features.

A great example is the generation of adversarial examples and data augmentation. It can also be used for neural style transfer and generative models. There are many other potential use cases as well.

See the following papers for reference:

Explaining and Harnessing Adversarial Examples (Goodfellow et al., 2014) https://arxiv.org/abs/1412.6572

Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization (Selvaraju et al., 2016) https://arxiv.org/abs/1610.02391

I have never done this with PyTorch because I primarily use TF/Keras/Jax; however, the concept is the same.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.