Inseq是一种基于Pytorch的可骇客工具包,可民主化在Seq Uence生成模型的特性分析中对可变的途径访问。
inseq可在PYPI上使用,可以使用pip安装python> = 3.10,<= 3.12:
# Install latest stable version
pip install inseq
# Alternatively, install latest development version
pip install git+https://github.com/inseq-team/inseq.git在Jupyter笔记本电脑中安装额外的可视化和?数据集归因为pip install inseq[notebook,datasets] 。
cd inseq
make uv-download # Download and install the UV package manager
make install # Installs the package and all dependencies对于库开发人员,您可以使用make install-dev命令来安装所有开发依赖项(质量,文档,附加功能)。
安装后,您应该能够make fast-test并无误地make lint 。
安装tokenizers软件包需要安装Rust编译器。您可以从https://rustup.rs安装Rust,然后在路径中添加$HOME/.cargo/env 。
安装sentencepiece需要各种软件包,使用sudo apt-get install cmake build-essential pkg-config或brew install cmake gperftools pkg-config 。
此示例使用集成梯度归因方法来归因于从Winomt语料库中获取的句子的英语 - 法语翻译:
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 ()这会产生输入句子中每个令牌的归因分数(令牌级的聚合会自动处理)。这是jupyter笔记本中的可视化看起来像:
InSeq还支持仅使用解码器的模型,例如GPT-2,可以直接从控制台中使用多种归因方法和可自定义的设置:
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 ()来自大多数ForConditionalGeneration (encoder-decoder)和ForCausalLM (仅解码器)模型的序列生成的特征归因?变压器
支持多种功能归因方法,扩展了Captum支持的方法
通过Aggregator类的后处理,过滤和合并归因地图。
笔记本,浏览器和命令行中的归因可视化。
单个示例还是整个示例的有效归因?带有Inseq CLI的数据集。
目标功能的自定义归因,支持高级方法,例如对比功能归因和上下文Reliance检测。
自定义分数(例如概率,熵)的提取和可视化在每个一代步骤中都符合归因地图。
使用inseq.list_feature_attribution_methods函数列出所有可用的方法标识符和inseq.list_step_functions ,以列出所有可用的步骤功能。目前支持以下方法:
saliency :内部卷积网络深处:可视化图像分类模型和显着图(Simonyan等,2013)
input_x_gradient :内部内部卷积网络:可视化图像分类模型和显着图(Simonyan等,2013)
integrated_gradients :深网的公理归因(Sundararajan等,2017)
deeplift :通过传播激活差异来学习重要特征(Shrikumar等,2017)
gradient_shap :一种统一的解释模型预测方法(Lundberg和Lee,2017年)
discretized_integrated_gradients :解释语言模型的离散的集成梯度(Sanyal和Ren,2021)
sequential_integrated_gradients :顺序集成梯度:一种简单但有效的语言模型的方法(Enguehard,2023)
attention :注意力归因,从共同学习和翻译的神经机器翻译(Bahdanau et al。,2014) occlusion :可视化和理解卷积网络(Zeiler和Fergus,2014年)
lime :“我为什么信任你?”:解释任何分类器的预测(Ribeiro等,2016)
value_zeroing :变压器中的量化上下文混合(Mohebbi等,2023)
reagent :试剂:一种生成语言模型的模型 - 不足的特征归因方法(Zhao等,2024)
步骤函数用于在属性过程的每个步骤中从模型中提取自定义得分,并在model.attribute中使用step_scores参数提取自定义分数。它们还可以用作依赖模型输出(例如基于梯度的方法)的归因方法的目标,通过将它们作为attributed_fn参数传递。目前支持以下步骤功能:
logits :目标令牌的逻辑。probability :目标令牌的概率。也可以通过传递logprob=True来用于对数概率。entropy :预测分布的熵。crossentropy :目标令牌和预测分布之间的跨凝性损失。perplexity :目标令牌的困惑。contrast_logits / contrast_prob :当向模型提供不同的对比输入时,目标令牌的逻辑 /概率。当没有提供对比度输入时,等效于logits / probability 。contrast_logits_diff / contrast_prob_diff :原始和箔目标令牌对之间的逻辑 /概率差异,可用于对比度评估,如相反的归因(Yin和Neubig,2022)。pcxmi :针对原始和对比度上下文的目标令牌的点上下文跨点信息(P-CXMI)(Yin等,2021)。kl_divergence :给定原始和对比的环境的预测分布的KL差异。可以使用top_k和top_p参数局限于最有可能的目标令牌选项。in_context_pvi :context Cointsise v-usable信息(PVI)测量模型预测中使用的上下文信息量(Lu等,2023)。mc_dropout_prob_avg :使用MC辍学的多个样本中目标令牌的平均概率(Gal和Ghahramani,2016年)。top_p_size :在模型的预测分布中,累积概率的令牌数大于top_p 。以下示例使用contrast_prob_diff步骤函数计算对比归因:
import inseq
attribution_model = inseq . load_model ( "gpt2" , "input_x_gradient" )
# Perform the contrastive attribution:
# Regular (forced) target -> "The manager went home because he was sick"
# Contrastive target -> "The manager went home because she was sick"
out = attribution_model . attribute (
"The manager went home because" ,
"The manager went home because he was sick" ,
attributed_fn = "contrast_prob_diff" ,
contrast_targets = "The manager went home because she was sick" ,
# We also visualize the corresponding step score
step_scores = [ "contrast_prob_diff" ]
)
out . show ()请参阅文档以获取包括自定义功能注册的示例。
Inseq库还提供了有用的客户端命令,以启用单个示例甚至整个示例的重复归因?直接来自控制台的数据集。在安装软件包后,通过在终端中输入inseq -h来查看可用选项。
支持三个命令:
inseq attribute :用于启用model.attribute的包装器。
inseq attribute-dataset :使用拥抱face datasets.load_dataset API将attribute扩展到完整数据集。
inseq attribute-context :使用Sarti等人的方法来检测和属性上下文依赖性。 (2023)。
所有命令都支持可用于attribute的全部参数,控制台中的归因可视化以及将输出保存到磁盘。
inseq attribute示例下面的示例使用来自transformers的Mariannmt翻译模型对英语句子进行了简单的特征归因。最终结果打印到控制台。
inseq attribute
--model_name_or_path Helsinki-NLP/opus-mt-en-it
--attribution_method saliency
--input_texts " Hello world this is Inseq! Inseq is a very nice library to perform attribution analysis "inseq attribute-dataset示例以下代码可用于使用玛丽安·弗洛雷斯(Flores-101)平行的语料库进行的20个英语句子的假人样本,以执行意大利翻译的归因(源和目标端),并使用玛丽安·弗洛姆特(Flores-101)平行的transformers采集了玛丽安·纳姆特(Mariannmt)翻译模型。我们在文件attributions.html中以HTML格式保存可视化。有关更多选项,请参见--help标志。
inseq attribute-dataset
--model_name_or_path Helsinki-NLP/opus-mt-en-it
--attribution_method saliency
--do_prefix_attribution
--dataset_name inseq/dummy_enit
--input_text_field en
--dataset_split " train[:20] "
--viz_path attributions.html
--batch_size 8
--hideinseq attribute-context示例以下示例使用小的LM生成input_current_text的延续,并使用input_context_text提供的其他上下文来估计其对生成的影响。在这种情况下, "to the hospital. He said he was fine" ,并且根据contrast_prob_diff步骤功能,发现代币hospital的产生依赖于情境代币sick 。
inseq attribute-context
--model_name_or_path HuggingFaceTB/SmolLM-135M
--input_context_text " George was sick yesterday. "
--input_current_text " His colleagues asked him to come "
--attributed_fn " contrast_prob_diff "结果:
支持更多基于注意力和基于闭塞的功能归因方法(#107和#108中记录)。
与雪貂的互操作性,以归因性和忠诚评估。
使用Gradio块在选项卡式界面中的丰富而交互式可视化。
我们对Inseq的愿景是创建一组集中,全面和健壮的工具集,以在序列生成模型的研究中实现公平和可重现的比较。为了实现这一目标,对这些主题感兴趣的研究人员和开发人员的贡献非常受欢迎。有关更多信息,请参阅我们的贡献指南和行为准则。
如果您在研究中使用inseq,我们建议包括提及特定版本(例如v0.6.0),我们请您将参考文件引用为:
@inproceedings { sarti-etal-2023-inseq ,
title = " Inseq: An Interpretability Toolkit for Sequence Generation Models " ,
author = " Sarti, Gabriele and
Feldhus, Nils and
Sickert, Ludwig and
van der Wal, Oskar and
Nissim, Malvina and
Bisazza, Arianna " ,
booktitle = " Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 3: System Demonstrations) " ,
month = jul,
year = " 2023 " ,
address = " Toronto, Canada " ,
publisher = " Association for Computational Linguistics " ,
url = " https://aclanthology.org/2023.acl-demo.40 " ,
doi = " 10.18653/v1/2023.acl-demo.40 " ,
pages = " 421--435 " ,
}
Inseq已用于各种研究项目。使用Inseq进行生成模型的可解释性分析的已知出版物列表如下所示。
提示
上次更新:2024年8月。请打开拉动请求,将您的出版物添加到列表中。