gpt4 book ai didi

python - 使用 Tensorflow Keras 将 CNN 与 LSTM 相结合

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:49 29 4
gpt4 key购买 nike

我正在使用预训练的 ResNet-50 模型,并希望将倒数第二层的输出提供给 LSTM 网络。这是我仅包含 CNN (ResNet-50) 的示例代码:

N = NUMBER_OF_CLASSES
#img_size = (224,224,3)....same as that of ImageNet
base_model = ResNet50(include_top=False, weights='imagenet',pooling=None)
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(1024, activation='relu')(x)
model = Model(inputs=base_model.input, outputs=predictions)

接下来,我想将它提供给一个 LSTM 网络,如下...

final_model = Sequential()
final_model.add((model))
final_model.add(LSTM(64, return_sequences=True, stateful=True))
final_model.add(Dense(N, activation='softmax'))

但我很困惑如何将输出 reshape 为 LSTM 输入。我的原始输入是 (224*224*3) 到 CNN。另外,我应该使用 TimeDistributed 吗?

感谢任何形式的帮助。

最佳答案

在 CNN 之后添加 LSTM 没有多大意义,因为 LSTM 主要用于时间/序列信息,而您的数据似乎只是空间数据,但是如果您仍然想使用它,请使用

x = Reshape((1024,1))(x)

这会将其转换为具有 1 个特征的 1024 个样本序列

如果您在谈论时空数据,请在 Resnet 层上使用 Timedistributed 然后您可以使用 convlstm2d

关于python - 使用 Tensorflow Keras 将 CNN 与 LSTM 相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52525362/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com