TensorFlow 모델 분석

TensorFlow Extended(TFX)의 핵심 구성 요소의 예

TensorFlow 모델 분석 (TFMA)는 데이터 조각의 다른 모델에 걸쳐 평가를 수행하는 라이브러리이다. TFMA 사용하여 대용량의 데이터를 통해 분산 된 방식으로도 계산을 수행 아파치 보를 .

이 예제 colab 노트북은 TFMA를 사용하여 데이터 세트의 특성과 관련하여 모델의 성능을 조사하고 시각화하는 방법을 보여줍니다. 우리는 이전에 훈련한 모델을 사용할 것이고 이제 결과를 가지고 놀 수 있습니다! 우리는 훈련이 모델은 위해이었다 시카고 택시 예 용도, 택시는 데이터 세트 여행 시카고시에서 발표합니다. 에서 전체 데이터 세트 탐색 BigQuery의 UI를 .

모델러이자 개발자로서 이 데이터가 어떻게 사용되는지, 모델 예측이 야기할 수 있는 잠재적인 이점과 피해에 대해 생각해 보십시오. 이와 같은 모델은 사회적 편견과 격차를 강화할 수 있습니다. 기능이 해결하려는 문제와 관련이 있습니까? 아니면 편향이 발생합니까? 자세한 내용은 읽어 ML의 공정성 .

데이터세트의 열은 다음과 같습니다.

픽업_커뮤니티_영역 요금 trip_start_month
trip_start_hour trip_start_day trip_start_timestamp
픽업 위도 픽업_경도 dropoff_latitude
dropoff_longitude trip_miles 픽업_센서스_트랙
dropoff_census_tract 지불 유형 회사
trip_seconds dropoff_community_area

Jupyter 확장 설치

jupyter nbextension enable --py widgetsnbextension --sys-prefix 
jupyter nbextension install --py --symlink tensorflow_model_analysis --sys-prefix 
jupyter nbextension enable --py tensorflow_model_analysis --sys-prefix 

TensorFlow 모델 분석(TFMA) 설치

이것은 모든 종속성을 가져오며 1분 정도 걸립니다.

# Upgrade pip to the latest, and install TFMA.
pip install -U pip
pip install tensorflow-model-analysis

이제 아래 셀을 실행하기 전에 런타임을 다시 시작해야 합니다.

# This setup was tested with TF 2.5 and TFMA 0.31 (using colab), but it should
# also work with the latest release.
import sys

# Confirm that we're using Python 3
assert sys.version_info.major==3, 'This notebook must be run using Python 3.'

import tensorflow as tf
print('TF version: {}'.format(tf.__version__))
import apache_beam as beam
print('Beam version: {}'.format(beam.__version__))
import tensorflow_model_analysis as tfma
print('TFMA version: {}'.format(tfma.__version__))
TF version: 2.4.4
Beam version: 2.34.0
TFMA version: 0.29.0

파일 로드

필요한 모든 것이 포함된 tar 파일을 다운로드합니다. 그것은 포함:

  • 훈련 및 평가 데이터세트
  • 데이터 스키마
  • 저장된 모델(keras 및 estimator)과 저장된 모델(estimator)을 교육하고 제공합니다.
# Download the tar file from GCP and extract it
import io, os, tempfile
TAR_NAME = 'saved_models-2.2'
BASE_DIR = tempfile.mkdtemp()
DATA_DIR = os.path.join(BASE_DIR, TAR_NAME, 'data')
MODELS_DIR = os.path.join(BASE_DIR, TAR_NAME, 'models')
SCHEMA = os.path.join(BASE_DIR, TAR_NAME, 'schema.pbtxt')
OUTPUT_DIR = os.path.join(BASE_DIR, 'output')

!curl -O https://storage.googleapis.com/artifacts.tfx-oss-public.appspot.com/datasets/{TAR_NAME}.tar
!tar xf {TAR_NAME}.tar
!mv {TAR_NAME} {BASE_DIR}
!rm {TAR_NAME}.tar

