gpt4 book ai didi

python - TypeError : The added layer must be an instance of class Layer. 在 0x7fc6f1b92240> 处找到 :
转载 作者:行者123 更新时间:2023-12-01 08:19:35 25 4
gpt4 key购买 nike

我正在尝试将 vgg16 层添加到顺序模型,但出现问题标题中提到的错误

from keras.applications.vgg16 import VGG16

from tensorflow.contrib.keras.api.keras.models import Sequential
vgg_model = VGG16()
model = Sequential()
#print(model.summary())
for layer in vgg_model.layers:
model.add(layer)

print(model.summary())

我使用的是keras 2.2.4

TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.input_layer.InputLayer object at 0x7fc6f1b92240>

最佳答案

假设您想要删除最后一层并添加您自己的带有 10 个节点的最后一个全连接层。为了实现这个功能可以使用keras API。

from tensorflow.contrib.keras.api.keras.models import Sequential
import keras
from keras_applications.vgg16 import VGG16
vgg_model = VGG16()

# replace the last layer with new layer with 10 nodes.
last_layer = vgg_model.layers[-2].output ##
output = keras.layers.Dense(10, activation="softmax")(last_layer)

model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)
model.summary()


print(model.summary())

或者使用 include_top = False

vgg_model = VGG16(include_top=False)
vgg_output = vgg_model.outputs[0]
output = keras.layers.Dense(10, activation="softmax")(vgg_output)

model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)

您可能想要使用预训练的权重。您可以通过使用权重参数来实现这一点

vgg_model = VGG16(weights='imagenet',include_top=False)

您可能想要卡住某些图层。

number_of_layers_to_freeze = 10
vgg_model = VGG16(include_top=False)
for i in range(number_of_layers_to_freeze):
vgg_model.layers[i].trainable = False
vgg_output = vgg_model.outputs[0]
output = keras.layers.Dense(10, activation="softmax")(vgg_output)

model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)

关于python - TypeError : The added layer must be an instance of class Layer. 在 0x7fc6f1b92240> 处找到 : <keras. engine.input_layer.InputLayer 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54742050/

25 4 0

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