AUC 类tf_keras.metrics.AUC(
num_thresholds=200,
curve="ROC",
summation_method="interpolation",
name=None,
dtype=None,
thresholds=None,
multi_label=False,
num_labels=None,
label_weights=None,
from_logits=False,
)
近似 ROC 或 PR 曲线的 AUC(曲线下面积)。
ROC(接收者操作特征;默认)或 PR(精确率-召回率)曲线的 AUC(曲线下面积)是二元分类器的质量度量。与准确率不同,并且像交叉熵损失函数一样,ROC-AUC 和 PR-AUC 会评估模型的全部操作点。
此类使用黎曼和来近似 AUC。在度量累积阶段,预测值会根据其值被累积到预定义的桶中。然后通过插值每桶的平均值来计算 AUC。这些桶定义了已评估的操作点。
此度量创建四个局部变量:true_positives(真阳性)、true_negatives(真阴性)、false_positives(假阳性)和 false_negatives(假阴性),用于计算 AUC。为了离散化 AUC 曲线,使用一组线性间隔的阈值来计算召回率和精确率值的对。因此,ROC 曲线下的面积是通过将召回率值的高度乘以假阳性率来计算的,而 PR 曲线下的面积是通过将精确率值的高度乘以召回率来计算的。
最终,该值将作为 auc 返回,这是一个幂等操作,它计算精确率与召回率值(使用上述变量计算)离散化曲线下的面积。num_thresholds 变量控制离散化程度,更多的阈值能更精确地近似真实 AUC。近似的质量可能因 num_thresholds 的不同而存在巨大差异。thresholds 参数可用于手动指定更均匀地分割预测的阈值。
为了最佳地近似真实 AUC,predictions 应该近似均匀分布在 [0, 1] 范围内(如果 from_logits=False)。如果不是这种情况,AUC 近似的质量可能会很差。将 summation_method 设置为 'minoring' 或 'majoring' 可以通过提供 AUC 的下限或上限估计来帮助量化近似中的误差。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
参数
200。ROC 应用中点求和方案。对于 PR-AUC,插值(真/假)正例,但不插值精确率(比例)(参见 Davis & Goadrich 2006 论文了解详情);'minoring' 对递增区间应用左求和,对递减区间应用右求和;'majoring' 则相反。num_thresholds 参数。值应在 [0, 1] 范围内。为了正确处理预测值等于 0 或 1 的情况,端点阈值 {-epsilon, 1+epsilon}(对于一个小的正 epsilon 值)将自动包含在内。multi_label 为 True 时使用。如果未指定 num_labels,则在首次调用 update_state 时创建状态变量。multi_label 为 True 时,在平均各个标签 AUC 以生成多标签 AUC 时,权重将应用于各个标签的 AUC。当 multi_label 为 False 时,它们用于在展平数据上计算混淆矩阵时加权各个标签的预测。请注意,这与 class_weights 不同,后者根据标签的值对示例进行加权,而 label_weights 仅取决于标签在展平之前的索引;因此,label_weights 不应用于多类数据。update_state 中的 y_pred)是概率还是 sigmoid logits。作为经验法则,当使用 Keras 损失函数时,损失函数的 from_logits 构造函数参数应与 AUC 的 from_logits 构造函数参数匹配。独立用法
>>> m = tf.keras.metrics.AUC(num_thresholds=3)
>>> m.update_state([0, 0, 1, 1], [0, 0.5, 0.3, 0.9])
>>> # threshold values are [0 - 1e-7, 0.5, 1 + 1e-7]
>>> # tp = [2, 1, 0], fp = [2, 0, 0], fn = [0, 1, 2], tn = [0, 2, 2]
>>> # tp_rate = recall = [1, 0.5, 0], fp_rate = [1, 0, 0]
>>> # auc = ((((1+0.5)/2)*(1-0)) + (((0.5+0)/2)*(0-0))) = 0.75
>>> m.result().numpy()
0.75
>>> m.reset_state()
>>> m.update_state([0, 0, 1, 1], [0, 0.5, 0.3, 0.9],
... sample_weight=[1, 0, 0, 1])
>>> m.result().numpy()
1.0
与 compile() API 一起使用
# Reports the AUC of a model outputting a probability.
model.compile(optimizer='sgd',
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=[tf.keras.metrics.AUC()])
# Reports the AUC of a model outputting a logit.
model.compile(optimizer='sgd',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.AUC(from_logits=True)])
Precision 类tf_keras.metrics.Precision(
thresholds=None, top_k=None, class_id=None, name=None, dtype=None
)
计算预测相对于标签的精确率。
该度量创建两个局部变量:true_positives(真阳性)和 false_positives(假阳性),用于计算精确率。最终,该值作为 precision 返回,这是一个幂等操作,它简单地将 true_positives 除以 true_positives 和 false_positives 的总和。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
如果设置了 top_k,我们将计算精确率,即一个批次条目中排名前 k 的类别中有多少比例的类别是正确的,并且存在于该条目的标签中。
如果指定了 class_id,我们将通过仅考虑批次中 class_id 高于阈值和/或排名前 k 的预测的条目来计算精确率,并计算其中 class_id 确实是正确标签的比例。
参数
true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。如果未设置 thresholds 或 top_k,则默认为使用 thresholds=0.5 计算精确率。[0, num_classes) 内,其中 num_classes 是预测的最后一个维度。独立用法
>>> m = tf.keras.metrics.Precision()
>>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
>>> m.result().numpy()
0.6666667
>>> m.reset_state()
>>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
>>> m.result().numpy()
1.0
>>> # With top_k=2, it will calculate precision over y_true[:2]
>>> # and y_pred[:2]
>>> m = tf.keras.metrics.Precision(top_k=2)
>>> m.update_state([0, 0, 1, 1], [1, 1, 1, 1])
>>> m.result().numpy()
0.0
>>> # With top_k=4, it will calculate precision over y_true[:4]
>>> # and y_pred[:4]
>>> m = tf.keras.metrics.Precision(top_k=4)
>>> m.update_state([0, 0, 1, 1], [1, 1, 1, 1])
>>> m.result().numpy()
0.5
与 compile() API 一起使用
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.Precision()])
与 from_logits=True 的损失函数一起使用
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.Precision(thresholds=0)])
Recall 类tf_keras.metrics.Recall(
thresholds=None, top_k=None, class_id=None, name=None, dtype=None
)
计算预测相对于标签的召回率。
该度量创建两个局部变量:true_positives(真阳性)和 false_negatives(假阴性),用于计算召回率。最终,该值作为 recall 返回,这是一个幂等操作,它简单地将 true_positives 除以 true_positives 和 false_negatives 的总和。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
如果设置了 top_k,召回率将计算为批次条目的标签中的一个类别平均有多少比例排名前 k 的预测中。
如果指定了 class_id,我们将通过仅考虑批次中 class_id 在标签中的条目来计算召回率,并计算其中 class_id 高于阈值和/或排名前 k 的预测的比例。
参数
true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。如果未设置 thresholds 或 top_k,则默认为使用 thresholds=0.5 计算召回率。[0, num_classes) 内,其中 num_classes 是预测的最后一个维度。独立用法
>>> m = tf.keras.metrics.Recall()
>>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
>>> m.result().numpy()
0.6666667
>>> m.reset_state()
>>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
>>> m.result().numpy()
1.0
与 compile() API 一起使用
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.Recall()])
与 from_logits=True 的损失函数一起使用
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.Recall(thresholds=0)])
TruePositives 类tf_keras.metrics.TruePositives(thresholds=None, name=None, dtype=None)
计算真阳性的数量。
如果提供了 sample_weight,则计算真阳性权重的总和。此度量创建了一个局部变量 true_positives,用于跟踪真阳性的数量。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
参数
true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。默认为 0.5。独立用法
>>> m = tf.keras.metrics.TruePositives()
>>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
>>> m.result().numpy()
2.0
>>> m.reset_state()
>>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
>>> m.result().numpy()
1.0
与 compile() API 一起使用
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.TruePositives()])
与 from_logits=True 的损失函数一起使用
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.TruePositives(thresholds=0)])
TrueNegatives 类tf_keras.metrics.TrueNegatives(thresholds=None, name=None, dtype=None)
计算真阴性的数量。
如果提供了 sample_weight,则计算真阴性权重的总和。此度量创建了一个局部变量 accumulator,用于跟踪真阴性的数量。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
参数
true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。默认为 0.5。独立用法
>>> m = tf.keras.metrics.TrueNegatives()
>>> m.update_state([0, 1, 0, 0], [1, 1, 0, 0])
>>> m.result().numpy()
2.0
>>> m.reset_state()
>>> m.update_state([0, 1, 0, 0], [1, 1, 0, 0], sample_weight=[0, 0, 1, 0])
>>> m.result().numpy()
1.0
与 compile() API 一起使用
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.TrueNegatives()])
与 from_logits=True 的损失函数一起使用
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.TrueNegatives(thresholds=0)])
FalsePositives 类tf_keras.metrics.FalsePositives(thresholds=None, name=None, dtype=None)
计算假阳性的数量。
如果提供了 sample_weight,则计算假阳性权重的总和。此度量创建了一个局部变量 accumulator,用于跟踪假阳性的数量。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
参数
true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。默认为 0.5。独立用法
>>> m = tf.keras.metrics.FalsePositives()
>>> m.update_state([0, 1, 0, 0], [0, 0, 1, 1])
>>> m.result().numpy()
2.0
>>> m.reset_state()
>>> m.update_state([0, 1, 0, 0], [0, 0, 1, 1], sample_weight=[0, 0, 1, 0])
>>> m.result().numpy()
1.0
与 compile() API 一起使用
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.FalsePositives()])
与 from_logits=True 的损失函数一起使用
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.FalsePositives(thresholds=0)])
FalseNegatives 类tf_keras.metrics.FalseNegatives(thresholds=None, name=None, dtype=None)
计算假阴性的数量。
如果提供了 sample_weight,则计算假阴性权重的总和。此度量创建了一个局部变量 accumulator,用于跟踪假阴性的数量。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
参数
true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。默认为 0.5。独立用法
>>> m = tf.keras.metrics.FalseNegatives()
>>> m.update_state([0, 1, 1, 1], [0, 1, 0, 0])
>>> m.result().numpy()
2.0
>>> m.reset_state()
>>> m.update_state([0, 1, 1, 1], [0, 1, 0, 0], sample_weight=[0, 0, 1, 0])
>>> m.result().numpy()
1.0
与 compile() API 一起使用
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.FalseNegatives()])
与 from_logits=True 的损失函数一起使用
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.FalseNegatives(thresholds=0)])
PrecisionAtRecall 类tf_keras.metrics.PrecisionAtRecall(
recall, num_thresholds=200, class_id=None, name=None, dtype=None
)
计算召回率大于等于指定值时的最佳精确率。
此度量创建四个局部变量:true_positives(真阳性)、true_negatives(真阴性)、false_positives(假阳性)和 false_negatives(假阴性),用于计算给定召回率下的精确率。将计算给定召回率值的阈值,并用于评估相应的精确率。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
如果指定了 class_id,我们将通过仅考虑批次中 class_id 高于阈值预测的条目来计算精确率,并计算其中 class_id 确实是正确标签的比例。
参数
[0, 1] 内的标量值。200。[0, num_classes) 内,其中 num_classes 是预测的最后一个维度。独立用法
>>> m = tf.keras.metrics.PrecisionAtRecall(0.5)
>>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
>>> m.result().numpy()
0.5
>>> m.reset_state()
>>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
... sample_weight=[2, 2, 2, 1, 1])
>>> m.result().numpy()
0.33333333
与 compile() API 一起使用
model.compile(
optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.PrecisionAtRecall(recall=0.8)])
SensitivityAtSpecificity 类tf_keras.metrics.SensitivityAtSpecificity(
specificity, num_thresholds=200, class_id=None, name=None, dtype=None
)
计算特异度大于等于指定值时的最佳灵敏度。
给定特异度下的灵敏度。
Sensitivity(灵敏度)衡量正确识别出的实际阳性样本的比例(tp / (tp + fn))。Specificity(特异度)衡量正确识别出的实际阴性样本的比例(tn / (tn + fp))。
此度量创建四个局部变量:true_positives(真阳性)、true_negatives(真阴性)、false_positives(假阳性)和 false_negatives(假阴性),用于计算给定特异度下的灵敏度。将计算给定特异度值的阈值,并用于评估相应的灵敏度。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
如果指定了 class_id,我们将通过仅考虑批次中 class_id 高于阈值预测的条目来计算精确率,并计算其中 class_id 确实是正确标签的比例。
有关特异度和灵敏度的更多信息,请参阅 此处。
参数
[0, 1] 内的标量值。200。[0, num_classes) 内,其中 num_classes 是预测的最后一个维度。独立用法
>>> m = tf.keras.metrics.SensitivityAtSpecificity(0.5)
>>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
>>> m.result().numpy()
0.5
>>> m.reset_state()
>>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
... sample_weight=[1, 1, 2, 2, 1])
>>> m.result().numpy()
0.333333
与 compile() API 一起使用
model.compile(
optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.SensitivityAtSpecificity()])
SpecificityAtSensitivity 类tf_keras.metrics.SpecificityAtSensitivity(
sensitivity, num_thresholds=200, class_id=None, name=None, dtype=None
)
计算灵敏度大于等于指定值时的最佳特异度。
Sensitivity(灵敏度)衡量正确识别出的实际阳性样本的比例(tp / (tp + fn))。Specificity(特异度)衡量正确识别出的实际阴性样本的比例(tn / (tn + fp))。
此度量创建四个局部变量:true_positives(真阳性)、true_negatives(真阴性)、false_positives(假阳性)和 false_negatives(假阴性),用于计算给定灵敏度下的特异度。将计算给定灵敏度值的阈值,并用于评估相应的特异度。
如果 sample_weight 为 None,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。
如果指定了 class_id,我们将通过仅考虑批次中 class_id 高于阈值预测的条目来计算精确率,并计算其中 class_id 确实是正确标签的比例。
有关特异度和灵敏度的更多信息,请参阅 此处。
参数
[0, 1] 内的标量值。200。[0, num_classes) 内,其中 num_classes 是预测的最后一个维度。独立用法
>>> m = tf.keras.metrics.SpecificityAtSensitivity(0.5)
>>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8])
>>> m.result().numpy()
0.66666667
>>> m.reset_state()
>>> m.update_state([0, 0, 0, 1, 1], [0, 0.3, 0.8, 0.3, 0.8],
... sample_weight=[1, 1, 2, 2, 2])
>>> m.result().numpy()
0.5
与 compile() API 一起使用
model.compile(
optimizer='sgd',
loss='binary_crossentropy',
metrics=[tf.keras.metrics.SpecificityAtSensitivity()])