Rbm
- 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:
+42
-35
@@ -68,12 +68,22 @@ double Rbm::rms_error_accu(arma::mat diffErr)
|
|||||||
|
|
||||||
arma::mat Rbm::toHiddenProbs(const arma::mat& visible) const
|
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
|
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)
|
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++)
|
for (int i=0; i < m_params.numGibbs; i++)
|
||||||
{
|
{
|
||||||
// Create hidden representation given v
|
// 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
|
// 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++)
|
for (int i=0; i < m_params.numGibbs; i++)
|
||||||
{
|
{
|
||||||
// Create visible reconstruction (a fantasy...) given hid
|
// 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
|
// 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)
|
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);
|
||||||
{
|
|
||||||
cd_hinton_hid_linear(v_data, dw, dbh, dbv);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cd_hinton_hid_binary(v_data, dw, dbh, dbv);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
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;
|
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
|
// Start positive phase
|
||||||
arma::mat poshidprobs = prob(v_to_h(v_data));
|
arma::mat poshidprobs = toHiddenProbs(v_data);
|
||||||
|
|
||||||
arma::mat posprods;
|
arma::mat posprods;
|
||||||
arma::mat poshidact = arma::sum(poshidprobs);
|
arma::mat poshidact = arma::sum(poshidprobs);
|
||||||
arma::mat posvisact = arma::sum(v_data);
|
arma::mat posvisact = arma::sum(v_data);
|
||||||
|
|
||||||
// End of positive phase
|
// 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)
|
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++)
|
for (int i=0; i < m_params.numGibbs; i++)
|
||||||
{
|
{
|
||||||
// Start negative phase
|
// Start negative phase
|
||||||
arma::mat negdata = prob(h_to_v(poshidstates));
|
arma::mat negdata = toVisibleProbs(poshidstates);
|
||||||
arma::mat neghidprobs;
|
arma::mat neghidprobs;
|
||||||
if (m_params.gibbsDoSampleVisible)
|
if (m_params.gibbsDoSampleVisible)
|
||||||
{
|
{
|
||||||
neghidprobs = prob(v_to_h(sample(negdata)));
|
neghidprobs = toHiddenProbs(sample(negdata));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
neghidprobs = prob(v_to_h(negdata));
|
neghidprobs = toHiddenProbs(negdata);
|
||||||
}
|
}
|
||||||
negprods = negdata.t() * neghidprobs;
|
negprods = negdata.t() * neghidprobs;
|
||||||
neghidact = arma::sum(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)
|
else if (m_params.doRaoBlackwell)
|
||||||
{
|
{
|
||||||
h_probs = prob(v_to_h(v_states));
|
h_probs = toHiddenProbs(v_states);
|
||||||
h_states = h_probs;
|
h_states = h_probs;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
h_probs = prob(v_to_h(v_states));
|
h_probs = toHiddenProbs(v_states);
|
||||||
h_states = sample(h_probs);
|
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
|
// Create visible reconstruction (a fantasy...) given hid
|
||||||
if (m_params.gibbsDoSampleHidden)
|
if (m_params.gibbsDoSampleHidden)
|
||||||
{
|
{
|
||||||
v_probs = prob(h_to_v(sample(h_probs)));
|
v_probs = toVisibleProbs(sample(h_probs));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
v_probs = prob(h_to_v(h_probs));
|
v_probs = toVisibleProbs(h_probs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create hidden representation given v
|
// Create hidden representation given v
|
||||||
if (m_params.doGaussianHidden)
|
if (m_params.gibbsDoSampleVisible)
|
||||||
{
|
{
|
||||||
h_probs = v_to_h(v_probs);
|
h_probs = toHiddenProbs(sample(v_probs));
|
||||||
}
|
|
||||||
else if (m_params.gibbsDoSampleVisible)
|
|
||||||
{
|
|
||||||
h_probs = prob(v_to_h(sample(v_probs)));
|
|
||||||
}
|
}
|
||||||
else
|
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;
|
lastProgress = status.progress;
|
||||||
|
|
||||||
// Calculate error
|
// 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)
|
||||||
{
|
{
|
||||||
if(!pListener->onProgress(this, status))
|
if(!pListener->onProgress(this, status))
|
||||||
|
|||||||
+3
-4
@@ -145,9 +145,6 @@ public:
|
|||||||
size_t numHidden() const;
|
size_t numHidden() const;
|
||||||
size_t numVisible() const;
|
size_t numVisible() const;
|
||||||
|
|
||||||
arma::mat v_to_h(const arma::mat &visible) const;
|
|
||||||
arma::mat h_to_v(const arma::mat &hidden) const;
|
|
||||||
|
|
||||||
void gibbs_hv(arma::mat &h_probs, arma::mat &v_probs) const;
|
void gibbs_hv(arma::mat &h_probs, arma::mat &v_probs) const;
|
||||||
void gibbs_vh(arma::mat &v_probs, arma::mat &h_probs) const;
|
void gibbs_vh(arma::mat &v_probs, arma::mat &h_probs) const;
|
||||||
|
|
||||||
@@ -161,10 +158,12 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
arma::mat m_whv;
|
arma::mat m_whv;
|
||||||
void cd_hinton_hid_binary(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
|
void cd_hinton(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
|
||||||
void cd_hinton_hid_linear(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
|
void cd_hinton_hid_linear(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
|
||||||
void cd_jens(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
|
void cd_jens(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
|
||||||
void cd(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
|
void cd(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
|
||||||
|
arma::mat v_to_h(const arma::mat &visible) const;
|
||||||
|
arma::mat h_to_v(const arma::mat &hidden) const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user