- refactored in gibbs
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@753 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+38
-26
@@ -71,10 +71,46 @@ Json::Value Rbm::toJson() const
|
|||||||
return rbm;
|
return rbm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Rbm::weightUpdate(arma::mat const &v_states, arma::mat const &c_states, arma::mat &dwhv, arma::mat &dwhc, arma::mat &dbh, arma::mat &dbv, arma::mat &dbc)
|
||||||
|
{
|
||||||
|
arma::mat hv_states = v_to_h(v_states);
|
||||||
|
arma::mat hc_states = c_to_h(c_states);
|
||||||
|
|
||||||
|
arma::mat h_states = hv_states + hc_states;
|
||||||
|
arma::mat h_probs = prob(h_states);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Rbm::gibbs(arma::mat &h_probs, arma::mat &v_probs)
|
||||||
|
{
|
||||||
|
for (int gibbs=0; gibbs < m_params.numGibbs; gibbs++)
|
||||||
|
{
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Rbm::weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, arma::mat &dbv)
|
void Rbm::weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, arma::mat &dbv)
|
||||||
{
|
{
|
||||||
arma::mat v_probs(dbv.n_rows, dbv.n_cols);
|
arma::mat v_probs(dbv.n_rows, dbv.n_cols);
|
||||||
|
|
||||||
arma::mat h_states = v_to_h(v_states);
|
arma::mat h_states = v_to_h(v_states);
|
||||||
arma::mat h_probs = prob(h_states);
|
arma::mat h_probs = prob(h_states);
|
||||||
|
|
||||||
@@ -93,31 +129,7 @@ void Rbm::weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh,
|
|||||||
dbv = sum(v_states, 0);
|
dbv = sum(v_states, 0);
|
||||||
dbh = sum(h_states, 0);
|
dbh = sum(h_states, 0);
|
||||||
|
|
||||||
for (int gibbs=0; gibbs < m_params.numGibbs; gibbs++)
|
gibbs(h_probs, 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create hidden representation given v
|
|
||||||
if (m_params.gibbsDoSampleVisible)
|
|
||||||
{
|
|
||||||
h_states = v_to_h(sample(v_probs));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
h_states = v_to_h(v_probs);
|
|
||||||
}
|
|
||||||
|
|
||||||
h_probs = prob(h_states);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update weights (negative phase)
|
// Update weights (negative phase)
|
||||||
dw -= v_probs.t() * h_probs;
|
dw -= v_probs.t() * h_probs;
|
||||||
|
|||||||
+3
-2
@@ -142,9 +142,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
Params m_params;
|
Params m_params;
|
||||||
arma::mat sample(arma::mat const &src);
|
arma::mat sample(arma::mat const &src);
|
||||||
void weightUpdate(arma::mat const &v_state, arma::mat &dw, arma::mat &dbh, arma::mat &dbv);
|
void weightUpdate(arma::mat const &v_states, arma::mat &dw, arma::mat &dbh, arma::mat &dbv);
|
||||||
|
void weightUpdate(arma::mat const &v_states, arma::mat const &c_states, arma::mat &dwhv, arma::mat &dwhc, arma::mat &dbh, arma::mat &dbv, arma::mat &dbc);
|
||||||
void uniform(arma::mat &srcDst, double stdDev=1.0, double mu=0.5);
|
void uniform(arma::mat &srcDst, double stdDev=1.0, double mu=0.5);
|
||||||
|
void gibbs(arma::mat &h_states, arma::mat &v_states);
|
||||||
protected:
|
protected:
|
||||||
arma::mat m_whv;
|
arma::mat m_whv;
|
||||||
arma::mat m_whc;
|
arma::mat m_whc;
|
||||||
|
|||||||
Reference in New Issue
Block a user