tensorflow - 如何使用 Tensorflow 或 GBM 调整模型以获得罕见的二元结果

标签 tensorflow machine-learning keras xgboost gbm

我目前正在处理具有罕见二进制结果的数据,即响应向量大部分包含 0,仅包含少量 1(大约 1.5%)。我有大约 20 个连续解释变量。我尝试使用 GBM、随机森林、TensorFlow 和 Keras 后端来训练模型。

无论我使用哪种方法,我都观察到模型的特殊行为:

准确度很高 (~98%),但模型预测所有结果的“0”类概率约为 98.5%,“1”类概率约为 1.5%。

如何防止这种行为?

我正在使用 RStudio。例如,带有 Keras 的 TF 模型将是:

model <- keras_model_sequential()

model %>%
  layer_dense(units = 256, activation = "relu", input_shape = c(20)) %>%
  layer_dense(units = 256, activation = "relu") %>%
  layer_dense(units = 2, activation = "sigmoid")

parallel_model <- multi_gpu_model(model, gpus=2)
parallel_model %>% compile(
  optimizer = "adam",             
  loss = "binary_crossentropy",
  metrics = "binary_accuracy")

histroy <- parallel_model %>% fit(
  x_train, y_train,
  batch_size = 64,
  epochs = 100,
  class_weight = list("0"=1,"1"=70),
  verbose = 1,
  validation_split = 0.2
)

但我的观察并不局限于TF。这使我的问题更加普遍。我并不是要求对上述模型进行具体调整,而是想讨论在什么时候所有结果都被分配相同的概率。

我可以猜测,问题与损失函数有关。 我知道没有办法使用 AUC 作为损失函数,因为它是不可微分的。如果我使用未知数据测试 AUC 模型,结果并不比随机猜测更好。

我不介意用 Python 代码来回答,因为这不是编码问题,而是一般行为和算法的问题。

最佳答案

当你的问题有不平衡的类时,我建议在训练模型之前使用 SMOTE(仅在训练数据上!!!不要在测试数据上使用 smote!!!)。

例如:

from imblearn.over_sampling import SMOTE
X_trn_balanced, Y_trn_balanced = SMOTE(random_state=1, ratio=1).fit_sample(X_trn, Y_trn)
#next fit the model with the balanced data
model.fit(X_trn_balanced, Y_trn_balanced )

关于tensorflow - 如何使用 Tensorflow 或 GBM 调整模型以获得罕见的二元结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57508031/

相关文章:

python - 属性错误: module 'tensorflow' has no attribute 'batch_matrix_band_part'

tensorflow - 构建 TensorFlow : bazel cannot find libstdc++ in non-standard directory

python - 创建临时 keras session ?

python - Keras 模型中的 Tensorflow 操作

python - TF Gradient Tape 有跨积问题?

python - 值错误: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`

machine-learning - 卷积神经网络中边缘检测矩阵背后的直觉

scala - 根据 scala 中工资列的平均值映射新列值(将字符串转换为 int)

python - 多标签二值化器 : float object not iterable

tensorflow - TensorFlow 2 和 Keras 中不同的正向和反向传播