Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ctc edit distance evaluator can not print information in v2 API. #3844

Merged
merged 3 commits into from
Sep 4, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions paddle/gserver/evaluators/CTCErrorEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CTCErrorEvaluator : public NotGetableEvaluator {
int numTimes_, numClasses_, numSequences_, blank_;
real deletions_, insertions_, substitutions_;
int seqClassficationError_;
mutable std::unordered_map<std::string, real> evalResults_;

std::vector<int> path2String(const std::vector<int>& path) {
std::vector<int> str;
Expand Down Expand Up @@ -183,6 +184,18 @@ class CTCErrorEvaluator : public NotGetableEvaluator {
return stringAlignment(gtStr, recogStr);
}

void storeLocalValues() const {
evalResults_["error"] = numSequences_ ? totalScore_ / numSequences_ : 0;
evalResults_["deletion_error"] =
numSequences_ ? deletions_ / numSequences_ : 0;
evalResults_["insertion_error"] =
numSequences_ ? insertions_ / numSequences_ : 0;
evalResults_["substitution_error"] =
numSequences_ ? substitutions_ / numSequences_ : 0;
evalResults_["sequence_error"] =
(real)seqClassficationError_ / numSequences_;
}

public:
CTCErrorEvaluator()
: numTimes_(0),
Expand Down Expand Up @@ -245,16 +258,12 @@ class CTCErrorEvaluator : public NotGetableEvaluator {
}

virtual void printStats(std::ostream& os) const {
os << config_.name() << "="
<< (numSequences_ ? totalScore_ / numSequences_ : 0);
os << " deletions error"
<< "=" << (numSequences_ ? deletions_ / numSequences_ : 0);
os << " insertions error"
<< "=" << (numSequences_ ? insertions_ / numSequences_ : 0);
os << " substitutions error"
<< "=" << (numSequences_ ? substitutions_ / numSequences_ : 0);
os << " sequences error"
<< "=" << (real)seqClassficationError_ / numSequences_;
storeLocalValues();
os << config_.name() << "=" << evalResults_["error"];
os << " deletions error = " << evalResults_["deletion_error"];
os << " insertions error = " << evalResults_["insertion_error"];
os << " substitution error = " << evalResults_["substitution_error"];
os << " sequence error = " << evalResults_["sequence_error"];
}

virtual void distributeEval(ParameterClient2* client) {
Expand All @@ -272,6 +281,33 @@ class CTCErrorEvaluator : public NotGetableEvaluator {
seqClassficationError_ = (int)buf[4];
numSequences_ = (int)buf[5];
}

void getNames(std::vector<std::string>* names) {
storeLocalValues();
names->reserve(names->size() + evalResults_.size());
for (auto it = evalResults_.begin(); it != evalResults_.end(); ++it) {
names->push_back(config_.name() + "." + it->first);
}
}

real getValue(const std::string& name, Error* err) const {
storeLocalValues();

const std::string delimiter(".");
std::string::size_type foundPos = name.find(delimiter, 0);
CHECK(foundPos != std::string::npos);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

或许split好些,例如
https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/gserver/evaluators/Evaluator.cpp#L783, 然后取分割后的最后一个:

auto it = this->values_.find(buffers[buffers.size() - 1]);

避免name含有多个.


auto it = evalResults_.find(
name.substr(foundPos + delimiter.size(), name.length()));
if (it == evalResults_.end()) {
*err = Error("Evaluator does not have the key %s", name.c_str());
return 0.0f;
}

return it->second;
}

std::string getTypeImpl() const { return "ctc_edit_distance"; }
};

REGISTER_EVALUATOR(ctc_edit_distance, CTCErrorEvaluator);
Expand Down