expand_right#
- tensordict.utils.expand_right(tensor: Tensor, shape: Sequence[int]) Tensor#
Expands a tensor on the right to
shape.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 matchshapeor be of size one. The result is a view oftensor, as withtorch.Tensor.expand().- Parameters:
- Returns:
A view of
tensorwith shapeshape.
Examples
>>> import torch >>> from tensordict.utils import expand_right >>> x = torch.arange(3) >>> expand_right(x, (3, 2)) tensor([[0, 0], [1, 1], [2, 2]]) >>> expand_right(torch.zeros(3, 4), (3, 4, 5)).shape torch.Size([3, 4, 5])