【发布时间】:2020-04-05 08:16:34
【问题描述】:
我正在尝试优化一些具有一些循环和矩阵运算的代码。但是,我遇到了一些错误。请在下面找到代码和输出。
代码:
@njit
def list_of_distance(d1): #d1 was declared as List()
list_of_dis = List()
for k in range(len(d1)):
sum_dist = List()
for j in range(3):
s = np.sum(square(np.reshape(d1[k][:,:,j].copy(),d1[k][:,:,j].shape[0]*d1[k][:,:,j].shape[1])))
sum_dist.append(s) # square each value in the resulting list (dimenstion)
distance = np.sum(sum_dist) # adding the total value for each dimension to a list
list_of_dis.append(np.round(np.sqrt(distance))) # Sum the values to get the total squared values of residual images
return list_of_dis
输出:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<function sum at 0x7f898814bd08>) with argument(s) of type(s): (list(int64))
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<function sum at 0x7f898814bd08>)
[2] During: typing of call at <ipython-input-18-8c787cc8deda> (7)
File "<ipython-input-18-8c787cc8deda>", line 7:
def list_of_distance(d1):
<source elided>
for j in range(3):
s = np.sum(square(np.reshape(d1[k][:,:,j].copy(),d1[k][:,:,j].shape[0]*d1[k][:,:,j].shape[1])))
^
This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.
To see Python/NumPy features supported by the latest release of Numba visit:
http://numba.pydata.org/numba-doc/latest/reference/pysupported.html
and
http://numba.pydata.org/numba-doc/latest/reference/numpysupported.html
For more information about typing errors and how to debug them visit:
http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile
If you think your code should work with Numba, please report the error message
and traceback, along with a minimal reproducer at:
https://github.com/numba/numba/issues/new
谁能帮我解决这个问题。
感谢和最好的问候
迈克尔
【问题讨论】:
-
你能提供 d1 包含的样本数据吗?另外,我们可以在没有 Numba 的情况下运行此代码吗?
-
嗨@Ethan,感谢您的回复。 d1 包含一个 RGB 图像列表。是的,它与普通的 python 代码完美配合。但是,它运行得很慢,因此需要优化代码。内核也在两个循环后重新启动。为此,我也不知道为什么。谢谢和最好的问候
标签: python-3.x numpy numba