Conscience Core
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
conscience_core::ai::nn::CscNeuralNetwork Class Referenceabstract

#include <CscNeuralNetwork.h>

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

Classes

struct  DataSet
 
struct  NeuralNetworkHyperparameters
 
struct  NeuralNetworkShape
 
struct  ResultTraining
 
struct  TrainingHyperparameters
 Structure representing the hyperparameters for model training. More...
 

Public Member Functions

 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
 
virtual ResultTraining trainNeuralNetwork (const int nbEpochs, const int nbMiniBatchs, DataSet *trainingDataSet, DataSet *validationDataSet, const bool debugMode=false)=0
 Trains the neural network using the provided input and target data. More...
 

Static Public Member Functions

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)
 

Protected Member Functions

virtual void createNeuralNetwork (CscActivationFunction hiddenLayersFunctionActivation, CscActivationFunction outputLayerFunctionActivation, CscLossFunction lossFunction, CscInitializationWeightsStrategy initilizationStrategy, CscInitializationBiasStrategy initializationBiasStrategy, bool withCostFunction)=0
 
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

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

CscNeuralNetworkType neuralNetworkType
 
TrainingHyperparameterstrainingHyperparameters = nullptr
 
NeuralNetworkHyperparametersneuralNetworkHyperparameters = nullptr
 
unique_ptr< CscLoggerlogger
 

Constructor & Destructor Documentation

◆ CscNeuralNetwork() [1/2]

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

◆ CscNeuralNetwork() [2/2]

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

◆ ~CscNeuralNetwork()

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

Member Function Documentation

◆ adaptDataForMiniBatches()

float *** conscience_core::ai::nn::CscNeuralNetwork::adaptDataForMiniBatches ( float **  dataToTransform,
const int  minibatchSize,
const int  dataSetSize 
)
static

Prepares the dataset for minibatch processing by dividing it into smaller batches.

This method organizes input data into a structure suitable for minibatch training. Each minibatch consists of a subset of the input data, allowing for more efficient memory usage and potentially faster convergence of the neural network due to more frequent updates.

Parameters
dataToTransformPointer to the original dataset to be divided into minibatches.
minibatchSizeThe size of each minibatch.
dataSetSizeThe total number of samples in the dataset.
Returns
A 3D array where the first dimension corresponds to the minibatch index, the second dimension indexes into a specific sample within a minibatch, and the third dimension represents the data of that sample.
Warning
The new tabs created must be deleted after they have been used for training.

◆ calculateLimitGlorot()

float conscience_core::ai::nn::CscNeuralNetwork::calculateLimitGlorot ( int  fanIn,
int  fanOut 
)
staticprotected

◆ calculateStddevGlorot()

float conscience_core::ai::nn::CscNeuralNetwork::calculateStddevGlorot ( int  fanIn,
int  fanOut 
)
staticprotected

◆ calculateStddevHe()

float conscience_core::ai::nn::CscNeuralNetwork::calculateStddevHe ( int  fanIn)
staticprotected

◆ calculateStddevLeCun()

float conscience_core::ai::nn::CscNeuralNetwork::calculateStddevLeCun ( int  fanIn)
staticprotected

◆ createNeuralNetwork()

virtual void conscience_core::ai::nn::CscNeuralNetwork::createNeuralNetwork ( CscActivationFunction  hiddenLayersFunctionActivation,
CscActivationFunction  outputLayerFunctionActivation,
CscLossFunction  lossFunction,
CscInitializationWeightsStrategy  initilizationStrategy,
CscInitializationBiasStrategy  initializationBiasStrategy,
bool  withCostFunction 
)
protectedpure virtual

◆ deleteAllDataMiniBatches()

void conscience_core::ai::nn::CscNeuralNetwork::deleteAllDataMiniBatches ( float ***  dataMiniBatches,
const int  nbMiniBatches,
const int  nbDataPerMinibatch 
)
static

Frees the memory allocated for the mini-batches of data.

