AI/PyTorch 13

[PyTorch] torch.reshape과 torch.Tensor.view의 차이

PyTorch에서 tensor의 차원을 재구성하는 함수인 reshape 함수와 view 함수의 각각의 특징과 차이점에 대해 알아보자. torch.reshape contiguous 속성을 만족하는 tensor가 인자로 주어진 경우, 데이터를 복사하지 않고, 같은 메모리 공간을 공유하는 새로운 tensor를 반환한다. 반면, contiguous 속성을 만족하지 않는 tensor가 인자로 주어진 경우, 데이터를 복사해 새로운 tensor를 생성해 반환한다. contiguous란 무엇일까? contiguous는 ‘인접한’이라는 뜻을 가진 단어로, tensor의 데이터들이 인덱스 순서대로 메모리가 인접해 있는지를 나타내는 속성이다. tensor1 = torch.randn(2, 3) tensor2 = tensor1..

AI/PyTorch 2023.03.14

[PyTorch] torch.mm과 torch.matmul의 차이

PyTorch에서 tensor의 곱을 연산하는 torch.mm과 torch.matmul의 각각의 특징과 차이점을 알아보자. torch.mm 2D tensor와 2D tensor의 행렬 곱셈을 수행한다. broadcast를 지원하지 않는다. torch.mm(input, mat2, *, out=None) → Tensor input의 크기가 (n x m)인 경우, mat2의 크기는 (m x p)여야 하고, output의 크기는 (n x p)가 된다. torch.matmul tensor와 tensor의 행렬 곱셈을 수행한다. broadcasting을 지원한다. 따라서 의도치 않은 결과가 나올 수 있다는 점에 주의해야 한다. vector와 vector의 연산 tensor1 = torch.tensor([1,2,3]..

AI/PyTorch 2023.03.14

[PyTorch] PyTorch 기초

오픈소스 머신 러닝 프레임워크인 PyTorch에 대해 알아보자. Tensor PyTorch에서 사용하는 자료구조이다. Numpy의 ndarray와 유사한 개념으로 사용된다. numpy import numpy as np n_array = np.arange(10).reshape(2, 5) print(n_array) # 출력: # [[0 1 2 3 4] # [5 6 7 8 9]] print(f"ndim : {n_array.ndim}, shape : {n_array.shape}") # 출력: ndim : 2, shape : (2, 5) pytorch import torch t_array = torch.FloatTensor(np.arange(10).reshape(2, 5)) print(t_array) # 출력: ..

AI/PyTorch 2023.03.13