gpt4 book ai didi

python - 在 Numba 中关闭列表反射

转载 作者:行者123 更新时间:2023-12-03 23:14:44 27 4
gpt4 key购买 nike

我正在尝试使用 Numba 加速我的代码。我传递给函数的参数之一是一个可变的列表列表。当我尝试更改子列表之一时,出现此错误:

Failed in nopython mode pipeline (step: nopython mode backend) cannot reflect element of reflected container: reflected list(reflected list(int64))



我实际上并不关心将我对 native 列表所做的更改反射(reflect)到原始 Python 列表中。我该如何告诉 Numba 不要反射(reflect)这些变化?关于 Numba 中的列表反射,文档非常模糊。

谢谢,

最佳答案

直接引用 the docs :

In nopython mode, Numba does not operate on Python objects. list arecompiled into an internal representation. Any list arguments must beconverted into this representation on the way in to nopython mode andtheir contained elements must be restored in the original Pythonobjects via a process called reflection.

Reflection is required to maintain the same semantics as found inregular Python code. However, the reflection process can be expensivefor large lists and it is not supported for lists that containreflected data types. Users cannot use list-of-list as an argumentbecause of this limitation.


最好的办法是给出一个形状为 len(ll) x max(len(x) for x in ll) 的二维 numpy 数组。 , ll 是列表的列表。我自己使用这样的东西来实现这一点,然后通过 arr, lengths到 njit 编译函数:
def make_2D_array(lis):
"""Funciton to get 2D array from a list of lists
"""
n = len(lis)
lengths = np.array([len(x) for x in lis])
max_len = np.max(lengths)
arr = np.zeros((n, max_len))

for i in range(n):
arr[i, :lengths[i]] = lis[i]
return arr, lengths
哈。

关于python - 在 Numba 中关闭列表反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53314071/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com