Avoid overflow when combining exponentials
Large-magnitude log-domain values
SciPy · Function
Computes the logarithm of a sum of exponentials in a numerically stable way. It supports axis reduction, broadcastable weights, singleton-dimension preservation, and optional sign output.
logsumexp evaluates the logarithm of a sum of exponentials while avoiding the direct overflow risks of forming those exponentials first. Reduce selected axes, optionally apply broadcastable weights—including negative weights—and request a separate sign when the weighted result may be nonpositive.
scipy.special.logsumexp(a, axis=None, b=None, keepdims=False, return_sign=False)| Name | Type | Description |
|---|---|---|
a | array_like | Input array. |
axis | None or int or tuple of ints, optional | Axis or axes over which the sum is taken. None, an integer, or a tuple of integers. |
b | array-like, optional | Scaling factors for exp(a), with the same shape as a or a broadcast-compatible shape. Negative values are allowed. |
keepdims | bool, optional | If True, retain reduced axes as dimensions of size one. |
return_sign | bool, optional | If True, return sign information along with the logarithmic magnitude. |
If return_sign is False, the logarithmic result is returned. If return_sign is True, a pair containing the logarithmic absolute value and sign information is returned.
import numpy as np
from scipy.special import logsumexp
a = np.array([1000.0, 1001.0])
print(logsumexp(a))
weights = np.array([1.0, -0.5])
log_abs, sign = logsumexp(a, b=weights, return_sign=True)
print(log_abs, sign)Computes a stable log-sum-exp and separately returns the sign for a weighted sum that can be negative.
Large-magnitude log-domain values
Weighted likelihoods or signed terms
Tensor reductions
It computes log(sum(exp(a))) , with optional weights through b. The routine is useful when exponentials could overflow, because it evaluates the expression in a numerically stable form.
Follow-up: How would you represent a negative weighted sum? Use return_sign=True; the result contains the log of the absolute value and a separate sign value.
axis selects the dimensions reduced by the sum. It accepts None, one integer, or a tuple of integers; None reduces all elements.
Follow-up: How can the result retain reduced dimensions for broadcasting? Set keepdims=True, which leaves those axes with size one.
b supplies scaling factors for exp(a), and it must match a's shape or be broadcastable to it. Negative factors are allowed, so b can express subtraction as well as weighted addition.
Follow-up: What happens if the weighted sum is negative and return_sign is false? The returned result is NaN; request the sign explicitly to avoid losing that information.
scipy.special.softmaxscipy.special.log_softmaxnumpy.logaddexp