From 82d93ca7b5bc6450feae4725f4755a96be4ee7c7 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sun, 5 Jun 2016 22:04:13 +0000 Subject: [PATCH] more stable git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@286 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- Source/DrawComponent.cpp | 1 + Source/MainComponent.cpp | 80 +++++++++++++++++++++------------------- Source/MainComponent.h | 4 +- Source/Rbm.hpp | 33 ++++++++++------- Source/Weights.hpp | 3 +- 5 files changed, 66 insertions(+), 55 deletions(-) diff --git a/Source/DrawComponent.cpp b/Source/DrawComponent.cpp index fbf71db..27d545e 100644 --- a/Source/DrawComponent.cpp +++ b/Source/DrawComponent.cpp @@ -64,6 +64,7 @@ DrawComponent::~DrawComponent() //[Destructor]. You can add your own custom destruction code here.. m_pG = nullptr; m_pListener = nullptr; + m_data.resize(0); //[/Destructor] } diff --git a/Source/MainComponent.cpp b/Source/MainComponent.cpp index 913e73f..83f5f34 100644 --- a/Source/MainComponent.cpp +++ b/Source/MainComponent.cpp @@ -96,7 +96,7 @@ MainComponent::MainComponent () testButton->addListener (this); addAndMakeVisible (numVisibleLabel = new Label ("Num Visible label", - TRANS("99999"))); + TRANS("16"))); numVisibleLabel->setFont (Font (15.00f, Font::plain)); numVisibleLabel->setJustificationType (Justification::centred); numVisibleLabel->setEditable (true, true, false); @@ -105,7 +105,7 @@ MainComponent::MainComponent () numVisibleLabel->addListener (this); addAndMakeVisible (numHiddenLabel = new Label ("Num Hidden label", - TRANS("99999"))); + TRANS("64"))); numHiddenLabel->setFont (Font (15.00f, Font::plain)); numHiddenLabel->setJustificationType (Justification::centred); numHiddenLabel->setEditable (true, true, false); @@ -135,7 +135,7 @@ MainComponent::MainComponent () saveButton->addListener (this); addAndMakeVisible (numVisibleYLabel = new Label ("Num Visible Y label", - TRANS("99999"))); + TRANS("16"))); numVisibleYLabel->setFont (Font (15.00f, Font::plain)); numVisibleYLabel->setJustificationType (Justification::centred); numVisibleYLabel->setEditable (true, true, false); @@ -273,14 +273,8 @@ MainComponent::MainComponent () //[UserPreSize] - m_vNumX = 16; - m_vNumY = 16; - m_hNum = 64; m_numGibbs = 1; - m_weights.setUnits(m_vNumX, m_vNumY, m_hNum); - m_pRbm = new Rbm(m_weights, this); create(); - //[/UserPreSize] setSize (800, 600); @@ -494,7 +488,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) else if (buttonThatWasClicked == ShakeButton) { //[UserButtonCode_ShakeButton] -- add your button handler code here.. - m_weights.shuffle(weightInitLabel->getText().getFloatValue()); + m_weights->shuffle(weightInitLabel->getText().getFloatValue()); redrawReconstruction(); redrawWeights((int)WeightsSlider->getValue()); //[/UserButtonCode_ShakeButton] @@ -509,11 +503,6 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) else if (buttonThatWasClicked == createButton) { //[UserButtonCode_createButton] -- add your button handler code here.. - m_vNumX = numVisibleLabel->getText().getIntValue(); - m_vNumY = numVisibleYLabel->getText().getIntValue(); - m_hNum = numHiddenLabel->getText().getIntValue(); - - m_weights.setUnits(m_vNumX, m_vNumY, m_hNum); create(); //[/UserButtonCode_createButton] } @@ -521,8 +510,6 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) { //[UserButtonCode_loadButton] -- add your button handler code here.. load(); - redrawReconstruction(); - redrawWeights((int)WeightsSlider->getValue()); //[/UserButtonCode_loadButton] } else if (buttonThatWasClicked == saveButton) @@ -536,7 +523,6 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) //[UserButtonCode_loadTrainingButton] -- add your button handler code here.. m_layers.clear(); m_layers.load((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); - redrawReconstruction(); //[/UserButtonCode_loadTrainingButton] } else if (buttonThatWasClicked == saveTrainingButton) @@ -799,26 +785,35 @@ void MainComponent::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails //[MiscUserCode] You can add your own definitions of your custom methods or any other code here... void MainComponent::load () { - m_weights.load((String(getBaseDir() + String(".weights.dat"))).toUTF8()); - - m_vNumX = m_weights.getNumVisibleX(); - m_vNumY = m_weights.getNumVisibleY(); - m_hNum = m_weights.getNumHidden(); - create(); + create((String(getBaseDir() + String(".weights.dat"))).toUTF8()); } void MainComponent::save () { - m_weights.save((String(getBaseDir() + String(".weights.dat"))).toUTF8()); + m_weights->save((String(getBaseDir() + String(".weights.dat"))).toUTF8()); } -void MainComponent::create() +void MainComponent::create(const char *pFilename) { + m_weights = nullptr; DrawTraining = nullptr; DrawReconstruction = nullptr; DrawWeights = nullptr; DrawVars = nullptr; DrawHidden = nullptr; + m_pRbm = nullptr; + + if (pFilename) + { + m_weights = new Weights(pFilename); + } + else + { + m_weights = new Weights(numVisibleLabel->getText().getIntValue(), numVisibleYLabel->getText().getIntValue(), numHiddenLabel->getText().getIntValue()); + } + m_vNumX = m_weights->getNumVisibleX(); + m_vNumY = m_weights->getNumVisibleY(); + m_hNum = m_weights->getNumHidden(); WeightsSlider->setRange(0, m_hNum-1, 1); @@ -833,6 +828,8 @@ void MainComponent::create() numVisibleYLabel->setText(String(m_vNumY), dontSendNotification ); numHiddenLabel->setText(String(m_hNum), dontSendNotification ); + m_pRbm = new Rbm(*m_weights, this); + m_pRbm->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState()); m_pRbm->setUseProbsForHiddenReconstruction(rbmReduceVarianceToggleButton->getToggleState()); m_pRbm->setUseVisibleGaussian(rbmUseVisibleGaussianToggleButton->getToggleState()); @@ -845,8 +842,10 @@ void MainComponent::create() m_pRbm->setSparsity(sparsityLabel->getText().getFloatValue()); m_pRbm->setNumGibbs((uint32_t)numGibbsSlider->getValue()); - resized(); + + redrawReconstruction(); + redrawWeights((int)WeightsSlider->getValue()); } void MainComponent::destroy() @@ -882,25 +881,30 @@ void MainComponent::onDraw(DrawComponent &obj) } if (&obj == DrawTraining) { -// DrawReconstruction->setData(m_pRbm->toVisible(m_pRbm->toHidden(obj.getData()))); - // DrawHidden->setData(m_pRbm->toHidden(obj.getData())); redrawReconstruction(); } } void MainComponent::redrawReconstruction() { - char table[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - VectorXd v; - MatrixXd m; - - DrawHidden->setData(m_pRbm->toHidden(DrawTraining->getData())); - v = m_pRbm->toVisible(DrawHidden->getData()); + RowVectorXd h = m_pRbm->toHidden(DrawTraining->getData()); + DrawHidden->setData(h); + + RowVectorXd v = m_pRbm->toVisible(DrawHidden->getData()); DrawReconstruction->setData(v); + +// RowVectorXd dataV(m_weights->getNumVisible()); +// DrawHidden->setData(m_pRbm->toHidden(dataV)); + +// RowVectorXd dataH(m_weights->getNumHidden()); +// VectorXd v = m_pRbm->toVisible(dataH); +// DrawReconstruction->setData(v); + if ((m_vNumX == 27) && (m_vNumY == 4)) { - m = v; + char table[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + MatrixXd m = m_pRbm->toVisible(DrawHidden->getData()); m.resize(m_vNumX, m_vNumY); cout << "Reconstruction" << endl; cout << m << endl; @@ -927,9 +931,9 @@ void MainComponent::redrawReconstruction() void MainComponent::redrawWeights(int index) { - VectorXd w = m_weights.weights().col(index); + VectorXd w = m_weights->weights().col(index); DrawWeights->setData(w); - DrawVars->setData(m_weights.sigma()); + DrawVars->setData(m_weights->sigma()); } diff --git a/Source/MainComponent.h b/Source/MainComponent.h index 45d6b01..e775590 100644 --- a/Source/MainComponent.h +++ b/Source/MainComponent.h @@ -73,7 +73,7 @@ public: private: //[UserVariables] -- You can add your own custom variables in this section. - Weights m_weights; + ScopedPointer m_weights; ScopedPointer m_pRbm; ScopedPointer DrawTraining; ScopedPointer DrawReconstruction; @@ -87,7 +87,7 @@ private: MatrixXd m_trainingData; void load(); void save(); - void create(); + void create(const char *pFilename=nullptr); void destroy(); const juce::String& getBaseDir(); void onChanged(const LayerArray &obj); diff --git a/Source/Rbm.hpp b/Source/Rbm.hpp index c9627c3..a1d5c6d 100644 --- a/Source/Rbm.hpp +++ b/Source/Rbm.hpp @@ -39,6 +39,8 @@ class Rbm public: Rbm(Weights &weights, RbmListener *pListener = nullptr) : m_w(weights) + , m_v(weights.getNumVisible()) + , m_h(weights.getNumHidden()) , m_pListener(pListener) , m_progress(0) , m_sigma(1.0) @@ -60,6 +62,7 @@ public: { Noise_Init(&m_noise, 0x32727155); +#if 1 VectorXd a(4); a << 1, 2, 3, 4; VectorXd b(4); @@ -67,12 +70,16 @@ public: b.array() = -a.array().exp(); cout << b << endl; + +#endif } ~Rbm() { cancel(); Noise_Free(&m_noise); + m_v.resize(0); + m_h.resize(0); } void sample(MatrixXd &src) @@ -396,23 +403,19 @@ public: return -energy/(m_sigma*m_sigma); } - RowVectorXd toHidden(const RowVectorXd& v) + RowVectorXd const & toHidden(const RowVectorXd& v) { - RowVectorXd h(m_w.getNumHidden()); - RowVectorXd vn(m_w.getNumVisible()); + m_h = v * m_w.weights(); + m_h += m_w.hiddenBias(); + probsLogistic(m_h); - h = v * m_w.weights(); - h += m_w.hiddenBias(); - probsLogistic(h); - - return h; + return m_h; } - RowVectorXd toVisible(const RowVectorXd& h) + RowVectorXd const & toVisible(const RowVectorXd& h) { - RowVectorXd v(m_w.getNumVisible()); - v = h * m_w.weights().transpose(); - v += m_w.visibleBias(); + m_v = h * m_w.weights().transpose(); + m_v += m_w.visibleBias(); if (m_useVisibleGaussian) { @@ -420,9 +423,9 @@ public: } else { - probsLogistic(v, m_w.sigma()); + probsLogistic(m_v, m_w.sigma()); } - return v; + return m_v; } void setSigma(double value) @@ -512,6 +515,8 @@ public: private: Weights &m_w; + RowVectorXd m_h; + RowVectorXd m_v; RbmListener *m_pListener; noise_gen_t m_noise; double m_progress; diff --git a/Source/Weights.hpp b/Source/Weights.hpp index f3e916e..3c2a112 100644 --- a/Source/Weights.hpp +++ b/Source/Weights.hpp @@ -70,7 +70,6 @@ public: void setUnits(uint32_t numVisibleX, uint32_t numVisibleY, uint32_t numHidden) { - shuffle(0); if ((m_numVisibleX == numVisibleX) && (m_numVisibleY == numVisibleY) && (m_numHidden == numHidden)) { return; @@ -85,6 +84,7 @@ public: m_mean.resize(m_numVisible); m_bv.resize(m_numVisible); m_bh.resize(m_numHidden); + shuffle(1.0); } void shuffle(double stdDev) @@ -299,6 +299,7 @@ private: void free() { + setUnits(0, 0, 0); } };