TypingError:在 nopython 模式管道中失败(步骤:nopython 前端)

Posted

技术标签:

【中文标题】TypingError:在 nopython 模式管道中失败(步骤:nopython 前端)【英文标题】:TypingError: Failed in nopython mode pipeline (step: nopython frontend) 【发布时间】:2019-07-30 14:47:25 【问题描述】:

我正在尝试使用 numba jit 编写我的第一个函数,我有一个 pandas 数据框,我需要遍历它并找到每 350 个点的均方根,因为 python 的 for 循环很慢我决定尝试numba jit,代码是:

@jit(nopython=True)
def find_rms(data, length):
    res = []
    for i in range(length, len(data)):
        interval = np.array(data[i-length:i])
        interval =np.power(interval, 2)
        sum = interval.sum()
        resI = sum/length
        resI = np.sqrt(res)
        res.appennd(resI)
    return res

mydf = np.array(df.iloc[:]['c0'], dtype=np.float64)
df.iloc[350:]['rms'] = find_rms(mydf, 350)

我在某处读到我需要指定数据类型,因此我写了“dtype = np.float64”但我仍然收到错误:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-39-4d388f72efdc> in <module>
----> 1 df.iloc[350:]['rms'] = find_rms(mydf, 350.0)

c:\users\1\appdata\local\programs\python\python35\lib\site-packages\numba\dispatcher.py in _compile_for_args(self, *args, **kws)
    346                 e.patch_message(msg)
    347 
--> 348             error_rewrite(e, 'typing')
    349         except errors.UnsupportedError as e:
    350             # Something unsupported is present in the user code, add help info

c:\users\1\appdata\local\programs\python\python35\lib\site-packages\numba\dispatcher.py in error_rewrite(e, issue_type)
    313                 raise e
    314             else:
--> 315                 reraise(type(e), e, None)
    316 
    317         argtypes = []

c:\users\1\appdata\local\programs\python\python35\lib\site-packages\numba\six.py in reraise(tp, value, tb)
    656             value = tp()
    657         if value.__traceback__ is not tb:
--> 658             raise value.with_traceback(tb)
    659         raise value
    660 

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function array>) with argument(s) of type(s): (array(float64, 1d, C))
 * parameterized
In definition 0:
    TypingError: array(float64, 1d, C) not allowed in a homogeneous sequence
    raised from c:\users\1\appdata\local\programs\python\python35\lib\site-packages\numba\typing\npydecl.py:463
In definition 1:
    TypingError: array(float64, 1d, C) not allowed in a homogeneous sequence
    raised from c:\users\1\appdata\local\programs\python\python35\lib\site-packages\numba\typing\npydecl.py:463
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<built-in function array>)
[2] During: typing of call at <ipython-input-34-edd252715b2d> (5)


File "<ipython-input-34-edd252715b2d>", line 5:
def find_rms(data, length):
    <source elided>
    for i in range(length, len(data)):
        interval = np.array(data[i-length:i])
        ^

This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.

To see Python/NumPy features supported by the latest release of Numba visit:
http://numba.pydata.org/numba-doc/dev/reference/pysupported.html
and
http://numba.pydata.org/numba-doc/dev/reference/numpysupported.html

For more information about typing errors and how to debug them visit:
http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile

If you think your code should work with Numba, please report the error message
and traceback, along with a minimal reproducer at:
https://github.com/numba/numba/issues/new

有人知道问题出在哪里吗?

【问题讨论】:

似乎在某些时候(作为输入或稍后创建的数组)并不具有相同类型的所有值。由于 numba 在 C 中编译东西,它只能处理填充了单一类型值的数组。检查数组data 是否满足此要求。 你的mydf 怎么样?我无法重现您在 IDE 中遇到的错误 【参考方案1】:

您在append 中有一个错字,我认为您在取平方根时也犯了一个错误(我相信resI 不是res)。

除此之外,唯一的问题是interval 的初始化。 Numba 不希望您将 numpy 数组传递给 numpy 数组。将 np.array 包裹在数组的切片周围没有任何帮助,python 根本不在乎你是否这样做,并且像你没​​有那样对待代码,但在 nopython 模式下 Numba 确实关心并抛出一个错误。把那部分排除在外就解决了问题。

@jit(nopython=True)
def find_rms(data, length):
    res = []
    for i in range(length, len(data)):
        interval = data[i-length:i]
        interval = np.power(interval, 2)
        sum = interval.sum()
        resI = sum/length
        resI = np.sqrt(resI)
        res.append(resI)
    return res

mydf = np.array(df.iloc[:]['c0'], dtype=np.float64)
target = find_rms(mydf, 350)

【讨论】:

以上是关于TypingError:在 nopython 模式管道中失败(步骤:nopython 前端)的主要内容,如果未能解决你的问题,请参考以下文章

Python:numba 可以在 nopython 模式下处理字符串数组吗?

numba - TypingError:无法确定 <class 'builtin_function_or_method'> 的 Numba 类型

多个输出和 numba 签名

如何在类的成员函数上使用 numba?

python部署web开发环境

使用带有 numba njit 功能的字典