I have a tensor with this size
torch.Size([128, 64])
how do I add one "dummy" dimension such as
torch.Size([1, 128, 64])
There are several ways:
torch.unsqueeze(x, 0)
Using None (or np.newaxis):
x[None, ...]
# or
x[np.newaxis, ...]
x.reshape(1, *x.shape)
# or
x.view(1, *x.shape)
x[None] ;)x[None] or x[None,...]! Use x[np.newaxis] or x[np.newaxis, ...] ("Explicit is better than implicit.")np.newaxis. Nevertheless, I like it better to use just None. Moreover, in some cases, I do not need/want to explicitly import numpy in my code.
torch.unsqueezetorch.unsqueeze(0)along the first dimension.