{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "iKENeWEEwKIb" }, "source": [ "Salency maps para NLP utilizando AllenNLP\n", "=========================================" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
PRECAUCIÓN 😱: El tema presentado en esta sección está clasificado como avanzado. El entendimiento de este contenido es totalmente opcional.
" ] }, { "cell_type": "markdown", "metadata": { "id": "JctlpI10jJ15" }, "source": [ "Introducción\n", "------------" ] }, { "cell_type": "markdown", "metadata": { "id": "sT_t9OxYwKIc" }, "source": [ "AllenNLP es un framework general de aprendizaje profundo para NLP, establecido por el mundialmente famoso Allen Institute for AI Lab. Contiene modelos de referencia de última generación que se ejecutan sobre el `PyTorch`. AllenNLP es una librería que ademas busca implementar abstracciones que permitan el rápido desarrollo de modelos y reutilización de componentes al despegarse de detalles de implementación de cada modelo.\n", "\n", "En este ejemplo, veremos como utilizar esta librería para generar salency maps utilizando los gradientes de las prediciones. Esto nos permita interpretar las predicciones de nuestros modelos basados en `transformers`." ] }, { "cell_type": "markdown", "metadata": { "id": "Dcyc_TQ6dis7" }, "source": [ "### Para ejecutar este notebook" ] }, { "cell_type": "markdown", "metadata": { "id": "nJFcNrbmjJ17" }, "source": [ "Para ejecutar este notebook, instale las siguientes librerias:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GgK8b6e_jJ17", "outputId": "53134203-0221-4675-88d2-b87c67559404" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[K |████████████████████████████████| 3.1 MB 4.4 MB/s \n", "\u001b[K |████████████████████████████████| 831.4 MB 2.6 kB/s \n", "\u001b[K |████████████████████████████████| 719 kB 57.2 MB/s \n", "\u001b[K |████████████████████████████████| 596 kB 53.4 MB/s \n", "\u001b[K |████████████████████████████████| 880 kB 52.3 MB/s \n", "\u001b[K |████████████████████████████████| 3.3 MB 39.5 MB/s \n", "\u001b[K |████████████████████████████████| 86 kB 4.3 MB/s \n", "\u001b[K |████████████████████████████████| 125 kB 73.3 MB/s \n", "\u001b[K |████████████████████████████████| 1.8 MB 44.9 MB/s \n", "\u001b[K |████████████████████████████████| 592 kB 57.0 MB/s \n", "\u001b[K |████████████████████████████████| 248 kB 63.0 MB/s \n", "\u001b[?25h Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", " Installing backend dependencies ... \u001b[?25l\u001b[?25hdone\n", " Preparing wheel metadata ... \u001b[?25l\u001b[?25hdone\n", "\u001b[K |████████████████████████████████| 1.2 MB 47.7 MB/s \n", "\u001b[K |████████████████████████████████| 132 kB 64.7 MB/s \n", "\u001b[K |████████████████████████████████| 77 kB 6.5 MB/s \n", "\u001b[K |████████████████████████████████| 8.9 MB 54.4 MB/s \n", "\u001b[K |████████████████████████████████| 79 kB 9.1 MB/s \n", "\u001b[K |████████████████████████████████| 138 kB 65.9 MB/s \n", "\u001b[K |████████████████████████████████| 127 kB 67.1 MB/s \n", "\u001b[K |████████████████████████████████| 21.0 MB 1.2 MB/s \n", "\u001b[K |████████████████████████████████| 23.2 MB 1.3 MB/s \n", "\u001b[K |████████████████████████████████| 23.3 MB 84.3 MB/s \n", "\u001b[K |████████████████████████████████| 23.3 MB 1.2 MB/s \n", "\u001b[K |████████████████████████████████| 22.1 MB 70.1 MB/s \n", "\u001b[K |████████████████████████████████| 22.1 MB 80.7 MB/s \n", "\u001b[K |████████████████████████████████| 181 kB 63.7 MB/s \n", "\u001b[K |████████████████████████████████| 145 kB 67.0 MB/s \n", "\u001b[K |████████████████████████████████| 63 kB 1.6 MB/s \n", "\u001b[?25h Building wheel for fairscale (PEP 517) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for jsonnet (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for pathtools (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for sacremoses (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", "torchtext 0.12.0 requires torch==1.11.0, but you have torch 1.9.0 which is incompatible.\n", "torchaudio 0.11.0+cu113 requires torch==1.11.0, but you have torch 1.9.0 which is incompatible.\n", "datascience 0.10.6 requires folium==0.2.1, but you have folium 0.8.3 which is incompatible.\u001b[0m\n" ] } ], "source": [ "!wget https://raw.githubusercontent.com/santiagxf/M72109/master/NLP/Datasets/mascorpus/tweets_marketing.csv \\\n", " --quiet --no-clobber --directory-prefix ./Datasets/mascorpus/\n", "\n", "!wget https://raw.githubusercontent.com/santiagxf/M72109/master/m72109/nlp/explanation.py \\\n", " --quiet --no-clobber --directory-prefix ./m72109/nlp/\n", " \n", "!wget https://raw.githubusercontent.com/santiagxf/M72109/master/docs/nlp/neural/allennlp_interpret.txt \\\n", " --quiet --no-clobber\n", "%pip install -r allennlp_interpret.txt --quiet" ] }, { "cell_type": "markdown", "metadata": { "id": "5YjjRwLK4leb" }, "source": [ "Si ejecuta en Google Colab, adicionalmente deberá cambiar la version de la libraria `google-cloud-storage`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LuxJI9Cd4lec", "outputId": "a0ac3ffb-2ab3-4fbd-ab7a-89bfb3457445" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[K |████████████████████████████████| 104 kB 2.6 MB/s \n", "\u001b[K |████████████████████████████████| 75 kB 5.0 MB/s \n", "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", "google-cloud-bigquery 1.21.0 requires google-resumable-media!=0.4.0,<0.5.0dev,>=0.3.1, but you have google-resumable-media 1.3.3 which is incompatible.\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "%pip install -U google-cloud-storage==1.40.0 --quiet" ] }, { "cell_type": "markdown", "metadata": { "id": "f1nl84gT4led" }, "source": [ "Descargaremos un modelo previamente entrenando el el problema de clasificación de Tweets:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "3yqczZze4lee" }, "outputs": [], "source": [ "!wget https://santiagxf.blob.core.windows.net/public/models/tweet_classification_bert.zip --no-clobber --quiet\n", "!unzip -qq tweet_classification_bert.zip" ] }, { "cell_type": "markdown", "metadata": { "id": "_gBXNzwYwKIu" }, "source": [ "Cargamos el set de datos" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "I8vqJD9JwKIv" }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "tweets = pd.read_csv('Datasets/mascorpus/tweets_marketing.csv')" ] }, { "cell_type": "markdown", "metadata": { "id": "-0uIrAVo4leg" }, "source": [ "Cargando un modelo entreando con Transformers en AllenNLP\n", "---------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": { "id": "nXgQvCX2wKJi" }, "source": [ "`allennlp` es un framework compatible con la libraría `transformers` lo cual resulta atractivo a la hora de utilizar modelos que son entrenados en una para luego llevarlo a la otra. Veamos entonces como podemos hacer para cargar el modelo que tenemos previamente entrenado para la clasificación de tweets utilizando una arquitectura `BERT` dentro de este framework. En particular, nuestro modelo se persistió en el directorio \"tweet_classification\"." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "dxqTaKSc4leh" }, "outputs": [], "source": [ "model_name = \"tweet_classification_bert\"" ] }, { "cell_type": "markdown", "metadata": { "id": "8nvYalyDem-4" }, "source": [ "El detalle de como utilizar AllenNLP esta fuera de este curso, pero utilizaremos el format JSON para cargar modelos de esta libreria. El siguiente codigo carga un modelo exactamente igual al que creamos utilizando la libraria HuggingFace anteriormente." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "6efVECkjehN0" }, "outputs": [], "source": [ "from allennlp.common import Params\n", "from allennlp.data.dataset_readers import DatasetReader\n", "\n", "params = Params({\n", " \"type\": \"text_classification_json\",\n", " \"tokenizer\": {\n", " \"type\": \"pretrained_transformer\",\n", " \"model_name\": model_name,\n", " },\n", " \"token_indexers\": {\n", " \"tokens\": {\n", " \"type\": \"pretrained_transformer\",\n", " \"model_name\": model_name,\n", " }\n", " }\n", "})\n", "\n", "dataset_reader = DatasetReader.from_params(params)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "-Wl4WwxEehES" }, "outputs": [], "source": [ "from allennlp.common import Params\n", "from allennlp.models import Model\n", "from transformers import AutoModelForSequenceClassification\n", "\n", "params = Params({\n", " \"type\": \"basic_classifier\",\n", " \"vocab\": {\n", " \"type\": \"from_pretrained_transformer\",\n", " \"model_name\": model_name,\n", " },\n", " \"text_field_embedder\": {\n", " \"type\": \"basic\",\n", " \"token_embedders\": {\n", " \"tokens\": {\n", " \"type\": \"pretrained_transformer\",\n", " \"model_name\": model_name\n", " }\n", " }\n", " },\n", " \"seq2vec_encoder\": {\n", " \"type\": \"bert_pooler\",\n", " \"pretrained_model\": model_name\n", " },\n", " \"dropout\": 0.1,\n", " \"num_labels\": 5,\n", "});\n", "\n", "model = Model.from_params(params)\n", "model._classification_layer.weight = AutoModelForSequenceClassification.from_pretrained(model_name).classifier.weight\n", "model._classification_layer.bias = AutoModelForSequenceClassification.from_pretrained(model_name).classifier.bias\n", "_ = model.eval()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "id": "8uAu57uDe1_5" }, "outputs": [], "source": [ "from allennlp.predictors import TextClassifierPredictor\n", "\n", "predictor = TextClassifierPredictor(model, dataset_reader)" ] }, { "cell_type": "markdown", "metadata": { "id": "B3TU4m2O4lem" }, "source": [ "Recordemos que en el conjunto de datos de entrenamiento, las etiquetas se distribuyen como sigue:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "MZDuP8n8fP89" }, "outputs": [], "source": [ "labels = [\n", " 'ALIMENTACION',\n", " 'AUTOMOCION',\n", " 'BANCA',\n", " 'BEBIDAS',\n", " 'DEPORTES',\n", " 'RETAIL',\n", " 'TELCO'\n", "]" ] }, { "cell_type": "markdown", "metadata": { "id": "3kB6qBzO4lem" }, "source": [ "Interpretando nuestras predicciones\n", "-----------------------------------" ] }, { "cell_type": "markdown", "metadata": { "id": "FavSqB7C4lem" }, "source": [ "Una vez que tenemos nuestro modelo correctamente cargado, veamos como podemos interpretar una predicción computando el salency map a partir de los gradientes." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "id": "NTX6HgF6sXrs" }, "outputs": [], "source": [ "from allennlp.interpret.saliency_interpreters import SimpleGradient, IntegratedGradient, SmoothGradient\n", "\n", "interpreter = SmoothGradient(predictor)" ] }, { "cell_type": "markdown", "metadata": { "id": "l5vXo70d4lem" }, "source": [ "Busquemos un tweet para interpretar:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GMOYNNQB4lem", "outputId": "725b473c-b5e3-4522-fe36-08a50605ae5c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Texto: @HyundaiPeru con Grupo Primax realiza este verano servicios de Inspección Digital Gratuita a vehículos Hyundai en e… https://t.co/TZ4XFziOd3 \\Sector: AUTOMOCION\n" ] } ], "source": [ "sample_text_idx = 1522\n", "sample_text = tweets['TEXTO'][sample_text_idx]\n", "sample_label = tweets['SECTOR'][sample_text_idx]\n", "\n", "print(\"Texto:\", sample_text, \"\\Sector:\", sample_label)" ] }, { "cell_type": "markdown", "metadata": { "id": "nCv3OzOq4len" }, "source": [ "Calculemos los gradientes para cada token:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "id": "3IMgIKxytBr5" }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7_00Ya8ZelBk", "outputId": "a76d2b47-2f56-4639-fca5-06207a49941c" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py:974: UserWarning: Using a non-full backward hook when the forward contains multiple autograd Nodes is deprecated and will be removed in future versions. This hook will be missing some grad_input. Please use register_full_backward_hook to get the documented behavior.\n", " warnings.warn(\"Using a non-full backward hook when the forward contains multiple autograd Nodes \"\n" ] } ], "source": [ "interpretation = interpreter.saliency_interpret_from_json({\"sentence\": sample_text })\n", "outputs = predictor.predict(sample_text)\n", "grads = np.array(interpretation['instance_1']['grad_input_1'])\n", "probs = np.array(outputs['probs'])" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eF1pX0-lh7QT", "outputId": "39d718e3-5705-4c26-a039-6f288dbc9156" }, "outputs": [ { "data": { "text/plain": [ "dict_keys(['logits', 'probs', 'token_ids', 'label', 'tokens'])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "outputs.keys()" ] }, { "cell_type": "markdown", "metadata": { "id": "9gF--Q8v4len" }, "source": [ "Podemos graficar los resultados utilizando un mapa de calor marcando con colores más intensos aquellos tokens que tienen mayor impacto en las predicciones:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "id": "dLcrGXYnhYcz" }, "outputs": [], "source": [ "from IPython.display import HTML\n", "from eli5.formatters import format_as_html\n", "from m72109.nlp.explanation import get_explanation_from_grads" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "id": "JtxoMSnJ-Rm5" }, "outputs": [], "source": [ "expl = get_explanation_from_grads(estimator_name=\"transformer\",\n", " estimator_description=\"NLP transformer explanation\",\n", " text=sample_text,\n", " tokens=outputs['tokens'],\n", " grads=grads,\n", " probas=probs,\n", " labels=labels)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 170 }, "id": "xPXW3jqq5BP-", "outputId": "e2d8a4b4-3a3b-4077-d005-035256c9cbca" }, "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", "
NLP transformer explanation
\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", " \n", "

\n", " \n", " \n", " y=AUTOMOCION\n", " \n", "\n", "\n", " \n", " (probability 0.900)\n", "\n", "top features\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", " Contribution?\n", " Feature
\n", " +1.000\n", " \n", " Highlighted in text\n", "
\n", "\n", " \n", " \n", "\n", " \n", "\n", "\n", "

\n", " @HyundaiPeru con Grupo Primax realiza este verano servicios de Inspección Digital Gratuita a vehículos Hyundai en e https://t.co/TZ4XFziOd3\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": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(format_as_html(expl))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "id": "CEyPEV0J4ZxH" }, "outputs": [], "source": [ "expl = get_explanation_from_grads(estimator_name=\"transformer\",\n", " estimator_description=\"NLP transformer explanation\",\n", " text=sample_text,\n", " tokens=outputs['tokens'],\n", " grads=grads,\n", " probas=probs,\n", " labels=labels,\n", " force_weights=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 934 }, "id": "McfMTL9u5EC8", "outputId": "6c154eea-8e0f-42e0-8216-d9d4abe2d5ab" }, "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", "
NLP transformer explanation
\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", " \n", "

\n", " \n", " \n", " y=AUTOMOCION\n", " \n", "\n", "\n", " \n", " (probability 0.900, score 1.000)\n", "\n", "top features\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", " \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", " \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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", "\n", " \n", " \n", "\n", " \n", " \n", "\n", " \n", "
\n", " Weight?\n", " Feature
\n", " +0.011\n", " \n", " [CLS]\n", "
\n", " +0.000\n", " \n", " @\n", "
\n", " +0.000\n", " \n", " hyun\n", "
\n", " +0.029\n", " \n", " ##da\n", "
\n", " +0.062\n", " \n", " ##ipe\n", "
\n", " +0.029\n", " \n", " ##ru\n", "
\n", " +0.000\n", " \n", " con\n", "
\n", " +0.044\n", " \n", " grupo\n", "
\n", " +0.000\n", " \n", " prima\n", "
\n", " +0.000\n", " \n", " ##x\n", "
\n", " +0.029\n", " \n", " realiza\n", "
\n", " +0.022\n", " \n", " este\n", "
\n", " +0.015\n", " \n", " verano\n", "
\n", " +0.087\n", " \n", " servicios\n", "
\n", " +0.029\n", " \n", " de\n", "
\n", " +0.029\n", " \n", " inspección\n", "
\n", " +0.018\n", " \n", " digital\n", "
\n", " +0.000\n", " \n", " gratuita\n", "
\n", " +0.022\n", " \n", " a\n", "
\n", " +0.058\n", " \n", " vehículos\n", "
\n", " +0.116\n", " \n", " hyun\n", "
\n", " +0.044\n", " \n", " ##da\n", "
\n", " +0.044\n", " \n", " ##i\n", "
\n", " +0.008\n", " \n", " en\n", "
\n", " +0.022\n", " \n", " e\n", "
\n", " +0.003\n", " \n", " [UNK]\n", "
\n", " +0.029\n", " \n", " h\n", "
\n", " +0.022\n", " \n", " ##tt\n", "
\n", " +0.000\n", " \n", " ##ps\n", "
\n", " +0.000\n", " \n", " :\n", "
\n", " +0.015\n", " \n", " /\n", "
\n", " +0.007\n", " \n", " /\n", "
\n", " +0.029\n", " \n", " t\n", "
\n", " +0.011\n", " \n", " .\n", "
\n", " +0.015\n", " \n", " co\n", "
\n", " +0.005\n", " \n", " /\n", "
\n", " +0.000\n", " \n", " t\n", "
\n", " +0.002\n", " \n", " ##z\n", "
\n", " +0.044\n", " \n", " ##4\n", "
\n", " +0.015\n", " \n", " ##x\n", "
\n", " +0.000\n", " \n", " ##f\n", "
\n", " +0.029\n", " \n", " ##zio\n", "
\n", " +0.000\n", " \n", " ##d\n", "
\n", " +0.058\n", " \n", " ##3\n", "
\n", " +0.000\n", " \n", " [SEP]\n", "
\n", "\n", " \n", " \n", "\n", " \n", "\n", "\n", "

\n", " @HyundaiPeru con Grupo Primax realiza este verano servicios de Inspección Digital Gratuita a vehículos Hyundai en e https://t.co/TZ4XFziOd3\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": [ "" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(format_as_html(expl, force_weights=True))" ] } ], "metadata": { "accelerator": "GPU", "colab": { "name": "allennlp_interpret.ipynb", "provenance": [] }, "kernel_info": { "name": "nlp-py38" }, "kernelspec": { "display_name": "Python 3.8 - PyTorch", "language": "python", "name": "azureml_py38_pytorch" }, "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.8.1" }, "nteract": { "version": "nteract-front-end@1.0.0" } }, "nbformat": 4, "nbformat_minor": 0 }