我对时间序列预测的预测是一条线

时间:2021-06-09 08:05:51

标签: python tensorflow keras lstm forecasting

我是神经网络的新手,我正在尝试构建 LSTM 模型来预测未来值。我的问题是预测结果图返回一条与测试数据相比的线。

在问这个问题之前我已经搜索了解决方案,而这对我的帮助是this one。 “OverLordGodDragon”给出的答案也有指向另一个问题的链接,他给出的答案解释了如何提供 LTSM 模型。记住这一点非常有趣和有用。但即便如此,我还是输了。

我的模型给出的情节不是一条直线,但即使行为是错误的,我的结果: enter image description here

要记住的一件事是,我正在使用一小部分数据,因为它的数量很大,大约 5 磨,为了防止内核死亡,我使用了大约 5 吨。 我从 CSV 加载的数据,并使用“frac”参数通过 pandas 的 sample() 函数获取它。现在我将展示我的代码:

在 -1 和 1 之间缩放和归一化:

# Feature Scaling Normalization
scaler = preprocessing.MinMaxScaler()
# min-max normalization and scale the features in the 0-1 range.
ifv_values = df['IFVα'].values.reshape(-1, 1)
# The scaler expects the data to be shaped as (x, y)
scaled_ifv = scaler.fit_transform(ifv_values)
# removing NaNs (if any)
scaled_ifv = scaled_ifv[~np.isnan(scaled_ifv)]
# reshaping data after removing NaNs
scaled_ifv = scaled_ifv.reshape(-1, 1)

在可训练数据和测试中拆分数据:

SEQ_LEN = 100
def to_sequences(data, seq_len):
    d = []
    for index in range(len(data) - seq_len):
       d.append(data[index: index + seq_len])
return np.array(d)
def preprocess(data_raw, seq_len, train_split):
   data = to_sequences(data_raw, seq_len)
   num_train = int(train_split * data.shape[0])
   X_train = data[:num_train, :-1, :]
   y_train = data[:num_train, -1, :]
   X_test = data[num_train:, :-1, :]
   y_test = data[num_train:, -1, :]
   return X_train, y_train, X_test, y_test
# 20% of the data saved for testing.
X_train, y_train, X_test, y_test = preprocess(scaled_ifv, SEQ_LEN, train_split = 0.80)

print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
>>(4337, 99, 1) (4337, 1) (1085, 99, 1) (1085, 1)

我的模型结构:

DROPOUT = 0.2
UNITS = SEQ_LEN - 1
model = Sequential()

# Input layer
model.add(LSTM(UNITS, activation='tanh',input_shape=(UNITS, 
X_train.shape[-1]),return_sequences=True))
model.add(Dropout(rate=DROPOUT))
# 1st Hidden layer
model.add(LSTM(UNITS, return_sequences = True))
model.add(Dropout(rate=DROPOUT))
# 2nd Hidden layer
model.add(LSTM(UNITS, return_sequences=False))
# output layer
model.add(Dense(units=1))
model.add(Activation('linear'))

以及编译和训练代码,我使用 MSE 作为损失函数和 Adam 优化器:

BATCH_SIZE = 64
lstm_model.compile(loss='mean_squared_error', optimizer='adam')
my_callbacks =[
   save_checkpoint_foreach_epoch('history_lstm_model'),
   #save_model_folder ('model_lstm'),
]
history_lstm = lstm_model.fit(X_train, y_train,
               batch_size= BATCH_SIZE,
               epochs=50,
               shuffle=False,
               callbacks= my_callbacks,
               validation_split=0.1,
               verbose=2)

感谢您的帮助。感谢您的时间。

0 个答案:

没有答案