简体   繁体   English

错误的输出 LSTM Keras

[英]Wrong output LSTM Keras

I want to train an LSTM model to predict two variables from two input variables.我想训练一个 LSTM 模型从两个输入变量中预测两个变量。

X and Y will contain the same data. X 和 Y 将包含相同的数据。 Here an example of X:这里是 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)]:我将其转换为三个维度 [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]].然后,我设置了输入 in_dim (2, 1) [(x.shape[1], x.shape[2])] 和输出 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: xtest的内容是:

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

with shape (2,1)形状 (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).警告显示您构建了具有形状 (None, 2, 1) 的模型,但随后在具有形状 (None, 1, 1) 的输入上调用了它。

I'd suggest you print out xtest and check it's shape.我建议你打印出 xtest 并检查它的形状。 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().我不确定最后一部分,但我也认为你不应该将 xtest 作为一个整体插入到 model.predict() 中。 Instead you should feed in an element of xtest.相反,您应该输入 xtest 的一个元素。 For this you might need to use tf.expand_dims(xtest[input_for_prediction_index], axis=0)为此,您可能需要使用 tf.expand_dims(xtest[input_for_prediction_index], axis=0)

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

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