BayesianDenseLayer<T>

Layers · AiDotNet.UncertaintyQuantification.Layers

Implements a Bayesian dense (fully-connected) layer using variational inference.

Inheritance: LayerBase<T> → BayesianDenseLayer<T>

Implements: IBayesianLayer<T>, IShapeContract

For Beginners

A Bayesian Dense Layer is similar to a regular dense layer, but instead of having fixed weights, it learns probability distributions over weights.

This is based on the “Bayes by Backprop” algorithm which uses variational inference to approximate the true posterior distribution of weights.

The layer maintains two sets of parameters for each weight:

  • Mean (μ): The average value of the weight
  • Standard deviation (σ): How much the weight varies

During forward passes, weights are sampled from these distributions, allowing the network to express uncertainty in its predictions.

Constructors

Constructor Summary
BayesianDenseLayer(int inputSize, int outputSize, IActivationFunction<T> scalarActivation, double priorSigma, int? randomSeed) Initializes a new instance of the BayesianDenseLayer class with a custom activation.
BayesianDenseLayer(int inputSize, int outputSize, double priorSigma, int? randomSeed) Initializes a new instance of the BayesianDenseLayer class.

Properties

Property Summary
bool SupportsTraining { get; } Gets a value indicating whether this layer supports training mode.

Methods

Method Summary
Dictionary<string, string> GetParameterRoles() Returns parameter roles for per-role optimizer configuration (e.g., weight decay exemption for biases).
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank)
IReadOnlyList<Tensor<T>> GetTrainableParameters() Returns all trainable parameter tensors marked with [TrainableParameter].
T GetKLDivergence() Computes the KL divergence between the weight distribution and the prior.
void AddKLDivergenceGradients(T klScale)
void ClearGradients()
void ResetState() Resets the internal state of the layer.
void SampleWeights() Samples weights from the learned distributions.
void SetTrainableParameters(IReadOnlyList<Tensor<T>> parameters) Replaces trainable parameter tensors (e.g., with ParameterBuffer views).
void UpdateParameters(T learningRate)
void UpdateParameters(Vector<T> parameters)
void ZeroGrad() Clears all gradient fields discovered by convention ({paramName}Gradient).