【发布时间】:2021-04-09 06:51:17
【问题描述】:
我正在尝试学习如何使用 Numba 模块。到目前为止,由于与 NumPy 接口的一些问题,我还没有得到任何工作。这是我正在运行的代码(来自 Numba 文档)和我得到的错误:
from numba import jit
import numpy as np
x = np.arange(100).reshape(10, 10)
@jit(nopython=True) # Set "nopython" mode for best performance, equivalent to @njit
def go_fast(a): # Function is compiled to machine code when called the first time
trace = 0.0
for i in range(a.shape[0]): # Numba likes loops
trace += np.tanh(a[i, i]) # Numba likes NumPy functions
return a + trace # Numba likes NumPy broadcasting
print(go_fast(x))
Traceback (most recent call last):
File "C:/Users/JoHn/Documents/Current Classes/MEEN575_Optimization/HW6/Optimal_controller/angle_wrapping.py", line 84, in <module>
print(go_fast(x))
TypeError: expected dtype object, got 'numpy.dtype[float64]'
我从一些搜索中知道,这曾经是或现在是最近的一个已知错误,并且与 Numba 的新版本需要更新的 NumPy 或类似的东西有关,但据我所知,我有最新的NumPy 构建,版本 1.20。关于我做错了什么的任何提示?需要明确的是,我从来没有很好地理解如何在 python 中干净地设置环境,所以很可能我只是在这里遗漏了一些明显的东西。
【问题讨论】:
-
来自docs:
Successful type inference is a prerequisite for compilation in nopython mode。您应该指定function signature。 -
这正是您的工作示例吗?这应该开箱即用(您使用哪个 Numba 版本?)或者 x 是其他一些 dtype 对象数组?
-
我使用的是 0.45.1 版本,这个例子是从 numba 文档中逐行复制的,对我不起作用。