Skip to content

Commit e426102

Browse files
authored
Merge pull request #748 from QData/fix-pytest-errors
Fix pytest errors - due to goal_func
2 parents 094025e + 987c926 commit e426102

20 files changed

Lines changed: 3885 additions & 3710 deletions

docs/2notebook/0_End_to_End.ipynb

Lines changed: 1487 additions & 1427 deletions
Large diffs are not rendered by default.

docs/2notebook/1_Introduction_and_Transformations.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@
927927
"name": "python",
928928
"nbconvert_exporter": "python",
929929
"pygments_lexer": "ipython3",
930-
"version": "3.7.7"
930+
"version": "3.7.11"
931931
}
932932
},
933933
"nbformat": 4,

docs/2notebook/2_Constraints.ipynb

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,6 @@
7676
"Let's import NLTK and download the required modules:"
7777
]
7878
},
79-
{
80-
"cell_type": "code",
81-
"execution_count": 1,
82-
"metadata": {},
83-
"outputs": [],
84-
"source": [
85-
"# cd .."
86-
]
87-
},
8879
{
8980
"cell_type": "code",
9081
"execution_count": 11,
@@ -147,7 +138,7 @@
147138
}
148139
],
149140
"source": [
150-
"!pip3 install .\n",
141+
"! pip3 install textattack[tensorflow]\n",
151142
"\n",
152143
"import nltk\n",
153144
"\n",
@@ -826,7 +817,7 @@
826817
")\n",
827818
"attacker = Attacker(attack, dataset, attack_args)\n",
828819
"\n",
829-
"attacker.attack_dataset()"
820+
"attack_results = attacker.attack_dataset()"
830821
]
831822
},
832823
{
@@ -884,7 +875,7 @@
884875
"provenance": []
885876
},
886877
"kernelspec": {
887-
"display_name": "Python 3 (ipykernel)",
878+
"display_name": "Python 3",
888879
"language": "python",
889880
"name": "python3"
890881
},
@@ -898,7 +889,7 @@
898889
"name": "python",
899890
"nbconvert_exporter": "python",
900891
"pygments_lexer": "ipython3",
901-
"version": "3.7.7"
892+
"version": "3.7.11"
902893
},
903894
"widgets": {
904895
"application/vnd.jupyter.widget-state+json": {

docs/2notebook/3_Augmentations.ipynb

Lines changed: 82 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
11
{
2-
"nbformat": 4,
3-
"nbformat_minor": 0,
4-
"metadata": {
5-
"colab": {
6-
"name": "Augmentation with TextAttack.ipynb",
7-
"provenance": []
8-
},
9-
"kernelspec": {
10-
"display_name": "Python 3",
11-
"language": "python",
12-
"name": "python3"
13-
},
14-
"language_info": {
15-
"codemirror_mode": {
16-
"name": "ipython",
17-
"version": 3
18-
},
19-
"file_extension": ".py",
20-
"mimetype": "text/x-python",
21-
"name": "python",
22-
"nbconvert_exporter": "python",
23-
"pygments_lexer": "ipython3",
24-
"version": "3.8.8"
25-
}
26-
},
272
"cells": [
283
{
294
"cell_type": "markdown",
@@ -84,9 +59,11 @@
8459
},
8560
{
8661
"cell_type": "code",
62+
"execution_count": null,
8763
"metadata": {
8864
"id": "5AXyxiLD4X93"
8965
},
66+
"outputs": [],
9067
"source": [
9168
"# import transformations, contraints, and the Augmenter\n",
9269
"from textattack.transformations import WordSwapRandomCharacterDeletion\n",
@@ -97,38 +74,18 @@
9774
"from textattack.constraints.pre_transformation import StopwordModification\n",
9875
"\n",
9976
"from textattack.augmentation import Augmenter"
100-
],
101-
"execution_count": null,
102-
"outputs": []
77+
]
10378
},
10479
{
10580
"cell_type": "code",
81+
"execution_count": null,
10682
"metadata": {
10783
"colab": {
10884
"base_uri": "https://localhost:8080/"
10985
},
11086
"id": "wFeXF_OL-vyw",
11187
"outputId": "c041e77e-accd-4a58-88be-9b140dd0cd56"
11288
},
113-
"source": [
114-
"# Set up transformation using CompositeTransformation()\n",
115-
"transformation = CompositeTransformation(\n",
116-
" [WordSwapRandomCharacterDeletion(), WordSwapQWERTY()]\n",
117-
")\n",
118-
"# Set up constraints\n",
119-
"constraints = [RepeatModification(), StopwordModification()]\n",
120-
"# Create augmenter with specified parameters\n",
121-
"augmenter = Augmenter(\n",
122-
" transformation=transformation,\n",
123-
" constraints=constraints,\n",
124-
" pct_words_to_swap=0.5,\n",
125-
" transformations_per_example=10,\n",
126-
")\n",
127-
"s = \"What I cannot create, I do not understand.\"\n",
128-
"# Augment!\n",
129-
"augmenter.augment(s)"
130-
],
131-
"execution_count": null,
13289
"outputs": [
13390
{
13491
"data": {
@@ -149,6 +106,24 @@
149106
"metadata": {},
150107
"output_type": "execute_result"
151108
}
109+
],
110+
"source": [
111+
"# Set up transformation using CompositeTransformation()\n",
112+
"transformation = CompositeTransformation(\n",
113+
" [WordSwapRandomCharacterDeletion(), WordSwapQWERTY()]\n",
114+
")\n",
115+
"# Set up constraints\n",
116+
"constraints = [RepeatModification(), StopwordModification()]\n",
117+
"# Create augmenter with specified parameters\n",
118+
"augmenter = Augmenter(\n",
119+
" transformation=transformation,\n",
120+
" constraints=constraints,\n",
121+
" pct_words_to_swap=0.5,\n",
122+
" transformations_per_example=10,\n",
123+
")\n",
124+
"s = \"What I cannot create, I do not understand.\"\n",
125+
"# Augment!\n",
126+
"augmenter.augment(s)"
152127
]
153128
},
154129
{
@@ -173,24 +148,14 @@
173148
},
174149
{
175150
"cell_type": "code",
151+
"execution_count": null,
176152
"metadata": {
177153
"colab": {
178154
"base_uri": "https://localhost:8080/"
179155
},
180156
"id": "WkYiVH6lQedu",
181157
"outputId": "cd5ffc65-ca80-45cd-b3bb-d023bcad09a4"
182158
},
183-
"source": [
184-
"# import the CheckListAugmenter\n",
185-
"from textattack.augmentation import CheckListAugmenter\n",
186-
"\n",
187-
"# Alter default values if desired\n",
188-
"augmenter = CheckListAugmenter(pct_words_to_swap=0.2, transformations_per_example=5)\n",
189-
"s = \"I'd love to go to Japan but the tickets are 500 dollars\"\n",
190-
"# Augment\n",
191-
"augmenter.augment(s)"
192-
],
193-
"execution_count": null,
194159
"outputs": [
195160
{
196161
"name": "stdout",
@@ -218,6 +183,16 @@
218183
"metadata": {},
219184
"output_type": "execute_result"
220185
}
186+
],
187+
"source": [
188+
"# import the CheckListAugmenter\n",
189+
"from textattack.augmentation import CheckListAugmenter\n",
190+
"\n",
191+
"# Alter default values if desired\n",
192+
"augmenter = CheckListAugmenter(pct_words_to_swap=0.2, transformations_per_example=5)\n",
193+
"s = \"I'd love to go to Japan but the tickets are 500 dollars\"\n",
194+
"# Augment\n",
195+
"augmenter.augment(s)"
221196
]
222197
},
223198
{
@@ -248,42 +223,25 @@
248223
},
249224
{
250225
"cell_type": "code",
226+
"execution_count": 9,
251227
"metadata": {
252228
"colab": {
253229
"base_uri": "https://localhost:8080/"
254230
},
255231
"id": "l2b-4scuXvkA",
256232
"outputId": "5a372fd2-226a-4970-a2c9-c09bf2af56c2"
257233
},
258-
"source": [
259-
"from textattack.augmentation import WordNetAugmenter\n",
260-
"\n",
261-
"augmenter = WordNetAugmenter(\n",
262-
" pct_words_to_swap=0.4,\n",
263-
" transformations_per_example=5,\n",
264-
" high_yield=True,\n",
265-
" enable_advanced_metrics=True,\n",
266-
")\n",
267-
"s = \"I'd love to go to Japan but the tickets are 500 dollars\"\n",
268-
"results = augmenter.augment(s)\n",
269-
"print(f\"Average Original Perplexity Score: {results[1]['avg_original_perplexity']}\\n\")\n",
270-
"print(f\"Average Augment Perplexity Score: {results[1]['avg_attack_perplexity']}\\n\")\n",
271-
"print(f\"Average Augment USE Score: {results[2]['avg_attack_use_score']}\\n\")\n",
272-
"print(f\"Augmentations:\")\n",
273-
"results[0]"
274-
],
275-
"execution_count": 9,
276234
"outputs": [
277235
{
278-
"output_type": "stream",
279236
"name": "stderr",
237+
"output_type": "stream",
280238
"text": [
281239
"Token indices sequence length is longer than the specified maximum sequence length for this model (1091 > 1024). Running this sequence through the model will result in indexing errors\n"
282240
]
283241
},
284242
{
285-
"output_type": "stream",
286243
"name": "stdout",
244+
"output_type": "stream",
287245
"text": [
288246
"Average Original Perplexity Score: 1.09\n",
289247
"\n",
@@ -295,7 +253,6 @@
295253
]
296254
},
297255
{
298-
"output_type": "execute_result",
299256
"data": {
300257
"text/plain": [
301258
"[\"I'd bang to operate to Japan but the ticket are 500 buck\",\n",
@@ -373,9 +330,27 @@
373330
" \"I'd screw to plump to Nihon but the tickets are 500 clam\"]"
374331
]
375332
},
333+
"execution_count": 9,
376334
"metadata": {},
377-
"execution_count": 9
335+
"output_type": "execute_result"
378336
}
337+
],
338+
"source": [
339+
"from textattack.augmentation import WordNetAugmenter\n",
340+
"\n",
341+
"augmenter = WordNetAugmenter(\n",
342+
" pct_words_to_swap=0.4,\n",
343+
" transformations_per_example=5,\n",
344+
" high_yield=True,\n",
345+
" enable_advanced_metrics=True,\n",
346+
")\n",
347+
"s = \"I'd love to go to Japan but the tickets are 500 dollars\"\n",
348+
"results = augmenter.augment(s)\n",
349+
"print(f\"Average Original Perplexity Score: {results[1]['avg_original_perplexity']}\\n\")\n",
350+
"print(f\"Average Augment Perplexity Score: {results[1]['avg_attack_perplexity']}\\n\")\n",
351+
"print(f\"Average Augment USE Score: {results[2]['avg_attack_use_score']}\\n\")\n",
352+
"print(f\"Augmentations:\")\n",
353+
"results[0]"
379354
]
380355
},
381356
{
@@ -388,5 +363,30 @@
388363
"We have now went through the basics in running `Augmenter` by either creating a new augmenter from scratch or using a pre-built augmenter. This could be done in as few as 4 lines of code so please give it a try if you haven't already! 🐙"
389364
]
390365
}
391-
]
392-
}
366+
],
367+
"metadata": {
368+
"colab": {
369+
"name": "Augmentation with TextAttack.ipynb",
370+
"provenance": []
371+
},
372+
"kernelspec": {
373+
"display_name": "Python 3",
374+
"language": "python",
375+
"name": "python3"
376+
},
377+
"language_info": {
378+
"codemirror_mode": {
379+
"name": "ipython",
380+
"version": 3
381+
},
382+
"file_extension": ".py",
383+
"mimetype": "text/x-python",
384+
"name": "python",
385+
"nbconvert_exporter": "python",
386+
"pygments_lexer": "ipython3",
387+
"version": "3.7.11"
388+
}
389+
},
390+
"nbformat": 4,
391+
"nbformat_minor": 1
392+
}

docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,31 @@
537537
"\n",
538538
"attack = Attack(goal_function, constraints, transformation, search_method)\n",
539539
"\n",
540+
"# here is a legacy code piece showing how the attack runs in details \n",
540541
"for example, label in custom_dataset:\n",
541542
" result = attack.attack(example, label)\n",
542543
" print(result.__str__(color_method=\"ansi\"))"
543544
]
545+
},
546+
{
547+
"cell_type": "code",
548+
"execution_count": null,
549+
"metadata": {},
550+
"outputs": [],
551+
"source": [
552+
"# here is currently recommendated API-centric way to use customized attack\n",
553+
"\n",
554+
"from textattack.loggers import CSVLogger # tracks a dataframe for us.\n",
555+
"from textattack.attack_results import SuccessfulAttackResult\n",
556+
"from textattack import Attacker, AttackArgs\n",
557+
"\n",
558+
"attack_args = AttackArgs(\n",
559+
" num_successful_examples=5, log_to_csv=\"results.csv\", csv_coloring_style=\"html\"\n",
560+
")\n",
561+
"attacker = Attacker(attack, dataset, attack_args)\n",
562+
"\n",
563+
"attack_results = attacker.attack_dataset()"
564+
]
544565
}
545566
],
546567
"metadata": {
@@ -549,7 +570,7 @@
549570
"provenance": []
550571
},
551572
"kernelspec": {
552-
"display_name": "Python 3 (ipykernel)",
573+
"display_name": "Python 3",
553574
"language": "python",
554575
"name": "python3"
555576
},
@@ -563,7 +584,7 @@
563584
"name": "python",
564585
"nbconvert_exporter": "python",
565586
"pygments_lexer": "ipython3",
566-
"version": "3.7.7"
587+
"version": "3.7.11"
567588
}
568589
},
569590
"nbformat": 4,

0 commit comments

Comments
 (0)