espnet2.asr.transducer.rnnt_multi_blank.utils.rnnt_helper.log_plus
Less than 1 minute
espnet2.asr.transducer.rnnt_multi_blank.utils.rnnt_helper.log_plus
espnet2.asr.transducer.rnnt_multi_blank.utils.rnnt_helper.log_plus(p1: float, p2: float)
Compute the log of the sum of exponentials of two values.
This function calculates the logarithm of the sum of exponentials of two input probabilities, p1 and p2, using a numerically stable approach. This is particularly useful in applications such as log-probability calculations in machine learning, where directly computing the sum of exponentials can lead to overflow issues.
- Parameters:
- p1 (float) – The first probability value, which may be negative infinity.
- p2 (float) – The second probability value, which may be negative infinity.
- Returns: The log of the sum of exponentials of p1 and p2.
- Return type: float
Examples
>>> log_plus(0.5, 0.5)
0.6931471805599453 # log(2) = log(0.5 + 0.5)
>>> log_plus(-float('inf'), 1.0)
1.0 # log(0 + exp(1.0)) = 1.0
>>> log_plus(-1.0, -2.0)
-0.3132616875182228 # log(exp(-1.0) + exp(-2.0))
NOTE
If either p1 or p2 is negative infinity, the other value will be returned directly.