Files
Rbm/source/Rbm.hpp
T
jensandClaude Sonnet 5 eb29e33b81 Fix Bernoulli-only sample() applied to Gaussian visible/hidden units
Matutils::sample() always binary-thresholds (src > uniform(src)), but it
was the only sampler in the codebase and was called unconditionally on
h_probs/v_probs/miniBatch in several CD Gibbs-loop branches regardless
of doGaussianVisible/doGaussianHidden. Binary-thresholding a Gaussian
unit's continuous activation is meaningless -- it would corrupt any
Gaussian-visible/hidden RBM (image-domain experiments via the GUI or
TEST target); doesn't affect poet's plain BB-RBM path since both flags
are false there.

Add sample_gaussian() (mean + N(0,1) noise) alongside the existing
Bernoulli sample() in matutils.hpp, plus Rbm::sampleVisible/sampleHidden
helpers that dispatch to the right one per the RBM's configured type.
Replace every visible/hidden Gibbs-step sample() call in cd_jens (the
active path) and cd_hinton (compiled but currently unused, behind
USE_CD_HINTON) with the appropriate dispatch helper, and fix the same
issue in Rbm::train's doSampleBatch path.

Behavior is unchanged for any RBM with doGaussianVisible/doGaussianHidden
both false (confirmed: poet.elf f output identical before/after).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016K8Gu7Qejd11JbdiHZqYAs
2026-07-27 13:45:03 +02:00

179 lines
4.8 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 <streambuf>
#include <armadillo>
#include <jsoncpp/json/json.h>
class Rbm
{
public:
struct Params
{
Params()
: learningRate(0.1)
, weightDecay(0.0)
, momentum(0.9)
, doGaussianHidden(false)
, doGaussianVisible(false)
, doRaoBlackwell(true)
, gibbsDoSampleVisible(false)
, gibbsDoSampleHidden(true)
, doSampleBatch(false)
, numGibbs(1)
, miniBatchSize(100)
, numEpochs(1000)
{
}
Json::Value toJson() const
{
std::cout << "Exporting Rbm::Params" << std::endl;
Json::Value params;
params["weightDecay"] = weightDecay;
params["learningRate"] = learningRate;
params["momentum"] = momentum;
params["doGaussianVisible"] = (int)doGaussianVisible;
params["doGaussianHidden"] = (int)doGaussianHidden;
params["doRaoBlackwell"] = (int)doRaoBlackwell;
params["gibbsDoSampleVisible"] = (int)gibbsDoSampleVisible;
params["gibbsDoSampleHidden"] = (int)gibbsDoSampleHidden;
params["doSampleBatch"] = (int)doSampleBatch;
params["numGibbs"] = (int)numGibbs;
params["miniBatchSize"] = (int)miniBatchSize;
params["numEpochs"] = (int)numEpochs;
return params;
}
void fromJson(Json::Value params)
{
std::cout << "Importing Rbm::Params" << std::endl;
weightDecay = params.get("weightDecay", weightDecay).asDouble();
learningRate = params.get("learningRate", learningRate).asDouble();
momentum = params.get("momentum", momentum).asDouble();
doGaussianVisible = params.get("doGaussianVisible", doGaussianVisible) == 1;
doGaussianHidden = params.get("doGaussianHidden", doGaussianHidden) == 1;
doRaoBlackwell = params.get("doRaoBlackwell", doRaoBlackwell) == 1;
gibbsDoSampleVisible = params.get("gibbsDoSampleVisible", gibbsDoSampleVisible) == 1;
gibbsDoSampleHidden = params.get("gibbsDoSampleHidden", gibbsDoSampleHidden) == 1;
doSampleBatch = params.get("doSampleBatch", doSampleBatch) == 1;
numGibbs = params.get("numGibbs", numGibbs).asUInt();
miniBatchSize = params.get("miniBatchSize", miniBatchSize).asUInt();
numEpochs = params.get("numEpochs", numEpochs).asUInt();
}
double weightDecay;
double learningRate;
double momentum;
bool doGaussianVisible;
bool doGaussianHidden;
bool doRaoBlackwell;
bool gibbsDoSampleVisible;
bool gibbsDoSampleHidden;
bool doSampleBatch;
int numGibbs;
int miniBatchSize;
int numEpochs;
};
struct Status
{
Status()
: progress(0)
, err(-1.0)
, err_total(-1.0)
, L1(-1.0)
, L2(-1.0)
{
}
int progress;
double err;
double err_total;
double L1;
double L2;
};
class IListener
{
public:
IListener() {}
virtual ~IListener() {}
virtual bool onProgress(Rbm *pRbm, const Status &status)
{
return true;
}
};
Rbm(size_t numVisible, size_t numHidden);
Rbm(const Rbm& orig);
virtual ~Rbm();
Params& params();
void weightsInit(double stddev, double mu=0.0);
void weightsAssign(const arma::mat &w, const arma::mat &bhv, const arma::mat &bv);
void train(arma::mat const &batch, IListener *pListener=nullptr);
const arma::mat& whv() const;
const arma::mat& bv() const;
const arma::mat& bh() const;
Json::Value toJson() const;
void fromJson(Json::Value params);
arma::mat toHiddenProbs(const arma::mat &visible) const;
arma::mat toVisibleProbs(const arma::mat &hidden) const;
size_t numHidden() const;
size_t numVisible() const;
void gibbs_hv(arma::mat &h_probs, arma::mat &v_probs) const;
void gibbs_vh(arma::mat &v_probs, arma::mat &h_probs) const;
static double rms_error_accu(arma::mat diffErr);
static arma::mat rms_error(arma::mat diffErr);
protected:
Params m_params;
arma::mat m_bh;
arma::mat m_bv;
private:
arma::mat m_whv;
void cd_hinton(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
void cd_hinton_hid_linear(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
void cd_jens(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
void cd(arma::mat const &v_states, arma::mat &dwhv, arma::mat &dbhv, arma::mat &dbv);
arma::mat v_to_h(const arma::mat &visible) const;
arma::mat h_to_v(const arma::mat &hidden) const;
// Sample a visible/hidden unit's state from its probability/mean matrix,
// dispatching to the Bernoulli or Gaussian sampler per doGaussianVisible/
// doGaussianHidden -- CD's Gibbs steps must not binary-threshold a
// Gaussian unit's continuous activation.
arma::mat sampleVisible(const arma::mat &visible) const;
arma::mat sampleHidden(const arma::mat &hidden) const;
};
#endif /* RBM_HPP */