問題描述
使用@jit 裝飾器運行代碼時出現錯誤.似乎無法找到函數 scipy.special.gammainc() 的某些信息:
I am having an error when running code using the @jit decorator. It appears that some information for the function scipy.special.gammainc() can't be located:
Failed at nopython (nopython frontend)
Unknown attribute 'gammainc' for Module(<module 'scipy.special' from 'C:homeMinicondalibsite-packagesscipyspecial\__init__.pyc'>) $164.2 $164.3 = getattr(attr=gammainc, value=$164.2)
沒有@jit 裝飾器,代碼將運行良好.也許需要一些東西才能使 scipy.special 模塊的屬性對 Numba 可見?
Without the @jit decorator the code will run fine. Maybe there is something required to make the attributes of the scipy.special module visible to Numba?
提前感謝您的任何建議、意見等.
Thanks in advance for any suggestions, comments, etc.
推薦答案
問題是 gammainc
并不是 Numba 天生知道如何處理的一小部分函數之一(參見 http://numba.pydata.org/numba-doc/dev/reference/numpysupported.html) - 實際上沒有任何 scipy 函數.這意味著你不能在nopython"模式下使用它,不幸的是——它只需要把它當作一個普通的 python 函數調用.
The problem is that gammainc
isn't one of the small list of functions that Numba inherently knows how to deal with (see http://numba.pydata.org/numba-doc/dev/reference/numpysupported.html) - in fact none of the scipy functions are. This means you can't use it in "nopython" mode, unfortunately - it just has to treat it as a normal python function call.
如果您刪除 nopython=True
,它應該可以工作.然而,這并不是非常令人滿意,因為它可能會更慢.如果沒有看到您的代碼,很難知道確切的建議.但是,總的來說:
If you remove nopython=True
, it should work. However, that isn't hugely satisfactory, because it may well be slower. Without seeing your code it's difficult to know exact what to suggest. However, in general:
循環(不包含諸如
gammainc
之類的東西)將被加速,即使沒有 nopython.
loops (that don't contain things like
gammainc
) will be sped up, even without nopython.
gammainc
是一個ufunc",這意味著它可以輕松地一次應用于整個數組,并且無論如何都應該快速運行.
gammainc
is a "ufunc", which means it can be readily applied to a whole array at a time, and should run quickly anyway.
你可以調用func.inspect_types()
來查看它是否能夠編譯.
you can call func.inspect_types()
to see it's been able to compile.
舉個簡單的例子:
from scipy.special import gammainc
import numba as nb
import numpy as np
@nb.jit # note - no "nopython"
def f(x):
for n in range(x.shape[0]):
x[n] += 1
y = gammainc(x,2.5)
for n in range(y.shape[0]):
y[n] -= 1
return y
f(np.linspace(0,20)) # forces it to be JIT'd and outputs an array
然后 f.inspect_types()
將這兩個循環標識為提升循環",這意味著它們將被 JIT 處理并快速運行.gammainc
的位不是 JIT 的,而是一次應用于整個數組,因此也應該很快.
Then f.inspect_types()
identifies the two loops as "lifted loops", meaning they'll be JIT'd and run quickly. The bit with gammainc
is not JIT'd, but is applied to the whole array at once and so should be fast too.
這篇關于Python/Numba:scipy.special.gammainc()的未知屬性錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!