吳恩達第一課第二週編程作業

鏈接:https://pan.baidu.com/s/1ENynOEU33LFsoEln3HeZGw 
提取碼:0spc 
 

本次作業是完成 一個“識別貓”的神經網絡網絡搭建。

代碼:

import numpy as np
import matplotlib.pyplot as plt
from lr_utils import load_dataset

train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
m_train = train_set_y.shape[1]
m_test = test_set_y.shape[1]
num_px = train_set_x_orig[1]
# 降維
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
# 標準化數據,讓數據在【0,1】之間
train_set_x = train_set_x_flatten / 255
test_set_x = test_set_x_flatten / 255


# 建立神經網絡的主要步驟是:
# 1. 定義模型結構(例如輸入特徵的數量)
# 2. 初始化模型的參數
# 3. 循環:
#           3.1 計算當前損失(正向傳播)
#           3.2 計算當前梯度(反向傳播)
#           3.3 更新參數(梯度下降)
def sigmoid(z):
    return 1 / (1 + np.exp(-z))


# 初始化w,b
def iniialize_with_zeros(dim):
    """
    :param dim: 所要的w的維度
    :return: w,b
    """
    b = 0
    w = np.zeros(shape=(dim, 1))
    # 斷言保證格式的正確
    assert (w.shape == (dim, 1))
    assert (isinstance(b, float) or isinstance(b, int))
    return (w, b)


def propagate(w, b, X, Y):
    """
    :param w: 權重
    :param b: 偏差
    :param X: 訓練集
    :param Y: 標籤
    :return: cost,w,b
    """
    m = X.shape[1]
    # 正向傳播
    A = sigmoid(np.dot(w.T, X) + b)
    cost = -np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)) / m
    # 反向傳播
    dw = np.dot(X, (A - Y).T) / m
    db = np.sum(A - Y) / m

    assert (dw.shape == w.shape)
    assert (db.dtype == float)
    cost = np.squeeze(cost)
    assert (cost.shape == ())
    # 創建一個字典保存w,b
    grads = {
        'dw': dw,
        'db': db
    }
    return (grads, cost)


def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False):
    """
    此函數通過運行梯度下降算法來優化w和b
    :param X:輸入的訓練集
    :param Y:標籤
    :param w:權重
    :param b:偏差
    :param num_iterations:迭代次數
    :param learning_rate:學習率
    :param print_cost:打印時間
    :return:w,b,dw,db
            costs:優化期間計算的所有成本列表,將用於繪製學習曲線
    """
    costs = []
    for i in range(num_iterations):
        grads, cost = propagate(w, b, X, Y)
        dw = grads['dw']
        db = grads['db']
        # 更新參數
        w = w - learning_rate * dw
        b = b - learning_rate * db
        # 記錄成本
        if i % 100 == 0:
            costs.append(cost)
        if (print_cost) and (i % 100 == 0):
            print("迭代的次數: %i , 誤差值: %f" % (i, cost))
    params = {
        'w': w,
        'b': b
    }
    grads = {
        'dw': dw,
        'db': db
    }
    return (params, grads, costs)


def predict(w, b, X):
    """
    :param w:權重
    :param b:偏差
    :param X:訓練集
    :return:Y_prediction  - 包含X中所有圖片的所有預測【0 | 1】的一個numpy數組(向量)
    """
    m = X.shape[1]
    w = w.reshape(X.shape[0], 1)
    Y_prediction = np.zeros((1, m))

    # 預測貓在圖像中出現的概率
    A = sigmoid(np.dot(w.T, X) + b)
    for i in range(A.shape[1]):
        Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0
    assert (Y_prediction.shape == (1, m))
    return Y_prediction


def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
    """
    :param X_train:訓練集
    :param Y_train:訓練集標籤
    :param X_test:測試集
    :param Y_test:測試集標籤
    :param num_iterations:迭代次數
    :param learning_rate:學習率
    :param print_cost:是否打印
    :return:有關於所有信息的字典
    """
    w, b = iniialize_with_zeros(X_train.shape[0])
    params, grads, costs=optimize(w,b,X_train,Y_train,num_iterations,learning_rate,print_cost)
    #檢索w,b
    w=params['w']
    b=params['b']
    #預測訓練集和測試集
    Y_prediction_train=predict(w,b,X_train)
    Y_prediction_test=predict(w,b,X_test)
    #打印訓練後的準確性
    print("訓練集準確性:", format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100), "%")
    print("測試集準確性:", format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100), "%")
    d={
        'costs':costs,
        'Y_prediction_train':Y_prediction_train,
        'Y_prediction_test':Y_prediction_test,
        'w':w,
        'b':b,
        'learning_rate':learning_rate,
        'num_iterations':num_iterations
    }
    return d
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)

參考網址:https://blog.csdn.net/u013733326/article/details/79827273

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章