openGPMP
Open Source Mathematics Package
|
Bayesian Neural Network class. More...
#include <bayes_net.hpp>
Public Member Functions | |
BNN (int input_size, int hidden_size, int output_size, double prior_variance=1.0) | |
Constructor for the BNN class. More... | |
void | fit (const std::vector< std::vector< double >> &X_train, const std::vector< std::vector< double >> &y_train, int epochs=1000) |
Train the Bayesian Neural Network. More... | |
std::vector< double > | predict (const std::vector< double > &input_vector) |
Predict the output for a given input. More... | |
double | activation_function (double x) |
Activation function for the hidden layer. More... | |
double | prior_log_likelihood () |
Compute the log-likelihood of the prior distribution. More... | |
double | log_likelihood (const std::vector< std::vector< double >> &X, const std::vector< std::vector< double >> &y) |
Compute the log-likelihood of the data. More... | |
double | compute_loss (const std::vector< std::vector< double >> &X, const std::vector< std::vector< double >> &y) |
Compute the negative log posterior (loss function) More... | |
void | update_weights (const std::vector< std::vector< double >> &X, const std::vector< std::vector< double >> &y, double learning_rate) |
Update weights using stochastic gradient descent. More... | |
Public Attributes | |
int | input_size |
Number of input features. More... | |
int | hidden_size |
Number of hidden units in the network. More... | |
int | output_size |
Number of output units in the network. More... | |
double | prior_variance |
Variance for the prior distribution. More... | |
std::vector< std::vector< double > > | input_to_hidden_weights |
Weights from input to hidden layer. More... | |
std::vector< double > | hidden_biases |
Biases for the hidden layer. More... | |
std::vector< std::vector< double > > | hidden_to_output_weights |
Weights from hidden to output layer. More... | |
std::vector< double > | output_biases |
Biases for the output layer. More... | |
std::mt19937 | rng |
Mersenne Twister random number generator. More... | |
Bayesian Neural Network class.
Definition at line 45 of file bayes_net.hpp.
BNN::BNN | ( | int | input_size, |
int | hidden_size, | ||
int | output_size, | ||
double | prior_variance = 1.0 |
||
) |
Constructor for the BNN class.
input_size | Number of input features |
hidden_size | Number of hidden units in the network |
output_size | Number of output units in the network |
prior_variance | Variance for the prior distribution (default is 1.0) |
Definition at line 36 of file bayes_net.cpp.
References hidden_biases, hidden_size, hidden_to_output_weights, input_size, input_to_hidden_weights, output_biases, output_size, and rng.
double BNN::activation_function | ( | double | x | ) |
Activation function for the hidden layer.
x | Input value |
Definition at line 69 of file bayes_net.cpp.
Referenced by log_likelihood(), predict(), and update_weights().
double BNN::compute_loss | ( | const std::vector< std::vector< double >> & | X, |
const std::vector< std::vector< double >> & | y | ||
) |
Compute the negative log posterior (loss function)
X | Input features |
y | True labels |
Definition at line 194 of file bayes_net.cpp.
References log_likelihood(), and prior_log_likelihood().
Referenced by fit().
void BNN::fit | ( | const std::vector< std::vector< double >> & | X_train, |
const std::vector< std::vector< double >> & | y_train, | ||
int | epochs = 1000 |
||
) |
Train the Bayesian Neural Network.
X_train | Input training data |
y_train | Target training data |
epochs | Number of training epochs (default is 1000) |
Definition at line 74 of file bayes_net.cpp.
References compute_loss(), and update_weights().
double BNN::log_likelihood | ( | const std::vector< std::vector< double >> & | X, |
const std::vector< std::vector< double >> & | y | ||
) |
Compute the log-likelihood of the data.
X | Input features |
y | True labels |
Definition at line 156 of file bayes_net.cpp.
References activation_function(), hidden_biases, hidden_size, hidden_to_output_weights, input_to_hidden_weights, output_biases, output_size, and prior_variance.
Referenced by compute_loss(), and prior_log_likelihood().
std::vector< double > BNN::predict | ( | const std::vector< double > & | input_vector | ) |
Predict the output for a given input.
input_vector | Input data for prediction |
Definition at line 90 of file bayes_net.cpp.
References activation_function(), hidden_biases, hidden_size, hidden_to_output_weights, input_to_hidden_weights, output_biases, and output_size.
double BNN::prior_log_likelihood | ( | ) |
Compute the log-likelihood of the prior distribution.
Definition at line 115 of file bayes_net.cpp.
References hidden_biases, hidden_size, hidden_to_output_weights, input_size, input_to_hidden_weights, log_likelihood(), output_biases, output_size, and prior_variance.
Referenced by compute_loss().
void BNN::update_weights | ( | const std::vector< std::vector< double >> & | X, |
const std::vector< std::vector< double >> & | y, | ||
double | learning_rate | ||
) |
Update weights using stochastic gradient descent.
X | Input features |
y | True labels |
learning_rate | Learning rate for the update |
Definition at line 200 of file bayes_net.cpp.
References activation_function(), hidden_biases, hidden_size, hidden_to_output_weights, input_size, input_to_hidden_weights, output_biases, and output_size.
Referenced by fit().
std::vector<double> BNN::hidden_biases |
Biases for the hidden layer.
Definition at line 105 of file bayes_net.hpp.
Referenced by BNN(), log_likelihood(), predict(), prior_log_likelihood(), and update_weights().
int BNN::hidden_size |
Number of hidden units in the network.
Definition at line 85 of file bayes_net.hpp.
Referenced by BNN(), log_likelihood(), predict(), prior_log_likelihood(), and update_weights().
std::vector<std::vector<double> > BNN::hidden_to_output_weights |
Weights from hidden to output layer.
Definition at line 110 of file bayes_net.hpp.
Referenced by BNN(), log_likelihood(), predict(), prior_log_likelihood(), and update_weights().
int BNN::input_size |
Number of input features.
Definition at line 80 of file bayes_net.hpp.
Referenced by BNN(), prior_log_likelihood(), and update_weights().
std::vector<std::vector<double> > BNN::input_to_hidden_weights |
Weights from input to hidden layer.
Definition at line 100 of file bayes_net.hpp.
Referenced by BNN(), log_likelihood(), predict(), prior_log_likelihood(), and update_weights().
std::vector<double> BNN::output_biases |
Biases for the output layer.
Definition at line 115 of file bayes_net.hpp.
Referenced by BNN(), log_likelihood(), predict(), prior_log_likelihood(), and update_weights().
int BNN::output_size |
Number of output units in the network.
Definition at line 90 of file bayes_net.hpp.
Referenced by BNN(), log_likelihood(), predict(), prior_log_likelihood(), and update_weights().
double BNN::prior_variance |
Variance for the prior distribution.
Definition at line 95 of file bayes_net.hpp.
Referenced by log_likelihood(), and prior_log_likelihood().
std::mt19937 BNN::rng |
Mersenne Twister random number generator.
Definition at line 120 of file bayes_net.hpp.
Referenced by BNN().