是否可以使用 label_smoothing
来自 tf.losses.softmax_cross_entropy
的功能与 tf.contrib.seq2seq.sequence_loss
?
我可以看到sequence_loss
可选地采用 softmax_loss_function
作为参数。但是,此函数将采用 targets
作为整数列表,而不是 tf.losses.softmax_cross_entropy
所需的 one-hot 编码向量,这也是唯一支持 label_smoothing
的函数在 TensorFlow 中。
你能推荐一种使 label_smoothing 与 sequence_loss
一起使用的方法吗? ?
最佳答案
这无法有效地完成。
tf.contrib.seq2seq.sequence_loss
被设计用于处理非常大的词汇表,因此它期望来自稀疏系列的损失函数(有关详细信息,请参阅this question)。主要区别在于标签使用ordinal编码而不是one-hot,因为后者占用太多内存。实际的 one-hot 编码从不计算。
tf.losses.softmax_cross_entropy
的 label_smoothing
参数另一方面是操作 one-hot 编码的选项。它的作用如下:
if label_smoothing > 0:
num_classes = math_ops.cast(
array_ops.shape(onehot_labels)[1], logits.dtype)
smooth_positives = 1.0 - label_smoothing
smooth_negatives = label_smoothing / num_classes
onehot_labels = onehot_labels * smooth_positives + smooth_negatives
如您所见,要计算此张量,必须显式存储 onehot_labels
,这正是稀疏函数试图避免的情况。这就是为什么 tf.nn.sparse_softmax_cross_entropy_with_logits 和 tf.contrib.seq2seq.sequence_loss 都没有提供类似的参数。当然,您可以自己进行转换,但这会破坏整个优化。
关于python - 带有 label_smoothing 的 TensorFlow sequence_loss,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49136472/