Keras/Theano VGG16 AttributeError: ‘Model’ 对象没有属性 ‘ndim’

我试图构建一个使用Theano后端的Keras模型,将一个时间分布的VGG16网络连接到LSTM层,最后连接到一系列的全连接层。然而,我遇到了以下错误:

Traceback (most recent call last):  File "osr.py", line 341, in <module>    osr.generate_osr_model()  File "osr.py", line 145, in generate_osr_model    cnn_out = GlobalAveragePooling2D()(cnn_base)  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 529, in __call__    self.assert_input_compatibility(x)  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 464, in assert_input_compatibility    if K.ndim(x) != spec.ndim:  File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 142, in ndim    return x.ndimAttributeError: 'Model' object has no attribute 'ndim'

这是我用来构建模型的代码部分:

        video = Input(shape=(self.frames_per_sequence,                             3,                             self.rows,                             self.columns))        cnn_base = VGG16(input_shape=(3,                                      self.rows,                                       self.columns),                         weights="imagenet",                         include_top=False)        cnn_out = GlobalAveragePooling2D()(cnn_base)        cnn = Model(input=cnn_base.input, output=cnn_out)        cnn.trainable = False        encoded_frames = TimeDistributed(cnn)(video)        encoded_vid = LSTM(256)(encoded_frames)        hidden_layer = Dense(output_dim=1024, activation="relu")(encoded_vid)        outputs = Dense(output_dim=class_count, activation="softmax")(hidden_layer)        osr = Model(, outputs)        optimizer = Nadam(lr=0.002,                          beta_1=0.9,                          beta_2=0.999,                          epsilon=1e-08,                          schedule_decay=0.004)        osr.compile(loss="categorical_crossentropy",                    optimizer=optimizer,                    metrics=["categorical_accuracy"])

回答:

解决方案是将cnn_base.output作为GlobalAveragePooling2D层的输入:

cnn_out = GlobalAveragePooling2D()(cnn_base.output)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注