unsqueeze

dragon.vm.torch.unsqueeze(
  input,
  dim,
  out=None
)[source]

Expand the dimensions of input with size 1.

dim could be negative:

x = torch.ones(2, 3, 4, 5)

# dimension is the size-1 position in output
print(torch.unsqueeze(x, dim=0).shape)  # (2, 3, 4, 5) -> (1, 2, 3, 4, 5)
print(torch.unsqueeze(x, dim=1).shape)  # (2, 3, 4, 5) -> (2, 1, 3, 4, 5)

# A negative dimension is the last-k dimension
print(torch.unsqueeze(x, dim=4).shape)   # (2, 3, 4, 5) -> (2, 3, 4, 5, 1)
print(torch.unsqueeze(x, dim=-1).shape)  # Equivalent

# Also, dimension could be a sequence of integers
print(torch.unsqueeze(x, dim=(-1, -3)).shape)  # (2, 3, 4, 5) -> (2, 3, 4, 1, 5, 1)
Parameters:
Returns:

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