expand_as_right#
- tensordict.utils.expand_as_right(tensor: Tensor | TensorDictBase, dest: Tensor | TensorDictBase) Tensor | TensorDictBase#
Expands a tensor on the right to the shape of
dest.Broadcasting adds dimensions on the left; this function adds them on the right instead, then expands every dimension of size one. The leading dimensions of
tensormust match those ofdestor be of size one. The result is a view oftensor, as withtorch.Tensor.expand().- Parameters:
tensor (Tensor or TensorDictBase) – the tensor to expand.
dest (Tensor or TensorDictBase) – the tensor whose shape the result takes. For a tensordict, the shape is its batch size.
- Returns:
A view of
tensorwith the shape ofdest.- Raises:
RuntimeError – if
desthas fewer dimensions thantensor, or if their leading dimensions do not match.
Examples
>>> import torch >>> from tensordict.utils import expand_as_right >>> x = torch.arange(3) >>> expand_as_right(x, torch.zeros(3, 2)) tensor([[0, 0], [1, 1], [2, 2]])