From 93b614486477c3127cccdb2f28ab37b1fd2beb65 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Tue, 14 Jun 2016 18:52:08 +0000 Subject: [PATCH] [RBM] - introduce RbmComponent git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@288 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- Source/MainComponent.cpp | 232 ++++++++-------------------- Source/MainComponent.h | 24 +-- Source/Rbm.hpp | 5 + Source/RbmComponent.cpp | 315 +++++++++++++++++++++++++++++++++++++++ Source/RbmComponent.h | 194 ++++++++++++++++++++++++ 5 files changed, 586 insertions(+), 184 deletions(-) create mode 100644 Source/RbmComponent.cpp create mode 100644 Source/RbmComponent.h diff --git a/Source/MainComponent.cpp b/Source/MainComponent.cpp index bd1e926..9709e9b 100644 --- a/Source/MainComponent.cpp +++ b/Source/MainComponent.cpp @@ -38,12 +38,8 @@ void mylog(const char* format, ...) //============================================================================== MainComponent::MainComponent () : Thread("RBM"), - m_layers(this), - m_pRbm(nullptr), - DrawTraining(nullptr), - DrawReconstruction(nullptr), - DrawWeights(nullptr), - DrawHidden(nullptr) + m_pRbmComponent(nullptr) + { addAndMakeVisible (trainButton = new TextButton ("Train button")); trainButton->setButtonText (TRANS("Train")); @@ -273,7 +269,6 @@ MainComponent::MainComponent () //[UserPreSize] - m_numGibbs = 1; create(); //[/UserPreSize] @@ -330,12 +325,7 @@ MainComponent::~MainComponent() //[Destructor]. You can add your own custom destruction code here.. - DrawTraining = nullptr; - DrawReconstruction = nullptr; - DrawWeights = nullptr; - DrawVars = nullptr; - DrawHidden = nullptr; - m_pRbm = nullptr; + m_pRbmComponent = nullptr; //[/Destructor] } @@ -452,14 +442,19 @@ void MainComponent::resized() rbmLearnVarianceButton->setBounds (336, 200, 128, 24); rbmNormalizeDataToggleButton->setBounds (336, 232, 160, 24); //[UserResized] Add your own custom resize handling here.. - DrawTraining->setBounds (16, 16, 100, 100); - DrawReconstruction->setBounds (110+16, 16, 100, 100); - DrawWeights->setBounds (220+16, 16, 100, 100); - DrawVars->setBounds (330+16, 16, 100, 100); - DrawHidden->setBounds (16, 16+110, 430, 20); //[/UserResized] } +void MainComponent::onLayerSizeChanged(size_t size) +{ + patterSlider->setRange(0, size-1, 1); +} + +void MainComponent::onRbmEpochTrained(size_t progressPercent) +{ + m_progressBarSlider->setValue(progressPercent); +} + void MainComponent::buttonClicked (Button* buttonThatWasClicked) { //[UserbuttonClicked_Pre] @@ -474,30 +469,28 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) else if (buttonThatWasClicked == addButton) { //[UserButtonCode_addButton] -- add your button handler code here.. - DrawReconstruction->setData(DrawTraining->getData()); - m_layers.add(DrawTraining->getData(), m_vNumX*m_vNumY); - patterSlider->setRange (0, m_layers.getSize()-1, 1); + m_pRbmComponent->addFromTraining(); //[/UserButtonCode_addButton] } else if (buttonThatWasClicked == reconstructButton) { //[UserButtonCode_reconstructButton] -- add your button handler code here.. - redrawReconstruction(numGibbsSlider->getValue()); + m_pRbmComponent->redrawReconstruction(); //[/UserButtonCode_reconstructButton] } else if (buttonThatWasClicked == ShakeButton) { //[UserButtonCode_ShakeButton] -- add your button handler code here.. m_weights->shuffle(weightInitLabel->getText().getFloatValue()); - redrawReconstruction(numGibbsSlider->getValue()); - redrawWeights((int)WeightsSlider->getValue()); + m_pRbmComponent->redrawReconstruction(); + m_pRbmComponent->redrawWeights(); //[/UserButtonCode_ShakeButton] } else if (buttonThatWasClicked == testButton) { //[UserButtonCode_testButton] -- add your button handler code here.. - DrawTraining->setData(DrawReconstruction->getData()); - redrawReconstruction(numGibbsSlider->getValue()); + m_pRbmComponent->copyReconstructionToTraining(); + m_pRbmComponent->redrawReconstruction(); //[/UserButtonCode_testButton] } else if (buttonThatWasClicked == createButton) @@ -521,73 +514,73 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) else if (buttonThatWasClicked == loadTrainingButton) { //[UserButtonCode_loadTrainingButton] -- add your button handler code here.. - m_layers.clear(); - m_layers.load((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); + m_pRbmComponent->clearTraining(); + m_pRbmComponent->loadTraining((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); //[/UserButtonCode_loadTrainingButton] } else if (buttonThatWasClicked == saveTrainingButton) { //[UserButtonCode_saveTrainingButton] -- add your button handler code here.. - m_layers.save((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); + m_pRbmComponent->saveTraining((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); //[/UserButtonCode_saveTrainingButton] } else if (buttonThatWasClicked == clearTrainingButton) { //[UserButtonCode_clearTrainingButton] -- add your button handler code here.. - m_layers.clear(); + m_pRbmComponent->clearTraining(); //[/UserButtonCode_clearTrainingButton] } else if (buttonThatWasClicked == removeTrainingButton) { //[UserButtonCode_removeTrainingButton] -- add your button handler code here.. - m_layers.removeAt((int)patterSlider->getValue()); + m_pRbmComponent->removeTrainingAt((int)patterSlider->getValue()); //[/UserButtonCode_removeTrainingButton] } else if (buttonThatWasClicked == reconstructEquButton) { //[UserButtonCode_reconstructEquButton] -- add your button handler code here.. - redrawReconstruction(100); + m_pRbmComponent->redrawReconstruction(); //[/UserButtonCode_reconstructEquButton] } else if (buttonThatWasClicked == rbmDoRaoBlackwellToggleButton) { //[UserButtonCode_rbmDoRaoBlackwellToggleButton] -- add your button handler code here.. - m_pRbm->setDoRaoBlackwell(buttonThatWasClicked->getToggleState()); + m_pRbmComponent->setDoRaoBlackwell(buttonThatWasClicked->getToggleState()); //[/UserButtonCode_rbmDoRaoBlackwellToggleButton] } else if (buttonThatWasClicked == rbmReduceVarianceToggleButton) { //[UserButtonCode_rbmReduceVarianceToggleButton] -- add your button handler code here.. - m_pRbm->setUseProbsForHiddenReconstruction(buttonThatWasClicked->getToggleState()); + m_pRbmComponent->setUseProbsForHiddenReconstruction(buttonThatWasClicked->getToggleState()); //[/UserButtonCode_rbmReduceVarianceToggleButton] } else if (buttonThatWasClicked == rbmUseVisibleGaussianToggleButton) { //[UserButtonCode_rbmUseVisibleGaussianToggleButton] -- add your button handler code here.. - m_pRbm->setUseVisibleGaussian(buttonThatWasClicked->getToggleState()); - redrawReconstruction(numGibbsSlider->getValue()); + m_pRbmComponent->setUseVisibleGaussian(buttonThatWasClicked->getToggleState()); + m_pRbmComponent->redrawReconstruction(); //[/UserButtonCode_rbmUseVisibleGaussianToggleButton] } else if (buttonThatWasClicked == rbmDoSparseToggleButton) { //[UserButtonCode_rbmDoSparseToggleButton] -- add your button handler code here.. - m_pRbm->setDoSparse(buttonThatWasClicked->getToggleState()); + m_pRbmComponent->setDoSparse(buttonThatWasClicked->getToggleState()); //[/UserButtonCode_rbmDoSparseToggleButton] } else if (buttonThatWasClicked == rbmLearnVarianceButton) { //[UserButtonCode_rbmLearnVarianceButton] -- add your button handler code here.. - m_pRbm->setDoLearnVariance(buttonThatWasClicked->getToggleState()); + m_pRbmComponent->setDoLearnVariance(buttonThatWasClicked->getToggleState()); if (!buttonThatWasClicked->getToggleState()) { - m_pRbm->setSigma(sigmaLabel->getText().getFloatValue()); + m_pRbmComponent->setSigma(sigmaLabel->getText().getFloatValue()); } //[/UserButtonCode_rbmLearnVarianceButton] } else if (buttonThatWasClicked == rbmNormalizeDataToggleButton) { //[UserButtonCode_rbmNormalizeDataToggleButton] -- add your button handler code here.. - m_pRbm->setNormalizeData(buttonThatWasClicked->getToggleState()); + m_pRbmComponent->setNormalizeData(buttonThatWasClicked->getToggleState()); //[/UserButtonCode_rbmNormalizeDataToggleButton] } @@ -603,26 +596,19 @@ void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved) if (sliderThatWasMoved == patterSlider) { //[UserSliderCode_patterSlider] -- add your slider handling code here.. - if (m_layers.getSize() > 0) - { - RowVectorXd t = m_layers.getAt((int)sliderThatWasMoved->getValue()); - - DrawTraining->setData(t); - redrawReconstruction(numGibbsSlider->getValue()); - } + m_pRbmComponent->selectTraining((int)sliderThatWasMoved->getValue()); //[/UserSliderCode_patterSlider] } else if (sliderThatWasMoved == WeightsSlider) { //[UserSliderCode_WeightsSlider] -- add your slider handling code here.. - redrawWeights((int)sliderThatWasMoved->getValue()); + m_pRbmComponent->selectWeights((int)sliderThatWasMoved->getValue()); //[/UserSliderCode_WeightsSlider] } else if (sliderThatWasMoved == numGibbsSlider) { //[UserSliderCode_numGibbsSlider] -- add your slider handling code here.. - m_pRbm->setNumGibbs((uint32_t)sliderThatWasMoved->getValue()); - redrawReconstruction(sliderThatWasMoved->getValue()); + m_pRbmComponent->setNumGibbs((uint32_t)sliderThatWasMoved->getValue()); //[/UserSliderCode_numGibbsSlider] } else if (sliderThatWasMoved == m_progressBarSlider) @@ -648,7 +634,7 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged) else if (labelThatHasChanged == learningRateLabel) { //[UserLabelCode_learningRateLabel] -- add your label text handling code here.. - m_pRbm->setMuWeights(labelThatHasChanged->getText().getFloatValue()); + m_pRbmComponent->setMuWeights(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_learningRateLabel] } else if (labelThatHasChanged == numVisibleLabel) @@ -674,47 +660,44 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged) else if (labelThatHasChanged == lambdaLabel) { //[UserLabelCode_lambdaLabel] -- add your label text handling code here.. - m_pRbm->setLambda(labelThatHasChanged->getText().getFloatValue()); - redrawReconstruction(numGibbsSlider->getValue()); + m_pRbmComponent->setLambda(labelThatHasChanged->getText().getFloatValue()); + m_pRbmComponent->redrawReconstruction(); //[/UserLabelCode_lambdaLabel] } else if (labelThatHasChanged == sigmaLabel) { //[UserLabelCode_sigmaLabel] -- add your label text handling code here.. - m_pRbm->setSigma(labelThatHasChanged->getText().getFloatValue()); - DrawVars->setData(m_pRbm->getSigma()); - redrawReconstruction(numGibbsSlider->getValue()); - + m_pRbmComponent->setSigma(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_sigmaLabel] } else if (labelThatHasChanged == sparsityLabel) { //[UserLabelCode_sparsityLabel] -- add your label text handling code here.. - m_pRbm->setSparsity(labelThatHasChanged->getText().getFloatValue()); + m_pRbmComponent->setSparsity(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_sparsityLabel] } else if (labelThatHasChanged == sigmaDecayLabel) { //[UserLabelCode_sigmaDecayLabel] -- add your label text handling code here.. - m_pRbm->setSigmaDecay(labelThatHasChanged->getText().getFloatValue()); + m_pRbmComponent->setSigmaDecay(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_sigmaDecayLabel] } else if (labelThatHasChanged == weightDecayLabel) { //[UserLabelCode_weightDecayLabel] -- add your label text handling code here.. - m_pRbm->setWeightDecay(labelThatHasChanged->getText().getFloatValue()); + m_pRbmComponent->setWeightDecay(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_weightDecayLabel] } else if (labelThatHasChanged == momentumLabel) { //[UserLabelCode_momentumLabel] -- add your label text handling code here.. - m_pRbm->setMomentum(labelThatHasChanged->getText().getFloatValue()); + m_pRbmComponent->setMomentum(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_momentumLabel] } else if (labelThatHasChanged == sparsityLearningRateLabel) { //[UserLabelCode_sparsityLearningRateLabel] -- add your label text handling code here.. - m_pRbm->setMuSparsity(labelThatHasChanged->getText().getFloatValue()); + m_pRbmComponent->setMuSparsity(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_sparsityLearningRateLabel] } else if (labelThatHasChanged == weightInitLabel) @@ -793,12 +776,7 @@ void MainComponent::save () void MainComponent::create(const char *pFilename) { m_weights = nullptr; - DrawTraining = nullptr; - DrawReconstruction = nullptr; - DrawWeights = nullptr; - DrawVars = nullptr; - DrawHidden = nullptr; - m_pRbm = nullptr; + m_pRbmComponent = nullptr; if (pFilename) { @@ -811,38 +789,35 @@ void MainComponent::create(const char *pFilename) m_vNumX = m_weights->getNumVisibleX(); m_vNumY = m_weights->getNumVisibleY(); m_hNum = m_weights->getNumHidden(); - WeightsSlider->setRange(0, m_hNum-1, 1); - addAndMakeVisible (DrawTraining = new DrawComponent (m_vNumX, m_vNumY)); - DrawTraining->setListener(this); - addAndMakeVisible (DrawReconstruction = new DrawComponent (m_vNumX, m_vNumY)); - addAndMakeVisible (DrawWeights = new DrawComponent (m_vNumX, m_vNumY)); - addAndMakeVisible (DrawVars = new DrawComponent (m_vNumX, m_vNumY)); - addAndMakeVisible (DrawHidden = new DrawComponent (m_hNum, 1)); - DrawHidden->setListener(this); numVisibleLabel->setText(String(m_vNumX), dontSendNotification ); numVisibleYLabel->setText(String(m_vNumY), dontSendNotification ); numHiddenLabel->setText(String(m_hNum), dontSendNotification ); - m_pRbm = new Rbm(*m_weights, this); + addAndMakeVisible(m_pRbmComponent = new RbmComponent(*m_weights, *this)); + m_pRbmComponent->setBounds (16, 16, 430, 130); + + m_pRbmComponent->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState()); + m_pRbmComponent->setUseProbsForHiddenReconstruction(rbmReduceVarianceToggleButton->getToggleState()); + m_pRbmComponent->setUseVisibleGaussian(rbmUseVisibleGaussianToggleButton->getToggleState()); + m_pRbmComponent->setDoSparse(rbmDoSparseToggleButton->getToggleState()); - m_pRbm->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState()); - m_pRbm->setUseProbsForHiddenReconstruction(rbmReduceVarianceToggleButton->getToggleState()); - m_pRbm->setUseVisibleGaussian(rbmUseVisibleGaussianToggleButton->getToggleState()); - m_pRbm->setDoSparse(rbmDoSparseToggleButton->getToggleState()); - - m_pRbm->setLambda(lambdaLabel->getText().getFloatValue()); - m_pRbm->setSigma(sigmaLabel->getText().getFloatValue()); - m_pRbm->setSigmaDecay(sigmaDecayLabel->getText().getFloatValue()); - m_pRbm->setWeightDecay(weightDecayLabel->getText().getFloatValue()); - m_pRbm->setSparsity(sparsityLabel->getText().getFloatValue()); - m_pRbm->setNumGibbs((uint32_t)numGibbsSlider->getValue()); + m_pRbmComponent->setLambda(lambdaLabel->getText().getFloatValue()); + m_pRbmComponent->setSigmaDecay(sigmaDecayLabel->getText().getFloatValue()); + m_pRbmComponent->setWeightDecay(weightDecayLabel->getText().getFloatValue()); + m_pRbmComponent->setSparsity(sparsityLabel->getText().getFloatValue()); + m_pRbmComponent->setNumGibbs((uint32_t)numGibbsSlider->getValue()); + m_pRbmComponent->setDoLearnVariance(rbmLearnVarianceButton->getToggleState()); + if (!rbmLearnVarianceButton->getToggleState()) + { + m_pRbmComponent->setSigma(sigmaLabel->getText().getFloatValue()); + } resized(); - redrawReconstruction(numGibbsSlider->getValue()); - redrawWeights((int)WeightsSlider->getValue()); + m_pRbmComponent->redrawReconstruction(); + m_pRbmComponent->selectWeights((int)WeightsSlider->getValue()); } void MainComponent::destroy() @@ -857,85 +832,10 @@ const juce::String& MainComponent::getBaseDir() } -void MainComponent::onChanged(const LayerArray &obj) -{ - patterSlider->setRange(0, std::max(0,(int)obj.getSize()-1), 1); -} - -void MainComponent::onEpochTrained(const Rbm &obj) -{ -// mylog("Training %f %%\n", obj.getProgress()*100); - redrawReconstruction(numGibbsSlider->getValue()); - redrawWeights((int)WeightsSlider->getValue()); - m_progressBarSlider->setValue((int)(100*obj.getProgress()), dontSendNotification); -} - -void MainComponent::onDraw(DrawComponent &obj) -{ - if (&obj == DrawHidden) - { - DrawReconstruction->setData(m_pRbm->toVisible(obj.getData())); - } - if (&obj == DrawTraining) - { - redrawReconstruction(numGibbsSlider->getValue()); - } -} - -void MainComponent::redrawReconstruction(uint32_t numIter) -{ - uint32_t i; - VectorXd V, H; - - V = DrawTraining->getData(); - for (i=0; i < numIter; i++) - { - H = m_pRbm->toHidden(V); - DrawHidden->setData(H); - V = m_pRbm->toVisible(H); - DrawReconstruction->setData(V); - } - - if ((m_vNumX == 27) && (m_vNumY == 4)) - { - char table[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - MatrixXd m = m_pRbm->toVisible(DrawHidden->getData()); - m.resize(m_vNumX, m_vNumY); - cout << "Reconstruction" << endl; - cout << m << endl; - - for (int y=0; y < m_vNumY; y++) - { - double maxV = -1000; - int maxX = 0; - for (int x=0; x < m_vNumX; x++) - { - if (m(x, y) > maxV) - { - maxV = m(x, y); - maxX = x; - } - } - cout << table[maxX]; - } - cout << endl; - } - - -} - -void MainComponent::redrawWeights(int index) -{ - VectorXd w = m_weights->weights().col(index); - DrawWeights->setData(w); - DrawVars->setData(m_pRbm->getSigma()); - -} - void MainComponent::run() { // trainButton->setEnabled(false); - m_pRbm->train(m_layers, numEpochslabel->getText().getIntValue()); + m_pRbmComponent->train(numEpochslabel->getText().getIntValue()); // trainButton->setEnabled(true); } diff --git a/Source/MainComponent.h b/Source/MainComponent.h index 6d3a019..750449c 100644 --- a/Source/MainComponent.h +++ b/Source/MainComponent.h @@ -24,7 +24,7 @@ #include "JuceHeader.h" #include "DrawComponent.h" #include "LayerArray.hpp" -#include "Rbm.hpp" +#include "RbmComponent.h" //[/Headers] @@ -38,9 +38,7 @@ //[/Comments] */ class MainComponent : public Component, - public LayerArrayListener, - public RbmListener, - public DrawListener, + public RbmComponentListener, public Thread, public ButtonListener, public SliderListener, @@ -69,35 +67,25 @@ public: void mouseDoubleClick (const MouseEvent& e); void mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel); + void onLayerSizeChanged(size_t size) override; + void onRbmEpochTrained(size_t progressPercent) override; + private: //[UserVariables] -- You can add your own custom variables in this section. ScopedPointer m_weights; - ScopedPointer m_pRbm; - ScopedPointer DrawTraining; - ScopedPointer DrawReconstruction; - ScopedPointer DrawWeights; - ScopedPointer DrawVars; - ScopedPointer DrawHidden; + ScopedPointer m_pRbmComponent; uint32_t m_vNumX; uint32_t m_vNumY; uint32_t m_hNum; - LayerArray m_layers; - MatrixXd m_trainingData; void load(); void save(); void create(const char *pFilename=nullptr); void destroy(); const juce::String& getBaseDir(); - void onChanged(const LayerArray &obj); - void onEpochTrained(const Rbm &obj); - void onDraw(DrawComponent &obj); - void redrawReconstruction(uint32_t numIter); - void redrawWeights(int index); void run(); String m_baseDir; - uint32_t m_numGibbs; //[/UserVariables] //============================================================================== diff --git a/Source/Rbm.hpp b/Source/Rbm.hpp index 575b71c..7ee465e 100644 --- a/Source/Rbm.hpp +++ b/Source/Rbm.hpp @@ -493,6 +493,11 @@ public: m_numGibbs = value; } + uint32_t getNumGibbs() + { + return m_numGibbs; + } + void setMuWeights(double value) { m_muWeights = value; diff --git a/Source/RbmComponent.cpp b/Source/RbmComponent.cpp new file mode 100644 index 0000000..2bf440c --- /dev/null +++ b/Source/RbmComponent.cpp @@ -0,0 +1,315 @@ +/* + ============================================================================== + + This is an automatically generated GUI class created by the Introjucer! + + Be careful when adding custom code to these files, as only the code within + the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded + and re-saved. + + Created with Introjucer version: 3.1.0 + + ------------------------------------------------------------------------------ + + The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-13 by Raw Material Software Ltd. + + ============================================================================== +*/ + + +#include "RbmComponent.h" + + + +//============================================================================== +RbmComponent::RbmComponent (Weights &weights, RbmComponentListener &listener) + : m_weights(weights) + , m_listener(listener) + , m_layers(this) + , m_currWeightIndexToDraw(0) + , m_currTrainingIndexToDraw(0) + , DrawTraining(nullptr) + , DrawReconstruction(nullptr) + , DrawWeights(nullptr) + , DrawHidden(nullptr) +{ + + //[UserPreSize] + m_pRbm = new Rbm(m_weights, this); + m_vNumX = m_weights.getNumVisibleX(); + m_vNumY = m_weights.getNumVisibleY(); + m_hNum = m_weights.getNumHidden(); + + addAndMakeVisible (DrawTraining = new DrawComponent (m_vNumX, m_vNumY)); + DrawTraining->setListener(this); + + addAndMakeVisible (DrawReconstruction = new DrawComponent (m_vNumX, m_vNumY)); + addAndMakeVisible (DrawWeights = new DrawComponent (m_vNumX, m_vNumY)); + addAndMakeVisible (DrawVars = new DrawComponent (m_vNumX, m_vNumY)); + addAndMakeVisible (DrawHidden = new DrawComponent (m_hNum, 1)); + DrawHidden->setListener(this); + //[/UserPreSize] + + setSize (430, 130); + resized(); + +} + +RbmComponent::~RbmComponent() +{ + //[Destructor_pre]. You can add your own custom destruction code here.. + //[/Destructor_pre] + + //[Destructor]. You can add your own custom destruction code here.. + DrawTraining = nullptr; + DrawReconstruction = nullptr; + DrawWeights = nullptr; + DrawVars = nullptr; + DrawHidden = nullptr; + m_pRbm = nullptr; + //[/Destructor] +} + +//============================================================================== +void RbmComponent::paint (Graphics& g) +{ + //[UserPrePaint] Add your own custom painting code here.. + //[/UserPrePaint] + + g.fillAll (Colours::white); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sigma"), + 32, 306, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Lambda"), + 120, 306, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Num. epochs"), + 32, 354, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Alpha"), + 120, 354, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sparsity"), + 208, 306, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sigma decay"), + 296, 306, 96, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Weight decay"), + 296, 354, 96, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Momentum"), + 208, 354, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sparsity Alpha"), + 408, 306, 96, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Weight init"), + 408, 354, 96, 14, + Justification::centred, true); + + //[UserPaint] Add your own custom painting code here.. + //[/UserPaint] +} + +void RbmComponent::resized() +{ + DrawTraining->setBoundsRelative(0, 0, 100./430, 100./130); + DrawReconstruction->setBoundsRelative(110./430, 0, 100./430, 100./130); + DrawWeights->setBoundsRelative (220./430, 0, 100./430, 100./130); + DrawVars->setBoundsRelative (330./430, 0, 100./430, 100./130); + DrawHidden->setBoundsRelative (0, 110./130, 430./430, 20./130); +} + +void RbmComponent::mouseMove (const MouseEvent& e) +{ + //[UserCode_mouseMove] -- Add your code here... + //[/UserCode_mouseMove] +} + +void RbmComponent::mouseEnter (const MouseEvent& e) +{ + //[UserCode_mouseEnter] -- Add your code here... + //[/UserCode_mouseEnter] +} + +void RbmComponent::mouseExit (const MouseEvent& e) +{ + //[UserCode_mouseExit] -- Add your code here... + //[/UserCode_mouseExit] +} + +void RbmComponent::mouseDown (const MouseEvent& e) +{ + //[UserCode_mouseDown] -- Add your code here... + //[/UserCode_mouseDown] +} + +void RbmComponent::mouseDrag (const MouseEvent& e) +{ + //[UserCode_mouseDrag] -- Add your code here... +// e.eventComponent->setCentrePosition(e.getPosition().x, e.getPosition().y); + cout << "Mouse = " << e.x << "," << e.y << endl; + //[/UserCode_mouseDrag] +} + +void RbmComponent::mouseUp (const MouseEvent& e) +{ + //[UserCode_mouseUp] -- Add your code here... + //[/UserCode_mouseUp] +} + +void RbmComponent::mouseDoubleClick (const MouseEvent& e) +{ + //[UserCode_mouseDoubleClick] -- Add your code here... + //[/UserCode_mouseDoubleClick] +} + +void RbmComponent::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel) +{ + //[UserCode_mouseWheelMove] -- Add your code here... + //[/UserCode_mouseWheelMove] +} + +void RbmComponent::onChanged(const LayerArray &obj) +{ + m_listener.onLayerSizeChanged((size_t)obj.getSize()); +} + +void RbmComponent::onEpochTrained(const Rbm &obj) +{ +// mylog("Training %f %%\n", obj.getProgress()*100); + redrawReconstruction(); + redrawWeights(); + m_listener.onRbmEpochTrained((size_t)(100*obj.getProgress())); +} + +void RbmComponent::onDraw(DrawComponent &obj) +{ + if (&obj == DrawHidden) + { + DrawReconstruction->setData(m_pRbm->toVisible(obj.getData())); + } + if (&obj == DrawTraining) + { + redrawReconstruction(); + } +} + +void RbmComponent::redrawReconstruction() +{ + uint32_t i; + VectorXd V, H; + + V = DrawTraining->getData(); + for (i=0; i < m_pRbm->getNumGibbs(); i++) + { + H = m_pRbm->toHidden(V); + DrawHidden->setData(H); + V = m_pRbm->toVisible(H); + DrawReconstruction->setData(V); + } +} + +void RbmComponent::redrawWeights() +{ + VectorXd w = m_weights.weights().col(m_currWeightIndexToDraw); + DrawWeights->setData(w); + DrawVars->setData(m_pRbm->getSigma()); + +} + +void RbmComponent::setDoLearnVariance(bool value) +{ + m_pRbm->setDoLearnVariance(value); +} + +void RbmComponent::setDoRaoBlackwell(bool enable) +{ + m_pRbm->setDoRaoBlackwell(enable); +} + +void RbmComponent::setUseProbsForHiddenReconstruction(bool enable) +{ + m_pRbm->setUseProbsForHiddenReconstruction(enable); +} + +void RbmComponent::setUseVisibleGaussian(bool enable) +{ + m_pRbm->setUseVisibleGaussian(enable); +} + +void RbmComponent::setDoSparse(bool enable) +{ + m_pRbm->setDoSparse(enable); +} + +void RbmComponent::setLambda(double value) +{ + m_pRbm->setLambda(value); +} + +void RbmComponent::setSigmaDecay(double value) +{ + m_pRbm->setSigmaDecay(value); +} + +void RbmComponent::setWeightDecay(double value) +{ + m_pRbm->setWeightDecay(value); +} + +void RbmComponent::setSparsity(double value) +{ + m_pRbm->setSparsity(value); +} + +void RbmComponent::setNumGibbs(size_t value) +{ + m_pRbm->setNumGibbs(value); + redrawReconstruction(); + +} + +void RbmComponent::setSigma(double value) +{ + m_pRbm->setSigma(value); + DrawVars->setData(m_pRbm->getSigma()); + redrawReconstruction(); +} + +//[/MiscUserCode] + + +//============================================================================== diff --git a/Source/RbmComponent.h b/Source/RbmComponent.h new file mode 100644 index 0000000..5991408 --- /dev/null +++ b/Source/RbmComponent.h @@ -0,0 +1,194 @@ +/* + ============================================================================== + + This is an automatically generated GUI class created by the Introjucer! + + Be careful when adding custom code to these files, as only the code within + the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded + and re-saved. + + Created with Introjucer version: 3.1.0 + + ------------------------------------------------------------------------------ + + The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-13 by Raw Material Software Ltd. + + ============================================================================== +*/ + +#ifndef __RBM_COMPONENT__ +#define __RBM_COMPONENT__ + +//[Headers] -- You can add your own extra header files here -- +#include "JuceHeader.h" +#include "DrawComponent.h" +#include "LayerArray.hpp" +#include "Rbm.hpp" +//[/Headers] + +class RbmComponentListener +{ +public: + RbmComponentListener() {} + virtual ~RbmComponentListener() + { + } + virtual void onLayerSizeChanged(size_t size) = 0; + virtual void onRbmEpochTrained(size_t progressPercent) = 0; +}; + + +//============================================================================== +/** + //[Comments] + An auto-generated component, created by the Introjucer. + + Describe your class and how it works here! + //[/Comments] +*/ +class RbmComponent : public Component, + public LayerArrayListener, + public RbmListener, + public DrawListener +{ +public: + //============================================================================== + RbmComponent (Weights &weights, RbmComponentListener &listener); + ~RbmComponent(); + + //============================================================================== + //[UserMethods] -- You can add your own custom methods in this section. + //[/UserMethods] + + void paint (Graphics& g); + void resized() override; + void mouseMove (const MouseEvent& e) override; + void mouseEnter (const MouseEvent& e) override; + void mouseExit (const MouseEvent& e) override; + void mouseDown (const MouseEvent& e) override; + void mouseDrag (const MouseEvent& e) override; + void mouseUp (const MouseEvent& e) override; + void mouseDoubleClick (const MouseEvent& e) override; + void mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel) override; + + void setDoLearnVariance(bool value); + void setDoRaoBlackwell(bool enable); + void setUseProbsForHiddenReconstruction(bool enable); + void setUseVisibleGaussian(bool enable); + void setDoSparse(bool enable); + void setNormalizeData(bool enable) + { + m_pRbm->setNormalizeData(enable); + } + + void setLambda(double value); + void setSigmaDecay(double value); + void setWeightDecay(double value); + void setSparsity(double value); + void setNumGibbs(size_t value); + void setSigma(double value); + + void setMuWeights(double value) + { + m_pRbm->setMuWeights(value); + } + + void setMuSparsity(double value) + { + m_pRbm->setMuSparsity(value); + } + + void setMomentum(double value) + { + m_pRbm->setMomentum(value); + } + + void addFromTraining() + { + DrawReconstruction->setData(DrawTraining->getData()); + m_layers.add(DrawTraining->getData(), m_vNumX*m_vNumY); + m_listener.onLayerSizeChanged(m_layers.getSize()); + } + + void selectWeights(size_t index) + { + m_currWeightIndexToDraw = index; + redrawWeights(); + } + + void selectTraining(size_t index) + { + if (m_layers.getSize() > 0) + { + m_currTrainingIndexToDraw = index; + RowVectorXd t = m_layers.getAt(index); + + DrawTraining->setData(t); + redrawReconstruction(); + } + } + + void copyReconstructionToTraining() + { + DrawTraining->setData(DrawReconstruction->getData()); + } + + void clearTraining() + { + m_layers.clear(); + } + + void loadTraining(const char *pFilename) + { + m_layers.load(pFilename); + } + + void saveTraining(const char *pFilename) + { + m_layers.save(pFilename); + } + + void removeTrainingAt(size_t index) + { + m_layers.removeAt(index); + } + + void redrawWeights(); + void redrawReconstruction(); + void train(size_t numEpochs) + { + m_pRbm->train(m_layers, numEpochs); + } + +private: + //[UserVariables] -- You can add your own custom variables in this section. + ScopedPointer m_pRbm; + Weights &m_weights; + RbmComponentListener &m_listener; + ScopedPointer DrawTraining; + ScopedPointer DrawReconstruction; + ScopedPointer DrawWeights; + ScopedPointer DrawVars; + ScopedPointer DrawHidden; + uint32_t m_vNumX; + uint32_t m_vNumY; + uint32_t m_hNum; + LayerArray m_layers; + size_t m_currWeightIndexToDraw; + size_t m_currTrainingIndexToDraw; + void onChanged(const LayerArray &obj) override; + void onEpochTrained(const Rbm &obj) override; + void onDraw(DrawComponent &obj) override; + //[/UserVariables] + + //============================================================================== + + //============================================================================== + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RbmComponent) +}; + +//[EndFile] You can add extra defines here... +//[/EndFile] + +#endif // __RBM_COMPONENT__