-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun_multi.py
611 lines (552 loc) · 31.1 KB
/
run_multi.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
from __future__ import absolute_import, division, print_function
import argparse
import logging
import os
import random
import math
import copy
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
from multiTask.MultiTaskModel import BertForMultiTask
from multiTask.MultiTaskDataset import InputExample, SighanProcessor, EcspellProcessor, TnewsProcessor, AfqmcProcessor
from multiTask.MultiTaskDataset import csc_convert_examples_to_features, seq_convert_examples_to_features
from transformers import SchedulerType
from transformers import AutoTokenizer
from utils.metrics import Metrics
from tqdm.auto import tqdm
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO)
logger = logging.getLogger(__name__)
def mask_tokens(inputs, targets, task_ids, tokenizer, device, mask_mode="noerror", noise_probability=0.2):
## mask_mode in ["all","error","noerror"]
inputs = inputs.clone()
probability_matrix = torch.full(inputs.shape, noise_probability).to(device)
inputs_shape = inputs.size()
csc_task_matrix = torch.ones(inputs_shape).to(device)
task_ids_expand=task_ids.unsqueeze(dim=-1).expand(inputs_shape)
probability_matrix.masked_fill_(task_ids_expand!=csc_task_matrix, value=0.0)
special_tokens_mask = [
tokenizer.get_special_tokens_mask(val, already_has_special_tokens=True) for val in inputs.tolist()
]
special_tokens_mask = torch.tensor(special_tokens_mask, dtype=torch.bool).to(device)
probability_matrix.masked_fill_(special_tokens_mask, value=0.0)
if mask_mode == "noerror":
probability_matrix.masked_fill_(inputs!=targets, value=0.0)
elif mask_mode == "error":
probability_matrix.masked_fill_(inputs==targets, value=0.0)
else:
assert mask_mode == "all"
masked_indices = torch.bernoulli(probability_matrix).bool()
inputs[masked_indices] = tokenizer.convert_tokens_to_ids(tokenizer.mask_token)
return inputs
def main():
parser = argparse.ArgumentParser()
# Data config.
parser.add_argument("--data_dir", type=str, default="data/",
help="Directory to contain the input data for all tasks.")
## mulitple tasks splited by " "
parser.add_argument("--task_name", type=str, default="SIGHAN tnews afqmc",
help="Name of the training task.")
parser.add_argument("--load_model_path", type=str, default="bert-base-chinese",
help="Pre-trained model path to load if needed.")
parser.add_argument("--cache_dir", type=str, default="../../cache/",
help="Directory to store the pre-trained language models downloaded from s3.")
parser.add_argument("--output_dir", type=str, default="model/",
help="Directory to output predictions and checkpoints.")
parser.add_argument("--load_checkpoint", type=str, default="",
help="Trained model weights to load for evaluation.")
# Training config.
parser.add_argument("--do_train", action="store_true",
help="Whether to run training.")
parser.add_argument("--do_eval", action="store_true",
help="Whether to evaluate on the dev set.")
parser.add_argument("--do_test", action="store_true",
help="Whether to evaluate on the test set.")
## multiple datasets splited by " "
parser.add_argument("--train_on", type=str, default="hybrid base base",
help="Choose a training set.")
## eval and test on only one task
parser.add_argument("--eval_on", type=str, default="15",
help="Choose a dev set.")
parser.add_argument("--test_on", type=str, default="15",
help="Choose a test set.")
parser.add_argument("--use_slow_tokenizer", action="store_true",
help="A slow tokenizer will be used if passed.")
parser.add_argument("--do_lower_case", action="store_true",
help="Set this flag if you are using an uncased model.")
parser.add_argument("--max_seq_length", type=int, default=64,
help="Maximum total input sequence length after word-piece tokenization.")
parser.add_argument("--train_batch_size", type=int, default=128,
help="Total batch size for training.")
parser.add_argument("--eval_batch_size", type=int, default=512,
help="Total batch size for evaluation.")
parser.add_argument("--learning_rate", type=float, default=5e-5,
help="Initial learning rate for Adam.")
parser.add_argument("--num_train_epochs", type=float, default=3.0,
help="Total number of training epochs to perform.")
parser.add_argument("--max_train_steps", type=int, default=None,
help="Total number of training steps to perform. If provided, overrides training epochs.")
parser.add_argument("--lr_scheduler_type", type=SchedulerType, default="linear",
help="Scheduler type for learning rate warmup.")
parser.add_argument("--warmup_proportion", type=float, default=0.1,
help="Proportion of training to perform learning rate warmup for.")
parser.add_argument("--weight_decay", type=float, default=0.,
help="L2 weight decay for training.")
parser.add_argument("--gradient_accumulation_steps", type=int, default=1,
help="Number of updates steps to accumulate before performing a backward pass.")
parser.add_argument("--no_cuda", action="store_true",
help="Whether not to use CUDA when available.")
parser.add_argument("--fp16", action="store_true",
help="Whether to use mixed precision.")
parser.add_argument("--seed", type=int, default=42,
help="Random seed for initialization.")
parser.add_argument("--save_steps", type=int, default=100,
help="How many steps to save the checkpoint once.")
parser.add_argument("--mft", action="store_true",
help="Training with masked-fine-tuning (not published yet).")
parser.add_argument("--mask_mode", type=str, default="noerror", help="noerror,error or all")
parser.add_argument("--mask_rate", type=float, default=0.2, help="the percentage we mask the source sentence in mask-ft technique")
parser.add_argument("--print_para_names", action="store_true", help="only print the parameters' names and do not train" )
parser.add_argument("--freeze_lm", action="store_true",
help="Whether to keep LM parameters frozen.")
args = parser.parse_args()
processors_all = {
"sighan": SighanProcessor,
"ecspell": EcspellProcessor,
"sghspell": SighanProcessor,## the data format in sghspell is the same as sighan
"tnews": TnewsProcessor,
"afqmc": AfqmcProcessor,
}
task_class={"csc":["sighan","ecspell","sghspell"],
"seq":["tnews","afqmc"]}
device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
n_gpu = torch.cuda.device_count()
logger.info("device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}".format(
device, n_gpu, "Unsupported", args.fp16))
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 n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
if args.do_train:
torch.save(args, os.path.join(args.output_dir, "train_args.bin"))
task_names = args.task_name.lower().split()
train_on_list = args.train_on.lower().split()
for task_name in task_names:
if task_name not in processors_all:
raise ValueError("Task not found: %s" % task_name)
# processors is a map containing all the processors we will use
processors={}
train_on_dataset={}
for task_name in task_names:
processors[task_name]=processors_all[task_name]()
for train_on,task_name in zip(train_on_list,task_names):
train_on_dataset[task_name]=train_on
cache_dir = args.cache_dir
tokenizer = AutoTokenizer.from_pretrained(args.load_model_path,
do_lower_case=args.do_lower_case,
cache_dir=cache_dir,
use_fast=not args.use_slow_tokenizer,
add_prefix_space=True)
if args.do_train:
train_examples = []
train_features = []
for task_name,processor in processors.items():
train_examples_=processor.get_train_examples(os.path.join(args.data_dir, task_name), train_on_dataset[task_name])
train_examples+=train_examples_
if task_name in task_class["csc"]:
train_features += csc_convert_examples_to_features(train_examples_, args.max_seq_length, tokenizer) ## do not apply static mask
else:
assert(task_name in task_class["seq"])
label_list = InputExample.get_label_list(train_examples_)
print(label_list)
train_features += seq_convert_examples_to_features(train_examples_, label_list, args.max_seq_length, tokenizer)
all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long)
## attention_mask
all_input_mask = torch.tensor([f.input_mask for f in train_features], dtype=torch.long)
## token_type_ids
all_input_segment = torch.tensor([f.segment_ids for f in train_features], dtype=torch.long)
all_label_ids = torch.tensor([f.label_ids for f in train_features], dtype=torch.long)##(batch,seq)
all_task_ids = torch.tensor([f.task_id for f in train_features], dtype=torch.long)
train_data = TensorDataset(all_input_ids, all_input_mask, all_input_segment, all_label_ids, all_task_ids)
## we have to disrupt the order the features from different tasks
train_sampler = RandomSampler(train_data)
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_batch_size)
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps)
if args.max_train_steps is None:
args.max_train_steps = int(args.num_train_epochs * num_update_steps_per_epoch)
else:
args.num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch)
model = BertForMultiTask.from_pretrained(args.load_model_path,
return_dict=True,
cache_dir=cache_dir)
model.to(device)
if args.load_checkpoint:
model.load_state_dict(torch.load(args.load_checkpoint))
if n_gpu > 1:
model = torch.nn.DataParallel(model)
no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": args.weight_decay
},
{
"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
"weight_decay": 0.0
}
]
if args.print_para_names:
classifier_params = ["qmc_","tnews_"]
for n,p in model.named_parameters():
if not any(nd in n for nd in classifier_params):##why not nd==n
p.requires_grad = False
print(n,'\n', p.requires_grad)
return
#######################################################################
if args.freeze_lm:##freeze the parameters in the lm except prompt parameters
classifier_params = ["qmc_","tnews_"]
for n,p in model.named_parameters():
if not any(nd in n for nd in classifier_params):##why not nd==n
p.requires_grad = False
logger.info("Freeze `{}`".format(n))
optimizer = torch.optim.AdamW(optimizer_grouped_parameters, lr=args.learning_rate)
'''
scheduler = get_scheduler(name=args.lr_scheduler_type,
optimizer=optimizer,
num_warmup_steps=args.max_train_steps * args.warmup_proportion,
num_training_steps=args.max_train_steps)
'''
scaler = None
if args.fp16:
from torch.cuda.amp import autocast, GradScaler
scaler = GradScaler()
if args.do_eval:
task_name = task_names[0] ## we choose the first task to evaluate
processor = processors[task_name]
eval_examples=processor.get_test_examples(os.path.join(args.data_dir, task_name), args.eval_on)
if task_name in task_class["csc"]:
eval_features = csc_convert_examples_to_features(eval_examples, args.max_seq_length, tokenizer) ## no mft in test
else:
assert(task_name in task_class["seq"])
label_list = InputExample.get_label_list(eval_examples)
eval_features = seq_convert_examples_to_features(eval_examples, label_list, args.max_seq_length, tokenizer)
all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
## attention_mask
all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
## token_type_ids
all_input_segment = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
all_label_ids = torch.tensor([f.label_ids for f in eval_features], dtype=torch.long)
all_task_ids = torch.tensor([f.task_id for f in eval_features], dtype=torch.long)
eval_data = TensorDataset(all_input_ids, all_input_mask, all_input_segment, all_label_ids, all_task_ids)
## we have to disrupt the order the features from different tasks
eval_sampler = RandomSampler(eval_data)
eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)
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", args.max_train_steps)
global_step = 0
best_result = list()
wrap = False
progress_bar = tqdm(range(args.max_train_steps))
for _ in range(int(args.num_train_epochs)):
train_loss = 0
num_train_examples = 0
if wrap: break
for step, batch in enumerate(train_dataloader):
model.train()
batch = tuple(t.to(device) for t in batch)
input_ids, attention_mask, token_type_ids, label_ids, task_id = batch
'''
print("size of input_ids:{}".format(input_ids.size()))
print("size of task_id:{}".format(task_id.size()))
print("size of label_ids:{}".format(label_ids.size()))
'''
if args.mft:
input_ids = mask_tokens(input_ids, label_ids, task_id, tokenizer, device, mask_mode=args.mask_mode, noise_probability=args.mask_rate)
if args.fp16:
with autocast():
outputs = model(input_ids=input_ids, ## (batch,seq)
attention_mask=attention_mask, ## (batch,seq)
token_type_ids=token_type_ids, ## (batch,seq)
task_id = task_id, ## batch
labels=label_ids) ## (batch,seq) or batch
else:
outputs = model(input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
task_id = task_id,
labels=label_ids)
loss = outputs[0]
if n_gpu > 1:
loss = loss.mean()
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
if args.fp16:
scaler.scale(loss).backward()
else:
loss.backward()
train_loss += loss.item()
num_train_examples += input_ids.size(0)
if (step + 1) % args.gradient_accumulation_steps == 0 or step == len(train_dataloader) - 1:
if args.fp16:
scaler.unscale_(optimizer)
scaler.step(optimizer)
scaler.update()
else:
optimizer.step()
optimizer.zero_grad()
#scheduler.step()
global_step += 1
progress_bar.update(1)
if args.do_eval and global_step % args.save_steps == 0:
logger.info("***** Running evaluation *****")
logger.info(" Num examples = %d", len(eval_examples))
logger.info(" Batch size = %d", args.eval_batch_size)
def decode(input_ids):
return tokenizer.convert_ids_to_tokens(input_ids, skip_special_tokens=True)
model.eval()
eval_loss = 0
eval_steps = 0
all_inputs, all_labels, all_predictions = [], [], []
for batch in tqdm(eval_dataloader, desc="Evaluation"):
batch = tuple(t.to(device) for t in batch)
input_ids, attention_mask, token_type_ids, label_ids, task_id = batch
with torch.no_grad():
outputs = model(input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
task_id = task_id,
labels=label_ids)
tmp_eval_loss = outputs[0]
logits = outputs[1] ##(batch_size,seq_length,vocab_size) or (batch_size,label_list_size)
src_ids = input_ids.cpu().tolist()
trg_ids = label_ids.cpu().numpy() ##(batch_size,seq_length)
eval_loss += tmp_eval_loss.mean().item()
_, prd_ids = torch.max(logits, -1) ##(batch_size,seq_length) or (batch_size)
if task_name in task_class["csc"]:
prd_ids = prd_ids.masked_fill(attention_mask == 0, 0).tolist()
for s, t, p in zip(src_ids, trg_ids, prd_ids):
all_inputs += [decode(s)]
all_labels += [decode(t)]
all_predictions += [decode(p)]
else:
assert(task_name in task_class["seq"])
all_predictions.extend(prd_ids.detach().cpu().numpy().squeeze().tolist())
all_labels.extend(trg_ids[:,0].squeeze().tolist())
eval_steps += 1
loss = train_loss / global_step
eval_loss = eval_loss / eval_steps
if task_name in task_class["csc"]:
p, r, f1, fpr, wpr, tp, fp, fn, wp = Metrics.csc_compute(all_inputs, all_labels, all_predictions)
else:
assert(task_name in task_class["seq"])
f1 = Metrics.f1(all_predictions, all_labels)
acc = Metrics.acc(all_predictions,all_labels)
if task_name in task_class["csc"]:
output_tp_file = os.path.join(args.output_dir, "sents.tp")
with open(output_tp_file, "w") as writer:
for line in tp:
writer.write(line + "\n")
output_fp_file = os.path.join(args.output_dir, "sents.fp")
with open(output_fp_file, "w") as writer:
for line in fp:
writer.write(line + "\n")
output_fn_file = os.path.join(args.output_dir, "sents.fn")
with open(output_fn_file, "w") as writer:
for line in fn:
writer.write(line + "\n")
output_wp_file = os.path.join(args.output_dir, "sents.wp")
with open(output_wp_file, "w") as writer:
for line in wp:
writer.write(line + "\n")
result = {
"global_step": global_step,
"loss": loss,
"eval_loss": eval_loss,
"eval_p": p * 100,
"eval_r": r * 100,
"eval_f1": f1 * 100,
"eval_fpr": fpr * 100,
}
else:
result = {
"global_step": global_step,
"loss": loss,
"eval_loss": eval_loss,
"eval_acc": acc*100,
"eval_f1": f1 * 100,
}
model_to_save = model.module if hasattr(model, "module") else model
output_model_file = os.path.join(args.output_dir, "step-%s_f1-%.2f.bin" % (str(global_step), result["eval_f1"]))
torch.save(model_to_save.state_dict(), output_model_file)
best_result.append((result["eval_f1"], output_model_file))
## sort by f1 and remove model whose f1 is the fourth biggest
best_result.sort(key=lambda x: x[0], reverse=True)
if len(best_result) > 3:
_, model_to_remove = best_result.pop()
os.remove(model_to_remove)
output_eval_file = os.path.join(args.output_dir, "eval_results.txt")
if task_name in task_class['csc']:
with open(output_eval_file, "a") as writer:
logger.info("***** Eval results *****")
writer.write(
"Global step = %s | eval precision = %.2f | eval recall = %.2f | eval f1 = %.2f | eval fp rate = %.2f\n"
% (str(result["global_step"]),
result["eval_p"],
result["eval_r"],
result["eval_f1"],
result["eval_fpr"]))
for key in sorted(result.keys()):
logger.info("Global step: %s, %s = %s", str(global_step), key, str(result[key]))
else:
with open(output_eval_file, "a") as writer:
logger.info("***** Eval results *****")
writer.write(
"Global step = %s | eval f1 = %.2f | eval acc = %.2f \n"
% (str(result["global_step"]),
result["eval_f1"],
result["eval_acc"]))
for key in sorted(result.keys()):
logger.info("Global step: %s, %s = %s", str(global_step), key, str(result[key]))
if global_step >= args.max_train_steps:
wrap = True
break
if args.do_test:
task_name = task_names[0] ## we choose the first task to evaluate
processor = processors[task_name]
eval_examples=processor.get_test_examples(os.path.join(args.data_dir, task_name), args.test_on)
if task_name in task_class["csc"]:
eval_features = csc_convert_examples_to_features(eval_examples, args.max_seq_length, tokenizer) ## no mft in test
else:
assert(task_name in task_class["seq"])
label_list = InputExample.get_label_list(eval_examples)
eval_features = seq_convert_examples_to_features(eval_examples, label_list, args.max_seq_length, tokenizer)
all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
## attention_mask
all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
## token_type_ids
all_input_segment = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
all_label_ids = torch.tensor([f.label_ids for f in eval_features], dtype=torch.long)
all_task_ids = torch.tensor([f.task_id for f in eval_features], dtype=torch.long)
eval_data = TensorDataset(all_input_ids, all_input_mask, all_input_segment, all_label_ids, all_task_ids)
## we have to disrupt the order the features from different tasks
eval_sampler = RandomSampler(eval_data)
eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)
model = BertForMultiTask.from_pretrained(args.load_model_path,
return_dict=True,
cache_dir=cache_dir)
model.to(device)
if args.load_checkpoint:
model.load_state_dict(torch.load(args.load_checkpoint))
if n_gpu > 1:
model = torch.nn.DataParallel(model)
logger.info("***** Running evaluation *****")
logger.info(" Num examples = %d", len(eval_examples))
logger.info(" Batch size = %d", args.eval_batch_size)
def decode(input_ids):
return tokenizer.convert_ids_to_tokens(input_ids, skip_special_tokens=True)
model.eval()
eval_loss = 0
eval_steps = 0
all_inputs, all_labels, all_predictions = [], [], []
for batch in tqdm(eval_dataloader, desc="Evaluation"):
batch = tuple(t.to(device) for t in batch)
input_ids, attention_mask, token_type_ids, label_ids, task_id = batch
with torch.no_grad():
outputs = model(input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
task_id = task_id,
labels=label_ids)
tmp_eval_loss = outputs[0]
logits = outputs[1] ##(batch_size,seq_length,vocab_size) or (batch_size,label_list_size)
src_ids = input_ids.cpu().tolist() ##(batch_size,seq_length)
trg_ids = label_ids.cpu().numpy() ##(batch_size,seq_length)
eval_loss += tmp_eval_loss.mean().item()
_, prd_ids = torch.max(logits, -1) ##(batch_size,seq_length) or (batch_size)
print("***label_id***")
print(trg_ids)
print("***pred_ids***")
print(prd_ids)
if task_name in task_class["csc"]:
prd_ids = prd_ids.masked_fill(attention_mask == 0, 0).tolist()
for s, t, p in zip(src_ids, trg_ids, prd_ids):
all_inputs += [decode(s)]
all_labels += [decode(t)]
all_predictions += [decode(p)]
else:
assert(task_name in task_class["seq"])
all_predictions.extend(prd_ids.detach().cpu().numpy().squeeze().tolist())
all_labels.extend(trg_ids[:,0].squeeze().tolist())
eval_steps += 1
eval_loss = eval_loss / eval_steps
if task_name in task_class["csc"]:
p, r, f1, fpr, wpr, tp, fp, fn, wp = Metrics.csc_compute(all_inputs, all_labels, all_predictions)
else:
assert(task_name in task_class["seq"])
f1 = Metrics.f1(all_predictions, all_labels)
acc = Metrics.acc(all_predictions,all_labels)
if task_name in task_class["csc"]:
output_tp_file = os.path.join(args.output_dir, "sents.tp")
with open(output_tp_file, "w") as writer:
for line in tp:
writer.write(line + "\n")
output_fp_file = os.path.join(args.output_dir, "sents.fp")
with open(output_fp_file, "w") as writer:
for line in fp:
writer.write(line + "\n")
output_fn_file = os.path.join(args.output_dir, "sents.fn")
with open(output_fn_file, "w") as writer:
for line in fn:
writer.write(line + "\n")
output_wp_file = os.path.join(args.output_dir, "sents.wp")
with open(output_wp_file, "w") as writer:
for line in wp:
writer.write(line + "\n")
result = {
"eval_loss": eval_loss,
"eval_p": p * 100,
"eval_r": r * 100,
"eval_f1": f1 * 100,
"eval_fpr": fpr * 100,
}
else:
result = {
"eval_loss": eval_loss,
"eval_acc": acc*100,
"eval_f1": f1 * 100,
}
output_eval_file = os.path.join(args.output_dir, "eval_results.txt")
if task_name in task_class['csc']:
with open(output_eval_file, "a") as writer:
logger.info("***** Eval results *****")
writer.write(
"Global step = %s | eval precision = %.2f | eval recall = %.2f | eval f1 = %.2f | eval fp rate = %.2f\n"
% (str(-1),
result["eval_p"],
result["eval_r"],
result["eval_f1"],
result["eval_fpr"]))
for key in sorted(result.keys()):
logger.info("Global step: %s, %s = %s", str(-1), key, str(result[key]))
else:
with open(output_eval_file, "a") as writer:
logger.info("***** Eval results *****")
writer.write(
"Global step = %s | eval f1 = %.2f | eval acc = %.2f \n"
% (str(-1),
result["eval_f1"],
result["eval_acc"]))
for key in sorted(result.keys()):
logger.info("Global step: %s, %s = %s", str(-1), key, str(result[key]))
if __name__ == "__main__":
main()