我可以使用真实概率分布作为 tf.nn.softmax_cross_entropy_with_logits 的标签吗?

Can I use real probability distributions as labels for tf.nn.softmax_cross_entropy_with_logits?

在Tensorflow手册中,标签的描述如下:

labels: Each row labels[i] must be a valid probability distribution.

那么,如果我对每个输入都有 类 的真实概率分布,这是否意味着标签可以像下面这样。

[[0.1, 0.2, 0.05, 0.007 ... ]
[0.001, 0.2, 0.5, 0.007 ... ]
[0.01, 0.0002, 0.005, 0.7 ... ]]

而且,它比单热编码标签更有效吗?

提前致谢。

一句话,可以,可以用概率作为标签

tf.nn.softmax_cross_entropy_with_logits 的文档说您可以:

NOTE: While the classes are mutually exclusive, their probabilities need not be. All that is required is that each row of labels is a valid probability distribution. If they are not, the computation of the gradient will be incorrect.

If using exclusive labels (wherein one and only one class is true at a time), see sparse_softmax_cross_entropy_with_logits.

让我们举一个简短的例子来确保它工作正常:

import numpy as np
import tensorflow as tf

labels = np.array([[0.2, 0.3, 0.5], [0.1, 0.7, 0.2]])
logits = np.array([[5.0, 7.0, 8.0], [1.0, 2.0, 4.0]])

sess = tf.Session()
ce = tf.nn.softmax_cross_entropy_with_logits(
     labels=labels, logits=logits).eval(session=sess)
print(ce)  # [ 1.24901222  1.86984602]

# manual check
predictions = np.exp(logits)
predictions = predictions / predictions.sum(axis=1, keepdims=True)
ce_np = (-labels * np.log(predictions)).sum(axis=1)
print(ce_np)  # [ 1.24901222  1.86984602]

而且如果你有专属标签,最好使用one-hot编码和tf.nn.sparse_softmax_cross_entropy_with_logits而不是tf.nn.softmax_cross_entropy_with_logits[1.0, 0.0, ...]这样的显式概率表示。这样你可以有更短的代表。