unnjena
궁금한이야기J
unnjena
전체 방문자
오늘
어제
  • 분류 전체보기 (28)
    • Graduate M.S. (0)
      • Paper Review (0)
      • Project Related (0)
    • Undergraduate (14)
      • ML & DL (9)
      • Otherwise (1)
      • Programming (4)
    • etc. (9)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 세바시
  • Annotation #tool
  • Jump_into_the_AI_World
  • 컴퓨터네트워크 #인터넷
  • #computer_network #컴퓨터네트워크
  • 인공지능입문
  • network #edge #컴퓨터네트워크
  • network #delay #loss #throughput #컴퓨터네트워크
  • 서비스기획
  • 카공
  • TEST
  • PM
  • 취준생
  • 취준일기
  • Coding
  • 컴퓨터네트워크 #Traceroute
  • AI_Production_Lifecycle
  • 업스테이지
  • Deepfake
  • 지나영교수님
  • Python
  • 인생질문
  • AI교육
  • Upstage

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
unnjena

궁금한이야기J

[PyTorch tutorial] PyTorch에서 GPU 활용하기
Undergraduate/ML & DL

[PyTorch tutorial] PyTorch에서 GPU 활용하기

2020. 5. 12. 15:16
  • PyTorch에서 GPU를 활용하는 법은 간단하다. "모델을 GPU에 넣어주면 됨"
device = torch.device("cuda:0")
model.to(device)
  • 모든 텐서를 GPU에 넣어줌(input, lable 등)
mytensor = my_tensor.to(device)

GPU 활용 예시

  • 데이터 로드
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader

# Parameters and DataLoaders
input_size = 5
output_size = 2

batch_size = 30
data_size = 100
  • GPU 설정
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  • 랜덤 데이터 클래스 설정
class RandomDataset(Dataset):

    def __init__(self, size, length):
        self.len = length
        self.data = torch.randn(length, size)

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return self.len

rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),
                         batch_size=batch_size, shuffle=True)
  • 모델 클래스 설정
class Model(nn.Module):
    # Our model

    def __init__(self, input_size, output_size):
        super(Model, self).__init__()
        self.fc = nn.Linear(input_size, output_size)

    def forward(self, input):
        output = self.fc(input)
        print("\tIn Model: input size", input.size(),
              "output size", output.size())

        return output

여러 GPU 사용하기

model = Model(input_size, output_size)
if torch.cuda.device_count() > 1:
  print("Let's use", torch.cuda.device_count(), "GPUs!")
  # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
  model = nn.DataParallel(model)

model.to(device)
  • Let's use 3 GPUs!

for data in rand_loader:
    input = data.to(device)
    output = model(input)
    print("Outside: input size", input.size(),
          "output_size", output.size())

'Undergraduate > ML & DL' 카테고리의 다른 글

[Face Recognition] 얼굴 인식 출입, 어떻게 하는걸까?  (0) 2020.05.20
[PyTorch tutorial] 컴퓨터 비전(Vision)을 위한 전이학습(Transfer Learning)  (0) 2020.05.14
[GPU] 다수의 GPU 중 원하는 GPU 타겟팅하기  (0) 2020.05.13
[PyTorch tutorial] 파이토치로 딥러닝하기 : 60분만에 끝장내기  (0) 2020.05.12
[PyTorch tutorial] 파이토치 설치하기  (0) 2020.05.12
    'Undergraduate/ML & DL' 카테고리의 다른 글
    • [PyTorch tutorial] 컴퓨터 비전(Vision)을 위한 전이학습(Transfer Learning)
    • [GPU] 다수의 GPU 중 원하는 GPU 타겟팅하기
    • [PyTorch tutorial] 파이토치로 딥러닝하기 : 60분만에 끝장내기
    • [PyTorch tutorial] 파이토치 설치하기
    unnjena
    unnjena
    모든 궁금증과 그 해답을 담는 공간 Github : https://github.com/JaeheeRyu

    티스토리툴바