[tensorflow损失函数系列]weighted_cross_entropy_with_logits

weighted_cross_entropy_with_logits

觉得有用的话,欢迎一起讨论相互学习~


我的微博我的github我的B站

weighted_cross_entropy_with_logits(targets, logits, pos_weight, name=None):

此函数功能以及计算方式基本与tf_nn_sigmoid_cross_entropy_with_logits差不多,但是加上了权重的功能,是计算具有权重的sigmoid交叉熵函数

计算方法 :

p o s w e i g h t ∗ t a r g e t s ∗ − l o g ( s i g m o i d ( l o g i t s ) ) + ( 1 − t a r g e t s ) ∗ − l o g ( 1 − s i g m o i d ( l o g i t s ) ) pos_weight*targets * -log(sigmoid(logits)) + (1 - targets) * -log(1 - sigmoid(logits)) posweighttargetslog(sigmoid(logits))+(1targets)log(1sigmoid(logits))

官方文档定义及推导过程:

通常的cross-entropy交叉熵函数定义如下:

t a r g e t s ∗ − l o g ( s i g m o i d ( l o g i t s ) ) + ( 1 − t a r g e t s ) ∗ − l o g ( 1 − s i g m o i d ( l o g i t s ) ) targets * -log(sigmoid(logits)) + (1 - targets) * -log(1 - sigmoid(logits)) targetslog(sigmoid(logits))+(1targets)log(1sigmoid(logits))

对于加了权值pos_weight的交叉熵函数:

t a r g e t s ∗ − l o g ( s i g m o i d ( l o g i t s ) ) ∗ p o s w e i g h t + ( 1 − t a r g e t s ) ∗ − l o g ( 1 − s i g m o i d ( l o g i t s ) ) targets * -log(sigmoid(logits)) * pos_weight + (1 - targets) * -log(1 - sigmoid(logits)) targetslog(sigmoid(logits))posweight+(1targets)log(1sigmoid(logits))

现在我们使用 x = logits, z = targets, q = pos_weight的代数式
  The loss is:

        qz * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
      = qz * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))
      = qz * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))
      = qz * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))
      = (1 - z) * x + (qz +  1 - z) * log(1 + exp(-x))
      = (1 - z) * x + (1 + (q - 1) * z) * log(1 + exp(-x))
我们把l = (1 + (q - 1) * z), 来确保稳定性并且比避免溢出,公式为:

( 1 − z ) ∗ x + l ∗ ( l o g ( 1 + e x p ( − a b s ( x ) ) ) + m a x ( − x , 0 ) ) (1 - z) * x + l * (log(1 + exp(-abs(x))) + max(-x, 0)) (1z)x+l(log(1+exp(abs(x)))+max(x,0))

logits and targets 必须要有相同的数据类型和shape.

参数:

_sentinel:本质上是不用的参数,不用填

targets:一个和logits具有相同的数据类型(type)和尺寸形状(shape)的张量(tensor)

shape:[batch_size,num_classes],单样本是[num_classes]

logits:一个数据类型(type)是float32或float64的张量

pos_weight:正样本的一个系数

name:操作的名字,可填可不填

实例代码

import numpy as np
import tensorflow as tf

input_data = tf.Variable(np.random.rand(3, 3), dtype=tf.float32)
# np.random.rand()传入一个shape,返回一个在[0,1)区间符合均匀分布的array

output = tf.nn.weighted_cross_entropy_with_logits(logits=input_data,
                                                  targets=[[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0]],
                                                  pos_weight=2.0)
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(output))
# [[ 1.04947078  0.89594436  0.92146152]
#  [ 0.70252579  1.00673866  1.08856964]
#  [ 1.07195592  1.18525708  1.04106498]]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值