bp神经网络python实现_机器学习(4):BP神经网络原理及其python实现

1 ## encoding=utf8

2 '''

3 Created on 2017-7-34

5 @author: Administrator6 '''

7 importrandom8 importpandas as pd9 importnumpy as np10 from matplotlib importpyplot as plt11 from sklearn.model_selection importtrain_test_split as ttsplit12

13 classLossFun:14 def __init__(self, lf_type="least_square"):15 self.name = "loss function"

16 self.type =lf_type17

18 defcal(self, t, z):19 loss =020 if self.type == "least_square":21 loss =self.least_square(t, z)22 returnloss23

24 defcal_deriv(self, t, z):25 delta =026 if self.type == "least_square":27 delta =self.least_square_deriv(t, z)28 returndelta29

30 defleast_square(self, t, z):31 zsize =z.shape32 sample_num = zsize[1]33 return np.sum(0.5 * (t - z) * (t - z) * t) /sample_num34

35 defleast_square_deriv(self, t, z):36 return z -t37

38 classActivationFun:39 '''

40 激活函数41 '''

42 def __init__(self, atype="sigmoid"):43 self.name = "activation function library"

44 self.type =atype;45

46 defcal(self, a):47 z =048 if self.type == "sigmoid":49 z =self.sigmoid(a)50 elif self.type == "relu":51 z =self.relu(a)52 returnz53

54 defcal_deriv(self, a):55 z =056 if self.type == "sigmoid":57 z =self.sigmoid_deriv(a)58 elif self.type == "relu":59 z =self.relu_deriv(a)60 returnz61

62 defsigmoid(self, a):63 return 1 / (1 + np.exp(-a))64

65 defsigmoid_deriv(self, a):66 fa =self.sigmoid(a)67 return fa * (1 -fa)68

69 defrelu(self, a):70 idx = a <=071 a[idx] = 0.1 *a[idx]72 return a #np.maximum(a, 0.0)

73

74 defrelu_deriv(self, a):75 #print a

76 a[a > 0] = 1.0

77 a[a <= 0] = 0.1

78 #print a

79 returna80

81 classLayer:82 '''

83 神经网络层84 '''

85 def __init__(self, num_neural, af_type="sigmoid", learn_rate=0.5):86 self.af_type = af_type #active function type

87 self.learn_rate =learn_rate88 self.num_neural =num_neural89 self.dim =None90 self.W =None91

92 self.a =None93 self.X =None94 self.z =None95 self.delta =None96 self.theta =None97 self.act_fun =ActivationFun(self.af_type)98

99 deffp(self, X):100 '''

101 Foward Propagation102 '''

103 self.X =X104 xsize =X.shape105 self.dim =xsize[0]106 self.num = xsize[1]107

108 if self.W ==None:109 #self.W = np.random.random((self.dim, self.num_neural))-0.5

110 #self.W = np.random.uniform(-1,1,size=(self.dim,self.num_neural))

111 if(self.af_type == "sigmoid"):112 self.W = np.random.normal(0, 1, size=(self.dim, self.num_neural)) /np.sqrt(self.num)113 elif(self.af_type == "relu"):114 self.W = np.random.normal(0, 1, size=(self.dim, self.num_neural)) * np.sqrt(2.0 /self.num)115 if self.theta ==None:116 #self.theta = np.random.random((self.num_neural, 1))-0.5

117 #self.theta = np.random.uniform(-1,1,size=(self.num_neural,1))

118

119 if(self.af_type == "sigmoid"):120 self.theta = np.random.normal(0, 1, size=(self.num_neural, 1)) /np.sqrt(self.num)121 elif(self.af_type == "relu"):122 self.theta = np.random.normal(0, 1, size=(self.num_neural, 1)) * np.sqrt(2.0 /self.num)123 #calculate the foreward a

124 self.a =(self.W.T).dot(self.X)125 ###calculate the foreward z####

126 self.z =self.act_fun.cal(self.a)127 returnself.z128

129 defbp(self, delta):130 '''

131 Back Propagation132 '''

133 self.delta = delta *self.act_fun.cal_deriv(self.a)134 self.theta = np.array([np.mean(self.theta - self.learn_rate * self.delta, 1)]).T #求所有样本的theta均值

135 dW = self.X.dot(self.delta.T) /self.num136 self.W = self.W - self.learn_rate *dW137 delta_out =self.W.dot(self.delta);138 returndelta_out139

140 classBpNet:141 '''

142 BP神经网络143 '''

144 def __init__(self, net_struct, stop_crit, max_iter, batch_size=10):145 self.name = "net work"

146 self.net_struct =net_struct147 if len(self.net_struct) ==0:148 print "no layer is specified!"

149 return

150

151 self.stop_crit =stop_crit152 self.max_iter =max_iter153 self.batch_size =batch_size154 self.layers =[]155 self.num_layers =0;156 #创建网络

157 self.create_net(net_struct)158 self.loss_fun = LossFun("least_square");159

160 defcreate_net(self, net_struct):161 '''

162 创建网络163 '''

164 self.num_layers =len(net_struct)165 for i inrange(self.num_layers):166 self.layers.append(Layer(net_struct[i][0], net_struct[i][1], net_struct[i][2]))167

168 def train(self, X, t, Xtest=None, ttest=None):169 '''

170 训练网络171 '''

172 eva_acc_list =[]173 eva_loss_list =[]174

175 xshape =X.shape;176 num =xshape[0]177 dim = xshape[1]178

179 for k inrange(self.max_iter):180 #i = random.randint(0,num-1)

181 idxs =random.sample(range(num), self.batch_size)182 xi =np.array([X[idxs, :]]).T[:, :, 0]183 ti =np.array([t[idxs, :]]).T[:, :, 0]184 #前向计算

185 zi =self.fp(xi)186

187 #偏差计算

188 delta_i =self.loss_fun.cal_deriv(ti, zi)189

190 #反馈计算

191 self.bp(delta_i)192

193 #评估精度

194 if Xtest !=None:195 if k % 100 ==0:196 [eva_acc, eva_loss] =self.test(Xtest, ttest)197 eva_acc_list.append(eva_acc)198 eva_loss_list.append(eva_loss)199 print "%4d,%4f,%4f" %(k, eva_acc, eva_loss)200 else:201 print "%4d" %(k)202 return[eva_acc_list, eva_loss_list]203

204 deftest(self, X, t):205 '''

206 测试模型精度207 '''

208 xshape =X.shape;209 num =xshape[0]210 z =self.fp_eval(X.T)211 t =t.T212 est_pos =np.argmax(z, 0)213 real_pos =np.argmax(t, 0)214 corrct_count = np.sum(est_pos ==real_pos)215 acc = 1.0 * corrct_count /num216 loss =self.loss_fun.cal(t, z)217 #print "%4f,loss:%4f"%(loss)

218 return[acc, loss]219

220 deffp(self, X):221 '''

222 前向计算223 '''

224 z =X225 for i inrange(self.num_layers):226 z =self.layers[i].fp(z)227 returnz228

229 defbp(self, delta):230 '''

231 反馈计算232 '''

233 z =delta234 for i in range(self.num_layers - 1, -1, -1):235 z =self.layers[i].bp(z)236 returnz237

238 deffp_eval(self, X):239 '''

240 前向计算241 '''

242 layers =self.layers243 z =X244 for i inrange(self.num_layers):245 z =layers[i].fp(z)246 returnz247

248 defz_score_normalization(x):249 mu =np.mean(x)250 sigma =np.std(x)251 x = (x - mu) /sigma;252 returnx;253

254 defsigmoid(X, useStatus):255 ifuseStatus:256 return 1.0 / (1 + np.exp(-float(X)));257 else:258 returnfloat(X);259

260 defplot_curve(data, title, lege, xlabel, ylabel):261 num =len(data)262 idx =range(num)263 plt.plot(idx, data, color="r", linewidth=1)264

265 plt.xlabel(xlabel, fontsize="xx-large")266 plt.ylabel(ylabel, fontsize="xx-large")267 plt.title(title, fontsize="xx-large")268 plt.legend([lege], fontsize="xx-large", loc='upper left');269 plt.show()270

271 if __name__ == "__main__":272 print ('This is main of module "bp_nn.py"')273

274 print("Import data")275 raw_data = pd.read_csv('./train.csv', header=0)276 data =raw_data.values277 imgs = data[0::, 1::]278 labels =data[::, 0]279 train_features, test_features, train_labels, test_labels =ttsplit(280 imgs, labels, test_size=0.33, random_state=23323)281

282 train_features =z_score_normalization(train_features)283 test_features =z_score_normalization(test_features)284 sample_num =train_labels.shape[0]285 tr_labels = np.zeros([sample_num, 10])286 for i inrange(sample_num):287 tr_labels[i][train_labels[i]] = 1

288

289 sample_num =test_labels.shape[0]290 te_labels = np.zeros([sample_num, 10])291 for i inrange(sample_num):292 te_labels[i][test_labels[i]] = 1

293

294 printtrain_features.shape295 printtr_labels.shape296 printtest_features.shape297 printte_labels.shape298

299 stop_crit = 100 #停止

300 max_iter = 10000 #最大迭代次数

301 batch_size = 100 #每次训练的样本个数

302 net_struct = [[100, "relu", 0.01], [10, "sigmoid", 0.1]] #网络结构[[batch_size,active function, learning rate]]

303 #net_struct = [[200,"sigmoid",0.5],[100,"sigmoid",0.5],[10,"sigmoid",0.5]] 网络结构[[batch_size,active function, learning rate]]

304

305 bpNNCls =BpNet(net_struct, stop_crit, max_iter, batch_size);306 #train model

307

308 [acc, loss] =bpNNCls.train(train_features, tr_labels, test_features, te_labels)309 #[acc, loss] = bpNNCls.train(train_features, tr_labels)

310 print("training model finished")311 #create test data

312 plot_curve(acc, "Bp Network Accuracy", "accuracy", "iter", "Accuracy")313 plot_curve(loss, "Bp Network Loss", "loss", "iter", "Loss")314

315

316 #test model

317 [acc, loss] =bpNNCls.test(test_features, te_labels);318 print "test accuracy:%f" %(acc)319

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值