【问题标题】:How to correctly create a multi input neural network如何正确创建多输入神经网络
【发布时间】: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


【解决方案1】:

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

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

收件人:

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

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

【讨论】:

  • 因为最后一层需要的神经元数量等于类的数量?
  • @thenoobdeveloper 使用 softmax 激活是
  • 好的,谢谢,它有效。您认为这是解决我的问题的好方法吗?
  • @thenoobdeveloper 这不可能回答,也不是编程问题
猜你喜欢
  • 2012-03-29
  • 2021-02-27
  • 1970-01-01
  • 2012-10-05
  • 2017-03-16
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
相关资源
最近更新 更多