- refactored
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@818 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+8
-32
@@ -197,43 +197,19 @@ bool AStack::saveWeights()
|
||||
return true;
|
||||
}
|
||||
|
||||
arma::mat AStack::upPass(size_t layerId, const arma::mat& v)
|
||||
arma::mat AStack::trainingBatchFrom(size_t layerId, const arma::mat& batch)
|
||||
{
|
||||
arma::mat h = arma::zeros(0,0);
|
||||
arma::mat tv = v;
|
||||
Layer *pLayer = getLayer(layerId);
|
||||
while(pLayer)
|
||||
arma::mat thisBatch = batch;
|
||||
Layer *pLayer = getLayer(0);
|
||||
while (pLayer)
|
||||
{
|
||||
if (pLayer->isEnable())
|
||||
if (pLayer->id() == layerId)
|
||||
{
|
||||
h = pLayer->to_h_gibbs(tv);
|
||||
tv = h;
|
||||
break;
|
||||
}
|
||||
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return h;
|
||||
return thisBatch;
|
||||
}
|
||||
|
||||
arma::mat AStack::downPass(size_t layerId, const arma::mat& h)
|
||||
{
|
||||
arma::mat v = arma::zeros(0,0);
|
||||
arma::mat th = h;
|
||||
Layer *pLayer = getLayer(layerId);
|
||||
while(pLayer)
|
||||
{
|
||||
if (pLayer->isEnable())
|
||||
{
|
||||
v = pLayer->to_v_gibbs(th);
|
||||
th = v;
|
||||
}
|
||||
pLayer = pLayer->prev;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
arma::mat AStack::upDownPass(size_t layerId, const arma::mat& v)
|
||||
{
|
||||
arma::mat h = upPass(layerId, v);
|
||||
arma::mat r = downPass(numLayers()-1, h);
|
||||
return r;
|
||||
}
|
||||
|
||||
+7
-4
@@ -19,6 +19,7 @@
|
||||
#include <armadillo>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include "Layer.hpp"
|
||||
#include <forward_list>
|
||||
|
||||
class LayerConstructor
|
||||
{
|
||||
@@ -63,16 +64,18 @@ public:
|
||||
bool loadWeights();
|
||||
bool saveWeights();
|
||||
|
||||
arma::mat upPass(size_t layerId, arma::mat const &v);
|
||||
arma::mat downPass(size_t layerId, arma::mat const &h);
|
||||
arma::mat upDownPass(size_t layerId, arma::mat const &v);
|
||||
|
||||
virtual void train(size_t layerId, const arma::mat& batch, Rbm::IListener* pListener) = 0;
|
||||
virtual void train(const arma::mat& batch, Rbm::IListener* pListener) = 0;
|
||||
arma::mat trainingBatchFrom(size_t layerId, const arma::mat& batch);
|
||||
|
||||
protected:
|
||||
StackType m_type;
|
||||
std::string m_name;
|
||||
Layer *m_pLayers;
|
||||
std::string m_dir;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif /* ASTACK_HPP */
|
||||
|
||||
@@ -123,3 +123,72 @@ void DeepStack::delTraining(int index)
|
||||
m_trainingBatch.shed_row(index);
|
||||
}
|
||||
|
||||
void DeepStack::train(const arma::mat& batch, Rbm::IListener* pListener)
|
||||
{
|
||||
arma::mat thisBatch = batch;
|
||||
Layer *pLayer = getLayer(0);
|
||||
while (pLayer)
|
||||
{
|
||||
pLayer->train(thisBatch, pListener);
|
||||
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
}
|
||||
|
||||
void DeepStack::train(size_t layerId, const arma::mat& batch, Rbm::IListener* pListener)
|
||||
{
|
||||
arma::mat thisBatch = batch;
|
||||
Layer *pLayer = getLayer(0);
|
||||
while (pLayer)
|
||||
{
|
||||
if (pLayer->id() == layerId)
|
||||
{
|
||||
break;
|
||||
}
|
||||
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
pLayer->train(thisBatch, pListener);
|
||||
}
|
||||
|
||||
arma::mat DeepStack::upPass(size_t layerId, const arma::mat& v)
|
||||
{
|
||||
arma::mat h = arma::zeros(0,0);
|
||||
arma::mat tv = v;
|
||||
Layer *pLayer = getLayer(layerId);
|
||||
while(pLayer)
|
||||
{
|
||||
if (pLayer->isEnable())
|
||||
{
|
||||
h = pLayer->to_h_gibbs(tv);
|
||||
tv = h;
|
||||
}
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
arma::mat DeepStack::downPass(size_t layerId, const arma::mat& h)
|
||||
{
|
||||
arma::mat v = arma::zeros(0,0);
|
||||
arma::mat th = h;
|
||||
Layer *pLayer = getLayer(layerId);
|
||||
while(pLayer)
|
||||
{
|
||||
if (pLayer->isEnable())
|
||||
{
|
||||
v = pLayer->to_v_gibbs(th);
|
||||
th = v;
|
||||
}
|
||||
pLayer = pLayer->prev;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
arma::mat DeepStack::upDownPass(size_t layerId, const arma::mat& v)
|
||||
{
|
||||
arma::mat h = upPass(layerId, v);
|
||||
arma::mat r = downPass(numLayers()-1, h);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,13 @@ public:
|
||||
arma::mat& trainingBatch();
|
||||
arma::mat trainingBatch(Layer *pLayer);
|
||||
|
||||
arma::mat upPass(size_t layerId, arma::mat const &v);
|
||||
arma::mat downPass(size_t layerId, arma::mat const &h);
|
||||
arma::mat upDownPass(size_t layerId, arma::mat const &v);
|
||||
|
||||
void train(const arma::mat& batch, Rbm::IListener* pListener) override;
|
||||
void train(size_t layerId, const arma::mat& batch, Rbm::IListener* pListener) override;
|
||||
|
||||
private:
|
||||
arma::mat m_trainingBatch;
|
||||
};
|
||||
|
||||
+2
-17
@@ -143,23 +143,8 @@ void Layer::calcContextBatch(arma::mat& batch)
|
||||
|
||||
void Layer::train(const arma::mat& batch, IListener* pListener)
|
||||
{
|
||||
Rbm::train(trainingData(batch), pListener);
|
||||
}
|
||||
|
||||
arma::mat Layer::trainingData(const arma::mat& batch)
|
||||
{
|
||||
arma::mat thisBatch = batch;
|
||||
Layer *pLayer = root();
|
||||
while (pLayer)
|
||||
{
|
||||
if (pLayer == this)
|
||||
{
|
||||
break;
|
||||
}
|
||||
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return thisBatch;
|
||||
std::cout << m_name << ": " << " Training of layer " << std::to_string(id()) << std::endl;
|
||||
Rbm::train(batch, pListener);
|
||||
}
|
||||
|
||||
bool Layer::weightsLoad(std::string const &dir, std::string const &prj)
|
||||
|
||||
@@ -684,7 +684,7 @@ void MainComponent::sliderValueChanged (Slider* sliderThatWasMoved)
|
||||
{
|
||||
//[UserSliderCode_patterSlider] -- add your slider handling code here..
|
||||
m_trainingIndex = (int)sliderThatWasMoved->getValue();
|
||||
m_pLayer->setTrainingData(trainingAt(m_trainingIndex));
|
||||
m_stack->upDownPass(m_pLayer->id(), m_stack->trainingBatchFrom(m_pLayer->id(), trainingAt(m_trainingIndex)));
|
||||
//[/UserSliderCode_patterSlider]
|
||||
}
|
||||
else if (sliderThatWasMoved == WeightsSlider)
|
||||
@@ -818,7 +818,7 @@ void MainComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
|
||||
m_pLayer->redrawWeights(m_weightIndex);
|
||||
if (m_stack->trainingBatch().n_rows > 0)
|
||||
{
|
||||
m_pLayer->setTrainingData(trainingAt(m_trainingIndex));
|
||||
m_stack->upDownPass(index, m_stack->trainingBatchFrom(index, trainingAt(m_trainingIndex)));
|
||||
}
|
||||
//[/UserComboBoxCode_m_rbmSelect]
|
||||
}
|
||||
@@ -902,7 +902,8 @@ void MainComponent::run()
|
||||
{
|
||||
trainButton->setButtonText (TRANS("Stop"));
|
||||
m_pLayer->calcContextBatch(m_stack->trainingBatch());
|
||||
m_pLayer->train(m_stack->trainingBatch(), this);
|
||||
// m_pLayer->train(m_stack->trainingBatch(), this);
|
||||
m_stack->train(m_pLayer->id(), m_stack->trainingBatch(), this);
|
||||
trainButton->setButtonText (TRANS("Train"));
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
//==============================================================================
|
||||
RbmComponent::RbmComponent (AStack &stack, const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, size_t numContext)
|
||||
: Layer(name, id, numVisibleX, numVisibleY, numHidden, numContext)
|
||||
, m_stack(stack)
|
||||
, m_stack(dynamic_cast<DeepStack&>(stack))
|
||||
, m_currWeightIndexToDraw(0)
|
||||
, DrawVisibleTrain(nullptr)
|
||||
, DrawVisibleReconst(nullptr)
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include "JuceHeader.h"
|
||||
#include "DrawComponent.hpp"
|
||||
#include "Layer.hpp"
|
||||
#include "AStack.hpp"
|
||||
#include "DeepStack.hpp"
|
||||
|
||||
//[/Headers]
|
||||
|
||||
@@ -87,7 +87,7 @@ private:
|
||||
void onUpPass(const arma::mat& v) override;
|
||||
void onDownPass(const arma::mat& h) override;
|
||||
|
||||
AStack &m_stack;
|
||||
DeepStack &m_stack;
|
||||
size_t m_currWeightIndexToDraw;
|
||||
void onDraw(DrawComponent &obj) override;
|
||||
void buttonClicked(Button* buttonThatWasClicked) override;
|
||||
|
||||
Reference in New Issue
Block a user