{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### SHAP for sentiment analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An example of SHAP on sentiment analysis. The SHAP explainer for NLP tasks only supports `TextClassificationPipeline` in the `transformer` library. If using this explainer, please cite the original work: https://github.com/slundberg/shap." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import transformers\n", "from omnixai.data.text import Text\n", "from omnixai.explainers.nlp import ShapText" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# A transformer model for sentiment analysis\n", "model = transformers.pipeline(\n", " 'sentiment-analysis',\n", " model='distilbert-base-uncased-finetuned-sst-2-english',\n", " return_all_scores=True\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/156 [00:00Instance 0: Class POSITIVE\n", "
What a great movie ! if you have no taste .

\n", "
Instance 1: Class POSITIVE
\n", "
it was a fantastic performance !

\n", "
Instance 2: Class POSITIVE
\n", "
best film ever

\n", "
Instance 3: Class POSITIVE
\n", "
such a great show !

\n", "
Instance 4: Class NEGATIVE
\n", "
it was a horrible movie

\n", "
Instance 5: Class POSITIVE
\n", "
i ' ve never watched something as bad

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x = Text([\n", " \"What a great movie! if you have no taste.\",\n", " \"it was a fantastic performance!\",\n", " \"best film ever\",\n", " \"such a great show!\",\n", " \"it was a horrible movie\",\n", " \"i've never watched something as bad\"\n", "])\n", "explainer = ShapText(model=model)\n", "explanations = explainer.explain(x)\n", "explanations.ipython_plot()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }