Conscience Core
CscNeuralNetworkPersistenceTensorFlow.h
Go to the documentation of this file.
1 /*
2  * CscNeuralNetworkPersistenceTensorFlow.h
3  *
4  * Created on: 14 juin 2024
5  * Author: Max Lhermitte
6  */
7 
8 #ifndef CSCNEURALNETWORKPERSISTENCETENSORFLOW_H_
9 #define CSCNEURALNETWORKPERSISTENCETENSORFLOW_H_
10 
12 #include "Util/conscience_util.h"
13 #include "CscCommon.h"
14 #include "tensorflow/c/c_api.h"
15 #include "Util/json.h"
16 
17 using namespace conscience_utils::JSON;
18 
19 #define TF_STORAGE_DIRECTORY "../conscience-resources/neuralNetworkTFModelsSaved"
20 
21 #define NN_SHAPE_TYPE "nn.shape.type"
22 #define NN_SHAPE_NB_INPUTS "nn.shape.nbInputs"
23 #define NN_SHAPE_NB_HIDDEN_LAYERS "nn.shape.nbHiddenLayers"
24 #define NN_SHAPE_NB_NEURONS_PER_LAYER_PREFIX "nn.shape.nbNeuronsPerHiddenLayer."
25 #define NN_SHAPE_NB_OUTPUTS "nn.shape.nbOutputs"
26 #define NN_HIDDEN_LAYERS_FUNCTIONS_ACTIVATION "nn.functions.hiddenLayersActivationFunction"
27 #define NN_OUTPUTS_FUNCTIONS_ACTIVATION "nn.functions.outputsActivationFunction"
28 #define NN_FUNCTIONS_LOSS "nn.functions.lossFunction"
29 #define NN_WEIGHTS_PREFIX "nn.weights."
30 #define NN_BIASES_PREFIX "nn.biases."
31 
32 namespace conscience_core::ai::nn {
33 
35 public:
37  private:
38  CscNeuralNetworkType* neuralNetworkType;
39  CscNeuralNetwork::NeuralNetworkHyperparameters* neuralNetworkHyperparameters = nullptr;
40  CscNeuralNetwork::TrainingHyperparameters* trainingHyperparameters = nullptr;
41  float** weightsLoadedFromModel = nullptr;
42  float** biasesLoadedFromModel = nullptr;
43 
44  public:
46  CscNeuralNetwork::NeuralNetworkHyperparameters* neuralNetworkHyperparameters,
47  CscNeuralNetwork::TrainingHyperparameters* trainingHyperparameters,
48  float** weightsLoadedFromModelInit,
49  float** biasesLoadedFromModelInit);
51 
52  CscNeuralNetworkType* getNeuralNetworkType() const;
53  CscNeuralNetwork::NeuralNetworkHyperparameters* getNeuralNetworkHyperparameters() const;
54  CscNeuralNetwork::TrainingHyperparameters* getTrainingHyperparameters() const;
55  float** getWeightsLoadedFromModel() const;
56  float** getBiasesLoadedFromModel() const;
57  };
58 
61 
62  static AllNeuralNetworkTensorFlowParams* loadModelFromDirectory(const fs::path &dirPath, bool forTraining);
63  CscJsonObject *fromNeuralNetworkTFToJSON(TF_Session* session, TF_Graph* graph, TF_Status* status, CscNeuralNetwork* neuralNetwork, vector<const char*> varBestValueNames);
64 
68  void saveToStorage(CscJsonObject *jsonObject, string pathDirectory, string fileDirectionAndName);
69 
70  inline static const string FILE_EXTENSION = ".cscnn";
71 
72 private:
73  // Attributes
74  unique_ptr<CscLogger> loggerPersistence;
75 
76  // Methods;
77 
78  void storeAllWeightsAndBiasesInTensorArray(TF_Session* session, TF_Graph* graph, TF_Status* status, vector<const char*> weightsAndBiasesVariarblesNames, TF_Tensor** tensorArrayToStoreWeightsAndBiases);
79 };
80 }
81 
82 #endif /* CSCTFNEURALNETWORKPERSISTENCETENSORFLOW_H_ */
CSC_DLL_IMPORTEXPORT
#define CSC_DLL_IMPORTEXPORT
Definition: os.h:31
conscience_util.h
conscience_core::ai::nn::CscNeuralNetwork::NeuralNetworkHyperparameters
Definition: CscNeuralNetwork.h:184
conscience_core::ai::nn::CscNeuralNetworkType
CscNeuralNetworkType
Definition: CscNeuralNetwork.h:20
conscience_core::ai::nn
Definition: CscNeuralNetwork.cpp:12
CscNeuralNetwork.h
conscience_utils::JSON
Definition: CscCommandMetadataBuilder.h:20
conscience_core::ai::nn::CscNeuralNetworkPersistenceTensorFlow::AllNeuralNetworkTensorFlowParams
Definition: CscNeuralNetworkPersistenceTensorFlow.h:36
CscCommon.h
conscience_core::ai::nn::CscNeuralNetwork::TrainingHyperparameters
Structure representing the hyperparameters for model training.
Definition: CscNeuralNetwork.h:139
conscience_core::ai::nn::CscNeuralNetworkPersistenceTensorFlow
Definition: CscNeuralNetworkPersistenceTensorFlow.h:34
json.h
conscience_core::ai::nn::CscNeuralNetwork
Definition: CscNeuralNetwork.h:79
conscience_utils::JSON::CscJsonObject
Definition: json.h:391