使用numba时引发异常

时间:2014-09-13 09:04:43

标签: python numpy overflow numba raiserror

here跟进,我不断溢出。因此,我试图提出异常,以便确切地知道哪里出了问题。

我有这样的事情:

@jit
def train_function(X, y, H):
     np.seterr(over="raise", under="raise", invalid="raise")
     # do some stuff, start a double loop, and then do:
     try: 
            z[i,j] = math.exp(-beta[j,i])
     except OverflowError:
            print "Calculation failed! z[i,j] = math.exp(-beta[j,i]), j: " + str(j) + ", i: " +str(i) + ", b: " + str(beta[j,i]) + ", omb: " + str(oneminusbeta[j,i])
            raise    


class MyClass(object):
     # init and other methods
     def train(self, X, y, H):
          train_function(X, y, H)

但是我收到了这个错误:

Traceback (most recent call last):
  File "C:\work_asaaki\code\gbc_classifier_train_7.py", line 55, in <module>
    gentlebooster.train(X_train, y_train, boosting_rounds)
  File "C:\work_asaaki\code\gentleboost_c_class_jit_v7_nolimit.py", line 297, in train
    self.g_per_round, self.g = train_function(X, y, H)  
  File "C:\Anaconda\lib\site-packages\numba\dispatcher.py", line 152, in _compile_for_args
    return self.jit(sig)
  File "C:\Anaconda\lib\site-packages\numba\dispatcher.py", line 143, in jit
    return self.compile(sig, **kws)
  File "C:\Anaconda\lib\site-packages\numba\dispatcher.py", line 131, in compile
    flags=flags, locals=locs)
  File "C:\Anaconda\lib\site-packages\numba\compiler.py", line 103, in compile_extra
    bc = bytecode.ByteCode(func=func)
  File "C:\Anaconda\lib\site-packages\numba\bytecode.py", line 305, in __init__
    table = utils.SortedMap(ByteCodeIter(code))
  File "C:\Anaconda\lib\site-packages\numba\utils.py", line 70, in __init__
    for i, (k, v) in enumerate(sorted(seq)):
  File "C:\Anaconda\lib\site-packages\numba\bytecode.py", line 219, in next
    raise NotImplementedError(ts % tv)
NotImplementedError: offset=742 opcode=0x79 opname=SETUP_EXCEPT

在我使用numba时,我不能提出异常吗?我在64位计算机上使用带有Numba 0.13.x和Numpy 1.8.x的Anaconda 2.0.1。

1 个答案:

答案 0 :(得分:3)

http://numba.pydata.org/numba-doc/dev/reference/pysupported.html

2.6.1.1。构建体

  

Numba努力支持尽可能多的Python语言,但Numba编译的函数中没有一些语言功能。目前不支持以下Python语言功能:

Class definition
Exception handling (try .. except, try .. finally)
Context management (the with statement)
  

以几种形式支持raise语句:

raise (to re-raise the current exception)
raise SomeException
raise SomeException(<arguments>)

让我们离开这里:

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

关于exp(-1000)的任何负面消息;非常小的将评估为零而没有溢出

math.exp(-1000000000)“有效”,可能不是你的问题(虽然它会返回0.0,但不是“真的”为零)

那会导致失败的原因是什么?我们知道:

print(math.exp(100))
>>>
2.6881171418161356e+43

愚蠢的大,远远超过......可能溢出

果然

print(math.exp(1000))
>>>
OverflowError: math range error

我没有引用,但我认为有效范围是-700到700,从双浮点的角度有效地评估为0和无穷大(溢出)

处理我们窗口函数:

n = beta
if n > 100:
    n = 100
z = math.exp(n)

但是这不起作用,因为math.exp(n)只接受浮点数而你的beta似乎是一个列表;你必须使用numpy.exp(n)和numpy.clip()到窗口

b = numpy.array(-beta[j,i])
n = numpy.clip(b, a_max=100)
z = numpy.exp(n)

或者引发溢出异常:

b = numpy.array(-beta[j,i])
n = numpy.clip(b, a_max=100)
if b != n:
    print (j,i,-beta[j,i])
    raise OverflowError
else:
    z = numpy.exp(n)