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).
 
 
原文地址:https://www.cnblogs.com/shenxiaolin/p/8619112.html