- improved RBM
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@567 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+43
-22
@@ -21,9 +21,9 @@ Rbm::Rbm(const Params& params, arma::mat &w, arma::mat &bv, arma::mat &bh)
|
||||
, m_bh(bh)
|
||||
{
|
||||
Noise_Init(&m_noise, 0x32727155);
|
||||
uniform(m_w, 0.0, 0.1);
|
||||
uniform(m_bh, 0.0, 0.1);
|
||||
uniform(m_bv, 0.0, 0.1);
|
||||
uniform(m_w, 0.0, m_params.m_weightInit);
|
||||
uniform(m_bh, 0.0, m_params.m_weightInit);
|
||||
uniform(m_bv, 0.0, m_params.m_weightInit);
|
||||
}
|
||||
|
||||
Rbm::Rbm(const Rbm& orig)
|
||||
@@ -90,7 +90,7 @@ void Rbm::train(const arma::mat& batch, size_t numEpochs, size_t miniBatchSize,
|
||||
vis_state = miniBatch;
|
||||
}
|
||||
|
||||
hid_state = vis_state * m_w + arma::repmat(m_bh, miniBatchSizeActual, 1);
|
||||
hid_state = toHiddenState(vis_state);
|
||||
hid_probs = probsLogistic(hid_state);
|
||||
|
||||
// Sample hidden
|
||||
@@ -110,32 +110,33 @@ void Rbm::train(const arma::mat& batch, size_t numEpochs, size_t miniBatchSize,
|
||||
|
||||
for (gibbs=0; gibbs < m_params.m_numGibbs; gibbs++)
|
||||
{
|
||||
|
||||
// Create hidden representation given v
|
||||
hid_state = sample(hid_probs);
|
||||
|
||||
// Create visible reconstruction (a fantasy...) given hid
|
||||
vis_state = hid_state * m_w.t() + arma::repmat(m_bv, miniBatchSizeActual, 1);
|
||||
vis_probs = probsLogistic(vis_state);
|
||||
if (m_params.m_doSampleVisible)
|
||||
if (m_params.m_gibbsDoSampleHidden)
|
||||
{
|
||||
vis_state = sample(vis_probs);
|
||||
vis_probs = toVisibleProbs(sample(hid_probs));
|
||||
}
|
||||
else
|
||||
{
|
||||
vis_state = vis_probs;
|
||||
vis_probs = toVisibleProbs(hid_probs);
|
||||
}
|
||||
|
||||
// Create hidden representation given v
|
||||
hid_state = vis_state * m_w + repmat(m_bh, miniBatchSizeActual, 1);
|
||||
if (m_params.m_gibbsDoSampleVisible)
|
||||
{
|
||||
hid_state = toHiddenState(sample(vis_probs));
|
||||
}
|
||||
else
|
||||
{
|
||||
hid_state = toHiddenState(vis_probs);
|
||||
}
|
||||
hid_probs = probsLogistic(hid_state);
|
||||
|
||||
}
|
||||
|
||||
// Update weights (negative phase)
|
||||
grad_weight -= vis_probs.t() * hid_probs;
|
||||
grad_bias_v -= sum(vis_probs, 0);
|
||||
grad_bias_h -= sum(hid_probs, 0);
|
||||
grad_weight -= vis_probs.t() * hid_probs;
|
||||
|
||||
penalty_weights = weight_decay*arma::sign(m_w);
|
||||
|
||||
@@ -157,7 +158,8 @@ void Rbm::train(const arma::mat& batch, size_t numEpochs, size_t miniBatchSize,
|
||||
arma::mat diffErr = miniBatch - vis_probs;
|
||||
arma::mat diffErr_squared = diffErr % diffErr;
|
||||
status.err = accu(diffErr_squared)/diffErr_squared.n_elem;
|
||||
|
||||
status.err_total = 0;
|
||||
|
||||
if (pListener)
|
||||
{
|
||||
if(!pListener->onProgress(status))
|
||||
@@ -167,6 +169,17 @@ void Rbm::train(const arma::mat& batch, size_t numEpochs, size_t miniBatchSize,
|
||||
}
|
||||
|
||||
} // number of mini batches
|
||||
|
||||
arma::mat hid_probs = toHiddenProbs(batch);
|
||||
arma::mat vis_probs = toVisibleProbs(hid_probs);
|
||||
arma::mat diffErr = batch - vis_probs;
|
||||
arma::mat diffErr_squared = diffErr % diffErr;
|
||||
status.err_total = accu(diffErr_squared)/diffErr_squared.n_elem;
|
||||
|
||||
if (pListener)
|
||||
{
|
||||
pListener->onProgress(status);
|
||||
}
|
||||
}
|
||||
|
||||
arma::mat Rbm::probsLogistic(const arma::mat &src)
|
||||
@@ -187,16 +200,24 @@ arma::mat Rbm::sample(const arma::mat &src)
|
||||
return dst;
|
||||
}
|
||||
|
||||
arma::mat Rbm::toHidden(const arma::mat &visible)
|
||||
arma::mat Rbm::toHiddenState(const arma::mat &visible)
|
||||
{
|
||||
arma::mat h = visible * m_w + arma::repmat(m_bh, visible.n_rows, 1);
|
||||
return probsLogistic(h);
|
||||
return visible * m_w + arma::repmat(m_bh, visible.n_rows, 1);
|
||||
}
|
||||
|
||||
arma::mat Rbm::toVisible(const arma::mat &hidden)
|
||||
arma::mat Rbm::toVisibleState(const arma::mat &hidden)
|
||||
{
|
||||
arma::mat v = hidden * m_w.t() + arma::repmat(m_bv, hidden.n_rows, 1);
|
||||
return probsLogistic(v);
|
||||
return hidden * m_w.t() + arma::repmat(m_bv, hidden.n_rows, 1);
|
||||
}
|
||||
|
||||
arma::mat Rbm::toHiddenProbs(const arma::mat &visible)
|
||||
{
|
||||
return probsLogistic(toHiddenState(visible));
|
||||
}
|
||||
|
||||
arma::mat Rbm::toVisibleProbs(const arma::mat &hidden)
|
||||
{
|
||||
return probsLogistic(toVisibleState(hidden));
|
||||
}
|
||||
|
||||
void Rbm::uniform(arma::mat& srcDst, double mu, double stdDev)
|
||||
|
||||
Reference in New Issue
Block a user