![]() |
![]() |
![]() |
![]() |
生成式 AI 产品相对较新,其行为差异可能远远超过 早期版本的软件。因此,探究机器学习 查看模型的行为示例, 意外惊喜。
Learning Interpretability Tool(LIT;网站、GitHub) 是一个用于调试和分析机器学习模型,以便了解相关原因和方式的平台 行为方式
在这里,您将了解如何设置 LIT,以便更充分地利用 Google Gemma 模型:使用序列 Salience 模块来分析不同的 提示工程方法。
设置 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+),以及用于下载
基本模型。
# 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 单元中渲染该界面。
lit_widget.render()
<IPython.core.display.Javascript object>
利用序列 Salience 提示进行调试
文本到文本大型语言模型 (LLM)(例如 Gemma)采用输入序列 以词元化文本的形式出现,并生成 逻辑上的跟进或完成。
显著性方法可用于检查文本的哪些部分, 对于模型所生成输出的不同部分而言,输入对模型来说很重要。 LIT 的序列 Salience 模块扩展了这些方法,以说明 从多个词元到词元, 生成句子等。
您可以在上方单元格中使用 LIT 来尝试序列 Salience 构建自己的模块要获得引导性更强的学习体验,您可以关注 借助使用序列 Salience 进行提示调试教程 这个 Colab 中的应用。
如需关于序列 Salience 工作原理的更多学术和技术信息, 请查看我们的论文。
附录:在 Kaggle Hub 上访问 Gemma
此笔记本使用本文档中 Gemma 的 KerasNLP 实现。 KerasNLP 将预训练模型的权重存储在 Kaggle 上,而 Gemma 需要 身份验证和许可确认,以访问这些权重。
以下说明会引导您设置 Kaggle 账号
使用 kagglehub
软件包进行 Kaggle 身份验证。
- 创建一个 Kaggle 账号(如果您还没有)
<ph type="x-smartling-placeholder">
- </ph>
- 访问:https://www.kaggle.com/account/login?phase=startRegisterTab
- 使用您希望采用的任何一种注册方法来设置账号。
- 请求对 Gemma 的访问权限
<ph type="x-smartling-placeholder">
- </ph>
- 确保你已使用以上账号登录 Kaggle
- 前往意见征求页面:https://www.kaggle.com/models/google/gemma/license/consent
- 选中“Verify via Kaggle Account”(通过 Kaggle 账号验证)选项(默认选项),然后点击“下一步”
- 填写用户意见征求表单(名字和姓氏字段位于顶部)
- 使用底部的复选框确认政策
- 点击“接受”按钮以授予访问权限
- 这应该会将您重定向到模型页面 (https://www.kaggle.com/models/google/gemma)
- 创建 API 令牌
<ph type="x-smartling-placeholder">
- </ph>
- 请务必使用您在上面创建的账号登录 Kaggle
- 前往“设置”页面:https://www.kaggle.com/settings
- 向下滚动到 API 部分
- 使用“创建新令牌”用于触发令牌生成的按钮
- 使用屏幕上的菜单将服务生成的 JSON 文件(名为 kaggle.json)保存到计算机上
- JSON 文件是一个包含两个属性(用户名和密钥)的对象,您稍后需要同时使用这两个属性通过其服务进行身份验证
- 在 Colab 中使用您的 API 令牌凭据向 kagglehub 进行身份验证
<ph type="x-smartling-placeholder">
- </ph>
- 转到 LIT 序列 Saleince 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 运行时断开连接时,您都需要重复上一步,因为断开连接会清除存储凭据的缓存。