如何正确创建多输入神经网络

我正在构建一个神经网络,它的输入是两张汽车图片,并判断它们是否属于相同的品牌和型号。我的问题出现在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激活的二分类,应该使用两个神经元。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注