python - 使用 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)
解决方案
推荐阅读
- django - 使用 DRF 缓存 ViewSet:TypeError: _wrapped_view()
- node.js - Node.js - 有没有办法为生产和开发目的设置“apn”模块?
- php - 一个奏鸣曲列表视图上的“访问被拒绝”,但另一个没有
- spring-mvc - 在Tomcat中部署spring应用程序war文件后出现404发现错误
- git - 从工作副本备份所有 gitignored 文件
- api - 如何通过 Sabre API 上的 GetReservationRQ 创建 PNR 后检索机票价格?
- neo4j - Display two separated records according to multiple values in properties neo4j
- javascript - 使用页面重定向 ajax 发布表单数据
- angularjs - 如何使用 AngularJS V1.6 绑定默认值以使用 ng-model 进行选择
- php - localhost 重定向到 wordpress 中的实时 url