print("Here's what we downloaded:")
!ls -R {BASE_DIR}
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 6800k  100 6800k    0     0  28.2M      0 --:--:-- --:--:-- --:--:-- 28.2M
Here's what we downloaded:
/tmp/tmp_at9q62d:
saved_models-2.2

/tmp/tmp_at9q62d/saved_models-2.2:
data  models  schema.pbtxt

/tmp/tmp_at9q62d/saved_models-2.2/data:
eval  train

/tmp/tmp_at9q62d/saved_models-2.2/data/eval:
data.csv

/tmp/tmp_at9q62d/saved_models-2.2/data/train:
data.csv

/tmp/tmp_at9q62d/saved_models-2.2/models:
estimator  keras

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator:
eval_model_dir  serving_model_dir

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/eval_model_dir:
1591221811

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/eval_model_dir/1591221811:
saved_model.pb  tmp.pbtxt  variables

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/eval_model_dir/1591221811/variables:
variables.data-00000-of-00001  variables.index

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/serving_model_dir:
checkpoint
eval_chicago-taxi-eval
events.out.tfevents.1591221780.my-pipeline-b57vp-237544850
export
graph.pbtxt
model.ckpt-100.data-00000-of-00001
model.ckpt-100.index
model.ckpt-100.meta

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/serving_model_dir/eval_chicago-taxi-eval:
events.out.tfevents.1591221799.my-pipeline-b57vp-237544850

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/serving_model_dir/export:
chicago-taxi

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/serving_model_dir/export/chicago-taxi:
1591221801

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/serving_model_dir/export/chicago-taxi/1591221801:
saved_model.pb  variables

/tmp/tmp_at9q62d/saved_models-2.2/models/estimator/serving_model_dir/export/chicago-taxi/1591221801/variables:
variables.data-00000-of-00001  variables.index

/tmp/tmp_at9q62d/saved_models-2.2/models/keras:
0  1  2

/tmp/tmp_at9q62d/saved_models-2.2/models/keras/0:
saved_model.pb  variables

/tmp/tmp_at9q62d/saved_models-2.2/models/keras/0/variables:
variables.data-00000-of-00001  variables.index

/tmp/tmp_at9q62d/saved_models-2.2/models/keras/1:
saved_model.pb  variables

/tmp/tmp_at9q62d/saved_models-2.2/models/keras/1/variables:
variables.data-00000-of-00001  variables.index

/tmp/tmp_at9q62d/saved_models-2.2/models/keras/2:
saved_model.pb  variables

/tmp/tmp_at9q62d/saved_models-2.2/models/keras/2/variables:
variables.data-00000-of-00001  variables.index

스키마 구문 분석

우리는 다운로드 무엇보다도 의해 만들어진 우리의 데이터에 대한 스키마했다 TensorFlow 데이터 유효성 검사 . 이제 TFMA와 함께 사용할 수 있도록 구문 분석해 보겠습니다.

import tensorflow as tf
from google.protobuf import text_format
from tensorflow.python.lib.io import file_io
from tensorflow_metadata.proto.v0 import schema_pb2
from tensorflow.core.example import example_pb2

schema = schema_pb2.Schema()
contents = file_io.read_file_to_string(SCHEMA)
schema = text_format.Parse(contents, schema)

스키마를 사용하여 TFRecord 생성

TFMA에 데이터 세트에 대한 액세스 권한을 부여해야 하므로 TFRecords 파일을 생성해 보겠습니다. 각 기능에 대한 올바른 유형을 제공하기 때문에 스키마를 사용하여 생성할 수 있습니다.

import csv

