Tensorflowモデル分析モデルの検証

概要

基づいた値のしきい値と変化するしきい値設定することで、モデルを検証TFMAサポートサポート指標を

構成

GenericValueThreshold

値のしきい値は、対応するメトリックが下限よりも大きいか、および/または上限よりも小さいかどうかをチェックすることにより、候補モデルをゲートするのに役立ちます。ユーザーは、lower_bound値とupper_bound値のいずれかまたは両方を設定できます。設定されていない場合、lower_boundはデフォルトで負の無限大になり、設定されていない場合、upper_boundはデフォルトで無限大になります。

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

変更しきい値は、対応するメトリックがベースラインモデルのメトリックよりも大きい/小さいかどうかを確認することにより、候補モデルをゲートするのに役立ちます。変化を測定するには、絶対変化と相対変化の2つの方法があります。 V_B V_C候補メトリック値を示し、V_Bベースライン値である- Aboslute変化が、候補とベースラインモデルの指標、すなわち、V_C間の値diferenceとして算出されます。相対値は、候補のメトリックとベースライン、即ち、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)

設定をプロト形式で書き留めた方が読みやすい場合があります。

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 }
      }
    }
  }