使用shap库解读huggingface分类模型

需要安装transformersshapmatplotlibipython等库。

Pycharm会显示 <IPython.core.display.HTML object>,只能在jupyter notebook中正常显示。

import shap
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import transformers

model_dir = "roberta/trained_model"

tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForSequenceClassification.from_pretrained(model_dir, num_labels=2)

classifier = transformers.pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=1)

explainer = shap.Explainer(classifier)
text = ["应用软件(Application software)是指专门用于满足用户特定需求的软件。"]
shap_values = explainer(text)
shap.plots.text(shap_values)

在这里插入图片描述

from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = 'AR PL UKai CN'
plt.rcParams['axes.unicode_minus'] = False
shap.plots.bar(shap_values[0, :, 1], order=shap.Explanation.argsort.flip)

在这里插入图片描述
其中,plt.rcParams['font.sans-serif'] = 'AR PL UKai CN'这句是为了在jupyter中显示中文。下面这段代码用于显示系统中所有字体。

from matplotlib.font_manager import FontManager
fm = FontManager()
my_fonts = set(f.name for f in fm.ttflist)
my_fonts

怎样用SHAP VALUE 解读深度学习 NLP 模型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值