機器學習(四):BP神經網路_手寫數字識別_Python
阿新 • • 發佈:2019-01-08
機器學習演算法Python實現
三、BP神經網路
1、神經網路model
先介紹個三層的神經網路,如下圖所示
- 輸入層(input layer)有三個units(為補上的bias,通常設為
1
) - 表示第
j
層的第i
個激勵,也稱為為單元unit - 為第
j
層到第j+1
層對映的權重矩陣,就是每條邊的權重
- 輸入層(input layer)有三個units(為補上的bias,通常設為
所以可以得到:
- 隱含層:
- 輸出層
其中,S型函式,也成為激勵函式
- 隱含層:
- 可以看出 為3x4的矩陣,為1x4的矩陣
- ==》
j+1
的單元數x(j
層的單元數+1)
- ==》
2、代價函式
- 假設最後輸出的,即代表輸出層有K個單元
- 其中,代表第
i
個單元輸出 - 與邏輯迴歸的代價函式
3、正則化
L
–>所有層的個數- –>第
l
層unit的個數 - 正則化後的代價函式為
![enter description here][16]
- 共有
L-1
層, - 然後是累加對應每一層的theta矩陣,注意不包含加上偏置項對應的theta(0)
- 共有
- 正則化後的代價函式實現程式碼:
# 代價函式
def nnCostFunction(nn_params,input_layer_size,hidden_layer_size,num_labels,X,y,Lambda):
length = nn_params.shape[0] # theta的中長度
# 還原theta1和theta2
Theta1 = nn_params[0:hidden_layer_size*(input_layer_size+1)].reshape(hidden_layer_size,input_layer_size+1)
Theta2 = nn_params[hidden_layer_size*(input_layer_size+1):length].reshape(num_labels,hidden_layer_size+1)
# np.savetxt("Theta1.csv",Theta1,delimiter=',')
m = X.shape[0 ]
class_y = np.zeros((m,num_labels)) # 資料的y對應0-9,需要對映為0/1的關係
# 對映y
for i in range(num_labels):
class_y[:,i] = np.int32(y==i).reshape(1,-1) # 注意reshape(1,-1)才可以賦值
'''去掉theta1和theta2的第一列,因為正則化時從1開始'''
Theta1_colCount = Theta1.shape[1]
Theta1_x = Theta1[:,1:Theta1_colCount]
Theta2_colCount = Theta2.shape[1]
Theta2_x = Theta2[:,1:Theta2_colCount]
# 正則化向theta^2
term = np.dot(np.transpose(np.vstack((Theta1_x.reshape(-1,1),Theta2_x.reshape(-1,1)))),np.vstack((Theta1_x.reshape(-1,1),Theta2_x.reshape(-1,1))))
'''正向傳播,每次需要補上一列1的偏置bias'''
a1 = np.hstack((np.ones((m,1)),X))
z2 = np.dot(a1,np.transpose(Theta1))
a2 = sigmoid(z2)
a2 = np.hstack((np.ones((m,1)),a2))
z3 = np.dot(a2,np.transpose(Theta2))
h = sigmoid(z3)
'''代價'''
J = -(np.dot(np.transpose(class_y.reshape(-1,1)),np.log(h.reshape(-1,1)))+np.dot(np.transpose(1-class_y.reshape(-1,1)),np.log(1-h.reshape(-1,1)))-Lambda*term/2)/m
return np.ravel(J)
4、反向傳播BP
- 上面正向傳播可以計算得到
J(θ)
,使用梯度下降法還需要求它的梯度 - BP反向傳播的目的就是求代價函式的梯度
- 假設4層的神經網路,記為–>
l
層第j
個單元的誤差
- 《===》(向量化)
- 沒有,因為對於輸入沒有誤差
因為S型函式的倒數為:,所以上面的和可以在前向傳播中計算出來
反向傳播計算梯度的過程為:
- (是大寫的)
- for i=1-m:
-
-正向傳播計算(l=2,3,4…L)
-反向計算、…;
-
-
最後,即得到代價函式的梯度
- 實現程式碼:
# 梯度
def nnGradient(nn_params,input_layer_size,hidden_layer_size,num_labels,X,y,Lambda):
length = nn_params.shape[0]
Theta1 = nn_params[0:hidden_layer_size*(input_layer_size+1)].reshape(hidden_layer_size,input_layer_size+1)
Theta2 = nn_params[hidden_layer_size*(input_layer_size+1):length].reshape(num_labels,hidden_layer_size+1)
m = X.shape[0]
class_y = np.zeros((m,num_labels)) # 資料的y對應0-9,需要對映為0/1的關係
# 對映y
for i in range(num_labels):
class_y[:,i] = np.int32(y==i).reshape(1,-1) # 注意reshape(1,-1)才可以賦值
'''去掉theta1和theta2的第一列,因為正則化時從1開始'''
Theta1_colCount = Theta1.shape[1]
Theta1_x = Theta1[:,1:Theta1_colCount]
Theta2_colCount = Theta2.shape[1]
Theta2_x = Theta2[:,1:Theta2_colCount]
Theta1_grad = np.zeros((Theta1.shape)) #第一層到第二層的權重
Theta2_grad = np.zeros((Theta2.shape)) #第二層到第三層的權重
Theta1[:,0] = 0;
Theta2[:,0] = 0;
'''正向傳播,每次需要補上一列1的偏置bias'''
a1 = np.hstack((np.ones((m,1)),X))
z2 = np.dot(a1,np.transpose(Theta1))
a2 = sigmoid(z2)
a2 = np.hstack((np.ones((m,1)),a2))
z3 = np.dot(a2,np.transpose(Theta2))
h = sigmoid(z3)
'''反向傳播,delta為誤差,'''
delta3 = np.zeros((m,num_labels))
delta2 = np.zeros((m,hidden_layer_size))
for i in range(m):
delta3[i,:] = h[i,:]-class_y[i,:]
Theta2_grad = Theta2_grad+np.dot(np.transpose(delta3[i,:].reshape(1,-1)),a2[i,:].reshape(1,-1))
delta2[i,:] = np.dot(delta3[i,:].reshape(1,-1),Theta2_x)*sigmoidGradient(z2[i,:])
Theta1_grad = Theta1_grad+np.dot(np.transpose(delta2[i,:].reshape(1,-1)),a1[i,:].reshape(1,-1))
'''梯度'''
grad = (np.vstack((Theta1_grad.reshape(-1,1),Theta2_grad.reshape(-1,1)))+Lambda*np.vstack((Theta1.reshape(-1,1),Theta2.reshape(-1,1))))/m
return np.ravel(grad)
5、BP可以求梯度的原因
- 實際是利用了
鏈式求導
法則 - 因為下一層的單元利用上一層的單元作為輸入進行計算
- 大體的推導過程如下,最終我們是想預測函式與已知的
y
非常接近,求均方差的梯度沿著此梯度方向可使代價函式最小化。可對照上面求梯度的過程。
- 求誤差更詳細的推到過程:
6、梯度檢查
- 檢查利用
BP
求的梯度是否正確 - 利用導數的定義驗證:
- 求出來的數值梯度應該與BP求出的梯度非常接近
- 驗證BP正確後就不需要再執行驗證梯度的演算法了
- 實現程式碼:
# 檢驗梯度是否計算正確
# 檢驗梯度是否計算正確
def checkGradient(Lambda = 0):
'''構造一個小型的神經網路驗證,因為數值法計算梯度很浪費時間,而且驗證正確後之後就不再需要驗證了'''
input_layer_size = 3
hidden_layer_size = 5
num_labels = 3
m = 5
initial_Theta1 = debugInitializeWeights(input_layer_size,hidden_layer_size);
initial_Theta2 = debugInitializeWeights(hidden_layer_size,num_labels)
X = debugInitializeWeights(input_layer_size-1,m)
y = 1+np.transpose(np.mod(np.arange(1,m+1), num_labels))# 初始化y
y = y.reshape(-1,1)
nn_params = np.vstack((initial_Theta1.reshape(-1,1),initial_Theta2.reshape(-1,1))) #展開theta
'''BP求出梯度'''
grad = nnGradient(nn_params, input_layer_size, hidden_layer_size,
num_labels, X, y, Lambda)
'''使用數值法計算梯度'''
num_grad = np.zeros((nn_params.shape[0]))
step = np.zeros((nn_params.shape[0]))
e = 1e-4
for i in range(nn_params.shape[0]):
step[i] = e
loss1 = nnCostFunction(nn_params-step.reshape(-1,1), input_layer_size, hidden_layer_size,
num_labels, X, y,
Lambda)
loss2 = nnCostFunction(nn_params+step.reshape(-1,1), input_layer_size, hidden_layer_size,
num_labels, X, y,
Lambda)
num_grad[i] = (loss2-loss1)/(2*e)
step[i]=0
# 顯示兩列比較
res = np.hstack((num_grad.reshape(-1,1),grad.reshape(-1,1)))
print res
7、權重的隨機初始化
- 神經網路不能像邏輯迴歸那樣初始化
theta
為0
,因為若是每條邊的權重都為0,每個神經元都是相同的輸出,在反向傳播中也會得到同樣的梯度,最終只會預測一種結果。 - 所以應該初始化為接近0的數
- 實現程式碼
# 隨機初始化權重theta
def randInitializeWeights(L_in,L_out):
W = np.zeros((L_out,1+L_in)) # 對應theta的權重
epsilon_init = (6.0/(L_out+L_in))**0.5
W = np.random.rand(L_out,1+L_in)*2*epsilon_init-epsilon_init # np.random.rand(L_out,1+L_in)產生L_out*(1+L_in)大小的隨機矩陣
return W
8、預測
- 正向傳播預測結果
- 實現程式碼
# 預測
def predict(Theta1,Theta2,X):
m = X.shape[0]
num_labels = Theta2.shape[0]
#p = np.zeros((m,1))
'''正向傳播,預測結果'''
X = np.hstack((np.ones((m,1)),X))
h1 = sigmoid(np.dot(X,np.transpose(Theta1)))
h1 = np.hstack((np.ones((m,1)),h1))
h2 = sigmoid(np.dot(h1,np.transpose(Theta2)))
'''
返回h中每一行最大值所在的列號
- np.max(h, axis=1)返回h中每一行的最大值(是某個數字的最大概率)
- 最後where找到的最大概率所在的列號(列號即是對應的數字)
'''
#np.savetxt("h2.csv",h2,delimiter=',')
p = np.array(np.where(h2[0,:] == np.max(h2, axis=1)[0]))
for i in np.arange(1, m):
t = np.array(np.where(h2[i,:] == np.max(h2, axis=1)[i]))
p = np.vstack((p,t))
return p
9、輸出結果
- 梯度檢查:
- 隨機顯示100個手寫數字
- 顯示theta1權重
- 訓練集預測準確度
- 歸一化後訓練集預測準確度