Pytorch Tutorial PDF
Pytorch Tutorial PDF
Pytorch Tutorial
Introduction
■ Why we need deep learning frameworks
(1) Easily build big computational graphs
(2) Easily compute gradients in computational graphs
(3) Run it all efficiently on GPU
■ Why we need Pytorch?
(1) Easy to implement, code, and debug
(2) More flexible due to its dynamic computational graph.
(3) High execution efficiency, since it developed from C.
Large-scale Intelligent Systems Laboratory
PyTorch: Tensors
PyTorch: Tensors
To run on GPU, just cast tensors to a
cuda data type!
(E,g torch.cuda.FloatTensor)
Create random tensors for data and
weights.
Forward pass:
compute predictions and loss
Backward pass:
manually compute gradients
Math Operation:
Other operations:
Large-scale Intelligent Systems Laboratory
PyTorch: Autograd
■ A PyTorch Variable is a node in a
computational graph
■ x.data is a Tensor
■ x.grad is a Variable of gradients
(same shape as x.data)
■ x.grad.data is a Tensor of gradients
■ PyTorch Tensors and Variables have
the same API!
■ Variables remember how they
were created (for backprop)
Large-scale Intelligent Systems Laboratory
PyTorch: Autograd
We will not want gradients (of loss)
with respect to data
PyTorch: nn
PyTorch: nn
Define our model as a sequence of
layers
Backward pass:
compute all gradients
You can find more details about API in the following link:
http://pytorch.org/docs/master/index.html
Large-scale Intelligent Systems Laboratory
PyTorch: optim
MNIST Example
https://github.com/pytorch/examples/blob/master/mnist/main.py
Large-scale Intelligent Systems Laboratory
Extending PyTorch
■ torch.autograd
■ customize forward() and backward()
■ check gradient
■ from torch.autograd import
gradcheck
Large-scale Intelligent Systems Laboratory
Extending PyTorch
■ torch.autograd
■ Adding function to a
Module
Large-scale Intelligent Systems Laboratory
Extending PyTorch
■ C-extension
a. prepare your C code
c file
Large-scale Intelligent Systems Laboratory
Extending PyTorch
■ C-extension
b. build custom extension c. include it in your Python code
Torchvision
■ popular datasets
■ cifar10, coco, lsun, mnist, ...
■ popular model architectures (pretrained)
■ alexnet, densenet, inception, resnet, squeezenet, vgg
■ common image transformations
■ Normalize, Scale, CenterCrop, Pad, RandomCrop,
RandomFlip, …
■ Compose
Large-scale Intelligent Systems Laboratory
Visualization
■ TensorboardX
■ pip install tensorboardX
■ tensorboard for pytorch (and chainer,
mxnet, numpy, ...)
■ Support scalar, image, histogram, audio,
text, graph, onnx_graph, embedding and
pr_curve
■ demo http://35.197.26.245:6006/
Visualization
■ TensorboardX
a. Run the demo script: python demo.py
b. Use TensorBoard with tensorboard --logdir runs (needs to install
TensorFlow)
Large-scale Intelligent Systems Laboratory
Visualization
■ Other choices
Visdom Seaborn
Large-scale Intelligent Systems Laboratory
Onnx
■ Open Neural Network Exchange (ONNX)
■ an open source format to move models between tools
■ defines an extensible computation graph model, built-in operators and
standard data types
■ Hardware Optimizations
■ Supported Tools
Frameworks
Converters Runtimes
Large-scale Intelligent Systems Laboratory
References
1. http://pytorch.org/
2. http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture
8.pdf
3. https://github.com/pytorch/pytorch
Large-scale Intelligent Systems Laboratory
Thank
You!