diff --git a/Source/MainComponent.cpp b/Source/MainComponent.cpp index a30c4e6..73d48d4 100644 --- a/Source/MainComponent.cpp +++ b/Source/MainComponent.cpp @@ -38,7 +38,10 @@ void mylog(const char* format, ...) //============================================================================== MainComponent::MainComponent () : Thread("RBM"), - m_pRbmComponent(nullptr) + m_pRbmComponent(nullptr), + m_layers(this) + + { addAndMakeVisible (trainButton = new TextButton ("Train button")); trainButton->setButtonText (TRANS("Train")); @@ -458,6 +461,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) else if (buttonThatWasClicked == addButton) { //[UserButtonCode_addButton] -- add your button handler code here.. + m_layers.add(m_pRbmComponent->getTrainingData(), m_weights->getNumVisible()); m_pRbmComponent->addFromTraining(); //[/UserButtonCode_addButton] } @@ -471,8 +475,8 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) { //[UserButtonCode_ShakeButton] -- add your button handler code here.. m_weights->shuffle(weightInitLabel->getText().getFloatValue()); - m_pRbmComponent->redrawReconstruction(); m_pRbmComponent->redrawWeights(); + m_pRbmComponent->redrawReconstruction(); //[/UserButtonCode_ShakeButton] } else if (buttonThatWasClicked == testButton) @@ -503,25 +507,25 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) else if (buttonThatWasClicked == loadTrainingButton) { //[UserButtonCode_loadTrainingButton] -- add your button handler code here.. - m_pRbmComponent->loadTraining((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); + loadTraining((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); //[/UserButtonCode_loadTrainingButton] } else if (buttonThatWasClicked == saveTrainingButton) { //[UserButtonCode_saveTrainingButton] -- add your button handler code here.. - m_pRbmComponent->saveTraining((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); + saveTraining((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8()); //[/UserButtonCode_saveTrainingButton] } else if (buttonThatWasClicked == clearTrainingButton) { //[UserButtonCode_clearTrainingButton] -- add your button handler code here.. - m_pRbmComponent->clearTraining(); + clearTraining(); //[/UserButtonCode_clearTrainingButton] } else if (buttonThatWasClicked == removeTrainingButton) { //[UserButtonCode_removeTrainingButton] -- add your button handler code here.. - m_pRbmComponent->removeTrainingAt((int)patterSlider->getValue()); + removeTrainingAt((int)patterSlider->getValue()); //[/UserButtonCode_removeTrainingButton] } else if (buttonThatWasClicked == reconstructEquButton) @@ -769,6 +773,7 @@ void MainComponent::create(juce::String const &projectName) if (!projectName.isEmpty()) { m_weights = new Weights((String(projectName + String(".weights.dat"))).toUTF8()); + loadTraining((String(projectName + String(".trainingStates.dat"))).toUTF8()); } else { @@ -783,11 +788,7 @@ void MainComponent::create(juce::String const &projectName) numVisibleYLabel->setText(String(vNumY), dontSendNotification ); numHiddenLabel->setText(String(hNum), dontSendNotification ); - addAndMakeVisible(m_pRbmComponent = new RbmComponent(*m_weights, *this)); - if (!projectName.isEmpty()) - { - m_pRbmComponent->loadTraining((String(projectName + String(".trainingStates.dat"))).toUTF8()); - } + addAndMakeVisible(m_pRbmComponent = new RbmComponent(*m_weights, m_layers.data(), *this)); m_pRbmComponent->setBounds (16, 16, 430, 130); m_pRbmComponent->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState()); @@ -830,9 +831,9 @@ void MainComponent::run() // trainButton->setEnabled(true); } -void MainComponent::onLayerSizeChanged(size_t size) +void MainComponent::onChanged(const LayerArray &obj) { - patterSlider->setRange(0, size-1, 1); + patterSlider->setRange(0, obj.getSize()-1, 1); } void MainComponent::onRbmEpochTrained(size_t progressPercent) diff --git a/Source/MainComponent.h b/Source/MainComponent.h index 3303e07..b7cbe56 100644 --- a/Source/MainComponent.h +++ b/Source/MainComponent.h @@ -39,6 +39,7 @@ */ class MainComponent : public Component, public RbmComponentListener, + public LayerArrayListener, public Thread, public ButtonListener, public SliderListener, @@ -51,7 +52,7 @@ public: //============================================================================== //[UserMethods] -- You can add your own custom methods in this section. - void onLayerSizeChanged(size_t size) override; + void onChanged(const LayerArray &obj) override; void onRbmEpochTrained(size_t progressPercent) override; //[/UserMethods] @@ -75,6 +76,7 @@ private: //[UserVariables] -- You can add your own custom variables in this section. ScopedPointer m_weights; ScopedPointer m_pRbmComponent; + LayerArray m_layers; void load(); void save(); void create(juce::String const &projectName=""); @@ -82,6 +84,26 @@ private: const juce::String& getBaseDir(); void run(); String m_baseDir; + void clearTraining() + { + m_layers.clear(); + } + + void loadTraining(const char *pFilename) + { + m_layers.clear(); + m_layers.load(pFilename); + } + + void saveTraining(const char *pFilename) + { + m_layers.save(pFilename); + } + + void removeTrainingAt(size_t index) + { + m_layers.removeAt(index); + } //[/UserVariables] //============================================================================== diff --git a/Source/Rbm.hpp b/Source/Rbm.hpp index 1dea022..9520e64 100644 --- a/Source/Rbm.hpp +++ b/Source/Rbm.hpp @@ -238,7 +238,6 @@ public: MatrixXd sumBiasH(1, m_w.getNumHidden()); MatrixXd sumWeights(m_w.getNumVisible(), m_w.getNumHidden()); - MatrixXd deltaVar(MatrixXd::Zero(1, m_w.getNumVisible())); MatrixXd deltaBiasV(MatrixXd::Zero(1, m_w.getNumVisible())); MatrixXd deltaBiasH(MatrixXd::Zero(1, m_w.getNumHidden())); MatrixXd deltaWeights(MatrixXd::Zero(m_w.getNumVisible(), m_w.getNumHidden())); @@ -303,7 +302,7 @@ public: } } - // Create hidden reconstruction given v + // Create hidden representation given v toHiddenBatch(m_h, m_v); probsLogistic(m_h); } @@ -329,8 +328,8 @@ public: if (m_doSparse) { - m_h = m_v * m_w.weights(); - m_h += m_w.hiddenBias().replicate(batchSize, 1); + // Create hidden representation given v + toHiddenBatch(m_h, m_v); probsLogistic(m_h); sumBiasH.fill(m_sparsity); @@ -515,6 +514,11 @@ public: return m_v; } + MatrixXd const& getBatch() + { + return m_batch; + } + private: Weights &m_w; MatrixXd const &m_batch; diff --git a/Source/RbmComponent.cpp b/Source/RbmComponent.cpp index 77659e7..0e99e7a 100644 --- a/Source/RbmComponent.cpp +++ b/Source/RbmComponent.cpp @@ -23,10 +23,9 @@ //============================================================================== -RbmComponent::RbmComponent (Weights &weights, RbmComponentListener &listener) +RbmComponent::RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponentListener &listener) : m_weights(weights) , m_listener(listener) - , m_layers(this) , m_currWeightIndexToDraw(0) , m_currTrainingIndexToDraw(0) , DrawTraining(nullptr) @@ -36,7 +35,7 @@ RbmComponent::RbmComponent (Weights &weights, RbmComponentListener &listener) { //[UserPreSize] - m_pRbm = new Rbm(m_weights, m_layers.data(), this); + m_pRbm = new Rbm(m_weights, batch, this); size_t vNumX = m_weights.getNumVisibleX(); size_t vNumY = m_weights.getNumVisibleY(); size_t hNum = m_weights.getNumHidden(); @@ -202,11 +201,6 @@ void RbmComponent::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& //[/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); @@ -246,12 +240,8 @@ void RbmComponent::redrawReconstruction() void RbmComponent::redrawWeights() { - VectorXd w = m_weights.weights().col(m_currWeightIndexToDraw); - DrawWeights->getData() = w; + DrawWeights->getData() = m_weights.weights().col(m_currWeightIndexToDraw); DrawWeights->DrawData(); - - DrawVars->getData() = m_pRbm->getVariableSigma(); - DrawVars->DrawData(); } void RbmComponent::setDoLearnVariance(bool value) diff --git a/Source/RbmComponent.h b/Source/RbmComponent.h index 1d03223..0dc8d3e 100644 --- a/Source/RbmComponent.h +++ b/Source/RbmComponent.h @@ -23,7 +23,6 @@ //[Headers] -- You can add your own extra header files here -- #include "JuceHeader.h" #include "DrawComponent.h" -#include "LayerArray.hpp" #include "Rbm.hpp" //[/Headers] @@ -34,7 +33,6 @@ public: virtual ~RbmComponentListener() { } - virtual void onLayerSizeChanged(size_t size) = 0; virtual void onRbmEpochTrained(size_t progressPercent) = 0; }; @@ -48,13 +46,12 @@ public: //[/Comments] */ class RbmComponent : public Component, - public LayerArrayListener, public RbmListener, public DrawListener { public: //============================================================================== - RbmComponent (Weights &weights, RbmComponentListener &listener); + RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponentListener &listener); ~RbmComponent(); //============================================================================== @@ -109,8 +106,6 @@ public: DrawReconstruction->getData() = DrawTraining->getData(); DrawReconstruction->DrawData(); - m_layers.add(DrawTraining->getData(), m_weights.getNumVisible()); - m_listener.onLayerSizeChanged(m_layers.getSize()); } void selectWeights(size_t index) @@ -121,12 +116,10 @@ public: void selectTraining(size_t index) { - if (m_layers.getSize() > 0) + if (m_pRbm->getBatch().rows() > 0) { m_currTrainingIndexToDraw = index; - RowVectorXd t = m_layers.getAt(index); - - DrawTraining->getData() = t; + DrawTraining->getData() = m_pRbm->getBatch().row(index); DrawTraining->DrawData(); redrawReconstruction(); @@ -139,27 +132,6 @@ public: DrawTraining->DrawData(); } - void clearTraining() - { - m_layers.clear(); - } - - void loadTraining(const char *pFilename) - { - m_layers.clear(); - 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) @@ -167,6 +139,11 @@ public: m_pRbm->train(numEpochs); } + RowVectorXd const& getTrainingData() + { + return DrawTraining->getData(); + } + private: //[UserVariables] -- You can add your own custom variables in this section. ScopedPointer m_pRbm; @@ -177,10 +154,8 @@ private: ScopedPointer DrawWeights; ScopedPointer DrawVars; ScopedPointer DrawHidden; - 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]