TensorDictBase.grad#
- property TensorDictBase.grad: Any#
A tensordict of the
.gradof every leaf, orNoneif no leaf has one.Leaves whose
.gradisNoneare left out. Assigningtd.grad = othersets the.gradof each leaf from the matching leaf ofother, or copies into it if it exists.Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(x=torch.ones(3, requires_grad=True), y=torch.ones(3, requires_grad=True), batch_size=[3]) >>> (td["x"] * 2).sum().backward() >>> td.grad["x"], "y" in td.grad.keys() (tensor([2., 2., 2.]), False)
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.