From 948218dab96369b24027c801b61cad699db7b4c6 Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Mon, 20 Feb 2017 14:08:35 +0800 Subject: [PATCH] Unify PrintLogic in PrintLayer/ValuePrinter. --- paddle/gserver/evaluators/Evaluator.cpp | 13 ++----------- paddle/gserver/layers/PrintLayer.cpp | 24 ++++++++---------------- paddle/parameter/Argument.cpp | 12 ++++++++++++ paddle/parameter/Argument.h | 3 +++ 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/paddle/gserver/evaluators/Evaluator.cpp b/paddle/gserver/evaluators/Evaluator.cpp index 1a43d3bd99..1b06519713 100644 --- a/paddle/gserver/evaluators/Evaluator.cpp +++ b/paddle/gserver/evaluators/Evaluator.cpp @@ -888,19 +888,10 @@ Evaluator* Evaluator::create(const EvaluatorConfig& config) { */ class ValuePrinter : public Evaluator { public: - ValuePrinter() {} - virtual void eval(const NeuralNetwork& nn) { for (const std::string& name : config_.input_layers()) { - auto& argu = nn.getLayer(name)->getOutput(); - std::unordered_map out; - argu.getValueString(&out); - for (auto field : {"value", "id", "sequence pos", "sub-sequence pos"}) { - auto it = out.find(field); - if (it != out.end()) { - LOG(INFO) << "layer=" << name << " " << field << ":\n" << it->second; - } - } + nn.getLayer(name)->getOutput().printValueString(LOG(INFO), + "layer=" + name + " "); } } diff --git a/paddle/gserver/layers/PrintLayer.cpp b/paddle/gserver/layers/PrintLayer.cpp index f1f3dd412c..de198af111 100644 --- a/paddle/gserver/layers/PrintLayer.cpp +++ b/paddle/gserver/layers/PrintLayer.cpp @@ -19,25 +19,17 @@ namespace paddle { class PrintLayer : public Layer { public: explicit PrintLayer(const LayerConfig& config) : Layer(config) {} - void forward(PassType passType) override; - void backward(const UpdateCallback& callback) override {} -}; -void PrintLayer::forward(PassType passType) { - Layer::forward(passType); - for (size_t i = 0; i != inputLayers_.size(); ++i) { - auto& argu = getInput(i); - const std::string& name = inputLayers_[i]->getName(); - std::unordered_map out; - argu.getValueString(&out); - for (auto field : {"value", "id", "sequence pos", "sub-sequence pos"}) { - auto it = out.find(field); - if (it != out.end()) { - LOG(INFO) << "layer=" << name << " " << field << ":\n" << it->second; - } + void forward(PassType passType) override { + Layer::forward(passType); + for (size_t i = 0; i != inputLayers_.size(); ++i) { + getInput(i).printValueString(LOG(INFO), + "layer=" + inputLayers_[i]->getName() + " "); } } -} + + void backward(const UpdateCallback& callback) override {} +}; REGISTER_LAYER(print, PrintLayer); diff --git a/paddle/parameter/Argument.cpp b/paddle/parameter/Argument.cpp index e9de0f6698..7a343cca33 100644 --- a/paddle/parameter/Argument.cpp +++ b/paddle/parameter/Argument.cpp @@ -628,6 +628,18 @@ void Argument::getValueString( } } +void Argument::printValueString(std::ostream& stream, + const std::string& prefix) const { + std::unordered_map out; + getValueString(&out); + for (auto field : {"value", "id", "sequence pos", "sub-sequence pos"}) { + auto it = out.find(field); + if (it != out.end()) { + stream << prefix << field << ":\n" << it->second; + } + } +} + void Argument::subArgFrom(const Argument& input, size_t offset, size_t height, diff --git a/paddle/parameter/Argument.h b/paddle/parameter/Argument.h index c751dbb855..e2a8500c3f 100644 --- a/paddle/parameter/Argument.h +++ b/paddle/parameter/Argument.h @@ -305,6 +305,9 @@ struct Argument { * @param out [out]: the return values. */ void getValueString(std::unordered_map* out) const; + + void printValueString(std::ostream& stream, + const std::string& prefix = "") const; }; } // namespace paddle -- GitLab