【发布时间】:2022-01-13 13:07:55
【问题描述】:
我正在尝试构建我的 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 文件执行时,没有任何反应。它既不显示任何内容,也不抛出错误。我的控制台只是在执行时返回这个:
>
如何在流光中显示我的图表?
【问题讨论】: