본문 바로가기

STUDY/인공지능

까먹지 않기 위한 파이토치 공부 내용 정리3

728x90

* View(Reshape)

원하는 shape으로 바꿀 수 있음

 

'''View(Reshape)'''

t = np.array([[[0, 1, 2],
			   [3, 4, 5]],
               
              [[6, 7, 8],
               [9, 10, 11]]])
ft = torch.FloatTensor(t)
print(ft.shape)
# torch.Size([2, 2, 3])

print(ft.view([-1, 3])) # -1 부분은 건너뛰고, 두 번째 차원이 3이 되게 만들어라
print(ft.view([-1, 3]).shape)
# tensor([[0., 1., 2.],
#		  [3., 4., 5.],
#		  [6., 7., 8.],
#		  [9., 10., 11.]])
# torch.Size([4, 3])

print(ft.view([-1, 1, 3])) # 첫 번째 차원은 건너뛰고, 두 번째 차원은 1, 세 번째 차원은 3이 되도록 만들어라.
print(ft.view([-1, 1, 3]).shape)
# tensor([[[0., 1., 2.]],
#		  [[3., 4., 5.]],
#		  [[6., 7., 8.]],
#		  [[9., 10., 11.]]])
# torch.Size([4, 1, 3])

 

* Squeeze

차원 중 1인 것은 제거

ft = torch.FloatTensor([[0], [1], [2]])
print(ft)
print(ft.shape)
# tensor([0.],
#		 [1.],
#		 [2.]])
# torch.Size([3, 1])

print(ft.squeeze()) # 1인 부분은 제거
print(ft.squeeze().shape)
# tensor([0., 1., 2.])
# torch.Size([3])

 

 

* Unsqueeze

원하는 차원에 1추가

ft = torch.Tensor([0, 1, 2])
print(ft.shape)
# torch.Size([3])

print(ft.unsqueeze(0))
print(ft.unsqueeze(0).shape)
# tensor([[0., 1., 2.]])
# torch.Size([1, 3])

print(ft.view(1, -1))
print(ft.view(1, -1).shape)
# tensor([[0., 1., 2.]])
# torch.Size([1, 3])

print(ft.unsqueeze(1))
print(ft.unsqueeze(1).shape)
# tensor([[0.], 
#		  [1.], 
#		  [2.]])
# torch.Size([3, 1])

print(ft.unsqueeze(-1))
print(ft.unsqueeze(-1).shape)
# tensor([[0.], 
#		  [1.], 
#		  [2.]])
# torch.Size([3, 1])

 

* Type Casting

lt = torch.LoneTensor([1, 2, 3, 4])
print(lt)
# tensor([1, 2, 3, 4])

print(lt.float())
# tensor([1., 2., 3., 4.])

bt = torch.ByteTensor([True, False, False, True])
print(bt)
# tensor([1, 0, 0, 1], dtype=torch.uint8)

print(bt.lone())
print(bt.float())
# tensor([1, 0, 0, 1])
# tensor([1., 0., 0., 1.])

 

* Concatenate

x = torch.FloatTensor([[1, 2], [3, 4]])
y = torch.FloatTensor([[5, 6], [7, 8]])
print(torch.cat([x, y], dim=0)) # 행끼리
print(torch.cat([x, y], dim=1)) # 열끼리
# x = 1 2
#	  3 4
# y = 5 6
# 	  7 8
# tensor([[1., 2.],
#		  [3., 4.],
#		  [5., 6.],
#		  [7., 8.]])
# tensor([[1., 2., 5., 6.],
#		  [3., 4., 7., 8.]])

 

* Satacking

x = torch.FloatTensor([1, 4])
y = torch.FloatTensor([2, 5])
z = torch.FloatTensor([3, 6])
print(torch.stack([x, y, z])) # 행 끼리
print(torch.stack([x, y, z], dim=1)) # 열 끼리
# tensor([[1., 4.],
#		  [2., 5.],
#		  [3., 6.]])
# tensor([[1., 2., 3.],
# 		  [4., 5., 6.]])

print(torch.cat([x.unsqueeze(0), y.unsqueeze(0), z.unsqueeze(0)], dim=0))
# tensor([[1., 4.],
#		  [2., 5.],
#		  [3., 6.]])

 

* Ones and Zeros

x = torch.FloatTensor([[0, 1, 2], [2, 1, 0]])
print(x)
# tensor([[0., 1., 2.],
#		  [2., 1., 0.]])

print(torch.ones_like(x))
print(torch.zeros_like(x))
# tensor([[1., 1., 1.],
#		  [1., 1., 1.]])
# tensor([[0., 0., 0.],
#		  [0., 0., 0.]])

 

* In-place Operation

x = torch.FloatTensor([[1, 2], [3, 4]])
print(x.mul(2.))
print(x)
print(x.mul_(2.))
print(x)
# tensor([[2., 4.],
#		  [6., 8.]])
# tensor([[1., 2.],
#		  [3., 4.]])
# tensor([[2., 4.],
#		  [6., 8.]])
# tensor([[2., 4.],
#		  [6., 8.]])

이 글은 공부 내용을 정리한 글로, 자세한 내용을 공부하고 싶으신 분들은 '부스트코스-파이토치로 시작하는 딥러닝 기초'를 보시기 바랍니다.

728x90