-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_conf_files.py
86 lines (67 loc) · 2.47 KB
/
write_conf_files.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
from copy import deepcopy
import json
import os
__author__ = 'mmb28'
def mkdirs_if_not_exists(dir):
"""
Creates a directory (and all intermediate directories) if it doesn't exists.
Behaves like mkdir -p, and is prone to race conditions
Source: http://stackoverflow.com/q/273192/419338
:param dir:
:return:
"""
if not (os.path.exists(dir) and os.path.isdir(dir)):
os.makedirs(dir)
template = {
'output_dir': None,
'corpus': None,
'taddy__iters': None,
'taddy__pretrained_pkl': None,
'taddy__specialised_pkl': None,
'yelp__three_way': None,
'clf__class': None,
}
def traditional_classifier_experiments():
for corpus in ['yelp', '20ng']:
for three_way in [True, False, None]:
for classifier in ['MultinomialNB', 'LinearSVC']:
if (three_way is not None) and corpus != 'yelp':
continue
conf = deepcopy(template)
conf['corpus'] = corpus
conf['yelp__three_way'] = three_way
conf['clf__class'] = classifier
yield conf
def taddy_experiments(first_id):
i = first_id
pretrained = 'models/wtv_model_cwiki_50perc.pkl'
classifier = 'TaddyClassifier'
for corpus in ['yelp', '20ng']:
for iters in [1, 10, 50]:
for three_way in [True, False, None]:
if (three_way is not None) and corpus != 'yelp':
continue
conf = deepcopy(template)
conf['corpus'] = corpus
conf['taddy__iters'] = iters
conf['taddy__pretrained_pkl'] = pretrained
conf['taddy__specialised_pkl'] = 'models/specialised_wtv_%d.pkl' % i
conf['yelp__three_way'] = three_way
conf['clf__class'] = classifier
i += 1
yield conf
def all_experiments():
experiments = []
experiments.extend(traditional_classifier_experiments())
experiments.extend(taddy_experiments(len(experiments) + 1))
return experiments
def write_conf(conf, output_dir):
mkdirs_if_not_exists(output_dir)
with open(os.path.join(output_dir, 'conf.txt'), 'w') as outf:
conf['output_dir'] = output_dir
json.dump(conf, outf, indent=1)
if __name__ == '__main__':
mkdirs_if_not_exists('results')
for i, conf in enumerate(all_experiments()):
output_dir = os.path.join('results', 'exp%d' % (i + 1))
write_conf(conf, output_dir)