diff --git a/MLP/include/MLP.hpp b/MLP/include/MLP.hpp index 68f1849caab821449cac0b1f0f5b18705c798861..be48db2d0f2d9a74d01d90b91f7fead8d85b7551 100644 --- a/MLP/include/MLP.hpp +++ b/MLP/include/MLP.hpp @@ -167,6 +167,12 @@ class MLP public : + /// @brief Convert a dynet expression to a string (usefull for debug purposes) + /// + /// @param expr The expression to convert. + /// + /// @return A string representing the expression. + static std::string expression2str(dynet::Expression & expr); /// @brief initialize a new untrained MLP from a desired topology. /// /// topology example for 2 hidden layers : (150,RELU,0.3)(50,ELU,0.2)\n diff --git a/MLP/src/MLP.cpp b/MLP/src/MLP.cpp index 95bb9f3552cbc51efb19501339ece4a47e178216..75ccfc0d932e43af596ef646d822b45774e2043a 100644 --- a/MLP/src/MLP.cpp +++ b/MLP/src/MLP.cpp @@ -247,9 +247,19 @@ dynet::Expression MLP::featValue2Expression(dynet::ComputationGraph & cg, const dynet::Expression MLP::run(dynet::ComputationGraph & cg, dynet::Expression x) { + static std::vector< std::pair<std::string,dynet::Expression> > exprForDebug; + // Expression for the current hidden state dynet::Expression h_cur = x; + if (ProgramParameters::showFeatureRepresentation) + { + for (unsigned int i = 0; i < 81; i++) + fprintf(stderr, "%s", i == 80 ? "\n" : "-"); + exprForDebug.clear(); + exprForDebug.emplace_back("Input layer", h_cur); + } + for(unsigned int l = 0; l < layers.size(); l++) { // Initialize parameters in computation graph @@ -275,9 +285,25 @@ dynet::Expression MLP::run(dynet::ComputationGraph & cg, dynet::Expression x) h_dropped = h; } + 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); + } + h_cur = h_dropped; } + 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" : "-"); + } + return h_cur; } @@ -413,3 +439,19 @@ dynet::ParameterCollection & MLP::getModel() return model; } +std::string MLP::expression2str(dynet::Expression & expr) +{ + std::string result = "<"; + + auto elem = dynet::as_vector(expr.value()); + + for (auto & f : elem) + result += float2str(f, "%f") + " "; + + result.pop_back(); + + result += ">"; + + return result; +} +