首页
/ Inseq 开源项目教程

Inseq 开源项目教程

2024-09-21 17:10:03作者:胡易黎Nicole

1. 项目介绍

Inseq 是一个基于 PyTorch 的可定制工具包,旨在简化对序列生成模型的解释性分析。它支持各种常见的后置解释性分析方法,包括基于梯度、内部和基于扰动的技术。Inseq 允许用户对各种序列生成模型(如编码器-解码器模型和仅解码器模型)进行特征归因,并提供多种归因方法和可视化工具。

2. 项目快速启动

Inseq 可以通过 PyPI 进行安装,支持 Python 3.10 到 3.12 版本。安装命令如下:

pip install inseq

此外,您还可以安装包含可视化功能的 Inseq 版本,以便在 Jupyter Notebook 中使用:

pip install inseq[notebook]

对于开发者,可以从 GitHub 克隆仓库并安装:

git clone https://github.com/inseq-team/inseq.git
cd inseq
make install

3. 应用案例和最佳实践

3.1 特征归因

以下示例使用 Integrated Gradients 方法对英文-法文翻译模型进行归因分析:

import inseq
model = inseq.load_model("Helsinki-NLP/opus-mt-en-fr", "integrated_gradients")
out = model.attribute("The developer argued with the designer because her idea cannot be implemented", n_steps=100)
out.show()

3.2 可视化

Inseq 支持在 Jupyter Notebook、浏览器和命令行中进行可视化。以下示例展示了在 Jupyter Notebook 中的可视化效果:

import inseq
model = inseq.load_model("gpt2", "integrated_gradients")
model.attribute("Hello ladies and", generation_args=["max_new_tokens": 9], n_steps=500, internal_batch_size=50).show()

4. 典型生态项目

Inseq 与多个开源项目兼容,例如 🤗 Transformers 和 Captum。这使得 Inseq 可以轻松集成到现有的深度学习工作流程中,并与其他解释性分析工具一起使用。

登录后查看全文
热门项目推荐