- refactored

- use Armadillo for load/save of weight and training data

git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@775 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2022-01-10 15:25:52 +00:00
parent 952cf26930
commit ff2086a1ff
9 changed files with 125 additions and 65 deletions
+51 -4
View File
@@ -31,9 +31,7 @@ public:
virtual ~Layer();
Json::Value toJson() const;
bool loadWeights(const std::string &prjname="");
bool saveWeights(const std::string &prjname="");
void setBatch(arma::mat const &batch)
{
if (batch.n_rows > 0)
@@ -83,6 +81,43 @@ public:
return whv().n_cols;
}
bool 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);
}
else
{
return loadWeights(prj);
}
return true;
}
bool 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;
}
arma::mat trainingData(arma::mat const &batch)
{
arma::mat thisBatch = batch;
@@ -131,12 +166,24 @@ public:
private:
std::string m_name;
std::string m_weightsFile;
size_t m_id;
size_t m_numVisibleX;
size_t m_numVisibleY;
size_t m_numContext;
arma::mat m_context;
// Compatibility
bool loadWeights(const std::string &prjname="");
bool saveWeights(const std::string &prjname="");
std::string filePrefix(const std::string &prjname) const
{
std::string filename = m_name + "." + std::to_string((int)m_id);
if (prjname.size() > 0)
{
filename = prjname + "." + filename;
}
return filename;
}
};