mindspore.nn.probability.bnn_layers.DenseReparam

class mindspore.nn.probability.bnn_layers.DenseReparam(in_channels, out_channels, activation=None, has_bias=True, weight_prior_fn=NormalPrior, weight_posterior_fn=normal_post_fn, bias_prior_fn=NormalPrior, bias_posterior_fn=normal_post_fn)[source]

Dense variational layers with Reparameterization.

For more details, refer to the paper Auto-Encoding Variational Bayes.

Applies dense-connected layer to the input. This layer implements the operation as:

\[\text{outputs} = \text{activation}(\text{inputs} * \text{weight} + \text{bias}),\]

where \(\text{activation}\) is the activation function passed as the activation argument (if passed in), \(\text{activation}\) is a weight matrix with the same data type as the inputs created by the layer, \(\text{weight}\) is a weight matrix sampling from posterior distribution of weight, and \(\text{bias}\) is a bias vector with the same data type as the inputs created by the layer (only if has_bias is True). The bias vector is sampling from posterior distribution of \(\text{bias}\).

Parameters
  • in_channels (int) – The number of input channel.

  • out_channels (int) – The number of output channel .

  • activation (str, Cell) – A regularization function applied to the output of the layer. The type of activation can be a string (eg. ‘relu’) or a Cell (eg. nn.ReLU()). Note that if the type of activation is Cell, it must be instantiated beforehand. Default: None.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_prior_fn (Cell) – The prior distribution for weight. It must return a mindspore distribution instance. Default: NormalPrior. (which creates an instance of standard normal distribution). The current version only supports normal distribution.

  • weight_posterior_fn (function) – The posterior distribution for sampling weight. It must be a function handle which returns a mindspore distribution instance. Default: normal_post_fn. The current version only supports normal distribution.

  • bias_prior_fn (Cell) – The prior distribution for bias vector. It must return a mindspore distribution. Default: NormalPrior(which creates an instance of standard normal distribution). The current version only supports normal distribution.

  • bias_posterior_fn (function) – The posterior distribution for sampling bias vector. It must be a function handle which returns a mindspore distribution instance. Default: normal_post_fn. The current version only supports normal distribution.

Inputs:
  • input (Tensor) - The shape of the tensor is \((N, in\_channels)\).

Outputs:

Tensor, the shape of the tensor is \((N, out\_channels)\).

Supported Platforms:

Ascend GPU

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor
>>> from mindspore.nn.probability import bnn_layers
>>> net = bnn_layers.DenseReparam(3, 4)
>>> input = Tensor(np.random.randint(0, 255, [2, 3]), mindspore.float32)
>>> output = net(input).shape
>>> print(output)
(2, 4)