├── DeepTimeSeries ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-36.pyc │ └── utils.cpython-36.pyc ├── models │ ├── RNN2Dense.py │ ├── Seq2Seq.py │ ├── TimeSeriesBase.py │ ├── __init__.py │ └── __pycache__ │ │ ├── RNN2Dense.cpython-36.pyc │ │ ├── Seq2Seq.cpython-36.pyc │ │ ├── TimeSeriesBase.cpython-36.pyc │ │ ├── __init__.cpython-36.pyc │ │ └── test.cpython-36.pyc └── utils.py ├── Doc ├── Seq2Seq_results.png ├── models.odg ├── models.pdf ├── models.png ├── run_model.png ├── save_model.png ├── supervised.png └── timeseries_dataframe.png ├── Example ├── PowerAnalysis.py ├── RNN2Dense_results.png ├── Seq2Seq_results.png ├── clean_data.csv ├── test.h5 └── test.info └── README.md /DeepTimeSeries/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/__init__.py -------------------------------------------------------------------------------- /DeepTimeSeries/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /DeepTimeSeries/__pycache__/utils.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/__pycache__/utils.cpython-36.pyc -------------------------------------------------------------------------------- /DeepTimeSeries/models/RNN2Dense.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from keras.models import Model 3 | from keras.layers import Input, SimpleRNN, Dense, GRU, LSTM, Dropout, Reshape, Lambda 4 | from keras import backend as K 5 | from DeepTimeSeries.models.TimeSeriesBase import TSBase 6 | 7 | class RNN2Dense_1(TSBase): 8 | def __init__(self, input_shape, output_shape, cell, cell_units, 9 | dense_units = None, dropout_rate = 0.3, reload = False ): 10 | """ 11 | input_shape: shape of input data, (n_memory_steps, n_in_features) 12 | output_shape: shape of output data, (n_forcast_steps, n_out_features) 13 | cell: cell in the RNN part, 'SimpleRNN' / 'LSTM' / 'GRU' 14 | cell_units: number of hidden cell unit in RNN part, integer, e.g. 100 15 | dense_units: units of the hidden dense layers, a tuple, e.g, (20,30) 16 | reload: True if feed externally, False if generate from scratch 17 | """ 18 | 19 | if reload: 20 | self.model = None 21 | self.class_info = None 22 | else: 23 | # just for future save 24 | self.class_info={'class': 'RNN2Dense_1', 'input_shape': input_shape, 25 | 'output_shape': output_shape, 'cell': cell, 'cell_units': cell_units, 26 | 'dense_units': dense_units} 27 | 28 | # check input variables 29 | assert cell in ['SimpleRNN', 'LSTM', 'GRU'] 30 | assert type(cell_units) == int 31 | assert type(dense_units) == tuple 32 | 33 | # batch_input_shape = [n_sample, n_backtrack_step, n_feature] 34 | x_in = Input(input_shape) 35 | if cell == 'SimpleRNN': 36 | x = SimpleRNN(units=cell_units)(x_in) 37 | elif cell == 'LSTM': 38 | x = LSTM(units=cell_units)(x_in) 39 | elif cell == 'GRU': 40 | x = GRU(units=cell_units)(x_in) 41 | if dense_units != None: 42 | for n_units in dense_units: 43 | x = Dense(n_units, activation='relu')(x) 44 | x = Dropout(dropout_rate)(x) 45 | x = Dense(np.prod(output_shape))(x) 46 | x_out = Reshape((output_shape))(x) 47 | self.model = Model(inputs = x_in, outputs = x_out) 48 | 49 | 50 | 51 | class RNN2Dense_2(TSBase): 52 | def __init__(self, input_shape, output_shape, cell, cell_units, 53 | dense_units = None, dropout_rate = 0.3, reload = False ): 54 | """ 55 | input_shape: shape of input data, (n_memory_steps, n_in_features) 56 | output_shape: shape of output data, (n_forcast_steps, n_out_features) 57 | cell: cell in the RNN part, 'SimpleRNN' / 'LSTM' / 'GRU' 58 | cell_units: number of hidden cell unit in RNN part, integer, e.g. 100 59 | dense_units: units of the hidden dense layers, a tuple, e.g, (20,30) 60 | reload: True if feed externally, False if generate from scratch 61 | """ 62 | 63 | if reload: 64 | self.model = None 65 | self.class_info = None 66 | else: 67 | # just for future save 68 | self.class_info={'class': 'RNN2Dense_2', 'input_shape': input_shape, 69 | 'output_shape': output_shape, 'cell': cell, 'cell_units': cell_units, 70 | 'dense_units': dense_units} 71 | 72 | # check input variables 73 | assert cell in ['SimpleRNN', 'LSTM', 'GRU'] 74 | assert type(cell_units) == int 75 | assert type(dense_units) == tuple 76 | 77 | # batch_input_shape = [n_sample, n_backtrack_step, n_feature] 78 | x_in = Input(input_shape) 79 | if cell == 'SimpleRNN': 80 | x = SimpleRNN(units=cell_units, return_sequences=True)(x_in) 81 | elif cell == 'LSTM': 82 | x = LSTM(units=cell_units, return_sequences=True)(x_in) 83 | elif cell == 'GRU': 84 | x = GRU(units=cell_units, return_sequences=True)(x_in) 85 | # if dense_units != None: 86 | # for n_units in dense_units: 87 | # x = Dense(n_units, activation='relu')(x) 88 | # x = Dropout(dropout_rate)(x) 89 | #x = Reshape((-1,output_shape[-1]*cell_units))(x) 90 | #x_out = Dense(20)(x) 91 | #x_out = Dense(np.prod(output_shape))(x) 92 | #x_out = Reshape((output_shape))(x) 93 | self.model = Model(inputs = x_in, outputs = x) -------------------------------------------------------------------------------- /DeepTimeSeries/models/Seq2Seq.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from keras.models import Model 3 | from keras.layers import Input, SimpleRNN, Dense, GRU, LSTM, Reshape, Lambda 4 | from keras import backend as K 5 | from DeepTimeSeries.models.TimeSeriesBase import TSBase 6 | 7 | 8 | class Seq2Seq_1(TSBase): 9 | def __init__(self, input_shape, output_shape, cell, cell_units, reload = False ): 10 | """ 11 | input_shape: shape of input data, (n_memory_steps, n_in_features) 12 | output_shape: shape of output data, (n_forcast_steps, n_out_features) 13 | cell: cell in the RNN part, 'SimpleRNN' / 'LSTM' / 'GRU' 14 | cell_units: number of hidden cell unit in RNN part, integer, e.g. 100 15 | reload: True if feed externally, False if generate from scratch 16 | """ 17 | if reload: 18 | self.model = None 19 | self.class_info = None 20 | else: 21 | # just for future reload 22 | self.class_info = {'class': 'Seq2Seq', 'input_shape': input_shape, 'output_shape': output_shape, 23 | 'cell': cell, 'cell_units': cell_units} 24 | 25 | if cell == 'LSTM': 26 | # declare encoder and decoder objects 27 | encoder = LSTM(units = cell_units, return_state = True) 28 | decoder = LSTM(units = cell_units, return_sequences=True, return_state = True) 29 | decoder_dense = Dense(output_shape[-1]) 30 | 31 | # data flow 32 | encoder_input = Input(input_shape) 33 | encoder_output, state_h, state_c = encoder(encoder_input) 34 | encoder_state = [state_h, state_c] 35 | 36 | decoder_input = Input((1,output_shape[-1])) 37 | 38 | # initial input and state for iteration 39 | iter_input = decoder_input 40 | iter_state = encoder_state 41 | all_output = [] 42 | 43 | for _ in range(output_shape[0]): 44 | # Run the decoder on one timestep, output == state_h since only one time step 45 | output, state_h, state_c = decoder(iter_input, initial_state=iter_state) 46 | output = decoder_dense(output) 47 | 48 | # Store the current prediction (we will concatenate all predictions later) 49 | all_output.append(output) 50 | 51 | # Reinject the outputs and state for the next loop iteration 52 | iter_input = output 53 | iter_state = [state_h, state_c] 54 | 55 | elif cell == 'SimpleRNN': 56 | # declare encoder and decoder objects 57 | encoder = SimpleRNN(units = cell_units, return_state = True) 58 | decoder = SimpleRNN(units = cell_units, return_sequences=True, return_state = True) 59 | decoder_dense = Dense(output_shape[-1]) 60 | 61 | # data flow 62 | encoder_input = Input(input_shape) 63 | encoder_output, state_h = encoder(encoder_input) 64 | encoder_state = state_h 65 | 66 | decoder_input = Input((1,output_shape[-1])) 67 | 68 | # initial input and state for iteration 69 | iter_input = decoder_input 70 | iter_state = encoder_state 71 | all_output = [] 72 | 73 | for _ in range(output_shape[0]): 74 | # Run the decoder on one timestep, output == state_h since only one time step 75 | output, state_h = decoder(iter_input, initial_state=iter_state) 76 | output = decoder_dense(output) 77 | 78 | # Store the current prediction (we will concatenate all predictions later) 79 | all_output.append(output) 80 | 81 | # Reinject the outputs and state for the next loop iteration 82 | iter_input = output 83 | iter_state = state_h 84 | 85 | elif cell == 'GRU': 86 | # declare encoder and decoder objects 87 | encoder = GRU(units = cell_units, return_state = True) 88 | decoder = GRU(units = cell_units, return_sequences=True, return_state = True) 89 | decoder_dense = Dense(output_shape[-1]) 90 | 91 | # data flow 92 | encoder_input = Input(input_shape) 93 | encoder_output, state_h = encoder(encoder_input) 94 | encoder_state = state_h 95 | 96 | decoder_input = Input((1,output_shape[-1])) 97 | 98 | # initial input and state for iteration 99 | iter_input = decoder_input 100 | iter_state = encoder_state 101 | all_output = [] 102 | 103 | for _ in range(output_shape[0]): 104 | # Run the decoder on one timestep, output == state_h since only one time step 105 | output, state_h = decoder(iter_input, initial_state=iter_state) 106 | output = decoder_dense(output) 107 | 108 | # Store the current prediction (we will concatenate all predictions later) 109 | all_output.append(output) 110 | 111 | # Reinject the outputs and state for the next loop iteration 112 | iter_input = output 113 | iter_state = state_h 114 | 115 | # Concatenate all predictions 116 | decoder_output = Lambda(lambda x: K.concatenate(x, axis=1))(all_output) 117 | self.model = Model([encoder_input, decoder_input], decoder_output) 118 | 119 | def data_preprocessing(self, x, y = None): 120 | x_new = [x, np.ones((len(x), 1, self.class_info['output_shape'][1]))] 121 | y_new = y 122 | 123 | return x_new, y_new 124 | 125 | 126 | class Seq2Seq_2(TSBase): 127 | def __init__(self, input_shape, output_shape, cell, cell_units, reload = False ): 128 | """ 129 | input_shape: shape of input data, (n_memory_steps, n_in_features) 130 | output_shape: shape of output data, (n_forcast_steps, n_out_features) 131 | cell: cell in the RNN part, 'SimpleRNN' / 'LSTM' / 'GRU' 132 | cell_units: number of hidden cell unit in RNN part, integer, e.g. 100 133 | reload: True if feed externally, False if generate from scratch 134 | """ 135 | if reload: 136 | self.model = None 137 | self.class_info = None 138 | else: 139 | # just for future reload 140 | self.class_info = {'class': 'Seq2Seq', 'input_shape': input_shape, 'output_shape': output_shape, 141 | 'cell': cell, 'cell_units': cell_units} 142 | 143 | if cell == 'LSTM': 144 | # declare encoder and decoder objects 145 | encoder = LSTM(units = cell_units, return_state = True) 146 | decoder = LSTM(units = cell_units, return_sequences=True, return_state = True) 147 | decoder_dense = Dense(output_shape[-1]) 148 | 149 | # data flow 150 | encoder_input = Input(input_shape) 151 | encoder_output, state_h, state_c = encoder(encoder_input) 152 | encoder_state = [state_h, state_c] 153 | 154 | # initial input and state for iteration 155 | iter_input = Reshape((1,output_shape[-1]))(decoder_dense(state_h)) 156 | iter_state = encoder_state 157 | all_output = [] 158 | 159 | for _ in range(output_shape[0]): 160 | # Run the decoder on one timestep, output == state_h since only one time step 161 | output, state_h, state_c = decoder(iter_input, initial_state=iter_state) 162 | output = decoder_dense(output) 163 | 164 | # Store the current prediction (we will concatenate all predictions later) 165 | all_output.append(output) 166 | 167 | # Reinject the outputs and state for the next loop iteration 168 | iter_input = output 169 | iter_state = [state_h, state_c] 170 | 171 | elif cell == 'SimpleRNN': 172 | # declare encoder and decoder objects 173 | encoder = SimpleRNN(units = cell_units, return_state = True) 174 | decoder = SimpleRNN(units = cell_units, return_sequences=True, return_state = True) 175 | decoder_dense = Dense(output_shape[-1]) 176 | 177 | # data flow 178 | encoder_input = Input(input_shape) 179 | encoder_output, state_h = encoder(encoder_input) 180 | encoder_state = state_h 181 | 182 | # initial input and state for iteration 183 | iter_input = Reshape((1,output_shape[-1]))(decoder_dense(state_h)) 184 | iter_state = encoder_state 185 | all_output = [] 186 | 187 | for _ in range(output_shape[0]): 188 | # Run the decoder on one timestep, output == state_h since only one time step 189 | output, state_h = decoder(iter_input, initial_state=iter_state) 190 | output = decoder_dense(output) 191 | 192 | # Store the current prediction (we will concatenate all predictions later) 193 | all_output.append(output) 194 | 195 | # Reinject the outputs and state for the next loop iteration 196 | iter_input = output 197 | iter_state = state_h 198 | 199 | elif cell == 'GRU': 200 | # declare encoder and decoder objects 201 | encoder = GRU(units = cell_units, return_state = True) 202 | decoder = GRU(units = cell_units, return_sequences=True, return_state = True) 203 | decoder_dense = Dense(output_shape[-1]) 204 | 205 | # data flow 206 | encoder_input = Input(input_shape) 207 | encoder_output, state_h = encoder(encoder_input) 208 | encoder_state = state_h 209 | 210 | # initial input and state for iteration 211 | iter_input = Reshape((1,output_shape[-1]))(decoder_dense(state_h)) 212 | iter_state = encoder_state 213 | all_output = [] 214 | 215 | for _ in range(output_shape[0]): 216 | # Run the decoder on one timestep, output == state_h since only one time step 217 | output, state_h = decoder(iter_input, initial_state=iter_state) 218 | output = decoder_dense(output) 219 | 220 | # Store the current prediction (we will concatenate all predictions later) 221 | all_output.append(output) 222 | 223 | # Reinject the outputs and state for the next loop iteration 224 | iter_input = output 225 | iter_state = state_h 226 | 227 | # Concatenate all predictions 228 | decoder_output = Lambda(lambda x: K.concatenate(x, axis=1))(all_output) 229 | self.model = Model(encoder_input, decoder_output) -------------------------------------------------------------------------------- /DeepTimeSeries/models/TimeSeriesBase.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | from keras.optimizers import Adam 3 | 4 | class TSBase: 5 | def summary(self): 6 | return self.model.summary() 7 | 8 | def compile(self, optimizer=Adam(0.001), loss='mse', metrics='mse'): 9 | self.model.compile(optimizer=optimizer, loss='mse') 10 | 11 | def save(self,model_name): 12 | self.model.save(model_name+'.h5') 13 | pickle.dump(self.class_info,open(model_name+'.info','wb')) 14 | 15 | def data_preprocessing(self,x, y = None): 16 | # overload this method if it doesn't fit your need. 17 | x_new = x 18 | y_new = y 19 | 20 | return x_new, y_new 21 | 22 | def fit(self, x=None, y=None, batch_size=None, epochs=1, verbose=1, validation_data=None): 23 | x_train, y_train = self.data_preprocessing(x, y) 24 | if validation_data != None: 25 | x_test, y_test = self.data_preprocessing(validation_data[0], validation_data[1]) 26 | validation_data = (x_test, y_test) 27 | 28 | self.history=self.model.fit( 29 | x_train, y_train, 30 | batch_size=batch_size, 31 | epochs=epochs, 32 | verbose=verbose, 33 | validation_data=validation_data, 34 | shuffle=False) 35 | 36 | return self.history 37 | 38 | def predict(self, x_test): 39 | x_test, _ = self.data_preprocessing(x_test) 40 | y_predict = self.model.predict(x_test) 41 | 42 | return y_predict -------------------------------------------------------------------------------- /DeepTimeSeries/models/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/models/__init__.py -------------------------------------------------------------------------------- /DeepTimeSeries/models/__pycache__/RNN2Dense.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/models/__pycache__/RNN2Dense.cpython-36.pyc -------------------------------------------------------------------------------- /DeepTimeSeries/models/__pycache__/Seq2Seq.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/models/__pycache__/Seq2Seq.cpython-36.pyc -------------------------------------------------------------------------------- /DeepTimeSeries/models/__pycache__/TimeSeriesBase.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/models/__pycache__/TimeSeriesBase.cpython-36.pyc -------------------------------------------------------------------------------- /DeepTimeSeries/models/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/models/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /DeepTimeSeries/models/__pycache__/test.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/DeepTimeSeries/models/__pycache__/test.cpython-36.pyc -------------------------------------------------------------------------------- /DeepTimeSeries/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | from DeepTimeSeries.models.RNN2Dense import * 4 | from DeepTimeSeries.models.Seq2Seq import * 5 | from keras.models import load_model 6 | 7 | def series_to_superviesed(x_timeseries, y_timeseries, n_memory_step, n_forcast_step, split = None): 8 | ''' 9 | x_timeseries: input time series data, numpy array, (time_step, features) 10 | y_timeseries: target time series data, numpy array, (time_step, features) 11 | n_memory_step: number of memory step in supervised learning, int 12 | n_forcast_step: number of forcase step in supervised learning, int 13 | split: portion of data to be used as train set, float, e.g. 0.8 14 | ''' 15 | assert len(x_timeseries.shape) == 2, 'x_timeseries must be shape of (time_step, features)' 16 | assert len(y_timeseries.shape) == 2, 'y_timeseries must be shape of (time_step, features)' 17 | 18 | input_step, input_feature = x_timeseries.shape 19 | output_step, output_feature = y_timeseries.shape 20 | assert input_step == output_step, 'number of time_step of x_timeseries and y_timeseries are not consistent!' 21 | 22 | n_RNN_sample=input_step-n_forcast_step-n_memory_step+1 23 | RNN_x=np.zeros((n_RNN_sample,n_memory_step, input_feature)) 24 | RNN_y=np.zeros((n_RNN_sample,n_forcast_step, output_feature)) 25 | 26 | for n in range(n_RNN_sample): 27 | RNN_x[n,:,:]=x_timeseries[n:n+n_memory_step,:] 28 | RNN_y[n,:,:]=y_timeseries[n+n_memory_step:n+n_memory_step+n_forcast_step,:] 29 | if split != None: 30 | assert (split <=0.9) & (split >= 0.1), 'split not in reasonable range' 31 | return RNN_x[:int(split*len(RNN_x))], RNN_y[:int(split*len(RNN_x))],\ 32 | RNN_x[int(split*len(RNN_x))+1:], RNN_y[int(split*len(RNN_x))+1:] 33 | else: 34 | return RNN_x, RNN_y, None, None 35 | 36 | 37 | def load_time_series_model(model_name): 38 | # load model from files 39 | model_timeseries = load_model(model_name+'.h5') 40 | model_info = pickle.load(open(model_name+'.info','rb')) 41 | print('\nloading '+model_info['class']+' model with '+model_info['cell']+' cell ...') 42 | 43 | # prepare model_info for input 44 | model_name = model_info['class'] 45 | model_info.pop('class') 46 | 47 | # reload models (only this part needs to change) 48 | if model_name == 'RNN2Dense_1': 49 | model = RNN2Dense_1(**model_info,reload = True) 50 | elif 'RNN2Dense_2': 51 | model = RNN2Dense_2(**model_info,reload = True) 52 | elif 'Seq2Seq_1': 53 | model = Seq2Seq_1(**model_info,reload = True) 54 | elif 'Seq2Seq_2': 55 | model = Seq2Seq_2(**model_info,reload = True) 56 | 57 | # restore model_name 58 | model_info['class'] = model_name 59 | # print model information 60 | print('information of the model:') 61 | print(model_info) 62 | 63 | # load data to model object 64 | model.model = model_timeseries 65 | model.class_info = model_info 66 | 67 | return model -------------------------------------------------------------------------------- /Doc/Seq2Seq_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Doc/Seq2Seq_results.png -------------------------------------------------------------------------------- /Doc/models.odg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Doc/models.odg -------------------------------------------------------------------------------- /Doc/models.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Doc/models.pdf -------------------------------------------------------------------------------- /Doc/models.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Doc/models.png -------------------------------------------------------------------------------- /Doc/run_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Doc/run_model.png -------------------------------------------------------------------------------- /Doc/save_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Doc/save_model.png -------------------------------------------------------------------------------- /Doc/supervised.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Doc/supervised.png -------------------------------------------------------------------------------- /Doc/timeseries_dataframe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Doc/timeseries_dataframe.png -------------------------------------------------------------------------------- /Example/PowerAnalysis.py: -------------------------------------------------------------------------------- 1 | ''' 2 | this is an example file to show how to use DeepTimeSeries. The dataset was download from: 3 | http://archive.ics.uci.edu/ml/datasets/Individual+household+electric+power+consumption 4 | 5 | This is a single household power comsumption data. The data has been cleaned and resampled. 6 | The dataset used here is "clean_data.csv" 7 | ''' 8 | import pandas as pd 9 | from sklearn.preprocessing import MinMaxScaler 10 | from DeepTimeSeries.models.RNN2Dense import * 11 | from DeepTimeSeries.models.Seq2Seq import * 12 | from DeepTimeSeries.utils import series_to_superviesed, load_time_series_model 13 | import numpy as np 14 | import matplotlib.pyplot as plt 15 | 16 | # hyperparameters ======================= 17 | project_name = 'test' # will be used to save model 18 | task = 'train' # 'train' / 'predict' 19 | n_memory_steps = 3 # time steps for encoder 20 | n_forcast_steps = 3 # time steps for decoder 21 | train_split = 0.8 # protion as train set 22 | batch_size = 72 # batch size for training 23 | epochs = 1 # epochs for training 24 | test_model = 'RNN2Dense_2' # 'RNN2Dense' / 'Seq2Seq_1' / 'Seq2Seq_2' 25 | cell = 'SimpleRNN' # 'SimpleRNN' / 'LSTM' / 'GRU' 26 | is_augmentation = False # if True, x^2, x^3 will be included 27 | # ======================================= 28 | 29 | # load data 30 | df = pd.read_csv('clean_data.csv', index_col=0, header=0) 31 | values = df.values 32 | 33 | # data augmentation, add x^2 and x^3 34 | if is_augmentation: 35 | values = np.hstack( 36 | (np.hstack((values,values**2)), 37 | values**3)) 38 | 39 | # scale data between 0 ~ 1 for better training results 40 | data_scaler = MinMaxScaler(feature_range=(0, 1)) 41 | scaled_values = data_scaler.fit_transform(values) 42 | 43 | # convert to supervised learning compatible format 44 | x_timeseries = scaled_values 45 | y_timeseries = scaled_values[:,1].reshape(-1,1) 46 | 47 | x_train, y_train, x_test, y_test = \ 48 | series_to_superviesed(x_timeseries, y_timeseries, n_memory_steps, n_forcast_steps, split = 0.8) 49 | print('\nsize of x_train, y_train, x_test, y_test:') 50 | print(x_train.shape, y_train.shape, x_test.shape, y_test.shape ) 51 | 52 | # train model & inference 53 | if task == 'train': 54 | # build model 55 | if test_model == 'RNN2Dense_1': 56 | model = RNN2Dense_1(x_train.shape[1:], y_train.shape[1:], cell, 300, (20,)) 57 | elif test_model == 'RNN2Dense_2': 58 | model = RNN2Dense_2(x_train.shape[1:], y_train.shape[1:], cell, 300, (20,)) 59 | elif test_model == 'Seq2Seq_1': 60 | model = Seq2Seq_1(x_train.shape[1:], y_train.shape[1:], cell, 300) 61 | elif test_model == 'Seq2Seq_2': 62 | model = Seq2Seq_2(x_train.shape[1:], y_train.shape[1:], cell, 300) 63 | print(model.summary()) 64 | # compile model 65 | model.compile() 66 | # train model 67 | history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, 68 | verbose=2, validation_data=(x_test,y_test)) 69 | # save model 70 | model.save(project_name) 71 | 72 | elif task == 'predict': 73 | # reload model 74 | model = load_time_series_model(project_name) 75 | # predict data 76 | y_pred = model.predict(x_test) 77 | # plot results 78 | for n in range(n_forcast_steps): 79 | plt.subplot(n_forcast_steps,1,n+1) 80 | plt.plot(y_test[500:800,n,:],'b', label = 'True') 81 | plt.plot(y_pred[500:800,n,:],'r', label = 'Predict') 82 | plt.legend() 83 | plt.show() -------------------------------------------------------------------------------- /Example/RNN2Dense_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Example/RNN2Dense_results.png -------------------------------------------------------------------------------- /Example/Seq2Seq_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Example/Seq2Seq_results.png -------------------------------------------------------------------------------- /Example/test.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Example/test.h5 -------------------------------------------------------------------------------- /Example/test.info: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pipidog/DeepTimeSeries/2e3f7b81702d2273b55265c514ed8762cba2f1cb/Example/test.info -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | # Welcome to the Deep Time Series Project 6 | **Deep Time Series** is a library to help you quickly build complicated time-series 7 | deep learning models such as RNN2Dense, Seq2Seq, Attention-Based, etc. This library 8 | is based on **Python** and the famous deep learning package **Keras**. All the APIs 9 | are made to as close to Keras as possible. Therefore, if you are familar with Keras, 10 | you should be able to hands-on in seconds. 11 | 12 | **Note:** Time Series data can be various. Any series data that can be vectorized 13 | can be considered as inputs to this library. Therefore, multi-variables time series 14 | data, time-dependent images, speeches, text translation, etc., should be all compatible 15 | to this library. 16 | 17 | # Usage 18 | ## Prepare Your Data in Time-Series Format 19 | **Deep Time Series** has built-in functions that helps you convert your standard 20 | time series dataframe to supervised learning format. For example, we have the following 21 | time series dataframe in Pandas: 22 |

23 | 24 |

25 | Usually, you should have two such dataframe. One is for the input data the other is the 26 | target data (we're doing supervised learning, right?). Then you can simply convert them 27 | to format that are good for supervised learning by calling the function: 28 |

29 | 30 |

31 | 32 | , where the **n_memory_step** is the number of previous time steps you want to use for 33 | each time step and **n_forcast_step** is the number of future time steps you want to 34 | forcast. **split = 0.8** will split the first 80% time steps as train set and the rest 35 | 20% time steps as test set. 36 | 37 | 38 | ## Build and Train Your Models 39 | 40 | In **Deep Time Series**, all models are wrapped into a single objects. To build a model, 41 | e.g. sequence-to-sequence model, you can just type: 42 |

43 | 44 |

45 | 46 | As you may immediately notice that the commands here are almost identical to Keras. Yes, 47 | this is the purpose of this library, i.e. a time-series model-based library that 48 | helps you build complicated time-series models in just a few lines. 49 | 50 | ## Save and Reload model 51 | 52 | Once your model is trained, you can save and reload the model for future inference. Also, 53 | the syntaxs are almost identical to Keras: 54 | 55 |

56 | 57 |

58 | 59 | [**A complete example can be found here**](https://github.com/pipidog/DeepTimeSeries/blob/master/Example/PowerAnalysis.py) 60 | 61 | # Supported Models 62 | 63 | Currently **Deep Time Series** supports three major frameworks as shown below. Each 64 | framework supports **simple RNN, LSTM, GRU** as their RNN cell. Therefore there are 65 | 9 popular models. Other models such as **teacher-forcing Seq2Seq** and **Attention-based** 66 | will be included soon. 67 |

68 | 69 |

70 | 71 | 72 | 73 | --------------------------------------------------------------------------------