为什么 softmax_cross_entropy_with_logits_v2 返回成本甚至相同 [英] why softmax_cross_entropy_with_logits_v2 return cost even same value

查看:35
本文介绍了为什么 softmax_cross_entropy_with_logits_v2 返回成本甚至相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经测试过softmax_cross_entropy_with_logits_v2"随机数

i have tested "softmax_cross_entropy_with_logits_v2" with a random number

import tensorflow as tf

x = tf.placeholder(tf.float32,shape=[None,5])
y = tf.placeholder(tf.float32,shape=[None,5])
softmax = tf.nn.softmax_cross_entropy_with_logits_v2(logits=x,labels=y)

with tf.Session() as sess:
    feedx=[[0.1,0.2,0.3,0.4,0.5],[0.,0.,0.,0.,1.]]
    feedy=[[1.,0.,0.,0.,0.],[0.,0.,0.,0.,1.]]
    softmax = sess.run(softmax, feed_dict={x:feedx, y:feedy})
    print("softmax", softmax)

控制台softmax [1.8194163 0.9048325]"

console "softmax [1.8194163 0.9048325]"

我对这个功能的理解是此函数仅在 logits 和 label 不同时返回成本.

what i understand about this function was This function only returns cost when logits and labels are different.

那为什么即使是相同的值,它也返回 0.9048325?

then why it returns 0.9048325 even same value?

推荐答案

tf.nn.softmax_cross_entropy_with_logits_v2 的工作方式是它在你的 x 数组上做 softmax将数组转化为概率:

The way tf.nn.softmax_cross_entropy_with_logits_v2 works is that it does softmax on your x array to turn the array into probabilities:

其中 i 是数组的索引.然后 tf.nn.softmax_cross_entropy_with_logits_v2 的输出将是 -log(p) 和标签之间的点积:

where i is the index of your array. Then the output of tf.nn.softmax_cross_entropy_with_logits_v2 will be the dotproduct between -log(p) and the labels:

由于标签是 0 或 1,因此只有标签等于 1 的术语有贡献.所以在你的第一个样本中,第一个索引的 softmax 概率是

Since the labels are either 0 or 1, only the term where the label is equal to one contributes. So in your first sample, the softmax probability of the first index is

输出将是

您的第二个示例会有所不同,因为 x[0]x[1] 不同.

Your second sample will be different, since x[0] is different than x[1].

这篇关于为什么 softmax_cross_entropy_with_logits_v2 返回成本甚至相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