SlideShare a Scribd company logo
Workflow
What we’re going to cover
A PyTorch workflow
(one of many)
Where can you get help?
•Follow along with the code
•Try it for yourself
•Press SHIFT + CMD + SPACE to read the docstring
•Search for it
•Try again
•Ask
Motto #1: “If in doubt, run the code”
https://www.github.com/mrdbourke/pytorch-deep-learning/discussions
Let’s code!
Machine learning: a game of two parts
[[116, 78, 15],
[117, 43, 96],
[125, 87, 23],
…,
[[0.983, 0.004, 0.013],
[0.110, 0.889, 0.001],
[0.023, 0.027, 0.985],
…,
Inputs
Numerical
encoding
Learns
representation
(patterns/features/
weights)
Representation
outputs Outputs
Ramen,
Spaghetti
Not spam
“Hey Siri, what’s
the weather
today?”
[[116, 78, 15],
[117, 43, 96],
[125, 87, 23],
…,
[[0.983, 0.004, 0.013],
[0.110, 0.889, 0.001],
[0.023, 0.027, 0.985],
…,
Inputs
Numerical
encoding
Learns
representation
(patterns/features/weights)
Representation
outputs Outputs
Ramen,
Spaghetti
Not spam
“Hey Siri, what’s
the weather
today?”
Part 1: Turn data into numbers Part 2: Build model to learn patterns in numbers
Three datasets
(possibly the most important
concept in machine learning…)
Final exam
(test set)
Course materials
(training set)
Practice exam
(validation set)
Generalization
The ability for a machine learning model to perform well on data it hasn’t seen
before.
Model learns patterns from here
Tune model patterns See if the model is ready for the
wild
Subclass nn.Module
(this contains all the building blocks for neural networks)
Any subclass of nn.Module needs to override forward()
(this de
fi
nes the forward computation of the model)
Initialise model parameters to be used in various
computations (these could be di
ff
erent layers from
torch.nn, single parameters, hard-coded values or
functions)
requires_grad=True means PyTorch will track the
gradients of this speci
fi
c parameter for use with
torch.autograd and gradient descent (for many
torch.nn modules, requires_grad=True is set by
default)
PyTorch essential neural network building modules
PyTorch module What does it do?
torch.nn
Contains all of the building blocks for computational graphs (essentially a series of
computations executed in a particular way).
torch.nn.Module
The base class for all neural network modules, all the building blocks for neural
networks are subclasses. If you're building a neural network in PyTorch, your models
should subclass nn.Module. Requires a forward() method be implemented.
torch.optim
Contains various optimization algorithms (these tell the model parameters stored
in nn.Parameter how to best change to improve gradient descent and in turn
reduce the loss).
torch.utils.data.Dataset
Represents a map between key (label) and sample (features) pairs of your data.
Such as images and their associated labels.
torch.utils.data.DataLoader
Creates a Python iterable over a torch Dataset (allows you to iterate over your
data).
See more: https://pytorch.org/tutorials/beginner/ptcheat.html
torch.optim torch.nn
torch.nn.Module
torchvision.models
torchmetrics
See more: https://pytorch.org/tutorials/beginner/ptcheat.html
torch.utils.data.Dataset
torch.utils.data.DataLoader
torchvision.transforms
torch.utils.tensorboard
Di
ff
erence (y_pred[0] - y_test[0]) = 0.4618
MAE_loss = torch.mean(torch.abs(y_pred-y_test))
or
MAE_loss = torch.nn.L1Loss
Mean absolute error (MAE)
Mean absolute error (MAE) = Repeat for all and take the mean
See more: https://pytorch.org/tutorials/beginner/ptcheat.html#loss-functions
Source: @mrdbourke Twitter & see the video version on YouTube.
PyTorch training loop
Pass the data through the model for a number of epochs
(e.g. 100 for 100 passes of the data)
Zero the optimizer gradients (they accumulate every
epoch, zero them to start fresh each forward pass)
Pass the data through the model, this will perform the
forward() method located within the model object
Calculate the loss value (how wrong the model’s
predictions are)
Perform backpropagation on the loss function (compute
the gradient of every parameter with
requires_grad=True)
Step the optimizer to update the model’s parameters with
respect to the gradients calculated by loss.backward()
Note: all of this can be turned into a function
PyTorch testing loop
Create empty lists for storing useful values (helpful for
tracking model progress)
Pass the test data through the model (this will call the
model’s implemented forward() method)
Tell the model we want to evaluate rather than train (this
turns o
ff
functionality used for training but not
evaluation)
Turn on torch.inference_mode() context manager to
disable functionality such as gradient tracking for
inference (gradient tracking not needed for inference)
Calculate the test loss value (how wrong the model’s
predictions are on the test dataset, lower is better)
Display information outputs for how the model is doing
during training/testing every ~10 epochs (note: what gets
printed out here can be adjusted for speci
fi
c problems)
See more: https://discuss.pytorch.org/t/model-eval-vs-with-torch-no-grad/19615 & PyTorch Twitter announcement of torch.inference_mode()
Note: all of this can be turned into a function
(faster performance!)
01_pytorch_workflow jutedssd huge hhgggdf
Linear regression model with nn.Parameter
Linear regression model with nn.Linear
[[116, 78, 15],
[117, 43, 96],
[125, 87, 23],
…,
[[0.983, 0.004, 0.013],
[0.110, 0.889, 0.001],
[0.023, 0.027, 0.985],
…,
Inputs
Numerical
encoding
Learns
representation
(patterns/features/weights)
Representation
outputs
Outputs
Ramen,
Spaghetti
[[0.092, 0.210, 0.415],
[0.778, 0.929, 0.030],
[0.019, 0.182, 0.555],
…,
1. Initialise with random
weights (only at beginning)
2. Show examples
3. Update representation
outputs
4. Repeat with more
examples
Supervised
learning
(overview)
Neural Networks
[[116, 78, 15],
[117, 43, 96],
[125, 87, 23],
…,
[[0.983, 0.004, 0.013],
[0.110, 0.889, 0.001],
[0.023, 0.027, 0.985],
…,
Inputs
Numerical
encoding
Learns
representation
(patterns/features/weights)
Representation
outputs
Outputs
(a human can
understand these)
Ramen,
Spaghetti
Not spam
“Hey Siri, what’s
the weather
today?”
(choose the appropriate
neural network for your
problem)
(before data gets used
with an algorithm, it
needs to be turned into
numbers)
These are tensors!
[[116, 78, 15],
[117, 43, 96],
[125, 87, 23],
…,
[[0.983, 0.004, 0.013],
[0.110, 0.889, 0.001],
[0.023, 0.027, 0.985],
…,
Inputs
Numerical
encoding
Learns
representation
(patterns/features/weights)
Representation
outputs
Outputs
Ramen,
Spaghetti
These are tensors!
How to approach this course
1. Code along
Motto #1: if in doubt, run the code! 2. Explore and
experiment
Motto #2:
Experiment, experiment,
experiment!
3. Visualize what you
don’t understand
Motto #3:
Visualize, visualize, visualize!
4. Ask questions
🛠
5. Do the exercises
🤗
6. Share your work
(including the
“dumb” ones)
How not to approach this course
Avoid:
🧠
🔥
🔥
🔥 “I can’t learn
______”
Resources
https://www.github.com/mrdbourke/pytorch-deep-learning
https://www.github.com/mrdbourke/pytorch-deep-learning/
discussions
https://learnpytorch.io
Course materials Course Q&A Course online book
PyTorch website &
forums
This course
All things PyTorch
Di
ff
erence (y_pred[0] - y_test[0]) = 0.4618

More Related Content

Similar to 01_pytorch_workflow jutedssd huge hhgggdf (20)

PPTX
From Tensorflow Graph to Tensorflow Eager
Guy Hadash
 
PPTX
"AI in the browser: predicting user actions in real time with TensorflowJS", ...
Fwdays
 
PPT
Cascading talk in Etsy (http://www.meetup.com/cascading/events/169390262/)
Jyotirmoy Sundi
 
PDF
pytdddddddddddddddddddddddddddddddddorch.pdf
drjigarsoni28
 
PDF
00_pytorch_and_deep_learning_fundamentals.pdf
eanyang7
 
PDF
Simple, fast, and scalable torch7 tutorial
Jin-Hwa Kim
 
PDF
Pytorch for tf_developers
Abdul Muneer
 
PDF
MCL 322 Optimizing Training on Apache MXNet
Julien SIMON
 
PDF
Power ai tensorflowworkloadtutorial-20171117
Ganesan Narayanasamy
 
PPTX
Ot performance webinar
Suite Solutions
 
PDF
C3 w1
Ajay Taneja
 
PDF
TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...
Chetan Khatri
 
PDF
Benchy: Lightweight framework for Performance Benchmarks
Marcel Caraciolo
 
PDF
maxbox starter60 machine learning
Max Kleiner
 
PDF
Viktor Tsykunov: Azure Machine Learning Service
Lviv Startup Club
 
PPTX
Chapter 2
application developer
 
PPT
TDD And Refactoring
Naresh Jain
 
PPTX
Deep learning image classification aplicado al mundo de la moda
Javier Abadía
 
PDF
maXbox Starter 45 Robotics
Max Kleiner
 
PDF
02_pytorch_classification.pdf might be the
stuartkyeswa4
 
From Tensorflow Graph to Tensorflow Eager
Guy Hadash
 
"AI in the browser: predicting user actions in real time with TensorflowJS", ...
Fwdays
 
Cascading talk in Etsy (http://www.meetup.com/cascading/events/169390262/)
Jyotirmoy Sundi
 
pytdddddddddddddddddddddddddddddddddorch.pdf
drjigarsoni28
 
00_pytorch_and_deep_learning_fundamentals.pdf
eanyang7
 
Simple, fast, and scalable torch7 tutorial
Jin-Hwa Kim
 
Pytorch for tf_developers
Abdul Muneer
 
MCL 322 Optimizing Training on Apache MXNet
Julien SIMON
 
Power ai tensorflowworkloadtutorial-20171117
Ganesan Narayanasamy
 
Ot performance webinar
Suite Solutions
 
TransmogrifAI - Automate Machine Learning Workflow with the power of Scala an...
Chetan Khatri
 
Benchy: Lightweight framework for Performance Benchmarks
Marcel Caraciolo
 
maxbox starter60 machine learning
Max Kleiner
 
Viktor Tsykunov: Azure Machine Learning Service
Lviv Startup Club
 
TDD And Refactoring
Naresh Jain
 
Deep learning image classification aplicado al mundo de la moda
Javier Abadía
 
maXbox Starter 45 Robotics
Max Kleiner
 
02_pytorch_classification.pdf might be the
stuartkyeswa4
 

Recently uploaded (20)

PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
NTPC PATRATU Summer internship report.pdf
hemant03701
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Distribution reservoir and service storage pptx
dhanashree78
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Digital water marking system project report
Kamal Acharya
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
NTPC PATRATU Summer internship report.pdf
hemant03701
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Ad

01_pytorch_workflow jutedssd huge hhgggdf

  • 2. What we’re going to cover A PyTorch workflow (one of many)
  • 3. Where can you get help? •Follow along with the code •Try it for yourself •Press SHIFT + CMD + SPACE to read the docstring •Search for it •Try again •Ask Motto #1: “If in doubt, run the code” https://www.github.com/mrdbourke/pytorch-deep-learning/discussions
  • 5. Machine learning: a game of two parts [[116, 78, 15], [117, 43, 96], [125, 87, 23], …, [[0.983, 0.004, 0.013], [0.110, 0.889, 0.001], [0.023, 0.027, 0.985], …, Inputs Numerical encoding Learns representation (patterns/features/ weights) Representation outputs Outputs Ramen, Spaghetti Not spam “Hey Siri, what’s the weather today?”
  • 6. [[116, 78, 15], [117, 43, 96], [125, 87, 23], …, [[0.983, 0.004, 0.013], [0.110, 0.889, 0.001], [0.023, 0.027, 0.985], …, Inputs Numerical encoding Learns representation (patterns/features/weights) Representation outputs Outputs Ramen, Spaghetti Not spam “Hey Siri, what’s the weather today?” Part 1: Turn data into numbers Part 2: Build model to learn patterns in numbers
  • 7. Three datasets (possibly the most important concept in machine learning…) Final exam (test set) Course materials (training set) Practice exam (validation set) Generalization The ability for a machine learning model to perform well on data it hasn’t seen before. Model learns patterns from here Tune model patterns See if the model is ready for the wild
  • 8. Subclass nn.Module (this contains all the building blocks for neural networks) Any subclass of nn.Module needs to override forward() (this de fi nes the forward computation of the model) Initialise model parameters to be used in various computations (these could be di ff erent layers from torch.nn, single parameters, hard-coded values or functions) requires_grad=True means PyTorch will track the gradients of this speci fi c parameter for use with torch.autograd and gradient descent (for many torch.nn modules, requires_grad=True is set by default)
  • 9. PyTorch essential neural network building modules PyTorch module What does it do? torch.nn Contains all of the building blocks for computational graphs (essentially a series of computations executed in a particular way). torch.nn.Module The base class for all neural network modules, all the building blocks for neural networks are subclasses. If you're building a neural network in PyTorch, your models should subclass nn.Module. Requires a forward() method be implemented. torch.optim Contains various optimization algorithms (these tell the model parameters stored in nn.Parameter how to best change to improve gradient descent and in turn reduce the loss). torch.utils.data.Dataset Represents a map between key (label) and sample (features) pairs of your data. Such as images and their associated labels. torch.utils.data.DataLoader Creates a Python iterable over a torch Dataset (allows you to iterate over your data). See more: https://pytorch.org/tutorials/beginner/ptcheat.html
  • 10. torch.optim torch.nn torch.nn.Module torchvision.models torchmetrics See more: https://pytorch.org/tutorials/beginner/ptcheat.html torch.utils.data.Dataset torch.utils.data.DataLoader torchvision.transforms torch.utils.tensorboard
  • 11. Di ff erence (y_pred[0] - y_test[0]) = 0.4618 MAE_loss = torch.mean(torch.abs(y_pred-y_test)) or MAE_loss = torch.nn.L1Loss Mean absolute error (MAE) Mean absolute error (MAE) = Repeat for all and take the mean See more: https://pytorch.org/tutorials/beginner/ptcheat.html#loss-functions
  • 12. Source: @mrdbourke Twitter & see the video version on YouTube.
  • 13. PyTorch training loop Pass the data through the model for a number of epochs (e.g. 100 for 100 passes of the data) Zero the optimizer gradients (they accumulate every epoch, zero them to start fresh each forward pass) Pass the data through the model, this will perform the forward() method located within the model object Calculate the loss value (how wrong the model’s predictions are) Perform backpropagation on the loss function (compute the gradient of every parameter with requires_grad=True) Step the optimizer to update the model’s parameters with respect to the gradients calculated by loss.backward() Note: all of this can be turned into a function
  • 14. PyTorch testing loop Create empty lists for storing useful values (helpful for tracking model progress) Pass the test data through the model (this will call the model’s implemented forward() method) Tell the model we want to evaluate rather than train (this turns o ff functionality used for training but not evaluation) Turn on torch.inference_mode() context manager to disable functionality such as gradient tracking for inference (gradient tracking not needed for inference) Calculate the test loss value (how wrong the model’s predictions are on the test dataset, lower is better) Display information outputs for how the model is doing during training/testing every ~10 epochs (note: what gets printed out here can be adjusted for speci fi c problems) See more: https://discuss.pytorch.org/t/model-eval-vs-with-torch-no-grad/19615 & PyTorch Twitter announcement of torch.inference_mode() Note: all of this can be turned into a function (faster performance!)
  • 16. Linear regression model with nn.Parameter Linear regression model with nn.Linear
  • 17. [[116, 78, 15], [117, 43, 96], [125, 87, 23], …, [[0.983, 0.004, 0.013], [0.110, 0.889, 0.001], [0.023, 0.027, 0.985], …, Inputs Numerical encoding Learns representation (patterns/features/weights) Representation outputs Outputs Ramen, Spaghetti [[0.092, 0.210, 0.415], [0.778, 0.929, 0.030], [0.019, 0.182, 0.555], …, 1. Initialise with random weights (only at beginning) 2. Show examples 3. Update representation outputs 4. Repeat with more examples Supervised learning (overview)
  • 18. Neural Networks [[116, 78, 15], [117, 43, 96], [125, 87, 23], …, [[0.983, 0.004, 0.013], [0.110, 0.889, 0.001], [0.023, 0.027, 0.985], …, Inputs Numerical encoding Learns representation (patterns/features/weights) Representation outputs Outputs (a human can understand these) Ramen, Spaghetti Not spam “Hey Siri, what’s the weather today?” (choose the appropriate neural network for your problem) (before data gets used with an algorithm, it needs to be turned into numbers) These are tensors!
  • 19. [[116, 78, 15], [117, 43, 96], [125, 87, 23], …, [[0.983, 0.004, 0.013], [0.110, 0.889, 0.001], [0.023, 0.027, 0.985], …, Inputs Numerical encoding Learns representation (patterns/features/weights) Representation outputs Outputs Ramen, Spaghetti These are tensors!
  • 20. How to approach this course 1. Code along Motto #1: if in doubt, run the code! 2. Explore and experiment Motto #2: Experiment, experiment, experiment! 3. Visualize what you don’t understand Motto #3: Visualize, visualize, visualize! 4. Ask questions 🛠 5. Do the exercises 🤗 6. Share your work (including the “dumb” ones)
  • 21. How not to approach this course Avoid: 🧠 🔥 🔥 🔥 “I can’t learn ______”
  • 23. Di ff erence (y_pred[0] - y_test[0]) = 0.4618