From ef82ae7ea61fe86f8dbcc862d61157909ad8b053 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 8 Jan 2022 11:10:26 +0000 Subject: [PATCH] - refactored in gibbs git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@753 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- source/Rbm.cpp | 64 ++++++++++++++++++++++++++++++-------------------- source/Rbm.hpp | 5 ++-- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/source/Rbm.cpp b/source/Rbm.cpp index 1c11ca3..d9a1da4 100644 --- a/source/Rbm.cpp +++ b/source/Rbm.cpp @@ -71,10 +71,46 @@ Json::Value Rbm::toJson() const return rbm; } +void Rbm::weightUpdate(arma::mat const &v_states, arma::mat const &c_states, arma::mat &dwhv, arma::mat &dwhc, arma::mat &dbh, arma::mat &dbv, arma::mat &dbc) +{ + arma::mat hv_states = v_to_h(v_states); + arma::mat hc_states = c_to_h(c_states); + + arma::mat h_states = hv_states + hc_states; + arma::mat h_probs = prob(h_states); + + +} + +void Rbm::gibbs(arma::mat &h_probs, arma::mat &v_probs) +{ + for (int gibbs=0; gibbs < m_params.numGibbs; gibbs++) + { + // Create visible reconstruction (a fantasy...) given hid + if (m_params.gibbsDoSampleHidden) + { + v_probs = prob(h_to_v(sample(h_probs))); + } + else + { + v_probs = prob(h_to_v(h_probs)); + } + + // Create hidden representation given v + if (m_params.gibbsDoSampleVisible) + { + h_probs = prob(v_to_h(sample(v_probs))); + } + else + { + h_probs = prob(v_to_h(v_probs)); + } + } +} + void Rbm::weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, arma::mat &dbv) { arma::mat v_probs(dbv.n_rows, dbv.n_cols); - arma::mat h_states = v_to_h(v_states); arma::mat h_probs = prob(h_states); @@ -93,31 +129,7 @@ void Rbm::weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, dbv = sum(v_states, 0); dbh = sum(h_states, 0); - for (int gibbs=0; gibbs < m_params.numGibbs; gibbs++) - { - // Create visible reconstruction (a fantasy...) given hid - if (m_params.gibbsDoSampleHidden) - { - v_probs = prob(h_to_v(sample(h_probs))); - } - else - { - v_probs = prob(h_to_v(h_probs)); - } - - // Create hidden representation given v - if (m_params.gibbsDoSampleVisible) - { - h_states = v_to_h(sample(v_probs)); - } - else - { - h_states = v_to_h(v_probs); - } - - h_probs = prob(h_states); - - } + gibbs(h_probs, v_probs); // Update weights (negative phase) dw -= v_probs.t() * h_probs; diff --git a/source/Rbm.hpp b/source/Rbm.hpp index 66b24c2..2124815 100644 --- a/source/Rbm.hpp +++ b/source/Rbm.hpp @@ -142,9 +142,10 @@ public: private: Params m_params; arma::mat sample(arma::mat const &src); - void weightUpdate(arma::mat const &v_state, arma::mat &dw, arma::mat &dbh, arma::mat &dbv); + void weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, arma::mat &dbv); + void weightUpdate(arma::mat const &v_states, arma::mat const &c_states, arma::mat &dwhv, arma::mat &dwhc, arma::mat &dbh, arma::mat &dbv, arma::mat &dbc); void uniform(arma::mat &srcDst, double stdDev=1.0, double mu=0.5); - + void gibbs(arma::mat &h_states, arma::mat &v_states); protected: arma::mat m_whv; arma::mat m_whc;