MAP Estimation and Regularization
Maximum likelihood trusts the data completely and overfits small samples. Adding a prior over the parameters and maximizing the posterior instead gives the maximum a posteriori (MAP) estimate. The log of a Gaussian prior is exactly an penalty, and a Laplace prior gives : regularization is a prior in disguise.
From posterior to penalty
By Bayes' theorem . Taking negative logs:
With a Gaussian prior , . For linear regression with noise variance :
This is ridge regression. A tight prior (small ) means a large and strong shrinkage towards 0. With a Laplace prior , the penalty is : the lasso, which produces sparse solutions (Norms).
Ridge fixes overfitting and singular systems
import numpy as np
rng = np.random.default_rng(0)
f_true = lambda x: np.sin(2 * x)
x_tr = rng.uniform(-2, 2, 15); y_tr = f_true(x_tr) + 0.2 * rng.normal(size=15)
x_te = rng.uniform(-2, 2, 1000); y_te = f_true(x_te) + 0.2 * rng.normal(size=1000)
Phi = lambda x: np.vander(x / 2, 15, increasing=True) # degree-14 polynomial features
for lam in (0.0, 1e-6, 1e-3, 1e-1, 10.0):
A = Phi(x_tr).T @ Phi(x_tr) + lam * np.eye(15)
theta = np.linalg.lstsq(A, Phi(x_tr).T @ y_tr, rcond=None)[0]
te = np.mean((Phi(x_te) @ theta - y_te) ** 2)
print(f"lambda={lam:<6g} |theta|={np.linalg.norm(theta):10.2f} test MSE {te:.4f}")lambda=0 |theta|= 4962.63 test MSE 45.0959
lambda=1e-06 |theta|= 128.90 test MSE 0.0999
lambda=0.001 |theta|= 6.12 test MSE 0.2528
lambda=0.1 |theta|= 2.57 test MSE 0.1708
lambda=10 |theta|= 0.26 test MSE 0.4845
Without regularization the degree-14 fit has huge coefficients and a terrible test error. A moderate shrinks the coefficients and generalizes well; too large a underfits. Adding also makes positive definite and well conditioned, so the system always has a unique solution.
Weight decay
In gradient descent, the term adds to the gradient, so each step shrinks the weights by a factor before the data step: weight decay. (For adaptive optimizers like Adam the two are not identical, which is the point of AdamW: Adam and AdamW.)
MAP versus the full posterior
MAP returns a single point, the mode of the posterior. It ignores the posterior's spread, so it says nothing about uncertainty, and the mode can be atypical in high dimensions. The full Bayesian approach keeps the whole posterior and averages predictions over it (Bayesian Linear Regression).
import numpy as np
# coin flips: 3 heads in 3 tosses. ML says mu = 1; a Beta(2, 2) prior says otherwise
h, t = 3, 0
a, b = 2, 2
mu_ml = h / (h + t)
mu_map = (h + a - 1) / (h + t + a + b - 2) # mode of Beta(h + a, t + b)
mu_mean = (h + a) / (h + t + a + b) # posterior mean
print(f"ML {mu_ml:.3f} MAP {mu_map:.3f} posterior mean {mu_mean:.3f}")ML 1.000 MAP 0.800 posterior mean 0.714
In software
Ridge(alpha=λ),Lasso(alpha=...),LogisticRegression(C=1/λ)in scikit-learn (Regularization).torch.optim.SGD(..., weight_decay=λ)andtorch.optim.AdamW.- Dropout and early stopping are other regularizers without such a simple prior interpretation (Regularization in Deep Learning).
Exercises
Exercise 1: Prior strength
Noise standard deviation and prior standard deviation . What is the equivalent ridge ?
Answer
Solution
. Noisy data and a confident prior both increase the regularization.
Exercise 2: One-dimensional ridge
For one feature with and , compare and with .
Answer
, .
Solution
: without, with regularization.
Exercise 3: Weight decay factor (code)
With learning rate and , by what factor does pure weight decay shrink a weight after 1,000 steps (no data gradient)?
Answer
About ().
Solution
Each step multiplies by , and .
import math
print(round(0.999 ** 1000, 4), round(math.exp(-1), 4))0.3677 0.3679
Test yourself
A Gaussian prior on the weights corresponds to which penalty?
- squared (ridge)
- no penalty
The ridge solution is…
Increasing the ridge parameter a lot leads to…
- overfitting
- underfitting: weights shrink towards 0
- no change
- sparse weights with exact zeros