如何在空间模型中使用shap?

rqdpfwrv  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(185)

我正在努力提高一篇文章的可解释性 SpaCy 通过使用 SHAP . 以下是我迄今为止尝试过的内容(在本教程之后):

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}]]

我的函数的输出与此匹配,但我仍然得到 AttributeError .

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题