Python3.4的Numba KeyError,`KeyError:“不支持选项:'arg_types'”`

时间:2016-05-12 19:20:03

标签: python python-3.x numpy numba keyerror

我正在从Python2.7 numba代码转换为Python3.4。此函数pairwise_distance从多维数组XY转换距离矩阵。

但是,我使用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'"

1 个答案:

答案 0 :(得分:2)

当我使用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)
相关问题