草庐IT

softplus

全部标签

python - 在 python 中使用 softplus 函数避免溢出

我正在尝试实现以下softplus功能:log(1+exp(x))我尝试过使用math/numpy和float64作为数据类型,但每当x变得太大(例如x=1000)时,结果就是inf.你能帮助我如何成功地处理这个函数吗? 最佳答案 TLDR:importnumpyasnpimportmathdefsoftplus_np(x):returnnp.log1p(np.exp(-np.abs(x)))+np.maximum(x,0)defsoftplus_math(x):returnmath.log1p(math.exp(-abs(x)))