【问题标题】:Getting dimension error while passing the features extracted by CNN to LSTM将 CNN 提取的特征传递给 LSTM 时出现维度错误
【发布时间】:2017-09-26 15:02:20
【问题描述】:

我正在使用 keras 中的深度学习进行视频分类。我使用形状为 (7,7,512) 的 VGG16 模型提取了特征。我有大约 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`

【问题讨论】:

  • 你能分享你的代码吗?我想知道你是如何提取特征的,然后将其传递给 LSTM。也许是你的脚本的演示?

标签: deep-learning lstm keras-layer


【解决方案1】:

输入形状

具有形状(batch_size、timesteps、input_dim)的 3D 张量,(可选)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')

【讨论】:

  • 你是如何将提取的特征传递到 LSTM 中的?
猜你喜欢
  • 2023-03-05
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2020-01-24
  • 1970-01-01
相关资源
最近更新 更多