- removed classes HiddenLayer.hpp and VisibleLayer.hpp
- fixed warnings
- RbmComponent inherits Rbm
- improved Rbm::train
- use gaussion weight initialization

git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@297 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2016-06-19 19:52:41 +00:00
parent 1705287e37
commit 9d00654932
9 changed files with 303 additions and 504 deletions
-37
View File
@@ -1,37 +0,0 @@
/*
* HiddenLayer.hpp
*
* Created on: 21.09.2014
* Author: jens
*/
#ifndef HIDDENLAYER_HPP_
#define HIDDENLAYER_HPP_
#include "Layer.hpp"
#include <cmath>
class HiddenLayer : public Layer
{
public:
HiddenLayer(uint32_t numUnits = 0, const RowVectorXd *pStatesInit = nullptr)
: Layer(numUnits, pStatesInit)
{
}
virtual ~HiddenLayer()
{
}
private:
double accum(Layer &layer, Weights &weights, uint32_t index)
{
double sum = ((Weights&)weights).hiddenBias()[index];
sum += layer.states() * ((Weights&)weights).weights().col(index);
return sum;
}
};
#endif /* HIDDENLAYER_HPP_ */
+1 -1
View File
@@ -138,7 +138,7 @@ public:
{
float v;
for (j=0; j < data.size(); j++)
for (j=0; j < data.cols(); j++)
{
fscanf(pFile, "%f", &v);
data(j) = v;
+157 -173
View File
@@ -51,20 +51,18 @@ MainComponent::MainComponent ()
addButton->addListener (this);
addAndMakeVisible (patterSlider = new Slider ("Pattern slider"));
patterSlider->setTooltip (TRANS("Training index"));
patterSlider->setRange (0, 0, 1);
patterSlider->setSliderStyle (Slider::LinearBar);
patterSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
patterSlider->addListener (this);
addAndMakeVisible (reconstructButton = new TextButton ("Reconstruct button"));
reconstructButton->setButtonText (TRANS("Reconstruct"));
reconstructButton->addListener (this);
addAndMakeVisible (ShakeButton = new TextButton ("Shake button"));
ShakeButton->setButtonText (TRANS("Shake"));
ShakeButton->addListener (this);
addAndMakeVisible (WeightsSlider = new Slider ("Weights slider"));
WeightsSlider->setTooltip (TRANS("Weight index"));
WeightsSlider->setRange (0, 0, 1);
WeightsSlider->setSliderStyle (Slider::LinearBar);
WeightsSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
@@ -88,12 +86,9 @@ MainComponent::MainComponent ()
learningRateLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
learningRateLabel->addListener (this);
addAndMakeVisible (testButton = new TextButton ("Test button"));
testButton->setButtonText (TRANS("Test"));
testButton->addListener (this);
addAndMakeVisible (numVisibleLabel = new Label ("Num Visible label",
TRANS("16")));
numVisibleLabel->setTooltip (TRANS("Number of visible units X"));
numVisibleLabel->setFont (Font (15.00f, Font::plain));
numVisibleLabel->setJustificationType (Justification::centred);
numVisibleLabel->setEditable (true, true, false);
@@ -103,6 +98,7 @@ MainComponent::MainComponent ()
addAndMakeVisible (numHiddenLabel = new Label ("Num Hidden label",
TRANS("64")));
numHiddenLabel->setTooltip (TRANS("Number of hidden units"));
numHiddenLabel->setFont (Font (15.00f, Font::plain));
numHiddenLabel->setJustificationType (Justification::centred);
numHiddenLabel->setEditable (true, true, false);
@@ -133,6 +129,7 @@ MainComponent::MainComponent ()
addAndMakeVisible (numVisibleYLabel = new Label ("Num Visible Y label",
TRANS("16")));
numVisibleYLabel->setTooltip (TRANS("Number of visible units Y"));
numVisibleYLabel->setFont (Font (15.00f, Font::plain));
numVisibleYLabel->setJustificationType (Justification::centred);
numVisibleYLabel->setEditable (true, true, false);
@@ -157,22 +154,21 @@ MainComponent::MainComponent ()
removeTrainingButton->addListener (this);
addAndMakeVisible (numGibbsSlider = new Slider ("Num. Gibbs slider"));
numGibbsSlider->setTooltip (TRANS("Number of gibbs samples"));
numGibbsSlider->setRange (1, 101, 1);
numGibbsSlider->setSliderStyle (Slider::LinearBar);
numGibbsSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
numGibbsSlider->addListener (this);
addAndMakeVisible (reconstructEquButton = new TextButton ("Reconstruct Equilibrium button"));
reconstructEquButton->setButtonText (TRANS("Reconst Equ."));
reconstructEquButton->addListener (this);
addAndMakeVisible (rbmDoRaoBlackwellToggleButton = new ToggleButton ("rbmDoRaoBlackwell toggle button"));
rbmDoRaoBlackwellToggleButton->setTooltip (TRANS("Don\'t sample hidden during learning"));
rbmDoRaoBlackwellToggleButton->setButtonText (TRANS("Rao-Blackwell"));
rbmDoRaoBlackwellToggleButton->addListener (this);
addAndMakeVisible (rbmReduceVarianceToggleButton = new ToggleButton ("rbmReduceVariance toggle button"));
rbmReduceVarianceToggleButton->setButtonText (TRANS("Reduce Variance"));
rbmReduceVarianceToggleButton->addListener (this);
addAndMakeVisible (rbmDoSampleVisibleToggleButton = new ToggleButton ("rbmDoSampleVisible toggle button"));
rbmDoSampleVisibleToggleButton->setTooltip (TRANS("Sample visible during learning\n"));
rbmDoSampleVisibleToggleButton->setButtonText (TRANS("Sample Visible"));
rbmDoSampleVisibleToggleButton->addListener (this);
addAndMakeVisible (lambdaLabel = new Label ("Lambda label",
TRANS("1.0")));
@@ -219,7 +215,7 @@ MainComponent::MainComponent ()
sigmaDecayLabel->addListener (this);
addAndMakeVisible (weightDecayLabel = new Label ("WeightDecay label",
TRANS("0.0")));
TRANS("0")));
weightDecayLabel->setFont (Font (15.00f, Font::plain));
weightDecayLabel->setJustificationType (Justification::centred);
weightDecayLabel->setEditable (true, true, false);
@@ -228,6 +224,7 @@ MainComponent::MainComponent ()
weightDecayLabel->addListener (this);
addAndMakeVisible (m_progressBarSlider = new Slider ("progressBar slider"));
m_progressBarSlider->setTooltip (TRANS("Progress"));
m_progressBarSlider->setRange (0, 100, 1);
m_progressBarSlider->setSliderStyle (Slider::LinearBar);
m_progressBarSlider->setTextBoxStyle (Slider::TextBoxLeft, true, 80, 20);
@@ -252,7 +249,7 @@ MainComponent::MainComponent ()
sparsityLearningRateLabel->addListener (this);
addAndMakeVisible (weightInitLabel = new Label ("WeightInit label",
TRANS("0.001")));
TRANS("0.01")));
weightInitLabel->setFont (Font (15.00f, Font::plain));
weightInitLabel->setJustificationType (Justification::centred);
weightInitLabel->setEditable (true, true, false);
@@ -275,6 +272,11 @@ MainComponent::MainComponent ()
m_rbmSelect->setTextWhenNoChoicesAvailable (TRANS("(no choices)"));
m_rbmSelect->addListener (this);
addAndMakeVisible (rbmDoSampleBatch = new ToggleButton ("rbmDoSampleBatch button"));
rbmDoSampleBatch->setTooltip (TRANS("Sample training data during learning\n"));
rbmDoSampleBatch->setButtonText (TRANS("Sample training"));
rbmDoSampleBatch->addListener (this);
//[UserPreSize]
memset(m_pRbmComponent, 0, sizeof(m_pRbmComponent));
@@ -284,7 +286,7 @@ MainComponent::MainComponent ()
m_progressBarSlider->setValue(100);
//[/UserPreSize]
setSize (1000, 600);
setSize (1200, 600);
//[Constructor] You can add your own custom stuff here..
@@ -299,12 +301,10 @@ MainComponent::~MainComponent()
trainButton = nullptr;
addButton = nullptr;
patterSlider = nullptr;
reconstructButton = nullptr;
ShakeButton = nullptr;
WeightsSlider = nullptr;
numEpochslabel = nullptr;
learningRateLabel = nullptr;
testButton = nullptr;
numVisibleLabel = nullptr;
numHiddenLabel = nullptr;
createButton = nullptr;
@@ -317,9 +317,8 @@ MainComponent::~MainComponent()
clearTrainingButton = nullptr;
removeTrainingButton = nullptr;
numGibbsSlider = nullptr;
reconstructEquButton = nullptr;
rbmDoRaoBlackwellToggleButton = nullptr;
rbmReduceVarianceToggleButton = nullptr;
rbmDoSampleVisibleToggleButton = nullptr;
lambdaLabel = nullptr;
sigmaLabel = nullptr;
rbmUseVisibleGaussianToggleButton = nullptr;
@@ -334,6 +333,7 @@ MainComponent::~MainComponent()
rbmLearnVarianceButton = nullptr;
rbmNormalizeDataToggleButton = nullptr;
m_rbmSelect = nullptr;
rbmDoSampleBatch = nullptr;
//[Destructor]. You can add your own custom destruction code here..
@@ -352,61 +352,61 @@ void MainComponent::paint (Graphics& g)
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Sigma"),
500, 138, 80, 14,
560, 138, 80, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Lambda"),
588, 138, 80, 14,
648, 138, 80, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Num. epochs"),
491, 186, 91, 14,
551, 186, 91, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Alpha"),
588, 186, 80, 14,
648, 186, 80, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Sparsity"),
676, 138, 80, 14,
736, 138, 80, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Sigma decay"),
764, 138, 96, 14,
824, 138, 96, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Weight decay"),
764, 186, 96, 14,
824, 186, 96, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Momentum"),
676, 186, 80, 14,
736, 186, 80, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Sparsity Alpha"),
876, 138, 96, 14,
936, 138, 96, 14,
Justification::centred, true);
g.setColour (Colours::black);
g.setFont (Font (15.00f, Font::plain));
g.drawText (TRANS("Weight init"),
876, 186, 96, 14,
936, 186, 96, 14,
Justification::centred, true);
//[UserPaint] Add your own custom painting code here..
@@ -415,44 +415,42 @@ void MainComponent::paint (Graphics& g)
void MainComponent::resized()
{
trainButton->setBounds (592, 240, 72, 24);
addButton->setBounds (496, 20, 72, 24);
patterSlider->setBounds (696, 280, 184, 24);
reconstructButton->setBounds (496, 240, 72, 24);
ShakeButton->setBounds (592, 280, 72, 24);
WeightsSlider->setBounds (696, 320, 184, 24);
numEpochslabel->setBounds (504, 200, 72, 24);
learningRateLabel->setBounds (592, 200, 72, 24);
testButton->setBounds (592, 20, 72, 24);
numVisibleLabel->setBounds (736, 368, 48, 24);
numHiddenLabel->setBounds (840, 368, 48, 24);
createButton->setBounds (660, 368, 72, 24);
projectNameLabel->setBounds (892, 368, 96, 24);
loadButton->setBounds (496, 368, 72, 24);
saveButton->setBounds (576, 368, 72, 24);
numVisibleYLabel->setBounds (788, 368, 48, 24);
loadTrainingButton->setBounds (496, 404, 72, 24);
saveTrainingButton->setBounds (576, 404, 72, 24);
clearTrainingButton->setBounds (736, 404, 72, 24);
removeTrainingButton->setBounds (656, 404, 72, 24);
numGibbsSlider->setBounds (696, 240, 184, 24);
reconstructEquButton->setBounds (496, 280, 72, 24);
rbmDoRaoBlackwellToggleButton->setBounds (496, 60, 112, 24);
rbmReduceVarianceToggleButton->setBounds (640, 60, 128, 24);
lambdaLabel->setBounds (592, 152, 72, 24);
sigmaLabel->setBounds (504, 152, 72, 24);
rbmUseVisibleGaussianToggleButton->setBounds (640, 92, 160, 24);
rbmDoSparseToggleButton->setBounds (496, 92, 128, 24);
sparsityLabel->setBounds (680, 152, 72, 24);
sigmaDecayLabel->setBounds (776, 152, 72, 24);
weightDecayLabel->setBounds (776, 200, 72, 24);
m_progressBarSlider->setBounds (688, 20, 184, 24);
momentumLabel->setBounds (680, 200, 72, 24);
sparsityLearningRateLabel->setBounds (888, 152, 72, 24);
weightInitLabel->setBounds (888, 200, 72, 24);
rbmLearnVarianceButton->setBounds (808, 60, 128, 24);
rbmNormalizeDataToggleButton->setBounds (808, 92, 160, 24);
m_rbmSelect->setBounds (844, 404, 62, 24);
trainButton->setBounds (648, 20, 72, 24);
addButton->setBounds (556, 20, 72, 24);
patterSlider->setBounds (896, 280, 220, 24);
ShakeButton->setBounds (740, 20, 72, 24);
WeightsSlider->setBounds (896, 320, 220, 24);
numEpochslabel->setBounds (564, 200, 72, 24);
learningRateLabel->setBounds (652, 200, 72, 24);
numVisibleLabel->setBounds (656, 248, 48, 24);
numHiddenLabel->setBounds (780, 248, 48, 24);
createButton->setBounds (728, 280, 72, 24);
projectNameLabel->setBounds (552, 248, 96, 24);
loadButton->setBounds (564, 280, 72, 24);
saveButton->setBounds (644, 280, 72, 24);
numVisibleYLabel->setBounds (716, 248, 48, 24);
loadTrainingButton->setBounds (564, 316, 72, 24);
saveTrainingButton->setBounds (644, 316, 72, 24);
clearTrainingButton->setBounds (804, 316, 72, 24);
removeTrainingButton->setBounds (724, 316, 72, 24);
numGibbsSlider->setBounds (896, 240, 220, 24);
rbmDoRaoBlackwellToggleButton->setBounds (556, 60, 112, 24);
rbmDoSampleVisibleToggleButton->setBounds (680, 60, 128, 24);
lambdaLabel->setBounds (652, 152, 72, 24);
sigmaLabel->setBounds (564, 152, 72, 24);
rbmUseVisibleGaussianToggleButton->setBounds (680, 92, 156, 24);
rbmDoSparseToggleButton->setBounds (556, 92, 96, 24);
sparsityLabel->setBounds (740, 152, 72, 24);
sigmaDecayLabel->setBounds (836, 152, 72, 24);
weightDecayLabel->setBounds (836, 200, 72, 24);
m_progressBarSlider->setBounds (844, 20, 220, 24);
momentumLabel->setBounds (740, 200, 72, 24);
sparsityLearningRateLabel->setBounds (948, 152, 72, 24);
weightInitLabel->setBounds (948, 200, 72, 24);
rbmLearnVarianceButton->setBounds (976, 92, 128, 24);
rbmNormalizeDataToggleButton->setBounds (844, 92, 124, 24);
m_rbmSelect->setBounds (812, 280, 62, 24);
rbmDoSampleBatch->setBounds (844, 60, 128, 24);
//[UserResized] Add your own custom resize handling here..
//[/UserResized]
}
@@ -475,12 +473,6 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
m_pRbmComponent[0]->batchchanged();
//[/UserButtonCode_addButton]
}
else if (buttonThatWasClicked == reconstructButton)
{
//[UserButtonCode_reconstructButton] -- add your button handler code here..
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_reconstructButton]
}
else if (buttonThatWasClicked == ShakeButton)
{
//[UserButtonCode_ShakeButton] -- add your button handler code here..
@@ -490,13 +482,6 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_ShakeButton]
}
else if (buttonThatWasClicked == testButton)
{
//[UserButtonCode_testButton] -- add your button handler code here..
m_pRbmComponentCurr->copyReconstructionToTraining();
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_testButton]
}
else if (buttonThatWasClicked == createButton)
{
//[UserButtonCode_createButton] -- add your button handler code here..
@@ -508,7 +493,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
//[UserButtonCode_loadButton] -- add your button handler code here..
m_rbmSelect->setSelectedId(1, sendNotification);
create(getBaseDir());
//[/UserButtonCode_loadButton]
//[/UserButtonCode_loadButton]
}
else if (buttonThatWasClicked == saveButton)
{
@@ -543,23 +528,17 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
m_pRbmComponent[0]->batchchanged();
//[/UserButtonCode_removeTrainingButton]
}
else if (buttonThatWasClicked == reconstructEquButton)
{
//[UserButtonCode_reconstructEquButton] -- add your button handler code here..
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_reconstructEquButton]
}
else if (buttonThatWasClicked == rbmDoRaoBlackwellToggleButton)
{
//[UserButtonCode_rbmDoRaoBlackwellToggleButton] -- add your button handler code here..
m_pRbmComponentCurr->setDoRaoBlackwell(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmDoRaoBlackwellToggleButton]
}
else if (buttonThatWasClicked == rbmReduceVarianceToggleButton)
else if (buttonThatWasClicked == rbmDoSampleVisibleToggleButton)
{
//[UserButtonCode_rbmReduceVarianceToggleButton] -- add your button handler code here..
m_pRbmComponentCurr->setUseProbsForHiddenReconstruction(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmReduceVarianceToggleButton]
//[UserButtonCode_rbmDoSampleVisibleToggleButton] -- add your button handler code here..
m_pRbmComponentCurr->setDoSampleVisible(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmDoSampleVisibleToggleButton]
}
else if (buttonThatWasClicked == rbmUseVisibleGaussianToggleButton)
{
@@ -580,7 +559,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
m_pRbmComponentCurr->setDoLearnVariance(buttonThatWasClicked->getToggleState());
if (!buttonThatWasClicked->getToggleState())
{
m_pRbmComponentCurr->setSigma(sigmaLabel->getText().getFloatValue());
m_pRbmComponentCurr->setConstantSigma(sigmaLabel->getText().getFloatValue());
}
//[/UserButtonCode_rbmLearnVarianceButton]
}
@@ -590,6 +569,12 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
m_pRbmComponentCurr->setNormalizeData(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmNormalizeDataToggleButton]
}
else if (buttonThatWasClicked == rbmDoSampleBatch)
{
//[UserButtonCode_rbmDoSampleBatch] -- add your button handler code here..
m_pRbmComponentCurr->setDoSampleBatch(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmDoSampleBatch]
}
//[UserbuttonClicked_Post]
//[/UserbuttonClicked_Post]
@@ -674,7 +659,7 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged)
else if (labelThatHasChanged == sigmaLabel)
{
//[UserLabelCode_sigmaLabel] -- add your label text handling code here..
m_pRbmComponentCurr->setSigma(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->setConstantSigma(labelThatHasChanged->getText().getFloatValue());
//[/UserLabelCode_sigmaLabel]
}
else if (labelThatHasChanged == sparsityLabel)
@@ -878,10 +863,12 @@ void MainComponent::updateControls()
{
rbmDoRaoBlackwellToggleButton->setToggleState(m_pRbmComponentCurr->params().m_doRaoBlackwell, dontSendNotification);
rbmReduceVarianceToggleButton->setToggleState(m_pRbmComponentCurr->params().m_useProbsForHiddenReconstruction, dontSendNotification);
rbmDoSampleVisibleToggleButton->setToggleState(m_pRbmComponentCurr->params().m_doSampleVisible, dontSendNotification);
rbmDoSampleBatch->setToggleState(m_pRbmComponentCurr->params().m_doSampleBatch, dontSendNotification);
rbmUseVisibleGaussianToggleButton->setToggleState(m_pRbmComponentCurr->params().m_useVisibleGaussian, dontSendNotification);
rbmDoSparseToggleButton->setToggleState(m_pRbmComponentCurr->params().m_doSparse, dontSendNotification);
rbmLearnVarianceButton->setToggleState(m_pRbmComponentCurr->params().m_doLearnVariance, dontSendNotification);
rbmDoSampleBatch->setToggleState(m_pRbmComponentCurr->params().m_doSampleBatch, dontSendNotification);
lambdaLabel->setText(String(m_pRbmComponentCurr->params().m_lambda), dontSendNotification);
sigmaDecayLabel->setText(String(m_pRbmComponentCurr->params().m_sigmaDecay), dontSendNotification);
@@ -918,7 +905,7 @@ BEGIN_JUCER_METADATA
parentClasses="public Component, public RbmComponentListener, public LayerArrayListener, public Thread"
constructorParams="" variableInitialisers="Thread(&quot;RBM&quot;),&#10;m_pRbmComponentCurr(nullptr),&#10;m_weightsCurr(nullptr),&#10;m_layers(this)"
snapPixels="4" snapActive="1" snapShown="1" overlayOpacity="0.330"
fixedSize="1" initialWidth="1000" initialHeight="600">
fixedSize="1" initialWidth="1200" initialHeight="600">
<METHODS>
<METHOD name="mouseMove (const MouseEvent&amp; e)"/>
<METHOD name="mouseEnter (const MouseEvent&amp; e)"/>
@@ -930,176 +917,173 @@ BEGIN_JUCER_METADATA
<METHOD name="mouseWheelMove (const MouseEvent&amp; e, const MouseWheelDetails&amp; wheel)"/>
</METHODS>
<BACKGROUND backgroundColour="ffffffff">
<TEXT pos="500 138 80 14" fill="solid: ff000000" hasStroke="0" text="Sigma"
<TEXT pos="560 138 80 14" fill="solid: ff000000" hasStroke="0" text="Sigma"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="588 138 80 14" fill="solid: ff000000" hasStroke="0" text="Lambda"
<TEXT pos="648 138 80 14" fill="solid: ff000000" hasStroke="0" text="Lambda"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="491 186 91 14" fill="solid: ff000000" hasStroke="0" text="Num. epochs"
<TEXT pos="551 186 91 14" fill="solid: ff000000" hasStroke="0" text="Num. epochs"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="588 186 80 14" fill="solid: ff000000" hasStroke="0" text="Alpha"
<TEXT pos="648 186 80 14" fill="solid: ff000000" hasStroke="0" text="Alpha"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="676 138 80 14" fill="solid: ff000000" hasStroke="0" text="Sparsity"
<TEXT pos="736 138 80 14" fill="solid: ff000000" hasStroke="0" text="Sparsity"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="764 138 96 14" fill="solid: ff000000" hasStroke="0" text="Sigma decay"
<TEXT pos="824 138 96 14" fill="solid: ff000000" hasStroke="0" text="Sigma decay"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="764 186 96 14" fill="solid: ff000000" hasStroke="0" text="Weight decay"
<TEXT pos="824 186 96 14" fill="solid: ff000000" hasStroke="0" text="Weight decay"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="676 186 80 14" fill="solid: ff000000" hasStroke="0" text="Momentum"
<TEXT pos="736 186 80 14" fill="solid: ff000000" hasStroke="0" text="Momentum"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="876 138 96 14" fill="solid: ff000000" hasStroke="0" text="Sparsity Alpha"
<TEXT pos="936 138 96 14" fill="solid: ff000000" hasStroke="0" text="Sparsity Alpha"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
<TEXT pos="876 186 96 14" fill="solid: ff000000" hasStroke="0" text="Weight init"
<TEXT pos="936 186 96 14" fill="solid: ff000000" hasStroke="0" text="Weight init"
fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/>
</BACKGROUND>
<TEXTBUTTON name="Train button" id="7909d74b5522987c" memberName="trainButton"
virtualName="" explicitFocusOrder="0" pos="592 240 72 24" buttonText="Train"
virtualName="" explicitFocusOrder="0" pos="648 20 72 24" buttonText="Train"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<TEXTBUTTON name="Add button" id="4609f7b526ec1aef" memberName="addButton"
virtualName="" explicitFocusOrder="0" pos="496 20 72 24" buttonText="Add"
virtualName="" explicitFocusOrder="0" pos="556 20 72 24" buttonText="Add"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<SLIDER name="Pattern slider" id="c3e0a2c816db81d1" memberName="patterSlider"
virtualName="" explicitFocusOrder="0" pos="696 280 184 24" min="0"
max="0" int="1" style="LinearBar" textBoxPos="TextBoxLeft" textBoxEditable="1"
textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<TEXTBUTTON name="Reconstruct button" id="c1901d121a5819d6" memberName="reconstructButton"
virtualName="" explicitFocusOrder="0" pos="496 240 72 24" buttonText="Reconstruct"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
virtualName="" explicitFocusOrder="0" pos="896 280 220 24" tooltip="Training index"
min="0" max="0" int="1" style="LinearBar" textBoxPos="TextBoxLeft"
textBoxEditable="1" textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<TEXTBUTTON name="Shake button" id="7fa4964a9a9ed199" memberName="ShakeButton"
virtualName="" explicitFocusOrder="0" pos="592 280 72 24" buttonText="Shake"
virtualName="" explicitFocusOrder="0" pos="740 20 72 24" buttonText="Shake"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<SLIDER name="Weights slider" id="699075a1fc01458a" memberName="WeightsSlider"
virtualName="" explicitFocusOrder="0" pos="696 320 184 24" min="0"
max="0" int="1" style="LinearBar" textBoxPos="TextBoxLeft" textBoxEditable="1"
textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
virtualName="" explicitFocusOrder="0" pos="896 320 220 24" tooltip="Weight index"
min="0" max="0" int="1" style="LinearBar" textBoxPos="TextBoxLeft"
textBoxEditable="1" textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<LABEL name="Num Epochs label" id="b23ae372ee931474" memberName="numEpochslabel"
virtualName="" explicitFocusOrder="0" pos="504 200 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="564 200 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="1000" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<LABEL name="Learning Rate label" id="49611a27914e910d" memberName="learningRateLabel"
virtualName="" explicitFocusOrder="0" pos="592 200 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="652 200 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0.01" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<TEXTBUTTON name="Test button" id="a1e6fed732ae7ee1" memberName="testButton"
virtualName="" explicitFocusOrder="0" pos="592 20 72 24" buttonText="Test"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<LABEL name="Num Visible label" id="60acd702770b77dd" memberName="numVisibleLabel"
virtualName="" explicitFocusOrder="0" pos="736 368 48 24" edTextCol="ff000000"
edBkgCol="0" labelText="16" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
virtualName="" explicitFocusOrder="0" pos="656 248 48 24" tooltip="Number of visible units X"
edTextCol="ff000000" edBkgCol="0" labelText="16" editableSingleClick="1"
editableDoubleClick="1" focusDiscardsChanges="0" fontname="Default font"
fontsize="15" bold="0" italic="0" justification="36"/>
<LABEL name="Num Hidden label" id="8a56c0419bd3ce86" memberName="numHiddenLabel"
virtualName="" explicitFocusOrder="0" pos="840 368 48 24" edTextCol="ff000000"
edBkgCol="0" labelText="64" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
virtualName="" explicitFocusOrder="0" pos="780 248 48 24" tooltip="Number of hidden units"
edTextCol="ff000000" edBkgCol="0" labelText="64" editableSingleClick="1"
editableDoubleClick="1" focusDiscardsChanges="0" fontname="Default font"
fontsize="15" bold="0" italic="0" justification="36"/>
<TEXTBUTTON name="Create button" id="6c71593a581844eb" memberName="createButton"
virtualName="" explicitFocusOrder="0" pos="660 368 72 24" buttonText="Create"
virtualName="" explicitFocusOrder="0" pos="728 280 72 24" buttonText="Create"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<LABEL name="Project Name label" id="b4e855167abdac6b" memberName="projectNameLabel"
virtualName="" explicitFocusOrder="0" pos="892 368 96 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="552 248 96 24" edTextCol="ff000000"
edBkgCol="0" labelText="test" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<TEXTBUTTON name="Load button" id="4e606c77b12e7d11" memberName="loadButton"
virtualName="" explicitFocusOrder="0" pos="496 368 72 24" buttonText="Load"
virtualName="" explicitFocusOrder="0" pos="564 280 72 24" buttonText="Load"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<TEXTBUTTON name="Save button" id="c435e774a51e39bf" memberName="saveButton"
virtualName="" explicitFocusOrder="0" pos="576 368 72 24" buttonText="Save"
virtualName="" explicitFocusOrder="0" pos="644 280 72 24" buttonText="Save"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<LABEL name="Num Visible Y label" id="101a4f77ca2445ba" memberName="numVisibleYLabel"
virtualName="" explicitFocusOrder="0" pos="788 368 48 24" edTextCol="ff000000"
edBkgCol="0" labelText="16" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
virtualName="" explicitFocusOrder="0" pos="716 248 48 24" tooltip="Number of visible units Y"
edTextCol="ff000000" edBkgCol="0" labelText="16" editableSingleClick="1"
editableDoubleClick="1" focusDiscardsChanges="0" fontname="Default font"
fontsize="15" bold="0" italic="0" justification="36"/>
<TEXTBUTTON name="Load Training button" id="c603a32c128de05d" memberName="loadTrainingButton"
virtualName="" explicitFocusOrder="0" pos="496 404 72 24" buttonText="Load T"
virtualName="" explicitFocusOrder="0" pos="564 316 72 24" buttonText="Load T"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<TEXTBUTTON name="Save Training button" id="8e665b97490357c4" memberName="saveTrainingButton"
virtualName="" explicitFocusOrder="0" pos="576 404 72 24" buttonText="Save T"
virtualName="" explicitFocusOrder="0" pos="644 316 72 24" buttonText="Save T"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<TEXTBUTTON name="Clear Training button" id="b4c148828519c013" memberName="clearTrainingButton"
virtualName="" explicitFocusOrder="0" pos="736 404 72 24" buttonText="Clear T"
virtualName="" explicitFocusOrder="0" pos="804 316 72 24" buttonText="Clear T"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<TEXTBUTTON name="Remove Training button" id="f0e1d068c39986f3" memberName="removeTrainingButton"
virtualName="" explicitFocusOrder="0" pos="656 404 72 24" buttonText="Remove T"
virtualName="" explicitFocusOrder="0" pos="724 316 72 24" buttonText="Remove T"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<SLIDER name="Num. Gibbs slider" id="fdd9d5ca4e05e18c" memberName="numGibbsSlider"
virtualName="" explicitFocusOrder="0" pos="696 240 184 24" min="1"
max="101" int="1" style="LinearBar" textBoxPos="TextBoxLeft"
virtualName="" explicitFocusOrder="0" pos="896 240 220 24" tooltip="Number of gibbs samples"
min="1" max="101" int="1" style="LinearBar" textBoxPos="TextBoxLeft"
textBoxEditable="1" textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<TEXTBUTTON name="Reconstruct Equilibrium button" id="208a587f5556207" memberName="reconstructEquButton"
virtualName="" explicitFocusOrder="0" pos="496 280 72 24" buttonText="Reconst Equ."
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<TOGGLEBUTTON name="rbmDoRaoBlackwell toggle button" id="7f585de46e182418"
memberName="rbmDoRaoBlackwellToggleButton" virtualName="" explicitFocusOrder="0"
pos="496 60 112 24" buttonText="Rao-Blackwell" connectedEdges="0"
needsCallback="1" radioGroupId="0" state="0"/>
<TOGGLEBUTTON name="rbmReduceVariance toggle button" id="1636f6389abe9225"
memberName="rbmReduceVarianceToggleButton" virtualName="" explicitFocusOrder="0"
pos="640 60 128 24" buttonText="Reduce Variance" connectedEdges="0"
needsCallback="1" radioGroupId="0" state="0"/>
pos="556 60 112 24" tooltip="Don't sample hidden during learning"
buttonText="Rao-Blackwell" connectedEdges="0" needsCallback="1"
radioGroupId="0" state="0"/>
<TOGGLEBUTTON name="rbmDoSampleVisible toggle button" id="1636f6389abe9225"
memberName="rbmDoSampleVisibleToggleButton" virtualName="" explicitFocusOrder="0"
pos="680 60 128 24" tooltip="Sample visible during learning&#10;"
buttonText="Sample Visible" connectedEdges="0" needsCallback="1"
radioGroupId="0" state="0"/>
<LABEL name="Lambda label" id="c51a5dfb587390b4" memberName="lambdaLabel"
virtualName="" explicitFocusOrder="0" pos="592 152 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="652 152 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="1.0" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<LABEL name="Sigma label" id="68d7dad32ed357f8" memberName="sigmaLabel"
virtualName="" explicitFocusOrder="0" pos="504 152 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="564 152 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="1.0" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<TOGGLEBUTTON name="rbmUseVisibleGaussian toggle button" id="c89b32a4deaa7f19"
memberName="rbmUseVisibleGaussianToggleButton" virtualName=""
explicitFocusOrder="0" pos="640 92 160 24" buttonText="Use gaussian visible"
explicitFocusOrder="0" pos="680 92 156 24" buttonText="Use gaussian visible"
connectedEdges="0" needsCallback="1" radioGroupId="0" state="0"/>
<TOGGLEBUTTON name="rbmDoSparse toggle button" id="ed3f2602ab35b5f2" memberName="rbmDoSparseToggleButton"
virtualName="" explicitFocusOrder="0" pos="496 92 128 24" buttonText="Do sparse"
virtualName="" explicitFocusOrder="0" pos="556 92 96 24" buttonText="Do sparse"
connectedEdges="0" needsCallback="1" radioGroupId="0" state="0"/>
<LABEL name="Sparsity label" id="f4efe44dc0ca4894" memberName="sparsityLabel"
virtualName="" explicitFocusOrder="0" pos="680 152 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="740 152 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0.05" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<LABEL name="SigmaDecay label" id="d5f2f5fffdafd926" memberName="sigmaDecayLabel"
virtualName="" explicitFocusOrder="0" pos="776 152 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="836 152 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="1.0" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<LABEL name="WeightDecay label" id="67bf3a0bec436468" memberName="weightDecayLabel"
virtualName="" explicitFocusOrder="0" pos="776 200 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0.0" editableSingleClick="1" editableDoubleClick="1"
virtualName="" explicitFocusOrder="0" pos="836 200 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<SLIDER name="progressBar slider" id="8c8748f39f6d0ec6" memberName="m_progressBarSlider"
virtualName="" explicitFocusOrder="0" pos="688 20 184 24" min="0"
max="100" int="1" style="LinearBar" textBoxPos="TextBoxLeft"
virtualName="" explicitFocusOrder="0" pos="844 20 220 24" tooltip="Progress"
min="0" max="100" int="1" style="LinearBar" textBoxPos="TextBoxLeft"
textBoxEditable="0" textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<LABEL name="Momentum label" id="570ca1355ccd4cdd" memberName="momentumLabel"
virtualName="" explicitFocusOrder="0" pos="680 200 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="740 200 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0.5" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<LABEL name="SparsityLearn label" id="8e452129810c0ed3" memberName="sparsityLearningRateLabel"
virtualName="" explicitFocusOrder="0" pos="888 152 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="948 152 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0.01" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<LABEL name="WeightInit label" id="c827025f2cc51ddf" memberName="weightInitLabel"
virtualName="" explicitFocusOrder="0" pos="888 200 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0.001" editableSingleClick="1" editableDoubleClick="1"
virtualName="" explicitFocusOrder="0" pos="948 200 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0.01" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
<TOGGLEBUTTON name="rbmLearnVariance button" id="92c647c1f8b110a2" memberName="rbmLearnVarianceButton"
virtualName="" explicitFocusOrder="0" pos="808 60 128 24" buttonText="Learn Variance"
virtualName="" explicitFocusOrder="0" pos="976 92 128 24" buttonText="Learn Variance"
connectedEdges="0" needsCallback="1" radioGroupId="0" state="0"/>
<TOGGLEBUTTON name="rbmNormalizeData toggle button" id="739772af1b096120" memberName="rbmNormalizeDataToggleButton"
virtualName="" explicitFocusOrder="0" pos="808 92 160 24" buttonText="Normalize data"
virtualName="" explicitFocusOrder="0" pos="844 92 124 24" buttonText="Normalize data"
connectedEdges="0" needsCallback="1" radioGroupId="0" state="0"/>
<COMBOBOX name="RBM Selector" id="71115a4f965bfd38" memberName="m_rbmSelect"
virtualName="" explicitFocusOrder="0" pos="844 404 62 24" editable="0"
virtualName="" explicitFocusOrder="0" pos="812 280 62 24" editable="0"
layout="33" items="" textWhenNonSelected="" textWhenNoItems="(no choices)"/>
<TOGGLEBUTTON name="rbmDoSampleBatch button" id="73858ad06ad70b1a" memberName="rbmDoSampleBatch"
virtualName="" explicitFocusOrder="0" pos="844 60 128 24" tooltip="Sample training data during learning&#10;"
buttonText="Sample training" connectedEdges="0" needsCallback="1"
radioGroupId="0" state="0"/>
</JUCER_COMPONENT>
END_JUCER_METADATA
+2 -4
View File
@@ -117,12 +117,10 @@ private:
ScopedPointer<TextButton> trainButton;
ScopedPointer<TextButton> addButton;
ScopedPointer<Slider> patterSlider;
ScopedPointer<TextButton> reconstructButton;
ScopedPointer<TextButton> ShakeButton;
ScopedPointer<Slider> WeightsSlider;
ScopedPointer<Label> numEpochslabel;
ScopedPointer<Label> learningRateLabel;
ScopedPointer<TextButton> testButton;
ScopedPointer<Label> numVisibleLabel;
ScopedPointer<Label> numHiddenLabel;
ScopedPointer<TextButton> createButton;
@@ -135,9 +133,8 @@ private:
ScopedPointer<TextButton> clearTrainingButton;
ScopedPointer<TextButton> removeTrainingButton;
ScopedPointer<Slider> numGibbsSlider;
ScopedPointer<TextButton> reconstructEquButton;
ScopedPointer<ToggleButton> rbmDoRaoBlackwellToggleButton;
ScopedPointer<ToggleButton> rbmReduceVarianceToggleButton;
ScopedPointer<ToggleButton> rbmDoSampleVisibleToggleButton;
ScopedPointer<Label> lambdaLabel;
ScopedPointer<Label> sigmaLabel;
ScopedPointer<ToggleButton> rbmUseVisibleGaussianToggleButton;
@@ -152,6 +149,7 @@ private:
ScopedPointer<ToggleButton> rbmLearnVarianceButton;
ScopedPointer<ToggleButton> rbmNormalizeDataToggleButton;
ScopedPointer<ComboBox> m_rbmSelect;
ScopedPointer<ToggleButton> rbmDoSampleBatch;
//==============================================================================
+116 -89
View File
@@ -8,10 +8,7 @@
#ifndef RBM_HPP_
#define RBM_HPP_
#include "VisibleLayer.hpp"
#include "HiddenLayer.hpp"
#include "Weights.hpp"
#include "LayerArray.hpp"
#include <cmath>
#include <Eigen/Dense>
@@ -20,20 +17,7 @@ using namespace Eigen;
void mylog(const char* format, ...);
#define printf mylog
#define EPSILON_SIGMA 0.05
class Rbm;
class RbmListener
{
public:
RbmListener() {}
virtual ~RbmListener()
{
}
virtual void onProgressChanged(const Rbm &obj) = 0;
};
#define EPSILON_SIGMA 0.001
class Rbm
{
@@ -43,15 +27,16 @@ public:
Params()
: m_constantSigma(1.0)
, m_sigmaDecay(1.0)
, m_weightDecay(0.0)
, m_weightDecay(0.01)
, m_lambda(1.0)
, m_sparsity(0.05)
, m_muWeights(0.01)
, m_muWeights(0.1)
, m_muSparsity(0.01)
, m_momentum(0.5)
, m_useVisibleGaussian(false)
, m_doRaoBlackwell(false)
, m_useProbsForHiddenReconstruction(false)
, m_doRaoBlackwell(true)
, m_doSampleVisible(false)
, m_doSampleBatch(false)
, m_doSparse(false)
, m_doNormalizeData(false)
, m_doLearnVariance(false)
@@ -69,18 +54,18 @@ public:
double m_momentum;
bool m_useVisibleGaussian;
bool m_doRaoBlackwell;
bool m_useProbsForHiddenReconstruction;
bool m_doSampleVisible;
bool m_doSampleBatch;
bool m_doSparse;
bool m_doNormalizeData;
bool m_doLearnVariance;
uint32_t m_numGibbs;
size_t m_numGibbs;
};
Rbm(Weights &weights, const MatrixXd &batch, RbmListener *pListener = nullptr)
Rbm(Weights &weights, const MatrixXd &batch)
: m_w(weights)
, m_batch(batch)
, m_variableSigma(weights.getNumVisible())
, m_pListener(pListener)
, m_progress(0)
{
Noise_Init(&m_noise, 0x32727155);
@@ -193,6 +178,16 @@ public:
src.array() *= k.array();
}
void sampleGaussian(MatrixXd &dst, MatrixXd const &src, const MatrixXd &sigma)
{
uint32_t i;
for (i=0; i < src.array().size(); i++)
{
dst.array()(i) = sigma(i)*Noise_Gaussian(&m_noise) + src.array()(i);
}
}
void sampleGaussian(MatrixXd &src, const MatrixXd &sigma)
{
uint32_t i;
@@ -258,24 +253,27 @@ public:
size_t batchSize = m_batch.rows();
double dProgress = 1.0/numEpochs;
double kTrain = 1.0/batchSize;
double mu_w = m_params.m_muWeights/batchSize;
double mu_biasV = m_params.m_muWeights/batchSize;
double mu_biasH = m_params.m_muWeights/batchSize;
m_v.resize(batchSize, m_w.getNumVisible());
MatrixXd h(batchSize, m_w.getNumHidden());
MatrixXd sumBiasV(1, m_w.getNumVisible());
MatrixXd sumBiasH(1, m_w.getNumHidden());
MatrixXd sumWeights(m_w.getNumVisible(), m_w.getNumHidden());
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()));
MatrixXd dBiasV_curr(MatrixXd::Zero(1, m_w.getNumVisible()));
MatrixXd dBiasH_curr(MatrixXd::Zero(1, m_w.getNumHidden()));
MatrixXd dW_curr(MatrixXd::Zero(m_w.getNumVisible(), m_w.getNumHidden()));
MatrixXd dBiasV(MatrixXd::Zero(1, m_w.getNumVisible()));
MatrixXd dBiasH(MatrixXd::Zero(1, m_w.getNumHidden()));
MatrixXd dW(MatrixXd::Zero(m_w.getNumVisible(), m_w.getNumHidden()));
MatrixXd diffErr(batchSize, m_w.getNumVisible());
MatrixXd batch = m_batch;
MatrixXd batch_sampled(batchSize, m_w.getNumVisible());
MatrixXd v_sampled(batchSize, m_w.getNumVisible());
if (m_params.m_doNormalizeData)
{
RowVectorXd mean = calcMean(batch);
@@ -289,93 +287,97 @@ public:
m_progress = 0;
for (epoch=0; epoch < numEpochs; epoch++)
{
if (m_pListener)
{
m_pListener->onProgressChanged(*this);
}
onProgressChanged();
if (!m_params.m_useProbsForHiddenReconstruction)
// When the hidden units are being driven by data, always use stochastic binary states
if (m_params.m_doSampleBatch)
{
sample(batch, m_batch);
}
sample(batch_sampled, batch);
// Create hidden layer base on training data
toHiddenBatch(h, batch);
// Create hidden layer base on sampled training data
toHiddenBatch(h, batch_sampled);
}
else
{
// Create hidden layer base on training data
toHiddenBatch(h, batch);
}
// Sample hidden
if (!m_params.m_doRaoBlackwell)
{
sample(h);
}
// Update weights (positive phase)
sumBiasV = batch.colwise().sum();
dBiasV_curr = batch.colwise().sum();
if (!m_params.m_doSparse)
{
sumBiasH = h.colwise().sum();
dBiasH_curr = h.colwise().sum();
}
sumWeights = batch.transpose() * h;
dW_curr = batch.transpose() * h;
for (gibbs=0; gibbs < m_params.m_numGibbs; gibbs++)
{
sample(h);
// Create visible reconstruction (a fantasy...) given h
toVisibleBatch(m_v, h);
if (m_params.m_useVisibleGaussian)
{
if (!m_params.m_useProbsForHiddenReconstruction)
{
sampleGaussian(m_v, m_variableSigma.replicate(batchSize, 1));
}
sampleGaussian(v_sampled, m_v, m_variableSigma.replicate(batchSize, 1));
toHiddenBatch(h, v_sampled);
}
else
{
probsLogistic(m_v, m_variableSigma.replicate(batchSize, 1));
if (!m_params.m_useProbsForHiddenReconstruction)
if (m_params.m_doSampleVisible)
{
sample(m_v);
sample(v_sampled, m_v);
// Create hidden representation given sampled v
toHiddenBatch(h, v_sampled);
}
else
{
// Create hidden representation given v
toHiddenBatch(h, m_v);
}
}
// Create hidden representation given v
toHiddenBatch(h, m_v);
if (!m_params.m_doRaoBlackwell)
{
sample(h);
}
}
if (!m_params.m_doRaoBlackwell)
{
sample(h);
}
// Update weights (negative phase)
sumBiasV -= m_v.colwise().sum();
dBiasV_curr -= m_v.colwise().sum();
if (!m_params.m_doSparse)
{
sumBiasH -= h.colwise().sum();
dBiasH_curr -= h.colwise().sum();
}
sumWeights -= m_v.transpose() * h;
deltaWeights = m_params.m_momentum*deltaWeights + m_params.m_muWeights*(kTrain*sumWeights - m_params.m_weightDecay*m_w.weights());
m_w.weights() += deltaWeights;
deltaBiasV = m_params.m_momentum*deltaBiasV + m_params.m_muWeights*kTrain*sumBiasV;
m_w.visibleBias() += deltaBiasV;
dW_curr -= m_v.transpose() * h;
m_w.visibleBias() += mu_biasV*(m_params.m_momentum*dBiasV + (1-m_params.m_momentum)*dBiasV_curr);
dBiasV = dBiasV_curr;
m_w.weights() += mu_w*(m_params.m_momentum*dW + (1-m_params.m_momentum)*dW_curr - m_params.m_weightDecay*m_w.weights());
dW = dW_curr;
if (m_params.m_doSparse)
{
// Create hidden representation given v
toHiddenBatch(h, m_v);
toHiddenBatch(h, batch);
sumBiasH.fill(m_params.m_sparsity);
sumBiasH -= h.colwise().mean();
deltaBiasH = m_params.m_momentum*deltaBiasH + m_params.m_muSparsity*sumBiasH;
dBiasH_curr = m_params.m_sparsity * MatrixXd::Ones(dBiasH.rows(), dBiasH.cols()) - h.colwise().mean();
m_w.hiddenBias() += m_params.m_muSparsity*(m_params.m_momentum*dBiasH + (1-m_params.m_momentum)*dBiasH_curr);
dBiasH = dBiasH_curr;
// cout << "Mean(" << m_sparsity << ") = " << (double)sumBiasH.array().mean() << endl;
// cout << sumBiasH << endl;
}
else
{
deltaBiasH = m_params.m_momentum*deltaBiasH + m_params.m_muWeights*kTrain*sumBiasH;
m_w.hiddenBias() += mu_biasH*(m_params.m_momentum*dBiasH + (1-m_params.m_momentum)*dBiasH_curr);
dBiasH = dBiasH_curr;
}
m_w.hiddenBias() += deltaBiasH;
if (m_variableSigma[0] > sigmaMin)
{
@@ -383,12 +385,8 @@ public:
}
m_progress += dProgress;
if (m_pListener)
{
m_pListener->onProgressChanged(*this);
}
diffErr = batch - m_v;
diffErr = m_batch - m_v;
diffErr.array() *= diffErr.array();
double err = diffErr.colwise().sum().sum();
@@ -398,7 +396,7 @@ public:
} // Number of epochs
updateHiddenBatch();
onProgressChanged();
}
double getProgress() const
@@ -432,7 +430,7 @@ public:
if (m_params.m_useVisibleGaussian)
{
// probsGaussian(v, m_sigmas);
probsGaussian(v, m_variableSigma);
}
else
{
@@ -444,6 +442,7 @@ public:
{
m_params.m_constantSigma = value;
m_variableSigma.fill(m_params.m_constantSigma);
onParamsChanged();
}
RowVectorXd& getVariableSigma()
@@ -454,46 +453,61 @@ public:
void setSigmaDecay(double value)
{
m_params.m_sigmaDecay = value;
onParamsChanged();
}
void setWeightDecay(double value)
{
m_params.m_weightDecay = value;
onParamsChanged();
}
void setLambda(double value)
{
m_params.m_lambda = value;
onParamsChanged();
}
void setSparsity(double value)
{
m_params.m_sparsity = value;
onParamsChanged();
}
void setUseVisibleGaussian(bool flag)
{
m_params.m_useVisibleGaussian = flag;
onParamsChanged();
}
void setDoRaoBlackwell(bool flag)
{
m_params.m_doRaoBlackwell = flag;
onParamsChanged();
}
void setUseProbsForHiddenReconstruction(bool flag)
void setDoSampleVisible(bool flag)
{
m_params.m_useProbsForHiddenReconstruction = flag;
m_params.m_doSampleVisible = flag;
onParamsChanged();
}
void setDoSampleBatch(bool flag)
{
m_params.m_doSampleBatch = flag;
onParamsChanged();
}
void setDoSparse(bool flag)
{
m_params.m_doSparse = flag;
onParamsChanged();
}
void setNormalizeData(bool flag)
{
m_params.m_doNormalizeData = flag;
onParamsChanged();
}
void setDoLearnVariance(bool flag)
@@ -507,26 +521,31 @@ public:
{
m_variableSigma.fill(m_params.m_constantSigma);
}
onParamsChanged();
}
void setNumGibbs(uint32_t value)
void setNumGibbs(size_t value)
{
m_params.m_numGibbs = value;
onParamsChanged();
}
void setMuWeights(double value)
{
m_params.m_muWeights = value;
onParamsChanged();
}
void setMuSparsity(double value)
{
m_params.m_muSparsity = value;
onParamsChanged();
}
void setMomentum(double value)
{
m_params.m_momentum = value;
onParamsChanged();
}
MatrixXd const& getHiddenBatch()
@@ -561,7 +580,6 @@ private:
MatrixXd m_v;
MatrixXd m_h;
RowVectorXd m_variableSigma;
RbmListener *m_pListener;
noise_gen_t m_noise;
double m_progress;
Params m_params;
@@ -581,6 +599,15 @@ private:
v = h * m_w.weights().transpose();
v += m_w.visibleBias().replicate(m_batch.rows(), 1);
}
protected:
virtual void onProgressChanged()
{
}
virtual void onParamsChanged()
{
}
};
+13 -103
View File
@@ -24,7 +24,8 @@
//==============================================================================
RbmComponent::RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponentListener &listener)
: m_weights(weights)
: Rbm(weights, batch)
, m_weights(weights)
, m_listener(listener)
, m_currWeightIndexToDraw(0)
, m_currTrainingIndexToDraw(0)
@@ -35,7 +36,6 @@ RbmComponent::RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponen
{
//[UserPreSize]
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();
@@ -69,7 +69,6 @@ RbmComponent::~RbmComponent()
DrawWeights = nullptr;
DrawVars = nullptr;
DrawHidden = nullptr;
m_pRbm = nullptr;
//[/Destructor]
}
@@ -204,13 +203,13 @@ void RbmComponent::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails&
//[/UserCode_mouseWheelMove]
}
void RbmComponent::onProgressChanged(const Rbm &obj)
void RbmComponent::onProgressChanged()
{
// mylog("Training %f %%\n", obj.getProgress()*100);
upPass(DrawHidden->getData());
redrawReconstruction();
redrawWeights();
m_listener.onProgressChanged((size_t)(100*obj.getProgress() + 0.5));
m_listener.onProgressChanged((size_t)(100*getProgress() + 0.5));
}
void RbmComponent::onDraw(DrawComponent &obj)
@@ -229,15 +228,11 @@ void RbmComponent::redrawReconstruction()
{
uint32_t i;
reconstructVisible(DrawReconstruction->getData(), DrawTraining->getData());
// toHidden(DrawHidden->getData(), DrawTraining->getData());
// toVisible(DrawReconstruction->getData(), DrawHidden->getData());
downPass(DrawReconstruction->getData(), DrawTraining->getData());
for (i=0; i < m_pRbm->params().m_numGibbs-1; i++)
for (i=0; i < params().m_numGibbs-1; i++)
{
reconstructVisible(DrawReconstruction->getData(), DrawReconstruction->getData());
// toHidden(DrawHidden->getData(), DrawReconstruction->getData());
// toVisible(DrawReconstruction->getData(), DrawHidden->getData());
downPass(DrawReconstruction->getData(), DrawReconstruction->getData());
}
DrawReconstruction->DrawData();
}
@@ -250,94 +245,20 @@ void RbmComponent::redrawWeights()
void RbmComponent::redrawVariances()
{
DrawVars->getData() = m_pRbm->getVariableSigma();
DrawVars->getData() = getVariableSigma();
DrawVars->DrawData();
}
void RbmComponent::setDoLearnVariance(bool value)
void RbmComponent::onParamsChanged()
{
m_pRbm->setDoLearnVariance(value);
redrawWeights();
redrawVariances();
redrawReconstruction();
}
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->setConstantSigma(value);
redrawVariances();
redrawReconstruction();
}
void RbmComponent::setNormalizeData(bool enable)
{
m_pRbm->setNormalizeData(enable);
}
void RbmComponent::setMuWeights(double value)
{
m_pRbm->setMuWeights(value);
}
void RbmComponent::setMuSparsity(double value)
{
m_pRbm->setMuSparsity(value);
}
void RbmComponent::setMomentum(double value)
{
m_pRbm->setMomentum(value);
}
void RbmComponent::batchchanged()
{
m_pRbm->updateHiddenBatch();
Rbm::updateHiddenBatch();
if (lower)
{
lower->batchchanged();
@@ -358,10 +279,10 @@ size_t RbmComponent::getWeightsIndex()
void RbmComponent::setTrainingIndex(size_t index)
{
if (m_pRbm->getBatch().rows() > 0)
if (getBatch().rows() > 0)
{
m_currTrainingIndexToDraw = index;
DrawTraining->getData() = m_pRbm->getBatch().row(index);
DrawTraining->getData() = getBatch().row(index);
DrawTraining->DrawData();
redrawReconstruction();
@@ -374,17 +295,6 @@ size_t RbmComponent::getTrainingIndex()
return m_currTrainingIndexToDraw;
}
void RbmComponent::copyReconstructionToTraining()
{
DrawTraining->getData() = DrawReconstruction->getData();
DrawTraining->DrawData();
}
void RbmComponent::train(size_t numEpochs)
{
m_pRbm->train(numEpochs);
}
RowVectorXd const& RbmComponent::getTrainingData()
{
return DrawTraining->getData();
+10 -55
View File
@@ -42,9 +42,7 @@ public:
}
virtual void batchchanged() = 0;
virtual void toHidden(RowVectorXd &h, RowVectorXd const &v) = 0;
virtual void toVisible(RowVectorXd &v, RowVectorXd const &h) = 0;
virtual void downPass(RowVectorXd &h) = 0;
virtual void downPass(RowVectorXd &dst, RowVectorXd const &src) = 0;
virtual void upPass(RowVectorXd const &v) = 0;
void registerRbm(IRbmComponent *pObj)
{
@@ -72,10 +70,10 @@ public:
Describe your class and how it works here!
//[/Comments]
*/
class RbmComponent : public Component,
public RbmListener,
public DrawListener,
public IRbmComponent
class RbmComponent : public Component
, public Rbm
, public DrawListener
, public IRbmComponent
{
public:
//==============================================================================
@@ -97,21 +95,6 @@ public:
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);
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);
void setMuSparsity(double value);
void setMomentum(double value);
void batchchanged() override;
void setWeightsIndex(size_t index);
size_t getWeightsIndex();
@@ -119,47 +102,19 @@ public:
void setTrainingIndex(size_t index);
size_t getTrainingIndex();
void copyReconstructionToTraining();
void redrawWeights();
void redrawReconstruction();
void redrawVariances();
void train(size_t numEpochs);
RowVectorXd const& getTrainingData();
MatrixXd const& getHiddenBatch()
{
return m_pRbm->getHiddenBatch();
}
Rbm::Params const& params()
{
return m_pRbm->params();
}
void reconstructVisible(RowVectorXd& dst, const RowVectorXd& src)
void downPass(RowVectorXd &dst, RowVectorXd const &src) override
{
toHidden(DrawHidden->getData(), src);
toVisible(dst, DrawHidden->getData());
DrawHidden->DrawData();
}
void toVisible(RowVectorXd &v, RowVectorXd const &h) override
{
m_pRbm->toVisible(v, h);
}
void toHidden(RowVectorXd &h, RowVectorXd const &v) override
{
m_pRbm->toHidden(h, v);
if (lower)
{
lower->downPass(h);
lower->downPass(DrawHidden->getData(), DrawHidden->getData());
}
}
void downPass(RowVectorXd& h) override
{
toHidden(DrawHidden->getData(), h);
m_pRbm->toVisible(h, DrawHidden->getData());
toVisible(dst, DrawHidden->getData());
DrawHidden->DrawData();
}
@@ -175,7 +130,6 @@ public:
private:
//[UserVariables] -- You can add your own custom variables in this section.
ScopedPointer<Rbm> m_pRbm;
Weights &m_weights;
RbmComponentListener &m_listener;
ScopedPointer<DrawComponent> DrawTraining;
@@ -185,7 +139,8 @@ private:
ScopedPointer<DrawComponent> DrawHidden;
size_t m_currWeightIndexToDraw;
size_t m_currTrainingIndexToDraw;
void onProgressChanged(const Rbm &obj) override;
void onParamsChanged() override;
void onProgressChanged() override;
void onDraw(DrawComponent &obj) override;
//[/UserVariables]
-37
View File
@@ -1,37 +0,0 @@
/*
* VisibleLayer.hpp
*
* Created on: 21.09.2014
* Author: jens
*/
#ifndef VISIBLELAYER_HPP_
#define VISIBLELAYER_HPP_
#include "Layer.hpp"
#include <cmath>
class VisibleLayer : public Layer
{
public:
VisibleLayer(uint32_t numUnits = 0, const RowVectorXd *pStatesInit = nullptr)
: Layer(numUnits, pStatesInit)
{
}
virtual ~VisibleLayer()
{
}
private:
double accum(Layer &layer, Weights &weights, uint32_t index)
{
double sum = ((Weights&)weights).visibleBias()[index];
sum += ((Weights&)weights).weights().row(index) * layer.states().transpose();
return sum;
}
};
#endif /* VISIBLELAYER_HPP_ */
+4 -5
View File
@@ -82,22 +82,21 @@ public:
m_w.resize(m_numVisible, m_numHidden);
m_bv.resize(m_numVisible);
m_bh.resize(m_numHidden);
shuffle(1.0);
shuffle(0.01);
}
void shuffle(double stdDev)
{
uint32_t i, j;
double kdev = stdDev*sqrt(12.0);
m_bv.array().fill(Noise_Uniform(&m_noise));
m_bh.array().fill(Noise_Uniform(&m_noise));
m_bv.array().fill(stdDev*Noise_Gaussian(&m_noise));
m_bh.array().fill(stdDev*Noise_Gaussian(&m_noise));
for (i=0; i < m_numVisible; i++)
{
for (j=0; j < m_numHidden; j++)
{
m_w(i,j) = kdev*Noise_Uniform(&m_noise);
m_w(i,j) = stdDev*Noise_Gaussian(&m_noise);
}
}
}