36 BNN::BNN(
int in_size,
int h_size,
int out_size,
double p_variance)
37 : input_size(in_size), hidden_size(h_size), output_size(out_size),
38 prior_variance(p_variance), rng(std::random_device{}()) {
40 std::normal_distribution<double> normal_distribution(0.0, 1.0);
71 return 1.0 / (1.0 + std::exp(-x));
74 void BNN::fit(
const std::vector<std::vector<double>> &X_train,
75 const std::vector<std::vector<double>> &y_train,
77 const double learning_rate = 0.01;
79 for (
int epoch = 0; epoch < epochs; ++epoch) {
83 if (epoch % 100 == 0) {
85 std::cout <<
"Epoch: " << epoch <<
", Loss: " << loss << std::endl;
90 std::vector<double>
BNN::predict(
const std::vector<double> &input_vector) {
95 std::inner_product(input_vector.begin(),
105 std::inner_product(hidden_output.begin(),
157 const std::vector<std::vector<double>> &y) {
161 for (
size_t i = 0; i < X.size(); ++i) {
166 std::inner_product(X[i].begin(),
176 std::inner_product(hidden_output.begin(),
195 const std::vector<std::vector<double>> &y) {
201 const std::vector<std::vector<double>> &y,
202 double learning_rate) {
204 for (
size_t i = 0; i < X.size(); ++i) {
209 std::inner_product(X[i].begin(),
219 std::inner_product(hidden_output.begin(),
229 double output_gradient =
230 (y[i][j] - output[j]) * output[j] * (1.0 - output[j]);
235 learning_rate * output_gradient * hidden_output[k];
245 double hidden_gradient = 0.0;
248 (y[i][k] - output[k]) * output[k] *
251 hidden_gradient *= hidden_output[j] * (1.0 - hidden_output[j]);
256 learning_rate * hidden_gradient * X[i][k];
std::vector< double > hidden_biases
Biases for the hidden layer.
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.
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.
int hidden_size
Number of hidden units in the network.
int input_size
Number of input features.
BNN(int input_size, int hidden_size, int output_size, double prior_variance=1.0)
Constructor for the BNN class.
double log_likelihood(const std::vector< std::vector< double >> &X, const std::vector< std::vector< double >> &y)
Compute the log-likelihood of the data.
double compute_loss(const std::vector< std::vector< double >> &X, const std::vector< std::vector< double >> &y)
Compute the negative log posterior (loss function)
double activation_function(double x)
Activation function for the hidden layer.
std::mt19937 rng
Mersenne Twister random number generator.
double prior_log_likelihood()
Compute the log-likelihood of the prior distribution.
std::vector< std::vector< double > > input_to_hidden_weights
Weights from input to hidden layer.
double prior_variance
Variance for the prior distribution.
std::vector< std::vector< double > > hidden_to_output_weights
Weights from hidden to output layer.
int output_size
Number of output units in the network.
std::vector< double > output_biases
Biases for the output layer.
std::vector< double > predict(const std::vector< double > &input_vector)
Predict the output for a given input.