From c68faa894ef3eaf2d5d2f8d5953e45b1c1bb16fc Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Fri, 25 Oct 2019 16:41:39 +0000 Subject: [PATCH] - moved all functions to Layer - added Stack git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@577 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- Makefile | 2 +- source/ILayer.hpp | 44 ++++++++++++++++++++++++++++++ source/Layer.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++-- source/Layer.hpp | 15 ++++++++--- source/Stack.cpp | 27 +++++++++++++++++++ source/Stack.hpp | 36 +++++++++++++++++++++++++ source/main.cpp | 14 +++++----- 7 files changed, 192 insertions(+), 14 deletions(-) create mode 100644 source/ILayer.hpp create mode 100644 source/Stack.cpp create mode 100644 source/Stack.hpp diff --git a/Makefile b/Makefile index 3639425..9ee8741 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CONFIG ?= release -SRCS := source/main.cpp source/Rbm.cpp source/Layer.cpp source/noise.c +SRCS := source/main.cpp source/Rbm.cpp source/Layer.cpp source/Stack.cpp LIBS := -larmadillo -ljsoncpp diff --git a/source/ILayer.hpp b/source/ILayer.hpp new file mode 100644 index 0000000..8caea5f --- /dev/null +++ b/source/ILayer.hpp @@ -0,0 +1,44 @@ +/* + * 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: ILayer.hpp + * Author: jens + * + * Created on 25. Oktober 2019, 18:33 + */ + +#ifndef ILAYER_HPP +#define ILAYER_HPP + +#include + +class ILayer +{ +public: + ILayer *upper; + ILayer *lower; + + ILayer() + : upper(nullptr) + , lower(nullptr) + {} + + virtual ~ILayer() + {} + + virtual void up_pass(arma::mat const &v) = 0; + virtual void down_pass(arma::mat &dst, arma::mat const &src) = 0; + void registerLayer(ILayer *pLayer) + { + lower = pLayer; + pLayer->upper = this; + } +}; + + +#endif /* ILAYER_HPP */ + diff --git a/source/Layer.cpp b/source/Layer.cpp index 84d1446..e9eac7b 100644 --- a/source/Layer.cpp +++ b/source/Layer.cpp @@ -14,10 +14,12 @@ #include "Layer.hpp" using namespace std; -Layer::Layer(const string &prjname, size_t id, size_t numVisible, size_t numHidden) +Layer::Layer(const string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden) : m_prjname(prjname) , m_id(id) -, m_rbm(m_rbm_params, numVisible, numHidden) +, m_numVisibleX(numVisibleX) +, m_numVisibleY(numVisibleY) +, m_rbm(m_rbm_params, numVisibleX*numVisibleY, numHidden) { } @@ -25,23 +27,85 @@ Layer::Layer(const string &prjname, size_t id, size_t numVisible, size_t numHidd Layer::Layer(const Layer& orig) : m_prjname(orig.m_prjname) , m_id(orig.m_id) +, m_numVisibleX(orig.m_numVisibleX) +, m_numVisibleY(orig.m_numVisibleY) , m_rbm_params(orig.m_rbm_params) , m_rbm(orig.m_rbm) { + m_weightsFile = m_prjname + string(".weights.dat"); } Layer::~Layer() { } +void Layer::saveWeight() +{ + FILE *pFile; + + pFile = fopen(m_weightsFile.c_str(), "w"); + + if (!pFile) + { + std::cout << "Could not open " << m_weightsFile << "!" << std::endl; + return; + } + + const arma::mat &bv = rbm().bv(); + const arma::mat &bh = rbm().bh(); + const arma::mat &w = rbm().w(); + + size_t numHidden = bh.n_elem; + size_t numVisible = bv.n_elem; + fprintf(pFile, "%d %d %d\n", (int)m_numVisibleX, (int)m_numVisibleY, (int)numHidden); + + uint32_t i, j; + + for (i=0; i < numVisible; i++) + { + fprintf(pFile, "%3.6f\n", bv(i)); + } + for (i=0; i < numHidden; i++) + { + fprintf(pFile, "%3.6f\n", bh(i)); + } + for (i=0; i < numVisible; i++) + { + for (j=0; j < numHidden; j++) + { + fprintf(pFile, "%3.6f ", w(i,j)); + } + fprintf(pFile, "\n"); + } + + fclose(pFile); + +} + +Rbm& Layer::rbm() +{ + return m_rbm; +} + Json::Value Layer::toJson() const { Json::Value layer; layer["name"] = string("Layer ") + to_string((int)m_id); layer["weights_file"] = m_prjname + string(".weights.dat"); + layer["numVisibleX"] = to_string((int)m_numVisibleX); + layer["numVisibleY"] = to_string((int)m_numVisibleX); layer["rbm"] = m_rbm.toJson(); return layer; } +void Layer::up_pass(const arma::mat& v) +{ + +} + +void Layer::down_pass(arma::mat& dst, const arma::mat& src) +{ + +} diff --git a/source/Layer.hpp b/source/Layer.hpp index 97c4114..317d25d 100644 --- a/source/Layer.hpp +++ b/source/Layer.hpp @@ -19,19 +19,28 @@ #include #include #include "Rbm.hpp" +#include "ILayer.hpp" -class Layer +class Layer : public ILayer { public: - Layer(const std::string &prjname, size_t id, size_t numVisible, size_t numHidden); + Layer(const std::string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden); Layer(const Layer& orig); virtual ~Layer(); Json::Value toJson() const; - + Rbm& rbm(); + void saveWeight(); + + void up_pass(const arma::mat& v) override; + void down_pass(arma::mat& dst, const arma::mat& src) override; + private: const std::string &m_prjname; + std::string m_weightsFile; size_t m_id; + size_t m_numVisibleX; + size_t m_numVisibleY; Rbm::Params m_rbm_params; Rbm m_rbm; diff --git a/source/Stack.cpp b/source/Stack.cpp new file mode 100644 index 0000000..cfaafec --- /dev/null +++ b/source/Stack.cpp @@ -0,0 +1,27 @@ +/* + * 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 "Stack.hpp" + +Stack::Stack() +{ +} + +Stack::Stack(const Stack& orig) +{ +} + +Stack::~Stack() +{ +} + diff --git a/source/Stack.hpp b/source/Stack.hpp new file mode 100644 index 0000000..f4083f2 --- /dev/null +++ b/source/Stack.hpp @@ -0,0 +1,36 @@ +/* + * 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 "Layer.hpp" + +class Stack +{ +public: + Stack(); + Stack(const Stack& orig); + virtual ~Stack(); + + void addLayer(const Layer &layer); + const Layer& getLayer(); + +private: + +}; + +#endif /* STACK_HPP */ + diff --git a/source/main.cpp b/source/main.cpp index 30243c0..fc075a8 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -144,22 +144,20 @@ int main() arma::mat batch = loadTraining(project + string(".training.dat")); size_t numTraining = batch.n_rows; - size_t numVisible = batch.n_cols; + size_t numVisibleX = 28; + size_t numVisibleY = 28; size_t numHidden = 64; - Layer layer(project, 0, numVisible, numHidden); + Layer layer(project, 0, numVisibleX, numVisibleY, numHidden); saveProject(project, layer, numTraining); printf("Loaded %d training samples\n", (int)numTraining); - arma::mat w = arma::zeros(numVisible, numHidden); - arma::mat bv = arma::zeros(1, numVisible); - arma::mat bh = arma::zeros(1, numHidden); - Rbm rbm(rbmParams, numVisible, numHidden); + Rbm &rbm = layer.rbm(); rbm.train(batch, 1000, 100, &statusDisplay); - saveWeight(project + string(".weights.dat"), 28, 28, rbm.w(), rbm.bv(), rbm.bh()); - arma::mat v = arma::randu(numTraining, numVisible); + layer.saveWeight(); + arma::mat v = arma::randu(numTraining, numVisibleX*numVisibleY); arma::mat h = rbm.toHiddenProbs(v); arma::mat r = rbm.toVisibleProbs(h); return 0;