1
$\begingroup$

I have a time series with 5 variables, and I want to predict the behavior of these 5 variables 112 periods ahead. For this, I use a dataset with information on the 21504 periods before (18278 periods for training and the remaining for validation). I am using LSTM to make this prediction. However, the model is predicting equal values for periods ahead. Can anyone help me find out what's going on?

I tried to change the number of neurons, the number of hidden layers, the learning rate, and nothing worked.

The code:

from pandas import read_csv
from math import sqrt
from numpy import concatenate
from pandas import DataFrame
from pandas import concat
from tensorflow import keras
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
import random
import numpy as np
import tensorflow as tf
import matplotlib
import matplotlib.pyplot as plt
from numpy import array
from numpy import hstack
from keras.layers import RepeatVector
from keras.layers import TimeDistributed
from keras.layers import Dropout
import sys
np.set_printoptions(threshold=sys.maxsize)

SEED = 123
random.seed(SEED)
np.random.seed(SEED)
tf.random.set_seed(SEED)

dataset = read_csv('c:/Users/ferna/Drive/Tese_Algoritmos/Artigo_3_Notebook/Novo_Teste/Framework/DATASET_MACHINE_1_FEATURES_MODIFIED_21504.csv', header = 0)

del dataset['Period']

def NormalizeData(data):
    return (data - np.min(data)) / (np.max(data) - np.min(data))

dataset.Pressure = NormalizeData(dataset['Pressure'].values)
dataset.Speed = NormalizeData(dataset['Speed'].values)
dataset.Temperature = NormalizeData(dataset['Temperature'].values)
dataset.Sound = NormalizeData(dataset['Sound'].values)
dataset.Vibration = NormalizeData(dataset['Vibration'].values)

train, test = dataset[0:18278], dataset[18278:]

# split a multivariate sequence into samples
def split_sequences(sequences, n_steps_in, n_steps_out):
    X, y = list(), list()
    for i in range(len(sequences)):
    # find the end of this pattern
        end_ix = i + n_steps_in
        out_end_ix = end_ix + n_steps_out
        # check if we are beyond the dataset
        if out_end_ix > len(sequences):
            break
        # gather input and output parts of the pattern
        seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix:out_end_ix, :]
        X.append(seq_x)
        y.append(seq_y)
    return array(X), array(y)

# Training set
# define input sequence
in_seq1 = array(train['Pressure'].values)
in_seq2 = array(train['Speed'].values)
in_seq3 = array(train['Temperature'].values)
in_seq4 = array(train['Sound'].values)
in_seq5 = array(train['Vibration'].values)


# convert to [rows, columns] structure
in_seq1 = in_seq1.reshape((len(in_seq1), 1))
in_seq2 = in_seq2.reshape((len(in_seq2), 1))
in_seq3 = in_seq3.reshape((len(in_seq3), 1))
in_seq4 = in_seq4.reshape((len(in_seq4), 1))
in_seq5 = in_seq5.reshape((len(in_seq5), 1))

# horizontally stack columns
dataset = hstack((in_seq1, in_seq2, in_seq3, in_seq4, in_seq5))

# choose a number of time steps
n_steps_in, n_steps_out = 224, 112

# covert into input/output
X, y = split_sequences(dataset, n_steps_in, n_steps_out)
print(X.shape, y.shape)

n_features = X.shape[2]

# Test set
# define input sequence
in_seq1_test = array(test['Pressure'].values)
in_seq2_test = array(test['Speed'].values)
in_seq3_test = array(test['Temperature'].values)
in_seq4_test = array(test['Sound'].values)
in_seq5_test = array(test['Vibration'].values)


# convert to [rows, columns] structure
in_seq1_test = in_seq1_test.reshape((len(in_seq1_test), 1))
in_seq2_test = in_seq2_test.reshape((len(in_seq2_test), 1))
in_seq3_test = in_seq3_test.reshape((len(in_seq3_test), 1))
in_seq4_test = in_seq4_test.reshape((len(in_seq4_test), 1))
in_seq5_test = in_seq5_test.reshape((len(in_seq5_test), 1))

# horizontally stack columns
dataset_test = hstack((in_seq1_test, in_seq2_test, in_seq3_test, in_seq4_test, in_seq5_test))

# covert into input/output
test_X, test_y = split_sequences(dataset_test, n_steps_in, n_steps_out)

# define model (Encoder-Decoder model)
model = Sequential()
model.add(LSTM(64, dropout=0.5, recurrent_dropout=0.5, activation = 'relu', input_shape=(n_steps_in,n_features)))
model.add(RepeatVector(n_steps_out))
model.add(LSTM(64, dropout=0.5, recurrent_dropout=0.5, activation = 'relu', return_sequences = True))
model.add(TimeDistributed(Dense(64, activation = 'relu')))
model.add(TimeDistributed(Dense(n_features)))


model.compile(optimizer = keras.optimizers.SGD(learning_rate=0.001), loss = 'mse')


# fit model
history_fit = model.fit(X, y, epochs = 1, steps_per_epoch = 4000, batch_size = 1, verbose = 1, validation_data = (test_X, test_y), shuffle = True)

test = array(test)

x_input_pred = list()
for i in range(len(test) - n_steps_in,len(test)):
    x_input_pred.append(test[i])

x_input_pred = array(x_input_pred)
x_input_pred = x_input_pred.reshape((1, n_steps_in, n_features))

