About tensor.grad

Hi, I have a really simple question.

image

In the above code, I understand why tensor b’s requires_grad and is_leaf changes after adding tensor a to b.
But I don’t understand why b.grad is None and the a.grad value is
a.grad == tensor([[1., 1.],
[1., 1.]])

Could you give some explanation about this?

Hi,

b.grad is None because it is not a leaf. You can call .retain_grad() on it (after the inplace update) if you want the .grad field to be populated anyways.

1 Like