- refactored
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@815 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* 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: DeepStack.cpp
|
||||
* Author: jens
|
||||
*
|
||||
* Created on 25. Oktober 2019, 18:26
|
||||
*/
|
||||
#include <cassert>
|
||||
#include "DeepStack.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
DeepStack::DeepStack(const std::string &dir, const std::string &name, StackType type)
|
||||
: AStack(dir, type, name)
|
||||
{
|
||||
}
|
||||
|
||||
DeepStack::DeepStack(const DeepStack& orig)
|
||||
: AStack(orig)
|
||||
{
|
||||
}
|
||||
|
||||
DeepStack::~DeepStack()
|
||||
{
|
||||
}
|
||||
|
||||
void DeepStack::weightsInit(double stddev)
|
||||
{
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
pLayer->weightsInit(stddev);
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
}
|
||||
|
||||
bool DeepStack::loadWeights()
|
||||
{
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
if (!pLayer->weightsLoad(m_dir, m_name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeepStack::saveWeights()
|
||||
{
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
if (!pLayer->weightsSave(m_dir, m_name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DeepStack::train(Rbm::IListener* pListener)
|
||||
{
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
std::cout << m_name << ": " << " Training of layer " << std::to_string(pLayer->id()) << std::endl;
|
||||
pLayer->calcContextBatch(m_trainingBatch);
|
||||
pLayer->train(m_trainingBatch, pListener);
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
}
|
||||
|
||||
arma::mat& DeepStack::trainingBatch()
|
||||
{
|
||||
return m_trainingBatch;
|
||||
}
|
||||
|
||||
arma::mat DeepStack::trainingBatch(Layer* pThatLayer)
|
||||
{
|
||||
arma::mat thisBatch = m_trainingBatch;
|
||||
Layer *pLayer = m_pLayers;
|
||||
while (pLayer)
|
||||
{
|
||||
if (pLayer->id() == pThatLayer->id())
|
||||
{
|
||||
break;
|
||||
}
|
||||
thisBatch = pLayer->toHiddenProbs(thisBatch);
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
return thisBatch;
|
||||
}
|
||||
|
||||
size_t DeepStack::loadTrainingBatch(bool doNormalize)
|
||||
{
|
||||
std::string filename = m_dir + "/" + m_name + ".training.dat";
|
||||
std::string path = m_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";
|
||||
}
|
||||
|
||||
// Migrate context part to training data
|
||||
size_t numTraining = m_trainingBatch.n_rows;
|
||||
size_t numVisible = getLayer(0)->numVisible();
|
||||
|
||||
if (m_trainingBatch.n_cols < numVisible)
|
||||
{
|
||||
size_t diff = numVisible - m_trainingBatch.n_cols;
|
||||
arma::mat training_with_ctx = arma::join_rows(m_trainingBatch, arma::zeros(numTraining, diff));
|
||||
m_trainingBatch = training_with_ctx;
|
||||
}
|
||||
else if (m_trainingBatch.n_cols > numVisible)
|
||||
{
|
||||
m_trainingBatch = m_trainingBatch.submat(0, 0, numTraining-1, numVisible-1);
|
||||
}
|
||||
|
||||
return m_trainingBatch.n_rows;
|
||||
}
|
||||
|
||||
size_t DeepStack::saveTrainingBatch()
|
||||
{
|
||||
std::string filename = m_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 DeepStack::numTraining()
|
||||
{
|
||||
return m_trainingBatch.n_rows;
|
||||
}
|
||||
|
||||
void DeepStack::addTraining(const arma::mat &toAdd)
|
||||
{
|
||||
m_trainingBatch.insert_rows(m_trainingBatch.n_rows, toAdd);
|
||||
}
|
||||
|
||||
void DeepStack::delTraining(int index)
|
||||
{
|
||||
m_trainingBatch.shed_row(index);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user