在 Vertex AI 上使用遠端模型進行預測


在本教學課程中,您將 Vertex AI 端點註冊為 BigQuery 中的遠端模型。接著,您可以使用 ML.PREDICT 函式,透過遠端模型進行預測。

當模型過大而無法匯入 BigQuery 時,您可以使用遠端模型。如要針對線上、批次和微批次用途使用單一推論點,這類模型也相當實用。

目標

  • 將預先訓練的 TensorFlow 模型匯入 Vertex AI Model Registry。
  • 將模型部署至 Vertex AI 端點。
  • 建立 Cloud 資源連線。
  • 使用 CREATE MODEL 陳述式,在 BigQuery 中建立遠端模型。
  • 使用 ML.PREDICT 函式,透過遠端模型進行預測。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

您可以使用 Pricing Calculator 根據預測用量產生預估費用。 新 Google Cloud 使用者可能符合申請免費試用的資格。

完成本文件所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱「清除所用資源」。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  4. Make sure that billing is enabled for your Google Cloud project.

  5. Enable the BigQuery, Vertex AI, Cloud Storage, and BigQuery Connection APIs.

    Enable the APIs

  6. 請確認您具備必要權限,才能執行本文所述的任務。
  7. 必要的角色

    如果您建立新專案,就會成為專案擁有者,並且會獲得完成本教學課程所需的所有 IAM 權限。

    如果您使用的是現有專案,請執行下列操作。

    Make sure that you have the following role or roles on the project:

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「身分與存取權管理」頁面
    2. 選取專案。
    3. 按一下 「授予存取權」
    4. 在「New principals」(新增主體) 欄位中輸入使用者 ID。 通常是 Google 帳戶的電子郵件地址。

    5. 在「請選擇角色」清單中,選取角色。
    6. 如要授予其他角色,請按一下 「Add another role」(新增其他角色),然後新增其他角色。
    7. 按一下 [Save]
    8. 如要進一步瞭解 BigQuery 中的 IAM 權限,請參閱「BigQuery 權限」一文。

將模型匯入 Vertex AI Model Registry

在本教學課程中,您將使用 Cloud Storage 中 gs://cloud-samples-data/bigquery/ml/remote_model_tutorial/ 可用的預先訓練 TensorFlow 模型。Cloud Storage 值區位於 US 多區域位置。

這個模型是名為 saved_model.pb 的 TensorFlow 模型。這是使用純文字 IMDB 電影評論資料微調 BERT 模型,所建立的自訂情緒分析模型。這個模型會使用電影評論的文字輸入內容,並傳回介於零和一之間的情緒分數。將模型匯入模型註冊中心時,您會使用預先建構的 TensorFlow 容器。

請按照下列步驟匯入模型。

  1. 在 Google Cloud 控制台中,前往 Vertex AI「Model Registry」頁面。

    前往 Model Registry

  2. 按一下「匯入」

  3. 針對步驟一:名稱和地區,請執行下列操作:

    1. 選取「Import as new model」

    2. 在「Name」(名稱) 中輸入 bert_sentiment

    3. 在「Description」中輸入 BQML tutorial model

    4. 在「區域」部分,選取 us-central1。您必須選擇美國區域,因為 Cloud Storage 值區位於 US 多地區位置。

    5. 按一下「繼續」

  4. 針對「步驟二:模型設定」,請執行下列操作:

    1. 選取「將模型構件匯入新的預建容器」

    2. 在「預先建構的容器設定」部分,執行下列操作:

      1. 在「Model framework」(模型架構) 中,選擇「TensorFlow」

      2. 在「Model framework version」(模型架構版本) 中,選擇「2.15」

      3. 在「加速器類型」中,選擇「GPU」

      4. 在「Model artifact location」中輸入 gs://cloud-samples-data/bigquery/ml/remote_model_tutorial/

      5. 保留所有剩餘選項的預設值,然後按一下「Import」

匯入完成後,模型就會顯示在「Model Registry」頁面上。

將模型部署至 Vertex AI 端點

請按照下列步驟將模型部署至端點。

  1. 在 Google Cloud 控制台中,前往 Vertex AI 的「Model Registry」頁面。

    前往 Model Registry

  2. 在「Name」(名稱) 欄中,按一下 bert_sentiment

  3. 按一下「Deploy & Test」分頁標籤。

  4. 按一下「Deploy to endpoint」

  5. 在第一步「定義端點」中,請執行下列操作:

    1. 按一下「建立新端點」

    2. 在「端點名稱」中輸入 bert sentiment endpoint

    3. 保留其餘預設值,然後按一下「繼續」

  6. 在步驟 2「模型設定」中,請按照下列步驟操作:

    1. 在「Compute settings」部分,輸入「Minimum number of compute nodes」的值 1。這是模型隨時需要提供的節點數量。

    2. 在「Advanced scaling options」部分,針對「Machine type」,選擇「Standard (n1-standard-2)」。由於您在匯入模型時選擇 GPU 做為加速器類型,因此在選擇機器類型後,系統會自動設定加速器類型和加速器數量。

    3. 保留剩下欄位的預設值,然後點選「部署」

      模型部署到端點後,狀態會變更為 Active

    4. 複製「ID」欄中的數字端點 ID 和「Region」欄中的值。稍後會用到。

