MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / trian

Method trian

neural_network/convolution_neural_network.py:193–269  ·  view source on GitHub ↗
(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_e = bool)

Source from the content-addressed store, hash-verified

191 return pd_all
192
193 def trian(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_e = bool):
194 #model traning
195 print('----------------------Start Training-------------------------')
196 print((' - - Shape: Train_Data ',np.shape(datas_train)))
197 print((' - - Shape: Teach_Data ',np.shape(datas_teach)))
198 rp = 0
199 all_mse = []
200 mse = 10000
201 while rp < n_repeat and mse >= error_accuracy:
202 alle = 0
203 print('-------------Learning Time %d--------------'%rp)
204 for p in range(len(datas_train)):
205 #print('------------Learning Image: %d--------------'%p)
206 data_train = np.asmatrix(datas_train[p])
207 data_teach = np.asarray(datas_teach[p])
208 data_focus1,data_conved1 = self.convolute(data_train,self.conv1,self.w_conv1,
209 self.thre_conv1,conv_step=self.step_conv1)
210 data_pooled1 = self.pooling(data_conved1,self.size_pooling1)
211 shape_featuremap1 = np.shape(data_conved1)
212 ''&#x27;
213 print(' -----original shape ', np.shape(data_train))
214 print(' ---- after convolution ',np.shape(data_conv1))
215 print(' -----after pooling ',np.shape(data_pooled1))
216 ''&#x27;
217 data_bp_input = self._expand(data_pooled1)
218 bp_out1 = data_bp_input
219
220 bp_net_j = np.dot(bp_out1,self.vji.T) - self.thre_bp2
221 bp_out2 = self.sig(bp_net_j)
222 bp_net_k = np.dot(bp_out2 ,self.wkj.T) - self.thre_bp3
223 bp_out3 = self.sig(bp_net_k)
224
225 #--------------Model Leaning ------------------------
226 # calcluate error and gradient---------------
227 pd_k_all = np.multiply((data_teach - bp_out3), np.multiply(bp_out3, (1 - bp_out3)))
228 pd_j_all = np.multiply(np.dot(pd_k_all,self.wkj), np.multiply(bp_out2, (1 - bp_out2)))
229 pd_i_all = np.dot(pd_j_all,self.vji)
230
231 pd_conv1_pooled = pd_i_all / (self.size_pooling1*self.size_pooling1)
232 pd_conv1_pooled = pd_conv1_pooled.T.getA().tolist()
233 pd_conv1_all = self._calculate_gradient_from_pool(data_conved1,pd_conv1_pooled,shape_featuremap1[0],
234 shape_featuremap1[1],self.size_pooling1)
235 #weight and threshold learning process---------
236 #convolution layer
237 for k_conv in range(self.conv1[1]):
238 pd_conv_list = self._expand_mat(pd_conv1_all[k_conv])
239 delta_w = self.rate_weight * np.dot(pd_conv_list,data_focus1)
240
241 self.w_conv1[k_conv] = self.w_conv1[k_conv] + delta_w.reshape((self.conv1[0],self.conv1[0]))
242
243 self.thre_conv1[k_conv] = self.thre_conv1[k_conv] - np.sum(pd_conv1_all[k_conv]) * self.rate_thre
244 #all connected layer
245 self.wkj = self.wkj + pd_k_all.T * bp_out2 * self.rate_weight
246 self.vji = self.vji + pd_j_all.T * bp_out1 * self.rate_weight
247 self.thre_bp3 = self.thre_bp3 - pd_k_all * self.rate_thre
248 self.thre_bp2 = self.thre_bp2 - pd_j_all * self.rate_thre
249 # calculate the sum error of all single image
250 errors = np.sum(abs((data_teach - bp_out3)))

Callers

nothing calls this directly

Calls 6

convoluteMethod · 0.95
poolingMethod · 0.95
_expandMethod · 0.95
sigMethod · 0.95
_expand_matMethod · 0.95

Tested by

no test coverage detected