【中文】【吳恩達課後編程作業】Course 1 - 神經網絡和深度學習 - 第三週作業

#【吳恩達課後編程作業】Course 1 - 神經網絡和深度學習 - 第三週作業 - 帶有一個隱藏層的平面數據分類


上一篇:【 課程1 - 第三週測驗】※※※※※ 【回到目錄】※※※※※下一篇:【課程1 - 第四周測驗】

聲明

   首先聲明本文參考【Kulbear】的github上的文章,本文參考Planar data classification with one hidden layer,我基於他的文章加以自己的理解發表這篇博客,力求讓大家以最輕鬆的姿態理解吳恩達的視頻,如有不妥的地方歡迎大家指正。


本文所使用的資料已上傳到百度網盤**【點擊下載】**,提取碼:qifu,請在開始之前下載好所需資料,或者在本文底部copy資料代碼。


【博主使用的python版本:3.6.2】


開始之前

   在開始之前,我們簡單說一下我們要做什麼。我們要建立一個神經網絡,它有一個隱藏層。你會發現這個模型和上一個邏輯迴歸實現的模型有很大的區別。你可以跟隨我的步驟在Jupyter Notebook中一步步地把代碼填進去,也可以直接複製完整代碼,在完整代碼在本文底部,testCases.py和planar_utils.py的完整代碼也在最底部。在這篇文章中,我們會講到以下的知識:

  • 構建具有單隱藏層的2類分類神經網絡。
  • 使用具有非線性激活功能激活函數,例如tanh。
  • 計算交叉熵損失(損失函數)。
  • 實現向前和向後傳播。

準備軟件包

我們需要準備一些軟件包:

  • numpy:是用Python進行科學計算的基本軟件包。
  • sklearn:爲數據挖掘和數據分析提供的簡單高效的工具。
  • matplotlib :是一個用於在Python中繪製圖表的庫。
  • testCases:提供了一些測試示例來評估函數的正確性,參見下載的資料或者在底部查看它的代碼。
  • planar_utils :提供了在這個任務中使用的各種有用的功能,參見下載的資料或者在底部查看它的代碼。
import numpy as np
import matplotlib.pyplot as plt
from testCases import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets

#%matplotlib inline #如果你使用用的是Jupyter Notebook的話請取消註釋。

np.random.seed(1) #設置一個固定的隨機種子,以保證接下來的步驟中我們的結果是一致的。


加載和查看數據集

首先,我們來看看我們將要使用的數據集, 下面的代碼會將一個花的圖案的2類數據集加載到變量X和Y中。

X, Y = load_planar_dataset()

  把數據集加載完成了,然後使用matplotlib可視化數據集,代碼如下:

plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral) #繪製散點圖

# 上一語句如出現問題,請使用下面的語句:
plt.scatter(X[0, :], X[1, :], c=np.squeeze(Y), s=40, cmap=plt.cm.Spectral) #繪製散點圖

flower
   數據看起來像一朵紅色(y = 0)和一些藍色(y = 1)的數據點的花朵的圖案。 我們的目標是建立一個模型來適應這些數據。現在,我們已經有了以下的東西:

  • X:一個numpy的矩陣,包含了這些數據點的數值
  • Y:一個numpy的向量,對應着的是X的標籤【0 | 1】(紅色:0 , 藍色 :1)

我們繼續來仔細地看數據:

shape_X = X.shape
shape_Y = Y.shape
m = Y.shape[1]  # 訓練集裏面的數量

print ("X的維度爲: " + str(shape_X))
print ("Y的維度爲: " + str(shape_Y))
print ("數據集裏面的數據有:" + str(m) + " 個")

運行結果爲:

X的維度爲: (2, 400)
Y的維度爲: (1, 400)
數據集裏面的數據有:400

查看簡單的Logistic迴歸的分類效果

  在構建完整的神經網絡之前,先讓我們看看邏輯迴歸在這個問題上的表現如何,我們可以使用sklearn的內置函數來做到這一點, 運行下面的代碼來訓練數據集上的邏輯迴歸分類器。

clf = sklearn.linear_model.LogisticRegressionCV()
clf.fit(X.T,Y.T)

  這裏會打印出以下的信息(不同的機器提示大同小異):
  E:\Anaconda3\lib\site-packages\sklearn\utils\validation.py:547: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)

我們可以把邏輯迴歸分類器的分類繪製出來:

plot_decision_boundary(lambda x: clf.predict(x), X, Y) #繪製決策邊界
plt.title("Logistic Regression") #圖標題
LR_predictions  = clf.predict(X.T) #預測結果
print ("邏輯迴歸的準確性: %d " % float((np.dot(Y, LR_predictions) + 
		np.dot(1 - Y,1 - LR_predictions)) / float(Y.size) * 100) +
       "% " + "(正確標記的數據點所佔的百分比)")

我們看一看都打印了些什麼吧!

邏輯迴歸的準確性: 47 % (正確標記的數據點所佔的百分比)

Logistic Regression
準確性只有47%的原因是數據集不是線性可分的,所以邏輯迴歸表現不佳,現在我們正式開始構建神經網絡。


