Rate this Page
★ ★ ★ ★ ★

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 tensor must match those of dest or be of size one. The result is a view of tensor, as with torch.Tensor.expand().

Parameters:
Returns:

A view of tensor with the shape of dest.

Raises:

RuntimeError – if dest has fewer dimensions than tensor, 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]])