Files
Rbm/source/Rbm.hpp
T
2019-10-24 18:51:45 +00:00

100 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: Rbm.hpp
* Author: jens
*
* Created on 21. Oktober 2019, 21:28
*/
#ifndef RBM_HPP
#define RBM_HPP
#include <armadillo>
#include "noise.h"
class Rbm
{
public:
struct Params
{
Params()
: weightInit(0.01)
, weightDecay(0.001)
, learningRate(0.1)
, momentum(0.5)
, doRaoBlackwell(true)
, gibbsDoSampleVisible(false)
, gibbsDoSampleHidden(true)
, doSampleBatch(false)
, numGibbs(1)
{
}
double weightInit;
double weightDecay;
double learningRate;
double momentum;
bool doRaoBlackwell;
bool gibbsDoSampleVisible;
bool gibbsDoSampleHidden;
bool doSampleBatch;
size_t numGibbs;
};
struct Status
{
size_t epoch;
size_t trainingSizeRemain;
double progress;
double err;
double err_total;
double L1;
double L2;
};
class IListener
{
public:
IListener() {}
virtual ~IListener() {}
virtual bool onProgress(const Status &status)
{
return true;
}
};
Rbm(const Params& params, arma::mat &w, arma::mat &bv, arma::mat &bh);
Rbm(const Rbm& orig);
virtual ~Rbm();
void train(arma::mat const &batch, size_t numEpochs, size_t sizeMiniBatch, IListener *pListener);
arma::mat toHiddenState(const arma::mat &visible);
arma::mat toVisibleState(const arma::mat &hidden);
arma::mat toHiddenProbs(const arma::mat &visible);
arma::mat toVisibleProbs(const arma::mat &hidden);
arma::mat& weights();
arma::mat& bias_visible();
arma::mat& bias_hidden();
private:
noise_gen_t m_noise;
const Params &m_params;
arma::mat &m_w;
arma::mat &m_bh;
arma::mat &m_bv;
arma::mat sample(arma::mat const &src);
static arma::mat probsLogistic(arma::mat const &src);
void uniform(arma::mat &srcDst, double mu=0.0, double stdDev=1.0);
};
#endif /* RBM_HPP */