- refactored
- added LayerContructor git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@618 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+8
-8
@@ -16,8 +16,8 @@ using namespace std;
|
|||||||
|
|
||||||
Layer::Layer(const string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden)
|
Layer::Layer(const string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden)
|
||||||
: Rbm(numVisibleX*numVisibleY, numHidden)
|
: Rbm(numVisibleX*numVisibleY, numHidden)
|
||||||
, upper(nullptr)
|
, next(nullptr)
|
||||||
, lower(nullptr)
|
, prev(nullptr)
|
||||||
, m_name(name)
|
, m_name(name)
|
||||||
, m_id(id)
|
, m_id(id)
|
||||||
, m_numVisibleX(numVisibleX)
|
, m_numVisibleX(numVisibleX)
|
||||||
@@ -29,8 +29,8 @@ Layer::Layer(const string &name, size_t id, size_t numVisibleX, size_t numVisibl
|
|||||||
|
|
||||||
Layer::Layer(const Layer& orig)
|
Layer::Layer(const Layer& orig)
|
||||||
: Rbm(orig.bv().n_elem, orig.bh().n_elem)
|
: Rbm(orig.bv().n_elem, orig.bh().n_elem)
|
||||||
, upper(nullptr)
|
, next(nullptr)
|
||||||
, lower(nullptr)
|
, prev(nullptr)
|
||||||
, m_name(orig.m_name)
|
, m_name(orig.m_name)
|
||||||
, m_weightsFile(orig.m_weightsFile)
|
, m_weightsFile(orig.m_weightsFile)
|
||||||
, m_id(orig.m_id)
|
, m_id(orig.m_id)
|
||||||
@@ -166,9 +166,9 @@ Json::Value Layer::toJson() const
|
|||||||
arma::mat Layer::up_pass(const arma::mat &hidden)
|
arma::mat Layer::up_pass(const arma::mat &hidden)
|
||||||
{
|
{
|
||||||
arma::mat reconstruction = toVisibleProbs(hidden);
|
arma::mat reconstruction = toVisibleProbs(hidden);
|
||||||
if (upper)
|
if (next)
|
||||||
{
|
{
|
||||||
return upper->up_pass(reconstruction);
|
return next->up_pass(reconstruction);
|
||||||
}
|
}
|
||||||
return toHiddenProbs(reconstruction);
|
return toHiddenProbs(reconstruction);
|
||||||
}
|
}
|
||||||
@@ -176,9 +176,9 @@ arma::mat Layer::up_pass(const arma::mat &hidden)
|
|||||||
arma::mat Layer::down_pass(const arma::mat &visible)
|
arma::mat Layer::down_pass(const arma::mat &visible)
|
||||||
{
|
{
|
||||||
arma::mat hidden = toHiddenProbs(visible);
|
arma::mat hidden = toHiddenProbs(visible);
|
||||||
if (lower)
|
if (prev)
|
||||||
{
|
{
|
||||||
return lower->down_pass(hidden);
|
return prev->down_pass(hidden);
|
||||||
}
|
}
|
||||||
return toVisibleProbs(hidden);
|
return toVisibleProbs(hidden);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -24,10 +24,10 @@
|
|||||||
class Layer : public Rbm
|
class Layer : public Rbm
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Layer *upper;
|
Layer *next;
|
||||||
Layer *lower;
|
Layer *prev;
|
||||||
|
|
||||||
Layer(const std::string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden);
|
Layer(const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden);
|
||||||
Layer(const Layer& orig);
|
Layer(const Layer& orig);
|
||||||
virtual ~Layer();
|
virtual ~Layer();
|
||||||
|
|
||||||
|
|||||||
+29
-17
@@ -10,7 +10,7 @@
|
|||||||
*
|
*
|
||||||
* Created on 25. Oktober 2019, 18:26
|
* Created on 25. Oktober 2019, 18:26
|
||||||
*/
|
*/
|
||||||
|
#include <cassert>
|
||||||
#include "Stack.hpp"
|
#include "Stack.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@@ -37,7 +37,7 @@ size_t Stack::numLayers()
|
|||||||
while(pLayer)
|
while(pLayer)
|
||||||
{
|
{
|
||||||
count++;
|
count++;
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
@@ -47,17 +47,17 @@ void Stack::addLayer(Layer *pOtherLayer)
|
|||||||
if (!m_pLayers)
|
if (!m_pLayers)
|
||||||
{
|
{
|
||||||
m_pLayers = pOtherLayer;
|
m_pLayers = pOtherLayer;
|
||||||
pOtherLayer->lower = nullptr;
|
pOtherLayer->prev = nullptr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Layer *pLayer = m_pLayers;
|
Layer *pLayer = m_pLayers;
|
||||||
while(pLayer->upper)
|
while(pLayer->next)
|
||||||
{
|
{
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
pLayer->upper = pOtherLayer;
|
pLayer->next = pOtherLayer;
|
||||||
pOtherLayer->lower = pLayer;
|
pOtherLayer->prev = pLayer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,13 +70,13 @@ Layer* Stack::getLayer(size_t layerId) const
|
|||||||
{
|
{
|
||||||
return pLayer;
|
return pLayer;
|
||||||
}
|
}
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Stack::load()
|
bool Stack::load(LayerConstructor *pLayerConstructor)
|
||||||
{
|
{
|
||||||
std::cout << "Importing Project " << m_name << std::endl;
|
std::cout << "Importing Project " << m_name << std::endl;
|
||||||
ifstream ifs(m_name + string(".prj"));
|
ifstream ifs(m_name + string(".prj"));
|
||||||
@@ -85,7 +85,7 @@ bool Stack::load()
|
|||||||
Json::Value project;
|
Json::Value project;
|
||||||
reader.parse(ifs, project);
|
reader.parse(ifs, project);
|
||||||
|
|
||||||
const string &prjname = project["stack"]["name"].asString();
|
const string &name = project["stack"]["name"].asString();
|
||||||
Json::Value &layers = project["stack"]["layers"];
|
Json::Value &layers = project["stack"]["layers"];
|
||||||
|
|
||||||
for (int i=0; i < layers.size(); i++)
|
for (int i=0; i < layers.size(); i++)
|
||||||
@@ -96,7 +96,19 @@ bool Stack::load()
|
|||||||
int numVisibleX = layer["numVisibleX"].asInt();
|
int numVisibleX = layer["numVisibleX"].asInt();
|
||||||
int numVisibleY = layer["numVisibleY"].asInt();
|
int numVisibleY = layer["numVisibleY"].asInt();
|
||||||
int numHidden = layer["numHidden"].asInt();
|
int numHidden = layer["numHidden"].asInt();
|
||||||
Layer *pLayer = new Layer(layername, i, numVisibleX, numVisibleY, numHidden);
|
|
||||||
|
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"]);
|
pLayer->fromJson(layer["rbm"]);
|
||||||
addLayer(pLayer);
|
addLayer(pLayer);
|
||||||
}
|
}
|
||||||
@@ -118,7 +130,7 @@ bool Stack::save()
|
|||||||
while(pLayer)
|
while(pLayer)
|
||||||
{
|
{
|
||||||
layers.append(pLayer->toJson());
|
layers.append(pLayer->toJson());
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
project["stack"]["layers"] = layers;
|
project["stack"]["layers"] = layers;
|
||||||
|
|
||||||
@@ -133,7 +145,7 @@ void Stack::weightsInit(double stddev)
|
|||||||
while(pLayer)
|
while(pLayer)
|
||||||
{
|
{
|
||||||
pLayer->weightsInit(stddev);
|
pLayer->weightsInit(stddev);
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +158,7 @@ bool Stack::loadWeights()
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -160,7 +172,7 @@ bool Stack::saveWeights()
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -171,7 +183,7 @@ void Stack::train(const arma::mat& batch, Rbm::IListener* pListener)
|
|||||||
while(pLayer)
|
while(pLayer)
|
||||||
{
|
{
|
||||||
train(pLayer->id(), batch, pListener);
|
train(pLayer->id(), batch, pListener);
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +198,7 @@ void Stack::train(size_t layerId, const arma::mat& batch, Rbm::IListener* pListe
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
||||||
pLayer = pLayer->upper;
|
pLayer = pLayer->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pLayer)
|
if (pLayer)
|
||||||
|
|||||||
+13
-1
@@ -20,6 +20,18 @@
|
|||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
#include "Layer.hpp"
|
#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
|
class Stack
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -32,7 +44,7 @@ public:
|
|||||||
|
|
||||||
void train(size_t layerId, const arma::mat& batch, Rbm::IListener* pListener);
|
void train(size_t layerId, const arma::mat& batch, Rbm::IListener* pListener);
|
||||||
void train(const arma::mat& batch, Rbm::IListener* pListener);
|
void train(const arma::mat& batch, Rbm::IListener* pListener);
|
||||||
bool load();
|
bool load(LayerConstructor *pLayerConstructor=nullptr);
|
||||||
bool save();
|
bool save();
|
||||||
void weightsInit(double stddev);
|
void weightsInit(double stddev);
|
||||||
bool loadWeights();
|
bool loadWeights();
|
||||||
|
|||||||
Reference in New Issue
Block a user