7 routes42 sections324 units

Unit 2.4 · about 3 h

139 / 324

Softmax regression and one-versus-rest

Most real decisions choose among more than two outcomes. Softmax generalizes the sigmoid without breaking probability semantics, and one-versus-rest is the fallback every library reaches for when a solver only does binary.

  • derive
  • implement

You can now

  • Derive the softmax from \(K\) linear scores, its cross-entropy loss, and the prediction-error-times-input gradient.
  • Implement max-subtracted softmax and multiclass cross-entropy that stay finite on extreme logits.
  • Contrast joint multinomial fitting with one-versus-rest on parameter counts, calibration and solver limits.

Covers

  • Softmax regression and multiclass cross-entropy
  • One-versus-rest classification

Study, in this order

6 steps

  1. Softmax, vectorization caution and cross-entropy loss
    Dive into Deep Learning - linear models slice

    Section 4.1: The Softmax (ordering preservation, argmax needs no exponentials), Vectorization (row-wise softmax with the overflow-underflow caution), Loss Function with Log-Likelihood and Softmax and Cross-Entropy Loss

  2. Softmax from scratch, including the instability warning
    Dive into Deep Learning - linear models slice

    Section 4.4: The Softmax (naive implementation with the do-not-use-verbatim caution), The Model, The Cross-Entropy Loss, Training, Prediction

    Depth · Read the naive softmax as the failure demo. Use the framework softmax with built-in protections for every real fit.

  3. Multinomial logistic regression
    An Introduction to Statistical Learning with Python

    Chapter 4, Section 4.3.5 (p. 144): the \(K\)-class extension (4.13) with log odds linear in \(x\) (4.14)

  4. Multiclass logistic form and gradient
    Tom Mitchell - Generative and Discriminative Classifiers: Naive Bayes and Logistic Regression

    Section 3.4 (\(K - 1\) linear functions in equations 27-28, regularized gradient rule 29)

  5. Multinomial geometry, OvR solvers and calibration
    scikit-learn - linear models, discriminant analysis and naive Bayes

    Section 1.1.11.2 (deliberate \(K\)-vector overparameterization, non-unique unpenalized solutions) and 1.1.11.3 with its solver table and Solvers' details (liblinear cannot fit a multinomial model, so multiclass use wraps it in OneVsRestClassifier; the multinomial loss is expected to calibrate better than one-versus-rest)

  6. One-versus-rest in practice
    Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear models

    Book Chapter 3: Multiclass Classification (printed pp. 124-126). Notebook 03 cells: Multiclass Classification section: the SVC OvO-training note, OneVsRestClassifier with its 10 estimators, and the SGDClassifier multiclass run

Focus on

  • Softmax preserves order, so \(\operatorname{argmax}\) needs no exponentials. Probabilities still need the max-subtracted form, because \(\exp(1000)\) overflows to \(\infty\) and \(\infty\) divided by \(\infty\) is nan.
  • sklearn fits \(K\) vectors for \(K\) classes on purpose, for symmetry under regularization. Without penalty that redundancy makes the solution non-unique, which is one more reason the default \(L_2\) matters.
  • One-versus-rest trains \(K\) binary problems and compares uncalibrated margins, while multinomial optimizes one joint loss. Expect worse-calibrated probabilities from the OvR scheme.

Reproduce closed-book

Derive the \((p_k - y_k)x\) gradient from the multiclass negative log likelihood. In NumPy show naive softmax of [1000, 1001, 999] returns [nan, nan, nan] while the max-subtracted form returns [0.2447, 0.6652, 0.0900]. Then do the D2L 4.4 exercise in float32, the dtype of D2L's PyTorch code: naive softmax of [-110, -111] returns [nan, nan] because both exponentials underflow to 0, and [-100, -101] gives 0.7297 instead of 0.7311 from subnormal rounding. In float64 both inputs still work; the cliff sits near -745.

