From c85bd34229a4c54fb3da748a2179d5bc47905a6e Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@etu.univ-amu.fr>
Date: Fri, 7 Dec 2018 10:34:26 +0100
Subject: [PATCH] Added a way to obtain old format when using
 showFeatureRepresentation option with argument 2

---
 MLP/src/MLP.cpp                               | 47 ++++++++++++++-----
 decoder/src/macaon_decode.cpp                 |  4 +-
 .../src/macaon_decode_error_detector.cpp      |  4 +-
 .../src/macaon_train_error_detector.cpp       |  4 +-
 maca_common/include/ProgramParameters.hpp     |  2 +-
 maca_common/src/ProgramParameters.cpp         |  2 +-
 trainer/src/macaon_train.cpp                  |  4 +-
 transition_machine/src/Classifier.cpp         |  2 +-
 8 files changed, 45 insertions(+), 24 deletions(-)

diff --git a/MLP/src/MLP.cpp b/MLP/src/MLP.cpp
index 8852ef5..daf41e8 100644
--- a/MLP/src/MLP.cpp
+++ b/MLP/src/MLP.cpp
@@ -270,10 +270,14 @@ dynet::Expression MLP::run(dynet::ComputationGraph & cg, dynet::Expression x)
 
   if (ProgramParameters::showFeatureRepresentation)
   {
-    for (unsigned int i = 0; i < 81; i++)
-      fprintf(stderr, "%s", i == 80 ? "\n" : "-");
+    if (ProgramParameters::showFeatureRepresentation == 1)
+      for (unsigned int i = 0; i < 81; i++)
+        fprintf(stderr, "%s", i == 80 ? "\n" : "-");
     exprForDebug.clear();
-    exprForDebug.emplace_back("Input layer", h_cur);
+    if (ProgramParameters::showFeatureRepresentation == 1)
+      exprForDebug.emplace_back("Input layer", h_cur);
+    if (ProgramParameters::showFeatureRepresentation >= 2)
+      exprForDebug.emplace_back("", h_cur);
   }
 
   for(unsigned int l = 0; l < layers.size(); l++)
@@ -303,9 +307,17 @@ dynet::Expression MLP::run(dynet::ComputationGraph & cg, dynet::Expression x)
 
     if (ProgramParameters::showFeatureRepresentation)
     {
-      exprForDebug.emplace_back("Result of h = h*W_" + std::to_string(l) + " + b_" + std::to_string(l), a);
-      exprForDebug.emplace_back("Result of h = a_" + std::to_string(l) + "(h)", h);
-      exprForDebug.emplace_back("Result of h = dropout_" + std::to_string(l) + "(h)", h_dropped);
+      if (ProgramParameters::showFeatureRepresentation == 1)
+      {
+        exprForDebug.emplace_back("Result of h = h*W_" + std::to_string(l) + " + b_" + std::to_string(l), a);
+        exprForDebug.emplace_back("Result of h = a_" + std::to_string(l) + "(h)", h);
+        exprForDebug.emplace_back("Result of h = dropout_" + std::to_string(l) + "(h)", h_dropped);
+      }
+      else if (ProgramParameters::showFeatureRepresentation >= 2)
+      {
+        exprForDebug.emplace_back("", a);
+        exprForDebug.emplace_back("", h);
+      }
     }
 
     h_cur = h_dropped;
@@ -314,10 +326,20 @@ dynet::Expression MLP::run(dynet::ComputationGraph & cg, dynet::Expression x)
   if (ProgramParameters::showFeatureRepresentation)
   {
     cg.forward(h_cur);
-    for (auto & it : exprForDebug)
-      fprintf(stderr, "%s (dimension=%lu) :\n%s\n", it.first.c_str(), dynet::as_vector(it.second.value()).size(), expression2str(it.second).c_str());
-    for (unsigned int i = 0; i < 81; i++)
-      fprintf(stderr, "%s", i == 80 ? "\n" : "-");
+
+    if (ProgramParameters::showFeatureRepresentation == 1)
+    {
+      for (auto & it : exprForDebug)
+        fprintf(stderr, "%s (dimension=%lu) :\n%s\n", it.first.c_str(), dynet::as_vector(it.second.value()).size(), expression2str(it.second).c_str());
+      for (unsigned int i = 0; i < 81; i++)
+        fprintf(stderr, "%s", i == 80 ? "\n" : "-");
+    }
+    else if (ProgramParameters::showFeatureRepresentation >= 2)
+    {
+      for (auto & it : exprForDebug)
+        fprintf(stderr, "| %s |", expression2str(it.second).c_str());
+      fprintf(stderr, "\n");
+    }
   }
 
   return h_cur;
@@ -457,17 +479,16 @@ dynet::ParameterCollection & MLP::getModel()
 
 std::string MLP::expression2str(dynet::Expression & expr)
 {
-  std::string result = "<";
+  std::string result = "";
 
   auto elem = dynet::as_vector(expr.value());
 
   for (auto & f : elem)
     result += float2str(f, "%f") + " ";
 
+  if (!result.empty())
   result.pop_back();
 
-  result += ">";
-
   return result;
 }
 
diff --git a/decoder/src/macaon_decode.cpp b/decoder/src/macaon_decode.cpp
index c7f811d..0fa11af 100644
--- a/decoder/src/macaon_decode.cpp
+++ b/decoder/src/macaon_decode.cpp
@@ -42,7 +42,7 @@ po::options_description getOptionsDescription()
       "The name of the buffer's tape that contains the delimiter token for a sequence")
     ("sequenceDelimiter", po::value<std::string>()->default_value("1"),
       "The value of the token that act as a delimiter for sequences")
-    ("showFeatureRepresentation", po::value<bool>()->default_value(false),
+    ("showFeatureRepresentation", po::value<int>()->default_value(0),
       "For each state of the Config, show its feature representation")
     ("lang", po::value<std::string>()->default_value("fr"),
       "Language you are working with");
@@ -110,7 +110,7 @@ int main(int argc, char * argv[])
   ProgramParameters::lang = vm["lang"].as<std::string>();
   ProgramParameters::sequenceDelimiterTape = vm["sequenceDelimiterTape"].as<std::string>();
   ProgramParameters::sequenceDelimiter = vm["sequenceDelimiter"].as<std::string>();
-  ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<bool>();
+  ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<int>();
 
   const char * MACAON_DIR = std::getenv("MACAON_DIR");
   std::string slash = "/";
diff --git a/error_correction/src/macaon_decode_error_detector.cpp b/error_correction/src/macaon_decode_error_detector.cpp
index 488fabf..8b82368 100644
--- a/error_correction/src/macaon_decode_error_detector.cpp
+++ b/error_correction/src/macaon_decode_error_detector.cpp
@@ -41,7 +41,7 @@ po::options_description getOptionsDescription()
       "The name of the buffer's tape that contains the delimiter token for a sequence")
     ("sequenceDelimiter", po::value<std::string>()->default_value("1"),
       "The value of the token that act as a delimiter for sequences")
-    ("showFeatureRepresentation", po::value<bool>()->default_value(false),
+    ("showFeatureRepresentation", po::value<int>()->default_value(0),
       "For each state of the Config, show its feature representation")
     ("lang", po::value<std::string>()->default_value("fr"),
       "Language you are working with");
@@ -109,7 +109,7 @@ int main(int argc, char * argv[])
   ProgramParameters::lang = vm["lang"].as<std::string>();
   ProgramParameters::sequenceDelimiterTape = vm["sequenceDelimiterTape"].as<std::string>();
   ProgramParameters::sequenceDelimiter = vm["sequenceDelimiter"].as<std::string>();
-  ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<bool>();
+  ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<int>();
 
   const char * MACAON_DIR = std::getenv("MACAON_DIR");
   std::string slash = "/";
diff --git a/error_correction/src/macaon_train_error_detector.cpp b/error_correction/src/macaon_train_error_detector.cpp
index 5221047..da5f3f5 100644
--- a/error_correction/src/macaon_train_error_detector.cpp
+++ b/error_correction/src/macaon_train_error_detector.cpp
@@ -61,7 +61,7 @@ po::options_description getOptionsDescription()
       "The number of models that will be trained, with only the random seed changing")
     ("duplicates", po::value<bool>()->default_value(true),
       "Remove identical training examples")
-    ("showFeatureRepresentation", po::value<bool>()->default_value(false),
+    ("showFeatureRepresentation", po::value<int>()->default_value(0),
       "For each state of the Config, show its feature representation")
     ("interactive", po::value<bool>()->default_value(true),
       "Is the shell interactive ? Display advancement informations")
@@ -515,7 +515,7 @@ int main(int argc, char * argv[])
   ProgramParameters::optimizer = vm["optimizer"].as<std::string>();
   ProgramParameters::dynamicEpoch = vm["epochd"].as<int>();
   ProgramParameters::dynamicProbability = vm["proba"].as<float>();
-  ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<bool>();
+  ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<int>();
   ProgramParameters::iterationSize = vm["iterationSize"].as<int>();
 
   if (ProgramParameters::nbTrain)
diff --git a/maca_common/include/ProgramParameters.hpp b/maca_common/include/ProgramParameters.hpp
index 0e25ebd..8e706fe 100644
--- a/maca_common/include/ProgramParameters.hpp
+++ b/maca_common/include/ProgramParameters.hpp
@@ -41,7 +41,7 @@ struct ProgramParameters
   static bool interactive;
   static int dynamicEpoch;
   static float dynamicProbability;
-  static bool showFeatureRepresentation;
+  static int showFeatureRepresentation;
   static int iterationSize;
   static int nbTrain;
   static bool randomEmbeddings;
diff --git a/maca_common/src/ProgramParameters.cpp b/maca_common/src/ProgramParameters.cpp
index c863179..0028364 100644
--- a/maca_common/src/ProgramParameters.cpp
+++ b/maca_common/src/ProgramParameters.cpp
@@ -36,7 +36,7 @@ float ProgramParameters::bias;
 bool ProgramParameters::interactive;
 int ProgramParameters::dynamicEpoch;
 float ProgramParameters::dynamicProbability;
-bool ProgramParameters::showFeatureRepresentation;
+int ProgramParameters::showFeatureRepresentation;
 bool ProgramParameters::randomEmbeddings;
 bool ProgramParameters::printEntropy;
 bool ProgramParameters::printTime;
diff --git a/trainer/src/macaon_train.cpp b/trainer/src/macaon_train.cpp
index 33ea8ad..e7965a7 100644
--- a/trainer/src/macaon_train.cpp
+++ b/trainer/src/macaon_train.cpp
@@ -59,7 +59,7 @@ po::options_description getOptionsDescription()
       "The number of models that will be trained, with only the random seed changing")
     ("duplicates", po::value<bool>()->default_value(true),
       "Remove identical training examples")
-    ("showFeatureRepresentation", po::value<bool>()->default_value(false),
+    ("showFeatureRepresentation", po::value<int>()->default_value(0),
       "For each state of the Config, show its feature representation")
     ("interactive", po::value<bool>()->default_value(true),
       "Is the shell interactive ? Display advancement informations")
@@ -262,7 +262,7 @@ int main(int argc, char * argv[])
   ProgramParameters::optimizer = vm["optimizer"].as<std::string>();
   ProgramParameters::dynamicEpoch = vm["epochd"].as<int>();
   ProgramParameters::dynamicProbability = vm["proba"].as<float>();
-  ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<bool>();
+  ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<int>();
   ProgramParameters::iterationSize = vm["iterationSize"].as<int>();
 
   if (ProgramParameters::nbTrain)
diff --git a/transition_machine/src/Classifier.cpp b/transition_machine/src/Classifier.cpp
index 25c7291..b54e3aa 100644
--- a/transition_machine/src/Classifier.cpp
+++ b/transition_machine/src/Classifier.cpp
@@ -94,7 +94,7 @@ Classifier::WeightedActions Classifier::weightActions(Config & config)
     auto fd = fm->getFeatureDescription(config);
     auto scores = mlp->predict(fd);
 
-    if (ProgramParameters::showFeatureRepresentation)
+    if (ProgramParameters::showFeatureRepresentation == 1)
       fd.printForDebug(stderr);
 
     for (unsigned int i = 0; i < scores.size(); i++)
-- 
GitLab