Conscience Core
Public Member Functions | Static Public Member Functions | List of all members
conscience_core::ai::nn::CscNeuralNetworkTensorFlow Class Reference

#include <CscNeuralNetworkTensorFlow.h>

Inheritance diagram for conscience_core::ai::nn::CscNeuralNetworkTensorFlow:
conscience_core::ai::nn::CscNeuralNetwork

Public Member Functions

 CscNeuralNetworkTensorFlow (CscNeuralNetworkType neuralNetworkType, NeuralNetworkHyperparameters *neuralNetworkHyperparametersInit, TrainingHyperparameters *trainingHyperparametersInit)
 
 CscNeuralNetworkTensorFlow (CscNeuralNetworkType neuralNetworkType, NeuralNetworkHyperparameters *neuralNetworkHyperparametersInit, TrainingHyperparameters *trainingHyperparametersInit, float **weightsLoadedFromModel, float **biasesLoadedFromModel, bool withCostFunction)
 
virtual ~CscNeuralNetworkTensorFlow ()
 
CscNeuralNetworkTensorFlowgetDuplicata ()
 
vector< TF_Session * > getSessions ()
 
CscNeuralNetworkBuilderTensorFlowgetBuilder () const
 
CscNeuralNetworkPersistenceTensorFlowgetPersistence () const
 
ResultTraining trainNeuralNetwork (const int nbEpochs, const int nbMiniBatchs, DataSet *trainingDataSet, DataSet *validationDataSet, const bool debugMode=false) override
 Trains a neural network using the specified parameters and datasets. More...
 
float testNeuralNetworkOnValidationDataSet (DataSet *validationDataSet, const bool nnNeverInitialized=false)
 Tests the neural network on the provided validation dataset and calculates the mean error. More...
 
vector< float > executeWithoutCostFunction (TF_Session *session, float **inputs)
 Executes the neural network without a cost function and returns the results. More...
 
- Public Member Functions inherited from conscience_core::ai::nn::CscNeuralNetwork
 CscNeuralNetwork (CscNeuralNetworkType neuralNetworkType, NeuralNetworkHyperparameters *neuralNetworkHyperparametersInit, TrainingHyperparameters *trainingHyperparametersInit)
 
 CscNeuralNetwork (CscNeuralNetworkType neuralNetworkType, NeuralNetworkHyperparameters *neuralNetworkHyperparametersInit, TrainingHyperparameters *trainingHyperparametersInit, float **weightsLoadedFromModel, float **biasesLoadedFromModel)
 
virtual ~CscNeuralNetwork ()
 
CscNeuralNetworkType getNeuralNetworkType () const
 
CscNeuralNetwork::NeuralNetworkHyperparametersgetNeuralNetworkHyperparameters () const
 
CscNeuralNetwork::TrainingHyperparametersgetTrainingHyperparameters () const
 

Static Public Member Functions

static CscNeuralNetworkTensorFlowloadModelFromDirectory (const fs::path &dirPath, bool forTraining)
 Loads a neural network model from a specified directory. More...
 
- Static Public Member Functions inherited from conscience_core::ai::nn::CscNeuralNetwork
static float *** adaptDataForMiniBatches (float **dataToTransform, const int minibatchSize, const int dataSetSize)
 Prepares the dataset for minibatch processing by dividing it into smaller batches. More...
 
static void normalizeDataAutoRange (float **data, size_t size, int nbInputsOrOutputs, CscNormalizationType normalizationType)
 
static void normalizeDataCustomRange0to1 (float **data, size_t size, int nbInputsOrOutputs, float *minValue, float *maxValue)
 
static void normalizeDataCustomRangeMinus1to1 (float **data, size_t size, int nbInputsOrOutputs, float *minValue, float *maxValue)
 
static void normalizeData (float **data, size_t size, int rowData, float newMin, float newMax, float minValue, float maxValue)
 
static void deleteAllDataMiniBatches (float ***dataMiniBatches, const int nbMiniBatches, const int nbDataPerMinibatch)
 Frees the memory allocated for the mini-batches of data. More...
 
static void initializeWeights (float *weightsToInitialize, int size, CscInitializationWeightsStrategy strategy, int fanIn, int fanOut=0, float *weightsLoaded=nullptr)
 Initializes the weights of the neural network according to a specified initialization strategy. More...
 
static void initializeBias (float *biasToInitialize, int size, CscInitializationBiasStrategy strategy, float *biasLoaded=nullptr)
 

Additional Inherited Members

- Protected Member Functions inherited from conscience_core::ai::nn::CscNeuralNetwork
void shuffleMinibatchData (float ***minibatchDataInputs, float ***minibatchDataTargets, const int numberOfBatches, const int minibatchSize)
 Shuffles the data within each minibatch as well as between minibatches. More...
 
- Static Protected Member Functions inherited from conscience_core::ai::nn::CscNeuralNetwork
static float calculateStddevHe (int fanIn)
 
static float calculateLimitGlorot (int fanIn, int fanOut)
 
static float calculateStddevGlorot (int fanIn, int fanOut)
 
static float calculateStddevLeCun (int fanIn)
 
static void initNormal (float *weights, int size, mt19937 &gen, double stddev)
 
static void initUniform (float *weights, int size, mt19937 &gen, float limit)
 
- Protected Attributes inherited from conscience_core::ai::nn::CscNeuralNetwork
CscNeuralNetworkType neuralNetworkType
 
TrainingHyperparameterstrainingHyperparameters = nullptr
 
NeuralNetworkHyperparametersneuralNetworkHyperparameters = nullptr
 
