简体   繁体   English

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

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

The inputs here are the 3. The output here (LSTM) is the probabilities that the next x1 input ought to be. 这里的输入是3。这里的输出(LSTM)是下一个x1输入应该是的概率。 Means here I have x1x2 and x3 input values. 意味着这里我有x1x2和x3输入值。 1st three inputs LSTM output1 and then next if x1 value = 0 then Lstm output1 is back into as input and predict next output2. 首先输入三个LSTM output1,然后如果x1值= 0,则返回Lstm output1作为输入并预测下一个output2。 If this output 2 value is equal to next x1 value then back into as input and predict output 3. If not equal not to take output 2 as x1 input and take it as mentioned x1 input. 如果此输出2的值等于下一个x1的值,则返回至输入并预测输出3。如果不相等,则不将输出2用作x1的输入,并将其作为x1的输入。 The output (Yt) at timestep t depends on the input X1t and on the previous output Yt-1. 时间步t的输出(Yt)取决于输入X1t和先前的输出Yt-1。 as a example 举个例子

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

here second value of x1 column is 0, x1 value = 0 then take the value as output 1 这里x1列的第二个值是0,x1值= 0,然后将该值作为输出1

x1 = output1

here x1 column 3rd value measured and output 2 value is not equal to x1 3rd value. 这里x1列的第3个值被测量,输出2值不等于x1第3个值。 then take input as measured value. 然后将输入作为测量值。 So this is the method that I want to do. 这就是我想要的方法。 But I don't know how to write it. 但是我不知道怎么写。 Can anyone helps me to do it? 谁能帮助我做到这一点? my LSTM code: 我的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])

But this is not working. 但这是行不通的。 not having relationship inbetween input and output. 输入和输出之间没有关系。

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

After change the code: 更改代码后:

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

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

Another example: here I have three inputs data x1,x2,x3 data with time series. 另一个例子:这里我有三个输入数据x1,x2,x3具有时间序列的数据。 I want to predict value in every one hour. 我想每1小时预测一次价值。 (at t+1) I have ay column that I got value in every one hour(t+1). (在t + 1时)我有一个y列,每隔一小时(t + 1)就会获得价值。 But sometime i measured value after two hours. 但是有时候我两个小时后才测得值。 So in between past time and now time there is a value not measured at t+1. 因此,在过去的时间和现在的时间之间,存在一个未在t + 1处测量的值。 So I will predict value that I didn't measured at t+1. 因此,我将预测在t + 1时未测量的值。 Here x1 value is depending on the output value(y) at t+1 . 这里的x1值取决于t + 1处的输出值(y)。 When I predict the value at that time period take it as y1(t+1), that value should have to read it as x1 value as x1(t) to predict the next output value at t+1 (y2). 当我预测该时间段的值作为y1(t + 1)时,该值应将其读取为x1值x1(t)才能预测下一个在t + 1(y2)处的输出值。 If I measured that value at t+1 ,if my prediction value == measured value then read it that value as x1(t) to predict the next value at t+1. 如果我在t + 1处测量了该值,则如果我的预测值==测量值,则将该值读取为x1(t)以预测在t + 1处的下一个值。 This is the process that I want to write it as a code. 这是我想将其编写为代码的过程。 here example in : 这里的例子:

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

when I predict it using LSTM, if that value is == measured value(t+1) read it as a second input value of x1(t) column. 当我使用LSTM预测时,如果该值为==测量值(t + 1),则将其读取为x1(t)列的第二个输入值。 if not equal read the measured value as second input value of x1(t). 如果不相等,则将测量值读取为x1(t)的第二输入值。 SO 所以

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

Then I predict the value at t+1 =y2 , assume it came 70 then that y2(t+1) value will be the third input of x1 column as x1(t). 然后,我预测t + 1 = y2处的值,假设它是70,则y2(t + 1)值将作为x1(t)作为x1列的第三个输入。 So 所以

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

this is the process that I want to run it using LSTM. 这是我要使用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