This function iterates through each mini-batch of data, and for each mini-batch, it iterates through each data point. It frees the memory allocated for each data point and then frees the memory allocated for the mini-batch itself. Finally, it frees the memory allocated for the array of pointers to the mini-batches.

Parameters
dataMiniBatchesA pointer to an array of pointers to arrays of pointers. Each element in the first array represents a mini-batch. Each element in a mini-batch represents a single data point. The memory for each data point, each mini-batch, and the array of mini-batches itself is dynamically allocated and needs to be freed.
nbMiniBatchesThe number of mini-batches in dataMiniBatches. This is the size of the first array.
nbDataPerMinibatchThe number of data points in each mini-batch. This is the size of each second-level array.

◆ getNeuralNetworkHyperparameters()

CscNeuralNetwork::NeuralNetworkHyperparameters * conscience_core::ai::nn::CscNeuralNetwork::getNeuralNetworkHyperparameters ( ) const

◆ getNeuralNetworkType()

CscNeuralNetworkType conscience_core::ai::nn::CscNeuralNetwork::getNeuralNetworkType ( ) const

◆ getTrainingHyperparameters()

CscNeuralNetwork::TrainingHyperparameters * conscience_core::ai::nn::CscNeuralNetwork::getTrainingHyperparameters ( ) const

◆ initializeBias()

void conscience_core::ai::nn::CscNeuralNetwork::initializeBias ( float *  biasToInitialize,
int  size,
CscInitializationBiasStrategy  strategy,
float *  biasLoaded = nullptr 
)
static

◆ initializeWeights()

void conscience_core::ai::nn::CscNeuralNetwork::initializeWeights ( float *  weightsToInitialize,
int  size,
CscInitializationWeightsStrategy  strategy,
int  fanIn,
int  fanOut = 0,
float *  weightsLoaded = nullptr 
)
static

Initializes the weights of the neural network according to a specified initialization strategy.

This method initializes the weights of the neural network using one of four supported initialization strategies: He, GlorotUniform, GlorotNormal, or LeCunNormal. Each strategy has its characteristics and is chosen based on the specific needs of the network's architecture. Proper weight initialization is crucial for the successful learning and convergence of the neural network.

Parameters
weightsToInitializePointer to the array of weights to be initialized. This array must be pre-allocated with enough size to hold all the weights.
sizeThe number of weights to initialize in the array.
strategyThe CscInitializationWeightsStrategy enumeration specifying the initialization strategy to use for the weights.
fanInThe number of incoming units to the node for which the weights are being initialized, used in the calculation for some strategies.
fanOutThe number of outgoing units from the node for which the weights are being initialized, used in the calculation for some strategies.
Note
The function uses specific distributions based on the chosen strategy to initialize the weights. For He, a normal distribution is used with a standard deviation calculated based on fanIn. For GlorotUniform, a uniform distribution is used with limits calculated based on fanIn and fanOut. GlorotNormal uses a normal distribution similar to GlorotUniform but with a different standard deviation. LeCunNormal uses a normal distribution with a standard deviation calculated solely based on fanIn.

◆ initNormal()

void conscience_core::ai::nn::CscNeuralNetwork::initNormal ( float *  weights,
int  size,
mt19937 &  gen,
double  stddev 
)
staticprotected

◆ initUniform()

void conscience_core::ai::nn::CscNeuralNetwork::initUniform ( float *  weights,
int  size,
mt19937 &  gen,
float  limit 
)
staticprotected

◆ normalizeData()

void conscience_core::ai::nn::CscNeuralNetwork::normalizeData ( float **  data,
size_t  size,
int  rowData,
float  newMin,
float  newMax,
float  minValue,
float  maxValue 
)
static

Normalizes the elements of a given float array to a specified new range.

This method adjusts the values within the array so that they fit within a new range, defined by newMin and newMax. The normalization is done based on a given range of values in the array, specified by the parameters minValue and maxValue. The method scales each element of the array from its original range (minValue to maxValue) to the new range (newMin to newMax). If the array's size is zero, or if newMin is equal to or greater than newMax, the method logs an error and returns without modifying the array.

Parameters
dataPointer to the first element of the array of float values to be normalized.
sizeThe number of elements in the array.
newMinThe minimum value of the new range to which the data should be normalized.
newMaxThe maximum value of the new range to which the data should be normalized.
minValueThe minimum value in the original range of the data. This parameter is used directly in the normalization calculation and is not modified by the method.
maxValueThe maximum value in the original range of the data. This parameter is used directly in the normalization calculation and is not modified by the method.

◆ normalizeDataAutoRange()

void conscience_core::ai::nn::CscNeuralNetwork::normalizeDataAutoRange ( float **  data,
size_t  size,
int  nbInputsOrOutputs,
CscNormalizationType  normalizationType 
)
static

◆ normalizeDataCustomRange0to1()

void conscience_core::ai::nn::CscNeuralNetwork::normalizeDataCustomRange0to1 ( float **  data,
size_t  size,
int  nbInputsOrOutputs,
float *  minValue,
float *  maxValue 
)
static

◆ normalizeDataCustomRangeMinus1to1()

void conscience_core::ai::nn::CscNeuralNetwork::normalizeDataCustomRangeMinus1to1 ( float **  data,
size_t  size,
int  nbInputsOrOutputs,
float *  minValue,
float *  maxValue 
)
static

◆ shuffleMinibatchData()

void conscience_core::ai::nn::CscNeuralNetwork::shuffleMinibatchData ( float ***  minibatchDataInputs,
float ***  minibatchDataTargets,
const int  numberOfBatches,
const int  minibatchSize 
)
protected

Shuffles the data within each minibatch as well as between minibatches.

This method performs an in-place shuffle of the minibatch data, both the inputs and the targets. The shuffling is performed within each minibatch and across minibatches to ensure that the model does not learn any unintended patterns from the order of the data.

Parameters
minibatchDataInputsA 3D array containing the input data divided into minibatches.
minibatchDataTargetsA 3D array containing the target data divided into minibatches.
numberOfBatchesThe total number of minibatches.
minibatchSizeThe size of each minibatch.

◆ trainNeuralNetwork()

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

Trains the neural network using the provided input and target data.

This method trains the neural network over a specified number of epochs and mini-batches, using the TensorFlow C API. It initializes the session and model parameters, shuffles the data for each epoch (except the first one), processes each mini-batch by executing the graph, and updates the weights and biases according to the cost function. It also provides optional debugging information, including logging the progress of training and the change in weights and biases.

Parameters
nbEpochsThe number of epochs to train the neural network for.
nbMiniBatchsThe number of mini-batches to divide the input data into.
debugModeA boolean flag that, when true, enables logging of debugging information during training.
Returns
The mean of the loss/cost function over the mini-batches in the final epoch of training.
Note
This function requires TensorFlow C API setup and assumes that the session, graph, and status are correctly initialized and available globally or within the class.
Warning
The method does not handle the creation and destruction of the TensorFlow session, graph, and status objects. These should be appropriately managed outside this method.
Attention
This method assumes that the neural network, its variables (weights and biases), and the cost function are already defined in the TensorFlow graph.

Implemented in conscience_core::ai::nn::CscNeuralNetworkTensorFlow.

Member Data Documentation

◆ logger

unique_ptr<CscLogger> conscience_core::ai::nn::CscNeuralNetwork::logger
protected

◆ neuralNetworkHyperparameters

NeuralNetworkHyperparameters* conscience_core::ai::nn::CscNeuralNetwork::neuralNetworkHyperparameters = nullptr
protected

◆ neuralNetworkType

CscNeuralNetworkType conscience_core::ai::nn::CscNeuralNetwork::neuralNetworkType
protected

◆ trainingHyperparameters

TrainingHyperparameters* conscience_core::ai::nn::CscNeuralNetwork::trainingHyperparameters = nullptr
protected

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