【发布时间】:2020-01-03 10:34:39
【问题描述】:
我正在构建一个神经网络,它有两个汽车图像作为输入,并分类它们是否是相同的品牌和型号。我的问题在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.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。有什么建议吗?
【问题讨论】:
-
如果您将目标作为
np.array([1,0,1,.....])仅作为一维列表传递会发生什么。 -
有这个错误:
ValueError: You are passing a target array of shape (10000, 1) while using as loss `categorical_crossentropy`. `categorical_crossentropy` expects targets to be binary matrices (1s and 0s) of shape (samples, classes). -
尝试使用
loss=sigmoid -
sigmoid不是激活函数吗? -
对不起,你正在训练一个连体网络。您需要修复架构,即您应该有一个基本模型。参考这个:keras.io/examples/mnist_siamese
标签: python machine-learning keras neural-network conv-neural-network