PyTorch Tensors & Autograd
Part of the Machine Learning Roadmap
In short
PyTorch is the library most AI research and many production models are written in. Learn tensors, autograd and the five-line training loop, and you can read most model code out there.
PyTorch tensors are like NumPy arrays that can run on a GPU and track operations. Autograd records those operations and computes gradients automatically when you call .backward(), which is what makes training possible.

Think of it like this
The kirana store's khata
The kirana uncle writes every sale in his khata book. At month end, he can trace exactly which items led to the total you owe. PyTorch keeps a similar record of every operation on a tensor, so it can trace back and work out gradients.
Watch it happen
The training loop
1/5Clear the gradients from the last step, because PyTorch adds new ones on top of old ones.
The big ideas
What you need to know
Tensors
A tensor is a NumPy-like array that can live on a GPU and track gradients. Shapes like (batch, features) matter everywhere.
Autograd
Set requires_grad=True, do maths, call .backward(). PyTorch fills in .grad for you.
nn.Module
Models are classes built from layers like nn.Linear. model.parameters() hands every weight to the optimiser.
DataLoader
It batches and shuffles your data so the loop gets a fresh mini-batch on every step.
See it in code
Walk through the code
1import torch2import torch.nn as nn3 4X = torch.linspace(0, 1, 100).unsqueeze(1) # 100 rows, 1 feature5y = 3 * X + 1 + 0.05 * torch.randn_like(X) # true line: y = 3x + 16 7model = nn.Linear(1, 1)8opt = torch.optim.SGD(model.parameters(), lr=0.5)9 10for epoch in range(200):11 opt.zero_grad() # clear old gradients12 loss = nn.functional.mse_loss(model(X), y) # forward pass + loss13 loss.backward() # autograd fills .grad14 opt.step() # update weight and bias15 16print(model.weight.item(), model.bias.item()) # close to 3 and 11/3Make 100 points on the line y = 3x + 1 with a little noise. unsqueeze makes the shape (100, 1).
A model with one weight and one bias learns a straight line. Bigger models use the exact same loop.
Save yourself the pain
Common mistakes
Oops
Mixing up shapes, like passing a (100,) tensor where (100, 1) is expected.
Do this instead
Print .shape often. Most PyTorch errors in your first month are shape mismatches.
Oops
Running evaluation with gradients on.
Do this instead
Wrap evaluation in torch.no_grad() and call model.eval(). It saves memory and switches off training-only layers.
Your mission
Your first training loop
Quick check
What does loss.backward() do in PyTorch?
Go deeper
- 1Create tensors, move them to a GPU with .to(device) and do basic maths.
- 2Set requires_grad=True on a tensor, compute y = x squared and call backward().
- 3Follow the official PyTorch 'Learn the Basics' tutorial.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.