3  Linear Classification

In Chapter 2, our goal was to predict a continuous quantity such as the sale price of a house. Many tasks ask for a discrete category instead: an online catalog photo depicts a shirt, a coat, or a sandal, never “5.5” of anything. Simply numbering the categories 1 through 10 and running linear regression fails badly, because it imposes an arbitrary ordering as though a pullover lay halfway between a pair of trousers and a dress, while leaving the model unable to express calibrated uncertainty (“most likely a shirt, possibly a pullover”).

Happily, almost all of our regression training machinery—minibatch data loaders, vectorized forward passes, automatic differentiation, and minibatch SGD—carries straight over to classification. Only three ingredients change: we represent unordered categories via one-hot vectors, compute one linear score (logit) per category and normalize them via the softmax function into a probability distribution, and replace squared error with the cross-entropy loss (derived from maximum likelihood under a categorical label model, just as squared error arose from Gaussian noise). Nearly every neural classifier in this book, right up to large language models, ends in this softmax layer and trains under this loss.

We first formulate softmax regression and the cross-entropy loss, derive its residual gradient \(\hat{\mathbf{y}} - \mathbf{y}\), and connect it to Shannon’s information theory (Section 3.1). We then build a reusable FashionMNIST image pipeline (Section 3.2) and implement softmax regression both from scratch and concisely (Section 3.3), diagnosing errors with confusion matrices and learned \(28 \times 28\) templates while curing floating-point overflow and underflow with a fused log-sum-exp loss.

Where classification departs sharply from regression is in evaluation and deployment, which occupy the second half of the chapter. Raw accuracy misleads whenever positive examples are rare or mistakes carry unequal costs, motivating precision, recall, cost-sensitive thresholds, and calibration diagnostics (Section 3.4). Even on balanced classes, a finite test set carries statistical error bars that erode under repeated reuse, leading us to Hoeffding bounds, adaptive overfitting, and Vapnik–Chervonenkis (VC) theory (Section 3.5). Finally, Section 3.6 asks what happens when deployment data drift away from the training distribution, developing importance-weighted covariate-shift correction, confusion-matrix label-shift estimation, and defenses against runaway feedback loops.