问题
Here I am tried to predict next future value of x with three inputs. So here I used LSTM model to predict future value. Here is my code:
num_time_step=2
from keras.layers import Masking
from keras.layers import Activation
from keras.layers import LeakyReLU
model = Sequential()
model.add(Masking(mask_value=0., input_shape=(num_time_step, x_train.shape[1])))
model.add(LSTM(4,return_sequences=True, input_dim=4))
model.add(LeakyReLU())
model.add(Dropout(0.01))
model.add(LSTM(8,return_sequences=True))
model.add(LeakyReLU())
model.add(Dropout(0.01))
model.add(LSTM(8,return_sequences=True))
model.add(LeakyReLU())
model.add(Dropout(0.01))
model.add(LSTM(8))
model.add(Dense(1, activation='sigmoid'))
batchsize = 2
model.compile(loss='mean_squared_error', optimizer="adam",metrics=['accuracy'])
history = model.fit(x_train_n,y_train_n, batch_size = batchsize, nb_epoch=40)
model.reset_states()
pred=[]
for col in range(len(x_test)-1): #here I tried to say that when you are predicting value in x_test column having value then take it as next input value if not take it as previous pred value
s= x_test[col][:,[0]]
if np.isnan(s).all():
pred[-1]= pred[-1] + [(pred[0]-pred[-1])]
x_test[col][:,[0]]= pred[-1]
else:
x_test[col][:,[0]]
pred=model.predict(x_test)
model.reset_states()
Then I got the graph is not a good prediction value, and my code is not correct also Here is my graph
Here then I tried this code :
future = []
currentStep = pred[:,-1:,:] #last step from the previous prediction
for i in range(1):
currentStep = model.predict(currentStep) #get the next step
future.append(currentStep) #store the future steps
#after processing a sequence, reset the states for safety
model.reset_states()
Then got an error:
IndexError Traceback (most recent call last)
<ipython-input-22-318022d984f9> in <module>()
97 pred=model.predict(x_test_n)
98 future = []
---> 99 currentStep = pred[:,-1:,:] #last step from the previous prediction
100
101 for i in range(1):
IndexError: too many indices for array
Here what I am looking forward:
Here prediction is x value
my csv file for train model :
My csv files for training
After training model my next csv file for test :
new csv file for test
来源:https://stackoverflow.com/questions/59367712/how-to-feed-my-output-prediction-value-as-next-input-value-in-lstm-model-for-fut