欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

给定数据利用神经网络算法模型进行计算

程序员文章站 2022-07-10 22:13:49
给定数据利用神经网络算法模型进行计算,利用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,'%')