Chisei v1.0
Lightweight AI/ML Framework
Loading...
Searching...
No Matches
chisei::NeuralNetwork Class Reference

Represents a fully connected feedforward neural network. More...

#include <neural_network.hpp>

Public Member Functions

 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.
 
NeuralNetworkoperator= (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.
 

Static Public Member Functions

static NeuralNetwork loadFromModel (const std::string &filename)
 Loads a neural network from a saved model file.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ NeuralNetwork() [1/2]

chisei::NeuralNetwork::NeuralNetwork ( const std::vector< size_t > &  _layers,
std::function< double(double)>  _activation,
std::function< double(double)>  _activation_derivative 
)
Parameters
_layersA vector specifying the number of neurons in each layer.
_activationThe activation function to use in the network.
_activation_derivativeThe derivative of the activation function.

◆ NeuralNetwork() [2/2]

chisei::NeuralNetwork::NeuralNetwork ( const NeuralNetwork other)

Creates a deep copy of the given neural network.

Parameters
otherThe neural network to copy.

◆ ~NeuralNetwork()

chisei::NeuralNetwork::~NeuralNetwork ( )

Member Function Documentation

◆ compute_accuracy()

double chisei::NeuralNetwork::compute_accuracy ( const std::vector< std::vector< double > > &  inputs,
const std::vector< std::vector< double > > &  targets 
)
Parameters
inputsThe input data.
targetsThe 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
predictionThe predicted output vector.
targetThe 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
predictionThe predicted output vector.
targetThe 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
predictionThe predicted output vector.
targetThe expected output vector.
Returns
True if the prediction is correct; otherwise, false.

◆ loadFromModel()

static NeuralNetwork chisei::NeuralNetwork::loadFromModel ( const std::string &  filename)
static
Parameters
filenameThe name of the file to load the model from.
Returns
A NeuralNetwork object initialized from the file data.
Exceptions
std::ios_base::failureif the file cannot be read or is malformed.

◆ operator=()

NeuralNetwork & chisei::NeuralNetwork::operator= ( NeuralNetwork &&  other)
noexcept

Moves the contents of another neural network into this instance.

Parameters
otherThe 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
inputThe input vector.
Returns
The output vector.

◆ save_model()

void chisei::NeuralNetwork::save_model ( const std::string &  filename)
Parameters
filenameThe name of the file to save the model to.
Exceptions
std::ios_base::failureif 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
inputsThe training input data.
targetsThe expected output data corresponding to the inputs.
learning_rateThe learning rate for gradient descent (default = 0.1).
epochsThe number of training iterations (default = 10,000).

The documentation for this class was generated from the following file: