From 5321e01ccad4b2cfadb9950e0c85c2ca10754ce1 Mon Sep 17 00:00:00 2001 From: jens Date: Thu, 1 Feb 2024 19:03:13 +0100 Subject: [PATCH] - improved cd_hinton() - uniform() returns vector --- source/Rbm.cpp | 36 ++++++++++++++++++++++-------------- source/matutils.hpp | 31 +++---------------------------- 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/source/Rbm.cpp b/source/Rbm.cpp index c2955f3..cb3a99e 100644 --- a/source/Rbm.cpp +++ b/source/Rbm.cpp @@ -17,7 +17,7 @@ using namespace Matutils; -#define USE_CD_HINTON 0 +#define USE_CD_HINTON 1 Rbm::Rbm(size_t numVisible, size_t numHidden) : m_params() @@ -97,9 +97,9 @@ void Rbm::weightsAssign(const arma::mat& w, const arma::mat& bh, const arma::mat void Rbm::weightsInit(double stddev, double mu) { - uniform(m_whv, stddev, mu); - uniform(m_bh, 0, mu); - uniform(m_bv, 0, mu); + m_whv = uniform(m_whv, stddev, mu); + m_bh = uniform(m_bh, 0, 0); + m_bv = uniform(m_bv, 0, 0); } void Rbm::fromJson(Json::Value rbm) @@ -190,7 +190,14 @@ void Rbm::cd_hinton(arma::mat const &v_data, arma::mat &dw, arma::mat &dbh, arma if (m_params.doGaussianHidden) { - poshidstates = poshidprobs + arma::randn(arma::size(poshidprobs)); + if (m_params.gibbsDoSampleHidden) + { + poshidstates = poshidprobs + arma::randn(arma::size(poshidprobs)); + } + else + { + poshidstates = poshidprobs; + } } else { @@ -212,7 +219,15 @@ void Rbm::cd_hinton(arma::mat const &v_data, arma::mat &dw, arma::mat &dbh, arma for (int i=0; i < m_params.numGibbs; i++) { // Start negative phase - arma::mat negdata = toVisibleProbs(poshidstates); + arma::mat negdata; + if (m_params.gibbsDoSampleHidden) + { + negdata = toVisibleProbs(sample(poshidstates)); + } + else + { + negdata = toVisibleProbs(poshidstates); + } arma::mat neghidprobs; if (m_params.gibbsDoSampleVisible) { @@ -226,14 +241,7 @@ void Rbm::cd_hinton(arma::mat const &v_data, arma::mat &dw, arma::mat &dbh, arma neghidact = arma::sum(neghidprobs); negvisact = arma::sum(negdata); - if (m_params.gibbsDoSampleHidden) - { - poshidprobs = sample(neghidprobs); - } - else - { - poshidprobs = neghidprobs; - } + poshidprobs = neghidprobs; } // Update weight deltas dw = posprods - negprods; diff --git a/source/matutils.hpp b/source/matutils.hpp index c6d5596..9cca18d 100644 --- a/source/matutils.hpp +++ b/source/matutils.hpp @@ -21,40 +21,15 @@ namespace Matutils { const size_t NORMALIZING_DIM = 1; - inline void uniform(arma::mat& srcDst, double stdDev=1.0, double mu=0.5) + inline arma::mat uniform(arma::mat const & sizeMat, double stdDev=1.0, double mu=0.5) { - #if 0 - for (size_t i=0; i < srcDst.n_rows; i++) - { - for (size_t j=0; j < srcDst.n_cols; j++) - { - srcDst(i, j) = stdDev*(Noise_Uniform(&m_noise) + mu - 0.5); - } - } - #else - srcDst = stdDev*(arma::randu(arma::size(srcDst)) + mu - 0.5); - #endif + return stdDev*(arma::randu(arma::size(sizeMat)) + mu - 0.5); } inline arma::mat sample(const arma::mat &src) { - arma::mat rand = src; - uniform(rand); - - #if 0 - for (size_t i=0; i < src.n_rows; i++) - { - for (size_t j=0; j < src.n_cols; j++) - { - dst(i, j) = src(i, j) >= dst(i, j); - } - } - return dst; - #else - arma::umat res = (src > rand); + arma::umat res = (src > uniform(src)); return arma::conv_to::from(res); - - #endif } inline arma::mat prob(const arma::mat &src)