- Layer: removed legacy weights load/save

git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@777 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2022-01-10 15:48:51 +00:00
parent bfbd837ed5
commit 03a6624017
3 changed files with 36 additions and 136 deletions
+21 -82
View File
@@ -43,99 +43,38 @@ Layer::~Layer()
{
}
bool Layer::loadWeights(const string &prjname)
bool Layer::weightsLoad(std::string const &dir, std::string const &prj)
{
string filename = filePrefix(prjname) + ".weights.dat";
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);
FILE *pFile = fopen(filename.c_str(),"r");
if (!pFile)
if (result)
{
std::cout << "loadWeights(): Could not open " << filename << " for reading!" << std::endl;
return false;
std::cout << "Layer " << m_id << ": Importing weights" << std::endl;
weightsAssign(w, bh, bv);
}
std::cout << "Importing weights for " << m_name << "." << to_string((int)m_id) << std::endl;
size_t numVisible = Rbm::numVisible();
size_t numHidden = Rbm::numHidden();
int i, j;
float v;
int result;
arma::mat _bv(1, numVisible);
for (i=0; i < numVisible; i++)
{
result = fscanf(pFile, "%f", &v);
if (result > 0)
{
_bv(i) = v;
}
}
arma::mat _bhv(1, numHidden);
for (i=0; i < numHidden; i++)
{
result = fscanf(pFile, "%f", &v);
if (result > 0)
{
_bhv(i) = v;
}
}
arma::mat _whv(numVisible, numHidden);
for (i=0; i < numVisible; i++)
{
for (j=0; j < numHidden; j++)
{
result = fscanf(pFile, "%f", &v);
if (result > 0)
{
_whv(i, j) = v;
}
}
}
weightsAssign(_whv, _bhv, _bv);
fclose(pFile);
return true;
return result;
}
bool Layer::saveWeights(const string &prjname)
bool Layer::weightsSave(std::string const &dir, std::string const &prj)
{
int numHidden = m_bhv.n_elem;
int numVisible = m_bv.n_elem;
string filename = filePrefix(prjname) + ".weights.dat";
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);
FILE *pFile = fopen(filename.c_str(),"w");
if (!pFile)
if (result)
{
std::cout << "saveWeights(): Could not open " << filename << " for writing!" << std::endl;
return false;
std::cout << "Layer " << m_id << ": Exporting weights" << std::endl;
}
std::cout << "Exporting weights for " << m_name << "." << to_string((int)m_id) << std::endl;
int i, j;
for (i=0; i < numVisible; i++)
{
fprintf(pFile, "%3.6f\n", m_bv(i));
}
for (i=0; i < numHidden; i++)
{
fprintf(pFile, "%3.6f\n", m_bhv(i));
}
const arma::mat &_whv = whv();
for (i=0; i < numVisible; i++)
{
for (j=0; j < numHidden; j++)
{
fprintf(pFile, "%3.6f ", _whv(i,j));
}
fprintf(pFile, "\n");
}
fclose(pFile);
return true;
return result;
}
Json::Value Layer::toJson() const