- refactored and cleaned up

git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@290 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2016-06-15 20:32:28 +00:00
parent 3d56e4a163
commit 2de8e9ac48
7 changed files with 277 additions and 277 deletions
+128 -135
View File
@@ -11,6 +11,7 @@
#include "VisibleLayer.hpp"
#include "HiddenLayer.hpp"
#include "Weights.hpp"
#include "LayerArray.hpp"
#include <cmath>
#include <Eigen/Dense>
@@ -37,11 +38,11 @@ public:
class Rbm
{
public:
Rbm(Weights &weights, RbmListener *pListener = nullptr)
Rbm(Weights &weights, const LayerArray &batch, RbmListener *pListener = nullptr)
: m_w(weights)
, m_v(weights.getNumVisible())
, m_h(weights.getNumHidden())
, m_sigmas(weights.getNumVisible())
, m_batch(batch)
, m_variableSigma(weights.getNumVisible())
, m_constantSigma(1.0)
, m_pListener(pListener)
, m_progress(0)
, m_sigmaDecay(1.0)
@@ -51,7 +52,6 @@ public:
, m_muWeights(0.01)
, m_muSparsity(0.01)
, m_momentum(0.5)
, m_doCancel(false)
, m_useVisibleGaussian(false)
, m_doRaoBlackwell(false)
, m_useProbsForHiddenReconstruction(false)
@@ -76,10 +76,7 @@ public:
~Rbm()
{
cancel();
Noise_Free(&m_noise);
m_v.resize(0);
m_h.resize(0);
}
void sample(MatrixXd &src)
@@ -222,20 +219,19 @@ public:
return t1;
}
void train(const LayerArray &vt, uint32_t numEpochs, double sigmaMin = 0.05)
void train(uint32_t numEpochs, double sigmaMin = 0.05)
{
uint32_t t, i;
uint32_t epoch;
uint32_t gibbs;
double dProgress = 1.0/numEpochs;
double kTrain = 1.0/vt.getSize();
double kTrain = 1.0/m_batch.getSize();
size_t batchSize = vt.getSize();
size_t batchSize = m_batch.getSize();
MatrixXd v(batchSize, m_w.getNumVisible());
MatrixXd h(batchSize, m_w.getNumHidden());
MatrixXd batch(batchSize, m_w.getNumVisible());
MatrixXd sumBiasV(1, m_w.getNumVisible());
MatrixXd sumBiasH(1, m_w.getNumHidden());
MatrixXd sumWeights(m_w.getNumVisible(), m_w.getNumHidden());
@@ -248,14 +244,8 @@ public:
MatrixXd diffErr(batchSize, m_w.getNumVisible());
m_progress = 0;
m_doCancel = false;
batch = vt.data();
if (m_doLearnVariance)
{
m_sigmas = calcSigma(batch);
}
MatrixXd batch = m_batch.data();
if (m_doNormalizeData)
{
@@ -263,7 +253,7 @@ public:
for (i=0; i < batchSize; i++)
{
RowVectorXd x = batch.row(i);
batch.row(i) = normalizeData(x, mean, m_sigmas);
batch.row(i) = normalizeData(x, mean, m_variableSigma);
}
}
@@ -271,16 +261,8 @@ public:
{
double err;
v = batch;
if (m_doCancel)
{
m_doCancel = false;
break;
}
// Create hidden layer base on training data
h = v * m_w.weights();
h += m_w.hiddenBias().replicate(batchSize, 1);
toHiddenBatch(h, batch);
probsLogistic(h);
if (!m_doRaoBlackwell)
@@ -289,41 +271,38 @@ public:
}
// Update weights (positive phase)
sumBiasV = v.colwise().sum();
sumBiasV = batch.colwise().sum();
if (!m_doSparse)
{
sumBiasH = h.colwise().sum();
}
sumWeights = v.transpose() * h;
diffErr = v;
sumWeights = batch.transpose() * h;
diffErr = batch;
for (gibbs=0; gibbs < m_numGibbs; gibbs++)
{
sample(h);
// Create visible reconstruction (a fantasy...)
v = h * m_w.weights().transpose();
v += m_w.visibleBias().replicate(batchSize, 1);
// Create visible reconstruction (a fantasy...) given h
toVisibleBatch(v, h);
if (m_useVisibleGaussian)
{
if (!m_useProbsForHiddenReconstruction)
{
sampleGaussian(v, m_sigmas.replicate(batchSize, 1));
sampleGaussian(v, m_variableSigma.replicate(batchSize, 1));
}
}
else
{
probsLogistic(v, m_sigmas.replicate(batchSize, 1));
probsLogistic(v, m_variableSigma.replicate(batchSize, 1));
if (!m_useProbsForHiddenReconstruction)
{
sample(v);
}
}
// Create hidden reconstruction
h = v * m_w.weights();
h += m_w.hiddenBias().replicate(batchSize, 1);
// Create hidden reconstruction given v
toHiddenBatch(h, v);
probsLogistic(h);
}
@@ -366,9 +345,9 @@ public:
}
m_w.hiddenBias() += deltaBiasH;
if (m_sigmas[0] > sigmaMin)
if (m_variableSigma[0] > sigmaMin)
{
m_sigmas.array() *= m_sigmaDecay;
m_variableSigma.array() *= m_sigmaDecay;
}
m_progress += dProgress;
@@ -394,7 +373,7 @@ public:
double getEnergy(const VectorXd& visible, const VectorXd& hidden)
{
double energy;
double sigma = m_sigmas.array().mean();
double sigma = m_variableSigma.array().mean();
energy = m_w.visibleBias() * visible;
energy += m_w.hiddenBias() * hidden;
@@ -403,19 +382,17 @@ public:
return -energy/(sigma*sigma);
}
RowVectorXd const & toHidden(const RowVectorXd& v)
void toHidden(RowVectorXd &h, RowVectorXd const &v)
{
m_h = v * m_w.weights();
m_h += m_w.hiddenBias();
probsLogistic(m_h);
return m_h;
h = v * m_w.weights();
h += m_w.hiddenBias();
probsLogistic(h);
}
RowVectorXd const & toVisible(const RowVectorXd& h)
void toVisible(RowVectorXd &v, RowVectorXd const &h)
{
m_v = h * m_w.weights().transpose();
m_v += m_w.visibleBias();
v = h * m_w.weights().transpose();
v += m_w.visibleBias();
if (m_useVisibleGaussian)
{
@@ -423,108 +400,114 @@ public:
}
else
{
probsLogistic(m_v, m_sigmas);
probsLogistic(v, m_variableSigma);
}
return m_v;
}
void setSigma(double value)
{
m_sigmas.fill(value);
}
void setConstantSigma(double value)
{
m_constantSigma = value;
m_variableSigma.fill(m_constantSigma);
}
RowVectorXd& getSigma()
{
return m_sigmas;
}
double getConstantSigma()
{
return m_constantSigma;
}
void setSigmaDecay(double value)
{
m_sigmaDecay = value;
}
RowVectorXd& getVariableSigma()
{
return m_variableSigma;
}
void setWeightDecay(double value)
{
m_weightDecay = value;
}
void setSigmaDecay(double value)
{
m_sigmaDecay = value;
}
void setLambda(double value)
{
m_lambda = value;
}
void setWeightDecay(double value)
{
m_weightDecay = value;
}
void setSparsity(double value)
{
m_sparsity = value;
}
void setLambda(double value)
{
m_lambda = value;
}
void setUseVisibleGaussian(bool flag)
{
m_useVisibleGaussian = flag;
}
void setSparsity(double value)
{
m_sparsity = value;
}
void setDoRaoBlackwell(bool flag)
{
m_doRaoBlackwell = flag;
}
void setUseVisibleGaussian(bool flag)
{
m_useVisibleGaussian = flag;
}
void setUseProbsForHiddenReconstruction(bool flag)
{
m_useProbsForHiddenReconstruction = flag;
}
void setDoRaoBlackwell(bool flag)
{
m_doRaoBlackwell = flag;
}
void setDoSparse(bool flag)
{
m_doSparse = flag;
}
void setUseProbsForHiddenReconstruction(bool flag)
{
m_useProbsForHiddenReconstruction = flag;
}
void setNormalizeData(bool flag)
{
m_doNormalizeData = flag;
}
void setDoSparse(bool flag)
{
m_doSparse = flag;
}
void setDoLearnVariance(bool flag)
{
m_doLearnVariance = flag;
}
void setNormalizeData(bool flag)
{
m_doNormalizeData = flag;
}
void setNumGibbs(uint32_t value)
{
m_numGibbs = value;
}
void setDoLearnVariance(bool flag)
{
m_doLearnVariance = flag;
if (m_doLearnVariance)
{
m_variableSigma = calcSigma(m_batch.data());
}
else
{
m_variableSigma.fill(m_constantSigma);
}
}
uint32_t getNumGibbs()
{
return m_numGibbs;
}
void setMuWeights(double value)
{
m_muWeights = value;
}
void setNumGibbs(uint32_t value)
{
m_numGibbs = value;
}
void setMuSparsity(double value)
{
m_muSparsity = value;
}
uint32_t getNumGibbs()
{
return m_numGibbs;
}
void setMomentum(double value)
{
m_momentum = value;
}
void setMuWeights(double value)
{
m_muWeights = value;
}
void cancel()
{
m_doCancel = true;
// while(m_doCancel);
}
void setMuSparsity(double value)
{
m_muSparsity = value;
}
void setMomentum(double value)
{
m_momentum = value;
}
private:
Weights &m_w;
RowVectorXd m_h;
RowVectorXd m_v;
RowVectorXd m_sigmas;
LayerArray const &m_batch;
RowVectorXd m_variableSigma;
double m_constantSigma;
RbmListener *m_pListener;
noise_gen_t m_noise;
double m_progress;
@@ -541,9 +524,19 @@ private:
bool m_doSparse;
bool m_doNormalizeData;
bool m_doLearnVariance;
volatile bool m_doCancel;
uint32_t m_numGibbs;
void toHiddenBatch(MatrixXd &h, MatrixXd const &v)
{
h = v * m_w.weights();
h += m_w.hiddenBias().replicate(m_batch.getSize(), 1);
}
void toVisibleBatch(MatrixXd &v, MatrixXd const &h)
{
v = h * m_w.weights().transpose();
v += m_w.visibleBias().replicate(m_batch.getSize(), 1);
}
};