Skip to main content

core.CurvatureFit

core.CurvatureFit(
    model=static(),
    likelihood=static(),
    params,
    curvature=static(),
    caps=None,
    *,
    _seed=_Seed.draw(),
)

A model, likelihood, params and single-pass method bound for fitting.

The fit steps are the plain fold, plus start() for uniform code. Build it with fit_curvature, which checks the likelihood.

Parameters

Name Type Description Default
model ModelFn The model as a function of the params. static()
likelihood Likelihood The likelihood. The curvature sees its unit version (see LateLikelihood). static()
params Params The params the curvature is taken at. required
curvature Curvature[S, Op] The single-pass curvature. static()
caps Capabilities | None The probed capabilities. None skips the model checks. None

Attributes

Name Description
ctx The context the curvature sees, with the unit likelihood.

Methods

Name Description
finalize The curvature from the statistic over all the data.
init The empty statistic.
posterior Build the posterior without repeating the model, likelihood, and layout.
start The first (and only) pass, for code that also runs multi-pass methods.
update Fold one batch into the statistic.

finalize

core.CurvatureFit.finalize(stat, /)

The curvature from the statistic over all the data.

Parameters

Name Type Description Default
stat FitStat[S] The statistic over all the data, after all_reduce. required

Returns

Name Type Description
Op The curvature’s finalize: the per-example average \(\bar{C}\). The posterior multiplies it by n_data.

Raises

Name Type Description
ValueError The curvature’s finalize refuses stat, e.g. a zero total weight.

init

core.CurvatureFit.init()

The empty statistic.

Returns

Name Type Description
FitStat[S] The identity of +: the curvature’s init, no data-fit term and a zero count.

Raises

Name Type Description
CapabilityError With caps, the capabilities lack something the curvature needs.
StructureError The curvature refuses the params or layout.
ValueError The curvature refuses its settings for these params (e.g. LowRankGGN with rank + oversample above the parameter count).

posterior

core.CurvatureFit.posterior(curvature, /, prior, n_data)

Build the posterior without repeating the model, likelihood, and layout.

Parameters

Name Type Description Default
curvature C The finalized curvature, the per-example average. required
prior Pr The prior. Its location must match the params. required
n_data Tensor The effective data size \(N\) the curvature is multiplied by. required

Returns

Name Type Description
LaplacePosterior[C, Pr] The Laplace posterior at the fit’s params.

Raises

Name Type Description
StructureError The prior’s location doesn’t match the params, or the curvature can’t absorb the prior precision (e.g. KFAC on KFAC).

start

core.CurvatureFit.start()

The first (and only) pass, for code that also runs multi-pass methods.

Returns

Name Type Description
Running[Op] A Running step whose finish gives the curvature as Finished.result.

Raises

Name Type Description
CapabilityError With caps, the capabilities lack something the curvature needs.

update

core.CurvatureFit.update(stat, batch, /)

Fold one batch into the statistic.

A HasTargetCheck likelihood checks the batch’s targets first.

Parameters

Name Type Description Default
stat FitStat[S] The statistic so far. required
batch Batch The next batch. Its generator comes from the fit’s seed and the examples seen before it. required

Returns

Name Type Description
FitStat[S] stat with the batch added.

Raises

Name Type Description
CapabilityError With caps, the batch fails the curvature’s requirements (Requirements.check_batch). With or without caps, the curvature needs targets the batch lacks.
ValueError The likelihood’s check_targets finds a bad target value.
StructureError The likelihood’s check_targets finds badly shaped targets, or a LateLikelihood has an active mask, which fit_stat can’t apply.