简体   繁体   中英

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. Means here I have x1x2 and x3 input values. 1st three inputs LSTM output1 and then next if x1 value = 0 then Lstm output1 is back into as input and predict next 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. The output (Yt) at timestep t depends on the input X1t and on the previous output 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 = output1

here x1 column 3rd value measured and output 2 value is not equal to x1 3rd value. 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:

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. I want to predict value in every one hour. (at t+1) I have ay column that I got value in every one hour(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. So I will predict value that I didn't measured at t+1. Here x1 value is depending on the output value(y) at t+1 . 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). 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. 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. if not equal read the measured value as second input value of 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). 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.

您应该使用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]]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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