Skip to main content

objectives.predictive_nll

objectives.predictive_nll(data, /, predictive=None, *, generator=None)

Mean negative log predictive density of the batches’ targets, per example.

With \(N\) the examples over all batches,

\[ \frac{1}{N} \sum_{n=1}^{N} -\log p(y_n \mid x_n, \mathcal{D}), \]

where \(p(\cdot \mid x_n, \mathcal{D})\) is the predictive distribution.

With a single HasMask likelihood the masked positions drop out, as in core.log_prob with inputs. The mask reads the outputs at the mean, which costs one extra model call per batch for a weight-space posterior and one extra at(inputs) for a function posterior. The built-in Categorical and from_log_prob without mask_fn skip it. The mask covers the leading axes of the log-probabilities of the distribution under any Independent wrappers.

Parameters

Name Type Description Default
data Data | Iterable[Any] Data with targets, in any form lk.laplace takes: a list or DataLoader of (inputs, targets) pairs, or a Data source. It is read once, into a list. Targets must be shaped like the predictive distribution’s batch and event shapes together. required
predictive Predictive[Posterior] | None The predictive method. None means Linearized(), resolved against the posterior. None
generator torch.Generator | None Seeds the draws of a sampled predictive (lk.Linearized(n_samples=64), say). One seed is drawn when the objective is built, and every call reuses it, so the objective is a function of the posterior alone, as lk.Minimize needs. None draws the seed from torch’s global RNG. None

Returns

Name Type Description
Objective[Posterior] The objective, a function of a posterior to a 0-dim tensor. When called, it raises ValueError for a batch without targets, and StructureError for targets of another shape or a mask that is not bool or doesn’t cover the leading axes. It also raises what the predictive raises.