試圖總結我的頭圍繞如何梯度代表,以及如何autograd作品:爲什麼autograd不會爲中間變量產生漸變?
import torch
from torch.autograd import Variable
x = Variable(torch.Tensor([2]), requires_grad=True)
y = x * x
z = y * y
z.backward()
print(x.grad)
#Variable containing:
#32
#[torch.FloatTensor of size 1]
print(y.grad)
#None
爲什麼它不會產生梯度y
?如果y.grad = dz/dy
,那麼它不應該至少產生像y.grad = 2*y
這樣的變量嗎?
我認爲這是一個有趣的問題發佈在https://discuss.pytorch.org/ –