gpt4 book ai didi

python - 手动制作的 3D 卷积神经网络的形状误差

转载 作者:行者123 更新时间:2023-11-30 09:18:57 28 4
gpt4 key购买 nike

我有以下用于在二维维度上进行卷积的代码:

matrix = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
kernel = np.array([[1,-1],[1,-1]])

kr, kc = kernel.shape
mr, mc = matrix.shape

output = np.empty((mc-kc+1, mr-kr+1))

for row in range(mc-kc+1):
for column in range(mr-kr+1):
output[row][column] = (matrix[row:row+kr,column:column+kc]*kernel).sum()

print(output)

我想在真实的彩色图像上尝试这个,这就是我尝试过的

#matrix is now 3D
print(matrix.shape)
Out: (340, 360, 3)

#kernel is now 3D
print(kernel.shape)
Out: (34, 36, 3)

kr, kc, kdim = kernel.shape
mr, mc, mdim = matrix.shape

output = np.empty((mc-kc+1, mr-kr+1, 3))

for row in range(mc-kc+1):
for column in range(mr-kr+1):
for dim in range(3):
output[row][column][dim] = (matrix[row:row+kr,column:column+kc]*kernel).sum()

print(output)
Out: ValueError: operands could not be broadcast together with shapes (33,36,3) (34,36,3)

我认为深度尺寸有问题,但我仍然不知道如何修复它。帮忙?

最佳答案

您已在 output 声明以及行和列循环中交换了行和列。试试这个版本:

# note: [rows, columns, channel]
output = np.empty((mr - kr + 1, mc - kc + 1, 3))

# note: `row` loops over matrix rows, `column` - over columns
for row in range(mr - kr + 1):
for column in range(mc - kc + 1):
for dim in range(3):
output[row][column][dim] = (matrix[row:row + kr, column:column + kc] * kernel).sum()

print(output)

关于python - 手动制作的 3D 卷积神经网络的形状误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47021118/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com