簡體   English   中英

如何解決 ValueError:模型的輸出張量必須是 Keras `Layer` 的輸出(因此保存過去的層元數據)。?

[英]How can I resolve ValueError: Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata).?

我正在嘗試在 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

您沒有正確地將您的層與 Functional 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM