The framework that powers modern machine learning
PyTorch is a framework — a collection of pre-built tools and functions — for building and training neural networks: programs that learn patterns from data.
Used by most researchers and increasingly in production apps
The main alternative was TensorFlow (Google) — PyTorch won the ecosystem war
Python-based, so it fits naturally into data science workflows
A tensor is just a container of numbers arranged in a grid. More dimensions = more axes in the grid. That's it.
Think of it like a spreadsheet: a single cell is a scalar, a row is a vector, the whole sheet is a matrix, and a workbook with multiple sheets is a 3D tensor.
The size along each dimension — how many rows, columns, layers, etc.
The type of number stored — whole numbers, decimals, or decimals with fewer digits (which use less memory).
Where the tensor lives in the computer — on the CPU (main processor) or on the GPU (graphics card).
A CPU has a few very powerful cores that do tasks one-by-one.
A GPU has thousands of small cores that do many simple tasks at the same time.
Great at complicated, sequential work
Great at simple math on huge arrays of numbers
ML training is mostly multiplying enormous grids of numbers together — exactly the kind of simple, repetitive math that thousands of GPU cores can split up and do all at once.
A neural network is full of numbers called weights. Training means adjusting those weights so the network's predictions get more accurate.
But which direction should each weight change? And by how much?
The answer is gradients — a gradient is a number that tells you: "if you increase this weight slightly, how much does the error change?"
Autograd (short for "automatic gradients") is PyTorch's system that computes all of these gradients for you, automatically, no matter how complex your network is.
.backward(), it walks backwards through that map and computes every gradientImagine hiking blindfolded on a hilly landscape. The gradient is like feeling the slope under your feet — it tells you which direction is downhill so you can walk toward the lowest valley (the smallest error).
The gradient tells you the direction of steepest increase in error. To reduce error, you go the opposite direction.
Controls how big a step you take each time you adjust the weights.
Every neural network learns through the same cycle. Each pass through this loop makes the model a little more accurate.
model(batch) — runs the forward pass, producing predictionsloss_fn(...) — measures the error as a single number.backward() — tells autograd to compute all gradients.step() — adjusts weights using those gradients.zero_grad() — clears old gradients so they don't pile up from the previous round
nn.Module is the base template that every neural network component in PyTorch is built from. When you create a model, you're defining a class that inherits from it.
It gives you two things:
The weights that live inside the model — the numbers that get adjusted during training. nn.Module keeps track of all of them automatically.
A method you write that defines what happens when data passes through this component — which math operations to run, in what order.
nn.Linear(10, 64) is a layer that takes 10 input numbers and produces 64 output numbers. F.relu is an activation function — it zeroes out any negative values, which helps the network learn non-obvious patterns. Without it, stacking layers would be no better than a single layer.
You rarely send your entire dataset through the model at once — you'd run out of memory. Instead, you split it into smaller groups called batches.
The DataLoader handles this automatically: it divides your data into batches, shuffles the order each time (so the model doesn't memorize the sequence), and loads data in parallel to keep the GPU busy.
Common batch sizes: 16, 32, 64, 128. You usually pick the largest one that fits in your GPU's memory.
optimizer.zero_grad() before loss.backward()
.to(device)
model.train() before training and model.eval() before predicting
Tensors are containers of numbers arranged in grids — they live on a device (CPU or GPU) and have a shape and number type.
Autograd automatically computes gradients — numbers that tell you how to adjust each weight to reduce error.
The training loop is four steps on repeat: predict → measure error → compute gradients → update weights.
nn.Module is the template for every model component — it holds weights and defines what happens when data flows through.
DataLoader splits data into batches, shuffles them, and feeds them to the model efficiently.