機器學習模型評估
模型評估是機器學習旅程中不可或缺的一部分。它可讓您根據未見過的資料集來基準化模型的效能。您可以擷取選擇的度量、建立視覺化效果、記錄中繼資料,並比較不同模型的效能。在您的 MLOps 生態系統中,模型評估步驟對於監控模型的演進或多個模型在資料集隨著時間推移而增長或變更以及重新訓練模型時至關重要。
Beam 支援使用名為 ExtractEvaluateAndWriteResults 的 PTransform,直接在您的管線內針對 TensorFlow 模型執行模型評估。此 PTransform 是 TensorFlow 模型分析 (TFMA) 的一部分,這是一個用於對不同資料片段執行模型評估的程式庫。TFMA 使用 Beam 以分散式方式對大量資料執行運算,讓您能夠以分散式方式對大量資料評估模型。這些度量會在資料片段之間進行比較,並在 Jupyter 或 Colab 筆記本中視覺化。
TFMA 範例
以下是如何使用 ExtractEvaluateAndWriteResults 評估線性迴歸模型的範例。
首先,定義組態以指定模型資訊、選擇的度量,以及可選的資料片段。
from google.protobuf import text_format
# Define the TFMA evaluation configuration
eval_config = text_format.Parse("""
## Model information
model_specs {
# For keras and serving models, you need to add a `label_key`.
label_key: "output"
}
## This post-training metric information is merged with any built-in
## metrics from training
metrics_specs {
metrics { class_name: "ExampleCount" }
metrics { class_name: "MeanAbsoluteError" }
metrics { class_name: "MeanSquaredError" }
metrics { class_name: "MeanPrediction" }
}
slicing_specs {}
""", tfma.EvalConfig())
然後,建立管線以執行評估
from tfx_bsl.public import tfxio
eval_shared_model = tfma.default_eval_shared_model(
eval_saved_model_path='model_path', eval_config=eval_config)
tfx_io = tfxio.TFExampleRecord(
file_pattern='tfrecords_path',
raw_record_column_name=tfma.ARROW_INPUT_COLUMN)
# Run evaluation
with beam.Pipeline() as pipeline:
_ = (
pipeline
| 'ReadData' >> tfx_io.BeamSource()
| 'EvalModel' >> tfma.ExtractEvaluateAndWriteResults(
eval_shared_model=eval_shared_model,
eval_config=eval_config,
output_path='output_path'))
此管線會將結果 (包括組態檔、度量、繪圖等等) 儲存至選定的 output_path。
TFMA 端對端範例
如需在 Beam 上使用 TFMA 進行模型評估的完整端對端範例,請參閱 tfma_beam 筆記本。
此範例顯示從開放原始碼資料集建立 tfrecord、訓練模型以及在 Beam 中進行評估。
上次更新日期:2024/10/31
您是否已找到您要尋找的所有內容?
一切都實用且清楚嗎?您有任何想要變更的地方嗎?請告訴我們!