具有label_smoothing的TensorFlow sequence_loss

问题描述 投票:2回答:1

是否有可能使用label_smoothingtf.losses.softmax_cross_entropytf.contrib.seq2seq.sequence_loss功能?

我可以看到sequence_loss可选择将softmax_loss_function作为参数。但是,这个函数会将targets作为一个整数列表,而不是tf.losses.softmax_cross_entropy所需的单热编码向量,这也是在TensorFlow中支持label_smoothing的唯一函数。

你能推荐一种使用sequence_loss制作label_smoothing的方法吗?

python tensorflow softmax cross-entropy sequence-to-sequence
1个回答
3
投票

这不能有效地完成。

tf.contrib.seq2seq.sequence_loss设计用于处理非常大的词汇表,因此它期望稀疏族的损失函数(详见this question)。主要区别在于标签使用序数编码而不是单热编码,因为后者需要太多内存。从不计算实际的单热编码。

另一方面,label_smoothingtf.losses.softmax_cross_entropy参数是操纵单热编码的选项。这是它的作用:

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_logitstf.contrib.seq2seq.sequence_loss都没有提供类似的参数。当然,您可以自己进行转换,但这会破坏整个优化。

© www.soinside.com 2019 - 2024. All rights reserved.