-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontrol_experiments.py
271 lines (218 loc) · 9.24 KB
/
control_experiments.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from sacred import Experiment
from sacred.observers import MongoObserver
import subprocess
import sys
import re
ex = Experiment('my_experiment')
@ex.config
def my_config():
skip_testing = 0
reload = 0
max_epochs = 50
dynet_gpu = 0
host="localhost"
experiment_name = "default_experiment_name"
datasets_root = "/home/onur/projects/research/turkish-ner/datasets"
learning_rate = 0.01
crf = 1
# lr_method = "sgd-learning_rate_float@%lf" % learning_rate
lr_method = "adam"
batch_size = 1
sparse_updates_enabled = 1
dropout = 0.5
char_dim = 64
char_lstm_dim = 64
morpho_tag_dim = 64
morpho_tag_lstm_dim = 64
morpho_tag_type = "char"
morpho_tag_column_index = 1
integration_mode = 0
active_models = 0
multilayer = 0
shortcut_connections = 0
word_dim = 64
word_lstm_dim = 64
cap_dim = 0
# char_dim = 200
# char_lstm_dim = 200
#
# morpho_tag_dim = 100
# morpho_tag_lstm_dim = 200
# morpho_tag_type = "wo_root"
#
# morpho_tag_column_index = 1
#
# integration_mode = 0
#
# word_dim = 300
# word_lstm_dim = 200
# cap_dim = 100
train_filepath = "turkish/gungor.ner.train.only_consistent"
dev_filepath = "turkish/gungor.ner.dev.only_consistent"
test_filepath = "turkish/gungor.ner.test.only_consistent"
yuret_train_filepath = "turkish/train.merge.utf8.gungor_format"
yuret_test_filepath = "turkish/test.merge.utf8.gungor_format"
train_with_yuret = 0
test_with_yuret = 1
use_golden_morpho_analysis_in_word_representation = 0
embeddings_filepath = "turkish/we-300.txt"
@ex.main
def my_main():
run_a_single_configuration_without_fabric()
from utils import read_args, form_parameters_dict, get_name, get_model_subpath
@ex.capture
def run_a_single_configuration_without_fabric(
datasets_root,
crf,
lr_method,
batch_size,
sparse_updates_enabled,
dropout,
char_dim,
char_lstm_dim,
morpho_tag_dim,
morpho_tag_lstm_dim,
morpho_tag_type,
morpho_tag_column_index,
word_dim,
word_lstm_dim,
cap_dim, skip_testing, max_epochs,
train_filepath,
dev_filepath,
test_filepath,
yuret_train_filepath,
yuret_test_filepath,
train_with_yuret,
test_with_yuret,
use_golden_morpho_analysis_in_word_representation,
embeddings_filepath,
integration_mode,
active_models,
multilayer,
shortcut_connections,
reload,
dynet_gpu,
_run):
"""
python train.py --pre_emb ../../data/we-300.txt --train dataset/gungor.ner.train.only_consistent --dev dataset/gungor.ner.dev.only_consistent --test dataset/gungor.ner.test.only_consistent --word_di
m 300 --word_lstm_dim 200 --word_bidirect 1 --cap_dim 100 --crf 1 [email protected] --maximum-epochs 50 --char_dim 200 --char_lstm_dim 200 --char_bid
irect 1 --overwrite-mappings 1 --batch-size 1 --morpho_tag_dim 100 --integration_mode 2
"""
execution_part = "python train.py --overwrite-mappings 1 "
if sparse_updates_enabled == 0:
execution_part += "--disable_sparse_updates "
if dynet_gpu == 1:
execution_part += "--dynet-gpu 1 "
if train_with_yuret == 1:
execution_part += "--train_with_yuret "
if use_golden_morpho_analysis_in_word_representation == 1:
execution_part += "--use_golden_morpho_analysis_in_word_representation "
if word_dim == 0:
embeddings_part = ""
else:
if embeddings_filepath:
embeddings_part = "--pre_emb %s/%s " % (datasets_root, embeddings_filepath)
else:
embeddings_part = ""
print (train_filepath, dev_filepath, test_filepath, skip_testing, max_epochs)
always_constant_part = "-T %s/%s " \
"-d %s/%s " \
"-t %s/%s " \
"%s" \
"%s" \
"--yuret_train %s/%s " \
"--yuret_test %s/%s " \
"%s" \
"--skip-testing %d " \
"--tag_scheme iobes " \
"--maximum-epochs %d " % (datasets_root, train_filepath,
datasets_root, dev_filepath,
datasets_root, test_filepath,
"--train_with_yuret " if train_with_yuret else "",
"--test_with_yuret " if test_with_yuret else "",
datasets_root, yuret_train_filepath,
datasets_root, yuret_test_filepath,
embeddings_part,
skip_testing, max_epochs)
commandline_args = always_constant_part + \
"--crf %d " \
"--lr_method %s " \
"--batch-size %d " \
"--dropout %1.1lf " \
"--char_dim %d " \
"--char_lstm_dim %d " \
"--morpho_tag_dim %d " \
"--morpho_tag_lstm_dim %d " \
"--morpho_tag_type %s " \
"--morpho-tag-column-index %d " \
"--word_dim %d " \
"--word_lstm_dim %d "\
"--cap_dim %d "\
"--integration_mode %d " \
"--active_models %d " \
"--multilayer %d " \
"--shortcut_connections %d " \
"--reload %d" % (crf,
lr_method,
batch_size,
dropout,
char_dim,
char_lstm_dim,
morpho_tag_dim,
morpho_tag_lstm_dim,
morpho_tag_type,
morpho_tag_column_index,
word_dim,
word_lstm_dim,
cap_dim,
integration_mode,
active_models,
multilayer,
shortcut_connections,
reload)
# tagger_root = "/media/storage/genie/turkish-ner/code/tagger"
print _run
print _run.info
print subprocess.check_output(["id"])
print subprocess.check_output(["pwd"])
opts = read_args(args_as_a_list=commandline_args.split(" "))
print opts
parameters = form_parameters_dict(opts)
print parameters
# model_path = get_name(parameters)
model_path = get_model_subpath(parameters)
print model_path
task_names = ["NER", "MORPH", "YURET"]
for task_name in task_names:
_run.info["%s_dev_f_score" % task_name] = dict()
_run.info["%s_test_f_score" % task_name] = dict()
_run.info['starting'] = 1
dummy_prefix = ""
print dummy_prefix + execution_part + commandline_args
process = subprocess.Popen((dummy_prefix + execution_part + commandline_args).split(" "), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
def record_metric(epoch, label, value):
if str(epoch) in _run.info[label]:
_run.info[label][str(epoch)].append(value)
else:
_run.info[label][str(epoch)] = list()
_run.info[label][str(epoch)].append(value)
def capture_information(line):
# 1
"""
NER Epoch: %d Best dev and accompanying test score, best_dev, best_test: %lf %lf
"""
for task_name in task_names:
m = re.match("^%s Epoch: (\d+) .* best_dev, best_test: (.+) (.+)$" % task_name, line)
if m:
epoch = int(m.group(1))
best_dev = float(m.group(2))
best_test = float(m.group(3))
record_metric(epoch, "%s_dev_f_score" % task_name, best_dev)
record_metric(epoch, "%s_test_f_score" % task_name, best_test)
for line in iter(process.stdout.readline, ''):
sys.stdout.write(line)
capture_information(line)
sys.stdout.flush()
return model_path
if __name__ == '__main__':
ex.run_commandline()