Traps

  • Copying the D2L 4.4 teaching softmax into real code. The page itself warns against verbatim use; framework implementations subtract the row maximum internally.
  • Calling OvR margins probabilities. One-versus-rest decision_function scores are not calibrated across the K classifiers; take predict_proba from a joint fit or calibrate afterwards.
  • Passing multi_class to LogisticRegression under sklearn 1.9. The parameter is gone and raises TypeError. Multiclass fits are multinomial under the default lbfgs, liblinear raises a ValueError on three or more classes, and one-versus-rest now means wrapping the estimator in OneVsRestClassifier.

Practice

D2L 4.4 Exercises: the sub-minus-100 softmax fix

Exercise 1: test softmax with an input of 100, then with the largest input below -100, then implement the fix relative to the largest entry

Write the max-subtracted version from a blank file, run both tests in float32 and float64, and match it against the framework softmax on random and extreme logits.

HoML 04 Exercise 12: batch gradient descent with early stopping for softmax regression

Exercise solutions, problem 12: NumPy-only batch gradient descent with early stopping on iris

Reuse your stable loss and stop on validation error, not training error.

Self-test

  1. Why can argmax skip the softmax entirely?

  2. sklearn fits 3 vectors for 3 classes while Mitchell fits \(K - 1 = 2\) linear functions. Is one of them wrong?

  3. Naive softmax of [1000, 1001, 999] returns nan. Show the exact failure and the fix.

  4. liblinear is your only allowed solver on a 5-class problem. Which scheme, and what do you lose?

Completion criterion

Stop when

Your max-subtracted softmax matches the framework on random and extreme logits, your multiclass gradient passes a numeric check, and you can state when OvR is forced and what it costs.

Mark passed, then open 2.5 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • An Introduction to Statistical Learning with Python
    James, Witten, Hastie, Tibshirani and Taylorprimarytheory
    Use Chapters 3, 4 and 6 with their Python labs as the first pass: least squares and its diagnostics, logistic regression, LDA, QDA and naive Bayes, basis expansion and regularization paths.
  • Dive into Deep Learning - linear models slice
    D2L authorsrequiredimplementation
    Use 3.1, 3.4, 3.6, 3.7, 4.1, 4.4, 22.7 and 22.9 for linear regression, generalization, weight decay, softmax, maximum likelihood and naive Bayes. Implement with NumPy first and use the PyTorch versions only as a differential reference.
  • scikit-learn - linear models, discriminant analysis and naive Bayes
    scikit-learnreferencereference
    Use the official Linear Models, LDA and QDA, and Naive Bayes guides to verify estimator semantics and selected edge cases after the independent implementation works.
  • Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear models
    Aurelien Geron (O'Reilly, 2025)supportimplementation
    Read the Chapter 3 and 4 sections each unit names, then run notebooks 03 and 04 for classification diagnostics, linear and polynomial regression, regularization and logistic regression. Recreate the target mechanism independently before inspecting worked exercise code.
  • Tom Mitchell - Generative and Discriminative Classifiers: Naive Bayes and Logistic Regression
    Tom Mitchell, Carnegie Mellon Universityrequiredtheory
    Read Sections 2 (naive Bayes, the smoothed estimates (7) and (9), parameter counts), 3 (Gaussian naive Bayes implies the logistic form, gradient ascent, the \(L_2\) penalty, the \(K\)-class rules) and 4 (generative versus discriminative, Ng and Jordan rates). This is the CS446 reading for Lectures 6-8; rework each derivation on a hand-sized dataset.

Also in this section

  • NeetCode ML - linear-model closing drill
    NeetCodesupportpractice
    After the section gate, solve Gradient Descent, Sigmoid and ReLU, Softmax, Cross-Entropy Loss, Linear Regression Forward and Linear Regression Training cold. Repeat only failed items from a blank file with changed values.
  • Deep-ML - linear and probabilistic model problems
    Deep-MLsupportpractice
    Use 15 Linear Regression Using Gradient Descent, 106 Train Logistic Regression with Gradient Descent and 140 Bernoulli Naive Bayes Classifier as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin. The local implementation remains the evidence.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels