Linear Regression

Dive into Deep Learning · §2

Linear Neural Networks for Regression
affine models · squared loss · normal equations · minibatch SGD · generalization · weight decay

Linear Regression Is Deep Learning in Miniature

The Proving Ground

  • Parameterized forward pass and loss: Predict via \hat{y} = \mathbf{w}^\top \mathbf{x} + b and score misfit via L(\mathbf{w}, b) = \frac{1}{2n}\|\mathbf{X}\mathbf{w} + b - \mathbf{y}\|_2^2, grounded in Gaussian maximum likelihood.
  • Exact closed form vs. scalable iteration: The normal equations \mathbf{X}^\top\mathbf{X}\mathbf{w}^* = \mathbf{X}^\top\mathbf{y} project \mathbf{y} orthogonally in O(nd^2 + d^3), whereas minibatch SGD updates in O(|\mathcal{B}|d) per step.
  • Two recurring failure modes: A silent shape bug ((32,) - (32, 1) \to 32 \times 32) still lowers loss, and a high-degree polynomial that interpolates 20 noisy points fails on new data.

From First Principles to a Reusable Training Pipeline

Architectural Roadmap

  • Theory & Synthetic Oracle: Formulate the affine model, squared loss, normal equations, and minibatch SGD, then verify against known ground truth (\mathbf{w}^*, b^*) and noise floor \sigma^2/2.
  • Three Implementation Tiers: Build the tensor autograd loop from scratch, decouple into Module / DataModule / Trainer, and swap in the framework’s dense layers and optimizers.
  • Generalization & Weight Decay: Diagnose underfitting vs. overfitting via bias–variance and K-fold CV, and damp collinear noise with \ell_2 ridge shrinkage.

Concrete Running Benchmarks Across the Chapter

Quantitative Anchors

  • Projection & Synthetic Testbed: The 3-point line (0,1),(1,1),(2,0) yields \hat{\mathbf{w}} = (7/6, -1/2)^\top with \mathbf{X}^\top\mathbf{r} = \mathbf{0}; on n = 1{,}000 examples, with \mathbf{w}^* = (2, -3.4)^\top, b^* = 4.2, \sigma = 0.01, loss drops from 16.6 to \sigma^2/2 = 5 \times 10^{-5}.
  • Polynomial Curve Fitting (n = 20): Noisy samples of a cubic (\sigma = 0.1): degree 1 underfits, degree 3 hits \sigma^2 = 0.01, degree 19 explodes.
  • High-Dimensional Ridge (d = 200, n = 300): The prior penalty \lambda = \sigma^2/(n\tau^2) = 1/3 shrinks the effective parameters to \operatorname{df} \approx 127.

The Complete Supervised Learning Loop

End-to-End Perspective

Every model in the rest of this book—from multilayer perceptrons to Transformers—reuses the exact pipeline we construct in this chapter:

  • Forward & loss (Module): Map minibatch inputs \mathbf{X} \in \mathbb{R}^{|\mathcal{B}| \times d} to predictions \hat{\mathbf{y}} and evaluate the penalized empirical loss L_{\mathcal{B}}(\mathbf{w}, b) + \frac{\lambda}{2}\|\mathbf{w}\|_2^2.
  • Backward & update (Trainer): Differentiate L_{\mathcal{B}} via reverse-mode autograd and step parameters via (\mathbf{w}, b) \leftarrow (\mathbf{w}, b) - \eta \nabla L_{\mathcal{B}}.
  • Validation & selection (DataModule): Track held-out risk after every epoch to tune hyperparameters (\eta, |\mathcal{B}|, \lambda) without leaking test data.

Viewed as a neural network, linear regression is a single fully connected layer with d inputs and 1 linear output neuron.