predictions = model.predict(x_input_pred, batch_size = 1)

pred = predictions.reshape((n_steps_out, n_features))

real_data = read_csv('c:/Users/ferna/Drive/Tese_Algoritmos/Artigo_3_Notebook/Novo_Teste/Framework/DATASET_MACHINE_1_FEATURES_MODIFIED_21504.csv', header = 0)

def RealData(data, initialdata):
    return data * (np.max(initialdata) - np.min(initialdata)) + np.min(initialdata)

predictions_Pressure = RealData(pred[:,0], real_data['Pressure'].values)
predictions_Speed = RealData(pred[:,1], real_data['Speed'].values)
predictions_Temp = RealData(pred[:,2], real_data['Temperature'].values)
predictions_Sound = RealData(pred[:,3], real_data['Sound'].values)
predictions_Vibration = RealData(pred[:,4], real_data['Vibration'].values)

The output:

[102.00408  102.072044 102.10676  102.12242  102.12631  102.12158 
 102.11417  102.10575  102.097565 102.08955  102.08235  102.07618 
 102.071075 102.066895 102.06351  102.060776 102.05859  102.056755
 102.05517  102.0539   102.0529   102.05212  102.0515   102.05101 
 102.05064  102.05035  102.05012  102.04994  102.0498   102.04969 
 102.04961  102.04954  102.049484 102.04944  102.04941  102.04938 
 102.04936  102.04935  102.04933  102.049324 102.04932  102.04931 
 102.0493   102.04929  102.04929  102.04929  102.049286 102.049286
 102.049286 102.049286 102.049286 102.049286 102.049286 102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928  102.04928  102.04928 
 102.04928  102.04928  102.04928  102.04928 ]
[92.15837  92.790245 93.25468  93.600006 93.85911  94.05541  94.20265
 94.31096  94.39296  94.45493  94.502    94.53786  94.56531  94.58636
 94.602516 94.61493  94.62446  94.63164  94.63693  94.64099  94.64409
 94.64646  94.64827  94.64965  94.65061  94.65133  94.65187  94.65228
 94.65259  94.652824 94.653    94.65313  94.65323  94.6533   94.65335
 94.65339  94.65342  94.65344  94.65346  94.65347  94.65348  94.65348
 94.65349  94.65349  94.65349  94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496
 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496 94.653496]
[81.04522  81.1118   81.168365 81.21548  81.2543   81.28658  81.31225
 81.331406 81.34647  81.358475 81.3679   81.37523  81.38094  81.385376
 81.38881  81.391464 81.39352  81.39511  81.39636  81.39733  81.39807
 81.39864  81.399086 81.39943  81.39969  81.399895 81.40005  81.40017
 81.40026  81.40034  81.40039  81.40044  81.40047  81.4005   81.40051
 81.40053  81.40054  81.40055  81.40056  81.400566 81.40057  81.40057
 81.40058  81.40058  81.40058  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059
 81.40059  81.40059  81.40059  81.40059  81.40059  81.40059  81.40059 ]
[67.74666  67.95573  68.12794  68.25902  68.351204 68.40985  68.4513
 68.469284 68.48202  68.48964  68.4945   68.49778  68.49968  68.500626
 68.50097  68.50094  68.5007   68.50069  68.50098  68.50115  68.50125
 68.501305 68.50133  68.50134  68.50119  68.50105  68.50094  68.500854
 68.50079  68.50074  68.50071  68.50068  68.500656 68.50064  68.50063
 68.500626 68.500626 68.50062  68.50062  68.50062  68.50062  68.50062
 68.50062  68.50062  68.50062  68.50062  68.50062  68.50062  68.50062
 68.50062  68.50062  68.50062  68.50062  68.50062  68.50062  68.50062
 68.50062  68.500626 68.500626 68.500626 68.500626 68.500626 68.500626
 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626
 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626
 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626
 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626
 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626
 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626
 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626 68.500626]
[125.25616  125.84559  126.319725 126.68867  126.96395  127.16097
 127.31009  127.42966  127.51958  127.58657  127.636696 127.67432
 127.7025   127.72361  127.739426 127.75129  127.76019  127.76732
 127.77327  127.777794 127.78125  127.783875 127.78587  127.7874
 127.7887   127.789696 127.79046  127.791046 127.7915   127.79183
 127.7921   127.7923   127.79245  127.792564 127.792656 127.792725
 127.79278  127.792816 127.79285  127.79287  127.792885 127.7929
 127.792915 127.79292  127.79292  127.79293  127.79294  127.79294
 127.79294  127.792946 127.792946 127.792946 127.792946 127.792946
 127.792946 127.792946 127.792946 127.792946 127.792946 127.792946
 127.792946 127.79295  127.79295  127.79295  127.79295  127.79295
 127.79295  127.792946 127.792946 127.79295  127.79295  127.79295
 127.79295  127.79295  127.79295  127.79295  127.79295  127.79295
 127.79295  127.79295  127.79295  127.79295  127.79295  127.79295
 127.79295  127.79295  127.79295  127.79295  127.79295  127.79295
 127.79295  127.79295  127.79295  127.79295  127.79295  127.79295
 127.79295  127.79295  127.79295  127.79295  127.79295  127.79295
 127.79295  127.79295  127.79295  127.79295  127.79295  127.79295
 127.79295  127.79295  127.79295  127.79295 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.