Posterior Approximation Methods
Methods that turn a network into a source of posterior samples, either after training or as part of it.
Laplace Approximation
bensemble.methods.laplace_approximation.LaplaceApproximation
LaplaceApproximation(model: Module, likelihood: str = 'regression', prior_precision: float = 1.0, damping: float = 1e-06, regularization: str = 'legacy', verbose: bool = False)
Kronecker-factored Laplace approximation for neural networks.
Initializes the LaplaceApproximation instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | Module | PyTorch model to approximate. | required |
likelihood | str | Task likelihood, either "classification" or "regression". Defaults to "regression". | 'regression' |
prior_precision | float | Prior precision hyperparameter (scalar tau). Defaults to 1.0. | 1.0 |
damping | float | Numerical stabilization term added to diagonals. Defaults to 1e-6. | 1e-06 |
regularization | str | Regularization formula type ("legacy" or "paper"). Defaults to "legacy". | 'legacy' |
verbose | bool | If True, prints progress details during computation. Defaults to False. | False |
Raises:
| Type | Description |
|---|---|
ValueError | If likelihood or regularization values are unsupported. |
Source code in bensemble/methods/laplace_approximation.py
build_ensemble
Builds an Ensemble module sampled from the Laplace posterior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_members | int | Number of ensemble members to draw. Defaults to 10. | 10 |
temperature | float | Sampling temperature for posterior weights. Defaults to 1.0. | 1.0 |
Returns:
| Name | Type | Description |
|---|---|---|
Ensemble | Ensemble | Ensemble instance wrapping the sampled models. |
Source code in bensemble/methods/laplace_approximation.py
compute_curvature
Estimates the Kronecker factors of the Hessian using training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_loader | DataLoader | DataLoader yielding training data and targets. | required |
num_samples | int | Maximum number of samples to process for curvature estimation. Defaults to 1000. | 1000 |
Source code in bensemble/methods/laplace_approximation.py
sample_models
Samples model parameters from the approximated Gaussian posterior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_models | int | Number of models to sample. Defaults to 10. | 10 |
temperature | float | Sampling temperature scaling factor. Defaults to 1.0. | 1.0 |
Returns:
| Type | Description |
|---|---|
list[Module] | list[nn.Module]: Sampled model instances in eval mode. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If curvature has not been computed prior to sampling. |
Source code in bensemble/methods/laplace_approximation.py
Probabilistic Backpropagation
bensemble.methods.probabilistic_backpropagation.PBPEngine
PBPEngine(model: Module | None = None, layer_sizes: list[int] | None = None, noise_alpha: float = 6.0, noise_beta: float = 6.0, weight_alpha: float = 6.0, weight_beta: float = 6.0, dtype: dtype = torch.float64, device: device | None = None)
Probabilistic Backpropagation (PBP) Engine for Bayesian regression.
Source code in bensemble/methods/probabilistic_backpropagation.py
build_ensemble
Builds an Ensemble of networks sampled from the posterior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_members | int | Number of sampled members. Defaults to 10. | 10 |
Returns:
| Name | Type | Description |
|---|---|---|
Ensemble | Ensemble | Ensemble wrapping the sampled networks. |
Source code in bensemble/methods/probabilistic_backpropagation.py
fit
fit(train_loader: DataLoader, val_loader: DataLoader | None = None, num_epochs: int = 100, step_clip: float | None = 2.0, prior_refresh: int = 1, **kwargs: Any) -> dict[str, list[float]]
Runs assumed-density filtering over the training data.
Each epoch visits every training point once in random order, updates the weight posteriors, then refreshes the noise and prior hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_loader | DataLoader | DataLoader yielding (inputs, targets) pairs. | required |
val_loader | DataLoader | None | Optional DataLoader evaluated after every epoch. | None |
num_epochs | int | Number of passes over the training data. Defaults to 100. | 100 |
step_clip | float | None | Clipping threshold for each ADF update, or None to disable clipping. Defaults to 2.0. | 2.0 |
prior_refresh | int | Number of prior-refresh iterations per epoch, or 0 to skip. Defaults to 1. | 1 |
**kwargs | Any | Ignored, accepted for interface compatibility. | {} |
Returns:
| Type | Description |
|---|---|
dict[str, list[float]] | dict[str, list[float]]: Per-epoch RMSE and NLPD on the training |
dict[str, list[float]] | data, plus validation values when |
Source code in bensemble/methods/probabilistic_backpropagation.py
noise_variance
Returns the posterior mean of the observation noise variance.
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Scalar noise variance under the current Gamma posterior. |
Source code in bensemble/methods/probabilistic_backpropagation.py
sample_models
Draws deterministic networks from the fitted weight posterior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_models | int | Number of networks to sample. Defaults to 10. | 10 |
**kwargs | Any | Ignored, accepted for interface compatibility. | {} |
Returns:
| Type | Description |
|---|---|
list[Module] | list[nn.Module]: Sampled networks in eval mode. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If |
Source code in bensemble/methods/probabilistic_backpropagation.py
bensemble.methods.probabilistic_backpropagation.PBPNet
Bases: Module
Network built from ProbLinear layers with analytic moment propagation.
Source code in bensemble/methods/probabilistic_backpropagation.py
forward_moments
Propagate mean/variance through the network.
Returns:
| Type | Description |
|---|---|
(mz, vz) | predictive mean and predictive variance of the network output, |
Tensor | under the current factorized Gaussian approximation over weights. |
Source code in bensemble/methods/probabilistic_backpropagation.py
bensemble.methods.probabilistic_backpropagation.ProbLinear
ProbLinear(in_features: int, out_features: int, dtype: dtype = torch.float64, device: device | None = None)
Bases: Module
Linear layer storing mean/variance parameters for PBP.
Source code in bensemble/methods/probabilistic_backpropagation.py
bensemble.methods.probabilistic_backpropagation.relu_moments
Moment matching for a ReLU applied to a Gaussian random variable.