Python程序数据溢出问题或出现 NAN 问题

【数据溢出问题】

overflow:溢出 

  • overflow:上溢
  • underflow:下溢 
数据溢出包括上溢和下溢。
上溢可以理解为:你想用一个int类型来保存一个非常非常大的数,而这个超出了int类型所能表示的最大的数的范围。
下溢同理:你要用double来表示一个非常非常小的数,超出它所能表示的最小数时,就会发生数据溢出错误。
 

【如何避免Python程序下溢问题】

浮点数的下溢一般是由很多很小的数(如很多独立同分布数据的概率相乘)的连乘造成的,连乘后的数值有时候会达到负几十万个指数级。
 
其中一个解决办法是:对这个累乘的乘积 取自然对数,可以将连乘化为求和:
在对数中有:
 

 

这样即可避免浮点数的下溢,但是要记得最后要 取回来(取对数)。
 
 
 
【老板给: Computing Log-Sum-Exp180711
  The "nan problem" is not due to the inference method but instead to a poor implementation of it. Since machines have limited precision, certain types of calculations involving many numbers in a different scale tend to underflow or overflow.
  If you have written the inference code by yourself, please make sure you are not doing products of probabilities to compute likelihoods, but instead, sum of logarithms. And even that, make sure your log-likelihood does not become -infinity (resulting in a "nan" value).
 
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值