給定數據利用神經網路演算法模型進行計算,利用FP、BP演算法,求得模型最優值。 神經網路初步學習使用。 ...
給定數據利用神經網路演算法模型進行計算,利用FP、BP演算法,求得模型最優值。
神經網路初步學習使用。
1 import numpy as np 2 import matplotlib.pylab as plt 3 from numpy import * 4 from pylab import * 5 6 from sklearn.neural_network import MLPClassifier 7 8 # 中文 負號 9 mpl.rcParams['font.sans-serif']=['SimHei'] 10 matplotlib.rcParams['axes.unicode_minus']=False 11 12 X1 = [0.697,0.774,0.634,0.608,0.556,0.403,0.481,0.437,0.666,0.243,0.245,0.343,0.639,0.657,0.360,0.593,0.719] 13 X2 = [0.460,0.376,0.264,0.318,0.215,0.237,0.149,0.211,0.091,0.267,0.057,0.099,0.161,0.198,0.370,0.042,0.103] 14 Y = [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0] 15 16 # 數據初始化 17 m = len(X1) 18 X = np.c_[np.ones(m),X1,X2] 19 y = np.c_[Y] 20 21 # 數據重新洗牌/打亂 22 np.random.seed(3) 23 order=np.random.permutation(m) 24 X=X[order] 25 y=y[order] 26 27 # 將數據切割 分為訓練集和測試集 28 d = int(m*0.75) 29 train_X,test_X=np.split(X,[d,]) 30 train_y,test_y=np.split(y,[d,]) 31 32 # 定義Sigmoid函數g(z)函數 33 def g(z,deriv=False): 34 if deriv == True: 35 return z*(1-z) 36 return 1.0/(1.0+np.exp(-z)) 37 38 # 初始化theta值 定義神經網路網格結構 L1=3,L2=17,L3=1 39 np.random.seed(3) 40 theta1=2*np.random.random((3,17))-1 41 theta2=2*np.random.random((17,1))-1 42 43 # 初始化代價值 44 J_hietory = np.zeros(15000) 45 46 # 開始神經網路迭代 47 for i in range(15000): 48 # 前項傳播演算法 49 a1 = train_X 50 z2 = a1.dot(theta1) 51 a2 = g(z2) 52 z3 = a2.dot(theta2) 53 a3 = g(z3) 54 h = a3 55 56 57 # 記錄每次代價 58 J_hietory[i] = -1.0/m*(np.dot(train_y.T,np.log(h))+np.dot((1-train_y).T,np.log((1-h)))) 59 60 # 反向傳播演算法 61 62 # 每層的delta 63 delta_L3 = a3-train_y 64 delta_L2 = delta_L3.dot(theta2.T)*g(a2,True) 65 # 每層的deltatheta 66 deltatheta2 = 1.0/m*np.dot(a2.T,delta_L3) 67 deltatheta1 = 1.0/m*np.dot(a1.T,delta_L2) 68 # 每層的theta更新 69 theta2 -= 0.8*deltatheta2 70 theta1 -= 0.8*deltatheta1 71 72 plt.plot(J_hietory) 73 plt.show() 74 75 76 # 定義準確率函數 77 def testAccuracy(X,y): 78 m = X.shape[0] 79 count = 0 80 81 for i in range(m): 82 a1 = X[i] 83 z2 = a1.dot(theta1) 84 a2 = g(z2) 85 z3 = a2.dot(theta2) 86 a3 = g(z3) 87 h = a3 88 89 if bool(np.where(h>=0.5,1,0)) == bool(y[i]): 90 count += 1 91 return count/m 92 93 # 分別計算訓練集和測試集的準確率 94 print('訓練集的準確率:',testAccuracy(train_X,train_y)*100,'%') 95 print('訓練集的準確率:',testAccuracy(test_X,test_y)*100,'%')