如何使用Keras的LSTM预测第二天的价值?

时间:2019-06-02 10:15:11

标签: python machine-learning keras deep-learning lstm

我希望您提出建议,以便在Keras中使用LSTM预测此后的值。 我有x_train 62796和x_test 15684,我想在此之后预测值。 每天有20个数据收集,因此,我将look_back设置为20。这是我的代码:

...
look_back = 20
train_size = int(len(data) * 0.80)
test_size = len(data) - train_size

train = data[0:train_size]
test = data[train_size:len(data)]
x_train, y_train = create_dataset(train, look_back)
x_test, y_test = create_dataset(test, look_back)

x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
y_train=np.repeat(y_train.reshape(-1,1), 20, axis=1).reshape(-1,20,1)
y_test=np.repeat(y_test.reshape(-1,1), 20, axis=1).reshape(-1,20,1)
...
model = Sequential()

model.add(LSTM(512,  return_sequences=True))
model.add(Dropout(0.3))

model.add(LSTM(512,  return_sequences=True))
model.add(Dropout(0.3))

model.add(LSTM(1, return_sequences=True))

model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])
model.summary()
model.fit(x_train, y_train, epochs=10, batch_size=64)
p = model.predict(x_test)

因此,predictions = model.predict(x_train)和形状为(62796, 20, 1)

我尝试了this代码

future = []
currentStep = predictions[-20:, :, :] # -20 is last look_back number

for i in range(10):
    currentStep = model.predict(currentStep)
    future.append(currentStep)

在此代码中,将来的结果是:

1

但是p = model.predict(x_test)的[:4000]结果是:

2

我想知道如何预测下一个确切的值。但是,两个结果之间的差异非常大。我不知道哪里出错或代码出错。这是full的来源。

感谢阅读。

0 个答案:

没有答案