/* * 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 #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) { arma::mat h = arma::zeros(0,0); arma::mat tv = v; Layer *pLayer = getLayer(layerId); while(pLayer) { if (pLayer->isEnable()) { h = pLayer->to_h_gibbs(tv); tv = h; } pLayer = pLayer->next; } return h; } arma::mat DeepStack::downPass(size_t layerId, const arma::mat& h) { arma::mat v = arma::zeros(0,0); arma::mat th = h; Layer *pLayer = getLayer(layerId); while(pLayer) { if (pLayer->isEnable()) { v = pLayer->to_v_gibbs(th); th = v; } pLayer = pLayer->prev; } return v; } 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; }