- added StackCreator
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@823 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+43
-67
@@ -12,15 +12,17 @@
|
||||
*/
|
||||
|
||||
#include "AStack.hpp"
|
||||
#include "StackCreator.hpp"
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
AStack::AStack(const std::string &dir, StackType type, const std::string &name)
|
||||
const char *AStack::stackTypeStrings[NUM_STACKTYPES] = {"None", "Deep", "Rnn"};
|
||||
|
||||
AStack::AStack(StackType type, const std::string &name)
|
||||
: m_name(name)
|
||||
, m_type(type)
|
||||
, m_pLayers(nullptr)
|
||||
, m_dir(dir)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -39,6 +41,39 @@ AStack::~AStack()
|
||||
}
|
||||
}
|
||||
|
||||
AStack::AStack(StackType type, const std::string &name, Json::Value& layers, LayerConstructor* pLayerConstructor)
|
||||
: m_type(type)
|
||||
, m_name(name)
|
||||
, m_pLayers(nullptr)
|
||||
{
|
||||
for (int i=0; i < layers.size(); i++)
|
||||
{
|
||||
Json::Value &layer = layers[i];
|
||||
|
||||
string layername = layer["name"].asString();
|
||||
int numVisibleX = layer["numVisibleX"].asInt();
|
||||
int numVisibleY = layer["numVisibleY"].asInt();
|
||||
int numHidden = layer["numHidden"].asInt();
|
||||
int numContext = layer["numContext"].asInt();
|
||||
|
||||
Layer *pLayer = nullptr;
|
||||
if (!pLayerConstructor)
|
||||
{
|
||||
pLayer = new Layer(layername, i, numVisibleX, numVisibleY, numHidden, numContext);
|
||||
}
|
||||
else
|
||||
{
|
||||
pLayer = pLayerConstructor->onConstruct(this, layername, i, numVisibleX, numVisibleY, numHidden, numContext);
|
||||
}
|
||||
|
||||
assert(pLayer != nullptr);
|
||||
|
||||
pLayer->fromJson(layer["rbm"]);
|
||||
addLayer(pLayer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
AStack::StackType AStack::type()
|
||||
{
|
||||
return m_type;
|
||||
@@ -100,68 +135,9 @@ Layer* AStack::getLayer(size_t layerId) const
|
||||
|
||||
}
|
||||
|
||||
bool AStack::load(LayerConstructor *pLayerConstructor)
|
||||
bool AStack::save(const std::string &dir)
|
||||
{
|
||||
std::cout << "Importing Project " << m_name << std::endl;
|
||||
ifstream ifs(m_dir + "/" + m_name + string(".prj"));
|
||||
|
||||
Json::Value project;
|
||||
ifs >> project;
|
||||
|
||||
const string &name = project["stack"]["name"].asString();
|
||||
Json::Value &layers = project["stack"]["layers"];
|
||||
|
||||
for (int i=0; i < layers.size(); i++)
|
||||
{
|
||||
Json::Value &layer = layers[i];
|
||||
|
||||
string layername = layer["name"].asString();
|
||||
int numVisibleX = layer["numVisibleX"].asInt();
|
||||
int numVisibleY = layer["numVisibleY"].asInt();
|
||||
int numHidden = layer["numHidden"].asInt();
|
||||
int numContext = layer["numContext"].asInt();
|
||||
|
||||
Layer *pLayer = nullptr;
|
||||
if (!pLayerConstructor)
|
||||
{
|
||||
pLayer = new Layer(layername, i, numVisibleX, numVisibleY, numHidden, numContext);
|
||||
}
|
||||
else
|
||||
{
|
||||
pLayer = pLayerConstructor->onConstruct(layername, i, numVisibleX, numVisibleY, numHidden, numContext);
|
||||
}
|
||||
|
||||
assert(pLayer != nullptr);
|
||||
|
||||
pLayer->fromJson(layer["rbm"]);
|
||||
addLayer(pLayer);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AStack::save()
|
||||
{
|
||||
std::cout << "Exporting Project " << m_name << std::endl;
|
||||
ofstream ofs(m_dir + "/" + m_name + string(".prj"));
|
||||
|
||||
Json::Value project;
|
||||
project["stack"]["name"] = m_name;
|
||||
project["stack"]["type_string"] = stackTypeStrings[m_type];
|
||||
project["stack"]["type"] = m_type;
|
||||
|
||||
Json::Value layers(Json::arrayValue);
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
layers.append(pLayer->toJson());
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
project["stack"]["layers"] = layers;
|
||||
|
||||
ofs << project;
|
||||
|
||||
return true;
|
||||
return StackCreator::toFile(this, dir, m_name);
|
||||
}
|
||||
|
||||
void AStack::weightsInit(double stddev)
|
||||
@@ -174,12 +150,12 @@ void AStack::weightsInit(double stddev)
|
||||
}
|
||||
}
|
||||
|
||||
bool AStack::loadWeights()
|
||||
bool AStack::loadWeights(const std::string &dir)
|
||||
{
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
if (!pLayer->weightsLoad(m_dir, m_name))
|
||||
if (!pLayer->weightsLoad(dir, m_name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -188,12 +164,12 @@ bool AStack::loadWeights()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AStack::saveWeights()
|
||||
bool AStack::saveWeights(const std::string &dir)
|
||||
{
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
if (!pLayer->weightsSave(m_dir, m_name))
|
||||
if (!pLayer->weightsSave(dir, m_name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user