PyTorch Cheat Sheet
Essential PyTorch syntax for tensors, autograd, building neural network modules, and writing a standard training loop for deep learning models.
2 PagesIntermediateApr 10, 2026
Tensor Basics
Creating and manipulating tensors.
python
import torchx = torch.tensor([[1.0, 2.0], [3.0, 4.0]])y = torch.zeros(2, 2)z = torch.rand(2, 2)device = "cuda" if torch.cuda.is_available() else "cpu"x = x.to(device)a = x + y # elementwise addb = x @ y # matrix multiplyc = x.view(-1, 4) # reshape (view shares memory)print(x.shape, x.dtype)
Autograd
Automatic differentiation for gradients.
python
x = torch.tensor(2.0, requires_grad=True)y = x ** 2 + 3 * xy.backward() # compute dy/dxprint(x.grad) # tensor(7.) since dy/dx = 2x + 3with torch.no_grad(): # disable grad tracking (inference) z = x * 2
Model & Training Loop
Define a network and train it.
python
import torch.nn as nnimport torch.optim as optimclass Net(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 128) self.fc2 = nn.Linear(128, 10) def forward(self, x): x = torch.relu(self.fc1(x)) return self.fc2(x)model = Net().to(device)optimizer = optim.Adam(model.parameters(), lr=1e-3)criterion = nn.CrossEntropyLoss()for epoch in range(epochs): for inputs, labels in dataloader: optimizer.zero_grad() # clear gradients outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() # backprop optimizer.step() # update weights
Common Layers & Losses
Frequently used nn.Module building blocks.
- nn.Linear- fully connected layer
- nn.Conv2d- 2D convolution for image data
- nn.LSTM- recurrent layer for sequence data
- nn.Dropout- regularization by zeroing random activations
- nn.BatchNorm2d- normalizes activations across the batch
- nn.CrossEntropyLoss- combines LogSoftmax + NLLLoss for classification
- nn.MSELoss- mean squared error for regression
- torch.optim.Adam / SGD- optimizers that update parameters from gradients
Pro Tip
Call model.eval() and wrap inference in torch.no_grad() to disable dropout/batchnorm training behavior and gradient tracking — forgetting this is a common source of inconsistent validation metrics.
Was this cheat sheet helpful?
Explore Topics
#PyTorch#PyTorchCheatSheet#DataScience#Intermediate#TensorBasics#Autograd#ModelTrainingLoop#CommonLayersLosses#Networking#MachineLearning#CheatSheet#SkillVeris