gpt4 book ai didi

python - Numba 函数没有参数类型的匹配定义 ListType[array(float64, 2d, C)] 错误

转载 作者:行者123 更新时间:2023-12-02 18:17:59 32 4
gpt4 key购买 nike

我在 Numba 中有一个使用 List(float64[:,::1]) 类型的函数,这是一个用来测试类型的虚拟函数,我会在for循环下做很多操作。它有一个奇怪的行为,而 to arr 列表具有相同的 excatly numba.typeof() 签名,一个有效,另一个无效,并告诉我它不匹配类型。

这绝对是一种对象错误,但我能搞清楚。

这是要加载的文件 test.npy:

https://drive.google.com/file/d/1guAe1C2sKZyy2U2_qXAhMA1v46PfeKnN/view?usp=sharing

错误

    raise TypeError(msg)
TypeError: No matching definition for argument type(s) ListType[array(float64, 2d, C)]

代码

import numpy as np
import numba
from numba.typed import List

branches = np.load('test.npy', allow_pickle=True).item()


@numba.njit('List(int64)(List(float64[:, ::1]))')
def not_test(branches):
a_list = []
for branch in branches:
for i in range(len(branch)):
a_list.append(i)
return a_list

# this does not work
arr = []
for branch in branches.features[:2]:
arr.append(np.asarray(branch.geometry.coordinates).copy())
arr = List(arr.copy())

print(numba.typeof(arr))
no_test(arr)

# this works
arr = List([np.array([np.array([7.0,7.3]), np.array([7.4,8.6])])])
print(numba.typeof(arr))
no_test(arr)

最佳答案

对于任何陷入这种琐碎事情的人来说,正确的签名类型是:

@numba.njit('List(int64)(ListType(float64[:, ::1]))')

我不明白 ListListType 之间的区别,我在 Numba 官方网站上找不到它。如果我有一个装饰器类型的备忘单,我真的很有帮助,因为仅仅从函数参数中找到的可用数据类型来推断它们应该如何编写是不容易的。此外,拥有一个基于 numba.typeof() 返回值的解析器函数,并且能够仅以此创建装饰器的字符串,真的会有很大帮助。

另外,到 List() 的转换非常慢,我在 Numba GitHub 上找到了一篇讨论这个问题的帖子。这是原帖improve performance of numba.typed.List constructor with Python list as arg

def convert2(x, dtype=np.float64):
try:
# Try and convert x to a Numpy array. If this succeeds
# then we have reached the end of the nesting-depth.
y = np.asarray(x, dtype=dtype)
except:
# If the conversion to a Numpy array fails, then it can
# be because not all elements of x can be converted to
# the given dtype. There is currently no way to distinguish
# if this is because x is a nested list, or just a list
# of simple elements with incompatible types.

# Recursively call this function on all elements of x.
y = [convert2(x_, dtype=dtype) for x_ in x]

# Convert Python list to Numba list.
y = numba.typed.List(y)

return y

编辑

我找到了一个 super 有用的行来获取 numba 函数的类型签名

print(not_test.inspect_types())
#not_test (ListType[array(float64, 2d, C)], array(float64, 1d, A))

关于python - Numba 函数没有参数类型的匹配定义 ListType[array(float64, 2d, C)] 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71286902/

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