티스토리 뷰

Record/AI

[인공지능] Pytorch Install

Mizzle 2022. 5. 9. 13:42

Python Pytorch Install with GPU

 

python : 3.7.5

  • 해당 기준은 python을 기준으로 설치하는 버전입니다.
  • conda를 사용하지 않았으며, 가상환경 생성을 통해 해당 python package를 설치합니다.

1. GPI : https://en.wikipedia.org/wiki/CUDA#Version_features_and_specifications

 

CUDA - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search Parallel computing platform and programming model CUDA (or Compute Unified Device Architecture) is a parallel computing platform and application programming interface (API) that allows

en.wikipedia.org

  • 자기 버전에 맞는 gpu 확인

2. CUDA : https://developer.nvidia.com/cuda-toolkit-archive

 

CUDA Toolkit Archive

Previous releases of the CUDA Toolkit, GPU Computing SDK, documentation and developer drivers can be found using the links below. Please select the release you want from the list below, and be sure to check www.nvidia.com/drivers for more recent production

developer.nvidia.com

  • gpu버전에 해당하는 cuda 설치
  • nvidia-smi : CUDA Version 확인

3. Pytorch : https://pytorch.org/get-started/locally/

 

PyTorch

An open source machine learning framework that accelerates the path from research prototyping to production deployment.

pytorch.org

  • build 확인, os 확인, package 확인, language 확인

4. 각자 환경에 맞는 버전을 설치

  • 작성자 환경 : stable(1.8.1) > Windows > Pip > Python > CUDA 11.1
pip3 install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio===0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

 

5. 가상환경(venv) 생성

  • 프로잭트 생성 위치 or 가상환경 관리 위치
python -m venv venv
venv\Scripts\activate
python -m pip install --upgrade pip

 

6. 4.의 shell 문구 내용을 콘솔에 붙여 넣기

  • 완료될때 까지 시간 소요

 

7. Pytorch 동작 코드 예제 : https://tutorials.pytorch.kr/beginner/pytorch_with_examples.html

 

예제로 배우는 파이토치(PyTorch)

Author: Justin Johnson 번역: 박정환 이 튜토리얼에서는 PyTorch 의 핵심적인 개념을 예제를 통해 소개합니다. 본질적으로, PyTorch에는 두가지 주요한 특징이 있습니다: NumPy와 유사하지만 GPU 상에서 실행

tutorials.pytorch.kr

 

import torch

dtype = torch.float
#device = torch.device("cpu") # cpu
device = torch.device("cuda:0") # gpu

# N은 배치 크기이며, D_in은 입력의 차원입니다;
# H는 은닉층의 차원이며, D_out은 출력 차원입니다.
N, D_in, H, D_out = 64, 1000, 100, 10

# 무작위의 입력과 출력 데이터를 생성합니다.
x = torch.randn(N, D_in, device=device, dtype=dtype)
y = torch.randn(N, D_out, device=device, dtype=dtype)

# 무작위로 가중치를 초기화합니다.
w1 = torch.randn(D_in, H, device=device, dtype=dtype)
w2 = torch.randn(H, D_out, device=device, dtype=dtype)

learning_rate = 1e-6
for t in range(500):
    # 순전파 단계: 예측값 y를 계산합니다.
    h = x.mm(w1)
    h_relu = h.clamp(min=0)
    y_pred = h_relu.mm(w2)

    # 손실(loss)을 계산하고 출력합니다.
    loss = (y_pred - y).pow(2).sum().item()
    if t % 100 == 99:
        print(t, loss)

    # 손실에 따른 w1, w2의 변화도를 계산하고 역전파합니다.
    grad_y_pred = 2.0 * (y_pred - y)
    grad_w2 = h_relu.t().mm(grad_y_pred)
    grad_h_relu = grad_y_pred.mm(w2.t())
    grad_h = grad_h_relu.clone()
    grad_h[h < 0] = 0
    grad_w1 = x.t().mm(grad_h)

    # 경사하강법(gradient descent)를 사용하여 가중치를 갱신합니다.
    w1 -= learning_rate * grad_w1
    w2 -= learning_rate * grad_w2

 

8. 실행 결과 확인

99 177.5563507080078
199 0.22159364819526672
299 0.0007461378700099885
399 4.888954572379589e-05
499 1.6039884940255433e-05

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함