繁体   English   中英

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

[英]How to correctly create a multi input neural network

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

ValueError:检查目标时出错:预期dense_3具有形状(1,)但得到形状为(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。 有什么建议吗?

由于您正在使用单热编码标签进行二进制分类,因此您应该更改这一行:

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

到:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM