Languages

PyTorch Cheat Sheet

Print-ready cheat sheet for PyTorch, organized for fast lookup, study, and desk-side use

Source
PyTorch Documentation (pytorch.org)
License
MIT
Updated
2026-03-27

Sample

What's in this cheat sheet

  • Tensors
  • Autograd
  • Neural Networks
  • Data Loading
  • Training Loop
  • Optimizers
  • Loss Functions
  • Saving & Loading
  • GPU
  • Common Patterns

Creating Tensors

import torch
a = torch.tensor([1, 2, 3])
b = torch.zeros(2, 3)
c = torch.ones(3, 3)
d = torch.randn(2, 4)  # normal dist

Tensor Constructors

torch.zeros(m, n)All zeros, shape (m, n)
torch.ones(m, n)All ones, shape (m, n)
torch.randn(m, n)Standard normal random
torch.arange(start, end, step)Evenly spaced values
torch.linspace(start, end, steps)Fixed number of points
torch.eye(n)Identity matrix
torch.empty(m, n)Uninitialized memory

NumPy Interop

t = torch.from_numpy(np_array)
arr = tensor.numpy()  # shares memory
t = torch.as_tensor(np_array)

Download the full cheat sheet for all sections, formatted for print

Download PDF

Downloads

Choose the treatment that matches how you read