- rename Layer into RbmLayer
- add some log info on toJson() git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@582 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
CONFIG ?= release
|
CONFIG ?= release
|
||||||
SRCS := source/main.cpp source/Rbm.cpp source/Layer.cpp source/Stack.cpp
|
SRCS := source/main.cpp source/Rbm.cpp source/RbmLayer.cpp source/Stack.cpp
|
||||||
|
|
||||||
|
|
||||||
LIBS := -larmadillo -ljsoncpp
|
LIBS := -larmadillo -ljsoncpp
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ void Rbm::fromJson(Json::Value params)
|
|||||||
|
|
||||||
Json::Value Rbm::toJson() const
|
Json::Value Rbm::toJson() const
|
||||||
{
|
{
|
||||||
|
std::cout << "Exporting Rbm" << std::endl;
|
||||||
Json::Value rbm;
|
Json::Value rbm;
|
||||||
rbm["numVisible"] = m_bv.n_elem;
|
rbm["numVisible"] = m_bv.n_elem;
|
||||||
rbm["numHidden"] = m_bh.n_elem;
|
rbm["numHidden"] = m_bh.n_elem;
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
#ifndef RBM_HPP
|
#ifndef RBM_HPP
|
||||||
#define RBM_HPP
|
#define RBM_HPP
|
||||||
|
|
||||||
|
#include <streambuf>
|
||||||
|
|
||||||
#include <armadillo>
|
#include <armadillo>
|
||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
|
|
||||||
@@ -38,6 +40,7 @@ public:
|
|||||||
|
|
||||||
Json::Value toJson() const
|
Json::Value toJson() const
|
||||||
{
|
{
|
||||||
|
std::cout << "Exporting Rbm::Params" << std::endl;
|
||||||
Json::Value params;
|
Json::Value params;
|
||||||
params["weightInit"] = weightInit;
|
params["weightInit"] = weightInit;
|
||||||
params["weightDecay"] = weightDecay;
|
params["weightDecay"] = weightDecay;
|
||||||
|
|||||||
@@ -11,10 +11,10 @@
|
|||||||
* Created on 25. Oktober 2019, 08:13
|
* Created on 25. Oktober 2019, 08:13
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Layer.hpp"
|
#include "RbmLayer.hpp"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Layer::Layer(const string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, const Rbm::Params ¶ms)
|
RbmLayer::RbmLayer(const string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, const Rbm::Params ¶ms)
|
||||||
: Rbm(params, numVisibleX*numVisibleY, numHidden)
|
: Rbm(params, numVisibleX*numVisibleY, numHidden)
|
||||||
, upper(nullptr)
|
, upper(nullptr)
|
||||||
, lower(nullptr)
|
, lower(nullptr)
|
||||||
@@ -27,7 +27,7 @@ Layer::Layer(const string &prjname, size_t id, size_t numVisibleX, size_t numVis
|
|||||||
m_weightsFile = m_prjname + string(".weights.") + to_string((int)m_id) + string(".dat");
|
m_weightsFile = m_prjname + string(".weights.") + to_string((int)m_id) + string(".dat");
|
||||||
}
|
}
|
||||||
|
|
||||||
Layer::Layer(const Layer& orig)
|
RbmLayer::RbmLayer(const RbmLayer& orig)
|
||||||
: Rbm(orig.m_rbm_params, orig.bv().n_elem, orig.bh().n_elem)
|
: Rbm(orig.m_rbm_params, orig.bv().n_elem, orig.bh().n_elem)
|
||||||
, upper(nullptr)
|
, upper(nullptr)
|
||||||
, lower(nullptr)
|
, lower(nullptr)
|
||||||
@@ -40,11 +40,11 @@ Layer::Layer(const Layer& orig)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Layer::~Layer()
|
RbmLayer::~RbmLayer()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Layer::saveWeights()
|
void RbmLayer::saveWeights()
|
||||||
{
|
{
|
||||||
FILE *pFile = fopen(m_weightsFile.c_str(), "w");
|
FILE *pFile = fopen(m_weightsFile.c_str(), "w");
|
||||||
|
|
||||||
@@ -81,8 +81,9 @@ void Layer::saveWeights()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value Layer::toJson() const
|
Json::Value RbmLayer::toJson() const
|
||||||
{
|
{
|
||||||
|
std::cout << "Exporting RbmLayer " << to_string((int)m_id) << std::endl;
|
||||||
Json::Value layer;
|
Json::Value layer;
|
||||||
layer["name"] = string("Layer ") + to_string((int)m_id);
|
layer["name"] = string("Layer ") + to_string((int)m_id);
|
||||||
layer["weights_file"] = m_weightsFile;
|
layer["weights_file"] = m_weightsFile;
|
||||||
@@ -94,7 +95,7 @@ Json::Value Layer::toJson() const
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
arma::mat Layer::up_pass(const arma::mat &hidden)
|
arma::mat RbmLayer::up_pass(const arma::mat &hidden)
|
||||||
{
|
{
|
||||||
arma::mat reconstruction = toVisibleProbs(hidden);
|
arma::mat reconstruction = toVisibleProbs(hidden);
|
||||||
if (upper)
|
if (upper)
|
||||||
@@ -104,7 +105,7 @@ arma::mat Layer::up_pass(const arma::mat &hidden)
|
|||||||
return toHiddenProbs(reconstruction);
|
return toHiddenProbs(reconstruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
arma::mat Layer::down_pass(const arma::mat &visible)
|
arma::mat RbmLayer::down_pass(const arma::mat &visible)
|
||||||
{
|
{
|
||||||
arma::mat hidden = toHiddenProbs(visible);
|
arma::mat hidden = toHiddenProbs(visible);
|
||||||
if (lower)
|
if (lower)
|
||||||
@@ -11,8 +11,8 @@
|
|||||||
* Created on 25. Oktober 2019, 08:13
|
* Created on 25. Oktober 2019, 08:13
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LAYER_HPP
|
#ifndef RBMLAYER_HPP
|
||||||
#define LAYER_HPP
|
#define RBMLAYER_HPP
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -21,15 +21,15 @@
|
|||||||
#include "Rbm.hpp"
|
#include "Rbm.hpp"
|
||||||
#include "ILayer.hpp"
|
#include "ILayer.hpp"
|
||||||
|
|
||||||
class Layer : public Rbm
|
class RbmLayer : public Rbm
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Layer *upper;
|
RbmLayer *upper;
|
||||||
Layer *lower;
|
RbmLayer *lower;
|
||||||
|
|
||||||
Layer(const std::string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, const Rbm::Params ¶ms);
|
RbmLayer(const std::string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, const Rbm::Params ¶ms);
|
||||||
Layer(const Layer& orig);
|
RbmLayer(const RbmLayer& orig);
|
||||||
virtual ~Layer();
|
virtual ~RbmLayer();
|
||||||
|
|
||||||
Json::Value toJson() const;
|
Json::Value toJson() const;
|
||||||
void saveWeights();
|
void saveWeights();
|
||||||
@@ -52,5 +52,5 @@ private:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* LAYER_HPP */
|
#endif /* RBMLAYER_HPP */
|
||||||
|
|
||||||
+6
-5
@@ -31,7 +31,7 @@ Stack::~Stack()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Stack::addLayer(Layer *pOtherLayer)
|
void Stack::addLayer(RbmLayer *pOtherLayer)
|
||||||
{
|
{
|
||||||
if (!m_pLayers)
|
if (!m_pLayers)
|
||||||
{
|
{
|
||||||
@@ -40,7 +40,7 @@ void Stack::addLayer(Layer *pOtherLayer)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Layer *pLayer = m_pLayers;
|
RbmLayer *pLayer = m_pLayers;
|
||||||
while(pLayer->upper)
|
while(pLayer->upper)
|
||||||
{
|
{
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->upper;
|
||||||
@@ -50,9 +50,9 @@ void Stack::addLayer(Layer *pOtherLayer)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Layer* Stack::getLayer(size_t id)
|
const RbmLayer* Stack::getLayer(size_t id)
|
||||||
{
|
{
|
||||||
Layer *pLayer = m_pLayers;
|
RbmLayer *pLayer = m_pLayers;
|
||||||
while(pLayer)
|
while(pLayer)
|
||||||
{
|
{
|
||||||
if (pLayer->id() == id)
|
if (pLayer->id() == id)
|
||||||
@@ -67,6 +67,7 @@ const Layer* Stack::getLayer(size_t id)
|
|||||||
|
|
||||||
void Stack::save(size_t numTraining)
|
void Stack::save(size_t numTraining)
|
||||||
{
|
{
|
||||||
|
std::cout << "Exporting Project " << m_prjname << std::endl;
|
||||||
ofstream ofs(m_prjname + string(".prj"));
|
ofstream ofs(m_prjname + string(".prj"));
|
||||||
|
|
||||||
Json::StyledWriter writer;
|
Json::StyledWriter writer;
|
||||||
@@ -76,7 +77,7 @@ void Stack::save(size_t numTraining)
|
|||||||
project["stack"]["training_file"] = m_prjname + string(".training.dat");
|
project["stack"]["training_file"] = m_prjname + string(".training.dat");
|
||||||
|
|
||||||
Json::Value layers(Json::arrayValue);
|
Json::Value layers(Json::arrayValue);
|
||||||
Layer *pLayer = m_pLayers;
|
RbmLayer *pLayer = m_pLayers;
|
||||||
while(pLayer)
|
while(pLayer)
|
||||||
{
|
{
|
||||||
layers.append(pLayer->toJson());
|
layers.append(pLayer->toJson());
|
||||||
|
|||||||
+4
-4
@@ -18,7 +18,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <armadillo>
|
#include <armadillo>
|
||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include "Layer.hpp"
|
#include "RbmLayer.hpp"
|
||||||
|
|
||||||
class Stack
|
class Stack
|
||||||
{
|
{
|
||||||
@@ -27,13 +27,13 @@ public:
|
|||||||
Stack(const Stack& orig);
|
Stack(const Stack& orig);
|
||||||
virtual ~Stack();
|
virtual ~Stack();
|
||||||
|
|
||||||
void addLayer(Layer *pLayer);
|
void addLayer(RbmLayer *pLayer);
|
||||||
const Layer* getLayer(size_t id);
|
const RbmLayer* getLayer(size_t id);
|
||||||
void save(size_t numTraining);
|
void save(size_t numTraining);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::string &m_prjname;
|
const std::string &m_prjname;
|
||||||
Layer *m_pLayers;
|
RbmLayer *m_pLayers;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+5
-5
@@ -7,7 +7,7 @@
|
|||||||
#include <armadillo>
|
#include <armadillo>
|
||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include "Rbm.hpp"
|
#include "Rbm.hpp"
|
||||||
#include "Layer.hpp"
|
#include "RbmLayer.hpp"
|
||||||
#include "Stack.hpp"
|
#include "Stack.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -93,10 +93,10 @@ int main()
|
|||||||
|
|
||||||
printf("Loaded %d training samples\n", (int)numTraining);
|
printf("Loaded %d training samples\n", (int)numTraining);
|
||||||
|
|
||||||
Layer layer0(project, 0, numVisibleX, numVisibleY, numHidden, rbmParams);
|
RbmLayer layer0(project, 0, numVisibleX, numVisibleY, numHidden, rbmParams);
|
||||||
Layer layer1(project, 1, numVisibleX, numVisibleY, numHidden, rbmParams);
|
RbmLayer layer1(project, 1, numVisibleX, numVisibleY, numHidden, rbmParams);
|
||||||
Layer layer2(project, 2, numVisibleX, numVisibleY, numHidden, rbmParams);
|
RbmLayer layer2(project, 2, numVisibleX, numVisibleY, numHidden, rbmParams);
|
||||||
Layer layer3(project, 3, numVisibleX, numVisibleY, numHidden, rbmParams);
|
RbmLayer layer3(project, 3, numVisibleX, numVisibleY, numHidden, rbmParams);
|
||||||
Stack stack(project);
|
Stack stack(project);
|
||||||
stack.addLayer(&layer0);
|
stack.addLayer(&layer0);
|
||||||
stack.addLayer(&layer1);
|
stack.addLayer(&layer1);
|
||||||
|
|||||||
Reference in New Issue
Block a user