【问题标题】:How to stop deprecation warning?如何停止弃用警告?
【发布时间】: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


    【解决方案1】:

    您可以禁止显示弃用警告。来自warnings filter

    from numba.core.errors import NumbaDeprecationWarning, NumbaPendingDeprecationWarning
    import warnings
    
    warnings.simplefilter('ignore', category=NumbaDeprecationWarning)
    warnings.simplefilter('ignore', category=NumbaPendingDeprecationWarning)
    

    编辑:从我们下面的 cmets 来看,我认为这不是为你做的。因此,我查看了您提供的内容,发现您没有发送 Numba 需要的数据类型。

    你正在向它发送一个 NumPy 数组。

    RatiosUp = np.array([np.random.uniform(size=rand) for rand in temp], dtype=object)
    

    当它需要类似这样的数组时

    >>> numba.float32[:]
    array(float32, 1d, A)
    

    再次,来自文档:

    As an optimizing compiler, Numba needs to decide on the type of each variable to generate efficient machine code. Python’s standard types are not precise enough for that, so we had to develop our own fine-grained type system.

    【讨论】:

    • 它不是关于压制,而是我必须做的不同,这样它就不会在未来被弃用。我很抱歉造成误解:(
    • 除非您是核心开发人员,否则您不知道(如果他们没有发布路线图,可能也不知道)将来会弃用或不会弃用什么。跨度>
    • 否则请参考错误提示链接:(numba.pydata.org/numba-doc/latest/reference/…)
    • 哦,我明白了。您发送的数据类型错误。
    • forceobj=False 做到了 -> 有没有办法将 np.array 对象转换为类似 nb.objects 的 nb 数组?找不到 nb.objects。或者是否有可变大小的数组?因为我目前正在使用 np.object
    猜你喜欢
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多