我正在从 Python2.7 numba
代码转换为 Python3.4。此函数pairwise_distance
将多维数组X
和Y
转换为距离矩阵。
但是,我使用 numba
装饰器 @jit
来加速代码:
import numpy as np
from numba import double
from numba.decorators import jit
@jit(arg_types = [double[:,:], double[:,:]])
def pairwise_distance(X, D):
M = X.shape[0]
N = X.shape[1]
for i in range(M):
for j in range(M):
d = 0.0
for k in range(N):
tmp = X[i, k] - X[j, k]
d += tmp * tmp
D[i, j] = np.sqrt(d)
# calculate the pairwise distance between X and Y
X = np.random.random((1000, 3))
Y = np.empty((1000, 1000))
pairwise_distance(X, Y)
这会输出以下错误:
KeyError: "Does not support option: 'arg_types'"
我不完全确定这个错误意味着什么,或者如何将其从 Python2.7 转换为与 Python3.4 兼容
这是完整的错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/targets/options.py in from_dict(self, dic)
15 try:
---> 16 ctor = self.OPTIONS[k]
17 except KeyError:
KeyError: 'arg_types'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-15-2c486d04f659> in <module>()
19 X = np.random.random((1000, 3))
20 Y = np.empty((1000, 1000))
---> 21 pairwise_numba(X, Y)
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
286 else:
287 real_args.append(self.typeof_pyval(a))
--> 288 return self.compile(tuple(real_args))
289
290 def inspect_llvm(self, signature=None):
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/dispatcher.py in compile(self, sig)
504
505 self._cache_misses[sig] += 1
--> 506 cres = self._compiler.compile(args, return_type)
507 self.add_overload(cres)
508 self._cache.save_overload(sig, cres)
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/dispatcher.py in compile(self, args, return_type)
76 def compile(self, args, return_type):
77 flags = compiler.Flags()
---> 78 self.targetdescr.options.parse_as_flags(flags, self.targetoptions)
79
80 impl = self._get_implementation(args, {})
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/targets/options.py in parse_as_flags(cls, flags, options)
24 def parse_as_flags(cls, flags, options):
25 opt = cls()
---> 26 opt.from_dict(options)
27 opt.set_flags(flags)
28 return flags
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numba/targets/options.py in from_dict(self, dic)
17 except KeyError:
18 fmt = "Does not support option: '%s'"
---> 19 raise KeyError(fmt % k)
20 else:
21 self.values[k] = ctor(v)
KeyError: "Does not support option: 'arg_types'"
最佳答案
当我使用 argtypes
而不是 arg_types
时,我没有收到 KeyError
,而是收到一条弃用警告,提示要使用 signature
相反。
以下内容对我使用 python 3.5 和 numba 0.25.0 有效
import numpy as np
from numba import jit
@jit('void(double[:,:], double[:,:])')
def pairwise_distance(X, D):
M = X.shape[0]
N = X.shape[1]
for i in range(M):
for j in range(M):
d = 0.0
for k in range(N):
tmp = X[i, k] - X[j, k]
d += tmp * tmp
D[i, j] = np.sqrt(d)
# calculate the pairwise distance between X and Y
X = np.random.random((1000, 3))
Y = np.empty((1000, 1000))
pairwise_distance(X, Y)
关于python - Python3.4 的 Numba KeyError, `KeyError: "不支持选项 : 'arg_types' "`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195708/