-
Notifications
You must be signed in to change notification settings - Fork 2
/
configuration.py
541 lines (488 loc) · 16.6 KB
/
configuration.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import logging
import os
import configargparse
def init_logger(
root_log_level=logging.DEBUG,
console_log_level=logging.NOTSET,
log_file=None,
log_file_level=logging.NOTSET,
):
"""This funtion initializes a customized logger
Keyword Arguments:
root_log_level {int} -- root logging level (default: {logging.DEBUG})
console_log_level {int} -- console logging level (default: {logging.NOTSET})
log_file {str} -- logging file path (default: {None})
log_file_level {int} -- logging file level (default: {logging.NOTSET})
"""
log_format = logging.Formatter(
"[%(asctime)s - %(filename)s - line:%(lineno)d - %(levelname)s]: %(message)s"
)
handlers = []
console_handler = logging.StreamHandler()
console_handler.setLevel(console_log_level)
console_handler.setFormatter(log_format)
handlers.append(console_handler)
if log_file is not None and log_file != "":
if os.path.exists(log_file):
os.remove(log_file)
elif not os.path.exists(os.path.dirname(log_file)):
os.makedirs(os.path.dirname(log_file))
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(log_file_level)
file_handler.setFormatter(log_format)
handlers.append(file_handler)
logging.basicConfig(level=root_log_level, handlers=handlers)
class StoreLoggingLevelAction(configargparse.Action):
"""This class converts string into logging level"""
LEVELS = {
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"WARNING": logging.WARNING,
"INFO": logging.INFO,
"DEBUG": logging.DEBUG,
"NOTSET": logging.NOTSET,
}
CHOICES = list(LEVELS.keys()) + [str(_) for _ in LEVELS.values()]
def __init__(self, option_strings, dest, help=None, **kwargs):
super().__init__(option_strings, dest, help=help, **kwargs)
def __call__(self, parser, namespace, value, option_string=None):
"""This function gets the key 'value' in the LEVELS, or just uses value"""
level = StoreLoggingLevelAction.LEVELS.get(value, value)
setattr(namespace, self.dest, level)
class CheckPathAction(configargparse.Action):
"""This class checks file path, if not exits, then create dir(file)"""
def __init__(self, option_strings, dest, help=None, **kwargs):
super().__init__(option_strings, dest, help=help, **kwargs)
def __call__(self, parser, namespace, value, option_string=None):
"""This function checks file path, if not exits, then create dir(file)"""
parent_path = os.path.dirname(value)
if not os.path.exists(parent_path):
os.makedirs(parent_path)
setattr(namespace, self.dest, value)
class ConfigurationParer:
"""This class defines customized configuration parser"""
def __init__(
self,
config_file_parser_class=configargparse.YAMLConfigFileParser,
formatter_class=configargparse.ArgumentDefaultsHelpFormatter,
**kwargs,
):
"""This funtion decides config parser and formatter
Keyword Arguments:
config_file_parser_class {configargparse.ConfigFileParser} -- config file parser (default: {configargparse.YAMLConfigFileParser})
formatter_class {configargparse.ArgumentDefaultsHelpFormatter} -- config formatter (default: {configargparse.ArgumentDefaultsHelpFormatter})
"""
self.parser = configargparse.ArgumentParser(
config_file_parser_class=config_file_parser_class,
formatter_class=formatter_class,
**kwargs,
)
def add_save_cfgs(self):
"""This function adds saving path arguments: config file, model file..."""
# config file configurations
group = self.parser.add_argument_group("Config-File")
group.add(
"-config_file",
"--config_file",
required=False,
is_config_file_arg=True,
help="config file path",
)
# model file configurations
group = self.parser.add_argument_group("Model-File")
group.add(
"-save_dir",
"--save_dir",
type=str,
required=True,
help="directory for saving checkpoints.",
)
def add_data_cfgs(self):
"""This function adds dataset arguments: data file path..."""
self.parser.add(
"-data_dir",
"--data_dir",
type=str,
required=True,
help="dataset directory.",
)
self.parser.add(
"-train_file",
"--train_file",
type=str,
required=False,
help="train data file.",
)
self.parser.add(
"-dev_file", "--dev_file", type=str, required=False, help="dev data file."
)
self.parser.add(
"-test_file",
"--test_file",
type=str,
required=False,
help="test data file.",
)
self.parser.add(
"-ent_rel_file",
"--ent_rel_file",
type=str,
required=False,
help="entity and relation file.",
)
self.parser.add(
"-max_sent_len",
"--max_sent_len",
type=int,
default=200,
help="max sentence length.",
)
self.parser.add(
"-max_wordpiece_len",
"--max_wordpiece_len",
type=int,
default=512,
help="max sentence length.",
)
self.parser.add("-test", "--test", action="store_true", help="testing mode")
def add_model_cfgs(self):
"""This function adds model (network) arguments: embedding, hidden unit..."""
# embedding configurations
group = self.parser.add_argument_group("Embedding")
group.add(
"--freeze_bert",
"--freeze_bert",
action="store_true",
help="freeze_bert",
)
group.add(
"-load_weight_path",
"--load_weight_path",
type=str,
default="",
help="load_weight_path",
)
group.add(
"-prune_topk",
"--prune_topk",
type=int,
default=0,
help="prune_topk",
)
group.add(
"-task",
"--task",
type=str,
choices=["quintuplet", "tagger", "triplet"],
default="quintuplet",
help="training task.",
)
group.add(
"-embedding_model",
"--embedding_model",
type=str,
choices=["bert", "pretrained"],
default="bert",
help="embedding model.",
)
group.add(
"-bert_model_name",
"--bert_model_name",
type=str,
required=False,
help="bert model name.",
)
group.add(
"-pretrained_model_name",
"--pretrained_model_name",
type=str,
required=False,
help="pretrained model name.",
)
group.add(
"-bert_output_size",
"--bert_output_size",
type=int,
default=768,
help="bert output size.",
)
group.add(
"-bert_dropout",
"--bert_dropout",
type=float,
default=0.1,
help="bert dropout rate.",
)
group.add(
"--fine_tune",
"--fine_tune",
action="store_true",
help="fine-tune pretrained model.",
)
# biaffine model
group = self.parser.add_argument_group("Biaffine")
group.add(
"-max_span_length",
"--max_span_length",
type=int,
default=10,
help="maximum span length.",
)
group.add(
"-mlp_hidden_size",
"--mlp_hidden_size",
type=int,
default=768,
help="mlp hidden units size.",
)
group.add(
"-dropout", "--dropout", type=float, default=0.5, help="dropout rate."
)
group.add(
"-separate_threshold",
"--separate_threshold",
type=float,
default=1.4,
help="the threshold for separating spans.",
)
group.add(
"-logit_dropout",
"--logit_dropout",
type=float,
default=0.1,
help="logit dropout rate for robustness.",
)
def add_optimizer_cfgs(self):
"""This function adds optimizer arguments"""
# gradient strategy
self.parser.add(
"-gradient_clipping",
"--gradient_clipping",
type=float,
default=1.0,
help="gradient clipping threshold.",
)
# learning rate
self.parser.add(
"--learning_rate",
"-learning_rate",
type=float,
default=3e-5,
help="Starting learning rate. "
"Recommended settings: sgd = 1, adagrad = 0.1, "
"adadelta = 1, adam = 0.001",
)
self.parser.add(
"--bert_learning_rate",
"-bert_learning_rate",
type=float,
default=3e-5,
help="learning rate for bert, should be smaller than followed parts.",
)
self.parser.add(
"-lr_decay_rate",
"--lr_decay_rate",
type=float,
default=0.9,
help="learn rate of layers decay rate.",
)
# Adam configurations
group = self.parser.add_argument_group("Adam")
group.add(
"-adam_beta1",
"--adam_beta1",
type=float,
default=0.9,
help="The beta1 parameter used by Adam. "
"Almost without exception a value of 0.9 is used in "
"the literature, seemingly giving good results, "
"so we would discourage changing this value from "
"the default without due consideration.",
)
group.add(
"-adam_beta2",
"--adam_beta2",
type=float,
default=0.999,
help="The beta2 parameter used by Adam. "
"Typically a value of 0.999 is recommended, as this is "
"the value suggested by the original paper describing "
"Adam, and is also the value adopted in other frameworks "
"such as Tensorflow and Kerras, i.e. see: "
"https://www.tensorflow.org/api_docs/python/tf/train/Adam"
"Optimizer or "
"https://keras.io/optimizers/ . "
'Whereas recently the paper "Attention is All You Need" '
"suggested a value of 0.98 for beta2, this parameter may "
"not work well for normal models / default "
"baselines.",
)
group.add(
"-adam_epsilon",
"--adam_epsilon",
type=float,
default=1e-6,
help="adam epsilon",
)
group.add(
"-adam_weight_decay_rate",
"--adam_weight_decay_rate",
type=float,
default=0.0,
help="adam weight decay rate.",
)
group.add(
"-adam_bert_weight_decay_rate",
"--adam_bert_weight_decay_rate",
type=float,
default=0.0,
help="adam weight decay rate of Bert module.",
)
def add_run_cfgs(self):
"""This function adds running arguments"""
# training configurations
group = self.parser.add_argument_group("Training")
group.add("-seed", "--seed", type=int, default=5216, help="radom seed.")
group.add(
"-epochs", "--epochs", type=int, default=1000, help="training epochs."
)
group.add(
"-pretrain_epochs",
"--pretrain_epochs",
type=int,
default=0,
help="pretrain epochs.",
)
group.add(
"-warmup_rate",
"--warmup_rate",
type=float,
default=0.0,
help="warmup rate.",
)
group.add(
"-early_stop",
"--early_stop",
type=int,
default=50,
help="early stop threshold.",
)
group.add(
"-train_batch_size",
"--train_batch_size",
type=int,
default=200,
help="batch size during training.",
)
group.add(
"-gradient_accumulation_steps",
"--gradient_accumulation_steps",
type=int,
default=1,
help="Number of updates steps to accumulate before performing a backward/update pass.",
)
# testing configurations
group = self.parser.add_argument_group("Testing")
group.add(
"-test_batch_size",
"--test_batch_size",
type=int,
default=100,
help="batch size during testing.",
)
group.add(
"-validate_every",
"--validate_every",
type=int,
default=20000,
help="output result every n samples during validating.",
)
# gpu configurations
group = self.parser.add_argument_group("GPU")
group.add(
"-device",
"--device",
type=int,
default=-1,
help="cpu: device = -1, gpu: gpu device id(device >= 0).",
)
# logging configurations
group = self.parser.add_argument_group("logging")
group.add(
"-root_log_level",
"--root_log_level",
type=str,
action=StoreLoggingLevelAction,
choices=StoreLoggingLevelAction.CHOICES,
default="DEBUG",
help="root logging out level.",
)
group.add(
"-console_log_level",
"--console_log_level",
type=str,
action=StoreLoggingLevelAction,
choices=StoreLoggingLevelAction.CHOICES,
default="NOTSET",
help="console logging output level.",
)
group.add(
"-log_file",
"--log_file",
type=str,
required=True,
help="logging file during running.",
)
group.add(
"-file_log_level",
"--file_log_level",
type=str,
action=StoreLoggingLevelAction,
choices=StoreLoggingLevelAction.CHOICES,
default="NOTSET",
help="file logging output level.",
)
group.add(
"-logging_steps",
"--logging_steps",
type=int,
default=10,
help="Logging every N update steps.",
)
def parse_args(self):
"""This function parses arguments and initializes logger
Returns:
dict -- config arguments
"""
cfg = self.parser.parse_args()
if not os.path.exists(cfg.save_dir):
os.makedirs(cfg.save_dir)
cfg.best_model_path = os.path.join(cfg.save_dir, "best_model")
cfg.last_model_path = os.path.join(cfg.save_dir, "last_model")
cfg.vocabulary_file = os.path.join(cfg.save_dir, "vocabulary.pickle")
cfg.model_checkpoints_dir = os.path.join(cfg.save_dir, "model_ckpts")
if not os.path.exists(cfg.model_checkpoints_dir):
os.makedirs(cfg.model_checkpoints_dir)
assert os.path.exists(
cfg.data_dir
), f"dataset directory {cfg.data_dir} not exists !!!"
for file in ["train_file", "dev_file", "test_file", "ent_rel_file"]:
if getattr(cfg, file, None) is not None:
setattr(cfg, file, os.path.join(cfg.data_dir, getattr(cfg, file, "")))
if getattr(cfg, "log_file", None) is not None:
cfg.log_file = os.path.join(cfg.save_dir, cfg.log_file)
# assert not os.path.exists(cfg.log_file), f"log file {cfg.log_file} exists !!!"
if os.path.exists(cfg.log_file):
cfg.log_file = None
cfg.log_file_level = logging.NOTSET
init_logger(
root_log_level=getattr(cfg, "root_log_level", logging.DEBUG),
console_log_level=getattr(cfg, "console_log_level", logging.NOTSET),
log_file=getattr(cfg, "log_file", None),
log_file_level=getattr(cfg, "log_file_level", logging.NOTSET),
)
if cfg.freeze_bert:
cfg.fine_tune = False
return cfg
def format_values(self):
return self.parser.format_values()