搭建神經網絡

我們要搭建的神經網絡模型如下圖:
Neural Network model image
當然還有我們的理論基礎(不懂可以去仔細看看視頻):
對於x(i)x^{(i)} 而言:
z[1](i)=W[1]x(i)+b[1](i)(1)z^{[1] (i)} = W^{[1]} x^{(i)} + b^{[1] (i)}\tag{1}
a[1](i)=tanh(z[1](i))(2)a^{[1] (i)} = \tanh(z^{[1] (i)})\tag{2}
z[2](i)=W[2]a[1](i)+b[2](i)(3)z^{[2] (i)} = W^{[2]} a^{[1] (i)} + b^{[2] (i)}\tag{3}
y^(i)=a[2](i)=σ(z[2](i))(4)\hat{y}^{(i)} = a^{[2] (i)} = \sigma(z^{ [2] (i)})\tag{4}
KaTeX parse error: Undefined control sequence: \mbox at position 42: …gin{cases} 1 & \̲m̲b̲o̲x̲{if } a^{[2](i)…
給出所有示例的預測結果,可以按如下方式計算成本J:
J=1mi=0m(y(i)log(a[2](i))+(1y(i))log(1a[2](i)))(6)J = - \frac{1}{m} \sum\limits_{i = 0}^{m} \large\left(\small y^{(i)}\log\left(a^{[2] (i)}\right) + (1-y^{(i)})\log\left(1- a^{[2] (i)}\right) \large \right) \small \tag{6}

構建神經網絡的一般方法是:

  1. 定義神經網絡結構(輸入單元的數量,隱藏單元的數量等)。
  2. 初始化模型的參數
  3. 循環
  • 實施前向傳播
  • 計算損失
  • 實現向後傳播
  • 更新參數(梯度下降)

  我們要它們合併到一個nn_model() 函數中,當我們構建好了nn_model()並學習了正確的參數,我們就可以預測新的數據。

定義神經網絡結構

在構建之前,我們要先把神經網絡的結構給定義好:

  • n_x: 輸入層的數量
  • n_h: 隱藏層的數量(這裏設置爲4)
  • n_y: 輸出層的數量
def layer_sizes(X , Y):
    """
    參數:
     X - 輸入數據集,維度爲(輸入的數量,訓練/測試的數量)
     Y - 標籤,維度爲(輸出的數量,訓練/測試數量)
    
    返回:
     n_x - 輸入層的數量
     n_h - 隱藏層的數量
     n_y - 輸出層的數量
    """
    n_x = X.shape[0] #輸入層
    n_h = 4 #,隱藏層,硬編碼爲4
    n_y = Y.shape[0] #輸出層
    
    return (n_x,n_h,n_y)

我們來測試一下:

#測試layer_sizes
print("=========================測試layer_sizes=========================")
X_asses , Y_asses = layer_sizes_test_case()
(n_x,n_h,n_y) =  layer_sizes(X_asses,Y_asses)
print("輸入層的節點數量爲: n_x = " + str(n_x))
print("隱藏層的節點數量爲: n_h = " + str(n_h))
print("輸出層的節點數量爲: n_y = " + str(n_y))

運行結果如下:

=========================測試layer_sizes=========================
輸入層的節點數量爲: n_x = 5
隱藏層的節點數量爲: n_h = 4
輸出層的節點數量爲: n_y = 2

初始化模型的參數

在這裏,我們要實現函數initialize_parameters()。我們要確保我們的參數大小合適,如果需要的話,請參考上面的神經網絡圖。
我們將會用隨機值初始化權重矩陣。

  • np.random.randn(a,b)* 0.01來隨機初始化一個維度爲(a,b)的矩陣。

將偏向量初始化爲零。

  • np.zeros((a,b))用零初始化矩陣(a,b)。
def initialize_parameters( n_x , n_h ,n_y):
    """
    參數:
        n_x - 輸入層節點的數量
        n_h - 隱藏層節點的數量
        n_y - 輸出層節點的數量
    
    返回:
        parameters - 包含參數的字典:
            W1 - 權重矩陣,維度爲(n_h,n_x)
            b1 - 偏向量,維度爲(n_h,1)
            W2 - 權重矩陣,維度爲(n_y,n_h)
            b2 - 偏向量,維度爲(n_y,1)

    """
    np.random.seed(2) #指定一個隨機種子,以便你的輸出與我們的一樣。
    W1 = np.random.randn(n_h,n_x) * 0.01
    b1 = np.zeros(shape=(n_h, 1))
    W2 = np.random.randn(n_y,n_h) * 0.01
    b2 = np.zeros(shape=(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

測試一下我們的代碼:

#測試initialize_parameters
print("=========================測試initialize_parameters=========================")    
n_x , n_h , n_y = initialize_parameters_test_case()
parameters = initialize_parameters(n_x , n_h , n_y)
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))

結果如下:

=========================測試initialize_parameters=========================
W1 = [[-0.00416758 -0.00056267]
 [-0.02136196  0.01640271]
 [-0.01793436 -0.00841747]
 [ 0.00502881 -0.01245288]]
b1 = [[ 0.]
 [ 0.]
 [ 0.]
 [ 0.]]
W2 = [[-0.01057952 -0.00909008  0.00551454  0.02292208]]
b2 = [[ 0.]]

循環

前向傳播

我們現在要實現前向傳播函數forward_propagation()。
我們可以使用sigmoid()函數,也可以使用np.tanh()函數。
步驟如下:

  • 使用字典類型的parameters(它是initialize_parameters() 的輸出)檢索每個參數。
  • 實現向前傳播, 計算Z[1],A[1],Z[2]Z^{[1]}, A^{[1]}, Z^{[2]}A[2]A^{[2]}( 訓練集裏面所有例子的預測向量)。
  • 反向傳播所需的值存儲在“cache”中,cache將作爲反向傳播函數的輸入。
def forward_propagation( X , parameters ):
    """
    參數:
         X - 維度爲(n_x,m)的輸入數據。
         parameters - 初始化函數(initialize_parameters)的輸出
    
    返回:
         A2 - 使用sigmoid()函數計算的第二次激活後的數值
         cache - 包含“Z1”,“A1”,“Z2”和“A2”的字典類型變量
     """
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    #前向傳播計算A2
    Z1 = np.dot(W1 , X) + b1
    A1 = np.tanh(Z1)
    Z2 = np.dot(W2 , A1) + b2
    A2 = sigmoid(Z2)
    #使用斷言確保我的數據格式是正確的
    assert(A2.shape == (1,X.shape[1]))
    cache = {"Z1": Z1,
             "A1": A1,
             "Z2": Z2,
             "A2": A2}
    
    return (A2, cache)

測試一下我的這個功能:

#測試forward_propagation
print("=========================測試forward_propagation=========================") 
X_assess, parameters = forward_propagation_test_case()
A2, cache = forward_propagation(X_assess, parameters)
print(np.mean(cache["Z1"]), np.mean(cache["A1"]), np.mean(cache["Z2"]), np.mean(cache["A2"]))

測試結果如下:

=========================測試forward_propagation=========================
-0.000499755777742 -0.000496963353232 0.000438187450959 0.500109546852

現在我們已經計算了A[2]A^{[2]}a[2](i)a^{[2](i)}包含了訓練集裏每個數值,現在我們就可以構建成本函數了。

計算損失

計算成本的公式如下:
J=1mi=0m(y(i)log(a[2](i))+(1y(i))log(1a[2](i)))(6)J = - \frac{1}{m} \sum\limits_{i = 0}^{m} \large\left(\small y^{(i)}\log\left(a^{[2] (i)}\right) + (1-y^{(i)})\log\left(1- a^{[2] (i)}\right) \large \right) \small \tag{6}
有很多的方法都可以計算交叉熵損失,比如下面的這個公式,我們在python中可以這麼實現:
KaTeX parse error: \tag works only in display equations:

logprobs = np.multiply(np.log(A2),Y)
cost = - np.sum(logprobs)                # 不需要使用循環就可以直接算出來。

當然,你也可以使用np.multiply()然後使用np.sum()或者直接使用np.dot()
現在我們正式開始構建計算成本的函數:

def compute_cost(A2,Y,parameters):
    """
    計算方程(6)中給出的交叉熵成本,
    
    參數:
         A2 - 使用sigmoid()函數計算的第二次激活後的數值
         Y - "True"標籤向量,維度爲(1,數量)
         parameters - 一個包含W1,B1,W2和B2的字典類型的變量
    
    返回:
         成本 - 交叉熵成本給出方程(13)
    """
    
    m = Y.shape[1]
    W1 = parameters["W1"]
    W2 = parameters["W2"]
    
    #計算成本
    logprobs = logprobs = np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2))
    cost = - np.sum(logprobs) / m
    cost = float(np.squeeze(cost))
    
    assert(isinstance(cost,float))
    
    return cost

