wsy
(sy w)
April 12, 2020, 11:06am
1
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 ```
wsy
(sy w)
April 13, 2020, 8:19am
3
Extra characters in output.
I define a function and input tensors.
Which extra characters are problematic?
wsy
(sy w)
April 13, 2020, 9:06am
5
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>)
wsy
(sy w)
April 13, 2020, 10:13am
7
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.
1 Like
wsy
(sy w)
April 14, 2020, 2:22am
9
I have understood the problem. Thank you very much
1 Like