啟用函式為relu函式
import numpy as np
n =64
# 樣本數
d_in =
1000
# 輸入維度
h =100
# 隱藏層維度
d_out =
10# 輸出維度
# 隨機建立一些訓練資料
x = np.random.randn(n, d_in)
y = np.random.randn(n, d_out)
# 隨機初始權重
w1 = np.random.randn(d_in, h)
w2 = np.random.randn(h, d_out)
# 設定學習率
learning_rate =1e-
6# 開始訓練
for t in
range
(500):
h = x.dot(w1)
# n*h
h_relu = np.maximum(h,0)
y_pred = h_relu.dot(w2)
# computer loss
loss = np.square(y_pred-y)
.sum()
# 最小均方誤差
print
(t, loss)
# backward pass
# computer the gradient
grad_y_pred =
2.0*
(y_pred - y)
grad_w2 = h_relu.t.dot(grad_y_pred)
grad_h_relu = grad_y_pred.dot(w2.t)
grad_h = grad_h_relu.copy(
) grad_h[h <0]
=0grad_w1 = x.t.dot(grad_h)
# update weights of w1 and w2
w1 -= learning_rate * grad_w1
w2 -= learning_rate * grad_w2
簡單bp演算法 1
import numpy as np sigmoid function sig函式和其導數整合 def nonlin x,deriv false if deriv true return x 1 x 如果deriv為true,求導數 return 1 1 np.exp x exp 是以e為底的指數函...
BP 演算法手動實現
github部落格傳送門 csdn部落格傳送門 numpy matplotlib 深度學習基礎網路模型 mnist手寫體識別資料集 import numpy as np import matplotlib.pyplot as plt x np.linspace 1,100,100 造出一些100個偽...
標準BP演算法用Python程式設計實現
1.需要匯入兩個模組 import pandas as pd import numpy as np 2.定於啟用函式 def sigmoid x return1 1 np.exp x 3.標準bp演算法 def bp x train,y train,numb,inta x train,y train...