測試一下我們的成本函數:

#測試compute_cost
print("=========================測試compute_cost=========================") 
A2 , Y_assess , parameters = compute_cost_test_case()
print("cost = " + str(compute_cost(A2,Y_assess,parameters)))

測試結果如下:

=========================測試compute_cost=========================
cost = 0.6929198937761266

使用正向傳播期間計算的cache,現在可以利用它實現反向傳播。

現在我們要開始實現函數backward_propagation()。

向後傳播

  說明:反向傳播通常是深度學習中最難(數學意義)部分,爲了幫助你,這裏有反向傳播講座的幻燈片, 由於我們正在構建向量化實現,因此我們將需要使用這下面的六個方程:
Summary of gradient desent
爲了計算dZ1,裏需要計算 g[1](Z[1])g^{[1]'}(Z^{[1]})g[1](...)g^{[1]}(...) 是tanh激活函數,如果a=g[1](z)a = g^{[1]}(z) 那麼g[1](z)=1a2g^{[1]'}(z) = 1-a^2。所以我們需要使用 (1 - np.power(A1, 2))來計算g[1](Z[1])g^{[1]'}(Z^{[1]})

def backward_propagation(parameters,cache,X,Y):
    """
    使用上述說明搭建反向傳播函數。
    
    參數:
     parameters - 包含我們的參數的一個字典類型的變量。
     cache - 包含“Z1”,“A1”,“Z2”和“A2”的字典類型的變量。
     X - 輸入數據,維度爲(2,數量)
     Y - “True”標籤,維度爲(1,數量)
    
    返回:
     grads - 包含W和b的導數一個字典類型的變量。
    """
    m = X.shape[1]
    
    W1 = parameters["W1"]
    W2 = parameters["W2"]
    
    A1 = cache["A1"]
    A2 = cache["A2"]
    
    dZ2= A2 - Y
    dW2 = (1 / m) * np.dot(dZ2, A1.T)
    db2 = (1 / m) * np.sum(dZ2, axis=1, keepdims=True)
    dZ1 = np.multiply(np.dot(W2.T, dZ2), 1 - np.power(A1, 2))
    dW1 = (1 / m) * np.dot(dZ1, X.T)
    db1 = (1 / m) * np.sum(dZ1, axis=1, keepdims=True)
    grads = {"dW1": dW1,
             "db1": db1,
             "dW2": dW2,
             "db2": db2 }
    
    return grads

測試一下反向傳播函數:

#測試backward_propagation
print("=========================測試backward_propagation=========================")
parameters, cache, X_assess, Y_assess = backward_propagation_test_case()

grads = backward_propagation(parameters, cache, X_assess, Y_assess)
print ("dW1 = "+ str(grads["dW1"]))
print ("db1 = "+ str(grads["db1"]))
print ("dW2 = "+ str(grads["dW2"]))
print ("db2 = "+ str(grads["db2"]))

測試結果如下:

=========================測試backward_propagation=========================
dW1 = [[ 0.01018708 -0.00708701]
 [ 0.00873447 -0.0060768 ]
 [-0.00530847  0.00369379]
 [-0.02206365  0.01535126]]
db1 = [[-0.00069728]
 [-0.00060606]
 [ 0.000364  ]
 [ 0.00151207]]
dW2 = [[ 0.00363613  0.03153604  0.01162914 -0.01318316]]
db2 = [[ 0.06589489]]

反向傳播完成了,我們開始對參數進行更新

更新參數

我們需要使用(dW1, db1, dW2, db2)來更新(W1, b1, W2, b2)。
更新算法如下:
$ \theta = \theta - \alpha \frac{\partial J }{ \partial \theta }$

  • α\alpha:學習速率
  • θ\theta :參數

我們需要選擇一個良好的學習速率,我們可以看一下下面這兩個圖(由Adam Harley提供):
sgdsgd_bad
上面兩個圖分別代表了具有良好學習速率(收斂)和不良學習速率(發散)的梯度下降算法。

def update_parameters(parameters,grads,learning_rate=1.2):
    """
    使用上面給出的梯度下降更新規則更新參數
    
    參數:
     parameters - 包含參數的字典類型的變量。
     grads - 包含導數值的字典類型的變量。
     learning_rate - 學習速率
    
    返回:
     parameters - 包含更新參數的字典類型的變量。
    """
    W1,W2 = parameters["W1"],parameters["W2"]
    b1,b2 = parameters["b1"],parameters["b2"]
    
    dW1,dW2 = grads["dW1"],grads["dW2"]
    db1,db2 = grads["db1"],grads["db2"]
    
    W1 = W1 - learning_rate * dW1
    b1 = b1 - learning_rate * db1
    W2 = W2 - learning_rate * dW2
    b2 = b2 - learning_rate * db2
    
    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}
    
    return parameters

