- RBM: added Gibbs sampler - Stack adjust training column vector according to needs git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@797 b431acfa-c32f-4a4a-93f1-934dc6c82436
89 lines
1.7 KiB
C++
89 lines
1.7 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <streambuf>
|
|
#include <cstdio>
|
|
#include <cmath>
|
|
#include <string>
|
|
#include <armadillo>
|
|
#include <jsoncpp/json/json.h>
|
|
#include "Rbm.hpp"
|
|
#include "Layer.hpp"
|
|
#include "Stack.hpp"
|
|
|
|
using namespace std;
|
|
class RbmListener : public Rbm::IListener
|
|
{
|
|
public:
|
|
RbmListener() {}
|
|
virtual ~RbmListener() {}
|
|
|
|
bool onProgress(Rbm *pRbm, const Rbm::Status &status)
|
|
{
|
|
std::cout << "Progress : " << status.progress << " %" << std::endl;
|
|
std::cout << "error (per mini batch) = " << status.err << std::endl;
|
|
std::cout << "error (total) = " << status.err_total << std::endl;
|
|
std::cout << "L1 = " << status.L1 << std::endl;
|
|
std::cout << "L2 = " << status.L2 << std::endl;
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
arma::mat createTraining(const string &filename)
|
|
{
|
|
FILE *pFile;
|
|
|
|
pFile = fopen(filename.c_str(), "r");
|
|
|
|
if (!pFile)
|
|
{
|
|
std::cout << "Could not open " << filename << "!" << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
int numTraining = 0;
|
|
int numVisible = 26 + 10;
|
|
|
|
arma::mat batch = arma::zeros(numTraining, numVisible);
|
|
while(!feof(pFile))
|
|
{
|
|
char c;
|
|
int result = fread(&c, 1, 1, pFile);
|
|
if (result < 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
c = toupper(c);
|
|
|
|
int index;
|
|
if (isalpha(c))
|
|
{
|
|
index = (int)(c-'A');
|
|
}
|
|
if (isdigit(c))
|
|
{
|
|
index = (int)(c-'0');
|
|
}
|
|
arma::mat data = arma::zeros(1, numVisible);
|
|
data[index] = 1;
|
|
|
|
batch.insert_rows(batch.n_rows, data);
|
|
}
|
|
fclose(pFile);
|
|
|
|
return batch;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
const string project("poet");
|
|
|
|
Stack stack(".", project);
|
|
|
|
arma::mat batch = createTraining("moby_ch1.txt");
|
|
batch.save("poet.training.dat", arma::arma_ascii);
|
|
|
|
return 0;
|
|
}
|