Skip to main content

function_space.laplace

function_space.laplace(
    model,
    data,
    likelihood,
    *,
    method,
    subset=subsets.last_layer,
    prior=priors.Isotropic(),
    generator=None,
)

Function-space Laplace: probe, pick a subset of parameters, run the method’s passes.

The probe is that of lk.laplace. The method’s requirements, the prior’s location and the method’s own budgets are checked before any data past the probe’s batch.

gp = lk.function_space.laplace(model, loader, lk.Gaussian(), method=lk.gp.ELLA(points, rank=20))

The result’s type B comes from method. Python’s types cannot bind the likelihood’s type inside it, so gp.likelihood is typed Likelihood; narrow it to reach a field:

lik = gp.likelihood
assert isinstance(lik, lk.Gaussian)

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 Data source, or a re-iterable of Batches or (inputs, targets) pairs, which lk.data.from_loader wraps. required
likelihood Likelihood The likelihood. The method’s needs are checked against it before any data is read. required
method FunctionSpaceMethod[B] The function-space method, such as SubsetOfData(n_subset) or lk.gp.ELLA(points, rank). required
subset Subset | Params | Selection The parameters to fit, as for lk.laplace: a subset function, called with the model and the probe’s capabilities, a parameter mapping, or a Selection. subsets.last_layer
prior PriorWithCovariance The prior. A fitted weight-space posterior works too, if it is over the same parameters. priors.Isotropic()
generator torch.Generator | None Seeds the data passes and the method’s draws, as in lk.laplace. None means torch’s global RNG, so torch.manual_seed makes the fit repeatable. None

Returns

Name Type Description
B The function-space posterior. It holds a copy of the fitted parameters as its mean, the prior as given, and the real likelihood (the passes see its unit version, so sigma stays tunable). Moving one of the module’s other parameters in place afterwards makes at raise.

Raises

Name Type Description
CapabilityError The likelihood or the model lacks what the method needs, a batch fails the method’s batch requirements (SubsetOfData refuses weights), or last_layer finds no parameterized module.
StructureError The prior’s location doesn’t match the fitted parameters, the method’s budgets fail, the subset refuses the model, or the probe or a pass finds badly shaped outputs or targets, or a mask that is not bool or not shaped like the leading axes of the outputs.
ValueError The data yields no batches or no examples, some batches have targets and others don’t, or the likelihood finds a bad target value.
TypeError data is a one-shot iterator, or an item is neither a Batch nor an (inputs, targets) pair.
NotImplementedError The model does not return a single tensor.