測試一下update_parameters():

#測試update_parameters
print("=========================測試update_parameters=========================")
parameters, grads = update_parameters_test_case()
parameters = update_parameters(parameters, grads)

print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))

測試結果如下:

=========================測試update_parameters=========================
W1 = [[-0.00643025  0.01936718]
 [-0.02410458  0.03978052]
 [-0.01653973 -0.02096177]
 [ 0.01046864 -0.05990141]]
b1 = [[ -1.02420756e-06]
 [  1.27373948e-05]
 [  8.32996807e-07]
 [ -3.20136836e-06]]
W2 = [[-0.01041081 -0.04463285  0.01758031  0.04747113]]
b2 = [[ 0.00010457]]

整合

我們現在把上面的東西整合到nn_model()中,神經網絡模型必須以正確的順序使用先前的功能。

def nn_model(X,Y,n_h,num_iterations,print_cost=False):
    """
    參數:
        X - 數據集,維度爲(2,示例數)
        Y - 標籤,維度爲(1,示例數)
        n_h - 隱藏層的數量
        num_iterations - 梯度下降循環中的迭代次數
        print_cost - 如果爲True,則每1000次迭代打印一次成本數值
    
    返回:
        parameters - 模型學習的參數,它們可以用來進行預測。
     """
     
    np.random.seed(3) #指定隨機種子
    n_x = layer_sizes(X, Y)[0]
    n_y = layer_sizes(X, Y)[2]
    
    parameters = initialize_parameters(n_x,n_h,n_y)
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    
    for i in range(num_iterations):
        A2 , cache = forward_propagation(X,parameters)
        cost = compute_cost(A2,Y,parameters)
        grads = backward_propagation(parameters,cache,X,Y)
        parameters = update_parameters(parameters,grads,learning_rate = 0.5)
        
        if print_cost:
            if i%1000 == 0:
                print("第 ",i," 次循環,成本爲:"+str(cost))
    return parameters

