I made computation graph for simple funtion.
function y=3*(x**2) + 4*x+2
however, PyTorch code as follows,
The gradient for ‘x’ came out as 22, but why does the gradient for ‘a’ come out as None?
I wanted it to come out as 3, but it comes out as None…
How can I make the gradient of a come out?
PyTorch code as follows,
Thanks in advance.
import torch
x = torch.tensor([3.0], requires_grad=True)
a = torch.pow(x,2)
b = torch.mul(3,a)
c = torch.mul(4,x)
y = b+c+2
y.backward()
print(‘x.data:’, x.data)
print(‘x.grad:’, x.grad)
print(‘x.grad_fn:’, x.grad_fn)
print()
print(‘a.data:’, a.data)
print(‘a.grad:’, a.grad)
print(‘a.grad_fn:’, a.grad_fn)