SlideShare a Scribd company logo
Stefan Seegerer, hi@stefanseegerer.de Matthias Zürl, matthias.zuerl@fau.de CC-BY-SA Last updated: 10/2021
PyTorch CHEAT SHEET
General
PyTorch is a open source machine learning framework. It uses torch.Tensor – multi-dimensional
matrices – to process. A core feature of neural networks in PyTorch is the autograd package,
which provides automatic derivative calculations for all operations on tensors.
There are several ways to
define a neural network in
PyTorch, e.g. with
nn.Sequential (a), as a
class (b) or using a
combination of both.
import torch
import torch.nn as nn
Root package
Neural networks
import torch.nn.functional as F Collection of layers,
activations & more
from torchvision import
datasets, models, transforms
Popular image datasets,
architectures & transforms
torch.randn(*size)
tnsr.view(a,b, ...)
Create random tensor
Reshape tensor to
size (a, b, ...)
requires_grad=True tracks computation history
for derivative calculations
torch.Tensor(L) Create tensor from list
class Net(nn.Module):
def __init__():
super(Net, self).__init__()
self.conv
= nn.Conv2D( , , )
self.pool
= nn.MaxPool2D( )
self.fc = nn.Linear( , )
def forward(self, x):
return x
model = Net()
x = self.pool(
F.relu(self.conv(x))
)
x = self.fc(x)
x = x.view(-1, )
nn.Conv2D( , , )
nn.MaxPool2D( )
nn.ReLU()
nn.Flatten()
nn.Linear( , )
model = nn.Sequential(
) a
Define model
b
It is common practice to save only the model parameters, not the
whole model using model.state_dict()
Save/Load model
model = torch.load('PATH')
torch.save(model, 'PATH') Save model
Load model
GPU Training
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
If a GPU with CUDA support is available, computations are sent to
the GPU with ID 0 using model.to(device) or
inputs, labels = data[0].to(device), data[1].to(device).
Activation functions
nn.ReLU() or F.relu()
Output between 0 and ∞,
most frequently used activation function
nn.Sigmoid() or F.sigmoid()
Output between 0 and 1,
often used for predicting probabilities
nn.Tanh() or F.tanh()
Output between -1 and 1,
often used for classification with two classes
Common activation functions include ReLU,
Sigmoid and Tanh, but there are other activation
functions as well.
Evaluate model
model.eval() Activates evaluation mode, some layers
behave differently
Prevents tracking history, reduces memory
usage, speeds up calculations
torch.no_grad()
The evaluation examines whether the model provides
satisfactory results on previously withheld data.
Depending on the objective, different metrics are used,
such as acurracy, precision, recall, F1, or BLEU.
Train model
LOSS FUNCTIONS
OPTIMIZATION (torch.optim)
PyTorch already offers a bunch of different
loss fuctions, e.g.:
Optimization algorithms are used to update
weights and dynamically adapt the learning
rate with gradient descent, e.g.:
nn.L1Loss Mean absolute error
Stochastic gradient descent
Adaptive moment estimation
optim.SGD
optim.Adam
Adaptive gradient
Root mean square prop
optim.Adagrad
optim.RMSProp
nn.MSELoss Mean squared error (L2Loss)
nn.CrossEntropyLoss Cross entropy, e.g. for single-label
classification or unbalanced training set
nn.BCELoss Binary cross entropy, e.g. for multi-label
classification or autoencoders
Load data
A dataset is represented by a class that
inherits from Dataset (resembles a list
of tuples of the form (features, label)).
DataLoader allows to load a dataset
without caring about its structure.
Usually the dataset is split into training
(e.g. 80%) and test data (e.g. 20%).
Layers
nn.Linear(m, n): Fully Connected
layer (or dense layer) from
m to n neurons
nn.BatchNormXd(n): Normalizes a X-dimensional
input batch with n features; X {1, 2, 3}
nn.RNN/LSTM/GRU: Recurrent networks
connect neurons of one layer with neurons of the
same or a previous layer
nn.Dropout(p=0.5): Randomly
sets input elements to zero during
training to prevent overfitting
nn.Flatten(): Flattens a contiguous
range of dimensions into a tensor
nn.ConvXd(m, n, s): X-dimensional
convolutional layer from m to n channels
with kernel size s; X {1, 2, 3}
nn.MaxPoolXd(s): X-dimensional pooling
layer with kernel size s; X {1, 2, 3}
torch.nn offers a bunch of other building blocks.
A list of state-of-the-art architectures can be found at https://paperswithcode.com/sota.
nn.Embedding(m, n): Lookup table
to map dictionary of size m to
embedding vector of size n
1 Load data 2 Define model 3 Train model 4 Evaluate model
nn.ReLU() creates a nn.Module for example to be used in
Sequential models. F.relu() ist just a call of the ReLU function
e.g. to be used in the forward method.

