gpt4 book ai didi

python - Numba v0.44 : cannot reflect element of reflected container

转载 作者:太空宇宙 更新时间:2023-11-03 20:42:17 24 4
gpt4 key购买 nike

我是 numba 的新手,并且每次都在努力获得我认为在 nopython 模式下工作简单的东西。

例如,受到这个问题的启发 coalescing-ranges我编写了以下函数:

@njit
# @jit
def coalesce(ranges):
coalesced = []
for i, (label_a, start_a, stop_a) in enumerate(ranges):
append_flag = True
for j, (label_b, start_b, stop_b) in enumerate(coalesced):
if label_a != label_b: # not of same type
continue
elif stop_a < start_b: # a does not start and then overlap b
continue
elif stop_b < start_a: # b does not start and then overlap a
continue
else:
# same type and overlap, merge into i, do not append
append_flag = False
coalesced[j] = [label_a, min([start_a, start_b]), max([stop_a, stop_b])]
break
if append_flag:
coalesced.append([label_a, start_a, stop_a])
return coalesced

它假设它是在列表的列表中传递的。每个子列表仅包含整数 [type, start, stop] 并且此函数提到合并重叠的相似类型范围,例如:

[
[1, 10, 100],
[0, 50, 75],
[1, 50, 150],
[0, 10, 100],
[0, 200, 300],
[0, 15, 150]
]
# becomes
[
[0, 10, 150],
[0, 200, 300],
[1, 10, 150]
]

此函数可与 @jit 配合使用(尽管它会发出大量警告)。当使用 @njit 和上面的列表调用此函数时:

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

我不知道这意味着什么或为什么会失败。

最佳答案

Numba 现在提供 Typed Lists ,它们接受列表的列表。据我所知,它们仍然需要单独附加(因为 TypedList.extend 方法不接受 LofL)。这导致:

from numba.typed import List  # As per the docs, since it's in beta, it needs to be imported explicitly

lol = [[1, 10, 100], [0, 50, 75], [1, 50, 150], [0, 10, 100], [0, 200, 300], [0, 15, 150]]

nb_list = List()
for lst in lol:
nb_list.append(lst)

coalesce(nb_list)

关于python - Numba v0.44 : cannot reflect element of reflected container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56792853/

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