PyTorch 설치 및 Jupyter 등록
환경은 Windows 10, Anaconda를 사용하고 있습니다.
conda create -y -n pytorch ipykernel
activate pytorch
PyTorch 링크를 보고 자신한테 맞는 환경을 골라 명령어를 입력한다.
conda install pytorch cuda90 -c pytorch
pip install torchvision
설치가 완료되면 예제 코드를 다운받아 실행 시킨다.
python tensor_tutorial.py
제대로 실행되면 pytorch 설치가 완료된 것이다.
주피터에 등록을 하기 위해선 다음 명령을 입력한다.
python -m ipykernel install --user --name pytorch --display-name "PyTorch"
tensor_tutorial.py
"""
What is PyTorch?
================
It’s a Python-based scientific computing package targeted at two sets of
audiences:
- A replacement for NumPy to use the power of GPUs
- a deep learning research platform that provides maximum flexibility
and speed
Getting Started
---------------
Tensors
^^^^^^^
Tensors are similar to NumPy’s ndarrays, with the addition being that
Tensors can also be used on a GPU to accelerate computing.
"""
from __future__ import print_function
import torch
x = torch.empty(5, 3)
print(x)
x = torch.rand(5, 3)
print(x)
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
x = torch.tensor([5.5, 3])
print(x)
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
x = torch.randn_like(x, dtype=torch.float)
print(x)
print(x.size())
y = torch.rand(5, 3)
print(x + y)
print(torch.add(x, y))
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
y.add_(x)
print(y)
print(x[:, 1])
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
x = torch.randn(1)
print(x)
print(x.item())
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)
a.add_(1)
print(a)
print(b)
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
if torch.cuda.is_available():
device = torch.device("cuda")
y = torch.ones_like(x, device=device)
x = x.to(device)
z = x + y
print(z)
print(z.to("cpu", torch.double))