gpt4 book ai didi

tensorflow - 如果二元交叉熵猜测不正确,则对样本进行权重

转载 作者:行者123 更新时间:2023-11-30 08:34:01 25 4
gpt4 key购买 nike

如果样本分类不正确,kerastensorflow 有没有办法给样本额外的权重。 IE。类别权重和样本权重的组合,但仅将样本权重应用于二元类别中的结果之一?

最佳答案

是的,这是可能的。您可以在下面找到一个示例,说明如何对真阳性假阳性真阴性等添加额外权重:

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

关于tensorflow - 如果二元交叉熵猜测不正确,则对样本进行权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48720197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com