class Widget(QWidget): def __init__(self): print('Creating Neural Network...') self.bpnn = BPNN(7, 7, 4) print('Training patterns...') self.bpnn.train(pat_train, iterations=100000) print('Training complete!') super().__init__() uifile = os.path.join(os.path.dirname(__file__), 'Widget.ui') self.ui = loadUi(uifile, self) # list to store which bits are selected self.bits = [0] * 7 # connect only bit buttons to toggle slot for elem in self.ui.children(): name = elem.objectName() if name[:3] == 'bit': elem.clicked.connect(self.toggle) # connect check button to check slot self.ui.buttonCheck.clicked.connect(self.check) def toggle(self): sender = self.sender() # take last number ie 'bit3' to '3' btnID = int(sender.objectName()[-1:]) # toggle and set color if self.bits[btnID] == 0: sender.setStyleSheet("background-color: red"); self.bits[btnID] = 1 else: sender.setStyleSheet("background-color: none"); self.bits[btnID] = 0 def check(self): # get bit pattern from ui drawing test_pat = self.bits # get predicted pattern pat_predicted = self.bpnn.test(test_pat) i=0 # match predicted pattern with output patterns for pat in pat_train: if pat[1] == pat_predicted: self.ui.result.setText("Possible number: <b>" + str(i) + "</b>") return i += 1 self.ui.result.setText("Unknown pattern")
class Widget(QWidget): def __init__(self): print('Creating Neural Network...') self.bpnn = BPNN(7, 7, 4) print('Training patterns...') self.bpnn.train(pat_train, iterations=100000) print('Training complete!') super().__init__() uifile = os.path.join(os.path.dirname(__file__), 'Widget.ui') self.ui = loadUi(uifile, self) # list to store which bits are selected self.bits = [0] * 7 # connect only bit buttons to toggle slot for elem in self.ui.children(): name = elem.objectName() if name[:3] == 'bit': elem.clicked.connect(self.toggle) # connect check button to check slot self.ui.buttonCheck.clicked.connect(self.check) def toggle(self): sender = self.sender() # take last number ie 'bit3' to '3' btnID = int(sender.objectName()[-1:]) # toggle and set color if self.bits[btnID] == 0: sender.setStyleSheet("background-color: red") self.bits[btnID] = 1 else: sender.setStyleSheet("background-color: none") self.bits[btnID] = 0 def check(self): # get bit pattern from ui drawing test_pat = self.bits # get predicted pattern pat_predicted = self.bpnn.test(test_pat) i = 0 # match predicted pattern with output patterns for pat in pat_train: if pat[1] == pat_predicted: self.ui.result.setText("Possible number: <b>" + str(i) + "</b>") return i += 1 self.ui.result.setText("Unknown pattern")
def get_bpnn_predict(X, y, param): model = BPNN(learning_rate=0.05, num_of_training=100, input_size=X.shape[1], hidden_n=2, parameters=list(param)) model.fit(X, y) predvalue = model.predict(X) return np.array(predvalue)
def run_main(): """ 这是主函数 """ # 导入数据 path = './voice_data.txt' Data, Label = Load_Voice_Data(path) # 分割数据集,并对数据集进行标准化 Train_Data, Test_Data, Train_Label, Test_Label = train_test_split( Data, Label, test_size=1 / 4, random_state=10) Train_Data = Normalizer().fit_transform(Train_Data) Test_Data = Normalizer().fit_transform(Test_Data) # 设置网络参数 input_n = np.shape(Data)[1] output_n = np.shape(Label)[1] hidden_n = int(np.sqrt(input_n * output_n)) lambd = 0.001 batch_size = 64 learn_rate = 0.01 epoch = 1000 iteration = 10000 # 训练并测试网络 bpnn = BPNN(input_n, hidden_n, output_n, lambd) train_loss, test_loss, test_accuracy = bpnn.train_test( Train_Data, Train_Label, Test_Data, Test_Label, learn_rate, epoch, iteration, batch_size) # 解决画图是的中文乱码问题 mpl.rcParams['font.sans-serif'] = [u'simHei'] mpl.rcParams['axes.unicode_minus'] = False # 结果可视化 col = ['Train_Loss', 'Test_Loss'] epoch = np.arange(epoch) plt.plot(epoch, train_loss, 'r') plt.plot(epoch, test_loss, 'b-.') plt.xlabel('Epoch') plt.ylabel('Loss') plt.grid(True) plt.legend(labels=col, loc='best') plt.savefig('./训练与测试损失.jpg') plt.show() plt.close() plt.plot(epoch, test_accuracy, 'r') plt.xlabel('Epoch') plt.ylabel('Test Accuracy') plt.grid(True) plt.legend(loc='best') plt.savefig('./测试精度.jpg') plt.show() plt.close()
def __init__(self): print('Creating Neural Network...') self.bpnn = BPNN(7, 7, 4) print('Training patterns...') self.bpnn.train(pat_train, iterations=100000) print('Training complete!') super().__init__() uifile = os.path.join(os.path.dirname(__file__), 'Widget.ui') self.ui = loadUi(uifile, self) # list to store which bits are selected self.bits = [0] * 7 # connect only bit buttons to toggle slot for elem in self.ui.children(): name = elem.objectName() if name[:3] == 'bit': elem.clicked.connect(self.toggle) # connect check button to check slot self.ui.buttonCheck.clicked.connect(self.check)