datafile = os.path.join(DATA_DIR, 'eval', 'data.csv')
reader = csv.DictReader(open(datafile, 'r'))
examples = []
for line in reader:
  example = example_pb2.Example()
  for feature in schema.feature:
    key = feature.name
    if feature.type == schema_pb2.FLOAT:
      example.features.feature[key].float_list.value[:] = (
          [float(line[key])] if len(line[key]) > 0 else [])
    elif feature.type == schema_pb2.INT:
      example.features.feature[key].int64_list.value[:] = (
          [int(line[key])] if len(line[key]) > 0 else [])
    elif feature.type == schema_pb2.BYTES:
      example.features.feature[key].bytes_list.value[:] = (
          [line[key].encode('utf8')] if len(line[key]) > 0 else [])
  # Add a new column 'big_tipper' that indicates if tips was > 20% of the fare. 
  # TODO(b/157064428): Remove after label transformation is supported for Keras.
  big_tipper = float(line['tips']) > float(line['fare']) * 0.2
  example.features.feature['big_tipper'].float_list.value[:] = [big_tipper]
  examples.append(example)

tfrecord_file = os.path.join(BASE_DIR, 'train_data.rio')
with tf.io.TFRecordWriter(tfrecord_file) as writer:
  for example in examples:
    writer.write(example.SerializeToString())

!ls {tfrecord_file}
/tmp/tmp_at9q62d/train_data.rio

TFMA 설정 및 실행

TFMA는 TF 케라스 모델, 일반 TF2 서명 API 기반 모델, TF 추정기 기반 모델을 비롯한 다양한 모델 유형을 지원합니다. get_started 가이드는 모델 지원되는 유형 및 제한 사항의 전체 목록이 있습니다. 예를 들어 우리는로 저장 한 추정 기반 모델뿐만 아니라 keras을 기반 모델을 구성하는 방법을 보여 가고있다 EvalSavedModel . 참고 항목 자주 묻는 질문 다른 구성의 예를 들어.

TFMA는 모델이 TFMA 구성 설정의 일부로 저장된 후 정의된 메트릭뿐만 아니라 훈련 시간에 사용된 메트릭(즉, 기본 제공 메트릭) 계산에 대한 지원을 제공합니다. 우리 keras 위해 설치 우리가합니다 (참조 우리의 구성의 한 부분으로 수동으로 우리의 측정 및 플롯을 추가 선보일 예정 메트릭 지원되는 메트릭 및 플롯에 대한 정보 가이드). 추정기 설정을 위해 모델과 함께 저장된 기본 제공 메트릭을 사용합니다. 우리의 설정에는 다음 섹션에서 더 자세히 논의되는 여러 슬라이싱 사양도 포함됩니다.

생성 한 후 tfma.EvalConfigtfma.EvalSharedModel 우리는 사용 TFMA를 실행할 수 있습니다 tfma.run_model_analysis . 이것은 만듭니다 tfma.EvalResult 우리는 우리의 통계 및 그래프를 렌더링 나중에 사용할 수 있습니다.

케라스

import tensorflow_model_analysis as tfma

# Setup tfma.EvalConfig settings
keras_eval_config = text_format.Parse("""
  ## Model information
  model_specs {
    # For keras (and serving models) we need to add a `label_key`.
    label_key: "big_tipper"
  }

  ## Post training metric information. These will be merged with any built-in
  ## metrics from training.
  metrics_specs {
    metrics { class_name: "ExampleCount" }
    metrics { class_name: "BinaryAccuracy" }
    metrics { class_name: "BinaryCrossentropy" }
    metrics { class_name: "AUC" }
    metrics { class_name: "AUCPrecisionRecall" }
    metrics { class_name: "Precision" }
    metrics { class_name: "Recall" }
    metrics { class_name: "MeanLabel" }
    metrics { class_name: "MeanPrediction" }
    metrics { class_name: "Calibration" }
    metrics { class_name: "CalibrationPlot" }
    metrics { class_name: "ConfusionMatrixPlot" }
    # ... add additional metrics and plots ...
  }

  ## Slicing information
  slicing_specs {}  # overall slice
  slicing_specs {
    feature_keys: ["trip_start_hour"]
  }
  slicing_specs {
    feature_keys: ["trip_start_day"]
  }
  slicing_specs {
    feature_values: {
      key: "trip_start_month"
      value: "1"
    }
  }
  slicing_specs {
    feature_keys: ["trip_start_hour", "trip_start_day"]
  }
""", tfma.EvalConfig())

