From 6e93c07863e5b7290a1acbc651ba3f61990473f9 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Wed, 29 Jun 2016 20:29:17 +0000 Subject: [PATCH] [RBM] - render lower layer weights as linear combination from top weights using up pass of reconstructions git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@303 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- Source/MainComponent.cpp | 5 ++- Source/RbmComponent.cpp | 89 ++++++++++++++++++++++++++++++++++++---- Source/RbmComponent.h | 8 +++- Source/Weights.hpp | 2 +- 4 files changed, 93 insertions(+), 11 deletions(-) diff --git a/Source/MainComponent.cpp b/Source/MainComponent.cpp index ed24af8..0ac58e3 100644 --- a/Source/MainComponent.cpp +++ b/Source/MainComponent.cpp @@ -815,14 +815,15 @@ void MainComponent::create(juce::String const &projectName) m_weights[id] = nullptr; m_pRbmComponent[id] = nullptr; m_weights[id] = new Weights(m_weights[id-1]->getNumHidden(), 1, numHiddenLabel->getText().getIntValue()); - addAndMakeVisible(m_pRbmComponent[id] = new RbmComponent(*m_weights[id], m_pRbmComponent[id-1]->getHiddenBatch(), *this)); - m_pRbmComponent[id-1]->registerRbm(m_pRbmComponent[id]); + addAndMakeVisible(m_pRbmComponent[id] = new RbmComponent(*m_weights[id], m_pRbmComponent[id-1], *this)); } m_pRbmComponentCurr = m_pRbmComponent[id]; m_weightsCurr = m_weights[id]; m_pRbmComponentCurr->setBounds (16, 140*id+16, 430, 130); m_pRbmComponentCurr->batchchanged(); m_pRbmComponentCurr->redrawReconstruction(); + m_pRbmComponentCurr->redrawWeights(); + m_pRbmComponentCurr->redrawVariances(); if (((id+1) < DBN_SIZE) and shouldAddItem) m_rbmSelect->addItem(String(id+1), id+2); diff --git a/Source/RbmComponent.cpp b/Source/RbmComponent.cpp index 54c020a..f1eb363 100644 --- a/Source/RbmComponent.cpp +++ b/Source/RbmComponent.cpp @@ -23,9 +23,43 @@ //============================================================================== -RbmComponent::RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponentListener &listener) - : Rbm(weights, batch) - , m_weights(weights) +RbmComponent::RbmComponent (Weights &_weights, RbmComponent *pRbmUpper, RbmComponentListener &listener) + : Rbm(_weights, pRbmUpper->getHiddenBatch()) + , m_weights(_weights) + , m_listener(listener) + , m_currWeightIndexToDraw(0) + , m_currTrainingIndexToDraw(0) + , DrawTraining(nullptr) + , DrawReconstruction(nullptr) + , DrawWeights(nullptr) + , DrawHidden(nullptr) +{ + pRbmUpper->registerRbm(this); + size_t vNumX = m_weights.getNumVisibleX(); + size_t vNumY = m_weights.getNumVisibleY(); + size_t hNum = m_weights.getNumHidden(); + + addAndMakeVisible (DrawTraining = new DrawComponent (vNumX, vNumY)); + DrawTraining->setListener(this); + + addAndMakeVisible (DrawReconstruction = new DrawComponent (vNumX, vNumY)); + + + addAndMakeVisible (DrawWeights = new DrawComponent (getTopWeights().getNumVisibleX(), getTopWeights().getNumVisibleY(), 0.5, 0.5)); + addAndMakeVisible (DrawVars = new DrawComponent (vNumX, vNumY)); + addAndMakeVisible (DrawHidden = new DrawComponent (hNum, 1)); + DrawHidden->setListener(this); + redrawWeights(); + redrawReconstruction(); + redrawVariances(); + + setSize (430, 130); + resized(); +} + +RbmComponent::RbmComponent (Weights &_weights, MatrixXd const &batch, RbmComponentListener &listener) + : Rbm(_weights, batch) + , m_weights(_weights) , m_listener(listener) , m_currWeightIndexToDraw(0) , m_currTrainingIndexToDraw(0) @@ -35,7 +69,6 @@ RbmComponent::RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponen , DrawHidden(nullptr) { - //[UserPreSize] size_t vNumX = m_weights.getNumVisibleX(); size_t vNumY = m_weights.getNumVisibleY(); size_t hNum = m_weights.getNumHidden(); @@ -44,14 +77,15 @@ RbmComponent::RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponen DrawTraining->setListener(this); addAndMakeVisible (DrawReconstruction = new DrawComponent (vNumX, vNumY)); - addAndMakeVisible (DrawWeights = new DrawComponent (vNumX, vNumY, 0.5, 0.5)); + + + addAndMakeVisible (DrawWeights = new DrawComponent (getTopWeights().getNumVisibleX(), getTopWeights().getNumVisibleY(), 0.5, 0.5)); addAndMakeVisible (DrawVars = new DrawComponent (vNumX, vNumY)); addAndMakeVisible (DrawHidden = new DrawComponent (hNum, 1)); DrawHidden->setListener(this); redrawWeights(); redrawReconstruction(); redrawVariances(); - //[/UserPreSize] setSize (430, 130); resized(); @@ -252,9 +286,38 @@ void RbmComponent::redrawReconstruction() DrawReconstruction->DrawData(); } +MatrixXd RbmComponent::getAccumulatedWeight(RowVectorXd const &h) +{ + if (upper) + { + RowVectorXd v(m_weights.getNumVisible()); + toVisible(v, h); + return upper->getAccumulatedWeight(v); + } + else + { + MatrixXd w = h * m_weights.weights().transpose(); +// cout << "w=" << endl; +// cout << w << endl; + return w; + } + +} void RbmComponent::redrawWeights() { - DrawWeights->getData() = m_weights.weights().col(m_currWeightIndexToDraw); + if (upper) + { + RowVectorXd h = RowVectorXd::Zero(m_weights.getNumHidden()); + h(m_currWeightIndexToDraw) = 1.0; + DrawWeights->getData() = getAccumulatedWeight(h); + } + else + { + RowVectorXd w = m_weights.weights().col(m_currWeightIndexToDraw); +// cout << "w=" << endl; +// cout << w << endl; + DrawWeights->getData() = w; + } DrawWeights->DrawData(); } @@ -315,6 +378,18 @@ RowVectorXd const& RbmComponent::getTrainingData() return DrawTraining->getData(); } +Weights& RbmComponent::getTopWeights() +{ + if (upper) + { + return upper->getTopWeights(); + } + else + { + return m_weights; + } + +} //[/MiscUserCode] diff --git a/Source/RbmComponent.h b/Source/RbmComponent.h index 94f3c23..46a7f04 100644 --- a/Source/RbmComponent.h +++ b/Source/RbmComponent.h @@ -49,6 +49,8 @@ public: lower = pObj; pObj->upper = this; } + virtual Weights& getTopWeights() = 0; + virtual MatrixXd getAccumulatedWeight(RowVectorXd const &h) = 0; }; class RbmComponentListener @@ -77,7 +79,8 @@ class RbmComponent : public Component { public: //============================================================================== - RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponentListener &listener); + RbmComponent (Weights &_weights, MatrixXd const &batch, RbmComponentListener &listener); + RbmComponent (Weights &_weights, RbmComponent *pRbmUpper, RbmComponentListener &listener); ~RbmComponent(); //============================================================================== @@ -128,6 +131,9 @@ public: } } + Weights& getTopWeights() override; + MatrixXd getAccumulatedWeight(RowVectorXd const &h) override; + private: //[UserVariables] -- You can add your own custom variables in this section. Weights &m_weights; diff --git a/Source/Weights.hpp b/Source/Weights.hpp index c367b4b..09bc5f8 100644 --- a/Source/Weights.hpp +++ b/Source/Weights.hpp @@ -110,7 +110,7 @@ public: return *this; } - MatrixXd& weights() + MatrixXd& weights() { return m_w; }