Skip to content

Commit

Permalink
Merge pull request #77 from QData/constraint-example
Browse files Browse the repository at this point in the history
Tutorial 2: custom constraint
  • Loading branch information
jxmorris12 committed Apr 22, 2020
2 parents 4934ea6 + c7caffa commit a9d6574
Show file tree
Hide file tree
Showing 5 changed files with 582 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# A custom transformation\n",
"## A custom transformation\n",
"\n",
"This lesson explains how to create a custom transformation. In TextAttack, many transformations involve *word swaps*: they take a word and try and find suitable substitutes. Some attacks focus on replacing characters with neighboring characters to create \"typos\". (These don't intend to preserve the grammaticality of inputs.) Other attacks rely on semantics: they take a word and try to replace it with semantic equivalents.\n",
"\n",
Expand All @@ -35,9 +35,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"from textattack.transformations.word_swap import WordSwap\n",
Expand Down Expand Up @@ -74,9 +72,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# Import the dataset.\n",
Expand All @@ -99,10 +95,8 @@
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from textattack.attack_methods import GreedyWordSwap\n",
Expand All @@ -115,6 +109,38 @@
"attack = GreedyWordSwap(goal_function, transformation, constraints)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's print our attack to see all the parameters:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GreedyWordSwap(\n",
" (goal_function): UntargetedClassification\n",
" (transformation): BananaWordSwap(\n",
" (replace_stopwords): False\n",
" )\n",
" (constraints): \n",
" \n",
" (is_black_box): True\n",
")\n"
]
}
],
"source": [
"print(attack)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -133,8 +159,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"CSVLogger exiting without calling flush().\n",
"100%|██████████| 10/10 [00:02<00:00, 4.61it/s]\n"
"12it [00:03, 3.18it/s] \n"
]
}
],
Expand Down Expand Up @@ -162,7 +187,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -185,12 +210,12 @@
" <tr>\n",
" <th>1</th>\n",
" <td>Toshiba 20 TV freaks out, sends distress signal See what <font color = purple>happens</font> when your warranty runs out?. In this case, a 20 Toshiba owned by Chris van Rossman started sending out the international distress signal at 121.</td>\n",
" <td>Toshiba 20 TV freaks out, sends distress signal See what <font color = blue>banana</font> when your warranty runs out?. In this case, a 20 Toshiba owned by Chris van Rossman started sending out the international distress signal at 121.</td>\n",
" <td>Toshiba 20 TV freaks out, sends distress signal See what <font color = blue>banana</font> when your warranty runs out?. In this case, a 20 Toshiba owned by Chris van Rossman started sending out the international distress signal at 121.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>British hostage tried fleeing before death: report The portrait of Ken <font color = purple>Bigley</font>, who was murdered in Iraq October 7, stands in front of the congregation during a service at Liverpool #39;s Roman Catholic Cathedral on October 10.</td>\n",
" <td>British hostage tried fleeing before death: report The portrait of Ken <font color = red>banana</font>, who was murdered in Iraq October 7, stands in front of the congregation during a service at Liverpool #39;s Roman Catholic Cathedral on October 10.</td>\n",
" <td>British hostage tried fleeing before death: report The portrait of Ken Bigley, who was murdered in Iraq October 7, stands in front of the congregation during a service at Liverpool #39;s Roman Catholic Cathedral on October 10.</td>\n",
" <td>British hostage tried fleeing before death: report The portrait of Ken Bigley, who was murdered in Iraq October 7, stands in front of the congregation during a service at Liverpool #39;s Roman Catholic Cathedral on October 10.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
Expand Down Expand Up @@ -222,6 +247,16 @@
" <td><font color = purple>Study</font>: Wild Monkeys Resort to Use of Tools WASHINGTON - Wild South <font color = purple>American</font> <font color = purple>monkeys</font> <font color = purple>routinely</font> <font color = purple>use</font> <font color = purple>fist</font>-<font color = purple>sized</font> <font color = purple>rocks</font> to crack open <font color = purple>seeds</font> and to dig in dry Brazilian soil for grubs and <font color = purple>edible</font> <font color = purple>tubers</font>, <font color = purple>researchers</font> report in the journal Science.</td>\n",
" <td><font color = blue>banana</font>: Wild Monkeys Resort to Use of Tools WASHINGTON - Wild South <font color = blue>banana</font> <font color = blue>banana</font> <font color = blue>banana</font> <font color = blue>banana</font> <font color = blue>banana</font>-<font color = blue>banana</font> <font color = blue>banana</font> to crack open <font color = blue>banana</font> and to dig in dry Brazilian soil for grubs and <font color = blue>banana</font> <font color = blue>banana</font>, <font color = blue>banana</font> report in the journal Science.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Bryant's Request to Seal Evidence <font color = green>Opposed</font> (AP) AP - The prosecutor who charged Kobe Bryant with <font color = green>felony</font> sexual assault has <font color = green>joined</font> news organizations in <font color = green>opposing</font> an attempt by the <font color = green>NBA</font> star's attorney to permanently seal evidence and documents in the case.</td>\n",
" <td>Bryant's Request to Seal Evidence <font color = blue>banana</font> (AP) AP - The prosecutor who charged Kobe Bryant with <font color = blue>banana</font> sexual assault has <font color = blue>banana</font> news organizations in <font color = blue>banana</font> an attempt by the <font color = blue>banana</font> star's attorney to permanently seal evidence and documents in the case.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td><font color = green>Eagles</font> have lift off Crystal Palace were triumphant last night (Oct 4) over 10-man Fulham at <font color = green>Selhurst</font> <font color = green>Park</font>, <font color = green>lifting</font> themselves off the bottom of the Premiership.</td>\n",
" <td><font color = blue>banana</font> have lift off Crystal Palace were triumphant last night (Oct 4) over 10-man Fulham at <font color = blue>banana</font> <font color = blue>banana</font>, <font color = blue>banana</font> themselves off the bottom of the Premiership.</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
Expand Down Expand Up @@ -255,9 +290,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "newtorch",
"display_name": "torch",
"language": "python",
"name": "newtorch"
"name": "build_central"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -269,7 +304,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.7"
}
},
"nbformat": 4,
Expand Down

0 comments on commit a9d6574

Please sign in to comment.