展开 LSTM 时 Keras 不兼容的输入维度

Keras incompatible input dimensions when unrolling LSTM

为什么下面的代码给出ValueError: Input 0 is incompatible with layer dense_14: expected min_ndim=2, found ndim=1?当我删除 unroll=True 时它起作用了,我不希望这个参数影响 LSTM 的输出维度。

from keras.models import Sequential
from keras.layers import LSTM, Dense
model = Sequential()
model.add(LSTM(
  100,
  batch_input_shape=(1, 1, 17),
  unroll=True
))
model.add(Dense(1))

我想这与此有关:

In [9]: from keras.models import Sequential
   ...: from keras.layers import LSTM, Dense
   ...: from keras import backend as K
   ...: def LSTM_output_dimensions(*args,**kwargs):
   ...:   model = Sequential()
   ...:   model.add(LSTM(
   ...:     *args,
   ...:     **kwargs
   ...:   ))
   ...:   return K.ndim(model.outputs[0])
   ...:

In [10]: LSTM_output_dimensions(50, batch_input_shape=(1, 1, 17))
Out[10]: 2

In [11]: LSTM_output_dimensions(50, batch_input_shape=(1, 1, 17),return_sequences=True)
Out[11]: 3

In [12]: LSTM_output_dimensions(50, batch_input_shape=(1, 1, 17),unroll=True)
Out[12]: 1

In [13]: LSTM_output_dimensions(50, batch_input_shape=(1, 1, 17),unroll=True,return_sequences=True)
Out[13]: 2

这似乎是 bug in Keras,因为 unroll=True(1, x) 的输入形状无关。