第四周作业
知识回顾
步骤
初始化网络参数
前向传播
2.1 计算一层的中线性求和的部分2.2 计算激活函数的部分(ReLU使用L-1次,Sigmod使用1次)
2.3 结合线性求和与激活函数
计算误差
反向传播
4.1 线性部分的反向传播公式
4.2 激活函数部分的反向传播公式
4.3 结合线性部分与激活函数的反向传播公式
更新参数
总结
代码
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1) #随机种子
# sigmoid 函数
def sigmoid(Z):
A = 1 / (1 + np.exp(-Z))
cache = Z
return A, cache
# 反向sigmoid函数求导
def sigmoid_backward(dA, cache):
Z = cache
s = 1 / (1 + np.exp(-Z))
dZ = dA * s * (1 - s)
assert(dZ.shape == Z.shape)
return dZ
# relu函数
def relu(Z):
A = np.maximum(0, Z)
assert(A.shape == Z.shape)
cache = Z
return A, cache
# 反向 relu 函数求导
def relu_backward(dA, cache):
Z = cache
dZ = np.array(dA, copy=True)
dZ[Z<=0] = 0
assert(dZ.shape == Z.shape)
return dZ
# 两层神经网络
def initialize_parameters(n_x, n_h, n_y):
W1 = np.random.randn(n_h, n_x)
b1 = np.random.randn(n_h, 1)
W2 = np.random.randn(n_y, n_h)
b2 = np.random.randn(n_y, 1)
assert(W1.shape == (n_h, n_x))
assert(b1.shape == (n_h, 1))
assert(W2.shape == (n_y, n_h))
assert(b2.shape == (n_y, 1))
parameters = {
"W1" : W1,
"b1" : b1,
"W2" : W2,
"b2" : b2
}
return parameters
# 多层神经网络参数初始化
def initialize_parameters_deep(layers_dims):
np.random.seed(3)
parameters = {}
L = len(layers_dims)
for l in range(1, L):
parameters["W" + str(l)] = np.random.randn(layers_dims[l], layers_dims[l - 1]) / np.sqrt(layers_dims[l - 1])
parameters["b" + str(l)] = np.zeros((layers_dims[l], 1))
assert(parameters["W" + str(l)].shape == (layers_dims[l], layers_dims[l - 1]))
assert(parameters["b" + str(l)].shape == (layers_dims[l], 1))
return parameters
# 前行传播
def linear_forward(A, W, b):
Z = np.dot(W, A) + b
assert(Z.shape == (W.shape[0], A.shape[1]))
cache = (A, W, b)
return Z, cache
# 线性激活
def linear_activation_forward(A_prev, W, b, activation):
if activation == "sigmoid":
Z, linear_cache = linear_forward(A_prev, W,b)
A, activation_cache = sigmoid(Z)
elif activation == "relu":
Z, linear_cache = linear_forward(A_prev, W, b)
A, activation_cache = relu(Z)
assert(A.shape == (W.shape[0],A_prev.shape[1]))
cache = (linear_cache, activation_cache)
# print(relu(Z))
return A,cache
# 多层前行传播
def L_model_forward(X, parameters):
caches = []
A = X
L = len(parameters) // 2
for l in range(1, L):
A_prev = A
A, cache = linear_activation_forward( A_prev, parameters["W"+str(l)], parameters['b'+str(l)],'relu')
caches.append(cache)
AL, cache = linear_activation_forward(A, parameters["W"+str(L)], parameters['b'+str(L)],'sigmoid')
caches.append(cache)
assert(AL.shape == (1, X.shape[1]))
return AL, caches
# 计算成本
def compute_cost(AL, Y):
m = Y.shape[1]
cost = -np.sum(np.multiply(np.log(AL), Y) + np.multiply(np.log(1 - AL), 1 - Y)) / m
cost = np.squeeze(cost)
assert(cost.shape == ())
return cost
# 后向传播线性部分
def linear_backward(dZ, cache):
A_prev, W, b = cache
m = A_prev.shape[1]
dW = np.dot(dZ, A_prev.T) / m
db = np.sum(dZ, axis=1, keepdims=True) / m
dA_prev = np.dot(W.T, dZ)
assert(dA_prev.shape == A_prev.shape)
assert(dW.shape == W.shape)
assert(db.shape == b.shape)
return dA_prev, dW, db
# 向后传播激活
def linear_activation_backward(dA, cache, activation="relu"):
linear_cache, activation_cache = cache
if activation == "relu":
dZ = relu_backward(dA, activation_cache)
dA_prev, dW, db = linear_backward(dZ, linear_cache)
elif activation == "sigmoid":
dZ = sigmoid_backward(dA, activation_cache)
dA_prev, dW, db = linear_backward(dZ, linear_cache)
return dA_prev, dW, db
# 多层向后传播函数
def L_model_backward(AL, Y, caches):
grads = {}
L = len(caches)
m = AL.shape[1]
Y = Y.reshape(AL.shape)
dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))
current_cache = caches[L-1]
grads["dA" + str(L)], grads["dW"+str(L)], grads["db"+str(L)] = linear_activation_backward(dAL, current_cache, "sigmoid")
for l in reversed(range(L-1)):
current_cache = caches[l]
dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA"+str(l+2)], current_cache, "relu")
grads["dA"+str(l + 1)] = dA_prev_temp
grads["dW"+str(l + 1)] = dW_temp
grads["db"+str(l + 1)] = db_temp
return grads
def update_parameters(parameters, grads, learning_rate):
L = len(parameters) // 2
for l in range(L):
parameters["W" + str(l + 1)] = parameters["W"+str(l+1)] - learning_rate*grads["dW"+str(l+1)]
parameters["b" + str(l + 1)] = parameters["b"+str(l+1)] - learning_rate*grads["db"+str(l+1)]
return parameters
# 搭建多层神经网络
def L_layer_model(X, Y, layerdims, learning_rate=0.0075, num_iterations=3000, print_cost=False,isPlot=True):
np.random.seed(1)
costs = []
parameters = initialize_parameters_deep(layerdims)
for i in range(0,num_iterations):
AL, caches = L_model_forward(X, parameters)
cost = compute_cost(AL, Y)
grads = L_model_backward(AL, Y, caches)
parameters = update_parameters(parameters, grads, learning_rate)
if i % 100 == 0:
costs.append(cost)
if print_cost:
print("第", i, "次迭代,成本值为: ", np.squeeze(cost))
if isPlot:
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
return parameters
def predict(X, y, parameters):
m = X.shape[1]
n = len(parameters) // 2
p = np.zeros((1,m))
probas, caches = L_model_forward(X, parameters)
for i in range(0, probas.shape[1]):
if probas[0,i] > 0.5:
p[0,i] = 1
else:
p[0,i] = 0
print("准确度为: " + str(float(np.sum((p == y))/m)))
return p