Numba @jit和@njit给出了错误

时间:2018-01-06 14:25:51

标签: python numba

我在多个行和列中迭代x和y,我有一个2DArray' inarray'它保存了每平方的刷火算法成本,类似于:

array[[900   0   1   2   3   4   5   6   7]
      [  2   1   2   3   4   5   6   7   8]
      [  3   2 900   4   5   6   7   8   9]
      [  4   3   4   5   6   7   8   9  10]
      [  5   4   5   6   7   8   9  10  11]
      [  6   5   6   7   8   9  10  11  12]
      [  7   6   7   8   9  10  11  12  13]
      [  8   7   8   9  10  11  12  13  14]
      [  9   8   9  10  11  12  13  14  15]
      [ 10   9  10  11  12  13  14  15  16]]

如果900是障碍物,则0是目标,而数字是步数成本。

从这个2DArray我想生成一个矢量场,所以我实现了代码:

def vectorfield(inarray, goal, r, c):
    vectorarray = np.zeros((r,c), dtype=np.int32)
    for y in range(0,c):
        for x in range(0,r):
            xy = (x,y)
            neighbours = get_direction(xy, r, c)
            best_nbr = min(neighbours.keys(), key=lambda n: inarray[n])
            vectorarray[xy] = neighbours[best_nbr]

    vectorarray[goal] = -1
    return vectorarray

我制作了另一个2D数组,不会覆盖地图,因此仍然可以引用成本。然后我循环遍历行和列,并为每个点生成方向值,这是一个点的字典:angle:

directions = {
    (x - 1, y + 1): 225, (x, y + 1): 180, (x + 1, y + 1): 135,
    (x - 1, y): 270,                           (x + 1, y): 90,
    (x - 1, y - 1): 315,  (x, y - 1): 0,   (x + 1, y - 1): 45,
}

如果每个点都在地图外,则将其过滤掉。

然后我使用min()函数来获得邻居中成本最低的点。最后,邻居的值(指向下一个方块的角度)放在vectorarray中。

我应该说它没有装饰器

问题在于我想要@jit或@njit装饰器来加速执行,我不明白如何解决它们。

@njit的错误:

Traceback (most recent call last):
      File "/Users/aditya/Desktop/PycharmPrograms/RandomIdeas/lel1.py", line 334, in <module>
        main()
      File "/Users/aditya/Desktop/PycharmPrograms/RandomIdeas/lel1.py", line 313, in main
        inarray = vectorfield(inarray, (0,1), w, h)
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/dispatcher.py", line 330, in _compile_for_args
        raise e
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/dispatcher.py", line 307, in _compile_for_args
        return self.compile(tuple(argtypes))
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/dispatcher.py", line 579, in compile
        cres = self._compiler.compile(args, return_type)
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/dispatcher.py", line 80, in compile
        flags=flags, locals=self.locals)
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 766, in compile_extra
        return pipeline.compile_extra(func)
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 362, in compile_extra
        return self._compile_bytecode()
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 725, in _compile_bytecode
        return self._compile_core()
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 712, in _compile_core
        res = pm.run(self.status)
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 248, in run
        raise patched_exception
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 240, in run
        stage()
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 454, in stage_nopython_frontend
        self.locals)
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 868, in type_inference_stage
        infer.propagate()
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/typeinfer.py", line 844, in propagate
        raise errors[0]
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/typeinfer.py", line 137, in propagate
        constraint(typeinfer)
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/typeinfer.py", line 415, in __call__
        self.resolve(typeinfer, typevars, fnty)
      File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/typeinfer.py", line 450, in resolve
        raise TypingError(msg, loc=self.loc)
    numba.errors.TypingError: Failed at nopython (nopython frontend)
    Invalid usage of type(CPUDispatcher(<function get_direction at 0x105f218c8>)) with parameters ((int64 x 2), int64, int64)
     * parameterized
    File "lel1.py", line 298
    [1] During: resolving callee type: type(CPUDispatcher(<function get_direction at 0x105f218c8>))
    [2] During: typing of call at /Users/aditya/Desktop/PycharmPrograms/RandomIdeas/lel1.py (298)

@jit的错误:

