expand

dragon.vm.torch.expand(
  input,
  sizes
)[source]

Broadcast input according to given sizes.

The length of sizes could either be less or more than the number of input dimensions:

a = torch.tensor([[1], [2], [3]])
# Shape: (3, 1) -> (3, 2)
print(torch.expand(a, sizes=(3, 2)))
print(torch.expand(a, sizes=(2,)))     # Equivalent
print(torch.expand(a, sizes=(-1, 2)))  # Equivalent

# Shape: (3,) -> (1, 3) -> (2, 3)
b = torch.tensor([1, 2, 3])
print(torch.expand(b, sizes=(2, 3)))

# Wrong remapping shape: (3,) -> (6,)
# Only the dimension with size 1 could broadcast
print(torch.expand(b, sizes=(6,)))
Parameters:
  • input (dragon.vm.torch.Tensor) – The input tensor.
  • sizes (Sequence[int]) – The output dimensions to broadcast to.
Returns:

dragon.vm.torch.Tensor – The output tensor.