如何在python中使用numba.jit将计算值传递给列表排序? [英] How do I pass in calculated values to a list sort using numba.jit in python?
问题描述
我正在尝试使用Python中的numba-jit函数中的自定义键对列表进行排序.简单的自定义键可以工作,例如,我知道我可以使用如下所示的绝对值进行排序:
I am trying to sort a list using a custom key within a numba-jit function in Python. Simple custom keys work, for example I know that I can just sort by the absolute value using something like this:
import numba
@numba.jit(nopython=True)
def myfunc():
mylist = [-4, 6, 2, 0, -1]
mylist.sort(key=lambda x: abs(x))
return mylist # [0, -1, 2, -4, 6]
但是,在以下更复杂的示例中,我收到了一个我不理解的错误.
However, in the following more complicated example, I get an error that I do not understand.
import numba
import numpy as np
@numba.jit(nopython=True)
def dist_from_mean(val, mu):
return abs(val - mu)
@numba.jit(nopython=True)
def func():
l = [1,7,3,9,10,-4,-2,0]
avg_val = np.array(l).mean()
l.sort(key=lambda x: dist_from_mean(x, mu=avg_val))
return l
它正在报告的错误如下:
The error that it is reporting is the following:
Traceback (most recent call last):
File "testitout.py", line 18, in <module>
ret = func()
File "/.../python3.6/site-packages/numba/core/dispatcher.py", line 415, in _compile_for_args
error_rewrite(e, 'typing')
File "/.../python3.6/site-packages/numba/core/dispatcher.py", line 358, in error_rewrite
reraise(type(e), e, None)
File "/.../python3.6/site-packages/numba/core/utils.py", line 80, in reraise
raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: convert make_function into JIT functions)
Cannot capture the non-constant value associated with variable 'avg_val' in a function that will escape.
File "testitout.py", line 14:
def func():
<source elided>
l.sort(key=lambda x: dist_from_mean(x, mu=avg_val))
^
你知道这里发生了什么吗?
Do you know what is happening here?
推荐答案
你知道这里发生了什么吗?
Do you know what is happening here?
使用参数 nopython = True
可以停用对象模式,因此Numba不能将所有值都作为Python对象处理(请参阅:如何在numba中的"@guvectorize"中调用"@guvectorize"?)
By using the parameter nopython = True
you deactivate the object mode and hence Numba isn't able to handle all values as Python objects (refer to: https://numba.pydata.org/numba-doc/latest/glossary.html#term-object-mode). (Reference is actually another post I coincidentally wrote today: How call a `@guvectorize` inside a `@guvectorize` in numba?)
@numba.jit(nopython=True)
def func():
l = [1,7,3,9,10,-4,-2,0]
avg_val = np.array(l).mean()
l.sort(key=lambda x: dist_from_mean(x, mu=avg_val))
return l
反正 lambda
太过",numba jit函数的复合体-至少在将其作为参数传递时比较(比较 https://github.com/numba/numba/issues/4481 ).启用 nopython
模式后,您只能使用有限数量的库-完整列表可在此处找到:
Anyhow, lambda
is "too" complex for a numba jit function - at least when it's passed as an argument (compare https://github.com/numba/numba/issues/4481). With the nopython
mode activated you can only use a limited amount of libraries - the full list can be found here: https://numba.pydata.org/numba-doc/dev/reference/numpysupported.html
这就是为什么它引发以下错误的原因:
That's why it throws the following error:
numba.core.errors.TypingError:在nopython模式管道中失败(步骤:将make_function转换为JIT函数)无法捕获与函数中的变量"avg_val"相关的非常数值会逃脱的.
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: convert make_function into JIT functions) Cannot capture the non-constant value associated with variable 'avg_val' in a function that will escape.
此外,当您使用 nopython = True
时,您在另一个引用了jit加速函数.那也可能是问题的根源.
Moreover, your referencing a jit accelerated function within another - when having nopython = True
. That could be the source of the problem as well.
我强烈建议您看以下教程: http://numba.pydata.org/numba-doc/latest/user/5minguide.html#will-numba-work-for-my-code; 它应该可以帮助您有类似的问题!
I'd highly suggest taking a look at the following tutorial: http://numba.pydata.org/numba-doc/latest/user/5minguide.html#will-numba-work-for-my-code; it should help you out with similar problems!
更多阅读内容和来源:
- https://github.com/numba/numba/issues/5120
- http://numba.pydata.org/numba-doc/latest/user/5minguide.html#will-numba-work-for-my-code
- TypingError:在nopython模式管道中失败(步骤:nopython前端)
- https://github.com/numba/numba/issues/4481
- https://numba.pydata.org/numba-doc/dev/reference/numpysupported.html
- 如何调用"@guvectorize"numba中的"@guvectorize"中?
这篇关于如何在python中使用numba.jit将计算值传递给列表排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!