建立資料集

建立 BigQuery 資料集來儲存機器學習模型。

控制台

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

    前往「BigQuery」頁面

  2. 在「Explorer」窗格中,按一下專案名稱。

  3. 依序點選 「View actions」(查看動作) >「Create dataset」(建立資料集)

    「建立資料集」選單選項。

  4. 在「Create dataset」頁面上執行下列操作:

    • 在「Dataset ID」(資料集 ID) 中輸入 bqml_tutorial

    • 在「位置類型」中選取「多區域」,然後選取「美國 (多個美國區域)」

    • 保留其餘預設設定,然後點選「Create dataset」(建立資料集)

bq

如要建立新的資料集,請使用 bq mk 指令搭配 --location 旗標。如需可能參數的完整清單,請參閱 bq mk --dataset 指令參考資料。

  1. 建立名為 bqml_tutorial 的資料集,並將資料位置設為 US,說明為 BigQuery ML tutorial dataset

    bq --location=US mk -d \
     --description "BigQuery ML tutorial dataset." \
     bqml_tutorial

    這個指令採用 -d 捷徑,而不是使用 --dataset 旗標。如果您省略 -d--dataset,該指令預設會建立資料集。

  2. 確認資料集已建立:

    bq ls

API

請呼叫 datasets.insert 方法,搭配已定義的資料集資源

{
  "datasetReference": {
     "datasetId": "bqml_tutorial"
  }
}

BigQuery DataFrames

在嘗試這個範例之前,請先參閱 BigQuery 快速入門:使用 BigQuery DataFrames,按照 BigQuery DataFrames 設定說明進行操作。詳情請參閱 BigQuery DataFrames 參考資料說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定 ADC」。

import google.cloud.bigquery

bqclient = google.cloud.bigquery.Client()
bqclient.create_dataset("bqml_tutorial", exists_ok=True)

建立 BigQuery Cloud 資源連線

您必須擁有 Cloud 資源連線,才能連線至 Vertex AI 端點。

主控台

  1. 前往「BigQuery」頁面

    前往 BigQuery

  2. 在「Explorer」窗格中,按一下 「Add data」

    「Add data」對話方塊隨即開啟。

  3. 在「Filter By」窗格中的「Data Source Type」部分,選取「Databases」

    或者,您也可以在「Search for data sources」欄位中輸入 Vertex AI

  4. 在「精選資料來源」部分,按一下「Vertex AI」

  5. 按一下「Vertex AI 模型:BigQuery 聯盟」解決方案資訊卡。

  6. 在「連線類型」清單中,選取「Vertex AI 遠端模型、遠端函式和 BigLake (Cloud 資源)」

  7. 在「連線 ID」欄位中輸入 bqml_tutorial

  8. 確認已選取「Multi-region - US」(多區域 - 美國)

  9. 點選「建立連線」

  10. 按一下視窗底部的「前往連線」。或者,您也可以在「Explorer」窗格中展開「External connections」,然後按一下 us.bqml_tutorial

  11. 在「連線資訊」窗格中,複製服務帳戶 ID,您需要這個 ID 才能設定連線的權限。建立連線資源時,BigQuery 會建立專屬的系統服務帳戶,並將該帳戶與連線建立關聯。

bq

  1. 建立連線:

    bq mk --connection --location=US --project_id=PROJECT_ID \
        --connection_type=CLOUD_RESOURCE bqml_tutorial

    PROJECT_ID 替換為您的Google Cloud 專案 ID。--project_id 參數會覆寫預設專案。

    建立連線資源時,BigQuery 會建立專屬的系統服務帳戶,並將該帳戶與連線建立關聯。

    疑難排解:如果您收到下列連線錯誤,請更新 Google Cloud SDK

    Flags parsing error: flag --connection_type=CLOUD_RESOURCE: value should be one of...
    
  2. 擷取並複製服務帳戶 ID,以便在後續步驟中使用:

    bq show --connection PROJECT_ID.us.bqml_tutorial

    輸出結果會與下列內容相似:

    name                          properties
    1234.REGION.CONNECTION_ID {"serviceAccountId": "connection-1234-9u56h9@gcp-sa-bigquery-condel.iam.gserviceaccount.com"}
    

設定連線存取權

為雲端資源連線的服務帳戶授予 Vertex AI 使用者角色。您必須在建立遠端模型端點的專案中授予這個角色。

