python - 如何解决 ValueError : Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata). ?

标签 python tensorflow machine-learning keras deep-learning

我正在尝试在 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')

## define the model with input layer and output layer
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` (thus holding past layer metadata). Found: <keras.layers.core.Dense object at 0x000001EDAEC47348>

有人可以告诉我我在这里做错了什么以及如何正确定义我的模型吗?感谢期待。

Window 10
Python 3.7.6
Tensorflow-gpu==1.14
Keras==2.3.1
Wrote the code based on 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)

关于python - 如何解决 ValueError : Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata). ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60152154/

相关文章:

python - 如何解决 Gurobi 中的 ImportError?

python - 如何通过用 python 编写解析器来提取与 C 程序的函数定义相关的起始行号?

python - 在 matplotlib 中绘制 Needleman-Wunsch 成对序列比对的得分矩阵

python - 模块 'tensorflow.python.keras.api._v2.keras.layers' 没有属性 'CuDNNLSTM'

tensorflow - TensorFlow 中 logits 一词的含义是什么?

python - 如何在python中转储不带引号的json

python - 没有名为 'scipy' 的模块

python - 在多类分类问题中将 class_weights 参数与 Keras 一起使用时出错

python - scikit learn中的GridSearchCV如何选择k折的最佳参数

python - cross_val_score 和 cross_val_predict 的区别