【深度学习_1.4】搭建多层神经网络模型

目的:搭建隐藏层多于2层的神经网络

【准备】

1.导入相关包

import xxxx

【搭建神经网络】

1.初始化参数

搭建二层神经网络模型架构:LINEAR -> RELU -> LINEAR -> SIGMOID

def initialize_parameters(n_x, n_h, n_y):

    W1 = np.random.randn(n_h, n_x)*0.01
    b1 = np.zeros((n_h, 1))
    W2 = np.random.randn(n_y, n_h)*0.01
    b2 = np.zeros((n_y, 1))

搭建多层神经网络模型架构[LINEAR -> RELU] ×× (L-1) -> LINEAR -> SIGMOID

def initialize_parameters_deep(layer_dims):

    for l in range(1, L):
        parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1]) * 0.01

        parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))

2.正向传播

2.1正向传播基本模型

2.1.1线性正向传播

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)

2.1.2激活函数正向传播

def linear_activation_forward(A_prev, W, b, activation):

#注:此处用到辅助函数(helper function),可直接调用

 
 

  A,activation_cache =sigmoid(Z)

A, activation_cache = relu(Z)

        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)

2.2 L层正向传播

def L_model_forward(X, parameters):

    for l in range(1, L):
        A_prev = A 
        A, cache = linear_activation_forward(A_prev, parameters["W"+str(l)], parameters["b"+str(l)], activation = "relu")
        caches.append(cache)

    AL, cache = linear_activation_forward(A, parameters["W"+str(L)], parameters["b"+str(L)], activation = "relu")

    caches.append(cache)

3.损失函数

def compute_cost(AL, Y):

cost = -np.sum(Y*np.log(AL)+(1-Y)*np.log(1-AL))/m

4.反向传播

4.1 线性反向传播

def linear_backward(dZ, cache):

    dW = np.dot(dZ,A_prev.T)/m
    db = np.sum(dZ, axis=1, keepdims=True)/m
    dA_prev = np.dot(W.T,dZ)

4.2 激活函数反向传播

def linear_activation_backward(dA, cache, activation):

此处用到2个辅助函数,可以在程序里直接调用

dZ = sigmoid_backward(dA, activation_cache)
dZ = relu_backward(dA, activation_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)

5.L层反向传播

def L_model_backward(AL, Y, caches):

此处用到辅助函数:

dAL =- (np.divide(Y,AL)-np.divide(1-Y,1-AL))

    # Initializing the backpropagation
    dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))

# Lth layer (SIGMOID -> LINEAR) gradients. Inputs: "AL, Y, caches". Outputs: "grads["dAL"], grads["dWL"], grads["dbL"]
    current_cache = caches[L-1]

    grads["dA" + str(L)], grads["dW" + str(L)], grads["db" + str(L)] = linear_activation_backward(dAL, current_cache, activation = "sigmoid")

    for l in reversed(range(L-1)):
        # lth layer: (RELU -> LINEAR) gradients.
        # Inputs: "grads["dA" + str(l + 2)], caches". Outputs: "grads["dA" + str(l + 1)] , grads["dW" + str(l + 1)] , grads["db" + str(l + 1)] 
        current_cache = caches[l]
        dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA" + str(l + 2)], current_cache, activation = "relu")
        grads["dA" + str(l + 1)] = dA_prev_temp
        grads["dW" + str(l + 1)] = dW_temp
        grads["db" + str(l + 1)] = db_temp

6.参数更新

def update_parameters(parameters, grads, learning_rate):

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)]











猜你喜欢

转载自blog.csdn.net/oliverchrist/article/details/79350925