From fac88d488a9bd52528ace0bcf0b993aee1d08b50 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Wed, 13 Nov 2019 20:18:16 +0000 Subject: [PATCH] - added Stack::setName() git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@668 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- source/MainComponent.cpp | 4 + source/Stack.cpp | 645 ++++++++++++++++++++------------------- source/Stack.hpp | 145 ++++----- 3 files changed, 402 insertions(+), 392 deletions(-) diff --git a/source/MainComponent.cpp b/source/MainComponent.cpp index 4a87147..7d2ac7f 100644 --- a/source/MainComponent.cpp +++ b/source/MainComponent.cpp @@ -737,6 +737,10 @@ void MainComponent::labelTextChanged (Label* labelThatHasChanged) else if (labelThatHasChanged == projectNameLabel) { //[UserLabelCode_projectNameLabel] -- add your label text handling code here.. + if (m_stack) + { + m_stack->setName(labelThatHasChanged->getText().toStdString()); + } //[/UserLabelCode_projectNameLabel] } else if (labelThatHasChanged == numVisibleYLabel) diff --git a/source/Stack.cpp b/source/Stack.cpp index c6850e9..3dd79d2 100644 --- a/source/Stack.cpp +++ b/source/Stack.cpp @@ -1,320 +1,325 @@ -/* - * 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; - } -} - -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::Reader reader; - Json::Value project; - reader.parse(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(); - - Layer *pLayer = nullptr; - if (!pLayerConstructor) - { - pLayer = new Layer(layername, i, numVisibleX, numVisibleY, numHidden); - } - else - { - pLayer = pLayerConstructor->onConstruct(layername, i, numVisibleX, numVisibleY, numHidden); - } - - 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::StyledWriter writer; - 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 << writer.write(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->loadWeights(m_dir + "/" + m_name)) - { - return false; - } - pLayer = pLayer->next; - } - return true; -} - -bool Stack::saveWeights() -{ - Layer *pLayer = m_pLayers; - while(pLayer) - { - if (!pLayer->saveWeights(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->train(m_trainingData, pListener); - pLayer = pLayer->next; - } -} - -arma::mat& Stack::trainingData() -{ - return m_trainingData; -} - -arma::mat Stack::trainingData(Layer* pThatLayer) -{ - arma::mat thisBatch = m_trainingData; - Layer *pLayer = m_pLayers; - while (pLayer) - { - if (pLayer->id() == pThatLayer->id()) - { - break; - } - thisBatch = pLayer->toHiddenProbs(thisBatch); - pLayer = pLayer->next; - } - return thisBatch; -} - -size_t Stack::loadTraining(bool doNormalize) -{ - uint32_t numTraining = 0; - uint32_t numVisible = 0; - - std::string filename = m_dir + "/" + m_name + ".training.dat"; - FILE *pFile = fopen(filename.c_str(), "r"); - - if (!pFile) - { - std::cout << "Could not open " << filename << "!" << std::endl; - return 0; - } - - int result = fscanf(pFile, "%d\n", &numTraining); - if (result < 0) - { - return 0; - } - result = fscanf(pFile, "%d\n", &numVisible); - if (result < 0) - { - return 0; - } - m_trainingData = arma::zeros(numTraining, numVisible); - - uint32_t i, j; - for (i=0; i < numTraining; i++) - { - for (j=0; j < numVisible; j++) - { - float v; - int result = fscanf(pFile, "%f", &v); - if (result > 0) - { - m_trainingData(i, j) = v; - } - } - } - fclose(pFile); - std::cout << "Loaded " << numTraining << " training samples\n"; - - if (doNormalize) - { - m_trainingData = Rbm::normalize(m_trainingData); - } - return numTraining; -} - -size_t Stack::saveTraining() -{ - std::string filename = m_dir + "/" + m_name + ".training.dat"; - FILE *pFile = fopen(filename.c_str(), "w"); - - if (!pFile) - { - std::cout << "Could not open " << filename << "!" << std::endl; - return 0; - } - - fprintf(pFile, "%u\n", (uint32_t)m_trainingData.n_rows); - fprintf(pFile, "%u\n", (uint32_t)m_trainingData.n_cols); - - uint32_t i, j; - - for (i=0; i < m_trainingData.n_rows; i++) - { - for (j=0; j < m_trainingData.n_cols; j++) - { - fprintf(pFile, "%3.6f\n", m_trainingData(i, j)); - } - } - - fclose(pFile); - - std::cout << "Saved " << m_trainingData.n_rows << " training samples\n"; - return m_trainingData.n_rows; -} - -size_t Stack::numTraining() -{ - return m_trainingData.n_rows; -} - -void Stack::addTraining(const arma::mat &toAdd) -{ - m_trainingData.insert_rows(m_trainingData.n_rows, toAdd); -} - -void Stack::delTraining(int index) -{ - m_trainingData.shed_row(index); -} - +/* + * 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::Reader reader; + Json::Value project; + reader.parse(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(); + + Layer *pLayer = nullptr; + if (!pLayerConstructor) + { + pLayer = new Layer(layername, i, numVisibleX, numVisibleY, numHidden); + } + else + { + pLayer = pLayerConstructor->onConstruct(layername, i, numVisibleX, numVisibleY, numHidden); + } + + 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::StyledWriter writer; + 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 << writer.write(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->loadWeights(m_dir + "/" + m_name)) + { + return false; + } + pLayer = pLayer->next; + } + return true; +} + +bool Stack::saveWeights() +{ + Layer *pLayer = m_pLayers; + while(pLayer) + { + if (!pLayer->saveWeights(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->train(m_trainingData, pListener); + pLayer = pLayer->next; + } +} + +arma::mat& Stack::trainingData() +{ + return m_trainingData; +} + +arma::mat Stack::trainingData(Layer* pThatLayer) +{ + arma::mat thisBatch = m_trainingData; + Layer *pLayer = m_pLayers; + while (pLayer) + { + if (pLayer->id() == pThatLayer->id()) + { + break; + } + thisBatch = pLayer->toHiddenProbs(thisBatch); + pLayer = pLayer->next; + } + return thisBatch; +} + +size_t Stack::loadTraining(bool doNormalize) +{ + uint32_t numTraining = 0; + uint32_t numVisible = 0; + + std::string filename = m_dir + "/" + m_name + ".training.dat"; + FILE *pFile = fopen(filename.c_str(), "r"); + + if (!pFile) + { + std::cout << "Could not open " << filename << "!" << std::endl; + return 0; + } + + int result = fscanf(pFile, "%d\n", &numTraining); + if (result < 0) + { + return 0; + } + result = fscanf(pFile, "%d\n", &numVisible); + if (result < 0) + { + return 0; + } + m_trainingData = arma::zeros(numTraining, numVisible); + + uint32_t i, j; + for (i=0; i < numTraining; i++) + { + for (j=0; j < numVisible; j++) + { + float v; + int result = fscanf(pFile, "%f", &v); + if (result > 0) + { + m_trainingData(i, j) = v; + } + } + } + fclose(pFile); + std::cout << "Loaded " << numTraining << " training samples\n"; + + if (doNormalize) + { + m_trainingData = Rbm::normalize(m_trainingData); + } + return numTraining; +} + +size_t Stack::saveTraining() +{ + std::string filename = m_dir + "/" + m_name + ".training.dat"; + FILE *pFile = fopen(filename.c_str(), "w"); + + if (!pFile) + { + std::cout << "Could not open " << filename << "!" << std::endl; + return 0; + } + + fprintf(pFile, "%u\n", (uint32_t)m_trainingData.n_rows); + fprintf(pFile, "%u\n", (uint32_t)m_trainingData.n_cols); + + uint32_t i, j; + + for (i=0; i < m_trainingData.n_rows; i++) + { + for (j=0; j < m_trainingData.n_cols; j++) + { + fprintf(pFile, "%3.6f\n", m_trainingData(i, j)); + } + } + + fclose(pFile); + + std::cout << "Saved " << m_trainingData.n_rows << " training samples\n"; + return m_trainingData.n_rows; +} + +size_t Stack::numTraining() +{ + return m_trainingData.n_rows; +} + +void Stack::addTraining(const arma::mat &toAdd) +{ + m_trainingData.insert_rows(m_trainingData.n_rows, toAdd); +} + +void Stack::delTraining(int index) +{ + m_trainingData.shed_row(index); +} + diff --git a/source/Stack.hpp b/source/Stack.hpp index 3506580..66409a5 100644 --- a/source/Stack.hpp +++ b/source/Stack.hpp @@ -1,72 +1,73 @@ -/* - * 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) - { - return nullptr; - } -}; - -class Stack -{ -public: - Stack(const std::string &dir, const std::string &name); - Stack(const Stack& orig); - virtual ~Stack(); - - 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 loadTraining(bool doNormalize=false); - size_t saveTraining(); - arma::mat& trainingData(); - arma::mat trainingData(Layer *pLayer); - -private: - std::string m_dir; - std::string m_name; - Layer *m_pLayers; - arma::mat m_trainingData; - -}; - - -#endif /* STACK_HPP */ - +/* + * 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) + { + 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 loadTraining(bool doNormalize=false); + size_t saveTraining(); + arma::mat& trainingData(); + arma::mat trainingData(Layer *pLayer); + +private: + std::string m_dir; + std::string m_name; + Layer *m_pLayers; + arma::mat m_trainingData; + +}; + + +#endif /* STACK_HPP */ +