【发布时间】:2021-12-06 05:48:12
【问题描述】:
通过 Huggingface 给定一个零样本分类任务,如下所示:
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
example_text = "This is an example text about snowflakes in the summer"
labels = ["weather", "sports", "computer industry"]
output = classifier(example_text, labels, multi_label=True)
output
{'sequence': 'This is an example text about snowflakes in the summer',
'labels': ['weather', 'sports'],
'scores': [0.9780895709991455, 0.021910419687628746]}
我正在尝试提取 SHAP 值来为预测结果生成基于文本的解释,如下所示:SHAP for Transformers
我已经根据上面的网址尝试了以下方法:
from transformers import AutoModelForSequenceClassification, AutoTokenizer, ZeroShotClassificationPipeline
model = AutoModelForSequenceClassification.from_pretrained('facebook/bart-large-mnli')
tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-mnli')
pipe = ZeroShotClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
def score_and_visualize(text):
prediction = pipe([text])
print(prediction[0])
explainer = shap.Explainer(pipe)
shap_values = explainer([text])
shap.plots.text(shap_values)
score_and_visualize(example_text)
有什么建议吗?提前感谢您的帮助!
除了上述管道之外,以下方法也可以使用:
from transformers import AutoModelForSequenceClassification, AutoTokenizer, ZeroShotClassificationPipeline
model = AutoModelForSequenceClassification.from_pretrained('facebook/bart-large-mnli')
tokenizer = AutoTokenizer.from_pretrained('facebook/bart-large-mnli')
classifier = ZeroShotClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
example_text = "This is an example text about snowflakes in the summer"
labels = ["weather", "sports"]
output = classifier(example_text, labels)
output
{'sequence': 'This is an example text about snowflakes in the summer',
'labels': ['weather', 'sports'],
'scores': [0.9780895709991455, 0.021910419687628746]}
【问题讨论】:
标签: pytorch huggingface-transformers transformer shap