我有一个函数,它接受一个数组,执行一个任意的计算,然后返回一个新的形状,可以在其中广播。我想在numba.njit
环境中使用这个函数:
import numpy as np
import numba as nb
@nb.njit
def generate_target_shape(my_array):
### some functionality that calculates the desired target shape ###
return tuple([2,2])
@nb.njit
def test():
my_array = np.array([1,2,3,4])
target_shape = generate_target_shape(my_array)
reshaped = my_array.reshape(target_shape)
print(reshaped)
test()
然而,numba不支持元组创建,当我尝试使用tuple()
操作符将generate_target_shape
的结果转换为元组时,我得到了以下错误消息:
No implementation of function Function(<class 'tuple'>) found for signature:
>>> tuple(list(int64)<iv=None>)
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload of function 'tuple': File: numba/core/typing/builtins.py: Line 572.
With argument(s): '(list(int64)<iv=None>)':
No match.
During: resolving callee type: Function(<class 'tuple'>
如果我尝试将generate_target_shape
的返回类型从tuple
更改为list
或np.array
,则会收到以下错误消息:
Invalid use of BoundFunction(array.reshape for array(float64, 1d, C)) with parameters (array(int64, 1d, C))
有没有办法在nb.njit
函数内部创建一个可迭代对象,并将其传递给np.reshape
?
1条答案
按热度按时间mlnl4t2r1#
看起来numba不支持标准的python函数
tuple()
,你可以通过重写代码来解决这个问题: