使用預先訓練的 TensorFlow 模型嵌入文字

本教學課程將說明如何使用預先訓練的 TensorFlow 模型,在 BigQuery 中產生 NNLM、SWIVEL 和 BERT 文字嵌入資料。文字嵌入是文字的密集向量表示法,如果兩段文字的語意相似,則各自的嵌入在嵌入向量空間中會相距不遠。

NNLM、SWIVEL 和 BERT 模型

NNLM、SWIVEL 和 BERT 模型的大小、準確度、可擴充性和成本各有不同。請參考下表,決定要使用哪個模型:

模型 模型大小 嵌入項目維度 用途 說明
NNLM 小於 150 MB 50 簡短短句、新聞、推文、評論 神經網路語言模型
旋轉 小於 150 MB 20 簡短短句、新聞、推文、評論 以子矩陣為單位的向量嵌入學習器
BERT ~200MB 768 短短的短語、新聞、推文、評論、短段文字 基於變換器的雙向編碼器表示法 (BERT)

在本教學課程中,NNLM 和 SWIVEL 模型是匯入的 TensorFlow 模型,而 BERT 模型則是 Vertex AI 上的遠端模型

所需權限

  • 如要建立資料集,您必須具備 bigquery.datasets.create 身分與存取權管理 (IAM) 權限。

  • 如要建立值區,您必須具備 storage.buckets.create IAM 權限。

  • 如要將模型上傳至 Cloud Storage,您必須具備 storage.objects.createstorage.objects.get IAM 權限。

  • 如要建立連線資源,您必須具備下列 IAM 權限:

    • bigquery.connections.create
    • bigquery.connections.get
  • 如要將模型載入 BigQuery ML,您需要具備下列身分與存取權管理權限:

    • bigquery.jobs.create
    • bigquery.models.create
    • bigquery.models.getData
    • bigquery.models.updateData
  • 如要執行推論,您必須具備下列 IAM 權限:

    • 物件資料表上的 bigquery.tables.getData
    • 模型的 bigquery.models.getData
    • bigquery.jobs.create

費用

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

  • BigQuery: You incur costs for the queries that you run in BigQuery.
  • BigQuery ML: You incur costs for the model that you create and the inference that you perform in BigQuery ML.
  • Cloud Storage: You incur costs for the objects that you store in Cloud Storage.
  • Vertex AI: If you follow the instructions for generating the BERT model, then you incur costs for deploying the model to an endpoint.

