- refactored Rbm
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@750 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+38
-39
@@ -71,54 +71,54 @@ Json::Value Rbm::toJson() const
|
|||||||
return rbm;
|
return rbm;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rbm::weightUpdate(arma::mat const &v_state, 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 hid_probs = toHiddenProbs(v_state);
|
arma::mat h_probs = toHiddenProbs(v_states);
|
||||||
arma::mat vis_probs(dbv.n_rows, dbv.n_cols);
|
arma::mat v_probs(dbv.n_rows, dbv.n_cols);
|
||||||
|
|
||||||
arma::mat h_state = hid_probs;
|
arma::mat h_states = h_probs;
|
||||||
|
|
||||||
// Sample hidden
|
// Sample hidden
|
||||||
if (!m_params.doRaoBlackwell)
|
if (!m_params.doRaoBlackwell)
|
||||||
{
|
{
|
||||||
h_state = sample(hid_probs);
|
h_states = sample(h_probs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update weights (positive phase)
|
// Update weights (positive phase)
|
||||||
dw = v_state.t() * h_state;
|
dw = v_states.t() * h_states;
|
||||||
dbv = sum(v_state, 0);
|
dbv = sum(v_states, 0);
|
||||||
dbh = sum(h_state, 0);
|
dbh = sum(h_states, 0);
|
||||||
|
|
||||||
for (int gibbs=0; gibbs < m_params.numGibbs; gibbs++)
|
for (int gibbs=0; gibbs < m_params.numGibbs; gibbs++)
|
||||||
{
|
{
|
||||||
// Create visible reconstruction (a fantasy...) given hid
|
// Create visible reconstruction (a fantasy...) given hid
|
||||||
if (m_params.gibbsDoSampleHidden)
|
if (m_params.gibbsDoSampleHidden)
|
||||||
{
|
{
|
||||||
vis_probs = toVisibleProbs(sample(hid_probs));
|
v_probs = toVisibleProbs(sample(h_probs));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vis_probs = toVisibleProbs(hid_probs);
|
v_probs = toVisibleProbs(h_probs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create hidden representation given v
|
// Create hidden representation given v
|
||||||
if (m_params.gibbsDoSampleVisible)
|
if (m_params.gibbsDoSampleVisible)
|
||||||
{
|
{
|
||||||
h_state = toHiddenState(sample(vis_probs));
|
h_states = toHiddenState(sample(v_probs));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
h_state = toHiddenState(vis_probs);
|
h_states = toHiddenState(v_probs);
|
||||||
}
|
}
|
||||||
|
|
||||||
hid_probs = probsLogistic(h_state);
|
h_probs = probsLogistic(h_states);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update weights (negative phase)
|
// Update weights (negative phase)
|
||||||
dw -= vis_probs.t() * hid_probs;
|
dw -= v_probs.t() * h_probs;
|
||||||
dbv -= sum(vis_probs, 0);
|
dbv -= sum(v_probs, 0);
|
||||||
dbh -= sum(hid_probs, 0);
|
dbh -= sum(h_probs, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rbm::train(const arma::mat& batch, IListener* pListener)
|
void Rbm::train(const arma::mat& batch, IListener* pListener)
|
||||||
@@ -154,74 +154,73 @@ void Rbm::train(const arma::mat& batch, IListener* pListener)
|
|||||||
double weight_decay = m_params.weightDecay/scaler;
|
double weight_decay = m_params.weightDecay/scaler;
|
||||||
|
|
||||||
#if RBM_TRAIN_FLAT
|
#if RBM_TRAIN_FLAT
|
||||||
arma::mat hid_probs(miniBatchSizeActual, m_bh.n_cols);
|
arma::mat h_probs(miniBatchSizeActual, m_bh.n_cols);
|
||||||
arma::mat vis_probs(miniBatchSizeActual, m_bv.n_cols);
|
arma::mat v_probs(miniBatchSizeActual, m_bv.n_cols);
|
||||||
|
arma::mat hid_states(miniBatchSizeActual, m_bh.n_cols);
|
||||||
#endif
|
#endif
|
||||||
|
arma::mat v_states(miniBatchSizeActual, m_bv.n_cols);
|
||||||
arma::mat hid_state(miniBatchSizeActual, m_bh.n_cols);
|
|
||||||
arma::mat vis_state(miniBatchSizeActual, m_bv.n_cols);
|
|
||||||
|
|
||||||
// Create hidden layer base on training data
|
// Create hidden layer base on training data
|
||||||
if (m_params.doSampleBatch)
|
if (m_params.doSampleBatch)
|
||||||
{
|
{
|
||||||
// When the hidden units are being driven by data, always use stochastic binary states
|
// When the hidden units are being driven by data, always use stochastic binary states
|
||||||
vis_state = sample(miniBatch);
|
v_states = sample(miniBatch);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vis_state = miniBatch;
|
v_states = miniBatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int epoch=0; epoch < m_params.numEpochs; epoch++)
|
for (int epoch=0; epoch < m_params.numEpochs; epoch++)
|
||||||
{
|
{
|
||||||
#if RBM_TRAIN_FLAT
|
#if RBM_TRAIN_FLAT
|
||||||
// Sample hidden
|
// Sample hidden
|
||||||
hid_probs = probsLogistic(toHiddenState(vis_state));
|
h_probs = probsLogistic(toHiddenState(v_states));
|
||||||
if (m_params.doRaoBlackwell)
|
if (m_params.doRaoBlackwell)
|
||||||
{
|
{
|
||||||
hid_state = hid_probs;
|
hid_states = h_probs;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
hid_state = sample(hid_probs);
|
hid_states = sample(h_probs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update weights (positive phase)
|
// Update weights (positive phase)
|
||||||
grad_weight = vis_state.t() * hid_state;
|
grad_weight = v_states.t() * hid_states;
|
||||||
grad_bias_v = sum(vis_state, 0);
|
grad_bias_v = sum(v_states, 0);
|
||||||
grad_bias_h = sum(hid_state, 0);
|
grad_bias_h = sum(hid_states, 0);
|
||||||
|
|
||||||
for (int gibbs=0; gibbs < m_params.numGibbs; gibbs++)
|
for (int gibbs=0; gibbs < m_params.numGibbs; gibbs++)
|
||||||
{
|
{
|
||||||
// Create visible reconstruction (a fantasy...) given hid
|
// Create visible reconstruction (a fantasy...) given hid
|
||||||
if (m_params.gibbsDoSampleHidden)
|
if (m_params.gibbsDoSampleHidden)
|
||||||
{
|
{
|
||||||
vis_probs = toVisibleProbs(sample(hid_probs));
|
v_probs = toVisibleProbs(sample(h_probs));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vis_probs = toVisibleProbs(hid_probs);
|
v_probs = toVisibleProbs(h_probs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create hidden representation given v
|
// Create hidden representation given v
|
||||||
if (m_params.gibbsDoSampleVisible)
|
if (m_params.gibbsDoSampleVisible)
|
||||||
{
|
{
|
||||||
hid_state = toHiddenState(sample(vis_probs));
|
hid_states = toHiddenState(sample(v_probs));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
hid_state = toHiddenState(vis_probs);
|
hid_states = toHiddenState(v_probs);
|
||||||
}
|
}
|
||||||
hid_probs = probsLogistic(hid_state);
|
h_probs = probsLogistic(hid_states);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update weights (negative phase)
|
// Update weights (negative phase)
|
||||||
grad_weight -= vis_probs.t() * hid_probs;
|
grad_weight -= v_probs.t() * h_probs;
|
||||||
grad_bias_v -= sum(vis_probs, 0);
|
grad_bias_v -= sum(v_probs, 0);
|
||||||
grad_bias_h -= sum(hid_probs, 0);
|
grad_bias_h -= sum(h_probs, 0);
|
||||||
#else
|
#else
|
||||||
weightUpdate(vis_state, grad_weight, grad_bias_h, grad_bias_v);
|
weightUpdate(v_states, grad_weight, grad_bias_h, grad_bias_v);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
penalty_weights = weight_decay*arma::sign(m_whv);
|
penalty_weights = weight_decay*arma::sign(m_whv);
|
||||||
@@ -244,7 +243,7 @@ void Rbm::train(const arma::mat& batch, IListener* pListener)
|
|||||||
lastProgress = status.progress;
|
lastProgress = status.progress;
|
||||||
|
|
||||||
// Calculate error
|
// Calculate error
|
||||||
arma::mat diffErr = miniBatch - toVisibleProbs(toHiddenProbs(vis_state));
|
arma::mat diffErr = miniBatch - toVisibleProbs(toHiddenProbs(v_states));
|
||||||
arma::mat diffErr_squared = diffErr % diffErr;
|
arma::mat diffErr_squared = diffErr % diffErr;
|
||||||
status.err = accu(diffErr_squared)/diffErr_squared.n_elem;
|
status.err = accu(diffErr_squared)/diffErr_squared.n_elem;
|
||||||
if (pListener)
|
if (pListener)
|
||||||
|
|||||||
Reference in New Issue
Block a user