python-3.x - Numba 在 np.astype 上无效使用 BoundFunction

标签 python-3.x numpy jit numba

我正在尝试编译一个函数,该函数使用 numba 对图像补丁进行一些计算。这是代码的一部分:

@jit(nopython=True, parallel=True)
def value_at_patch(img, coords, imgsize, patch_radius):
    x_center = coords[0]; y_center = coords[1];
    r = patch_radius
    s = 2*r+1
    xvec = np.arange(x_center-r, x_center+r+1)
    xvec[xvec <= 0] = 0 #prevent negative index
    xvec = xvec.astype(int)
    yvec = np.arange(y_center-r, y_center+r+1)
    yvec[yvec <= 0] = 0
    yvec = yvec.astype(int)
    A = np.zeros((s,s))

    #do some parallel computation on A

    p = np.any(A)
    return p

我能够编译该函数,但是当我运行它时,我收到以下错误消息:
Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of BoundFunction(array.astype for array(float64, 1d, C)) with parameters (Function(<class 'int'>))
 * parameterized
[1] During: resolving callee type: BoundFunction(array.astype for array(float64, 1d, C))
[2] During: typing of call at <ipython-input-17-90e27ac302a8> (42)


File "<ipython-input-17-90e27ac302a8>", line 42:
def value_at_patch(img, coords, imgsize, patch_radius):
    <source elided>
    xvec[xvec <= 0] = 0 #prevent negative index
    xvec = xvec.astype(int)
    ^

我检查了 numba 文档和 np.astype 应该只支持一个参数。你知道是什么导致了这个问题吗?

最佳答案

使用 np.int64代替 int在以下地方:

xvec = xvec.astype(np.int64)

yvec = yvec.astype(np.int64)

关于python-3.x - Numba 在 np.astype 上无效使用 BoundFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58714618/

相关文章:

python - 无法导入模块 'lambda_function' : cannot import name 'WinDLL' from 'ctypes' (/var/lang/lib/python3. 7/ctypes/__init__.py

python - 如何在 pandas 中获取满足特定条件的组中的第一项?

python - 如何将 numpy 数组转换为标准 TensorFlow 格式?

python - 在 Numpy 中编写滑动中位数的有效方法是什么?

python - 多进程: identify process in queue

python - 从脚本内更改目录后如何返回脚本目录?

python - 如何计算一系列阈值的聚合?

您可以动态编译和链接/加载 C 代码到 C 程序中吗?

typeerror - Numba - 类型错误 : 'type' object has no attribute '__getitem__

c# - CLR 会优化和内联这个 GetHashCode() 吗?