- load training, save weights
- provide params at construction time git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@566 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+93
-33
@@ -23,45 +23,105 @@ class RbmListener : public Rbm::IListener
|
||||
}
|
||||
};
|
||||
|
||||
arma::mat loadTraining(const char *pFilename)
|
||||
{
|
||||
uint32_t numTraining = 0;
|
||||
uint32_t numVisible = 0;
|
||||
FILE *pFile;
|
||||
|
||||
pFile = fopen(pFilename,"r");
|
||||
|
||||
if (!pFile)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int result = fscanf(pFile, "%d\n", &numTraining);
|
||||
if (result < 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
result = fscanf(pFile, "%d\n", &numVisible);
|
||||
if (result < 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
arma::mat data = arma::zeros(numTraining, numVisible);
|
||||
|
||||
uint32_t i, j;
|
||||
for (i=0; i < numTraining; i++)
|
||||
{
|
||||
for (j=0; j < numVisible; j++)
|
||||
{
|
||||
float v;
|
||||
int result = fscanf(pFile, "%f", &v);
|
||||
if (result > 0)
|
||||
{
|
||||
data(i, j) = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(pFile);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
void saveWeight(const char *pFilename, size_t numVisibleX, size_t numVisibleY, const arma::mat &w, const arma::mat &bv, const arma::mat &bh)
|
||||
{
|
||||
FILE *pFile;
|
||||
|
||||
pFile = fopen(pFilename,"w");
|
||||
|
||||
if (!pFile)
|
||||
return;
|
||||
|
||||
size_t numHidden = bh.n_elem;
|
||||
size_t numVisible = bv.n_elem;
|
||||
fprintf(pFile, "%d %d %d\n", (int)numVisibleX, (int)numVisibleY, (int)numHidden);
|
||||
|
||||
uint32_t i, j;
|
||||
|
||||
for (i=0; i < numVisible; i++)
|
||||
{
|
||||
fprintf(pFile, "%3.6f\n", bv(i));
|
||||
}
|
||||
for (i=0; i < numHidden; i++)
|
||||
{
|
||||
fprintf(pFile, "%3.6f\n", bh(i));
|
||||
}
|
||||
for (i=0; i < numVisible; i++)
|
||||
{
|
||||
for (j=0; j < numHidden; j++)
|
||||
{
|
||||
fprintf(pFile, "%3.6f ", w(i,j));
|
||||
}
|
||||
fprintf(pFile, "\n");
|
||||
}
|
||||
|
||||
fclose(pFile);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Hallo, Welt!\n");
|
||||
|
||||
// Position of a particle // |
|
||||
arma::vec Pos = {{0}, // | (0,1)
|
||||
{1}}; // +---x-->
|
||||
|
||||
// Rotation matrix
|
||||
double phi = -3.1416/2;
|
||||
arma::mat RotM = {{+cos(phi), -sin(phi)},
|
||||
{+sin(phi), +cos(phi)}};
|
||||
|
||||
Pos.print("Current position of the particle:");
|
||||
std::cout << "Rotating the point " << phi*180/3.1416 << " deg" << std::endl;
|
||||
|
||||
Pos = RotM*Pos;
|
||||
|
||||
Pos.print("New position of the particle:"); // ^
|
||||
// x (1,0)
|
||||
// |
|
||||
arma::mat Z = arma::eye<arma::mat>(4000,4000);
|
||||
// Z.print("Z:");
|
||||
printf("Z.n_rows = %d\n", (int)Z.n_rows);
|
||||
printf("Z.n_cols = %d\n", (int)Z.n_cols);
|
||||
printf("Z.n_elem = %d\n", (int)Z.n_elem);
|
||||
// +------>
|
||||
|
||||
|
||||
RbmListener statusDisplay;
|
||||
Rbm::Params params;
|
||||
arma::mat w = arma::zeros(28*28, 64);
|
||||
arma::mat bv = arma::zeros(1, 28*28);
|
||||
arma::mat bh = arma::zeros(1, 64);
|
||||
Rbm rbm(w, bv, bh);
|
||||
arma::mat batch = arma::randu(100, 28*28);
|
||||
rbm.train(batch, 10, 10, params, &statusDisplay);
|
||||
|
||||
arma::mat v = arma::randu(100, 28*28);
|
||||
arma::mat batch = loadTraining("mnist_2.training.dat");
|
||||
|
||||
size_t numTraining = batch.n_rows;
|
||||
size_t numVisible = batch.n_cols;
|
||||
size_t numHidden = 64;
|
||||
|
||||
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.train(batch, 100, 100, &statusDisplay);
|
||||
saveWeight("mnist_2.weights.dat", 28, 28, w, bv, bh);
|
||||
arma::mat v = arma::randu(numTraining, numVisible);
|
||||
arma::mat h = rbm.toHidden(v);
|
||||
arma::mat r = rbm.toVisible(h);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user