forked from wangxinyu0922/Second_Order_SDP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
321 lines (291 loc) · 12.3 KB
/
main.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright 2017 Timothy Dozat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import re
import os
import shutil
import sys
import time
import six
from six.moves import input
import codecs
from argparse import ArgumentParser
from parser.config import Config
import parser
from hpo import MVGHPO
#from hpo.evals.syndep_eval import evaluate_tokens
from hpo.evals.conll18_eval import evaluate
import pdb
section_names = set()
with codecs.open(os.path.join('config', 'defaults.cfg')) as f:
section_regex = re.compile('\[(.*)\]')
for line in f:
match = section_regex.match(line)
if match:
section_names.add(match.group(1))
#-------------------------------------------------------------
def resolve_network_dependencies(config, network_class, network_list, networks):
if network_list in ('None', ''):
return set(), networks
else:
network_list = network_list.split(':')
if network_class not in networks:
for _network_class in network_list:
config_file = os.path.join(config.get('DEFAULT', _network_class + '_dir'), 'config.cfg')
_config = Config(config_file=config_file)
_network_list = _config.get(_network_class, 'input_network_classes')
input_networks, networks = resolve_network_dependencies(_config, _network_class, _network_list, networks)
NetworkClass = getattr(parser, _network_class)
networks[_network_class] = NetworkClass(input_networks=input_networks, config=config)
return set(networks[_network_class] for _network_class in network_list), networks
#-------------------------------------------------------------
# TODO make all DEFAULT options as regular flags
#***************************************************************
# Set up the argument parser
def main():
""" --section_name opt1=value1 opt2=value2 opt3=value3 """
argparser = ArgumentParser('Network')
argparser.add_argument('--save_metadir')
argparser.add_argument('--save_dir')
subparsers = argparser.add_subparsers()
# set up the training parser
train_parser = subparsers.add_parser('train')
train_parser.set_defaults(action=train)
train_parser.add_argument('network_class')
train_parser.add_argument('--force', action='store_true')
train_parser.add_argument('--noscreen', action='store_true')
train_parser.add_argument('--load', action='store_true')
train_parser.add_argument('--config_file', default='')
train_parser.add_argument('--debug', action='store_true')
train_parser.add_argument('--nornn', action='store_true')
train_parser.add_argument('--nowarning', action='store_true')
for section_name in section_names:
train_parser.add_argument('--'+section_name, nargs='+')
hpo_parser = subparsers.add_parser('hpo')
hpo_parser.set_defaults(action=hpo)
hpo_parser.add_argument('network_class')
hpo_parser.add_argument('--noscreen', action='store_true')
hpo_parser.add_argument('--config_file', default='')
hpo_parser.add_argument('--rand_file', default='hpo/config/default.csv')
hpo_parser.add_argument('--eval_metric', default='LAS')
for section_name in section_names:
hpo_parser.add_argument('--'+section_name, nargs='+')
# set up the hpo parser
# set up the inference parser
run_parser = subparsers.add_parser('run')
run_parser.set_defaults(action=run)
run_parser.add_argument('conllu_files', nargs='+')
run_parser.add_argument('--output_dir')
run_parser.add_argument('--output_filename')
run_parser.add_argument('--testing', action='store_true')
run_parser.add_argument('--debug', action='store_true')
run_parser.add_argument('--check_iter', action='store_true')
run_parser.add_argument('--gen_tree', action='store_true')
run_parser.add_argument('--get_argmax', action='store_true')
run_parser.add_argument('--nornn', action='store_true')
for section_name in section_names:
run_parser.add_argument('--'+section_name, nargs='+')
# parse the arguments
kwargs = vars(argparser.parse_args())
kwargs.pop('action')(**kwargs)
return
#===============================================================
# Train
def train(**kwargs):
""""""
# Get the special arguments
load = kwargs.pop('load')
force = kwargs.pop('force')
noscreen = kwargs.pop('noscreen')
save_dir = kwargs.pop('save_dir')
save_metadir = kwargs.pop('save_metadir')
network_class = kwargs.pop('network_class')
config_file = kwargs.pop('config_file')
debug = kwargs.pop('debug')
nornn = kwargs.pop('nornn')
nowarning = kwargs.pop('nowarning')
# Get the cl-defined options
kwargs = {key: value for key, value in six.iteritems(kwargs) if value is not None}
for section, values in six.iteritems(kwargs):
if section in section_names:
values = [value.split('=', 1) for value in values]
kwargs[section] = {opt: value for opt, value in values}
if 'DEFAULT' not in kwargs:
kwargs['DEFAULT'] = {}
kwargs['DEFAULT']['network_class'] = network_class
# Figure out the save_directory
if save_metadir is not None:
kwargs['DEFAULT']['save_metadir'] = save_metadir
if save_dir is not None:
kwargs['DEFAULT']['save_dir'] = save_dir
config = Config(config_file=config_file, **kwargs)
save_dir = config.get('DEFAULT', 'save_dir')
if config.get('DEFAULT','AUTO_dir')=='True':
save_dir=save_dir+config.get('DEFAULT','modelname')
kwargs['DEFAULT']['save_dir'] = save_dir
config = Config(config_file=config_file, **kwargs)
#pdb.set_trace()
print (save_dir)
# If not loading, ask the user if they want to overwrite the directory
if not load and os.path.isdir(save_dir) and not nowarning:
#'''
if not force:
input_str = ''
while input_str not in ('y', 'n', 'yes', 'no'):
input_str = input('{} already exists. It will be deleted if you continue. Do you want to proceed? [Y/n] '.format(save_dir)).lower()
if input_str in ('n', 'no'):
print()
sys.exit(0)
elif noscreen:
sys.exit(0)
#'''
shutil.rmtree(save_dir)
# If the save_dir wasn't overwritten, load its config_file
if os.path.isdir(save_dir):
config_file = os.path.join(save_dir, 'config.cfg')
else:
os.makedirs(save_dir)
os.system('git rev-parse HEAD >> {}'.format(os.path.join(save_dir, 'HEAD')))
network_list = config.get(network_class, 'input_network_classes')
if not load:
with open(os.path.join(save_dir, 'config.cfg'), 'w') as f:
config.write(f)
input_networks, networks = resolve_network_dependencies(config, network_class, network_list, {})
NetworkClass = getattr(parser, network_class)
network = NetworkClass(input_networks=input_networks, config=config)
network.train(load=load, noscreen=noscreen, debug=debug,nornn=nornn)
return
#===============================================================
# HPO
def hpo(**kwargs):
""""""
# Get the special arguments
noscreen = kwargs.pop('noscreen')
save_dir = kwargs.pop('save_dir')
save_metadir = kwargs.pop('save_metadir')
network_class = kwargs.pop('network_class')
config_file = kwargs.pop('config_file')
rand_file = kwargs.pop('rand_file')
eval_metric = kwargs.pop('eval_metric')
# Get the cl-defined options
kwargs = {key: value for key, value in six.iteritems(kwargs) if value is not None}
for section, values in six.iteritems(kwargs):
if section in section_names:
values = [value.split('=', 1) for value in values]
kwargs[section] = {opt: value for opt, value in values}
if 'DEFAULT' not in kwargs:
kwargs['DEFAULT'] = {}
kwargs['DEFAULT']['network_class'] = network_class
# Figure out the save_directory
if save_metadir is not None:
kwargs['DEFAULT']['save_metadir'] = save_metadir
if save_dir is None:
save_dir = Config(**kwargs).get('DEFAULT', 'save_dir')
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# Get the randomly generated options and possibly add them
#-------------------------------------------------------------
lang = kwargs['DEFAULT']['LANG']
treebank = kwargs['DEFAULT']['TREEBANK']
lc = kwargs['DEFAULT']['LC']
tb = kwargs['DEFAULT']['TB']
base = 'data/CoNLL18/UD_{}-{}/{}_{}-ud-dev.conllu'.format(lang, treebank, lc, tb)
def eval_func(save_dir):
return evaluate(base, os.path.join(save_dir, 'parsed', base), eval_metric)
#-------------------------------------------------------------
rargs = next(MVGHPO(rand_file, save_dir, eval_func=eval_func))
for section in rargs:
if section not in kwargs:
kwargs[section] = rargs[section]
else:
for option, value in six.iteritems(rargs[section]):
if option not in kwargs[section]:
kwargs[section][option] = value
save_dir = os.path.join(save_dir, str(int(time.time()*100000)))
# If not loading, ask the user if they want to overwrite the directory
if os.path.isdir(save_dir):
print()
sys.exit(0)
else:
os.mkdir(save_dir)
os.system('git rev-parse HEAD >> {}'.format(os.path.join(save_dir, 'HEAD')))
kwargs['DEFAULT']['save_dir'] = save_dir
config = Config(config_file=config_file, **kwargs)
network_list = config.get(network_class, 'input_network_classes')
with open(os.path.join(save_dir, 'config.cfg'), 'w') as f:
config.write(f)
input_networks, networks = resolve_network_dependencies(config, network_class, network_list, {})
NetworkClass = getattr(parser, network_class)
network = NetworkClass(input_networks=input_networks, config=config)
network.train(noscreen=noscreen)
return
#===============================================================
def run(**kwargs):
""""""
#pdb.set_trace()
# Get the special arguments
save_dir = kwargs.pop('save_dir')
save_metadir = kwargs.pop('save_metadir')
conllu_files = kwargs.pop('conllu_files')
output_dir = kwargs.pop('output_dir')
output_filename = kwargs.pop('output_filename')
testing = kwargs.pop('testing')
debug = kwargs.pop('debug')
nornn = kwargs.pop('nornn')
check_iter = kwargs.pop('check_iter')
gen_tree = kwargs.pop('gen_tree')
get_argmax = kwargs.pop('get_argmax')
# Get the cl-defined options
kwargs = {key: value for key, value in six.iteritems(kwargs) if value is not None}
for section, values in six.iteritems(kwargs):
if section in section_names:
values = [value.split('=', 1) for value in values]
kwargs[section] = {opt: value for opt, value in values}
if 'DEFAULT' not in kwargs:
kwargs['DEFAULT'] = {}
# Figure out the save_directory
if save_metadir is not None:
kwargs['DEFAULT']['save_metadir'] = save_metadir
if save_dir is None:
save_dir = Config(**kwargs).get('DEFAULT', 'save_dir')
config_file = os.path.join(save_dir, 'config.cfg')
namelist=save_dir.split('GraphParserNetwork')
kwargs['DEFAULT']['save_dir'] = save_dir
#kwargs['DEFAULT']['save_dir'] = namelist[0]+'GraphParserNetwork'
if testing:
#kwargs['DEFAULT']['AUTO_dir'] = 'True'
#kwargs['DEFAULT']['modelname'] = namelist[-1]
kwargs['CoNLLUDataset']={}
if debug:
kwargs['CoNLLUDataset']['batch_size'] = 1000
kwargs['CoNLLUDataset']['max_buckets'] = 30
else:
kwargs['CoNLLUDataset']['batch_size'] = 1000
config = Config(defaults_file='', config_file=config_file, **kwargs)
with open('debug.cfg', 'w') as f:
config.write(f)
network_class = config.get('DEFAULT', 'network_class')
network_list = config.get(network_class, 'input_network_classes')
input_networks, networks = resolve_network_dependencies(config, network_class, network_list, {})
NetworkClass = getattr(parser, network_class)
network = NetworkClass(input_networks=input_networks, config=config)
network.parse(conllu_files, output_dir=output_dir, output_filename=output_filename, testing=testing, debug=debug,nornn=nornn, check_iter=check_iter, gen_tree=gen_tree, get_argmax=get_argmax)
return
#***************************************************************
if __name__ == '__main__':
main()