More Related Content

PPTX
2Wisjshsbebe pehele isienew Dorene isksnwnw
PDF
Machine learning with py torch
PPTX
PyTorch Tutorial for NTU Machine Learing Course 2017
PDF
PyTorch for Deep Learning Practitioners
PDF
pytdddddddddddddddddddddddddddddddddorch.pdf
PPTX
Pytroch-basic.pptx
PPTX
[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
PDF
Pytorch A Detailed Overview Agladze Mikhail
2Wisjshsbebe pehele isienew Dorene isksnwnw
Machine learning with py torch
PyTorch Tutorial for NTU Machine Learing Course 2017
PyTorch for Deep Learning Practitioners
pytdddddddddddddddddddddddddddddddddorch.pdf
Pytroch-basic.pptx
[Update] PyTorch Tutorial for NTU Machine Learing Course 2017
Pytorch A Detailed Overview Agladze Mikhail

Similar to pytorch-cheatsheet.pdf for ML study with pythroch (20)

PDF
Dive Into PyTorch
PDF
Pytorch for tf_developers
PDF
01_pytorch_workflow jutedssd huge hhgggdf
PPTX
pytorch_tutorial_follow_this_to_start.pptx
PDF
OpenPOWER Workshop in Silicon Valley
PDF
1-pytorch-CNN-RNN.pdf
PDF
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
PDF
TensorFlow example for AI Ukraine2016
PDF
TensorFlow meetup: Keras - Pytorch - TensorFlow.js
PDF
Reproducible AI using MLflow and PyTorch
PDF
PyTorch Introduction
PDF
Julien Simon - Deep Dive: Compiling Deep Learning Models
PDF
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
PDF
Icpp power ai-workshop 2018
PDF
Pytorch meetup
PPTX
Soumith Chintala - Increasing the Impact of AI Through Better Software
PDF
A Tour of Tensorflow's APIs
PDF
CSSC ML Workshop
PDF
TensorFlow and Keras: An Overview
PPTX
TensorFlow for IITians
Dive Into PyTorch
Pytorch for tf_developers
01_pytorch_workflow jutedssd huge hhgggdf
pytorch_tutorial_follow_this_to_start.pptx
OpenPOWER Workshop in Silicon Valley
1-pytorch-CNN-RNN.pdf
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
TensorFlow example for AI Ukraine2016
TensorFlow meetup: Keras - Pytorch - TensorFlow.js
Reproducible AI using MLflow and PyTorch
PyTorch Introduction
Julien Simon - Deep Dive: Compiling Deep Learning Models
A Tale of Three Deep Learning Frameworks: TensorFlow, Keras, & PyTorch with B...
Icpp power ai-workshop 2018
Pytorch meetup
Soumith Chintala - Increasing the Impact of AI Through Better Software
A Tour of Tensorflow's APIs
CSSC ML Workshop
TensorFlow and Keras: An Overview
TensorFlow for IITians
Ad

More from JunZhao68 (20)

PDF
语法专题3-状语从句.pdf 英语语法基础部分,涉及到状语从句部分的内容来米爱上
PDF
愛小孩的歐拉一 兼論 108 數學課綱.pdf for 欧拉&数论相关课程描述啊
PDF
svd15_86.pdf for SVD study and revosited
PDF
Quadra-T1-T2-T4_TechSpec.pdf for netint VPA
PDF
Python Advanced Course - part III.pdf for Python
PDF
Python Advanced Course - part I.pdf for Python
PDF
3 - Intro to SVE.pdf for intro ARM SVE part
PDF
Vocabulary Cards for AI and KIDs MIT.pdf
PDF
how CNN works for tech Every parts introductions.pdf
PDF
eics22-slides for researchers need when implementing novel imteraction tech
PDF
Netflix-talk for live video streaming tech
PPTX
Linear system 1_linear in linear algebra.pptx
PDF
GDC2012 JMV Rotations with jim van verth
PDF
1-MIV-tutorial-part-1.pdf
PDF
GOP-Size_report_11_16.pdf
PDF
02-VariableLengthCodes_pres.pdf
PDF
MHV-Presentation-Forman (1).pdf
PDF
CODA_presentation.pdf
PDF
http3-quic-streaming-2020-200121234036.pdf
PDF
NTTW4-FFmpeg.pdf
语法专题3-状语从句.pdf 英语语法基础部分,涉及到状语从句部分的内容来米爱上
愛小孩的歐拉一 兼論 108 數學課綱.pdf for 欧拉&数论相关课程描述啊
svd15_86.pdf for SVD study and revosited
Quadra-T1-T2-T4_TechSpec.pdf for netint VPA
Python Advanced Course - part III.pdf for Python
Python Advanced Course - part I.pdf for Python
3 - Intro to SVE.pdf for intro ARM SVE part
Vocabulary Cards for AI and KIDs MIT.pdf
how CNN works for tech Every parts introductions.pdf
eics22-slides for researchers need when implementing novel imteraction tech
Netflix-talk for live video streaming tech
Linear system 1_linear in linear algebra.pptx
GDC2012 JMV Rotations with jim van verth
1-MIV-tutorial-part-1.pdf
GOP-Size_report_11_16.pdf
02-VariableLengthCodes_pres.pdf
MHV-Presentation-Forman (1).pdf
CODA_presentation.pdf
http3-quic-streaming-2020-200121234036.pdf
NTTW4-FFmpeg.pdf
Ad

Recently uploaded (20)

PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
history of c programming in notes for students .pptx
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
medical staffing services at VALiNTRY
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
CHAPTER 2 - PM Management and IT Context
ManageIQ - Sprint 268 Review - Slide Deck
history of c programming in notes for students .pptx
Understanding Forklifts - TECH EHS Solution
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
top salesforce developer skills in 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Online Work Permit System for Fast Permit Processing
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Which alternative to Crystal Reports is best for small or large businesses.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
How to Migrate SBCGlobal Email to Yahoo Easily
Softaken Excel to vCard Converter Software.pdf
PTS Company Brochure 2025 (1).pdf.......
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
medical staffing services at VALiNTRY
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
ISO 45001 Occupational Health and Safety Management System
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool

pytorch-cheatsheet.pdf for ML study with pythroch

  • 1. Stefan Seegerer, hi@stefanseegerer.de Matthias Zürl, matthias.zuerl@fau.de CC-BY-SA Last updated: 10/2021 PyTorch CHEAT SHEET General PyTorch is a open source machine learning framework. It uses torch.Tensor – multi-dimensional matrices – to process. A core feature of neural networks in PyTorch is the autograd package, which provides automatic derivative calculations for all operations on tensors. There are several ways to define a neural network in PyTorch, e.g. with nn.Sequential (a), as a class (b) or using a combination of both. import torch import torch.nn as nn Root package Neural networks import torch.nn.functional as F Collection of layers, activations & more from torchvision import datasets, models, transforms Popular image datasets, architectures & transforms torch.randn(*size) tnsr.view(a,b, ...) Create random tensor Reshape tensor to size (a, b, ...) requires_grad=True tracks computation history for derivative calculations torch.Tensor(L) Create tensor from list class Net(nn.Module): def __init__(): super(Net, self).__init__() self.conv = nn.Conv2D( , , ) self.pool = nn.MaxPool2D( ) self.fc = nn.Linear( , ) def forward(self, x): return x model = Net() x = self.pool( F.relu(self.conv(x)) ) x = self.fc(x) x = x.view(-1, ) nn.Conv2D( , , ) nn.MaxPool2D( ) nn.ReLU() nn.Flatten() nn.Linear( , ) model = nn.Sequential( ) a Define model b It is common practice to save only the model parameters, not the whole model using model.state_dict() Save/Load model model = torch.load('PATH') torch.save(model, 'PATH') Save model Load model GPU Training device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') If a GPU with CUDA support is available, computations are sent to the GPU with ID 0 using model.to(device) or inputs, labels = data[0].to(device), data[1].to(device). Activation functions nn.ReLU() or F.relu() Output between 0 and ∞, most frequently used activation function nn.Sigmoid() or F.sigmoid() Output between 0 and 1, often used for predicting probabilities nn.Tanh() or F.tanh() Output between -1 and 1, often used for classification with two classes Common activation functions include ReLU, Sigmoid and Tanh, but there are other activation functions as well. Evaluate model model.eval() Activates evaluation mode, some layers behave differently Prevents tracking history, reduces memory usage, speeds up calculations torch.no_grad() The evaluation examines whether the model provides satisfactory results on previously withheld data. Depending on the objective, different metrics are used, such as acurracy, precision, recall, F1, or BLEU. Train model LOSS FUNCTIONS OPTIMIZATION (torch.optim) PyTorch already offers a bunch of different loss fuctions, e.g.: Optimization algorithms are used to update weights and dynamically adapt the learning rate with gradient descent, e.g.: nn.L1Loss Mean absolute error Stochastic gradient descent Adaptive moment estimation optim.SGD optim.Adam Adaptive gradient Root mean square prop optim.Adagrad optim.RMSProp nn.MSELoss Mean squared error (L2Loss) nn.CrossEntropyLoss Cross entropy, e.g. for single-label classification or unbalanced training set nn.BCELoss Binary cross entropy, e.g. for multi-label classification or autoencoders Load data A dataset is represented by a class that inherits from Dataset (resembles a list of tuples of the form (features, label)). DataLoader allows to load a dataset without caring about its structure. Usually the dataset is split into training (e.g. 80%) and test data (e.g. 20%). Layers nn.Linear(m, n): Fully Connected layer (or dense layer) from m to n neurons nn.BatchNormXd(n): Normalizes a X-dimensional input batch with n features; X {1, 2, 3} nn.RNN/LSTM/GRU: Recurrent networks connect neurons of one layer with neurons of the same or a previous layer nn.Dropout(p=0.5): Randomly sets input elements to zero during training to prevent overfitting nn.Flatten(): Flattens a contiguous range of dimensions into a tensor nn.ConvXd(m, n, s): X-dimensional convolutional layer from m to n channels with kernel size s; X {1, 2, 3} nn.MaxPoolXd(s): X-dimensional pooling layer with kernel size s; X {1, 2, 3} torch.nn offers a bunch of other building blocks. A list of state-of-the-art architectures can be found at https://paperswithcode.com/sota. nn.Embedding(m, n): Lookup table to map dictionary of size m to embedding vector of size n 1 Load data 2 Define model 3 Train model 4 Evaluate model nn.ReLU() creates a nn.Module for example to be used in Sequential models. F.relu() ist just a call of the ReLU function e.g. to be used in the forward method.