避免在Python中使用softplus函数溢出 [英] Avoid overflow with softplus function in python
本文介绍了避免在Python中使用softplus函数溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现以下softplus功能:
I am trying to implement the following softplus function:
log(1 + exp(x))
我已经尝试使用math/numpy和float64作为数据类型,但是只要x
太大(例如x = 1000
),结果就是inf
.
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?
推荐答案
有一种关系可以使用:
log(1+exp(x)) = log(1+exp(x)) - log(exp(x)) + x = log(1+exp(-x)) + x
因此,从数学上讲,安全的实施方式应该是:
So a safe implementation, as well as mathematically sound, would be:
log(1+exp(-abs(x))) + max(x,0)
这既适用于数学函数又适用于numpy函数(例如:np.log,np.exp,np.abs,np.maximum).
This works both for math and numpy functions (use e.g.: np.log, np.exp, np.abs, np.maximum).
这篇关于避免在Python中使用softplus函数溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文