10315 Recitation 8
PyTorch Basics
What is PyTorch?
Why PyTorch?
Why is important?
Research
thousands of papers
CVPR
ICML
NeurIPS
…
AI Jobs
machine learning engineer
perceptions engineer
(list goes on …)
Applications
object recognition
text classification
deep reinforcement learning
image generation
….
Coursework
11485
16385
10403
10417
…
Why is important?
Research
thousands of papers
CVPR
ICML
NeurIPS
…
AI Jobs
machine learning engineer
perceptions engineer
(list goes on …)
Applications
object recognition
text classification
deep reinforcement learning
image generation
….
Coursework
11485
16385
10403
10417
…
HW7
What does PyTorch Do?
gradient descent
backpropagation
directed acyclic graphs
GPU computing
distributed processing
transform impls.
Abstracts Away Complexities of Deep Learning
Layers
Models
Datasets
Data Transforms
Optimizers
Loss Functions
How to Use PyTorch
Tensors
The basic building block of PyTorch
PyTorch Tensors are a lot like NumPy arrays
DataLoaders
Used for inputs to machine learning models and allows us to iterate through the data
Just instantiate object: requires dataset as input
Optional parameters
Datasets
Dataset Class used by subclassing
Modules and Layers
Use Modules to define neural networks and Layers to define internal layers of the Neural Network
Module: the neural network base class
Layers: instantiate in subclassed module
Losses and Optimizers
Use to train neural network and update parameters
Losses: compute the loss between model output and label
Optimizer: performs gradient descent updates on model weights
Training
Using the basic building blocks from before to train model
Pseudocode Training Loop
for each epoch:
for each (x_batch, y_batch) of data:
reset gradients to 0
preds = model(batch)
loss = loss_fn(preds, y_batch)
compute gradients
update weights
Training Loop
GPU Training
If we have access to a GPU, we can move our data and model over to the GPU so that we can train using a faster processor.
Need to move 2 objects over
Use the .to(...) method to move objects to the GPU
You can now define and train a NN with
Quick Note about Google Colab
Colab allows access to GPU architectures
For more efficient computing, adjust your code to run on the GPU and select the GPU runtime.
Runtime > Change Runtime type > Hardware Accelerator > GPU > SAVE
Sources & Documentation
Tutorials
Documentation