您可以使用 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. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the BigQuery, BigQuery Connection, and Vertex AI APIs.

    Enable the APIs

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

    Go to project selector

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

  7. Enable the BigQuery, BigQuery Connection, and Vertex AI APIs.

    Enable the APIs

  8. 建立資料集

    如要建立名為 tf_models_tutorial 的資料集,以便儲存您建立的模型,請選取下列其中一個選項:

    SQL

    使用 CREATE SCHEMA 陳述式

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

      前往 BigQuery

    2. 在查詢編輯器中輸入以下陳述式:

      CREATE SCHEMA `PROJECT_ID.tf_models_tutorial`;

      PROJECT_ID 替換為您的專案 ID。

    3. 按一下 「Run」

    如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。

    bq

    1. 在 Google Cloud 控制台中啟用 Cloud Shell。

      啟用 Cloud Shell

    2. 如要建立資料集,請執行 bq mk 指令

      bq mk --dataset --location=us PROJECT_ID:tf_models_tutorial

      PROJECT_ID 替換為您的專案 ID。

    產生模型並上傳至 Cloud Storage

    如要進一步瞭解如何使用預先訓練的 TensorFlow 模型產生文字嵌入資料,請參閱 Colab 筆記本。否則請選取下列任一型號:

    NNLM

    1. 使用 pip 安裝 bigquery-ml-utils 程式庫

      pip install bigquery-ml-utils
      
    2. 產生 NNLM 模型。下列 Python 程式碼會從 TensorFlow Hub 載入 NNLM 模型,並為 BigQuery 做好準備:

      from bigquery_ml_utils import model_generator
      
      # Establish an instance of TextEmbeddingModelGenerator.
      text_embedding_model_generator = model_generator.TextEmbeddingModelGenerator()
      
      # Generate an NNLM model.
      text_embedding_model_generator.generate_text_embedding_model('nnlm', OUTPUT_MODEL_PATH)
      

      OUTPUT_MODEL_PATH 替換為可暫時儲存模型的本機資料夾路徑。

    3. 選用步驟:列印產生的模型簽名:

      import tensorflow as tf
      
      reload_embedding_model = tf.saved_model.load(OUTPUT_MODEL_PATH)
      print(reload_embedding_model.signatures["serving_default"])
      
    4. 如要將產生的模型從本機資料夾複製到 Cloud Storage 值區,請使用 Google Cloud CLI

      gcloud storage cp OUTPUT_MODEL_PATH gs://BUCKET_PATH/nnlm_model --recursive
      

      BUCKET_PATH 替換為您要複製模型的 Cloud Storage 值區名稱。

    SWIVEL

    1. 使用 pip 安裝 bigquery-ml-utils 程式庫

      pip install bigquery-ml-utils
      
    2. 產生 SWIVEL 模型。下列 Python 程式碼會從 TensorFlow Hub 載入 SWIVEL 模型,並為 BigQuery 做好準備:

      from bigquery_ml_utils import model_generator
      
      # Establish an instance of TextEmbeddingModelGenerator.
      text_embedding_model_generator = model_generator.TextEmbeddingModelGenerator()
      
      # Generate a SWIVEL model.
      text_embedding_model_generator.generate_text_embedding_model('swivel', OUTPUT_MODEL_PATH)
      

      OUTPUT_MODEL_PATH 替換為可暫時儲存模型的本機資料夾路徑。

    3. 選用步驟:列印產生的模型簽名:

      import tensorflow as tf
      
      reload_embedding_model = tf.saved_model.load(OUTPUT_MODEL_PATH)
      print(reload_embedding_model.signatures["serving_default"])
      
    4. 如要將產生的模型從本機資料夾複製到 Cloud Storage 值區,請使用 Google Cloud CLI

      gcloud storage cp OUTPUT_MODEL_PATH gs://BUCKET_PATH/swivel_model --recursive
      

      BUCKET_PATH 替換為您要複製模型的 Cloud Storage 值區名稱。

    BERT

    1. 使用 pip 安裝 bigquery-ml-utils 程式庫

      pip install bigquery-ml-utils
      
    2. 產生 BERT 模型。下列 Python 程式碼會從 TensorFlow Hub 載入 BERT 模型,並為 BigQuery 做好準備:

      from bigquery_ml_utils import model_generator
      
      # Establish an instance of TextEmbeddingModelGenerator.
      text_embedding_model_generator = model_generator.TextEmbeddingModelGenerator()
      
      # Generate a BERT model.
      text_embedding_model_generator.generate_text_embedding_model('bert', OUTPUT_MODEL_PATH)
      

      OUTPUT_MODEL_PATH 替換為可暫時儲存模型的本機資料夾路徑。

    3. 選用步驟:列印產生的模型簽名:

      import tensorflow as tf
      
      reload_embedding_model = tf.saved_model.load(OUTPUT_MODEL_PATH)
      print(reload_embedding_model.signatures["serving_default"])
      
    4. 如要將產生的模型從本機資料夾複製到 Cloud Storage 值區,請使用 Google Cloud CLI

      gcloud storage cp OUTPUT_MODEL_PATH gs://BUCKET_PATH/bert_model --recursive
      

      BUCKET_PATH 替換為您要複製模型的 Cloud Storage 值區名稱。

    將模型載入 BigQuery

    請選取下列其中一個型號:

    NNLM

    使用 CREATE MODEL 陳述式

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

      前往 BigQuery

    2. 在查詢編輯器中輸入以下陳述式:

      CREATE OR REPLACE MODEL tf_models_tutorial.nnlm_model
      OPTIONS (
        model_type = 'TENSORFLOW',
        model_path = 'gs://BUCKET_NAME/nnlm_model/*');

      BUCKET_NAME 替換為您先前建立的值區名稱。

    3. 按一下 「Run」

    如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。

    SWIVEL

    使用 CREATE MODEL 陳述式

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

      前往 BigQuery

    2. 在查詢編輯器中輸入以下陳述式:

      CREATE OR REPLACE MODEL tf_models_tutorial.swivel_model
      OPTIONS (
        model_type = 'TENSORFLOW',
        model_path = 'gs://BUCKET_NAME/swivel_model/*');

      BUCKET_NAME 替換為您先前建立的值區名稱。

    3. 按一下 「Run」

    如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。

    BERT

    如要將 BERT 模型載入 BigQuery,請將 BERT 模型匯入 Vertex AI,將模型部署至 Vertex AI 端點,建立連線,然後在 BigQuery 中建立遠端模型。

    如要將 BERT 模型匯入 Vertex AI,請按照下列步驟操作:

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

      前往 Model Registry

    2. 按一下「匯入」,然後執行下列操作:

      • 在「Name」(名稱) 中輸入 BERT
      • 在「Region」中,選取與 Cloud Storage 值區所在地區相符的地區。
    3. 按一下「繼續」,然後執行下列操作:

      • 在「Model framework version」(模型架構版本) 中選取 2.8
      • 在「模型構件位置」中,輸入儲存模型檔案的 Cloud Storage 值區路徑。例如:gs://BUCKET_PATH/bert_model
    4. 按一下 [匯入]。匯入完成後,模型就會顯示在「Model registry」頁面上。

    如要將 BERT 模型部署至 Vertex AI 端點,並將其連結至 BigQuery,請按照下列步驟操作:

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

      前往 Model Registry

    2. 按一下模型名稱。

    3. 按一下「Deploy & test」(部署及測試)

    4. 按一下「Deploy to endpoint」

    5. 在「端點名稱」中輸入 bert_model_endpoint

    6. 按一下「繼續」

    7. 選取運算資源。

    8. 按一下 [Deploy] (部署)

    9. 建立 BigQuery Cloud 資源連線,並授予連線的服務帳戶存取權

    如要根據 Vertex AI 端點建立遠端模型,請使用 CREATE MODEL 陳述式

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

      前往 BigQuery

    2. 在查詢編輯器中輸入以下陳述式:

      CREATE OR REPLACE MODEL tf_models_tutorial.bert_model
      INPUT(content STRING)
      OUTPUT(embedding ARRAY<FLOAT64>)
      REMOTE WITH CONNECTION `PROJECT_ID.CONNECTION_LOCATION.CONNECTION_ID`
      OPTIONS (
        ENDPOINT = "https://ENDPOINT_LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/ENDPOINT_LOCATION/endpoints/ENDPOINT_ID");

      請依指示取代下列項目:

      • PROJECT_ID:專案 ID
      • CONNECTION_LOCATION:BigQuery 連線的位置
      • CONNECTION_ID:BigQuery 連線的 ID

        在 Google Cloud 控制台查看連線詳細資料時,這是連線 ID 中顯示的完整限定連線 ID 最後一個部分的值,例如 projects/myproject/locations/connection_location/connections/myconnection

      • ENDPOINT_LOCATION:Vertex AI 端點的位置。例如「us-central1」。
      • ENDPOINT_ID:模型端點的 ID

    3. 按一下 「Run」

    如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。

    產生文字嵌入

    在本節中,您將使用 ML.PREDICT() 推論函式,從公開資料集 bigquery-public-data.imdb.reviews 產生 review 欄的文字嵌入資料。這項查詢會將表格限制在 500 列,以減少所處理的資料量。

    NNLM

    SELECT
      *
    FROM
      ML.PREDICT(
        MODEL `tf_models_tutorial.nnlm_model`,
        (
        SELECT
          review AS content
        FROM
          `bigquery-public-data.imdb.reviews`
        LIMIT
          500)
      );

    結果類似下列內容:

    +-----------------------+----------------------------------------+
    | embedding             | content                                |
    +-----------------------+----------------------------------------+
    |  0.08599445223808289  | Isabelle Huppert must be one of the... |
    | -0.04862852394580841  |                                        |
    | -0.017750458791851997 |                                        |
    |  0.8658871650695801   |                                        |
    | ...                   |                                        |
    +-----------------------+----------------------------------------+
    

    SWIVEL

    SELECT
      *
    FROM
      ML.PREDICT(
        MODEL `tf_models_tutorial.swivel_model`,
        (
        SELECT
          review AS content
        FROM
          `bigquery-public-data.imdb.reviews`
        LIMIT
          500)
      );

    結果類似下列內容:

    +----------------------+----------------------------------------+
    | embedding            | content                                |
    +----------------------+----------------------------------------+
    |  2.5952553749084473  | Isabelle Huppert must be one of the... |
    | -4.015787601470947   |                                        |
    |  3.6275434494018555  |                                        |
    | -6.045154333114624   |                                        |
    | ...                  |                                        |
    +----------------------+----------------------------------------+
    

    BERT

    SELECT
      *
    FROM
      ML.PREDICT(
        MODEL `tf_models_tutorial.bert_model`,
        (
        SELECT
          review AS content
        FROM
          `bigquery-public-data.imdb.reviews`
        LIMIT
          500)
      );

    結果類似下列內容:

    +--------------+---------------------+----------------------------------------+
    | embedding    | remote_model_status | content                                |
    +--------------+---------------------+----------------------------------------+
    | -0.694072425 | null                | Isabelle Huppert must be one of the... |
    |  0.439208865 |                     |                                        |
    |  0.99988997  |                     |                                        |
    | -0.993487895 |                     |                                        |
    | ...          |                     |                                        |
    +--------------+---------------------+----------------------------------------+
    

    清除所用資源

    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.