Why move from NumPy to PyTorch?
In Lesson 3, you hand-coded forward passes, loss gradients, and weight updates in raw NumPy. You understood every multiplication and every chain-rule step. That was the point: build intuition.
But hand-coded backprop doesn't scale. A real transformer has 100+ million parameters. Writing gradient formulas for every layer by hand is impossible. PyTorch solves this with autograd — it records every operation on your tensors and computes all gradients automatically with a single .backward() call.
In Lesson 3 you wrote ~80 lines of NumPy to train a 2-layer network on the moons dataset. Today you'll rebuild that exact network in PyTorch — same architecture, same dataset, same accuracy — in under 30 lines. The difference? PyTorch handles gradient computation, weight initialization, and optimization for you.
PyTorch is also the framework behind GPT, LLaMA, Stable Diffusion, and nearly every LLM you'll build in this track. Learning it now means every future lesson — tokenizers, transformers, training loops — speaks the same language.
Tensors: NumPy arrays with superpowers
A PyTorch tensor is the direct equivalent of a NumPy array. Same shape, same dtypes, same broadcasting rules. The difference: tensors can track gradients and run on GPUs.
Notice: torch.from_numpy() shares memory with the original array. Change one, the other changes too. Use torch.tensor(np_array) if you want an independent copy.
NumPy vs PyTorch: side-by-side
Nearly every NumPy operation has a PyTorch equivalent. The API is deliberately similar:
| Operation | NumPy | PyTorch |
|---|---|---|
| Create array | np.array([1, 2, 3]) | torch.tensor([1, 2, 3]) |
| Zeros | np.zeros((3, 4)) | torch.zeros(3, 4) |
| Random | np.random.randn(3, 4) | torch.randn(3, 4) |
| Dot product | np.dot(a, b) | torch.dot(a, b) |
| Matrix multiply | a @ b / np.matmul(a, b) | a @ b / torch.matmul(a, b) |
| Reshape | a.reshape(2, 3) | a.reshape(2, 3) / a.view(2, 3) |
| Sum | np.sum(a, axis=0) | torch.sum(a, dim=0) |
| Backprop | Manual chain rule | loss.backward() (automatic) |
| GPU | Not supported | a.to('cuda') |
Autograd: automatic differentiation
In Lesson 3 you computed gradients by hand: derive the formula for dL/dW, implement it in code, hope you got the math right. PyTorch's autograd does this automatically. Set requires_grad=True on a tensor, do math with it, then call .backward(). PyTorch traces every operation and computes all gradients.
The derivative of x² + 3x + 1 is 2x + 3. At x=2, that's 7. PyTorch computed this automatically by tracing the operations x**2, 3*x, and +1, then applying the chain rule backward through that graph.
The computational graph
PyTorch builds a directed acyclic graph (DAG) of every operation. Each node stores the operation and a reference to its inputs. When you call .backward(), PyTorch walks this graph in reverse and accumulates gradients:
This is exactly the chain rule you implemented manually in Lesson 3 — but PyTorch builds and walks this graph for you, no matter how deep your network is. A 100-layer transformer? Same .backward() call.
loss.backward(). The math is the same; the bookkeeping is automated.
nn.Module: building blocks for networks
In NumPy, you initialized weights with np.random.randn() and wrote X @ W + b for every layer. PyTorch wraps this into nn.Linear — a single line that creates the weight matrix, bias vector, and forward computation.
To build a model, you subclass nn.Module and define two things:
__init__— create your layers (nn.Linear, nn.ReLU, etc.)forward— wire them together (data flows in, prediction comes out)
That's it. PyTorch handles weight tracking, gradient computation, and device management. Your nn.Module knows all its parameters because nn.Linear registers them automatically.
Loss functions and optimizers
In Lesson 3 you wrote MSE loss by hand: np.mean((predictions - targets)**2). PyTorch provides nn.MSELoss(), nn.CrossEntropyLoss(), and dozens more. Similarly, you implemented SGD manually — adjusting each weight by w -= lr * grad. PyTorch's torch.optim.SGD does this for all parameters at once, and supports advanced optimizers like Adam that adapt learning rates per parameter.
Full model: moons dataset in PyTorch
Let's rebuild the exact same neural network from Lesson 3. Same moons dataset, same architecture (2 -> 16 -> 8 -> 1), same goal. Watch how the code shrinks:
Compare this to Lesson 3's NumPy version: no manual gradient math, no hand-coded weight updates, no chain-rule derivations. The training loop is 6 lines. The model definition is 10 lines. PyTorch does the rest.
Breaking down the training loop
output = model(X)— forward pass through all layersloss = criterion(output, y)— compute MSE lossoptimizer.zero_grad()— clear old gradients (PyTorch accumulates by default)loss.backward()— compute all gradients via autogradoptimizer.step()— update all weights using SGD
.backward() call adds to existing .grad values rather than replacing them. This is useful for gradient accumulation across mini-batches, but in a standard loop you must zero them first. Forgetting optimizer.zero_grad() is the most common PyTorch bug.
Parameter count: 193
Where do 193 parameters come from?
- Layer 1: nn.Linear(2, 16) = 2*16 weights + 16 biases = 48
- Layer 2: nn.Linear(16, 8) = 16*8 weights + 8 biases = 136
- Layer 3: nn.Linear(8, 1) = 8*1 weights + 1 bias = 9
- Total: 48 + 136 + 9 = 193
GPT-2 has 117 million. GPT-3 has 175 billion. Same building blocks — just many more layers and wider hidden dimensions.
NumPy vs PyTorch: the same model side-by-side
Here's the core difference. In NumPy (Lesson 3), your forward pass and backprop looked like this:
In PyTorch, the equivalent is:
18 lines of manual calculus reduced to 5. And the PyTorch version works for any architecture — add 50 more layers and the training loop stays the same 5 lines.
GPU basics: moving tensors to CUDA
GPUs are why deep learning took off. A GPU can multiply thousands of matrix elements simultaneously. PyTorch makes GPU usage trivial — move your model and data to the GPU with .to('cuda'):
The entire training loop is identical — you just moved the data and model to the GPU. PyTorch handles all the GPU memory management, kernel launches, and synchronization. For a 193-parameter model, the GPU won't help much. But when you're training a transformer with millions of parameters on thousands of text samples, GPU acceleration is the difference between hours and weeks.
RuntimeError. Always move both model and data with .to(device).
Why no interactive editor?
pip install torch torchvision scikit-learn
Then paste any code block into a
.py file and run it with python your_file.py. Google Colab also works — it gives you a free GPU.
Why this matters for building an LLM
Every component you'll build in the rest of this track — tokenizers, embedding layers, attention mechanisms, transformer blocks, the full GPT architecture — uses PyTorch. The patterns you learned today are the foundation:
Next lesson: you'll use PyTorch to convert raw text into numbers — tokenization — the first step of every language model.
Challenge
Use sklearn.datasets.load_iris() to load the Iris dataset (4 features, 3 classes). Build a PyTorch model with two hidden layers (4 -> 32 -> 16 -> 3). Use nn.CrossEntropyLoss instead of MSELoss (it handles multi-class targets). Train for 500 epochs and report accuracy.
torch.long dtype, not float. Use torch.argmax(output, dim=1) to get predicted class indices.Take the moons model from this lesson and add a learning rate scheduler. Use torch.optim.lr_scheduler.StepLR(optimizer, step_size=200, gamma=0.5) to halve the learning rate every 200 epochs. Compare final loss with and without the scheduler. Print the learning rate at each milestone.
scheduler.step() after optimizer.step() in the training loop. Access the current learning rate with optimizer.param_groups[0]['lr']. You should see the loss curve change shape — faster initial descent, then finer convergence as the rate drops.Quiz
requires_grad=True do when creating a PyTorch tensor?optimizer.zero_grad() before loss.backward() in a training loop?nn.Linear(16, 8) have?.to('cuda')) but your input data is still on CPU?You've bridged NumPy to PyTorch.
Now build the models that power ChatGPT.
Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Agentic Engineer →