Keras 2 API 文档 / 度量 / 基于真/假正例和负例的分类度量

基于真/假阳性和阴性的分类指标

[源代码]

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_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

参数

  • num_thresholds:(可选) 用于离散化 ROC 曲线的阈值数量。值必须大于 1。默认为 200
  • curve:(可选) 指定要计算的曲线名称,'ROC' [默认] 或 'PR' 表示精确率-召回率曲线。
  • summation_method:(可选) 指定使用的 黎曼求和方法。'interpolation' (默认) 对 ROC 应用中点求和方案。对于 PR-AUC,插值(真/假)正例,但不插值精确率(比例)(参见 Davis & Goadrich 2006 论文了解详情);'minoring' 对递增区间应用左求和,对递减区间应用右求和;'majoring' 则相反。
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。
  • thresholds:(可选) 一个浮点值列表或浮点阈值列表/元组,范围在 [0, 1]。如果设置了此参数,则会忽略 num_thresholds 参数。值应在 [0, 1] 范围内。为了正确处理预测值等于 0 或 1 的情况,端点阈值 {-epsilon, 1+epsilon}(对于一个小的正 epsilon 值)将自动包含在内。
  • multi_label:布尔值,指示多标签数据是否应被视为多标签数据(其中 AUC 分别为每个标签计算,然后平均),或者(当为 False 时)数据是否应在 AUC 计算之前展平成单个标签。在后一种情况下,当多标签数据传递给 AUC 时,每个标签-预测对被视为一个单独的数据点。多类数据应设置为 False。
  • num_labels:(可选) 标签数量,当 multi_label 为 True 时使用。如果未指定 num_labels,则在首次调用 update_state 时创建状态变量。
  • label_weights:(可选) 用于计算多标签数据 AUC 的非负权重列表、数组或张量。当 multi_label 为 True 时,在平均各个标签 AUC 以生成多标签 AUC 时,权重将应用于各个标签的 AUC。当 multi_label 为 False 时,它们用于在展平数据上计算混淆矩阵时加权各个标签的预测。请注意,这与 class_weights 不同,后者根据标签的值对示例进行加权,而 label_weights 仅取决于标签在展平之前的索引;因此,label_weights 不应用于多类数据。
  • from_logits:布尔值,指示预测值(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_positivesfalse_positives 的总和。

如果 sample_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

如果设置了 top_k,我们将计算精确率,即一个批次条目中排名前 k 的类别中有多少比例的类别是正确的,并且存在于该条目的标签中。

如果指定了 class_id,我们将通过仅考虑批次中 class_id 高于阈值和/或排名前 k 的预测的条目来计算精确率,并计算其中 class_id 确实是正确标签的比例。

参数

  • thresholds:(可选) 一个浮点值,或一个浮点阈值列表/元组,范围在 [0, 1]。阈值与预测值进行比较,以确定预测的真值(即,高于阈值为 true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。如果未设置 thresholdstop_k,则默认为使用 thresholds=0.5 计算精确率。
  • top_k:(可选) 默认未设置。一个整数值,指定计算精确率时要考虑的排名前 k 的预测。
  • class_id:(可选) 我们想要二元度量的整数类 ID。此 ID 必须在半开区间 [0, num_classes) 内,其中 num_classes 是预测的最后一个维度。
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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_positivesfalse_negatives 的总和。

如果 sample_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

如果设置了 top_k,召回率将计算为批次条目的标签中的一个类别平均有多少比例排名前 k 的预测中。

如果指定了 class_id,我们将通过仅考虑批次中 class_id 在标签中的条目来计算召回率,并计算其中 class_id 高于阈值和/或排名前 k 的预测的比例。

参数

  • thresholds:(可选) 一个浮点值,或一个浮点阈值列表/元组,范围在 [0, 1]。阈值与预测值进行比较,以确定预测的真值(即,高于阈值为 true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。如果未设置 thresholdstop_k,则默认为使用 thresholds=0.5 计算召回率。
  • top_k:(可选) 默认未设置。一个整数值,指定计算召回率时要考虑的排名前 k 的预测。
  • class_id:(可选) 我们想要二元度量的整数类 ID。此 ID 必须在半开区间 [0, num_classes) 内,其中 num_classes 是预测的最后一个维度。
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

参数

  • thresholds:(可选) 一个浮点值,或一个浮点阈值列表/元组,范围在 [0, 1]。阈值与预测值进行比较,以确定预测的真值(即,高于阈值为 true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。默认为 0.5
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

参数

  • thresholds:(可选) 一个浮点值,或一个浮点阈值列表/元组,范围在 [0, 1]。阈值与预测值进行比较,以确定预测的真值(即,高于阈值为 true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。默认为 0.5
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

参数

  • thresholds:(可选) 一个浮点值,或一个浮点阈值列表/元组,范围在 [0, 1]。阈值与预测值进行比较,以确定预测的真值(即,高于阈值为 true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。默认为 0.5
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

参数

  • thresholds:(可选) 一个浮点值,或一个浮点阈值列表/元组,范围在 [0, 1]。阈值与预测值进行比较,以确定预测的真值(即,高于阈值为 true,低于阈值为 false)。如果与设置了 from_logits=True 的损失函数一起使用(即,不对预测应用 sigmoid),则 thresholds 应设置为 0。为每个阈值生成一个度量值。默认为 0.5
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

如果指定了 class_id,我们将通过仅考虑批次中 class_id 高于阈值预测的条目来计算精确率,并计算其中 class_id 确实是正确标签的比例。

参数

  • recall:范围在 [0, 1] 内的标量值。
  • num_thresholds:(可选) 用于匹配给定召回率的阈值数量。默认为 200
  • class_id:(可选) 我们想要二元度量的整数类 ID。此 ID 必须在半开区间 [0, num_classes) 内,其中 num_classes 是预测的最后一个维度。
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

如果指定了 class_id,我们将通过仅考虑批次中 class_id 高于阈值预测的条目来计算精确率,并计算其中 class_id 确实是正确标签的比例。

有关特异度和灵敏度的更多信息,请参阅 此处

参数

  • specificity:范围在 [0, 1] 内的标量值。
  • num_thresholds:(可选) 用于匹配给定特异度的阈值数量。默认为 200
  • class_id:(可选) 我们想要二元度量的整数类 ID。此 ID 必须在半开区间 [0, num_classes) 内,其中 num_classes 是预测的最后一个维度。
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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_weightNone,则权重默认为 1。使用 sample_weight 为 0 来屏蔽值。

如果指定了 class_id,我们将通过仅考虑批次中 class_id 高于阈值预测的条目来计算精确率,并计算其中 class_id 确实是正确标签的比例。

有关特异度和灵敏度的更多信息,请参阅 此处

参数

  • sensitivity:范围在 [0, 1] 内的标量值。
  • num_thresholds:(可选) 用于匹配给定灵敏度的阈值数量。默认为 200
  • class_id:(可选) 我们想要二元度量的整数类 ID。此 ID 必须在半开区间 [0, num_classes) 内,其中 num_classes 是预测的最后一个维度。
  • name:(可选)指标实例的字符串名称。
  • dtype:(可选)指标结果的数据类型。

独立用法

>>> 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()])