+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-04(14)

2020-05(32)

2020-06(34)

2020-07(35)

2020-08(51)

NumPy实现ML

发布于2021-04-03 20:07     阅读(2474)     评论(0)     点赞(8)     收藏(4)


0

1

2

3

4

5

6

7

CNN

  • Conv1D
'''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)
  • Conv2D
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黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)