Chisei v1.0
Lightweight AI/ML Framework
Loading...
Searching...
No Matches
neural_network.hpp
Go to the documentation of this file.
1/*
2 *
3 * Copyright 2025 Nathanne Isip
4 *
5 * Redistribution and use in source and binary forms,
6 * with or without modification, are permitted provided
7 * that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the
10 * above copyright notice, this list of conditions
11 * and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce
14 * the above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 *
33 */
34
41#ifndef CHISEI_NEURAL_NETWORK_HPP
42#define CHISEI_NEURAL_NETWORK_HPP
43
44#include <exception>
45#include <fstream>
46#include <functional>
47#include <random>
48#include <vector>
49
52
53namespace chisei {
54
65 private:
66
75 std::vector<size_t> layer_sizes;
76
84 std::vector<std::vector<std::vector<double>>> weights;
85
91 std::vector<std::vector<double>> biases;
92
98 std::function<double(double)> activation;
99
105 std::function<double(double)> activation_derivative;
106
110 std::random_device rd;
111
115 std::mt19937 gen{rd()};
116
122 std::normal_distribution<> weight_dist{0, 0.1};
123
124 public:
133 const std::vector<size_t>& _layers,
134 std::function<double(double)> _activation,
135 std::function<double(double)> _activation_derivative
136 );
137
146
151
161
170 std::vector<double> predict(const std::vector<double>& input);
171
182 void train(
183 const std::vector<std::vector<double>>& inputs,
184 const std::vector<std::vector<double>>& targets,
185 double learning_rate = 0.1,
186 int epochs = 10000
187 );
188
197 const std::vector<double>& prediction,
198 const std::vector<double>& target
199 );
200
208 std::vector<double> compute_output_gradient(
209 const std::vector<double>& prediction,
210 const std::vector<double>& target
211 );
212
221 const std::vector<std::vector<double>>& inputs,
222 const std::vector<std::vector<double>>& targets
223 );
224
233 const std::vector<double>& prediction,
234 const std::vector<double>& target
235 );
236
244 void save_model(const std::string& filename);
245
254 static NeuralNetwork loadFromModel(const std::string& filename);
255 };
256}
257
258#endif
Represents a fully connected feedforward neural network.
double compute_mse_loss(const std::vector< double > &prediction, const std::vector< double > &target)
Computes the mean squared error (MSE) loss.
NeuralNetwork(const NeuralNetwork &other)
Copy constructor.
NeuralNetwork & operator=(NeuralNetwork &&other) noexcept
Move assignment operator.
NeuralNetwork(const std::vector< size_t > &_layers, std::function< double(double)> _activation, std::function< double(double)> _activation_derivative)
Constructs a neural network with the specified layers and activation functions.
static NeuralNetwork loadFromModel(const std::string &filename)
Loads a neural network from a saved model file.
bool is_correct_prediction(const std::vector< double > &prediction, const std::vector< double > &target)
Determines whether a prediction is correct based on a target.
~NeuralNetwork()
Destructor for the neural network.
void train(const std::vector< std::vector< double > > &inputs, const std::vector< std::vector< double > > &targets, double learning_rate=0.1, int epochs=10000)
Trains the neural network using the provided training data.
void save_model(const std::string &filename)
Saves the current state of the neural network to a file.
std::vector< double > compute_output_gradient(const std::vector< double > &prediction, const std::vector< double > &target)
Computes the gradient of the loss with respect to the output layer.
double compute_accuracy(const std::vector< std::vector< double > > &inputs, const std::vector< std::vector< double > > &targets)
Computes the accuracy of the network on a dataset.
std::vector< double > predict(const std::vector< double > &input)
Predicts the output for a given input vector.