is this correctly work on predict next value in keras?

断了今生、忘了曾经 提交于 2019-12-11 14:23:23

问题


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)

and I want to predict the next value So, predictions = model.predict(x_train) and shape is (62796, 20, 1)

and I coded the following site how to use the Keras model to forecast for future dates or events?

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's result is

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

The difference between the two results is very large.

is this right way to Predict the next value??

I don't know where it went wrong or the code went wrong.

I hope for your opinion.

full source is https://gist.github.com/Lay4U/654f70bd1fb9c4f7d5bdb21ddcb588ab


回答1:


According to your code you are trying to predict next value using lstm. So here you have to reshape your input data correctly to reflect the time steps and features.

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

instead of this code you have to write :

model.add(LSTM(512, input_shape=(look_back,x)))

x = input features in your training data.

I guess this article will help to moderate your code and predict the future value:

enter link description here

This article will help you to understand more about how to predict future value:

enter link description here

Thank you




回答2:


There are multiple methods you can try. There is no one right way at the moment. You can train a seperate model for predicting t+1, t+2 ... t+n. One LSTM model predicts t+1 while another predicts t+n. That is called a DIRMO strategy.

Your strategy (recursive strategy) is particularly risky because the model can propagate the error through multiple time horizons.

You can find a good comparison of alternative strategies in this paper. https://www.sciencedirect.com/science/article/pii/S0957417412000528?via%3Dihub



来源:https://stackoverflow.com/questions/56381754/is-this-correctly-work-on-predict-next-value-in-keras

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