most recent call last):
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/errors.py", line 259, in new_error_context
    yield
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/lowering.py", line 216, in lower_block
    self.lower_inst(inst)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/objmode.py", line 65, in lower_inst
    value = self.lower_assign(inst)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/objmode.py", line 159, in lower_assign
    return self.lower_expr(value)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/objmode.py", line 373, in lower_expr
    raise NotImplementedError(expr)
NotImplementedError: make_function(name=$const50.17, code=<code object <lambda> at 0x102295150, file "/Users/aditya/Desktop/PycharmPrograms/RandomIdeas/lel1.py", line 300>, closure=$50.15, defaults=None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/aditya/Desktop/PycharmPrograms/RandomIdeas/lel1.py", line 334, in <module>
    main()
  File "/Users/aditya/Desktop/PycharmPrograms/RandomIdeas/lel1.py", line 313, in main
    inarray = vectorfield(inarray, (0,1), w, h)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/dispatcher.py", line 307, in _compile_for_args
    return self.compile(tuple(argtypes))
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/dispatcher.py", line 658, in compile
    lifted_from=self.lifted_from)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 780, in compile_ir
    lifted_from=lifted_from)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 370, in compile_ir
    return self._compile_ir()
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 732, in _compile_ir
    return self._compile_core()
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 712, in _compile_core
    res = pm.run(self.status)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 248, in run
    raise patched_exception
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 240, in run
    stage()
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 625, in stage_objectmode_backend
    self._backend(lowerfn, objectmode=True)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 602, in _backend
    lowered = lowerfn()
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 576, in backend_object_mode
    self.flags)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/compiler.py", line 909, in py_lowering_stage
    lower.lower()
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/lowering.py", line 135, in lower
    self.lower_normal_function(self.fndesc)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/lowering.py", line 176, in lower_normal_function
    entry_block_tail = self.lower_function_body()
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/lowering.py", line 201, in lower_function_body
    self.lower_block(block)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/lowering.py", line 216, in lower_block
    self.lower_inst(inst)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 100, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/errors.py", line 265, in new_error_context
    six.reraise(type(newerr), newerr, sys.exc_info()[2])
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/errors.py", line 259, in new_error_context
    yield
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/lowering.py", line 216, in lower_block
    self.lower_inst(inst)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/objmode.py", line 65, in lower_inst
    value = self.lower_assign(inst)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/objmode.py", line 159, in lower_assign
    return self.lower_expr(value)
  File "/Users/aditya/Library/Python/3.6/lib/python/site-packages/numba/objmode.py", line 373, in lower_expr
    raise NotImplementedError(expr)
numba.errors.LoweringError: Failed at object (object mode backend)
make_function(name=$const50.17, code=<code object <lambda> at 0x102295150, file "/Users/aditya/Desktop/PycharmPrograms/RandomIdeas/lel1.py", line 300>, closure=$50.15, defaults=None)
File "lel1.py", line 300
[1] During: loweri

编辑: Inarray(从字面上将我的inarray转换为列表):

numpy.array([[900, 0, 1, 2, 3, 4, 5, 6, 7], [2, 1, 2, 3, 4, 5, 6, 7, 8], [3, 2, 900, 4, 5, 6, 7, 8, 9], [4, 3, 4, 5, 6, 7, 8, 9, 10], [5, 4, 5, 6, 7, 8, 9, 10, 11], [6, 5, 6, 7, 8, 9, 10, 11, 12], [7, 6, 7, 8, 9, 10, 11, 12, 13], [8, 7, 8, 9, 10, 11, 12, 13, 14], [9, 8, 9, 10, 11, 12, 13, 14, 15], [10, 9, 10, 11, 12, 13, 14, 15, 16]])

获取方向功能就是这样(我相信这是在了解inarray之后再现问题的唯一必要部分。)

@jit
def get_direction(point, rows, cols):
    (x, y) = point
    directions = {
        (x - 1, y + 1): 225, (x, y + 1): 180, (x + 1, y + 1): 135,
        (x - 1, y): 270,                       (x + 1, y): 90,
        (x - 1, y - 1): 315, (x, y - 1): 0,    (x + 1, y - 1): 45,
    }

    delete = []
    for k in directions.keys():
        if k[0] >= rows or k[0] < 0 or k[1] >= cols or k[1] < 0:
            delete.append(k)

    for i in delete:
        del directions[i]

    return directions

0 个答案:

没有答案