Leaf tensor but without grad after bachward

import torch

a = torch.rand(5, 5, requires_grad=False)
b = torch.rand(5, 5, requires_grad=False)
c = torch.rand(5, 5, requires_grad=True)

f = a + b
g = b + c

y = f + g
y.backward(torch.ones_like(y))

print(f.is_leaf, f.grad_fn, f.grad)

output: True None None

Why f doesn’t have its grad? It is a leaf variable.

It is but it does not require gradients. So no gradients are computed for it.

1 Like

Try manually setting f to require gradients:

f.requires_grad_(True)

1 Like