• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 // LINT.IfChange
16 
17 // Collection of scoring classes that can be extended and provided to the
18 // CTCBeamSearchDecoder to incorporate additional scoring logic (such as a
19 // language model).
20 //
21 // To build a custom scorer extend and implement the pure virtual methods from
22 // BeamScorerInterface. The default CTC decoding behavior is implemented
23 // through BaseBeamScorer.
24 
25 #ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_
26 #define TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_
27 
28 #include "tensorflow/core/util/ctc/ctc_beam_entry.h"
29 
30 namespace tensorflow {
31 namespace ctc {
32 
33 // Base implementation of a beam scorer used by default by the decoder that can
34 // be subclassed and provided as an argument to CTCBeamSearchDecoder, if complex
35 // scoring is required. Its main purpose is to provide a thin layer for
36 // integrating language model scoring easily.
37 template <typename CTCBeamState>
38 class BaseBeamScorer {
39  public:
~BaseBeamScorer()40   virtual ~BaseBeamScorer() {}
41   // State initialization.
InitializeState(CTCBeamState * root)42   virtual void InitializeState(CTCBeamState* root) const {}
43   // ExpandState is called when expanding a beam to one of its children.
44   // Called at most once per child beam. In the simplest case, no state
45   // expansion is done.
ExpandState(const CTCBeamState & from_state,int from_label,CTCBeamState * to_state,int to_label)46   virtual void ExpandState(const CTCBeamState& from_state, int from_label,
47                            CTCBeamState* to_state, int to_label) const {}
48   // ExpandStateEnd is called after decoding has finished. Its purpose is to
49   // allow a final scoring of the beam in its current state, before resorting
50   // and retrieving the TopN requested candidates. Called at most once per beam.
ExpandStateEnd(CTCBeamState * state)51   virtual void ExpandStateEnd(CTCBeamState* state) const {}
52   // GetStateExpansionScore should be an inexpensive method to retrieve the
53   // (cached) expansion score computed within ExpandState. The score is
54   // multiplied (log-addition) with the input score at the current step from
55   // the network.
56   //
57   // The score returned should be a log-probability. In the simplest case, as
58   // there's no state expansion logic, the expansion score is zero.
GetStateExpansionScore(const CTCBeamState & state,float previous_score)59   virtual float GetStateExpansionScore(const CTCBeamState& state,
60                                        float previous_score) const {
61     return previous_score;
62   }
63   // GetStateEndExpansionScore should be an inexpensive method to retrieve the
64   // (cached) expansion score computed within ExpandStateEnd. The score is
65   // multiplied (log-addition) with the final probability of the beam.
66   //
67   // The score returned should be a log-probability.
GetStateEndExpansionScore(const CTCBeamState & state)68   virtual float GetStateEndExpansionScore(const CTCBeamState& state) const {
69     return 0;
70   }
71 };
72 
73 }  // namespace ctc
74 }  // namespace tensorflow
75 
76 #endif  // TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_
77 // LINT.ThenChange(//tensorflow/lite/experimental/kernels/ctc_beam_scorer.h)
78