# Create a tfma.EvalSharedModel that points at our keras model.
keras_model_path = os.path.join(MODELS_DIR, 'keras', '2')
keras_eval_shared_model = tfma.default_eval_shared_model(
    eval_saved_model_path=keras_model_path,
    eval_config=keras_eval_config)

keras_output_path = os.path.join(OUTPUT_DIR, 'keras')

# Run TFMA
keras_eval_result = tfma.run_model_analysis(
    eval_shared_model=keras_eval_shared_model,
    eval_config=keras_eval_config,
    data_location=tfrecord_file,
    output_path=keras_output_path)
2021-12-04 10:18:15.463173: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcusolver.so.10'; dlerror: libcusolver.so.10: cannot open shared object file: No such file or directory
2021-12-04 10:18:15.464249: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1757] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
WARNING:absl:Tensorflow version (2.4.4) found. Note that TFMA support for TF 2.0 is currently in beta
WARNING:apache_beam.runners.interactive.interactive_environment:Dependencies required for Interactive Beam PCollection visualization are not available, please use: `pip install apache-beam[interactive]` to install necessary dependencies to enable all data visualization features.
WARNING:root:Make sure that locally built Python SDK docker image has Python 3.7 interpreter.
WARNING:apache_beam.io.tfrecordio:Couldn't find python-snappy so the implementation of _TFRecordUtil._masked_crc32c is not as fast as it could be.
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow_model_analysis/writers/metrics_plots_and_validations_writer.py:113: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.
Instructions for updating:
Use eager execution and: 
`tf.data.TFRecordDataset(path)`
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow_model_analysis/writers/metrics_plots_and_validations_writer.py:113: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.
Instructions for updating:
Use eager execution and: 
`tf.data.TFRecordDataset(path)`

평가자

import tensorflow_model_analysis as tfma

# Setup tfma.EvalConfig settings
estimator_eval_config = text_format.Parse("""
  ## Model information
  model_specs {
    # To use EvalSavedModel set `signature_name` to "eval".
    signature_name: "eval"
  }

  ## Post training metric information. These will be merged with any built-in
  ## metrics from training.
  metrics_specs {
    metrics { class_name: "ConfusionMatrixPlot" }
    # ... add additional metrics and plots ...
  }

  ## Slicing information
  slicing_specs {}  # overall slice
  slicing_specs {
    feature_keys: ["trip_start_hour"]
  }
  slicing_specs {
    feature_keys: ["trip_start_day"]
  }
  slicing_specs {
    feature_values: {
      key: "trip_start_month"
      value: "1"
    }
  }
  slicing_specs {
    feature_keys: ["trip_start_hour", "trip_start_day"]
  }
""", tfma.EvalConfig())

# Create a tfma.EvalSharedModel that points at our eval saved model.
estimator_base_model_path = os.path.join(
    MODELS_DIR, 'estimator', 'eval_model_dir')
estimator_model_path = os.path.join(
    estimator_base_model_path, os.listdir(estimator_base_model_path)[0])
estimator_eval_shared_model = tfma.default_eval_shared_model(
    eval_saved_model_path=estimator_model_path,
    eval_config=estimator_eval_config)

estimator_output_path = os.path.join(OUTPUT_DIR, 'estimator')

# Run TFMA
estimator_eval_result = tfma.run_model_analysis(
    eval_shared_model=estimator_eval_shared_model,
    eval_config=estimator_eval_config,
    data_location=tfrecord_file,
    output_path=estimator_output_path)
