unsqueeze

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

Expand the dimensions of input with size 1.

The argument dim could be negative or None:

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

# ``dim`` determines 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 ``dim`` is the last-k axis
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, ``dim`` 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.