Last active
January 5, 2022 18:32
-
-
Save A-safarji/a43d73f3acf4055369f6d1461db2b87a to your computer and use it in GitHub Desktop.
Deep learning models for time series forecasting
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
| ### Create the Bidirectional LSTM model | |
| from random import random | |
| from numpy import array | |
| from numpy import cumsum | |
| from keras.models import Sequential | |
| from tensorflow.keras.layers import Dense,Dropout | |
| from keras.layers import TimeDistributed | |
| from keras.layers import Bidirectional | |
| from tensorflow.keras.layers import LSTM | |
| import tensorflow as tf | |
| # Set random seed for reproducibility: get the same result after each time running the model | |
| tf.random.set_seed(1234) | |
| # # Build the BiLSTM model | |
| modell = Sequential() | |
| modell.add(Bidirectional(LSTM(100, activation='relu', input_shape=(100,1)))) #elu | |
| #modell.add(Bidirectional(LSTM(50, dropout=0.5))) | |
| #modell.add(Bidirectional(LSTM(100, dropout=0.5))) | |
| #modell.add(BatchNormalization(momentum=0.6)) | |
| modell.add(Dense(1)) | |
| modell.compile(loss='mean_squared_error', optimizer='adam') #rmsprop | adam | |
| # use early stop to avoid over-fiting | |
| from tensorflow.keras.callbacks import EarlyStopping | |
| monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=30, | |
| verbose=1, mode='auto', restore_best_weights=True) | |
| # fit | |
| history1=modell.fit(X_train,y_train,validation_data=(X_test,ytest), | |
| callbacks=[monitor],verbose=1,epochs=1000) | |
| modell.summary() | |
| # Output: | |
| # Model: "sequential_2" | |
| # _________________________________________________________________ | |
| # Layer (type) Output Shape Param # | |
| # ================================================================= | |
| # bidirectional_2 (Bidirectio (None, 200) 81600 | |
| # nal) | |
| # dense_2 (Dense) (None, 1) 201 | |
| # ================================================================= | |
| # Total params: 81,801 | |
| # Trainable params: 81,801 | |
| # Non-trainable params: 0 | |
| # _________________________________________________________________ | |
| # save the best model | |
| #modell.save("bilstm.h5") | |
| # load the best model | |
| #new_model = tf.keras.models.load_model('bilstm.h5') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment