为什么softmax_cross_entropy_with_logits_v2返回成本甚至相同的值

问题描述 投票:3回答:2

我用随机数测试了“softmax_cross_entropy_with_logits_v2”

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]”

我对此功能的理解是此功能仅在logits和标签不同时返回成本。

那为什么它返回0.9048325甚至相同的值?

tensorflow softmax cross-entropy
2个回答
5
投票

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

enter image description here

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

enter image description here

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

enter image description here

并且输出将是

enter image description here

你的第二个样本会有所不同,因为x[0]x[1]不同。


0
投票

根据tf.nn.softmax_cross_etnropy_with_logits_v2the documentation预计未缩放的输入,因为它在softmax内部执行logits操作。因此,您的第二个输入[0,0,0,0,1]在内部软加宽到大致类似[0.15,0.15,0.15,0.15,0.4]的值,然后,此logit的交叉熵和真实标签[0,0, 0,0,1]被计算为你得到的值

© www.soinside.com 2019 - 2024. All rights reserved.