import numpy as np
x = np.arange(1, 21).reshape(5, 4)
w = np.array([[1, 1], [-1, -1]])
def my_conv(input, kernel, s):
output_size_0 = int((len(input) - len(kernel)) / s + 1) # 输出结果的第0维长度
output_size_1 = int((len(input[0]) - len(kernel[0])) / s + 1) # 输出结果的第1维长度
res = np.zeros([output_size_0, output_size_1], np.float32)
for i in range(len(res)):
for j in range(len(res[0])):
a = input[i*s:i*s + len(kernel), j*s: j*s + len(kernel)] # 从输入矩阵中取出子矩阵
b = a * kernel # 对应元素相乘
res[i][j] = b.sum()
return res
z = my_conv(x, w, 1)
步长为s的二维卷积Python实现
最新推荐文章于 2024-06-26 00:04:43 发布
本文详细介绍了如何使用Python和NumPy实现二维卷积运算,包括输入矩阵和卷积核的定义,以及步长参数的使用。通过具体示例,展示了卷积过程中的子矩阵抽取、元素相乘和求和步骤。

1209

被折叠的 条评论
为什么被折叠?



