"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "requests"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "API_TOKEN=\"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\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [],
- "source": [
- "import json\n",
- "\n",
- "import requests\n",
- "\n",
- "API_URL = \"https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment\"\n",
- "headers = {\"Authorization\": f\"Bearer {API_TOKEN}\"}\n",
- "\n",
- "def query(payload):\n",
- " data = json.dumps(payload)\n",
- " response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n",
- " return json.loads(response.content.decode(\"utf-8\"))\n",
- "\n",
- "data = query(\"This is very nice\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "Downloading: 100%|██████████| 39.0/39.0 [00:00<00:00, 21.5kB/s]\n",
- "Downloading: 100%|██████████| 953/953 [00:00<00:00, 492kB/s]\n",
- "Downloading: 100%|██████████| 851k/851k [00:00<00:00, 933kB/s] \n",
- "Downloading: 100%|██████████| 112/112 [00:00<00:00, 70.8kB/s]\n",
- "Downloading: 100%|██████████| 638M/638M [00:50<00:00, 13.4MB/s] \n"
- ]
- }
- ],
- "source": [
- "from transformers import AutoModelForSequenceClassification\n",
- "from transformers import AutoTokenizer\n",
- "import numpy as np\n",
- "import pandas as pd\n",
- "from eli5.lime import TextExplainer\n",
- "from typing import List\n",
- "\n",
- "\n",
- "MODEL=\"nlptown/bert-base-multilingual-uncased-sentiment\"\n",
- "\n",
- "tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
- "model = AutoModelForSequenceClassification.from_pretrained(MODEL)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 80,
- "metadata": {},
- "outputs": [],
- "source": [
- "\n",
- "\n",
- "def model_adapter(texts: List[str]):\n",
- " \n",
- " all_scores = []\n",
- "\n",
- " for i in range(0, len(texts), 64):\n",
- "\n",
- " batch = texts[i:i+64]\n",
- " \n",
- " # use bert encoder to tokenize text \n",
- " encoded_input = tokenizer(batch, return_tensors='pt', padding=True, truncation=True, max_length=model.config.max_position_embeddings-2)\n",
- " # run the model\n",
- " output = model(**encoded_input)\n",
- " # by default this model gives raw logits rather than a nice smooth softmax \n",
- " # so we apply it ourselves here\n",
- " scores = output[0].softmax(1).detach().numpy()\n",
- "\n",
- " all_scores.extend(scores)\n",
- "\n",
- " return np.array(all_scores)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 82,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "/home/james/miniconda3/envs/pgesg/lib/python3.7/site-packages/sklearn/base.py:213: FutureWarning: From version 0.24, get_params will raise an AttributeError if a parameter cannot be retrieved as an instance attribute. Previously it would return None.\n",
- " FutureWarning)\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "TextExplainer(char_based=False,\n",
- " clf=SGDClassifier(alpha=0.001, loss='log', penalty='elasticnet',\n",
- " random_state=RandomState(MT19937) at 0x7FE441F8F050),\n",
- " n_samples=1000, random_state=42,\n",
- " sampler=MaskingTextSamplers(random_state=RandomState(MT19937) at 0x7FE441F8F050,\n",
- " sampler_params=None,\n",
- " token_pattern='(?u)\\\\b\\\\w+\\\\b',\n",
- " weights=array([0.7, 0.3])),\n",
- " token_pattern='(?u)\\\\b\\\\w+\\\\b',\n",
- " vec=CountVectorizer(ngram_range=(1, 2),\n",
- " token_pattern='(?u)\\\\b\\\\w+\\\\b'))"
- ]
- },
- "execution_count": 82,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "te = TextExplainer(n_samples=1000, random_state=42)\n",
- "te.fit(\"I was sad and disappointed by this comment\", model_adapter)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 79,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "\n",
- " \n",
- "\n",
- "\n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " y=1 star\n",
- " \n",
- "\n",
- "\n",
- " \n",
- " (probability 0.322, score -0.640)\n",
- "\n",
- "top features\n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " Contribution?\n",
- " | \n",
- " \n",
- " Feature | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " -0.055\n",
- " | \n",
- " \n",
- " Highlighted in text (sum)\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " -0.585\n",
- " | \n",
- " \n",
- " <BIAS>\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- "\n",
- " \n",
- "
\n",
- "\n",
- " \n",
- "\n",
- "\n",
- "\n",
- " \n",
- " i was sad and disappointed by this comment\n",
- "
\n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " y=2 stars\n",
- " \n",
- "\n",
- "\n",
- " \n",
- " (probability 0.514, score 0.209)\n",
- "\n",
- "top features\n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " Contribution?\n",
- " | \n",
- " \n",
- " Feature | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " +0.917\n",
- " | \n",
- " \n",
- " Highlighted in text (sum)\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " -0.708\n",
- " | \n",
- " \n",
- " <BIAS>\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- "\n",
- " \n",
- "
\n",
- "\n",
- " \n",
- "\n",
- "\n",
- "\n",
- " \n",
- " i was sad and disappointed by this comment\n",
- "
\n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " y=3 stars\n",
- " \n",
- "\n",
- "\n",
- " \n",
- " (probability 0.161, score -1.565)\n",
- "\n",
- "top features\n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " Contribution?\n",
- " | \n",
- " \n",
- " Feature | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " -0.603\n",
- " | \n",
- " \n",
- " <BIAS>\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " -0.962\n",
- " | \n",
- " \n",
- " Highlighted in text (sum)\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- "\n",
- " \n",
- "
\n",
- "\n",
- " \n",
- "\n",
- "\n",
- "\n",
- " \n",
- " i was sad and disappointed by this comment\n",
- "
\n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " y=4 stars\n",
- " \n",
- "\n",
- "\n",
- " \n",
- " (probability 0.002, score -6.296)\n",
- "\n",
- "top features\n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " Contribution?\n",
- " | \n",
- " \n",
- " Feature | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " -0.369\n",
- " | \n",
- " \n",
- " <BIAS>\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " -5.927\n",
- " | \n",
- " \n",
- " Highlighted in text (sum)\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- "\n",
- " \n",
- "
\n",
- "\n",
- " \n",
- "\n",
- "\n",
- "\n",
- " \n",
- " i was sad and disappointed by this comment\n",
- "
\n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " y=5 stars\n",
- " \n",
- "\n",
- "\n",
- " \n",
- " (probability 0.001, score -6.650)\n",
- "\n",
- "top features\n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " \n",
- " Contribution?\n",
- " | \n",
- " \n",
- " Feature | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " \n",
- "\n",
- " \n",
- " \n",
- " \n",
- " \n",
- " -0.360\n",
- " | \n",
- " \n",
- " <BIAS>\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " -6.289\n",
- " | \n",
- " \n",
- " Highlighted in text (sum)\n",
- " | \n",
- " \n",
- "
\n",
- " \n",
- "\n",
- " \n",
- "
\n",
- "\n",
- " \n",
- "\n",
- "\n",
- "\n",
- " \n",
- " i was sad and disappointed by this comment\n",
- "
\n",
- "\n",
- " \n",
- "\n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- " \n",
- "\n",
- "\n",
- "\n"
- ],
- "text/plain": [
- "Explanation(estimator=\"SGDClassifier(alpha=0.001, loss='log', penalty='elasticnet',\\n random_state=RandomState(MT19937) at 0x7FE441F8FC00)\", description=None, error=None, method='linear model', is_regression=False, targets=[TargetExplanation(target='1 star', feature_weights=FeatureWeights(pos=[FeatureWeight(feature='sad and', weight=0.5346186851714406, std=None, value=1.0), FeatureWeight(feature='by this', weight=0.42658779616112424, std=None, value=1.0), FeatureWeight(feature='and disappointed', weight=0.26653522108846317, std=None, value=1.0), FeatureWeight(feature='i was', weight=0.19021551478926602, std=None, value=1.0), FeatureWeight(feature='this comment', weight=0.17398835561819753, std=None, value=1.0), FeatureWeight(feature='sad', weight=0.154955236219833, std=None, value=1.0), FeatureWeight(feature='disappointed', weight=0.11045707013459205, std=None, value=1.0)], neg=[FeatureWeight(feature='', weight=-0.5847697578742233, std=None, value=1.0), FeatureWeight(feature='i', weight=-0.4710052143644072, std=None, value=1.0), FeatureWeight(feature='by', weight=-0.3865256334829903, std=None, value=1.0), FeatureWeight(feature='and', weight=-0.3741197710655346, std=None, value=1.0), FeatureWeight(feature='was', weight=-0.21584403881881575, std=None, value=1.0), FeatureWeight(feature='this', weight=-0.1771012983233238, std=None, value=1.0), FeatureWeight(feature='disappointed by', weight=-0.13037261951049633, std=None, value=1.0), FeatureWeight(feature='comment', weight=-0.09160800881871993, std=None, value=1.0), FeatureWeight(feature='was sad', weight=-0.06625548348972331, std=None, value=1.0)], pos_remaining=0, neg_remaining=0), proba=0.3216432779568045, score=-0.6402439465653179, weighted_spans=WeightedSpans(docs_weighted_spans=[DocWeightedSpans(document='i was sad and disappointed by this comment', spans=[('i', [(0, 1)], -0.4710052143644072), ('was', [(2, 5)], -0.21584403881881575), ('sad', [(6, 9)], 0.154955236219833), ('and', [(10, 13)], -0.3741197710655346), ('disappointed', [(14, 26)], 0.11045707013459205), ('by', [(27, 29)], -0.3865256334829903), ('this', [(30, 34)], -0.1771012983233238), ('comment', [(35, 42)], -0.09160800881871993), ('i was', [(0, 1), (2, 5)], 0.19021551478926602), ('was sad', [(2, 5), (6, 9)], -0.06625548348972331), ('sad and', [(6, 9), (10, 13)], 0.5346186851714406), ('and disappointed', [(10, 13), (14, 26)], 0.26653522108846317), ('disappointed by', [(14, 26), (27, 29)], -0.13037261951049633), ('by this', [(27, 29), (30, 34)], 0.42658779616112424), ('this comment', [(30, 34), (35, 42)], 0.17398835561819753)], preserve_density=False, vec_name=None)], other=FeatureWeights(pos=[], neg=[FeatureWeight(feature='', weight=-0.5847697578742233, std=None, value=1.0), FeatureWeight(feature=, weight=-0.05547418869109433, std=None, value=None)], pos_remaining=0, neg_remaining=0)), heatmap=None), TargetExplanation(target='2 stars', feature_weights=FeatureWeights(pos=[FeatureWeight(feature='disappointed', weight=0.7328982078589731, std=None, value=1.0), FeatureWeight(feature='and disappointed', weight=0.5320969197071604, std=None, value=1.0), FeatureWeight(feature='was sad', weight=0.2131501740286752, std=None, value=1.0), FeatureWeight(feature='sad', weight=0.204538558077756, std=None, value=1.0), FeatureWeight(feature='by this', weight=0.1664729577435313, std=None, value=1.0), FeatureWeight(feature='this comment', weight=0.1331403830562311, std=None, value=1.0), FeatureWeight(feature='i was', weight=0.0992793624540375, std=None, value=1.0), FeatureWeight(feature='disappointed by', weight=0.0757687718883966, std=None, value=1.0)], neg=[FeatureWeight(feature='', weight=-0.7081592777076793, std=None, value=1.0), FeatureWeight(feature='this', weight=-0.4236960149446214, std=None, value=1.0), FeatureWeight(feature='and', weight=-0.36806936626741465, std=None, value=1.0), FeatureWeight(feature='comment', weight=-0.2259694258591071, std=None, value=1.0), FeatureWeight(feature='was', weight=-0.2024479929487215, std=None, value=1.0), FeatureWeight(feature='by', weight=-0.014466480265356482, std=None, value=1.0), FeatureWeight(feature='i', weight=-0.005985952449988492, std=None, value=1.0)], pos_remaining=0, neg_remaining=0), proba=0.5142968951266649, score=0.20855082437187222, weighted_spans=WeightedSpans(docs_weighted_spans=[DocWeightedSpans(document='i was sad and disappointed by this comment', spans=[('i', [(0, 1)], -0.005985952449988492), ('was', [(2, 5)], -0.2024479929487215), ('sad', [(6, 9)], 0.204538558077756), ('and', [(10, 13)], -0.36806936626741465), ('disappointed', [(14, 26)], 0.7328982078589731), ('by', [(27, 29)], -0.014466480265356482), ('this', [(30, 34)], -0.4236960149446214), ('comment', [(35, 42)], -0.2259694258591071), ('i was', [(0, 1), (2, 5)], 0.0992793624540375), ('was sad', [(2, 5), (6, 9)], 0.2131501740286752), ('and disappointed', [(10, 13), (14, 26)], 0.5320969197071604), ('disappointed by', [(14, 26), (27, 29)], 0.0757687718883966), ('by this', [(27, 29), (30, 34)], 0.1664729577435313), ('this comment', [(30, 34), (35, 42)], 0.1331403830562311)], preserve_density=False, vec_name=None)], other=FeatureWeights(pos=[FeatureWeight(feature=, weight=0.9167101020795517, std=None, value=None)], neg=[FeatureWeight(feature='', weight=-0.7081592777076793, std=None, value=1.0)], pos_remaining=0, neg_remaining=0)), heatmap=None), TargetExplanation(target='3 stars', feature_weights=FeatureWeights(pos=[FeatureWeight(feature='disappointed by', weight=0.49534031715083565, std=None, value=1.0), FeatureWeight(feature='was sad', weight=0.38185475125317725, std=None, value=1.0), FeatureWeight(feature='this comment', weight=0.3040274759378381, std=None, value=1.0), FeatureWeight(feature='i was', weight=0.08740115905472043, std=None, value=1.0), FeatureWeight(feature='sad and', weight=0.08027723553189965, std=None, value=1.0)], neg=[FeatureWeight(feature='', weight=-0.6027694124901809, std=None, value=1.0), FeatureWeight(feature='disappointed', weight=-0.5150103773886033, std=None, value=1.0), FeatureWeight(feature='sad', weight=-0.44967947400070096, std=None, value=1.0), FeatureWeight(feature='this', weight=-0.41934779999200184, std=None, value=1.0), FeatureWeight(feature='and', weight=-0.38879109152041874, std=None, value=1.0), FeatureWeight(feature='and disappointed', weight=-0.20839201881188554, std=None, value=1.0), FeatureWeight(feature='comment', weight=-0.17453889925444666, std=None, value=1.0), FeatureWeight(feature='by', weight=-0.08144982607176687, std=None, value=1.0), FeatureWeight(feature='was', weight=-0.07386605125736567, std=None, value=1.0)], pos_remaining=0, neg_remaining=0), proba=0.16114092285641266, score=-1.5649440118588995, weighted_spans=WeightedSpans(docs_weighted_spans=[DocWeightedSpans(document='i was sad and disappointed by this comment', spans=[('was', [(2, 5)], -0.07386605125736567), ('sad', [(6, 9)], -0.44967947400070096), ('and', [(10, 13)], -0.38879109152041874), ('disappointed', [(14, 26)], -0.5150103773886033), ('by', [(27, 29)], -0.08144982607176687), ('this', [(30, 34)], -0.41934779999200184), ('comment', [(35, 42)], -0.17453889925444666), ('i was', [(0, 1), (2, 5)], 0.08740115905472043), ('was sad', [(2, 5), (6, 9)], 0.38185475125317725), ('sad and', [(6, 9), (10, 13)], 0.08027723553189965), ('and disappointed', [(10, 13), (14, 26)], -0.20839201881188554), ('disappointed by', [(14, 26), (27, 29)], 0.49534031715083565), ('this comment', [(30, 34), (35, 42)], 0.3040274759378381)], preserve_density=False, vec_name=None)], other=FeatureWeights(pos=[], neg=[FeatureWeight(feature=, weight=-0.9621745993687185, std=None, value=None), FeatureWeight(feature='', weight=-0.6027694124901809, std=None, value=1.0)], pos_remaining=0, neg_remaining=0)), heatmap=None), TargetExplanation(target='4 stars', feature_weights=FeatureWeights(pos=[FeatureWeight(feature='i was', weight=0.3280158349131319, std=None, value=1.0), FeatureWeight(feature='by this', weight=0.24485117988897515, std=None, value=1.0), FeatureWeight(feature='this comment', weight=0.22136455004546846, std=None, value=1.0)], neg=[FeatureWeight(feature='disappointed', weight=-2.2818466043358065, std=None, value=1.0), FeatureWeight(feature='sad', weight=-1.5584253852713434, std=None, value=1.0), FeatureWeight(feature='this', weight=-0.6638038328645915, std=None, value=1.0), FeatureWeight(feature='and disappointed', weight=-0.5323899161000051, std=None, value=1.0), FeatureWeight(feature='was', weight=-0.46923892676653156, std=None, value=1.0), FeatureWeight(feature='by', weight=-0.37154981102885365, std=None, value=1.0), FeatureWeight(feature='', weight=-0.3690717498285274, std=None, value=1.0), FeatureWeight(feature='comment', weight=-0.330310147529379, std=None, value=1.0), FeatureWeight(feature='i', weight=-0.27051122014568324, std=None, value=1.0), FeatureWeight(feature='was sad', weight=-0.18528806395587175, std=None, value=1.0), FeatureWeight(feature='and', weight=-0.05813554259772132, std=None, value=1.0)], pos_remaining=0, neg_remaining=0), proba=0.0017141517689746404, score=-6.2963396355767385, weighted_spans=WeightedSpans(docs_weighted_spans=[DocWeightedSpans(document='i was sad and disappointed by this comment', spans=[('i', [(0, 1)], -0.27051122014568324), ('was', [(2, 5)], -0.46923892676653156), ('sad', [(6, 9)], -1.5584253852713434), ('and', [(10, 13)], -0.05813554259772132), ('disappointed', [(14, 26)], -2.2818466043358065), ('by', [(27, 29)], -0.37154981102885365), ('this', [(30, 34)], -0.6638038328645915), ('comment', [(35, 42)], -0.330310147529379), ('i was', [(0, 1), (2, 5)], 0.3280158349131319), ('was sad', [(2, 5), (6, 9)], -0.18528806395587175), ('and disappointed', [(10, 13), (14, 26)], -0.5323899161000051), ('by this', [(27, 29), (30, 34)], 0.24485117988897515), ('this comment', [(30, 34), (35, 42)], 0.22136455004546846)], preserve_density=False, vec_name=None)], other=FeatureWeights(pos=[], neg=[FeatureWeight(feature=, weight=-5.92726788574821, std=None, value=None), FeatureWeight(feature='', weight=-0.3690717498285274, std=None, value=1.0)], pos_remaining=0, neg_remaining=0)), heatmap=None), TargetExplanation(target='5 stars', feature_weights=FeatureWeights(pos=[FeatureWeight(feature='by this', weight=0.5199374968512099, std=None, value=1.0), FeatureWeight(feature='i was', weight=0.11021409204987892, std=None, value=1.0)], neg=[FeatureWeight(feature='disappointed', weight=-2.542609677519791, std=None, value=1.0), FeatureWeight(feature='sad', weight=-1.6205444349003744, std=None, value=1.0), FeatureWeight(feature='and disappointed', weight=-0.6823758319028586, std=None, value=1.0), FeatureWeight(feature='was', weight=-0.5884377438193445, std=None, value=1.0), FeatureWeight(feature='by', weight=-0.47836975878854626, std=None, value=1.0), FeatureWeight(feature='disappointed by', weight=-0.4713742071188074, std=None, value=1.0), FeatureWeight(feature='', weight=-0.36028536795303406, std=None, value=1.0), FeatureWeight(feature='comment', weight=-0.23648279521049556, std=None, value=1.0), FeatureWeight(feature='was sad', weight=-0.2026448613238884, std=None, value=1.0), FeatureWeight(feature='sad and', weight=-0.09655848179203014, std=None, value=1.0)], pos_remaining=0, neg_remaining=0), proba=0.0012047522911433645, score=-6.649531571428082, weighted_spans=WeightedSpans(docs_weighted_spans=[DocWeightedSpans(document='i was sad and disappointed by this comment', spans=[('was', [(2, 5)], -0.5884377438193445), ('sad', [(6, 9)], -1.6205444349003744), ('disappointed', [(14, 26)], -2.542609677519791), ('by', [(27, 29)], -0.47836975878854626), ('comment', [(35, 42)], -0.23648279521049556), ('i was', [(0, 1), (2, 5)], 0.11021409204987892), ('was sad', [(2, 5), (6, 9)], -0.2026448613238884), ('sad and', [(6, 9), (10, 13)], -0.09655848179203014), ('and disappointed', [(10, 13), (14, 26)], -0.6823758319028586), ('disappointed by', [(14, 26), (27, 29)], -0.4713742071188074), ('by this', [(27, 29), (30, 34)], 0.5199374968512099)], preserve_density=False, vec_name=None)], other=FeatureWeights(pos=[], neg=[FeatureWeight(feature=, weight=-6.289246203475048, std=None, value=None), FeatureWeight(feature='', weight=-0.36028536795303406, std=None, value=1.0)], pos_remaining=0, neg_remaining=0)), heatmap=None)], feature_importances=None, decision_tree=None, highlight_spaces=None, transition_features=None, image=None)"
- ]
- },
- "execution_count": 79,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "te.explain_prediction(target_names=list(model.config.id2label.values()))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " | \n",
- " 0 | \n",
- " 1 | \n",
- " 2 | \n",
- "
\n",
- " \n",
- " \n",
- " \n",
- " 0 | \n",
- " {'label': 'LABEL_0', 'score': 0.00171170581597... | \n",
- " {'label': 'LABEL_1', 'score': 0.00916427746415... | \n",
- " {'label': 'LABEL_2', 'score': 0.9891239404678345} | \n",
- "
\n",
- " \n",
- "
\n",
- "
"
- ],
- "text/plain": [
- " 0 ... 2\n",
- "0 {'label': 'LABEL_0', 'score': 0.00171170581597... ... {'label': 'LABEL_2', 'score': 0.9891239404678345}\n",
- "\n",
- "[1 rows x 3 columns]"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pd.DataFrame(data)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "interpreter": {
- "hash": "6f245f586ce379714bd6be03acc40f31e300ec6e2c664326cf81e22a8633b6cc"
- },
- "kernelspec": {
- "display_name": "Python 3.7.11 64-bit ('pgesg': conda)",
- "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.11"
- },
- "orig_nbformat": 4
- },
- "nbformat": 4,
- "nbformat_minor": 2
-}