Created
July 10, 2018 23:42
-
-
Save llSourcell/734493ad8e64aa5a65d55c0169683be8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #for data loading | |
| import etl | |
| #for machine learning | |
| import keras | |
| #load data | |
| dl = etl.ETL() | |
| #build model | |
| def build_network(layers): | |
| model = Sequential() | |
| model.add(LSTM( | |
| input_dim=layers[0], | |
| output_dim=layers[1], | |
| return_sequences=True)) | |
| model.add(Dropout(0.2)) | |
| model.add(LSTM( | |
| layers[2], | |
| return_sequences=False)) | |
| model.add(Dropout(0.2)) | |
| model.add(Dense( | |
| output_dim=layers[3])) | |
| model.add(Activation("tanh")) | |
| start = time.time() | |
| model.compile( | |
| loss=configs['model']['loss_function'], | |
| optimizer=configs['model']['optimiser_function']) | |
| print("> Compilation Time : ", time.time() - start) | |
| return model | |
| #generate training data | |
| data_gen_train = dl.generate_clean_data( | |
| configs['data']['filename_clean'], | |
| batch_size=configs['data']['batch_size'] | |
| ) | |
| with h5py.File(configs['data']['filename_clean'], 'r') as hf: | |
| nrows = hf['x'].shape[0] | |
| ncols = hf['x'].shape[2] | |
| #train model | |
| model = lstm.build_network([ncols, 150, 150, 1]) | |
| model.fit_generator( | |
| data_gen_train, | |
| steps_per_epoch=steps_per_epoch, | |
| epochs=configs['model']['epochs'] | |
| ) | |
| #generate testing data | |
| data_gen_test = dl.generate_clean_data( | |
| configs['data']['filename_clean'], | |
| batch_size=configs['data']['batch_size'], | |
| start_index=ntrain | |
| ) | |
| #make predictions | |
| predictions = model.predict_generator( | |
| generator_strip_xy(data_gen_test, true_values), | |
| steps=steps_test | |
| #plot results | |
| plot_results(predictions[:800], true_values[:800]) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment