Прогноз точно такой же, как ввод, но с запаздыванием - PullRequest
0 голосов
/ 05 февраля 2019

Я использую LSTM и чувствую, что моя модель имитирует данные тестирования. Predicted + Actual

Я пытался найти решения, почему у меня возникла эта проблема, и лучшее, что я смог найти, это следующее из другого поста: "похоже, у вас есть шаг впередмежду действительным временным рядом и предсказанным временным рядом, это наиболее заметная «ловушка», если вы делаете прогнозирование временного ряда, как это, в котором NN всегда будет имитировать предыдущий ввод временного ряда. "

Тем не менее,Я не уверен, как решить проблему.

Мои данные выглядят так:

print(df_training_processed)
[[60]
 [57]
 [53]
 ...
 [48]
 [48]
 [39]]

#Data scaling
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range = (0, 1)) #scale the data between 0 and 1
df__training_scaled = scaler.fit_transform(df_training_processed)

Это код:

features_set = []
labels = []
for i in range(60, 2101): #the data has 2101 records
    features_set.append(df__training_scaled[i-60:i, 0]) #loop that starts from 61st record and stores all the previous 60 records to the feature_set list"
    labels.append(df__training_scaled[i, 0]) #the 61st record is stored in the labels list

features_set, labels = np.array(features_set), np.array(labels)  #convert to numpy array
#Converting to 3D
features_set = np.reshape(features_set, (features_set.shape[0], features_set.shape[1], 1))

#Define the model
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout

model = Sequential() #the model class. We will add LSTM, Dropout and Dense layers to this model.
#CuDNNLSTM are normal LSTM accelerated via CUDA, so runs faster on a GPU 

#Creating LSTM and Dropout Layers
model.add(LSTM(units=50, return_sequences=True, input_shape=(features_set.shape[1], 1))) #use add() to add a layer
#The first parameter to the LSTM layer is the number of neurons or nodes that we want in the layer. 
#The second parameter is return_sequences, set to true since we will add more LSTM layers to the model:
#it basically returns a sequence-if we were to put a dense layer afterwards we would set it to false.

#When defining the input layer of your LSTM network, the network assumes you have 1 or more samples and requires 
#that you specify the number of time steps and the number of features.

#Ours: The first parameter to the input_shape is the number of time steps while the last parameter is the number of indicators (features)


#Adding a dropout layer to our model (to avoid over-fitting)
model.add(Dropout(0.2))

#Adding three more LSTM and dropout layers to our model
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(units=50))
model.add(Dropout(0.2))

#Creating Dense Layer
"To make our model more robust, we add a dense layer at the end of the model. The number of neurons in the dense layer" \
" will be set to 1 since we want to predict a single value in the output"
model.add(Dense(units = 1)) #how many classes we have - 1 (e.g if we had digits it would be 10)

#Define an optimizer
opt=keras.optimizers.Adam(lr=1e-3, beta_1=0.9, beta_2=0.999, epsilon=None, decay=1e-5)

#Compile the LSTM
#adam optimizer to reduce the loss or to optimize the algorithm
model.compile(optimizer = opt, loss = 'mean_squared_error', metrics=['accuracy']) #mean squared error as loss function
...