- moved sigma and mean from WEIGHTS to RBM
- use Gibbs slider also for reconstruction draw


git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@287 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2016-06-13 18:05:17 +00:00
parent 82d93ca7b5
commit 80135d0498
4 changed files with 59 additions and 95 deletions
+23 -21
View File
@@ -41,9 +41,9 @@ public:
: m_w(weights)
, m_v(weights.getNumVisible())
, m_h(weights.getNumHidden())
, m_sigmas(weights.getNumVisible())
, m_pListener(pListener)
, m_progress(0)
, m_sigma(1.0)
, m_sigmaDecay(1.0)
, m_weightDecay(0.0)
, m_lambda(1.0)
@@ -222,7 +222,7 @@ public:
return t1;
}
void train(const LayerArray &vt, uint32_t numEpochs, uint32_t batchSize, double sigmaMin = 0.05)
void train(const LayerArray &vt, uint32_t numEpochs, double sigmaMin = 0.05)
{
uint32_t t, i;
uint32_t epoch;
@@ -230,7 +230,7 @@ public:
double dProgress = 1.0/numEpochs;
double kTrain = 1.0/vt.getSize();
batchSize = vt.getSize();
size_t batchSize = vt.getSize();
MatrixXd v(batchSize, m_w.getNumVisible());
MatrixXd h(batchSize, m_w.getNumHidden());
@@ -252,19 +252,18 @@ public:
batch = vt.data();
m_w.mean() = calcMean(batch);
if (m_doLearnVariance)
{
m_w.sigma() = calcSigma(batch);
m_sigmas = calcSigma(batch);
}
if (m_doNormalizeData)
{
RowVectorXd mean = calcMean(batch);
for (i=0; i < batchSize; i++)
{
RowVectorXd x = batch.row(i);
batch.row(i) = normalizeData(x, m_w.mean(), m_w.sigma());
batch.row(i) = normalizeData(x, mean, m_sigmas);
}
}
@@ -310,12 +309,12 @@ public:
{
if (!m_useProbsForHiddenReconstruction)
{
sampleGaussian(v, m_w.sigma().replicate(batchSize, 1));
sampleGaussian(v, m_sigmas.replicate(batchSize, 1));
}
}
else
{
probsLogistic(v, m_w.sigma().replicate(batchSize, 1));
probsLogistic(v, m_sigmas.replicate(batchSize, 1));
if (!m_useProbsForHiddenReconstruction)
{
sample(v);
@@ -367,9 +366,9 @@ public:
}
m_w.hiddenBias() += deltaBiasH;
if (m_w.sigma()[0] > sigmaMin)
if (m_sigmas[0] > sigmaMin)
{
m_w.sigma().array() *= m_sigmaDecay;
m_sigmas.array() *= m_sigmaDecay;
}
m_progress += dProgress;
@@ -395,12 +394,13 @@ public:
double getEnergy(const VectorXd& visible, const VectorXd& hidden)
{
double energy;
double sigma = m_sigmas.array().mean();
energy = m_w.visibleBias() * visible;
energy += m_w.hiddenBias() * hidden;
energy += visible.transpose() * m_w.weights() * hidden;
return -energy/(m_sigma*m_sigma);
return -energy/(sigma*sigma);
}
RowVectorXd const & toHidden(const RowVectorXd& v)
@@ -419,18 +419,23 @@ public:
if (m_useVisibleGaussian)
{
// probsGaussian(v, m_w.sigma());
// probsGaussian(v, m_sigmas);
}
else
{
probsLogistic(m_v, m_w.sigma());
probsLogistic(m_v, m_sigmas);
}
return m_v;
}
void setSigma(double value)
{
m_sigma = value;
m_sigmas.fill(value);
}
RowVectorXd& getSigma()
{
return m_sigmas;
}
void setSigmaDecay(double value)
@@ -481,10 +486,6 @@ public:
void setDoLearnVariance(bool flag)
{
m_doLearnVariance = flag;
if (!flag)
{
m_w.sigma().fill(m_sigma);
}
}
void setNumGibbs(uint32_t value)
@@ -517,10 +518,11 @@ private:
Weights &m_w;
RowVectorXd m_h;
RowVectorXd m_v;
RowVectorXd m_sigmas;
RbmListener *m_pListener;
noise_gen_t m_noise;
double m_progress;
double m_sigma;
double m_sigmaDecay;
double m_weightDecay;
double m_lambda;