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 16 // Collection of scoring classes that can be extended and provided to the 17 // CTCBeamSearchDecoder to incorporate additional scoring logic (such as a 18 // language model). 19 // 20 // To build a custom scorer extend and implement the pure virtual methods from 21 // BeamScorerInterface. The default CTC decoding behavior is implemented 22 // through BaseBeamScorer. 23 24 #ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_ 25 #define TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_ 26 27 #include "tensorflow/core/util/ctc/ctc_beam_entry.h" 28 29 namespace tensorflow { 30 namespace ctc { 31 32 // Base implementation of a beam scorer used by default by the decoder that can 33 // be subclassed and provided as an argument to CTCBeamSearchDecoder, if complex 34 // scoring is required. Its main purpose is to provide a thin layer for 35 // integrating language model scoring easily. 36 template <typename CTCBeamState> 37 class BaseBeamScorer { 38 public: 39 virtual ~BaseBeamScorer() {} 40 // State initialization. 41 virtual void InitializeState(CTCBeamState* root) const {} 42 // ExpandState is called when expanding a beam to one of its children. 43 // Called at most once per child beam. In the simplest case, no state 44 // expansion is done. 45 virtual void ExpandState(const CTCBeamState& from_state, int from_label, 46 CTCBeamState* to_state, int to_label) const {} 47 // ExpandStateEnd is called after decoding has finished. Its purpose is to 48 // allow a final scoring of the beam in its current state, before resorting 49 // and retrieving the TopN requested candidates. Called at most once per beam. 50 virtual void ExpandStateEnd(CTCBeamState* state) const {} 51 // GetStateExpansionScore should be an inexpensive method to retrieve the 52 // (cached) expansion score computed within ExpandState. The score is 53 // multiplied (log-addition) with the input score at the current step from 54 // the network. 55 // 56 // The score returned should be a log-probability. In the simplest case, as 57 // there's no state expansion logic, the expansion score is zero. 58 virtual float GetStateExpansionScore(const CTCBeamState& state, 59 float previous_score) const { 60 return previous_score; 61 } 62 // GetStateEndExpansionScore should be an inexpensive method to retrieve the 63 // (cached) expansion score computed within ExpandStateEnd. The score is 64 // multiplied (log-addition) with the final probability of the beam. 65 // 66 // The score returned should be a log-probability. 67 virtual float GetStateEndExpansionScore(const CTCBeamState& state) const { 68 return 0; 69 } 70 }; 71 72 } // namespace ctc 73 } // namespace tensorflow 74 75 #endif // TENSORFLOW_CORE_UTIL_CTC_CTC_BEAM_SCORER_H_ 76