tf.nn.sparse_softmax_cross_entropy_with_logits - tensorflow 中没有一种热编码的标签

tf.nn.sparse_softmax_cross_entropy_with_logits - labels without one hot encoding in tensorflow

我正在尝试了解 tf.nn.sparse_softmax_cross_entropy_with_logits 的工作原理。

描述说:

    A common use case is to have logits of shape [batch_size, num_classes]
 and labels of shape [batch_size]. But higher dimensions are supported.

因此它建议我们可以以原始形式提供标签,例如 [1,2,3]

现在由于所有计算都是按批次完成的,我相信以下是可能的:

在所有情况下,我们假设批处理的大小等于二。

案例一(一批): 登录:

0.4 0.2 0.4
0.3 0.3 0.4

对应标签:

2
3

我猜标签可能被编码为

[1 0 0]
[0 1 0] 

案例2(另一批次): 登录:

0.4 0.2 0.4
0.3 0.3 0.4

对应标签:

1
2

我猜标签可能被编码为(我看不出是什么阻止我们进行这种编码,除非 tensorflow 跟踪它之前的编码方式)

[1 0 0]
[0 1 0] 

所以我们有两种不同的编码。假设 tensorflow 在批次之间保持编码一致是否安全?

没有真正的编码发生。标签只是 1 在相应的单热向量中的位置:

0 -> [1, 0, 0]
1 -> [0, 1, 0]
2 -> [0, 0, 1]

这个"coding"将在每批中使用。