pytorch 배경

  • 페이스북에서 루아 언어라는 언어로 개발한 torch 프레임 워크를 python으로 다시 개발해서 pytorch로 개발
  • 초기에는 numpy와 같이 데이터 분석 프레임워크와 같은 느낌으로 기능 제공
  • 추후 GPU를 사용해서 딥러닝 병렬처리가 가능하게끔 발전

pytorch 구조

  • low-level : 하위 부분은 하드웨어 입장에서 GPU 접근 등 처리 계산을 빠르게 하기 위한 구조로 개발돼 있음, C, CUDA와 같은 저수준 언어 사용
  • middle-level : C++ 활용하여 중간 레벨의 언어로 개발. 엔진 역할을 담당
  • top-level : Python 모듈 구조로 wrapping돼 있는 API 제공

pytorch 구성 요소

  • torch : 텐서와 같은 수학 함수 기능 포함
  • torch.autograd : 자동 미분 기능 제공하는 라이브러리
  • torch.nn : 딥러닝 Neural Network 신경망 구성을 위한 라이브러리
  • torch.multiprosessing : 병렬처리 계산 기능 제공
  • torch.optim : 신경망 파라미터 최적화 알고리즘 제공
  • torch.utils : 데이터 조작 기능 제공
  • torch.onnx : ONNX(Open Neural Network Exchange) -> pytorch로 구현한 딥러닝 모델을 다른 프레임워크 형태의 모델로 공유할 수 있도록 도와주는 라이브러리

Tensor

  • 데이터 표현하는 하나의 구조로서 다차원 데이터를 효율적으로 표현 가능
  • 수치형 데이터를 담는 컨테이너 구조로 이뤄져 있음
  • GPU 사용해서 tensor 계사할 때 연산 가속 기능 제공
  • scalar : 0D, 차원이 없는
  • vector : 1D, 1차원
  • matrix : 2D, 2차원
  • 그 외 다차원 tensor 확장 가능

Tensor 초기화

# pytorch import & 버전 확인
import torch
torch.__version__
# 초기화 되지 않은 tensor 생성 (3,3) 2차원 matrix
non_init_tensor = torch.empty(3,3)
print(non_init_tensor)
"""
tensor([[1.0286e-38, 1.0653e-38, 1.0194e-38],
        [8.4490e-39, 1.0469e-38, 9.3674e-39],
        [9.9184e-39, 8.7245e-39, 9.2755e-39]])
"""
# random(무작위)으로 초기화된 tensor
init_random_torch=torch.rand(4,2)

"""
tensor([[0.8102, 0.2902],
        [0.7740, 0.6910],
        [0.5866, 0.2347],
        [0.6740, 0.8726]])
"""


# 데이터 타입이 long type이면서 0으로 채워진 텐서
x = torch.zeros(4,2, dtype=torch.long)
"""
tensor([[0, 0],
        [0, 0],
        [0, 0],
        [0, 0]])
"""


# 사용자 정의 입력값 tensor 초기화
x = torch.tensor([3, 2.3])
"""
tensor([7.0000, 4.1000])
"""


# 4 x 2 크기의 double 타입으로 1이 채워진 tensor
# 기존 만들어진 tensor를 변경
x = x.new_ones(4, 2, dtype=torch.double)
"""
tensor([[1., 1.],
        [1., 1.],
        [1., 1.],
        [1., 1.]], dtype=torch.float64)
"""


# 이전 tensor의 크기로, 무작위 tensor 생성, 데이터 타입은 float
new_x = torch.randn_like(x, dtype=torch.float)
"""
tensor([[-2.3465, -2.2080],
        [-0.2785,  0.6612],
        [-1.4324,  0.8104],
        [ 1.1557, -1.0430]])
"""


# 텐서 사이즈 확인
x.size()
"""
torch.Size([4, 2])
"""

데이터 타입 확인

  • 다양한 tensor 데이터 타입 제공
# floattensor로 데이터타입 지정
ft = torch.FloatTensor([3,6,2])
print(ft)
print(ft.dtype)
"""
tensor([3., 6., 2.])
torch.float32  # ==> 기본이 32bit
"""


# tensor 데이터 타입 casting
print(ft.short())
print(ft.int())
print(ft.long())
"""
tensor([3, 6, 2], dtype=torch.int16)      # ==> int 16bit
tensor([3, 6, 2], dtype=torch.int32)    # ==> int의 default도 32bit
tensor([3, 6, 2])                        # ==> 64 bit
"""


# IntTensor 확인
it = torch.IntTensor([4,6,9])
print(it)
print(it.dtype)
"""
tensor([4, 6, 9], dtype=torch.int32)
torch.int32
"""


# IntTensor를 Type Casting
print(it.float())
print(it.double())
print(it.half())
"""
tensor([4., 6., 9.])
tensor([4., 6., 9.], dtype=torch.float64)
tensor([4., 6., 9.], dtype=torch.float16)
"""


# 실제 데이터 값 확인
x = torch.randn(1)
print(x.item())
print(x.dtype)
"""
-0.4453190863132477
torch.float32
"""


# tensor 연산 device setting

# device setting
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)

# 위에서 만든 x와 같은 size로 1 값이 tensor 생성하고, device 부여
y = torch.ones_like(x, device=device)
print(y)

# x 또한 device 부여 (to 메서드 사용)
x = x.to(device)
print(x)

# 연산
z = x + y
print(z)

# 위 연산된 결과 값을 cpu로 이동, dtype은 double로
z.to('cpu', dtype=torch.double)
print(z)

+ Recent posts