-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_best.py
177 lines (149 loc) · 5.22 KB
/
find_best.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import argparse
import itertools
import os
import subprocess
import tempfile
import multiprocessing
import nltk
nltk.download('omw')
def run(log_dir, dataset, T, s, feature_set, max_vocab, preprocessor, num_clauses, drop_p, max_literals):
fname = f"{dataset}_drop-p={drop_p}_max-literals={max_literals}_num-clauses={num_clauses}_max-vocab={max_vocab}_pre={preprocessor}_{'_'.join(feature_set)}_T={T}_s={s}"
fnamelog = f'{fname}.log'
fnametmp = f'{fname}.tmp.'
log_file = os.path.join(log_dir, fnamelog)
if os.path.isfile(log_file):
return
with tempfile.NamedTemporaryFile(delete=False, dir=log_dir, prefix=fnametmp) as temp_file:
cmd = ['python', '-u', 'pipeline.py']
cmd += ['--dataset', dataset]
cmd += ['--max-vocab', max_vocab]
cmd += ['--preprocessor', preprocessor]
cmd += ['--T', T]
cmd += ['--s', s]
cmd += ['--drop-p', drop_p]
cmd += ['--max-literals', max_literals]
cmd += ['--epochs', '150']
cmd += ['--feature'] + list(feature_set)
print(f'$ poetry run {" ".join(cmd)}', flush=True)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(p.stdout.readline, b''):
if args.monitor:
print(line.decode('utf-8'), end='', flush=True)
temp_file.write(line)
temp_file.flush()
p.stdout.close()
p.wait()
os.rename(temp_file.name, log_file)
def main(args):
datasets = ('FakeNewsNet-gossipcop', 'FakeNewsNet-politifact')
features = ('text', 'domain')
ts = ('50', '150', '200', '250')
ss = ('5', '10', '15')
max_vocabs = ('10000',)
preprocessors = ('v2',)
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
combinations = [
(subset, t, s, max_vocab, preprocessor) for i in range(len(features)+1)
for subset in itertools.combinations(features, i)
for t, s, max_vocab, preprocessor in itertools.product(ts, ss, max_vocabs, preprocessors)
]
batch_size = 2
with multiprocessing.Pool(batch_size) as p:
for dataset in datasets:
for feature_set, T, s, max_vocab, preprocessor in combinations:
if len(feature_set) > 0:
p.apply_async(run, kwds={
'log_dir': log_dir,
'dataset': dataset,
'T': T,
's': s,
'max_vocab': max_vocab,
'preprocessor': preprocessor,
'feature_set': feature_set,
})
p.close()
p.join()
# for simpler comparison
def main2(args):
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
batch_size = 2
with multiprocessing.Pool(batch_size) as p:
dataset = 'HateXPlain'
T = '150'
s = '10'
max_vocab = '15000'
preprocessor = 'v2'
num_clauses = '5000'
feature_set = ['text']
p.apply_async(run, kwds={
'log_dir': log_dir,
'dataset': dataset,
'T': T,
's': s,
'max_vocab': max_vocab,
'preprocessor': preprocessor,
'feature_set': feature_set,
'num_clauses': num_clauses,
})
T = '200'
s = '15'
num_clauses = '10000'
p.apply_async(run, kwds={
'log_dir': log_dir,
'dataset': dataset,
'T': T,
's': s,
'max_vocab': max_vocab,
'preprocessor': preprocessor,
'feature_set': feature_set,
'num_clauses': num_clauses,
})
p.close()
p.join()
# for A B testing
def main3(args):
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
datasets = ['HateXPlain']
max_literalss = ['3', '8', '16', '32']
max_vocab = '15000'
preprocessor = 'v2'
feature_set = ['text']
drop_p = '0.75'
batch_size = 2
with multiprocessing.Pool(batch_size) as p:
for dataset in datasets:
for max_literals in max_literalss:
p.apply_async(run, kwds={
'log_dir': log_dir,
'dataset': dataset,
'T': '150',
's': '10',
'max_vocab': max_vocab,
'preprocessor': preprocessor,
'num_clauses': '5000',
'feature_set': feature_set,
'drop_p': drop_p,
'max_literals': max_literals,
})
p.apply_async(run, kwds={
'log_dir': log_dir,
'dataset': dataset,
'T': '200',
's': '15',
'max_vocab': max_vocab,
'preprocessor': preprocessor,
'num_clauses': '10000',
'feature_set': feature_set,
'drop_p': drop_p,
'max_literals': max_literals,
})
p.close()
p.join()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--monitor', action='store_true')
args = parser.parse_args()
main3(args)