tensorflow 计算交叉熵错误问题解决

ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).

碰到上面这个错误的时候,应该是用了后面这个计算交叉熵的方法了,如果没有用请绕过。 tf.nn.sparse_softmax_cross_entropy_with_logits

自己照着例子做练习的时候碰到上面这个错误,在网上搜,看了好多都没解决,最后看了一下,这个方法的的帮助文档。

sparse_softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=None)

这个是这个函数的参数说明, 第一个参数网上都说的是不用填,我们暂时先不管他

labels: `Tensor` of shape `[d_0, d_1, ..., d_{r-1}]` (where `r` is rank of 
`labels` and result) and dtype `int32` or `int64`. Each entry in `labels` 
must be an index in `[0, num_classes)`. Other values will raise an exception when this op is run on CPU, and return `NaN` for corresponding 
loss and gradient rows on GPU.
logits: Unscaled log probabilities of shape `[d_0, d_1, ..., d_{r-1}, num_classes]` and dtype `float32` or `float64`.

logits 的长度需要比 labels 多一维,就可以了。

例如:

import tensorflow as tf

labels = [[0.2,0.3,0.5],
          [0.1,0.6,0.3]]
logits = [[2,0.5,1],
          [0.1,1,3]]
result1 = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits)


如果这样用就会报这个错
ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).

下面演示一下,两个方法的正确用法:

import tensorflow as tf

labels = [[0.2,0.3,0.5],
          [0.1,0.6,0.3]]
logits = [[2,0.5,1],
          [0.1,1,3]]
result1 = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)
import tensorflow as tf

labels = [0,2]

logits = [[2,0.5,1],
          [0.1,1,3]]


result1 = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits)

总结:sparse_softmax_cross_entropy_with_logits 主要用余结果是排他唯一性的计算, 如果是非唯一的需要用到 softmax_cross_entropy_with_logits

多看看官方文档,多理解。