keras - 如何使用 LSTM 预测与训练范围不同的值

标签 keras lstm

我尝试训练一个简单的 LSTM 来预测序列中的下一个数字 (1,2,3,4,5 --> 6)。

from keras.models import Sequential
from keras.layers import LSTM, Dense
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

xs = [[[(j+i)/100] for j in range(5)] for i in range(100)]
ys = [(i+5)/100 for i in range(100)]

x_train, x_test, y_train, y_test = train_test_split(xs, ys)

model = Sequential()
model.add(LSTM(1, input_shape=(5,1), return_sequences=True))
model.add(LSTM(1, return_sequences=False))
model.add(Dense(1))

model.compile(loss='mae', optimizer='adam', metrics=['accuracy'])

training = model.fit(x_train, y_train, epochs=200)

new_xs = np.array(xs)*5
new_ys = np.array(ys)*5
pred = model.predict(new_xs)
plt.scatter(range(len(pred)), pred, c='r')
plt.scatter(range(len(new_ys)), new_ys, c='b')

为了让网络学习任何东西,我必须规范化训练数据(除以 100)。它确实适用于训练范围内的数据。

我希望它能够预测超出其训练范围的数字,但一旦离开该范围,它就会开始发散:

enter image description here

当我将两个 LSTM 层中的单元数增加到 30 时,它看起来稍微好一点,但仍然存在差异:

enter image description here

LSTM 是否能够在不添加无限数量的单元的情况下学习该任务?

最佳答案

我在寻找同一个问题的答案,然后遇到了 the following paper from 2019corresponding Git repo .特别是,请参阅本文的第 5.3 节。 ABBA-LSTM 似乎是解决方案,但它取决于您要解决的时间序列问题。

关于keras - 如何使用 LSTM 预测与训练范围不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49374709/

相关文章:

python - 是否可以在 keras LSTM 的标签集中屏蔽 NaN 值?

python - 了解用于 NLP 文本分类的 LSTM 和 RNN 中的词嵌入、卷积层和最大池化层

machine-learning - 仅在 Keras 中对某些输入进行 BatchNormalization

python - Keras LSTM 在线学习

python - 如何在 Keras 中通过 (2000,7,7,512) 形状的张量喂养 LSTM 网络?

python - 属性错误: module 'keras.backend' has no attribute '_BACKEND'

python - 无法在 Jupyter 中导入 Keras

python - 对于 Keras LSTM,传递滞后特征与特征时间步长有什么区别?

machine-learning - Keras model.save() 和 model.save_weights() 之间的区别?

python - R 的 Keras 和 Python 的 Keras 之间的差异——准确性错误?