- added DBN stack
- concentrated RBM params into structure


git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@295 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2016-06-17 22:14:55 +00:00
parent b10d06d96c
commit 46bffda38f
5 changed files with 319 additions and 198 deletions
+131 -82
View File
@@ -38,10 +38,9 @@ void mylog(const char* format, ...)
//==============================================================================
MainComponent::MainComponent ()
: Thread("RBM"),
m_pRbmComponent(nullptr),
m_layers(this)
m_pRbmComponentCurr(nullptr),
m_weightsCurr(nullptr),
m_layers(this)
{
addAndMakeVisible (trainButton = new TextButton ("Train button"));
trainButton->setButtonText (TRANS("Train"));
@@ -269,10 +268,20 @@ MainComponent::MainComponent ()
rbmNormalizeDataToggleButton->setButtonText (TRANS("Normalize data"));
rbmNormalizeDataToggleButton->addListener (this);
addAndMakeVisible (m_rbmSelect = new ComboBox ("RBM Selector"));
m_rbmSelect->setEditableText (false);
m_rbmSelect->setJustificationType (Justification::centredLeft);
m_rbmSelect->setTextWhenNothingSelected (String::empty);
m_rbmSelect->setTextWhenNoChoicesAvailable (TRANS("(no choices)"));
m_rbmSelect->addListener (this);
//[UserPreSize]
memset(m_pRbmComponent, 0, sizeof(m_pRbmComponent));
memset(m_weights, 0, sizeof(m_weights));
m_rbmSelect->addItem(String(0), 1);
m_rbmSelect->setSelectedId(1, dontSendNotification);
m_progressBarSlider->setValue(100);
create();
//[/UserPreSize]
setSize (1000, 600);
@@ -324,10 +333,10 @@ MainComponent::~MainComponent()
weightInitLabel = nullptr;
rbmLearnVarianceButton = nullptr;
rbmNormalizeDataToggleButton = nullptr;
m_rbmSelect = nullptr;
//[Destructor]. You can add your own custom destruction code here..
m_pRbmComponent = nullptr;
//[/Destructor]
}
@@ -443,6 +452,7 @@ void MainComponent::resized()
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);
//[UserResized] Add your own custom resize handling here..
//[/UserResized]
}
@@ -461,29 +471,30 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
else if (buttonThatWasClicked == addButton)
{
//[UserButtonCode_addButton] -- add your button handler code here..
m_layers.add(m_pRbmComponent->getTrainingData(), m_weights->getNumVisible());
m_pRbmComponent->addFromTraining();
m_layers.add(m_pRbmComponent[0]->getTrainingData(), m_weights[0]->getNumVisible());
m_pRbmComponent[0]->batchchanged();
//[/UserButtonCode_addButton]
}
else if (buttonThatWasClicked == reconstructButton)
{
//[UserButtonCode_reconstructButton] -- add your button handler code here..
m_pRbmComponent->redrawReconstruction();
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_reconstructButton]
}
else if (buttonThatWasClicked == ShakeButton)
{
//[UserButtonCode_ShakeButton] -- add your button handler code here..
m_weights->shuffle(weightInitLabel->getText().getFloatValue());
m_pRbmComponent->redrawWeights();
m_pRbmComponent->redrawReconstruction();
m_weightsCurr->shuffle(weightInitLabel->getText().getFloatValue());
m_pRbmComponentCurr->batchchanged();
m_pRbmComponentCurr->redrawWeights();
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_ShakeButton]
}
else if (buttonThatWasClicked == testButton)
{
//[UserButtonCode_testButton] -- add your button handler code here..
m_pRbmComponent->copyReconstructionToTraining();
m_pRbmComponent->redrawReconstruction();
m_pRbmComponentCurr->copyReconstructionToTraining();
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_testButton]
}
else if (buttonThatWasClicked == createButton)
@@ -495,7 +506,8 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
else if (buttonThatWasClicked == loadButton)
{
//[UserButtonCode_loadButton] -- add your button handler code here..
load();
m_rbmSelect->setSelectedId(1, sendNotification);
create(getBaseDir());
//[/UserButtonCode_loadButton]
}
else if (buttonThatWasClicked == saveButton)
@@ -531,48 +543,48 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
else if (buttonThatWasClicked == reconstructEquButton)
{
//[UserButtonCode_reconstructEquButton] -- add your button handler code here..
m_pRbmComponent->redrawReconstruction();
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_reconstructEquButton]
}
else if (buttonThatWasClicked == rbmDoRaoBlackwellToggleButton)
{
//[UserButtonCode_rbmDoRaoBlackwellToggleButton] -- add your button handler code here..
m_pRbmComponent->setDoRaoBlackwell(buttonThatWasClicked->getToggleState());
m_pRbmComponentCurr->setDoRaoBlackwell(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmDoRaoBlackwellToggleButton]
}
else if (buttonThatWasClicked == rbmReduceVarianceToggleButton)
{
//[UserButtonCode_rbmReduceVarianceToggleButton] -- add your button handler code here..
m_pRbmComponent->setUseProbsForHiddenReconstruction(buttonThatWasClicked->getToggleState());
m_pRbmComponentCurr->setUseProbsForHiddenReconstruction(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmReduceVarianceToggleButton]
}
else if (buttonThatWasClicked == rbmUseVisibleGaussianToggleButton)
{
//[UserButtonCode_rbmUseVisibleGaussianToggleButton] -- add your button handler code here..
m_pRbmComponent->setUseVisibleGaussian(buttonThatWasClicked->getToggleState());
m_pRbmComponent->redrawReconstruction();
m_pRbmComponentCurr->setUseVisibleGaussian(buttonThatWasClicked->getToggleState());
m_pRbmComponentCurr->redrawReconstruction();
//[/UserButtonCode_rbmUseVisibleGaussianToggleButton]
}
else if (buttonThatWasClicked == rbmDoSparseToggleButton)
{
//[UserButtonCode_rbmDoSparseToggleButton] -- add your button handler code here..
m_pRbmComponent->setDoSparse(buttonThatWasClicked->getToggleState());
m_pRbmComponentCurr->setDoSparse(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmDoSparseToggleButton]
}
else if (buttonThatWasClicked == rbmLearnVarianceButton)
{
//[UserButtonCode_rbmLearnVarianceButton] -- add your button handler code here..
m_pRbmComponent->setDoLearnVariance(buttonThatWasClicked->getToggleState());
m_pRbmComponentCurr->setDoLearnVariance(buttonThatWasClicked->getToggleState());
if (!buttonThatWasClicked->getToggleState())
{
m_pRbmComponent->setSigma(sigmaLabel->getText().getFloatValue());
m_pRbmComponentCurr->setSigma(sigmaLabel->getText().getFloatValue());
}
//[/UserButtonCode_rbmLearnVarianceButton]
}
else if (buttonThatWasClicked == rbmNormalizeDataToggleButton)
{
//[UserButtonCode_rbmNormalizeDataToggleButton] -- add your button handler code here..
m_pRbmComponent->setNormalizeData(buttonThatWasClicked->getToggleState());
m_pRbmComponentCurr->setNormalizeData(buttonThatWasClicked->getToggleState());
//[/UserButtonCode_rbmNormalizeDataToggleButton]
}
@@ -588,19 +600,19 @@ void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved)
if (sliderThatWasMoved == patterSlider)
{
//[UserSliderCode_patterSlider] -- add your slider handling code here..
m_pRbmComponent->selectTraining((int)sliderThatWasMoved->getValue());
m_pRbmComponentCurr->setTrainingIndex((int)sliderThatWasMoved->getValue());
//[/UserSliderCode_patterSlider]
}
else if (sliderThatWasMoved == WeightsSlider)
{
//[UserSliderCode_WeightsSlider] -- add your slider handling code here..
m_pRbmComponent->selectWeights((int)sliderThatWasMoved->getValue());
m_pRbmComponentCurr->setWeightsIndex((int)sliderThatWasMoved->getValue());
//[/UserSliderCode_WeightsSlider]
}
else if (sliderThatWasMoved == numGibbsSlider)
{
//[UserSliderCode_numGibbsSlider] -- add your slider handling code here..
m_pRbmComponent->setNumGibbs((uint32_t)sliderThatWasMoved->getValue());
m_pRbmComponentCurr->setNumGibbs((uint32_t)sliderThatWasMoved->getValue());
//[/UserSliderCode_numGibbsSlider]
}
else if (sliderThatWasMoved == m_progressBarSlider)
@@ -626,7 +638,7 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged)
else if (labelThatHasChanged == learningRateLabel)
{
//[UserLabelCode_learningRateLabel] -- add your label text handling code here..
m_pRbmComponent->setMuWeights(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->setMuWeights(labelThatHasChanged->getText().getFloatValue());
//[/UserLabelCode_learningRateLabel]
}
else if (labelThatHasChanged == numVisibleLabel)
@@ -652,44 +664,44 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged)
else if (labelThatHasChanged == lambdaLabel)
{
//[UserLabelCode_lambdaLabel] -- add your label text handling code here..
m_pRbmComponent->setLambda(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponent->redrawReconstruction();
m_pRbmComponentCurr->setLambda(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->redrawReconstruction();
//[/UserLabelCode_lambdaLabel]
}
else if (labelThatHasChanged == sigmaLabel)
{
//[UserLabelCode_sigmaLabel] -- add your label text handling code here..
m_pRbmComponent->setSigma(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->setSigma(labelThatHasChanged->getText().getFloatValue());
//[/UserLabelCode_sigmaLabel]
}
else if (labelThatHasChanged == sparsityLabel)
{
//[UserLabelCode_sparsityLabel] -- add your label text handling code here..
m_pRbmComponent->setSparsity(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->setSparsity(labelThatHasChanged->getText().getFloatValue());
//[/UserLabelCode_sparsityLabel]
}
else if (labelThatHasChanged == sigmaDecayLabel)
{
//[UserLabelCode_sigmaDecayLabel] -- add your label text handling code here..
m_pRbmComponent->setSigmaDecay(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->setSigmaDecay(labelThatHasChanged->getText().getFloatValue());
//[/UserLabelCode_sigmaDecayLabel]
}
else if (labelThatHasChanged == weightDecayLabel)
{
//[UserLabelCode_weightDecayLabel] -- add your label text handling code here..
m_pRbmComponent->setWeightDecay(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->setWeightDecay(labelThatHasChanged->getText().getFloatValue());
//[/UserLabelCode_weightDecayLabel]
}
else if (labelThatHasChanged == momentumLabel)
{
//[UserLabelCode_momentumLabel] -- add your label text handling code here..
m_pRbmComponent->setMomentum(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->setMomentum(labelThatHasChanged->getText().getFloatValue());
//[/UserLabelCode_momentumLabel]
}
else if (labelThatHasChanged == sparsityLearningRateLabel)
{
//[UserLabelCode_sparsityLearningRateLabel] -- add your label text handling code here..
m_pRbmComponent->setMuSparsity(labelThatHasChanged->getText().getFloatValue());
m_pRbmComponentCurr->setMuSparsity(labelThatHasChanged->getText().getFloatValue());
//[/UserLabelCode_sparsityLearningRateLabel]
}
else if (labelThatHasChanged == weightInitLabel)
@@ -702,6 +714,29 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged)
//[/UserlabelTextChanged_Post]
}
void MainComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
{
//[UsercomboBoxChanged_Pre]
//[/UsercomboBoxChanged_Pre]
if (comboBoxThatHasChanged == m_rbmSelect)
{
//[UserComboBoxCode_m_rbmSelect] -- add your combo box handling code here..
m_pRbmComponentCurr = m_pRbmComponent[m_rbmSelect->getSelectedId()-1];
m_weightsCurr = m_weights[m_rbmSelect->getSelectedId()-1];
if (m_pRbmComponentCurr == nullptr)
{
create();
}
updateControls();
//[/UserComboBoxCode_m_rbmSelect]
}
//[UsercomboBoxChanged_Post]
//[/UsercomboBoxChanged_Post]
}
void MainComponent::mouseMove (const MouseEvent& e)
{
//[UserCode_mouseMove] -- Add your code here...
@@ -755,61 +790,44 @@ void MainComponent::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails
//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
void MainComponent::load ()
{
create(getBaseDir());
}
void MainComponent::save ()
{
m_weights->save((String(getBaseDir() + String(".weights.dat"))).toUTF8());
m_weights[0]->save((String(getBaseDir() + String(".weights.dat"))).toUTF8());
}
void MainComponent::create(juce::String const &projectName)
{
m_weights = nullptr;
m_pRbmComponent = nullptr;
size_t id = m_rbmSelect->getSelectedId()-1;
if ((m_pRbmComponent[id] == nullptr) and ((id+1) < DBN_SIZE))
m_rbmSelect->addItem(String(id+1), id+2);
if (!projectName.isEmpty())
m_weights[id] = nullptr;
m_pRbmComponent[id] = nullptr;
if (id == 0)
{
m_weights = new Weights((String(projectName + String(".weights.dat"))).toUTF8());
loadTraining((String(projectName + String(".trainingStates.dat"))).toUTF8());
if (!projectName.isEmpty())
{
m_weights[id] = new Weights((String(projectName + String(".weights.dat"))).toUTF8());
loadTraining((String(projectName + String(".trainingStates.dat"))).toUTF8());
}
else
{
m_weights[id] = new Weights(numVisibleLabel->getText().getIntValue(), numVisibleYLabel->getText().getIntValue(), numHiddenLabel->getText().getIntValue());
}
addAndMakeVisible(m_pRbmComponent[id] = new RbmComponent(*m_weights[id], m_layers.data(), *this));
}
else
{
m_weights = new Weights(numVisibleLabel->getText().getIntValue(), numVisibleYLabel->getText().getIntValue(), numHiddenLabel->getText().getIntValue());
m_weights[id] = new Weights(m_weights[id-1]->getNumHidden(), 1, numHiddenLabel->getText().getIntValue());
addAndMakeVisible(m_pRbmComponent[id] = new RbmComponent(*m_weights[id], m_pRbmComponent[id-1]->getHiddenBatch(), *this));
}
size_t vNumX = m_weights->getNumVisibleX();
size_t vNumY = m_weights->getNumVisibleY();
size_t hNum = m_weights->getNumHidden();
WeightsSlider->setRange(0, hNum-1, 1);
numVisibleLabel->setText(String(vNumX), dontSendNotification );
numVisibleYLabel->setText(String(vNumY), dontSendNotification );
numHiddenLabel->setText(String(hNum), dontSendNotification );
addAndMakeVisible(m_pRbmComponent = new RbmComponent(*m_weights, m_layers.data(), *this));
m_pRbmComponent->setBounds (16, 16, 430, 130);
m_pRbmComponent->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState());
m_pRbmComponent->setUseProbsForHiddenReconstruction(rbmReduceVarianceToggleButton->getToggleState());
m_pRbmComponent->setUseVisibleGaussian(rbmUseVisibleGaussianToggleButton->getToggleState());
m_pRbmComponent->setDoSparse(rbmDoSparseToggleButton->getToggleState());
m_pRbmComponent->setLambda(lambdaLabel->getText().getFloatValue());
m_pRbmComponent->setSigmaDecay(sigmaDecayLabel->getText().getFloatValue());
m_pRbmComponent->setWeightDecay(weightDecayLabel->getText().getFloatValue());
m_pRbmComponent->setSparsity(sparsityLabel->getText().getFloatValue());
m_pRbmComponent->setNumGibbs((uint32_t)numGibbsSlider->getValue());
m_pRbmComponent->setDoLearnVariance(rbmLearnVarianceButton->getToggleState());
if (!rbmLearnVarianceButton->getToggleState())
{
m_pRbmComponent->setSigma(sigmaLabel->getText().getFloatValue());
}
m_pRbmComponent->redrawReconstruction();
m_pRbmComponent->selectWeights((int)WeightsSlider->getValue());
m_pRbmComponent->selectTraining((int)patterSlider->getValue());
m_pRbmComponentCurr = m_pRbmComponent[id];
m_weightsCurr = m_weights[id];
m_pRbmComponentCurr->setBounds (16, 140*id+16, 430, 130);
m_pRbmComponentCurr->batchchanged();
m_pRbmComponentCurr->redrawReconstruction();
updateControls();
}
void MainComponent::destroy()
@@ -827,7 +845,7 @@ const juce::String& MainComponent::getBaseDir()
void MainComponent::run()
{
// trainButton->setEnabled(false);
m_pRbmComponent->train(numEpochslabel->getText().getIntValue());
m_pRbmComponentCurr->train(numEpochslabel->getText().getIntValue());
// trainButton->setEnabled(true);
}
@@ -841,6 +859,34 @@ void MainComponent::onRbmEpochTrained(size_t progressPercent)
m_progressBarSlider->setValue(progressPercent);
}
void MainComponent::updateControls()
{
rbmDoRaoBlackwellToggleButton->setToggleState(m_pRbmComponentCurr->params().m_doRaoBlackwell, dontSendNotification);
rbmReduceVarianceToggleButton->setToggleState(m_pRbmComponentCurr->params().m_useProbsForHiddenReconstruction, 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);
lambdaLabel->setText(String(m_pRbmComponentCurr->params().m_lambda), dontSendNotification);
sigmaDecayLabel->setText(String(m_pRbmComponentCurr->params().m_sigmaDecay), dontSendNotification);
weightDecayLabel->setText(String(m_pRbmComponentCurr->params().m_weightDecay), dontSendNotification);
sparsityLabel->setText(String(m_pRbmComponentCurr->params().m_sparsity), dontSendNotification);
sigmaLabel->setText(String(m_pRbmComponentCurr->params().m_constantSigma), dontSendNotification);
sparsityLearningRateLabel->setText(String(m_pRbmComponentCurr->params().m_muSparsity), dontSendNotification);
learningRateLabel->setText(String(m_pRbmComponentCurr->params().m_muWeights), dontSendNotification);
momentumLabel->setText(String(m_pRbmComponentCurr->params().m_momentum), dontSendNotification);
numVisibleLabel->setText(String(m_weightsCurr->getNumVisibleX()), dontSendNotification );
numVisibleYLabel->setText(String(m_weightsCurr->getNumVisibleY()), dontSendNotification );
numHiddenLabel->setText(String(m_weightsCurr->getNumHidden()), dontSendNotification );
WeightsSlider->setRange(0, m_weightsCurr->getNumHidden()-1, 1);
numGibbsSlider->setValue(m_pRbmComponentCurr->params().m_numGibbs);
WeightsSlider->setValue(m_pRbmComponentCurr->getWeightsIndex());
patterSlider->setValue(m_pRbmComponentCurr->getTrainingIndex());
}
//[/MiscUserCode]
@@ -854,8 +900,8 @@ void MainComponent::onRbmEpochTrained(size_t progressPercent)
BEGIN_JUCER_METADATA
<JUCER_COMPONENT documentType="Component" className="MainComponent" componentName=""
parentClasses="public Component, public RbmComponentListener, public Thread"
constructorParams="" variableInitialisers="Thread(&quot;RBM&quot;),&#10;m_pRbmComponent(nullptr)"
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">
<METHODS>
@@ -1036,6 +1082,9 @@ BEGIN_JUCER_METADATA
<TOGGLEBUTTON name="rbmNormalizeData toggle button" id="739772af1b096120" memberName="rbmNormalizeDataToggleButton"
virtualName="" explicitFocusOrder="0" pos="808 92 160 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"
layout="33" items="" textWhenNonSelected="" textWhenNoItems="(no choices)"/>
</JUCER_COMPONENT>
END_JUCER_METADATA
+12 -4
View File
@@ -43,7 +43,8 @@ class MainComponent : public Component,
public Thread,
public ButtonListener,
public SliderListener,
public LabelListener
public LabelListener,
public ComboBoxListener
{
public:
//==============================================================================
@@ -61,6 +62,7 @@ public:
void buttonClicked (Button* buttonThatWasClicked);
void sliderValueChanged (Slider* sliderThatWasMoved);
void labelTextChanged (Label* labelThatHasChanged);
void comboBoxChanged (ComboBox* comboBoxThatHasChanged);
void mouseMove (const MouseEvent& e);
void mouseEnter (const MouseEvent& e);
void mouseExit (const MouseEvent& e);
@@ -74,10 +76,12 @@ public:
private:
//[UserVariables] -- You can add your own custom variables in this section.
ScopedPointer<Weights> m_weights;
ScopedPointer<RbmComponent> m_pRbmComponent;
static const size_t DBN_SIZE = 4;
ScopedPointer<Weights> m_weights[DBN_SIZE];
ScopedPointer<RbmComponent> m_pRbmComponent[DBN_SIZE];
Weights *m_weightsCurr;
RbmComponent *m_pRbmComponentCurr;
LayerArray m_layers;
void load();
void save();
void create(juce::String const &projectName="");
void destroy();
@@ -104,6 +108,9 @@ private:
{
m_layers.removeAt(index);
}
void updateControls();
//[/UserVariables]
//==============================================================================
@@ -144,6 +151,7 @@ private:
ScopedPointer<Label> weightInitLabel;
ScopedPointer<ToggleButton> rbmLearnVarianceButton;
ScopedPointer<ToggleButton> rbmNormalizeDataToggleButton;
ScopedPointer<ComboBox> m_rbmSelect;
//==============================================================================
+122 -92
View File
@@ -38,27 +38,50 @@ public:
class Rbm
{
public:
struct Params
{
Params()
: m_constantSigma(1.0)
, m_sigmaDecay(1.0)
, m_weightDecay(0.0)
, m_lambda(1.0)
, m_sparsity(0.05)
, m_muWeights(0.01)
, m_muSparsity(0.01)
, m_momentum(0.5)
, m_useVisibleGaussian(false)
, m_doRaoBlackwell(false)
, m_useProbsForHiddenReconstruction(false)
, m_doSparse(false)
, m_doNormalizeData(false)
, m_doLearnVariance(false)
, m_numGibbs(1)
{
}
double m_constantSigma;
double m_sigmaDecay;
double m_weightDecay;
double m_lambda;
double m_sparsity;
double m_muWeights;
double m_muSparsity;
double m_momentum;
bool m_useVisibleGaussian;
bool m_doRaoBlackwell;
bool m_useProbsForHiddenReconstruction;
bool m_doSparse;
bool m_doNormalizeData;
bool m_doLearnVariance;
uint32_t m_numGibbs;
};
Rbm(Weights &weights, const MatrixXd &batch, RbmListener *pListener = nullptr)
: m_w(weights)
, m_batch(batch)
, m_variableSigma(weights.getNumVisible())
, m_constantSigma(1.0)
, m_pListener(pListener)
, m_progress(0)
, m_sigmaDecay(1.0)
, m_weightDecay(0.0)
, m_lambda(1.0)
, m_sparsity(0)
, m_muWeights(0.01)
, m_muSparsity(0.01)
, m_momentum(0.5)
, m_useVisibleGaussian(false)
, m_doRaoBlackwell(false)
, m_useProbsForHiddenReconstruction(false)
, m_doSparse(false)
, m_doNormalizeData(false)
, m_doLearnVariance(false)
, m_numGibbs(1)
{
Noise_Init(&m_noise, 0x32727155);
@@ -72,6 +95,8 @@ public:
cout << b << endl;
#endif
m_variableSigma.fill(m_params.m_constantSigma);
updateHiddenBatch();
}
~Rbm()
@@ -79,13 +104,18 @@ public:
Noise_Free(&m_noise);
}
void sample(MatrixXd &src)
void sample(MatrixXd &srcDst)
{
sample(srcDst, srcDst);
}
void sample(MatrixXd &dst, MatrixXd const &src)
{
uint32_t i;
for (i=0; i < src.array().size(); i++)
{
src.array()(i) = (double)(src.array()(i) > Noise_Uniform(&m_noise));
dst.array()(i) = (double)(src.array()(i) > Noise_Uniform(&m_noise));
}
}
@@ -232,7 +262,7 @@ public:
m_v.resize(batchSize, m_w.getNumVisible());
m_h.resize(batchSize, m_w.getNumHidden());
MatrixXd h(batchSize, m_w.getNumHidden());
MatrixXd sumBiasV(1, m_w.getNumVisible());
MatrixXd sumBiasH(1, m_w.getNumHidden());
@@ -248,7 +278,7 @@ public:
MatrixXd batch = m_batch;
if (m_doNormalizeData)
if (m_params.m_doNormalizeData)
{
RowVectorXd mean = calcMean(batch);
for (i=0; i < batchSize; i++)
@@ -258,37 +288,40 @@ public:
}
}
if (!m_params.m_useProbsForHiddenReconstruction)
{
sample(batch);
}
for (epoch=0; epoch < numEpochs; epoch++)
{
double err;
// Create hidden layer base on training data
toHiddenBatch(m_h, batch);
probsLogistic(m_h);
if (!m_doRaoBlackwell)
toHiddenBatch(h, batch);
if (!m_params.m_doRaoBlackwell)
{
sample(m_h);
sample(h);
}
// Update weights (positive phase)
sumBiasV = batch.colwise().sum();
if (!m_doSparse)
if (!m_params.m_doSparse)
{
sumBiasH = m_h.colwise().sum();
sumBiasH = h.colwise().sum();
}
sumWeights = batch.transpose() * m_h;
sumWeights = batch.transpose() * h;
diffErr = batch;
for (gibbs=0; gibbs < m_numGibbs; gibbs++)
for (gibbs=0; gibbs < m_params.m_numGibbs; gibbs++)
{
sample(m_h);
sample(h);
// Create visible reconstruction (a fantasy...) given h
toVisibleBatch(m_v, m_h);
if (m_useVisibleGaussian)
toVisibleBatch(m_v, h);
if (m_params.m_useVisibleGaussian)
{
if (!m_useProbsForHiddenReconstruction)
if (!m_params.m_useProbsForHiddenReconstruction)
{
sampleGaussian(m_v, m_variableSigma.replicate(batchSize, 1));
}
@@ -296,59 +329,57 @@ public:
else
{
probsLogistic(m_v, m_variableSigma.replicate(batchSize, 1));
if (!m_useProbsForHiddenReconstruction)
if (!m_params.m_useProbsForHiddenReconstruction)
{
sample(m_v);
}
}
// Create hidden representation given v
toHiddenBatch(m_h, m_v);
probsLogistic(m_h);
toHiddenBatch(h, m_v);
}
if (!m_doRaoBlackwell)
if (!m_params.m_doRaoBlackwell)
{
sample(m_h);
sample(h);
}
// Update weights (negative phase)
sumBiasV -= m_v.colwise().sum();
if (!m_doSparse)
if (!m_params.m_doSparse)
{
sumBiasH -= m_h.colwise().sum();
sumBiasH -= h.colwise().sum();
}
sumWeights -= m_v.transpose() * m_h;
sumWeights -= m_v.transpose() * h;
diffErr -= m_v;
deltaWeights = m_momentum*deltaWeights + m_muWeights*(kTrain*sumWeights - m_weightDecay*m_w.weights());
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_momentum*deltaBiasV + m_muWeights*kTrain*sumBiasV;
deltaBiasV = m_params.m_momentum*deltaBiasV + m_params.m_muWeights*kTrain*sumBiasV;
m_w.visibleBias() += deltaBiasV;
if (m_doSparse)
if (m_params.m_doSparse)
{
// Create hidden representation given v
toHiddenBatch(m_h, m_v);
probsLogistic(m_h);
toHiddenBatch(h, m_v);
sumBiasH.fill(m_sparsity);
sumBiasH -= m_h.colwise().mean();
sumBiasH.fill(m_params.m_sparsity);
sumBiasH -= h.colwise().mean();
deltaBiasH = m_momentum*deltaBiasH + m_muSparsity*sumBiasH;
deltaBiasH = m_params.m_momentum*deltaBiasH + m_params.m_muSparsity*sumBiasH;
// cout << "Mean(" << m_sparsity << ") = " << (double)sumBiasH.array().mean() << endl;
// cout << sumBiasH << endl;
}
else
{
deltaBiasH = m_momentum*deltaBiasH + m_muWeights*kTrain*sumBiasH;
deltaBiasH = m_params.m_momentum*deltaBiasH + m_params.m_muWeights*kTrain*sumBiasH;
}
m_w.hiddenBias() += deltaBiasH;
if (m_variableSigma[0] > sigmaMin)
{
m_variableSigma.array() *= m_sigmaDecay;
m_variableSigma.array() *= m_params.m_sigmaDecay;
}
m_progress += dProgress;
@@ -364,6 +395,9 @@ public:
cout << err << endl;
} // Number of epochs
updateHiddenBatch();
}
double getProgress() const
@@ -395,7 +429,7 @@ public:
v = h * m_w.weights().transpose();
v += m_w.visibleBias();
if (m_useVisibleGaussian)
if (m_params.m_useVisibleGaussian)
{
// probsGaussian(v, m_sigmas);
}
@@ -407,13 +441,13 @@ public:
void setConstantSigma(double value)
{
m_constantSigma = value;
m_variableSigma.fill(m_constantSigma);
m_params.m_constantSigma = value;
m_variableSigma.fill(m_params.m_constantSigma);
}
double getConstantSigma()
{
return m_constantSigma;
return m_params.m_constantSigma;
}
RowVectorXd& getVariableSigma()
@@ -423,85 +457,80 @@ public:
void setSigmaDecay(double value)
{
m_sigmaDecay = value;
m_params.m_sigmaDecay = value;
}
void setWeightDecay(double value)
{
m_weightDecay = value;
m_params.m_weightDecay = value;
}
void setLambda(double value)
{
m_lambda = value;
m_params.m_lambda = value;
}
void setSparsity(double value)
{
m_sparsity = value;
m_params.m_sparsity = value;
}
void setUseVisibleGaussian(bool flag)
{
m_useVisibleGaussian = flag;
m_params.m_useVisibleGaussian = flag;
}
void setDoRaoBlackwell(bool flag)
{
m_doRaoBlackwell = flag;
m_params.m_doRaoBlackwell = flag;
}
void setUseProbsForHiddenReconstruction(bool flag)
{
m_useProbsForHiddenReconstruction = flag;
m_params.m_useProbsForHiddenReconstruction = flag;
}
void setDoSparse(bool flag)
{
m_doSparse = flag;
m_params.m_doSparse = flag;
}
void setNormalizeData(bool flag)
{
m_doNormalizeData = flag;
m_params.m_doNormalizeData = flag;
}
void setDoLearnVariance(bool flag)
{
m_doLearnVariance = flag;
if (m_doLearnVariance && m_batch.rows())
m_params.m_doLearnVariance = flag;
if (m_params.m_doLearnVariance && m_batch.rows())
{
m_variableSigma = calcSigma(m_batch);
}
else
{
m_variableSigma.fill(m_constantSigma);
m_variableSigma.fill(m_params.m_constantSigma);
}
}
void setNumGibbs(uint32_t value)
{
m_numGibbs = value;
}
uint32_t getNumGibbs()
{
return m_numGibbs;
m_params.m_numGibbs = value;
}
void setMuWeights(double value)
{
m_muWeights = value;
m_params.m_muWeights = value;
}
void setMuSparsity(double value)
{
m_muSparsity = value;
m_params.m_muSparsity = value;
}
void setMomentum(double value)
{
m_momentum = value;
m_params.m_momentum = value;
}
MatrixXd const& getHiddenBatch()
@@ -519,35 +548,36 @@ public:
return m_batch;
}
void updateHiddenBatch()
{
m_h.resize(m_batch.rows(), m_w.getNumHidden());
toHiddenBatch(m_h, m_batch);
}
Params const& params()
{
return m_params;
}
private:
Weights &m_w;
MatrixXd const &m_batch;
MatrixXd m_v;
MatrixXd m_h;
RowVectorXd m_variableSigma;
double m_constantSigma;
RbmListener *m_pListener;
noise_gen_t m_noise;
double m_progress;
double m_sigmaDecay;
double m_weightDecay;
double m_lambda;
double m_sparsity;
double m_muWeights;
double m_muSparsity;
double m_momentum;
bool m_useVisibleGaussian;
bool m_doRaoBlackwell;
bool m_useProbsForHiddenReconstruction;
bool m_doSparse;
bool m_doNormalizeData;
bool m_doLearnVariance;
uint32_t m_numGibbs;
Params m_params;
void toHiddenBatch(MatrixXd &h, MatrixXd const &v)
{
h = v * m_w.weights();
h += m_w.hiddenBias().replicate(m_batch.rows(), 1);
if (v.cols() == m_w.weights().rows())
{
h = v * m_w.weights();
h += m_w.hiddenBias().replicate(m_batch.rows(), 1);
probsLogistic(h);
}
}
void toVisibleBatch(MatrixXd &v, MatrixXd const &h)
+25 -11
View File
@@ -48,6 +48,9 @@ RbmComponent::RbmComponent (Weights &weights, MatrixXd const &batch, RbmComponen
addAndMakeVisible (DrawVars = new DrawComponent (vNumX, vNumY));
addAndMakeVisible (DrawHidden = new DrawComponent (hNum, 1));
DrawHidden->setListener(this);
redrawWeights();
redrawReconstruction();
redrawVariances();
//[/UserPreSize]
setSize (430, 130);
@@ -229,7 +232,7 @@ void RbmComponent::redrawReconstruction()
m_pRbm->toHidden(DrawHidden->getData(), DrawTraining->getData());
m_pRbm->toVisible(DrawReconstruction->getData(), DrawHidden->getData());
for (i=0; i < m_pRbm->getNumGibbs()-1; i++)
for (i=0; i < m_pRbm->params().m_numGibbs-1; i++)
{
m_pRbm->toHidden(DrawHidden->getData(), DrawReconstruction->getData());
m_pRbm->toVisible(DrawReconstruction->getData(), DrawHidden->getData());
@@ -244,12 +247,16 @@ void RbmComponent::redrawWeights()
DrawWeights->DrawData();
}
void RbmComponent::redrawVariances()
{
DrawVars->getData() = m_pRbm->getVariableSigma();
DrawVars->DrawData();
}
void RbmComponent::setDoLearnVariance(bool value)
{
m_pRbm->setDoLearnVariance(value);
DrawVars->getData() = m_pRbm->getVariableSigma();
DrawVars->DrawData();
redrawVariances();
redrawReconstruction();
}
@@ -303,9 +310,7 @@ void RbmComponent::setNumGibbs(size_t value)
void RbmComponent::setSigma(double value)
{
m_pRbm->setConstantSigma(value);
DrawVars->getData() = m_pRbm->getVariableSigma();
DrawVars->DrawData();
redrawVariances();
redrawReconstruction();
}
@@ -329,11 +334,10 @@ void RbmComponent::setMomentum(double value)
m_pRbm->setMomentum(value);
}
void RbmComponent::addFromTraining()
void RbmComponent::batchchanged()
{
DrawReconstruction->getData() = DrawTraining->getData();
DrawReconstruction->DrawData();
m_pRbm->updateHiddenBatch();
redrawVariances();
}
void RbmComponent::setWeightsIndex(size_t index)
@@ -342,6 +346,11 @@ void RbmComponent::setWeightsIndex(size_t index)
redrawWeights();
}
size_t RbmComponent::getWeightsIndex()
{
return m_currWeightIndexToDraw;
}
void RbmComponent::setTrainingIndex(size_t index)
{
if (m_pRbm->getBatch().rows() > 0)
@@ -354,6 +363,11 @@ void RbmComponent::setTrainingIndex(size_t index)
}
}
size_t RbmComponent::getTrainingIndex()
{
return m_currTrainingIndexToDraw;
}
void RbmComponent::copyReconstructionToTraining()
{
DrawTraining->getData() = DrawReconstruction->getData();
+22 -2
View File
@@ -26,6 +26,16 @@
#include "Rbm.hpp"
//[/Headers]
class IRbm
{
public:
IRbm() {}
virtual ~IRbm()
{
}
};
class RbmComponentListener
{
public:
@@ -84,19 +94,29 @@ public:
void setMuWeights(double value);
void setMuSparsity(double value);
void setMomentum(double value);
void addFromTraining();
void batchchanged();
void setWeightsIndex(size_t index);
size_t getWeightsIndex();
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 getHiddenBatch()
MatrixXd const& getHiddenBatch()
{
return m_pRbm->getHiddenBatch();
}
Rbm::Params const& params()
{
return m_pRbm->params();
}
private:
//[UserVariables] -- You can add your own custom variables in this section.
ScopedPointer<Rbm> m_pRbm;