- refactored
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@751 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+21
-21
@@ -73,7 +73,7 @@ Json::Value Rbm::toJson() const
|
||||
|
||||
void Rbm::weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, arma::mat &dbv)
|
||||
{
|
||||
arma::mat h_probs = toHiddenProbs(v_states);
|
||||
arma::mat h_probs = prob(v_to_h(v_states));
|
||||
arma::mat v_probs(dbv.n_rows, dbv.n_cols);
|
||||
|
||||
arma::mat h_states = h_probs;
|
||||
@@ -94,24 +94,24 @@ void Rbm::weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh,
|
||||
// Create visible reconstruction (a fantasy...) given hid
|
||||
if (m_params.gibbsDoSampleHidden)
|
||||
{
|
||||
v_probs = toVisibleProbs(sample(h_probs));
|
||||
v_probs = prob(h_to_v(sample(h_probs)));
|
||||
}
|
||||
else
|
||||
{
|
||||
v_probs = toVisibleProbs(h_probs);
|
||||
v_probs = prob(h_to_v(h_probs));
|
||||
}
|
||||
|
||||
// Create hidden representation given v
|
||||
if (m_params.gibbsDoSampleVisible)
|
||||
{
|
||||
h_states = toHiddenState(sample(v_probs));
|
||||
h_states = v_to_h(sample(v_probs));
|
||||
}
|
||||
else
|
||||
{
|
||||
h_states = toHiddenState(v_probs);
|
||||
h_states = v_to_h(v_probs);
|
||||
}
|
||||
|
||||
h_probs = probsLogistic(h_states);
|
||||
h_probs = prob(h_states);
|
||||
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ void Rbm::train(const arma::mat& batch, IListener* pListener)
|
||||
{
|
||||
#if RBM_TRAIN_FLAT
|
||||
// Sample hidden
|
||||
h_probs = probsLogistic(toHiddenState(v_states));
|
||||
h_probs = prob(v_to_h(v_states));
|
||||
if (m_params.doRaoBlackwell)
|
||||
{
|
||||
hid_states = h_probs;
|
||||
@@ -195,23 +195,23 @@ void Rbm::train(const arma::mat& batch, IListener* pListener)
|
||||
// Create visible reconstruction (a fantasy...) given hid
|
||||
if (m_params.gibbsDoSampleHidden)
|
||||
{
|
||||
v_probs = toVisibleProbs(sample(h_probs));
|
||||
v_probs = prob(h_to_v(sample(h_probs)));
|
||||
}
|
||||
else
|
||||
{
|
||||
v_probs = toVisibleProbs(h_probs);
|
||||
v_probs = prob(h_to_v(h_probs));
|
||||
}
|
||||
|
||||
// Create hidden representation given v
|
||||
if (m_params.gibbsDoSampleVisible)
|
||||
{
|
||||
hid_states = toHiddenState(sample(v_probs));
|
||||
hid_states = v_to_h(sample(v_probs));
|
||||
}
|
||||
else
|
||||
{
|
||||
hid_states = toHiddenState(v_probs);
|
||||
hid_states = v_to_h(v_probs);
|
||||
}
|
||||
h_probs = probsLogistic(hid_states);
|
||||
h_probs = prob(hid_states);
|
||||
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ void Rbm::train(const arma::mat& batch, IListener* pListener)
|
||||
lastProgress = status.progress;
|
||||
|
||||
// Calculate error
|
||||
arma::mat diffErr = miniBatch - toVisibleProbs(toHiddenProbs(v_states));
|
||||
arma::mat diffErr = miniBatch - prob(h_to_v(prob(v_to_h(v_states))));
|
||||
arma::mat diffErr_squared = diffErr % diffErr;
|
||||
status.err = accu(diffErr_squared)/diffErr_squared.n_elem;
|
||||
if (pListener)
|
||||
@@ -260,7 +260,7 @@ void Rbm::train(const arma::mat& batch, IListener* pListener)
|
||||
|
||||
} // number of mini batches
|
||||
|
||||
arma::mat diffErr = batch - toVisibleProbs(toHiddenProbs(batch));
|
||||
arma::mat diffErr = batch - prob(h_to_v(prob(v_to_h(batch))));
|
||||
arma::mat diffErr_squared = diffErr % diffErr;
|
||||
status.err_total = accu(diffErr_squared)/diffErr_squared.n_elem;
|
||||
|
||||
@@ -270,7 +270,7 @@ void Rbm::train(const arma::mat& batch, IListener* pListener)
|
||||
}
|
||||
}
|
||||
|
||||
arma::mat Rbm::probsLogistic(const arma::mat &src)
|
||||
arma::mat Rbm::prob(const arma::mat &src)
|
||||
{
|
||||
return 1 / (1 + (arma::exp(-src)));
|
||||
}
|
||||
@@ -290,24 +290,24 @@ arma::mat Rbm::sample(const arma::mat &src)
|
||||
return dst;
|
||||
}
|
||||
|
||||
arma::mat Rbm::toHiddenState(const arma::mat &visible) const
|
||||
arma::mat Rbm::v_to_h(const arma::mat &visible) const
|
||||
{
|
||||
return visible * m_whv + arma::repmat(m_bh, visible.n_rows, 1);
|
||||
}
|
||||
|
||||
arma::mat Rbm::toVisibleState(const arma::mat &hidden) const
|
||||
arma::mat Rbm::h_to_v(const arma::mat &hidden) const
|
||||
{
|
||||
return hidden * m_whv.t() + arma::repmat(m_bv, hidden.n_rows, 1);
|
||||
}
|
||||
|
||||
arma::mat Rbm::toHiddenProbs(const arma::mat &visible) const
|
||||
arma::mat Rbm::c_to_h(const arma::mat &context) const
|
||||
{
|
||||
return probsLogistic(toHiddenState(visible));
|
||||
return context * m_whc + arma::repmat(m_bh, context.n_rows, 1);
|
||||
}
|
||||
|
||||
arma::mat Rbm::toVisibleProbs(const arma::mat &hidden) const
|
||||
arma::mat Rbm::h_to_c(const arma::mat &hidden) const
|
||||
{
|
||||
return probsLogistic(toVisibleState(hidden));
|
||||
return hidden * m_whc.t() + arma::repmat(m_bc, hidden.n_rows, 1);
|
||||
}
|
||||
|
||||
arma::mat Rbm::normalize(const arma::mat& src)
|
||||
|
||||
Reference in New Issue
Block a user