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" ,
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 " ,
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" : {
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 {
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" ,
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 {
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 " ,
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 " ,
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 {
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+ }
0 commit comments