Hi Arijit!



 arijitthegame:

I am trying to use autograd.grad to compute derivative of some function I have defined.
…
But the output I get is (None,).


The short answer is that you aren’t using pytorch to calculate your
function.  (You are using python and numpy.)  So pytorch knows
nothing about the relationship between y and x, and can’t calculate
the derivative (gradient) for you.


import numpy as np



Clear your mind of numpy – work with pytorch tensors, instead.  Only
import numpy if there is something you need to do that you can’t do
without numpy.


import torch
from torch.autograd import grad

x = np.array([1, 2, 3 , 4])
y = x*x 



x doesn’t have requires_grad set yet (e.g. x.requires_grad_(True)),
so autograd won’t track the gradient of y with respect to x.  (Of course,
x isn’t even a pytorch tensor yet.)


x.requires_grad_(True)



At this point, the cow is already out of the barn.  You’ve already
calculated x*x, and there is no way for pytorch to go back and
figure out what you did before you set x.requires_grad_(True).
It would be well worth taking a look at pytorch’s autograd tutorial 3.
Good luck.
K. Frank