展开LSTM时,Keras不兼容输入尺寸

时间:2017-07-19 23:55:57

标签: keras keras-layer

为什么以下代码会提供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

1 个答案:

答案 0 :(得分:0)

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