- moved all functions to Layer
- added Stack git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@577 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: ILayer.hpp
|
||||
* Author: jens
|
||||
*
|
||||
* Created on 25. Oktober 2019, 18:33
|
||||
*/
|
||||
|
||||
#ifndef ILAYER_HPP
|
||||
#define ILAYER_HPP
|
||||
|
||||
#include <armadillo>
|
||||
|
||||
class ILayer
|
||||
{
|
||||
public:
|
||||
ILayer *upper;
|
||||
ILayer *lower;
|
||||
|
||||
ILayer()
|
||||
: upper(nullptr)
|
||||
, lower(nullptr)
|
||||
{}
|
||||
|
||||
virtual ~ILayer()
|
||||
{}
|
||||
|
||||
virtual void up_pass(arma::mat const &v) = 0;
|
||||
virtual void down_pass(arma::mat &dst, arma::mat const &src) = 0;
|
||||
void registerLayer(ILayer *pLayer)
|
||||
{
|
||||
lower = pLayer;
|
||||
pLayer->upper = this;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif /* ILAYER_HPP */
|
||||
|
||||
+66
-2
@@ -14,10 +14,12 @@
|
||||
#include "Layer.hpp"
|
||||
using namespace std;
|
||||
|
||||
Layer::Layer(const string &prjname, size_t id, size_t numVisible, size_t numHidden)
|
||||
Layer::Layer(const string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden)
|
||||
: m_prjname(prjname)
|
||||
, m_id(id)
|
||||
, m_rbm(m_rbm_params, numVisible, numHidden)
|
||||
, m_numVisibleX(numVisibleX)
|
||||
, m_numVisibleY(numVisibleY)
|
||||
, m_rbm(m_rbm_params, numVisibleX*numVisibleY, numHidden)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -25,23 +27,85 @@ Layer::Layer(const string &prjname, size_t id, size_t numVisible, size_t numHidd
|
||||
Layer::Layer(const Layer& orig)
|
||||
: m_prjname(orig.m_prjname)
|
||||
, m_id(orig.m_id)
|
||||
, m_numVisibleX(orig.m_numVisibleX)
|
||||
, m_numVisibleY(orig.m_numVisibleY)
|
||||
, m_rbm_params(orig.m_rbm_params)
|
||||
, m_rbm(orig.m_rbm)
|
||||
{
|
||||
m_weightsFile = m_prjname + string(".weights.dat");
|
||||
}
|
||||
|
||||
Layer::~Layer()
|
||||
{
|
||||
}
|
||||
|
||||
void Layer::saveWeight()
|
||||
{
|
||||
FILE *pFile;
|
||||
|
||||
pFile = fopen(m_weightsFile.c_str(), "w");
|
||||
|
||||
if (!pFile)
|
||||
{
|
||||
std::cout << "Could not open " << m_weightsFile << "!" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
const arma::mat &bv = rbm().bv();
|
||||
const arma::mat &bh = rbm().bh();
|
||||
const arma::mat &w = rbm().w();
|
||||
|
||||
size_t numHidden = bh.n_elem;
|
||||
size_t numVisible = bv.n_elem;
|
||||
fprintf(pFile, "%d %d %d\n", (int)m_numVisibleX, (int)m_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);
|
||||
|
||||
}
|
||||
|
||||
Rbm& Layer::rbm()
|
||||
{
|
||||
return m_rbm;
|
||||
}
|
||||
|
||||
Json::Value Layer::toJson() const
|
||||
{
|
||||
Json::Value layer;
|
||||
layer["name"] = string("Layer ") + to_string((int)m_id);
|
||||
layer["weights_file"] = m_prjname + string(".weights.dat");
|
||||
layer["numVisibleX"] = to_string((int)m_numVisibleX);
|
||||
layer["numVisibleY"] = to_string((int)m_numVisibleX);
|
||||
layer["rbm"] = m_rbm.toJson();
|
||||
|
||||
return layer;
|
||||
|
||||
}
|
||||
|
||||
void Layer::up_pass(const arma::mat& v)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Layer::down_pass(arma::mat& dst, const arma::mat& src)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
+12
-3
@@ -19,19 +19,28 @@
|
||||
#include <armadillo>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include "Rbm.hpp"
|
||||
#include "ILayer.hpp"
|
||||
|
||||
class Layer
|
||||
class Layer : public ILayer
|
||||
{
|
||||
public:
|
||||
Layer(const std::string &prjname, size_t id, size_t numVisible, size_t numHidden);
|
||||
Layer(const std::string &prjname, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden);
|
||||
Layer(const Layer& orig);
|
||||
virtual ~Layer();
|
||||
|
||||
Json::Value toJson() const;
|
||||
|
||||
Rbm& rbm();
|
||||
void saveWeight();
|
||||
|
||||
void up_pass(const arma::mat& v) override;
|
||||
void down_pass(arma::mat& dst, const arma::mat& src) override;
|
||||
|
||||
private:
|
||||
const std::string &m_prjname;
|
||||
std::string m_weightsFile;
|
||||
size_t m_id;
|
||||
size_t m_numVisibleX;
|
||||
size_t m_numVisibleY;
|
||||
Rbm::Params m_rbm_params;
|
||||
Rbm m_rbm;
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: Stack.cpp
|
||||
* Author: jens
|
||||
*
|
||||
* Created on 25. Oktober 2019, 18:26
|
||||
*/
|
||||
|
||||
#include "Stack.hpp"
|
||||
|
||||
Stack::Stack()
|
||||
{
|
||||
}
|
||||
|
||||
Stack::Stack(const Stack& orig)
|
||||
{
|
||||
}
|
||||
|
||||
Stack::~Stack()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: Stack.hpp
|
||||
* Author: jens
|
||||
*
|
||||
* Created on 25. Oktober 2019, 18:26
|
||||
*/
|
||||
|
||||
#ifndef STACK_HPP
|
||||
#define STACK_HPP
|
||||
|
||||
#include <armadillo>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include "Layer.hpp"
|
||||
|
||||
class Stack
|
||||
{
|
||||
public:
|
||||
Stack();
|
||||
Stack(const Stack& orig);
|
||||
virtual ~Stack();
|
||||
|
||||
void addLayer(const Layer &layer);
|
||||
const Layer& getLayer();
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif /* STACK_HPP */
|
||||
|
||||
+6
-8
@@ -144,22 +144,20 @@ int main()
|
||||
arma::mat batch = loadTraining(project + string(".training.dat"));
|
||||
|
||||
size_t numTraining = batch.n_rows;
|
||||
size_t numVisible = batch.n_cols;
|
||||
size_t numVisibleX = 28;
|
||||
size_t numVisibleY = 28;
|
||||
size_t numHidden = 64;
|
||||
|
||||
Layer layer(project, 0, numVisible, numHidden);
|
||||
Layer layer(project, 0, numVisibleX, numVisibleY, 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(rbmParams, numVisible, numHidden);
|
||||
Rbm &rbm = layer.rbm();
|
||||
rbm.train(batch, 1000, 100, &statusDisplay);
|
||||
saveWeight(project + string(".weights.dat"), 28, 28, rbm.w(), rbm.bv(), rbm.bh());
|
||||
arma::mat v = arma::randu(numTraining, numVisible);
|
||||
layer.saveWeight();
|
||||
arma::mat v = arma::randu(numTraining, numVisibleX*numVisibleY);
|
||||
arma::mat h = rbm.toHiddenProbs(v);
|
||||
arma::mat r = rbm.toVisibleProbs(h);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user