编程作业-卷积基础1

  1. Zero pad
  2. 单步卷积
  3. 向前传播
  4. 向前池化
  5. 向后传播
  6. 向后池化

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import h5py
import matplotlib.pyplot as plt

%matplotlib inline
plt.rcParams["figure.figsize"] = (5.0, 4.0)
plt.rcParams["image.interpolation"]= 'nearest'
plt.rcParams['image.cmap'] = 'gray'

%load_ext autoreload
%autoreload 2

np.random.seed(1)

Zero pad

1
2
3
def zero_pad(X, pad):
X_pad = np.pad(X, ((0,0), (pad,pad), (pad,pad),(0,0)), 'constant', constant_values=(0,0))
return X_pad
1
2
3
4
5
6
7
8
9
10
11
12
13
np.random.seed(1)
x = np.random.randn(4, 3, 3, 2)
x_pad = zero_pad(x, 2)
print ("x.shape =", x.shape)
print ("x_pad.shape =", x_pad.shape)
print ("x[1,1] =", x[1,1])
print ("x_pad[1,1] =", x_pad[1,1])

fig, axarr = plt.subplots(1, 2)
axarr[0].set_title('x')
axarr[0].imshow(x[0,:,:,0])
axarr[1].set_title('x_pad')
axarr[1].imshow(x_pad[0,:,:,0])
x.shape = (4, 3, 3, 2)
x_pad.shape = (4, 7, 7, 2)
x[1,1] = [[ 0.90085595 -0.68372786]
 [-0.12289023 -0.93576943]
 [-0.26788808  0.53035547]]
x_pad[1,1] = [[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]





<matplotlib.image.AxesImage at 0x151c1be9da0>

单步卷积

1
2
3
4
5
6
def conv_single_step(a_slice_prev, W, b):
s = np.multiply(a_slice_prev, W)
Z = np.sum(s)
Z = Z+float(b)

return Z

向前传播

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def conv_forward(A_prev, W, b, hparameters):
(m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape

(f, f, n_C_prev, n_C) = W.shape

stride = hparameters["stride"]
pad = hparameters["pad"]

n_H = int((n_H_prev+2*pad-f)/stride+1)
n_W = int((n_W_prev+2*pad-f)/stride+1)

Z = np.zeros((m, n_H, n_W, n_C))

A_prev_pad = zero_pad(A_prev, pad)

for i in range(m):
a_prev_pad = A_prev_pad[i]
for h in range(n_H):
for w in range(n_W):
for c in range(n_C):

vert_start = h*stride
vert_end = vert_start+f
horiz_start = w*stride
horiz_end = horiz_start+f

a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]
Z[i, h, w, c] = conv_single_step(a_slice_prev, W[:, :, :, c], b[0, 0, 0, c])

assert(Z.shape == (m, n_H, n_W, n_C))

cache = (A_prev, W, b, hparameters)

return Z, cache

向前池化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def pool_forward(A_prev, hparameters, mode = "max"):
(m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape

f = hparameters["f"]
stride = hparameters["stride"]

n_H = int(1+(n_H_prev - f)/stride)
n_W = int(1+(n_W_prev - f)/stride)
n_C = n_C_prev

A = np.zeros((m, n_H, n_W, n_C))

for i in range(m):
for h in range(n_H):
for w in range(n_W):
for c in range(n_C):

vert_start = n_H * stride
vert_end = vert_start + f
horiz_start = n_W * stride
horiz_end = horiz_start + f

a_prev_slice = A_prev[i, vert_start:vert_end, horiz_start:horiz_end, c]

if mode == "max":
A[i, h, w, c] = np.max(a_prev_slice)
elif mode == "average":
A[i, h, w, c] = np.average(a_prev_slice)

cache = (A_prev, hparameters)

assert(A.shape == (m, n_H, n_W, n_C))

return A, cache

向后传播

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def conv_backward(dZ, cache):
(A_prev, W, b, hparameters) = cache
(m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape
(f, f, n_C_prev, n_C) = W.shape
stride = hparameters["stride"]
pad = hparameters["pad"]

(m, n_H, n_W, n_C) = dZ.shape

dA_prev = np.zeros(A_prev.shape)
dW = np.zeros(W.shape)
db = np.zeros(b.shape)

A_prev_pad = zero_pad(A_prev, pad)
dA_prev_pad = zero_pad(dA_prev, pad)

for i in range(m):
a_prev_pad = A_prev_pad[i]
da_prev_pad = dA_prev_pad[i]
for h in range(n_H):
for w in range(n_W):
for c in range(n_C):

vert_start = stride*h
vert_end = vert_start+f
horiz_start = stride*w
horiz_end = horiz_start+f

a_slice = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]

da_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :] += W[:, :, :, c]*dZ[i, h, w, c]
dW[:, :, :, c] += a_slice*dZ[i, h, w, c]
db[:, :, :, c] += dZ[i, h, w, c]
dA_prev[i, :, :, :] = da_prev_pad[pad:-pad, pad:-pad, :]

assert(dA_prev.shape == (m, n_H_prev, n_W_prev, n_C_prev))

return dA_prev, dW, db

1
2
3
def create_mask_from_window(x):
mask = (x==np.max(x))
return mask

1
2
3
4
5
6
def distribute_value(dz, shape):
(n_H, n_W)=shape
average=dz/(n_H*n_W)
a=np.ones(shape)*average

return a

向后池化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def pool_backward(dA, cache, mode="max"):
(A_prev, hparameters)= cache
stride=hparameters["stride"]
f=hparameters["f"]
m, n_H_prev, n_W_prev, n_C_prev = A_prev.shape
m, n_H, n_W, n_C = dA.shape

dA_prev = np.zeros(A_prev.shape)

for i in range(m):
a_prev = A_prev[i]

for h in range(n_H):
for w in range(n_W):
for c in range(n_C):

vert_start = stride*h
vert_end = vert_start+f
horize_start = stride*w
horize_end = horize_start+f

if(mode=="max"):
a_prev_slice = a_prev[vert_start:vert_end, horize_start:horize_end, c]
mask = creat_mask_from_window(a_prev_slice)
dA_prev[i, vert_start:vert_end, horize_start:horize_end, c] += np.multiply(mask, dA[i, h, w, c])

elif(mode=="average"):
da = dA[i, h, w, c]
shape = (f, f)
dA_prev[i, vert_start:vert_end, horize_start:horize_end, c] += distribute_value(da, shape)

assert(dA_prev.shape==A_prev.shape)
return dA_prev

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2470290795@qq.com

文章标题:编程作业-卷积基础1

文章字数:748

本文作者:runze

发布时间:2020-03-02, 21:12:17

最后更新:2020-03-02, 22:48:59

原始链接:http://yoursite.com/2020/03/02/%E5%90%B4%E6%81%A9%E8%BE%BE%20%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/04%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/%E7%BC%96%E7%A8%8B%E4%BD%9C%E4%B8%9A-%E5%8D%B7%E7%A7%AF%E5%9F%BA%E7%A1%801/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