使用預先訓練的 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.create
和storage.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 根據預測用量產生預估費用。
詳情請參閱下列資源:
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the BigQuery, BigQuery Connection, and Vertex AI APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the BigQuery, BigQuery Connection, and Vertex AI APIs.
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中輸入以下陳述式:
CREATE SCHEMA `PROJECT_ID.tf_models_tutorial`;
將
PROJECT_ID
替換為您的專案 ID。按一下
「Run」。在 Google Cloud 控制台中啟用 Cloud Shell。
如要建立資料集,請執行
bq mk
指令:bq mk --dataset --location=us PROJECT_ID:tf_models_tutorial
將
PROJECT_ID
替換為您的專案 ID。使用 pip 安裝
bigquery-ml-utils
程式庫:pip install bigquery-ml-utils
產生 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
替換為可暫時儲存模型的本機資料夾路徑。選用步驟:列印產生的模型簽名:
import tensorflow as tf reload_embedding_model = tf.saved_model.load(OUTPUT_MODEL_PATH) print(reload_embedding_model.signatures["serving_default"])
如要將產生的模型從本機資料夾複製到 Cloud Storage 值區,請使用 Google Cloud CLI:
gcloud storage cp OUTPUT_MODEL_PATH gs://BUCKET_PATH/nnlm_model --recursive
將
BUCKET_PATH
替換為您要複製模型的 Cloud Storage 值區名稱。使用 pip 安裝
bigquery-ml-utils
程式庫:pip install bigquery-ml-utils
產生 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
替換為可暫時儲存模型的本機資料夾路徑。選用步驟:列印產生的模型簽名:
import tensorflow as tf reload_embedding_model = tf.saved_model.load(OUTPUT_MODEL_PATH) print(reload_embedding_model.signatures["serving_default"])
如要將產生的模型從本機資料夾複製到 Cloud Storage 值區,請使用 Google Cloud CLI:
gcloud storage cp OUTPUT_MODEL_PATH gs://BUCKET_PATH/swivel_model --recursive
將
BUCKET_PATH
替換為您要複製模型的 Cloud Storage 值區名稱。使用 pip 安裝
bigquery-ml-utils
程式庫:pip install bigquery-ml-utils
產生 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
替換為可暫時儲存模型的本機資料夾路徑。選用步驟:列印產生的模型簽名:
import tensorflow as tf reload_embedding_model = tf.saved_model.load(OUTPUT_MODEL_PATH) print(reload_embedding_model.signatures["serving_default"])
如要將產生的模型從本機資料夾複製到 Cloud Storage 值區,請使用 Google Cloud CLI:
gcloud storage cp OUTPUT_MODEL_PATH gs://BUCKET_PATH/bert_model --recursive
將
BUCKET_PATH
替換為您要複製模型的 Cloud Storage 值區名稱。前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中輸入以下陳述式:
CREATE OR REPLACE MODEL
tf_models_tutorial.nnlm_model
OPTIONS ( model_type = 'TENSORFLOW', model_path = 'gs://BUCKET_NAME/nnlm_model/*');將
BUCKET_NAME
替換為您先前建立的值區名稱。按一下
「Run」。前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中輸入以下陳述式:
CREATE OR REPLACE MODEL
tf_models_tutorial.swivel_model
OPTIONS ( model_type = 'TENSORFLOW', model_path = 'gs://BUCKET_NAME/swivel_model/*');將
BUCKET_NAME
替換為您先前建立的值區名稱。按一下
「Run」。在 Google Cloud 控制台中,前往 Vertex AI「Model registry」頁面。
按一下「匯入」,然後執行下列操作:
- 在「Name」(名稱) 中輸入
BERT
。 - 在「Region」中,選取與 Cloud Storage 值區所在地區相符的地區。
- 在「Name」(名稱) 中輸入
按一下「繼續」,然後執行下列操作:
- 在「Model framework version」(模型架構版本) 中選取
2.8
。 - 在「模型構件位置」中,輸入儲存模型檔案的 Cloud Storage 值區路徑。例如:
gs://BUCKET_PATH/bert_model
。
- 在「Model framework version」(模型架構版本) 中選取
按一下 [匯入]。匯入完成後,模型就會顯示在「Model registry」頁面上。
在 Google Cloud 控制台中,前往 Vertex AI「Model registry」頁面。
按一下模型名稱。
按一下「Deploy & test」(部署及測試)。
按一下「Deploy to endpoint」。
在「端點名稱」中輸入
bert_model_endpoint
。按一下「繼續」。
選取運算資源。
按一下 [Deploy] (部署)。
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中輸入以下陳述式:
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
:專案 IDCONNECTION_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
按一下
「Run」。- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
建立資料集
如要建立名為 tf_models_tutorial
的資料集,以便儲存您建立的模型,請選取下列其中一個選項:
SQL
如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。
bq
產生模型並上傳至 Cloud Storage
如要進一步瞭解如何使用預先訓練的 TensorFlow 模型產生文字嵌入資料,請參閱 Colab 筆記本。否則請選取下列任一型號:
NNLM
SWIVEL
BERT
將模型載入 BigQuery
請選取下列其中一個型號:
NNLM
使用 CREATE MODEL
陳述式:
如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。
SWIVEL
使用 CREATE MODEL
陳述式:
如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。
BERT
如要將 BERT 模型載入 BigQuery,請將 BERT 模型匯入 Vertex AI,將模型部署至 Vertex AI 端點,建立連線,然後在 BigQuery 中建立遠端模型。
如要將 BERT 模型匯入 Vertex AI,請按照下列步驟操作:
如要將 BERT 模型部署至 Vertex AI 端點,並將其連結至 BigQuery,請按照下列步驟操作:
如要根據 Vertex AI 端點建立遠端模型,請使用 CREATE MODEL
陳述式:
如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」一文。
產生文字嵌入
在本節中,您將使用 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 | | | | ... | | | +--------------+---------------------+----------------------------------------+