- pulled LayerArray out of RbmComponent

git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@293 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2016-06-16 19:16:33 +00:00
parent 4dccddd50f
commit ff88a62e73
5 changed files with 56 additions and 64 deletions
+14 -13
View File
@@ -38,7 +38,10 @@ void mylog(const char* format, ...)
//============================================================================== //==============================================================================
MainComponent::MainComponent () MainComponent::MainComponent ()
: Thread("RBM"), : Thread("RBM"),
m_pRbmComponent(nullptr) m_pRbmComponent(nullptr),
m_layers(this)
{ {
addAndMakeVisible (trainButton = new TextButton ("Train button")); addAndMakeVisible (trainButton = new TextButton ("Train button"));
trainButton->setButtonText (TRANS("Train")); trainButton->setButtonText (TRANS("Train"));
@@ -458,6 +461,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
else if (buttonThatWasClicked == addButton) else if (buttonThatWasClicked == addButton)
{ {
//[UserButtonCode_addButton] -- add your button handler code here.. //[UserButtonCode_addButton] -- add your button handler code here..
m_layers.add(m_pRbmComponent->getTrainingData(), m_weights->getNumVisible());
m_pRbmComponent->addFromTraining(); m_pRbmComponent->addFromTraining();
//[/UserButtonCode_addButton] //[/UserButtonCode_addButton]
} }
@@ -471,8 +475,8 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
{ {
//[UserButtonCode_ShakeButton] -- add your button handler code here.. //[UserButtonCode_ShakeButton] -- add your button handler code here..
m_weights->shuffle(weightInitLabel->getText().getFloatValue()); m_weights->shuffle(weightInitLabel->getText().getFloatValue());
m_pRbmComponent->redrawReconstruction();
m_pRbmComponent->redrawWeights(); m_pRbmComponent->redrawWeights();
m_pRbmComponent->redrawReconstruction();
//[/UserButtonCode_ShakeButton] //[/UserButtonCode_ShakeButton]
} }
else if (buttonThatWasClicked == testButton) else if (buttonThatWasClicked == testButton)
@@ -503,25 +507,25 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
else if (buttonThatWasClicked == loadTrainingButton) else if (buttonThatWasClicked == loadTrainingButton)
{ {
//[UserButtonCode_loadTrainingButton] -- add your button handler code here.. //[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] //[/UserButtonCode_loadTrainingButton]
} }
else if (buttonThatWasClicked == saveTrainingButton) else if (buttonThatWasClicked == saveTrainingButton)
{ {
//[UserButtonCode_saveTrainingButton] -- add your button handler code here.. //[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] //[/UserButtonCode_saveTrainingButton]
} }
else if (buttonThatWasClicked == clearTrainingButton) else if (buttonThatWasClicked == clearTrainingButton)
{ {
//[UserButtonCode_clearTrainingButton] -- add your button handler code here.. //[UserButtonCode_clearTrainingButton] -- add your button handler code here..
m_pRbmComponent->clearTraining(); clearTraining();
//[/UserButtonCode_clearTrainingButton] //[/UserButtonCode_clearTrainingButton]
} }
else if (buttonThatWasClicked == removeTrainingButton) else if (buttonThatWasClicked == removeTrainingButton)
{ {
//[UserButtonCode_removeTrainingButton] -- add your button handler code here.. //[UserButtonCode_removeTrainingButton] -- add your button handler code here..
m_pRbmComponent->removeTrainingAt((int)patterSlider->getValue()); removeTrainingAt((int)patterSlider->getValue());
//[/UserButtonCode_removeTrainingButton] //[/UserButtonCode_removeTrainingButton]
} }
else if (buttonThatWasClicked == reconstructEquButton) else if (buttonThatWasClicked == reconstructEquButton)
@@ -769,6 +773,7 @@ void MainComponent::create(juce::String const &projectName)
if (!projectName.isEmpty()) if (!projectName.isEmpty())
{ {
m_weights = new Weights((String(projectName + String(".weights.dat"))).toUTF8()); m_weights = new Weights((String(projectName + String(".weights.dat"))).toUTF8());
loadTraining((String(projectName + String(".trainingStates.dat"))).toUTF8());
} }
else else
{ {
@@ -783,11 +788,7 @@ void MainComponent::create(juce::String const &projectName)
numVisibleYLabel->setText(String(vNumY), dontSendNotification ); numVisibleYLabel->setText(String(vNumY), dontSendNotification );
numHiddenLabel->setText(String(hNum), dontSendNotification ); numHiddenLabel->setText(String(hNum), dontSendNotification );
addAndMakeVisible(m_pRbmComponent = new RbmComponent(*m_weights, *this)); addAndMakeVisible(m_pRbmComponent = new RbmComponent(*m_weights, m_layers.data(), *this));
if (!projectName.isEmpty())
{
m_pRbmComponent->loadTraining((String(projectName + String(".trainingStates.dat"))).toUTF8());
}
m_pRbmComponent->setBounds (16, 16, 430, 130); m_pRbmComponent->setBounds (16, 16, 430, 130);
m_pRbmComponent->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState()); m_pRbmComponent->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState());
@@ -830,9 +831,9 @@ void MainComponent::run()
// trainButton->setEnabled(true); // 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) void MainComponent::onRbmEpochTrained(size_t progressPercent)
+23 -1
View File
@@ -39,6 +39,7 @@
*/ */
class MainComponent : public Component, class MainComponent : public Component,
public RbmComponentListener, public RbmComponentListener,
public LayerArrayListener,
public Thread, public Thread,
public ButtonListener, public ButtonListener,
public SliderListener, public SliderListener,
@@ -51,7 +52,7 @@ public:
//============================================================================== //==============================================================================
//[UserMethods] -- You can add your own custom methods in this section. //[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; void onRbmEpochTrained(size_t progressPercent) override;
//[/UserMethods] //[/UserMethods]
@@ -75,6 +76,7 @@ private:
//[UserVariables] -- You can add your own custom variables in this section. //[UserVariables] -- You can add your own custom variables in this section.
ScopedPointer<Weights> m_weights; ScopedPointer<Weights> m_weights;
ScopedPointer<RbmComponent> m_pRbmComponent; ScopedPointer<RbmComponent> m_pRbmComponent;
LayerArray m_layers;
void load(); void load();
void save(); void save();
void create(juce::String const &projectName=""); void create(juce::String const &projectName="");
@@ -82,6 +84,26 @@ private:
const juce::String& getBaseDir(); const juce::String& getBaseDir();
void run(); void run();
String m_baseDir; 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] //[/UserVariables]
//============================================================================== //==============================================================================
+8 -4
View File
@@ -238,7 +238,6 @@ public:
MatrixXd sumBiasH(1, m_w.getNumHidden()); MatrixXd sumBiasH(1, m_w.getNumHidden());
MatrixXd sumWeights(m_w.getNumVisible(), 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 deltaBiasV(MatrixXd::Zero(1, m_w.getNumVisible()));
MatrixXd deltaBiasH(MatrixXd::Zero(1, m_w.getNumHidden())); MatrixXd deltaBiasH(MatrixXd::Zero(1, m_w.getNumHidden()));
MatrixXd deltaWeights(MatrixXd::Zero(m_w.getNumVisible(), 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); toHiddenBatch(m_h, m_v);
probsLogistic(m_h); probsLogistic(m_h);
} }
@@ -329,8 +328,8 @@ public:
if (m_doSparse) if (m_doSparse)
{ {
m_h = m_v * m_w.weights(); // Create hidden representation given v
m_h += m_w.hiddenBias().replicate(batchSize, 1); toHiddenBatch(m_h, m_v);
probsLogistic(m_h); probsLogistic(m_h);
sumBiasH.fill(m_sparsity); sumBiasH.fill(m_sparsity);
@@ -515,6 +514,11 @@ public:
return m_v; return m_v;
} }
MatrixXd const& getBatch()
{
return m_batch;
}
private: private:
Weights &m_w; Weights &m_w;
MatrixXd const &m_batch; MatrixXd const &m_batch;
+3 -13
View File
@@ -23,10 +23,9 @@
//============================================================================== //==============================================================================
RbmComponent::RbmComponent (Weights &weights, RbmComponentListener &listener) RbmComponent::RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponentListener &listener)
: m_weights(weights) : m_weights(weights)
, m_listener(listener) , m_listener(listener)
, m_layers(this)
, m_currWeightIndexToDraw(0) , m_currWeightIndexToDraw(0)
, m_currTrainingIndexToDraw(0) , m_currTrainingIndexToDraw(0)
, DrawTraining(nullptr) , DrawTraining(nullptr)
@@ -36,7 +35,7 @@ RbmComponent::RbmComponent (Weights &weights, RbmComponentListener &listener)
{ {
//[UserPreSize] //[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 vNumX = m_weights.getNumVisibleX();
size_t vNumY = m_weights.getNumVisibleY(); size_t vNumY = m_weights.getNumVisibleY();
size_t hNum = m_weights.getNumHidden(); size_t hNum = m_weights.getNumHidden();
@@ -202,11 +201,6 @@ void RbmComponent::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails&
//[/UserCode_mouseWheelMove] //[/UserCode_mouseWheelMove]
} }
void RbmComponent::onChanged(const LayerArray &obj)
{
m_listener.onLayerSizeChanged((size_t)obj.getSize());
}
void RbmComponent::onEpochTrained(const Rbm &obj) void RbmComponent::onEpochTrained(const Rbm &obj)
{ {
// mylog("Training %f %%\n", obj.getProgress()*100); // mylog("Training %f %%\n", obj.getProgress()*100);
@@ -246,12 +240,8 @@ void RbmComponent::redrawReconstruction()
void RbmComponent::redrawWeights() void RbmComponent::redrawWeights()
{ {
VectorXd w = m_weights.weights().col(m_currWeightIndexToDraw); DrawWeights->getData() = m_weights.weights().col(m_currWeightIndexToDraw);
DrawWeights->getData() = w;
DrawWeights->DrawData(); DrawWeights->DrawData();
DrawVars->getData() = m_pRbm->getVariableSigma();
DrawVars->DrawData();
} }
void RbmComponent::setDoLearnVariance(bool value) void RbmComponent::setDoLearnVariance(bool value)
+8 -33
View File
@@ -23,7 +23,6 @@
//[Headers] -- You can add your own extra header files here -- //[Headers] -- You can add your own extra header files here --
#include "JuceHeader.h" #include "JuceHeader.h"
#include "DrawComponent.h" #include "DrawComponent.h"
#include "LayerArray.hpp"
#include "Rbm.hpp" #include "Rbm.hpp"
//[/Headers] //[/Headers]
@@ -34,7 +33,6 @@ public:
virtual ~RbmComponentListener() virtual ~RbmComponentListener()
{ {
} }
virtual void onLayerSizeChanged(size_t size) = 0;
virtual void onRbmEpochTrained(size_t progressPercent) = 0; virtual void onRbmEpochTrained(size_t progressPercent) = 0;
}; };
@@ -48,13 +46,12 @@ public:
//[/Comments] //[/Comments]
*/ */
class RbmComponent : public Component, class RbmComponent : public Component,
public LayerArrayListener,
public RbmListener, public RbmListener,
public DrawListener public DrawListener
{ {
public: public:
//============================================================================== //==============================================================================
RbmComponent (Weights &weights, RbmComponentListener &listener); RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponentListener &listener);
~RbmComponent(); ~RbmComponent();
//============================================================================== //==============================================================================
@@ -109,8 +106,6 @@ public:
DrawReconstruction->getData() = DrawTraining->getData(); DrawReconstruction->getData() = DrawTraining->getData();
DrawReconstruction->DrawData(); DrawReconstruction->DrawData();
m_layers.add(DrawTraining->getData(), m_weights.getNumVisible());
m_listener.onLayerSizeChanged(m_layers.getSize());
} }
void selectWeights(size_t index) void selectWeights(size_t index)
@@ -121,12 +116,10 @@ public:
void selectTraining(size_t index) void selectTraining(size_t index)
{ {
if (m_layers.getSize() > 0) if (m_pRbm->getBatch().rows() > 0)
{ {
m_currTrainingIndexToDraw = index; m_currTrainingIndexToDraw = index;
RowVectorXd t = m_layers.getAt(index); DrawTraining->getData() = m_pRbm->getBatch().row(index);
DrawTraining->getData() = t;
DrawTraining->DrawData(); DrawTraining->DrawData();
redrawReconstruction(); redrawReconstruction();
@@ -139,27 +132,6 @@ public:
DrawTraining->DrawData(); 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 redrawWeights();
void redrawReconstruction(); void redrawReconstruction();
void train(size_t numEpochs) void train(size_t numEpochs)
@@ -167,6 +139,11 @@ public:
m_pRbm->train(numEpochs); m_pRbm->train(numEpochs);
} }
RowVectorXd const& getTrainingData()
{
return DrawTraining->getData();
}
private: private:
//[UserVariables] -- You can add your own custom variables in this section. //[UserVariables] -- You can add your own custom variables in this section.
ScopedPointer<Rbm> m_pRbm; ScopedPointer<Rbm> m_pRbm;
@@ -177,10 +154,8 @@ private:
ScopedPointer<DrawComponent> DrawWeights; ScopedPointer<DrawComponent> DrawWeights;
ScopedPointer<DrawComponent> DrawVars; ScopedPointer<DrawComponent> DrawVars;
ScopedPointer<DrawComponent> DrawHidden; ScopedPointer<DrawComponent> DrawHidden;
LayerArray m_layers;
size_t m_currWeightIndexToDraw; size_t m_currWeightIndexToDraw;
size_t m_currTrainingIndexToDraw; size_t m_currTrainingIndexToDraw;
void onChanged(const LayerArray &obj) override;
void onEpochTrained(const Rbm &obj) override; void onEpochTrained(const Rbm &obj) override;
void onDraw(DrawComponent &obj) override; void onDraw(DrawComponent &obj) override;
//[/UserVariables] //[/UserVariables]