grad_fn=<DivBackward0>,what's problems with the console output?

d=2
n=50
X=torch.randn(n,d)
true_w=torch.tensor([[-1.0],[2.0]])
y=X@true_w+torch.randn(n,1)*0.1
print(‘X shape’,X.shape)
print(‘y shape’,y.shape)
print(‘w shape’,true_w.shape)

def model(X,w):
return X@w

def rss(y,y_hat):
return torch.norm(y-y_hat)**2/n
def grad_rss(X,y,y_hat):
return -2*X.t()@(y-y_hat)/n

w=torch.tensor([[1.0],[0.0]],requires_grad=True)
y_hat=model(X,w)
loss=rss(y,y_hat)
loss.backward()
print(‘Analytical gradient’, grad_rss(X, y, y_hat).detach().view(2).numpy())
print(‘PyTorch’s gradient’, w.grad.view(2).numpy())
print(grad_rss(X,y,y_hat))

console:
grad_rss(X,y,y_hat)
tensor([[ 5.1305],
[-3.3439]], grad_fn=)

Could you explain the issue with the output, please?
What do you expect and what are you seeing at the moment?

PS: You can post code snippets by wrapping them into three backticks ``` :wink:

Extra characters in output.
I define a function and input tensors.

Which extra characters are problematic?

So why does this output appear?(grad_fn=)

Using your code I get the following output:

X shape torch.Size([50, 2])
y shape torch.Size([50, 1])
w shape torch.Size([2, 1])
Analytical gradient [ 4.254795  -3.4482186]
PyTorch’s gradient [ 4.2547956 -3.4482183]
tensor([[ 4.2548],
        [-3.4482]], grad_fn=<DivBackward0>)

Oh,Thank you for your previous reply.I just want to know what the words (<grad_fn=>)on the right mean except numbers.

grad_fn gives you the name of the backward function for this particular tensor.
Since the last operation was a division, the result will have the gradient function grad_fn=<DivBackward> attached to it. Autograd uses these functions to step back through the computation graph and calculate the gradients for all parameters.

I have understood the problem. Thank you very much

1 Like