keras - 神经网络只预测二进制类中的一个类

标签 keras neural-network classification mlp

我的任务是了解工厂中的缺陷品。这意味着,我尝试检测有缺陷的商品或优质商品。这导致了一个问题,即一个类别支配其他类别(一个类别占数据的 99.7%),因为有缺陷的项目非常罕见。训练精度为 0.9971,验证精度为 0.9970。听起来很棒。 但问题是,该模型只预测所有商品都是 0 类,即优质商品。这意味着,它无法对任何有缺陷的商品进行分类。 我怎么解决这个问题?我查了其他问题也试过了,还是有这种情况。总数据点为 122400 行和 5 个特征。

最后我的测试集混淆矩阵是这样的

array([[30508,     0],
       [   92,     0]], dtype=int64)

这做的很糟糕。

我的代码如下:

le = LabelEncoder()
y = le.fit_transform(y)



ohe = OneHotEncoder(sparse=False)
y = y.reshape(-1,1)
y = ohe.fit_transform(y)


scaler = StandardScaler()
x = scaler.fit_transform(x)


x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.25, random_state = 777) 




#DNN Modelling


epochs = 15
batch_size =128
Learning_rate_optimizer = 0.001



model = Sequential() 

model.add(Dense(5, 
                kernel_initializer='glorot_uniform',
                activation='relu', 
                input_shape=(5,)))  

model.add(Dense(5,
                kernel_initializer='glorot_uniform', 
                activation='relu'))   
model.add(Dense(8,
                kernel_initializer='glorot_uniform', 
                activation='relu'))

model.add(Dense(2,
                kernel_initializer='glorot_uniform', 
                activation='softmax')) 



model.compile(loss='binary_crossentropy',
              optimizer=Adam(lr = Learning_rate_optimizer), 
              metrics=['accuracy']) 


history = model.fit(x_train, y_train,
                    batch_size=batch_size, 
                    epochs=epochs,  
                    verbose=1, 
                    validation_data=(x_test, y_test))



y_pred = model.predict(x_test)

confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1)) 

谢谢

最佳答案

听起来您的数据集高度不平衡,该模型只学习如何对优质商品进行分类。 您可以尝试此处列出的方法之一: https://machinelearningmastery.com/tactics-to-combat-imbalanced-classes-in-your-machine-learning-dataset/

关于keras - 神经网络只预测二进制类中的一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095545/

相关文章:

python - 嵌入输入形状 : expected embedding_1_input to have shape (25, 时出错,但得到形状为 (1,) 的数组

artificial-intelligence - 您可以在哪些领域对人工智能进行编程?

machine-learning - Pytorch 中 GRU 单元的隐藏和输出是否相同?

C++ 库/框架,用于机器学习中混合模型的 API

python - ssim 作为自动编码器中的自定义损失函数(keras 或/和tensorflow)

python - Keras,为什么向模型添加层这么慢?

python - 如何将 report_tensor_allocations_upon_oom 添加到 Keras 中的 RunOptions

python - Keras Fit_generator 回调

machine-learning - ANN、SVM 和 KNN 分类器有什么区别?

python - 为 scikit learn 构建功能集