0% found this document useful (0 votes)
185 views19 pages

Py Torch

This document provides an overview of PyTorch, explaining how to convert between PyTorch and NumPy tensors, use tensors on GPUs, define neural network models and modules, and implement custom autograd functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
185 views19 pages

Py Torch

This document provides an overview of PyTorch, explaining how to convert between PyTorch and NumPy tensors, use tensors on GPUs, define neural network models and modules, and implement custom autograd functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

Follow

1 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

import torch
import numpy as np

numpy_tensor = np.random.randn(10, 20)

# convert numpy array to pytorch array


pytorch_tensor = torch.Tensor(numpy_tensor)
# or another way
pytorch_tensor = torch.from_numpy(numpy_tensor)

# convert torch tensor to numpy representation


pytorch_tensor.numpy()

# if we want to use tensor on GPU provide another type


dtype = torch.cuda.FloatTensor
gpu_tensor = torch.randn(10, 20).type(dtype)
# or just call `cuda()` method
gpu_tensor = pytorch_tensor.cuda()
# call back to the CPU

2 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

.data .grad

import torch
from torch.autograd import Variable

# define an inputs
x_tensor = torch.randn(10, 20)
y_tensor = torch.randn(10, 5)
x = Variable(x_tensor, requires_grad=False)
y = Variable(y_tensor, requires_grad=False)
# define some weights
w = Variable(torch.randn(20, 5), requires_grad=True)

# get variable tensor


print(type(w.data)) # torch.FloatTensor
# get variable gradient
print(w.grad) # None

loss = torch.mean((y - x @ w) ** 2)

3 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

import torch
from torch.autograd import Variable
import torch.nn.functional as F

x = Variable(torch.randn(10, 20), requires_grad=False)


y = Variable(torch.randn(10, 3), requires_grad=False)
# define some weights
w1 = Variable(torch.randn(20, 5), requires_grad=True)
w2 = Variable(torch.randn(5, 3), requires_grad=True)

learning_rate = 0.1
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.SGD([w1, w2], lr=learning_rate)
for step in range(5):
pred = F.sigmoid(x @ w1)
pred = F.sigmoid(pred @ w2)

4 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

import tensorflow as tf

first_counter = tf.constant(0)
second_counter = tf.constant(10)
some_value = tf.Variable(15)

# condition should handle all args:


def cond(first_counter, second_counter, *args):
return first_counter < second_counter

def body(first_counter, second_counter, some_value):


first_counter = tf.add(first_counter, 2)
second_counter = tf.add(second_counter, 1)
return first_counter, second_counter, some_value

import torch

first_counter = torch.Tensor([0])
second_counter = torch.Tensor([10])
some_value = torch.Tensor(15)

while (first_counter < second_counter)[0]:

nn

nn

5 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

from collections import OrderedDict

import torch.nn as nn

# Example of using Sequential


model = nn.Sequential(
nn.Conv2d(1, 20, 5),
nn.ReLU(),
nn.Conv2d(20, 64, 5),
nn.ReLU()
)

# Example of using Sequential with OrderedDict


