megengine.functional.nn.logsumexp¶
- logsumexp(inp, axis, keepdims=False)[源代码]¶
计算输入数据沿给定维度
axis
的指数之和的对数。该计算是数值稳定的。\[\text{logsumexp}(x)= \log \sum_{j=1}^{n} \exp \left(x_{j}\right)\]为了提高数值稳定性,实现根据以下的变换:
\[\text{logsumexp}(x)= \log \sum_{j=1}^{n} \exp \left(x_{j}\right) = \text{logsumexp}(x)=b+\log \sum_{j=1}^{n} \exp \left(x_{j}-b\right)\]式中,
\[b = \max(x_j)\]实际案例
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(-5, 5, dtype=np.float32)).reshape(2,5) y = F.logsumexp(x, axis=1, keepdims=False) print(y.numpy().round(decimals=4))
输出:
[-0.5481 4.4519]
- 返回类型