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

FgGPLVMBaseModel.h
1
11#pragma once
12
13#include <MachineLearning/FgGPNode.h>
14#include <MachineLearning/FgGPBaseModel.h>
15#include <MachineLearning/FgBackconstraints.h>
16#include <boost/serialization/map.hpp>
17
18 namespace NeuralEngine
19{
20 namespace MachineLearning
21 {
22 namespace GPModels
23 {
31 template<typename Scalar>
32 class NE_IMPEXP Style
33 {
34 public:
35
43 Style(std::string name);
44
50 Style() { };
51
57 virtual ~Style();
58
67 virtual void AddSubstyle(std::string name, int numFrames);
68
76 virtual int GetNumSubstyles();
77
85 virtual int GetNumInducingSubstyles();
86
94 af::array GetStyle();
95
103 std::map<std::string, int> GetSubstyles();
104
112 af::array GetStyleExpanded();
113
121 af::array GetInducingStyleExpanded();
122
130 const af::array GetStyleIndex();
131
139 void SetInducingStyleIndex(const af::array& indx);
140
148 void GetFactorsExpanded(af::array& X1, af::array& X2);
149
157 std::string GetName();
158
166 int GetNumParameters();
167
175 int GetNumInducingParameters();
176
184 af::array GetParameters();
185
193 void SetParameters(const af::array& param);
194
202 af::array GetInducingParameters();
203
211 void SetInducingParameters(const af::array& param);
212
218 void UpdatePosterior(const af::array& postX1, const af::array& postX2);
219
229 af::array GetGradientCollapsed(const af::array& factorGradient);
230
240 af::array GetInducingGradientCollapsed(const af::array& styleGradient);
241
250 void InitFactors(const af::array& expandedX1, const af::array& expandedX2);
251
252 protected:
253 af::array afStyleIdx;
254 af::array afStyleInducingIdx;
255 af::array afStyle;
256 af::array afInducingStyle;
257 af::array afFactorX1;
258 af::array afFactorX2;
259 af::array afPosteriorX1;
260 af::array afPosteriorX2;
261 std::map<std::string, int> mSubStyles;
262
263 std::string sName;
264
265 af::dtype m_dType;
266
267 private:
268 friend class boost::serialization::access;
269
270 template<class Archive>
271 void serialize(Archive& ar, unsigned int version)
272 {
273 //ar& boost::serialization::base_object<GPLVMBaseModel<Scalar>>(*this);
274
275 ar& BOOST_SERIALIZATION_NVP(afStyleIdx);
276 ar& BOOST_SERIALIZATION_NVP(afStyleInducingIdx);
277 ar& BOOST_SERIALIZATION_NVP(afStyle);
278 ar& BOOST_SERIALIZATION_NVP(afInducingStyle);
279 ar& BOOST_SERIALIZATION_NVP(mSubStyles);
280 ar& BOOST_SERIALIZATION_NVP(sName);
281 ar& BOOST_SERIALIZATION_NVP(m_dType);
282 }
283 };
284
293 template<typename Scalar>
294 class NE_IMPEXP GPLVMBaseModel : public GPBaseModel<Scalar>, public GPNode<Scalar>
295 {
296 public:
297
311 GPLVMBaseModel(const af::array& Y, int latentDimension, Scalar priorMean = 0.0, Scalar priorVariance = 1.0,
312 LogLikType lType = LogLikType::Gaussian, XInit emethod = XInit::pca);
313
319 GPLVMBaseModel();
320
326 virtual ~GPLVMBaseModel();
327
341 virtual void Optimise(
342 OptimizerType method = L_BFGS,
343 Scalar tol = 0.0,
344 bool reinit_hypers = true,
345 int maxiter = 1000,
346 int mb_size = 0,
347 LineSearchType lsType = MoreThuente,
348 bool disp = true,
349 int* cycle = nullptr
350 ) override;
351
359 virtual bool Init(af::array& mx);
360
368 virtual bool Init() override;
369
379 virtual void PosteriorLatents(af::array& mx, af::array& vx);
380
388 virtual int GetNumParameters() override;
389
397 virtual void SetParameters(const af::array& param) override;
398
406 virtual af::array GetParameters() override;
407
413 virtual void UpdateParameters() override;
414
422 virtual void FixKernelParameters(bool isfixed);
423
431 virtual void FixInducing(bool isfixed);
432
433 void FixLatents(bool isFixed);
434
442 af::array GetMeanGradient();
443
451 af::array GetVarGradient();
452
461 void SetPrior(const af::array mean, const af::array var);
462
471 void SetPriorCavity(const af::array meanCav, const af::array varCav);
472
481 void SetLatentGradient(const af::array& dmParent, const af::array& dvParent);
482
491 void SetLatentGradientCavity(const af::array& dmParent, const af::array& dvParent);
492
500 int GetLatentDimension();
501
509 void SetBackConstraint(IBackconstraint<Scalar>* constraint);
510
518 IBackconstraint<Scalar>* GetBackConstraint();
519
527 void SetStyles(std::map<std::string, Style<Scalar>>* styles);
528
536 void AddStyle(Style<Scalar> style);
537
545 std::map<std::string, Style<Scalar>>* GetStyles();
546
547 protected:
548
557 virtual af::array PosteriorGradientLatents(const af::array& dmx, const af::array& dvx);
558
564 virtual void UpdateParametersInternal();
565
573 virtual af::array LatentGradient(const af::array& dm, const af::array& dv);
574
575 int iq;
576
577 Scalar dPriorMean;
578 Scalar dPriorVariance;
579 Scalar dPriorX1;
580 Scalar dPriorX2;
581
582 af::array afFactorX1;
583 af::array afFactorX2;
584 af::array afPosteriorX1;
585 af::array afPosteriorX2;
586
587 af::array afPriorMean;
588 af::array afPriorVariance;
589 af::array afPriorMeanCav;
590 af::array afPriorVarianceCav;
591 af::array afPriorX1;
592 af::array afPriorX2;
593 af::array afPriorX1Cav;
594 af::array afPriorX2Cav;
595 af::array afGradMean;
596 af::array afGradVariance;
597 af::array afGradMeanCav;
598 af::array afGradVarianceCav;
599
600 af::array afLatentGradientX;
601
602 XInit eEmMethod;
603
604 IBackconstraint<Scalar>* backConst;
605 std::map<std::string, Style<Scalar>>* mStyles;
606
607 bool bIsLatetsFixed;
608
609 private:
610 friend class boost::serialization::access;
611 friend class KBR<Scalar>;
612 friend class PTC<Scalar>;
613
614 template<class Archive>
615 void serialize(Archive& ar, unsigned int version)
616 {
617 ar& boost::serialization::base_object<GPBaseModel<Scalar>>(*this);
618 ar& boost::serialization::base_object<GPNode<Scalar>>(*this);
619
620 /*ar& boost::serialization::make_nvp("GPBaseModel", boost::serialization::base_object<GPBaseModel<Scalar>>(*this));
621 ar& boost::serialization::make_nvp("GPNode", boost::serialization::base_object<GPNode<Scalar>>(*this));*/
622
623 ar.register_type<KBR<Scalar>>();
624 ar.register_type<PTC<Scalar>>();
625
626 ar& BOOST_SERIALIZATION_NVP(iq);
627 ar& BOOST_SERIALIZATION_NVP(dPriorMean);
628 ar& BOOST_SERIALIZATION_NVP(dPriorVariance);
629 ar& BOOST_SERIALIZATION_NVP(afFactorX1);
630 ar& BOOST_SERIALIZATION_NVP(afFactorX2);
631 ar& BOOST_SERIALIZATION_NVP(eEmMethod);
632 ar& BOOST_SERIALIZATION_NVP(afPriorMean);
633 ar& BOOST_SERIALIZATION_NVP(afPriorVariance);
634 ar& BOOST_SERIALIZATION_NVP(afPriorMeanCav);
635 ar& BOOST_SERIALIZATION_NVP(afPriorVarianceCav);
636 ar& BOOST_SERIALIZATION_NVP(afPriorX1);
637 ar& BOOST_SERIALIZATION_NVP(afPriorX2);
638 ar& BOOST_SERIALIZATION_NVP(backConst);
639 ar& BOOST_SERIALIZATION_NVP(mStyles);
640 ar& BOOST_SERIALIZATION_NVP(bIsLatetsFixed);
641 }
642
643 };
644 }
645 }
646}
NeuralEngine::MachineLearning::GPModels::GPBaseModel
Base class with abstract and basic function definitions. All GP models will be derived from this clas...
Definition: FgGPBaseModel.h:40
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel
Base class with abstract and basic function definitions. All deep GP models will be derived from this...
Definition: FgGPLVMBaseModel.h:295
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPriorVariance
af::array afPriorVariance
prior variance for hiersrchy mode
Definition: FgGPLVMBaseModel.h:588
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afGradVarianceCav
af::array afGradVarianceCav
prior variance gradient for hiersrchy mode
Definition: FgGPLVMBaseModel.h:598
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::UpdateParameters
virtual void UpdateParameters() override
Updates the parameters.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::FixInducing
virtual void FixInducing(bool isfixed)
Set fixation for inducing inputs.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::SetPrior
void SetPrior(const af::array mean, const af::array var)
Sets the prior.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::dPriorX1
Scalar dPriorX1
prior /f$x_1/f$
Definition: FgGPLVMBaseModel.h:579
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::dPriorVariance
Scalar dPriorVariance
prior variance
Definition: FgGPLVMBaseModel.h:578
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::UpdateParametersInternal
virtual void UpdateParametersInternal()
Updates the internal parameters.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GetStyles
std::map< std::string, Style< Scalar > > * GetStyles()
Gets the styles.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::SetParameters
virtual void SetParameters(const af::array &param) override
Sets the parameters for each optimization iteration.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GPLVMBaseModel
GPLVMBaseModel()
Default constructor.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPriorMeanCav
af::array afPriorMeanCav
prior mean for hiersrchy mode
Definition: FgGPLVMBaseModel.h:589
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afFactorX1
af::array afFactorX1
natural parameter factor 1 for latent variable
Definition: FgGPLVMBaseModel.h:582
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GetLatentDimension
int GetLatentDimension()
Gets latent dimension.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afLatentGradientX
af::array afLatentGradientX
top down gradient
Definition: FgGPLVMBaseModel.h:600
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::mStyles
std::map< std::string, Style< Scalar > > * mStyles
style variable
Definition: FgGPLVMBaseModel.h:605
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::Init
virtual bool Init() override
Initializes the model.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GetNumParameters
virtual int GetNumParameters() override
Gets number of parameters.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::SetStyles
void SetStyles(std::map< std::string, Style< Scalar > > *styles)
Sets the syles.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::~GPLVMBaseModel
virtual ~GPLVMBaseModel()
Destructor.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afGradMean
af::array afGradMean
prior mean gradient for hiersrchy mode
Definition: FgGPLVMBaseModel.h:595
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::Optimise
virtual void Optimise(OptimizerType method=L_BFGS, Scalar tol=0.0, bool reinit_hypers=true, int maxiter=1000, int mb_size=0, LineSearchType lsType=MoreThuente, bool disp=true, int *cycle=nullptr) override
Optimizes the model parameters for best fit.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPriorX2Cav
af::array afPriorX2Cav
prior /f$x_2/f$
Definition: FgGPLVMBaseModel.h:594
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::FixKernelParameters
virtual void FixKernelParameters(bool isfixed)
Sets fixation for hyperparameters.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::SetLatentGradient
void SetLatentGradient(const af::array &dmParent, const af::array &dvParent)
Sets latent gradient.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::SetLatentGradientCavity
void SetLatentGradientCavity(const af::array &dmParent, const af::array &dvParent)
Sets the latent cavity gradient.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::Init
virtual bool Init(af::array &mx)
Initializes the model.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPosteriorX1
af::array afPosteriorX1
posterior natural parameter 1 for latent variable
Definition: FgGPLVMBaseModel.h:584
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afGradMeanCav
af::array afGradMeanCav
prior mean gradient for hiersrchy mode
Definition: FgGPLVMBaseModel.h:597
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afFactorX2
af::array afFactorX2
natural parameter factor 2 for latent variable
Definition: FgGPLVMBaseModel.h:583
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::SetPriorCavity
void SetPriorCavity(const af::array meanCav, const af::array varCav)
Sets the cavity prior.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPriorVarianceCav
af::array afPriorVarianceCav
prior variance for hiersrchy mode
Definition: FgGPLVMBaseModel.h:590
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::backConst
IBackconstraint< Scalar > * backConst
back-constraint
Definition: FgGPLVMBaseModel.h:604
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPriorX1
af::array afPriorX1
prior /f$x_1/f$
Definition: FgGPLVMBaseModel.h:591
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GetMeanGradient
af::array GetMeanGradient()
Gets prior mean gradient.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::AddStyle
void AddStyle(Style< Scalar > style)
Adds a style.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GPLVMBaseModel
GPLVMBaseModel(const af::array &Y, int latentDimension, Scalar priorMean=0.0, Scalar priorVariance=1.0, LogLikType lType=LogLikType::Gaussian, XInit emethod=XInit::pca)
Constructor.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GetVarGradient
af::array GetVarGradient()
Gets prior variance gradient.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPriorMean
af::array afPriorMean
prior mean for hiersrchy mode
Definition: FgGPLVMBaseModel.h:587
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afGradVariance
af::array afGradVariance
prior variance gradient for hiersrchy mode
Definition: FgGPLVMBaseModel.h:596
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::dPriorMean
Scalar dPriorMean
prior mean
Definition: FgGPLVMBaseModel.h:577
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::PosteriorLatents
virtual void PosteriorLatents(af::array &mx, af::array &vx)
Get posterior distribution of latent variables /f$\mathbf{X}/f$.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPosteriorX2
af::array afPosteriorX2
posterior natural parameter 2 for latent variable
Definition: FgGPLVMBaseModel.h:585
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPriorX2
af::array afPriorX2
prior /f$x_2/f$
Definition: FgGPLVMBaseModel.h:592
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::afPriorX1Cav
af::array afPriorX1Cav
prior /f$x_1/f$
Definition: FgGPLVMBaseModel.h:593
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::PosteriorGradientLatents
virtual af::array PosteriorGradientLatents(const af::array &dmx, const af::array &dvx)
Posterior gradient of latent inputs /f$\mathbf{X}/f$.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::LatentGradient
virtual af::array LatentGradient(const af::array &dm, const af::array &dv)
Latent gradient.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GetParameters
virtual af::array GetParameters() override
Gets the parameters for each optimization iteration.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::SetBackConstraint
void SetBackConstraint(IBackconstraint< Scalar > *constraint)
Sets a back-constraint.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::GetBackConstraint
IBackconstraint< Scalar > * GetBackConstraint()
Gets the back-constraint.
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::iq
int iq
latent dimension
Definition: FgGPLVMBaseModel.h:575
NeuralEngine::MachineLearning::GPModels::GPLVMBaseModel::dPriorX2
Scalar dPriorX2
prior /f$x_2/f$
Definition: FgGPLVMBaseModel.h:580
NeuralEngine::MachineLearning::GPModels::IBackconstraint
Abstract class for back-constraints, a kind of prior knowledge to force topological positions of unce...
Definition: FgIBackconstraint.h:42
NeuralEngine::MachineLearning::GPModels::KBR
Back-constraints via kernel based regression.
Definition: FgKernelBasedRegression.h:68
NeuralEngine::MachineLearning::GPModels::PTC
Back-constraints via Periodic topological constraint.
Definition: FgPeriodicTopologicalConstraint.h:54
NeuralEngine::MachineLearning::GPModels::Style
Style variable.
Definition: FgGPLVMBaseModel.h:33
NeuralEngine::MachineLearning::GPModels::Style::GetNumInducingSubstyles
virtual int GetNumInducingSubstyles()
Gets number of inducing sub-styles.
NeuralEngine::MachineLearning::GPModels::Style::GetGradientCollapsed
af::array GetGradientCollapsed(const af::array &factorGradient)
Gets gradient collapsed.
NeuralEngine::MachineLearning::GPModels::Style::~Style
virtual ~Style()
Destructor.
NeuralEngine::MachineLearning::GPModels::Style::UpdatePosterior
void UpdatePosterior(const af::array &postX1, const af::array &postX2)
Updates the parameters.
NeuralEngine::MachineLearning::GPModels::Style::SetParameters
void SetParameters(const af::array &param)
Sets the parameters.
NeuralEngine::MachineLearning::GPModels::Style::GetNumInducingParameters
int GetNumInducingParameters()
Gets number of inducing parameters.
NeuralEngine::MachineLearning::GPModels::Style::GetStyleIndex
const af::array GetStyleIndex()
Gets style index.
NeuralEngine::MachineLearning::GPModels::Style::InitFactors
void InitFactors(const af::array &expandedX1, const af::array &expandedX2)
Initializes the factors.
NeuralEngine::MachineLearning::GPModels::Style::GetNumSubstyles
virtual int GetNumSubstyles()
Gets number of sub-styles.
NeuralEngine::MachineLearning::GPModels::Style::GetParameters
af::array GetParameters()
Gets the parameters.
NeuralEngine::MachineLearning::GPModels::Style::GetFactorsExpanded
void GetFactorsExpanded(af::array &X1, af::array &X2)
Gets factor x coordinate 1.
NeuralEngine::MachineLearning::GPModels::Style::GetInducingGradientCollapsed
af::array GetInducingGradientCollapsed(const af::array &styleGradient)
Gets style inducing gradient collapsed.
NeuralEngine::MachineLearning::GPModels::Style::AddSubstyle
virtual void AddSubstyle(std::string name, int numFrames)
Adds a sub-style.
NeuralEngine::MachineLearning::GPModels::Style::GetInducingParameters
af::array GetInducingParameters()
Gets inducing parameters.
NeuralEngine::MachineLearning::GPModels::Style::GetStyleExpanded
af::array GetStyleExpanded()
Gets style variable expanded to match associated /f$\matchbf{x}$/f.
NeuralEngine::MachineLearning::GPModels::Style::GetName
std::string GetName()
Gets the name of the style.
NeuralEngine::MachineLearning::GPModels::Style::GetNumParameters
int GetNumParameters()
Gets number parameters.
NeuralEngine::MachineLearning::GPModels::Style::GetStyle
af::array GetStyle()
Gets the style variable.
NeuralEngine::MachineLearning::GPModels::Style::Style
Style()
Default constructor.
Definition: FgGPLVMBaseModel.h:50
NeuralEngine::MachineLearning::GPModels::Style::GetSubstyles
std::map< std::string, int > GetSubstyles()
Gets the sub-styles.
NeuralEngine::MachineLearning::GPModels::Style::SetInducingParameters
void SetInducingParameters(const af::array &param)
Sets inducing parameters.
NeuralEngine::MachineLearning::GPModels::Style::m_dType
af::dtype m_dType
floating point precision flag for af::array
Definition: FgGPLVMBaseModel.h:265
NeuralEngine::MachineLearning::GPModels::Style::GetInducingStyleExpanded
af::array GetInducingStyleExpanded()
Gets inducing style expanded.
NeuralEngine::MachineLearning::GPModels::Style::SetInducingStyleIndex
void SetInducingStyleIndex(const af::array &indx)
Sets inducing style index.
NeuralEngine::MachineLearning::GPModels::Style::Style
Style(std::string name)
Default constructor.
NeuralEngine::MachineLearning::GPNode
This class represents grouping nodes in a hiearchy.
Definition: FgGPNode.h:42
Scalar
NeuralEngine
Definition: NeArray2.h:18