【ML/DL】深层神经网络模型python实现

深层神经网络模型python实现

文章目录

  • 深层神经网络模型python实现
    • 注:
    • 1.准备工作
      • 导入必要的包
      • 导入数据集
    • 2. 创建模型
      • (1) 初始化参数
      • (2) 前向传播
      • (3) 计算cost
      • (4) 反向传播
      • (5) 更新参数
      • (6) 合成模型
    • 3.训练模型

注:

数据集及详细讲解请查找吴恩达深度学习第一课第四周,本篇博客为编程作业总结。
GitHub资料:https://github.com/TangZhaoXiang/deeplearning.ai.git

1.准备工作

导入必要的包

import numpy as np
import h5py                           # 操作h5格式文件(一般为图片数据集)
import matplotlib.pyplot as plt
import time
import scipy
from PIL import Image
from scipy import ndimage
# 自定义的文件
from testCases_v2 import *            
from dnn_utils_v2 import sigmoid, sigmoid_backward, relu, relu_backward      
from dnn_app_utils_v2 import *  
  
# 将matplotlib的图表直接嵌入到Notebook之中
%matplotlib inline                    
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

#自动重新加载更改的模块
%load_ext autoreload
%autoreload 2

np.random.seed(1)

注:testCases_v2和dnn_utils_v2、dnn_app_utils_v2为自定义的python文件,太长这里就不贴出来了,可以下载github后查看源文件。

导入数据集

train_x_orig, train_y, test_x_orig, test_y, classes = load_data()

2. 创建模型

(1) 初始化参数

# GRADED FUNCTION: initialize_parameters_deep
def initialize_parameters_deep(layer_dims):
    
    np.random.seed(3)
    parameters = {}
    L = len(layer_dims)            # number of layers in the network

    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))
        
        assert(parameters['W' + str(l)].shape == (layer_dims[l], layer_dims[l-1]))
        assert(parameters['b' + str(l)].shape == (layer_dims[l], 1))
        
    return parameters

(2) 前向传播

# GRADED FUNCTION: linear_activation_forward
def linear_activation_forward(A_prev, W, b, activation):
    
    if activation == "sigmoid":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
    
    elif activation == "relu":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        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)

    return A, cache


# GRADED FUNCTION: L_model_forward
def L_model_forward(X, parameters):
    
    caches = []
    A = X
    L = len(parameters) // 2                  # number of layers in the neural network
    
    # Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.
    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)
    
    # Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list.
    AL, cache = linear_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

(3) 计算cost

# GRADED FUNCTION: compute_cost
def compute_cost(AL, Y):
     
    m = Y.shape[1]

    # Compute loss from aL and y.
    logprobs = np.multiply(Y, np.log(AL) + np.multiply(1 - Y, np.log(1 - AL)))
    cost = np.sum(logprobs,axis=1,keepdims=True) / (-m) 
    cost = np.squeeze(cost)      # To make sure your cost's shape is what we expect (e.g. this turns [[17]] into 17).
    
    assert(cost.shape == ()) 
    return cost

(4) 反向传播

# GRADED FUNCTION: linear_backward
def linear_backward(dZ, cache):
    
    A_prev, W, b = cache
    m = A_prev.shape[1]
    
    dW = np.matmul(dZ, A_prev.T) / m
    db = np.sum(dZ, axis = 1, keepdims=True) / m
    dA_prev = np.matmul(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
    

# GRADED FUNCTION: linear_activation_backward
def linear_activation_backward(dA, cache, activation):
    
    linear_cache, activation_cache = cache
    
    # 根据激活函数不同求dZ,再由dz计算dA_prev, dW, db
    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
    

# GRADED FUNCTION: L_model_backward
def L_model_backward(AL, Y, caches):
    """
    注:cashe缓存的Z值 ,chaces里面包含的是全部层计算的Z值,即[Z1,Z2 ....ZL]
    """
    grads = {}
    L = len(caches) # the number of layers
    m = AL.shape[1]
    Y = Y.reshape(AL.shape) # after this line, Y is the same shape as AL

    # Initializing the backpropagation
    # 公式: dAL = -(Y/A -(1-Y)/(1-A)) 由sigmoid版的L求导而得
    dAL =  - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL)) # derivative of cost with respect to AL
    
    # Lth layer (SIGMOID -> LINEAR) gradients. Inputs: "AL, Y, caches". Outputs: "grads["dAL"], grads["dWL"], grads["dbL"]
    current_cache = caches[L-1]     # 即最后一层的Z值
    grads["dA" + str(L)], grads["dW" + str(L)], grads["db" + str(L)] = linear_activation_backward(dAL, current_cache, activation = "sigmoid")
    
    # 计算前L-1层的dA,dW,db
    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]  # 读取每层前向传播计算的Z值
        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

    return grads

(5) 更新参数

# GRADED FUNCTION: update_parameters
def update_parameters(parameters, grads, learning_rate):
    
    L = len(parameters) // 2 # number of layers in the neural network

    # Update rule for each parameter. Use a for loop.
    for l in range(L):
        parameters["W" + str(l+1)] -= grads['dW' + str(l+1)] * learning_rate
        parameters["b" + str(l+1)] -= grads['db' + str(l+1)] * learning_rate
        
    return parameters

(6) 合成模型

# GRADED FUNCTION: L_layer_model
def L_layer_model(X, Y, layers_dims, learning_rate = 0.0075, num_iterations = 3000, print_cost=False):#lr was 0.009
    np.random.seed(1)
    costs = []                         # keep track of cost
    
    # Parameters initialization.
    parameters = initialize_parameters_deep(layers_dims)
    
    # Loop (gradient descent)
    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)
                
        # Print the cost every 100 training example
        if print_cost and i % 100 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))
        if print_cost and i % 100 == 0:
            costs.append(cost)
            
    # plot the 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

3.训练模型

# 得到参数
parameters = L_layer_model(train_x, train_y, layers_dims, num_iterations = 2500, print_cost = True)

# 计算准确率
pred_train = predict(train_x, train_y, parameters_L)  # 训练集准确率
pred_test = predict(test_x, test_y, parameters_L)     # 测试集准确率

你可能感兴趣的:(算法篇)