[Rbm]
- added JSON - refactored git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@574 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+52
-20
@@ -14,16 +14,13 @@
|
||||
#include "Rbm.hpp"
|
||||
#include "noise.h"
|
||||
|
||||
Rbm::Rbm(const Params& params, arma::mat &w, arma::mat &bv, arma::mat &bh)
|
||||
Rbm::Rbm(const Params& params, size_t numVisible, size_t numHidden)
|
||||
: m_params(params)
|
||||
, m_w(w)
|
||||
, m_bv(bv)
|
||||
, m_bh(bh)
|
||||
, m_w(numVisible, numHidden)
|
||||
, m_bv(1, numVisible)
|
||||
, m_bh(1, numHidden)
|
||||
{
|
||||
Noise_Init(&m_noise, 0x32727155);
|
||||
uniform(m_w, 0.0, m_params.weightInit);
|
||||
uniform(m_bh, 0.0, m_params.weightInit);
|
||||
uniform(m_bv, 0.0, m_params.weightInit);
|
||||
weightsInit(0.0, m_params.weightInit);
|
||||
}
|
||||
|
||||
Rbm::Rbm(const Rbm& orig)
|
||||
@@ -38,6 +35,28 @@ Rbm::~Rbm()
|
||||
{
|
||||
}
|
||||
|
||||
void Rbm::weightsInit(double mu, double stddev)
|
||||
{
|
||||
uniform(m_w, mu, stddev);
|
||||
uniform(m_bh, mu, stddev);
|
||||
uniform(m_bv, mu, stddev);
|
||||
|
||||
}
|
||||
|
||||
void Rbm::fromJson(Json::Value params)
|
||||
{
|
||||
}
|
||||
|
||||
Json::Value Rbm::toJson() const
|
||||
{
|
||||
Json::Value rbm;
|
||||
rbm["numVisible"] = m_bv.n_elem;
|
||||
rbm["numHidden"] = m_bh.n_elem;
|
||||
rbm["params"] = m_params.toJson();
|
||||
return rbm;
|
||||
}
|
||||
|
||||
|
||||
void Rbm::train(const arma::mat& batch, size_t miniBatchSize, size_t numEpochs, IListener* pListener)
|
||||
{
|
||||
size_t epoch;
|
||||
@@ -188,43 +207,56 @@ arma::mat Rbm::probsLogistic(const arma::mat &src)
|
||||
arma::mat Rbm::sample(const arma::mat &src)
|
||||
{
|
||||
arma::mat dst = src;
|
||||
uniform(dst);
|
||||
|
||||
for (size_t i=0; i < src.n_rows; i++)
|
||||
{
|
||||
for (size_t j=0; j < src.n_cols; j++)
|
||||
{
|
||||
dst(i, j) = src(i, j) >= Noise_Uniform(&m_noise);
|
||||
dst(i, j) = src(i, j) >= dst(i, j);
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
arma::mat Rbm::toHiddenState(const arma::mat &visible)
|
||||
arma::mat Rbm::toHiddenState(const arma::mat &visible) const
|
||||
{
|
||||
return visible * m_w + arma::repmat(m_bh, visible.n_rows, 1);
|
||||
}
|
||||
|
||||
arma::mat Rbm::toVisibleState(const arma::mat &hidden)
|
||||
arma::mat Rbm::toVisibleState(const arma::mat &hidden) const
|
||||
{
|
||||
return hidden * m_w.t() + arma::repmat(m_bv, hidden.n_rows, 1);
|
||||
}
|
||||
|
||||
arma::mat Rbm::toHiddenProbs(const arma::mat &visible)
|
||||
arma::mat Rbm::toHiddenProbs(const arma::mat &visible) const
|
||||
{
|
||||
return probsLogistic(toHiddenState(visible));
|
||||
}
|
||||
|
||||
arma::mat Rbm::toVisibleProbs(const arma::mat &hidden)
|
||||
arma::mat Rbm::toVisibleProbs(const arma::mat &hidden) const
|
||||
{
|
||||
return probsLogistic(toVisibleState(hidden));
|
||||
}
|
||||
|
||||
void Rbm::uniform(arma::mat& srcDst, double mu, double stdDev)
|
||||
{
|
||||
for (size_t i=0; i < srcDst.n_rows; i++)
|
||||
{
|
||||
for (size_t j=0; j < srcDst.n_cols; j++)
|
||||
{
|
||||
srcDst(i, j) = stdDev*Noise_Uniform(&m_noise) + mu;
|
||||
}
|
||||
}
|
||||
srcDst = stdDev*arma::randu(srcDst.n_rows, srcDst.n_cols) + mu;
|
||||
}
|
||||
|
||||
const arma::mat& Rbm::w() const
|
||||
{
|
||||
return m_w;
|
||||
}
|
||||
|
||||
const arma::mat& Rbm::bv() const
|
||||
{
|
||||
return m_bv;
|
||||
}
|
||||
|
||||
const arma::mat& Rbm::bh() const
|
||||
{
|
||||
return m_bh;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user