首页 > 解决方案 > 使用 LSTM 进行预测

问题描述

我想使用 LSTM 预测价格,但时间段结束时的模型失去了轨道,无法跟随趋势。我不知道问题可能是什么,有人有解决方案吗?

我的代码的不同部分如下所示:


X_train = []
y_train = []

n_future = 8   # Number of hours we want top predict into the future
n_past = 144    # Number of past hours we want to use to predict the future

for i in range(n_past, len(training_set_scaled) - n_future + 1):
    X_train.append(training_set_scaled[i - n_past:i, 0:dataset_train.shape[1] -1 ])
    y_train.append(training_set_scaled[i + n_future -1 :i + n_future, 0])

X_train, y_train = np.array(X_train), np.array(y_train)

print('X_train shape == {}.'.format(X_train.shape))
print('y_train shape == {}.'.format(y_train.shape))

from tensorflow import keras
model = keras.Sequential()


model.add(keras.layers.Bidirectional(keras.layers.LSTM(units=64, return_sequences=True, input_shape=(n_past, dataset_train.shape[1]-1))))
model.add(keras.layers.Dropout(rate=0.2))

model.add(keras.layers.Bidirectional(keras.layers.LSTM(units=32, return_sequences=False)))
[enter image description here][1]
model.add(keras.layers.Dense(units=1, activation = 'linear'))

model.compile(loss='mean_squared_error', optimizer='adam')

%%time
es = EarlyStopping(monitor='val_loss', min_delta=1e-10, patience=2, verbose=1)
rlr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=2, verbose=1)
mcp = ModelCheckpoint(filepath='weights.h5', monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=True)

tb = TensorBoard('logs')

validation_split=0.2, verbose=1, batch_size=256)
history = model.fit(X_train, y_train, shuffle=True, epochs=3, callbacks=[es, rlr, mcp, tb], validation_split=0.2, verbose=1, batch_size=1000)

标签: pythontensorflowmachine-learninglstm

解决方案


推荐阅读