为什么model.predict给出3个输出?

时间:2020-04-21 00:27:49

标签: python tensorflow machine-learning keras lstm

我正在尝试使用Keras对单变量时间序列进行预测。

NN模型看起来像

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d (Conv1D)              (None, None, 25)          150       
_________________________________________________________________
lstm (LSTM)                  (None, None, 1024)        4300800   
_________________________________________________________________
dropout (Dropout)            (None, None, 1024)        0         
_________________________________________________________________
lstm_1 (LSTM)                (None, None, 1024)        8392704   
_________________________________________________________________
dropout_1 (Dropout)          (None, None, 1024)        0         
_________________________________________________________________
lstm_2 (LSTM)                (None, None, 1024)        8392704   
_________________________________________________________________
dropout_2 (Dropout)          (None, None, 1024)        0         
_________________________________________________________________
lstm_3 (LSTM)                (None, None, 1024)        8392704   
_________________________________________________________________
dropout_3 (Dropout)          (None, None, 1024)        0         
_________________________________________________________________
dense (Dense)                (None, None, 1)           1025      
=================================================================
Total params: 29,480,087
Trainable params: 29,480,087
Non-trainable params: 0
_________________________________________________________________

使用该系列的前三个值对我的数据进行加窗预测下一个。 因此我的测试数据集看起来像

list(dataset.as_numpy_iterator())
[array([[[ 0.        ],
         [ 0.02346429],
         [ 0.04559132]],

        [[ 0.        ],
         [ 0.02161974],
         [ 0.13014923]],

        [[ 0.        ],
         [ 0.10623277],
         [-0.02918068]],

        [[ 0.        ],
         [-0.12240955],
         [-0.21869095]]])]

一切顺利,但是当我将其输入model.predict(dataset)时,它输出的结果是

array([[[ 0.01316399],
        [ 0.03728709],
        [ 0.06164959]],

       [[ 0.01316399],
        [ 0.03512047],
        [ 0.1292857 ]],

       [[ 0.01316399],
        [ 0.1172413 ],
        [-0.01671433]],

       [[ 0.01316399],
        [-0.10654409],
        [-0.16395506]]], dtype=float32)

,此示例的形状为(4, 3, 1)

由于我的NN的最后一层是具有单个单位的密集区域,因此我只希望对输入要素的每个三元组得到一个预测。为什么每个训练示例的预测中似乎都有三个输出?

1 个答案:

答案 0 :(得分:1)

在最后一个LSTM层中,设置参数return_sequences = False。

LSTM(..., return_sequences=False)