【发布时间】:2021-06-15 04:30:59
【问题描述】:
我刚开始熟悉如何使用 Numba 加速我的代码,所以我正在测试非常简单的函数:
import numpy as np
from numba import jit
from time import time
@jit(nopython = True)
def dotter(X):
Z = np.dot(X,X.T)
return(Z)
tic = time()
dotter(X)
toc = time()
print(1000*(toc-tic), ' ms')
X = np.random.randint(1, 1000, (1000, 1))
我收到此错误消息:
> --------------------------------------------------------------------------- TypingError Traceback (most recent call
> last) <ipython-input-8-ae7154a2d7d0> in <module>
> 1 tic = time()
> ----> 2 dotter(X)
> 3 toc = time()
> 4 print(1000*(toc-tic), ' ms')
>
> ~/anaconda3/lib/python3.8/site-packages/numba/core/dispatcher.py in
> _compile_for_args(self, *args, **kws)
> 418 e.patch_message(msg)
> 419
> --> 420 error_rewrite(e, 'typing')
> 421 except errors.UnsupportedError as e:
> 422 # Something unsupported is present in the user code, add help info
>
> ~/anaconda3/lib/python3.8/site-packages/numba/core/dispatcher.py in
> error_rewrite(e, issue_type)
> 359 raise e
> 360 else:
> --> 361 raise e.with_traceback(None)
> 362
> 363 argtypes = []
>
> TypingError: Failed in nopython mode pipeline (step: nopython
> frontend) No implementation of function Function(<function dot at
> 0x7fdbf7c19670>) found for signature:
>
> >>> dot(array(int64, 2d, C), array(int64, 2d, F)) There are 2 candidate implementations:
> - Of which 2 did not match due to:
> Overload in function 'dot': File: numba/core/typing/npydecl.py: Line 957.
> With argument(s): '(array(int64, 2d, C), array(int64, 2d, F))':
> Rejected as the implementation raised a specific error:
> TypingError: np.dot() only supported on float and complex arrays raised from
> /Users/Zsolti/anaconda3/lib/python3.8/site-packages/numba/core/typing/npydecl.py:942
>
> During: resolving callee type: Function(<function dot at
> 0x7fdbf7c19670>) During: typing of call at
> <ipython-input-7-093e59547e26> (3)
>
>
> File "<ipython-input-7-093e59547e26>", line 3: def dotter(X):
> Z = np.dot(X,X.T)
> ^
我把X的定义改成了np.random.rand()解决了这个问题,还是不明白为什么numba不能理解random.randint。 有人可以向我解释吗? 提前致谢!
【问题讨论】: