import matplotlib.pyplot as plt
import numpy as np
import torch
from sklearn.datasets import make_blobs
from torch import nn
import laplacekit as lk
x_np, y_np = make_blobs(
n_samples=500,
centers=4,
cluster_std=1.2,
center_box=(-10, 10),
random_state=37,
)
x, y = torch.tensor(x_np, dtype=torch.float32), torch.tensor(y_np)
torch.manual_seed(99999)
model = nn.Sequential(
nn.Linear(2, 20),
nn.BatchNorm1d(20),
nn.ReLU(),
nn.Linear(20, 20),
nn.BatchNorm1d(20),
nn.ReLU(),
nn.Linear(20, 4, bias=False),
)
opt = torch.optim.SGD(
model.parameters(), lr=1e-3, momentum=0.9, weight_decay=5e-4
)
for _ in range(5000):
opt.zero_grad()
nn.functional.cross_entropy(model(x), y).backward()
opt.step()Classification
A ReLU classifier is confident far away from its training data. A Laplace approximation on its last layer makes it much less so. To see this, we train a small network on four 2D blobs.
The fit is one call. The loss is a mean over the \(N\) points, so the prior precision that matches the weight decay is \(N\) times it. We keep it fixed. We also print the prior precision that the default tuning picks.
model.eval()
post = lk.laplace(
model,
[(x, y)],
likelihood=lk.Categorical(),
prior=lk.priors.Isotropic(len(x) * 5e-4),
hyper_tuner=None,
)
print(post)
tuned = lk.laplace(
model, [(x, y)], likelihood=lk.Categorical()
) # the default, for comparison
print("tuned prior precision:", float(tuned.prior.prior_precision))LaplacePosterior(
subset = last_layer {"6.weight": (4, 20)}
hessian_approx = PerBlockCurvature({'*': DenseGGN()})
curvature = BlockDiag[Dense] (1 block, 80 parameters)
prior = Isotropic(prior_precision=0.25)
likelihood = Categorical()
hyper_tuner = None
n_data = 500
)
tuned prior precision: 1.0543646812438965
We plot the confidence, the highest class probability, on a grid that reaches well past the data.
lin = np.linspace(-15, 15, 50)
gx, gy = np.meshgrid(lin, lin)
grid = torch.tensor(
np.stack([gx.ravel(), gy.ravel()], axis=1), dtype=torch.float32
)
with torch.no_grad():
conf_map = model(grid).softmax(-1).max(-1).values
conf_lap = lk.predict(post, grid).probs.max(-1).values
fig, axes = plt.subplots(1, 2, figsize=(9, 4), sharey=True)
for ax, conf, name in zip(
axes, [conf_map, conf_lap], ["MAP", "Laplace"], strict=True
):
im = ax.contourf(
gx,
gy,
conf.reshape(gx.shape),
levels=np.arange(0.3, 1.01, 0.1),
cmap="Blues",
)
ax.scatter(
x_np[:, 0],
x_np[:, 1],
c=y_np,
cmap="Set2",
edgecolors="k",
linewidths=0.5,
s=12,
)
ax.set_title(name)
ax.set_aspect("equal")
fig.colorbar(im, ax=list(axes), label="max class probability")
plt.show()
The MAP network is sure of itself almost everywhere. The Laplace predictive is sure near the data. Away from it, its confidence falls off toward the bottom left but stays high toward the top and the right. How fast it falls off depends on the prior. By default, lk.laplace tunes the prior precision on the marginal likelihood. Here, that gives \(\lambda \approx 1.05\), about four times \(N \delta = 0.25\). With it, the predictive stays confident far away in more than half of the directions. The full tutorial compares the two.
To use another \(\lambda\), we can fix it. Or we can pick it by the NLL on held-out data (a loader or a list of (inputs, targets) pairs), either with L-BFGS (lk.Minimize) or on a grid (lk.GridSearch):
post = lk.laplace(model, data, lik, prior=lk.priors.Isotropic(λ), hyper_tuner=None)
post = lk.tune(post, lk.Minimize(lk.objectives.predictive_nll(val_loader)))
post = lk.tune(post, lk.GridSearch(lk.objectives.predictive_nll(val_loader), values=lams))Tuning for calibration
The held-out objective doesn’t have to be the NLL. lk.objectives.brier is the Brier score, and lk.objectives.ece the expected calibration error. We draw 200 validation points from the same four blobs and pick \(\lambda\) with each. The ECE counts points in bins, so it has no gradient. lk.Minimize refuses it, so we search a grid with lk.GridSearch instead. The Brier score is smooth, so lk.Minimize works.
_, _, centers = make_blobs(
n_samples=500,
centers=4,
center_box=(-10, 10),
random_state=37,
return_centers=True,
)
xv_np, yv_np = make_blobs(
n_samples=200, centers=centers, cluster_std=1.2, random_state=1
)
val = [(torch.tensor(xv_np, dtype=torch.float32), torch.tensor(yv_np))]
ece, brier = lk.objectives.ece(val), lk.objectives.brier(val)
by_ece = lk.tune(post, lk.GridSearch(ece, values=torch.logspace(-4, 2, 25)))
by_brier = lk.tune(post, lk.Minimize(brier))
print(f"{'λ from':<16} {'λ':>8} {'ECE':>6} {'Brier':>6}")
for name, p in [
("weight decay", post),
("marg. lik.", tuned),
("grid on ECE", by_ece),
("tune on Brier", by_brier),
]:
print(
f"{name:<16} {float(p.prior.prior_precision):8.3g} "
f"{float(ece(p)):6.3f} {float(brier(p)):6.3f}"
)λ from λ ECE Brier
weight decay 0.25 0.034 0.038
marg. lik. 1.05 0.023 0.037
grid on ECE 10 0.020 0.037
tune on Brier 3.27e+05 0.022 0.036
With \(\lambda = N \delta\), the ECE is 0.034. The marginal-likelihood \(\lambda \approx 1.05\) brings it down to 0.023. The grid on the ECE picks \(\lambda = 10\), with 0.020. The Brier score barely moves, from 0.038 to 0.037 with either. lk.Minimize on the Brier score keeps raising \(\lambda\), to about \(3 \times 10^5\), where the posterior has almost no variance left, and gets 0.036. On validation data like the training data, the Brier score prefers the MAP network.
Laplace bridge
The probit is one way to turn the Gaussian over the logits into class probabilities. The Laplace bridge is another: it matches a Dirichlet to the Gaussian and predicts its mean. It is a link, like the probit, so we pass it to lk.Linearized.
bridge = (
lk.predict(post, grid, lk.Linearized(link=lk.Bridge())).probs.max(-1).values
)
normed = (
lk.predict(post, grid, lk.Linearized(link=lk.Bridge(normalized=True)))
.probs.max(-1)
.values
)
links = {
"MAP": conf_map,
"probit": conf_lap,
"bridge": bridge,
"normalized": normed,
}
for name, conf in links.items():
print(f"{name:<11} mean confidence on the grid {float(conf.mean()):.3f}")
cov = lk.predict_outputs(post, grid).cov.per_input() # [2500, 4, 4]
row = cov.sum(-1)
var = cov.diagonal(dim1=-2, dim2=-1) - row**2 / row.sum(
-1, keepdim=True
) # shared shift removed
ratio = var.max(-1).values / var.min(-1).values
print(
f"logit variance: mean {float(var.mean()):.0f}, median max/min "
f"{float(ratio.median()):.1f}"
)MAP mean confidence on the grid 0.936
probit mean confidence on the grid 0.741
bridge mean confidence on the grid 0.933
normalized mean confidence on the grid 0.570
logit variance: mean 65, median max/min 1.7
The probit gives 0.741. But the plain bridge is about as confident as the MAP network: 0.933 on average over the grid, against 0.936. Its mean depends on the logit variances (after removing the shift shared by all logits) only through their ratios across classes. Here, they are all large, 65 on average with a median max/min ratio of 1.7, so their size mostly cancels. The normalized bridge, lk.Bridge(normalized=True), folds the size back in by dividing the logit mean by a factor that grows with the average variance. It gives 0.570.
The bridge is for three classes or more. With two classes, it ignores the variances, so there we keep the probit, which is the default.
Binary and multi-label
When each input can have several labels at once, every output is its own yes-or-no question. lk.Bernoulli() is the sigmoid likelihood for that, with targets of 0 and 1 shaped like the outputs. Our toy has three labels on the plane: “right of the origin”, “above it” and “within distance 2 of it”.
torch.manual_seed(0)
xb = 6 * torch.rand(300, 2) - 3
yb = torch.stack(
[xb[:, 0] > 0, xb[:, 1] > 0, xb.norm(dim=1) < 2], dim=1
).float()
net = nn.Sequential(
nn.Linear(2, 32), nn.ReLU(), nn.Linear(32, 32), nn.ReLU(), nn.Linear(32, 3)
)
opt = torch.optim.Adam(net.parameters(), lr=1e-2)
for _ in range(500):
opt.zero_grad()
nn.functional.binary_cross_entropy_with_logits(net(xb), yb).backward()
opt.step()
post_b = lk.laplace(net, [(xb, yb)], likelihood=lk.Bernoulli())
print(post_b)LaplacePosterior(
subset = last_layer {"4.weight": (3, 32), "4.bias": (3,)}
hessian_approx = PerBlockCurvature({'*': DenseGGN()})
curvature = BlockDiag[Dense] (1 block, 99 parameters)
prior = Isotropic(prior_precision=0.507)
likelihood = Bernoulli()
hyper_tuner = MarginalLikelihood()
n_data = 300
)
lk.predict gives one probability per label. We ask at a point inside the data and at one far outside it.
points = torch.tensor([[1.0, 1.0], [20.0, 20.0]])
with torch.no_grad():
p_map = net(points).sigmoid()
p_lap = lk.predict(post_b, points).probs
for pt, a, b in zip(
points.tolist(), p_map.tolist(), p_lap.tolist(), strict=True
):
print(
f"x = {pt}: MAP {[round(v, 2) for v in a]}, Laplace "
f"{[round(v, 2) for v in b]}"
)x = [1.0, 1.0]: MAP [1.0, 1.0, 1.0], Laplace [0.97, 0.96, 0.98]
x = [20.0, 20.0]: MAP [1.0, 1.0, 0.0], Laplace [0.95, 0.93, 0.01]
The point \((1, 1)\) has all three labels, and both the MAP and the Laplace predictive are sure of them. The point \((20, 20)\) is right of and above the origin but far from it, and the MAP network gives 1.0, 1.0 and 0.0. The Laplace probabilities there, 0.95, 0.93 and 0.01, are only a little less sure.
Write the network as \(f(x) = W \phi(x)\), with the last layer \(W\) on top of the features \(\phi\). The Laplace approximation keeps \(\phi\) fixed and puts a Gaussian on \(W\),
\[ p(W \mid \mathcal{D}) \approx \mathcal{N}\big(\mathrm{vec}(W) \mid \mathrm{vec}(W_*), (\lambda I + N G)^{-1}\big) , \]
with \(G\) the generalized Gauss-Newton matrix of the last layer and \(\lambda\) the prior precision. Here, \(\lambda = N \delta\), with \(\delta\) the weight decay, since the training objective is the negative log posterior divided by \(N\). The logits are linear in \(W\), so they are Gaussian with mean \(W_* \phi(x)\) and a covariance \(V(x)\), and the probit approximation gives
\[ p(y \mid x, \mathcal{D}) \approx \mathrm{softmax}\Big(\frac{C W_* \phi(x)}{\sqrt{1 + \frac{\pi}{8} \mathrm{diag} \big( C V(x) C \big)}}\Big) , \]
with \(C = I - \frac{1}{K} \mathbf{1} \mathbf{1}^\top\), which subtracts the mean over the \(K\) classes from the logits.
For a ReLU network, \(\lVert \phi(x) \rVert\) grows away from the data, and so does \(V(x)\), which pulls the probabilities toward uniform. With two classes, the probit is applied to the difference of the two logits, \(f_1 - f_2\), with its variance \(v_d = V_{11} + V_{22} - 2 V_{12}\).
A Bernoulli output is one logit \(f_k\) per label, and each gets its own sigmoid probit, \(\sigma\big(\mu_k / \sqrt{1 + \frac{\pi}{8} V_{kk}(x)}\big)\).
The Laplace bridge (Hobbhahn et al., 2022) maps the logit mean \(\mu\) and variances \(v\) (after removing the shift shared by all logits) to a Dirichlet with
\[ \alpha_k = \frac{1}{v_k} \Big(1 - \frac{2}{K} + \frac{e^{\mu_k}}{K^2} \sum_j e^{-\mu_j}\Big) , \]
and predicts its mean \(\alpha / \sum_j \alpha_j\). At \(K = 2\), the two variances are equal, so they cancel in that mean.
The full tutorial: Classification