繁体   English   中英

错误的输出 LSTM Keras

[英]Wrong output LSTM Keras

我想训练一个 LSTM 模型从两个输入变量中预测两个变量。

X 和 Y 将包含相同的数据。 这里是 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]])

我将其转换为三个维度 [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]],

然后,我设置了输入 in_dim (2, 1) [(x.shape[1], x.shape[2])] 和输出 out_dim 2 [y.shape[1]] 的层。 配置模型后

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

并安装它:

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

xtest的内容是:

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

形状 (2,1)

我从一个样本中得到这个预测:

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)

但是,输出看起来不像输入。 任何想法?

警告显示您构建了具有形状 (None, 2, 1) 的模型,但随后在具有形状 (None, 1, 1) 的输入上调用了它。

我建议你打印出 xtest 并检查它的形状。 如果它不符合您的预期,请修复它,否则还要检查其他元素的形状。

我不确定最后一部分,但我也认为你不应该将 xtest 作为一个整体插入到 model.predict() 中。 相反,您应该输入 xtest 的一个元素。 为此,您可能需要使用 tf.expand_dims(xtest[input_for_prediction_index], axis=0)

暂无
暂无

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

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