简体   繁体   English

Keras 验证准确度为 1,但在二进制分类中仅预测 class 0?

[英]Keras validation accuracy is 1 but only predicts only class 0 in binary classification?

Using these same stratified training and test splits I'm getting >99% true predictions using Linear SVC and Decision Tree with the following configurations:使用这些相同的分层训练和测试拆分,我使用具有以下配置的线性 SVC 和决策树获得了 >99% 的真实预测:

LinearSVC(C = 0.025, tol = 5e-05, random_state = 21)
DecisionTreeClassifier(max_depth=3, random_state = 21)

I also tested the above two models on a completely separate dataset with 1000 rows, they're working near perfectly.我还在一个包含 1000 行的完全独立的数据集上测试了上述两个模型,它们几乎完美地工作。 Why is the Neural Net below failing?为什么下面的神经网络会失败? Only predicts 0. Using Standard Scaler and Min Max scaler has no effect and only reduces the accuracy of the other two models.只预测 0。使用 Standard Scaler 和 Min Max scaler 没有效果,只会降低其他两个模型的准确度。

print('All dtypes: float64')
print('')
print('X_train maximum value: {0}\nX_train minimum value: {1}'.format(X_train.max().max(), X_train.min().min()))
print('y_train maximum value: {0}\ny_train minimum value: {1}'.format(y_train.max(), y_train.min()))
print('')
print('X_test maximum value: {0}\nX_test minimum value: {1}'.format(X_test.max().max(), X_test.min().min()))
print('y_test maximum value: {0}\ny_test minimum value: {1}'.format(y_test.max(), y_test.min()))
print('')
print('X_train shape: {0}'.format(X_train.shape))
print('y_train shape: {0}'.format(y_train.shape))
print('')
print('X_test shape: {0}'.format(X_test.shape))
print('y_test shape: {0}'.format(y_test.shape))

model_net = Sequential()

model_net.add(Dense(12, activation = 'relu', input_dim = 24))
model_net.add(Dense(1, activation = 'sigmoid'))

print(model_net.summary())
print('')
model_net.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])

epochs = 5
batch_size = 64

run_hist = model_net.fit(X_train, y_train,
                          batch_size = batch_size, 
                          epochs = epochs, verbose = 1, 
                          validation_split = 0.2, validation_data = (X_test, y_test))

y_pred = model_net.predict(X_test).argmax(1)

accuracy = metrics.accuracy_score(y_true=y_test, y_pred=y_pred)
print('')
print('Accuracy: {0}'.format(round(accuracy, 2)))
print('')
      
unique, counts = np.unique(y_pred, return_counts=True)
print('\nUnique prediction values:')
np.asarray((unique, counts)).T

Output: Output:

All dtypes: float64

X_train maximum value: 21781.0
X_train minimum value: 0.0
y_train maximum value: 1.0
y_train minimum value: 0.0

X_test maximum value: 7730.25
X_test minimum value: 0.0
y_test maximum value: 1.0
y_test minimum value: 0.0

X_train shape: (66064, 24)
y_train shape: (66064,)

X_test shape: (16516, 24)
y_test shape: (16516,)
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 12)                300       
                                                                 
 dense_1 (Dense)             (None, 1)                 13        
                                                                 
=================================================================
Total params: 313
Trainable params: 313
Non-trainable params: 0
_________________________________________________________________
None

Epoch 1/5
1033/1033 [==============================] - 5s 3ms/step - loss: 0.3795 - accuracy: 0.9834 - val_loss: 0.0015 - val_accuracy: 0.9998
Epoch 2/5
1033/1033 [==============================] - 3s 3ms/step - loss: 9.6806e-04 - accuracy: 0.9999 - val_loss: 5.3518e-04 - val_accuracy: 0.9999
Epoch 3/5
1033/1033 [==============================] - 4s 3ms/step - loss: 5.0223e-04 - accuracy: 1.0000 - val_loss: 3.1895e-04 - val_accuracy: 0.9999
Epoch 4/5
1033/1033 [==============================] - 3s 3ms/step - loss: 1.8808e-04 - accuracy: 1.0000 - val_loss: 2.7151e-04 - val_accuracy: 0.9999
Epoch 5/5
1033/1033 [==============================] - 3s 3ms/step - loss: 2.3315e-04 - accuracy: 1.0000 - val_loss: 1.0257e-04 - val_accuracy: 1.0000
517/517 [==============================] - 1s 1ms/step

Accuracy: 0.23


Unique prediction values:
array([[    0, 16516]], dtype=int64)

In second layer try relu instead of sigmoid.在第二层尝试 relu 而不是 sigmoid。 Sigmoid/SoftMax is generally used in last dense(fully connected layer). Sigmoid/SoftMax 一般用于最后一个dense(全连接层)。

Second, check if your test and train have mixed classes data as well.(Sometime it happens that the test or train have a particular class as dominant class and the model become bias).其次,检查您的测试和训练是否也有混合类数据。(有时,测试或训练有一个特定的 class 作为主要 class 和 model 成为偏差)。

This is a comment but I don't have 50 reputation.这是一条评论,但我没有 50 声望。

y_pred = model_net.predict(X_test)
y_pred = tf.greater(y_pred, .5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 训练精度高,验证精度低 CNN二元分类 keras - High training accuracy, low validation accuracy CNN binary classification keras Keras 二元分类器教程示例仅提供 50% 的验证准确度 - Keras Binary Classifier Tutorial Example gives only 50% validation accuracy Keras CNN只能预测一个班级 - Keras CNN predicts only a single class 即使在使用类权重训练之后,二进制分类神经网络也只能预测一个看不见的数据值 - Binary Classification neural net predicts only one value for unseen data even after training using class weight 验证准确性如何确定Keras二进制分类中哪个类别正确? - How does the validation accuracy decide which class is correct in a binary classification with Keras? keras cnn模型仅为所有测试图像预测一个类 - keras cnn model only predicts one class for all the test images Keras 多输入 CNN 只预测一个 class - Keras multi input CNN only predicts one class 有没有办法使用多标签分类但当 model 预测 keras 中只有一个 label 时认为是正确的? - Is there a way to use multilabel classification but take as correct when the model predicts only one label in keras? Keras 在二元分类问题中准确率停留在 50% - Keras accuracy stuck at 50% in a binary classification problem Keras 如何计算多类分类问题的验证准确率和训练准确率? - How does Keras compute validation accuracy and training accuracy for multi-class classification problems?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM