Home | History | Annotate | Download | only in ctc
      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 #ifndef TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_UTIL_H_
     17 #define TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_UTIL_H_
     18 
     19 #include <cmath>
     20 #include <limits>
     21 
     22 namespace tensorflow {
     23 namespace ctc {
     24 
     25 const float kLogZero = -std::numeric_limits<float>::infinity();
     26 
     27 // Add logarithmic probabilities using:
     28 // ln(a + b) = ln(a) + ln(1 + exp(ln(b) - ln(a)))
     29 // The two inputs are assumed to be log probabilities.
     30 // (GravesTh) Eq. 7.18
     31 inline float LogSumExp(float log_prob_1, float log_prob_2) {
     32   // Always have 'b' be the smaller number to avoid the exponential from
     33   // blowing up.
     34   if (log_prob_1 == kLogZero && log_prob_2 == kLogZero) {
     35     return kLogZero;
     36   } else {
     37     return (log_prob_1 > log_prob_2)
     38                ? log_prob_1 + log1pf(expf(log_prob_2 - log_prob_1))
     39                : log_prob_2 + log1pf(expf(log_prob_1 - log_prob_2));
     40   }
     41 }
     42 
     43 }  // namespace ctc
     44 }  // namespace tensorflow
     45 
     46 #endif  // TENSORFLOW_CORE_UTIL_CTC_CTC_LOSS_UTIL_H_
     47