From 084c4902d8920c19e58e4cb46579d1ef4867df1f Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sun, 16 Jan 2022 13:35:57 +0000 Subject: [PATCH] - refactored git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@815 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- Makefile | 6 +- source/DeepStack.cpp | 163 ++++++++++++++++++++++ source/DeepStack.hpp | 50 +++++++ source/MainComponent.cpp | 4 +- source/MainComponent.hpp | 4 +- source/Stack.cpp | 292 --------------------------------------- source/Stack.hpp | 72 ---------- source/main.cpp | 4 +- source/poet.cpp | 4 +- 9 files changed, 224 insertions(+), 375 deletions(-) create mode 100644 source/DeepStack.cpp create mode 100644 source/DeepStack.hpp delete mode 100644 source/Stack.cpp delete mode 100644 source/Stack.hpp diff --git a/Makefile b/Makefile index d2c8333..38278d5 100644 --- a/Makefile +++ b/Makefile @@ -5,13 +5,13 @@ BUILD_DIR := ./build/${CONFIG} JUCE_ROOT := juce/JUCE-3.1.1 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 := -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 := -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 := CXX_OBJS := $(addprefix ${BUILD_DIR}/, $(subst .cpp,.o,$(${PRJ}_CXX_SRCS))) diff --git a/source/DeepStack.cpp b/source/DeepStack.cpp new file mode 100644 index 0000000..d2e4279 --- /dev/null +++ b/source/DeepStack.cpp @@ -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 +#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); +} + diff --git a/source/DeepStack.hpp b/source/DeepStack.hpp new file mode 100644 index 0000000..d20b219 --- /dev/null +++ b/source/DeepStack.hpp @@ -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 +#include +#include +#include +#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 */ + diff --git a/source/MainComponent.cpp b/source/MainComponent.cpp index 7289a2c..dd3358b 100644 --- a/source/MainComponent.cpp +++ b/source/MainComponent.cpp @@ -557,7 +557,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) 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); if (pPrev) @@ -583,7 +583,7 @@ void MainComponent::buttonClicked (Button* buttonThatWasClicked) { m_file = fc->getResult(); 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->loadWeights(); m_rbmSelect->clear(dontSendNotification); diff --git a/source/MainComponent.hpp b/source/MainComponent.hpp index 4f5b721..708c74f 100644 --- a/source/MainComponent.hpp +++ b/source/MainComponent.hpp @@ -23,7 +23,7 @@ //[Headers] -- You can add your own extra header files here -- #include #include "JuceHeader.h" -#include "Stack.hpp" +#include "DeepStack.hpp" #include "RbmComponent.hpp" //[/Headers] @@ -83,7 +83,7 @@ public: private: //[UserVariables] -- You can add your own custom variables in this section. static const size_t DBN_SIZE = 4; - ScopedPointer m_stack; + ScopedPointer m_stack; RbmComponent *m_pLayer; int m_weightIndex; int m_trainingIndex; diff --git a/source/Stack.cpp b/source/Stack.cpp deleted file mode 100644 index 470af8a..0000000 --- a/source/Stack.cpp +++ /dev/null @@ -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 -#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); -} - diff --git a/source/Stack.hpp b/source/Stack.hpp deleted file mode 100644 index 020a16b..0000000 --- a/source/Stack.hpp +++ /dev/null @@ -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 -#include -#include -#include -#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 */ - diff --git a/source/main.cpp b/source/main.cpp index e93e780..231f7c6 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -8,7 +8,7 @@ #include #include "Rbm.hpp" #include "Layer.hpp" -#include "Stack.hpp" +#include "DeepStack.hpp" using namespace std; class RbmListener : public Rbm::IListener @@ -35,7 +35,7 @@ int main() const string project("context99"); - Stack stack(".", project); + DeepStack stack(".", project); #if CREATE_TEST stack.addTraining(stack.trainingBatch().row(1)); diff --git a/source/poet.cpp b/source/poet.cpp index 5554d93..93ff39c 100644 --- a/source/poet.cpp +++ b/source/poet.cpp @@ -9,7 +9,7 @@ #include #include "Rbm.hpp" #include "Layer.hpp" -#include "Stack.hpp" +#include "DeepStack.hpp" using namespace std; using namespace arma; @@ -199,7 +199,7 @@ int main() return 0; #endif - Stack stack(".", "poet5"); + DeepStack stack(".", "poet5"); // Load project stack.load();