项目场景:
使用numba加速自定义函数
问题描述:
@numba.jit(nopython=True)
def meshgrid(vessel):
''' ... '''
faces = np.zeros([len(face), 3], dtype=int)
''' ... '''
遇到faces = np.zeros([len(face), 3], dtype=int)
这句时报了两个错,错误如下
No implementation of function Function(<built-in function zeros>) found for signature:
>>> zeros(list(int64)<iv=None>, dtype=Function(<class 'int'>))
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload of function 'zeros': File: numba\core\typing\npydecl.py: Line 507.
With argument(s): '(list(int64)<iv=None>, dtype=Function(<class 'int'>))':
No match.
During: resolving callee type: Function(<built-in function zeros>)
During: typing of call at <ipython-input-6-f8e133d7960d> (3)
原因分析:
参考NumPy zeros in Numba function is not working when multi-dimensional shape is a list可知:
- 尽管平时在使用numpy时大家都习惯用类似
[shape1,shape2]
这样的方法来写,但是正规的numpy写法应该使用tuple
而不是list
- 在装饰器中使用了
nopython=True
,那么dtype
就不能是python内置的数据类型,要改为numba
中的数据类型。
解决方案:
faces = np.zeros((len(face),3),dtype=numba.int64)