전체 글(18)
-
PyTorch 기본 함수 정리(In-place Operation)
x = torch.FloatTensor([[1,2], [3,4]]) print(x) # tensor([[1., 2.], # [3., 4.]]) # inplace 안했을 때 print(x.mul(2.)) print(x) #tensor([[2., 4.], # [6., 8.]]) #tensor([[1., 2.], # [3., 4.]]) # inplace 했을 때 # PyTorch는 garbage collector가 잘 구현되어 있어서 in-place 사용 이점이 없을 수 있음 print(x.mul_(2.)) print(x) #tensor([[2., 4.], # [6., 8.]]) #tensor([[2., 4.], # [6., 8.]])
2022.06.18 -
PyTorch 기본 함수 정리 (Ones, Zeros)
x = torch.FloatTensor([[0, 1, 2], [2, 1, 0]]) print(x) # tensor([[0., 1., 2.], # [2., 1., 0.]]) print(torch.ones_like(x)) # device도 동일하게 간다. 같은 device에 텐서 선언 print(torch.zeros_like(x)) # tensor([[1., 1., 1.], # [1., 1., 1.]]) # tensor([[0., 0., 0.], # [0., 0., 0.]])
2022.06.18 -
PyTorch 기본 함수 정리 (Concatenate, Stack)
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)) # tensor([[1., 2.], # [3., 4.], # [5., 6.], # [7., 8.]]) # tensor([[1., 2., 5., 6.], # [3., 4., 7., 8.]]) Stacking x = torch.FloatTensor([1, 4]) y = torch.FloatTensor([2, 5]) z = torch.FloatTensor([3, 6]) print(x.shape) # torch.Size([2]) # 3개가 쌓임 #..
2022.06.18 -
PyTorch 기본 함수 정리 (Type Casting)
lt = torch.LongTensor([1, 2, 3, 4]) print(lt) # tensor([1, 2, 3, 4]) print(lt.float()) # tensor([1., 2., 3., 4.]) # ByteTensor: Boolean을 저장 bt = torch.ByteTensor([True,False,False,True]) print(bt) print(bt.long()) print(bt.float()) # tensor([1, 0, 0, 1], dtype=torch.uint8) # tensor([1, 0, 0, 1]) # tensor([1., 0., 0., 1.]) lt == 3 # tensor([False, False, True, False])
2022.06.18 -
PyTorch 기본 함수 정리 (View, Squeeze, Unsqueeze)
View (Numpy의 Reshape) t = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]) t array([[[ 0, 1, 2], [ 3, 4, 5]], [[ 6, 7, 8], [ 9, 10, 11]]]) t.shape (2, 2, 3) ft = torch.FloatTensor(t) ft tensor([[[ 0., 1., 2.], [ 3., 4., 5.]], [[ 6., 7., 8.], [ 9., 10., 11.]]]) ft.shape torch.Size([2, 2, 3]) print(ft.view(-1,3).shape) ft.view(-1,3) torch.Size([4, 3]) tensor([[ 0., 1., 2.], [ 3., 4., 5..
2022.06.18 -
PyTorch 기본 함수 정리 (mean, max)
t = torch.FloatTensor([[1,2],[3,4]]) ## 평균 print(t.mean()) print(t.mean(dim=0)) print(t.mean(dim=1)) print(t.mean(dim=-1)) ## 결과 # tensor(2.5000) # tensor([2., 3.]) # tensor([1.5000, 3.5000]) # tensor([1.5000, 3.5000]) ## max 함수 t = torch.FloatTensor([[1,2],[3,4]]) # base print(t.max()) #tensor(4.) # dim = 0 print("\ndim=0") print(t.max(dim=0)) print("value:",t.max(dim=0)[0]) print("argmax:",t.m..
2022.06.18