Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x numba在重塑numpy数组时出错_Python 3.x_Numpy_Numba - Fatal编程技术网

Python 3.x numba在重塑numpy数组时出错

Python 3.x numba在重塑numpy数组时出错,python-3.x,numpy,numba,Python 3.x,Numpy,Numba,我正在尝试优化一些包含循环和矩阵运算的代码。然而,我遇到了一些错误。请在下面找到代码和输出 代码: @njit def list of_distance(d1):#d1被声明为list() 列表中的列表=列表() 对于范围内的k(len(d1)): sum_dist=List() 对于范围(3)内的j: s=np.sum(正方形(np.reformate(d1[k][:,:,j].copy(),d1[k][:,:,j].形状[0]*d1[k][:,:,j].形状[1])) sum_dist.ap

我正在尝试优化一些包含循环和矩阵运算的代码。然而,我遇到了一些错误。请在下面找到代码和输出

代码:

@njit
def list of_distance(d1):#d1被声明为list()
列表中的列表=列表()
对于范围内的k(len(d1)):
sum_dist=List()
对于范围(3)内的j:
s=np.sum(正方形(np.reformate(d1[k][:,:,j].copy(),d1[k][:,:,j].形状[0]*d1[k][:,:,j].形状[1]))
sum_dist.append(s)#将结果列表中的每个值平方(dimenstation)
距离=np.sum(sum_dist)#将每个维度的总值添加到列表中
dis.append的列表(np.round(np.sqrt(distance))#对值求和以获得剩余图像的总平方值
返回列表
输出:

TypingError:在nopython模式管道中失败(步骤:nopython前端)
函数()与类型为(列表(int64))的参数的使用无效
*参数化
在定义0中:
所有模板均拒绝使用文字。
在定义1中:
拒绝所有没有文字的模板。
此错误通常由传递指定函数不支持的类型的参数引起。
[1] 期间:解析被调用方类型:函数()
[2] 期间:在(7)处键入呼叫
文件“”,第7行:
def列表中的距离(d1):
对于范围(3)内的j:
s=np.sum(正方形(np.reformate(d1[k][:,:,j].copy(),d1[k][:,:,j].形状[0]*d1[k][:,:,j].形状[1]))
^
这通常不是麻木本身的问题,而是由
使用不支持的功能或解析类型时出现问题。
要查看最新版本的Numba支持的Python/NumPy功能,请访问:
http://numba.pydata.org/numba-doc/latest/reference/pysupported.html
和
http://numba.pydata.org/numba-doc/latest/reference/numpysupported.html
有关键入错误以及如何调试错误的更多信息,请访问:
http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-代码不编译
如果您认为您的代码应该与Numba一起使用,请报告错误消息
和回溯,以及在以下位置的最小复制器:
https://github.com/numba/numba/issues/new
有谁能帮我解决这个问题吗

d1 = [np.arange(27).reshape(3,3,3), np.arange(27,54).reshape(3,3,3)]

@njit
def list_of_distance(d1): #d1 was declared as List()
    list_of_dis = [] #List() Changed - would not compile
    for k in range(len(d1)):
        sum_dist = [] #List() #List() Changed - would not compile
        for j in range(3):
            s = np.sum(np.square(np.reshape(d1[k][:,:,j].copy(),d1[k][:,:,j].shape[0]*d1[k][:,:,j].shape[1]))) #Added np. to "square"
            sum_dist.append(s) # square each value in the resulting list (dimenstion)   
        distance = np.sum(np.array(sum_dist)) # adding the total value for each dimension to a list - Wrapped list in np.array
        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

list_of_distance(d1)
Out[11]: [79.0, 212.0]
谢谢并致以最良好的问候


Michael

我不得不做一些修改才能让它工作,并模拟了“d1”,但这对我和Numba来说确实有效。导致运行时错误的主要问题似乎是np.sum在使用Numba的列表上不起作用,尽管在我注释掉@jit时它确实正确运行。用np.array()包装sumdist可以解决这个问题

d1 = [np.arange(27).reshape(3,3,3), np.arange(27,54).reshape(3,3,3)]

@njit
def list_of_distance(d1): #d1 was declared as List()
    list_of_dis = [] #List() Changed - would not compile
    for k in range(len(d1)):
        sum_dist = [] #List() #List() Changed - would not compile
        for j in range(3):
            s = np.sum(np.square(np.reshape(d1[k][:,:,j].copy(),d1[k][:,:,j].shape[0]*d1[k][:,:,j].shape[1]))) #Added np. to "square"
            sum_dist.append(s) # square each value in the resulting list (dimenstion)   
        distance = np.sum(np.array(sum_dist)) # adding the total value for each dimension to a list - Wrapped list in np.array
        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

list_of_distance(d1)
Out[11]: [79.0, 212.0]

您能提供d1包含的样本数据吗?另外,我们能让你在没有Numba的情况下运行这段代码吗?嗨@Ethan,谢谢你的回复。d1包含RGB图像的列表。是的,它与普通python代码完美配合。但是,它运行得非常慢,因此需要对代码进行优化。内核也在两次循环后重新启动。对此,我也不知道原因。谢谢&最好的回复@Ethan,非常感谢你的功能。它起作用了。谢谢并致以最良好的问候