gpt4 book ai didi

python - numba 的 Scipy 特殊函数

转载 作者:行者123 更新时间:2023-12-04 03:58:42 24 4
gpt4 key购买 nike

此代码失败:

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那个 xlogy可能会重载。安 answer from 2015说这不能完成(对于其他一些功能),但也许我认为它改变了。
当然,我可以自己实现它,但使用已知的解决方案会很好。
编辑:
根据下面 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相反,来自 numba 本身的创造者 ( link to pypi )。对包使用第一方扩展总是一个好主意。

very documentation you linkednumba_special 有关.如果你看 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.

关于python - numba 的 Scipy 特殊函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63434971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com