- moved method of layer interaction from Layer to Stack
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@817 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
* 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::upPass(size_t layerId, const arma::mat& v)
|
||||
{
|
||||
arma::mat h = arma::zeros(0,0);
|
||||
arma::mat tv = v;
|
||||
Layer *pLayer = getLayer(layerId);
|
||||
while(pLayer)
|
||||
{
|
||||
if (pLayer->isEnable())
|
||||
{
|
||||
h = pLayer->to_h_gibbs(tv);
|
||||
tv = h;
|
||||
}
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
arma::mat AStack::downPass(size_t layerId, const arma::mat& h)
|
||||
{
|
||||
arma::mat v = arma::zeros(0,0);
|
||||
arma::mat th = h;
|
||||
Layer *pLayer = getLayer(layerId);
|
||||
while(pLayer)
|
||||
{
|
||||
if (pLayer->isEnable())
|
||||
{
|
||||
v = pLayer->to_v_gibbs(th);
|
||||
th = v;
|
||||
}
|
||||
pLayer = pLayer->prev;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
arma::mat AStack::upDownPass(size_t layerId, const arma::mat& v)
|
||||
{
|
||||
arma::mat h = upPass(layerId, v);
|
||||
arma::mat r = downPass(numLayers()-1, h);
|
||||
return r;
|
||||
}
|
||||
Reference in New Issue
Block a user