TensorFlow 计算交叉熵

简介: 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 ],经one-hot编码[ [ 1 , 0 , 0 ] , [ 0 , 1 , 1 ] , [ 0 , 0 , 1 ] ] [[1,0,0],[0,1,1],[0,0,1]](或者概率分布,每个数值表示属于每个类的概率)

logits和labels的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表示真实值。

相关文章
|
人工智能 安全 Cloud Native
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(1)
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(1)
1142 0
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(1)
|
Cloud Native 安全 TensorFlow
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(2)
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(2)
632 0
|
弹性计算 Cloud Native TensorFlow
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(3)
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(3)
518 0
|
Cloud Native TensorFlow 算法框架/工具
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(4)
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow Serving在线推理服务(4)
426 0
|
安全 搜索推荐 TensorFlow
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow横向联邦学习(1)
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow横向联邦学习(1)
577 0
|
Cloud Native 搜索推荐 TensorFlow
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow横向联邦学习(2)
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow横向联邦学习(2)
429 0
|
搜索推荐 Cloud Native TensorFlow
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow横向联邦学习(3)
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow横向联邦学习(3)
439 0
|
Cloud Native 搜索推荐 TensorFlow
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow横向联邦学习(4)
带你读《云原生机密计算最佳实践白皮书》——部署TensorFlow横向联邦学习(4)
408 0
|
机器学习/深度学习 存储 人工智能
TensorFlow 2.0将把Eager Execution变为默认执行模式,你该转向动态计算图了
8 月中旬,谷歌大脑成员 Martin Wicke 在一封公开邮件中宣布,新版本开源框架——TensorFlow 2.0 预览版将在年底之前正式发布。今日,在上海谷歌开发者大会上,机器之心独家了解到一个重大的改变将会把 Eager Execution 变为 TensorFlow 默认的执行模式。这意味着 TensorFlow 如同 PyTorch 那样,由编写静态计算图全面转向了动态计算图。
436 0
TensorFlow 2.0将把Eager Execution变为默认执行模式,你该转向动态计算图了
|
Serverless TensorFlow 算法框架/工具
TensorFlow提取和重用层计算图中的节点
TensorFlow提取和重用层计算图中的节点
321 0

热门文章

最新文章