实现卷积神经网络中的 池化:
python代码:
def pooling(input_h, input_w, kernel_h, kernel_w, stride_h, stride_w, array):
res = []
for i in range(0, input_h - kernel_h + 1, stride_h):
out_arr = []
for j in range(0, input_w - kernel_w + 1, stride_w):
tmp_array = []
for k in range(i, i+kernel_h):
tmp_array += array[k][j:j+kernel_w]
out_arr.append(max(tmp_array))
res.append(out_arr)
return res
if __name__ == "__main__":
array = [[1,2,3,4,5],
[3,4,2,1,7],
[8,3,4,2,5],
[9,3,1,6,2],
[0,3,2,7,3]]
res = pooling(5,5,3,3,2,2,array)
print(res)