測試nn_model():

#測試nn_model
print("=========================測試nn_model=========================")
X_assess, Y_assess = nn_model_test_case()

parameters = nn_model(X_assess, Y_assess, 4, num_iterations=10000, print_cost=False)
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))

測試結果如下:

=========================測試nn_model=========================
W1 = [[-4.18494482  5.33220319]
 [-7.52989354  1.24306197]
 [-4.19295428  5.32631786]
 [ 7.52983748 -1.24309404]]
b1 = [[ 2.32926815]
 [ 3.7945905 ]
 [ 2.33002544]
 [-3.79468791]]
W2 = [[-6033.83672179 -6008.12981272 -6033.10095329  6008.06636901]]
b2 = [[-52.66607704]]

參數更新完了我們就可以來進行預測了。

預測

構建predict()來使用模型進行預測, 使用向前傳播來預測結果。
predictions = KaTeX parse error: Undefined control sequence: \ at position 46: …1 & 激活值> 0.5 \\\̲ ̲ 0 & \text…

def predict(parameters,X):
    """
    使用學習的參數,爲X中的每個示例預測一個類
    
    參數:
		parameters - 包含參數的字典類型的變量。
	    X - 輸入數據(n_x,m)
    
    返回
		predictions - 我們模型預測的向量(紅色:0 /藍色:1)
     
     """
    A2 , cache = forward_propagation(X,parameters)
    predictions = np.round(A2)
    
    return predictions

測試一下predict

#測試predict
print("=========================測試predict=========================")

parameters, X_assess = predict_test_case()

predictions = predict(parameters, X_assess)
print("預測的平均值 = " + str(np.mean(predictions)))

測試結果:

=========================測試predict=========================
預測的平均值 = 0.666666666667

現在我們把所有的東西基本都做完了,我們開始正式運行。


正式運行

parameters = nn_model(X, Y, n_h = 4, num_iterations=10000, print_cost=True)

#繪製邊界
plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
plt.title("Decision Boundary for hidden layer size " + str(4))

predictions = predict(parameters, X)
print ('準確率: %d' % float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100) + '%')

運行結果:

0  次循環,成本爲:0.69304802012398231000  次循環,成本爲:0.288083293569018352000  次循環,成本爲:0.254385494073244963000  次循環,成本爲:0.233864150389521964000  次循環,成本爲:0.226792487448540085000  次循環,成本爲:0.222644275492990156000  次循環,成本爲:0.219731404042813167000  次循環,成本爲:0.217503654051312948000  次循環,成本爲:0.219503964694673159000  次循環,成本爲:0.2185709575018246
準確率: 90%

resulat


更改隱藏層節點數量

我們上面的實驗把隱藏層定爲4個節點,現在我們更改隱藏層裏面的節點數量,看一看節點數量是否會對結果造成影響。

plt.figure(figsize=(16, 32))
hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50] #隱藏層數量
for i, n_h in enumerate(hidden_layer_sizes):
    plt.subplot(5, 2, i + 1)
    plt.title('Hidden Layer of size %d' % n_h)
    parameters = nn_model(X, Y, n_h, num_iterations=5000)
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    predictions = predict(parameters, X)
    accuracy = float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100)
    print ("隱藏層的節點數量: {}  ,準確率: {} %".format(n_h, accuracy))

打印結果:

隱藏層的節點數量: 1  ,準確率: 67.5 %
隱藏層的節點數量: 2  ,準確率: 67.25 %
隱藏層的節點數量: 3  ,準確率: 90.75 %
隱藏層的節點數量: 4  ,準確率: 90.5 %
隱藏層的節點數量: 5  ,準確率: 91.25 %
隱藏層的節點數量: 20  ,準確率: 90.0 %
隱藏層的節點數量: 50  ,準確率: 90.75 %

units

較大的模型(具有更多隱藏單元)能夠更好地適應訓練集,直到最終的最大模型過度擬合數據。
最好的隱藏層大小似乎在n_h = 5附近。實際上,這裏的值似乎很適合數據,而且不會引起過度擬合。
我們還將在後面學習有關正則化的知識,它允許我們使用非常大的模型(如n_h = 50),而不會出現太多過度擬合。


##【可選】探索

  • 當改變sigmoid激活或ReLU激活的tanh激活時會發生什麼?
  • 改變learning_rate的數值會發生什麼
  • 如果我們改變數據集呢?
# 數據集
noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure = load_extra_datasets()

datasets = {"noisy_circles": noisy_circles,
            "noisy_moons": noisy_moons,
            "blobs": blobs,
            "gaussian_quantiles": gaussian_quantiles}

