Scipy numba 的特殊功能
Scipy special functions with numba
此代码失败:
import numpy as np
from numba import jit
import scipy.special as sp
@jit(nopython=True)
def f(a):
return sp.xlogy(a, a)
a = np.array([1,2,0,1], dtype=float)
f(a)
出现如下错误
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'xlogy' of type Module(<module 'scipy.special' from '/home/yair/.local/lib/python3.8/site-packages/scipy/special/__init__.py'>)
但是,我相信它应该可以工作,因为它说 here that xlogy
could be overloaded. An 说这不能完成(对于某些其他功能)但也许我认为它改变了。
当然,我可以自己实现,但最好使用已知的解决方案。
编辑:
根据下面 Andras Deak 的建议,我尝试使用 numba-scipy
。我无法让它工作,希望有一些使用示例。这是我的尝试 - 它们都失败了,请参阅以下错误:
import numpy as np
from numba import jit
import numba_scipy
import scipy as sp
from scipy import special as spc
@jit(nopython=True)
def f1(a):
return numba_scipy.special.xlogy(a,a)
@jit(nopython=True)
def f2(a):
return numba_scipy.xlogy(a,a)
@jit(nopython=True)
def f3(a):
return sp.special.xlogy(a,a)
@jit(nopython=True)
def f4(a):
return spc.xlogy(a,a)
a = np.array([0,1,2])
try:
f1(a)
except Exception as e:
print('f1 fails')
print(e)
try:
f2(a)
except Exception as e:
print('f2 fails')
print(e)
try:
f3(a)
except Exception as e:
print('f3 fails')
print(e)
try:
f4(a)
except Exception as e:
print('f4 fails')
print(e)
完整的错误信息:
/home/yair/.local/lib/python3.8/site-packages/numba/core/dispatcher.py:238: UserWarning: Numba extension module 'numba_scipy' failed to load due to 'ValueError(No function '__pyx_fuse_0pdtr' found in __pyx_capi__ of 'scipy.special.cython_special')'.
entrypoints.init_all()
f1 fails
Failed in nopython mode pipeline (step: nopython rewrites)
module 'numba_scipy' has no attribute 'special'
f2 fails
Failed in nopython mode pipeline (step: nopython rewrites)
module 'numba_scipy' has no attribute 'xlogy'
f3 fails
Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'xlogy' of type Module(<module 'scipy.special' from '/home/yair/.local/lib/python3.8/site-packages/scipy/special/__init__.py'>)
File "example.py", line 19:
def f3(a):
return sp.special.xlogy(a,a)
^
During: typing of get attribute at example.py (19)
File "example.py", line 19:
def f3(a):
return sp.special.xlogy(a,a)
^
f4 fails
Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'xlogy' of type Module(<module 'scipy.special' from '/home/yair/.local/lib/python3.8/site-packages/scipy/special/__init__.py'>)
File "example.py", line 23:
def f4(a):
return spc.xlogy(a,a)
^
During: typing of get attribute at example.py (23)
File "example.py", line 23:
def f4(a):
return spc.xlogy(a,a)
^
由于您根据评论遇到安装问题,我特别建议使用 numba-scipy instead, from the creators of numba itself (link to pypi)。使用 first-party 包扩展总是一个好主意。
very documentation you linked relates to numba_special
. If you look at the main page你会看到第一个例子:
>>> import numba
>>> import scipy.special as sc
>>> import numba_special # The import generates Numba overloads for special
>>> @numba.njit
... def gamma_plus_1(x):
... return sc.gamma(x) + 1.0
...
>>> gamma_plus_1(5.0)
25.0
请注意您遗漏的 import numba_special
行。
您还必须先安装 numba-special。另请注意项目页面上的警告:
Installing
Numba special depends on SciPy and Numba. Until the fix for this issue is included in a Numba release, numba_special must be installed against the master branch of Numba.
你是从scipy.special调用xlogy函数,其实对于@jit修饰函数调用的函数也需要@jit修饰。
所以这就是为什么 xlogy 与您的 f 函数和 numba 抛出错误不兼容。解决方法可能是:
import numpy as np
from numba import jit
@jit(nopython=True)
def myxlogy(x,y):
return x*np.log(y)
@jit(nopython=True)
def f(a):
return myxlogy(a,a)
a = np.array([1,2,0,1], dtype=float)
f(a)
>>array([0. , 1.38629436, nan, 0. ])
此代码失败:
import numpy as np
from numba import jit
import scipy.special as sp
@jit(nopython=True)
def f(a):
return sp.xlogy(a, a)
a = np.array([1,2,0,1], dtype=float)
f(a)
出现如下错误
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'xlogy' of type Module(<module 'scipy.special' from '/home/yair/.local/lib/python3.8/site-packages/scipy/special/__init__.py'>)
但是,我相信它应该可以工作,因为它说 here that xlogy
could be overloaded. An
当然,我可以自己实现,但最好使用已知的解决方案。
编辑:
根据下面 Andras Deak 的建议,我尝试使用 numba-scipy
。我无法让它工作,希望有一些使用示例。这是我的尝试 - 它们都失败了,请参阅以下错误:
import numpy as np
from numba import jit
import numba_scipy
import scipy as sp
from scipy import special as spc
@jit(nopython=True)
def f1(a):
return numba_scipy.special.xlogy(a,a)
@jit(nopython=True)
def f2(a):
return numba_scipy.xlogy(a,a)
@jit(nopython=True)
def f3(a):
return sp.special.xlogy(a,a)
@jit(nopython=True)
def f4(a):
return spc.xlogy(a,a)
a = np.array([0,1,2])
try:
f1(a)
except Exception as e:
print('f1 fails')
print(e)
try:
f2(a)
except Exception as e:
print('f2 fails')
print(e)
try:
f3(a)
except Exception as e:
print('f3 fails')
print(e)
try:
f4(a)
except Exception as e:
print('f4 fails')
print(e)
完整的错误信息:
/home/yair/.local/lib/python3.8/site-packages/numba/core/dispatcher.py:238: UserWarning: Numba extension module 'numba_scipy' failed to load due to 'ValueError(No function '__pyx_fuse_0pdtr' found in __pyx_capi__ of 'scipy.special.cython_special')'.
entrypoints.init_all()
f1 fails
Failed in nopython mode pipeline (step: nopython rewrites)
module 'numba_scipy' has no attribute 'special'
f2 fails
Failed in nopython mode pipeline (step: nopython rewrites)
module 'numba_scipy' has no attribute 'xlogy'
f3 fails
Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'xlogy' of type Module(<module 'scipy.special' from '/home/yair/.local/lib/python3.8/site-packages/scipy/special/__init__.py'>)
File "example.py", line 19:
def f3(a):
return sp.special.xlogy(a,a)
^
During: typing of get attribute at example.py (19)
File "example.py", line 19:
def f3(a):
return sp.special.xlogy(a,a)
^
f4 fails
Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'xlogy' of type Module(<module 'scipy.special' from '/home/yair/.local/lib/python3.8/site-packages/scipy/special/__init__.py'>)
File "example.py", line 23:
def f4(a):
return spc.xlogy(a,a)
^
During: typing of get attribute at example.py (23)
File "example.py", line 23:
def f4(a):
return spc.xlogy(a,a)
^
由于您根据评论遇到安装问题,我特别建议使用 numba-scipy instead, from the creators of numba itself (link to pypi)。使用 first-party 包扩展总是一个好主意。
very documentation you linked relates to numba_special
. If you look at the main page你会看到第一个例子:
>>> import numba
>>> import scipy.special as sc
>>> import numba_special # The import generates Numba overloads for special
>>> @numba.njit
... def gamma_plus_1(x):
... return sc.gamma(x) + 1.0
...
>>> gamma_plus_1(5.0)
25.0
请注意您遗漏的 import numba_special
行。
您还必须先安装 numba-special。另请注意项目页面上的警告:
Installing
Numba special depends on SciPy and Numba. Until the fix for this issue is included in a Numba release, numba_special must be installed against the master branch of Numba.
你是从scipy.special调用xlogy函数,其实对于@jit修饰函数调用的函数也需要@jit修饰。 所以这就是为什么 xlogy 与您的 f 函数和 numba 抛出错误不兼容。解决方法可能是:
import numpy as np
from numba import jit
@jit(nopython=True)
def myxlogy(x,y):
return x*np.log(y)
@jit(nopython=True)
def f(a):
return myxlogy(a,a)
a = np.array([1,2,0,1], dtype=float)
f(a)
>>array([0. , 1.38629436, nan, 0. ])