Skip to main content

sample_outputs

sample_outputs(
    posterior,
    inputs,
    n_samples,
    /,
    *,
    generator=None,
    kind='linearized',
    strategy=None,
)

n_samples draws of the model’s outputs at inputs, stacked along a new leading axis.

For a weight-space posterior, draw \(s\) uses one weight noise \(\varepsilon_s \sim \mathcal{N}(0, \Sigma)\) (posterior.noise) for every input, so the draws are joint across the batch. The draws are in stat_dtype of the outputs. All n_samples weight draws are held at once (\(n_samples P\) entries).

A function-space posterior gives posterior.at(inputs)’s mean plus cov.sample(n_samples). Those are joint across the batch for a FactorCov or a SampleCov, and independent per input for a PerInputCov (see OutputCov).

The outputs have no observation noise: the predictive samples of a classifier are a link on top, e.g. torch.softmax(lk.sample_outputs(post, x, 100), dim=-1). These are laplace-torch’s functional_samples (pred_type="glm" for linearized, "nn" for network).

Parameters

Name Type Description Default
posterior Posterior A weight-space posterior or a function-space posterior. required
inputs Inputs A batch of inputs. required
n_samples int Number of draws, at least 1. required
generator torch.Generator | None Seeds the draws. One seed is drawn from it, or from torch’s global RNG when None, so torch.manual_seed makes the draws repeatable. None
kind Literal['linearized', 'network'] "linearized" draws \(f(x; \mu) + J \varepsilon_s\), with \(J\) the Jacobian at the mean \(\mu\). "network" draws \(f(x; \mu + \varepsilon_s)\), one forward per draw; it needs a weight-space posterior. 'linearized'
strategy Strategy | None The pushforward of a linearized draw on a weight-space posterior, no vmap: one JVP per draw ("forward-mode", needs jvp) or a VJP of a VJP ("reverse-mode", needs vjp and vjp_twice). None picks vjp when the probe found vjp_twice (the forward is recorded once for all draws), else jvp. The capabilities are checked before the forward. Leave it unset for kind="network" and for a function-space posterior. None

Returns

Name Type Description
Tensor The draws, [n_samples, N, *event]: the model must return a single tensor, as for outputs.

Raises

Name Type Description
ValueError n_samples is below 1; kind is neither "linearized" nor "network"; an explicit strategy with kind="network" or a function-space posterior; or, for a function-space posterior whose covariance is a SampleCov, n_samples above its number of draws.
TypeError kind="network" with a function-space posterior, which has no weights.
RuntimeError The posterior’s check_versions finds a parameter changed in place since the fit.
CapabilityError For kind="linearized" on a weight-space posterior, the model lacks what strategy needs.
StructureError For a weight-space posterior, the model does not return one tensor (raised on the first forward), or the posterior’s covariance products fail, for example on a precision that is not positive definite.