如何将我的输出预测值作为LSTM模型中的下一个输入值提供给将来的预测模型

时间:2019-12-17 04:41:17

标签: python tensorflow lstm

在这里,我尝试通过三个输入来预测x的下一个未来值。因此,在这里我使用LSTM模型来预测未来价值。 这是我的代码:

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

然后我得到的图形不是很好的预测值,我的代码也不正确 这是我的图

enter image description here

然后我在这里尝试了此代码:

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

然后出现错误:

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

这就是我的期待:

enter image description here

这里的预测是x值

我的火车模型的csv文件:

My csv files for training

训练模型后,我的下一个csv文件进行测试:

new csv file for test

0 个答案:

没有答案