提交 73c3a602 编写于 作者: Y Yibing Liu

Add the scoring of last word/char of prefixes in CTC beam search decoder

上级 8bd02c42
#!/usr/bin/env bash
set -e
readonly VERSION="3.9"
readonly VERSION="3.6"
version=$(clang-format -version)
......
......@@ -110,17 +110,17 @@ std::vector<std::pair<double, std::string>> ctc_beam_search_decoder(
// language model scoring
if (ext_scorer != nullptr &&
(c == space_id || ext_scorer->is_character_based())) {
PathTrie *prefix_toscore = nullptr;
PathTrie *prefix_to_score = nullptr;
// skip scoring the space
if (ext_scorer->is_character_based()) {
prefix_toscore = prefix_new;
prefix_to_score = prefix_new;
} else {
prefix_toscore = prefix;
prefix_to_score = prefix;
}
double score = 0.0;
float score = 0.0;
std::vector<std::string> ngram;
ngram = ext_scorer->make_ngram(prefix_toscore);
ngram = ext_scorer->make_ngram(prefix_to_score);
score = ext_scorer->get_log_cond_prob(ngram) * ext_scorer->alpha;
log_p += score;
log_p += ext_scorer->beta;
......@@ -131,6 +131,7 @@ std::vector<std::pair<double, std::string>> ctc_beam_search_decoder(
} // end of loop over prefix
} // end of loop over vocabulary
prefixes.clear();
// update log probs
root.iterate_to_vec(prefixes);
......@@ -147,6 +148,23 @@ std::vector<std::pair<double, std::string>> ctc_beam_search_decoder(
}
} // end of loop over time
// score the last word/character of each prefix
if (ext_scorer != nullptr) {
for (size_t i = 0; i < beam_size && i < prefixes.size(); ++i) {
auto prefix = prefixes[i];
if (prefix->character != space_id && !prefix->is_empty()) {
float score = 0.0;
std::vector<std::string> ngram = ext_scorer->make_ngram(prefix);
score = ext_scorer->get_log_cond_prob(ngram) * ext_scorer->alpha;
score += ext_scorer->beta;
prefix->score += score;
}
}
}
size_t num_prefixes = std::min(prefixes.size(), beam_size);
std::sort(prefixes.begin(), prefixes.begin() + num_prefixes, prefix_compare);
// compute aproximate ctc score as the return score, without affecting the
// return order of decoding result. To delete when decoder gets stable.
for (size_t i = 0; i < beam_size && i < prefixes.size(); ++i) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册