- refactored
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@815 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -5,13 +5,13 @@ BUILD_DIR := ./build/${CONFIG}
|
|||||||
JUCE_ROOT := juce/JUCE-3.1.1
|
JUCE_ROOT := juce/JUCE-3.1.1
|
||||||
JUCE_LIB := juce/build/${CONFIG}/libjuce.a
|
JUCE_LIB := juce/build/${CONFIG}/libjuce.a
|
||||||
|
|
||||||
GUI_CXX_SRCS := gui.cpp Rbm.cpp Layer.cpp Stack.cpp MainComponent.cpp RbmComponent.cpp DrawComponent.cpp
|
GUI_CXX_SRCS := gui.cpp Rbm.cpp Layer.cpp AStack.cpp DeepStack.cpp MainComponent.cpp RbmComponent.cpp DrawComponent.cpp
|
||||||
GUI_GCC_SRCS :=
|
GUI_GCC_SRCS :=
|
||||||
|
|
||||||
TEST_CXX_SRCS := main.cpp Rbm.cpp Layer.cpp Stack.cpp
|
TEST_CXX_SRCS := main.cpp Rbm.cpp Layer.cpp AStack.cpp DeepStack.cpp
|
||||||
TEST_GCC_SRCS :=
|
TEST_GCC_SRCS :=
|
||||||
|
|
||||||
POET_CXX_SRCS := poet.cpp Rbm.cpp Layer.cpp Stack.cpp
|
POET_CXX_SRCS := poet.cpp Rbm.cpp Layer.cpp AStack.cpp DeepStack.cpp
|
||||||
POET_GCC_SRCS :=
|
POET_GCC_SRCS :=
|
||||||
|
|
||||||
CXX_OBJS := $(addprefix ${BUILD_DIR}/, $(subst .cpp,.o,$(${PRJ}_CXX_SRCS)))
|
CXX_OBJS := $(addprefix ${BUILD_DIR}/, $(subst .cpp,.o,$(${PRJ}_CXX_SRCS)))
|
||||||
|
|||||||
@@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* File: DeepStack.cpp
|
||||||
|
* Author: jens
|
||||||
|
*
|
||||||
|
* Created on 25. Oktober 2019, 18:26
|
||||||
|
*/
|
||||||
|
#include <cassert>
|
||||||
|
#include "DeepStack.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
DeepStack::DeepStack(const std::string &dir, const std::string &name, StackType type)
|
||||||
|
: AStack(dir, type, name)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
DeepStack::DeepStack(const DeepStack& orig)
|
||||||
|
: AStack(orig)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
DeepStack::~DeepStack()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeepStack::weightsInit(double stddev)
|
||||||
|
{
|
||||||
|
Layer *pLayer = m_pLayers;
|
||||||
|
while(pLayer)
|
||||||
|
{
|
||||||
|
pLayer->weightsInit(stddev);
|
||||||
|
pLayer = pLayer->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeepStack::loadWeights()
|
||||||
|
{
|
||||||
|
Layer *pLayer = m_pLayers;
|
||||||
|
while(pLayer)
|
||||||
|
{
|
||||||
|
if (!pLayer->weightsLoad(m_dir, m_name))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pLayer = pLayer->next;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeepStack::saveWeights()
|
||||||
|
{
|
||||||
|
Layer *pLayer = m_pLayers;
|
||||||
|
while(pLayer)
|
||||||
|
{
|
||||||
|
if (!pLayer->weightsSave(m_dir, m_name))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pLayer = pLayer->next;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeepStack::train(Rbm::IListener* pListener)
|
||||||
|
{
|
||||||
|
Layer *pLayer = m_pLayers;
|
||||||
|
while(pLayer)
|
||||||
|
{
|
||||||
|
std::cout << m_name << ": " << " Training of layer " << std::to_string(pLayer->id()) << std::endl;
|
||||||
|
pLayer->calcContextBatch(m_trainingBatch);
|
||||||
|
pLayer->train(m_trainingBatch, pListener);
|
||||||
|
pLayer = pLayer->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
arma::mat& DeepStack::trainingBatch()
|
||||||
|
{
|
||||||
|
return m_trainingBatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
arma::mat DeepStack::trainingBatch(Layer* pThatLayer)
|
||||||
|
{
|
||||||
|
arma::mat thisBatch = m_trainingBatch;
|
||||||
|
Layer *pLayer = m_pLayers;
|
||||||
|
while (pLayer)
|
||||||
|
{
|
||||||
|
if (pLayer->id() == pThatLayer->id())
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
||||||
|
pLayer = pLayer->next;
|
||||||
|
}
|
||||||
|
return thisBatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t DeepStack::loadTrainingBatch(bool doNormalize)
|
||||||
|
{
|
||||||
|
std::string filename = m_dir + "/" + m_name + ".training.dat";
|
||||||
|
std::string path = m_dir + "/" + m_name + ".training.dat";
|
||||||
|
bool success = m_trainingBatch.load(filename, arma::arma_ascii);
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
if (doNormalize)
|
||||||
|
{
|
||||||
|
m_trainingBatch = Rbm::normalize(m_trainingBatch);
|
||||||
|
}
|
||||||
|
std::cout << "Loaded " << m_trainingBatch.n_rows << " training samples\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migrate context part to training data
|
||||||
|
size_t numTraining = m_trainingBatch.n_rows;
|
||||||
|
size_t numVisible = getLayer(0)->numVisible();
|
||||||
|
|
||||||
|
if (m_trainingBatch.n_cols < numVisible)
|
||||||
|
{
|
||||||
|
size_t diff = numVisible - m_trainingBatch.n_cols;
|
||||||
|
arma::mat training_with_ctx = arma::join_rows(m_trainingBatch, arma::zeros(numTraining, diff));
|
||||||
|
m_trainingBatch = training_with_ctx;
|
||||||
|
}
|
||||||
|
else if (m_trainingBatch.n_cols > numVisible)
|
||||||
|
{
|
||||||
|
m_trainingBatch = m_trainingBatch.submat(0, 0, numTraining-1, numVisible-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_trainingBatch.n_rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t DeepStack::saveTrainingBatch()
|
||||||
|
{
|
||||||
|
std::string filename = m_dir + "/" + m_name + ".training.dat";
|
||||||
|
bool success = m_trainingBatch.save(filename, arma::arma_ascii);
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
std::cout << "Saved " << m_trainingBatch.n_rows << " training samples\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_trainingBatch.n_rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t DeepStack::numTraining()
|
||||||
|
{
|
||||||
|
return m_trainingBatch.n_rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeepStack::addTraining(const arma::mat &toAdd)
|
||||||
|
{
|
||||||
|
m_trainingBatch.insert_rows(m_trainingBatch.n_rows, toAdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeepStack::delTraining(int index)
|
||||||
|
{
|
||||||
|
m_trainingBatch.shed_row(index);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* File: DeepStack.hpp
|
||||||
|
* Author: jens
|
||||||
|
*
|
||||||
|
* Created on 25. Oktober 2019, 18:26
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef DEEPSTACK_HPP
|
||||||
|
#define DEEPSTACK_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <armadillo>
|
||||||
|
#include <jsoncpp/json/json.h>
|
||||||
|
#include "Layer.hpp"
|
||||||
|
#include "AStack.hpp"
|
||||||
|
|
||||||
|
class DeepStack : public AStack
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DeepStack(const std::string &dir, const std::string &name, StackType type=StackType::None);
|
||||||
|
DeepStack(const DeepStack& orig);
|
||||||
|
virtual ~DeepStack();
|
||||||
|
|
||||||
|
void train(Rbm::IListener* pListener);
|
||||||
|
void weightsInit(double stddev);
|
||||||
|
bool loadWeights();
|
||||||
|
bool saveWeights();
|
||||||
|
|
||||||
|
size_t numTraining();
|
||||||
|
void addTraining(const arma::mat &toAdd);
|
||||||
|
void delTraining(int index);
|
||||||
|
size_t loadTrainingBatch(bool doNormalize=false);
|
||||||
|
size_t saveTrainingBatch();
|
||||||
|
arma::mat& trainingBatch();
|
||||||
|
arma::mat trainingBatch(Layer *pLayer);
|
||||||
|
|
||||||
|
private:
|
||||||
|
arma::mat m_trainingBatch;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* DEEPSTACK_HPP */
|
||||||
|
|
||||||
@@ -557,7 +557,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
|
|||||||
|
|
||||||
if (!m_stack)
|
if (!m_stack)
|
||||||
{
|
{
|
||||||
m_stack = new Stack(m_file.getParentDirectory().getFullPathName().toStdString(), std::string(projectNameLabel->getText().getCharPointer()));
|
m_stack = new DeepStack(m_file.getParentDirectory().getFullPathName().toStdString(), std::string(projectNameLabel->getText().getCharPointer()));
|
||||||
}
|
}
|
||||||
Layer *pPrev = m_stack->getLayer(next_index-1);
|
Layer *pPrev = m_stack->getLayer(next_index-1);
|
||||||
if (pPrev)
|
if (pPrev)
|
||||||
@@ -583,7 +583,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked)
|
|||||||
{
|
{
|
||||||
m_file = fc->getResult();
|
m_file = fc->getResult();
|
||||||
projectNameLabel->setText(m_file.getFileNameWithoutExtension(), NotificationType::dontSendNotification);
|
projectNameLabel->setText(m_file.getFileNameWithoutExtension(), NotificationType::dontSendNotification);
|
||||||
m_stack = new Stack(m_file.getParentDirectory().getFullPathName().toStdString(), std::string(projectNameLabel->getText().getCharPointer()));
|
m_stack = new DeepStack(m_file.getParentDirectory().getFullPathName().toStdString(), std::string(projectNameLabel->getText().getCharPointer()));
|
||||||
m_stack->load(this);
|
m_stack->load(this);
|
||||||
m_stack->loadWeights();
|
m_stack->loadWeights();
|
||||||
m_rbmSelect->clear(dontSendNotification);
|
m_rbmSelect->clear(dontSendNotification);
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
//[Headers] -- You can add your own extra header files here --
|
//[Headers] -- You can add your own extra header files here --
|
||||||
#include <armadillo>
|
#include <armadillo>
|
||||||
#include "JuceHeader.h"
|
#include "JuceHeader.h"
|
||||||
#include "Stack.hpp"
|
#include "DeepStack.hpp"
|
||||||
#include "RbmComponent.hpp"
|
#include "RbmComponent.hpp"
|
||||||
|
|
||||||
//[/Headers]
|
//[/Headers]
|
||||||
@@ -83,7 +83,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.
|
||||||
static const size_t DBN_SIZE = 4;
|
static const size_t DBN_SIZE = 4;
|
||||||
ScopedPointer<Stack> m_stack;
|
ScopedPointer<DeepStack> m_stack;
|
||||||
RbmComponent *m_pLayer;
|
RbmComponent *m_pLayer;
|
||||||
int m_weightIndex;
|
int m_weightIndex;
|
||||||
int m_trainingIndex;
|
int m_trainingIndex;
|
||||||
|
|||||||
@@ -1,292 +0,0 @@
|
|||||||
/*
|
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
|
||||||
* To change this template file, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* File: Stack.cpp
|
|
||||||
* Author: jens
|
|
||||||
*
|
|
||||||
* Created on 25. Oktober 2019, 18:26
|
|
||||||
*/
|
|
||||||
#include <cassert>
|
|
||||||
#include "Stack.hpp"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
Stack::Stack(const std::string &dir, const std::string &name)
|
|
||||||
: m_dir(dir)
|
|
||||||
, m_name(name)
|
|
||||||
, m_pLayers(nullptr)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Stack::Stack(const Stack& orig)
|
|
||||||
: m_dir(orig.m_dir)
|
|
||||||
, m_name(orig.m_name)
|
|
||||||
, m_pLayers(orig.m_pLayers)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Stack::~Stack()
|
|
||||||
{
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer)
|
|
||||||
{
|
|
||||||
Layer *pNextLayer = pLayer->next;
|
|
||||||
delete pLayer;
|
|
||||||
pLayer = pNextLayer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Stack::setName(const std::string& name)
|
|
||||||
{
|
|
||||||
m_name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Stack::numLayers()
|
|
||||||
{
|
|
||||||
size_t count = 0;
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer)
|
|
||||||
{
|
|
||||||
count++;
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Stack::addLayer(Layer *pOtherLayer)
|
|
||||||
{
|
|
||||||
if (!m_pLayers)
|
|
||||||
{
|
|
||||||
m_pLayers = pOtherLayer;
|
|
||||||
pOtherLayer->prev = nullptr;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer->next)
|
|
||||||
{
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
pLayer->next = pOtherLayer;
|
|
||||||
pOtherLayer->prev = pLayer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Stack::delLayer(Layer* pLayer)
|
|
||||||
{
|
|
||||||
assert(!"Stack::delLayer: Not implemented!");
|
|
||||||
}
|
|
||||||
|
|
||||||
Layer* Stack::getLayer(size_t layerId) const
|
|
||||||
{
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer)
|
|
||||||
{
|
|
||||||
if (pLayer->id() == layerId)
|
|
||||||
{
|
|
||||||
return pLayer;
|
|
||||||
}
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Stack::load(LayerConstructor *pLayerConstructor)
|
|
||||||
{
|
|
||||||
std::cout << "Importing Project " << m_name << std::endl;
|
|
||||||
ifstream ifs(m_dir + "/" + m_name + string(".prj"));
|
|
||||||
|
|
||||||
Json::Value project;
|
|
||||||
ifs >> project;
|
|
||||||
|
|
||||||
const string &name = project["stack"]["name"].asString();
|
|
||||||
Json::Value &layers = project["stack"]["layers"];
|
|
||||||
|
|
||||||
for (int i=0; i < layers.size(); i++)
|
|
||||||
{
|
|
||||||
Json::Value &layer = layers[i];
|
|
||||||
|
|
||||||
string layername = layer["name"].asString();
|
|
||||||
int numVisibleX = layer["numVisibleX"].asInt();
|
|
||||||
int numVisibleY = layer["numVisibleY"].asInt();
|
|
||||||
int numHidden = layer["numHidden"].asInt();
|
|
||||||
int numContext = layer["numContext"].asInt();
|
|
||||||
|
|
||||||
Layer *pLayer = nullptr;
|
|
||||||
if (!pLayerConstructor)
|
|
||||||
{
|
|
||||||
pLayer = new Layer(layername, i, numVisibleX, numVisibleY, numHidden, numContext);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
pLayer = pLayerConstructor->onConstruct(layername, i, numVisibleX, numVisibleY, numHidden, numContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(pLayer != nullptr);
|
|
||||||
|
|
||||||
pLayer->fromJson(layer["rbm"]);
|
|
||||||
addLayer(pLayer);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Stack::save()
|
|
||||||
{
|
|
||||||
std::cout << "Exporting Project " << m_name << std::endl;
|
|
||||||
ofstream ofs(m_dir + "/" + m_name + string(".prj"));
|
|
||||||
|
|
||||||
Json::Value project;
|
|
||||||
project["stack"]["name"] = m_name;
|
|
||||||
|
|
||||||
Json::Value layers(Json::arrayValue);
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer)
|
|
||||||
{
|
|
||||||
layers.append(pLayer->toJson());
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
project["stack"]["layers"] = layers;
|
|
||||||
|
|
||||||
ofs << project;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Stack::weightsInit(double stddev)
|
|
||||||
{
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer)
|
|
||||||
{
|
|
||||||
pLayer->weightsInit(stddev);
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Stack::loadWeights()
|
|
||||||
{
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer)
|
|
||||||
{
|
|
||||||
if (!pLayer->weightsLoad(m_dir, m_name))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Stack::saveWeights()
|
|
||||||
{
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer)
|
|
||||||
{
|
|
||||||
if (!pLayer->weightsSave(m_dir, m_name))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Stack::train(Rbm::IListener* pListener)
|
|
||||||
{
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while(pLayer)
|
|
||||||
{
|
|
||||||
std::cout << m_name << ": " << " Training of layer " << std::to_string(pLayer->id()) << std::endl;
|
|
||||||
pLayer->calcContextBatch(m_trainingBatch);
|
|
||||||
pLayer->train(m_trainingBatch, pListener);
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
arma::mat& Stack::trainingBatch()
|
|
||||||
{
|
|
||||||
return m_trainingBatch;
|
|
||||||
}
|
|
||||||
|
|
||||||
arma::mat Stack::trainingBatch(Layer* pThatLayer)
|
|
||||||
{
|
|
||||||
arma::mat thisBatch = m_trainingBatch;
|
|
||||||
Layer *pLayer = m_pLayers;
|
|
||||||
while (pLayer)
|
|
||||||
{
|
|
||||||
if (pLayer->id() == pThatLayer->id())
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
|
||||||
pLayer = pLayer->next;
|
|
||||||
}
|
|
||||||
return thisBatch;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Stack::loadTrainingBatch(bool doNormalize)
|
|
||||||
{
|
|
||||||
std::string filename = m_dir + "/" + m_name + ".training.dat";
|
|
||||||
std::string path = m_dir + "/" + m_name + ".training.dat";
|
|
||||||
bool success = m_trainingBatch.load(filename, arma::arma_ascii);
|
|
||||||
|
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
if (doNormalize)
|
|
||||||
{
|
|
||||||
m_trainingBatch = Rbm::normalize(m_trainingBatch);
|
|
||||||
}
|
|
||||||
std::cout << "Loaded " << m_trainingBatch.n_rows << " training samples\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Migrate context part to training data
|
|
||||||
size_t numTraining = m_trainingBatch.n_rows;
|
|
||||||
size_t numVisible = getLayer(0)->numVisible();
|
|
||||||
|
|
||||||
if (m_trainingBatch.n_cols < numVisible)
|
|
||||||
{
|
|
||||||
size_t diff = numVisible - m_trainingBatch.n_cols;
|
|
||||||
arma::mat training_with_ctx = arma::join_rows(m_trainingBatch, arma::zeros(numTraining, diff));
|
|
||||||
m_trainingBatch = training_with_ctx;
|
|
||||||
}
|
|
||||||
else if (m_trainingBatch.n_cols > numVisible)
|
|
||||||
{
|
|
||||||
m_trainingBatch = m_trainingBatch.submat(0, 0, numTraining-1, numVisible-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_trainingBatch.n_rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Stack::saveTrainingBatch()
|
|
||||||
{
|
|
||||||
std::string filename = m_dir + "/" + m_name + ".training.dat";
|
|
||||||
bool success = m_trainingBatch.save(filename, arma::arma_ascii);
|
|
||||||
|
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
std::cout << "Saved " << m_trainingBatch.n_rows << " training samples\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_trainingBatch.n_rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Stack::numTraining()
|
|
||||||
{
|
|
||||||
return m_trainingBatch.n_rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Stack::addTraining(const arma::mat &toAdd)
|
|
||||||
{
|
|
||||||
m_trainingBatch.insert_rows(m_trainingBatch.n_rows, toAdd);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Stack::delTraining(int index)
|
|
||||||
{
|
|
||||||
m_trainingBatch.shed_row(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
|
||||||
* To change this template file, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* File: Stack.hpp
|
|
||||||
* Author: jens
|
|
||||||
*
|
|
||||||
* Created on 25. Oktober 2019, 18:26
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef STACK_HPP
|
|
||||||
#define STACK_HPP
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <armadillo>
|
|
||||||
#include <jsoncpp/json/json.h>
|
|
||||||
#include "Layer.hpp"
|
|
||||||
|
|
||||||
class LayerConstructor
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
LayerConstructor() {}
|
|
||||||
virtual ~LayerConstructor() {}
|
|
||||||
|
|
||||||
virtual Layer* onConstruct(const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, size_t numContext)
|
|
||||||
{
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Stack
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Stack(const std::string &dir, const std::string &name);
|
|
||||||
Stack(const Stack& orig);
|
|
||||||
virtual ~Stack();
|
|
||||||
|
|
||||||
void setName(const std::string &name);
|
|
||||||
size_t numLayers();
|
|
||||||
void addLayer(Layer *pLayer);
|
|
||||||
void delLayer(Layer *pLayer);
|
|
||||||
Layer* getLayer(size_t layerId) const;
|
|
||||||
|
|
||||||
void train(Rbm::IListener* pListener);
|
|
||||||
bool load(LayerConstructor *pLayerConstructor=nullptr);
|
|
||||||
bool save();
|
|
||||||
void weightsInit(double stddev);
|
|
||||||
bool loadWeights();
|
|
||||||
bool saveWeights();
|
|
||||||
|
|
||||||
size_t numTraining();
|
|
||||||
void addTraining(const arma::mat &toAdd);
|
|
||||||
void delTraining(int index);
|
|
||||||
size_t loadTrainingBatch(bool doNormalize=false);
|
|
||||||
size_t saveTrainingBatch();
|
|
||||||
arma::mat& trainingBatch();
|
|
||||||
arma::mat trainingBatch(Layer *pLayer);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string m_dir;
|
|
||||||
std::string m_name;
|
|
||||||
Layer *m_pLayers;
|
|
||||||
arma::mat m_trainingBatch;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* STACK_HPP */
|
|
||||||
|
|
||||||
+2
-2
@@ -8,7 +8,7 @@
|
|||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include "Rbm.hpp"
|
#include "Rbm.hpp"
|
||||||
#include "Layer.hpp"
|
#include "Layer.hpp"
|
||||||
#include "Stack.hpp"
|
#include "DeepStack.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
class RbmListener : public Rbm::IListener
|
class RbmListener : public Rbm::IListener
|
||||||
@@ -35,7 +35,7 @@ int main()
|
|||||||
|
|
||||||
const string project("context99");
|
const string project("context99");
|
||||||
|
|
||||||
Stack stack(".", project);
|
DeepStack stack(".", project);
|
||||||
|
|
||||||
#if CREATE_TEST
|
#if CREATE_TEST
|
||||||
stack.addTraining(stack.trainingBatch().row(1));
|
stack.addTraining(stack.trainingBatch().row(1));
|
||||||
|
|||||||
+2
-2
@@ -9,7 +9,7 @@
|
|||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include "Rbm.hpp"
|
#include "Rbm.hpp"
|
||||||
#include "Layer.hpp"
|
#include "Layer.hpp"
|
||||||
#include "Stack.hpp"
|
#include "DeepStack.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace arma;
|
using namespace arma;
|
||||||
@@ -199,7 +199,7 @@ int main()
|
|||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Stack stack(".", "poet5");
|
DeepStack stack(".", "poet5");
|
||||||
|
|
||||||
// Load project
|
// Load project
|
||||||
stack.load();
|
stack.load();
|
||||||
|
|||||||
Reference in New Issue
Block a user