我尝试训练一个简单的 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)。它确实适用于训练范围内的数据。
我希望它能够预测超出其训练范围的数字,但一旦离开该范围,它就会开始发散:
当我将两个 LSTM 层中的单元数增加到 30 时,它看起来稍微好一点,但仍然存在差异:
LSTM 是否能够在不添加无限数量的单元的情况下学习该任务?
最佳答案
我在寻找同一个问题的答案,然后遇到了 the following paper from 2019和 corresponding Git repo .特别是,请参阅本文的第 5.3 节。 ABBA-LSTM 似乎是解决方案,但它取决于您要解决的时间序列问题。
关于keras - 如何使用 LSTM 预测与训练范围不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49374709/