def to_graphic(model): # singlstep test X, Y = valid_feeder.get_singlstep_test_data(params['test_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.singlstep_predict(X) pred_class = data_util.continue2discrete(pred_value, intervals) print("Predicted classes of single-step test", pred_class.reshape(-1)) # draw figure f1 = draw(true_value, pred_value) f2 = draw(true_class, pred_class, prob) f1.savefig(params['model_path'] + '.singlstep_pred.epoch{:0>4d}.jpg'.format(model.current_epoch)) f2.savefig( params['model_path'] + '.singlstep_pred.epoch{:0>4d}.hm.jpg'.format(model.current_epoch)) # multistep test X, Y = valid_feeder.get_multistep_test_data(params['test_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.multistep_predict(X, params['test_length']) pred_class = data_util.continue2discrete(pred_value, intervals) print("Predicted classes of multi-step test", pred_class.reshape(-1)) # draw figure f1 = draw(true_value, pred_value) f2 = draw(true_class, pred_class, prob) f1.savefig(params['model_path'] + '.multistep_pred.epoch{:0>4d}.jpg'.format(model.current_epoch)) f2.savefig( params['model_path'] + '.multistep_pred.epoch{:0>4d}.hm.jpg'.format(model.current_epoch))
def draw_test_summary(): # singlstep test X, Y = test_feeder.get_singlstep_test_data(params['test_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.singlstep_predict(X) pred_class = data_util.continue2discrete(pred_value, intervals) # draw figure f1 = draw(true_value, pred_value) f2 = draw(true_class, pred_class, prob) f1.savefig( params['model_path'] + '.singlstep_pred.epoch{:0>4d}.test.jpg'.format(model.current_epoch)) f2.savefig( params['model_path'] + '.singlstep_pred.epoch{:0>4d}.hm.test.jpg'.format(model.current_epoch)) # multistep test X, Y = test_feeder.get_multistep_test_data(params['test_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) # (if MCMC) prob : (n_samples, length, input_size, n_classes) # (if MCMC) pred_value : (n_samples, input_length+length, input_size) prob, pred_value = model.multistep_predict(X, params['test_length']) # (if MCMC) prob : (length, input_size, n_classes) if params['montecarlo']: prob = prob.mean(axis=0) if params['montecarlo_pred_mode'] == 'modal': pred_class = continue2discrete(pred_value, intervals) _, l, input_size = pred_value.shape pred_class = mode(pred_class, axis=0)[0].reshape((l, input_size)) elif params['montecarlo_pred_mode'] == 'mean': pred_class = np.vstack( [true_class[:-prob.shape[0]], prob.argmax(axis=-1)]) pred_value = discrete2continue(pred_class, intervals) else: pred_class = continue2discrete(pred_value, intervals) # draw figure f1 = draw(true_value, pred_value) f2 = draw(true_class, pred_class, prob) if params['montecarlo']: fname1 = params['model_path'] + '.multistep_pred.epoch{:0>4d}.test.MCMC.{}.jpg' \ .format(model.current_epoch, params['montecarlo_pred_mode']) fname2 = params['model_path'] + '.multistep_pred.epoch{:0>4d}.hm.test.MCMC.{}.jpg' \ .format(model.current_epoch, params['montecarlo_pred_mode']) else: fname1 = params['model_path'] + '.multistep_pred.epoch{:0>4d}.test.jpg' \ .format(model.current_epoch) fname2 = params['model_path'] + '.multistep_pred.epoch{:0>4d}.hm.test.jpg' \ .format(model.current_epoch) f1.savefig(fname1) f2.savefig(fname2)
def draw_test_summary(): # singlstep test X, Y = test_feeder.get_singlstep_test_data(params['test_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.singlstep_predict(X) pred_class = data_util.continue2discrete(pred_value, intervals) # draw figure f1 = draw(true_value, pred_value) f2 = draw(true_class, pred_class, prob) f1.savefig(params['model_path'] + '.singlstep_pred.epoch{:0>4d}.test.jpg'.format(model.current_epoch)) f2.savefig(params['model_path'] + '.singlstep_pred.epoch{:0>4d}.hm.test.jpg'.format(model.current_epoch)) # multistep test X, Y = test_feeder.get_multistep_test_data(params['test_length']) true = Y pred = model.multistep_predict(X, params['test_length']) # (if MCMC) prob : (length, input_size, n_classes) pred_class = continue2discrete(pred_value, intervals) # draw figure f1 = draw(true_value, pred_value) f2 = draw(true_class, pred_class, prob) fname1 = params['model_path'] + '.multistep_pred.epoch{:0>4d}.test.jpg' \ .format(model.current_epoch) fname2 = params['model_path'] + '.multistep_pred.epoch{:0>4d}.hm.test.jpg' \ .format(model.current_epoch) f1.savefig(fname1) f2.savefig(fname2)
def montecarlo_pred_summary(prob, pred_value, intervals): prob = prob.mean(axis=0) if params['montecarlo_pred_mode'] == 'modal': pred_class = continue2discrete(pred_value, intervals) _, l, input_size = pred_value.shape pred_class = mode(pred_class, axis=0)[0].reshape((l,input_size)) elif params['montecarlo_pred_mode'] == 'mean': pred_class = np.vstack( [continue2discrete(pred_value[0,:-prob.shape[0],:], intervals), prob.argmax(axis=-1)]) pred_value = discrete2continue(pred_class, intervals) return pred_class, pred_value, prob
def on_stage_end(self, model, logs=None): self.SaveModel(model) hist = [] if os.path.exists(params['hist_path']): hist = pkl.loads(open(params['hist_path'],'rb').read()) RMSE = {'epoch' : model.current_epoch} # set solver with batch_size=1 and input_length=1 solver_test._solver.set_weights(solver_train._solver.get_weights()) model._set_solver(solver_test) # singlstep : deterministic predictor model._set_predictor(predictor_d) # singlstep test for trainset X, Y = train_feeder.get_singlstep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.singlstep_predict(X, lead_length=params['lead_length']) solver_test._solver.reset_states() pred_class = data_util.continue2discrete(pred_value, intervals) RMSE['train.singlstep.true.value'] = true_value RMSE['train.singlstep.true.class'] = true_class RMSE['train.singlstep.pred.value'] = pred_value RMSE['train.singlstep.pred.class'] = pred_class RMSE['train.singlstep.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # singlestep test for validset.2 X, Y = valid_feeder2.get_singlstep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.singlstep_predict(X, lead_length=params['lead_length']) solver_test._solver.reset_states() pred_class = data_util.continue2discrete(pred_value, intervals) RMSE['valid.2.singlstep.true.value'] = true_value RMSE['valid.2.singlstep.true.class'] = true_class RMSE['valid.2.singlstep.pred.value'] = pred_value RMSE['valid.2.singlstep.pred.class'] = pred_class RMSE['valid.2.singlstep.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # singlstep test for validset.1 X, Y = valid_feeder1.get_singlstep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.singlstep_predict(X, lead_length=params['lead_length']) solver_test._solver.reset_states() pred_class = data_util.continue2discrete(pred_value, intervals) RMSE['valid.1.singlstep.true.value'] = true_value RMSE['valid.1.singlstep.true.class'] = true_class RMSE['valid.1.singlstep.pred.value'] = pred_value RMSE['valid.1.singlstep.pred.class'] = pred_class RMSE['valid.1.singlstep.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # draw figure f1 = self.draw(true_value, pred_value) f2 = self.draw(true_class, pred_class, prob) f1.savefig(params['model_path'] + '.singlstep_pred.epoch{:0>4d}.jpg'.format(model.current_epoch)) f2.savefig(params['model_path'] + '.singlstep_pred.epoch{:0>4d}.hm.jpg'.format(model.current_epoch)) # multistep : deterministic predictor model._set_predictor(predictor_d) # multistep test for trainset X, Y = train_feeder.get_multistep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.multistep_predict(X, length=params['test_length'], lead_length=params['lead_length']) solver_test._solver.reset_states() pred_class = data_util.continue2discrete(pred_value, intervals) RMSE['train.multistep.determ.true.value'] = true_value RMSE['train.multistep.determ.true.class'] = true_class RMSE['train.multistep.determ.pred.value'] = pred_value RMSE['train.multistep.determ.pred.class'] = pred_class RMSE['train.multistep.determ.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # multistep test for validset.2 X, Y = valid_feeder2.get_multistep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.multistep_predict(X, length=params['test_length'], lead_length=params['lead_length']) solver_test._solver.reset_states() pred_class = data_util.continue2discrete(pred_value, intervals) RMSE['valid.2.multistep.determ.true.value'] = true_value RMSE['valid.2.multistep.determ.true.class'] = true_class RMSE['valid.2.multistep.determ.pred.value'] = pred_value RMSE['valid.2.multistep.determ.pred.class'] = pred_class RMSE['valid.2.multistep.determ.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # multistep test for validset.1 X, Y = valid_feeder1.get_multistep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.multistep_predict(X, length=params['test_length'], lead_length=params['lead_length']) solver_test._solver.reset_states() pred_class = data_util.continue2discrete(pred_value, intervals) RMSE['valid.1.multistep.determ.true.value'] = true_value RMSE['valid.1.multistep.determ.true.class'] = true_class RMSE['valid.1.multistep.determ.pred.value'] = pred_value RMSE['valid.1.multistep.determ.pred.class'] = pred_class RMSE['valid.1.multistep.determ.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # draw figure f1 = self.draw(true_value, pred_value) f2 = self.draw(true_class, pred_class, prob) f1.savefig(params['model_path'] + '.multistep_pred.determ.epoch{:0>4d}.jpg'.format(model.current_epoch)) f2.savefig(params['model_path'] + '.multistep_pred.determ.epoch{:0>4d}.hm.jpg'.format(model.current_epoch)) def montecarlo_pred_summary(prob, pred_value, intervals): prob = prob.mean(axis=0) if params['montecarlo_pred_mode'] == 'modal': pred_class = continue2discrete(pred_value, intervals) _, l, input_size = pred_value.shape pred_class = mode(pred_class, axis=0)[0].reshape((l,input_size)) elif params['montecarlo_pred_mode'] == 'mean': pred_class = np.vstack( [continue2discrete(pred_value[0,:-prob.shape[0],:], intervals), prob.argmax(axis=-1)]) pred_value = discrete2continue(pred_class, intervals) return pred_class, pred_value, prob if params['montecarlo']: solver_test_mcmc._solver.set_weights(solver_train._solver.get_weights()) model._set_solver(solver_test_mcmc) # multistep : MCMC predictor model._set_predictor(predictor_m) # multistep test for trainset X, Y = train_feeder.get_multistep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.multistep_predict(X, length=params['test_length'], lead_length=params['lead_length']) solver_test_mcmc._solver.reset_states() pred_class, pred_value, prob = montecarlo_pred_summary(prob, pred_value, intervals) RMSE['train.multistep.mcmc.true.value'] = true_value RMSE['train.multistep.mcmc.true.class'] = true_class RMSE['train.multistep.mcmc.pred.value'] = pred_value RMSE['train.multistep.mcmc.pred.class'] = pred_class RMSE['train.multistep.mcmc.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # multistep test for validset.2 X, Y = valid_feeder2.get_multistep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.multistep_predict(X, length=params['test_length'], lead_length=params['lead_length']) solver_test_mcmc._solver.reset_states() pred_class, pred_value, prob = montecarlo_pred_summary(prob, pred_value, intervals) RMSE['valid.2.multistep.mcmc.true.value'] = true_value RMSE['valid.2.multistep.mcmc.true.class'] = true_class RMSE['valid.2.multistep.mcmc.pred.value'] = pred_value RMSE['valid.2.multistep.mcmc.pred.class'] = pred_class RMSE['valid.2.multistep.mcmc.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # multistep test for validset.1 X, Y = valid_feeder1.get_multistep_test_data(params['test_length'], lead_length=params['lead_length']) true_class = Y.argmax(axis=-1) true_value = data_util.discrete2continue(true_class, intervals) prob, pred_value = model.multistep_predict(X, length=params['test_length'], lead_length=params['lead_length']) solver_test_mcmc._solver.reset_states() pred_class, pred_value, prob = montecarlo_pred_summary(prob, pred_value, intervals) RMSE['valid.1.multistep.mcmc.true.value'] = true_value RMSE['valid.1.multistep.mcmc.true.class'] = true_class RMSE['valid.1.multistep.mcmc.pred.value'] = pred_value RMSE['valid.1.multistep.mcmc.pred.class'] = pred_class RMSE['valid.1.multistep.mcmc.rmse'] = self.rmse(true_value, pred_value[-true_value.shape[0]:,:]) # draw figure f1 = self.draw(true_value, pred_value) f2 = self.draw(true_class, pred_class, prob) f1.savefig(params['model_path'] + '.multistep_pred.mcmc.epoch{:0>4d}.jpg'.format(model.current_epoch)) f2.savefig(params['model_path'] + '.multistep_pred.mcmc.epoch{:0>4d}.hm.jpg'.format(model.current_epoch)) plt.close('all') print("\n" + "#"*80) print("RMSE: epoch = {}".format(RMSE['epoch'])) print("{} | {} | {} | {}".format( ''.ljust(15), 'train'.ljust(15), 'valid.1'.ljust(15), 'valid.2'.ljust(10))) print("{} | {:>.10} | {:>.10} | {:>.10}".format( 'singlstep'.ljust(20), str(RMSE['train.singlstep.rmse']).ljust(15), str(RMSE['valid.1.singlstep.rmse']).ljust(15), str(RMSE['valid.2.singlstep.rmse']).ljust(15))) print("{} | {:>.10} | {:>.10} | {:>.10}".format( 'multistep.determ'.ljust(20), str(RMSE['train.multistep.determ.rmse']).ljust(15), str(RMSE['valid.1.multistep.determ.rmse']).ljust(15), str(RMSE['valid.2.multistep.determ.rmse']).ljust(15))) if params['montecarlo']: print("{} | {:>.10} | {:>.10} | {:>.10}".format( 'multistep.mcmc'.ljust(20), str(RMSE['train.multistep.mcmc.rmse']).ljust(15), str(RMSE['valid.1.multistep.mcmc.rmse']).ljust(15), str(RMSE['valid.2.multistep.mcmc.rmse']).ljust(15))) print("#"*80 + '\n') hist.append(RMSE) open(params['hist_path'], 'wb').write(pkl.dumps(hist)) # set normal solver model._set_solver(solver_train)