Bayesian Layer Baseclass
This is the parent class to all Bayesian layers.
The NormalBayesianLayer is an abstract baseclass and a subclass of torch.nn.Module.
The documentation in the forward() and extra_repr() on this page may be inherited from PyTorch docstrings.
Methods
- class NormalBayesianLayer(parameter_shapes, sampling=True, prior_mu=0.0, prior_sigma=0.1, posterior_mu_initial=(0.0, 0.1), posterior_rho_initial=(-3.0, 0.1), device=None, dtype=None)[source]
Initialize the random variables governing the parameters of the layer.
- Parameters:
parameter_shapes (
dict) – Dictionary with"name":shapepairs for each parameter. For each key in the dictionary, assign learnable parametersname_muandname_rhowith the given shape.sampling (
bool) – IfTrue, sample layer parameters from their respective Gaussian distributions. IfFalse, use distribution mean as parameter values. Default:Trueprior_mu (
float) – Prior mean, \(\mu_\text{prior}\) of the prior normal distribution. Default: 0.0prior_sigma (
float) – Prior standard deviation, \(\sigma_\text{prior}\), of the prior normal distribution. Default: 0.1posterior_mu_initial (
tuple[float,float]) – Mean and standard deviation of the initial posterior distribution for \(\mu\). The initial posterior is \(\mathcal{N}(\mu_\text{posterior}[0], \mu_\text{posterior}[1])\). Default: (0.0, 0.1)posterior_rho_initial (
tuple[float,float]) – Mean and standard deviation of the initial posterior distribution for \(\rho\). The initial posterior is \(\mathcal{N}(\rho_\text{posterior}[0], \rho_\text{posterior}[1])\). The standard deviation of the posterior is computed as \(\sigma = \ln( 1 + \exp(\rho))\) to ensure it is positive. Default: (-3.0, 0.1)device (
Union[device,str,None]) – Atorch.devicerepresenting the device on which tensors are allocateddtype (
Union[dtype,tuple,None]) – Atorch.dtype(or tuple of them) representing the data type of the tensor
- get_bayesian_weights()[source]
Get the weights for the Bayesian layer.
If
samplingisTrue, then sample weights from their respective distributions. Otherwise, use distribution means for weights.- Return type:
- Returns:
Tuple containing weight tensors
- sample(mode=True)[source]
Set sampling mode for this and all child Modules
Note
This method and
self.samplingonly affects Bayesian layers- Parameters:
mode (
bool) – IfTrue, sample from distributions, otherwise use distribution means. Default:True
- abstract forward(*args, **kwargs)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
Attributes
- NormalBayesianLayer.parameter_shapes: dict
Prefix names and shapes of all learnable parameters
- NormalBayesianLayer.sampling: bool
Boolean represents whether this module is in sampling mode or not.
- NormalBayesianLayer.prior_mu: float
- NormalBayesianLayer.prior_sigma: float
- NormalBayesianLayer.posterior_mu_initial: tuple[float, float]
- NormalBayesianLayer.posterior_rho_initial: tuple[float, float]