평가 저장된 모델 구성

TFMA(TensorFlow Model Analysis)는 모델의 평가 그래프를 EvalSavedModel 이라는 특별한 SavedModel 로 내보낼 수 있습니다. (훈련이나 추론을 위한 그래프가 아닌 평가 그래프가 사용된다는 점에 유의하세요.) EvalSavedModel 에는 TFMA가 대량의 데이터 및 사용자 정의에 대해 분산 방식으로 모델에 정의된 동일한 평가 지표를 계산할 수 있도록 하는 추가 정보가 포함되어 있습니다. 조각.

기존 모델 수정

TFMA와 함께 기존 모델을 사용하려면 먼저 모델을 수정하여 EvalSavedModel 을 내보냅니다. 이는 tfma.export.export_eval_savedmodel 에 대한 호출을 추가하여 수행되며 estimator.export_savedmodel 과 유사합니다. 예를 들어:

# Define, train and export your estimator as usual
estimator = tf.estimator.DNNClassifier(...)
estimator.train(...)
estimator.export_savedmodel(...)

# Also export the EvalSavedModel
tfma.export.export_eval_savedmodel(
  estimator=estimator, export_dir_base=export_dir,
  eval_input_receiver_fn=eval_input_receiver_fn)

eval_input_receiver_fn 정의해야 하며 이는 estimator.export_savedmodelserving_input_receiver_fn 과 유사합니다. serving_input_receiver_fn 과 마찬가지로 eval_input_receiver_fn 함수는 입력 자리 표시자 예시를 정의하고, 예시의 기능을 구문 분석하고, 구문 분석된 기능을 반환합니다. 레이블을 구문 분석하고 반환합니다.

다음 스니펫은 eval_input_receiver_fn 예제를 정의합니다.

country = tf.feature_column.categorical_column_with_hash('country', 100)
language = tf.feature_column.categorical_column_with_hash('language', 100)
age = tf.feature_column.numeric_column('age')
label = tf.feature_column.numeric_column('label')

def eval_input_receiver_fn():
  serialized_tf_example = tf.compat.v1.placeholder(
      dtype=tf.string, shape=[None], name='input_example_placeholder')

  # This *must* be a dictionary containing a single key 'examples', which
  # points to the input placeholder.
  receiver_tensors = {'examples': serialized_tf_example}

  feature_spec =  tf.feature_column.make_parse_example_spec(
      [country, language, age, label])
  features = tf.io.parse_example(serialized_tf_example, feature_spec)

  return tfma.export.EvalInputReceiver(
    features=features,
    receiver_tensors=receiver_tensors,
    labels=features['label'])

이 예에서는 다음을 볼 수 있습니다.

  • labels 사전일 수도 있습니다. 다중 헤드 모델에 유용합니다.
  • eval_input_receiver_fn 함수는 serving_input_receiver_fn 함수와 동일할 가능성이 높습니다. 그러나 경우에 따라 슬라이싱을 위한 추가 기능을 정의해야 할 수도 있습니다. 예를 들어 age 특성을 여러 버킷으로 나누는 age_category 특성을 도입합니다. 그런 다음 TFMA에서 이 기능을 분석하여 다양한 연령 범주에 걸쳐 모델 성능이 어떻게 다른지 이해하는 데 도움을 받을 수 있습니다.

내보내기 후 측정항목 추가

모델에 포함되지 않은 추가 측정항목은 add_metrics_callbacks 사용하여 추가할 수 있습니다. 자세한 내용은 run_model_analysis 에 대한 Python 도움말을 참조하세요.

엔드투엔드 예시

기능 사전 처리를 위한 TensorFlow Transform , 교육을 위한 TensorFlow Estimator , 평가를 위한 TensorFlow 모델 분석 및 Jupyter, 제공을 위한 TensorFlow Serving을 갖춘 광범위한 엔드투엔드 예제를 사용해 보세요.

사용자 정의 사후 내보내기 측정항목 추가

TFMA에 사용자 정의 사후 내보내기 측정항목을 추가하려면 여기에서 설명서를 확인하세요.