pub struct BayesianNeuralNetwork<F> {
pub architecture: Vec<usize>,
pub activations: Vec<ActivationType>,
pub weight_priors: Vec<DistributionType<F>>,
pub bias_priors: Vec<DistributionType<F>>,
pub weight_samples: Option<Vec<Vec<Array2<F>>>>,
pub bias_samples: Option<Vec<Vec<Array1<F>>>>,
}Expand description
Bayesian neural network implementation
Fields§
§architecture: Vec<usize>Network architecture
activations: Vec<ActivationType>Activation functions per layer
weight_priors: Vec<DistributionType<F>>Weight priors
bias_priors: Vec<DistributionType<F>>Bias priors
weight_samples: Option<Vec<Vec<Array2<F>>>>Trained posterior ensemble of weights: weight_samples[m][l] is the
weight matrix of layer l for ensemble member m. Populated by
BayesianNeuralNetwork::fit; None until then.
bias_samples: Option<Vec<Vec<Array1<F>>>>Trained posterior ensemble of biases: bias_samples[m][l] is the bias
vector of layer l for ensemble member m. Populated by
BayesianNeuralNetwork::fit; None until then.
Implementations§
Source§impl<F: AdvancedBayesianFloat> BayesianNeuralNetwork<F>
impl<F: AdvancedBayesianFloat> BayesianNeuralNetwork<F>
Sourcepub fn fit(
&mut self,
x: &ArrayView2<'_, F>,
y: &ArrayView2<'_, F>,
config: &BnnTrainingConfig,
) -> StatsResult<()>
pub fn fit( &mut self, x: &ArrayView2<'_, F>, y: &ArrayView2<'_, F>, config: &BnnTrainingConfig, ) -> StatsResult<()>
Train a deep ensemble of posterior weight/bias samples: each member is
randomly initialized from the network’s priors, optionally
bootstrap-resampled, and trained to a local optimum of the mean
squared error via exact backpropagation + gradient descent. Populates
self.weight_samples / self.bias_samples, which
Self::predict_with_uncertainty then draws from.
Sourcepub fn predict_with_uncertainty(
&self,
x: &ArrayView2<'_, F>,
n_samples_: usize,
) -> StatsResult<(Array2<F>, Array2<F>)>
pub fn predict_with_uncertainty( &self, x: &ArrayView2<'_, F>, n_samples_: usize, ) -> StatsResult<(Array2<F>, Array2<F>)>
Make predictions with uncertainty quantification.
If Self::fit has already trained a posterior ensemble, the ensemble
is a small finite population (typically a handful to a few dozen
members) whose exact predictive mean/variance is directly computable,
so this forward-propagates through every trained member exactly
once (n_samples_ is ignored in this case: randomly resampling a
finite, fully-known population with replacement would only add
spurious Monte Carlo noise around a quantity that has no randomness
left to estimate). Otherwise – no ensemble has been trained yet –
this falls back to real prior-predictive Monte Carlo: n_samples_
independent draws of the whole network from its priors, each
forward-propagated through the actual input x, where genuine random
sampling is unavoidable since the prior is a continuous distribution.
Either way, the reported mean/variance are genuine empirical
statistics of real forward passes – never a constant placeholder.
Source§impl<F: AdvancedBayesianFloat> BayesianNeuralNetwork<F>
impl<F: AdvancedBayesianFloat> BayesianNeuralNetwork<F>
Sourcepub fn new(
architecture: Vec<usize>,
activations: Vec<ActivationType>,
) -> StatsResult<Self>
pub fn new( architecture: Vec<usize>, activations: Vec<ActivationType>, ) -> StatsResult<Self>
Create new Bayesian neural network
Sourcepub fn forward(
&self,
x: &ArrayView2<'_, F>,
weights: &[Array2<F>],
biases: &[Array1<F>],
) -> StatsResult<Array2<F>>
pub fn forward( &self, x: &ArrayView2<'_, F>, weights: &[Array2<F>], biases: &[Array1<F>], ) -> StatsResult<Array2<F>>
Forward pass through the network
Trait Implementations§
Source§impl<F: Clone> Clone for BayesianNeuralNetwork<F>
impl<F: Clone> Clone for BayesianNeuralNetwork<F>
Source§fn clone(&self) -> BayesianNeuralNetwork<F>
fn clone(&self) -> BayesianNeuralNetwork<F>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<F> !RefUnwindSafe for BayesianNeuralNetwork<F>
impl<F> !UnwindSafe for BayesianNeuralNetwork<F>
impl<F> Freeze for BayesianNeuralNetwork<F>
impl<F> Send for BayesianNeuralNetwork<F>where
F: Send,
impl<F> Sync for BayesianNeuralNetwork<F>where
F: Sync,
impl<F> Unpin for BayesianNeuralNetwork<F>where
F: Unpin,
impl<F> UnsafeUnpin for BayesianNeuralNetwork<F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.