/* * 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 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() { 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) { 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; } 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 = Rbm::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); }