import numpy as np
import torch
t = np.zeros((4,4,3)) # 0으로 채워진 4x4x3 numpy array 생성
ft = torch.FloatTensor(t) # 텐서로 변환
print(ft.shape) # output : torch.Size([4, 4, 3])

# ft라는 텐서를 (?, 3)의 크기로 변경 => [16 , 3]
print(ft.view([-1, 3]).shape)
# ft라는 텐서를 (?, 2, 3)의 크기로 변경 => [8, 2, 3]
print(ft.view([-1, 2, 3]).shape)
r = np.zeros((5, 5, 10))
fr = torch.FloatTensor(r)
print(fr.shape)
print(fr.reshape(10, 5, 5).shape) #torch.Size([10, 5, 5])
print(fr.reshape(1, -1).shape) #torch.Size([1, 250])
위와 같은 2가지 예시에서는 결과가 동일함.
그렇다면 차이는?
차원 변환을 적용하려는 텐서의 상태에 대하여 정확하게 파악하기가 모호할 경우에는 view 대신 reshape을 사용할 것을 권장
a.is_contiguous() # False
# 텐서를 contiguous = True 상태로 변경
a = a.contiguous()
a.is_contiguous() # True