Every chapter of this book leaned on PyTorch; this appendix is the foundation course. If you worked through chapters 2–7 first, read it as a “now I see why” tour — each concept here has already earned its keep somewhere in the GPT.
A.1 What is PyTorch?
Three components, one library:
A.2 Understanding tensors
A tensor tensor PyTorch's n-dimensional array (NumPy-like), with GPU acceleration and autograd integration. defined in ch. A — open in glossary generalizes the number → list → grid progression to any number of dimensions, with a NumPy-like API:
2 |
3 | 1 | 3 |
3 | 5 | 1 | 2 |
1 | 7 | 2 | 3 |
3 | 3 | 4 | 9 |
A.3 Seeing models as computation graphs
A computation graph computation graph The directed graph of operations PyTorch records during the forward pass; gradients flow back through it during backpropagation. defined in ch. A — open in glossary is a directed graph that expresses a sequence of calculations. PyTorch’s autograd autograd PyTorch's automatic differentiation engine — computes gradients of tensor operations automatically. defined in ch. A — open in glossary system builds one in the background during every forward pass — and that recorded structure is what lets it compute gradients for backpropagation backpropagation The chain rule applied over the computation graph to obtain the loss's gradient with respect to every parameter. defined in ch. A — open in glossary , the main training algorithm for neural networks. The book’s example graph is a one-neuron logistic classifier:
A.4 Automatic differentiation made easy
Backpropagation can be thought of as an implementation of the chain rule from calculus for neural networks. Two definitions first:
| symbol | meaning |
|---|---|
| a partial derivative partial derivative A function's rate of change with respect to one of its variables. defined in ch. A — open in glossary — how fast the loss changes as ONE variable () changes | |
| the product chain | the chain rule: multiply local derivatives along the graph path from the loss back to |
| the gradient gradient The vector of all partial derivatives of a multivariate function — the direction gradient descent follows to reduce the loss. defined in ch. A — open in glossary — the vector of ALL partial derivatives; exactly what gradient descent needs to update each parameter to minimize the loss |
Follow the gradients flowing backward through the graph:
Forward pass — the graph gets recorded
Because w₁ and b were created with requires_grad=True, every operation touching them adds a node: ×, +, σ, loss. The graph is DYNAMIC — rebuilt fresh each forward pass, which is why ordinary Python control flow works inside models.
Backpropagation over the one-neuron graph: the forward pass records the operations; the backward pass multiplies local derivatives along each path (shown symbolically — the code below computes the actual numbers).
import torch.nn.functional as F
from torch.autograd import grad
y = torch.tensor([1.0])
x1 = torch.tensor([1.1])
w1 = torch.tensor([2.2], requires_grad=True) #A
b = torch.tensor([0.0], requires_grad=True)
z = x1 * w1 + b
a = torch.sigmoid(z)
loss = F.binary_cross_entropy(a, y)
# Way 1: explicit, per-tensor
grad_L_w1 = grad(loss, w1, retain_graph=True) #B
grad_L_b = grad(loss, b, retain_graph=True)
# Way 2: the high-level workhorse
loss.backward() #C
print(w1.grad)
print(b.grad) - #A requires_grad=True is the opt-in: from here on, autograd records every operation involving this tensor. (The same flag ch3 set to False on its demo weights to reduce output clutter.)
- #B grad(loss, w1) returns ∂loss/∂w1 explicitly. retain_graph=True keeps the recorded graph alive so a second grad() call can reuse it.
- #C backward() traverses the whole graph once and stores every parameter's gradient in .grad — exactly what optimizer.step() reads afterward. This pairing IS the ch5/ch6/ch7 training loop's engine room.
Key idea — you write the forward pass, autograd writes the backward
Deep learning frameworks earn their keep here: define any composition of tensor operations, and the recorded computation graph plus the chain rule yield every parameter’s gradient automatically. The 12-block GPT of ch4 is just a much bigger graph — same mechanism, same one-lineloss.backward().
Next: assembling real multilayer networks and the canonical training
loop.📝 Check yourself: PyTorch fundamentals
0 / 51.What are PyTorch's three core components?
2.In tensor terminology, what are a scalar, a vector, and a matrix?
3.What is a computation graph, and why does PyTorch build one?
4.For the graph z = x1·w1 + b, a = σ(z), loss = L(a, y): how does the chain rule produce ∂loss/∂w1?
5.What's the difference between torch.autograd.grad(loss, w1) and loss.backward()?