MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / Step

Method Step

tensorflow/core/util/ctc/ctc_beam_search.h:249–382  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

247template <typename T, typename CTCBeamState, typename CTCBeamComparer>
248template <typename Vector>
249void CTCBeamSearchDecoder<T, CTCBeamState, CTCBeamComparer>::Step(
250 const Vector& raw_input) {
251 std::vector<T> top_k_logits;
252 std::vector<int> top_k_indices;
253 const bool top_k =
254 (label_selection_size_ > 0 && label_selection_size_ < raw_input.size());
255 // Number of character classes to consider in each step.
256 const int max_classes =
257 top_k ? label_selection_size_ : (this->num_classes_ - 1);
258 // Get max coefficient and remove it from raw_input later.
259 T max_coeff;
260 if (top_k) {
261 max_coeff = GetTopK(label_selection_size_, raw_input, &top_k_logits,
262 &top_k_indices);
263 } else {
264 max_coeff = raw_input.maxCoeff();
265 }
266 // Get normalization term of softmax: log(sum(exp(logit[j]-max_coeff))).
267 T logsumexp = T(0.0);
268 for (int j = 0; j < raw_input.size(); ++j) {
269 logsumexp += Eigen::numext::exp(raw_input(j) - max_coeff);
270 }
271 logsumexp = Eigen::numext::log(logsumexp);
272 // Final normalization offset to get correct log probabilities.
273 T norm_offset = max_coeff + logsumexp;
274
275 const T label_selection_input_min =
276 (label_selection_margin_ >= 0) ? (max_coeff - label_selection_margin_)
277 : -std::numeric_limits<T>::infinity();
278
279 // Extract the beams sorted in decreasing new probability
280 CHECK_EQ(this->num_classes_, raw_input.size());
281
282 std::unique_ptr<std::vector<BeamEntry*>> branches(leaves_.Extract());
283 leaves_.Reset();
284
285 for (BeamEntry* b : *branches) {
286 // P(.. @ t) becomes the new P(.. @ t-1)
287 b->oldp = b->newp;
288 }
289
290 for (BeamEntry* b : *branches) {
291 if (b->parent != nullptr) { // if not the root
292 if (b->parent->Active()) {
293 // If last two sequence characters are identical:
294 // Plabel(l=acc @ t=6) = (Plabel(l=acc @ t=5)
295 // + Pblank(l=ac @ t=5))
296 // else:
297 // Plabel(l=abc @ t=6) = (Plabel(l=abc @ t=5)
298 // + P(l=ab @ t=5))
299 T previous = (b->label == b->parent->label) ? b->parent->oldp.blank
300 : b->parent->oldp.total;
301 b->newp.label =
302 LogSumExp(b->newp.label,
303 beam_scorer_->GetStateExpansionScore(b->state, previous));
304 }
305 // Plabel(l=abc @ t=6) *= P(c @ 6)
306 b->newp.label += raw_input(b->label) - norm_offset;

Callers

nothing calls this directly

Calls 12

infinityFunction · 0.85
GetChildMethod · 0.80
LogSumExpFunction · 0.70
expClass · 0.50
logClass · 0.50
sizeMethod · 0.45
ExtractMethod · 0.45
ResetMethod · 0.45
ActiveMethod · 0.45
pushMethod · 0.45
ExpandStateMethod · 0.45

Tested by

no test coverage detected