PyTorch
PyTorch is an open-source Python framework for building, training, and running machine learning models. It can run calculations on a CPU or GPU and automatically compute the gradients used to update a model.
Also known as: TensorFlow
What PyTorch does
PyTorch gives you the building blocks for writing machine learning models in Python. You can define a neural network, measure how far its predictions are from the expected answers, and update its weights. PyTorch provides the numerical operations and derivative calculations, but you still choose the model, data, and training procedure. TensorFlow is a separate framework with a similar purpose, not another name for PyTorch.
From a batch to a weight update
PyTorch stores inputs and model parameters in tensors, which are arrays with any number of dimensions. A batch of 32 RGB images might have shape (32, 3, 224, 224): batch size, channels, height, and width. A DataLoader can assemble individual examples into batches, and an nn.Module holds the model's layers and defines how inputs pass through them.
During a forward pass, the model makes predictions and a loss function turns their errors into a single number. When gradient tracking is enabled, PyTorch's autograd system records the relevant operations as they run. Calling backward() on the loss works back through those operations to find how sensitive the loss is to each trainable parameter. An optimizer then uses those gradients to update the parameters.
Example: one training step
import torch
from torch import nn
model = nn.Linear(1, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
x = torch.tensor([[1.0], [2.0]])
target = torch.tensor([[3.0], [5.0]])
optimizer.zero_grad()
prediction = model(x)
loss = nn.functional.mse_loss(prediction, target)
loss.backward()
optimizer.step()
The model predicts one number per input using a weight and a bias. The loss averages the squared errors on the two examples. The final line makes one update; it does not fit the line in a single call. To learn the relationship y = 2x + 1, you would repeat the step.
Where it is used and what trips people up
Teams use PyTorch to train image classifiers, fine-tune language models, and run trained models on new data. When moving a model to a GPU, move the inputs there as well: for a typical forward pass, the model and inputs must be on the same device.
Evaluation mode and gradient tracking are separate controls. model.eval() changes how layers such as dropout and batch normalization behave. It does not turn off gradient tracking. For prediction without gradients, use a context such as torch.inference_mode() in addition to setting evaluation mode.
In interviews
A common question is why a training loop calls optimizer.zero_grad(). The key is that loss.backward() adds gradients to the existing .grad buffers rather than replacing them, while optimizer.step() updates the weights without clearing those buffers. Usually you clear gradients before each independent update. If you deliberately accumulate gradients across several smaller batches, you clear them before the group and update the weights after the last batch.