【发布时间】:2021-01-05 22:22:01
【问题描述】:
我的函数必须是 jit 编译的,但我收到以下弃用警告:
我该如何解决这个问题,这样问题就解决了? (这样我以后就不用担心这个功能不能正常工作了)
e_labeling.py:418: NumbaWarning:
Compilation is falling back to object mode WITH looplifting enabled because Function "get_result" failed type inference due to: non-precise type array(pyobject, 1d, C)
During: typing of argument at D:/Arc/Arc_Project\Architecture\_3_Labeling\CRV_Weighted_Score_labeling.py (422)
File "..\_3_Labeling\CRV_Weighted_Score_labeling.py", line 422:
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
<source elided>
for i in prange(len_result):
^
@nb.jit
D:/Arc/Arc_Project\Architecture\_3_Labeling\CRV_Weighted_Score_labeling.py:418: NumbaWarning:
Compilation is falling back to object mode WITHOUT looplifting enabled because Function "get_result" failed type inference due to: cannot determine Numba type of <class 'numba.core.dispatcher.LiftedLoop'>
File "..\_3_Labeling\CRV_Weighted_Score_labeling.py", line 422:
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
<source elided>
for i in prange(len_result):
^
@nb.jit
c:\users\ben\appdata\local\programs\python\python38\lib\site-packages\numba\core\object_mode_passes.py:177: NumbaWarning: Function "get_result" was compiled in object mode without forceobj=True, but has lifted loops.
File "..\_3_Labeling\CRV_Weighted_Score_labeling.py", line 422:
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
<source elided>
for i in prange(len_result):
^
warnings.warn(errors.NumbaWarning(warn_msg,
c:\users\ben\appdata\local\programs\python\python38\lib\site-packages\numba\core\object_mode_passes.py:187: NumbaDeprecationWarning:
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.
For more information visit https://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit
File "..\_3_Labeling\CRV_Weighted_Score_labeling.py", line 422:
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
<source elided>
for i in prange(len_result):
^
warnings.warn(errors.NumbaDeprecationWarning(msg,
D:/Arc/Arc_Project\Architecture\_3_Labeling\CRV_Weighted_Score_labeling.py:418: NumbaWarning:
Compilation is falling back to object mode WITHOUT looplifting enabled because Function "get_result" failed type inference due to: non-precise type pyobject
During: typing of argument at D:/Arc/Arc_Project\Architecture\_3_Labeling\CRV_Weighted_Score_labeling.py (422)
File "..\_3_Labeling\CRV_Weighted_Score_labeling.py", line 422:
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
<source elided>
for i in prange(len_result):
^
@nb.jit
c:\users\ben\appdata\local\programs\python\python38\lib\site-packages\numba\core\object_mode_passes.py:177: NumbaWarning: Function "get_result" was compiled in object mode without forceobj=True.
File "..\_3_Labeling\CRV_Weighted_Score_labeling.py", line 422:
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
<source elided>
for i in prange(len_result):
^
warnings.warn(errors.NumbaWarning(warn_msg,
c:\users\ben\appdata\local\programs\python\python38\lib\site-packages\numba\core\object_mode_passes.py:187: NumbaDeprecationWarning:
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.
For more information visit https://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit
File "..\_3_Labeling\CRV_Weighted_Score_labeling.py", line 422:
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
<source elided>
for i in prange(len_result):
^
warnings.warn(errors.NumbaDeprecationWarning(msg,
它会创建第 5 个随机值数组 result 并根据条件设置 1s 或 0s:(是的,每个条件都有 2 个条件,但这是因为它们必须在顺序不同。
RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices 是 numpy 对象数组(因为它的 numpy 数组由不同大小的 numpy 数组组成。(在同一索引处,这 4 个 np.array 的子数组的长度相同)
这是函数:(底部有一个可重现的样本)
from numba import prange
@nb.njit
def compare_size_filter(a,b):
return a > b
@nb.njit(parallel=True)
def loop_func(sub_RatiosUp, sub_RatiosDown, sub_UpPointsSlices, sub_DownPointsSlices, sub_result, len_shape):
for j in prange(len_shape):
if compare_size_filter(sub_RatiosUp[j],sub_RatiosDown[j]):
sub_result[j] = 1
elif compare_size_filter(sub_RatiosDown[j],sub_RatiosUp[j]):
sub_result[j] = 0
elif compare_size_filter(sub_DownPointsSlices[j], sub_UpPointsSlices[j]):
sub_result[j] = 0
else:
sub_result[j] = 1
@nb.jit
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
for i in prange(len_result):
loop_func(RatiosUp[i], RatiosDown[i], UpPointsSlices[i], DownPointsSlices[i], result[i], shapes[i])
return result
可重现的样本:(使用正确的结果列表-理解以确保更改任何内容后结果仍然正确)
import numpy as np, numba as nb, time
# generate sample data
LEN = 1000; Amount_Of_Elements = 4000
temp = np.random.randint(Amount_Of_Elements*0.7,high=Amount_Of_Elements, size=LEN)
RatiosUp = np.array([np.random.uniform(size=rand) for rand in temp], dtype=object)
RatiosDown = np.array([np.random.uniform(size=rand) for rand in temp], dtype=object)
UpPointsSlices = np.array([np.random.uniform(size=rand) for rand in temp], dtype=object)
DownPointsSlices = np.array([np.random.uniform(size=rand) for rand in temp], dtype=object)
# this is the original result without numba (== same result as above when used without decorators)
correct_result = [
[1 if (ratUp >ratDown) else 0 if (ratDown>ratUp) else 0 if (pointsDown>pointsUp) else 1
for ratUp,ratDown,pointsUp,pointsDown
in zip(ratiosUpSlice,ratiosDownSlice,upPointsSlice,downPointsSlice)]
for ratiosUpSlice,ratiosDownSlice,upPointsSlice,downPointsSlice
in zip(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices)]
from numba import prange
@nb.njit
def compare_size_filter(a,b):
return a > b
@nb.njit(parallel=True)
def loop_func(sub_RatiosUp, sub_RatiosDown, sub_UpPointsSlices, sub_DownPointsSlices, sub_result, len_shape):
for j in prange(len_shape):
if compare_size_filter(sub_RatiosUp[j],sub_RatiosDown[j]):
sub_result[j] = 1
elif compare_size_filter(sub_RatiosDown[j],sub_RatiosUp[j]):
sub_result[j] = 0
elif compare_size_filter(sub_DownPointsSlices[j], sub_UpPointsSlices[j]):
sub_result[j] = 0
else:
sub_result[j] = 1
@nb.jit
def get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes, result, len_result):
for i in prange(len_result):
loop_func(RatiosUp[i], RatiosDown[i], UpPointsSlices[i], DownPointsSlices[i], result[i], shapes[i])
return result
shapes = np.asarray([arr.size for arr in RatiosUp],dtype=np.object)
result = np.asarray([np.empty(s,np.int8) for s in shapes],dtype=np.object)
result1 = get_result(RatiosUp, RatiosDown, UpPointsSlices, DownPointsSlices, shapes,result, nb.int64(len(result)))
test = all(np.allclose(a,b, equal_nan=True) for a,b in zip(correct_result,result1.tolist()))
print(test)```
EDIT: (to avoid misunderstandings)
its not about supression, but what i have to do different so that it wont be deprecated in the future.
【问题讨论】:
标签: python numpy warnings deprecated numba