-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (130 loc) · 3.82 KB
/
main.py
File metadata and controls
144 lines (130 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Main.py script."""
from argparse import ArgumentParser
import importlib
import time
from src.Experiment import Experiment
from src.Tasks.Negate import Negate
from src.Tasks.POSDrop import POSDrop
from src.Tasks.DropWordsOneDim import DropWordsOneDim
from src.Tasks.Repetition import Repetition
from src.Tasks.SwapWordsOneDim import SwapWordsOneDim
if __name__ == "__main__":
""" Main script
Main script to run experiments.
Passing of different command line arguments is supported:
-m / --metric : name of metric-class in
the metrics.custom_metrics folder
-n / --number : number of samples, int
-s / --steps : number of steps, int
-d / --dir : path to directory, str
-t / --title : title of the experiment, str
-sd / --seed : seed for sampling : int
-st / --sentence : set, if sample should
consists of sentences instead of texts
"""
parser = ArgumentParser()
parser.add_argument(
"-m",
"--metrics_class",
required=True,
type=str,
dest="m",
help="Name of metric-class in the metrics.custom_metrics folder.")
parser.add_argument(
"-n",
"--number",
default=5,
type=int,
dest="n",
help="Number of texts.")
parser.add_argument(
"-s",
"--steps",
default=2,
type=int,
dest="steps",
help="Amount of damaging steps.")
parser.add_argument(
"-d",
"--dir",
default=None,
type=str,
dest="dir",
help="Already existing directory to continue computation.")
parser.add_argument(
"-t",
"--title",
default="exp",
type=str,
dest="title",
help="Title for a new experiment. Will be the folder name.")
parser.add_argument(
"-sd",
"--seed",
default=None,
type=int,
dest="seed",
help="Seed for sampling.")
parser.add_argument(
"-st",
"--sentence",
default=False,
action='store_true',
dest="st",
help="Set to sample sentences instead of texts.")
parser.add_argument(
"-sc",
"--scale-down",
default=None,
type=int,
dest="sc",
help="Scale sample down (if computational costs are too high).")
parser.add_argument(
"-stn",
"--sentence-n",
default=None,
type=int,
dest="stn",
help="Number of sentences, if -st option is chosen.")
args = parser.parse_args()
args = vars(args)
# Import Custom Metric dynamically
metric_class: type = getattr(
importlib.import_module("src.metrics.custom_metrics." + args['m']),
args['m'])
instance: any = metric_class()
tasks: list = [
(DropWordsOneDim, ),
(SwapWordsOneDim, ),
(Repetition,),
(Negate, ),
(POSDrop,)]
metrics: list = [instance]
exp = Experiment(
loc=args['dir'],
name=args['title'],
sentence=args['st'],
verbose=True,
scale_down=args['sc'],
sentence_n=args['stn'])
exp.setup(
tasks,
metrics,
data_specs={
'name': 'cnn_dailymail',
'version': '3.0.0',
'n': args['n'],
'f': (lambda data, sample: data['train'][sample]['article']),
'seed': args['seed']},
steps={
'steps': args['steps'],
'txt': args['steps'],
'snt': args['steps']},
pos_list=['ADJ', 'DET', 'VERB', 'NOUN'])
# Perturbate
start_time = time.time()
exp.perturbate()
print("--- Perturbation took %s seconds ---" % (time.time() - start_time))
start_time = time.time()
exp.evaluate()
print("--- Evaluation took %s seconds ---" % (time.time() - start_time))