BP神经网络用于Iris数据集的分类

  全连接神经网络BP算法的原理在此不再赘述了,网上有大量的资料可以参考,我就直接贴代码:(用着还行的,帮忙点个推荐啊)

  1 # _*_ coding:utf-8 _*_
  2 import numpy as np
  3 import matplotlib.pyplot as plt
  4 import math
  5 
  6 #学习率
  7 LearnRate = 0.1
  8 #迭代次数最小值
  9 start_num = 100
 10 #迭代次数最大值
 11 stop_num = 1100
 12 #步长
 13 step_num = 100
 14 #误差隔多少次迭代输出
 15 int_con = 100
 16 #是否输出每次学习的测试结果
 17 input_predict = False
 18 #是否优化
 19 better = True
 20 
 21 
 22 
 23 #get data
 24 def getdata(num):
 25     
 26     file1 = np.loadtxt("Iris666.txt", dtype=np.str, delimiter=' ')
 27     x_tr = file1[0:,0:4].astype(np.float)
 28     y_tr = file1[0:,4].astype(np.float)
 29     y_tr = y_tr.reshape(75,1)
 30 
 31     file2 = np.loadtxt("Iris-test.txt",dtype=np.str,delimiter= ' ')
 32     x_te = file2[0:,0:4].astype(np.float)
 33     y_te = file2[0:,4].astype(np.float)
 34     y_te = y_te.reshape(75,1)
 35     if num==1:
 36         return x_tr.T,y_tr.T
 37     elif num==0:
 38         return x_te.T,y_te.T
 39     else:
 40         print("wrong!\n")
 41 
 42 #层数可以改变
 43 def layer_size(X, Y):
 44     dim_input = X.shape[0]
 45     dim_label = Y.shape[0]
 46     return (dim_input, dim_label)
 47 
 48 
 49 #随机初始化一个W1,b1,b2,W2矩阵
 50 def initialize_parameters(dim_input, dim_label):
 51     W1 = np.random.randn(10, dim_input) * 0.01
 52     b1 = np.zeros((10, 1))
 53     W2 = np.random.randn(dim_label, 10) * 0.01
 54     b2 = np.zeros((dim_label, 1))
 55 
 56     parameters = {
 57         'W1': W1,
 58         'b1': b1,
 59         'W2': W2,
 60         'b2': b2,
 61     }
 62     return parameters
 63 
 64 #sigmoid函数
 65 def sigmoid_normalized(z):
 66         return 1.0 / (1.0 + np.exp(-z))
 67 
 68 #正向传播
 69 def forward_propagation(X, parameters):
 70     W1 = parameters['W1']
 71     b1 = parameters['b1']
 72     W2 = parameters['W2']
 73     b2 = parameters['b2']
 74 
 75     y1 = sigmoid_normalized(np.dot(W1, X) + b1)
 76     y2 = sigmoid_normalized(np.dot(W2, y1) + b2)
 77 
 78 
 79     mid_data = {
 80         'y1': y1,
 81         'y2': y2,
 82     }
 83 
 84     return y2, mid_data
 85 
 86 #损失函数
 87 def compute_cost(y2, Y, parameters):
 88 
 89     m = Y.shape[1]  # number of example
 90 
 91     W1 = parameters['W1']
 92     W2 = parameters['W2']
 93 
 94     cost = np.sum(np.multiply((Y- y2), (Y-y2)))/2
 95     cost = np.squeeze(cost)
 96 
 97     return cost
 98 
 99 #反向传播学,梯度生成
100 def backward_propagation(parameters, mid_data, X, Y):
101     m = X.shape[1]
102 
103     W1 = parameters['W1']
104     W2 = parameters['W2']
105 
106     y1 = mid_data['y1']
107     y2 = mid_data['y2']
108 
109     dW2 = np.dot(np.multiply(np.multiply(y2,1.-y2),y2 - Y), y1.T)
110     db2 = np.multiply(np.multiply(y2,1.-y2),y2 - Y)
111     dW1=np.dot(np.multiply(np.multiply(y1, 1. - y1),np.multiply(np.multiply(y2,1.-y2),y2 - Y)),X.T)
112     db1 = np.multiply(np.multiply(y1, 1. - y1), np.multiply(np.multiply(y2, 1. - y2), y2 - Y))
113 
114     SDG = {
115         'dW1': dW1,
116         'db1': db1,
117         'dW2': dW2,
118         'db2': db2,
119     }
120 
121     return SDG
122 
123 #更新参数,更新模型
124 def update_para(parameters, SDG, learning_rate):
125     W1 = parameters['W1']
126     b1 = parameters['b1']
127     W2 = parameters['W2']
128     b2 = parameters['b2']
129 
130     dW1 = SDG['dW1']
131     db1 = SDG['db1']
132     dW2 = SDG['dW2']
133     db2 = SDG['db2']
134 
135     W1 = W1 - learning_rate * dW1
136     b1 = b1 - learning_rate * db1
137     W2 = W2 - learning_rate * dW2
138     b2 = b2 - learning_rate * db2
139 
140     parameters = {
141         "W1": W1,
142         "b1": b1,
143         "W2": W2,
144         "b2": b2,
145     }
146 
147     return parameters
148 
149 #神经网络
150 def nerual_network(X, Y, num_iterations, learning_rate):
151     dim_input = layer_size(X, Y)[0]
152     dim_label = layer_size(X, Y)[1]
153 
154     parameters = initialize_parameters(dim_input, dim_label)
155     W1 = parameters['W1']
156     b1 = parameters['b1']
157     W2 = parameters['W2']
158     b2 = parameters['b2']
159 
160     cost_list = []
161     for i in range(0, num_iterations):
162 
163         y2, mid_data = forward_propagation(X, parameters)
164         cost = compute_cost(y2, Y, parameters)
165         cost_list.append(cost)
166         SDG = backward_propagation(parameters, mid_data, X, Y)
167         parameters = update_para(parameters, SDG, learning_rate)
168         if (i %  int_con== 0) and (i <= 5000):
169             print("经过%i次迭代,误差为: %f" % (math.floor(i/int_con)*int_con, cost))
170 
171     return parameters, cost_list
172 
173 
174 def main():
175 
176     if better:
177         a = ((sigmoid_normalized(1)- sigmoid_normalized(0))/(sigmoid_normalized(0) + sigmoid_normalized(1)))*sigmoid_normalized(0)
178         b = ((sigmoid_normalized(2) - sigmoid_normalized(1)) / (sigmoid_normalized(2) + sigmoid_normalized(
179             1))) * sigmoid_normalized(1)
180     #训练集
181     X, Y = getdata(1)
182     X = sigmoid_normalized(X)
183     Y = sigmoid_normalized(Y)
184     correct = []
185     for j in range(start_num,stop_num,step_num):
186         parameter, cost_list = nerual_network(X, Y, num_iterations=j, learning_rate=LearnRate)
187 
188         #测试
189         X1,Y1 = getdata(0)
190         X1 = sigmoid_normalized(X1)
191         y2, mid_data = forward_propagation(X1,parameters=parameter)
192         if input_predict:
193             print(y2)
194         Label = y2
195         if better:
196             x1=sigmoid_normalized(1)+b
197             x2=sigmoid_normalized(0)+a
198             Label[Label > x1] = 2
199             Label[Label <x2 ] = 0
200             Label[(Label >= x2) & (Label <= x1)] = 1
201         else:
202             Label[Label >= sigmoid_normalized(2)] = 2
203             Label[Label <= sigmoid_normalized(0)] = 0
204             Label[(Label > sigmoid_normalized(0)) & (Label < sigmoid_normalized(2))] = 1
205         if input_predict:
206             print(Label)
207             print(Y1)
208         count = 0
209         for i in range(0, 75):
210             if Label[0, i] == Y1[0, i]:
211                 count += 1
212         correct.append((100 * count / 75))
213         #print(y2)
214         #print(Y1)
215         #plt.plot(cost_list)
216          #plt.show()
217 
218     print('平均准确率:%f \n'%(np.mean(correct)))
219     plt.figure()
220     plt.plot(correct)
221     plt.figure()
222     plt.plot(cost_list)
223     plt.show()
224 
225 if __name__ == '__main__':
226     main()
227 
228     '''结果:
229             未优化:
230                 迭代大于1000后稳定在61%左右
231                 小于一千是优化效果明显
232             优化:
233                 50次以上,》=91%(人工优化,邻近加权分区)
234     '''

 

posted @ 2020-02-22 15:51  无极183  阅读(1852)  评论(0编辑  收藏  举报
/*
*/