Cannot coerce to or from object in nopython context: Error after python

做~自己de王妃 提交于 2019-12-11 16:31:47

问题


Following up from here, Numba is finally working (after weeks) on my machine, without any weird indentation errors.

I've implemented it as in the solution to the linked question.

However, I now get this string of errors from Numba, the last line being that it can't coerce to or from object in nopython context:

Traceback (most recent call last):
  File "C:\Users\app\Documents\Python Scripts\gbc_classifier_train.py", line 19, in <module>
    import gentleboost_c_class_jit_v6_nolimit as gbc
  File "C:\Users\app\Documents\Python Scripts\gentleboost_c_class_jit_v6_nolimit.py", line 26, in <module>
    @jit(void(object_, float_[:,:], int_[:], int_))
  File "C:\Users\app\Anaconda\lib\site-packages\numba\decorators.py", line 224, in _jit_decorator
    nopython=nopython, func_ast=func_ast, **kwargs)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\decorators.py", line 133, in compile_function
    func_env = pipeline.compile2(env, func, restype, argtypes, func_ast=func_ast, **kwds)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 144, in compile2
    post_ast = pipeline(func_ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 191, in __call__
    ast = self.transform(ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 656, in transform
    ast = stage(ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 641, in _stage
    return _check_stage_object(stage_obj)(ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 194, in __call__
    ast = self.transform(ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 474, in transform
    return transformer.visit(ast)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 311, in visit_ControlBlock
    self.visitlist(node.body)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 286, in visitlist
    list[:] = _flatmap(self.visit, list)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 33, in _flatmap
    res = func(elem)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 306, in generic_visit
    new_node = self.visit(old_value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 311, in visit_ControlBlock
    self.visitlist(node.body)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 286, in visitlist
    list[:] = _flatmap(self.visit, list)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 33, in _flatmap
    res = func(elem)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 306, in generic_visit
    new_node = self.visit(old_value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 311, in visit_ControlBlock
    self.visitlist(node.body)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 286, in visitlist
    list[:] = _flatmap(self.visit, list)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\visitors.py", line 33, in _flatmap
    res = func(elem)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 306, in generic_visit
    new_node = self.visit(old_value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 297, in generic_visit
    value = self.visit(value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\array_expressions.py", line 100, in visit_Assign
    node.value = self.visit(node.value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\ast.py", line 306, in generic_visit
    new_node = self.visit(old_value)
  File "C:\Users\app\Anaconda\lib\ast.py", line 241, in visit
    return visitor(node)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\array_expressions.py", line 136, in visit_BinOp
    return self.visit_elementwise(elementwise, node)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\array_expressions.py", line 84, in visit_elementwise
    return self.register_array_expression(node)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\array_expressions.py", line 272, in register_array_expression
    wrap=False, link=False, nopython=True,
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 90, in run_pipeline2
    post_ast = pipeline(func_ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 191, in __call__
    ast = self.transform(ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 656, in transform
    ast = stage(ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 641, in _stage
    return _check_stage_object(stage_obj)(ast, env)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\pipeline.py", line 202, in __call__
    reporting.report(env, exc=e)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\reporting.py", line 181, in report
    raise exc
numba.error.NumbaError: (see below)
--------------------- Numba Encountered Errors or Warnings ---------------------
Error 133:22: Cannot coerce to or from object in nopython context
--------------------------------------------------------------------------------

What does this mean? I hope it doesn't mean I can't use objects!

Edit

I've edited all relevant code so that I'm no longer passing any objects around, and the definition now looks like this:

@jit(float_[:,:,:](float_[:,:], int_[:], int_))
def train_function(X, y, H):

But I'm still getting that error, except now it's not Error:133:22, but:

Error 176:22: Cannot coerce to or from object in nopython context

回答1:


I found out somewhere that Numba doesn't recognize np.dot() directly, and the Error: 176:22 was pointing to line 176, where I was using np.dot(). This was the relevant line:

 dot = 1. + np.dot(X1, g1) - np.dot(X1,gc)

So I cast the dot products it into floats, and it worked:

dot = 1. + float(np.dot(X1, g1)) - float(np.dot(X1,gc))


来源:https://stackoverflow.com/questions/25685916/cannot-coerce-to-or-from-object-in-nopython-context-error-after-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!