Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Text Attacks
Manage
Activity
Members
Plan
Wiki
Redmine
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Adversarial Attacks
Text Attacks
Commits
f4ca3f8f
Commit
f4ca3f8f
authored
Aug 10, 2023
by
Paweł Walkowiak
Browse files
Options
Downloads
Patches
Plain Diff
Add parameters
parent
cdd255c7
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
experiments/parameters.json
+58
-0
58 additions, 0 deletions
experiments/parameters.json
experiments/scripts/attack.py
+24
-16
24 additions, 16 deletions
experiments/scripts/attack.py
with
82 additions
and
16 deletions
experiments/parameters.json
0 → 100644
+
58
−
0
View file @
f4ca3f8f
{
"max_sub"
:
12
,
"word_change_size"
:
0.4
,
"similarity_bound"
:
0.95
,
"word_synonym_threshold"
:
0.65
,
"lang"
:
{
"enron_spam"
:
"en"
,
"poleval"
:
"pl"
,
"20_news"
:
"en"
,
"wiki_pl"
:
"pl"
,
"ag_news"
:
"en"
,
"multi_emo"
:
"pl"
,
"imdb"
:
"en"
},
"class_mapping"
:
{
"enron_spam"
:
{
"ham"
:
"spam"
,
"spam"
:
"ham"
},
"wiki_pl"
:
{
"Albania"
:
"Arabowie"
,
"Amerykanscy-prozaicy"
:
"Albania"
,
"Arabowie"
:
"Albania"
,
"Astronautyka"
:
"Albania"
,
"Choroby"
:
"Albania"
,
"Egipt"
:
"Albania"
,
"Ekologia-roslin"
:
"Albania"
,
"Filmy-animowane"
:
"Albania"
,
"Galezie-prawa"
:
"Albania"
,
"Gry-komputerowe"
:
"Albania"
,
"Katolicyzm"
:
"Albania"
,
"Karkonosze"
:
"Albania"
,
"Komiksy"
:
"Albania"
,
"Komputery"
:
"Albania"
,
"Kotowate"
:
"Albania"
,
"Kultura-Chin"
:
"Albania"
,
"Monety"
:
"Albania"
,
"Muzyka-powazna"
:
"Albania"
,
"Narciarstwo"
:
"Albania"
,
"Narkomania"
:
"Albania"
,
"Niemieccy-wojskowi"
:
"Albania"
},
"ag_news"
:
{
"Business"
:
"Sci_Tech"
,
"Sci_Tech"
:
"Bussines"
,
"Sports"
:
"Bussines"
,
"World"
:
"Bussines"
},
"multi_emo"
:
{
"negative"
:
"positive"
,
"positive"
:
"negative"
},
"imdb"
:
{
"negative"
:
"positive"
,
"positive"
:
"negative"
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
experiments/scripts/attack.py
+
24
−
16
View file @
f4ca3f8f
...
...
@@ -9,7 +9,8 @@ import os
import
torch
from
tqdm
import
tqdm
from
textfooler
import
Attack
,
TextFooler
,
Similarity
,
BaseLine
,
\
process
,
run_queue
,
filter_similarity_queue
,
spoil_queue
,
AttackMethod
process
,
run_queue
,
filter_similarity_queue
,
spoil_queue
,
\
AttackMethod
,
get_xai_importance_diff
from
time
import
sleep
,
time
from
multiprocessing
import
Process
from
multiprocessing
import
Queue
,
Manager
...
...
@@ -259,24 +260,28 @@ def load_xai_importance(input_dir):
)
def
main
(
dataset_name
:
str
,
attack_type
:
str
):
"""
Downloads the dataset to the output directory.
"""
lang
=
{
"
enron_spam
"
:
"
en
"
,
"
poleval
"
:
"
pl
"
,
"
20_news
"
:
"
en
"
,
"
wiki_pl
"
:
"
pl
"
,
"
ag_news
"
:
"
en
"
,
"
multi_emo
"
:
"
pl
"
,
"
imdb
"
:
"
en
"
,
}[
dataset_name
]
params
=
{}
with
open
(
'
../parameters.json
'
,
'
r
'
)
as
fin
:
params
=
json
.
load
(
fin
)
lang
=
params
.
get
(
"
lang
"
,
{})[
dataset_name
]
xai_global
,
xai_local
=
{},
{}
xai_global_directed
=
{}
if
"
attack_xai
"
in
attack_type
:
importance
=
load_xai_importance
(
f
"
data/explanations/
{
dataset_name
}
"
)
xai_global
,
xai_local
=
importance
[
0
],
importance
[
1
]
max_sub
=
12
word_change_size
=
0.4
similarity_bound
=
0.95
word_synonym_threshold
=
0.65
if
"
attack_xai_directed
"
in
attack_type
:
class_mapping
=
params
.
get
(
"
class_mapping
"
,
{})[
dataset_name
]
xai_global_directed
=
{
get_xai_importance_diff
(
xai_global
[
source
],
xai_global
[
target
])
for
source
,
target
in
class_mapping
.
items
()
}
max_sub
=
params
.
get
(
"
max_sub
"
,
10
)
word_change_size
=
params
.
get
(
"
word_change_size
"
,
0.5
)
similarity_bound
=
params
.
get
(
"
similarity_bound
"
,
0.8
)
word_synonym_threshold
=
params
.
get
(
"
word_synonym_threshold
"
,
0.65
)
params
=
{
"
attack_textfooler
"
:
[
lang
,
AttackMethod
.
SYNONYM
,
word_synonym_threshold
],
...
...
@@ -291,7 +296,10 @@ def main(dataset_name: str, attack_type: str):
"
attack_xai_char_insert
"
:
[
lang
,
xai_global
,
xai_local
,
GLOBAL
,
AttackMethod
.
LETTER_INSERT
,
0.0
,
word_change_size
],
"
attack_xai_char_substitute
"
:
[
lang
,
xai_global
,
xai_local
,
GLOBAL
,
AttackMethod
.
LETTER_SUBSTITUTE
,
0.0
,
word_change_size
],
"
attack_xai_char_mixin
"
:
[
lang
,
xai_global
,
xai_local
,
GLOBAL
,
AttackMethod
.
LETTER_MIX
,
0.0
,
word_change_size
],
"
attack_xai_char_discard_local
"
:
[
lang
,
xai_global
,
xai_local
,
LOCAL
,
AttackMethod
.
LETTER_DISCARD
,
0.0
,
word_change_size
]
"
attack_xai_char_discard_local
"
:
[
lang
,
xai_global
,
xai_local
,
LOCAL
,
AttackMethod
.
LETTER_DISCARD
,
0.0
,
word_change_size
],
"
attack_xai_directed_char_mixin
"
:
[
lang
,
xai_global_directed
,
xai_local
,
GLOBAL
,
AttackMethod
.
LETTER_MIX
,
0.0
,
word_change_size
],
"
attack_xai_directed
"
:
[
lang
,
xai_global_directed
,
xai_local
,
GLOBAL
,
AttackMethod
.
SYNONYM
,
similarity_bound
],
"
attack_xai_directed_discard
"
:
[
lang
,
xai_global_directed
,
xai_local
,
GLOBAL
,
AttackMethod
.
DISCARD
,
similarity_bound
],
}[
attack_type
]
output_dir
=
f
"
data/results/
{
attack_type
}
/
{
dataset_name
}
/
"
input_file
=
f
"
data/classification/
{
dataset_name
}
/test.jsonl
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment