![]() |
![]() |
![]() |
![]() |
生成式 AI 產品相對新穎,行為變化可能大於 先前的軟體類型這讓探測機器都很重要 檢視模型行為的範例,並 驚嘆號
學習可解釋性工具 (LIT;網站、GitHub) 是一個平台,用於偵錯及分析機器學習模型,以便瞭解原因和做法 他們的行為表現出來
本單元將說明如何設定 LIT,讓 Google Gemma 模型:使用「序列顯著性」模組分析 及提示工程的方法
設定 LIT 以便對 Gemma 提示進行偵錯
ERROR: pip's dependency resolver does not currently take into account all the
packages that are installed. This behaviour is the source of the following
dependency conflicts.
bigframes 0.21.0 requires scikit-learn>=1.2.2, but you have scikit-learn 1.0.2
which is incompatible.
google-colab 1.0.0 requires ipython==7.34.0, but you have ipython 8.14.0
which is incompatible.
您可以放心忽略這些警告
安裝 LIT 和 Keras NLP
這個筆記本使用 Gemma 的 KerasNLP 實作 (進一步瞭解如何
請於下方進行設定)。您需要最新版的 keras
(3.0 以上版本)
keras-nlp
(0.12 以上版本) 和 lit-nlp
(1.2 以上版本),以及 Kaggle 帳戶,以便下載
基礎模型
# Keras is included in Colab runtimes, but needs to be updated to to v3.0+.
# LIT and Keras NLP are not icnldued by default and must be installed.
# Running this cell may require you to restart your session to ensure the newer
# packages are imported correctly.
pip install -q -U "keras >= 3.0, <4.0" "keras-nlp >= 0.14" "lit-nlp >= 1.2"
Kaggle 存取權
KerasNLP 將預先訓練模型權重儲存在 Kaggle 上。
使用 kagglehub
套件
進行驗證請務必一併接受授權協議
Gemma。
如要進一步瞭解如何設定 Kaggle,請參閱結尾的附錄 讓他們使用服務帳戶
import kagglehub
kagglehub.login()
設定 LIT
LIT 提供用於設定提示的 make_notebook_widget()
函式
筆記本結構定義中的偵錯工具
LIT 提供一系列範例提示資料集, 。
請參閱以下註解,瞭解如何設定小工具使用不同的模型,和/或
from lit_nlp.examples.prompt_debugging import notebook as lit_pdbnb
# The following function initializes a LIT Notebook Widget. It's configured by
# two required positional arguments:
#
# * `datasets_config`: A list of strings containing the dataset names and
# paths to load from, as "dataset:path", where path can be a URL or a
# local file path. The example below uses a special value,
# `sample_prompts`, to load the example prompts provided in the LIT
# distribution; no other special values are supported.
# * `models_config`: A list of strings containing the model names and paths to
# load from, as "model:path", where path can be a URL, a local file path,
# or the name of a preset for the configured deep learning framework.
#
# LIT supports salience computation for KerasNLP and Hugging Face Transformers
# models running on TensorFlow or PyTorch. Note that all models passed to the
# `models_config` parameter will be loaded using the same framework and runtime.
# You can cofnigre these with the following keywork arguments.
#
# * `dl_framework`: Must be one of "kerasnlp" or "transformers".
# * `dl_runtime`: Must be one of "tensorflow" or "torch".
#
# Changing the `dl_framework` value will affect the authentication method used
# to access Gemma model weights.
lit_widget = lit_pdbnb.make_notebook_widget(
['sample_prompts'],
["gemma_2b_it:gemma_1.1_instruct_2b_en"],
dl_framework="kerasnlp",
dl_runtime="tensorflow",
batch_size=1,
max_examples=5,
precision="bfloat16",
)
你現在可以在 Colab 儲存格中算繪 UI。
lit_widget.render()
<IPython.core.display.Javascript object>
使用序列顯著性進行提示偵錯
Gemma 等文字轉文字大型語言模型 (LLM) 會接收輸入序列 以「代碼化」文字的形式產生新符記, 邏輯後續情形或完成度
語意方法可讓您 對模型生成輸出內容的不同部分而言都很重要 LIT 的序列斷言模組擴充了這些方法, 從分詞到每個精細程度,序列的重要性 來表達自己的意思
你可以在上方儲存格中使用 LIT 測試「序列顯著」效果 自行建構模組如需更多引導式學習體驗,你和 請參閱「使用序列斷層進行提示偵錯」教學課程 安裝於 Colab
如要進一步瞭解 Sequence Salience 如何運作, 請參閱我們的論文。
附錄:在 Kaggle Hub 上存取 Gemma
這個筆記本使用本文件中 Gemma 的 KerasNLP 實作。 KerasNLP 在 Kaggle 上儲存預先訓練模型權重,而 Gemma 需要 驗證及授權許可才能使用這些權重
下列操作說明會逐步引導您設定 Kaggle 帳戶
使用 kagglehub
套件向 Kaggle 進行驗證。
- 如果沒有 Kaggle 帳戶,請建立一個
- 前往 https://www.kaggle.com/account/login?phase=startRegisterTab
- 使用您偏好的重設方法設定您的帳戶。
- 要求存取 Gemma
- 請確認你已使用上述帳戶登入 Kaggle
- 前往同意聲明頁面:https://www.kaggle.com/models/google/gemma/license/consent
- 選取「透過 Kaggle 帳戶進行驗證」選項,然後點選「下一步」
- 填寫同意聲明表單 (頂端的名字和姓氏欄位)
- 使用底部的核取方塊確認政策
- 按一下「接受」以授予存取權
- 這麼做應該會將您重新導向至模型頁面 (https://www.kaggle.com/models/google/gemma)
- 建立 API 權杖
- 請務必使用您在上述步驟中建立的帳戶登入 Kaggle
- 前往「設定」頁面:https://www.kaggle.com/settings
- 向下捲動至「API」部分
- 使用「建立新權杖」觸發權杖產生作業的按鈕
- 使用畫面上的選單儲存名為 kaggle.json 的 JSON 檔案。此服務產生的服務會儲存到電腦上
- JSON 檔案是包含兩個屬性的物件 (使用者名稱和金鑰),稍後您需要使用兩者的服務進行驗證
- 使用 API 權杖憑證在 Colab 中透過 kagglehub 進行驗證
- 前往 LIT 序列銷售計畫 Colab:https://colab.sandbox.google.com/github/google/generative-ai-docs/blob/main/site/en/gemma/docs/lit_gemma.ipynb#scrollTo=yKw8gDsh_nVR
- 連結至 GPU 執行階段
- 對於 Gemma 2B,您可以使用免費方案 T4 執行階段
- 如要使用 Gemma 7B,你必須有 Colab 預付運算抵免額或 Colab Pro 帳戶,才能使用 V100、L4 或 A100 GPU
- 執行
kagglehub
程式碼儲存格,顯示 HTML 表單。此表單會要求您提供使用者名稱和權杖 - 從您在上一個步驟下載的
kaggle.json
檔案中複製username
欄位,並貼到表單的username
欄位中 - 從您在上一個步驟下載的
kaggle.json
檔案中複製key
欄位,並貼到表單的token
欄位中 - 按一下登入按鈕,將這些憑證儲存在執行階段中
每當 Colab 執行階段中斷連線時,都必須重複執行最後一個步驟,因為連線中斷會清除儲存的憑證快取。