Avoid overflow with softplus function in python

前端 未结 4 680
不思量自难忘°
不思量自难忘° 2021-02-14 05:20

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

4条回答
  •  清酒与你
    2021-02-14 06:08

    What I'm currently using (slightly inefficient but clean and vectorized):

    def safe_softplus(x, limit=30):
        return np.where(x>limit, x, np.log1p(np.exp(x)))
    

提交回复
热议问题