[Rbm]
- added JSON - refactored git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@574 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+18
-42
@@ -7,6 +7,7 @@
|
||||
#include <armadillo>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include "Rbm.hpp"
|
||||
#include "Layer.hpp"
|
||||
|
||||
using namespace std;
|
||||
class RbmListener : public Rbm::IListener
|
||||
@@ -111,51 +112,22 @@ void saveWeight(const string &filename, size_t numVisibleX, size_t numVisibleY,
|
||||
fclose(pFile);
|
||||
}
|
||||
|
||||
void saveProject(const string &prjname)
|
||||
void saveProject(const string &prjname, const Layer &layer, size_t numTraining)
|
||||
{
|
||||
ofstream ofs(prjname + string(".prj"));
|
||||
|
||||
Json::StyledWriter writer;
|
||||
Json::Value project;
|
||||
project["project"]["name"] = prjname;
|
||||
project["project"]["num_training"] = (int)numTraining;
|
||||
project["project"]["training_file"] = prjname + string(".training.dat");
|
||||
|
||||
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 jsonLayer = layer.toJson();
|
||||
|
||||
Json::Value jsonLayers(Json::arrayValue);
|
||||
jsonLayers.append(jsonLayer);
|
||||
|
||||
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;
|
||||
project["project"]["layers"] = jsonLayers;
|
||||
|
||||
ofs << writer.write(project);
|
||||
}
|
||||
@@ -164,25 +136,29 @@ int main()
|
||||
{
|
||||
printf("Hallo, Welt!\n");
|
||||
|
||||
const string project("mnist_2");
|
||||
saveProject(project);
|
||||
const string project("mnist");
|
||||
|
||||
RbmListener statusDisplay;
|
||||
Rbm::Params params;
|
||||
Rbm::Params rbmParams;
|
||||
|
||||
arma::mat batch = loadTraining(project + string(".training.dat"));
|
||||
|
||||
size_t numTraining = batch.n_rows;
|
||||
size_t numVisible = batch.n_cols;
|
||||
size_t numHidden = 64;
|
||||
|
||||
Layer layer(project, 0, numVisible, numHidden);
|
||||
|
||||
saveProject(project, layer, numTraining);
|
||||
|
||||
printf("Loaded %d training samples\n", (int)numTraining);
|
||||
|
||||
arma::mat w = arma::zeros(numVisible, numHidden);
|
||||
arma::mat bv = arma::zeros(1, numVisible);
|
||||
arma::mat bh = arma::zeros(1, numHidden);
|
||||
Rbm rbm(params, w, bv, bh);
|
||||
Rbm rbm(rbmParams, numVisible, numHidden);
|
||||
rbm.train(batch, 1000, 100, &statusDisplay);
|
||||
saveWeight(project + string(".weights.dat"), 28, 28, w, bv, bh);
|
||||
saveWeight(project + string(".weights.dat"), 28, 28, rbm.w(), rbm.bv(), rbm.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