ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 2, the array at index 0 has size 3

ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 2, the array at index 0 has size 3

我收到错误 ValueError: 串联轴的所有输入数组维度必须完全匹配,但沿着维度 2,索引 0 处的数组大小为 3,索引 1 处的数组大小为尺寸 1 而 运行 下面的代码。

for i in range(6):
    print('current b', current_batch)
   
    current_pred = model.predict(current_batch)[0]
    print('current pred', current_pred)
    
    test_predictions.append(current_pred) 
    print('current batch', current_batch)
    print('current batch => ', current_batch[:,1:,:])
    
    current_batch = np.append(current_batch[:,1:,:], [[current_pred]], axis=1)

收到此错误

谁能解释一下为什么会这样。

谢谢,

基本上,Numpy 告诉您串联矩阵的形状应该对齐。例如,可以将 3x4 矩阵与 3x5 矩阵连接起来,以便我们得到 3x9 矩阵(我们添加了维度 1)。

这里的问题是 Numpy 告诉你轴没有对齐。在我的示例中,这将尝试将 3x4 矩阵与 10x10 矩阵连接起来。这是不可能的,因为形状没有对齐。

这通常意味着您正在尝试连接错误的东西。如果您确定,请尝试使用 np.reshape 函数,这将更改其中一个矩阵的形状,以便将它们连接起来。

如回溯所示,np.append 实际上使用的是 np.concatenate。您是否阅读(研究)了这两个功能的文档?了解他们对维度的看法吗?

从显示[[current_pred]],转换为数组将是(1,1,1)形状。你明白吗?

current_batch[:,1:,:] 是,据我所知,从小图像 (1,5,3)

您要求加入轴 1,即 1 和 5,好的。但它说最后一个维度,轴 2 不匹配。那1不等于3,你明白吗?

像使用 test_predictions.append(current_pred) 一样添加列表在迭代中效果很好。

np.append 效果不佳。即使它有效,它也很慢。在这里它不起作用,因为您没有足够注意匹配尺寸。