view/reshape
a = torch.rand(4,1,28,28)
print(a.shape)
print(a.view(4, 28*28).shape)
print(a.reshape(4, 28*28).shape)
print(a.view(4, -1).shape)
print(a.reshape(4, -1).shape)
squeeze/unsqueeze
a = torch.rand(4,1,28,28)
print(a.shape)
print(a.unsqueeze(0).shape)
print(a.unsqueeze(4).shape)
print(a.unsqueeze(-1).shape)
print(a.squeeze().shape)
print(a.squeeze(1).shape)
transpose/permute
a = torch.rand(10,3,32,32)
print(a.shape)
print(a.transpose(1,3).shape)
print(a.permute(0,3,2,1).shape)
expand/repeat
b = torch.randint(1, 10, (1, 3))
print(b)
print(b.shape)
print(b.storage())
print(b.storage().data_ptr())
tensor([[7, 8, 9]])
torch.Size([1, 3])
7
8
9
[torch.LongStorage of size 3]
2530665948608
b_1 = b.expand(3, 3)
print(b_1)
print(b_1.shape)
print(b_1.storage())
print(b_1.storage().data_ptr())
tensor([[7, 8, 9],
[7, 8, 9],
[7, 8, 9]])
torch.Size([3, 3])
7
8
9
[torch.LongStorage of size 3]
2530665948608
b_2 = b.repeat(3, 1)
print(b_2)
print(b_2.shape)
print(b_2.storage())
print(b_2.storage().data_ptr())
tensor([[7, 8, 9],
[7, 8, 9],
[7, 8, 9]])
torch.Size([3, 3])
7
8
9
7
8
9
7
8
9
[torch.LongStorage of size 9]
2530678187136