【发布时间】:2020-11-07 21:35:18
【问题描述】:
我们正在 TensorFlow 中进行抽取式文本摘要任务。我们已经能够启动并运行基准模型。现在,为了得到一个更全面的模型,我们想要做的是能够补偿填充的序列,同时对最初作为摘要候选的序列给予更多的偏好。
这是我们迄今为止所做的:
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True, reduction='none')
def loss_function(real, pred):
# account for the padded sequences
mask = tf.math.logical_not(tf.math.equal(real, 0))
# account for the summary tags
important_tags = tf.math.equal(real, 2)
# calculate the original loss
loss_ = loss_object(real, pred)
# compensation scheme
mask = tf.cast(mask, dtype=loss_.dtype)
important_tags = tf.cast(important_tags, dtype=loss_.dtype)
loss_ *= mask # for the padded values
loss_ = 3 * important_tags # for giving more weightage to the summary candidates
return tf.reduce_mean(loss_)
这会导致梯度未找到错误。一般而言,任何补救这种情况的指示,甚至是使用SparseCategoricalCrossentropy 进行条件加权的更好方法都会有所帮助。
【问题讨论】:
标签: python tensorflow keras nlp tensorflow2.0