執行監控工作

執行工作時,Model Monitoring 2.0 會使用目標和基準資料集的資料、計算指標,並可能產生快訊。模型監控 v2 提供隨選工作,可用於臨時監控,或排程工作,用於持續監控。無論您選擇哪個選項,每個工作都是單一批次執行作業。

如要進一步瞭解監控目標和支援的模型,請參閱「Model Monitoring v2 總覽」和「設定模型監控」。

依需求執行工作

執行一次性監控工作。設定目標和基準資料集,以及目標的監控規格。您的設定會覆寫模型監控器定義的任何預設值 (如果有設定的話)。

控制台

  1. 前往 Google Cloud 控制台的「Monitoring」頁面。

    前往「Monitoring」頁面

  2. 按一下要為其執行監控工作的模型監控器。

  3. 在模型監控器詳細資料頁面中,按一下「立即執行」,設定監控工作。

  4. 設定工作,或使用模型監控器中定義的預設值。

  5. 按一下「執行」

Python SDK

from vertexai.resources.preview import ml_monitoring

FEATURE_THRESHOLDS = {
  "culmen_length_mm": 0.001,
  "body_mass_g": 0.002,
}

FEATURE_DRIFT_SPEC=ml_monitoring.spec.DataDriftSpec(
  categorical_metric_type="l_infinity",
  numeric_metric_type="jensen_shannon_divergence",
  default_categorical_alert_threshold=0.001,
  default_numeric_alert_threshold=0.002,
  feature_alert_thresholds=FEATURE_THRESHOLDS,
)

PREDICTION_OUTPUT_DRIFT_SPEC=ml_monitoring.spec.DataDriftSpec(
  categorical_metric_type="l_infinity",
  numeric_metric_type="jensen_shannon_divergence",
  default_categorical_alert_threshold=0.001,
  default_numeric_alert_threshold=0.001,
)

FEATURE_ATTRIBUTION_SPEC=ml_monitoring.spec.FeatureAttributionSpec(
  default_alert_threshold=0.0003,
  feature_alert_thresholds={"cnt_ad_reward":0.0001},
)

EXPLANATION_SPEC=ExplanationSpec(
  parameters=ExplanationParameters(
      {"sampled_shapley_attribution": {"path_count": 2}}
  ),
  metadata=ExplanationMetadata(
      inputs={
          "cnt_ad_reward": ExplanationMetadata.InputMetadata({
              "input_tensor_name": "cnt_ad_reward",
              "encoding": "IDENTITY",
              "modality": "numeric"
          }),
          ...
      },
      ...
  )
)

TRAINING_DATASET=ml_monitoring.spec.MonitoringInput(
  gcs_uri=TRAINING_URI,
  data_format="csv"
)

TARGET_DATASET=ml_monitoring.spec.MonitoringInput(
  table_uri=BIGQUERY_URI
)

model_monitoring_job=my_model_monitor.run(
  display_name=JOB_DISPLAY_NAME,
  baseline_dataset=TRAINING_DATASET,
  target_dataset=TARGET_DATASET,
  tabular_objective_spec=ml_monitoring.spec.TabularObjective(
      # Optional: set to monitor input feature drift.
      feature_drift_spec=FEATURE_DRIFT_SPEC,

      # Optional: set to monitor prediction output drift.
      prediction_output_drift_spec=PREDICTION_OUTPUT_DRIFT_SPEC,

      # Optional: set to monitor feature attribution drift.
      feature_attribution_spec=FEATURE_ATTRIBUTION_SPEC
  ),

  # Optional: additional configurations to override default values.
  explanation_config=EXPLANATION_SPEC,
  notification_spec=NOTIFICATION_SPEC,
  output_spec=OUTPUT_SPEC
)

排定持續執行作業

您可以為模型監控器設定一或多個排程執行作業。如要使用持續監控功能搭配時間指定,資料集必須包含時間戳記資料欄,以便模型監控 v2 從指定的時間範圍中擷取資料。

控制台

  1. 前往 Google Cloud 控制台的「Monitoring」頁面。

    前往「Monitoring」頁面

  2. 按一下要設定持續監控的模型監控器。

  3. 按一下「排定週期性執行作業」

  4. 設定目標和基準資料集,然後按一下「繼續」

  5. 設定要監控的目標、閾值,以及產生快訊時的通知設定。

  6. 按一下「繼續」

  7. 設定週期性工作的時間表:

    1. 指定排程器的名稱。
    2. 在「開始時間」中,指定第一個工作開始的時間。
    3. 針對「頻率」,請使用 Cron 運算式設定頻率,然後設定時區。
    4. 在「結束時間」中,指定排程器的結束時間。
  8. 按一下 [建立]。

