我正在尝试构建我的NLP项目的仪表板。因此,我使用BERT模型进行预测,SHAP包用于可视化,Streamlit用于创建仪表板:
tokenizer = AutoTokenizer.from_pretrained(model_name_cla)
model = AutoModelForSequenceClassification.from_pretrained(model_name_cla)
labels = ['1- Tarife','2- Dateneingabe','3- Bestätigungsmail','4- Kundenbetreuung','5- Aufwand vom Vergleich bis Abschluss',
'6- After-sales Wechselprozess','7 - Werbung/VX Kommunikation','8 - Sonstiges','9 - Nicht auswertbar']
def f(x):
tv = torch.tensor([tokenizer.encode(v, padding='max_length', max_length=128, truncation=True) for v in x])
attention_mask = (tv!=0).type(torch.int64)
outputs = model(tv,attention_mask=attention_mask)[0].detach().cpu().numpy()
scores = (np.exp(outputs).T / np.exp(outputs).sum(-1)).T
val = sp.special.logit(scores)
return val
text = ['This is just a test']
# build an explainer using a token masker
explainer = shap.Explainer(f, tokenizer, output_names=labels)
shap_values = explainer(text, fixed_context=1)
shap.plots.text(shap_values)
代码在我的jupyter笔记本上工作得很好,但是当我试图以.py文件的形式执行它时,没有任何事情发生。它既不显示任何内容,也不抛出错误。我的控制台在执行时只返回以下内容:
>
如何在流光中显示我的图形?
发布于 2021-12-10 05:15:48
这可以用Streamlit Components
和最新的SHAP v0.36+
(它们定义了一个新的getjs
方法)可视化,以绘制JS SHAP plots
(有些像
summary_plot
这样的情节实际上是Matplotlib
,可以用st.pyplot
绘制)
import streamlit as st
import streamlit.components.v1 as components
def st_shap(plot, height=None):
shap_html = f"<head>{shap.getjs()}</head><body>{plot.html()}</body>"
components.html(shap_html, height=height)
st_shap(shap.plots.text(shap_values),400)
在streamlit
- 用Streamlit显示形状图中找到关于可视化形状的更详细的讨论
发布于 2022-03-15 23:58:08
您可以捕获shap图的输出并使用components.html
呈现它。
import streamlit.components.v1 as components
from IPython.core.interactiveshell import InteractiveShell
from IPython.utils import capture
def st_plot_text_shap(shap_val, height=None)
InteractiveShell().instance()
with capture.capture_output() as cap:
shap.plots.text(shap_val)
components.html(cap.outputs[1].data['text/html'], height=height scrolling=True)
https://stackoverflow.com/questions/70277425
复制相似问题