From cfae9a32308f5937f1e33d5c6fcf8910bde5f6b1 Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@etu.univ-amu.fr>
Date: Mon, 10 Dec 2018 16:22:05 +0100
Subject: [PATCH] Added an option to chose between random parameters and
 1-initialized parameters

---
 MLP/src/MLP.cpp                                      | 5 +++++
 error_correction/src/macaon_train_error_detector.cpp | 3 +++
 maca_common/include/ProgramParameters.hpp            | 1 +
 maca_common/src/ProgramParameters.cpp                | 1 +
 trainer/src/Trainer.cpp                              | 6 ++++++
 trainer/src/macaon_train.cpp                         | 3 +++
 6 files changed, 19 insertions(+)

diff --git a/MLP/src/MLP.cpp b/MLP/src/MLP.cpp
index daf41e8..409b054 100644
--- a/MLP/src/MLP.cpp
+++ b/MLP/src/MLP.cpp
@@ -149,6 +149,11 @@ void MLP::addLayerToModel(Layer & layer)
 {
   dynet::Parameter W = model.add_parameters({(unsigned)layer.output_dim, (unsigned)layer.input_dim});
   dynet::Parameter b = model.add_parameters({(unsigned)layer.output_dim});
+  if (!ProgramParameters::randomParameters)
+  {
+    W.set_value(std::vector<float>((unsigned)layer.output_dim * (unsigned)layer.input_dim, 1.0));
+    b.set_value(std::vector<float>((unsigned)layer.output_dim, 1.0));
+  }
   parameters.push_back({W,b});
 }
 
diff --git a/error_correction/src/macaon_train_error_detector.cpp b/error_correction/src/macaon_train_error_detector.cpp
index 1751e92..e1f87e8 100644
--- a/error_correction/src/macaon_train_error_detector.cpp
+++ b/error_correction/src/macaon_train_error_detector.cpp
@@ -67,6 +67,8 @@ po::options_description getOptionsDescription()
       "Is the shell interactive ? Display advancement informations")
     ("randomEmbeddings", po::value<bool>()->default_value(false),
       "When activated, the embeddings will be randomly initialized")
+    ("randomParameters", po::value<bool>()->default_value(true),
+      "When activated, the parameters will be randomly initialized")
     ("sequenceDelimiterTape", po::value<std::string>()->default_value("EOS"),
       "The name of the buffer's tape that contains the delimiter token for a sequence")
     ("sequenceDelimiter", po::value<std::string>()->default_value("1"),
@@ -510,6 +512,7 @@ int main(int argc, char * argv[])
   ProgramParameters::interactive = vm["interactive"].as<bool>();
   ProgramParameters::shuffleExamples = vm["shuffle"].as<bool>();
   ProgramParameters::randomEmbeddings = vm["randomEmbeddings"].as<bool>();
+  ProgramParameters::randomParameters = vm["randomParameters"].as<bool>();
   ProgramParameters::sequenceDelimiterTape = vm["sequenceDelimiterTape"].as<std::string>();
   ProgramParameters::sequenceDelimiter = vm["sequenceDelimiter"].as<std::string>();
   ProgramParameters::learningRate = vm["lr"].as<float>();
diff --git a/maca_common/include/ProgramParameters.hpp b/maca_common/include/ProgramParameters.hpp
index 8e706fe..0bc4074 100644
--- a/maca_common/include/ProgramParameters.hpp
+++ b/maca_common/include/ProgramParameters.hpp
@@ -45,6 +45,7 @@ struct ProgramParameters
   static int iterationSize;
   static int nbTrain;
   static bool randomEmbeddings;
+  static bool randomParameters;
   static bool printEntropy;
   static bool printTime;
   static std::string sequenceDelimiterTape;
diff --git a/maca_common/src/ProgramParameters.cpp b/maca_common/src/ProgramParameters.cpp
index 0028364..c5c8328 100644
--- a/maca_common/src/ProgramParameters.cpp
+++ b/maca_common/src/ProgramParameters.cpp
@@ -38,6 +38,7 @@ int ProgramParameters::dynamicEpoch;
 float ProgramParameters::dynamicProbability;
 int ProgramParameters::showFeatureRepresentation;
 bool ProgramParameters::randomEmbeddings;
+bool ProgramParameters::randomParameters;
 bool ProgramParameters::printEntropy;
 bool ProgramParameters::printTime;
 int ProgramParameters::iterationSize;
diff --git a/trainer/src/Trainer.cpp b/trainer/src/Trainer.cpp
index 4d2b881..991cc15 100644
--- a/trainer/src/Trainer.cpp
+++ b/trainer/src/Trainer.cpp
@@ -210,6 +210,12 @@ void Trainer::train()
 
         if (zeroCostActions.empty())
         {
+          if (trainConfig.head >= (int)trainConfig.tapes[0].ref.size()-1)
+          {
+            while (!trainConfig.stackEmpty())
+              trainConfig.stackPop();
+            break;
+          }
           fprintf(stderr, "ERROR (%s) : Unable to find any zero cost action. Aborting.\n", ERRINFO);
           fprintf(stderr, "State : %s\n", currentState->name.c_str());
           trainConfig.printForDebug(stderr);
diff --git a/trainer/src/macaon_train.cpp b/trainer/src/macaon_train.cpp
index e7965a7..3585999 100644
--- a/trainer/src/macaon_train.cpp
+++ b/trainer/src/macaon_train.cpp
@@ -65,6 +65,8 @@ po::options_description getOptionsDescription()
       "Is the shell interactive ? Display advancement informations")
     ("randomEmbeddings", po::value<bool>()->default_value(false),
       "When activated, the embeddings will be randomly initialized")
+    ("randomParameters", po::value<bool>()->default_value(true),
+      "When activated, the parameters will be randomly initialized")
     ("sequenceDelimiterTape", po::value<std::string>()->default_value("EOS"),
       "The name of the buffer's tape that contains the delimiter token for a sequence")
     ("sequenceDelimiter", po::value<std::string>()->default_value("1"),
@@ -253,6 +255,7 @@ int main(int argc, char * argv[])
   ProgramParameters::interactive = vm["interactive"].as<bool>();
   ProgramParameters::shuffleExamples = vm["shuffle"].as<bool>();
   ProgramParameters::randomEmbeddings = vm["randomEmbeddings"].as<bool>();
+  ProgramParameters::randomParameters = vm["randomParameters"].as<bool>();
   ProgramParameters::sequenceDelimiterTape = vm["sequenceDelimiterTape"].as<std::string>();
   ProgramParameters::sequenceDelimiter = vm["sequenceDelimiter"].as<std::string>();
   ProgramParameters::learningRate = vm["lr"].as<float>();
-- 
GitLab