1 /* Copyright 2018 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 // Copied from tensorflow/core/util/ctc/ctc_loss_util.h 17 // TODO(b/111524997): Remove this file. 18 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_KERNELS_CTC_LOSS_UTIL_H_ 19 #define TENSORFLOW_LITE_EXPERIMENTAL_KERNELS_CTC_LOSS_UTIL_H_ 20 21 #include <cmath> 22 #include <limits> 23 24 namespace tflite { 25 namespace experimental { 26 namespace ctc { 27 28 const float kLogZero = -std::numeric_limits<float>::infinity(); 29 30 // Add logarithmic probabilities using: 31 // ln(a + b) = ln(a) + ln(1 + exp(ln(b) - ln(a))) 32 // The two inputs are assumed to be log probabilities. 33 // (GravesTh) Eq. 7.18 LogSumExp(float log_prob_1,float log_prob_2)34inline float LogSumExp(float log_prob_1, float log_prob_2) { 35 // Always have 'b' be the smaller number to avoid the exponential from 36 // blowing up. 37 if (log_prob_1 == kLogZero && log_prob_2 == kLogZero) { 38 return kLogZero; 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 experimental 48 } // namespace tflite 49 50 #endif // TENSORFLOW_LITE_EXPERIMENTAL_KERNELS_CTC_LOSS_UTIL_H_ 51