GaussianPosterior#

class torchbayesian.bnn.GaussianPosterior(shape: Size | list[int] | tuple[int, ...], *, dtype: dtype | None = None, device: device | str | int | None = None)#

Bases: VariationalPosterior

This class is a diagonal Gaussian variational posterior.

Samples tensors via the reparametrization trick.

This is a commonly used variational posterior for Bayes by Backprop (BBB) variational inference (VI), as described in “Weight Uncertainty in Neural Networks” by Blundell et al.

Parameters#

shape_size

The shape of the parameter being replaced by the variational posterior.

dtypeOptional[_dtype]

The dtype of the parameter being replaced by the variational posterior. Optional. Defaults to torch default dtype.

deviceDevice

The device of the parameter being replaced by the variational posterior. Optional. Defaults to torch default device.

Attributes#

muParameter

The variational parameter of the mean of the distribution.

rhoParameter

The variational parameter that parametrizes the standard deviation of the distribution via softplus.

reset_parameters() None#

Initializes the variational parameters ‘mu’ and ‘rho’ of the Gaussian posterior N(mu, sigma), where sigma = softplus(rho).

property sigma: Tensor#

Returns the standard deviation parameter of the Gaussian distribution.

Returns#

sigmaTensor

The standard deviation parameter of the Gaussian distribution

property distribution: Normal#

Returns a ‘torch.distributions.Normal’ for KL divergence computation.

Returns#

distributionNormal

A diagonal Gaussian distribution.

sample_parameters() Tensor#

Samples parameter values from the variational posterior distribution.

Returns#

paramTensor

A value of the parameter, sampled from the variational posterior.