importnlpimportnumpyasnpimportscipyasspimporttorchimporttransformersimportshap# load a BERT sentiment analysis modeltokenizer=transformers.DistilBertTokenizerFast.from_pretrained("distilbert-base-uncased")model=transformers.DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").cuda()# define a prediction functiondeff(x):tv=torch.tensor([tokenizer.encode(v,padding="max_length",max_length=500,truncation=True)forvinx]).cuda()outputs=model(tv)[0].detach().cpu().numpy()scores=(np.exp(outputs).T/np.exp(outputs).sum(-1)).Tval=sp.special.logit(scores[:,1])# use one vs rest logit unitsreturnval# build an explainer using a token maskerexplainer=shap.Explainer(f,tokenizer)# explain the model's predictions on IMDB reviewsimdb_train=nlp.load_dataset("imdb")["train"]shap_values=explainer(imdb_train[:10],fixed_context=1)
当我们将多行解释对象传递给文本图时,我们会得到每个输入实例的单实例图,这些图经过缩放,因此它们具有一致的可比较的 x 轴和颜色范围。
[11]:
# plot the first sentence's explanationshap.plots.text(shap_values[:3])
Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
第 0 个实例
-2.165315base value-5.158718-8.152122-11.1455260.8280893.821492-3.729354-3.729354f(x)1.306The scramble to survive financially, the insightful students who can see right through their pathetic teachers' pomp, the pettiness of the whole situation, all remind me of the schools I knew and their students. When I saw the episode in which a student repeatedly tried to burn down the school, I immediately recalled ......... at .........0.153other programs about school life,0.098Bromwell High is0.085STUDENT: Welcome to Bromwell High.0.022think that Bromwell0.00.0-0.396ran-0.329"Teachers".-0.319My 35 years in the teaching profession lead-0.318satire is much closer to reality than is-0.275same time as some-0.216High is-0.177What a pity that it isn't!-0.168a cartoon comedy-0.143It-0.128m here to sack one of your teachers.-0.121such as "Teachers".-0.116.-0.115A classic line: INSPECTOR: I'-0.101me to believe that Bromwell High's-0.1fetched-0.058at the-0.051.-0.04I expect that many adults of my age-0.033. High.-0.026far
0.0
0.098 / 5
Bromwell High is
-0.168 / 3
a cartoon comedy
-0.051
.
-0.143
It
-0.396
ran
-0.058 / 2
at the
-0.275 / 4
same time as some
0.153 / 6
other programs about school life,
-0.121 / 6
such as "Teachers".
-0.319 / 8
My 35 years in the teaching profession lead
-0.101 / 10
me to believe that Bromwell High's
-0.318 / 8
satire is much closer to reality than is
-0.329 / 4
"Teachers".
1.306 / 82
The scramble to survive financially, the insightful students who can see right through their pathetic teachers' pomp, the pettiness of the whole situation, all remind me of the schools I knew and their students. When I saw the episode in which a student repeatedly tried to burn down the school, I immediately recalled ......... at .........
-0.033 / 3
. High.
-0.115 / 8
A classic line: INSPECTOR: I'
-0.128 / 9
m here to sack one of your teachers.
0.085 / 9
STUDENT: Welcome to Bromwell High.
-0.04 / 8
I expect that many adults of my age
0.022 / 5
think that Bromwell
-0.216 / 2
High is
-0.026
far
-0.1 / 2
fetched
-0.116
.
-0.177 / 9
What a pity that it isn't!
0.0
第 1 个实例
-0.722620base value-3.716024-6.709427-9.7028312.2707845.264187-4.128328-4.128328f(x)1.915it shows a tender side compared to his slapstick work such as Blazing Saddles,1.385films where prior to being a comedy,1.159Maybe they should give it to the homeless instead of using it like Monopoly money.<br /><br />Or maybe this film will inspire you to help others.0.838<br /><br />While the love connection between Molly0.557The bet's on where Bolt is thrown on the street with a bracelet on his leg to monitor his every move where he can't step off the sidewalk.0.386it's fight or flight, kill or be killed.0.324to show what it's like having something valuable before losing it the next day or on the other hand making a stupid bet like all rich people do when they don't know what to do with their money.0.3be one of Mel Brooks' observant0.119Young Frankenstein, or Spaceballs for the matter,0.046and her pals Sailor (Howard Morris) and Fumes (Teddy Wilson) who are already used to the streets. They're survivors. Bolt isn't.0.00.0-2.169Stinks-2.105I found-1.711"Life-0.948necessary to plot,-0.909not used-0.451" to-0.407He's given the nickname Pepto by a vagrant after it's written on his forehead where Bolt meets other-0.378Homelessness (or Houselessness as George Carlin stated) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter.-0.347to reaching-0.275He's-0.269mutual agreements like he once did when being rich where-0.2Most people think of the homeless as just a lost cause while worrying about things such as racism, the war on Iraq, pressuring kids to succeed, technology, the elections, inflation, or worrying if they'll be next to end up on the streets.-0.154and Bolt wasn't-0.086<br /><br />But what if you were given a bet to live on the streets for a month without the luxuries you once had from a home, the entertainment sets, a bathroom, pictures on the wall, a computer, and everything you once treasure to see what it's like to be homeless? That is Goddard Bolt's lesson.<br /><br />Mel Brooks (who directs) who stars as Bolt plays a rich man who has everything in the world until deciding to make a bet with a sissy rival (Jeffery Tambor) to see if he can live in the streets for thirty days without the luxuries; if Bolt succeeds, he can do what he wants with a future project of making more buildings.-0.024characters including a woman by the name of Molly (Lesley Ann Warren) an ex-dancer who got divorce before losing her home,
0.0
-0.378 / 49
Homelessness (or Houselessness as George Carlin stated) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter.
-0.2 / 52
Most people think of the homeless as just a lost cause while worrying about things such as racism, the war on Iraq, pressuring kids to succeed, technology, the elections, inflation, or worrying if they'll be next to end up on the streets.
-0.086 / 157
<br /><br />But what if you were given a bet to live on the streets for a month without the luxuries you once had from a home, the entertainment sets, a bathroom, pictures on the wall, a computer, and everything you once treasure to see what it's like to be homeless? That is Goddard Bolt's lesson.<br /><br />Mel Brooks (who directs) who stars as Bolt plays a rich man who has everything in the world until deciding to make a bet with a sissy rival (Jeffery Tambor) to see if he can live in the streets for thirty days without the luxuries; if Bolt succeeds, he can do what he wants with a future project of making more buildings.
0.557 / 33
The bet's on where Bolt is thrown on the street with a bracelet on his leg to monitor his every move where he can't step off the sidewalk.
-0.407 / 24
He's given the nickname Pepto by a vagrant after it's written on his forehead where Bolt meets other
-0.024 / 26
characters including a woman by the name of Molly (Lesley Ann Warren) an ex-dancer who got divorce before losing her home,
0.046 / 34
and her pals Sailor (Howard Morris) and Fumes (Teddy Wilson) who are already used to the streets. They're survivors. Bolt isn't.
-0.275 / 3
He's
-0.909 / 2
not used
-0.347 / 2
to reaching
-0.269 / 10
mutual agreements like he once did when being rich where
0.386 / 12
it's fight or flight, kill or be killed.
0.838 / 14
<br /><br />While the love connection between Molly
-0.154 / 5
and Bolt wasn't
-0.948 / 4
necessary to plot,
-2.105 / 2
I found
-1.711 / 2
"Life
-2.169 / 2
Stinks
-0.451 / 2
" to
0.3 / 9
be one of Mel Brooks' observant
1.385 / 8
films where prior to being a comedy,
1.915 / 17
it shows a tender side compared to his slapstick work such as Blazing Saddles,
0.119 / 10
Young Frankenstein, or Spaceballs for the matter,
0.324 / 43
to show what it's like having something valuable before losing it the next day or on the other hand making a stupid bet like all rich people do when they don't know what to do with their money.
1.159 / 35
Maybe they should give it to the homeless instead of using it like Monopoly money.<br /><br />Or maybe this film will inspire you to help others.
0.0
第 2 个实例
-2.184386base value-5.177789-8.171193-11.1645970.8090183.8024214.3469024.346902f(x)1.598is also0.836Brilliant over-acting by Lesley Ann Warren. Best dramatic hobo lady I have ever seen, and love scenes in clothes warehouse are second to none.0.718superb0.695is a0.441After being accused of0.373in Blazing Saddles.0.322as anything0.299being a turncoat,0.272on0.255The0.24selling out his boss,0.232as good0.215and being dishonest the lawyer of Pepto Bolt shrugs indifferently "I'm a lawyer" he says.0.179The corn on face0.173Three funny words. Jeffrey Tambor, a favorite from the later Larry Sanders show, is fantastic here too as a mad millionaire who wants to crush the ghetto. His character is more malevolent than usual. The hospital scene, and the scene where the homeless invade a demolition site, are all-time classics.0.158take0.143lawyers0.1classic0.087Look for the legs scene and the two big diggers fighting (one bleeds).0.022(which is quite often).0.00.0-0.59.-0.225This movie gets better each time I see it-0.013,
0.0
0.836 / 30
Brilliant over-acting by Lesley Ann Warren. Best dramatic hobo lady I have ever seen, and love scenes in clothes warehouse are second to none.
0.179 / 4
The corn on face
0.695 / 2
is a
0.1
classic
-0.013
,
0.232 / 2
as good
0.322 / 2
as anything
0.373 / 5
in Blazing Saddles.
0.255
The
0.158
take
0.272
on
0.143
lawyers
1.598 / 2
is also
0.718
superb
-0.59
.
0.441 / 4
After being accused of
0.299 / 5
being a turncoat,
0.24 / 5
selling out his boss,
0.215 / 24
and being dishonest the lawyer of Pepto Bolt shrugs indifferently "I'm a lawyer" he says.
0.173 / 63
Three funny words. Jeffrey Tambor, a favorite from the later Larry Sanders show, is fantastic here too as a mad millionaire who wants to crush the ghetto. His character is more malevolent than usual. The hospital scene, and the scene where the homeless invade a demolition site, are all-time classics.
0.087 / 18
Look for the legs scene and the two big diggers fighting (one bleeds).
fromtransformersimportAutoModelForSeq2SeqLM,AutoTokenizerimportshaptokenizer=AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-es")model=AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-es").cuda()s=["In this picture, there are four persons: my father, my mother, my brother and my sister."]explainer=shap.Explainer(model,tokenizer)shap_values=explainer(s)