NeuralEngine A Game Engine with embeded Machine Learning algorithms based on Gaussian Processes. |

FgIModel.h
1
11#pragma once
12
13#include <NeMachineLearningLib.h>
14#include <MachineLearning/IGradientOptimizationMethod.h>
15
16 namespace NeuralEngine
17{
18 namespace MachineLearning
19 {
20 enum class ModelType
21 {
22 NONE = 0,
23 GPR = 1,
24 GPLVM = 2,
25 DGPR = 3,
26 DGPLVM = 4,
27 SSM = 5
28 };
29
35 class HiddenLayerDescription
36 {
37 public:
38
47 HiddenLayerDescription(int numPseudos, int numHiddenDimensions)
48 {
49 iNumPseudos = numPseudos;
50 iNumHidden = numHiddenDimensions;
51 }
52
53 HiddenLayerDescription()
54 {
55 iNumPseudos = 0;
56 iNumHidden = 0;
57 }
58
66 int GetNumPseudoInputs() { return iNumPseudos; }
67
75 int GetNumHiddenDimensions() { return iNumHidden; }
76
77 private:
78 int iNumPseudos;
79 int iNumHidden;
80
81 friend class boost::serialization::access;
82
83 template<class Archive>
84 void serialize(Archive& ar, unsigned int version)
85 {
86 //ar& boost::serialization::base_object<GPLVMBaseModel<Scalar>>(*this);
87
88 ar& BOOST_SERIALIZATION_NVP(iNumPseudos);
89 ar& BOOST_SERIALIZATION_NVP(iNumHidden);
90 }
91 };
92
101 template<typename Scalar>
102 class NE_IMPEXP IModel
103 {
104 public:
105
116 virtual Scalar Function(const af::array& x, af::array& outGradient);
117
125 virtual int GetNumParameters() = 0;
126
134 virtual void SetParameters(const af::array& param) = 0;
135
143 virtual af::array GetParameters() = 0;
144
150 virtual void UpdateParameters() = 0;
151
159 int GetDataLenght();
160
168 int GetDataDimensionality();
169
177 ModelType GetModelType();
178
186 virtual void SetBatchSize(int size);
187
195 int GetBatchSize();
196
202 void SetIndexes(af::array& indexes);
203
204 protected:
205
214 IModel(int numData, int numDimension, ModelType type);
215
216
217
218 ModelType mType;
219
220 int iN;
221 int iD;
222 int iBatchSize;
223 af::array afIndexes;
224
225 af::dtype m_dType;
226
227 private:
228 friend class boost::serialization::access;
229
230 template<class Archive>
231 void serialize(Archive& ar, unsigned int version)
232 {
233 ar& BOOST_SERIALIZATION_NVP(iN);
234 ar& BOOST_SERIALIZATION_NVP(iD);
235 ar& BOOST_SERIALIZATION_NVP(iBatchSize);
236 ar& BOOST_SERIALIZATION_NVP(afIndexes);
237 ar& BOOST_SERIALIZATION_NVP(mType);
238 ar& BOOST_SERIALIZATION_NVP(m_dType);
239 }
240 };
241
251 template<class T>
252 void SaveModel(const std::string& file, T* model)
253 {
254 std::ofstream ofs(file.c_str(), std::ios::binary);
255 boost::archive::binary_oarchive oa(ofs);
256 oa << BOOST_SERIALIZATION_NVP(*model);
257 ofs.close();
258 }
259
270 template<class T>
271 T* LoadModel(const std::string& file)
272 {
273 T* model = new T();
274 std::ifstream ifs(file.c_str(), std::ios::binary);
275 boost::archive::binary_iarchive ia(ifs);
276 ia >> BOOST_SERIALIZATION_NVP(*model);
277 ifs.close();
278 model->UpdateParameters();
279 return model;
280 }
281 }
282}
NeuralEngine::MachineLearning::HiddenLayerDescription
Description of the layer.
Definition: FgIModel.h:36
NeuralEngine::MachineLearning::HiddenLayerDescription::GetNumPseudoInputs
int GetNumPseudoInputs()
Gets number pseudo inputs.
Definition: FgIModel.h:66
NeuralEngine::MachineLearning::HiddenLayerDescription::HiddenLayerDescription
HiddenLayerDescription(int numPseudos, int numHiddenDimensions)
Constructor.
Definition: FgIModel.h:47
NeuralEngine::MachineLearning::HiddenLayerDescription::GetNumHiddenDimensions
int GetNumHiddenDimensions()
Gets number hidden dimensions.
Definition: FgIModel.h:75
NeuralEngine::MachineLearning::IModel
Base class with abstract and basic function definitions. All models will be derived from this class.
Definition: FgIModel.h:103
NeuralEngine::MachineLearning::IModel::UpdateParameters
virtual void UpdateParameters()=0
Updates the parameters.
NeuralEngine::MachineLearning::IModel::IModel
IModel(int numData, int numDimension, ModelType type)
Constructor.
NeuralEngine::MachineLearning::IModel::SetBatchSize
virtual void SetBatchSize(int size)
Sets batch size.
NeuralEngine::MachineLearning::IModel::SetIndexes
void SetIndexes(af::array &indexes)
Sets the batch indexes.
NeuralEngine::MachineLearning::IModel::GetBatchSize
int GetBatchSize()
Gets batch size.
NeuralEngine::MachineLearning::IModel::SetParameters
virtual void SetParameters(const af::array &param)=0
Sets the parameters for each optimization iteration.
NeuralEngine::MachineLearning::IModel::afIndexes
af::array afIndexes
indexes of /f$\mathbf{X}/f$ for batch learning
Definition: FgIModel.h:223
NeuralEngine::MachineLearning::IModel::GetNumParameters
virtual int GetNumParameters()=0
Gets number of parameters to be optimized.
NeuralEngine::MachineLearning::IModel::iD
int iD
dataset dimension
Definition: FgIModel.h:221
NeuralEngine::MachineLearning::IModel::iN
int iN
dataset length
Definition: FgIModel.h:220
NeuralEngine::MachineLearning::IModel::GetDataDimensionality
int GetDataDimensionality()
Gets data dimensionality.
NeuralEngine::MachineLearning::IModel::GetParameters
virtual af::array GetParameters()=0
Gets the parameters for each optimization iteration.
NeuralEngine::MachineLearning::IModel::GetModelType
ModelType GetModelType()
Gets model type.
NeuralEngine::MachineLearning::IModel::GetDataLenght
int GetDataLenght()
Gets data lenght.
NeuralEngine::MachineLearning::IModel::m_dType
af::dtype m_dType
floating point precision flag for af::array
Definition: FgIModel.h:225
NeuralEngine::MachineLearning::IModel::Function
virtual Scalar Function(const af::array &x, af::array &outGradient)
Cost function the given x inputs.
NeuralEngine::MachineLearning::IModel::iBatchSize
int iBatchSize
size of the batch
Definition: FgIModel.h:222
Scalar
NeuralEngine
Definition: NeArray2.h:18