- 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:
+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)
|
||||
|
||||
Reference in New Issue
Block a user