總覽
TFMA 支援透過根據支援的指標設定值門檻和變更門檻來驗證模型。
設定
GenericValueThreshold
值門檻適用於透過檢查對應指標是否大於下限和/或小於上限來閘控候選模型。使用者可以設定下限和上限值其中之一或同時設定。如果未設定,下限預設為負無限大,上限則預設為無限大。
import tensorflow_model_analysis as tfma
lower_bound = tfma.GenericValueThreshold(lower_bound={'value':0})
upper_bound = tfma.GenericValueThreshold(upper_bound={'value':1})
lower_upper_bound = tfma.GenericValueThreshold(lower_bound={'value':0},
upper_bound={'value':1))
GenericChangeThreshold
變更門檻適用於透過檢查對應指標是否大於/小於基準模型的指標來閘控候選模型。變更有兩種衡量方式:絕對變更和相對變更。絕對變更計算為候選模型與基準模型指標之間的值差,即 *v_c - v_b*,其中 *v_c* 表示候選指標值,而 *v_b* 表示基準值。相對值是候選模型與基準模型指標之間的相對差異,即 *v_c/v_b*。絕對和相對門檻可以共存,以透過這兩種條件閘控模型。除了設定門檻值之外,使用者還需要設定 MetricDirection。對於值越高越有利的指標 (例如 AUC),請將方向設為 HIGHER_IS_BETTER;對於值越低越有利的指標 (例如損失),請將方向設為 LOWER_IS_BETTER。變更門檻需要與候選模型一起評估基準模型。如需範例,請參閱入門指南。
import tensorflow_model_analysis as tfma
absolute_higher_is_better = tfma.GenericChangeThreshold(absolute={'value':1},
direction=tfma.MetricDirection.HIGHER_IS_BETTER)
absolute_lower_is_better = tfma.GenericChangeThreshold(absolute={'value':1},
direction=tfma.MetricDirection.LOWER_IS_BETTER)
relative_higher_is_better = tfma.GenericChangeThreshold(relative={'value':1},
direction=tfma.MetricDirection.HIGHER_IS_BETTER)
relative_lower_is_better = tfma.GenericChangeThreshold(relative={'value':1},
direction=tfma.MetricDirection.LOWER_IS_BETTER)
absolute_and_relative = tfma.GenericChangeThreshold(relative={'value':1},
absolute={'value':0.2},
direction=tfma.MetricDirection.LOWER_IS_BETTER)
整合在一起
以下範例結合值門檻和變更門檻
import tensorflow_model_analysis as tfma
lower_bound = tfma.GenericValueThreshold(lower_bound={'value':0.7})
relative_higher_is_better =
tfma.GenericChangeThreshold(relative={'value':1.01},
direction=tfma.MetricDirection.HIGHER_IS_BETTER)
auc_threshold = tfma.MetricThreshold(value_threshold=lower_bound,
change_threshold=relative_higher_is_better)
以 proto 格式寫下設定檔可能會更易於閱讀
from google.protobuf import text_format
auc_threshold = text_format.Parse("""
value_threshold { lower_bound { value: 0.6 } }
change_threshold { relative { value: 1.01 } }
""", tfma.MetricThreshold())
MetricThreshold 可以設定為閘控模型訓練時間指標 (EvalSavedModel 或 Keras 儲存模型) 和訓練後指標 (在 TFMA 設定中定義)。對於訓練時間指標,門檻在 tfma.MetricsSpec 中指定。
metrics_spec = tfma.MetricSpec(thresholds={'auc': auc_threshold})
對於訓練後指標,門檻直接在 tfma.MetricConfig 中定義。
metric_config = tfma.MetricConfig(class_name='TotalWeightedExample',
threshold=lower_bound)
以下範例連同 EvalConfig 中的其他設定
# Run in a Jupyter Notebook.
from google.protobuf import text_format
eval_config = text_format.Parse("""
model_specs {
# This assumes a serving model with a "serving_default" signature.
label_key: "label"
example_weight_key: "weight"
}
metrics_spec {
# Training Time metric thresholds
thresholds {
key: "auc"
value: {
value_threshold {
lower_bound { value: 0.7 }
}
change_threshold {
direction: HIGHER_IS_BETTER
absolute { value: -1e-10 }
}
}
}
# Post Training metrics and their thesholds.
metrics {
# This assumes a binary classification model.
class_name: "AUC"
threshold {
value_threshold {
lower_bound { value: 0 }
}
}
}
}
slicing_specs {}
slicing_specs {
feature_keys: ["age"]
}
""", tfma.EvalConfig())
eval_shared_models = [
tfma.default_eval_shared_model(
model_name=tfma.CANDIDATE_KEY,
eval_saved_model_path='/path/to/saved/candiate/model',
eval_config=eval_config),
tfma.default_eval_shared_model(
model_name=tfma.BASELINE_KEY,
eval_saved_model_path='/path/to/saved/baseline/model',
eval_config=eval_config),
]
eval_result = tfma.run_model_analysis(
eval_shared_models,
eval_config=eval_config,
# This assumes your data is a TFRecords file containing records in the
# tf.train.Example format.
data_location="/path/to/file/containing/tfrecords",
output_path="/path/for/output")
tfma.view.render_slicing_metrics(eval_result)
tfma.load_validation_result(output_path)
輸出
除了評估器輸出的指標檔案之外,使用驗證時,還會額外輸出「驗證」檔案。酬載格式為 ValidationResult。如果沒有失敗,輸出會將「validation_ok」設為 True。如果發生失敗,則會提供與相關指標、門檻和觀察到的指標值相關的資訊。以下範例說明「weighted_examle_count」未通過值門檻 (1.5 不小於 1.0,因此失敗)。
validation_ok: False
metric_validations_per_slice {
failures {
metric_key {
name: "weighted_example_count"
model_name: "candidate"
}
metric_threshold {
value_threshold {
upper_bound { value: 1.0 }
}
}
metric_value {
double_value { value: 1.5 }
}
}
}