git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@818 b431acfa-c32f-4a4a-93f1-934dc6c82436
216 lines
3.8 KiB
C++
216 lines
3.8 KiB
C++
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
|
|
/*
|
|
* File: AStack.cpp
|
|
* Author: jens
|
|
*
|
|
* Created on 16. Januar 2022, 13:55
|
|
*/
|
|
|
|
#include "AStack.hpp"
|
|
#include <cassert>
|
|
|
|
using namespace std;
|
|
|
|
AStack::AStack(const std::string &dir, StackType type, const std::string &name)
|
|
: m_name(name)
|
|
, m_type(type)
|
|
, m_pLayers(nullptr)
|
|
, m_dir(dir)
|
|
{
|
|
}
|
|
|
|
AStack::AStack(const AStack& orig)
|
|
{
|
|
}
|
|
|
|
AStack::~AStack()
|
|
{
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
Layer *pNextLayer = pLayer->next;
|
|
delete pLayer;
|
|
pLayer = pNextLayer;
|
|
}
|
|
}
|
|
|
|
void AStack::setName(const std::string& name)
|
|
{
|
|
m_name = name;
|
|
}
|
|
|
|
size_t AStack::numLayers()
|
|
{
|
|
size_t count = 0;
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
count++;
|
|
pLayer = pLayer->next;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
void AStack::addLayer(Layer *pOtherLayer)
|
|
{
|
|
if (!m_pLayers)
|
|
{
|
|
m_pLayers = pOtherLayer;
|
|
pOtherLayer->prev = nullptr;
|
|
}
|
|
else
|
|
{
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer->next)
|
|
{
|
|
pLayer = pLayer->next;
|
|
}
|
|
pLayer->next = pOtherLayer;
|
|
pOtherLayer->prev = pLayer;
|
|
}
|
|
}
|
|
|
|
void AStack::delLayer(Layer* pLayer)
|
|
{
|
|
assert(!"Stack::delLayer: Not implemented!");
|
|
}
|
|
|
|
Layer* AStack::getLayer(size_t layerId) const
|
|
{
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
if (pLayer->id() == layerId)
|
|
{
|
|
return pLayer;
|
|
}
|
|
pLayer = pLayer->next;
|
|
}
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
bool AStack::load(LayerConstructor *pLayerConstructor)
|
|
{
|
|
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;
|
|
}
|
|
|
|
void AStack::weightsInit(double stddev)
|
|
{
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
pLayer->weightsInit(stddev);
|
|
pLayer = pLayer->next;
|
|
}
|
|
}
|
|
|
|
bool AStack::loadWeights()
|
|
{
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
if (!pLayer->weightsLoad(m_dir, m_name))
|
|
{
|
|
return false;
|
|
}
|
|
pLayer = pLayer->next;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool AStack::saveWeights()
|
|
{
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
if (!pLayer->weightsSave(m_dir, m_name))
|
|
{
|
|
return false;
|
|
}
|
|
pLayer = pLayer->next;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
arma::mat AStack::trainingBatchFrom(size_t layerId, const arma::mat& batch)
|
|
{
|
|
arma::mat thisBatch = batch;
|
|
Layer *pLayer = getLayer(0);
|
|
while (pLayer)
|
|
{
|
|
if (pLayer->id() == layerId)
|
|
{
|
|
break;
|
|
}
|
|
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
|
pLayer = pLayer->next;
|
|
}
|
|
return thisBatch;
|
|
}
|
|
|