暂无分类
暂无标签
发布于2021-04-03 20:07 阅读(2474) 评论(0) 点赞(8) 收藏(4)
0
1
2
3
4
5
6
7
'''x->[2, 7, 5], kernel->[2, 4, 5]'''
x = np.arange(70).reshape(2, 7, -1) # 2个句子,每个句子维度为[7, 5]
kernel = np.arange(40).reshape(2, -1, 5) # 一个2层的卷积核,每层维度为[4, 5]
kernel_size = kernel.shape[1]
kernel_out = []
for b in range(x.shape[0]):
layer_max = []
for k in range(kernel.shape[0]):
layer_out = []
for i in range(x.shape[1] - kernel.shape[1] + 1):
# b表示当前批量中的一个句子,k表示卷积核中的一个卷积层
val = np.sum((x[b, i:i+kernel_size, :].reshape(1, -1) * kernel[k].reshape(1, -1)).squeeze(0))
layer_out.append(val)
# print(out)
layer_max.append(max(layer_out)) # maxpool
kernel_out.append(layer_max)
print(kernel_out)
w, h, c = 5, 5, 3
input_array = np.arange(w*h*c).reshape(c, w, h)
img_array = []
for i in range(w):
for j in range(h):
img_array.append([input_array[0,i,j],input_array[1,i,j],input_array[2,i,j]])
img_array = np.reshape(img_array, (w, h, c))
# img_array
# 确定padding的范围。
w_pad, h_pad, c = 7, 7, 3
padding_array = np.zeros((w_pad, h_pad, c))
for i in range(c):
for j in range(w):
for k in range(h):
padding_array[j+1,k+1,i] = img_array[j,k,i]
# print(padding_array[:, :, 0])
k_w, k_h = 3, 3
channel = 3
stride = 3
w0 = np.arange(3*3*3).reshape(-1, 3, 3)
b_0 = 1
pic = []
for i in range(k_w):
for j in range(k_h):
temp = 0
for c in range(channel):
temp = temp+np.sum(padding_array[i*2:i*2+stride, j*2:j*2+stride, c] * w0[:, :, c])
pic.append(temp)
pic_array = np.array(pic)
pic_reshape = np.reshape(pic_array,(3,3))
pic_reshape = pic_reshape + b_0
pic_reshape
原文链接:https://blog.csdn.net/newCraftsman/article/details/115397393
0
1
2
3
4
5
6
7
8
9
作者:遥远的她
链接: https://www.pythonheidong.com/blog/article/915690/be2af5bf562c02ca217b/
来源: python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!