简体   繁体   中英

Keras lstm multi output model predict two features (time series)

I need to ask how to use keras predict from keras functional api. I need to write multivariate LSTM model with multioutput. I have written such model:

inp = Input((train_X.shape[1],train_X.shape[2]))
x = LSTM(192,return_sequences=True)(inp)
x = Dropout(0.5)(x)
x = Flatten()(x)
out1 = Dense(1,activation='softsign')(x)
out2 = Dense(1,activation='softsign')(x)
model = Model(inputs =inp,outputs= (out1,out2))
model.compile(optimizer='rmsprop',
          loss='binary_crossentropy',
          metrics=['accuracy'])

history = model.fit(train_X,[y1,y2])
ypred = model.predict([pred_X,pred_Y])

What I want (expect) is ypred returning two time series,but it gives me error:

ValueError: Error when checking model : the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays: 

It is expecting one array as predict argument, but with only one array it will return one predicted time series.

What I need to correct to get prediction of two feautures?

My data have 6 columns,first two I'm trying to predict,others are features. Dimensions of my data are:

train_X.shape= (24576, 192, 6)
pred_X.shape= (672, 192, 6)
pred_Y.shape= (672, 192, 6)
y1.shape = (24576,1)
y2.shape = (24576,1)

You are confusing number of inputs with number of outputs. Let's look at this line:

ypred = model.predict(pred_X)
# equally
out1, out2 = model.predict(pred_X)

now ypred will be a list of outputs, namely 2. So predict will return both outputs for the same input because that is precisely how you defined your model, 1 input -> 2 outputs. That is what the error is warning. To fix, you'll need to give predict 1 input and get a list of outputs.

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