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

FgAEPSparseGPLayer.h
1
11#pragma once
12
13#include <MachineLearning/FgSparseGPBaseLayer.h>
14
15 namespace NeuralEngine
16{
17 namespace MachineLearning
18 {
19 namespace GPModels
20 {
21 namespace AEP
22 {
23
38 template<typename Scalar>
39 class NE_IMPEXP SGPLayer : public SparseGPBaseLayer<Scalar>
40 {
41 public:
42
53 SGPLayer(int numPoints, int numPseudos, int outputDim, int inputDim);
54
60 virtual ~SGPLayer();
61
78 void ForwardPredictionCavity(af::array& outMout, af::array& outVout, af::array* psi1out, af::array* psi2out, const af::array& mx, const af::array* vx = nullptr, Scalar alpha = 1.0, PropagationMode mode = PropagationMode::MomentMatching);
79
91 void ForwardPredictionRandomCavityMC(af::array& mout, af::array& vout, af::array& xout, af::array& eps, const af::array& mx, const af::array& vx, Scalar alpha = 1.0);
92
110 af::array BackpropGradientsReg(const af::array& m, const af::array& v, const af::array& dlogZ_dm, const af::array& dlogZ_dv, const af::array& x,
111 std::map<std::string, af::array>* outGrad_cav = nullptr, Scalar alpha = 1.0);
112
130 af::array BackpropGradientsMM(const af::array& m, const af::array& v, const af::array& dlogZ_dm, const af::array& dlogZ_dv,
131 const af::array& psi1, const af::array& psi2, const af::array& mx, const af::array& vx, std::map<std::string, af::array>* outGrad_cav, Scalar alpha = 1.0);
132
149 af::array BackpropGradientsMC(const af::array& m, const af::array& v, const af::array& eps, const af::array& dlogZ_dm, const af::array& dlogZ_dv, const af::array& x,
150 std::map<std::string, af::array>* outGradInput, Scalar alpha = 1.0);
151
161 Scalar ComputePhi(Scalar alpha);
162
176 void UpdateFactor(af::array& n, std::map<std::string, af::array> grad_cav, Scalar alpha, Scalar decay = 0);
177
183 virtual void UpdateParameters() override;
184
185 protected:
191 SGPLayer() { }
192
193 void ForwardPredictionDeterministicPost(const af::array& mx, af::array* mout, af::array* vout) override;
194
214 void ForwardPredictionDeterministicCavity(af::array& outMout, af::array& outVout, af::array* kfuOut, const af::array& mx, Scalar alpha = 1.0);
215
229 void ForwardPredictionRandomCavity(af::array& mout, af::array& vout, af::array* psi1out, af::array* psi2out, const af::array& mx, const af::array& vx, PropagationMode mode, Scalar alpha = 1.0);
230
242 void ForwardPredictionRandomCavityMM(af::array& mout, af::array& vout, af::array* psi1out, af::array* psi2out, const af::array& mx, const af::array& vx, Scalar alpha = 1.0);
243
255 virtual void ForwardPredictionRandomPost(const af::array& mx, const af::array& vx, af::array& mout, af::array& vout, PropagationMode mode = PropagationMode::MomentMatching) override;
256
267 void ForwardPredictionRandomPostMM(const af::array& mx, const af::array& vx, af::array& mout, af::array& vout);
268
290 void ComputeCavity(Scalar alpha = 1.0f);
291
304 void ComputeCavityGradientU(af::array& dMucav, af::array& dSucav, af::array& out_dT1, af::array& out_dT2, af::array& out_dInvKuu, Scalar alpha = 1.0f);
305
316 void ComputePosteriorGradientU(af::array& dMu, af::array& dSu, af::array& out_dT1, af::array& out_dT2, af::array& out_dInvKuu);
317
325 Scalar ComputePhiPrior();
326
334 Scalar ComputePhiPosterior();
335
343 Scalar ComputePhiCavity();
344
345 // AEP variables
346 af::array afGamma;
347 af::array afBeta;
348 af::array afGammaHat;
349 af::array afBetaHat;
350 af::array afSuMuMu;
351 af::array afBetaStochastic;
352 af::array afBetaHatStochastic;
353
354 // Cavity temp variables
355 af::array afMuHat; // mean^{\n} of q(u)
356 af::array afSuHat; // covariance^{\n} of q(u)
357 af::array afInvSuHat; // T_{2,u}
358 af::array afInvSuMuHat; // T_{1,u}
359 af::array afSuMuMuHat;
360
361 private:
362 friend class boost::serialization::access;
363 template<typename> friend class SDGPR;
364
365 template<class Archive>
366 void serialize(Archive& ar, unsigned int version)
367 {
368 ar& boost::serialization::base_object<SparseGPBaseLayer<Scalar>>(*this);
369 //ar& boost::serialization::make_nvp("SparseGPBaseLayer", boost::serialization::base_object<SparseGPBaseLayer<Scalar>>(*this));
370 /*ar& BOOST_SERIALIZATION_NVP(afGamma);
371 ar& BOOST_SERIALIZATION_NVP(afBeta);
372 ar& BOOST_SERIALIZATION_NVP(afGammaHat);
373 ar& BOOST_SERIALIZATION_NVP(afBetaHat);
374 ar& BOOST_SERIALIZATION_NVP(afMuHat);
375 ar& BOOST_SERIALIZATION_NVP(afSuHat);
376 ar& BOOST_SERIALIZATION_NVP(afInvSuHat);
377 ar& BOOST_SERIALIZATION_NVP(afInvSuMuHat);
378 ar& BOOST_SERIALIZATION_NVP(afSuMuMuHat);
379 ar& BOOST_SERIALIZATION_NVP(afSuMuMu);
380 ar& BOOST_SERIALIZATION_NVP(afBetaStochastic);
381 ar& BOOST_SERIALIZATION_NVP(afBetaHatStochastic);*/
382 }
383 };
384 }
385 }
386 }
387}
388
NeuralEngine::MachineLearning::GPModels::AEP::SDGPR
Deep sparse Gaussian process via Approximated Expectation Propagation (AEP).
Definition: FgAEPSparseDGPR.h:51
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer
Sparse GP layer.
Definition: FgAEPSparseGPLayer.h:40
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ComputePhiPrior
Scalar ComputePhiPrior()
Calculates energy contribution phi prior.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::SGPLayer
SGPLayer()
Default constructor.
Definition: FgAEPSparseGPLayer.h:191
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ForwardPredictionRandomPostMM
void ForwardPredictionRandomPostMM(const af::array &mx, const af::array &vx, af::array &mout, af::array &vout)
Forward prediction through posterior via Moment Matching.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ComputeCavity
void ComputeCavity(Scalar alpha=1.0f)
Calculates the cavity.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::BackpropGradientsMC
af::array BackpropGradientsMC(const af::array &m, const af::array &v, const af::array &eps, const af::array &dlogZ_dm, const af::array &dlogZ_dv, const af::array &x, std::map< std::string, af::array > *outGradInput, Scalar alpha=1.0)
Back propagation gradients through MCMC.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ForwardPredictionDeterministicPost
void ForwardPredictionDeterministicPost(const af::array &mx, af::array *mout, af::array *vout) override
Deterministic forward propagation through posterior.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ForwardPredictionRandomCavity
void ForwardPredictionRandomCavity(af::array &mout, af::array &vout, af::array *psi1out, af::array *psi2out, const af::array &mx, const af::array &vx, PropagationMode mode, Scalar alpha=1.0)
Forward prediction through random cavity.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::UpdateParameters
virtual void UpdateParameters() override
Updates the parameters.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ComputePosteriorGradientU
void ComputePosteriorGradientU(af::array &dMu, af::array &dSu, af::array &out_dT1, af::array &out_dT2, af::array &out_dInvKuu)
Calculates the gradient contributions of posterior.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::~SGPLayer
virtual ~SGPLayer()
Destructor.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ComputeCavityGradientU
void ComputeCavityGradientU(af::array &dMucav, af::array &dSucav, af::array &out_dT1, af::array &out_dT2, af::array &out_dInvKuu, Scalar alpha=1.0f)
Calculates gradient contributions of cavity distribution.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ForwardPredictionRandomCavityMC
void ForwardPredictionRandomCavityMC(af::array &mout, af::array &vout, af::array &xout, af::array &eps, const af::array &mx, const af::array &vx, Scalar alpha=1.0)
Forward prediction random cavity MCMC.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ForwardPredictionCavity
void ForwardPredictionCavity(af::array &outMout, af::array &outVout, af::array *psi1out, af::array *psi2out, const af::array &mx, const af::array *vx=nullptr, Scalar alpha=1.0, PropagationMode mode=PropagationMode::MomentMatching)
Forward prediction through cavity.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::UpdateFactor
void UpdateFactor(af::array &n, std::map< std::string, af::array > grad_cav, Scalar alpha, Scalar decay=0)
Update step of PowerEP.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::BackpropGradientsMM
af::array BackpropGradientsMM(const af::array &m, const af::array &v, const af::array &dlogZ_dm, const af::array &dlogZ_dv, const af::array &psi1, const af::array &psi2, const af::array &mx, const af::array &vx, std::map< std::string, af::array > *outGrad_cav, Scalar alpha=1.0)
Back propagation gradients through moment matching.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ComputePhiCavity
Scalar ComputePhiCavity()
Calculates energy contribution phi cavity.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ForwardPredictionRandomPost
virtual void ForwardPredictionRandomPost(const af::array &mx, const af::array &vx, af::array &mout, af::array &vout, PropagationMode mode=PropagationMode::MomentMatching) override
Forward prediction through random posterior.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::SGPLayer
SGPLayer(int numPoints, int numPseudos, int outputDim, int inputDim)
Constructor.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ComputePhiPosterior
Scalar ComputePhiPosterior()
Calculates energy contribution phi posterior.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::BackpropGradientsReg
af::array BackpropGradientsReg(const af::array &m, const af::array &v, const af::array &dlogZ_dm, const af::array &dlogZ_dv, const af::array &x, std::map< std::string, af::array > *outGrad_cav=nullptr, Scalar alpha=1.0)
Back propagation gradients through usual GP regression task.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ComputePhi
Scalar ComputePhi(Scalar alpha)
Computes the weighted sum of the log-partitions of prior, post and cav.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ForwardPredictionRandomCavityMM
void ForwardPredictionRandomCavityMM(af::array &mout, af::array &vout, af::array *psi1out, af::array *psi2out, const af::array &mx, const af::array &vx, Scalar alpha=1.0)
Forward prediction random cavity moment matching.
NeuralEngine::MachineLearning::GPModels::AEP::SGPLayer::ForwardPredictionDeterministicCavity
void ForwardPredictionDeterministicCavity(af::array &outMout, af::array &outVout, af::array *kfuOut, const af::array &mx, Scalar alpha=1.0)
Forward prediction through deterministic cavity.
NeuralEngine::MachineLearning::GPModels::SparseGPBaseLayer
Abstract class for different GP likelihood layers.
Definition: FgSparseGPBaseLayer.h:35
Scalar
NeuralEngine
Definition: NeArray2.h:18