- 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)
|
||||
: Rbm(numVisibleX*numVisibleY, numHidden)
|
||||
, upper(nullptr)
|
||||
, lower(nullptr)
|
||||
, next(nullptr)
|
||||
, prev(nullptr)
|
||||
, m_name(name)
|
||||
, m_id(id)
|
||||
, 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)
|
||||
: Rbm(orig.bv().n_elem, orig.bh().n_elem)
|
||||
, upper(nullptr)
|
||||
, lower(nullptr)
|
||||
, next(nullptr)
|
||||
, prev(nullptr)
|
||||
, m_name(orig.m_name)
|
||||
, m_weightsFile(orig.m_weightsFile)
|
||||
, 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 reconstruction = toVisibleProbs(hidden);
|
||||
if (upper)
|
||||
if (next)
|
||||
{
|
||||
return upper->up_pass(reconstruction);
|
||||
return next->up_pass(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 hidden = toHiddenProbs(visible);
|
||||
if (lower)
|
||||
if (prev)
|
||||
{
|
||||
return lower->down_pass(hidden);
|
||||
return prev->down_pass(hidden);
|
||||
}
|
||||
return toVisibleProbs(hidden);
|
||||
}
|
||||
|
||||
+3
-3
@@ -24,10 +24,10 @@
|
||||
class Layer : public Rbm
|
||||
{
|
||||
public:
|
||||
Layer *upper;
|
||||
Layer *lower;
|
||||
Layer *next;
|
||||
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);
|
||||
virtual ~Layer();
|
||||
|
||||
|
||||
+29
-17
@@ -10,7 +10,7 @@
|
||||
*
|
||||
* Created on 25. Oktober 2019, 18:26
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include "Stack.hpp"
|
||||
|
||||
using namespace std;
|
||||
@@ -37,7 +37,7 @@ size_t Stack::numLayers()
|
||||
while(pLayer)
|
||||
{
|
||||
count++;
|
||||
pLayer = pLayer->upper;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -47,17 +47,17 @@ void Stack::addLayer(Layer *pOtherLayer)
|
||||
if (!m_pLayers)
|
||||
{
|
||||
m_pLayers = pOtherLayer;
|
||||
pOtherLayer->lower = nullptr;
|
||||
pOtherLayer->prev = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer->upper)
|
||||
while(pLayer->next)
|
||||
{
|
||||
pLayer = pLayer->upper;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
pLayer->upper = pOtherLayer;
|
||||
pOtherLayer->lower = pLayer;
|
||||
pLayer->next = pOtherLayer;
|
||||
pOtherLayer->prev = pLayer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,13 +70,13 @@ Layer* Stack::getLayer(size_t layerId) const
|
||||
{
|
||||
return pLayer;
|
||||
}
|
||||
pLayer = pLayer->upper;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return nullptr;
|
||||
|
||||
}
|
||||
|
||||
bool Stack::load()
|
||||
bool Stack::load(LayerConstructor *pLayerConstructor)
|
||||
{
|
||||
std::cout << "Importing Project " << m_name << std::endl;
|
||||
ifstream ifs(m_name + string(".prj"));
|
||||
@@ -85,7 +85,7 @@ bool Stack::load()
|
||||
Json::Value 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"];
|
||||
|
||||
for (int i=0; i < layers.size(); i++)
|
||||
@@ -96,7 +96,19 @@ bool Stack::load()
|
||||
int numVisibleX = layer["numVisibleX"].asInt();
|
||||
int numVisibleY = layer["numVisibleY"].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"]);
|
||||
addLayer(pLayer);
|
||||
}
|
||||
@@ -118,7 +130,7 @@ bool Stack::save()
|
||||
while(pLayer)
|
||||
{
|
||||
layers.append(pLayer->toJson());
|
||||
pLayer = pLayer->upper;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
project["stack"]["layers"] = layers;
|
||||
|
||||
@@ -133,7 +145,7 @@ void Stack::weightsInit(double stddev)
|
||||
while(pLayer)
|
||||
{
|
||||
pLayer->weightsInit(stddev);
|
||||
pLayer = pLayer->upper;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +158,7 @@ bool Stack::loadWeights()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
pLayer = pLayer->upper;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -160,7 +172,7 @@ bool Stack::saveWeights()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
pLayer = pLayer->upper;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -171,7 +183,7 @@ void Stack::train(const arma::mat& batch, Rbm::IListener* pListener)
|
||||
while(pLayer)
|
||||
{
|
||||
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;
|
||||
}
|
||||
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
||||
pLayer = pLayer->upper;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
|
||||
if (pLayer)
|
||||
|
||||
+13
-1
@@ -20,6 +20,18 @@
|
||||
#include <jsoncpp/json/json.h>
|
||||
#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:
|
||||
@@ -32,7 +44,7 @@ public:
|
||||
|
||||
void train(size_t layerId, 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();
|
||||
void weightsInit(double stddev);
|
||||
bool loadWeights();
|
||||
|
||||
Reference in New Issue
Block a user