Linear Classification

Dive into Deep Learning · §3

Linear Neural Networks for Classification: From Logits to Trusted Decisions
one-hot labels · softmax · cross-entropy · Fashion-MNIST · calibration · generalization · distribution shift

Why Classification Needs Its Own Loss and Diagnostics

The Central Challenge

Predicting an unordered discrete category (y \in \{1, \ldots, q\}) departs from continuous regression in three fundamental ways:

  • Arbitrary integer codes distort geometry: Numbering ten garments 1 through 10 under squared loss penalizes T-shirt (1) \to boot (10) 81\times more than T-shirt (1) \to trouser (2).
  • Raw accuracy hides asymmetric failures: Under 1\% disease prevalence, always predicting healthy scores 99\% accuracy while detecting 0\% of sick patients.
  • Test sets vouch only for their own distribution: A linear model at 85\% source accuracy can collapse to \sim 50\% under deployment shift.

From Regression to Classification: What Changes

The Modelling Recipe

Our regression training pipeline—minibatch loaders, vectorized GEMM, autodiff, and SGD—carries over with three targeted upgrades:

  • One-hot target & multi-logit head: Replace scalar y \in \mathbb{R} and \hat{y} with one-hot \mathbf{y} \in \{0, 1\}^q and q affine logits \mathbf{o} = \mathbf{W}^\top\mathbf{x} + \mathbf{b} normalized via \hat{\mathbf{y}} = \mathrm{softmax}(\mathbf{o}).
  • Cross-entropy likelihood loss: Replace Gaussian squared error with categorical negative log-likelihood l(\mathbf{y}, \hat{\mathbf{y}}) = -\sum_{j=1}^q y_j \log \hat{y}_j = -\log \hat{y}_y.
  • Identical residual gradient form: The logit gradient simplifies to \nabla_{\mathbf{o}}\, l = \hat{\mathbf{y}} - \mathbf{y} (prediction minus target).

Architecture and Roadmap of Chapter 3

Chapter Roadmap

The six sections progress from mathematical formulation and benchmark data to implementation, decision metrics, statistical guarantees, and deployment robustness:

  • Model & pipeline (§3.1–§3.3): Softmax probabilities, cross-entropy loss, Fashion-MNIST DataModule (60\text{k} + 10\text{k} garments), Classifier base class, 10\times 10 confusion matrices, and fused LogSumExp loss.
  • Decisions, bounds & shift (§3.4–§3.6): Precision/recall, Bayes cost thresholds, ECE calibration, test-set error bars (\le 0.5/\sqrt{n}), adaptive overfitting, VC dimension, and covariate/label shift correction.

Concrete Running Benchmarks Across Every Section

Empirical Anchors

Every concept in this chapter is anchored to concrete numbers and reproducible experiments:

  • Core model (§3.1–§3.3): 2\times 2 toy logits (1.0, 2.2, 0.3) \to (0.21, 0.69, 0.10) (0.37\text{ nats}); 7{,}850-parameter Fashion-MNIST classifier reaching \sim 83\% validation accuracy in 10 epochs.
  • Auditing & shift (§3.4–§3.6): 1\%-prevalence screening (90\% recall \to 15.4\% precision, \tau^* \approx 1\% at 100:1 cost); \pm 0.7\% normal vs. \pm 1.4\% Hoeffding radius at n=10\text{k}; importance weighting (\sim 55\% \to 95\%) and \mathbf{C}^{-1}\boldsymbol{\mu} label-shift recovery.

Concept Check & Chapter Study Guide

Self-Assessment

Before diving into §3.1, test your intuition on three foundational questions that motivate the chapter:

  1. Output count: Why does a 10-class linear classifier on 28 \times 28 images require 7{,}850 parameters instead of the 785 parameters used in linear regression?
  2. Numerical range: In single-precision floating point (float32), why does evaluating \exp(1000) / (\exp(1000) + \exp(0) + \exp(0)) return NaN even though the mathematical ratio is 1?
  3. Evaluation precision: If two models differ by 0.3 percentage points on a 10{,}000-image test set around 83\% accuracy, is that gap statistically distinguishable from sampling noise?

Answers: (1) Each of the q = 10 classes needs its own 784-pixel weight vector \mathbf{w}_j and bias b_j to score class j against all others (784 \times 10 + 10 = 7{,}850). (2) float32 overflows to inf around \exp(88.7) \approx 3.4 \times 10^{38}, turning the ratio into \infty / \infty = \texttt{NaN}; subtracting \max_k o_k = 1000 first keeps all exponents \le 0. (3) No: at \epsilon = 0.17 and n = 10{,}000, a single model’s standard error is \sqrt{0.17 \times 0.83 / 10{,}000} \approx 0.38 percentage points (95\% radius \pm 0.74 points).