Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stanza test and notebooks minor fix #750

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading