- use std::string for filenames

git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@572 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2019-10-24 19:10:44 +00:00
parent 0e544c1afc
commit 8f7e17e03c
+12 -6
View File
@@ -1,6 +1,7 @@
#include <streambuf> #include <streambuf>
#include <cstdio> #include <cstdio>
#include <cmath> #include <cmath>
#include <string>
#include <armadillo> #include <armadillo>
#include "Rbm.hpp" #include "Rbm.hpp"
@@ -24,16 +25,17 @@ class RbmListener : public Rbm::IListener
} }
}; };
arma::mat loadTraining(const char *pFilename) arma::mat loadTraining(const std::string &filename)
{ {
uint32_t numTraining = 0; uint32_t numTraining = 0;
uint32_t numVisible = 0; uint32_t numVisible = 0;
FILE *pFile; FILE *pFile;
pFile = fopen(pFilename,"r"); pFile = fopen(filename.c_str(), "r");
if (!pFile) if (!pFile)
{ {
std::cout << "Could not open " << filename << "!" << std::endl;
return 0; return 0;
} }
@@ -67,14 +69,17 @@ arma::mat loadTraining(const char *pFilename)
} }
void saveWeight(const char *pFilename, size_t numVisibleX, size_t numVisibleY, const arma::mat &w, const arma::mat &bv, const arma::mat &bh) void saveWeight(const std::string &filename, size_t numVisibleX, size_t numVisibleY, const arma::mat &w, const arma::mat &bv, const arma::mat &bh)
{ {
FILE *pFile; FILE *pFile;
pFile = fopen(pFilename,"w"); pFile = fopen(filename.c_str(), "w");
if (!pFile) if (!pFile)
{
std::cout << "Could not open " << filename << "!" << std::endl;
return; return;
}
size_t numHidden = bh.n_elem; size_t numHidden = bh.n_elem;
size_t numVisible = bv.n_elem; size_t numVisible = bv.n_elem;
@@ -106,9 +111,10 @@ int main()
{ {
printf("Hallo, Welt!\n"); printf("Hallo, Welt!\n");
const std::string project("mnist");
RbmListener statusDisplay; RbmListener statusDisplay;
Rbm::Params params; Rbm::Params params;
arma::mat batch = loadTraining("mnist_2.training.dat"); arma::mat batch = loadTraining(project + std::string(".training.dat"));
size_t numTraining = batch.n_rows; size_t numTraining = batch.n_rows;
size_t numVisible = batch.n_cols; size_t numVisible = batch.n_cols;
@@ -121,7 +127,7 @@ int main()
arma::mat bh = arma::zeros(1, numHidden); arma::mat bh = arma::zeros(1, numHidden);
Rbm rbm(params, w, bv, bh); Rbm rbm(params, w, bv, bh);
rbm.train(batch, 1000, 100, &statusDisplay); rbm.train(batch, 1000, 100, &statusDisplay);
saveWeight("mnist_2.weights.dat", 28, 28, w, bv, bh); saveWeight(project + std::string(".weights.dat"), 28, 28, w, bv, bh);
arma::mat v = arma::randu(numTraining, numVisible); arma::mat v = arma::randu(numTraining, numVisible);
arma::mat h = rbm.toHiddenProbs(v); arma::mat h = rbm.toHiddenProbs(v);
arma::mat r = rbm.toVisibleProbs(h); arma::mat r = rbm.toVisibleProbs(h);