简体   繁体   English

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

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

i'm building a NN that has, as input, two car images and classifies if thery are the same make and model.我正在构建一个神经网络,它有两个汽车图像作为输入,并分类它们是否具有相同的品牌和型号。 My problem is in the fit method of keras, because there is this error我的问题出在keras的fit方法上,因为有这个错误

ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (2,) ValueError:检查目标时出错:预期dense_3具有形状(1,)但得到形状为(2,)的数组

The network architecture is the following:网络架构如下:

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)

My idea is that the error is due to the to_catogorical method that i have called on the array which stores, as 0 or 1, if the two cars have the same make and model or not.我的想法是,错误是由于我在数组上调用的to_catogorical方法,该方法将两辆车的品牌和型号存储为 0 或 1。 Any suggestion?有什么建议吗?

Since you are doing binary classification with one-hot encoded labels, then you should change this line:由于您正在使用单热编码标签进行二进制分类,因此您应该更改这一行:

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

To:到:

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

Softmax with a single neuron makes no sense, two neurons should be used for binary classification with softmax activation.使用单个神经元的 Softmax 没有意义,应该使用两个神经元进行 softmax 激活的二元分类。

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

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