From eaecc0ec6c2de7d6c8fb8ddb158c8c0ffd8c85c0 Mon Sep 17 00:00:00 2001 From: Yanjun Qi Date: Mon, 11 Sep 2023 12:46:28 -0400 Subject: [PATCH 1/2] comment out the Stanza based test --- tests/test_command_line/test_attack.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test_command_line/test_attack.py b/tests/test_command_line/test_attack.py index afdbc8081..83ea05d80 100644 --- a/tests/test_command_line/test_attack.py +++ b/tests/test_command_line/test_attack.py @@ -135,16 +135,16 @@ "tests/sample_outputs/kuleshov_cnn_sst_2.txt", ), # - # test: run_attack on LSTM MR using word embedding transformation and greedy search with Stanza part-of-speech tagger as a constraint - # - ( - "run_attack_stanza_pos_tagger", - ( - "textattack attack --model lstm-mr --num-examples 4 --search-method greedy --transformation word-swap-embedding " - "--constraints repeat stopword part-of-speech^tagger_type=\\'stanza\\' " - ), - "tests/sample_outputs/run_attack_stanza_pos_tagger.txt", - ), + # # test: run_attack on LSTM MR using word embedding transformation and greedy search with Stanza part-of-speech tagger as a constraint + # # + # ( + # "run_attack_stanza_pos_tagger", + # ( + # "textattack attack --model lstm-mr --num-examples 4 --search-method greedy --transformation word-swap-embedding " + # "--constraints repeat stopword part-of-speech^tagger_type=\\'stanza\\' " + # ), + # "tests/sample_outputs/run_attack_stanza_pos_tagger.txt", + # ), # # test: run_attack on CNN Yelp using the WordNet transformation and greedy search WIR # with a CoLA constraint and BERT score From 4a17abdcd1bddde3a12338d54c3dbf7c10c38423 Mon Sep 17 00:00:00 2001 From: Yanjun Qi Date: Mon, 11 Sep 2023 12:55:53 -0400 Subject: [PATCH 2/2] fixing minor errors in notebooks and format change --- .../1_Introduction_and_Transformations.ipynb | 15 ++++++++-- docs/2notebook/2_Constraints.ipynb | 21 +------------ .../4_Custom_Datasets_Word_Embedding.ipynb | 30 +++++++++++++++++-- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/docs/2notebook/1_Introduction_and_Transformations.ipynb b/docs/2notebook/1_Introduction_and_Transformations.ipynb index b895702fd..8406af4cf 100644 --- a/docs/2notebook/1_Introduction_and_Transformations.ipynb +++ b/docs/2notebook/1_Introduction_and_Transformations.ipynb @@ -29,6 +29,15 @@ "Please remember to run **pip3 install textattack[tensorflow]** in your notebook enviroment before the following codes:" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "!pip3 install textattack[tensorflow]" + ] + }, { "cell_type": "markdown", "metadata": { @@ -668,11 +677,13 @@ "logger = CSVLogger(color_method=\"html\")\n", "\n", "for result in attack_results:\n", - " logger.log_attack_result(result)\n", + " if isinstance(result, SuccessfulAttackResult):\n", + " logger.log_attack_result(result)\n", "\n", "from IPython.core.display import display, HTML\n", "\n", - "display(HTML(logger.df[[\"original_text\", \"perturbed_text\"]].to_html(escape=False)))" + "results = pd.DataFrame.from_records(logger.row_list)\n", + "display(HTML(results[[\"original_text\", \"perturbed_text\"]].to_html(escape=False)))" ] }, { diff --git a/docs/2notebook/2_Constraints.ipynb b/docs/2notebook/2_Constraints.ipynb index 261c8f124..f9c44752d 100644 --- a/docs/2notebook/2_Constraints.ipynb +++ b/docs/2notebook/2_Constraints.ipynb @@ -76,25 +76,6 @@ "Let's import NLTK and download the required modules:" ] }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.4.0\n" - ] - } - ], - "source": [ - "import tensorflow as tf\n", - "\n", - "print(tf.__version__)" - ] - }, { "cell_type": "code", "execution_count": 2, @@ -138,7 +119,7 @@ } ], "source": [ - "! pip3 install textattack[tensorflow]\n", + "!pip3 install textattack[tensorflow]\n", "\n", "import nltk\n", "\n", diff --git a/docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb b/docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb index 0afbb0707..1c789b5e5 100644 --- a/docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb +++ b/docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb @@ -123,7 +123,7 @@ } ], "source": [ - "!pip3 install textattack" + "!pip3 install textattack[tensorflow]" ] }, { @@ -558,10 +558,36 @@ "attack_args = AttackArgs(\n", " num_successful_examples=5, log_to_csv=\"results.csv\", csv_coloring_style=\"html\"\n", ")\n", - "attacker = Attacker(attack, dataset, attack_args)\n", + "attacker = Attacker(attack, custom_dataset, attack_args)\n", "\n", "attack_results = attacker.attack_dataset()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# now we visualize the attack results\n", + "\n", + "import pandas as pd\n", + "\n", + "pd.options.display.max_colwidth = (\n", + " 480 # increase colum width so we can actually read the examples\n", + ")\n", + "\n", + "logger = CSVLogger(color_method=\"html\")\n", + "\n", + "for result in attack_results:\n", + " if isinstance(result, SuccessfulAttackResult):\n", + " logger.log_attack_result(result)\n", + "\n", + "from IPython.core.display import display, HTML\n", + "\n", + "results = pd.DataFrame.from_records(logger.row_list)\n", + "display(HTML(results[[\"original_text\", \"perturbed_text\"]].to_html(escape=False)))" + ] } ], "metadata": {