TensorFlow showed one way to train a model, with layers stacked and compiled. PyTorch, released by Meta's AI research lab in 2016, takes a more hands-on approach: you write ordinary Python, tensors track their own gradients, and the training loop is a loop you can read, print from, and change. That openness made it the favourite of researchers, and it is now just as common in production, from image models to language models.
Tensors, autograd, and the training loop
PyTorch rests on a few distinctive ideas:
- Tensors that feel like arrays: a tensor is a multi-dimensional array, like a NumPy array, with the added ability to run on a graphics card for speed.
- Autograd: operations on tensors remember how they were built, so calling backward fills in every gradient automatically instead of asking you to derive them by hand.
- Modules, composed freely: a model is a module, layers are modules inside it, and you assemble them with plain Python control flow, including loops and conditions.
- A training loop you own: zero the gradients, run the model, measure the loss, step the optimiser, and repeat, with every step visible in your own code.
A tiny example shows the shape, a model that learns the line y = 2x + 1 from four examples:
import torch
import torch.nn as nn
xs = torch.tensor([[0.0], [1.0], [2.0], [3.0]])
ys = torch.tensor([[1.0], [3.0], [5.0], [7.0]])
model = nn.Linear(1, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
loss_fn = nn.MSELoss()
for _ in range(200):
optimizer.zero_grad()
loss = loss_fn(model(xs), ys)
loss.backward()
optimizer.step()
print(model(torch.tensor([[4.0]])))
nn.Linear(1, 1) creates one input neuron connected to one output, zero_grad clears the previous step's gradients so they do not pile up, loss.backward computes fresh gradients, and step nudges the weights a little in the direction that lowers the loss. After 200 passes, predicting for 4.0 lands near 9.0, not exactly, because training improves gradually rather than solving the equation outright.
The honest trade
PyTorch trades hand-holding for control: the training loop is yours to write, which means yours to debug, and moving a model into production takes extra tooling compared with a more packaged framework. When to reach for it: when you want to understand, customise, or research a neural network; a higher-level library when the model is standard and speed of setup matters more than control.
Tensors, trained in the open
PyTorch is machine learning you can watch learn: tensors, automatic gradients, and a training loop written out in plain Python. It trades convenience for control over every step, and for anyone who wants to see how a model actually improves, that openness is the point.
Originally published at https://practicaldevblog.blogspot.com/2026/11/pytorch-explained-machine-learning-you.html
Top comments (0)