Tensorflow 모델 분석 모델 검증

컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.

개요

TFMA 값 임계 값과에 따라 변경 임계 값을 설정하여 모델을 검증 지원 지원 메트릭을 .

구성

일반 값 임계값

값 임계값은 해당 메트릭이 하한보다 크거나 상한보다 작은지 확인하여 후보 모델을 제어하는 ​​데 유용합니다. 사용자는 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))

일반 변경 임계값

변경 임계값은 해당 메트릭이 기준 모델의 메트릭보다 크거나 작은지 확인하여 후보 모델을 게이트하는 데 유용합니다. 변화를 측정할 수 있는 방법에는 절대 변화와 상대 변화의 두 가지가 있습니다. Aboslute 변경 후보와 기준 모델의 측정 항목의 값으로서 산출된다 diference, 즉 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 }
      }
    }
  }