From 4e21dd81148b9b7999fbb36da7b67789476296bf Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Thu, 7 Nov 2019 16:21:35 +0000 Subject: [PATCH] - fixed progress indicator - cleaned up git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@624 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- source/DrawComponent.cpp | 7 +++---- source/MainComponent.cpp | 22 +++++++++++----------- source/MainComponent.hpp | 39 +++++++++++++++++++++++++++------------ source/RbmComponent.cpp | 7 +++---- source/RbmComponent.hpp | 8 ++++++-- 5 files changed, 50 insertions(+), 33 deletions(-) diff --git a/source/DrawComponent.cpp b/source/DrawComponent.cpp index 1671d79..b346c4d 100644 --- a/source/DrawComponent.cpp +++ b/source/DrawComponent.cpp @@ -185,7 +185,6 @@ void DrawComponent::drawAt(int x, int y, bool setColor) double fy = (double)y / m_scaleY; index = (int)(x/m_scaleX) + m_width*(int)(y/m_scaleY); -// mylog("Write(%d)\n", index); if (index >= 0) { @@ -228,8 +227,7 @@ arma::mat& DrawComponent::getData () void DrawComponent::DrawData () { - - double max = abs(m_data).max(); + double max = arma::abs(m_data).max(); if (max < 1.0) { @@ -241,10 +239,11 @@ void DrawComponent::DrawData () { for (int j=0; j < m_width; j++) { - m_pG->setColour(Colour(Colours::white).greyLevel(m_offset + scale*m_data[i*m_width + j])); + m_pG->setColour(Colour(Colours::white).greyLevel(m_offset + scale*m_data(i*m_width + j))); m_pG->fillRect(m_scaleX*j, m_scaleY*i, m_scaleX, m_scaleY); } } + repaint(); } diff --git a/source/MainComponent.cpp b/source/MainComponent.cpp index 929a6ec..27570fe 100644 --- a/source/MainComponent.cpp +++ b/source/MainComponent.cpp @@ -499,16 +499,14 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) { //[UserButtonCode_addButton] -- add your button handler code here.. // m_layers.add(m_pRbmComponent[0]->getTrainingData(), m_weights[0]->getNumVisible()); -// m_pRbmComponent[0]->batchchanged(); //[/UserButtonCode_addButton] } else if (buttonThatWasClicked == ShakeButton) { //[UserButtonCode_ShakeButton] -- add your button handler code here.. m_pLayer->weightsInit(weightInitLabel->getText().getFloatValue()); -// m_pLayer->batchchanged(); -// m_pLayer->redrawWeights(); -// m_pLayer->redrawReconstruction(); + m_pLayer->redrawWeights(); + m_pLayer->redrawReconstruction(); //[/UserButtonCode_ShakeButton] } else if (buttonThatWasClicked == createButton) @@ -529,6 +527,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) m_rbmSelect->addItem(String(id), id+1); } m_rbmSelect->setSelectedId(1, sendNotification); + loadTraining(); //[/UserButtonCode_loadButton] } else if (buttonThatWasClicked == saveButton) @@ -540,28 +539,25 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) else if (buttonThatWasClicked == loadTrainingButton) { //[UserButtonCode_loadTrainingButton] -- add your button handler code here.. - loadTraining((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); - m_stack->batchchanged(); + loadTraining(); //[/UserButtonCode_loadTrainingButton] } else if (buttonThatWasClicked == saveTrainingButton) { //[UserButtonCode_saveTrainingButton] -- add your button handler code here.. - saveTraining((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); + saveTraining(); //[/UserButtonCode_saveTrainingButton] } else if (buttonThatWasClicked == clearTrainingButton) { //[UserButtonCode_clearTrainingButton] -- add your button handler code here.. clearTraining(); - m_stack->batchchanged(); //[/UserButtonCode_clearTrainingButton] } else if (buttonThatWasClicked == removeTrainingButton) { //[UserButtonCode_removeTrainingButton] -- add your button handler code here.. removeTrainingAt((int)patterSlider->getValue()); - m_stack->batchchanged(); //[/UserButtonCode_removeTrainingButton] } else if (buttonThatWasClicked == rbmDoRaoBlackwellToggleButton) @@ -621,6 +617,7 @@ void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved) { //[UserSliderCode_patterSlider] -- add your slider handling code here.. m_trainingIndex = (int)sliderThatWasMoved->getValue(); + m_pLayer->setTrainingIndex(m_trainingData.row(m_trainingIndex)); //[/UserSliderCode_patterSlider] } else if (sliderThatWasMoved == WeightsSlider) @@ -653,6 +650,7 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged) if (labelThatHasChanged == numEpochslabel) { //[UserLabelCode_numEpochslabel] -- add your label text handling code here.. + m_pLayer->params().numEpochs = labelThatHasChanged->getText().getFloatValue(); //[/UserLabelCode_numEpochslabel] } else if (labelThatHasChanged == learningRateLabel) @@ -726,6 +724,7 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged) else if (labelThatHasChanged == sizeMiniBatch) { //[UserLabelCode_sizeMiniBatch] -- add your label text handling code here.. + m_pLayer->params().miniBatchSize = labelThatHasChanged->getText().getFloatValue(); //[/UserLabelCode_sizeMiniBatch] } @@ -741,7 +740,7 @@ void MainComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged) if (comboBoxThatHasChanged == m_rbmSelect) { int index = m_rbmSelect->getSelectedItemIndex(); - m_pLayer = m_stack->getLayer(index); + m_pLayer = static_cast(m_stack->getLayer(index)); updateControls(); //[/UserComboBoxCode_m_rbmSelect] } @@ -881,9 +880,10 @@ void MainComponent::run() m_pLayer->train(m_trainingData); } -void MainComponent::onProgressChanged(size_t progressPercent) +bool MainComponent::onProgressChanged(size_t progressPercent) { m_progressBarSlider->setValue(progressPercent); + return !m_doStop; } void MainComponent::updateControls() diff --git a/source/MainComponent.hpp b/source/MainComponent.hpp index b9fcab8..50024a2 100644 --- a/source/MainComponent.hpp +++ b/source/MainComponent.hpp @@ -38,13 +38,15 @@ Describe your class and how it works here! //[/Comments] */ -class MainComponent : public Component, - public Thread, - public LayerConstructor, - public ButtonListener, - public SliderListener, - public LabelListener, - public ComboBoxListener +class MainComponent +: public Component +, public Thread +, public LayerConstructor +, public RbmComponentListener +, public ButtonListener +, public SliderListener +, public LabelListener +, public ComboBoxListener { public: //============================================================================== @@ -53,7 +55,7 @@ public: //============================================================================== //[UserMethods] -- You can add your own custom methods in this section. - void onProgressChanged(size_t progressPercent); + bool onProgressChanged(size_t progressPercent) override; //[/UserMethods] void paint (Graphics& g); @@ -73,16 +75,17 @@ public: Layer* onConstruct(const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden) { - RbmComponent *pComp = new RbmComponent(name, id, numVisibleX, numVisibleY, numHidden); + RbmComponent *pComp = new RbmComponent(name, id, numVisibleX, numVisibleY, numHidden, this); addAndMakeVisible(pComp); return pComp; } + private: //[UserVariables] -- You can add your own custom variables in this section. static const size_t DBN_SIZE = 4; ScopedPointer m_stack; - Layer *m_pLayer; + RbmComponent *m_pLayer; arma::mat m_trainingData; int m_weightIndex; int m_trainingIndex; @@ -96,18 +99,30 @@ private: bool m_doStop; void clearTraining() { + patterSlider->setRange(0, m_stack->numTraining(m_trainingData)-1, 1); } - void loadTraining(const char *pFilename) + void loadTraining() { + m_trainingData = m_stack->loadTraining(); + patterSlider->setRange(0, m_stack->numTraining(m_trainingData)-1, 1); } - void saveTraining(const char *pFilename) + void saveTraining() { + m_stack->save(); } + void addTraining(const arma::mat &training) + { + m_stack->addTraining(m_trainingData, training); + patterSlider->setRange(0, m_stack->numTraining(m_trainingData)-1, 1); + } + void removeTrainingAt(size_t index) { + m_stack->delTraining(m_trainingData, index); + patterSlider->setRange(0, m_stack->numTraining(m_trainingData)-1, 1); } void updateControls(); diff --git a/source/RbmComponent.cpp b/source/RbmComponent.cpp index f77127a..3a97a27 100644 --- a/source/RbmComponent.cpp +++ b/source/RbmComponent.cpp @@ -23,7 +23,7 @@ //============================================================================== -RbmComponent::RbmComponent (const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden) +RbmComponent::RbmComponent (const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, RbmComponentListener *pListener) : Layer(name, id, numVisibleX, numVisibleY, numHidden) , m_currWeightIndexToDraw(0) , m_currTrainingIndexToDraw(0) @@ -31,7 +31,7 @@ RbmComponent::RbmComponent (const std::string &name, size_t id, size_t numVisibl , DrawReconstruction(nullptr) , DrawWeights(nullptr) , DrawHidden(nullptr) - , m_listener(nullptr) + , m_listener(pListener) { addAndMakeVisible (DrawTraining = new DrawComponent (numVisibleX, numVisibleY)); DrawTraining->setListener(this); @@ -195,13 +195,12 @@ void RbmComponent::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& bool RbmComponent::onProgress(const Rbm::Status &status) { -// mylog("Training %f %%\n", obj.getProgress()*100); upPass(DrawHidden->getData()); redrawReconstruction(); redrawWeights(); if (m_listener) { - m_listener->onProgressChanged((size_t)(status.progress + 0.5)); + return m_listener->onProgressChanged((size_t)(100*status.progress + 0.5)); } return true; } diff --git a/source/RbmComponent.hpp b/source/RbmComponent.hpp index f5d8307..355d239 100644 --- a/source/RbmComponent.hpp +++ b/source/RbmComponent.hpp @@ -60,7 +60,7 @@ public: virtual ~RbmComponentListener() { } - virtual void onProgressChanged(size_t progressPercent) = 0; + virtual bool onProgressChanged(size_t progressPercent) = 0; }; @@ -80,7 +80,7 @@ class RbmComponent : public Component { public: //============================================================================== - RbmComponent (const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden); + RbmComponent (const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, RbmComponentListener *pListener=nullptr); ~RbmComponent(); //============================================================================== @@ -133,6 +133,10 @@ public: arma::mat getConvolutedWeight(arma::mat const &h) override; arma::mat const& getWeights() override; + void train(const arma::mat& batch) + { + Layer::train(batch, this); + } private: //[UserVariables] -- You can add your own custom variables in this section. RbmComponentListener *m_listener;