expand_to_rank#
- sionna.phy.utils.expand_to_rank(tensor: torch.Tensor, target_rank: int, axis: int = -1) torch.Tensor[source]#
Inserts as many axes to a tensor as needed to achieve a desired rank.
This operation inserts additional dimensions to a
tensorstarting ataxis, so that the rank of the resulting tensor has ranktarget_rank. The dimension index follows Python indexing rules, i.e., zero-based, where a negative index is counted backward from the end.- Parameters:
tensor (torch.Tensor) – Input tensor
target_rank (int) – Rank of the output tensor. If
target_rankis smaller than the rank oftensor, the function does nothing.axis (int) – Dimension index at which to expand the shape of
tensor. Given atensorof D dimensions,axismust be within the range [-(D+1), D] (inclusive).
- Outputs:
tensor – A tensor with the same data as
tensor, withtarget_rank- rank(tensor) additional dimensions inserted at the index specified byaxis. Iftarget_rank<= rank(tensor),tensoris returned.
Examples
import torch from sionna.phy.utils import expand_to_rank x = torch.ones([3, 4]) print(x.shape) # torch.Size([3, 4]) y = expand_to_rank(x, 4, axis=-1) print(y.shape) # torch.Size([3, 4, 1, 1])