为什么在Keras中CNN的所有结果都相同

我在Keras的CNN代码中遇到了一个问题。在构建了一个简单的神经网络架构并运行了预测函数后,所有预测结果都是相同的。我的数据分为两类(健康和不健康)。我之前认为问题出在图片的复杂性上,但当我将代码应用到另一组数据(包含白色图像和黑色图像)时,发现结果仍然相同。您认为问题的根源是什么?


我的CNN架构代码

#batch_size to trainbatch_size = 32# number of output classesnb_classes = 2# number of epochs to trainnb_epoch = 2# number of convolutional filters to usenb_filters = 32# size of pooling area for max poolingnb_pool = 2# convolution kernel sizenb_conv = 3HIDDEN_LAYERS = 4sgd = SGD(lr=0.5, decay=1e-6, momentum=0.6, nesterov=True)Y_train = np_utils.to_categorical(Y_train, nb_classes)Y_test = np_utils.to_categorical(Y_test, nb_classes)X_train /= 255X_test /= 255# first set of CONV => RELU => POOL layersmodel = Sequential()model.add(Conv2D(20, (5, 5), padding="same",input_shape=shape_ord))model.add(Activation("relu"))model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool), strides=(2, 2)))# second set of CONV => RELU => POOL layersmodel.add(Conv2D(50, (5, 5), padding="same"))model.add(Activation("relu"))model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool), strides=(2, 2)))model.add(Dropout(0.5))# first (and only) set of FC => RELU layersmodel.add(Flatten())model.add(Dense(128))model.add(Activation('relu'))#model.add(Dropout(0.5))# softmax classifiermodel.add(Dense(nb_classes))model.add(Activation(K.sigmoid))#categorical_crossentropymodel.compile(loss="binary_crossentropy", optimizer=sgd,metrics=["accuracy"])

结果预测结果


回答:

由于您使用了最终的sigmoid层并采用binary_crossentropy,请尝试将您的标签编码为Nx1的0和1向量。

下面的代码行将它们转换为Nx2的向量,这通常是与最终的softmax层一起使用的。

Y_train = np_utils.to_categorical(Y_train, nb_classes)Y_test = np_utils.to_categorical(Y_test, nb_classes)

如果Y_trainY_test已经是Nx1的0和1向量,请尝试注释掉上述代码行。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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