- Layer: do gibbs sampling in calcContextBatch()

- RBM: added Gibbs sampler
- Stack adjust training column vector according to needs

git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@797 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2022-01-13 07:19:47 +00:00
parent dba52a651d
commit be64a88e04
5 changed files with 48 additions and 13 deletions
+32 -6
View File
@@ -61,28 +61,54 @@ Json::Value Rbm::toJson() const
return rbm;
}
void Rbm::gibbs(arma::mat &hv_probs, arma::mat &v_probs)
void Rbm::gibbs_vh(arma::mat &v_probs, arma::mat &h_probs)
{
for (int i=0; i < m_params.numGibbs; i++)
{
// 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));
}
// 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));
}
}
}
void Rbm::gibbs_hv(arma::mat &h_probs, arma::mat &v_probs)
{
for (int i=0; i < m_params.numGibbs; i++)
{
// Create visible reconstruction (a fantasy...) given hid
if (m_params.gibbsDoSampleHidden)
{
v_probs = prob(h_to_v(sample(hv_probs)));
v_probs = prob(h_to_v(sample(h_probs)));
}
else
{
v_probs = prob(h_to_v(hv_probs));
v_probs = prob(h_to_v(h_probs));
}
// Create hidden representation given v
if (m_params.gibbsDoSampleVisible)
{
hv_probs = prob(v_to_h(sample(v_probs)));
h_probs = prob(v_to_h(sample(v_probs)));
}
else
{
hv_probs = prob(v_to_h(v_probs));
h_probs = prob(v_to_h(v_probs));
}
}
}
@@ -108,7 +134,7 @@ void Rbm::contrastiveDivergence(arma::mat const &v_states, arma::mat &dw, arma::
dbv = sum(v_states, 0);
dbhv = sum(h_states, 0);
gibbs(h_probs, v_probs);
gibbs_hv(h_probs, v_probs);
// Update weights (negative phase)
dw -= v_probs.t() * h_probs;