简体   繁体   中英

Wrong output LSTM Keras

I want to train an LSTM model to predict two variables from two input variables.

X and Y will contain the same data. Here an example of X:

array([[41.39084204,  2.16312765],
       [41.39063094,  2.16710319],
       [41.39048993,  2.16705291],
       ...,
       [41.3937295 ,  2.16270432],
       [41.39130639,  2.16328958],
       [41.39079175,  2.16311477]])

I convert it to three dimensions [x.reshape(x.shape[0], x.shape[1], 1)]:

array([[[41.39084204],
        [ 2.16312765]],

       [[41.39063094],
        [ 2.16710319]],

       [[41.39048993],
        [ 2.16705291]],

       ...,

       [[41.3937295 ],
        [ 2.16270432]],

Then, I set up the layers of input in_dim (2, 1) [(x.shape[1], x.shape[2])] and output out_dim 2 [y.shape[1]]. After configuring the model

model = Sequential()
model.add(LSTM(64, input_shape=in_dim, activation="relu"))
model.add(Dense(out_dim))
model.compile(loss="mse", optimizer="adam") 

And fiting it:

model.fit(xtrain, ytrain, epochs=100, batch_size=12, verbose=0)

The content of xtest is:

array([[41.39059914],
       [ 2.16686587]])

with shape (2,1)

I get this prediction from a single sample :

ypred = model.predict(xtest)
ypred

WARNING:tensorflow:Model was constructed with shape (None, 2, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 2, 1), dtype=tf.float32, name='lstm_16_input'), name='lstm_16_input', description="created by layer 'lstm_16_input'"), but it was called on an input with incompatible shape (None, 1, 1).
array([[0.56766266, 2.1052783 ],
       [0.05906536, 0.03917462]], dtype=float32)

However, the output doesn't look like the imput. Any idea?

The warning shows you that you constructed your model with shape (None, 2, 1), but then called it on an input with shape (None, 1, 1).

I'd suggest you print out xtest and check it's shape. If it's not in line with what you expect then fix it, otherwise also check the shape of your other elements.

I'm not sure about this last part, but I also think you shouldn't insert xtest as a whole inside of model.predict(). Instead you should feed in an element of xtest. For this you might need to use tf.expand_dims(xtest[input_for_prediction_index], axis=0)

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