print('Best parameters:') print(grid.best_params_) results = pd.DataFrame(grid.cv_results_) results.sort_values(by='rank_test_score', inplace=True) print(results.to_string()) # Build top 3 models idx = 0 top_models = [] for index, row in results.iterrows(): top_model = False if (single_branch): top_model = cnn_batchnorm_lstm.build_model( row['param_layers'], row['param_cnn_layers'], row['param_kernel_size']) else: top_model = bidir_multiple_branch_lstm.build_model( row['param_layers'], row['param_ksize1'], row['param_step_size'], row['param_lstm_units'], row['param_single_lstm_layer']) top_models.append(top_model) top_models[-1].batch_size = row['param_batch_size'] idx = idx + 1 if idx > 2: break early_stopping = EarlyStopping(patience=20) fit_models = [] for i in reversed(range(len(top_models))):
if __name__=='__main__': global_start_time = time.time() #epochs = 1 epochs = 100 seq_len = 40 print('> Loading data... ') X_train, y_train, X_test, y_test = dataload.load_data('daily_spx.csv', seq_len, True) print('> Data Loaded. Compiling...') #lstm_model = lstm.build_model([1, seq_len, 100, 1]) model_layers = [1, 2, 3, 4] cnn_models = [ cnn_batchnorm_lstm.build_model([1, seq_len, 100, 1], x) for x in model_layers ] [model.fit( X_train, y_train, batch_size=512, nb_epoch=epochs, validation_split=0.05) for model in cnn_models ] #lstm_predictions = dataload.predict_sequences_multiple(lstm_model, X_test, seq_len, seq_len) cnn_predictions = [dataload.predict_sequences_multiple(model, X_test, seq_len, seq_len) for model in cnn_models] #predictions = dataload.predict_sequence_full(model, X_test, seq_len) #predictions = dataload.predict_point_by_point(model, X_test)