openGPMP
Open Source Mathematics Package
bayes_net.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
34 #ifndef BNN_HPP
35 #define BNN_HPP
36 
37 #include <cmath>
38 #include <random>
39 #include <vector>
40 
45 class BNN {
46  public:
55  BNN(int input_size,
56  int hidden_size,
57  int output_size,
58  double prior_variance = 1.0);
59 
66  void fit(const std::vector<std::vector<double>> &X_train,
67  const std::vector<std::vector<double>> &y_train,
68  int epochs = 1000);
69 
75  std::vector<double> predict(const std::vector<double> &input_vector);
76 
81 
86 
91 
96 
100  std::vector<std::vector<double>> input_to_hidden_weights;
101 
105  std::vector<double> hidden_biases;
106 
110  std::vector<std::vector<double>> hidden_to_output_weights;
111 
115  std::vector<double> output_biases;
116 
120  std::mt19937 rng;
121 
127  double activation_function(double x);
128 
133  double prior_log_likelihood();
134 
141  double log_likelihood(const std::vector<std::vector<double>> &X,
142  const std::vector<std::vector<double>> &y);
143 
150  double compute_loss(const std::vector<std::vector<double>> &X,
151  const std::vector<std::vector<double>> &y);
152 
159  void update_weights(const std::vector<std::vector<double>> &X,
160  const std::vector<std::vector<double>> &y,
161  double learning_rate);
162 };
163 
164 #endif
Bayesian Neural Network class.
Definition: bayes_net.hpp:45
std::vector< double > hidden_biases
Biases for the hidden layer.
Definition: bayes_net.hpp:105
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.
Definition: bayes_net.cpp:74
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.
Definition: bayes_net.cpp:200
int hidden_size
Number of hidden units in the network.
Definition: bayes_net.hpp:85
int input_size
Number of input features.
Definition: bayes_net.hpp:80
BNN(int input_size, int hidden_size, int output_size, double prior_variance=1.0)
Constructor for the BNN class.
Definition: bayes_net.cpp:36
double log_likelihood(const std::vector< std::vector< double >> &X, const std::vector< std::vector< double >> &y)
Compute the log-likelihood of the data.
Definition: bayes_net.cpp:156
double compute_loss(const std::vector< std::vector< double >> &X, const std::vector< std::vector< double >> &y)
Compute the negative log posterior (loss function)
Definition: bayes_net.cpp:194
double activation_function(double x)
Activation function for the hidden layer.
Definition: bayes_net.cpp:69
std::mt19937 rng
Mersenne Twister random number generator.
Definition: bayes_net.hpp:120
double prior_log_likelihood()
Compute the log-likelihood of the prior distribution.
Definition: bayes_net.cpp:115
std::vector< std::vector< double > > input_to_hidden_weights
Weights from input to hidden layer.
Definition: bayes_net.hpp:100
double prior_variance
Variance for the prior distribution.
Definition: bayes_net.hpp:95
std::vector< std::vector< double > > hidden_to_output_weights
Weights from hidden to output layer.
Definition: bayes_net.hpp:110
int output_size
Number of output units in the network.
Definition: bayes_net.hpp:90
std::vector< double > output_biases
Biases for the output layer.
Definition: bayes_net.hpp:115
std::vector< double > predict(const std::vector< double > &input_vector)
Predict the output for a given input.
Definition: bayes_net.cpp:90