CS236 Introduction To PyTorch
CS236 Introduction To PyTorch
Research prototyping
A machine learning framework that
accelerates the path from research Models are Python code, Automatic differentiation, and eager mode
Production deployment
Building Blocks
Tensors
Operations
Overview
Modules
Examples
MNIST
Beyond PyTorch
Tools
High Level Libraries
Domain Specific Libraries
Motivations
Python vs. NumPy
for i in range(10000):
Z[i] = X[i] * Y[i]
# 2.772092819213867 ms # 0.08273124694824219 ms
# Interpreter Overhead # Low Level Implementation
# 64 bit # Vectorization
Motivations
NumPy vs. PyTorch
# 0.3185272216796875 ms
# GPU Acceleration
Z.sum().backward()
# 0.08273124694824219 ms dX = X.grad
# Low Level Implementation
# Vectorization # Automatic Differentiation
Building Blocks
TENSORS
Building Blocks
Tensors / Initialization
torch.tensor([5., 3.])
tensor([ 5., 3.,]) # defaults to
torch.float32
torch.from_numpy(np.array([5., 3.]))
tensor([ 5., 3.,], dtype=torch.float64) #
because numpy defaults to 64bit
torch.tensor([5., 3.]).numpy()
array([5., 3.], dtype=float32)
Building Blocks
Tensors / Initialization
torch.ones(5, 3)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
Building Blocks
Tensors / Initialization
torch.randn(5, 3)
torch.ones_like(tensor)
Input: tensor([[ 0.2349, -0.0427, -0.5053],
[ 0.6455, 0.1199,
0.4239]])
Output: tensor([[1., 1., 1.],
[1., 1., 1.],
dtype=torch.float64)
Building Blocks
Tensors / Initialization
torch.empty(5, 3)
tensor([[ 0.0000e+00, 2.5244e-29, 0.0000e+00],
[ 2.5244e-29, 1.4569e-19, 2.7517e+12],
[ 7.5338e+28, 3.0313e+32, 6.3828e+28],
[ 1.4603e-19, 1.0899e+27, 6.8943e+34],
[ 1.1835e+22, 7.0976e+22, 1.8515e+28]])
torch.tensor([[5., 3.]])[0, :]
tensor([ 5., 3.,])
torch.tensor([[5., 3.]]).size()
torch.Size([1, 2])
Building Blocks
Tensors / Broadcasting
X = torch.ones((3, 3, 3))
Y = torch.ones((1, 1, 3))
Z = X * Y
Z.size()
torch.Size([3, 3, 3])
#
https://pytorch.org/docs/stable/notes/broad
casting.html
Building Blocks
Tensors / Devices
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA device object
x = torch.ones(2, device=device) # directly create a tensor on GPU
y = torch.ones(2).to(device) # or just use strings
`.to("cuda")`
z = x + y
print(z) # z is on GPU
print(z.to("cpu", torch.double)) # to(‘cpu’) moves array to CPU
z = torch.add(x, y)
torch.add(x, y, out=z)
y = y.add_(x) # inplace y += x
torch.tanh(y)
torch.stack([x, y])
# https://pytorch.org/docs/stable/torch.html
Building Blocks
Operations / Functional
import torch.nn.functional as F
# Like SciPy
# https://pytorch.org/docs/stable/nn.functional.html
Building Blocks
Operations / Automatic Differentiation
x = torch.ones(2, 2, requires_grad=True)
tensor([[1., 1.],
[1., 1.]], requires_grad=True)
∂+
y = x + 2
tensor([[3., 3.],
[3., 3.]], grad_fn=<AddBackward0>)
y
Building Blocks
Operations / Automatic Differentiation
x 2
z = y * 3
out = z.mean()
∂+
tensor(9., grad_fn=<MeanBackward1>)
y 3
x.requires_grad # True
(x ** 2).requires_grad # True
with torch.no_grad():
(x ** 2).requires_grad # False
Dataloader
Example Network
MNIST
Optimizer
Training
Examples
MNIST / Preprocessing
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
Import torch
import torchvision
trainset = torchvision.datasets.CIFAR10(
root='./data', train=True,
download=True, transform=transform)
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
Examples
MNIST / Network
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
...
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = torch.flatten(self.pool(F.relu(self.conv2(x))))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
return self.fc3(x)
Examples
MNIST / Optimizer