How correctly calculate tf.nn.weighted_cross_entropy_with_logits pos_weight variable

别等时光非礼了梦想. 提交于 2019-12-04 18:22:28

From the documentation:

pos_weight: A coefficient to use on the positive examples.

and

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

So if your first class is positive, then pos_weights = 52,377 / 551,462, otherwise 551,462 / 52,377

As @Salvador Dali said, the best source is the source code https://github.com/tensorflow/tensorflow/blob/5b10b3474bea72e29875264bb34be476e187039c/tensorflow/python/ops/nn_impl.py#L183

We have

log_weight = 1 + (pos_weight - 1) * targets

so it only applies if targets==1.

If targets==0 then log_weight = 1

If targets==1 then log_weight = pos_weight

So if we have ratio of positives to negatives x/y we need pos_weight to be y/x so both categories will contribute equally in total

Please note that each scalar in targets tensor corresponds to each category so each member of pos_weight corresponds to each category as well (not positive or negative probability for one category) .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!