TensorFlow计算交叉熵

TensorFlow中提供了两个计算交叉熵的函数,分别是

tf.nn.softmax_cross_entropy_with_logits_v2(
    _sentinel=None,
    labels=None,
    logits=None,
    name=None
)

tf.nn.sparse_softmax_cross_entropy_with_logits(
    _sentinel=None,
    labels=None,
    logits=None,
    name=None
)

两个函数作用是一样的,官方文档中描述均如下:
Measures the probability error in discrete classification tasks in which the classes are mutually exclusive (each entry is in exactly one class). For example, each CIFAR-10 image is labeled with one and only one label: an image can be a dog or a truck, but not both.

不同之处在于

1. tf.nn.softmax_cross_entropy_with_logits_v2

labels要求传入one-hot编码形式,如如 [ 0 , 1 , 2 ] [0,1,2] ,经one-hot编码 [ [ 1 , 0 , 0 ] , [ 0 , 1 , 1 ] , [ 0 , 0 , 1 ] ] [[1,0,0],[0,1,1],[0,0,1]] (或者概率分布,每个数值表示属于每个类的概率)
logitslabels的shape均为[batch_size,num_classes]

官方文档描述如下:
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.
logits and labels must have the same shape,
e.g.[batch_size, num_classes] and the same dtype (either float16, float32,
or float64).

使用如下:

cem = tf.nn.softmax_cross_entropy_with_logits_v2(logits = Y_, labels = Y)
2. tf.nn.sparse_softmax_cross_entropy_with_logits

labels要求传入的为真实分类的标签值, shape为[batch_size],每个值 [0,num_classes)
logitsshape为[batch_size,num_classes]

官方文档描述如下:
NOTE: For this operation, the probability of a given label is considered exclusive. That is, soft classes are not allowed, and the labels vector must provide a single specific index for the true class for each row of logits (each minibatch entry).
A common use case is to have logits of shape [batch_size, num_classes] and labels of shape [batch_size].

使用如下:

cem = tf.nn.softmax_cross_entropy_with_logits_v2(logits = Y_, labels =  tf.argmax(Y,1))

其中logits表示预测值(神经网络输出),labels表示真实值。

发布了3 篇原创文章 · 获赞 2 · 访问量 169

猜你喜欢

转载自blog.csdn.net/apr15/article/details/104445077