Skip to main content

Marglik training

Weight decay and noise are usually picked by hand, or by a grid search over several training runs. Marginal-likelihood training learns them in a single run. We use the data and the network of the regression page.

NotePlugin

This page uses the laplacekit-marglik plugin. Install it with:

pip install "laplacekit[marglik]"
import matplotlib.pyplot as plt
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset

import laplacekit as lk

torch.set_default_dtype(torch.float64)
torch.manual_seed(711)

x = torch.rand(150, 1) * 8.0
y = torch.sin(x) + 0.3 * torch.randn(150, 1)
x_test = torch.linspace(-2.0, 10.0, 200).reshape(-1, 1)

dataset = TensorDataset(x, y)
loader = DataLoader(dataset, batch_size=50, shuffle=True)
model = nn.Sequential(nn.Linear(1, 50), nn.Tanh(), nn.Linear(50, 1))

lk.marglik.train replaces the training loop. The optimizer has no weight decay, since the prior precision \(\lambda\) plays that role, one per layer by default.

trained = lk.marglik.train(
    model,
    loader,
    n_data=len(dataset),
    likelihood=lk.Gaussian(),
    optimizer=torch.optim.Adam(model.parameters(), lr=1e-2),
    epochs=200,
    generator=torch.Generator().manual_seed(0),
)
post = trained.posterior
print(post)

print(
    "lambda:",
    {k: round(float(v), 3) for k, v in post.prior.prior_precision.items()},
)
print("sigma: ", round(float(post.likelihood.sigma), 3))
print("best epoch:", trained.best_epoch)
LaplacePosterior(
  subset         = trainable {"0.weight": (50, 1), "0.bias": (50,), "2.weight": (1, 50), "2.bias": (1,)}
  hessian_approx = PerBlockCurvature({'*': DenseGGN()})
  curvature      = BlockDiag[Dense] (2 blocks, 151 parameters)
  prior          = Layerwise(prior_precision={"0": 0.201, "2": 0.716})
  likelihood     = Gaussian(sigma=0.3068 (learned))
  hyper_tuner    = None
  training       = marglik, best epoch 196 of 200
  n_data         = 150
)
lambda: {'0': 0.201, '2': 0.716}
sigma:  0.307
best epoch: 196
pred = lk.predict(post, x_test)
mean, std = pred.mean.squeeze(), pred.stddev.squeeze()
t = x_test.squeeze()

fig, ax = plt.subplots(figsize=(7, 4))
ax.scatter(x.squeeze(), y.squeeze(), s=8, color="0.5", label="data")
ax.plot(t, mean, label="mean")
ax.fill_between(
    t, mean - 2 * std, mean + 2 * std, alpha=0.3, label="mean ± 2 std"
)
ax.legend()
plt.show()

train kept epoch 196 of 200, the one with the highest evidence. The learned \(\sigma\) is 0.307, close to the 0.3 we used to make the data. The first layer gets a weaker prior than the last (\(\lambda\) of 0.201 against 0.716). trained.posterior is a Laplace posterior like any other, fitted at the kept epoch with the learned \(\lambda\) and \(\sigma\), so lk.predict takes it as it is.

With hyperparameters \(h\) (here \(\lambda\) per layer and \(\sigma\)), the Laplace approximation of the log marginal likelihood at the weights \(\theta_*\) is

\[ \log p(\mathcal{D} \mid h) \approx \log p(\mathcal{D} \mid \theta_*, h) + \log p(\theta_* \mid h) - \tfrac{1}{2} \log \det \tfrac{1}{2 \pi} \big(\Lambda(h) + N G\big) , \]

with \(\Lambda(h)\) the prior precision and \(G\) the GGN. Immer et al. (2021)1 alternate between fitting \(\theta\) and \(h\):

  1. One epoch of gradient steps on \(\theta\) for \(-\log p(\mathcal{D} \mid \theta, h) - \log p(\theta \mid h)\).
  2. After every marglik_frequency epochs (1 by default), a Laplace fit at the current \(\theta\).
  3. A few gradient steps on \(\log h\) for the negative log marginal likelihood above.

The network kept at the end is the one from the epoch with the highest evidence.

The full tutorial: Marginal-likelihood training

Footnotes

  1. Immer, A., Bauer, M., Fortuin, V., Rätsch, G., & Khan, M. E. (2021). Scalable marginal likelihood estimation for model selection in deep learning. ICML. https://arxiv.org/abs/2104.04975↩︎