tf.losses.sparse_softmax_cross_entropy()

损失函数,经常用语多分类,相比于softmax交叉熵,其区别主要在于,softmax 的label是onehot编码的,如[0,0,1],而sparse它的label是一个可能性最高位置的索引。

logits = tf.constant([0.1,0.1,0.8])
labels = tf.constant([2])
labels2 = tf.constant([0,0,1])
y1 = tf.losses.sparse_softmax_cross_entropy(labels=labels,logits=logits)
y2 = tf.losses.softmax_cross_entropy(onehot_labels=labels2,logits=logits)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run((y1,y2))
    #(0.68972665, 0.68972665)

其原理是说:先队logits进行softmax,具体原理网上很多,使用代码实现就是:

a = (math.e**0.1+math.e**0.1+math.e**0.8)
b,c,d = math.e**0.1/a,math.e**0.1/a,math.e**0.8/a
e = 0*np.log(b)+0*np.log(c)+1*np.log(d)
print(-e)
# 0.6897266409702166

关于weights的说明,从api介绍中:

weights acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If weights is a tensor of shape [batch_size], then the loss weights apply to each corresponding sample.

可以看出来,weight是一个和batch_size相同的系统,也就是,如果一个批次有10个,weight可以设置10个,这10个数会依次和计算出来的交叉熵相乘之后进行一个累加。

logits = tf.constant([[0.1,0.1,0.8],[0.1,0.9,0.0]])
labels = tf.constant([[2],[1]])
labels2 = tf.constant([[0,0,1],[0,1,0]])
y1 = tf.losses.sparse_softmax_cross_entropy(labels=labels,logits=logits)
y2 = tf.losses.softmax_cross_entropy(onehot_labels=labels2,logits=logits,weights=[0.1,0.10])
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run((y1,y2)))
# (0.65404785, 0.06540479)

关键是这个weight的使用时机,比如说做一个多分类的时候,如果类别之间差距特别大,class1 占比90%,class2,占 2%,class3 占比2%,那这时候就需要使用参数进行调整了,不然会出现模型针对某一个类别的占比很大,对其他的就不敏感了,导致模型效果变差。

一般在训练的时候,放入的都是一个batch,这时候得到的其实是针对每一个分别计算之后得到的均值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值