Represents a fully connected feedforward neural network.
More...
#include <neural_network.hpp>
|
| 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.
|
|
| NeuralNetwork (const NeuralNetwork &other) |
| Copy constructor.
|
|
| ~NeuralNetwork () |
| Destructor for the neural network.
|
|
NeuralNetwork & | operator= (NeuralNetwork &&other) noexcept |
| Move assignment operator.
|
|
std::vector< double > | predict (const std::vector< double > &input) |
| Predicts the output for a given input vector.
|
|
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.
|
|
double | compute_mse_loss (const std::vector< double > &prediction, const std::vector< double > &target) |
| Computes the mean squared error (MSE) loss.
|
|
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.
|
|
bool | is_correct_prediction (const std::vector< double > &prediction, const std::vector< double > &target) |
| Determines whether a prediction is correct based on a target.
|
|
void | save_model (const std::string &filename) |
| Saves the current state of the neural network to a file.
|
|
This class provides methods for creating, training, and using neural networks. It supports:
- Customizable activation functions.
- Training via backpropagation with mean squared error (MSE) loss.
- Saving and loading models to/from files.
Definition at line 64 of file neural_network.hpp.
◆ NeuralNetwork() [1/2]
chisei::NeuralNetwork::NeuralNetwork |
( |
const std::vector< size_t > & |
_layers, |
|
|
std::function< double(double)> |
_activation, |
|
|
std::function< double(double)> |
_activation_derivative |
|
) |
| |
- Parameters
-
_layers | A vector specifying the number of neurons in each layer. |
_activation | The activation function to use in the network. |
_activation_derivative | The derivative of the activation function. |
◆ NeuralNetwork() [2/2]
chisei::NeuralNetwork::NeuralNetwork |
( |
const NeuralNetwork & |
other | ) |
|
Creates a deep copy of the given neural network.
- Parameters
-
other | The neural network to copy. |
◆ ~NeuralNetwork()
chisei::NeuralNetwork::~NeuralNetwork |
( |
| ) |
|
◆ compute_accuracy()
double chisei::NeuralNetwork::compute_accuracy |
( |
const std::vector< std::vector< double > > & |
inputs, |
|
|
const std::vector< std::vector< double > > & |
targets |
|
) |
| |
- Parameters
-
inputs | The input data. |
targets | The expected outputs. |
- Returns
- The accuracy as a percentage (0.0 to 100.0).
◆ compute_mse_loss()
double chisei::NeuralNetwork::compute_mse_loss |
( |
const std::vector< double > & |
prediction, |
|
|
const std::vector< double > & |
target |
|
) |
| |
- Parameters
-
prediction | The predicted output vector. |
target | The expected output vector. |
- Returns
- The computed MSE loss.
◆ compute_output_gradient()
std::vector< double > chisei::NeuralNetwork::compute_output_gradient |
( |
const std::vector< double > & |
prediction, |
|
|
const std::vector< double > & |
target |
|
) |
| |
- Parameters
-
prediction | The predicted output vector. |
target | The expected output vector. |
- Returns
- The gradient vector.
◆ is_correct_prediction()
bool chisei::NeuralNetwork::is_correct_prediction |
( |
const std::vector< double > & |
prediction, |
|
|
const std::vector< double > & |
target |
|
) |
| |
- Parameters
-
prediction | The predicted output vector. |
target | The expected output vector. |
- Returns
- True if the prediction is correct; otherwise, false.
◆ loadFromModel()
static NeuralNetwork chisei::NeuralNetwork::loadFromModel |
( |
const std::string & |
filename | ) |
|
|
static |
- Parameters
-
filename | The name of the file to load the model from. |
- Returns
- A NeuralNetwork object initialized from the file data.
- Exceptions
-
std::ios_base::failure | if the file cannot be read or is malformed. |
◆ operator=()
Moves the contents of another neural network into this instance.
- Parameters
-
other | The neural network to move from. |
- Returns
- A reference to the current instance.
◆ predict()
std::vector< double > chisei::NeuralNetwork::predict |
( |
const std::vector< double > & |
input | ) |
|
Performs a forward pass through the network to compute the output.
- Parameters
-
- Returns
- The output vector.
◆ save_model()
void chisei::NeuralNetwork::save_model |
( |
const std::string & |
filename | ) |
|
- Parameters
-
filename | The name of the file to save the model to. |
- Exceptions
-
std::ios_base::failure | if the file cannot be written. |
◆ train()
void chisei::NeuralNetwork::train |
( |
const std::vector< std::vector< double > > & |
inputs, |
|
|
const std::vector< std::vector< double > > & |
targets, |
|
|
double |
learning_rate = 0.1 , |
|
|
int |
epochs = 10000 |
|
) |
| |
Uses backpropagation and gradient descent to minimize the loss function.
- Parameters
-
inputs | The training input data. |
targets | The expected output data corresponding to the inputs. |
learning_rate | The learning rate for gradient descent (default = 0.1). |
epochs | The number of training iterations (default = 10,000). |
The documentation for this class was generated from the following file: