【问题标题】:Conditional weighting for SparseCategoricalCrossentropy in TensorFlowTensorFlow 中 SparseCategoricalCrossentropy 的条件加权
【发布时间】: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


    【解决方案1】:

    您能否指定您的输入,我曾尝试重现此错误,但得到的答案没有任何错误。 对于输入,我使用过

    real = np.random.rand(1,20).round()
    pred = np.random.rand(20,20).round()
    

    【讨论】:

    • 我的输入是 (64, 29) 的形状,值为 0, 1, 2。
    猜你喜欢
    • 2020-05-04
    • 2021-06-04
    • 1970-01-01
    • 2017-08-04
    • 2019-08-02
    • 1970-01-01
    • 2017-04-03
    • 2018-02-02
    • 1970-01-01
    相关资源
    最近更新 更多