- use expectations

- improved gibbs sampling
- LayerArray is template class

git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@18 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2014-10-06 17:08:50 +00:00
parent f62f1283f8
commit fc758b853a
122 changed files with 1053 additions and 1149 deletions
+107 -32
View File
@@ -18,13 +18,16 @@
*/
//[Headers] You can add your own extra header files here...
#ifdef WIN32
#include <Windows.h>
#endif
//[/Headers]
#include "MainComponent.h"
//[MiscUserDefs] You can add your own user definitions and misc code here...
#ifdef WIN32
void mylog(const char* format, ...)
{
char log_buf[257];
@@ -34,6 +37,17 @@ void mylog(const char* format, ...)
va_end(argptr);
OutputDebugStringA (log_buf);
}
#else
void mylog(const char* format, ...)
{
char log_buf[257];
va_list argptr;
va_start(argptr, format);
vprintf(format, argptr);
va_end(argptr);
}
#endif
//[/MiscUserDefs]
//==============================================================================
@@ -53,7 +67,7 @@ MainComponent::MainComponent ()
addButton->addListener (this);
addAndMakeVisible (patterSlider = new Slider ("Pattern slider"));
patterSlider->setRange (0, 10, 1);
patterSlider->setRange (0, 0, 1);
patterSlider->setSliderStyle (Slider::LinearHorizontal);
patterSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
patterSlider->addListener (this);
@@ -67,7 +81,7 @@ MainComponent::MainComponent ()
ShakeButton->addListener (this);
addAndMakeVisible (WeightsSlider = new Slider ("Weights slider"));
WeightsSlider->setRange (0, 10, 1);
WeightsSlider->setRange (0, 0, 1);
WeightsSlider->setSliderStyle (Slider::LinearHorizontal);
WeightsSlider->setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
WeightsSlider->addListener (this);
@@ -158,6 +172,20 @@ MainComponent::MainComponent ()
removeTrainingButton->setButtonText (TRANS("Remove T"));
removeTrainingButton->addListener (this);
addAndMakeVisible (numGibbsSlider = new Slider ("Num. Gibbs slider"));
numGibbsSlider->setRange (1, 10, 1);
numGibbsSlider->setSliderStyle (Slider::LinearHorizontal);
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 (rbmUseExpectationsToggleButton = new ToggleButton ("rbmUseExpectations toggle button"));
rbmUseExpectationsToggleButton->setButtonText (TRANS("Use Expectations"));
rbmUseExpectationsToggleButton->addListener (this);
//[UserPreSize]
m_vNumX = 16;
@@ -166,11 +194,8 @@ MainComponent::MainComponent ()
m_vNumX_next = 16;
m_vNumY_next = 16;
m_hNum_next = 64;
// m_pWeights = new Weights(m_vNumX*m_vNumY, m_hNum);
// m_pWeights = new Weights("C:\\Dokumente und Einstellungen\\Jens\\Desktop\\weights.dat");
m_weights.load("C:\\Dokumente und Einstellungen\\Jens\\Desktop\\weights.dat");
m_layers.load("C:\\Dokumente und Einstellungen\\Jens\\Desktop\\training_states.dat");
m_numGibbs = 1;
m_weights.setUnits(m_vNumX*m_vNumY, m_hNum);
create();
//[/UserPreSize]
@@ -181,11 +206,9 @@ MainComponent::MainComponent ()
//[Constructor] You can add your own custom stuff here..
projectNameLabel->setText(String("TestPrj"), dontSendNotification );
numVisibleLabel->setText(String(m_vNumX), dontSendNotification );
numVisibleYLabel->setText(String(m_vNumY), dontSendNotification );
numHiddenLabel->setText(String(m_hNum), dontSendNotification );
numEpochslabel->setText(String(1000), dontSendNotification );
numEpochslabel->setText(String(100), dontSendNotification );
learningRateLabel->setText(String(0.2), dontSendNotification );
rbmUseExpectationsToggleButton->setToggleState(false, true);
//[/Constructor]
}
@@ -214,6 +237,9 @@ MainComponent::~MainComponent()
saveTrainingButton = nullptr;
clearTrainingButton = nullptr;
removeTrainingButton = nullptr;
numGibbsSlider = nullptr;
reconstructEquButton = nullptr;
rbmUseExpectationsToggleButton = nullptr;
//[Destructor]. You can add your own custom destruction code here..
@@ -245,8 +271,8 @@ void MainComponent::resized()
reconstructButton->setBounds (24, 280, 72, 24);
ShakeButton->setBounds (120, 320, 72, 24);
WeightsSlider->setBounds (224, 320, 184, 24);
numEpochslabel->setBounds (24, 248, 72, 24);
learningRateLabel->setBounds (120, 248, 72, 24);
numEpochslabel->setBounds (24, 240, 72, 24);
learningRateLabel->setBounds (120, 240, 72, 24);
testButton->setBounds (120, 176, 72, 24);
numVisibleLabel->setBounds (440, 256, 72, 24);
numHiddenLabel->setBounds (488, 288, 72, 24);
@@ -259,6 +285,9 @@ void MainComponent::resized()
saveTrainingButton->setBounds (520, 48, 72, 24);
clearTrainingButton->setBounds (520, 88, 72, 24);
removeTrainingButton->setBounds (440, 88, 72, 24);
numGibbsSlider->setBounds (224, 240, 184, 24);
reconstructEquButton->setBounds (24, 320, 72, 24);
rbmUseExpectationsToggleButton->setBounds (224, 176, 150, 24);
//[UserResized] Add your own custom resize handling here..
Draw->setBounds (16, 16, 100, 100);
Draw2->setBounds (110+16, 16, 100, 100);
@@ -274,17 +303,13 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
if (buttonThatWasClicked == trainButton)
{
//[UserButtonCode_trainButton] -- add your button handler code here..
m_pRbm->train(m_layers, numEpochslabel->getText().getIntValue(), learningRateLabel->getText().getFloatValue());
m_pRbm->train(m_layers, numEpochslabel->getText().getIntValue(), learningRateLabel->getText().getFloatValue(), m_numGibbs, m_rbmUseExpectations);
//[/UserButtonCode_trainButton]
}
else if (buttonThatWasClicked == addButton)
{
//[UserButtonCode_addButton] -- add your button handler code here..
Draw2->setData(Draw->getData());
for (int i=0; i < m_vNumX*m_vNumY; i++)
{
mylog("%3.6f\n", Draw->getData()[i]);
}
m_layers.add(Draw->getData(), m_vNumX*m_vNumY);
patterSlider->setRange (0, m_layers.getSize()-1, 1);
//[/UserButtonCode_addButton]
@@ -357,6 +382,27 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
m_layers.removeAt((int)patterSlider->getValue());
//[/UserButtonCode_removeTrainingButton]
}
else if (buttonThatWasClicked == reconstructEquButton)
{
//[UserButtonCode_reconstructEquButton] -- add your button handler code here..
uint32_t i;
const double *pV, *pH;
pV = Draw->getData();
for (i=0; i < 1000; i++)
{
pH = m_pRbm->toHidden(pV);
pV = m_pRbm->toVisible(pH);
Draw2->setData(pV);
}
//[/UserButtonCode_reconstructEquButton]
}
else if (buttonThatWasClicked == rbmUseExpectationsToggleButton)
{
//[UserButtonCode_rbmUseExpectationsToggleButton] -- add your button handler code here..
m_rbmUseExpectations = buttonThatWasClicked->getToggleState();
//[/UserButtonCode_rbmUseExpectationsToggleButton]
}
//[UserbuttonClicked_Post]
//[/UserbuttonClicked_Post]
@@ -372,7 +418,7 @@ void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved)
//[UserSliderCode_patterSlider] -- add your slider handling code here..
if (m_layers.getSize() > 0)
{
VisibleLayer &p = m_layers.getAt((int)sliderThatWasMoved->getValue());
VisibleLayer &p = (VisibleLayer&)m_layers.getAt((int)sliderThatWasMoved->getValue());
Draw2->setData(p.getStates());
}
//[/UserSliderCode_patterSlider]
@@ -380,8 +426,12 @@ void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved)
else if (sliderThatWasMoved == WeightsSlider)
{
//[UserSliderCode_WeightsSlider] -- add your slider handling code here..
double *pW = m_weights.getWeights()[(int)sliderThatWasMoved->getValue()];
ScopedPointer<double> pTemp = new double [m_vNumX*m_vNumY];
double **ppW = m_weights.getWeights();
if (!ppW)
return;
double *pW = ppW[(int)sliderThatWasMoved->getValue()];
double *pTemp = new double [m_vNumX*m_vNumY];
double min = +1E12;
double max = -1E12;
@@ -401,8 +451,15 @@ void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved)
}
DrawWeights->setData(pTemp);
delete pTemp;
//[/UserSliderCode_WeightsSlider]
}
else if (sliderThatWasMoved == numGibbsSlider)
{
//[UserSliderCode_numGibbsSlider] -- add your slider handling code here..
m_numGibbs = (uint32_t)sliderThatWasMoved->getValue();
//[/UserSliderCode_numGibbsSlider]
}
//[UsersliderValueChanged_Post]
//[/UsersliderValueChanged_Post]
@@ -457,8 +514,6 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged)
void MainComponent::load ()
{
m_weights.load((String(getBaseDir() + String(".weights.dat"))).toUTF8());
m_layers.clear();
m_layers.load((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8());
m_vNumX = m_vNumY = (int)sqrt((float)m_weights.getNumVisible());
m_hNum = m_weights.getNumHidden();
@@ -479,7 +534,10 @@ void MainComponent::create()
addAndMakeVisible (Draw = new DrawComponent (m_vNumX, m_vNumX));
addAndMakeVisible (Draw2 = new DrawComponent (m_vNumX, m_vNumX));
addAndMakeVisible (DrawWeights = new DrawComponent (m_vNumX, m_vNumX));
m_pRbm = new Rbm(m_weights);
numVisibleLabel->setText(String(m_vNumX), dontSendNotification );
numVisibleYLabel->setText(String(m_vNumY), dontSendNotification );
numHiddenLabel->setText(String(m_hNum), dontSendNotification );
m_pRbm = new Rbm(m_weights, this);
WeightsSlider->setRange(0, m_hNum-1, 1);
resized();
}
@@ -490,17 +548,23 @@ void MainComponent::destroy()
const juce::String& MainComponent::getBaseDir()
{
static String baseDir(String("C:\\Dokumente und Einstellungen\\Jens\\Desktop\\") + projectNameLabel->getText());
m_baseDir = String("./") + projectNameLabel->getText();
return baseDir;
return m_baseDir;
}
void MainComponent::onChanged(const VisibleLayerArray &obj)
void MainComponent::onChanged(const LayerArray<VisibleLayer> &obj)
{
patterSlider->setRange(0, std::max(0,(int)m_layers.getSize()-1), 1);
}
void MainComponent::onEpochTrained(const Rbm &obj)
{
m_trainingProgress = obj.getProgress();
mylog("Training %f %%\n", m_trainingProgress*100);
}
//[/MiscUserCode]
@@ -514,7 +578,7 @@ void MainComponent::onChanged(const VisibleLayerArray &obj)
BEGIN_JUCER_METADATA
<JUCER_COMPONENT documentType="Component" className="MainComponent" componentName=""
parentClasses="public Component, public VisibleLayerArrayListener"
parentClasses="public Component, public LayerArrayListener&lt;VisibleLayer&gt;, public RbmListener"
constructorParams="" variableInitialisers="m_layers(this),&#10;m_pRbm(nullptr)&#10;Draw(nullptr),&#10;Draw2(nullptr),&#10;DrawWeights(nullptr)"
snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330"
fixedSize="1" initialWidth="600" initialHeight="400">
@@ -527,7 +591,7 @@ BEGIN_JUCER_METADATA
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<SLIDER name="Pattern slider" id="c3e0a2c816db81d1" memberName="patterSlider"
virtualName="" explicitFocusOrder="0" pos="224 280 184 24" min="0"
max="10" int="1" style="LinearHorizontal" textBoxPos="TextBoxLeft"
max="0" int="1" style="LinearHorizontal" textBoxPos="TextBoxLeft"
textBoxEditable="1" textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<TEXTBUTTON name="Reconstruct button" id="c1901d121a5819d6" memberName="reconstructButton"
virtualName="" explicitFocusOrder="0" pos="24 280 72 24" buttonText="Reconstruct"
@@ -537,15 +601,15 @@ BEGIN_JUCER_METADATA
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<SLIDER name="Weights slider" id="699075a1fc01458a" memberName="WeightsSlider"
virtualName="" explicitFocusOrder="0" pos="224 320 184 24" min="0"
max="10" int="1" style="LinearHorizontal" textBoxPos="TextBoxLeft"
max="0" int="1" style="LinearHorizontal" textBoxPos="TextBoxLeft"
textBoxEditable="1" textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<LABEL name="Num Epochs label" id="b23ae372ee931474" memberName="numEpochslabel"
virtualName="" explicitFocusOrder="0" pos="24 248 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="24 240 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="99999" 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="120 248 72 24" edTextCol="ff000000"
virtualName="" explicitFocusOrder="0" pos="120 240 72 24" edTextCol="ff000000"
edBkgCol="0" labelText="0.001" editableSingleClick="1" editableDoubleClick="1"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="36"/>
@@ -593,6 +657,17 @@ BEGIN_JUCER_METADATA
<TEXTBUTTON name="Remove Training button" id="f0e1d068c39986f3" memberName="removeTrainingButton"
virtualName="" explicitFocusOrder="0" pos="440 88 72 24" buttonText="Remove T"
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<SLIDER name="Num. Gibbs slider" id="fdd9d5ca4e05e18c" memberName="numGibbsSlider"
virtualName="" explicitFocusOrder="0" pos="224 240 184 24" min="1"
max="10" int="1" style="LinearHorizontal" textBoxPos="TextBoxLeft"
textBoxEditable="1" textBoxWidth="80" textBoxHeight="20" skewFactor="1"/>
<TEXTBUTTON name="Reconstruct Equilibrium button" id="208a587f5556207" memberName="reconstructEquButton"
virtualName="" explicitFocusOrder="0" pos="24 320 72 24" buttonText="Reconst Equ."
connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<TOGGLEBUTTON name="rbmUseExpectations toggle button" id="62884e37cd027719"
memberName="rbmUseExpectationsToggleButton" virtualName="" explicitFocusOrder="0"
pos="224 176 150 24" buttonText="Use Expectations" connectedEdges="0"
needsCallback="1" radioGroupId="0" state="0"/>
</JUCER_COMPONENT>
END_JUCER_METADATA