/* * Rbm.hpp * * Created on: 21.09.2014 * Author: jens */ #ifndef RBM_HPP_ #define RBM_HPP_ #include "Weights.hpp" #include #include using namespace Eigen; class Rbm { public: struct Params { Params() : m_constantSigma(1.0) , m_sigmaDecay(0.0) , m_weightDecay(0.0) , m_lambda(1.0) , m_sparsity(0.05) , m_muWeights(0.1) , m_muSparsity(0.01) , m_momentum(0.5) , m_useVisibleGaussian(false) , m_useHiddenGaussian(false) , m_doRaoBlackwell(true) , m_doSampleVisible(false) , m_doSampleBatch(false) , m_doSparse(false) , m_doNormalizeData(false) , m_doLearnVariance(false) , m_numGibbs(1) { } double m_constantSigma; double m_sigmaDecay; double m_weightDecay; double m_lambda; double m_sparsity; double m_muWeights; double m_muSparsity; double m_momentum; bool m_useVisibleGaussian; bool m_useHiddenGaussian; bool m_doRaoBlackwell; bool m_doSampleVisible; bool m_doSampleBatch; bool m_doSparse; bool m_doNormalizeData; bool m_doLearnVariance; size_t m_numGibbs; }; Rbm(Weights &weights, const MatrixXd &batch); ~Rbm(); void sample(MatrixXd &srcDst); void sample(MatrixXd &dst, MatrixXd const &src); static void probsLogistic(MatrixXd &src); static void probsLogistic(RowVectorXd &src); static void probsLogistic(MatrixXd &src, const MatrixXd &sigma); static void probsLogistic(RowVectorXd &src, const RowVectorXd &sigma); static void probsGaussian(MatrixXd &src, const MatrixXd &sigma); static void probsGaussian(RowVectorXd &src, const RowVectorXd &sigma); void sampleGaussian(MatrixXd &dst, MatrixXd const &src, const MatrixXd &sigma); void sampleGaussian(MatrixXd &srcDst, const MatrixXd &sigma); void sampleGaussian(MatrixXd &srcDst, const double &sigma); RowVectorXd normalizeData(RowVectorXd const &src, RowVectorXd const &mu, RowVectorXd const &var); RowVectorXd calcMean(MatrixXd const &batch); RowVectorXd calcSigma(MatrixXd const &batch); MatrixXd calcZ(MatrixXd &v, MatrixXd &h); void train(size_t numEpochs, size_t miniBatchSize, double sigmaMin = 0.05); double getProgress() const; double getEnergy(const VectorXd& visible, const VectorXd& hidden); void toHidden(RowVectorXd &h, RowVectorXd const &v); void toVisible(RowVectorXd &v, RowVectorXd const &h); void setConstantSigma(double value); RowVectorXd& getVariableSigma(); void setSigmaDecay(double value); void setWeightDecay(double value); void setLambda(double value); void setSparsity(double value); void setUseVisibleGaussian(bool flag); void setUseHiddenGaussian(bool flag); void setDoRaoBlackwell(bool flag); void setDoSampleVisible(bool flag); void setDoSampleBatch(bool flag); void setDoSparse(bool flag); void setNormalizeData(bool flag); void setDoLearnVariance(bool flag); void setNumGibbs(size_t value); void setMuWeights(double value); void setMuSparsity(double value); void setMomentum(double value); MatrixXd const& getHiddenBatch(); MatrixXd const& getBatch(); void updateHiddenBatch(); Params const& params(); private: Weights &m_w; MatrixXd const &m_batch; MatrixXd m_h; RowVectorXd m_variableSigma; noise_gen_t m_noise; double m_progress; Params m_params; void noiseGaussian(MatrixXd &dst); void noiseUniform(MatrixXd &dst); protected: virtual void onProgressChanged() { } virtual void onParamsChanged() { } }; #endif /* RBM_HPP_ */