Files
Rbm/source/DeepStack.cpp
T
2024-01-21 14:09:30 +01:00

119 lines
2.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: 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(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;
}
void DeepStack::train(const arma::mat& batch, Rbm::IListener* pListener)
{
arma::mat thisBatch = batch;
Layer *pLayer = getLayer(0);
while (pLayer)
{
thisBatch = trainingBatchFrom(pLayer->id(), thisBatch);
pLayer->train(thisBatch, pListener);
pLayer = pLayer->next;
}
}
void DeepStack::train(size_t layerId, const arma::mat& batch, Rbm::IListener* pListener)
{
arma::mat thisBatch = batch;
Layer *pLayer = getLayer(0);
while (pLayer)
{
if (pLayer->id() == layerId)
{
break;
}
thisBatch = pLayer->toHiddenProbs(thisBatch);
pLayer = pLayer->next;
}
pLayer->train(thisBatch, pListener);
}
arma::mat DeepStack::upPass(size_t layerId, const arma::mat& v)
{
return upPass(getLayer(layerId), v);
}
arma::mat DeepStack::downPass(size_t layerId, const arma::mat& h)
{
return downPass(getLayer(layerId), h);
}
arma::mat DeepStack::upDownPass(size_t layerId, const arma::mat& v)
{
arma::mat h = upPass(layerId, v);
arma::mat r = downPass(numLayers()-1, h);
return r;
}
arma::mat DeepStack::upPass(Layer *pLayer, arma::mat const &v)
{
arma::mat h = arma::zeros(0,0);
arma::mat tv = v;
while(pLayer)
{
if (pLayer->isEnable())
{
h = pLayer->to_h_gibbs(tv);
tv = h;
}
pLayer = pLayer->next;
}
return h;
}
arma::mat DeepStack::downPass(Layer *pLayer, arma::mat const &h)
{
arma::mat v = arma::zeros(0,0);
arma::mat th = h;
while(pLayer)
{
if (pLayer->isEnable())
{
v = pLayer->to_v_gibbs(th);
th = v;
}
pLayer = pLayer->prev;
}
return v;
}