-
Notifications
You must be signed in to change notification settings - Fork 1
/
nest_ccg_main.py
713 lines (605 loc) · 31.8 KB
/
nest_ccg_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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
from __future__ import absolute_import, division, print_function
import argparse
import json
import logging
import os
import random
from os import path
import numpy as np
import torch
import torch.nn.functional as F
from pytorch_pretrained_bert.optimization import BertAdam, WarmupLinearSchedule
from tqdm import tqdm, trange
import subprocess
from nest_ccg_helper import get_vocab, get_labels
from nest_ccg_eval import Evaluation, ccgparse, candc_path
from nest_ccg_model import NeSTCCG
import datetime
def train(args):
if args.use_bert and args.use_xlnet:
raise ValueError('We cannot use both BERT and XLNet')
now_time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
log_file_name = './logs/log-' + now_time
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
filename=log_file_name,
filemode='w',
level=logging.INFO)
logger = logging.getLogger(__name__)
console_handler = logging.StreamHandler()
logger.addHandler(console_handler)
logger = logging.getLogger(__name__)
logger.info(vars(args))
if args.server_ip and args.server_port:
# Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script
import ptvsd
print("Waiting for debugger attach")
ptvsd.enable_attach(address=(args.server_ip, args.server_port), redirect_output=True)
ptvsd.wait_for_attach()
if args.local_rank == -1 or args.no_cuda:
device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
n_gpu = torch.cuda.device_count()
else:
torch.cuda.set_device(args.local_rank)
device = torch.device("cuda", args.local_rank)
n_gpu = 1
# Initializes the distributed backend which will take care of sychronizing nodes/GPUs
torch.distributed.init_process_group(backend='nccl')
logger.info("device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}".format(
device, n_gpu, bool(args.local_rank != -1), args.fp16))
if args.gradient_accumulation_steps < 1:
raise ValueError("Invalid gradient_accumulation_steps parameter: {}, should be >= 1".format(
args.gradient_accumulation_steps))
args.train_batch_size = args.train_batch_size // args.gradient_accumulation_steps
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if not os.path.exists('./models'):
os.mkdir('./models')
if args.model_name is None:
raise Warning('model name is not specified, the model will NOT be saved!')
output_model_dir = os.path.join('./models', args.model_name + '_' + now_time)
ngram2id, ngram2count = get_vocab(args.train_data_path,
args.max_ngram_length, args.ngram_freq_threshold)
label_map = get_labels(args.tag_path)
logger.info('# of supertags: %d' % (len(label_map) - 4))
hpara = NeSTCCG.init_hyper_parameters(args)
supertagger = NeSTCCG(labelmap=label_map, hpara=hpara, model_path=args.bert_model,
gram2id=ngram2id)
train_examples = supertagger.load_data(args.train_data_path, flag='train')
dev_examples = supertagger.load_data(args.dev_data_path, flag='dev')
test_examples = supertagger.load_data(args.test_data_path, flag='test')
all_eval_examples = {'dev': dev_examples, 'test': test_examples}
num_labels = supertagger.num_labels
convert_examples_to_features = supertagger.convert_examples_to_features
clipping_top_n = supertagger.clipping_top_n
clipping_threshold = supertagger.clipping_threshold
id2label = supertagger.id2label
feature2input = supertagger.feature2input
total_params = sum(p.numel() for p in supertagger.parameters() if p.requires_grad)
logger.info('# of trainable parameters: %d' % total_params)
num_train_optimization_steps = int(
len(train_examples) / args.train_batch_size / args.gradient_accumulation_steps) * args.num_train_epochs
if args.local_rank != -1:
num_train_optimization_steps = num_train_optimization_steps // torch.distributed.get_world_size()
if args.fp16:
supertagger.half()
supertagger.to(device)
if args.local_rank != -1:
try:
from apex.parallel import DistributedDataParallel as DDP
except ImportError:
raise ImportError(
"Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training.")
supertagger = DDP(supertagger)
elif n_gpu > 1:
supertagger = torch.nn.DataParallel(supertagger)
param_optimizer = list(supertagger.named_parameters())
no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
if args.fp16:
try:
from apex.optimizers import FP16_Optimizer
from apex.optimizers import FusedAdam
except ImportError:
raise ImportError(
"Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training.")
optimizer = FusedAdam(optimizer_grouped_parameters,
lr=args.learning_rate,
bias_correction=False,
max_grad_norm=1.0)
if args.loss_scale == 0:
optimizer = FP16_Optimizer(optimizer, dynamic_loss_scale=True)
else:
optimizer = FP16_Optimizer(optimizer, static_loss_scale=args.loss_scale)
warmup_linear = WarmupLinearSchedule(warmup=args.warmup_proportion,
t_total=num_train_optimization_steps)
else:
# num_train_optimization_steps=-1
optimizer = BertAdam(optimizer_grouped_parameters,
lr=args.learning_rate,
warmup=args.warmup_proportion,
t_total=num_train_optimization_steps)
global_step = 0
best_eval = -1
best_info_str = ''
history = {'epoch': [], 'dev_acc': [], 'dev_cats': [], 'dev_lf': [], 'dev_uf': [],
'test_acc': [], 'test_cats': [], 'test_lf': [], 'test_uf': []}
num_of_no_improvement = 0
patient = args.patient
evaluator = Evaluation(args.eval_data_dir)
if args.do_train:
logger.info("***** Running training *****")
logger.info(" Num examples = %d", len(train_examples))
logger.info(" Batch size = %d", args.train_batch_size)
logger.info(" Num steps = %d", num_train_optimization_steps)
for epoch in trange(int(args.num_train_epochs), desc="Epoch"):
np.random.shuffle(train_examples)
supertagger.train()
tr_loss = 0
nb_tr_examples, nb_tr_steps = 0, 0
for step, start_index in enumerate(tqdm(range(0, len(train_examples), args.train_batch_size))):
supertagger.train()
batch_examples = train_examples[start_index: min(start_index +
args.train_batch_size, len(train_examples))]
train_features = convert_examples_to_features(batch_examples)
input_ids, input_mask, l_mask, label_ids, segment_ids, valid_ids, \
dep_adjacency_matrix = feature2input(device, train_features)
loss = supertagger(input_ids, segment_ids, input_mask, label_ids, valid_ids, l_mask,
adjacency_matrix=dep_adjacency_matrix)
if n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu.
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
if args.fp16:
optimizer.backward(loss)
else:
loss.backward()
tr_loss += loss.item()
nb_tr_examples += input_ids.size(0)
nb_tr_steps += 1
if (step + 1) % args.gradient_accumulation_steps == 0:
if args.fp16:
# modify learning rate with special warm up BERT uses
# if args.fp16 is False, BertAdam is used that handles this automatically
lr_this_step = args.learning_rate * warmup_linear(global_step / num_train_optimization_steps,
args.warmup_proportion)
for param_group in optimizer.param_groups:
param_group['lr'] = lr_this_step
optimizer.step()
optimizer.zero_grad()
global_step += 1
supertagger.to(device)
if args.local_rank == -1 or torch.distributed.get_rank() == 0:
supertagger.eval()
output_model_dir = path.join('./models', args.model_name + '_' + now_time)
if not os.path.exists(output_model_dir):
os.mkdir(output_model_dir)
history['epoch'].append(epoch)
for flag in ['dev', 'test']:
eval_examples = all_eval_examples[flag]
all_y_true = []
all_y_pred = []
output_suppertag_list = []
for start_index in tqdm(range(0, len(eval_examples), args.eval_batch_size)):
eval_batch_examples = eval_examples[start_index:
min(start_index + args.eval_batch_size, len(eval_examples))]
eval_features = convert_examples_to_features(eval_batch_examples)
input_ids, input_mask, l_mask, label_ids, segment_ids, valid_ids, \
dep_adjacency_matrix = feature2input(device, eval_features)
with torch.no_grad():
logits = supertagger(input_ids, segment_ids, input_mask, None, valid_ids, l_mask,
adjacency_matrix=dep_adjacency_matrix
)
logits = F.softmax(logits, dim=2)
argmax_logits = torch.argmax(logits, dim=2)
argsort_loagits = torch.argsort(logits, dim=2, descending=True)
argmax_logits = argmax_logits.detach().cpu().numpy()
argsort_loagits = argsort_loagits.detach().cpu().numpy()[:, :, : clipping_top_n]
logits = logits.to('cpu').numpy()
label_ids = label_ids.to('cpu').numpy()
l_mask = l_mask.to('cpu').numpy()
for i, ex in enumerate(eval_batch_examples):
true_label_list = ex.label
temp = []
j_index = 1
for _ in range(len(true_label_list)):
temp.append(id2label[argmax_logits[i][j_index]])
assert l_mask[i][j_index] == 1
j_index += 1
assert l_mask[i][j_index] == 1
assert j_index+1 == len(l_mask[i]) or l_mask[i][j_index+1] == 0
all_y_true.append(true_label_list)
all_y_pred.append(temp)
for i in range(len(label_ids)):
ex = eval_batch_examples[i]
label = label_ids[i]
text = ex.text_a.split(' ')
output_line = []
for j in range(len(label)):
if j == 0:
continue
elif label_ids[i][j] == num_labels - 1:
assert len(text) == j - 1
output_suppertag_list.append('#word#'.join(output_line))
break
else:
super_tag_str_list = []
prob_str_list = []
for tag_id in argsort_loagits[i][j]:
if tag_id == 0:
continue
tag = id2label[tag_id]
prob = logits[i][j][tag_id]
if len(super_tag_str_list) > 0 and prob < clipping_threshold:
break
else:
super_tag_str_list.append(tag)
prob_str_list.append(str(prob))
word_str = text[j - 1] + '\t' + '#'.join(
super_tag_str_list) + '\t' + '#'.join(prob_str_list)
output_line.append(word_str)
y_true_all = []
y_pred_all = []
eval_sentence_all = []
for y_true_item in all_y_true:
y_true_all += y_true_item
for y_pred_item in all_y_pred:
y_pred_all += y_pred_item
for example, y_true_item in zip(eval_examples, all_y_true):
sen = example.text_a
sen = sen.strip()
sen = sen.split(' ')
if len(y_true_item) != len(sen):
# print(len(sen))
sen = sen[:len(y_true_item)]
eval_sentence_all.append(sen)
acc = evaluator.supertag_acc(y_pred_all, y_true_all)
history[flag + '_acc'].append(acc)
auto_output_file = os.path.join(output_model_dir, flag + '.auto')
supertag_output_file = os.path.join(output_model_dir, flag + '.supertag.txt')
with open(supertag_output_file, 'w', encoding='utf8') as f:
for line in output_suppertag_list:
f.write(line + '\n')
command = 'java -jar ' + ccgparse + ' -f ' + supertag_output_file + ' -o ' + auto_output_file + ' >' + auto_output_file
subprocess.run(command, shell=True)
dep_output_file = os.path.join(output_model_dir, flag + '.dep')
command = './auto2dep.sh ' + candc_path + ' ' + auto_output_file + ' ' + dep_output_file
subprocess.run(command, shell=True)
eval_output_file = os.path.join(output_model_dir, flag + '.eval')
tag_gold = os.path.join(args.eval_data_dir, 'gold_files', flag + '.stagged')
dep_gold = os.path.join(args.eval_data_dir, 'gold_files', flag + '.dep.gold')
command = 'python ccg_eval.py -r ' + tag_gold + ' ' + dep_gold + ' ' \
+ dep_output_file + ' ' + auto_output_file + ' >' + eval_output_file
subprocess.run(command, shell=True)
results = evaluator.eval_file_reader(eval_output_file)
for key, value in results.items():
h_key = flag + '_' + key
if h_key in history:
history[h_key].append(value)
log_info = []
for key, value in history.items():
log_info.append(key)
log_info.append(str(value[-1]))
info_str = ' '.join(log_info)
logger.info(info_str)
if history['dev_acc'][-1] > best_eval:
best_eval = history['dev_acc'][-1]
best_info_str = info_str
num_of_no_improvement = 0
model_to_save = supertagger.module if hasattr(supertagger, 'module') else supertagger
best_eval_model_dir = os.path.join(output_model_dir, 'model')
if not os.path.exists(best_eval_model_dir):
os.mkdir(best_eval_model_dir)
model_to_save.save_model(best_eval_model_dir, args.bert_model)
else:
num_of_no_improvement += 1
if num_of_no_improvement >= patient:
logger.info('\nEarly stop triggered at epoch %d\n' % epoch)
break
logger.info("\n======= best ========\n")
logger.info(best_info_str)
logger.info("\n======= best ========\n")
with open(os.path.join(output_model_dir, 'history.json'), 'w', encoding='utf8') as f:
json.dump(history, f)
f.write('\n')
def test(args):
evaluator = Evaluation(args.eval_data_dir)
if args.local_rank == -1 or args.no_cuda:
device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
n_gpu = torch.cuda.device_count()
else:
torch.cuda.set_device(args.local_rank)
device = torch.device("cuda", args.local_rank)
n_gpu = 1
# Initializes the distributed backend which will take care of sychronizing nodes/GPUs
torch.distributed.init_process_group(backend='nccl')
print("device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}".format(
device, n_gpu, bool(args.local_rank != -1), args.fp16))
supertagger = NeSTCCG.load_model(args.eval_model)
eval_examples = supertagger.load_data(args.eval_data_path, flag='test')
num_labels = supertagger.num_labels
convert_examples_to_features = supertagger.convert_examples_to_features
clipping_threshold = supertagger.clipping_threshold
clipping_top_n = supertagger.clipping_top_n
id2label = supertagger.id2label
feature2input = supertagger.feature2input
if args.fp16:
supertagger.half()
supertagger.to(device)
if args.local_rank != -1:
try:
from apex.parallel import DistributedDataParallel as DDP
except ImportError:
raise ImportError(
"Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training.")
supertagger = DDP(supertagger)
elif n_gpu > 1:
supertagger = torch.nn.DataParallel(supertagger)
supertagger.to(device)
supertagger.eval()
all_y_true = []
all_y_pred = []
output_suppertag_list = []
for start_index in tqdm(range(0, len(eval_examples), args.eval_batch_size)):
eval_batch_examples = eval_examples[start_index: min(start_index + args.eval_batch_size,
len(eval_examples))]
eval_features = convert_examples_to_features(eval_batch_examples)
input_ids, input_mask, l_mask, label_ids, segment_ids, valid_ids, \
dep_adjacency_matrix = feature2input(device, eval_features)
with torch.no_grad():
logits = supertagger(input_ids, segment_ids, input_mask, None, valid_ids, l_mask,
adjacency_matrix=dep_adjacency_matrix
)
logits = F.softmax(logits, dim=2)
argmax_logits = torch.argmax(logits, dim=2)
argsort_loagits = torch.argsort(logits, dim=2, descending=True)
argmax_logits = argmax_logits.detach().cpu().numpy()
argsort_loagits = argsort_loagits.detach().cpu().numpy()[:, :, : clipping_top_n]
logits = logits.to('cpu').numpy()
label_ids = label_ids.to('cpu').numpy()
l_mask = l_mask.to('cpu').numpy()
for i, ex in enumerate(eval_batch_examples):
true_label_list = ex.label
temp = []
for j in range(len(true_label_list)):
temp.append(id2label[argmax_logits[i][j + 1]])
assert l_mask[i][j + 1] == 1
all_y_true.append(true_label_list)
all_y_pred.append(temp)
for i in range(len(label_ids)):
ex = eval_batch_examples[i]
label = label_ids[i]
text = ex.text_a.split(' ')
output_line = []
for j in range(len(label)):
if j == 0:
continue
elif label_ids[i][j] == num_labels - 1:
assert len(text) == j - 1
output_suppertag_list.append('#word#'.join(output_line))
break
else:
super_tag_str_list = []
prob_str_list = []
for tag_id in argsort_loagits[i][j]:
if tag_id == 0:
continue
tag = id2label[tag_id]
prob = logits[i][j][tag_id]
if len(super_tag_str_list) > 0 and prob < clipping_threshold:
break
else:
super_tag_str_list.append(tag)
prob_str_list.append(str(prob))
word_str = text[j - 1] + '\t' + '#'.join(
super_tag_str_list) + '\t' + '#'.join(prob_str_list)
output_line.append(word_str)
y_true_all = []
y_pred_all = []
eval_sentence_all = []
for y_true_item in all_y_true:
y_true_all += y_true_item
for y_pred_item in all_y_pred:
y_pred_all += y_pred_item
acc = evaluator.supertag_acc(y_pred_all, y_true_all)
for example, y_true_item in zip(eval_examples, all_y_true):
sen = example.text_a
sen = sen.strip()
sen = sen.split(' ')
if len(y_true_item) != len(sen):
# print(len(sen))
sen = sen[:len(y_true_item)]
eval_sentence_all.append(sen)
if not os.path.exists('./tmp'):
os.mkdir('./tmp')
correct_results_file = os.path.join('./tmp', 'test.correct.result.txt')
with open(correct_results_file, 'w', encoding='utf8') as f:
for index, (sen, y_true, y_pred) in enumerate(zip(eval_sentence_all, all_y_true, all_y_pred)):
correct = True
for y_t, y_p in zip(y_true, y_pred):
if not y_t == y_p:
correct = False
break
if correct and len(sen) < 20:
f.write('ID=%d\n' % (index + 1))
f.write(' '.join(sen) + '\n')
for w, y_t in zip(sen, y_true):
f.write('%s\t%s\n' % (w, y_t))
f.write('\n')
auto_output_file = os.path.join('./tmp', 'test.auto')
supertag_output_file = os.path.join('./tmp', 'test.supertag.txt')
with open(supertag_output_file, 'w', encoding='utf8') as f:
for line in output_suppertag_list:
f.write(line + '\n')
command = 'java -jar ' + ccgparse + ' -f ' + supertag_output_file + ' -o ' + auto_output_file + ' >' + auto_output_file
print(command)
subprocess.run(command, shell=True)
dep_output_file = os.path.join('./tmp', 'test.dep')
command = './auto2dep.sh ' + candc_path + ' ' + auto_output_file + ' ' + dep_output_file
print(command)
subprocess.run(command, shell=True)
eval_output_file = os.path.join('./tmp', 'test.eval')
if args.eval_data_path.find('dev') > -1:
tag_gold = os.path.join(args.eval_data_dir, 'gold_files', 'dev.stagged')
dep_gold = os.path.join(args.eval_data_dir, 'gold_files', 'dev.dep.gold')
else:
tag_gold = os.path.join(args.eval_data_dir, 'gold_files', 'test.stagged')
dep_gold = os.path.join(args.eval_data_dir, 'gold_files', 'test.dep.gold')
command = 'python ccg_eval.py -r ' + tag_gold + ' ' + dep_gold + ' ' \
+ dep_output_file + ' ' + auto_output_file + ' >' + eval_output_file
print(command)
subprocess.run(command, shell=True)
results = evaluator.eval_file_reader(eval_output_file)
for key, value in results.items():
h_key = 'test_' + key
if h_key in results:
results[h_key] = value
results['acc'] = acc
log_info = []
for key, value in results.items():
log_info.append(key)
log_info.append(str(value))
info_str = ' '.join(log_info)
print(info_str)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--do_train",
action='store_true',
help="Whether to run training.")
parser.add_argument("--do_test",
action='store_true',
help="Whether to run testing.")
parser.add_argument("--do_predict",
action='store_true',
help="Whether to run predicting.")
parser.add_argument("--train_data_path",
default=None,
type=str,
help="The training data path. Should contain the .tsv files for the task.")
parser.add_argument("--dev_data_path",
default=None,
type=str,
help="The eval/testing data path. Should contain the .tsv files for the task.")
parser.add_argument("--test_data_path",
default=None,
type=str,
help="The eval/testing data path. Should contain the .tsv files for the task.")
parser.add_argument("--tag_path",
default=None,
type=str)
parser.add_argument("--eval_data_path",
default=None,
type=str,
help="The eval/testing data path. Should contain the .tsv files for the task.")
parser.add_argument("--eval_data_dir",
default=None,
type=str,
help="The eval/testing data path. Should contain the .tsv files for the task.")
parser.add_argument("--input_file",
default=None,
type=str,
help="The data path containing the sentences to be segmented")
parser.add_argument("--output_file",
default=None,
type=str,
help="The output path of segmented file")
parser.add_argument("--use_bert",
action='store_true',
help="Whether to use BERT.")
parser.add_argument("--use_xlnet",
action='store_true',
help="Whether to use XLNet.")
parser.add_argument("--bert_model", default=None, type=str,
help="Bert pre-trained model selected in the list: bert-base-uncased, "
"bert-large-uncased, bert-base-cased, bert-large-cased, bert-base-multilingual-uncased, "
"bert-base-multilingual-cased, bert-base-chinese.")
parser.add_argument("--eval_model", default=None, type=str,
help="")
parser.add_argument("--cache_dir",
default="",
type=str,
help="Where do you want to store the pre-trained models downloaded from s3")
parser.add_argument("--max_seq_length",
default=128,
type=int,
help="The maximum total input sequence length after WordPiece tokenization. \n"
"Sequences longer than this will be truncated, and sequences shorter \n"
"than this will be padded.")
parser.add_argument("--do_lower_case",
action='store_true',
help="Set this flag if you are using an uncased model.")
parser.add_argument("--train_batch_size",
default=16,
type=int,
help="Total batch size for training.")
parser.add_argument("--eval_batch_size",
default=16,
type=int,
help="Total batch size for eval.")
parser.add_argument("--learning_rate",
default=5e-5,
type=float,
help="The initial learning rate for Adam.")
parser.add_argument("--num_train_epochs",
default=3.0,
type=float,
help="Total number of training epochs to perform.")
parser.add_argument("--warmup_proportion",
default=0.1,
type=float,
help="Proportion of training to perform linear learning rate warmup for. "
"E.g., 0.1 = 10%% of training.")
parser.add_argument("--no_cuda",
action='store_true',
help="Whether not to use CUDA when available")
parser.add_argument("--local_rank",
type=int,
default=-1,
help="local_rank for distributed training on gpus")
parser.add_argument('--seed',
type=int,
default=42,
help="random seed for initialization")
parser.add_argument('--gradient_accumulation_steps',
type=int,
default=1,
help="Number of updates steps to accumulate before performing a backward/update pass.")
parser.add_argument('--fp16',
action='store_true',
help="Whether to use 16-bit float precision instead of 32-bit")
parser.add_argument('--loss_scale',
type=float, default=0,
help="Loss scaling to improve fp16 numeric stability. Only used when fp16 set to True.\n"
"0 (default value): dynamic loss scaling.\n"
"Positive power of 2: static loss scaling value.\n")
parser.add_argument('--server_ip', type=str, default='', help="Can be used for distant debugging.")
parser.add_argument('--server_port', type=str, default='', help="Can be used for distant debugging.")
parser.add_argument('--patient', type=int, default=3, help="Patient for the early stop.")
parser.add_argument('--ngram_freq_threshold', type=int, default=0, help="The threshold of n-gram frequency")
parser.add_argument('--max_ngram_length', type=int, default=5,
help="The maximum length of n-grams to be considered.")
parser.add_argument('--model_name', type=str, default=None, help="")
parser.add_argument("--use_weight", action='store_true', help="")
parser.add_argument("--use_gcn", action='store_true', help="")
parser.add_argument("--use_in_chunk", action='store_true', help="")
parser.add_argument("--use_cross_chunk", action='store_true', help="")
parser.add_argument('--gcn_layer_number', type=int, default=2,
help="The maximum length of n-grams to be considered.")
parser.add_argument('--clipping_top_n', type=int, default=5, help="Can be used for distant debugging.")
parser.add_argument('--clipping_threshold', type=float, default=0.0005, help="Can be used for distant debugging.")
args = parser.parse_args()
if args.do_train:
train(args)
elif args.do_test:
test(args)
elif args.do_predict:
raise ValueError()
# predict(args)
else:
raise ValueError('At least one of `do_train`, `do_eval`, `do_predict` must be True.')
if __name__ == "__main__":
main()