Part 6 of 12
PyTorch on the CPU: First Steps with a Bot
GuideBy Bot Tutorials · Published · Facts last verified Nov 3, 2026
Part 6: install a CPU-only PyTorch wheel, check it, and train a tiny model with a real loss curve, then ask a bot to explain each line.
PyTorch is one of the most widely used deep learning libraries, and you don't need a GPU to start learning it. In this guide we install the CPU-only build, check it works, and train the smallest useful model: a straight line fitted to noisy points. A bot explains the code, and we compare what it says with PyTorch's docs.
What the bot can and can't do here
It can:
- Explain the training loop line by line.
- Spot a missing
zero_grad()or a wrong loss function. - Translate a NumPy idea into PyTorch.
It can't:
- Know which wheel suits your machine. Use the selector on PyTorch's Start Locally page.
- Guarantee identical numbers on another machine or version. PyTorch's Reproducibility notes say results aren't guaranteed across releases or platforms.
- Promise speed. Time it yourself.
Check first, scale second
Train something tiny and check the result is right before you train something big. A model that can't learn y = 2x + 1 has a bug.
Install the CPU build
On PyTorch's Start Locally page, choose your OS, Pip, Python and CPU, then run the command it shows. When we checked, the stable version was 2.15.0 and the page said the latest stable release requires Python 3.11 or later. Our box installed torch 2.15.0+cpu. The +cpu tag tells you it's the CPU build.
The page's own check is to make a random tensor, and to run torch.cuda.is_available() for GPU builds. On a CPU build that returns False, as our Part 1 report showed.
The script
import torch
torch.manual_seed(0)
x = torch.rand(5, 3)
print("tensor", tuple(x.shape), x.dtype, "device", x.device)
# Learn y = 2x + 1 from noisy points
X = torch.linspace(0, 1, 100).unsqueeze(1)
y = 2 * X + 1 + 0.05 * torch.randn_like(X)
model = torch.nn.Linear(1, 1)
opt = torch.optim.SGD(model.parameters(), lr=0.5)
loss_fn = torch.nn.MSELoss()
for step in range(201):
opt.zero_grad()
loss = loss_fn(model(X), y)
loss.backward()
opt.step()
if step % 50 == 0:
print(f"step {step:3d} loss {loss.item():.4f}")
model.eval()
with torch.no_grad():
print("weight", round(model.weight.item(), 2), "bias", round(model.bias.item(), 2))
Real output on our box:
tensor (5, 3) torch.float32 device cpu
step 0 loss 6.7915
step 50 loss 0.0027
step 100 loss 0.0027
step 150 loss 0.0027
step 200 loss 0.0027
weight 2.01 bias 1.0
What happened
- The tensor line confirms PyTorch works: a 5 by 3 tensor of
float32on the CPU. - The data is 100 points on the line
y = 2x + 1, with a little noise added. - The loss starts at 6.79 and drops to 0.0027 by step 50, then stays there. What's left is roughly the noise we added.
- The learned weight and bias are 2.01 and 1.0, close to the 2 and 1 we used to make the data. That's the check that the loop works.
The training loop, line by line
opt.zero_grad()clears old gradients. PyTorch adds gradients up by default, so forgetting this is a classic bug.loss = loss_fn(model(X), y)runs the model and measures the error with mean squared error.loss.backward()computes gradients.opt.step()updates the weight and bias.model.eval()andtorch.no_grad()switch to prediction mode and stop gradient tracking.
A good prompt:
Explain this PyTorch training loop line by line for a beginner. Then list three bugs a beginner might introduce, and what the loss would do in each case.
Ask the bot what happens without zero_grad(), then try it. Comparing its prediction with the real run is the best way to learn how much to trust it.
Seeds and reproducibility
torch.manual_seed(0) makes the random numbers repeatable. PyTorch's Reproducibility notes add that fully reproducible results aren't guaranteed across PyTorch releases, commits or platforms, and that some operations need extra settings to be deterministic. Record your versions with the model.
Saving and loading
PyTorch's Serialization semantics notes recommend saving a model's state_dict. The torch.load reference shows weights_only=True as the default in the version we checked, which limits what loading a file can do. Keep it that way for files you didn't create yourself.
Try next
- Change the learning rate to 5.0 and watch the loss. Ask the bot to predict what happens first.
- Follow PyTorch's official Quickstart tutorial, which trains a small image classifier.
Words you'll see
- Tensor: PyTorch's array type, which can live on a CPU or GPU and track gradients.
- Module: a model or layer, such as
nn.Linear. Its weights are its parameters. - Loss: a number that says how wrong the model is. Training tries to make it smaller.
- Optimizer: the object that updates parameters using gradients, such as SGD or Adam.
- Epoch and step: a pass over all the data, and a single update.
Moving to a GPU later
When you have a GPU build, the code above needs two changes: choose a device once and move the model and data to it with .to(device). Check torch.cuda.is_available() first, and run Part 1's report so you know the wheel matches your driver.
Tested vs documented
- Tested by us: the install, the script and its output, on PyTorch 2.15.0 CPU.
- Documented, not tested by us: reproducibility and serialization advice, from PyTorch's docs.
Where to go next
Tomorrow brings TensorFlow and Keras, JAX on the CPU, and reading Hugging Face model cards and licenses.
Safety, licensing and privacy: read this before you run anything
This series is about learning and small personal projects with machine learning libraries. A bot is a helper, not an authority.
- Check bot advice against official docs. Version numbers, install commands and compatibility tables change often. Compare every answer with the PyTorch, TensorFlow, JAX, scikit-learn, NVIDIA, Hugging Face or ONNX Runtime pages we link. If the bot and the docs disagree, the docs win.
- Work in a virtual environment. Don't install or remove packages in your system Python, and don't run
sudo pip. - Run untrusted files carefully. Pickle-based model files can run code when loaded. Only load models from sources you trust, and prefer safer formats like safetensors where the library supports them.
- Respect licenses. Datasets and model weights carry their own licenses and terms. Read the model card and license before you use, share or sell anything built on them. We don't give legal advice; ask a qualified person if it matters.
- Protect personal data. Don't paste private, customer or medical data into a bot or a training set unless you have the right to, and check the bot's own privacy settings first.
- No hardware claims you didn't measure. Treat any speed figure from a bot as a guess until you've timed it on your own machine.
- No medical, legal or financial decisions from a toy model. These guides are for learning only.
FAQ
Do I need a GPU to learn PyTorch?
No. PyTorch's install page has a CPU option, and small models train fine on a CPU. Our example trained in well under a second.
Why call model.eval() and torch.no_grad()?
eval() switches layers like dropout and batch norm to inference behaviour, and no_grad() stops PyTorch tracking gradients when you only want predictions.
Was this tested?
Yes. The loss values and learned weights shown are real output from PyTorch 2.15.0 CPU on our box.
Sources
- PyTorch: Start Locally
- PyTorch Tutorials: Quickstart
- PyTorch docs: Reproducibility
- PyTorch docs: Serialization semantics
- PyTorch docs: torch.load
More in this series
- Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
- Part 2: How Much Memory Does a Model Need? A Data Brief · Data brief
- Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
- Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
- Part 5: Your First scikit-learn Model, Built with a Bot · Walkthrough
- Part 6: PyTorch on the CPU: First Steps with a Bot (you are here) · Guide
- Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
- Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
- Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
- Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
- Part 11: Read ML Error Messages and Profile Code: Cheat Sheet · Cheat sheet
- Part 12: Review Your Training Code with a Bot: Checklist · Checklist
See the ML Libraries with a Bot series hub · Latest on Bot Tutorials
Bot advice can be wrong. We cite official docs; check them yourself before acting, especially for anything involving money, safety or your data.