WARNING:absl:Tensorflow version (2.4.4) found. Note that TFMA support for TF 2.0 is currently in beta
WARNING:root:Make sure that locally built Python SDK docker image has Python 3.7 interpreter.
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow_model_analysis/eval_saved_model/load.py:169: load (from tensorflow.python.saved_model.loader_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.loader.load or tf.compat.v1.saved_model.load. There will be a new function for importing SavedModels in Tensorflow 2.0.
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow_model_analysis/eval_saved_model/load.py:169: load (from tensorflow.python.saved_model.loader_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.loader.load or tf.compat.v1.saved_model.load. There will be a new function for importing SavedModels in Tensorflow 2.0.
INFO:tensorflow:Restoring parameters from /tmp/tmp_at9q62d/saved_models-2.2/models/estimator/eval_model_dir/1591221811/variables/variables
INFO:tensorflow:Restoring parameters from /tmp/tmp_at9q62d/saved_models-2.2/models/estimator/eval_model_dir/1591221811/variables/variables
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow_model_analysis/eval_saved_model/graph_ref.py:189: get_tensor_from_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.utils.get_tensor_from_tensor_info or tf.compat.v1.saved_model.get_tensor_from_tensor_info.
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow_model_analysis/eval_saved_model/graph_ref.py:189: get_tensor_from_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.utils.get_tensor_from_tensor_info or tf.compat.v1.saved_model.get_tensor_from_tensor_info.

메트릭 및 플롯 시각화

이제 평가를 실행했으므로 TFMA를 사용한 시각화를 살펴보겠습니다. 다음 예에서는 keras 모델에서 평가를 실행한 결과를 시각화합니다. 추 기반 모델 업데이트를보기에 eval_result 우리에 지점을 estimator_eval_result 변수입니다.

eval_result = keras_eval_result
# eval_result = estimator_eval_result

렌더링 메트릭

보기 측정하는 방법이 사용 tfma.view.render_slicing_metrics

기본적으로 뷰가 표시됩니다 Overall 조각. 특정 슬라이스를 보려면 당신도 (설정에 의한 컬럼의 이름을 사용할 수 있습니다 slicing_column ) 또는 제공 tfma.SlicingSpec .

메트릭 시각화는 다음 상호 작용을 지원합니다.

  • 클릭하고 드래그하여 이동
  • 스크롤하여 확대/축소
  • 보기를 재설정하려면 마우스 오른쪽 버튼을 클릭하세요.
  • 자세한 내용을 보려면 원하는 데이터 포인트 위로 마우스를 가져갑니다.
  • 하단의 선택 항목을 사용하여 4가지 유형의 보기 중에서 선택합니다.

예를 들어, 우리는 설정됩니다 slicing_column 상기 볼 trip_start_hour 이전에서 기능 slicing_specs .

tfma.view.render_slicing_metrics(eval_result, slicing_column='trip_start_hour')
SlicingMetricsViewer(config={'weightedExamplesColumn': 'example_count'}, data=[{'slice': 'trip_start_hour:2', …

슬라이스 개요

조각의 수가 적은 경우 기본 구상은 조각 개요입니다. 각 조각에 대한 메트릭 값을 보여줍니다. 우리가 선택한 이후 trip_start_hour 위, 그것은 우리에게 우리가 약간의 시간이 아닌 다른 사람에게 특정 문제를 찾을 수 있도록 각시, 정확성 및 AUC 등의 통계를 보여주는 것.

위의 시각화에서:

  • 우리 인 기능 열 정렬 시도 trip_start_hours 열 머리글을 클릭하여, 기능을
  • 예제와 함께 시간 중 일부에 대한 정밀도가 0 있다는 문제가 있음을 나타낼 수있는 정밀도로 정렬 시도하고 통지

차트를 사용하면 조각에서 다양한 측정항목을 선택하고 표시할 수도 있습니다.

  • '표시' 메뉴에서 다른 측정항목을 선택하세요.
  • "연락처보기"메뉴에서 리콜을 선택하십시오, 통지 예제와 함께 시간의 일부에 대한 리콜에 문제가 있음을 나타낼 수 0이라고

더 적은 수의 예제 또는 "가중치"가 있는 슬라이스를 필터링하기 위해 임계값을 설정할 수도 있습니다. 최소한의 예를 입력하거나 슬라이더를 사용할 수 있습니다.

메트릭 히스토그램

이보기는 조각의 개수가 많은 경우에도 기본보기입니다 대안 시각화와 같은 통계 히스토그램을 지원합니다. 결과는 버킷으로 나뉘며 슬라이스 수/총 가중치/둘 모두를 시각화할 수 있습니다. 열 머리글을 클릭하여 열을 정렬할 수 있습니다. 임계값을 설정하여 가중치가 작은 조각을 필터링할 수 있습니다. 회색 밴드를 드래그하여 추가 필터링을 적용할 수 있습니다. 범위를 재설정하려면 밴드를 두 번 클릭합니다. 필터링을 사용하여 시각화 및 메트릭 테이블에서 이상값을 제거할 수도 있습니다. 선형 눈금 대신 대수 눈금으로 전환하려면 톱니바퀴 아이콘을 클릭합니다.

  • 시각화 메뉴에서 "메트릭 히스토그램"을 선택하십시오.

더 많은 조각

우리의 초기 tfma.EvalConfig 의 전체 목록을 만들어 slicing_specs 우리가 통과 업데이트 슬라이스 정보에 의해 시각화 할 수 있습니다, tfma.view.render_slicing_metrics . 여기에서 우리는 선택하겠습니다 trip_start_day 조각 (요일을). 변화 시도 trip_start_daytrip_start_month 다른 조각을 검사하기 위해 다시 렌더링.

tfma.view.render_slicing_metrics(eval_result, slicing_column='trip_start_day')
SlicingMetricsViewer(config={'weightedExamplesColumn': 'example_count'}, data=[{'slice': 'trip_start_day:3', '…

TFMA는 또한 기능 조합을 분석하기 위해 기능 교차 생성을 지원합니다. 우리의 원래 설정은 크로스 만들어 trip_start_hourtrip_start_day :

tfma.view.render_slicing_metrics(
    eval_result,
    slicing_spec=tfma.SlicingSpec(
        feature_keys=['trip_start_hour', 'trip_start_day']))
SlicingMetricsViewer(config={'weightedExamplesColumn': 'example_count'}, data=[{'slice': 'trip_start_day_X_tri…

두 열을 교차하면 많은 조합이 생성됩니다! 의 정오에 시작 여행에서만 보면 우리의 십자가를 좁혀 보자. 그럼 선택할 수 binary_accuracy 시각화에서를 :

tfma.view.render_slicing_metrics(
    eval_result,
    slicing_spec=tfma.SlicingSpec(
        feature_keys=['trip_start_day'], feature_values={'trip_start_hour': '12'}))
SlicingMetricsViewer(config={'weightedExamplesColumn': 'example_count'}, data=[{'slice': 'trip_start_day_X_tri…

렌더링 플롯

받는 추가 된 플롯 tfma.EvalConfig 후 교육으로 metric_specs 사용하여 표시 할 수 있습니다 tfma.view.render_plot .

메트릭과 마찬가지로 플롯은 슬라이스로 볼 수 있습니다. 측정과는 달리, 그래서 특정 슬라이스 값 만 플롯을 표시 할 수 tfma.SlicingSpec 사용되어야하며 슬라이스 기능의 이름과 값을 모두 지정해야합니다. 어떤 조각이 후 제공되지 않는 경우에 대한 플롯 Overall 슬라이스가 사용됩니다.

이 예에서 우리는 표시되어 아래 CalibrationPlotConfusionMatrixPlot 에 대해 계산 된 플롯 trip_start_hour:1 조각.

tfma.view.render_plot(
    eval_result,
    tfma.SlicingSpec(feature_values={'trip_start_hour': '1'}))
PlotViewer(config={'sliceName': 'trip_start_hour:1', 'metricKeys': {'calibrationPlot': {'metricName': 'calibra…

시간 경과에 따른 모델 성능 추적

훈련 데이터 세트는 모델 훈련에 사용되며 테스트 데이터 세트와 프로덕션에서 모델로 전송될 데이터를 대표할 것입니다. 그러나 추론 요청의 데이터는 훈련 데이터와 동일하게 유지될 수 있지만 많은 경우 모델의 성능이 변경될 정도로 충분히 변경되기 시작합니다.

즉, 변경 사항을 인식하고 대응할 수 있도록 지속적으로 모델의 성능을 모니터링하고 측정해야 합니다. TFMA가 어떻게 도움이 되는지 살펴보겠습니다.

그들이 사용하는 비교 방식을하자 부하 3 가지 모델 실행 및 사용 TFMA 확인합니다 render_time_series .

# Note this re-uses the EvalConfig from the keras setup.

# Run eval on each saved model
output_paths = []
for i in range(3):
  # Create a tfma.EvalSharedModel that points at our saved model.
  eval_shared_model = tfma.default_eval_shared_model(
      eval_saved_model_path=os.path.join(MODELS_DIR, 'keras', str(i)),
      eval_config=keras_eval_config)

  output_path = os.path.join(OUTPUT_DIR, 'time_series', str(i))
  output_paths.append(output_path)

  # Run TFMA
  tfma.run_model_analysis(eval_shared_model=eval_shared_model,
                          eval_config=keras_eval_config,
                          data_location=tfrecord_file,
                          output_path=output_path)
WARNING:absl:Tensorflow version (2.4.4) found. Note that TFMA support for TF 2.0 is currently in beta
WARNING:root:Make sure that locally built Python SDK docker image has Python 3.7 interpreter.
WARNING:absl:Tensorflow version (2.4.4) found. Note that TFMA support for TF 2.0 is currently in beta
WARNING:root:Make sure that locally built Python SDK docker image has Python 3.7 interpreter.
WARNING:absl:Tensorflow version (2.4.4) found. Note that TFMA support for TF 2.0 is currently in beta
WARNING:root:Make sure that locally built Python SDK docker image has Python 3.7 interpreter.

먼저, 어제 모델을 훈련하고 배포했다고 가정하고 이제 오늘 들어오는 새 데이터에서 모델이 어떻게 작동하는지 확인하고 싶습니다. 시각화는 AUC를 표시하여 시작됩니다. UI에서 다음을 수행할 수 있습니다.

  • "측정항목 시리즈 추가" 메뉴를 사용하여 다른 측정항목을 추가합니다.
  • x를 클릭하여 원하지 않는 그래프 닫기
  • 자세한 내용을 보려면 데이터 포인트(그래프에서 선분의 끝) 위로 마우스를 가져갑니다.
eval_results_from_disk = tfma.load_eval_results(output_paths[:2])

tfma.view.render_time_series(eval_results_from_disk)
TimeSeriesViewer(config={'isModelCentric': True}, data=[{'metrics': {'': {'': {'binary_accuracy': {'doubleValu…

이제 다른 날이 지났다고 상상하고 이전 2일과 비교하여 오늘 들어오는 새 데이터가 어떻게 되는지 확인하려고 합니다.

eval_results_from_disk = tfma.load_eval_results(output_paths)

tfma.view.render_time_series(eval_results_from_disk)
TimeSeriesViewer(config={'isModelCentric': True}, data=[{'metrics': {'': {'': {'binary_accuracy': {'doubleValu…

모델 검증

TFMA는 동시에 여러 모델을 평가하도록 구성할 수 있습니다. 일반적으로 이는 기준(예: AUC 등)의 성능 차이가 기준에 상대적인 것을 결정하기 위해 기준(예: 현재 서비스 중인 모델)과 새 모델을 비교하기 위해 수행됩니다. 때 임계 값이 구성되어 TFMA는 생산합니다 tfma.ValidationResult 성능이 expecations 일치하는지 여부를 나타내는 기록을.

후보 모델과 기준 모델을 비교하기 위해 keras 평가를 다시 구성해 보겠습니다. 우리는 또한 설정하여 기준에 대한 후보자의 성능을 검증하는 tmfa.MetricThreshold AUC에 메트릭에.

# Setup tfma.EvalConfig setting
eval_config_with_thresholds = text_format.Parse("""
  ## Model information
  model_specs {
    name: "candidate"
    # For keras we need to add a `label_key`.
    label_key: "big_tipper"
  }
  model_specs {
    name: "baseline"
    # For keras we need to add a `label_key`.
    label_key: "big_tipper"
    is_baseline: true
  }

  ## Post training metric information
  metrics_specs {
    metrics { class_name: "ExampleCount" }
    metrics { class_name: "BinaryAccuracy" }
    metrics { class_name: "BinaryCrossentropy" }
    metrics {
      class_name: "AUC"
      threshold {
        # Ensure that AUC is always > 0.9
        value_threshold {
          lower_bound { value: 0.9 }
        }
        # Ensure that AUC does not drop by more than a small epsilon
        # e.g. (candidate - baseline) > -1e-10 or candidate > baseline - 1e-10
        change_threshold {
          direction: HIGHER_IS_BETTER
          absolute { value: -1e-10 }
        }
      }
    }
    metrics { class_name: "AUCPrecisionRecall" }
    metrics { class_name: "Precision" }
    metrics { class_name: "Recall" }
    metrics { class_name: "MeanLabel" }
    metrics { class_name: "MeanPrediction" }
    metrics { class_name: "Calibration" }
    metrics { class_name: "CalibrationPlot" }
    metrics { class_name: "ConfusionMatrixPlot" }
    # ... add additional metrics and plots ...
  }

  ## Slicing information
  slicing_specs {}  # overall slice
  slicing_specs {
    feature_keys: ["trip_start_hour"]
  }
  slicing_specs {
    feature_keys: ["trip_start_day"]
  }
  slicing_specs {
    feature_keys: ["trip_start_month"]
  }
  slicing_specs {
    feature_keys: ["trip_start_hour", "trip_start_day"]
  }
""", tfma.EvalConfig())

# Create tfma.EvalSharedModels that point at our keras models.
candidate_model_path = os.path.join(MODELS_DIR, 'keras', '2')
baseline_model_path = os.path.join(MODELS_DIR, 'keras', '1')
eval_shared_models = [
  tfma.default_eval_shared_model(
      model_name=tfma.CANDIDATE_KEY,
      eval_saved_model_path=candidate_model_path,
      eval_config=eval_config_with_thresholds),
  tfma.default_eval_shared_model(
      model_name=tfma.BASELINE_KEY,
      eval_saved_model_path=baseline_model_path,
      eval_config=eval_config_with_thresholds),
]

validation_output_path = os.path.join(OUTPUT_DIR, 'validation')

# Run TFMA
eval_result_with_validation = tfma.run_model_analysis(
    eval_shared_models,
    eval_config=eval_config_with_thresholds,
    data_location=tfrecord_file,
    output_path=validation_output_path)
WARNING:absl:Tensorflow version (2.4.4) found. Note that TFMA support for TF 2.0 is currently in beta
WARNING:root:Make sure that locally built Python SDK docker image has Python 3.7 interpreter.

기준선에 대해 하나 이상의 모델로 평가를 실행할 때 TFMA는 평가 중에 계산된 모든 메트릭에 대해 diff 메트릭을 자동으로 추가합니다. 이 메트릭은 해당 메트릭 만에 이름을 따서 명명 _diff 메트릭 이름에 추가.

실행으로 생성된 메트릭을 살펴보겠습니다.

tfma.view.render_time_series(eval_result_with_validation)
TimeSeriesViewer(config={'isModelCentric': True}, data=[{'metrics': {'': {'': {'binary_accuracy': {'doubleValu…

이제 유효성 검사의 결과를 살펴보겠습니다. 우리가 사용하는 검증 결과를 보려면 tfma.load_validator_result . 이 예에서는 AUC가 임계값 미만이기 때문에 검증이 실패합니다.

validation_result = tfma.load_validation_result(validation_output_path)
print(validation_result.validation_ok)
False