Files
Rbm/source/Layer.cpp
T
2022-01-10 15:48:51 +00:00

96 lines
2.3 KiB
C++

/*
* 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: Layer.cpp
* Author: jens
*
* Created on 25. Oktober 2019, 08:13
*/
#include "Layer.hpp"
using namespace std;
Layer::Layer(const string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, size_t numContext)
: Rbm(numVisibleX*numVisibleY+numContext, numHidden)
, next(nullptr)
, prev(nullptr)
, m_name(name)
, m_id(id)
, m_numVisibleX(numVisibleX)
, m_numVisibleY(numVisibleY)
, m_numContext(numContext)
, m_context(0, numContext)
{
cout << "Create Layer " << m_name << "." << to_string((int)m_id) << endl;
}
Layer::Layer(const Layer& orig)
: Rbm(orig.bv().n_elem, orig.bh().n_elem)
, next(nullptr)
, prev(nullptr)
, m_name(orig.m_name)
, m_id(orig.m_id)
, m_numVisibleX(orig.m_numVisibleX)
, m_numVisibleY(orig.m_numVisibleY)
{
}
Layer::~Layer()
{
}
bool Layer::weightsLoad(std::string const &dir, std::string const &prj)
{
arma::mat w;
arma::mat bh;
arma::mat bv;
bool result = true;
result &= w.load(filePrefix(prj) + ".w.dat", arma::arma_ascii);
result &= bh.load(filePrefix(prj) + ".bh.dat", arma::arma_ascii);
result &= bv.load(filePrefix(prj) + ".bv.dat", arma::arma_ascii);
if (result)
{
std::cout << "Layer " << m_id << ": Importing weights" << std::endl;
weightsAssign(w, bh, bv);
}
return result;
}
bool Layer::weightsSave(std::string const &dir, std::string const &prj)
{
bool result = true;
result &= whv().save(filePrefix(prj) + ".w.dat", arma::arma_ascii);
result &= bh().save(filePrefix(prj) + ".bh.dat", arma::arma_ascii);
result &= bv().save(filePrefix(prj) + ".bv.dat", arma::arma_ascii);
if (result)
{
std::cout << "Layer " << m_id << ": Exporting weights" << std::endl;
}
return result;
}
Json::Value Layer::toJson() const
{
std::cout << "Exporting Layer " << to_string((int)m_id) << std::endl;
Json::Value layer;
layer["name"] = m_name;
layer["id"] = (int)m_id;
layer["weights_file"] = filePrefix("") + ".weights.dat";
layer["numVisibleX"] = (int)m_numVisibleX;
layer["numVisibleY"] = (int)m_numVisibleY;
layer["numHidden"] = (int)whv().n_cols;
layer["numContext"] = m_numContext;
layer["rbm"] = Rbm::toJson();
return layer;
}