Skip to content

Commit

Permalink
lstm_residual
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoabreu1002 committed Apr 11, 2021
1 parent a1db067 commit 62c5b85
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
19 changes: 12 additions & 7 deletions ACOLSTM.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras import backend as K
from mlopt.ACO import ACO
from sklearn.metrics import mean_absolute_error as MAE
from numpy.random import seed
from tensorflow.random import set_seed
import tensorflow as tf
import warnings
import os
import warnings
warnings.filterwarnings("ignore")
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

seed(1)
Expand Down Expand Up @@ -51,23 +54,25 @@ def setACO(self):
return ACOsearch

def setModel(self, parameters):
model = None
K.clear_session()
model = Sequential()
if self._verbose:
print(parameters)

model.add(LSTM(units=parameters['fl_qtn'], activation=parameters['fl_func'],
recurrent_activation=parameters['fl_func'],
return_sequences=True, input_shape=(self._X_train.shape[1], self._n_variables)))
recurrent_activation=parameters['fl_func'],
return_sequences=True, input_shape=(self._X_train.shape[1], self._n_variables)))

model.add(LSTM(units=parameters['sl_qtn'], activation=parameters['sl_func'],
recurrent_activation=parameters['fl_func']))
recurrent_activation=parameters['fl_func']))

model.add(Dense(units=parameters['tl_qtn'], activation=parameters['tl_func']))

model.add(Dense(self._y_train.shape[1]))

model.compile(optimizer=parameters['optimizer'], loss='mae', metrics=['mse'])

return model

def fitModel(self, X):
Expand All @@ -76,11 +81,11 @@ def fitModel(self, X):
'tl_qtn':X[4],'tl_func':self._activations[X[5]],
'optimizer':self._optimizers[X[6]]}

model = self.setModel(search_parameters)
model.fit(self._X_train, self._y_train, epochs=self._epochs[X[7]], verbose=0, shuffle=False,
setedModel = self.setModel(search_parameters)
setedModel.fit(self._X_train, self._y_train, epochs=self._epochs[X[7]], verbose=0, shuffle=False,
use_multiprocessing=True)

return model
return setedModel

def optimize(self, searchSpace, activations=['elu', 'selu', 'tanh', 'relu', 'linear', 'sigmoid'],
optimizers=['SGD', 'adam', 'rmsprop','Adagrad'],
Expand Down
2 changes: 1 addition & 1 deletion AGMLP_Residual.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def gen_population(self):
"""
population = [[1,1,1,1,'objeto_erro','objeto_ass',np.inf]]*self._size_pop
for i in range(0, self._size_pop):
population[i] = [random.randint(1, 20), random.randint(1, 20), random.randint(1, 20), random.randint(1, 20), 'objeto_erro', 'objeto_ass', 10]
population[i] = [random.randint(1, 30), random.randint(1, 30), random.randint(1, 30), random.randint(1, 30), 'objeto_erro', 'objeto_ass', 10]

return population

Expand Down
90 changes: 90 additions & 0 deletions LSTM_Residual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from sklearn.metrics import mean_absolute_error as mae
import numpy as np
import random
from tqdm import tqdm
from mlopt.ACOLSTM import ACOLSTM

class LSTM_Residual:
""" A Residual correction aproach for time series"""
def __init__(self, data, y_arima, tr_ts_percents=[80,20]):
"""
data - original data
y_arima - forecasted data
num_epochs - number of epochs
size_pop - size of population
prob_mut - probability of mutation
tr_ts_percents - list of train and test percentages. E.G: [80,20]
alpha_stop - early stop criteria.
"""
self._data = data
self._data_train = data[:int(tr_ts_percents[0]/100*len(data))]
self._data_test = data[int(tr_ts_percents[0]/100*len(data)):]
self._y_arima = y_arima
self._erro = data-y_arima
self._data_train_arima = y_arima[:int(tr_ts_percents[0]/100*len(y_arima))]
self._data_test_arima = y_arima[int(tr_ts_percents[0]/100*len(y_arima)):]
self._tr_ts_percents = tr_ts_percents
self._best_of_all = None

def train_test_split(self, serie, num_lags, print_shapes = False):
"""
Slipts a time series to train and test Data.
X data are data num_lags behind y data.
"""
len_serie = len(serie)
X = np.zeros((len_serie, num_lags))
y = np.zeros((len_serie,1))
for i in np.arange(0, len_serie):
if i-num_lags>0:
X[i,:] = serie[i-num_lags:i]
y[i] = serie[i]

len_train = np.floor(len_serie*self._tr_ts_percents[0]/100).astype('int')
len_test = np.ceil(len_serie*self._tr_ts_percents[1]/100).astype('int')

X_train = X[0:len_train]
y_train = y[0:len_train]
X_test = X[len_train:len_train+len_test]
y_test = y[len_train:len_train+len_test]

return X_train, y_train, X_test, y_test

def gen_population(self):
"""
Generates the population.
The population is a list of lists where every element in the inner list corresponds to:
[lag_residue_regression, lag_original_sarimax_association, lag_estimated_residue, forecast_estimated_residue
, 'object_resiue_regression', 'object_association', fitness]
The lags and forecast variables are token from a uniform distribution from 1 to 20.
"""
population = [[1,1,1,1,'objeto_erro','objeto_ass',np.inf]]*self._size_pop
for i in range(0, self._size_pop):
population[i] = [random.randint(1, 20), random.randint(1, 20), random.randint(1, 20), random.randint(1, 20), 'objeto_erro', 'objeto_ass', 10]

return population

def fit(self, lag_error, searchSpace, options_ACO):

erro_train_entrada, erro_train_saida, erro_test_entrada, erro_test_saida = self.train_test_split(
self._erro, lag_error)

#LSTM_erro
lstmOptimizer = ACOLSTM(erro_train_entrada, erro_train_saida, erro_test_entrada, erro_test_saida, 1,
options_ACO=options_ACO)

final_model, error_hat_test = lstmOptimizer.optimize(searchSpace)

print("shape data test arima: ")
print(self._data_test_arima.shape)
print("shape error_hat_test")
print(error_hat_test.shape)

y_hat_test = self._data_test_arima[:] + error_hat_test[:,0]
print("shape y_hat_test")
print(y_hat_test.shape)

fitness = mae(y_hat_test, self._data_test)
print("Final ftiness MAE {0}".format((fitness)))

return final_model, y_hat_test
1 change: 0 additions & 1 deletion TimeSeriesUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import warnings
import itertools
import copy
from pathlib import Path
import logging
from logging.handlers import RotatingFileHandler

Expand Down

0 comments on commit 62c5b85

Please sign in to comment.