python - 如何正确创建多输入神经网络
问题描述
我正在构建一个神经网络,它有两个汽车图像作为输入,并分类它们是否是相同的品牌和型号。我的问题出在fit
keras的方法上,因为有这个错误
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,如果两辆车具有相同的品牌和型号。有什么建议吗?
解决方案
由于您正在使用 one-hot 编码标签进行二进制分类,因此您应该更改此行:
dense3=Dense(1, activation='softmax')(do2)
至:
dense3=Dense(2, activation='softmax')(do2)
带有单个神经元的 Softmax 没有意义,应该使用两个神经元进行带有 softmax 激活的二元分类。
推荐阅读
- node.js - 使用 google apis 更改 google 电子表格的访问权限
- puppet - Puppet 模块是否使用互联网安装软件?
- javascript - 过滤对象删除某些属性
- python - 数据帧的使用
- vue.js - 手动添加 Vue 组件并将数据绑定到本地数据变量
- asp.net - 如何在asp net中链接2个表?
- sql - 访问表单上文本框的 SQL“Where”子句
- python - 在 python 中显示 sqlite3 数据
- android - OpenGL ES 2 不渲染任何东西
- android - 将字节码转换为dex时出错:多个dex文件定义了Landroid/arch/lifecycle/LiveData$LifecycleBoundObserver;