1# ADD ONE DIMENSION: .unsqueeze(dim)
2
3my_tensor = torch.tensor([1,3,4])
4# tensor([1,3,4])
5
6my_tensor.unsqueeze(0)
7# tensor([[1,3,4]])
8
9my_tensor.unsqueeze(1)
10# tensor([[1],
11# [3],
12# [4]])
13
1x = torch.zeros(2, 1, 2, 1, 2)
2x.size()
3>>> torch.Size([2, 1, 2, 1, 2])
4
5y = torch.squeeze(x) # remove 1
6y.size()
7>>> torch.Size([2, 2, 2])
8
9y = torch.squeeze(x, 0)
10y.size()
11>>> torch.Size([2, 1, 2, 1, 2])
12
13y = torch.squeeze(x, 1)
14y.size()
15>>> torch.Size([2, 2, 1, 2])
16
1ft = torch.Tensor([0, 1, 2])
2print(ft.shape)
3>>> torch.Size([3])
4
5print(ft.unsqueeze(0)) # 0 means first dimension
6print(ft.unsqueeze(0).shape)
7>>> tensor([[0., 1., 2.]])
8>>> torch.Size([1, 3])