model = nn.Sequential(OrderedDict([

nn.Module

from torch import nn

class Model(nn.Module):
def __init__(self):
super().__init__()
self.feature_extractor = nn.Sequential(
nn.Conv2d(3, 12, kernel_size=3, padding=1, stride
nn.Conv2d(12, 24, kernel_size=3, padding=1, stride
)
self.second_extractor = nn.Conv2d(
24, 36, kernel_size=3, padding=1, stride=1)

__init__

6 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

forward

7 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

import torch

class MyFunction(torch.autograd.Function):

@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
output = torch.sign(input)
return output

@staticmethod
def backward(ctx, grad_output):
# saved tensors - tuple of tensors, so we need get first
input, = ctx.saved_variables
grad_output[input.ge(1)] = 0
grad_output[input.le(-1)] = 0
return grad_output

# usage

ctx

self.save_for_backward(input)

input, _ = self.saved_tensors

.cuda()

.cuda()

.cpu()

8 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

import torch

### tensor example


x_cpu = torch.randn(10, 20)
w_cpu = torch.randn(20, 10)
# direct transfer to the GPU
x_gpu = x_cpu.cuda()
w_gpu = w_cpu.cuda()
result_gpu = x_gpu @ w_gpu
# get back from GPU to CPU
result_cpu = result_gpu.cpu()

### model example

import torch

# check is cuda enabled


torch.cuda.is_available()

# set required device


torch.cuda.set_device(0)

# work with some required cuda device


with torch.cuda.device(1):
# allocates a tensor on GPU 1
a = torch.cuda.FloatTensor(1)

9 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

class Trainer:
def __init__(self, model, use_cuda=False, gpu_idx=0):
self.use_cuda = use_cuda
self.gpu_idx = gpu_idx
self.model = self.to_gpu(model)

def to_gpu(self, tensor):


if self.use_cuda:
return tensor.cuda(self.gpu_idx)
else:
return tensor

def from_gpu(self, tensor):


if self.use_cuda:
return tensor.cpu()

torch.nn.init

10 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

import torch
from torch.autograd import Variable

# new way with `init` module


w = torch.Tensor(3, 5)
torch.nn.init.normal(w)
# work for Variables also
w2 = Variable(w)
torch.nn.init.normal(w2)
# old styled direct access to tensors data attribute
w2.data.normal_()

# example for some module


def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)

# for loop approach with direct access

requires_grad volatile

11 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

import torch
from torch.autograd import Variable

# requires grad
# If theres a single input to an operation that requires gradient,
# its output will also require gradient.
x = Variable(torch.randn(5, 5))
y = Variable(torch.randn(5, 5))
z = Variable(torch.randn(5, 5), requires_grad=True)
a = x + y
a.requires_grad # False
b = a + z
b.requires_grad # True

# scheduler example
from torch.optim import lr_scheduler

optimizer = torch.optim.SGD(model.parameters(), lr=0.01)


scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma

for epoch in range(100):


scheduler.step()
train()
validate()

# Train flag can be updated with boolean


# to disable dropout and batch norm learning
model.train(True)
# execute train step

12 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

from collections import OrderedDict

import torch.nn as nn

model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1, 20, 5)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(20, 64, 5)),
('relu2', nn.ReLU())
]))

print(model)

# Sequential (
# (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
# (relu1): ReLU ()
# (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
# (relu2): ReLU ()

state_dict()

13 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

torch.utils.data.Dataset

14 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

import torch
import torchvision as tv

class ImagesDataset(torch.utils.data.Dataset):
def __init__(self, df, transform=None,
loader=tv.datasets.folder.default_loader):
self.df = df
self.transform = transform
self.loader = loader

def __getitem__(self, index):


row = self.df.iloc[index]

target = row['class_']
path = row['path']
img = self.loader(path)
if self.transform is not None:
img = self.transform(img)

return img, target

def __len__(self):
n, _ = self.df.shape
return n

# what transformations should be done with our images


data_transforms = tv.transforms.Compose([
tv.transforms.RandomCrop((64, 64), padding=4),
tv.transforms.RandomHorizontalFlip(),
tv.transforms.ToTensor(),
])

torchvision.transforms.ToTensor()

15 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

async=True

cuda()

pin_memory=True

16 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

class ImagesDataset(torch.utils.data.Dataset):
pass

class Net(nn.Module):
pass

model = Net()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma
criterion = torch.nn.MSELoss()

dataset = ImagesDataset(path_to_images)
data_loader = torch.utils.data.DataLoader(dataset, batch_size

train = True
for epoch in range(epochs):
if train:
lr_scheduler.step()

for inputs, labels in data_loader:


inputs = Variable(to_gpu(inputs))

17 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

18 of 19 10/6/2017, 10:01 PM
PyTorch tutorial distilled Towards Data Science Medium https://medium.com/towards-data-science/pytorch-tutorial-distilled-95ce...

19 of 19 10/6/2017, 10:01 PM

You might also like