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

Genetic Algorithm now only saves the best MLP

parent bf22fa5a
No related branches found
No related tags found
No related merge requests found
......@@ -43,7 +43,9 @@ class GeneticAlgorithm : public NeuralNetwork
/// @param nbOutputs The size of the mlp output layer..
Individual(dynet::ParameterCollection & model, int nbInputs, const std::string & topology, int nbOutputs);
/// @brief Create a blank individual, so that it will be loaded from a saved model.
Individual();
///
/// @param bestId The id of the individual that was saved in the file.
Individual(unsigned int bestId);
void becomeChildOf(Individual * other);
void becomeChildOf(Individual * mom, Individual * dad);
void mutate(float probability);
......
......@@ -39,19 +39,7 @@ void GeneticAlgorithm::init(int nbInputs, const std::string & topology, int nbOu
std::vector<float> GeneticAlgorithm::predict(FeatureModel::FeatureDescription & fd)
{
std::vector<float> prediction = generation[0]->mlp.predict(fd);
for (unsigned int i = 1; i < generation.size(); i++)
{
auto otherPred = generation[i]->mlp.predict(fd);
for (unsigned int j = 0; j < prediction.size(); j++)
prediction[j] += otherPred[j];
}
for (unsigned int j = 0; j < prediction.size(); j++)
prediction[j] /= generation.size();
return prediction;
return generation[0]->mlp.predict(fd);
}
float GeneticAlgorithm::update(FeatureModel::FeatureDescription & fd, int gold)
......@@ -128,16 +116,10 @@ float GeneticAlgorithm::update(FeatureModel::FeatureDescription & fd, int gold)
void GeneticAlgorithm::save(const std::string & filename)
{
File * file = new File(filename, "w");
fprintf(file->getDescriptor(), "%d %d %s\n", nbInputs, nbOutputs, topology.c_str());
fprintf(file->getDescriptor(), "%u\n", generation[0]->id);
delete file;
unsigned int quarter = generation.size() / 4;
for (unsigned int i = 0; i < quarter; i++)
{
generation[i]->mlp.saveStruct(filename);
generation[i]->mlp.saveParameters(filename);
}
generation[0]->mlp.saveStruct(filename);
generation[0]->mlp.saveParameters(filename);
}
void GeneticAlgorithm::printTopology(FILE * output)
......@@ -156,37 +138,17 @@ void GeneticAlgorithm::printTopology(FILE * output)
void GeneticAlgorithm::load(const std::string & filename)
{
File * file = new File(filename, "r");
int i, o;
char buffer[1024];
if (fscanf(file->getDescriptor(), "%d %d %[^\n]\n", &i, &o, buffer) != 3)
unsigned int bestId;
if (fscanf(file->getDescriptor(), "%u\n", &bestId) != 1)
{
fprintf(stderr, "ERROR (%s) : file \'%s\' bad format. Aborting.\n", ERRINFO, filename.c_str());
fprintf(stderr, "ERROR (%s) : expected best id when reading file \'%s\'. Aborting.\n", ERRINFO, filename.c_str());
exit(1);
}
delete file;
generation.emplace_back(new Individual(bestId));
this->nbInputs = i;
this->nbOutputs = o;
this->topology = buffer;
auto splited = split(topology, ' ');
if (splited.size() != 2 || !isNum(splited[0]))
{
fprintf(stderr, "ERROR (%s) : wrong topology \'%s\'. Aborting.\n", ERRINFO, topology.c_str());
exit(1);
}
int nbElems = std::stoi(splited[0]);
for (int i = 0; i < nbElems; i++)
generation.emplace_back(new Individual());
for (int i = 0; i < nbElems; i++)
{
generation[i]->mlp.loadStruct(model, filename, 0);
generation[i]->mlp.loadParameters(model, filename);
}
generation[0]->mlp.loadStruct(model, filename, 0);
generation[0]->mlp.loadParameters(model, filename);
}
GeneticAlgorithm::Individual::Individual(dynet::ParameterCollection & model, int nbInputs, const std::string & topology, int nbOutputs) : mlp("MLP_" + std::to_string(idCount))
......@@ -199,10 +161,10 @@ GeneticAlgorithm::Individual::Individual(dynet::ParameterCollection & model, int
mutate(1.0);
}
GeneticAlgorithm::Individual::Individual()
: mlp("MLP_" + std::to_string(idCount))
GeneticAlgorithm::Individual::Individual(unsigned int bestId)
: mlp("MLP_" + std::to_string(bestId))
{
this->id = idCount++;
this->id = bestId;
this->loss = 0.0;
this->value = 0.0;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment