繁体   English   中英

如何在python中使用LSTM将预测值的输出反馈回输入

[英]How to feed output of predict value back into the input using LSTM in python

这里的输入是3。这里的输出(LSTM)是下一个x1输入应该是的概率。 意味着这里我有x1x2和x3输入值。 首先输入三个LSTM output1,然后如果x1值= 0,则返回Lstm output1作为输入并预测下一个output2。 如果此输出2的值等于下一个x1的值,则返回至输入并预测输出3。如果不相等,则不将输出2用作x1的输入,并将其作为x1的输入。 时间步t的输出(Yt)取决于输入X1t和先前的输出Yt-1。 举个例子

x1   x2    x3    predict (output)
100  30    40     120
 0   20    10     130
140  15    30     160

这里x1列的第二个值是0,x1值= 0,然后将该值作为输出1

x1 = output1

这里x1列的第3个值被测量,输出2值不等于x1第3个值。 然后将输入作为测量值。 这就是我想要的方法。 但是我不知道怎么写。 谁能帮助我做到这一点? 我的LSTM代码:

fit1 = Sequential ()
fit1.add(LSTM(32, return_sequences=True, activation='relu',input_shape=(3,1)))
fit1.add((LSTM(32, return_sequences=True)))
fit1.add(LSTM(32))
fit1.add(Dense(1))
batchsize = 3
fit1.compile(loss="mean_squared_error",optimizer="adam")
fit1.fit(x_train , y_train , batch_size = batchsize, nb_epoch =10,shuffle=True)
pred1=fit1.predict(x_test)
for i in range(len(x)):
    pred1.append(fit1.predict([x[i,None,:],pred1[i]]))
pred1 = np.asarray(pred1)
pred1 = pred1.reshape(pred1.shape[0],pred1.shape[2])

但这是行不通的。 输入和输出之间没有关系。

错误: 在此处输入图片说明

更改代码后:

for i in range(len(x)):
pred1 = np.append(pred1, fit1.predict([x[i,None,:],pred1[i]]))
pred1 = np.asarray(pred1)
pred1 = pred1.reshape(pred1.shape[0],pred1.shape[2])

错误: 在此处输入图片说明

另一个例子:这里我有三个输入数据x1,x2,x3具有时间序列的数据。 我想每1小时预测一次价值。 (在t + 1时)我有一个y列,每隔一小时(t + 1)就会获得价值。 但是有时候我两个小时后才测得值。 因此,在过去的时间和现在的时间之间,存在一个未在t + 1处测量的值。 因此,我将预测在t + 1时未测量的值。 这里的x1值取决于t + 1处的输出值(y)。 当我预测该时间段的值作为y1(t + 1)时,该值应将其读取为x1值x1(t)才能预测下一个在t + 1(y2)处的输出值。 如果我在t + 1处测量了该值,则如果我的预测值==测量值,则将该值读取为x1(t)以预测在t + 1处的下一个值。 这是我想将其编写为代码的过程。 这里的例子:

time     x1(t)     x2(t)     x3(t)     y(t+1)
6:00:00  120        0         0         110 (I measured it at t+1)

当我使用LSTM预测时,如果该值为==测量值(t + 1),则将其读取为x1(t)列的第二个输入值。 如果不相等,则将测量值读取为x1(t)的第二输入值。 所以

7:00:00  110       40         10         0  (not measured value at t+1)

然后,我预测t + 1 = y2处的值,假设它是70,则y2(t + 1)值将作为x1(t)作为x1列的第三个输入。 所以

8:00:00  70        0          30         200 (I measured value at t+1)

这是我要使用LSTM运行它的过程。

您应该使用pred1 = np.append(pred1, fit1.predict([x[i,None,:],pred1[i]]))而不是pred1.append(fit1.predict([x[i,None,:],pred1[i]]))pred1.append(fit1.predict([x[i,None,:],pred1[i]]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM