I am trying to implement the following softplus function:
log(1 + exp(x))
I've tried it with math/numpy and float64 as data type, but whenever x
gets too large (e.g. x = 1000
) the result is inf
.
Can you assist me on how to successfully handle this function with large numbers?
Since for
x>30
we havelog(1+exp(x)) ~= log(exp(x)) = x
, a simple stable implementation isIn fact
| log(1+exp(30)) - 30 | < 1e-10
, so this implementation makes errors smaller than1e-10
and never overflows. In particular for x=1000 the error of this approximation will be much smaller than float64 resolution, so it is impossible to even measure it on the computer.i use this code to work in arrays
There is a relation which one can use:
So a safe implementation, as well as mathematically sound, would be:
This works both for math and numpy functions (use e.g.: np.log, np.exp, np.abs, np.maximum).