Hi,
Hope everyone is staying safe. I realize this may be a really stupid question. I am trying to use autograd.grad to compute derivative of some function I have defined. For simplicity, let us use a simple function f(x) = x^2. Here is my code:
import numpy as np
import torch
from torch.autograd import grad
x = np.array([1, 2, 3 , 4])
y = x*x
x = torch.from_numpy(x).float()
y = torch.from_numpy(y).float()
x.requires_grad_(True)
y.requires_grad_(True)
#Now I want to compute the derivative of y with respect to x
dy = grad(y, x, grad_outputs = torch.ones_like(x),
create_graph=True, retain_graph=True,
only_inputs=True,
allow_unused=True
)[0]
But the output I get is (None,).
I am very confused. Any help will be highly appreciated. Thanks a lot in advance.