Python OverflowError:数学范围错误在不同的运行中以不同的方式引发 [英] Python OverflowError: math range error being raised differently in different runs

查看:953
本文介绍了Python OverflowError:数学范围错误在不同的运行中以不同的方式引发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序似乎几乎崩溃了.

My program seems to be crashing almost arbitrarily.

我的代码包括这两行:

z[i, j] = -math.exp(oneminusbeta[j, i])
weights[i,j] = math.exp(beta[j,i] + oneminusbeta[j,i])

我之前已经对具有2维的数据运行了我的整个代码,这是7341 x 648.运行该代码我没有任何问题.

I've run my whole code before on data that had 2 dimensions, it was 7341 x 648. I had no issues at all running that code.

但是现在我正在使用的数据大约是它的十倍.它是71678 x 648,并且出现此错误:

But now the data I'm using is about ten times as big. It's 71678 x 648, and I'm getting this error:

OverflowError: math range error

我在任何特定点上都没有.我在每行代码之前记录注释,以便我可以了解导致崩溃的原因,并且看来崩溃在上述第二行(weights[i,j] = math.exp(beta[j,i] + oneminusbeta[j,i]))上的发生频率更高.

And I'm not getting this on any specific point. I'm logging comments before every line of code so that I can see what's causing the crash, and it appears the crash is happening more often on the second line mentioned above (weights[i,j] = math.exp(beta[j,i] + oneminusbeta[j,i])).

问题是,它在不同时间崩溃. 最初,它在weights[30816, 42]崩溃了.然后在weights[55399, 43].然后在z[33715,45].但是,这三种情况下的数据都是相同的.

The thing is, it crashes at different times. At first, it crashed at weights[30816, 42]. Then at weights[55399, 43]. Then at z[33715,45]. But the data is the same in all 3 cases.

可能是什么问题?这是python与内存相关的问题吗?顺便说一下,我也在使用Numba.

What could the problem be? Is this a memory related issue with python? I'm using Numba too, by the way.

我忘了提一下,我已经设置了阈值,以使exp()函数中使用的阈值不超过709或-708,因此从技术上讲不应出现溢出.

I forgot to mention, I've put thresholds so that what goes into the exp() function doesn't exceed what 709 or -708, so technically there shouldn't be an overflow.

推荐答案

计算结果无法在计算机上显示.这可能意味着math.exp(...)大于大约10 308 ,或者传递给math.exp()的参数大于大约710.

The result of your calculation cannot be represented on your computer. This probably means that math.exp(...) is greater than about 10308, or the argument passed to math.exp() is greater than about 710.

尝试在每次计算之前打印beta[j,i]oneminusbeta[j,i]的值.

Try printing the values of beta[j,i] and oneminusbeta[j,i] before each calculation.

实际上,您不必在每行代码之前都打印注释.相反,尝试使用try块包装计算,如下所示:

In fact, you don't have to print comments before every line of code. Instead, try wrapping the calculations with a try block, like so:

try:
  weights[i,j] = math.exp(beta[j,i] + oneminusbeta[j,i])
except OverflowError:
  print "Calculation failed! j=%d i=%d beta=%f oneminusbeta=%f"%(j,i,beta[j,i],oneminusbeta[j,i])
  raise

这篇关于Python OverflowError:数学范围错误在不同的运行中以不同的方式引发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