Bot TutorialsGrok BotChatGPTClaudeGeminiCopilotPerplexityMore botsGuidesArticlesPapers
Topics
CookingHome automationFitnessCreative writingMusic productionCodingIndustrialPros (Professional track)
Search

Part 6 of 12

PyTorch on the CPU: First Steps with a Bot

GuideBy Bot Tutorials · Published · Facts last verified Nov 3, 2026

ML libraries series complete. "Review Your Training Code with a Bot: Checklist" was the finale. See every part in the series hub.

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.

Video: PyTorch on the CPU: First Steps with a Bot (5:00). Narrated from this guide, with the real code and real output we ran. Captions included (caption file). Prefer reading? Everything in the video is in the guide below.

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:

It can't:

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 training loop, line by line

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

Words you'll see

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

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.

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

  1. PyTorch: Start Locally
  2. PyTorch Tutorials: Quickstart
  3. PyTorch docs: Reproducibility
  4. PyTorch docs: Serialization semantics
  5. PyTorch docs: torch.load

More in this series

  1. Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
  2. Part 2: How Much Memory Does a Model Need? A Data Brief · Data brief
  3. Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
  4. Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet · Cheat sheet
  5. Part 5: Your First scikit-learn Model, Built with a Bot · Walkthrough
  6. Part 6: PyTorch on the CPU: First Steps with a Bot (you are here) · Guide
  7. Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
  8. Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
  9. Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
  10. Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
  11. Part 11: Read ML Error Messages and Profile Code: Cheat Sheet · Cheat sheet
  12. 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.