評価保存モデルの構成
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
TensorFlow Model Analysis (TFMA) は、モデルの評価グラフを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_savedmodel
のserving_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 Estimators 、評価にはTensorFlow Model Analysisと Jupyter、そしてサービスにはTensorFlow Servingを備えた広範なエンドツーエンドの例をお試しください。
エクスポート後のカスタム指標の追加
TFMA に独自のカスタム ポスト エクスポート メトリックを追加したい場合は、こちらのドキュメントを確認してください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-25 UTC。
[null,null,["最終更新日 2025-07-25 UTC。"],[],[],null,["# Configuring an Eval Saved Model\n\n\u003cbr /\u003e\n\nTensorFlow Model Analysis (TFMA) can export a model's *evaluation graph* to a\nspecial `SavedModel` called `EvalSavedModel`. (Note that the evaluation graph is\nused and not the graph for training or inference.) The `EvalSavedModel` contains\nadditional information that allows TFMA to compute the same evaluation metrics\ndefined in the model in a distributed manner over a large amount of data and\nuser-defined slices.\n\nModify an existing model\n------------------------\n\nTo use an existing model with TFMA, first modify the model to export the\n`EvalSavedModel`. This is done by adding a call to\n`tfma.export.export_eval_savedmodel` and is similar to\n`estimator.export_savedmodel`. For example: \n\n # Define, train and export your estimator as usual\n estimator = tf.estimator.DNNClassifier(...)\n estimator.train(...)\n estimator.export_savedmodel(...)\n\n # Also export the EvalSavedModel\n tfma.export.export_eval_savedmodel(\n estimator=estimator, export_dir_base=export_dir,\n eval_input_receiver_fn=eval_input_receiver_fn)\n\n`eval_input_receiver_fn` must be defined and is similar to the\n`serving_input_receiver_fn` for `estimator.export_savedmodel`. Like\n`serving_input_receiver_fn`, the `eval_input_receiver_fn` function defines an\ninput placeholder example, parses the features from the example, and returns the\nparsed features. It parses and returns the label.\n\nThe following snippet defines an example `eval_input_receiver_fn`: \n\n country = tf.feature_column.categorical_column_with_hash('country', 100)\n language = tf.feature_column.categorical_column_with_hash('language', 100)\n age = tf.feature_column.numeric_column('age')\n label = tf.feature_column.numeric_column('label')\n\n def eval_input_receiver_fn():\n serialized_tf_example = tf.compat.v1.placeholder(\n dtype=tf.string, shape=[None], name='input_example_placeholder')\n\n # This *must* be a dictionary containing a single key 'examples', which\n # points to the input placeholder.\n receiver_tensors = {'examples': serialized_tf_example}\n\n feature_spec = tf.feature_column.make_parse_example_spec(\n [country, language, age, label])\n features = tf.io.parse_example(serialized_tf_example, feature_spec)\n\n return tfma.export.EvalInputReceiver(\n features=features,\n receiver_tensors=receiver_tensors,\n labels=features['label'])\n\nIn this example you can see that:\n\n- `labels` can also be a dictionary. Useful for a multi-headed model.\n- The `eval_input_receiver_fn` function will, most likely, be the same as your `serving_input_receiver_fn` function. But, in some cases, you may want to define additional features for slicing. For example, you introduce an `age_category` feature which divides the `age` feature into multiple buckets. You can then slice on this feature in TFMA to help understand how your model's performance differs across different age categories.\n\nAdding Post Export Metrics\n--------------------------\n\nAdditional metrics that are not included in the model can be aded using\n`add_metrics_callbacks`. For more details, see the Python help for\n`run_model_analysis`.\n\nEnd-to-end examples\n-------------------\n\nTry the extensive\n[end-to-end example](https://github.com/tensorflow/tfx/tree/master/tfx/examples/chicago_taxi_pipeline)\nfeaturing [TensorFlow Transform](https://github.com/tensorflow/transform) for\nfeature preprocessing,\n[TensorFlow Estimators](https://www.tensorflow.org/guide/estimators) for\ntraining,\n[TensorFlow Model Analysis](https://github.com/tensorflow/model-analysis) and\nJupyter for evaluation, and\n[TensorFlow Serving](https://github.com/tensorflow/serving) for serving.\n\nAdding a Custom Post Export Metric\n----------------------------------\n\nIf you want to add your own custom post export metric in TFMA, please checkout\nthe documentation\n[here](https://github.com/tensorflow/model-analysis/blob/master/g3doc/post_export_metrics.md)."]]