dataset = "noisy_moons"

X, Y = datasets[dataset]
X, Y = X.T, Y.reshape(1, Y.shape[0])

if dataset == "blobs":
    Y = Y % 2

plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral)

#上一語句如出現問題請使用下面的語句:
plt.scatter(X[0, :], X[1, :], c=np.squeeze(Y), s=40, cmap=plt.cm.Spectral)

new dataset


完整代碼

作業代碼

# -*- coding: utf-8 -*-
"""
本文博客地址:https://blog.csdn.net/u013733326/article/details/79702148

@author: Oscar
"""

import numpy as np
import matplotlib.pyplot as plt
from testCases import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets

#%matplotlib inline #如果你使用用的是Jupyter Notebook的話請取消註釋。

np.random.seed(1) #設置一個固定的隨機種子,以保證接下來的步驟中我們的結果是一致的。

X, Y = load_planar_dataset()
#plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral) #繪製散點圖
shape_X = X.shape
shape_Y = Y.shape
m = Y.shape[1]  # 訓練集裏面的數量

print ("X的維度爲: " + str(shape_X))
print ("Y的維度爲: " + str(shape_Y))
print ("數據集裏面的數據有:" + str(m) + " 個")

def layer_sizes(X , Y):
    """
    參數:
     X - 輸入數據集,維度爲(輸入的數量,訓練/測試的數量)
     Y - 標籤,維度爲(輸出的數量,訓練/測試數量)

    返回:
     n_x - 輸入層的數量
     n_h - 隱藏層的數量
     n_y - 輸出層的數量
    """
    n_x = X.shape[0] #輸入層
    n_h = 4 #,隱藏層,硬編碼爲4
    n_y = Y.shape[0] #輸出層

    return (n_x,n_h,n_y)

def initialize_parameters( n_x , n_h ,n_y):
    """
    參數:
        n_x - 輸入節點的數量
        n_h - 隱藏層節點的數量
        n_y - 輸出層節點的數量

    返回:
        parameters - 包含參數的字典:
            W1 - 權重矩陣,維度爲(n_h,n_x)
            b1 - 偏向量,維度爲(n_h,1)
            W2 - 權重矩陣,維度爲(n_y,n_h)
            b2 - 偏向量,維度爲(n_y,1)

    """
    np.random.seed(2) #指定一個隨機種子,以便你的輸出與我們的一樣。
    W1 = np.random.randn(n_h,n_x) * 0.01
    b1 = np.zeros(shape=(n_h, 1))
    W2 = np.random.randn(n_y,n_h) * 0.01
    b2 = np.zeros(shape=(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 forward_propagation( X , parameters ):
    """
    參數:
         X - 維度爲(n_x,m)的輸入數據。
         parameters - 初始化函數(initialize_parameters)的輸出

    返回:
         A2 - 使用sigmoid()函數計算的第二次激活後的數值
         cache - 包含“Z1”,“A1”,“Z2”和“A2”的字典類型變量
     """
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    #前向傳播計算A2
    Z1 = np.dot(W1 , X) + b1
    A1 = np.tanh(Z1)
    Z2 = np.dot(W2 , A1) + b2
    A2 = sigmoid(Z2)
    #使用斷言確保我的數據格式是正確的
    assert(A2.shape == (1,X.shape[1]))
    cache = {"Z1": Z1,
             "A1": A1,
             "Z2": Z2,
             "A2": A2}

    return (A2, cache)

def compute_cost(A2,Y,parameters):
    """
    計算方程(6)中給出的交叉熵成本,

    參數:
         A2 - 使用sigmoid()函數計算的第二次激活後的數值
         Y - "True"標籤向量,維度爲(1,數量)
         parameters - 一個包含W1,B1,W2和B2的字典類型的變量

    返回:
         成本 - 交叉熵成本給出方程(13)
    """

    m = Y.shape[1]
    W1 = parameters["W1"]
    W2 = parameters["W2"]

    #計算成本
    logprobs = logprobs = np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2))
    cost = - np.sum(logprobs) / m
    cost = float(np.squeeze(cost))

    assert(isinstance(cost,float))

    return cost

def backward_propagation(parameters,cache,X,Y):
    """
    使用上述說明搭建反向傳播函數。

    參數:
     parameters - 包含我們的參數的一個字典類型的變量。
     cache - 包含“Z1”,“A1”,“Z2”和“A2”的字典類型的變量。
     X - 輸入數據,維度爲(2,數量)
     Y - “True”標籤,維度爲(1,數量)

    返回:
     grads - 包含W和b的導數一個字典類型的變量。
    """
    m = X.shape[1]

    W1 = parameters["W1"]
    W2 = parameters["W2"]

    A1 = cache["A1"]
    A2 = cache["A2"]

    dZ2= A2 - Y
    dW2 = (1 / m) * np.dot(dZ2, A1.T)
    db2 = (1 / m) * np.sum(dZ2, axis=1, keepdims=True)
    dZ1 = np.multiply(np.dot(W2.T, dZ2), 1 - np.power(A1, 2))
    dW1 = (1 / m) * np.dot(dZ1, X.T)
    db1 = (1 / m) * np.sum(dZ1, axis=1, keepdims=True)
    grads = {"dW1": dW1,
             "db1": db1,
             "dW2": dW2,
             "db2": db2 }

    return grads

