python - 带有 label_smoothing 的 TensorFlow sequence_loss

标签 python tensorflow softmax cross-entropy sequence-to-sequence

是否可以使用 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/

相关文章:

python - Numpy 独特的二维子数组

python - SQLAlchemy:选择多个表

python - 在 keras 中,Float16 比 float32 慢

tensorflow - Keras ImageDataGenerator 预处理

python - 具有 Softmax 输出的神经网络

tensorflow - 使用 Sigmoid 激活函数代替线性激活与在损失中使用 sigmoid 的区别

python - traceback.print_stack() 使用 IPython 的 ultratb

python - 在 apscheduler 中捕获日志

python - 行轴的 np.sum 在 Numpy 中不起作用

tensorflow - TF2 : Compute gradients in keras callback in non-eager mode