Pytorch 101: Deep Learning PHD Course 2017/2018
Pytorch 101: Deep Learning PHD Course 2017/2018
PyTorch 101
Deep Learning PhD Course
2017/2018
Marco Ciccone
Dipartimento di Informatica Elettronica e Bioingegneria
Politecnico di Milano
Deep Learning Phd Course
What is PyTorch?
It’s a Python based scientific computing package targeted at two sets of audiences:
import torch
x = torch.Tensor(5, 3)
print(x)
Deep Learning Phd Course
Multiple syntaxes
Syntax 1 Addition: providing an output tensor as argument
Syntax 2 In-place
print(torch.add(x, y)) # adds x to y
y.add_(x)
print(y)
NumPy Bridge
Converting a Torch Tensor to a NumPy array and vice versa is a breeze.
NOTE: The Torch Tensor and NumPy array will share their underlying memory locations,
and changing one will change the other.
Deep Learning Phd Course
CUDA Tensors
# let us run this cell only if CUDA is available
if torch.cuda.is_available():
x = x.cuda()
y = y.cuda()
x + y
Deep Learning Phd Course
Once you finish your computation you can call .backward() and have all the gradients computed
automatically.
You can access the raw tensor through the .data attribute, while the gradient w.r.t. this variable is
accumulated into .grad.
PyTorch Variables have the same API as PyTorch tensors: (almost) any operation you can do on a
Tensor you can also do on a Variable; the difference is that autograd allows you to automatically
compute gradients.
Deep Learning Phd Course
Autograd Example
import torch
from torch.autograd import Variable
y = x + 2
print(y)
print(y.grad_fn)
z = y * y * 3
out = z.mean()
print(z, out)
out.backward() Try it on jupyter!
print(x.grad)
Deep Learning Phd Course
torch.nn package
Neural network module.
Convenient way of encapsulating parameters, with helpers for moving them to GPU,
exporting, loading, etc…
>>> Container example
model = torch.nn.Sequential(
torch.nn.Linear(D_in, H),
torch.nn.ReLU(),
torch.nn.Linear(H, D_out),
)
Deep Learning Phd Course
net = Net()
print(net)
>>>>>
Net(
(conv1): Conv2d (1, 6, kernel_size=(5, 5), stride=(1, 1))
(conv2): Conv2d (6, 16, kernel_size=(5, 5), stride=(1, 1))
(fc1): Linear(in_features=400, out_features=120)
(fc2): Linear(in_features=120, out_features=84)
(fc3): Linear(in_features=84, out_features=10)
)
params = list(net.parameters())
print(len(params))
print(params[0].size()) # conv1's .weight
Deep Learning Phd Course
Mini-batches in torch.nn
torch.nn only supports mini-batches
The entire torch.nn package only supports inputs that are a mini-batch of samples, and not a
single sample.
For example, nn.Conv2d will take in a 4D Tensor of nSamples x nChannels x Height x Width.
If you have a single sample, just use input.unsqueeze(0) to add a fake batch dimension.
Deep Learning Phd Course
Loss function
output = net(input)
target = Variable(torch.arange(1, 11)) # a dummy target, for example
criterion = nn.MSELoss()
Now, if you follow loss in the backward direction, using it’s .grad_fn attribute, you will see a graph of computations that looks like
this:
input -> conv2d -> relu -> maxpool2d -> conv2d -> relu -> maxpool2d
-> view -> linear -> relu -> linear -> relu -> linear
-> MSELoss
-> loss
So, when we call loss.backward(), the whole graph is differentiated w.r.t. the loss, and all Variables in the graph will have their
.grad Variable accumulated with the gradient.
Deep Learning Phd Course
BackProp
To backpropagate the error all we have to do is to loss.backward().
You need to clear the existing gradients, otherwise gradients will be accumulated to existing gradients
Now we shall call loss.backward(), and have a look at conv1’s bias gradients before and after the backward.
loss.backward()
learning_rate = 0.01
for f in net.parameters():
f.data.sub_(f.grad.data * learning_rate)
Deep Learning Phd Course
Optimizers
However, as you use neural networks, you want to use various different update rules such as SGD, Nesterov-SGD, Adam,
RMSProp, etc. To enable this, we built a small package: torch.optim that implements all these methods. Using it is very
simple:
Acknowledgements
Slides based on http://pytorch.org/tutorials/