首页 > 解决方案 > 如何正确创建多输入神经网络

问题描述

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

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,如果两辆车具有相同的品牌和型号。有什么建议吗?

标签: pythonmachine-learningkerasneural-networkconv-neural-network

解决方案


由于您正在使用 one-hot 编码标签进行二进制分类,因此您应该更改此行:

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

至:

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

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


推荐阅读