Skip to main content
 首页 » 编程设计

python之如何正确计算 tf.nn.weighted_cross_entropy_with_logits pos_weight 变量

2025年05月04日25over140

我正在使用卷积神经网络。

我的数据很不平衡,我有两个类。

我的第一个类包含:551,462 个图像文件

我的第二堂课包含:52,377 个图像文件

我想使用 weighted_cross_entropy_with_logits,但我不确定我是否正确计算了 pos_weight 变量。

我现在正在使用

classes_weights = tf.constant([0.0949784, 1.0]) 
cross_entropy = tf.reduce_mean(tf.nn.weighted_cross_entropy_with_logits(logits=logits, targets=y_, pos_weight=classes_weights)) 
train_step = tf.train.AdamOptimizer(LEARNING_RATE, epsilon=1e-03).minimize( 
      cross_entropy 
    , global_step=global_step 
    ) 

或者我应该使用

classes_weights = 10.5287

请您参考如下方法:

来自文档:

pos_weight: A coefficient to use on the positive examples.

和

The argument pos_weight is used as a multiplier for the positive targets:

因此,如果您的第一类是正数,则 pos_weights = 52,377/551,462,否则 551,462/52,377