Numba 函数参数类型没有匹配的定义 ListType[array(float64, 2d, C)] 错误
我在 Numba
中有一个使用 List(float64[:,::1])
类型的函数,这是一个尝试该类型的虚拟函数,我会这样做for循环下有很多操作。它有一种奇怪的行为,而 to arr 列表具有相同的 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)
I have a function in Numba
that uses a List(float64[:,::1])
type, this is a dummy function to try out the type, I will do a lot of operations under the for loop. It has an strange behavior, while the to arr lists have the same excatly numba.typeof() signature, one works and the other does not and tells me it is not matching type.
This is definitely a type of object error, but I can figure it out.
this is the file test.npy to load:
https://drive.google.com/file/d/1guAe1C2sKZyy2U2_qXAhMA1v46PfeKnN/view?usp=sharing
Error
raise TypeError(msg)
TypeError: No matching definition for argument type(s) ListType[array(float64, 2d, C)]
Code
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于陷入这样的琐事的任何人来说,事实证明正确的签名类型是:
我不明白
List
和ListType
之间的区别,并且我无法在List 上找到它。代码>Numba
官方网站。我可以为装饰器中的类型提供一份备忘单,因为仅从函数参数中找到的可用数据类型来推断它们应该如何编写并不容易。另外,拥有一个基于 numba.typeof() 返回的解析器函数确实会有很大帮助,并且能够创建装饰器的字符串。另外,到 List() 的转换非常慢,我在 Numba GitHub 上找到了一篇讨论这个问题的帖子。这是原始帖子 以 Python 列表作为参数提高 numba.typed.List 构造函数的性能
编辑
我发现了一个超级有用的行来获取 numba 函数的类型签名
For anybody stuck in something trivial like this, turns out the correct signature type is:
I do not understand the differences between
List
andListType
and I could not find it on theNumba
official website. I could be really helpfull to have a cheatsheet for types in decorators as is not easy to infer how they should be written just from the available data type found in the arguments of the function. Also, it could really be of great help to have a parser function based on numba.typeof() return, and been able to create the string of the decorator just of off this.Plus, the conversion to List() is pretty slow, I found a post on Numba GitHub that talks about this this problem. This is the original post improve performance of numba.typed.List constructor with Python list as arg
EDIT
I found a super helpfull line to get the type signatures off of a numba function