laplace
laplace(
model,
data,
likelihood,
*,
hessian_approx=None,
subset=subsets.last_layer,
prior=priors.Isotropic(),
hyper_tuner=MarginalLikelihood(),
generator=None,
)Fit a Laplace approximation in weight space, then tune its hyperparameters.
post = lk.laplace(model, train_loader, lk.Categorical())
probs = lk.predict(post, x).probsThe probe runs first and checks what the model supports, so a method the model can’t run raises before any data past the first batch is read. The default picks the last layer and a curvature per block (lk.curvature.default), and tunes the prior precision (and a learned sigma) by the marginal likelihood.
subset may give a Selection. Its entries are fitted as parameters of their own ("fc.weight[idx]"), and post.index holds their indices. Hook-based methods (KFAC) then raise CapabilityError, and the default never picks them for those blocks.
The result holds a copy of the fitted parameters as its mean. Training the module afterwards leaves it in place, so it can be the prior of the next fit (continual learning). The prior and the new curvature must add: a KFAC posterior can’t be the prior of a KFAC fit, which raises StructureError, and lk.priors.KronSum.of(post) is the approximate opt-in that sums the Kronecker factors. A LowRankGGN posterior as the prior of a LowRankGGN fit is exact, but the width grows with every fit; lk.priors.LowRankSum.of(post) truncates it to a fixed rank. When the method has a template (core.HasTemplate; the built-ins do), a prior that doesn’t fit raises before any data past the probe’s batch, with the message prefixed “before the data pass:”.
On several ranks (a torch.distributed process group), each rank passes its own shard and the statistics are combined with all_reduce. Every rank needs at least one batch.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | nn.Module | ModelFn | The model. A module is wrapped with ModelFn.from_module. |
required |
| data | Data | Iterable[Any] | A re-iterable (a list, a DataLoader) of (inputs, targets) pairs or Batches, or a Data source such as lk.data.from_loader(..., n_data=N). |
required |
| likelihood | L | The likelihood, such as lk.Categorical() or lk.Gaussian(). |
required |
| hessian_approx | HessianApprox[Any] | None | The curvature method, such as lk.KFAC() or lk.DiagGGN(). None picks one per block from the probe (lk.curvature.default). Its needs are checked against the likelihood before any data is read. |
None |
| subset | Subset | Params | Selection | The parameters to fit: a subset function such as lk.subsets.all, called with the model and the probe’s capabilities, a parameter mapping, or a Selection. |
subsets.last_layer |
| prior | Prior | The prior. Its location must match the fitted parameters, which is checked before the fit. A Layerwise or Diagonal prior with one shared precision is expanded to one precision per block or per weight first. |
priors.Isotropic() |
| hyper_tuner | HyperTuner | None | Tunes the prior precision and the likelihood’s learned hyperparameters after the fit. None skips the tune. A posterior with nothing to tune (a prior that is not a TunablePrior, say, and a likelihood that learns nothing) is returned untuned. Each Dense curvature gets its eigendecomposition first (core.with_eigen), so the tune needs no Cholesky per step. |
MarginalLikelihood() |
| generator | torch.Generator | None | Seeds the data passes and the method’s draws. One seed is drawn from it, or from torch’s global RNG when None, so torch.manual_seed makes the fit repeatable. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| LaplacePosterior[Any, Any, L] | The posterior, a LaplacePosterior. print(post) shows each resolved choice. With targets, it keeps the data-fit term, so post.log_marglik() works. |
Raises
| Name | Type | Description |
|---|---|---|
| TypeError | An argument has the wrong kind: a string ("kron"), a class instead of an instance (lk.KFAC), a number as the prior, or data that is one (inputs, targets) pair or a Dataset. The message names what to pass. |
|
| CapabilityError | The likelihood or the model lacks what the method needs, the probe’s batch fails the method’s batch requirements, a hook-based method meets a Selection, or last_layer finds no parameterized module. |
|
| StructureError | The prior’s tunable() breaks its rules, the prior’s location doesn’t match the fitted parameters, the prior can’t be added to the curvature, the subset refuses the model, or the probe or the fit finds badly shaped outputs or targets. |
|
| ValueError | The data yields no batches, the likelihood finds a bad target value (float targets for a Categorical, say), a method refuses its settings, or the hyper tuner can’t work on this data (MarginalLikelihood without targets). |