在将CNN提取的功能传递给LSTM时获取尺寸错误

时间:2017-04-28 13:13:52

标签: deep-learning lstm keras-layer

我正在使用keras中的深度学习进行视频分类。我使用VGG16模型提取了这些特征,其形状为(7,7,512)。我有大约55000张图像。我把它传递给了LSTM层,但是得到了尺寸误差。 这是代码,

print len(train_data)
print train_data.shape[1:]
print train_data.shape
model = Sequential()
model.add(LSTM(128,input_shape=train_data.shape[1:]))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(5, activation='softmax'))

这是输出,

55936
(7, 7, 512)
(55936, 7, 7, 512)
Traceback (most recent call last):
File "train_rnn.py", line 135, in <module>
model.add(LSTM(128,input_shape=train_data.shape[1:]))
File "/usr/local/lib/python2.7/site-packages/keras/models.py", line 430, in add
layer(x)
File "/usr/local/lib/python2.7/site-    packages/keras/layers/recurrent.py", line 257, in __call__
return super(Recurrent, self).__call__(inputs, **kwargs)
File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 534, in __call__
self.assert_input_compatibility(inputs)
File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 433, in assert_input_compatibility
str(K.ndim(x)))
**ValueError**: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4`

1 个答案:

答案 0 :(得分:1)

  

输入形状

     

具有形状的3D张量(batch_size,timesteps,input_dim),(可选)2D   有形状的张量(batch_size,output_dim)。

您在4D中的输入(包括输入长度)。

尝试将其重塑为3D:

train_data = train_data.reshape(train_data.shape[0],
                   train_data.shape[1] * train_data.shape[2],
                   train_data.shape[3])

示例(这将引发valueError

X = np.zeros((5, 7, 7, 512)

model = Sequential()
model.add(LSTM(128, input_shape=(7, 7, 512)))
model.add(Dense(1, activation='softmax')
model.compile(loss='binary_crossentropy', optimizer='sgd')

但这不会

X = np.zeros((5, 7, 7, 512)
X = X.reshape(5, 49, 512)

model = Sequential()
model.add(LSTM(128, input_shape=(49, 512)))
model.add(Dense(1, activation='softmax')
model.compile(loss='binary_crossentropy', optimizer='sgd')