NNFS
Neural network library from scratch
Loading...
Searching...
No Matches
NNFS::NeuralNetwork Class Reference

A neural network model. More...

#include <NeuralNetwork.hpp>

Inheritance diagram for NNFS::NeuralNetwork:
[legend]

Public Member Functions

 NeuralNetwork (std::shared_ptr< Loss > loss=nullptr, std::shared_ptr< Optimizer > optimizer=nullptr)
 Constructor for NeuralNetwork.
 
void fit (const Eigen::MatrixXd &examples, const Eigen::MatrixXd &labels, const Eigen::MatrixXd &test_examples, const Eigen::MatrixXd &test_labels, int epochs, int batch_size, bool verbose=true) override
 Fit the neural network model to the given data.
 
void add_layer (std::shared_ptr< Layer > layer)
 Adds a layer to the neural network model.
 
void compile ()
 Compiles the neural network model.
 
void save (std::string path)
 Saves the model to a file in a custom binary format. The model can be loaded using the NNFS::load method.
 
void load (std::string path)
 Loads a model from a file in a custom binary format. The model must have been saved using the NNFS::save method.
 
void accuracy (double &accuracy, const Eigen::MatrixXd &examples, const Eigen::MatrixXd &labels)
 Calculates the accuracy of the neural network on the provided examples and labels.
 
Eigen::MatrixXd predict (const Eigen::MatrixXd &sample)
 Predicts the class of the provided sample(s).
 
- Public Member Functions inherited from NNFS::Model
virtual ~Model ()=default
 Basic destructor.
 
virtual void fit (const Eigen::MatrixXd &examples, const Eigen::MatrixXd &labels, const Eigen::MatrixXd &test_examples, const Eigen::MatrixXd &test_labels, int epochs, int batch_size, bool verbose=false)=0
 Evaluate the model on the given examples.
 

Detailed Description

A neural network model.

This class represents a neural network model capable of training on data and making predictions.

Constructor & Destructor Documentation

◆ NeuralNetwork()

NNFS::NeuralNetwork::NeuralNetwork ( std::shared_ptr< Loss loss = nullptr,
std::shared_ptr< Optimizer optimizer = nullptr 
)
inline

Constructor for NeuralNetwork.

Parameters
[in]lossThe loss function, must be a subclass of Loss, defaults to nullptr. If nullptr, training will not be possible.
[in]optimizerThe optimizer, must be a subclass of Optimizer, defaults to nullptr. If nullptr, training will not be possible.

Member Function Documentation

◆ accuracy()

void NNFS::NeuralNetwork::accuracy ( double &  accuracy,
const Eigen::MatrixXd &  examples,
const Eigen::MatrixXd &  labels 
)
inline

Calculates the accuracy of the neural network on the provided examples and labels.

Parameters
[out]accuracyAccuracy of the neural network on the provided examples and labels.
[in]examplesExamples to calculate the accuracy on.
[in]labelsLabels to calculate the accuracy on.

◆ add_layer()

void NNFS::NeuralNetwork::add_layer ( std::shared_ptr< Layer layer)
inline

Adds a layer to the neural network model.

This method adds a layer to the neural network model. The layer is added to the end of the neural network model.

Parameters
[in]layerThe layer to be added to the neural network model

◆ compile()

void NNFS::NeuralNetwork::compile ( )
inline

Compiles the neural network model.

This method compiles the neural network model by initializing the weights and biases of the layers and setting the input and output dimensions of each layer.

◆ fit()

void NNFS::NeuralNetwork::fit ( const Eigen::MatrixXd &  examples,
const Eigen::MatrixXd &  labels,
const Eigen::MatrixXd &  test_examples,
const Eigen::MatrixXd &  test_labels,
int  epochs,
int  batch_size,
bool  verbose = true 
)
inlineoverridevirtual

Fit the neural network model to the given data.

This function trains the neural network model on the given data for the given number of epochs.

Parameters
[in]examplesExamples to evaluate the model on
[in]labelsLabels of the examples
[in]test_examplesExamples to validate the model on
[in]test_labelsLabels of the validation examples
[in]epochsThe number of epochs to train for
[in]batch_sizeThe batch size, i.e. the number of examples to train on in each batch
[in]verboseWhether to print out information about the training process

Implements NNFS::Model.

◆ load()

void NNFS::NeuralNetwork::load ( std::string  path)
inline

Loads a model from a file in a custom binary format. The model must have been saved using the NNFS::save method.

Parameters
[in]pathThe path to load the model from

◆ predict()

Eigen::MatrixXd NNFS::NeuralNetwork::predict ( const Eigen::MatrixXd &  sample)
inline

Predicts the class of the provided sample(s).

Parameters
[in]sampleSample(s) to predict the class of.
Returns
Predictions of the neural network for the provided sample(s).

◆ save()

void NNFS::NeuralNetwork::save ( std::string  path)
inline

Saves the model to a file in a custom binary format. The model can be loaded using the NNFS::load method.

Parameters
[in]pathThe path to save the model to

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