如要授予角色,請按照下列步驟操作:

  1. 前往「IAM & Admin」(IAM 與管理) 頁面。

    前往「IAM & Admin」(IAM 與管理)

  2. 按一下 「授予存取權」

  3. 在「新增主體」欄位中,輸入先前複製的雲端資源連線服務帳戶 ID。

  4. 在「請選擇角色」欄位中,依序選取「Vertex AI」和「Vertex AI 使用者」

  5. 按一下 [儲存]

建立 BigQuery ML 遠端模型

您可以使用 CREATE MODEL 陳述式搭配 REMOTE WITH CONNECTION 子句,建立 BigQuery ML 遠端模型。如要進一步瞭解 CREATE MODEL 陳述式,請參閱「透過自訂模型建立遠端模型的 CREATE MODEL 陳述式」。

您在 US 多地區位置建立模型。在 BigQuery 多區域 (USEU) 資料集中,您只能建立遠端模型,連線至位於同一多區域位置 (USEU) 的區域中端點。

建立遠端模型時,您需要在將模型部署至 Vertex AI 時產生的端點 ID。此外,輸入和輸出欄位名稱和類型必須與 Vertex AI 模型的輸入和輸出完全相同。在這個範例中,輸入內容是文字 STRING,輸出內容則是 FLOAT64 類型的 ARRAY

控制台

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

    前往 BigQuery

  2. 針對「建立新」,請點選「SQL 查詢」

  3. 在查詢編輯器中輸入以下 CREATE MODEL 陳述式,然後按一下「Run」

    CREATE OR REPLACE MODEL `PROJECT_ID.bqml_tutorial.bert_sentiment`
    INPUT (text STRING)
    OUTPUT(scores ARRAY)
    REMOTE WITH CONNECTION `PROJECT_ID.us.bqml_tutorial`
    OPTIONS(ENDPOINT = 'https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/endpoints/ENDPOINT_ID')

    更改下列內容:

    • PROJECT_ID:您的專案名稱。
    • ENDPOINT_ID:您先前複製的端點 ID。

    作業完成後,您會看到類似 Successfully created model named bert_sentiment 的訊息。

    新模型會顯示在「資源」面板中。模型會以模型圖示 模型圖示 來表示。

    如果您在「Resources」面板中選取新模型,「Query editor」下方就會顯示該模型的相關資訊。

bq

  1. 輸入下列 CREATE MODEL 陳述式,建立遠端模型:

    bq query --use_legacy_sql=false \
    "CREATE OR REPLACE MODEL `PROJECT_ID.bqml_tutorial.bert_sentiment`
    INPUT (text STRING)
    OUTPUT(scores ARRAY)
    REMOTE WITH CONNECTION `PROJECT_ID.us.bqml_tutorial`
    OPTIONS(ENDPOINT = 'https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/endpoints/ENDPOINT_ID')"

    更改下列內容:

    • PROJECT_ID:您的專案名稱。
    • ENDPOINT_ID:您先前複製的端點 ID。
  2. 建立模型後,請確認模型是否顯示在資料集中:

    bq ls -m bqml_tutorial

    輸出結果會與下列內容相似:

    Id               Model Type   Labels    Creation Time
    ---------------- ------------ -------- -----------------
    bert_sentiment                         28 Jan 17:39:43

使用 ML.PREDICT 取得預測結果

您可以使用 ML.PREDICT 函式,從遠端模型取得情緒預測結果。輸入內容是文字欄位 (review),其中包含 bigquery-public-data.imdb.reviews 資料表中的電影評論。

在本範例中,系統會選取 10,000 筆記錄並傳送進行預測。遠端模型的預設要求批次大小為 128 個執行個體。

控制台

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

    前往 BigQuery

  2. 在「Create new」部分,按一下「SQL query」

  3. 在查詢編輯器中輸入使用 ML.PREDICT 函式的查詢,然後按一下「Run」

    SELECT *
    FROM ML.PREDICT (
        MODEL `PROJECT_ID.bqml_tutorial.bert_sentiment`,
        (
            SELECT review as text
            FROM `bigquery-public-data.imdb.reviews`
            LIMIT 10000
        )
    )

    查詢結果應如下所示:

    查詢結果

bq

輸入下列指令,執行使用 ML.PREDICT 的查詢。

bq query --use_legacy_sql=false \
'SELECT *
FROM ML.PREDICT (
MODEL `PROJECT_ID.bqml_tutorial.bert_sentiment`,
  (
    SELECT review as text
    FROM `bigquery-public-data.imdb.reviews`
    LIMIT 10000
  )
)'

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

刪除專案

控制台

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

gcloud

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

刪除個別資源

或者,如要移除本教學課程中使用的個別資源,請按照下列步驟操作:

  1. 刪除模型

  2. 選用步驟:刪除資料集

  3. 取消部署模型並刪除端點

  4. 從 Model Registry 中刪除模型

  5. 刪除 Cloud 資源連線

後續步驟