使用調整和評估功能提升模型效能
本文說明如何建立參照 Vertex AI gemini-2.0-flash-001
模型的 BigQuery ML 遠端模型。接著使用監督式微調,以新訓練資料微調模型,然後使用 ML.EVALUATE
函式評估模型。
如果您需要自訂代管的 Vertex AI 模型,例如難以在提示中簡潔定義模型的預期行為,或是提示無法持續產生預期結果,即可使用調整功能。監督式微調也會透過下列方式影響模型:
- 引導模型回覆特定風格的內容,例如更簡潔或更詳細。
- 教導模型新的行為,例如以特定角色回應提示。
- 讓模型根據新資訊更新自己。
在本教學課程中,目標是讓模型生成的文字風格和內容,盡可能符合提供的基準真相內容。
必要的角色
如要執行本教學課程,您需要下列 Identity and Access Management (IAM) 角色:
- 建立及使用 BigQuery 資料集、連線和模型:
BigQuery 管理員 (
roles/bigquery.admin
)。 - 將權限授予連線的服務帳戶:專案 IAM 管理員 (
roles/resourcemanager.projectIamAdmin
)。
這些預先定義的角色具備執行本文中工作所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
- 建立資料集:
bigquery.datasets.create
- 建立表格:
bigquery.tables.create
- 建立、委派及使用連線:
bigquery.connections.*
- 設定預設連線:
bigquery.config.*
- 設定服務帳戶權限:
resourcemanager.projects.getIamPolicy
和resourcemanager.projects.setIamPolicy
- 建立模型並執行推論:
bigquery.jobs.create
bigquery.models.create
bigquery.models.getData
bigquery.models.updateData
bigquery.models.updateMetadata
事前準備
-
In the Trusted Cloud console, on the project selector page, select or create a Trusted Cloud project.
-
Verify that billing is enabled for your Trusted Cloud project.
-
Enable the BigQuery, BigQuery Connection, Vertex AI, and Compute Engine APIs.
費用
在本文件中,您會使用 Trusted Cloud by S3NS的下列計費元件:
- 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 processing that you perform in BigQuery ML.
- Vertex AI: You incur costs for calls to and
supervised tuning of the
gemini-1.0-flash-002
model.
如要根據預測用量估算費用,請使用 Pricing Calculator。
詳情請參閱下列資源:
建立資料集
建立 BigQuery 資料集來儲存機器學習模型。
控制台
前往 Trusted Cloud 控制台的「BigQuery」頁面。
在「Explorer」窗格中,按一下專案名稱。
依序點按
「View actions」(查看動作) >「Create dataset」(建立資料集)。在「建立資料集」頁面中,執行下列操作:
在「Dataset ID」(資料集 ID) 中輸入
bqml_tutorial
。針對「Location type」(位置類型) 選取「Multi-region」(多區域),然後選取「US (multiple regions in United States)」(us (多個美國區域))。
其餘設定請保留預設狀態,然後按一下「Create dataset」(建立資料集)。
bq
如要建立新的資料集,請使用 bq mk
指令搭配 --location
旗標。如需可能的完整參數清單,請參閱 bq mk --dataset
指令參考資料。
建立名為「
bqml_tutorial
」的資料集,並將資料位置設為「US
」,以及說明設為「BigQuery ML tutorial dataset
」:bq --location=US mk -d \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial
這個指令採用
-d
捷徑,而不是使用--dataset
旗標。如果您省略-d
和--dataset
,該指令預設會建立資料集。確認資料集已建立完成:
bq ls
API
請呼叫 datasets.insert
方法,搭配已定義的資料集資源。
{ "datasetReference": { "datasetId": "bqml_tutorial" } }
BigQuery DataFrames
在嘗試這個範例之前,請按照使用 BigQuery DataFrames 的 BigQuery 快速入門導覽課程中的 BigQuery DataFrames 設定說明操作。 詳情請參閱 BigQuery DataFrames 參考說明文件。
如要向 BigQuery 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定 ADC」。
建立測試資料表
根據 Hugging Face 的公開 task955_wiki_auto_style_transfer 資料集,建立訓練和評估資料表。
開啟 Cloud Shell。
在 Cloud Shell 中執行下列指令,建立測試和評估資料表:
python3 -m pip install pandas pyarrow fsspec huggingface_hub python3 -c "import pandas as pd; df_train = pd.read_parquet('hf://datasets/Lots-of-LoRAs/task955_wiki_auto_style_transfer/data/train-00000-of-00001.parquet').drop('id', axis=1); df_train['output'] = [x[0] for x in df_train['output']]; df_train.to_json('wiki_auto_style_transfer_train.jsonl', orient='records', lines=True);" python3 -c "import pandas as pd; df_valid = pd.read_parquet('hf://datasets/Lots-of-LoRAs/task955_wiki_auto_style_transfer/data/valid-00000-of-00001.parquet').drop('id', axis=1); df_valid['output'] = [x[0] for x in df_valid['output']]; df_valid.to_json('wiki_auto_style_transfer_valid.jsonl', orient='records', lines=True);" bq rm -t bqml_tutorial.wiki_auto_style_transfer_train bq rm -t bqml_tutorial.wiki_auto_style_transfer_valid bq load --source_format=NEWLINE_DELIMITED_JSON bqml_tutorial.wiki_auto_style_transfer_train wiki_auto_style_transfer_train.jsonl input:STRING,output:STRING bq load --source_format=NEWLINE_DELIMITED_JSON bqml_tutorial.wiki_auto_style_transfer_valid wiki_auto_style_transfer_valid.jsonl input:STRING,output:STRING
建立基準模型
透過 Vertex AI gemini-1.0-flash-002
模型建立遠端模型。
前往 Trusted Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式,建立遠端模型:
CREATE OR REPLACE MODEL `bqml_tutorial.gemini_baseline` REMOTE WITH CONNECTION DEFAULT OPTIONS (ENDPOINT ='gemini-2.0-flash-001');
查詢作業會在幾秒內完成,完成後,
gemini_baseline
模型會顯示在「Explorer」(探索工具) 窗格的bqml_tutorial
資料集中。由於查詢使用CREATE MODEL
陳述式建立模型,因此不會有查詢結果。
查看基準模型成效
使用遠端模型執行 ML.GENERATE_TEXT
函式,查看模型在評估資料上的成效,無需進行任何調整。
前往 Trusted Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式:
SELECT ml_generate_text_llm_result, ground_truth FROM ML.GENERATE_TEXT( MODEL `bqml_tutorial.gemini_baseline`, ( SELECT input AS prompt, output AS ground_truth FROM `bqml_tutorial.wiki_auto_style_transfer_valid` LIMIT 10 ), STRUCT(TRUE AS flatten_json_output));
如果您檢查輸出資料並比較
ml_generate_text_llm_result
和ground_truth
值,會發現基準模型產生的文字雖然準確反映了真實內容中提供的資訊,但風格相當不同。
評估基準模型
如要更詳細地評估模型成效,請使用 ML.EVALUATE
函式。這項函式會計算模型指標,用來評估生成文字的準確度和品質,藉此比較模型回覆與理想回覆。
前往 Trusted Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式:
SELECT * FROM ML.EVALUATE( MODEL `bqml_tutorial.gemini_baseline`, ( SELECT input AS input_text, output AS output_text FROM `bqml_tutorial.wiki_auto_style_transfer_valid` ), STRUCT('text_generation' AS task_type));
輸出看起來類似以下內容:
+---------------------+---------------------+-------------------------------------------+--------------------------------------------+ | bleu4_score | rouge-l_precision | rouge-l_recall | rouge-l_f1_score | evaluation_status | +---------------------+---------------------+---------------------+---------------------+--------------------------------------------+ | 0.23317359667074181 | 0.37809145226740043 | 0.45902937167791508 | 0.40956844061733139 | { | | | | | | "num_successful_rows": 176, | | | | | | "num_total_rows": 176 | | | | | | } | +---------------------+---------------------+ --------------------+---------------------+--------------------------------------------+
從評估指標來看,基準模型的效能表現還不錯,但生成的文字與基準真相的相似度偏低。這表示值得執行監督式調整,看看是否能改善這個用途的模型效能。
建立調整後模型
建立與「建立模型」中建立的遠端模型非常相似的模型,但這次要指定 AS SELECT
子句,提供訓練資料來調整模型。
前往 Trusted Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式,建立遠端模型:
CREATE OR REPLACE MODEL `bqml_tutorial.gemini_tuned` REMOTE WITH CONNECTION DEFAULT OPTIONS ( endpoint = 'gemini-2.0-flash-001', max_iterations = 500, data_split_method = 'no_split') AS SELECT input AS prompt, output AS label FROM `bqml_tutorial.wiki_auto_style_transfer_train`;
查詢需要幾分鐘才會完成,完成後,
gemini_tuned
模型會顯示在「Explorer」(探索工具) 窗格的bqml_tutorial
資料集中。由於查詢是使用CREATE MODEL
建立模型,因此不會有查詢結果。
查看微調模型的成效
執行 ML.GENERATE_TEXT
函式,查看微調模型在評估資料上的成效。
前往 Trusted Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式:
SELECT ml_generate_text_llm_result, ground_truth FROM ML.GENERATE_TEXT( MODEL `bqml_tutorial.gemini_tuned`, ( SELECT input AS prompt, output AS ground_truth FROM `bqml_tutorial.wiki_auto_style_transfer_valid` LIMIT 10 ), STRUCT(TRUE AS flatten_json_output));
如果檢查輸出資料,會發現微調模型產生的文字風格與基準真相內容更為相似。
評估微調後的模型
使用 ML.EVALUATE
函式,比較微調模型的回覆與理想回覆。
前往 Trusted Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式:
SELECT * FROM ML.EVALUATE( MODEL `bqml_tutorial.gemini_tuned`, ( SELECT input AS prompt, output AS label FROM `bqml_tutorial.wiki_auto_style_transfer_valid` ), STRUCT('text_generation' AS task_type));
輸出看起來類似以下內容:
+---------------------+---------------------+-------------------------------------------+--------------------------------------------+ | bleu4_score | rouge-l_precision | rouge-l_recall | rouge-l_f1_score | evaluation_status | +---------------------+---------------------+---------------------+---------------------+--------------------------------------------+ | 0.416868792119966 | 0.642001000843349 | 0.55910008048151372 | 0.5907226262084847 | { | | | | | | "num_successful_rows": 176, | | | | | | "num_total_rows": 176 | | | | | | } | +---------------------+---------------------+ --------------------+---------------------+--------------------------------------------+
您可以看到,雖然訓練資料集只使用了 1,408 個範例,但評估指標較高,表示成效顯著提升。
清除所用資源
- In the Trusted 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.