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