File size: 10,805 Bytes
578b6a8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | PyTorch Cheat Sheet
******************************
Imports
=========
General
-------
.. code-block:: python
import torch # root package
from torch.utils.data import Dataset, Dataloader # dataset representation and loading
Neural Network API
------------------
.. code-block:: python
import torch.autograd as autograd # computation graph
from torch import Tensor # tensor node in the computation graph
import torch.nn as nn # neural networks
import torch.nn.functional as F # layers, activations and more
import torch.optim as optim # optimizers e.g. gradient descent, ADAM, etc.
from torch.jit import script, trace # hybrid frontend decorator and tracing jit
See `autograd <https://pytorch.org/docs/stable/autograd.html>`__,
`nn <https://pytorch.org/docs/stable/nn.html>`__,
`functional <https://pytorch.org/docs/stable/nn.html#torch-nn-functional>`__
and `optim <https://pytorch.org/docs/stable/optim.html>`__
Torchscript and JIT
---------------
.. code-block:: python
torch.jit.trace() # takes your module or function and an example
# data input, and traces the computational steps
# that the data encounters as it progresses through the model
@script # decorator used to indicate data-dependent
# control flow within the code being traced
See `Torchscript <https://pytorch.org/docs/stable/jit.html>`__
ONNX
----
.. code-block:: python
torch.onnx.export(model, dummy data, xxxx.proto) # exports an ONNX formatted
# model using a trained model, dummy
# data and the desired file name
model = onnx.load("alexnet.proto") # load an ONNX model
onnx.checker.check_model(model) # check that the model
# IR is well formed
onnx.helper.printable_graph(model.graph) # print a human readable
# representation of the graph
See `onnx <https://pytorch.org/docs/stable/onnx.html>`__
Vision
------
.. code-block:: python
from torchvision import datasets, models, transforms # vision datasets,
# architectures &
# transforms
import torchvision.transforms as transforms # composable transforms
See
`torchvision <https://pytorch.org/docs/stable/torchvision/index.html>`__
Distributed Training
--------------------
.. code-block:: python
import torch.distributed as dist # distributed communication
from multiprocessing import Process # memory sharing processes
See `distributed <https://pytorch.org/docs/stable/distributed.html>`__
and
`multiprocessing <https://pytorch.org/docs/stable/multiprocessing.html>`__
Tensors
=========
Creation
--------
.. code-block:: python
torch.randn(*size) # tensor with independent N(0,1) entries
torch.[ones|zeros](*size) # tensor with all 1's [or 0's]
torch.Tensor(L) # create tensor from [nested] list or ndarray L
x.clone() # clone of x
with torch.no_grad(): # code wrap that stops autograd from tracking tensor history
requires_grad=True # arg, when set to True, tracks computation
# history for future derivative calculations
See `tensor <https://pytorch.org/docs/stable/tensors.html>`__
Dimensionality
--------------
.. code-block:: python
x.size() # return tuple-like object of dimensions
torch.cat(tensor_seq, dim=0) # concatenates tensors along dim
x.view(a,b,...) # reshapes x into size (a,b,...)
x.view(-1,a) # reshapes x into size (b,a) for some b
x.transpose(a,b) # swaps dimensions a and b
x.permute(*dims) # permutes dimensions
x.unsqueeze(dim) # tensor with added axis
x.unsqueeze(dim=2) # (a,b,c) tensor -> (a,b,1,c) tensor
See `tensor <https://pytorch.org/docs/stable/tensors.html>`__
Algebra
-------
.. code-block:: python
A.mm(B) # matrix multiplication
A.mv(x) # matrix-vector multiplication
x.t() # matrix transpose
See `math
operations <https://pytorch.org/docs/stable/torch.html?highlight=mm#math-operations>`__
GPU Usage
---------
.. code-block:: python
torch.cuda.is_available # check for cuda
x.cuda() # move x's data from
# CPU to GPU and return new object
x.cpu() # move x's data from GPU to CPU
# and return new object
if not args.disable_cuda and torch.cuda.is_available(): # device agnostic code
args.device = torch.device('cuda') # and modularity
else: #
args.device = torch.device('cpu') #
net.to(device) # recursively convert their
# parameters and buffers to
# device specific tensors
mytensor.to(device) # copy your tensors to a device
# (gpu, cpu)
See `cuda <https://pytorch.org/docs/stable/cuda.html>`__
Deep Learning
=============
.. code-block:: python
nn.Linear(m,n) # fully connected layer from
# m to n units
nn.ConvXd(m,n,s) # X dimensional conv layer from
# m to n channels where X⍷{1,2,3}
# and the kernel size is s
nn.MaxPoolXd(s) # X dimension pooling layer
# (notation as above)
nn.BatchNorm # batch norm layer
nn.RNN/LSTM/GRU # recurrent layers
nn.Dropout(p=0.5, inplace=False) # dropout layer for any dimensional input
nn.Dropout2d(p=0.5, inplace=False) # 2-dimensional channel-wise dropout
nn.Embedding(num_embeddings, embedding_dim) # (tensor-wise) mapping from
# indices to embedding vectors
See `nn <https://pytorch.org/docs/stable/nn.html>`__
Loss Functions
--------------
.. code-block:: python
nn.X # where X is BCELoss, CrossEntropyLoss,
# L1Loss, MSELoss, NLLLoss, SoftMarginLoss,
# MultiLabelSoftMarginLoss, CosineEmbeddingLoss,
# KLDivLoss, MarginRankingLoss, HingeEmbeddingLoss
# or CosineEmbeddingLoss
See `loss
functions <https://pytorch.org/docs/stable/nn.html#loss-functions>`__
Activation Functions
--------------------
.. code-block:: python
nn.X # where X is ReLU, ReLU6, ELU, SELU, PReLU, LeakyReLU,
# Threshold, HardTanh, Sigmoid, Tanh,
# LogSigmoid, Softplus, SoftShrink,
# Softsign, TanhShrink, Softmin, Softmax,
# Softmax2d or LogSoftmax
See `activation
functions <https://pytorch.org/docs/stable/nn.html#non-linear-activations-weighted-sum-nonlinearity>`__
Optimizers
----------
.. code-block:: python
opt = optim.x(model.parameters(), ...) # create optimizer
opt.step() # update weights
optim.X # where X is SGD, Adadelta, Adagrad, Adam,
# SparseAdam, Adamax, ASGD,
# LBFGS, RMSProp or Rprop
See `optimizers <https://pytorch.org/docs/stable/optim.html>`__
Learning rate scheduling
------------------------
.. code-block:: python
scheduler = optim.X(optimizer,...) # create lr scheduler
scheduler.step() # update lr at start of epoch
optim.lr_scheduler.X # where X is LambdaLR, StepLR, MultiStepLR,
# ExponentialLR or ReduceLROnPLateau
See `learning rate
scheduler <https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate>`__
Data Utilities
==============
Datasets
--------
.. code-block:: python
Dataset # abstract class representing dataset
TensorDataset # labelled dataset in the form of tensors
Concat Dataset # concatenation of Datasets
See
`datasets <https://pytorch.org/docs/stable/data.html?highlight=dataset#torch.utils.data.Dataset>`__
Dataloaders and DataSamplers
----------------------------
.. code-block:: python
DataLoader(dataset, batch_size=1, ...) # loads data batches agnostic
# of structure of individual data points
sampler.Sampler(dataset,...) # abstract class dealing with
# ways to sample from dataset
sampler.XSampler where ... # Sequential, Random, Subset,
# WeightedRandom or Distributed
See
`dataloader <https://pytorch.org/docs/stable/data.html?highlight=dataloader#torch.utils.data.DataLoader>`__
Also see
--------
- `Deep Learning with PyTorch: A 60 Minute
Blitz <https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html>`__
*(pytorch.org)*
- `PyTorch Forums <https://discuss.pytorch.org/>`__
*(discuss.pytorch.org)*
- `PyTorch for Numpy
users <https://github.com/wkentaro/pytorch-for-numpy-users>`__
*(github.com/wkentaro/pytorch-for-numpy-users)*
|