def update_parameters(parameters,grads,learning_rate=1.2):
    """
    使用上面給出的梯度下降更新規則更新參數

    參數:
     parameters - 包含參數的字典類型的變量。
     grads - 包含導數值的字典類型的變量。
     learning_rate - 學習速率

    返回:
     parameters - 包含更新參數的字典類型的變量。
    """
    W1,W2 = parameters["W1"],parameters["W2"]
    b1,b2 = parameters["b1"],parameters["b2"]

    dW1,dW2 = grads["dW1"],grads["dW2"]
    db1,db2 = grads["db1"],grads["db2"]

    W1 = W1 - learning_rate * dW1
    b1 = b1 - learning_rate * db1
    W2 = W2 - learning_rate * dW2
    b2 = b2 - learning_rate * db2

    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters

def nn_model(X,Y,n_h,num_iterations,print_cost=False):
    """
    參數:
        X - 數據集,維度爲(2,示例數)
        Y - 標籤,維度爲(1,示例數)
        n_h - 隱藏層的數量
        num_iterations - 梯度下降循環中的迭代次數
        print_cost - 如果爲True,則每1000次迭代打印一次成本數值

    返回:
        parameters - 模型學習的參數,它們可以用來進行預測。
     """

    np.random.seed(3) #指定隨機種子
    n_x = layer_sizes(X, Y)[0]
    n_y = layer_sizes(X, Y)[2]

    parameters = initialize_parameters(n_x,n_h,n_y)
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]

    for i in range(num_iterations):
        A2 , cache = forward_propagation(X,parameters)
        cost = compute_cost(A2,Y,parameters)
        grads = backward_propagation(parameters,cache,X,Y)
        parameters = update_parameters(parameters,grads,learning_rate = 0.5)

        if print_cost:
            if i%1000 == 0:
                print("第 ",i," 次循環,成本爲:"+str(cost))
    return parameters

def predict(parameters,X):
    """
    使用學習的參數,爲X中的每個示例預測一個類

    參數:
        parameters - 包含參數的字典類型的變量。
        X - 輸入數據(n_x,m)

    返回
        predictions - 我們模型預測的向量(紅色:0 /藍色:1)

     """
    A2 , cache = forward_propagation(X,parameters)
    predictions = np.round(A2)

    return predictions

parameters = nn_model(X, Y, n_h = 4, num_iterations=10000, print_cost=True)

#繪製邊界
plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
plt.title("Decision Boundary for hidden layer size " + str(4))

predictions = predict(parameters, X)
print ('準確率: %d' % float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100) + '%')

"""
plt.figure(figsize=(16, 32))
hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50] #隱藏層數量
for i, n_h in enumerate(hidden_layer_sizes):
    plt.subplot(5, 2, i + 1)
    plt.title('Hidden Layer of size %d' % n_h)
    parameters = nn_model(X, Y, n_h, num_iterations=5000)
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    predictions = predict(parameters, X)
    accuracy = float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100)
    print ("隱藏層的節點數量: {}  ,準確率: {} %".format(n_h, accuracy))
"""

testCases.py

#-*- coding: UTF-8 -*-
"""
# WANGZHE12
"""
import numpy as np

def layer_sizes_test_case():
    np.random.seed(1)
    X_assess = np.random.randn(5, 3)
    Y_assess = np.random.randn(2, 3)
    return X_assess, Y_assess

def initialize_parameters_test_case():
    n_x, n_h, n_y = 2, 4, 1
    return n_x, n_h, n_y

def forward_propagation_test_case():
    np.random.seed(1)
    X_assess = np.random.randn(2, 3)

    parameters = {'W1': np.array([[-0.00416758, -0.00056267],
        [-0.02136196,  0.01640271],
        [-0.01793436, -0.00841747],
        [ 0.00502881, -0.01245288]]),
     'W2': np.array([[-0.01057952, -0.00909008,  0.00551454,  0.02292208]]),
     'b1': np.array([[ 0.],
        [ 0.],
        [ 0.],
        [ 0.]]),
     'b2': np.array([[ 0.]])}

    return X_assess, parameters

def compute_cost_test_case():
    np.random.seed(1)
    Y_assess = np.random.randn(1, 3)
    parameters = {'W1': np.array([[-0.00416758, -0.00056267],
        [-0.02136196,  0.01640271],
        [-0.01793436, -0.00841747],
        [ 0.00502881, -0.01245288]]),
     'W2': np.array([[-0.01057952, -0.00909008,  0.00551454,  0.02292208]]),
     'b1': np.array([[ 0.],
        [ 0.],
        [ 0.],
        [ 0.]]),
     'b2': np.array([[ 0.]])}

    a2 = (np.array([[ 0.5002307 ,  0.49985831,  0.50023963]]))

    return a2, Y_assess, parameters

