Rate this Page
★ ★ ★ ★ ★

TensorDictBase.abs#

TensorDictBase.abs() → Self#

Returns a new tensordict with torch.abs() applied to every leaf.

The other unary operations (exp(), log(), sqrt(), sin()…) work the same way. Each has an in-place twin with a trailing underscore (abs_(), exp_()…) that writes into the leaves and returns self. See In-place operations.

Examples

>>> import torch
>>> from tensordict import TensorDict
>>> td = TensorDict(
...     a=torch.tensor([-1.0, 2.0]),
...     b=TensorDict(c=torch.tensor([-3.0, 0.0]), batch_size=[2]),
...     batch_size=[2],
... )
>>> td.abs()["b", "c"]
tensor([3., 0.])
>>> td.exp()["a"]
tensor([0.3679, 7.3891])
>>> td.abs_() is td
True
>>> td["a"]
tensor([1., 2.])

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.