Skip to content
Snippets Groups Projects
Commit 1799b40a authored by Franck Dary's avatar Franck Dary
Browse files

We now save only best epochs in training

parent d8964edd
No related branches found
No related tags found
No related merge requests found
...@@ -35,6 +35,9 @@ void processAllExamples( ...@@ -35,6 +35,9 @@ void processAllExamples(
void printIterationScores(FILE * output, void printIterationScores(FILE * output,
std::map< std::string, std::pair<int, int> > & nbExamplesTrain, std::map< std::string, std::pair<int, int> > & nbExamplesTrain,
std::map< std::string, std::pair<int, int> > & nbExamplesDev, std::map< std::string, std::pair<int, int> > & nbExamplesDev,
std::map< std::string, std::vector<float> > & trainScores,
std::map< std::string, std::vector<float> > & devScores,
std::map<std::string, int> & bestIter,
int nbIter, int curIter); int nbIter, int curIter);
void shuffleAllExamples(std::map<Classifier*, MLP::Examples > &); void shuffleAllExamples(std::map<Classifier*, MLP::Examples > &);
......
...@@ -68,6 +68,9 @@ void Trainer::processAllExamples( ...@@ -68,6 +68,9 @@ void Trainer::processAllExamples(
void Trainer::printIterationScores(FILE * output, void Trainer::printIterationScores(FILE * output,
std::map< std::string, std::pair<int, int> > & nbExamplesTrain, std::map< std::string, std::pair<int, int> > & nbExamplesTrain,
std::map< std::string, std::pair<int, int> > & nbExamplesDev, std::map< std::string, std::pair<int, int> > & nbExamplesDev,
std::map< std::string, std::vector<float> > & trainScores,
std::map< std::string, std::vector<float> > & devScores,
std::map<std::string, int> & bestIter,
int nbIter, int curIter) int nbIter, int curIter)
{ {
fprintf(output, "Iteration %d/%d :\n", curIter+1, nbIter); fprintf(output, "Iteration %d/%d :\n", curIter+1, nbIter);
...@@ -75,10 +78,34 @@ void Trainer::printIterationScores(FILE * output, ...@@ -75,10 +78,34 @@ void Trainer::printIterationScores(FILE * output,
{ {
float scoreTrain = 100.0*it.second.second / it.second.first; float scoreTrain = 100.0*it.second.second / it.second.first;
float scoreDev = devConfig ? 100.0*nbExamplesDev[it.first].second / nbExamplesDev[it.first].first : -1.0; float scoreDev = devConfig ? 100.0*nbExamplesDev[it.first].second / nbExamplesDev[it.first].first : -1.0;
trainScores[it.first].emplace_back(scoreTrain);
devScores[it.first].emplace_back(scoreDev);
bool isBest = curIter ? false : true;
if (devConfig) if (devConfig)
fprintf(output, "\t%s accuracy : train(%.2f%%) dev(%.2f%%)\n", it.first.c_str(), scoreTrain, scoreDev); {
if (scoreDev > devScores[it.first][bestIter[it.first]])
{
isBest = true;
bestIter[it.first] = devScores[it.first].size()-1;
}
}
else else
fprintf(output, "\t%s accuracy : train(%.2f%%)\n", it.first.c_str(), scoreTrain); {
if (scoreTrain > trainScores[it.first][bestIter[it.first]])
{
isBest = true;
bestIter[it.first] = trainScores[it.first].size()-1;
}
}
if (devConfig)
fprintf(output, "\t%s accuracy : train(%.2f%%) dev(%.2f%%)", it.first.c_str(), scoreTrain, scoreDev);
else
fprintf(output, "\t%s accuracy : train(%.2f%%)", it.first.c_str(), scoreTrain);
fprintf(output, "%s", isBest ? " SAVED\n" : "\n");
} }
} }
...@@ -100,6 +127,10 @@ void Trainer::trainBatched(int nbIter, int batchSize, bool mustShuffle) ...@@ -100,6 +127,10 @@ void Trainer::trainBatched(int nbIter, int batchSize, bool mustShuffle)
if(devMcd && devConfig) if(devMcd && devConfig)
getExamplesByClassifier(devExamples, *devConfig); getExamplesByClassifier(devExamples, *devConfig);
std::map< std::string, std::vector<float> > trainScores;
std::map< std::string, std::vector<float> > devScores;
std::map<std::string, int> bestIter;
for (int i = 0; i < nbIter; i++) for (int i = 0; i < nbIter; i++)
{ {
std::map< std::string, std::pair<int, int> > nbExamplesTrain; std::map< std::string, std::pair<int, int> > nbExamplesTrain;
...@@ -120,14 +151,16 @@ void Trainer::trainBatched(int nbIter, int batchSize, bool mustShuffle) ...@@ -120,14 +151,16 @@ void Trainer::trainBatched(int nbIter, int batchSize, bool mustShuffle)
return c->getScoreOnBatch(ex, s, e); return c->getScoreOnBatch(ex, s, e);
}); });
printIterationScores(stderr, nbExamplesTrain, nbExamplesDev, nbIter, i); printIterationScores(stderr, nbExamplesTrain, nbExamplesDev,
} trainScores, devScores, bestIter, nbIter, i);
auto & classifiers = tm.getClassifiers(); auto & classifiers = tm.getClassifiers();
for(Classifier * cla : classifiers) for(Classifier * cla : classifiers)
if(cla->needsTrain()) if(cla->needsTrain())
if(bestIter[cla->name] == i)
cla->save(); cla->save();
} }
}
void Trainer::train(int nbIter, int batchSize, bool mustShuffle) void Trainer::train(int nbIter, int batchSize, bool mustShuffle)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment