小编Pee*_*eet的帖子

如何获取 Huggingface Transformer 模型预测 [零样本分类] 的 SHAP 值?

通过 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]}
Run Code Online (Sandbox Code Playgroud)

我正在尝试提取 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 = …
Run Code Online (Sandbox Code Playgroud)

transformer-model pytorch shap huggingface-transformers

8
推荐指数
1
解决办法
3985
查看次数

用列表中的配对字符串之前的数字替换大熊猫中的字符串

鉴于以下熊猫df:

import pandas as pd

df = pd.DataFrame({'1' : ['title1','R','R','R'],
               '2' : ["title2", "NR" ,"NR", "NR"],
               '3' : ["title3", "R" , "NR", "NR"],
               '4' : ["title4", "R", "NR", "R"]})
Run Code Online (Sandbox Code Playgroud)

以及更长的字符串列表:

List = ['2633', 'title1', '3327', 'title2', '18', 'title3', '5', 'title4', '5835', 'title5', '394', 'title6']
Run Code Online (Sandbox Code Playgroud)

在python环境中是否有可能用字符串列表中每个对标题之前的数字替换df中的标题。

预期产量:

dfnew = pd.DataFrame({'1' : ['2633','R','R','R'],
               '2' : ["3327", "NR" ,"NR", "NR"],
               '3' : ["28", "R" , "NR", "NR"],
               '4' : ["5", "R", "NR", "R"]})

dfnew
    1    2      3   4
0   2633 3327   28  5 …
Run Code Online (Sandbox Code Playgroud)

python dataframe python-3.x pandas

4
推荐指数
2
解决办法
67
查看次数