[Pytorch] Permute vs View
tensor.view() : tensor 형태를 원하는 모양으로 바꿔준다. numpy reshpae와 동일
tensor.permute() : tensor 차원 순서를 원하는 순서로 변경해준다. numpy transpose와 동일
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import torch
import numpy as np
ten = torch.Tensor([[1, 2], [3, 4], [5, 6]])
num = np.array([[1, 2], [3, 4], [5, 6]])
print('-'*30)
print('Origin')
print('-'*30)
print('torch.tensor : \n')
print(ten)
print(ten.shape)
print()
print('numpy array : \n')
print(num)
print(num.shape)
print()
print('-'*30)
print('Permute')
print('-'*30)
print('torch.tensor.permute : \n')
pten = ten.permute(1, 0)
print(pten)
print(pten.shape)
print()
print('np.array.transpose : \n')
pnum = num.transpose(1, 0)
print(pnum)
print(pnum.shape)
print()
print('-'*30)
print('View')
print('-'*30)
print('torch.tensor.view : \n')
vten = ten.view(-1, 1)
print(vten)
print(vten.shape)
print()
print('np.array.reshape : \n')
vnum = num.reshape(-1, 1)
print(vnum)
print(vnum.shape)
print()
Results : results
This post is licensed under CC BY 4.0 by the author.