BayesianNeuralNetwork<T>
Models & Types · AiDotNet.UncertaintyQuantification.BayesianNeuralNetworks
Implements a Bayesian Neural Network that provides uncertainty estimates with predictions.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → VectorModelLayoutBase<T> → SequentialVectorModelLayoutBase<T> → NeuralNetwork<T> → BayesianNeuralNetwork<T>
Implements: IUncertaintyEstimator<T>
For Beginners
A Bayesian Neural Network (BNN) is a neural network that can tell you not just what it predicts, but also how uncertain it is about that prediction.
This is incredibly important for safety-critical applications like:
- Medical diagnosis: “This might be cancer, but I’m very uncertain - get a second opinion”
- Autonomous driving: “I’m not sure what that object is - proceed with caution”
- Financial predictions: “The market might go up, but there’s high uncertainty”
The network achieves this by making multiple predictions with slightly different weights (sampled from learned probability distributions) and analyzing how much these predictions vary.
Example
var inputTensor = Tensor<float>.CreateRandom(2, 4);
// Create a Bayesian Neural Network with uncertainty estimation
var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 10, outputSize: 1);
var bnn = new BayesianNeuralNetwork<float>(architecture, options: new BayesianNeuralNetworkOptions { NumSamples = 30 });
// Get prediction with uncertainty via multiple weight-sampled forward passes
var result = bnn.PredictWithUncertainty(inputTensor);
// result.Mean, result.Variance provide prediction and uncertainty
Constructors
| Constructor | Summary |
|---|---|
BayesianNeuralNetwork(NeuralNetworkArchitecture<T> architecture, BayesianNeuralNetworkOptions options) |
Initializes a new instance of the BayesianNeuralNetwork class. |
Properties
| Property | Summary |
|---|---|
bool SupportsTraining { get; } |
Methods
| Method | Summary |
|---|---|
T ComputeKLDivergence() |
Computes the total KL divergence from all Bayesian layers. |
Tensor<T> EstimateAleatoricUncertainty(Tensor<T> input) |
Estimates aleatoric (data) uncertainty. |
Tensor<T> EstimateEpistemicUncertainty(Tensor<T> input) |
Estimates epistemic (model) uncertainty. |
UncertaintyPredictionResult<T, Tensor<T>> PredictWithUncertainty(Tensor<T> input) |
Predicts output with uncertainty estimates. |