In this pytorch code:
JavaScript
x
13
13
1
import torch
2
a = torch.tensor([2.], requires_grad=True)
3
y = torch.zeros((10))
4
gt = torch.zeros((10))
5
6
y[0] = a
7
y[1] = y[0] * 2
8
y.retain_grad()
9
10
loss = torch.sum((y-gt) ** 2)
11
loss.backward()
12
print(y.grad)
13
I want y[0]’s gradient to consist 2 parts:
- loss backward to y[0] itself.
- y[0] is used to calculate y[1], so it should have the part of y[1]’s gradient.
but when I run this code, there is only part 1 in y[0]’s gradient.
So how to make y[0]’s gradient to have all 2 parts?
edit: the output is:
JavaScript
1
2
1
tensor([4., 8., 0., 0., 0., 0., 0., 0., 0., 0.])
2
but I expect:
JavaScript
1
2
1
tensor([20., 8., 0., 0., 0., 0., 0., 0., 0., 0.])
2
Advertisement
Answer
y[0]
and y[1]
are two different elements, therefore they have different grad
. The only thing that “binds” them is the underlying relation to a
. If you inspect the grad of a
, you’ll see:
JavaScript
1
2
1
print(a.grad)
2
JavaScript121tensor([20.])
2
That is, the two parts of the gradients are combined in a.grad
.