By the end
- Describe a forward pass and a loss.
- Use `requires_grad`, `backward`, and `.grad` on a tiny example.
- Apply one gradient-descent update by hand and in PyTorch.
- Distinguish gradient calculation from parameter updating.
Week 2 · Hands-on foundations
How does a model know which way to change its parameters?
The current course spends substantial time on PyTorch autograd, computation graphs, optimization, and backpropagation. Those ideas remain essential. The lower-intensity path follows one scalar parameter from prediction to loss, gradient, and update before introducing larger networks.
This chapter condenses materials taught in the 2023–2025 Advanced Data Science course into a smaller core path. Optional depth remains available in the companion notebook.
Use the explorer and three short Python examples before changing a longer model.
Derive and verify the gradient of `(2*w + 1 - target)**2`.
Core ideas
PyTorch records operations connecting tensors that require gradients. Backward traversal applies the chain rule to determine how the final loss changes with each leaf parameter.
A gradient is a local sensitivity. Its sign gives a direction of increase; its magnitude gives the local rate of change. It is information, not the update itself.
Gradient descent multiplies the gradient by a chosen step size. A very small rate moves slowly; a very large one can cross the minimum repeatedly or diverge.
Mathematical intuition
The learning rate η converts local sensitivity into an update. The minus sign moves against the direction in which the loss increases.
Identify the input, learned quantity, output, and aggregation. Re-create the relationship with a tiny hand-checkable example before using a library layer.
Interactive explorer
Predict the direction of change before moving a control.
Change a control to inspect the relationship.
Hands-on path
Minimal Python
Each example is independent. Explain its output before copying it into a larger workflow.
import torch
w = torch.tensor(3.0, requires_grad=True)
loss = (w - 1) ** 2
loss.backward()
print(loss.item(), w.grad.item())Predict the result, then click Run.
w, target, rate = 3.0, 1.0, 0.2
grad = 2 * (w - target)
w = w - rate * grad
print(round(w, 2))Predict the result, then click Run.
for rate in [0.05, 0.2, 0.8]:
w = 3.0 - rate * 4.0
print(rate, round((w - 1) ** 2, 3))Predict the result, then click Run.
Failure checks
What goes wrong: Repeated backward calls add gradients when they were not reset.
Check: Print the gradient before and after zeroing.
What goes wrong: Conversion to NumPy or `.item()` breaks the graph too early.
Check: Inspect `grad_fn` and keep tensor operations until reporting.
What goes wrong: Loss grows or oscillates.
Check: Compare several learning rates on the same starting state.
Check your understanding
Answer in your own words before opening each panel.
Standard answer: A strong answer connects the central idea to a visible computation and a held-out or shape-based check. The lower-intensity path follows one scalar parameter from prediction to loss, gradient, and update before introducing larger networks.
Standard answer: PyTorch records operations connecting tensors that require gradients. Backward traversal applies the chain rule to determine how the final loss changes with each leaf parameter.
Standard answer: A gradient is a local sensitivity. Its sign gives a direction of increase; its magnitude gives the local rate of change. It is information, not the update itself.
Standard answer: Gradient descent multiplies the gradient by a chosen step size. A very small rate moves slowly; a very large one can cross the minimum repeatedly or diverge.
Standard answer: The learning rate η converts local sensitivity into an update. The minus sign moves against the direction in which the loss increases. It does not by itself prove useful behavior on unseen intended-use cases.
Standard answer: The visible calculation and interpretation should change together. Predict the direction first, then use the result to correct your mental model.
Standard answer: Make a prediction with current parameters.
Standard answer: Print the gradient before and after zeroing.
Standard answer: Record data and split assumptions, input/output shapes, settings, the baseline, held-out metrics, representative failures, and the decision supported by that evidence.
Standard answer: Week 3 embeds the same update inside linear regression, where a weight and bias learn from several observations.
Terminology
Go further
Next: Week 3 embeds the same update inside linear regression, where a weight and bias learn from several observations.