【发布时间】:2021-07-27 12:47:34
【问题描述】:
我正在尝试提高 SpaCy 二进制文本分类模型的可解释性,该模型是通过使用 SHAP 解释预测来训练的。这是我迄今为止尝试过的(遵循this 教程):
nlp = spacy.load("my_model") # load my model
explainer = shap.Explainer(nlp_predict)
shap_values = explainer(["This is an example"])
但我得到AttributeError: 'str' object has no attribute 'shape'。 nlp_predict 是我编写的一种方法,它采用文本列表并以教程中使用的格式输出每个文本的预测概率。我在这里错过了什么?
这是我的格式化函数:
def nlp_predict(texts):
result = []
for text in texts:
prediction = nlp_fn(text) # This returns label probability but in the wrong format
sub_result = []
sub_result.append({'label': 'label1', 'score': prediction["label1"]})
sub_result.append({'label': 'label2', 'score': prediction["label2"]})
result.append(sub_result)
return(result)
这是他们在教程中使用的预测格式(针对 2 个数据点):
[[{'label': 'label1', 'score': 2.850986311386805e-05},
{'label': 'label2', 'score': 0.9999715089797974}],
[{'label': 'label1', 'score': 0.00010622951958794147},
{'label': 'label2', 'score': 0.9998937845230103}]]
【问题讨论】:
-
我认为您在这里没有提供足够的信息来弄清楚发生了什么。你能提供整个堆栈跟踪吗? spaCy 又从何而来?
-
在本教程中,他们使用了具有特定格式输出的 transformers.pipeline(每个文本的列表 -> 每个类别的列表 -> 具有键类别和值预测的字典),但是 spacy 有不同的预测格式,所以我想如果我把我的预测放在相同的格式中它应该可以工作,但我仍然收到这个错误消息。
-
@GSwart,你最终弄清楚如何在 spaCy 中使用 SHAP 了吗?