def __init__(self, parent=None): super(Main, self).__init__(parent) QMainWindow.__init__(self) loadUi('core/window.ui', self) #self.setupUi(self) self.setWindowIcon(QIcon('main.ico')) '''pushButton等控件信号''' self.time_point_training.clicked.connect(self.clickedTime_point_training) self.pushButton_pause.clicked.connect(self.clickedPause) #self.dynamic_display.clicked.connect(self.dynamic_display_click) self.change_dynamic.clicked.connect(self.change_dynamic_click) self.change_hot.clicked.connect(self.change_hot_click) self.commandLink_about.clicked.connect(self.about) self.time_point.currentIndexChanged.connect(self.currentIndexChanged) '''newForm''' self.new_dig.clicked.connect(self.test) #self.close_signal.connect(self.newForm.close) '''widget_plot,数据可视化''' self.main_widget = QWidget(self) self.widget_plot = matplot.MyccMplCanvas(self.main_widget, width=5, height=4, dpi=100) self.verticalLayout_cell.addWidget(self.widget_plot) GlobalMap().set(running_global = 0) self.initial_value() #初始化全局变量 self.timer = QTimer(self) #动态更新全局变量 self.timer.timeout.connect(self.update_value) self.global_temp= GlobalMap().get('all')
def loss(self): #绘制损失度动态图 self.axes.hold(False) self.a += [GlobalMap().get('times')] self.b += [GlobalMap().get('loss_now') / 100] self.axes.plot(self.a, self.b, linewidth=2) self.axes.set(xlabel="times", ylabel="loss", title='Real time training state') self.draw()
def save_question(self): '''询问是否使用历史训练数据,提高速度''' reply = QMessageBox.question(self, '已有历史训练数据', "你还要重新训练吗?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: GlobalMap().set( save_nopass = 0 ) GlobalMap().set( save_question = 0 )
def update_value(self): '''更新UI状态显示''' self.global_temp= GlobalMap().get('all') try: if not GlobalMap().get('running_global'): time.sleep(0.3) self.timer.stop() self.initial_value() #print(self.global_temp) self.progress_num.setValue(self.global_temp['progress_num']) self.iter_times.setText(str(self.global_temp['times'])) self.loss_now.setText(str(self.global_temp['loss_now'])) self.used_time.display(self.global_temp['used_time']) except: pass
def dig_change_accept(self): '''23小时各小时变化动态图''' if GlobalMap().get('running_global'): QMessageBox.warning(self, "Warning","Is_running!!!\nMaybe you want to click '停止当前任务'") return self.initial_value() GlobalMap().set(running_global = 1, running_status = 2, time_point = self.time_point.currentIndex(), fix_seed = self.fix_seed.isChecked()) self.update_value() self.timer.start(1000) self.verticalLayout_cell.removeWidget(self.widget_plot) self.widget_plot = matplot.MyDig_ChangeMplCanvas(self.main_widget, width=5, height=4, dpi=100) self.verticalLayout_cell.addWidget(self.widget_plot) self.widget_plot.start()
def start(self): self.times = 1 GlobalMap().set(progress_num=0) self.lists = [] #记录时间 self.list_height = [[], []] #用于记录高密度的区域 self.stat = 0 self.fig.subplots_adjust(left=0.02, bottom=0.02, right=0.98, top=0.92, hspace=0.04, wspace=0.02) self.axes1 = self.fig.add_subplot(211) self.axes2 = self.fig.add_subplot(212) for i in range(24): self.lists += [i] if exists('db/temp/20180515.csv'): self.temp1 = DataFrame(read_csv('db/temp/20180515.csv', header=0)) else: self.temp1 = dataclear(20180515).run() self.compute_initial_figure() self.timer = QTimer(self) self.timer.timeout.connect(self.compute_initial_figure) self.timer.start(1000)
def pause(self): try: self.timer.stop() self.thread.quit() except: pass GlobalMap().set(running_global=0, )
def clickedTime_point_training(self): '''点击单时间的预测''' self.initial_value() if GlobalMap().get('running_global'): QMessageBox.warning(self, "Warning","Is_running!!!\nMaybe you want to click '停止当前任务'") return GlobalMap().set(running_global = 1, running_status = 0, time_point = self.time_point.currentIndex(), fix_seed = self.fix_seed.isChecked()) self.update_value() if exists('ckpt/model-'+ str(self.global_temp['time_point']) +'.ckpt.index') and GlobalMap().get('save_question'): #判断模型是否存在 self.save_question() self.timer.start(600) self.verticalLayout_cell.removeWidget(self.widget_plot) self.widget_plot = matplot.MySticMplCanvas(self.main_widget, width=5, height=4, dpi=100) self.verticalLayout_cell.addWidget(self.widget_plot) self.widget_plot.start()
def update_figure(self): if GlobalMap().get('userdefined') == 'Null_': #检测从自定义对话框传递过来的数据 return if not self.begin: #防止重绘 return self.begin = 0 GlobalMap().set(time_point=self.times, ) self.times += 1 if self.times == 2: self.begin = 1 self.temp0 = NN.main([], [], GlobalMap().get('userdefined')) #传入BP网络预测 return else: temp01 = self.temp0 self.temp0 = NN.main([], [], temp01) imgP = imread('core/map.png', ) self.axes.imshow(imgP, extent=[112.1495, 112.288, 30.308, 30.391], aspect='auto') x = np.array(self.temp0[0]) y = np.array(self.temp0[1]) z = gaussian_kde(self.temp0)(self.temp0) #生成密度颜色 self.axes.hold(True) self.axes.scatter(x, y, marker='.', s=100, c=z, edgecolors='') self.axes.grid(True) self.axes.set_xlim(112.150, 112.284) self.axes.set_ylim(30.309, 30.385) self.axes.set(xlabel="longitude", ylabel="latitude") self.axes.set_title('The ' + str(self.times) + ':00 ’s distribution prediction') self.draw() self.begin = 1 self.axes.hold(False) if self.times >= 2: self.pause()
def test(self, a, b, c): '''入口''' if a == []: #仅用于自定义模式的预测 #print(self.ckpt_path, c) self.setup([2, self.init_temp['neurons_num'], 2]) self.saver.Saver().restore(self.session, 'ckpt/modelc-' + str(self.init_temp['time_point']) + '.ckpt') #存在就从模型中恢复变量 GlobalMap().set(used_time=0, times=0) return noturn(self.predict(turn(c))) x_data = turn(a) y_data = turn(b) test_data = turn(c) self.setup([2, self.init_temp['neurons_num'], 2]) if exists(self.ckpt_path + '.index') and GlobalMap().get('save_nopass'): #判断模型是否存在 try: self.saver.Saver().restore(self.session, self.ckpt_path) #存在就从模型中恢复变量 self.loss = reduce_mean( reduce_sum(square((self.label_layer - self.output_layer)), reduction_indices=[1])) loss_now = self.session.run(self.loss, feed_dict={ self.input_layer: x_data, self.label_layer: y_data }) GlobalMap().set(used_time=0, times=0, loss_now=loss_now) except: print(2) self.train(x_data, y_data) else: self.train(x_data, y_data) GlobalMap().set(running_global=0, ) return noturn(self.predict(test_data))
def start(self): self.times = 1 GlobalMap().set(progress_num=0) self.lists = [] self.changenum00 = [] self.changenum01 = [] for i in range(24): self.lists += [i] if exists('db/temp/20180515.csv'): self.temp1 = DataFrame(read_csv('db/temp/20180515.csv', header=0)) else: self.temp1 = dataclear(20180515).run() self.compute_initial_figure() self.timer = QTimer(self) self.timer.timeout.connect(self.compute_initial_figure) self.timer.start(1000)
def __init__(self, session): self.session = session #记录session self.saver = train #提供Saver服务 self.loss = None #记录误差 self.optimizer = None #优化器 self.input_n = 0 #输入维度 self.hidden_n = 0 #中间层个数 self.hidden_size = [] #中间层大小 self.output_n = 0 #输出维度 self.input_layer = None self.hidden_layers = [] self.output_layer = None self.label_layer = None self.init_temp = GlobalMap().get('all') #获取所有前台传递过来的数据 self.auto_times = 0 #print(self.init_temp) #测试用 self.ckpt_path = 'ckpt/model-' + str( self.init_temp['time_point']) + '.ckpt' #定义训练模型保存位置 if self.init_temp['fix_seed']: set_random_seed(523456789)
def initial_value(self): '''初始化全局变量''' #print(temp) GlobalMap().set(max_itera = self.max_itera.value(), neurons_num = self.neurons_num.value(), dg_initial = self.dg_initial.value(), dg_adaptive = self.dg_adaptive.value(), precision = self.precision.value(), save_question = 1, save_nopass = 1, temp_path = 'temp', fix_auto = self.fix_auto.isChecked() ) for i in range(24): if exists('ckpt/model-'+ str(i) +'.ckpt.index'): '''每个小时点的训练状况''' {0:self.checkBox_0.setChecked, 1:self.checkBox_1.setChecked, 2:self.checkBox_2.setChecked, 3:self.checkBox_3.setChecked, 4:self.checkBox_4.setChecked, 5:self.checkBox_5.setChecked, 6:self.checkBox_6.setChecked, 7:self.checkBox_7.setChecked, 8:self.checkBox_8.setChecked, 9:self.checkBox_9.setChecked, 10:self.checkBox_10.setChecked, 11:self.checkBox_11.setChecked, 12:self.checkBox_12.setChecked, 13:self.checkBox_13.setChecked, 14:self.checkBox_14.setChecked, 15:self.checkBox_15.setChecked, 16:self.checkBox_16.setChecked, 17:self.checkBox_17.setChecked, 18:self.checkBox_18.setChecked, 19:self.checkBox_19.setChecked, 20:self.checkBox_20.setChecked, 21:self.checkBox_21.setChecked, 22:self.checkBox_22.setChecked, 23:self.checkBox_23.setChecked, }.get(i)(True)
def compute_initial_figure(self, a, b): self.timer.stop() self.axes.hold(True) self.axes.set_title('The ' + str(GlobalMap().get('time_point')) + ':00 ’s distribution prediction') x = a[0] y = a[1] x0 = b[0] y0 = b[1] imgP = imread('core/map.png', ) self.axes.imshow(imgP, extent=[112.1495, 112.288, 30.308, 30.391], aspect='auto') self.axes.plot(x, y, '.b', markersize=3, label='prediction') #预测的结果 self.axes.plot(x0, y0, '.r', markersize=2, label='original') #期望的结果 self.axes.grid(True) self.axes.legend(shadow=True) self.axes.set_xlim(112.150, 112.284) self.axes.set_ylim(30.309, 30.385) self.axes.set(xlabel="longitude", ylabel="latitude") self.draw()
def run(self): if self.working == True: times = GlobalMap().get('time_point') GlobalMap().set(progress_num=0) lists = [] for i in range(24): lists += [i] GlobalMap().set(progress_num=2) if exists('db/temp/20180515.csv') and exists( 'db/temp/20180516.csv'): temp1 = DataFrame(read_csv('db/temp/20180515.csv', header=0)) temp2 = DataFrame(read_csv('db/temp/20180516.csv', header=0)) else: temp1 = dataclear(20180515).run() temp2 = dataclear(20180516).run() GlobalMap().set(progress_num=5) temp11 = temp1[temp1['time'] == lists[times - 1]] temp12 = temp1[temp1['time'] == lists[times]] temp1112 = merge(temp11, temp12, how='outer', on='id').dropna() temp21 = temp2[temp2['time'] == lists[times - 1]] temp22 = temp2[temp2['time'] == lists[times]] temp2122 = merge(temp21, temp22, how='outer', on='id').dropna() temp000 = np.vstack((temp1112['x_x'], temp1112['y_x'])) temp001 = np.vstack((temp1112['x_y'], temp1112['y_y'])) temp020 = np.vstack((temp2122['x_x'], temp2122['y_x'])) #temp021 = np.vstack((temp2122['x_y'],temp2122['y_y'])) cells_array = np.array(NN.main(temp000, temp001, temp020)) np.save(GlobalMap().get('temp_path') + '/test' + str(times), cells_array) GlobalMap().set(progress_num=100, running_global=0) self.changed2_signal.emit(1)
def run(self): if self.working == True: times = GlobalMap().get('time_point') GlobalMap().set(progress_num=0) lists = [] for i in range(24): lists += [i] GlobalMap().set(progress_num=2) if exists('db/temp/20180515.csv') and exists( 'db/temp/20180516.csv'): temp1 = DataFrame(read_csv('db/temp/20180515.csv', header=0)) temp2 = DataFrame(read_csv('db/temp/20180516.csv', header=0)) else: temp1 = dataclear(20180515).run() temp2 = dataclear(20180516).run() GlobalMap().set(progress_num=5) temp11 = temp1[temp1['time'] == lists[times - 1]] temp12 = temp1[temp1['time'] == lists[times]] temp1112 = merge(temp11, temp12, how='outer', on='id').dropna() '''begin 另一种数据处理方式''' # temp1_x = DataFrame(read_csv('db/temp/20180515-x.csv' ,header=0)) # temp1_y = DataFrame(read_csv('db/temp/20180515-y.csv' ,header=0)) # temp2_x = DataFrame(read_csv('db/temp/20180516-x.csv' ,header=0)) # temp2_y = DataFrame(read_csv('db/temp/20180516-y.csv' ,header=0)) # # temp11_x = temp1_x[str(lists[times])] # temp11_y = temp1_y[str(lists[times])] # temp12_x = temp2_x[str(lists[times])] # temp12_y = temp2_y[str(lists[times])] # # temp21_x = temp1_x[str(lists[times+1])] # temp21_y = temp1_y[str(lists[times+1])] # temp22_x = temp2_x[str(lists[times+1])] # temp22_y = temp2_y[str(lists[times+1])] # # cc01 = np.vstack((temp11_x, temp11_y)) # cc02 = np.vstack((temp21_x, temp21_y)) # cc03 = np.vstack((temp12_x, temp12_y)) # cc04 = np.vstack((temp22_x, temp22_y)) '''end''' temp21 = temp2[temp2['time'] == lists[times - 1]] temp22 = temp2[temp2['time'] == lists[times]] temp2122 = merge(temp21, temp22, how='outer', on='id').dropna() temp000 = np.vstack((temp1112['x_x'], temp1112['y_x'])) temp001 = np.vstack((temp1112['x_y'], temp1112['y_y'])) temp020 = np.vstack((temp2122['x_x'], temp2122['y_x'])) temp021 = np.vstack((temp2122['x_y'], temp2122['y_y'])) #用20180515 T 和 T+1 时刻的训练 来预测 20180516 T 时刻 在 T+1 时刻的分布情况 cells_array = np.array(NN.main(temp000, temp001, temp020)) #cells_array = np.array(NN.main(cc01, cc02, cc03 )) np.save(GlobalMap().get('temp_path') + '/test' + str(times), cells_array) GlobalMap().set(progress_num=100) self.changed_signal.emit(cells_array, temp021)
def train(self, cases, labels): '''训练''' limit = self.init_temp['max_itera'] learn_rate = self.init_temp['dg_initial'] * 0.00000001 self.loss = reduce_mean( reduce_sum(square((self.label_layer - self.output_layer)), reduction_indices=[1])) learning_rate = placeholder(float32, shape=[]) #self.train_step = #tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(self.loss) self.train_step = train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9, use_locking=False, use_nesterov=True).minimize( self.loss) #动量优化器 self.session.run(global_variables_initializer()) next_done = 0 starttime = datetime.now() for i in range(limit): self.session.run(self.train_step, feed_dict={ self.input_layer: cases, self.label_layer: labels, learning_rate: learn_rate }) if i % 2 == 0: if not GlobalMap().get('running_global'): break next_done = 1 next_times = i + 1 train_0 = self.train_step loss0 = self.session.run(self.loss, feed_dict={ self.input_layer: cases, self.label_layer: labels }) #更新前台和处理数据 if next_done == 1 and i % next_times == 0: loss1 = self.session.run(self.loss, feed_dict={ self.input_layer: cases, self.label_layer: labels }) GlobalMap().set(loss_now=loss0, times=i, used_time=(datetime.now() - starttime).seconds) if loss1 < self.init_temp['precision']: break if (i + 1) % 50 == 0 and self.init_temp['fix_auto'] and ( loss0 - loss1 < 0.1 or isnan(loss1)): self.auto_times += 1 #print(self.auto_times) GlobalMap().set(progress_num=5 + i * 100 // limit + 10) #进度条 if self.auto_times > 8: break if loss1 < loss0: #在动量的基础上调整学习率 learn_rate *= 1.0 + self.init_temp['dg_adaptive'] else: learn_rate *= 1.0 - self.init_temp['dg_adaptive'] self.train_step = train_0 #print("1:", loss1) #print("shu:",learn_rate) next_done = 0 self.saver.Saver().save(self.session, self.ckpt_path)
def pause(self): GlobalMap().set(running_global=0, )
def accept(self): self.mun = self.dialog_setting.spinBox.value() self.cum[0]=append(self.cum[0], uniform(self.x_n0,self.x_n1,size=(1,self.mun))) self.cum[1]=append(self.cum[1], uniform(self.y_n0,self.y_n1,size=(1,self.mun))) self.accept_move += [[self.x0, self.y0, self.x1-self.x0, self.y1-self.y0, self.mun]] GlobalMap().set(userdefined = self.cum, )
def pause(self): self.timer.stop() GlobalMap().set(running_global=0)
def compute_initial_figure(self): if self.times <= 23: temp11 = self.temp1[self.temp1['time'] == self.lists[self.times - 1]] temp12 = self.temp1[self.temp1['time'] == self.lists[self.times]] temp1112 = merge(temp11, temp12, how='outer', on='id') temp1112 = temp1112[[ 'x_x', 'y_x', 'x_y', 'y_y' ]][temp1112['x_x'] != temp1112['x_y']] #np.isnan(temp1112['y_y'])] temp1112 = temp1112[[ 'x_x', 'y_x', 'x_y', 'y_y' ]][np.square(temp1112['x_x'] - temp1112['x_y']) + np.square(temp1112['y_x'] - temp1112['y_y']) > np.square(0.0001)] #车至少移动了直线距离100 temp1112 = temp1112[['x_x', 'y_x', 'x_y', 'y_y']][np.isfinite(temp1112['x_x'])] temp11120 = merge(temp11, temp12, how='outer', on='id') temp11120 = temp11120[[ 'x_x', 'y_x', 'x_y', 'y_y' ]][np.square(temp11120['x_x'] - temp11120['x_y']) + np.square(temp1112['y_x'] - temp11120['y_y']) > np.square(0.001)] #车至少移动了直线距离1km temp11120 = temp11120[['x_x', 'y_x']][np.isfinite(temp11120['x_x'])] self.changenum00 += [temp1112.shape[0]] self.changenum01 += [temp11120.shape[0]] imgP = imread('core/map.png', ) self.axes.imshow(imgP, extent=[112.1495, 112.288, 30.308, 30.391], aspect='auto') x = temp1112['x_x'] y = temp1112['y_x'] np.save( GlobalMap().get('temp_path') + '/change' + str(self.times) + '.npy', np.vstack((x, y))) self.axes.hold(True) self.axes.scatter(x, y, marker='o', c='', edgecolors='g') self.axes.grid(True) self.axes.set_xlim(112.150, 112.284) self.axes.set_ylim(30.309, 30.385) self.axes.set(xlabel="longitude", ylabel="latitude") self.axes.set_title('The ' + str(self.times) + ':00 ’s distribution statistics') self.draw() self.axes.hold(False) if self.times >= 23: if self.times == 23: self.axes.bar(range(1, 24), self.changenum00, 0.8, color=['g', 'c'], linewidth=0.1, yerr=[ 0.8, ] * 23, error_kw=dict(elinewidth=1.5, ecolor='green')) self.axes.hold(True) self.axes.plot(range(1, 24), self.changenum00, marker="o", linestyle="-", color="r") self.axes.set_xticks(range(1, 24)) self.axes.set(xlabel="hour", ylabel="number") for x, y in zip(range(1, 24), self.changenum00): self.axes.text(x, y + 0.5, '%d' % y, ha='center') self.axes.set_title( 'Number of changes at each time point(At least 100m)') self.draw() self.axes.hold(False) else: self.pause() sleep(1) self.axes.bar(range(1, 24), self.changenum01, 0.8, color=['g', 'c'], linewidth=0.1, yerr=[ 0.8, ] * 23, error_kw=dict(elinewidth=1.5, ecolor='green')) self.axes.hold(True) self.axes.plot(range(1, 24), self.changenum01, marker="o", linestyle="-", color="r") self.axes.set_xticks(range(1, 24)) self.axes.set(xlabel="hour", ylabel="number") for x, y in zip(range(1, 24), self.changenum01): self.axes.text(x, y + 0.5, '%d' % y, ha='center') self.axes.set_title( 'Number of changes at each time point(At least 1km)') self.draw() self.axes.hold(False) self.times += 1