模型評估元件

模型評估元件

模型評估元件會將真值和批次預測結果做為輸入內容,並產生評估指標。實際資料是經過人工判定的「正確標籤」資料,通常會以您用來訓練模型的測試資料集中的目標欄位形式提供。詳情請參閱「Vertex AI 的模型評估」。

Vertex AI 提供下列模型評估元件:

支援的模型類型

下表列出各模型評估元件支援的模型類型:

模型評估元件 支援的模型類型
ModelEvaluationClassificationOp
  • AutoML 表格、圖片、文字、影片
  • 自訂表格
  • ModelEvaluationRegressionOp
  • AutoML 表格
  • 自訂表格
  • ModelEvaluationForecastingOp
  • AutoML 表格
  • 移除目標欄位

    對於部分模型類型,BatchPredictionJob 元件會要求您從資料集中排除目標資料欄 (基準真相)。

    設定表格資料集的切片格式

    配量是具有指定特性的表格資料子集。切片指標可為 AutoML 表格和自訂訓練分類模型提供更精細的評估指標。

    舉例來說,假設有一個寵物領養資料集,模型經過訓練後,可預測寵物是否會在一個星期內被領養。雖然查看整個資料集的指標很有用,但您可能對寵物的物種和年齡相關指標感興趣。換句話說,您感興趣的是資料集的下列子集:

    功能
    age (in years) 1、2、3、4、5、6、7、8、9、10、11、12
    species 「dog」、「cat」、「rabbit」

    這項細部分析有助於找出模型最容易出錯的地方,或確保模型在重要資料子集上表現良好。

    設定區隔

    如要設定切片,請建立包含下列設定的 JSON 檔案:

    {
      "FEATURE": {
        "KIND": {
          CONFIG
        }
      }
    }
    

    其中:

    • FEATURE 是您要建立切片的特徵。例如:age

    • KIND 是下列其中一項:

      • value

        value 會針對特徵的單一值建立單一切片。將 CONFIG 指定為鍵/值組合,其中鍵為 float_valuestring_value。例如:"float_value": 1.0

        以下範例設定會建立單一切片,其中包含 age1 的資料:

        {
          "age": {
            "value": {
              "float_value": 1.0
            }
          }
        }
      • range

        range 會建立單一切片,其中包含指定範圍內的資料。 將 CONFIG 指定為兩個鍵/值組合,其中鍵為 lowhigh

        以下範例設定會建立單一資料切片,其中包含 age 介於 13 之間的資料:

        {
          "age": {
            "range": {
              "low": 1,
              "high": 3
            }
          }
        }
      • all_values

        all_values 會為特徵的每個可能標籤建立個別切片。將 CONFIG 指定為 "value": true

        下列範例設定會建立三個切片,分別對應 "dog""cat""rabbit"

        {
          "species": {
            "all_values": {
              "value": true
            }
          }
        }

    你也可以使用多項功能建立一或多個切片。

    下列設定會建立單一切片,其中包含 age 介於 13 之間,且 species"dog" 的資料:

    {
      "age": {
        "range": {
          "low": 1,
          "high": 3
        }
      },
      "species": {
        "value": {
          "string_value": "dog"
        }
      }
    }
    

    下列設定會建立多個切片,資料集中的每個不重複物種各有一個切片,其中 age1

    {
      "species": {
        "all_values": {
          "value": true
        }
      },
      "age": {
        "value": {
          "float_value": 1.0
        }
      }
    }
    

    產生的切片包含範例資料集中的下列資料:

    • 投影片 1: age:1species:"dog"

    • 區塊 2: age:1species:"cat"

    • 區塊 3: age:1species:"rabbit"

    設定區隔格式

    如要為 ModelEvaluationClassificationOp 元件設定切片格式,請執行下列操作:

    1. 建立 slicing_spec。例如:

      from google.cloud.aiplatform_v1.types.ModelEvaluationSlice.Slice import SliceSpec
      from google.cloud.aiplatform_v1.types.ModelEvaluationSlice.Slice.SliceSpec import SliceConfig
      
      slicing_spec = SliceSpec(configs={ 'feature_a': SliceConfig(SliceSpec.Value(string_value='label_a') ) })
    2. 建立清單以儲存切片設定。例如:

      slicing_specs = []
    3. 將每個 slicing_spec 格式設為 JSON 或字典。例如:

      slicing_spec_json = json_format.MessageToJson(slicing_spec)
      
      slicing_spec_dict = json_format.MessageToDict(slicing_spec)
    4. 將每個 slicing_spec 合併成清單。例如:

      slicing_specs.append(slicing_spec_json)
    5. slicing_specs 做為參數傳遞至 ModelEvaluationClassificationOp 元件。例如:

      ModelEvaluationClassificationOp(slicing_specs=slicing_specs)

    格式化公平性指標設定切片

    如要評估模型的公平性,請使用下列元件:

    如要為 DetectDataBiasOpDetectModelBiasOp 元件設定切片格式,請按照下列步驟操作:

    1. 建立 BiasConfig。例如:

      from google.cloud.aiplatform_v1.types.ModelEvaluation import BiasConfig
      from google.cloud.aiplatform_v1.types.ModelEvaluationSlice.Slice import SliceSpec
      from google.cloud.aiplatform_v1.types.ModelEvaluationSlice.Slice.SliceSpec import SliceConfig
      
      bias_config = BiasConfig(bias_slices=SliceSpec(configs={ 'feature_a': SliceConfig(SliceSpec.Value(string_value='label_a') ) }))
    2. 建立清單以儲存偏誤設定。例如:

      bias_configs = []
    3. 將每個 bias_config 格式設為 JSON 或字典。例如:

      bias_config_json = json_format.MessageToJson(bias_config)
      
      bias_config_dict = json_format.MessageToDict(bias_config)
    4. 將每個 bias_config 合併成清單。例如:

      bias_configs.append(bias_config_json)
    5. bias_configs 做為參數傳遞至 DetectDataBiasOpDetectModelBiasOp 元件。例如:

      DetectDataBiasOp(bias_configs=bias_configs)
      
      DetectModelBiasOp(bias_configs=bias_configs)

    必要輸入參數

    模型評估元件的必要輸入參數會因評估的模型類型而異。

    AutoML 模型

    訓練 AutoML 模型時,Vertex AI 會使用預設結構定義。只有在您使用非預設結構定義訓練模型時,才需要提供模型評估元件的部分輸入參數。

    您可以從下列 Cloud Storage 位置查看及下載結構定義檔案:
    gs://google-cloud-aiplatform/schema/modelevaluation/

    以下分頁分別顯示批次預測輸出檔案範例,以及各模型類型所需的輸入參數:

    表格

    分類

    預設結構定義包含下列欄位:

    • scores:陣列
    • classes:陣列

    以下是批次預測輸出檔案的範例,其中包含 male 的目標資料欄:

    {
      "instance": {
        "male": "1",
        "age": "49",
        "heartRate": "67",
        "education": "medium_high",
        "totChol": "282",
        "diaBP": "79",
        "BPMeds": "0",
        "currentSmoker": "current_high",
        "cigsPerDay": "25",
        "prevalentStroke": "stroke_0",
        "prevalentHyp": "hyp_0",
        "TenYearCHD": "FALSE",
        "diabetes": "0",
        "sysBP": "134",
        "BMI": "26.87",
        "glucose": "78"
        },
      "prediction": {
          "scores": [0.3666940927505493, 0.6333059072494507],
          "classes": ["0", "1"]
        }
    }
              

    下表說明 ModelEvaluationClassificationOp 元件搭配表格模型時的必要參數:

    評估元件參數 必填
    target_field_name
    prediction_label_column 預設結構定義不需要此屬性。
    使用 prediction.classes 處理非預設結構定義。
    prediction_score_column 預設結構定義不需要此屬性。
    使用 prediction.scores 處理非預設結構定義。
    evaluation_class_labels 預設結構定義不需要此屬性。
    使用 ["0","1"] 處理非預設結構定義。

    迴歸

    預設結構定義包含下列欄位:

    • value:浮點數
    • lower_bound:浮點數
    • upper_bound:浮點數

    以下是批次預測輸出檔案的範例,其中包含 age 的目標資料欄:

    {
      "instance": {
        "BPMeds": "0",
        "diabetes": "0",
        "diaBP": "65",
        "cigsPerDay": "45",
        "prevalentHyp": "hyp_0",
        "age": "46",
        "male": "1",
        "sysBP": "112.5",
        "glucose": "78",
        "BMI": "27.48",
        "TenYearCHD": "FALSE",
        "totChol": "209",
        "education": "high",
        "prevalentStroke": "stroke_0",
        "heartRate": "75",
        "currentSmoker": "current_high"
      },
      "prediction": {
        "value": 44.96103286743164,
        "lower_bound": 44.61349868774414,
        "upper_bound": 44.590206146240234
      }
    }
              

    下表說明 ModelEvaluationRegressionOp 元件搭配表格模型時的必要參數:

    評估元件參數 必填
    target_field_name
    prediction_label_column 迴歸模型則不需提供這項資訊。
    prediction_score_column 預設結構定義不需要此屬性。
    使用 prediction.value 處理非預設結構定義。
    evaluation_class_labels 迴歸模型則不需提供這項資訊。

    預測

    如果是表格預測模型,BatchPredictionJob 元件會要求您從資料集中排除目標欄 (真值)。

    預設結構定義包含下列欄位:

    • id:字串
    • displayNames:字串
    • type:列舉
    • timeSegmentStart:字串
    • timeSegmentEnd:字串
    • confidence:浮點數

    以下是 CSV 檔案範例,其中 sale_dollars 為目標欄:

    date,store_name,city,zip_code,county,sale_dollars
    2020-03-17,Thriftway,,,,774.08999999999992
    2020-03-10,Thriftway,,,,1160.67
    2020-03-03,Thriftway,,,,2247.24
    2020-06-08,New Star / Fort Dodge,,,,753.98
    2020-06-01,New Star / Fort Dodge,,,,967.73
    2020-01-10,Casey's General Store #1280 / Fort Dodge,,,,1040.92
    2020-10-30,KUM & GO #76 / ADAIR,Adair,50002,ADAIR,1387.02
              

    TargetFieldDataRemoverOp 元件會先移除目標資料欄,再將檔案傳送至 BatchPredictionJob 元件:

    date,store_name,city,zip_code,county
    2020-03-17,Thriftway,nan,nan,nan
    2020-03-10,Thriftway,nan,nan,nan
    2020-03-03,Thriftway,nan,nan,nan
    2020-06-08,New Star / Fort Dodge,nan,nan,nan
    2020-06-01,New Star / Fort Dodge,nan,nan,nan
    2020-01-10,Casey's General Store #1280 / Fort Dodge,nan,nan,nan
    2020-10-30,KUM & GO #76 / ADAIR,Adair,50002.0,ADAIR
              

    以下是沒有 sale_dollars 目標資料欄的批次預測輸出檔案範例:

    {
      "instance": {
        "content": "gs://kbn-us-central1-test/datasets/text/happy_11556.txt",
        "mimeType":"text/plain"
      },
      "prediction": {
        "ids":    ["7903916851837534208","3292230833410146304","986387824196452352","2139309328803299328","5598073842623840256","6750995347230687232","4559431178561519616"],
        "displayNames": ["affection","bonding","achievement","exercise","nature","enjoy_the_moment","leisure"],
        "confidences": [0.99483216,0.005162797,4.1117933E-6,3.9997E-7,2.4624453E-7,1.9969502E-7,1.16997434E-7]
      }
    }
              

    下表說明 ModelEvaluationForecastingOp 元件搭配表格模型時的必要參數:

    評估元件參數 必填
    target_field_name
    prediction_label_column 預測模型不需要這項資訊。
    prediction_score_column 預設結構定義不需要此屬性。
    使用 "prediction.value" 處理非預設結構定義。
    evaluation_class_labels 預測模型不需要這項資訊。
    ground_truth_gcs_source 是:原始資料集的 Cloud Storage 連結,其中包含目標資料欄。
    ground_truth_format 是:原始資料集的格式。例如 "jsonl""csv""bigquery"

    文字

    分類

    如果是文字分類模型,BatchPredictionJob 元件會要求您從資料集中排除目標資料欄 (基準真相)。您可以使用 TargetFieldDataRemoverOp 元件,提升工作流程效率。

    預設結構定義包含下列欄位:

    • ids:字串陣列
    • displayName:字串陣列
    • confidence:浮點數陣列

    以下是目標欄為「label」的資料集範例:

    {
      "content": "gs://kbn-us-central1-test/datasets/text/happy_0.txt",
      "mimeType": "text/plain",
      "label": "affection"
    }
              

    TargetFieldDataRemoverOp 元件會先移除目標資料欄,再將檔案傳送至 BatchPredictionJob 元件:

    {
      "content": "gs://kbn-us-central1-test/datasets/text/happy_0.txt",
      "mimeType": "text/plain"
    }
              

    以下是沒有 label 目標資料欄的批次預測輸出檔案範例:

    {
      "instance": {
        "content": "gs://kbn-us-central1-test/datasets/text/happy_11556.txt",
        "mimeType":"text/plain"
      },
      "prediction": {
        "ids":    ["7903916851837534208","3292230833410146304","986387824196452352","2139309328803299328","5598073842623840256","6750995347230687232","4559431178561519616"],
        "displayNames": ["affection","bonding","achievement","exercise","nature","enjoy_the_moment","leisure"],
        "confidences": [0.99483216,0.005162797,4.1117933E-6,3.9997E-7,2.4624453E-7,1.9969502E-7,1.16997434E-7]
      }
    }
              

    下表說明使用文字模型時,ModelEvaluationClassificationOp 元件的必要參數:

    評估元件參數 必填
    target_field_name
    prediction_label_column 預設結構定義不需要此屬性。
    使用 "prediction.displayNames" 處理非預設結構定義。
    prediction_score_column 預設結構定義不需要此屬性。
    使用 "prediction.confidences" 處理非預設結構定義。
    evaluation_class_labels 預設結構定義或提供 prediction_label_column 時,則不需要。
    ground_truth_gcs_source 是:原始資料集的 Cloud Storage 連結,其中包含目標資料欄。
    ground_truth_format 是:原始資料集的格式。例如 "jsonl""csv""bigquery"

    影片

    分類

    如果是影片分類模型,BatchPredictionJob 元件會要求您從資料集中排除目標資料欄 (基準真相)。您可以使用 TargetFieldDataRemoverOp 元件,提升工作流程效率。

    預設結構定義包含下列欄位:

    • id:字串
    • displayName:字串
    • type:列舉
    • timeSegmentStart:字串
    • timeSegmentEnd:字串
    • confidence:浮點數

    以下是目標欄為「ground_truth」的資料集範例:

    {
      "content": "gs://automl-video-demo-data/hmdb51/Aerial_Cartwheel_Tutorial_By_Jujimufu_cartwheel_f_nm_np1_ri_med_0.avi",
      "ground_truth": "cartwheel",
      "timeSegmentStart": "0.0",
      "timeSegmentEnd": "inf"
    }
              

    TargetFieldDataRemoverOp 元件會先移除目標資料欄,再將檔案傳送至 BatchPredictionJob 元件:

    {
      "content": "gs://automl-video-demo-data/hmdb51/Aerial_Cartwheel_Tutorial_By_Jujimufu_cartwheel_f_nm_np1_ri_med_0.avi",
      "timeSegmentStart": "0.0",
      "timeSegmentEnd": "inf"
    }
              

    以下是沒有 label 目標資料欄的批次預測輸出檔案範例:

    {
      "instance": {
        "content": "gs://automl-video-demo-data/hmdb51/20_Marine_Corps_Pull_Ups___JimmyDShea_pullup_f_cm_np1_ba_bad_3.avi"
        "mimeType": "video/avi",
        "timeSegmentStart": "0.0",
        "timeSegmentEnd": "inf"
      },
      "prediction": [
        {
          "id":"5015420640832782336",
          "displayName":"pullup",
          "type":"segment-classification",
          "timeSegmentStart":"0s",
          "timeSegmentEnd":"2.600s",
          "confidence":0.98152995
        },
        {
          "id":"2709577631619088384",
          "displayName":"cartwheel",
          "type":"segment-classification",
          "timeSegmentStart":"0s",
          "timeSegmentEnd":"2.600s",
          "confidence":0.0047166348
        },
        {
          "id":"403734622405394432",
          "displayName":"golf",
          "type":"segment-classification",
          "timeSegmentStart":"0s",
          "timeSegmentEnd":"2.600s",
          "confidence":0.0046260506
        },
        {
          "id":"1556656127012241408",
          "displayName":"ride_horse",
          "type":"segment-classification",
          "timeSegmentStart":"0s",
          "timeSegmentEnd":"2.600s",
          "confidence":0.004590442
        },
        {
          "id":"7321263650046476288",
          "displayName":"kick_ball",
          "type":"segment-classification",
          "timeSegmentStart":"0s",
          "timeSegmentEnd":"2.600s",
          "confidence":0.004536863
        }
      ]
    }
              

    下表說明 ModelEvaluationClassificationOp 元件的必要參數 (使用影片模型):

    評估元件參數 必填
    target_field_name
    prediction_label_column 預設結構定義不需要此屬性。
    使用 "prediction.displayName" 處理非預設結構定義。
    prediction_score_column 預設結構定義不需要此屬性。
    使用 "prediction.confidence" 處理非預設結構定義。
    evaluation_class_labels 如果使用預設結構定義,或提供 prediction_label_column,則不需要這個欄位。
    ground_truth_gcs_source 是:原始資料集的 Cloud Storage 連結,其中包含目標資料欄。
    ground_truth_format 是:原始資料集的格式。例如 "jsonl""csv""bigquery"

    自訂訓練模型

    如果是自訂訓練模型,BatchPredictionJob 元件會要求您從資料集中排除目標資料欄 (基準真相)。您可以使用 TargetFieldDataRemoverOp 元件,提升工作流程效率。

    以下範例顯示批次預測輸出檔案,以及自訂訓練表格分類模型所需的輸入參數:

    表格

    分類

    一般結構定義包含下列欄位:

    • instance:輸入資料陣列,順序與模型訓練資料集資料欄相同。
    • prediction:輸入資料陣列,順序與模型訓練資料集資料欄相同。

    以下是 CSV 資料集範例,其中 species 為目標欄:

    petal_length,petal_width,sepal_length,sepal_width,species
    6.4,2.8,5.6,2.2,2
    5.0,2.3,3.3,1.0,1
    4.9,2.5,4.5,1.7,2
    4.9,3.1,1.5,0.1,0
              

    TargetFieldDataRemoverOp 元件會先移除目標資料欄,再將檔案傳送至批次預測元件:

    petal_length,petal_width,sepal_length,sepal_width
    6.4,2.8,5.6,2.2
    5.0,2.3,3.3,1.0
    4.9,2.5,4.5,1.7
    4.9,3.1,1.5,0.1
              

    以下是沒有 species 目標資料欄的批次預測輸出檔案範例:

    {
      "instance": [5.6, 2.5, 3.9, 1.1],
      "prediction": [0.0005816521588712931, 0.9989032745361328, 0.0005150380893610418]
    }
    {
      "instance": [5.0, 3.6, 1.4, 0.2],
      "prediction": [0.999255359172821, 0.000527293945197016, 0.00021737271163146943]
    }
    {
      "instance": [6.0, 2.9, 4.5, 1.5],
      "prediction": [0.00025063654175028205, 0.9994204044342041, 0.00032893591560423374]
    }
              

    下表說明 ModelEvaluationClassificationOp 元件的必要參數 (使用影片模型):

    評估元件參數 必填
    target_field_name
    prediction_label_column 必須留空;自訂訓練模型中沒有這個資料欄。
    prediction_score_column 預設結構定義不需要此設定;系統會自動為 JSONLCSVBIGQUERY 格式設定此值。
    evaluation_class_labels 可以。長度必須與輸出預測陣列相同。
    ground_truth_gcs_source 是:原始資料集的 Cloud Storage 連結,其中包含目標資料欄。
    ground_truth_format 是:原始資料集的格式。例如 "jsonl""csv""bigquery"

    不使用 Vertex AI 產生的批次預測結果進行模型評估

    您可以搭配使用模型評估管道元件和批次預測,但批次預測並非在 Vertex AI 中產生。但請注意,模型評估管道元件的輸入內容必須是批次預測目錄,其中包含符合下列前置字元的檔案:

    • prediction.results-

    • explanation.results-

    • predictions_

    API 參考資料

    版本記錄和版本資訊

    如要進一步瞭解版本記錄和 Google Cloud Pipeline Components SDK 的變更,請參閱 Google Cloud Pipeline Components SDK 版本資訊