1. Realize the effect
2. Related code
Threading class for implementing BP training model
1 classWorkthread (qtcore.qthread):2Finish_trigger = Qtcore.pyqtsignal ()#Close Waiting_gif3Result_trigger = Qtcore.pyqtsignal (PD. Series)#transmitting the signal of the prediction result4Evaluate_trigger = qtcore.pyqtsignal (list)#transmit the correct rate signal5 6 def __int__(self):7Super (Workthread, self).__init__()8 9 definit (self, dataset, feature, label, info):TenSelf.dataset =DataSet OneSelf.feature =feature ASelf.label =label -Self.info =Info - the #you can assume that the run () function is the code that the new thread needs to execute - defRun (self): - Self . BP () - + defBP (self): - """ + BP Neural Network, returns the prediction data of the label A :p Aram Parent: at :p Aram DataSet: - :p Aram Feature: - :p Aram Label: - :p Aram Info: - : return: - """ inDataSet =Self.dataset -Feature =self.feature toLabel =Self.label +info =Self.info - theInput_dim =len (feature) *data_x = Dataset[feature]#Feature Data $data_y = Dataset[label]#Tag DataPanax Notoginseng -X_train, X_test, y_train, y_test = Train_test_split (data_x, data_y, test_size=info[0][3]) the + #********************** establish a simple BP neural network model ********************************* ASelf.model = Sequential ()#declaring a sequential model theCount =Len (info) + forIinchRange (1, count-1): - ifi = = 1: $Self.model.add (Dense (info[i][0], activation=info[i][1], Input_dim=input_dim, kernel_initializer=info[i][2]))#input layer, dense represents the BP layer $ Else: -Self.model.add (Dense (info[i][0], activation=info[i][1], kernel_initializer=info[i][2])) - the #Adding an output layer -Self.model.add (Dense (info[count-1][0], activation=info[count-1][1], kernel_initializer=info[count-1][2]))Wuyi theSGD = SGD (lr=info[0][0], decay=1e-6, momentum=0.9, nesterov=True) -Self.model.compile (loss='binary_crossentropy', OPTIMIZER=SGD, metrics=['accuracy'])#compiling the Model Wu -Self.model.fit (X_train, Y_train, Validation_data= (X_test, Y_test), epochs=info[0][1], batch_size=info[0][2])#Training Model 1000 times About $Scores_train = Self.model.evaluate (X_train, Y_train, batch_size=10) -Scores_test = Self.model.evaluate (X_test, Y_test, batch_size=10) -Scores = Self.model.evaluate (Data_x, data_y, batch_size=10) - ASelf.finish_trigger.emit ()#send a signal after the loop is complete +List = [scores_train[1]*100, scores_test[1]*100, scores[1]*100] the self.evaluate_trigger.emit (list) -result =PD. Series (Self.model.predict (data_x). T[0]) $Result.name ='Prediction (BP)' the self.result_trigger.emit (Result) theK.clear_session ()#repeatedly calling model models the the defSave_model (Self, save_dir): -Self.model.save (Save_dir)#Save the Model
GUI display code (partial):
1 classMainWindow (Qtgui.qmainwindow):2save_dir_signal = qtcore.pyqtsignal (str)#Pass the Save directory signal3 4 defShow_evaluate_result (Self, evaluate_result):5Help = QtGui.QMessageBox.information (self,'Evaluation Results',6 "Training Set accuracy:%.2f%%\n test Set accuracy:%.2f%%\n data Set accuracy:%.2f%%"%7(Evaluate_result[0], evaluate_result[1], evaluate_result[2]),8 QtGui.QMessageBox.Yes)9 Ten Self.pop_save_dir () One A defPop_save_dir (self): -msg = QtGui.QMessageBox.information (self,'Tips','do you want to save the model? ', QtGui.QMessageBox.Yes |QtGui.QMessageBox.No) - ifmsg = =QtGui.QMessageBox.Yes: theSave_dir = QtGui.QFileDialog.getSaveFileName (self,'Select Save Directory','C:\\users\\fuqia\\desktop') - - ifSave_dir! ="': -Save_dir = Save_dir +'. Model' + self.save_dir_signal.emit (Save_dir) - + defShow_bp_result (self, result): A atSelf.predict_data =result - Tablewidgetdeal.add_predict_data (self.table, result) - - defWaiting_label_close (self): - self.label.close () - in defshow_waiting (self): -Self.label =Qtgui.qlabel (self) toSelf.label.setFixedSize (640, 480)#do you have any questions??? +Self.label.setWindowFlags (QtCore.Qt.FramelessWindowHint)#No Border -Self.label.setAttribute (QtCore.Qt.WA_TranslucentBackground)#Transparent Background the *Screen =qtgui.qdesktopwidget (). Screengeometry () $Size =self.label.geometry ()Panax Notoginseng #if it is Self.label.move ((Screen.width ()-size.width ())/2, (Screen.height ()-size.height ())/2) cannot be centered -Self.label.move ((Screen.width ()-size.width ())/2 +, (Screen.height ()-size.height ())/2) the + #Open gif file AMovie = Qtgui.qmovie ("./icon/waiting.gif") the #when setting Cachemode to Cacheall, the GIF infinite loop is displayed, note that Loopcount () returns 1 + Movie.setcachemode (QTGUI.QMOVIE.CACHEALL) - #Playback Speed $Movie.setspeed (100) $ Self.label.setMovie (Movie) - #start playback, corresponding to Movie.start () - Movie.start () the self.label.show () -Q =Qtcore.qeventloop ()WuyiQ.EXEC_ ()
1 w = workthread ()2w.init (self.object.data_set, feature, label, Self.bp_ui.bp_info) 3w.start ()4w.finish_trigger.connect (self.waiting_label_close)5 w.result_trigger.connect (self.show_bp_result)6w.evaluate_trigger.connect ( Self.show_evaluate_result)7self.save_dir_signal.connect (w.save_model)8 Self.show_waiting ()
PYQT to train BP model, show Waiting (multithreading)