user961627
user961627

Reputation: 12747

Python OverflowError: math range error being raised differently in different runs

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.

Upvotes: 0

Views: 6453

Answers (2)

abarnert
abarnert

Reputation: 365717

Your overflow is almost certainly a real overflow; one of your values is too large to fit in a Python float (meaning a C double).


So, why does it happen in different places each time?

Because you're using Numba.

Numba JIT-compiles your code. If it detects that there's no contention, it can reorder your code—or even, at least in theory, run it in parallel on multiple cores or on the GPU (although I think at present you only get GPU computation if you code it explicitly with numba.cuda).

At any rate, this means that the path through your code can be nondeterministic. If there's more than one place an overflow could happen, you can't predict which one will fail and trigger the exception.


At any rate, that doesn't really matter. If your calculations are overflowing, you have to fix that. And the fact that different ones overflow each time shouldn't make it that much harder to debug—especially given that it apparently usually happens in a single place, just not always.

Upvotes: 1

Robᵩ
Robᵩ

Reputation: 168626

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

Upvotes: 2

Related Questions