Route project files through prj/<name>/ instead of flat at repo root
Repo root had ~35 groups of .prj/.dat files loose alongside the build system and source, all sharing one flat namespace. Centralize the new prj/<name>/ convention inside AStack::loadWeights/saveWeights/ loadTrainingBatch/saveTrainingBatch and StackCreator::fromFile/toFile (via a new AStack::projectDir() helper) -- these already have access to the project name, so callers (poet.cpp, the GUI) need no changes at all; they keep passing the same base directory they always did. Filenames inside each project folder are unchanged (e.g. prj/mnist_2/mnist_2.prj, prj/mnist_2/mnist_2.Layer.0.w.dat) -- only the directory moves. Added Matutils::ensureDir() (mkdir -p equivalent; no std::filesystem in C++11) since ofstream won't create directories, used wherever a project is saved for the first time. File migration for existing projects is a separate commit. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016K8Gu7Qejd11JbdiHZqYAs
This commit is contained in:
+14
-5
@@ -117,6 +117,11 @@ Layer* AStack::getLastLayer() const
|
||||
return getLayer(numLayers()-1);
|
||||
}
|
||||
|
||||
std::string AStack::projectDir(const std::string &dir) const
|
||||
{
|
||||
return dir + "/prj/" + m_name;
|
||||
}
|
||||
|
||||
bool AStack::save(const std::string &dir)
|
||||
{
|
||||
return StackCreator::toFile(this, dir, m_name);
|
||||
@@ -134,11 +139,12 @@ void AStack::weightsInit(double stddev)
|
||||
|
||||
bool AStack::loadWeights(const std::string &dir)
|
||||
{
|
||||
std::string pdir = projectDir(dir);
|
||||
bool result = true;
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
bool success = pLayer->weightsLoad(dir, m_name);
|
||||
bool success = pLayer->weightsLoad(pdir, m_name);
|
||||
if (!success)
|
||||
{
|
||||
pLayer->weightsInit(0.01, 0);
|
||||
@@ -151,11 +157,13 @@ bool AStack::loadWeights(const std::string &dir)
|
||||
|
||||
bool AStack::saveWeights(const std::string &dir)
|
||||
{
|
||||
std::string pdir = projectDir(dir);
|
||||
ensureDir(pdir);
|
||||
bool result = true;
|
||||
Layer *pLayer = m_pLayers;
|
||||
while(pLayer)
|
||||
{
|
||||
bool success = pLayer->weightsSave(dir, m_name);
|
||||
bool success = pLayer->weightsSave(pdir, m_name);
|
||||
result &= success;
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
@@ -169,8 +177,7 @@ arma::mat& AStack::trainingBatch()
|
||||
|
||||
size_t AStack::loadTrainingBatch(const std::string &dir, bool doNormalize)
|
||||
{
|
||||
std::string filename = dir + "/" + m_name + ".training.dat";
|
||||
std::string path = dir + "/" + m_name + ".training.dat";
|
||||
std::string filename = projectDir(dir) + "/" + m_name + ".training.dat";
|
||||
bool success = m_trainingBatch.load(filename, arma::auto_detect);
|
||||
|
||||
if (success)
|
||||
@@ -190,7 +197,9 @@ size_t AStack::loadTrainingBatch(const std::string &dir, bool doNormalize)
|
||||
|
||||
size_t AStack::saveTrainingBatch(const std::string &dir)
|
||||
{
|
||||
std::string filename = dir + "/" + m_name + ".training.dat";
|
||||
std::string pdir = projectDir(dir);
|
||||
ensureDir(pdir);
|
||||
std::string filename = pdir + "/" + m_name + ".training.dat";
|
||||
bool success = m_trainingBatch.save(filename, arma::arma_ascii);
|
||||
|
||||
if (success)
|
||||
|
||||
+4
-1
@@ -89,7 +89,10 @@ private:
|
||||
arma::mat m_trainingBatch;
|
||||
size_t m_numLayers;
|
||||
void updateNumLayers();
|
||||
|
||||
|
||||
// Every project's files live under {dir}/prj/{m_name}/, not flat at {dir}.
|
||||
std::string projectDir(const std::string &dir) const;
|
||||
|
||||
};
|
||||
|
||||
#endif /* ASTACK_HPP */
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "StackCreator.hpp"
|
||||
#include "DeepStack.hpp"
|
||||
#include "RnnStack.hpp"
|
||||
#include "matutils.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
@@ -77,8 +78,8 @@ AStack* StackCreator::fromJson(Json::Value& project, LayerConstructor *pLayerCon
|
||||
AStack* StackCreator::fromFile(const std::string &dir, const std::string &name, LayerConstructor *pLayerConstructor)
|
||||
{
|
||||
std::cout << "Importing Project " << name << std::endl;
|
||||
ifstream ifs(dir + "/" + name + string(".prj"));
|
||||
|
||||
ifstream ifs(dir + "/prj/" + name + "/" + name + string(".prj"));
|
||||
|
||||
Json::Value project;
|
||||
ifs >> project;
|
||||
|
||||
@@ -88,8 +89,10 @@ AStack* StackCreator::fromFile(const std::string &dir, const std::string &name,
|
||||
bool StackCreator::toFile(AStack* pStack, const std::string& dir, const std::string& name)
|
||||
{
|
||||
std::cout << "Exporting Project " << name << std::endl;
|
||||
ofstream ofs(dir + "/" + name + string(".prj"));
|
||||
|
||||
std::string pdir = dir + "/prj/" + name;
|
||||
Matutils::ensureDir(pdir);
|
||||
ofstream ofs(pdir + "/" + name + string(".prj"));
|
||||
|
||||
Json::Value project;
|
||||
project["stack"]["name"] = name;
|
||||
project["stack"]["type_string"] = AStack::stackTypeStrings[pStack->type()];
|
||||
|
||||
@@ -17,9 +17,32 @@
|
||||
#include "RnnTextHelper.hpp"
|
||||
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace Matutils
|
||||
{
|
||||
// Creates every missing directory component of path (like `mkdir -p`).
|
||||
// No std::filesystem in C++11, so this is a small POSIX-only helper.
|
||||
inline void ensureDir(const std::string &path)
|
||||
{
|
||||
size_t pos = 0;
|
||||
while (pos <= path.size())
|
||||
{
|
||||
size_t next = path.find('/', pos);
|
||||
if (next == std::string::npos)
|
||||
{
|
||||
next = path.size();
|
||||
}
|
||||
std::string prefix = path.substr(0, next);
|
||||
if (!prefix.empty())
|
||||
{
|
||||
mkdir(prefix.c_str(), 0755);
|
||||
}
|
||||
pos = next + 1;
|
||||
}
|
||||
}
|
||||
|
||||
const size_t NORMALIZING_DIM = 1;
|
||||
inline arma::mat uniform(arma::mat const & sizeMat, double stdDev=1.0, double mu=0.5)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user