unique_ptr< CscLoggerlogger
 

Constructor & Destructor Documentation

◆ CscNeuralNetworkTensorFlow() [1/2]

conscience_core::ai::nn::CscNeuralNetworkTensorFlow::CscNeuralNetworkTensorFlow ( CscNeuralNetworkType  neuralNetworkType,
NeuralNetworkHyperparameters neuralNetworkHyperparametersInit,
TrainingHyperparameters trainingHyperparametersInit 
)

◆ CscNeuralNetworkTensorFlow() [2/2]

conscience_core::ai::nn::CscNeuralNetworkTensorFlow::CscNeuralNetworkTensorFlow ( CscNeuralNetworkType  neuralNetworkType,
NeuralNetworkHyperparameters neuralNetworkHyperparametersInit,
TrainingHyperparameters trainingHyperparametersInit,
float **  weightsLoadedFromModel,
float **  biasesLoadedFromModel,
bool  withCostFunction 
)

◆ ~CscNeuralNetworkTensorFlow()

conscience_core::ai::nn::CscNeuralNetworkTensorFlow::~CscNeuralNetworkTensorFlow ( )
virtual

Member Function Documentation

◆ executeWithoutCostFunction()

vector< float > conscience_core::ai::nn::CscNeuralNetworkTensorFlow::executeWithoutCostFunction ( TF_Session *  session,
float **  inputs 
)

Executes the neural network without a cost function and returns the results.

Parameters
sessionPointer to the TensorFlow session.
inputsPointer to the input data.
Returns
A vector of floats containing the results of the neural network execution.

This function initializes the neural network values and prepares the input data as a TensorFlow tensor. It then executes the graph without a cost function and retrieves the results. The results are converted into a vector of floats and returned. The function ensures proper cleanup of TensorFlow tensors used in the process.

◆ getBuilder()

CscNeuralNetworkBuilderTensorFlow * conscience_core::ai::nn::CscNeuralNetworkTensorFlow::getBuilder ( ) const

◆ getDuplicata()

CscNeuralNetworkTensorFlow * conscience_core::ai::nn::CscNeuralNetworkTensorFlow::getDuplicata ( )

◆ getPersistence()

CscNeuralNetworkPersistenceTensorFlow * conscience_core::ai::nn::CscNeuralNetworkTensorFlow::getPersistence ( ) const

◆ getSessions()

vector< TF_Session * > conscience_core::ai::nn::CscNeuralNetworkTensorFlow::getSessions ( )

◆ loadModelFromDirectory()

CscNeuralNetworkTensorFlow * conscience_core::ai::nn::CscNeuralNetworkTensorFlow::loadModelFromDirectory ( const fs::path &  dirPath,
bool  forTraining 
)
static

Loads a neural network model from a specified directory.

Parameters
filePathThe file path to the directory containing the model.
forTrainingFlag indicating if the model is to be loaded for training.
Returns
A pointer to the loaded CscNeuralNetworkTensorFlow object.

This function loads a neural network model from the specified directory. It utilizes the CscNeuralNetworkPersistenceTensorFlow class to retrieve all the necessary parameters (including training hyperparameters, neural network hyperparameters, weights, and biases). It then constructs a new CscNeuralNetworkTensorFlow object with these parameters and returns a pointer to it.

◆ testNeuralNetworkOnValidationDataSet()

float conscience_core::ai::nn::CscNeuralNetworkTensorFlow::testNeuralNetworkOnValidationDataSet ( DataSet validationDataSet,
const bool  nnNeverInitialized = false 
)

Tests the neural network on the provided validation dataset and calculates the mean error.

Parameters
validationDataSetPointer to the dataset used for validation.
nnNeverInitializedFlag indicating if the neural network has never been initialized.
Returns
The mean error of the neural network on the validation dataset.

This function tests the neural network on a given validation dataset. It first checks that the number of data points per mini-batch does not exceed a predefined maximum. If the neural network has never been initialized (nnNeverInitialized is true), it initializes the weights and biases. The function then calculates and returns the mean error by executing the network on mini-batches of the validation dataset.

◆ trainNeuralNetwork()

CscNeuralNetwork::ResultTraining conscience_core::ai::nn::CscNeuralNetworkTensorFlow::trainNeuralNetwork ( const int  nbEpochs,
const int  nbMiniBatchs,
DataSet trainingDataSet,
DataSet validationDataSet,
const bool  debugMode = false 
)
overridevirtual

Trains a neural network using the specified parameters and datasets.

Parameters
nbEpochsThe number of epochs for training.
nbMiniBatchsThe number of mini-batches to use for each epoch.
trainingDataSetPointer to the dataset used for training.
validationDataSetPointer to the dataset used for validation.
debugModeFlag to enable or disable debug mode, which logs additional information.
Returns
A ResultTraining object containing details about the training process, such as the number of epochs performed, the epoch with the best validation result, the mean error on training data, and the best mean error on validation data.

The function first asserts that the number of data per mini-batch matches the expected value based on the training dataset size and number of mini-batches. It then initializes the training process, including session creation, weight and bias initialization, and optimizer-specific initializations. The main training loop iterates through the specified number of epochs, shuffling the training data, and executing training on mini-batches. During each epoch, the mean error on training and validation data is calculated and logged if debug mode is enabled. The function also includes an early stopping mechanism, which terminates training if there is no improvement in validation error for a specified number of epochs (patience). Finally, the function returns a ResultTraining object summarizing the training results.

Implements conscience_core::ai::nn::CscNeuralNetwork.


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