99import numpy as np
1010import matplotlib .pyplot as plt
1111
12- class Bpnw ():
12+ class Bpnn ():
1313
1414 def __init__ (self ,n_layer1 ,n_layer2 ,n_layer3 ,rate_w = 0.3 ,rate_t = 0.3 ):
1515 '''
@@ -38,7 +38,7 @@ def sig_plain(self,x):
3838 def do_round (self ,x ):
3939 return round (x , 3 )
4040
41- def trian (self ,patterns ,data_train , data_teach , n_repeat , error_accuracy ,draw_e = bool ):
41+ def trian (self ,patterns ,data_train , data_teach , n_repeat , error_accuracy , draw_e = False ):
4242 '''
4343 :param patterns: the number of patterns
4444 :param data_train: training data x; numpy.ndarray
@@ -49,9 +49,9 @@ def trian(self,patterns,data_train, data_teach, n_repeat, error_accuracy,draw_e
4949 '''
5050 data_train = np .asarray (data_train )
5151 data_teach = np .asarray (data_teach )
52- print ('-------------------Start Training-------------------------' )
53- print (' - - Shape: Train_Data ' ,np .shape (data_train ))
54- print (' - - Shape: Teach_Data ' ,np .shape (data_teach ))
52+ # print('-------------------Start Training-------------------------')
53+ # print(' - - Shape: Train_Data ',np.shape(data_train))
54+ # print(' - - Shape: Teach_Data ',np.shape(data_teach))
5555 rp = 0
5656 all_mse = []
5757 mse = 10000
@@ -95,9 +95,9 @@ def draw_error():
9595 plt .ylabel ('All_mse' )
9696 plt .grid (True ,alpha = 0.7 )
9797 plt .show ()
98- print ('------------------Training Complished---------------------' )
99- print (' - - Training epoch: ' , rp , ' - - Mse: %.6f' % mse )
100- print (' - - Last Output: ' , final_out3 )
98+ # print('------------------Training Complished---------------------')
99+ # print(' - - Training epoch: ', rp, ' - - Mse: %.6f'%mse)
100+ # print(' - - Last Output: ', final_out3)
101101 if draw_e :
102102 draw_error ()
103103
@@ -108,9 +108,9 @@ def predict(self,data_test):
108108 '''
109109 data_test = np .asarray (data_test )
110110 produce_out = []
111- print ('-------------------Start Testing-------------------------' )
112- print (' - - Shape: Test_Data ' ,np .shape (data_test ))
113- print (np .shape (data_test ))
111+ # print('-------------------Start Testing-------------------------')
112+ # print(' - - Shape: Test_Data ',np.shape(data_test))
113+ # print(np.shape(data_test))
114114 for g in range (np .shape (data_test )[0 ]):
115115
116116 net_i = data_test [g ]
@@ -127,8 +127,26 @@ def predict(self,data_test):
127127
128128
129129def main ():
130- #I will fish the mian function later
131- pass
130+ #example data
131+ data_x = [[1 ,2 ,3 ,4 ],
132+ [5 ,6 ,7 ,8 ],
133+ [2 ,2 ,3 ,4 ],
134+ [7 ,7 ,8 ,8 ]]
135+ data_y = [[1 ,0 ,0 ,0 ],
136+ [0 ,1 ,0 ,0 ],
137+ [0 ,0 ,1 ,0 ],
138+ [0 ,0 ,0 ,1 ]]
139+
140+ test_x = [[1 ,2 ,3 ,4 ],
141+ [3 ,2 ,3 ,4 ]]
142+
143+ #building network model
144+ model = Bpnn (4 ,10 ,4 )
145+ #training the model
146+ model .trian (patterns = 4 ,data_train = data_x ,data_teach = data_y ,
147+ n_repeat = 100 ,error_accuracy = 0.1 ,draw_e = True )
148+ #predicting data
149+ model .predict (test_x )
132150
133151if __name__ == '__main__' :
134152 main ()
0 commit comments