gpt4 book ai didi

python - 如何正确创建多输入神经网络

转载 作者:行者123 更新时间:2023-11-30 09:59:28 25 4
gpt4 key购买 nike

我正在构建一个神经网络,它有两个汽车图像作为输入,并对它们是否具有相同的品牌和型号进行分类。我的问题出在keras的fit方法上,因为有这个错误

ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (2,)

网络架构如下:

input1=Input((150,200,3))
model1=InceptionV3(include_top=False, weights='imagenet', input_tensor=input1)
model1.layers.pop()
input2=Input((150,200,3))
model2=InceptionV3(include_top=False, weights='imagenet', input_tensor=input2)
model2.layers.pop()
for layer in model2.layers:
layer.name = "custom_layer_"+ layer.name
concat = concatenate([model1.layers[-1].output,model2.layers[-1].output])
flat = Flatten()(concat)
dense1=Dense(100, activation='relu')(flat)
do1=Dropout(0.25)(dense1)
dense2=Dense(50, activation='relu')(do1)
do2=Dropout(0.25)(dense2)
dense3=Dense(1, activation='softmax')(do2)
model = Model(inputs=[model1.input,model2.input],outputs=dense3)

我的想法是,该错误是由于我在数组上调用的 to_catogorical 方法造成的,该方法将两辆车是否具有相同的品牌和型号存储为 0 或 1。有什么建议吗?

最佳答案

由于您正在使用 one-hot 编码标签进行二元分类,因此您应该更改这一行:

dense3=Dense(1, activation='softmax')(do2)

致:

dense3=Dense(2, activation='softmax')(do2)

带有单个神经元的 Softmax 没有意义,应该使用两个神经元进行带有 Softmax 激活的二元分类。

关于python - 如何正确创建多输入神经网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59577013/

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