Skip to content

Instantly share code, notes, and snippets.

@kris-singh
Created June 4, 2017 19:52
Show Gist options
  • Save kris-singh/935d5d46b78935cfafb81133c68acee6 to your computer and use it in GitHub Desktop.
Save kris-singh/935d5d46b78935cfafb81133c68acee6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <mlpack/core.hpp>
#include <mlpack/prereqs.hpp>
#include <mlpack/core/dists/gaussian_distribution.hpp>
using namespace mlpack;
using namespace mlpack::distribution;
/*
* This class implements Gibbs
* Sampler. The sampler samples
* from the conditional distrbution
* P(y|x).
* ActivationFnc: Probability Distribution
*/
template<typename ActivationFnc, typename... Args>
class GibbsSampler
{
public:
/**
* Create the GibbsSampler object using the specified Probability Distribution.
*
* @param dist probability distribution
* @param args arguments for the distribution
*/
GibbsSampler(Args&... args):
distribution(new ActivationFnc(args...))
{/*Nothing to do here*/}
/**
* Genereate the samples from the distribution
* store the sampled value in output variable
*/
template<typename eT>
void Sampler(arma::Mat<eT>& output)
{
output = distribution.Random();
}
private:
/* Distribution class*/
ActivationFnc distribution;
};
int main()
{
// combine the incoming mean and variance in some way
arma::mat visible = {1,0};
arma::mat hidden = {1,0};
arma::vec mean = visible;
arma::vec variance = arma::ones(2);
GibbsSampler<GaussianDistribution, arma::vec, arma::vec> gibbs(mean, variance);
// suppose we want to sample hidden layer given the visible layer
gibbs.Sampler(hidden);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment