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

huangapple go评论87阅读模式
英文:

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 fitmethod 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.

huangapple
  • 本文由 发表于 2020年1月3日 18:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定