TensorDictBase.zero_grad#
- TensorDictBase.zero_grad(set_to_none: bool = True) Self#
Resets the
.gradof every leaf, nested ones included, and returnsself.- Parameters:
set_to_none (bool, optional) – if
True, sets each.gradtoNone. IfFalse, fills each existing.gradwith zeros in place. Defaults toTrue.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(x=torch.ones(3, requires_grad=True), batch_size=[3]) >>> td["x"].sum().backward() >>> print(td.zero_grad()["x"].grad) None
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.