From 57761fe7439bbdd1a44e3b6a6d479969595fce0a Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 18 Oct 2014 15:38:11 +0000 Subject: [PATCH] - RBM: changed calcualation of pre-weight update data in RBM - always use expectations - removed "Use Expectations Button" - removed Robbins-Monro - added sparsity learning rate - added momentum - added weight decay - added Slider as progress bar git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@30 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- Source/MainComponent.cpp | 287 +++++++++++++++++++++++++-------------- Source/MainComponent.h | 14 +- Source/Rbm.hpp | 228 +++++++++++++------------------ 3 files changed, 284 insertions(+), 245 deletions(-) diff --git a/Source/MainComponent.cpp b/Source/MainComponent.cpp index cb9aeda..9159375 100644 --- a/Source/MainComponent.cpp +++ b/Source/MainComponent.cpp @@ -72,7 +72,7 @@ MainComponent::MainComponent () addAndMakeVisible (patterSlider = new Slider ("Pattern slider")); patterSlider->setRange (0, 0, 1); - patterSlider->setSliderStyle (Slider::LinearHorizontal); + patterSlider->setSliderStyle (Slider::LinearBar); patterSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20); patterSlider->addListener (this); @@ -86,7 +86,7 @@ MainComponent::MainComponent () addAndMakeVisible (WeightsSlider = new Slider ("Weights slider")); WeightsSlider->setRange (0, 0, 1); - WeightsSlider->setSliderStyle (Slider::LinearHorizontal); + WeightsSlider->setSliderStyle (Slider::LinearBar); WeightsSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20); WeightsSlider->addListener (this); @@ -100,7 +100,7 @@ MainComponent::MainComponent () numEpochslabel->addListener (this); addAndMakeVisible (learningRateLabel = new Label ("Learning Rate label", - TRANS("0.03"))); + TRANS("0.01"))); learningRateLabel->setFont (Font (15.00f, Font::plain)); learningRateLabel->setJustificationType (Justification::centred); learningRateLabel->setEditable (true, true, false); @@ -178,7 +178,7 @@ MainComponent::MainComponent () addAndMakeVisible (numGibbsSlider = new Slider ("Num. Gibbs slider")); numGibbsSlider->setRange (1, 10, 1); - numGibbsSlider->setSliderStyle (Slider::LinearHorizontal); + numGibbsSlider->setSliderStyle (Slider::LinearBar); numGibbsSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20); numGibbsSlider->addListener (this); @@ -186,18 +186,10 @@ MainComponent::MainComponent () reconstructEquButton->setButtonText (TRANS("Reconst Equ.")); reconstructEquButton->addListener (this); - addAndMakeVisible (rbmUseExpectationsToggleButton = new ToggleButton ("rbmUseExpectations toggle button")); - rbmUseExpectationsToggleButton->setButtonText (TRANS("Use Expectations")); - rbmUseExpectationsToggleButton->addListener (this); - addAndMakeVisible (rbmDoRaoBlackwellToggleButton = new ToggleButton ("rbmDoRaoBlackwell toggle button")); rbmDoRaoBlackwellToggleButton->setButtonText (TRANS("Rao-Blackwell")); rbmDoRaoBlackwellToggleButton->addListener (this); - addAndMakeVisible (rbmDoRobbinsMonroToggleButton = new ToggleButton ("rbmDoRobbinsMonro toggle button")); - rbmDoRobbinsMonroToggleButton->setButtonText (TRANS("Robbins-Monro")); - rbmDoRobbinsMonroToggleButton->addListener (this); - addAndMakeVisible (rbmReduceVarianceToggleButton = new ToggleButton ("rbmReduceVariance toggle button")); rbmReduceVarianceToggleButton->setButtonText (TRANS("Reduce Variance")); rbmReduceVarianceToggleButton->addListener (this); @@ -212,7 +204,7 @@ MainComponent::MainComponent () lambdaLabel->addListener (this); addAndMakeVisible (sigmaLabel = new Label ("Sigma label", - TRANS("0.5"))); + TRANS("1.0"))); sigmaLabel->setFont (Font (15.00f, Font::plain)); sigmaLabel->setJustificationType (Justification::centred); sigmaLabel->setEditable (true, true, false); @@ -246,17 +238,44 @@ MainComponent::MainComponent () sigmaDecayLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); sigmaDecayLabel->addListener (this); - addAndMakeVisible (m_progressBar = new ProgressBar (m_trainingProgress)); - m_progressBar->setName ("Progress Bar"); + addAndMakeVisible (weightDecayLabel = new Label ("WeightDecay label", + TRANS("0.0002"))); + weightDecayLabel->setFont (Font (15.00f, Font::plain)); + weightDecayLabel->setJustificationType (Justification::centred); + weightDecayLabel->setEditable (true, true, false); + weightDecayLabel->setColour (TextEditor::textColourId, Colours::black); + weightDecayLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); + weightDecayLabel->addListener (this); + + addAndMakeVisible (m_progressBarSlider = new Slider ("progressBar slider")); + m_progressBarSlider->setRange (0, 100, 1); + m_progressBarSlider->setSliderStyle (Slider::LinearBar); + m_progressBarSlider->setTextBoxStyle (Slider::TextBoxLeft, true, 80, 20); + m_progressBarSlider->addListener (this); + + addAndMakeVisible (momentumLabel = new Label ("Momentum label", + TRANS("0.5"))); + momentumLabel->setFont (Font (15.00f, Font::plain)); + momentumLabel->setJustificationType (Justification::centred); + momentumLabel->setEditable (true, true, false); + momentumLabel->setColour (TextEditor::textColourId, Colours::black); + momentumLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); + momentumLabel->addListener (this); + + addAndMakeVisible (sparsityLearningRateLabel = new Label ("SparsityLearn label", + TRANS("0.01"))); + sparsityLearningRateLabel->setFont (Font (15.00f, Font::plain)); + sparsityLearningRateLabel->setJustificationType (Justification::centred); + sparsityLearningRateLabel->setEditable (true, true, false); + sparsityLearningRateLabel->setColour (TextEditor::textColourId, Colours::black); + sparsityLearningRateLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000)); + sparsityLearningRateLabel->addListener (this); //[UserPreSize] m_vNumX = 16; m_vNumY = 16; m_hNum = 64; - m_vNumX_next = 16; - m_vNumY_next = 16; - m_hNum_next = 64; m_numGibbs = 1; m_weights.setUnits(m_vNumX*m_vNumY, m_hNum); create(); @@ -267,8 +286,7 @@ MainComponent::MainComponent () //[Constructor] You can add your own custom stuff here.. - m_trainingProgress = 0; - m_progressBar->setTextToDisplay("Finished"); + m_progressBarSlider->setValue(100); //[/Constructor] } @@ -299,9 +317,7 @@ MainComponent::~MainComponent() removeTrainingButton = nullptr; numGibbsSlider = nullptr; reconstructEquButton = nullptr; - rbmUseExpectationsToggleButton = nullptr; rbmDoRaoBlackwellToggleButton = nullptr; - rbmDoRobbinsMonroToggleButton = nullptr; rbmReduceVarianceToggleButton = nullptr; lambdaLabel = nullptr; sigmaLabel = nullptr; @@ -309,7 +325,10 @@ MainComponent::~MainComponent() rbmDoSparseToggleButton = nullptr; sparsityLabel = nullptr; sigmaDecayLabel = nullptr; - m_progressBar = nullptr; + weightDecayLabel = nullptr; + m_progressBarSlider = nullptr; + momentumLabel = nullptr; + sparsityLearningRateLabel = nullptr; //[Destructor]. You can add your own custom destruction code here.. @@ -366,6 +385,24 @@ void MainComponent::paint (Graphics& g) 296, 306, 96, 14, Justification::centred, true); + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Weight decay"), + 296, 354, 96, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Momentum"), + 208, 354, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sparsity Alpha"), + 408, 306, 96, 14, + Justification::centred, true); + //[UserPaint] Add your own custom painting code here.. //[/UserPaint] } @@ -374,37 +411,38 @@ void MainComponent::resized() { trainButton->setBounds (120, 408, 72, 24); addButton->setBounds (24, 160, 72, 24); - patterSlider->setBounds (224, 408, 184, 24); + patterSlider->setBounds (224, 448, 184, 24); reconstructButton->setBounds (24, 408, 72, 24); ShakeButton->setBounds (120, 448, 72, 24); - WeightsSlider->setBounds (224, 448, 184, 24); + WeightsSlider->setBounds (224, 488, 184, 24); numEpochslabel->setBounds (32, 368, 72, 24); learningRateLabel->setBounds (120, 368, 72, 24); testButton->setBounds (120, 160, 72, 24); - numVisibleLabel->setBounds (456, 328, 72, 24); - numHiddenLabel->setBounds (504, 360, 72, 24); - createButton->setBounds (504, 392, 72, 24); - projectNameLabel->setBounds (488, 296, 96, 24); - loadButton->setBounds (456, 264, 72, 24); - saveButton->setBounds (536, 264, 72, 24); - numVisibleYLabel->setBounds (528, 328, 72, 24); - loadTrainingButton->setBounds (440, 48, 72, 24); - saveTrainingButton->setBounds (520, 48, 72, 24); - clearTrainingButton->setBounds (520, 88, 72, 24); - removeTrainingButton->setBounds (440, 88, 72, 24); - numGibbsSlider->setBounds (224, 368, 184, 24); + numVisibleLabel->setBounds (504, 224, 72, 24); + numHiddenLabel->setBounds (552, 256, 72, 24); + createButton->setBounds (552, 288, 72, 24); + projectNameLabel->setBounds (536, 192, 96, 24); + loadButton->setBounds (504, 160, 72, 24); + saveButton->setBounds (584, 160, 72, 24); + numVisibleYLabel->setBounds (576, 224, 72, 24); + loadTrainingButton->setBounds (504, 48, 72, 24); + saveTrainingButton->setBounds (584, 48, 72, 24); + clearTrainingButton->setBounds (584, 88, 72, 24); + removeTrainingButton->setBounds (504, 88, 72, 24); + numGibbsSlider->setBounds (224, 408, 184, 24); reconstructEquButton->setBounds (24, 448, 72, 24); - rbmUseExpectationsToggleButton->setBounds (24, 232, 128, 24); rbmDoRaoBlackwellToggleButton->setBounds (24, 200, 112, 24); - rbmDoRobbinsMonroToggleButton->setBounds (168, 232, 120, 24); rbmReduceVarianceToggleButton->setBounds (168, 200, 128, 24); lambdaLabel->setBounds (120, 320, 72, 24); sigmaLabel->setBounds (32, 320, 72, 24); - rbmUseVisibleGaussianToggleButton->setBounds (168, 264, 160, 24); - rbmDoSparseToggleButton->setBounds (24, 264, 128, 24); + rbmUseVisibleGaussianToggleButton->setBounds (168, 232, 160, 24); + rbmDoSparseToggleButton->setBounds (24, 232, 128, 24); sparsityLabel->setBounds (208, 320, 72, 24); sigmaDecayLabel->setBounds (304, 320, 72, 24); - m_progressBar->setBounds (224, 160, 168, 24); + weightDecayLabel->setBounds (304, 368, 72, 24); + m_progressBarSlider->setBounds (216, 160, 184, 24); + momentumLabel->setBounds (208, 368, 72, 24); + sparsityLearningRateLabel->setBounds (416, 320, 72, 24); //[UserResized] Add your own custom resize handling here.. DrawTraining->setBounds (16, 16, 100, 100); DrawReconstruction->setBounds (110+16, 16, 100, 100); @@ -459,9 +497,10 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) else if (buttonThatWasClicked == createButton) { //[UserButtonCode_createButton] -- add your button handler code here.. - m_vNumX = m_vNumX_next; - m_vNumY = m_vNumY_next; - m_hNum = m_hNum_next; + m_vNumX = numVisibleLabel->getText().getIntValue(); + m_vNumY = numVisibleYLabel->getText().getIntValue(); + m_hNum = numHiddenLabel->getText().getIntValue(); + m_weights.setUnits(m_vNumX*m_vNumY, m_hNum); create(); //[/UserButtonCode_createButton] @@ -522,24 +561,12 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) } //[/UserButtonCode_reconstructEquButton] } - else if (buttonThatWasClicked == rbmUseExpectationsToggleButton) - { - //[UserButtonCode_rbmUseExpectationsToggleButton] -- add your button handler code here.. - m_pRbm->setUseExpectations(buttonThatWasClicked->getToggleState()); - //[/UserButtonCode_rbmUseExpectationsToggleButton] - } else if (buttonThatWasClicked == rbmDoRaoBlackwellToggleButton) { //[UserButtonCode_rbmDoRaoBlackwellToggleButton] -- add your button handler code here.. m_pRbm->setDoRaoBlackwell(buttonThatWasClicked->getToggleState()); //[/UserButtonCode_rbmDoRaoBlackwellToggleButton] } - else if (buttonThatWasClicked == rbmDoRobbinsMonroToggleButton) - { - //[UserButtonCode_rbmDoRobbinsMonroToggleButton] -- add your button handler code here.. - m_pRbm->setDoRobbinsMonro(buttonThatWasClicked->getToggleState()); - //[/UserButtonCode_rbmDoRobbinsMonroToggleButton] - } else if (buttonThatWasClicked == rbmReduceVarianceToggleButton) { //[UserButtonCode_rbmReduceVarianceToggleButton] -- add your button handler code here.. @@ -550,6 +577,23 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) { //[UserButtonCode_rbmUseVisibleGaussianToggleButton] -- add your button handler code here.. m_pRbm->setUseVisibleGaussian(buttonThatWasClicked->getToggleState()); + rbmReduceVarianceToggleButton->setEnabled(!buttonThatWasClicked->getToggleState()); + + if (buttonThatWasClicked->getToggleState()) + { + rbmReduceVarianceToggleButton_binary = rbmReduceVarianceToggleButton->getToggleState(); + rbmReduceVarianceToggleButton->setToggleState(false, sendNotification); + sigmaLabel->setText(String(sigma_gauss, 1), sendNotification); + sigmaDecayLabel->setText(String(sigmaDecay_gauss, 1), sendNotification); + } + else + { + sigma_gauss = sigmaLabel->getText().getFloatValue(); + sigmaDecay_gauss = sigmaDecayLabel->getText().getFloatValue(); + sigmaLabel->setText("1.0", sendNotification); + sigmaDecayLabel->setText("1.0", sendNotification); + rbmReduceVarianceToggleButton->setToggleState(rbmReduceVarianceToggleButton_binary, sendNotification); + } redrawReconstruction(); //[/UserButtonCode_rbmUseVisibleGaussianToggleButton] } @@ -589,9 +633,14 @@ void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved) else if (sliderThatWasMoved == numGibbsSlider) { //[UserSliderCode_numGibbsSlider] -- add your slider handling code here.. - m_numGibbs = (uint32_t)sliderThatWasMoved->getValue(); + m_pRbm->setNumGibbs((uint32_t)sliderThatWasMoved->getValue()); //[/UserSliderCode_numGibbsSlider] } + else if (sliderThatWasMoved == m_progressBarSlider) + { + //[UserSliderCode_m_progressBarSlider] -- add your slider handling code here.. + //[/UserSliderCode_m_progressBarSlider] + } //[UsersliderValueChanged_Post] //[/UsersliderValueChanged_Post] @@ -610,18 +659,17 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged) else if (labelThatHasChanged == learningRateLabel) { //[UserLabelCode_learningRateLabel] -- add your label text handling code here.. + m_pRbm->setMuWeights(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_learningRateLabel] } else if (labelThatHasChanged == numVisibleLabel) { //[UserLabelCode_numVisibleLabel] -- add your label text handling code here.. - m_vNumX_next = labelThatHasChanged->getText().getIntValue(); //[/UserLabelCode_numVisibleLabel] } else if (labelThatHasChanged == numHiddenLabel) { //[UserLabelCode_numHiddenLabel] -- add your label text handling code here.. - m_hNum_next = labelThatHasChanged->getText().getIntValue(); //[/UserLabelCode_numHiddenLabel] } else if (labelThatHasChanged == projectNameLabel) @@ -632,7 +680,6 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged) else if (labelThatHasChanged == numVisibleYLabel) { //[UserLabelCode_numVisibleYLabel] -- add your label text handling code here.. - m_vNumY_next = labelThatHasChanged->getText().getIntValue(); //[/UserLabelCode_numVisibleYLabel] } else if (labelThatHasChanged == lambdaLabel) @@ -661,6 +708,24 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged) m_pRbm->setSigmaDecay(labelThatHasChanged->getText().getFloatValue()); //[/UserLabelCode_sigmaDecayLabel] } + else if (labelThatHasChanged == weightDecayLabel) + { + //[UserLabelCode_weightDecayLabel] -- add your label text handling code here.. + m_pRbm->setWeightDecay(labelThatHasChanged->getText().getFloatValue()); + //[/UserLabelCode_weightDecayLabel] + } + else if (labelThatHasChanged == momentumLabel) + { + //[UserLabelCode_momentumLabel] -- add your label text handling code here.. + m_pRbm->setMomentum(labelThatHasChanged->getText().getFloatValue()); + //[/UserLabelCode_momentumLabel] + } + else if (labelThatHasChanged == sparsityLearningRateLabel) + { + //[UserLabelCode_sparsityLearningRateLabel] -- add your label text handling code here.. + m_pRbm->setMuSparsity(labelThatHasChanged->getText().getFloatValue()); + //[/UserLabelCode_sparsityLearningRateLabel] + } //[UserlabelTextChanged_Post] //[/UserlabelTextChanged_Post] @@ -704,9 +769,9 @@ void MainComponent::create() numHiddenLabel->setText(String(m_hNum), dontSendNotification ); m_pRbm = new Rbm(m_weights, this); - m_pRbm->setUseExpectations(rbmUseExpectationsToggleButton->getToggleState()); + sigma_gauss = 0.5; + sigmaDecay_gauss = 0.99; m_pRbm->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState()); - m_pRbm->setDoRobbinsMonro(rbmDoRobbinsMonroToggleButton->getToggleState()); m_pRbm->setUseProbsForHiddenReconstruction(rbmReduceVarianceToggleButton->getToggleState()); m_pRbm->setUseVisibleGaussian(rbmUseVisibleGaussianToggleButton->getToggleState()); m_pRbm->setDoSparse(rbmDoSparseToggleButton->getToggleState()); @@ -714,7 +779,10 @@ void MainComponent::create() m_pRbm->setLambda(lambdaLabel->getText().getFloatValue()); m_pRbm->setSigma(sigmaLabel->getText().getFloatValue()); m_pRbm->setSigmaDecay(sigmaDecayLabel->getText().getFloatValue()); + m_pRbm->setWeightDecay(weightDecayLabel->getText().getFloatValue()); m_pRbm->setSparsity(sparsityLabel->getText().getFloatValue()); + m_pRbm->setNumGibbs((uint32_t)numGibbsSlider->getValue()); + resized(); } @@ -738,10 +806,10 @@ void MainComponent::onChanged(const LayerArray &obj) void MainComponent::onEpochTrained(const Rbm &obj) { - m_trainingProgress = obj.getProgress(); -// mylog("Training %f %%\n", m_trainingProgress*100); +// mylog("Training %f %%\n", obj.getProgress()*100); redrawReconstruction(); redrawWeights((int)WeightsSlider->getValue()); + m_progressBarSlider->setValue((int)(100*obj.getProgress()), dontSendNotification); } void MainComponent::onDraw(DrawComponent &obj) @@ -790,11 +858,8 @@ void MainComponent::redrawWeights(int index) void MainComponent::run() { trainButton->setEnabled(false); - m_progressBar->setPercentageDisplay(true); - m_pRbm->train(m_layers, numEpochslabel->getText().getIntValue(), learningRateLabel->getText().getFloatValue(), m_numGibbs); + m_pRbm->train(m_layers, numEpochslabel->getText().getIntValue()); trainButton->setEnabled(true); - m_trainingProgress = 0.0; - m_progressBar->setTextToDisplay("Finished"); } //[/MiscUserCode] @@ -827,6 +892,12 @@ BEGIN_JUCER_METADATA fontname="Default font" fontsize="15" bold="0" italic="0" justification="36"/> + + + + virtualName="" explicitFocusOrder="0" pos="224 448 184 24" min="0" + max="0" int="1" style="LinearBar" textBoxPos="TextBoxLeft" textBoxEditable="1" + textBoxWidth="80" textBoxHeight="20" skewFactor="1"/> @@ -845,9 +916,9 @@ BEGIN_JUCER_METADATA virtualName="" explicitFocusOrder="0" pos="120 448 72 24" buttonText="Shake" connectedEdges="0" needsCallback="1" radioGroupId="0"/> + virtualName="" explicitFocusOrder="0" pos="224 488 184 24" min="0" + max="0" int="1" style="LinearBar" textBoxPos="TextBoxLeft" textBoxEditable="1" + textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>