如何解决 ValueError: Output tensors to a Model must be the output of a Keras `Layer` (因此包含之前层的元数据)?

我在尝试使用keras实现一个3Dcnn模型时,遇到了关于模型调用的问题。运行以下代码时:

.........input_layer = Input((16, 16, 16, 3))x = inception_v4_stem(input_layer)for i in range(num_A_blocks):    x = inception_v4_A(x)x = inception_v4_reduction_A(x)for i in range(num_B_blocks):    x = inception_v4_B(x)x = inception_v4_reduction_B(x)for i in range(num_C_blocks):    x = inception_v4_C(x)x = AveragePooling3D((4, 4, 4), strides=(1, 1, 1), padding="same", data_format="channels_last")x = Dropout(0.5)x = Flatten()x = Dense(nb_classes, activation='softmax')## 用输入层和输出层定义模型model = Model(inputs = input_layer, outputs = x)model.summary()model.compile(loss=categorical_crossentropy, optimizer=Adadelta(lr=0.1), metrics=['acc'])model.fit(x=xtrain, y=y_train, batch_size=128, epochs=50, validation_split=0.2)

我得到了以下错误:

Traceback (most recent call last):  File "kI3DV2y.py", line 275, in <module>    model = Model(inputs = [input_layer], outputs = x)  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper    return func(*args, **kwargs)  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 94, in __init__    self._init_graph_network(*args, **kwargs)  File "C:\Users\sancy\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 198, in _init_graph_network    'Found: ' + str(x))ValueError: Output tensors to a Model must be the output of a Keras `Layer` (因此包含之前层的元数据). Found: <keras.layers.core.Dense object at 0x000001EDAEC47348>

请问有人能告诉我我哪里做错了,以及如何正确定义我的模型吗?提前感谢。

Window 10Python 3.7.6Tensorflow-gpu==1.14Keras==2.3.1根据keras 2 API编写代码

回答:

你没有正确地使用函数式API,因为你没有给层提供输入。这是正确的方法:

x = AveragePooling3D((4, 4, 4), strides=(1, 1, 1), padding="same", data_format="channels_last")(x)x = Dropout(0.5)(x)x = Flatten()(x)x = Dense(nb_classes, activation='softmax')(x)

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中创建了一个多类分类项目。该项目可以对…

发表回复

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