r"""Calculates the logarithm of the inputs' exponential sum along the given :attr:`axis`. .. math:: \text{logsumexp}(x)= \log \sum_{j=1}^{ n} \exp \left(x_{ j}\right) For numerical stability, the implementation follows this transformation: .. math:: \text{log
(
inp: Tensor, axis: Union[int, Sequence[int]], keepdims: bool = False
)
| 972 | |
| 973 | |
| 974 | def logsumexp( |
| 975 | inp: Tensor, axis: Union[int, Sequence[int]], keepdims: bool = False |
| 976 | ) -> Tensor: |
| 977 | r"""Calculates the logarithm of the inputs' exponential sum along the given :attr:`axis`. |
| 978 | |
| 979 | .. math:: |
| 980 | |
| 981 | \text{logsumexp}(x)= \log \sum_{j=1}^{ |
| 982 | n} \exp \left(x_{ |
| 983 | j}\right) |
| 984 | |
| 985 | For numerical stability, the implementation follows this transformation: |
| 986 | |
| 987 | .. math:: |
| 988 | |
| 989 | \text{logsumexp}(x)= \log \sum_{j=1}^{ |
| 990 | n} \exp \left(x_{ |
| 991 | j}\right) |
| 992 | = \text{logsumexp}(x)=b+\log \sum_{j=1}^{ |
| 993 | n} \exp \left(x_{j}-b\right) |
| 994 | |
| 995 | where |
| 996 | |
| 997 | .. math:: |
| 998 | b = \max(x_j) |
| 999 | |
| 1000 | Examples: |
| 1001 | >>> import numpy as np |
| 1002 | >>> x = Tensor(np.arange(-5, 5, dtype=np.float32)).reshape(2,5) |
| 1003 | >>> y = F.logsumexp(x, axis=1, keepdims=False) |
| 1004 | >>> y.numpy().round(decimals=4) |
| 1005 | array([-0.5481, 4.4519], dtype=float32) |
| 1006 | """ |
| 1007 | max_value = max(inp.detach(), axis, keepdims=True) |
| 1008 | if keepdims: |
| 1009 | return max_value + log(sum(exp(inp - max_value), axis, keepdims)) |
| 1010 | else: |
| 1011 | return squeeze(max_value, axis=None) + log( |
| 1012 | sum(exp(inp - max_value), axis, keepdims) |
| 1013 | ) |
| 1014 | |
| 1015 | |
| 1016 | def _get_softmax_axis(ndim: int) -> int: |