Quick Reference#
The Quick Reference collects the PyTorch patterns, tensor conventions, formulas, and workflow rules that recur throughout the course. Use this section when you know roughly what you need to do but do not remember the exact syntax, tensor shape, formula, or workflow pattern. For conceptual explanations and complete examples, return to the lessons or the tutorials.
Important
The Quick Reference is not a tutorial. It is a concise summary of key concepts for experienced users. If you are new to deep learning with PyTorch, start with the lessons or the tutorials.
Find What You Need#
I need to… |
Go to |
|---|---|
inspect, reshape, or move a tensor |
|
create a dataset or mini-batches |
|
define a model or inspect its parameters |
|
calculate a loss or update parameters |
|
evaluate a model or restore the best checkpoint |
|
diagnose poor learning or design a controlled comparison |
|
calculate CNN shapes or convolution parameters |
|
use batch normalization, residual connections, or global pooling |
|
use or fine-tune a pretrained model |
Common Tensor Shapes#
Object |
Typical shape |
|---|---|
Tabular example |
|
Tabular batch |
|
Grayscale image |
|
RGB image |
|
Image batch |
|
Multiclass logits |
|
Multiclass targets |
|
Binary logits |
|
Binary targets |
|
Regression predictions |
|
Notation:
N= batch size;D= feature dimension;K= number of classes;T= number of regression targets;C= number of channels;H= height;W= width;
Training, Validation, Test#
Data is typically split into three partitions, each with a different role in the development workflow.
Partition |
May influence |
|---|---|
Training |
Model parameters and fitted preprocessing |
Validation |
Model selection and development decisions |
Test |
Final reported evaluation only |
Training and Evaluation#
Training updates the model parameters. Evaluation measures a fixed model without parameter updates.
Training |
Evaluation |
|---|---|
|
|
gradients recorded for backpropagation |
gradient recording disabled with |
backward pass computes gradients |
no backward pass |
optimizer updates parameters |
parameters remain fixed |
PyTorch Imports#
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset, Subset, random_split
from torchvision import datasets
import torchvision.transforms.v2 as v2