如何将 2x2 矩阵添加到 3x2x2 矩阵?
How to add a 2x2 matrix to a 3x2x2 matrix?
我正在尝试实现一个非常简单的池化函数。输入是一个 3x4x4 矩阵(3 维、4 行、4 列),我希望我的输出是一个 3x2x2 矩阵
def pooling_layers(image):
pooling_layer = np.zeros((3, 2, 2))
for i in range(3):
a = image[i][:][:]
result = skimage.measure.block_reduce(a, (2, 2), np.mean)
# now I have my result, I want to add it to the 2x2 block of `pooling_layer`
pooling_layer = pooling_layers[i][:][:] + result
print(pooling_layer)
return pooling_layer
上面我设法得到平均二维数组,但我想将它添加到我的 pooling_layers
矩阵的正确维度,我该怎么做?
例如。我有输入矩阵 C
array([[[ 37, 41, 46, 50],
[ 64, 68, 73, 78],
[ 91, 96, 100, 105],
[118, 123, 127, 132]],
[[ 26, 30, 35, 39],
[ 52, 56, 61, 65],
[ 78, 83, 87, 91],
[104, 109, 113, 117]],
[[ 28, 31, 35, 38],
[ 47, 50, 54, 57],
[ 66, 70, 73, 76],
[ 85, 89, 92, 95]]])
我的输出 pooling_layer
将是:
array([[[ 52.5, 61.75],
[ 107., 116. ]],
[[ 41., 50. ],
[ 93.5, 102.]],
[[ 39. , 46. ],
[ 77.5, 84. ]]])
不用for循环,直接用下面一行代码就可以得到结果。
skimage.measure.block_reduce(image, (1, 2, 2), np.mean)
另一方面,如果你想使用for循环的方法,你可以直接赋值而不是加法。
def pooling_layers(image):
pooling_layer = np.zeros((3, 2, 2))
for i in range(3):
a = image[i][:][:]
result = skimage.measure.block_reduce(a, (2, 2), np.mean)
pooling_layer[i] = result
return pooling_layer
我正在尝试实现一个非常简单的池化函数。输入是一个 3x4x4 矩阵(3 维、4 行、4 列),我希望我的输出是一个 3x2x2 矩阵
def pooling_layers(image):
pooling_layer = np.zeros((3, 2, 2))
for i in range(3):
a = image[i][:][:]
result = skimage.measure.block_reduce(a, (2, 2), np.mean)
# now I have my result, I want to add it to the 2x2 block of `pooling_layer`
pooling_layer = pooling_layers[i][:][:] + result
print(pooling_layer)
return pooling_layer
上面我设法得到平均二维数组,但我想将它添加到我的 pooling_layers
矩阵的正确维度,我该怎么做?
例如。我有输入矩阵 C
array([[[ 37, 41, 46, 50],
[ 64, 68, 73, 78],
[ 91, 96, 100, 105],
[118, 123, 127, 132]],
[[ 26, 30, 35, 39],
[ 52, 56, 61, 65],
[ 78, 83, 87, 91],
[104, 109, 113, 117]],
[[ 28, 31, 35, 38],
[ 47, 50, 54, 57],
[ 66, 70, 73, 76],
[ 85, 89, 92, 95]]])
我的输出 pooling_layer
将是:
array([[[ 52.5, 61.75],
[ 107., 116. ]],
[[ 41., 50. ],
[ 93.5, 102.]],
[[ 39. , 46. ],
[ 77.5, 84. ]]])
不用for循环,直接用下面一行代码就可以得到结果。
skimage.measure.block_reduce(image, (1, 2, 2), np.mean)
另一方面,如果你想使用for循环的方法,你可以直接赋值而不是加法。
def pooling_layers(image):
pooling_layer = np.zeros((3, 2, 2))
for i in range(3):
a = image[i][:][:]
result = skimage.measure.block_reduce(a, (2, 2), np.mean)
pooling_layer[i] = result
return pooling_layer