squeeze(1) pytorch code example

Example 1: pytorch squeeze

x = torch.zeros(2, 1, 2, 1, 2)
x.size()
>>> torch.Size([2, 1, 2, 1, 2])

y = torch.squeeze(x) # remove 1
y.size()
>>> torch.Size([2, 2, 2])

y = torch.squeeze(x, 0)
y.size()
>>> torch.Size([2, 1, 2, 1, 2])

y = torch.squeeze(x, 1)
y.size()
>>> torch.Size([2, 2, 1, 2])

Example 2: what is squeeze function in pytorch?

import torch
t = torch.tensor([[1,17,7,
                     3,9,10]])
print(t)
>>>tensor([[1,17,7,3,9,10]])
print(torch.squeeze(t))
>>>tensor([ 1, 17,  7,  3,  9, 10])

t = torch.tensor([[[1,17,7,
                     3,9,10]]])
print(t)
>>>tensor([[[1,17,7,3,9,10]]])
print(torch.squeeze(t))
>>>tensor([ 1, 17,  7,  3,  9, 10])