刷到的一道面试题。
一.单通道输入,单卷积核
def numpy_conv(inputs, myfilter):
h_ori, w_ori = inputs.shape
h_k, w_k= myfilter.shape
h_new, w_new = h_ori-h_k+1,w_ori-w_k+1
result = np.zeros((h_new, w_new))
for row in range(0, h_new, 1):
for col in range(0, w_new, 1):
# 池化大小的输入区域
cur_input = inputs[row:row+h_k, col:col+w_k]
#和核进行乘法计算
cur_output = cur_input * myfilter
#再把所有值求和
conv_sum = np.sum(cur_output)
#当前点输出值
result[row, col] = conv_sum
return result
二.多通道多卷积核
def _conv(inputs, filter,strides=[1,1], padding="SAME"):
C_in, H, W = inputs.shape
filter_size = filter.shape[2]
# C_out指核对个数,也是最后结果对通道个数
C_out = filter.shape[0]