def backward_propagation_test_case():
    np.random.seed(1)
    X_assess = np.random.randn(2, 3)
    Y_assess = np.random.randn(1, 3)
    parameters = {'W1': np.array([[-0.00416758, -0.00056267],
        [-0.02136196,  0.01640271],
        [-0.01793436, -0.00841747],
        [ 0.00502881, -0.01245288]]),
     'W2': np.array([[-0.01057952, -0.00909008,  0.00551454,  0.02292208]]),
     'b1': np.array([[ 0.],
        [ 0.],
        [ 0.],
        [ 0.]]),
     'b2': np.array([[ 0.]])}

    cache = {'A1': np.array([[-0.00616578,  0.0020626 ,  0.00349619],
         [-0.05225116,  0.02725659, -0.02646251],
         [-0.02009721,  0.0036869 ,  0.02883756],
         [ 0.02152675, -0.01385234,  0.02599885]]),
  'A2': np.array([[ 0.5002307 ,  0.49985831,  0.50023963]]),
  'Z1': np.array([[-0.00616586,  0.0020626 ,  0.0034962 ],
         [-0.05229879,  0.02726335, -0.02646869],
         [-0.02009991,  0.00368692,  0.02884556],
         [ 0.02153007, -0.01385322,  0.02600471]]),
  'Z2': np.array([[ 0.00092281, -0.00056678,  0.00095853]])}
    return parameters, cache, X_assess, Y_assess

def update_parameters_test_case():
    parameters = {'W1': np.array([[-0.00615039,  0.0169021 ],
        [-0.02311792,  0.03137121],
        [-0.0169217 , -0.01752545],
        [ 0.00935436, -0.05018221]]),
 'W2': np.array([[-0.0104319 , -0.04019007,  0.01607211,  0.04440255]]),
 'b1': np.array([[ -8.97523455e-07],
        [  8.15562092e-06],
        [  6.04810633e-07],
        [ -2.54560700e-06]]),
 'b2': np.array([[  9.14954378e-05]])}

    grads = {'dW1': np.array([[ 0.00023322, -0.00205423],
        [ 0.00082222, -0.00700776],
        [-0.00031831,  0.0028636 ],
        [-0.00092857,  0.00809933]]),
 'dW2': np.array([[ -1.75740039e-05,   3.70231337e-03,  -1.25683095e-03,
          -2.55715317e-03]]),
 'db1': np.array([[  1.05570087e-07],
        [ -3.81814487e-06],
        [ -1.90155145e-07],
        [  5.46467802e-07]]),
 'db2': np.array([[ -1.08923140e-05]])}
    return parameters, grads

def nn_model_test_case():
    np.random.seed(1)
    X_assess = np.random.randn(2, 3)
    Y_assess = np.random.randn(1, 3)
    return X_assess, Y_assess

def predict_test_case():
    np.random.seed(1)
    X_assess = np.random.randn(2, 3)
    parameters = {'W1': np.array([[-0.00615039,  0.0169021 ],
        [-0.02311792,  0.03137121],
        [-0.0169217 , -0.01752545],
        [ 0.00935436, -0.05018221]]),
     'W2': np.array([[-0.0104319 , -0.04019007,  0.01607211,  0.04440255]]),
     'b1': np.array([[ -8.97523455e-07],
        [  8.15562092e-06],
        [  6.04810633e-07],
        [ -2.54560700e-06]]),
     'b2': np.array([[  9.14954378e-05]])}
    return parameters, X_assess


planar_utils.py

import matplotlib.pyplot as plt
import numpy as np
import sklearn
import sklearn.datasets
import sklearn.linear_model

def plot_decision_boundary(model, X, y):
    # Set min and max values and give it some padding
    x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
    y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
    h = 0.01
    # Generate a grid of points with distance h between them
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    # Predict the function value for the whole grid
    Z = model(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    # Plot the contour and training examples
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
    plt.ylabel('x2')
    plt.xlabel('x1')
    plt.scatter(X[0, :], X[1, :], c=np.squeeze(y), cmap=plt.cm.Spectral)


def sigmoid(x):
    s = 1/(1+np.exp(-x))
    return s

def load_planar_dataset():
    np.random.seed(1)
    m = 400 # number of examples
    N = int(m/2) # number of points per class
    D = 2 # dimensionality
    X = np.zeros((m,D)) # data matrix where each row is a single example
    Y = np.zeros((m,1), dtype='uint8') # labels vector (0 for red, 1 for blue)
    a = 4 # maximum ray of the flower

    for j in range(2):
        ix = range(N*j,N*(j+1))
        t = np.linspace(j*3.12,(j+1)*3.12,N) + np.random.randn(N)*0.2 # theta
        r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius
        X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
        Y[ix] = j

    X = X.T
    Y = Y.T

    return X, Y

def load_extra_datasets():  
    N = 200
    noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3)
    noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2)
    blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6)
    gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None)
    no_structure = np.random.rand(N, 2), np.random.rand(N, 2)

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