I don’t know what might create the non-leaf tensor in your code, but this minimal code snippet seems to work:
x = torch.randn(1, 1)
x.requires_grad_()
print(x.is_leaf)

model = nn.Linear(1, 1)
out = model(x)
out.backward()
print(x.grad)




 enterthevoidf22:

furthermore, perhaps you can shed light about the data attribute. i can’t figure out when to use it when updating a tensor.


I wouldn’t recommend to use the .data attribute at all but instead wrap the code in a with torch.no_grad() block, if you want to apply an operation without tracking it via Autograd.
There were some use cases e.g. inside optimizers, which should have been removed by now.