Alex
Alex

Reputation: 1537

torch Tensor add dimension

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])

Upvotes: 1

Views: 2964

Answers (1)

Shai
Shai

Reputation: 114976

There are several ways:

torch.unsqueeze:

torch.unsqueeze(x, 0)

Using None (or np.newaxis):

x[None, ...]
# or
x[np.newaxis, ...]

reshape or view:

x.reshape(1, *x.shape)
# or
x.view(1, *x.shape)

Upvotes: 3

Related Questions