gpt4 book ai didi

python - 异常训练Resnet50 : "The shape of the input to "Flatten"is not fully defined"

转载 作者:行者123 更新时间:2023-12-01 01:51:19 24 4
gpt4 key购买 nike

我想使用 keras.applications.resnet50 通过以下设置来训练 Resnet 以解决两类问题:

from keras.layers import Dropout, Flatten, Dense
from keras.applications.resnet50 import ResNet50
from keras.models import Model

resNet = ResNet50(include_top=False, weights=None)
y = resNet.output
y = Flatten()(y)
y = Dense(2, activation='softmax')(y)
model = Model(inputs=resNet.input, outputs=y)
opt = keras.optimizers.Adam()
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
epochs = 15
model.fit(train_tensors, train_targets,
validation_data=(valid_tensors, valid_targets),
epochs=epochs, batch_size=10, callbacks=[checkpointer], verbose=1)

运行代码会抛出错误

Exception: The shape of the input to "Flatten" is not fully defined 

所以输出层的输入张量肯定有问题,在我的例子中是一个单热编码向量,即大小为 2 的一维数组。我做错了什么?

最佳答案

你得到

Exception: The shape of the input to "Flatten" is not fully defined

因为您尚未在 resnet 网络中设置输入形状。尝试:

resNet = ResNet50(include_top=False, weights=None, input_shape=(224, 224, 3)) 

此外,由于您在输出层中使用带有 sigmoid 激活的 binary_crossentropy,因此您应该仅使用 1 个神经元而不是 2 个,如下所示:

y = Dense(1, activation='sigmoid')(y)

关于python - 异常训练Resnet50 : "The shape of the input to "Flatten"is not fully defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50693322/

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