python如何区别不同错误_Python OverflowError:数学范围错误在不同的运行中被不同地引发...

1586010002-jmsa.png

My program seems to be crashing almost arbitrarily.

My code includes these two lines:

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

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

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.

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

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])).

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.

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

Edit

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.

解决方案

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.

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值