英文:
How to correctly create a multi input neural network
问题
我正在构建一个神经网络,其输入是两张汽车图片,并且分类它们是否是相同的制造商和型号。我的问题出现在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_categorical
方法,该数组以0或1表示两辆汽车是否具有相同的制造商和型号。有什么建议吗?
英文:
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
>ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (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. Any suggestion?
答案1
得分: 1
将这行代码从:
dense3=Dense(1, activation='softmax')(do2)
改成:
dense3=Dense(2, activation='softmax')(do2)
使用只有一个神经元的softmax在二元分类中没有意义,应该使用两个神经元来进行softmax激活。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论