How can I predict, forecast the value of the next day using Keras' LSTM?

此生再无相见时 提交于 2019-12-04 06:22:08

问题


I seek your advice to forecast the values after that using LSTM in Keras. I have x_train 62796 and x_test 15684 and I want to predict the values after that. Twenty data collections correspond to a day So, I set the look_back to 20. and Here is my code :

...
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)

So, predictions = model.predict(x_train) and shape is (62796, 20, 1)

I tried this code

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

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

In this code future result is :

but p = model.predict(x_test)'s [:4000] result is :

I want to know how to predict the exact next value. But, The difference between the two results is very large. I don't know where it went wrong or the code went wrong. Here is full source.

Thanks for reading.

来源:https://stackoverflow.com/questions/56414109/how-can-i-predict-forecast-the-value-of-the-next-day-using-keras-lstm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!