Skip to content

Commit

Permalink
Merge pull request #750 from QData/stanza-test
Browse files Browse the repository at this point in the history
Stanza test and notebooks minor fix
  • Loading branch information
qiyanjun authored Sep 11, 2023
2 parents bde7a36 + 4a17abd commit 6c1d2f8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 34 deletions.
15 changes: 13 additions & 2 deletions docs/2notebook/1_Introduction_and_Transformations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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)))"
]
},
{
Expand Down
21 changes: 1 addition & 20 deletions docs/2notebook/2_Constraints.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -138,7 +119,7 @@
}
],
"source": [
"! pip3 install textattack[tensorflow]\n",
"!pip3 install textattack[tensorflow]\n",
"\n",
"import nltk\n",
"\n",
Expand Down
30 changes: 28 additions & 2 deletions docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
}
],
"source": [
"!pip3 install textattack"
"!pip3 install textattack[tensorflow]"
]
},
{
Expand Down Expand Up @@ -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": {
Expand Down
20 changes: 10 additions & 10 deletions tests/test_command_line/test_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6c1d2f8

Please sign in to comment.