预测与输入完全相同,但有滞后

时间:2019-02-05 04:47:47

标签: machine-learning lstm data-science lag

我正在使用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

0 个答案:

没有答案