Open Ended GPT2 Text Generation Explanations
This notebook demonstrates how to get explanations for the output of gpt2 used for open ended text generation. In this demo, we use the pretrained gpt2 model provided by hugging face (https://huggingface.co/gpt2 ) to explain the generated text by gpt2. We further showcase how to get explanations for custom output generated text and plot global input token importances for any output generated token.
Load model and tokenizer
Below, we set certain model configurations. We need to define if the model is a decoder or encoder-decoder. This can be set through the ‘is_decoder’ or ‘is_encoder_decoder’ param in model’s config file. We can also set custom model generation parameters which will be used during the output text generation decoding process.
Create an explainer object and compute the SHAP values
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
Visualize shap explanations
outputs
,
but
I
'm
not
sure
if
I
'll
ever
be
able
to
0 -2 -4 2 4 -4.04941 -4.04941 base value -1.27522 -1.27522 f, (inputs) 4.064 dog 0.072 with -0.431 enjoy -0.427 walking -0.238 cute -0.15 my -0.117 I -3 -4 -5 -2 -1 0 -4.04941 -4.04941 base value -1.27522 -1.27522 f, (inputs) 4.064 dog 0.072 with -0.431 enjoy -0.427 walking -0.238 cute -0.15 my -0.117 I 0 -2 -4 2 4 -3.54949 -3.54949 base value -1.89655 -1.89655 fbut (inputs) 1.015 enjoy 0.873 I 0.378 cute 0.311 with -0.781 dog -0.112 my -0.029 walking -3 -4 -2 -1 -3.54949 -3.54949 base value -1.89655 -1.89655 fbut (inputs) 1.015 enjoy 0.873 I 0.378 cute 0.311 with -0.781 dog -0.112 my -0.029 walking 0 -2 -4 2 4 -2.10979 -2.10979 base value -0.929316 -0.929316 fI (inputs) 1.005 I 0.405 my 0.023 enjoy 0.016 dog -0.16 cute -0.107 with -0.002 walking -1.5 -1.8 -2.1 -2.4 -1.2 -0.9 -2.10979 -2.10979 base value -0.929316 -0.929316 fI (inputs) 1.005 I 0.405 my 0.023 enjoy 0.016 dog -0.16 cute -0.107 with -0.002 walking 0 -2 -4 2 4 -1.89837 -1.89837 base value -1.8425 -1.8425 f'm (inputs) 0.168 cute 0.126 I 0.106 my -0.158 enjoy -0.082 with -0.068 dog -0.035 walking -1.9 -2 -2.1 -2.2 -1.8 -1.7 -1.6 -1.5 -1.89837 -1.89837 base value -1.8425 -1.8425 f'm (inputs) 0.168 cute 0.126 I 0.106 my -0.158 enjoy -0.082 with -0.068 dog -0.035 walking 0 -2 -4 2 4 -1.16268 -1.16268 base value -1.02646 -1.02646 fnot (inputs) 0.199 I 0.167 enjoy 0.018 with 0.003 cute -0.196 walking -0.041 dog -0.014 my -1.1 -1.2 -1.3 -1.4 -1 -0.9 -0.8 -1.16268 -1.16268 base value -1.02646 -1.02646 fnot (inputs) 0.199 I 0.167 enjoy 0.018 with 0.003 cute -0.196 walking -0.041 dog -0.014 my 0 -2 -4 2 4 -0.883383 -0.883383 base value -1.38105 -1.38105 fsure (inputs) 0.408 with 0.149 enjoy 0.08 dog 0.067 cute -0.469 my -0.41 I -0.323 walking -1.1 -1.4 -1.7 -2 -0.8 -0.5 -0.2 -0.883383 -0.883383 base value -1.38105 -1.38105 fsure (inputs) 0.408 with 0.149 enjoy 0.08 dog 0.067 cute -0.469 my -0.41 I -0.323 walking 0 -2 -4 2 4 -1.47763 -1.47763 base value -0.953806 -0.953806 fif (inputs) 0.24 cute 0.188 walking 0.062 dog 0.053 with 0.036 enjoy 0.009 my -0.064 I -1.2 -1.3 -1.4 -1.5 -1.1 -1 -0.9 -1.47763 -1.47763 base value -0.953806 -0.953806 fif (inputs) 0.24 cute 0.188 walking 0.062 dog 0.053 with 0.036 enjoy 0.009 my -0.064 I 0 -2 -4 2 4 -1.57991 -1.57991 base value -0.735161 -0.735161 fI (inputs) 0.406 I 0.356 enjoy 0.204 my 0.171 walking -0.183 dog -0.094 with -0.016 cute -1.2 -1.4 -1.6 -1.8 -1 -0.8 -0.6 -1.57991 -1.57991 base value -0.735161 -0.735161 fI (inputs) 0.406 I 0.356 enjoy 0.204 my 0.171 walking -0.183 dog -0.094 with -0.016 cute 0 -2 -4 2 4 -2.1621 -2.1621 base value -1.84185 -1.84185 f'll (inputs) 0.457 enjoy 0.061 cute 0.031 dog -0.13 I -0.047 my -0.046 walking -0.005 with -2 -2.1 -2.2 -2.3 -2.4 -1.9 -1.8 -1.7 -2.1621 -2.1621 base value -1.84185 -1.84185 f'll (inputs) 0.457 enjoy 0.061 cute 0.031 dog -0.13 I -0.047 my -0.046 walking -0.005 with 0 -2 -4 2 4 -1.20951 -1.20951 base value -0.811076 -0.811076 fever (inputs) 0.261 dog 0.232 walking 0.07 cute 0.013 I 0.012 enjoy -0.151 with -0.038 my -1 -1.1 -1.2 -1.3 -1.4 -0.9 -0.8 -0.7 -1.20951 -1.20951 base value -0.811076 -0.811076 fever (inputs) 0.261 dog 0.232 walking 0.07 cute 0.013 I 0.012 enjoy -0.151 with -0.038 my 0 -2 -4 2 4 -1.54094 -1.54094 base value -0.869925 -0.869925 fbe (inputs) 0.392 walking 0.128 my 0.078 I 0.032 with 0.03 cute 0.016 enjoy -0.005 dog -1.2 -1.3 -1.4 -1.5 -1.1 -1 -0.9 -1.54094 -1.54094 base value -0.869925 -0.869925 fbe (inputs) 0.392 walking 0.128 my 0.078 I 0.032 with 0.03 cute 0.016 enjoy -0.005 dog 0 -2 -4 2 4 1.499 1.499 base value 1.44732 1.44732 fable (inputs) 0.203 walking 0.145 dog 0.11 my 0.088 with -0.333 I -0.241 cute -0.023 enjoy 1.5 1.3 1.1 0.9 1.7 1.9 1.499 1.499 base value 1.44732 1.44732 fable (inputs) 0.203 walking 0.145 dog 0.11 my 0.088 with -0.333 I -0.241 cute -0.023 enjoy 0 -2 -4 2 4 5.05761 5.05761 base value 6.52183 6.52183 fto (inputs) 0.758 cute 0.421 dog 0.171 walking 0.123 my 0.089 I 0.058 enjoy -0.156 with 5.8 5.5 5.2 4.9 6.1 6.4 6.7 5.05761 5.05761 base value 6.52183 6.52183 fto (inputs) 0.758 cute 0.421 dog 0.171 walking 0.123 my 0.089 I 0.058 enjoy -0.156 with
Another example…
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
outputs
in
the
coming
days
.
-3 -5 -7 -1 1 -4.7396 -4.7396 base value -1.7384 -1.7384 fin (inputs) 1.436 Earth 1.28 collide 0.489 with 0.442 Scientists 0.24 worst 0.221 asteroid 0.093 massive 0.075 outcome -0.385 the -0.365 : -0.168 the -0.166 will -0.156 possible -0.035 confirmed -3 -4 -5 -6 -2 -1 -4.7396 -4.7396 base value -1.7384 -1.7384 fin (inputs) 1.436 Earth 1.28 collide 0.489 with 0.442 Scientists 0.24 worst 0.221 asteroid 0.093 massive 0.075 outcome -0.385 the -0.365 : -0.168 the -0.166 will -0.156 possible -0.035 confirmed -3 -5 -7 -1 1 -1.88136 -1.88136 base value -1.90419 -1.90419 fthe (inputs) 0.308 the 0.291 with 0.252 collide 0.251 the 0.065 possible -0.308 will -0.26 Scientists -0.203 asteroid -0.145 Earth -0.117 outcome -0.074 confirmed -0.062 worst -0.011 massive -0.009 : -1.9 -2.3 -2.7 -3.1 -1.5 -1.1 -0.7 -1.88136 -1.88136 base value -1.90419 -1.90419 fthe (inputs) 0.308 the 0.291 with 0.252 collide 0.251 the 0.065 possible