more stable
git-svn-id: http://moon:8086/svn/software/trunk/projects/RBM@286 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -64,6 +64,7 @@ DrawComponent::~DrawComponent()
|
|||||||
//[Destructor]. You can add your own custom destruction code here..
|
//[Destructor]. You can add your own custom destruction code here..
|
||||||
m_pG = nullptr;
|
m_pG = nullptr;
|
||||||
m_pListener = nullptr;
|
m_pListener = nullptr;
|
||||||
|
m_data.resize(0);
|
||||||
//[/Destructor]
|
//[/Destructor]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+41
-37
@@ -96,7 +96,7 @@ MainComponent::MainComponent ()
|
|||||||
testButton->addListener (this);
|
testButton->addListener (this);
|
||||||
|
|
||||||
addAndMakeVisible (numVisibleLabel = new Label ("Num Visible label",
|
addAndMakeVisible (numVisibleLabel = new Label ("Num Visible label",
|
||||||
TRANS("99999")));
|
TRANS("16")));
|
||||||
numVisibleLabel->setFont (Font (15.00f, Font::plain));
|
numVisibleLabel->setFont (Font (15.00f, Font::plain));
|
||||||
numVisibleLabel->setJustificationType (Justification::centred);
|
numVisibleLabel->setJustificationType (Justification::centred);
|
||||||
numVisibleLabel->setEditable (true, true, false);
|
numVisibleLabel->setEditable (true, true, false);
|
||||||
@@ -105,7 +105,7 @@ MainComponent::MainComponent ()
|
|||||||
numVisibleLabel->addListener (this);
|
numVisibleLabel->addListener (this);
|
||||||
|
|
||||||
addAndMakeVisible (numHiddenLabel = new Label ("Num Hidden label",
|
addAndMakeVisible (numHiddenLabel = new Label ("Num Hidden label",
|
||||||
TRANS("99999")));
|
TRANS("64")));
|
||||||
numHiddenLabel->setFont (Font (15.00f, Font::plain));
|
numHiddenLabel->setFont (Font (15.00f, Font::plain));
|
||||||
numHiddenLabel->setJustificationType (Justification::centred);
|
numHiddenLabel->setJustificationType (Justification::centred);
|
||||||
numHiddenLabel->setEditable (true, true, false);
|
numHiddenLabel->setEditable (true, true, false);
|
||||||
@@ -135,7 +135,7 @@ MainComponent::MainComponent ()
|
|||||||
saveButton->addListener (this);
|
saveButton->addListener (this);
|
||||||
|
|
||||||
addAndMakeVisible (numVisibleYLabel = new Label ("Num Visible Y label",
|
addAndMakeVisible (numVisibleYLabel = new Label ("Num Visible Y label",
|
||||||
TRANS("99999")));
|
TRANS("16")));
|
||||||
numVisibleYLabel->setFont (Font (15.00f, Font::plain));
|
numVisibleYLabel->setFont (Font (15.00f, Font::plain));
|
||||||
numVisibleYLabel->setJustificationType (Justification::centred);
|
numVisibleYLabel->setJustificationType (Justification::centred);
|
||||||
numVisibleYLabel->setEditable (true, true, false);
|
numVisibleYLabel->setEditable (true, true, false);
|
||||||
@@ -273,14 +273,8 @@ MainComponent::MainComponent ()
|
|||||||
|
|
||||||
|
|
||||||
//[UserPreSize]
|
//[UserPreSize]
|
||||||
m_vNumX = 16;
|
|
||||||
m_vNumY = 16;
|
|
||||||
m_hNum = 64;
|
|
||||||
m_numGibbs = 1;
|
m_numGibbs = 1;
|
||||||
m_weights.setUnits(m_vNumX, m_vNumY, m_hNum);
|
|
||||||
m_pRbm = new Rbm(m_weights, this);
|
|
||||||
create();
|
create();
|
||||||
|
|
||||||
//[/UserPreSize]
|
//[/UserPreSize]
|
||||||
|
|
||||||
setSize (800, 600);
|
setSize (800, 600);
|
||||||
@@ -494,7 +488,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
|
|||||||
else if (buttonThatWasClicked == ShakeButton)
|
else if (buttonThatWasClicked == ShakeButton)
|
||||||
{
|
{
|
||||||
//[UserButtonCode_ShakeButton] -- add your button handler code here..
|
//[UserButtonCode_ShakeButton] -- add your button handler code here..
|
||||||
m_weights.shuffle(weightInitLabel->getText().getFloatValue());
|
m_weights->shuffle(weightInitLabel->getText().getFloatValue());
|
||||||
redrawReconstruction();
|
redrawReconstruction();
|
||||||
redrawWeights((int)WeightsSlider->getValue());
|
redrawWeights((int)WeightsSlider->getValue());
|
||||||
//[/UserButtonCode_ShakeButton]
|
//[/UserButtonCode_ShakeButton]
|
||||||
@@ -509,11 +503,6 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
|
|||||||
else if (buttonThatWasClicked == createButton)
|
else if (buttonThatWasClicked == createButton)
|
||||||
{
|
{
|
||||||
//[UserButtonCode_createButton] -- add your button handler code here..
|
//[UserButtonCode_createButton] -- add your button handler code here..
|
||||||
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();
|
create();
|
||||||
//[/UserButtonCode_createButton]
|
//[/UserButtonCode_createButton]
|
||||||
}
|
}
|
||||||
@@ -521,8 +510,6 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
|
|||||||
{
|
{
|
||||||
//[UserButtonCode_loadButton] -- add your button handler code here..
|
//[UserButtonCode_loadButton] -- add your button handler code here..
|
||||||
load();
|
load();
|
||||||
redrawReconstruction();
|
|
||||||
redrawWeights((int)WeightsSlider->getValue());
|
|
||||||
//[/UserButtonCode_loadButton]
|
//[/UserButtonCode_loadButton]
|
||||||
}
|
}
|
||||||
else if (buttonThatWasClicked == saveButton)
|
else if (buttonThatWasClicked == saveButton)
|
||||||
@@ -536,7 +523,6 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
|
|||||||
//[UserButtonCode_loadTrainingButton] -- add your button handler code here..
|
//[UserButtonCode_loadTrainingButton] -- add your button handler code here..
|
||||||
m_layers.clear();
|
m_layers.clear();
|
||||||
m_layers.load((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8());
|
m_layers.load((String(getBaseDir() + String(".trainingStates.dat"))).toUTF8());
|
||||||
redrawReconstruction();
|
|
||||||
//[/UserButtonCode_loadTrainingButton]
|
//[/UserButtonCode_loadTrainingButton]
|
||||||
}
|
}
|
||||||
else if (buttonThatWasClicked == saveTrainingButton)
|
else if (buttonThatWasClicked == saveTrainingButton)
|
||||||
@@ -799,26 +785,35 @@ void MainComponent::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails
|
|||||||
//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
|
//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
|
||||||
void MainComponent::load ()
|
void MainComponent::load ()
|
||||||
{
|
{
|
||||||
m_weights.load((String(getBaseDir() + String(".weights.dat"))).toUTF8());
|
create((String(getBaseDir() + String(".weights.dat"))).toUTF8());
|
||||||
|
|
||||||
m_vNumX = m_weights.getNumVisibleX();
|
|
||||||
m_vNumY = m_weights.getNumVisibleY();
|
|
||||||
m_hNum = m_weights.getNumHidden();
|
|
||||||
create();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainComponent::save ()
|
void MainComponent::save ()
|
||||||
{
|
{
|
||||||
m_weights.save((String(getBaseDir() + String(".weights.dat"))).toUTF8());
|
m_weights->save((String(getBaseDir() + String(".weights.dat"))).toUTF8());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainComponent::create()
|
void MainComponent::create(const char *pFilename)
|
||||||
{
|
{
|
||||||
|
m_weights = nullptr;
|
||||||
DrawTraining = nullptr;
|
DrawTraining = nullptr;
|
||||||
DrawReconstruction = nullptr;
|
DrawReconstruction = nullptr;
|
||||||
DrawWeights = nullptr;
|
DrawWeights = nullptr;
|
||||||
DrawVars = nullptr;
|
DrawVars = nullptr;
|
||||||
DrawHidden = nullptr;
|
DrawHidden = nullptr;
|
||||||
|
m_pRbm = nullptr;
|
||||||
|
|
||||||
|
if (pFilename)
|
||||||
|
{
|
||||||
|
m_weights = new Weights(pFilename);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_weights = new Weights(numVisibleLabel->getText().getIntValue(), numVisibleYLabel->getText().getIntValue(), numHiddenLabel->getText().getIntValue());
|
||||||
|
}
|
||||||
|
m_vNumX = m_weights->getNumVisibleX();
|
||||||
|
m_vNumY = m_weights->getNumVisibleY();
|
||||||
|
m_hNum = m_weights->getNumHidden();
|
||||||
|
|
||||||
WeightsSlider->setRange(0, m_hNum-1, 1);
|
WeightsSlider->setRange(0, m_hNum-1, 1);
|
||||||
|
|
||||||
@@ -833,6 +828,8 @@ void MainComponent::create()
|
|||||||
numVisibleYLabel->setText(String(m_vNumY), dontSendNotification );
|
numVisibleYLabel->setText(String(m_vNumY), dontSendNotification );
|
||||||
numHiddenLabel->setText(String(m_hNum), dontSendNotification );
|
numHiddenLabel->setText(String(m_hNum), dontSendNotification );
|
||||||
|
|
||||||
|
m_pRbm = new Rbm(*m_weights, this);
|
||||||
|
|
||||||
m_pRbm->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState());
|
m_pRbm->setDoRaoBlackwell(rbmDoRaoBlackwellToggleButton->getToggleState());
|
||||||
m_pRbm->setUseProbsForHiddenReconstruction(rbmReduceVarianceToggleButton->getToggleState());
|
m_pRbm->setUseProbsForHiddenReconstruction(rbmReduceVarianceToggleButton->getToggleState());
|
||||||
m_pRbm->setUseVisibleGaussian(rbmUseVisibleGaussianToggleButton->getToggleState());
|
m_pRbm->setUseVisibleGaussian(rbmUseVisibleGaussianToggleButton->getToggleState());
|
||||||
@@ -845,8 +842,10 @@ void MainComponent::create()
|
|||||||
m_pRbm->setSparsity(sparsityLabel->getText().getFloatValue());
|
m_pRbm->setSparsity(sparsityLabel->getText().getFloatValue());
|
||||||
m_pRbm->setNumGibbs((uint32_t)numGibbsSlider->getValue());
|
m_pRbm->setNumGibbs((uint32_t)numGibbsSlider->getValue());
|
||||||
|
|
||||||
|
|
||||||
resized();
|
resized();
|
||||||
|
|
||||||
|
redrawReconstruction();
|
||||||
|
redrawWeights((int)WeightsSlider->getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainComponent::destroy()
|
void MainComponent::destroy()
|
||||||
@@ -882,25 +881,30 @@ void MainComponent::onDraw(DrawComponent &obj)
|
|||||||
}
|
}
|
||||||
if (&obj == DrawTraining)
|
if (&obj == DrawTraining)
|
||||||
{
|
{
|
||||||
// DrawReconstruction->setData(m_pRbm->toVisible(m_pRbm->toHidden(obj.getData())));
|
|
||||||
// DrawHidden->setData(m_pRbm->toHidden(obj.getData()));
|
|
||||||
redrawReconstruction();
|
redrawReconstruction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainComponent::redrawReconstruction()
|
void MainComponent::redrawReconstruction()
|
||||||
{
|
{
|
||||||
char table[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
RowVectorXd h = m_pRbm->toHidden(DrawTraining->getData());
|
||||||
VectorXd v;
|
DrawHidden->setData(h);
|
||||||
MatrixXd m;
|
|
||||||
|
|
||||||
DrawHidden->setData(m_pRbm->toHidden(DrawTraining->getData()));
|
RowVectorXd v = m_pRbm->toVisible(DrawHidden->getData());
|
||||||
v = m_pRbm->toVisible(DrawHidden->getData());
|
|
||||||
DrawReconstruction->setData(v);
|
DrawReconstruction->setData(v);
|
||||||
|
|
||||||
|
// RowVectorXd dataV(m_weights->getNumVisible());
|
||||||
|
// DrawHidden->setData(m_pRbm->toHidden(dataV));
|
||||||
|
|
||||||
|
// RowVectorXd dataH(m_weights->getNumHidden());
|
||||||
|
// VectorXd v = m_pRbm->toVisible(dataH);
|
||||||
|
// DrawReconstruction->setData(v);
|
||||||
|
|
||||||
|
|
||||||
if ((m_vNumX == 27) && (m_vNumY == 4))
|
if ((m_vNumX == 27) && (m_vNumY == 4))
|
||||||
{
|
{
|
||||||
m = v;
|
char table[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
MatrixXd m = m_pRbm->toVisible(DrawHidden->getData());
|
||||||
m.resize(m_vNumX, m_vNumY);
|
m.resize(m_vNumX, m_vNumY);
|
||||||
cout << "Reconstruction" << endl;
|
cout << "Reconstruction" << endl;
|
||||||
cout << m << endl;
|
cout << m << endl;
|
||||||
@@ -927,9 +931,9 @@ void MainComponent::redrawReconstruction()
|
|||||||
|
|
||||||
void MainComponent::redrawWeights(int index)
|
void MainComponent::redrawWeights(int index)
|
||||||
{
|
{
|
||||||
VectorXd w = m_weights.weights().col(index);
|
VectorXd w = m_weights->weights().col(index);
|
||||||
DrawWeights->setData(w);
|
DrawWeights->setData(w);
|
||||||
DrawVars->setData(m_weights.sigma());
|
DrawVars->setData(m_weights->sigma());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//[UserVariables] -- You can add your own custom variables in this section.
|
//[UserVariables] -- You can add your own custom variables in this section.
|
||||||
Weights m_weights;
|
ScopedPointer<Weights> m_weights;
|
||||||
ScopedPointer<Rbm> m_pRbm;
|
ScopedPointer<Rbm> m_pRbm;
|
||||||
ScopedPointer<DrawComponent> DrawTraining;
|
ScopedPointer<DrawComponent> DrawTraining;
|
||||||
ScopedPointer<DrawComponent> DrawReconstruction;
|
ScopedPointer<DrawComponent> DrawReconstruction;
|
||||||
@@ -87,7 +87,7 @@ private:
|
|||||||
MatrixXd m_trainingData;
|
MatrixXd m_trainingData;
|
||||||
void load();
|
void load();
|
||||||
void save();
|
void save();
|
||||||
void create();
|
void create(const char *pFilename=nullptr);
|
||||||
void destroy();
|
void destroy();
|
||||||
const juce::String& getBaseDir();
|
const juce::String& getBaseDir();
|
||||||
void onChanged(const LayerArray &obj);
|
void onChanged(const LayerArray &obj);
|
||||||
|
|||||||
+19
-14
@@ -39,6 +39,8 @@ class Rbm
|
|||||||
public:
|
public:
|
||||||
Rbm(Weights &weights, RbmListener *pListener = nullptr)
|
Rbm(Weights &weights, RbmListener *pListener = nullptr)
|
||||||
: m_w(weights)
|
: m_w(weights)
|
||||||
|
, m_v(weights.getNumVisible())
|
||||||
|
, m_h(weights.getNumHidden())
|
||||||
, m_pListener(pListener)
|
, m_pListener(pListener)
|
||||||
, m_progress(0)
|
, m_progress(0)
|
||||||
, m_sigma(1.0)
|
, m_sigma(1.0)
|
||||||
@@ -60,6 +62,7 @@ public:
|
|||||||
{
|
{
|
||||||
Noise_Init(&m_noise, 0x32727155);
|
Noise_Init(&m_noise, 0x32727155);
|
||||||
|
|
||||||
|
#if 1
|
||||||
VectorXd a(4);
|
VectorXd a(4);
|
||||||
a << 1, 2, 3, 4;
|
a << 1, 2, 3, 4;
|
||||||
VectorXd b(4);
|
VectorXd b(4);
|
||||||
@@ -67,12 +70,16 @@ public:
|
|||||||
b.array() = -a.array().exp();
|
b.array() = -a.array().exp();
|
||||||
|
|
||||||
cout << b << endl;
|
cout << b << endl;
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
~Rbm()
|
~Rbm()
|
||||||
{
|
{
|
||||||
cancel();
|
cancel();
|
||||||
Noise_Free(&m_noise);
|
Noise_Free(&m_noise);
|
||||||
|
m_v.resize(0);
|
||||||
|
m_h.resize(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sample(MatrixXd &src)
|
void sample(MatrixXd &src)
|
||||||
@@ -396,23 +403,19 @@ public:
|
|||||||
return -energy/(m_sigma*m_sigma);
|
return -energy/(m_sigma*m_sigma);
|
||||||
}
|
}
|
||||||
|
|
||||||
RowVectorXd toHidden(const RowVectorXd& v)
|
RowVectorXd const & toHidden(const RowVectorXd& v)
|
||||||
{
|
{
|
||||||
RowVectorXd h(m_w.getNumHidden());
|
m_h = v * m_w.weights();
|
||||||
RowVectorXd vn(m_w.getNumVisible());
|
m_h += m_w.hiddenBias();
|
||||||
|
probsLogistic(m_h);
|
||||||
|
|
||||||
h = v * m_w.weights();
|
return m_h;
|
||||||
h += m_w.hiddenBias();
|
|
||||||
probsLogistic(h);
|
|
||||||
|
|
||||||
return h;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RowVectorXd toVisible(const RowVectorXd& h)
|
RowVectorXd const & toVisible(const RowVectorXd& h)
|
||||||
{
|
{
|
||||||
RowVectorXd v(m_w.getNumVisible());
|
m_v = h * m_w.weights().transpose();
|
||||||
v = h * m_w.weights().transpose();
|
m_v += m_w.visibleBias();
|
||||||
v += m_w.visibleBias();
|
|
||||||
|
|
||||||
if (m_useVisibleGaussian)
|
if (m_useVisibleGaussian)
|
||||||
{
|
{
|
||||||
@@ -420,9 +423,9 @@ public:
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
probsLogistic(v, m_w.sigma());
|
probsLogistic(m_v, m_w.sigma());
|
||||||
}
|
}
|
||||||
return v;
|
return m_v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSigma(double value)
|
void setSigma(double value)
|
||||||
@@ -512,6 +515,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Weights &m_w;
|
Weights &m_w;
|
||||||
|
RowVectorXd m_h;
|
||||||
|
RowVectorXd m_v;
|
||||||
RbmListener *m_pListener;
|
RbmListener *m_pListener;
|
||||||
noise_gen_t m_noise;
|
noise_gen_t m_noise;
|
||||||
double m_progress;
|
double m_progress;
|
||||||
|
|||||||
+2
-1
@@ -70,7 +70,6 @@ public:
|
|||||||
|
|
||||||
void setUnits(uint32_t numVisibleX, uint32_t numVisibleY, uint32_t numHidden)
|
void setUnits(uint32_t numVisibleX, uint32_t numVisibleY, uint32_t numHidden)
|
||||||
{
|
{
|
||||||
shuffle(0);
|
|
||||||
if ((m_numVisibleX == numVisibleX) && (m_numVisibleY == numVisibleY) && (m_numHidden == numHidden))
|
if ((m_numVisibleX == numVisibleX) && (m_numVisibleY == numVisibleY) && (m_numHidden == numHidden))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -85,6 +84,7 @@ public:
|
|||||||
m_mean.resize(m_numVisible);
|
m_mean.resize(m_numVisible);
|
||||||
m_bv.resize(m_numVisible);
|
m_bv.resize(m_numVisible);
|
||||||
m_bh.resize(m_numHidden);
|
m_bh.resize(m_numHidden);
|
||||||
|
shuffle(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shuffle(double stdDev)
|
void shuffle(double stdDev)
|
||||||
@@ -299,6 +299,7 @@ private:
|
|||||||
|
|
||||||
void free()
|
void free()
|
||||||
{
|
{
|
||||||
|
setUnits(0, 0, 0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user