How to obtain the derivative of the network output with respect to the input？

username: Inz-Zos
cooked: <p>I want to get the derivative of the network output with respect to the input to see the state of the prediction model. My initial idea was to use the hook function to get the gradient of each layer and then multiply it to get the result. But I don’t know exactly how to do it.</p>

username: srishti-git1110
cooked: <p>Hi,<br>
You could simply use torch.autograd functionality. There could be a bunch of ways to do the same thing.</p>
<p>I recommend reading up on the official documentation to get hold of the autograd engine. Feel free to post any queries here.</p>

username: Inz-Zos
cooked: <p>Thanks, but after I used the autograd function, it only supports derivatives of scalars. I used weighting the output tensor, but it doesn’t seem to work.I would like to ask if there is another way.</p>
<pre><code class="lang-auto">for step in range(200):
    pred_y=model(input_x)
    dy_dx=torch.autograd.grad(pred_y,input_x)
    print('dy/dx: ',dy_dx)
    loss=loss_func(pred_y,input_y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
</code></pre>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:\Users\Slive\PycharmProjects\pythonProject\main.py", line 41, in &lt;module&gt;
    dy_dx=torch.autograd.grad(pred_y,input_x)
  File "C:\Users\Slive\anaconda3\envs\Pt\lib\site-packages\torch\autograd\__init__.py", line 150, in grad
    grad_outputs = _make_grads(outputs, grad_outputs)
  File "C:\Users\Slive\anaconda3\envs\Pt\lib\site-packages\torch\autograd\__init__.py", line 34, in _make_grads
    raise RuntimeError("grad can be implicitly created only for scalar outputs")
RuntimeError: grad can be implicitly created only for scalar outputs

</code></pre>

username: srishti-git1110
cooked: <p>Hi, yes that behaviour is expected in case <code>loss</code> tensor (the one you call backward on) isn’t a scalar (a tensor containing a single element). You could choose to apply some reduction on it like -</p>
<pre><code class="lang-python">loss.sum().backward()
</code></pre>
<p>Or,<br>
As is specified in the <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.backward.html#torch-tensor-backward" rel="noopener nofollow ugc">docs</a>, an additional <code>gradient</code> argument needs to be specified in the backward call on a multidimensional tensor.</p>
<p><code>gradient</code> is a tensor of matching type and location, and contains the gradient of the differentiated function w.r.t.  itself. Mathematically, it’s <code>dLoss/dLoss</code>. So, the following should work -</p>
<pre><code class="lang-python">loss.backward(torch.ones_like(loss))
</code></pre>

username: AlphaBetaGamma96
cooked: <p>Hi <a class="mention" href="/u/inz-zos">@Inz-Zos</a>,</p>
<p>If you want an explicit function for your derivative, you can use the functorch library (which comes packaged with the latest PyTorch install). More info can be found in the docs: <a href="https://pytorch.org/functorch/1.13/notebooks/per_sample_grads.html" class="inline-onebox" rel="noopener nofollow ugc">Per-sample-gradients — functorch 1.13 documentation</a></p>
<p>An example,</p>
<pre><code class="lang-auto">from functorch import make_functional, vmap, grad jacrev

model = Model(*args, **kwargs) #model instance

fnet, params = make_functional(model) #functorch needs a functionalized model

#if your model has multiple outputs use jacrev instead of grad, i.e. jacrev(fnet, argnums=1)
grad_fnet = grad(fnet, argnums=1) #creates expilict function of gradient of y w.r.t x

per_sample_jacobian = vmap(grad_fnet, in_dims=(None, 0))(params, x) #per-sample gradients 
</code></pre>

username: Inz-Zos
cooked: <p>Thank you so much! Just got through the modifications and can already come up with the results.</p>

username: Inz-Zos
cooked: <p>Thank you, I will continue to learn more about your method. <img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=12" title=":+1:" class="emoji" alt=":+1:" loading="lazy" width="20" height="20"></p>