Files
Rbm/source/AStack.cpp
T
jens e55f8e2153 - added StackCreator
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@823 b431acfa-c32f-4a4a-93f1-934dc6c82436
2022-01-17 18:40:50 +00:00

197 lines
3.3 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 "StackCreator.hpp"
#include <cassert>
using namespace std;
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)
{
}
AStack::AStack(const AStack& orig)
{
}
AStack::~AStack()
{
Layer *pLayer = m_pLayers;
while(pLayer)
{
Layer *pNextLayer = pLayer->next;
delete pLayer;
pLayer = pNextLayer;
}
}
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;
}
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::save(const std::string &dir)
{
return StackCreator::toFile(this, dir, m_name);
}
void AStack::weightsInit(double stddev)
{
Layer *pLayer = m_pLayers;
while(pLayer)
{
pLayer->weightsInit(stddev);
pLayer = pLayer->next;
}
}
bool AStack::loadWeights(const std::string &dir)
{
Layer *pLayer = m_pLayers;
while(pLayer)
{
if (!pLayer->weightsLoad(dir, m_name))
{
return false;
}
pLayer = pLayer->next;
}
return true;
}
bool AStack::saveWeights(const std::string &dir)
{
Layer *pLayer = m_pLayers;
while(pLayer)
{
if (!pLayer->weightsSave(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;
}