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实现
最新推荐文章于 2025-04-15 12:07:08 发布