- improved cd_hinton()

- uniform() returns vector
This commit is contained in:
2024-02-01 19:03:13 +01:00
parent 8e44aff7a5
commit 5321e01cca
2 changed files with 25 additions and 42 deletions
+22 -14
View File
@@ -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;