我尝试训练一个简单的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是否能够在不添加无限数量单位的情况下学习该任务?