- enable linear hidden : instead of prob(v_to_h()): use toHiddenProbs()
- enable linear visible: instead of prob(h_to_v()): use toVisibleProbs()
- make v_to_h() and h_to_v() private and force to use toHiddenProbs() and toVisibleProbs()
- use cd_jens or cd_hinton. cd_hinton_hid_lineaer is not of use anymre, since it is not capable of CDn
This commit is contained in:
2024-01-31 12:03:47 +01:00
parent 285be92cdb
commit e211c568ac
2 changed files with 45 additions and 39 deletions
+42 -35
View File
@@ -68,12 +68,22 @@ double Rbm::rms_error_accu(arma::mat diffErr)
arma::mat Rbm::toHiddenProbs(const arma::mat& visible) const
{
return prob(v_to_h(visible));
arma::mat state = v_to_h(visible);
if (m_params.doGaussianHidden)
{
return state;
}
return prob(state);
}
arma::mat Rbm::toVisibleProbs(const arma::mat& hidden) const
{
return prob(h_to_v(hidden));
arma::mat state = h_to_v(hidden);
if (m_params.doGaussianVisible)
{
return state;
}
return prob(state);
}
void Rbm::weightsAssign(const arma::mat& w, const arma::mat& bh, const arma::mat& bv)
@@ -109,10 +119,10 @@ void Rbm::gibbs_vh(arma::mat &v_probs, arma::mat &h_probs) const
for (int i=0; i < m_params.numGibbs; i++)
{
// Create hidden representation given v
h_probs = prob(v_to_h(v_probs));
h_probs = toHiddenProbs(v_probs);
// Create visible reconstruction (a fantasy...) given hid
v_probs = prob(h_to_v(h_probs));
v_probs = toVisibleProbs(h_probs);
}
}
@@ -121,29 +131,22 @@ void Rbm::gibbs_hv(arma::mat &h_probs, arma::mat &v_probs) const
for (int i=0; i < m_params.numGibbs; i++)
{
// Create visible reconstruction (a fantasy...) given hid
v_probs = prob(h_to_v(h_probs));
v_probs = toVisibleProbs(h_probs);
// Create hidden representation given v
h_probs = prob(v_to_h(v_probs));
h_probs = toHiddenProbs(v_probs);
}
}
void Rbm::cd(arma::mat const &v_data, arma::mat &dw, arma::mat &dbh, arma::mat &dbv)
{
if (m_params.doGaussianVisible)
if (m_params.doGaussianHidden)
{
if (m_params.doGaussianHidden)
{
cd_hinton_hid_linear(v_data, dw, dbh, dbv);
}
else
{
cd_hinton_hid_binary(v_data, dw, dbh, dbv);
}
cd_hinton_hid_linear(v_data, dw, dbh, dbv);
}
else
{
cd_jens(v_data, dw, dbh, dbv);
cd_hinton(v_data, dw, dbh, dbv);
}
}
@@ -171,17 +174,25 @@ void Rbm::cd_hinton_hid_linear(arma::mat const &v_data, arma::mat &dw, arma::mat
dbh = poshidact - neghidact;
}
void Rbm::cd_hinton_hid_binary(arma::mat const &v_data, arma::mat &dw, arma::mat &dbh, arma::mat &dbv)
void Rbm::cd_hinton(arma::mat const &v_data, arma::mat &dw, arma::mat &dbh, arma::mat &dbv)
{
// Start positive phase
arma::mat poshidprobs = prob(v_to_h(v_data));
arma::mat poshidprobs = toHiddenProbs(v_data);
arma::mat posprods;
arma::mat poshidact = arma::sum(poshidprobs);
arma::mat posvisact = arma::sum(v_data);
// End of positive phase
arma::mat poshidstates = sample(poshidprobs);
arma::mat poshidstates;
if (m_params.doGaussianHidden)
{
poshidstates = poshidprobs + arma::randn(arma::size(poshidprobs));
}
else
{
poshidstates = sample(poshidprobs);
}
if (m_params.doRaoBlackwell)
{
@@ -198,15 +209,15 @@ void Rbm::cd_hinton_hid_binary(arma::mat const &v_data, arma::mat &dw, arma::mat
for (int i=0; i < m_params.numGibbs; i++)
{
// Start negative phase
arma::mat negdata = prob(h_to_v(poshidstates));
arma::mat negdata = toVisibleProbs(poshidstates);
arma::mat neghidprobs;
if (m_params.gibbsDoSampleVisible)
{
neghidprobs = prob(v_to_h(sample(negdata)));
neghidprobs = toHiddenProbs(sample(negdata));
}
else
{
neghidprobs = prob(v_to_h(negdata));
neghidprobs = toHiddenProbs(negdata);
}
negprods = negdata.t() * neghidprobs;
neghidact = arma::sum(neghidprobs);
@@ -241,12 +252,12 @@ void Rbm::cd_jens(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, arma
}
else if (m_params.doRaoBlackwell)
{
h_probs = prob(v_to_h(v_states));
h_probs = toHiddenProbs(v_states);
h_states = h_probs;
}
else
{
h_probs = prob(v_to_h(v_states));
h_probs = toHiddenProbs(v_states);
h_states = sample(h_probs);
}
@@ -261,25 +272,21 @@ void Rbm::cd_jens(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, arma
// Create visible reconstruction (a fantasy...) given hid
if (m_params.gibbsDoSampleHidden)
{
v_probs = prob(h_to_v(sample(h_probs)));
v_probs = toVisibleProbs(sample(h_probs));
}
else
{
v_probs = prob(h_to_v(h_probs));
v_probs = toVisibleProbs(h_probs);
}
// Create hidden representation given v
if (m_params.doGaussianHidden)
if (m_params.gibbsDoSampleVisible)
{
h_probs = v_to_h(v_probs);
}
else if (m_params.gibbsDoSampleVisible)
{
h_probs = prob(v_to_h(sample(v_probs)));
h_probs = toHiddenProbs(sample(v_probs));
}
else
{
h_probs = prob(v_to_h(v_probs));
h_probs = toHiddenProbs(v_probs);
}
}
@@ -347,7 +354,7 @@ void Rbm::train(arma::mat const &batch, IListener* pListener)
lastProgress = status.progress;
// Calculate error
status.err = rms_error_accu(miniBatch - prob(h_to_v(prob(v_to_h(v_states)))));
status.err = rms_error_accu(miniBatch - toVisibleProbs(toHiddenProbs(v_states)));
if (pListener)
{
if(!pListener->onProgress(this, status))