我正在构建一个神经网络,它的输入是两张汽车图片,并判断它们是否属于相同的品牌和型号。我的问题出现在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.nameconcat = 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)
我认为这个错误是因为我对存储两辆车是否为同一品牌和型号的数组(以0或1表示)调用了to_catogorical
方法。有什么建议吗?
回答:
由于您在进行二分类且使用了一热编码标签,因此您应该将以下这行代码改为:
dense3=Dense(1, activation='softmax')(do2)
改为:
dense3=Dense(2, activation='softmax')(do2)
单个神经元使用Softmax是没有意义的,对于使用Softmax激活的二分类,应该使用两个神经元。