diff --git a/Makefile b/Makefile index dcfa362..e2cf011 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/source/main.cpp b/source/main.cpp index a4fe449..1f20892 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,10 +1,14 @@ +#include +#include #include #include #include #include #include +#include #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);