Bayesian Layers
Drop-in replacements for standard torch.nn layers that implement the Local Reparameterization Trick (LRT).
Bayesian Linear
bensemble.layers.linear.BayesianLinear
BayesianLinear(
in_features: int,
out_features: int,
prior_sigma: float = 1.0,
init_sigma: float = 0.1,
weight_init: str = "kaiming",
)
Bases: BaseBayesianLayer
Bayesian Linear layer with Local Reparameterization Trick.
Weights and biases are parameterized as Gaussian distributions with learnable means and standard deviations (parametrized by rho).
Initializes the BayesianLinear layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features | int | Size of each input sample. | required |
out_features | int | Size of each output sample. | required |
prior_sigma | float | Standard deviation of the prior Gaussian distribution. Defaults to 1.0. | 1.0 |
init_sigma | float | Initial standard deviation for posterior distributions. Defaults to 0.1. | 0.1 |
weight_init | str | Weight initialization scheme ("kaiming", "xavier", or "normal"). Defaults to "kaiming". | 'kaiming' |
Source code in bensemble/layers/linear.py
forward
Applies linear transformation using deterministic means in eval mode or LRT sampling in train mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | Input tensor of shape (..., in_features). | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Output tensor of shape (..., out_features). |
Source code in bensemble/layers/linear.py
reset_parameters
Initializes layer weights and bias parameters.
Source code in bensemble/layers/linear.py
Bayesian Conv2d
bensemble.layers.conv.BayesianConv2d
BayesianConv2d(
in_channels: int,
out_channels: int,
kernel_size: int | tuple[int, int],
stride: int | tuple[int, int] = 1,
padding: int | tuple[int, int] = 0,
dilation: int | tuple[int, int] = 1,
groups: int = 1,
prior_sigma: float = 1.0,
init_sigma: float = 0.1,
)
Bases: BaseBayesianLayer
Bayesian 2D Convolutional layer with Local Reparameterization Trick.
Initializes the BayesianConv2d layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels | int | Number of channels in the input image. | required |
out_channels | int | Number of channels produced by the convolution. | required |
kernel_size | int | tuple[int, int] | Size of the convolving kernel. | required |
stride | int | tuple[int, int] | Stride of the convolution. Defaults to 1. | 1 |
padding | int | tuple[int, int] | Zero-padding added to both sides of the input. Defaults to 0. | 0 |
dilation | int | tuple[int, int] | Spacing between kernel elements. Defaults to 1. | 1 |
groups | int | Number of blocked connections from input to output channels. Defaults to 1. | 1 |
prior_sigma | float | Standard deviation of the Gaussian prior distribution. Defaults to 1.0. | 1.0 |
init_sigma | float | Initial standard deviation for posterior parameters. Defaults to 0.1. | 0.1 |
Source code in bensemble/layers/conv.py
forward
Executes forward pass using deterministic means in eval mode or LRT sampling in train mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | Input tensor of shape (batch_size, in_channels, height, width). | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Convolved output tensor of shape (batch_size, out_channels, out_height, out_width). |
Source code in bensemble/layers/conv.py
reset_parameters
Initializes layer weights and bias parameters.
Source code in bensemble/layers/conv.py
Base Class
bensemble.layers.base.BaseBayesianLayer
Bases: Module
Base class for all bayesian layers.
Computes KL-divergence automatically for all parameters ending with _mu and _rho.
Source code in bensemble/layers/base.py
apply_pruning
Applies pruning in-place: zeros out the means and minimizes the variance for weights that fall below the SNR threshold.
Returns:
| Name | Type | Description |
|---|---|---|
float | float | Sparsity of the layer (percentage of pruned weights, 0.0 to 1.0). |
Source code in bensemble/layers/base.py
get_pruning_masks
Returns binary masks for parameters satisfying the SNR threshold.
Implements Graves' pruning heuristic where weights with low Signal-to-Noise Ratio are considered redundant and can be removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold | float | The SNR threshold (|mu|/sigma). Defaults to 0.83, the "safe" threshold suggested by Graves. | 0.83 |
Returns:
| Type | Description |
|---|---|
dict | dict[str, torch.Tensor]: A dictionary mapping parameter names to binary masks (1.0 for keeping, 0.0 for pruning). |
Source code in bensemble/layers/base.py
kl_divergence
Computes KL-divergence KL(q || p) for all bayesian weights of the layer. p(w) = N(0, prior_sigma^2) q(w) = N(mu, sigma^2), where sigma = softplus(rho)