- added JSON for project support
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@573 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -2,7 +2,7 @@ CONFIG ?= release
|
||||
SRCS := source/main.cpp source/Rbm.cpp source/noise.c
|
||||
|
||||
|
||||
LIBS := -larmadillo
|
||||
LIBS := -larmadillo -ljsoncpp
|
||||
BUILD_DIR := ./build/${CONFIG}
|
||||
|
||||
all: ${BUILD_DIR}/main.elf
|
||||
|
||||
+60
-5
@@ -1,10 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <streambuf>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <armadillo>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include "Rbm.hpp"
|
||||
|
||||
using namespace std;
|
||||
class RbmListener : public Rbm::IListener
|
||||
{
|
||||
public:
|
||||
@@ -25,7 +29,7 @@ class RbmListener : public Rbm::IListener
|
||||
}
|
||||
};
|
||||
|
||||
arma::mat loadTraining(const std::string &filename)
|
||||
arma::mat loadTraining(const string &filename)
|
||||
{
|
||||
uint32_t numTraining = 0;
|
||||
uint32_t numVisible = 0;
|
||||
@@ -69,7 +73,7 @@ arma::mat loadTraining(const std::string &filename)
|
||||
|
||||
}
|
||||
|
||||
void saveWeight(const std::string &filename, size_t numVisibleX, size_t numVisibleY, const arma::mat &w, const arma::mat &bv, const arma::mat &bh)
|
||||
void saveWeight(const string &filename, size_t numVisibleX, size_t numVisibleY, const arma::mat &w, const arma::mat &bv, const arma::mat &bh)
|
||||
{
|
||||
FILE *pFile;
|
||||
|
||||
@@ -107,14 +111,65 @@ void saveWeight(const std::string &filename, size_t numVisibleX, size_t numVisib
|
||||
fclose(pFile);
|
||||
}
|
||||
|
||||
void saveProject(const string &prjname)
|
||||
{
|
||||
ofstream ofs(prjname + string(".prj"));
|
||||
|
||||
Json::StyledWriter writer;
|
||||
Json::Value project;
|
||||
project["project"]["name"] = prjname;
|
||||
|
||||
Json::Value layer1;
|
||||
layer1["name"] = "1";
|
||||
layer1["num_hidden"] = 64;
|
||||
layer1["num_visible"] = 28*28;
|
||||
Json::Value params1;
|
||||
params1["weightInit"] = 0.01;
|
||||
params1["weightDecay"] = 0.001;
|
||||
params1["learningRate"] = 0.1;
|
||||
params1["momentum"] = 0.5;
|
||||
params1["doRaoBlackwell"] = 1;
|
||||
params1["gibbsDoSampleVisible"] = 0;
|
||||
params1["gibbsDoSampleHidden"] = 1;
|
||||
params1["doSampleBatch"] = 0;
|
||||
params1["numGibbs"] = 1;
|
||||
layer1["params"] = params1;
|
||||
|
||||
Json::Value layer2;
|
||||
layer2["name"] = "2";
|
||||
layer2["num_hidden"] = 16;
|
||||
layer2["num_visible"] = 64;
|
||||
Json::Value params2;
|
||||
params2["weightInit"] = 0.01;
|
||||
params2["weightDecay"] = 0.001;
|
||||
params2["learningRate"] = 0.1;
|
||||
params2["momentum"] = 0.5;
|
||||
params2["doRaoBlackwell"] = 1;
|
||||
params2["gibbsDoSampleVisible"] = 0;
|
||||
params2["gibbsDoSampleHidden"] = 1;
|
||||
params2["doSampleBatch"] = 0;
|
||||
params2["numGibbs"] = 1;
|
||||
layer2["params"] = params2;
|
||||
|
||||
Json::Value layers(Json::arrayValue);
|
||||
layers.append(layer1);
|
||||
layers.append(layer2);
|
||||
|
||||
project["project"]["layers"] = layers;
|
||||
|
||||
ofs << writer.write(project);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Hallo, Welt!\n");
|
||||
|
||||
const std::string project("mnist");
|
||||
const string project("mnist_2");
|
||||
saveProject(project);
|
||||
|
||||
RbmListener statusDisplay;
|
||||
Rbm::Params params;
|
||||
arma::mat batch = loadTraining(project + std::string(".training.dat"));
|
||||
arma::mat batch = loadTraining(project + string(".training.dat"));
|
||||
|
||||
size_t numTraining = batch.n_rows;
|
||||
size_t numVisible = batch.n_cols;
|
||||
@@ -127,7 +182,7 @@ int main()
|
||||
arma::mat bh = arma::zeros(1, numHidden);
|
||||
Rbm rbm(params, w, bv, bh);
|
||||
rbm.train(batch, 1000, 100, &statusDisplay);
|
||||
saveWeight(project + std::string(".weights.dat"), 28, 28, w, bv, bh);
|
||||
saveWeight(project + string(".weights.dat"), 28, 28, w, bv, bh);
|
||||
arma::mat v = arma::randu(numTraining, numVisible);
|
||||
arma::mat h = rbm.toHiddenProbs(v);
|
||||
arma::mat r = rbm.toVisibleProbs(h);
|
||||
|
||||
Reference in New Issue
Block a user