TensorDictBase.pad#
- TensorDictBase.pad(pad_size: Sequence[int], value: float = 0.0, inplace: bool = False, safe: bool = True) Self#
Returns the tensordict padded along its batch dimensions with a constant value.
- Parameters:
pad_size (sequence of int) – the padding, applied to the batch dimensions starting from the first. For a tensordict with
ndimbatch dimensions, it is a sequence of even length up to2 * ndim:(dim0_left, dim0_right, dim1_left, dim1_right, ...).value (float, optional) – the fill value. Defaults to
0.0.inplace (bool, optional) –
if
True, the tensordict keeps its identity and keys, its leaves are replaced one by one by their padded copies, and the call returnsself. Peak memory stays close to1xinstead of2x. See In-place operations. Defaults toFalse.Warning
If
inplace=Trueand the padding of a later leaf fails (for example, out of memory), the tensordict is left in an inconsistent state.safe=Truecatches the user errors before any leaf is changed.safe (bool, optional) – if
True, checks that the padding works for every leaf before any leaf is changed. PassFalseto skip this check when the inputs are known to be valid. Defaults toTrue.
- Returns:
The padded tensordict, or
selfwithinplace=True.
Note
On a lazy stack whose tensordicts have no names, the result of
inplace=Falsehas no names, even when the stack dimension has a name. Seepad().Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict({"a": torch.zeros(3, 4)}, batch_size=[3, 4]) >>> td.pad([0, 0, 0, 1]).batch_size torch.Size([3, 5]) >>> td.pad([0, 0, 0, 1], inplace=True) is td True >>> td.batch_size torch.Size([3, 5])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.