Pytorch
Pytorch
Willie Chang
Pranay Manocha
Installing PyTorch
• 💻💻 On your own computer
• Anaconda/Miniconda: conda install pytorch -c pytorch
• Others via pip: pip3 install torch
• First blank is username, second is hostname code: see Piazza @108 for info.
Why talk about libraries?
• Advantage of various deep learning frameworks
PyTorch!
• Easy Interface − easy to use API. The code execution in this framework is quite easy. Also need a
fewer lines to code in comparison.
• It is easy to debug and understand the code.
• Python usage − This library is considered to be Pythonic which smoothly integrates with the Python
data science stack.
• It can be considered as NumPy extension to GPUs.
• Computational graphs − PyTorch provides an excellent platform which offers dynamic
computational graphs. Thus a user can change them during runtime.
• It includes many layers as Torch.
• It includes lot of loss functions.
• It allows building networks whose structure is dependent on computation itself.
• NLP: account for variable length sentences. Instead of padding the sentence to a
fixed length, we create graphs with different number of LSTM cells based on the sentence’s
length.
PyTorch
• Fundamental Concepts of PyTorch
• Tensors
• Autograd
• Modular structure
• Models / Layers
• Datasets
• Dataloader
• Visualization Tools like
• TensorboardX (monitor training)
• PyTorchViz (visualise computation graph)
• Various other functions
• loss (MSE,CE etc..)
• optimizers
Prepare Train Evaluate
Input Data Model Model
•Load data •Train •Visualise
•Iterate over weights
examples
Tensor
• Tensor?
• PyTorch Tensors are just like numpy arrays, but they can run on GPU.
• Examples:
• Properties:
• model = ManualLinearRegression()
• model.state_dic() - returns a dictionary of trainable parameters with their current
values
• model.parameters() - returns a list of all trainable parameters in the model
• model.train() or model.eval()
Putting things together
• Sample Code in practice
Complex Models
• Complex Model Class
• Predefined 'layer' modules
https://pytorch.org/tutorials/beginner/saving_loading_models.html
Saving / Loading Weights (continued)
• Method 2
• Checkpoint – resume training / inference
• Save:
• torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss,
...
}, PATH)
• Load:
• model = TheModelClass(*args, **kwargs)
optimizer = TheOptimizerClass(*args, **kwargs)
checkpoint = torch.load(PATH)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
epoch = checkpoint['epoch']
loss = checkpoint['loss']
model.eval()
# - or -
model.train()
Evaluation
• Two important things:
• torch.no_grad()
• Don’t store the history of all computations
• eval()
• Tell compiler which mode to run on.
Visualization
• TensorboardX (visualise training)
• PyTorchViz (visualise computation graph)
https://github.com/lanpa/tensorboardX/
Visualization (continued)
• PyTorchViz
https://github.com/szagoruyko/pytorchviz
References
• Important References:
• For setting up jupyter notebook on princeton ionic cluster
• https://oncomputingwell.princeton.edu/2018/05/jupyter-on-the-cluster/
• Best reference is PyTorch Documentation
• https://pytorch.org/ and https://github.com/pytorch/pytorch
• Good Blogs: (with examples and code)
• https://lelon.io/blog/2018/02/08/pytorch-with-baby-steps
• https://www.tutorialspoint.com/pytorch/index.htm
• https://github.com/hunkim/PyTorchZeroToAll
• Free GPU access for short time:
• Google Colab provides free Tesla K80 GPU of about 12GB. You can run the session in
an interactive Colab Notebook for 12 hours.
• https://colab.research.google.com/
Misc
• Dynamic VS Static Computation Graph
Epoch 1
a b x_train_tensor
Misc
• Dynamic VS Static Computation Graph
a b x_train_tensor
yhat
Misc
• Dynamic VS Static Computation Graph
a b x_train_tensor
loss
yhat y_train_tensor
loss
Misc
• Dynamic VS Static Computation Graph
Epoch 2
a b x_train_tensor
Misc
• Dynamic VS Static Computation Graph
a b x_train_tensor
yhat
Misc
• Dynamic VS Static Computation Graph
a b x_train_tensor
loss
yhat y_train_tensor
loss
Misc
• Dynamic VS Static Computation Graph
Alternative: Static
graphs
Step 1: Build
computational graph
describing our
computation (including
finding paths for
backprop)