def main(): configs = json.load(open('config.json', 'r')) if not os.path.exists(configs['model']['save_dir']): os.makedirs(configs['model']['save_dir']) data = DataProcessor(os.path.join('data', configs['data']['filename']), configs['data']['train_test_split'], configs['data']['columns']) model = Model() model.build_model(configs) x, y = data.get_train_data(seq_len=configs['data']['sequence_length'], normalise=configs['data']['normalise']) model.train(x, y, epochs=configs['training']['epochs'], batch_size=configs['training']['batch_size'], save_dir=".") x_test, y_test = data.get_test_data( seq_len=configs['data']['sequence_length'], normalise=configs['data']['normalise']) predictions_pointbypoint = model.predict_point_by_point(x_test) plot_results(predictions_pointbypoint, y_test) predictions_fullseq = model.predict_sequence_full( x_test, configs['data']['sequence_length']) plot_results(predictions_fullseq, y_test)
def predict(): configs = json.load(open(CONFIG, 'r')) data = DataLoader(DATA, configs['data']['train_test_split'], configs['data']['columns']) global model if model == None: model = Model() model.load_model(MODEL) x_test, y_test = data.get_test_data( seq_len=configs['data']['sequence_length'], normalise=configs['data']['normalise']) if TYPE == "sequence": predictions = model.predict_sequences_multiple( x_test, configs['data']['sequence_length'], configs['data']['sequence_length']) plot_results_multiple(predictions, y_test, configs['data']['sequence_length']) if TYPE == "point" or TYPE == "predict": predictions = model.predict_point_by_point(x_test) if TYPE == "full": predictions = model.predict_sequence_full( x_test, configs['data']['sequence_length']) if TYPE == "full" or TYPE == "point": plot_results(predictions, y_test) if TYPE == "predict": predicted_value = data.denormalize_windows( predictions[-1], configs['data']['sequence_length']) sys.stdout.write("--END--{}--END--\n".format(predicted_value)) else: sys.stdout.write("--END--")
def main(): configs = json.load(open('config-test.json', 'r')) if not os.path.exists(configs['model']['save_dir']): os.makedirs(configs['model']['save_dir']) data = DataLoader( os.path.join('data', configs['data']['filename']), configs['data']['train_test_split'], configs['data']['columns'] ) model = Model() model.build_model(configs) x, y = data.get_train_data( seq_len=configs['data']['sequence_length'], normalise=configs['data']['normalise'] ) ''' # in-memory training model.train( x, y, epochs = configs['training']['epochs'], batch_size = configs['training']['batch_size'], save_dir = configs['model']['save_dir'] ) ''' # out-of memory generative training steps_per_epoch = math.ceil((data.len_train - configs['data']['sequence_length']) / configs['training']['batch_size']) model.train_generator( data_gen=data.generate_train_batch( seq_len=configs['data']['sequence_length'], batch_size=configs['training']['batch_size'], normalise=configs['data']['normalise'] ), epochs=configs['training']['epochs'], batch_size=configs['training']['batch_size'], steps_per_epoch=steps_per_epoch, save_dir=configs['model']['save_dir'] ) x_test, y_test = data.get_test_data( seq_len=configs['data']['sequence_length'], normalise=configs['data']['normalise'] ) # predictions = model.predict_sequences_multiple(x_test, configs['data']['sequence_length'], configs['data']['sequence_length']) predictions = model.predict_sequence_full(x_test, configs['data']['sequence_length']) # predictions = model.predict_point_by_point(x_test) # plot_results_multiple(predictions, y_test, configs['data']['sequence_length']) plot_results(predictions, y_test)
def main(choice): data = DataLoader(os.path.join('data', configs['data']['filename']), configs['data']['train_test_split'], configs['data']['columns']) model = Model() model.build_model(configs) if (choice != 'info'): x, y = data.get_train_data(seq_len=configs['data']['sequence_length'], normalise=configs['data']['normalise']) # in-memory training model.train(x, y, epochs=configs['training']['epochs'], batch_size=configs['training']['batch_size']) # out-of memory generative training # steps_per_epoch = math.ceil((data.len_train - configs['data']['sequence_length']) / configs['training']['batch_size']) # model.train_generator( # data_gen = data.generate_train_batch( # seq_len = configs['data']['sequence_length'], # batch_size = configs['training']['batch_size'], # normalise = configs['data']['normalise'] # ), # epochs = configs['training']['epochs'], # batch_size = configs['training']['batch_size'], # steps_per_epoch = steps_per_epoch # ) x_test, y_test = data.get_test_data( seq_len=configs['data']['sequence_length'], normalise=configs['data']['normalise']) if (choice == "multi"): predictions = model.predict_sequences_multiple( x_test, configs['data']['sequence_length'], configs['data']['sequence_length']) plot_results_multiple(predictions, y_test, configs['data']['sequence_length']) elif (choice == "seq"): predictions = model.predict_sequence_full( x_test, configs['data']['sequence_length']) plot_results(predictions, y_test) else: predictions = model.predict_point_by_point(x_test) plot_results(predictions, y_test)
return predicted def predict_sequence_full(self, data, window_size): #Shift the window by 1 new prediction each time, re-run predictions on new window curr_frame = data[0] predicted = [] for i in range(len(data)): predicted.append(self.model.predict(curr_frame[newaxis, :, :])[0, 0]) curr_frame = curr_frame[1:] curr_frame = np.insert(curr_frame, [window_size - 2], predicted[-1], axis=0) return predicted def plot_results(predicted_data, true_data): fig = plt.figure(facecolor='white') ax = fig.add_subplot(111) ax.plot(true_data, label='True Data') plt.plot(predicted_data, label='Prediction') plt.legend() plt.show() predictions_pointbypoint = model.predict_point_by_point(x_test) plot_results(predictions_pointbypoint, y_test) predictions_fullseq = model.predict_sequence_full( x_test, configs['data']['sequence_length']) plot_results(predictions_fullseq, y_test)