Files
Rbm/source/DeepStack.cpp
T
jens 938368f1fa - refactored
- constify
2024-01-22 12:26:03 +01:00

96 lines
1.9 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: DeepStack.cpp
* Author: jens
*
* Created on 25. Oktober 2019, 18:26
*/
#include <cassert>
#include "DeepStack.hpp"
using namespace std;
DeepStack::DeepStack(const std::string &name)
: AStack(StackType::Deep, name)
{
}
DeepStack::~DeepStack()
{
}
arma::mat DeepStack::trainingBatchFrom(Layer *pLayer, const arma::mat& batch)
{
arma::mat thisBatch = batch;
Layer *pLayerForward = getFirstLayer();
while (pLayerForward)
{
if (pLayerForward == pLayer)
{
break;
}
thisBatch = pLayerForward->toHiddenProbs(thisBatch);
pLayerForward = pLayerForward->next;
}
return thisBatch;
}
void DeepStack::train(const arma::mat& batch, Rbm::IListener* pListener)
{
arma::mat thisBatch = batch;
Layer *pLayer = getFirstLayer();
while (pLayer)
{
thisBatch = trainingBatchFrom(pLayer, thisBatch);
pLayer->train(thisBatch, pListener);
pLayer = pLayer->next;
}
}
void DeepStack::train(Layer *pLayer, const arma::mat& batch, Rbm::IListener* pListener)
{
arma::mat thisBatch = trainingBatchFrom(pLayer, batch);
pLayer->train(thisBatch, pListener);
}
arma::mat DeepStack::upPass(Layer *pLayer, arma::mat const &v)
{
arma::mat h = v;
while(pLayer)
{
if (pLayer->isEnable())
{
h = pLayer->to_h_gibbs(h);
}
pLayer = pLayer->next;
}
return h;
}
arma::mat DeepStack::downPass(Layer *pLayer, arma::mat const &h)
{
arma::mat v = h;
while(pLayer)
{
if (pLayer->isEnable())
{
v = pLayer->to_v_gibbs(v);
}
pLayer = pLayer->prev;
}
return v;
}
arma::mat DeepStack::upDownPass(Layer *pLayer, const arma::mat& v)
{
arma::mat h = upPass(pLayer, v);
arma::mat r = downPass(getLastLayer(), h);
return r;
}