吴恩达《深度学习》第一课第四周编程作业(多层神经网络)

参考链接:https://blog.csdn.net/u013733326/article/details/79767169

搭建多层神经网络步骤:

1、初始化

2、前向传播

  (1)线性部分

  (2)激活部分

3、计算代价(判断有没有学习)

4、反向传播

  (1)线性部分

  (2)激活部分

5、更新参数

6、预测

# coding=utf-8
# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import numpy as np
import h5py
import matplotlib.pyplot as plt
import testCases
from dnn_utils import sigmoid, sigmoid_backward, relu, relu_backward
import lr_utils
def init(layers_dims):
    parameters = {}
    L = len(layers_dims)
    for l in range(1, L):
        # print("l:", 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 liner_activation_forward(A_pre, W, b, activation):
    if activation == "sigmoid":
        Z, linear_cache = linear_forward(A_pre, W, b)
        A, activation_cache = sigmoid(Z)
    elif activation == "relu":
        Z, linear_cache = linear_forward(A_pre, W, b)
        A, activation_cache = relu(Z)
    assert A.shape == (W.shape[0], A_pre.shape[1])
    cache = (linear_cache, activation_cache)
    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 = liner_activation_forward(A_prev, parameters["W" + str(l)], parameters["b" + str(l)],
                                            activation="relu")
        caches.append(cache)

    AL, cache = liner_activation_forward(A, parameters["W" + str(L)], parameters["b" + str(L)],
                                         activation="sigmoid")
    caches.append(cache)

    assert AL.shape == (1, X.shape[1])

    return AL, caches


def cal_cost(AL, Y):
    m = Y.shape[1]
    cost = -np.sum(np.multiply(Y, np.log(AL)) + np.multiply(1 - Y,  np.log(1 - AL))) / m
    cost = np.squeeze(cost)
    assert cost.shape == ()

    return cost


# Press the green button in the gutter to run the script.

def liner_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 liner_activation_backward(dA, cache, activation):
    liner_cache, activation_cache = cache
    if activation == "relu":
        dZ = relu_backward(dA, activation_cache)
        dA_prev, dW, db = liner_backward(dZ, liner_cache)
    elif activation == "sigmoid":
        dZ = sigmoid_backward(dA, activation_cache)
        dA_prev, dW, db = liner_backward(dZ, liner_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)] = liner_activation_backward(dAL, current_cache,
                                                                                                 "sigmoid")
    for l in reversed((range(L - 1))):
        current_cache = caches[l]
        dA_prev_tmp, dW_tmp, db_tmp = liner_activation_backward(grads["dA" + str(l + 2)], current_cache, "relu")
        grads["dA" + str(l + 1)] = dA_prev_tmp
        grads["dW" + str(l + 1)] = dW_tmp
        grads["db" + str(l + 1)] = db_tmp

    return grads


def update(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 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

def solve(X, Y, layer_dims, learning_rate, num_iterations):
    costs = []
    parameters = init(layer_dims)

    for i in range(0, num_iterations):
        AL, caches = l_model_forward(X, parameters)
        cost = cal_cost(AL, Y)
        grads = L_model_backward(AL, Y, caches)
        parameters = update(parameters, grads, learning_rate)
        if i % 100 == 0:
            costs.append(cost)
                # 是否打印成本值
            print("第", i, "次迭代,成本值为:", np.squeeze(cost))

    plt.plot(np.squeeze(costs))
    plt.ylabel('cost')
    plt.xlabel('iterations (per tens)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()

    return parameters


if __name__ == '__main__':
    train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = lr_utils.load_dataset()

    train_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
    test_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T

    train_x = train_x_flatten / 255
    train_y = train_set_y
    test_x = test_x_flatten / 255
    test_y = test_set_y
    # layers_dims = [12288, 20, 7, 5, 1]  # 5-layer model
    layers_dims = [12288, 20, 7, 5, 1]
    parameters = solve(train_x, train_y, layers_dims, 0.0075, num_iterations=2500)
    predictions_train = predict(train_x, train_y, parameters)  # 训练集
    predictions_test = predict(test_x, test_y, parameters)  # 测试集
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

  

import numpy as np

def sigmoid(Z):
    """
    Implements the sigmoid activation in numpy

    Arguments:
    Z -- numpy array of any shape

    Returns:
    A -- output of sigmoid(z), same shape as Z
    cache -- returns Z as well, useful during backpropagation
    """

    A = 1/(1+np.exp(-Z))
    cache = Z

    return A, cache

def sigmoid_backward(dA, cache):
    """
    Implement the backward propagation for a single SIGMOID unit.

    Arguments:
    dA -- post-activation gradient, of any shape
    cache -- 'Z' where we store for computing backward propagation efficiently

    Returns:
    dZ -- Gradient of the cost with respect to Z
    """

    Z = cache

    s = 1/(1+np.exp(-Z))
    dZ = dA * s * (1-s)

    assert (dZ.shape == Z.shape)

    return dZ

def relu(Z):
    """
    Implement the RELU function.

    Arguments:
    Z -- Output of the linear layer, of any shape

    Returns:
    A -- Post-activation parameter, of the same shape as Z
    cache -- a python dictionary containing "A" ; stored for computing the backward pass efficiently
    """

    A = np.maximum(0,Z)

    assert(A.shape == Z.shape)

    cache = Z 
    return A, cache

def relu_backward(dA, cache):
    """
    Implement the backward propagation for a single RELU unit.

    Arguments:
    dA -- post-activation gradient, of any shape
    cache -- 'Z' where we store for computing backward propagation efficiently

    Returns:
    dZ -- Gradient of the cost with respect to Z
    """

    Z = cache
    dZ = np.array(dA, copy=True) # just converting dz to a correct object.

    # When z <= 0, you should set dz to 0 as well. 
    dZ[Z <= 0] = 0

    assert (dZ.shape == Z.shape)

    return dZ

  

posted @ 2021-01-19 11:28  维和战艇机  阅读(255)  评论(0编辑  收藏  举报