- 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;
+3 -28
View File
@@ -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<arma::mat>::from(res);
#endif
}
inline arma::mat prob(const arma::mat &src)