git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@855 b431acfa-c32f-4a4a-93f1-934dc6c82436
199 lines
3.4 KiB
C++
199 lines
3.4 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>
|
|
|
|
#include "matutils.hpp"
|
|
|
|
using namespace std;
|
|
using namespace Matutils;
|
|
|
|
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()
|
|
{
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
Layer *pNextLayer = pLayer->next;
|
|
delete pLayer;
|
|
pLayer = pNextLayer;
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
bool result = true;
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
bool success = pLayer->weightsLoad(dir, m_name);
|
|
if (!success)
|
|
{
|
|
pLayer->weightsInit(0.01, 0);
|
|
}
|
|
result &= success;
|
|
pLayer = pLayer->next;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool AStack::saveWeights(const std::string &dir)
|
|
{
|
|
bool result = true;
|
|
Layer *pLayer = m_pLayers;
|
|
while(pLayer)
|
|
{
|
|
bool success = pLayer->weightsSave(dir, m_name);
|
|
result &= success;
|
|
pLayer = pLayer->next;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
arma::mat& AStack::trainingBatch()
|
|
{
|
|
return m_trainingBatch;
|
|
}
|
|
|
|
size_t AStack::loadTrainingBatch(const std::string &dir, bool doNormalize)
|
|
{
|
|
std::string filename = dir + "/" + m_name + ".training.dat";
|
|
std::string path = dir + "/" + m_name + ".training.dat";
|
|
bool success = m_trainingBatch.load(filename, arma::arma_ascii);
|
|
|
|
if (success)
|
|
{
|
|
if (doNormalize)
|
|
{
|
|
m_trainingBatch = normalize(m_trainingBatch);
|
|
}
|
|
std::cout << "Loaded " << m_trainingBatch.n_rows << " training samples\n";
|
|
}
|
|
|
|
return m_trainingBatch.n_rows;
|
|
}
|
|
|
|
size_t AStack::saveTrainingBatch(const std::string &dir)
|
|
{
|
|
std::string filename = dir + "/" + m_name + ".training.dat";
|
|
bool success = m_trainingBatch.save(filename, arma::arma_ascii);
|
|
|
|
if (success)
|
|
{
|
|
std::cout << "Saved " << m_trainingBatch.n_rows << " training samples\n";
|
|
}
|
|
|
|
return m_trainingBatch.n_rows;
|
|
}
|
|
|
|
size_t AStack::numTraining()
|
|
{
|
|
return m_trainingBatch.n_rows;
|
|
}
|
|
|
|
void AStack::addTraining(const arma::mat &toAdd)
|
|
{
|
|
m_trainingBatch.insert_rows(m_trainingBatch.n_rows, toAdd);
|
|
}
|
|
|
|
void AStack::delTraining(int index)
|
|
{
|
|
m_trainingBatch.shed_row(index);
|
|
}
|