【问题标题】:Weight samples if incorrect guessed in binary cross entropy如果在二进制交叉熵中猜测不正确,则加权样本
【发布时间】:2018-02-10 11:22:33
【问题描述】:

kerastensorflow 中是否有办法在仅对错误分类的样本赋予额外权重的情况下。 IE。类权重和样本权重的组合,但仅将样本权重应用于二元类中的一个结果?

【问题讨论】:

  • 您希望这些权重在每个时期单独计算还是在整个训练过程中固定?
  • 不管怎样。据我了解,它们通常在每批后更新?
  • 有可能。向我们提供您使用的损失和您想要计算权重的方式 - 这样我们就可以为您提供实施。
  • 我只是使用keras的二元交叉熵函数。

标签: tensorflow machine-learning neural-network keras weighted


【解决方案1】:

是的,这是可能的。您可以在下面找到一个示例,说明如何在 true positivesfalse positivestruenegatives 等上添加额外权重:

def reweight(y_true, y_pred, tp_weight=0.2, tn_weight=0.2, fp_weight=1.2, fn_weight=1.2):
    # Get predictions
    y_pred_classes = K.greater_equal(y_pred, 0.5)
    y_pred_classes_float = K.cast(y_pred_classes, K.floatx())

    # Get misclassified examples
    wrongly_classified = K.not_equal(y_true, y_pred_classes_float)
    wrongly_classified_float = K.cast(wrongly_classified, K.floatx())

    # Get correctly classified examples
    correctly_classified = K.equal(y_true, y_pred_classes_float)
    correctly_classified_float = K.cast(wrongly_classified, K.floatx())

    # Get tp, fp, tn, fn
    tp = correctly_classified_float * y_true
    tn = correctly_classified_float * (1 - y_true)
    fp = wrongly_classified_float * y_true
    fn = wrongly_classified_float * (1 - y_true)

    # Get weights
    weight_tensor = tp_weight * tp + fp_weight * fp + tn_weight * tn + fn_weight * fn

    loss = K.binary_crossentropy(y_true, y_pred)
    weighted_loss = loss * weight_tensor
    return weighted_loss

【讨论】:

  • 非常有趣,但是如果分类不正确,我怎样才能传入带有单个权重的向量作为对每个样本的惩罚?
  • 但是这个函数计算错误分类样本的索引——所以你不需要传递一个。你想手动选择你想惩罚更多的例子吗?
  • 不,只是分类错误的需要更多的惩罚。 IE。有一个收益是+1(正确分类),或者是-x(错误分类)。我在这里对问题的描述略有不同:stats.stackexchange.com/questions/327763/…。我假设需要将权重向量传递给损失函数。
  • tn = .. *(1-y_true) 是否正确。是不是应该... * K.equal(y_true, False)?类似于我在这里的描述:stackoverflow.com/questions/48744092/…
  • 这很好。目标是float32,所以可以使用这种启发式方法。顺便提一句。 False 等于 0。
猜你喜欢
  • 1970-01-01
  • 2018-11-28
  • 2017-01-17
  • 1970-01-01
  • 2021-03-19
  • 2020-08-27
  • 2019-07-13
  • 1970-01-01
  • 2018-06-06
相关资源
最近更新 更多