Python SDK

如要設定監控工作的頻率,請使用 Cron 運算式

my_model_monitoring_schedule=my_model_monitor.create_schedule(
  display_name=SCHEDULE_DISPLAY_NAME,
  # Every day at 0:00(midnight)
  cron='"0 * * * *"',
  baseline_dataset=ml_monitoring.spec.MonitoringInput(
      endpoints=[ENDPOINT_RESOURCE_NAME],
      offset="24h",
      window="24h",
  ),
  target_dataset=ml_monitoring.spec.MonitoringInput(
      endpoints=[ENDPOINT_RESOURCE_NAME],
      window="24h"
  ),
  tabular_objective_spec=ml_monitoring.spec.TabularObjective(
      # Optional: set to monitor input feature drift.
      feature_drift_spec=FEATURE_DRIFT_SPEC,

      # Optional: set to monitor prediction output drift.
      prediction_output_drift_spec=PREDICTION_OUTPUT_DRIFT_SPEC,

      # Optional: set to monitor feature attribution drift.
      feature_attribution_spec=FEATURE_ATTRIBUTION_SPEC
  ),

  # Optional: additional configurations to override default values.
  explanation_config=EXPLANATION_SPEC,
  output_spec=OUTPUT_SPEC,
  notification_spec=NOTIFICATION_SPEC,
)

暫停或繼續執行排程

您可以暫停及繼續排程,藉此略過或暫時停止監控工作執行作業。

控制台

  1. 前往 Google Cloud 控制台的「Monitoring」頁面。

    前往「Monitoring」頁面

  2. 按一下含有要修改時間表的模型監控器。

  3. 在詳細資料頁面中,前往「時間表」分頁。

  4. 按一下要修改的時間表。

  5. 按一下「暫停」或「繼續」,即可暫停或繼續排程。

Python SDK

# Pause schedule
my_model_monitor.pause_schedule(my_monitoring_schedule.name)

# Resume schedule
my_model_monitor.resume_schedule(my_monitoring_schedule.name)

刪除時間表

如果目前未使用,請刪除排程。現有資料和先前建立的工作都會保留。

控制台

  1. 前往 Google Cloud 控制台的「Monitoring」頁面。

    前往「Monitoring」頁面

  2. 按一下含有要修改的時間表的模型監控器。

  3. 在詳細資料頁面中,前往「時間表」分頁。

  4. 按一下要修改的時間表。

  5. 按一下「刪除」和「刪除」,確認刪除。

Python SDK

my_model_monitor.delete_schedule(my_monitoring_schedule.name)

分析監控工作結果

您可以使用 Google Cloud 控制台,以視覺化方式呈現每個監控目標的資料分布情形,並瞭解哪些變更會導致時間漂移。

視覺化報表會顯示直方圖,比較目標資料和基準資料之間的資料分布情形。例如,您可以根據容忍度決定調整特徵產生管道或重新訓練模型。

查看工作詳細資料

查看監控工作執行的詳細資料,例如受監控功能的清單,以及哪些功能產生快訊。

控制台

  1. 前往 Google Cloud 控制台的「Monitoring」頁面。

    前往「Monitoring」頁面

  2. 按一下包含要分析的工作的模型監控器。

  3. 在「監控詳細資料」頁面上,按一下「執行次數」分頁。

  4. 在執行作業清單中,按一下執行作業即可查看詳細資料,例如執行作業中包含的所有功能。

    以下範例顯示批次預測工作中 country 功能的分布比較。 Google Cloud 主控台也會根據指標提供比較結果的詳細資料,例如不重複值數量、平均值和標準差。

    直方圖:顯示目標資料集和基準資料集的特徵分布範例。

查看功能詳細資料

查看某項功能的相關資訊,以及包含該功能的監控工作清單。

控制台

  1. 前往 Google Cloud 控制台的「Monitoring」頁面。

    前往「Monitoring」頁面

  2. 按一下包含要分析的工作的模型監控器。

  3. 在「總覽」分頁中,您可以查看摘要,其中包含所有監控目標的偏離趨勢 (如果您已設定持續監控)。您也可以深入瞭解特定目標,查看詳細資料,例如監控的功能名稱和監控執行作業清單。

    以下範例顯示「國家/地區」地圖項目的分布比較。在直方圖後方,您可以查看哪些執行作業產生警報,或是選取另一項監控工作,以便監控這項功能的資料。

    直方圖:顯示目標資料集和基準資料集的輸入資料分布範例。