from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# 定义数据
X = array([
[10, 20, 30],
[20, 30, 40],
[30, 40, 50],
[40, 50, 60]])
y = array([40, 50, 60, 70])
X = X.reshape(X.shape[0], X.shape[1], 1)
# 定义模型
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(3, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# 训练模型
model.fit(X, y, epochs=2000, verbose=1)
print('学习完成')
# 开始预测
x_input = array([60, 70, 80])
x_input = x_input.reshape((1, 3, 1))
yhat = model.predict(x_input, verbose=0)
print('输入:')
print([60, 70, 80])
print('预测下一个出现的数字:')
print(yhat[0][0])