ner_model.to(device) ner_model.eval() output_list = list() fout = open(args.output_text, 'w') iterator = data_loader.get_tqdm(device) max_score = -float('inf') min_score = float('inf') for word_t, char_t, chunk_mask, chunk_index, chunk_surface in iterator: output = ner_model(word_t, char_t, chunk_mask) chunk_score = ner_model.chunking(output) tmp_min = utils.to_scalar(chunk_score.min()) tmp_max = utils.to_scalar(chunk_score.max()) max_score = max(max_score, tmp_max) min_score = min(min_score, tmp_min) pred_chunk = (chunk_score < args.threshold) chunk_index = chunk_index.masked_select(pred_chunk).data.cpu() output = ner_model.typing(output, pred_chunk) output = output.data.cpu() offset = chunk_index[0] for ind in range(0, output.size(0)): st, ed = chunk_index[ind].item(), chunk_index[ind + 1].item() surface = ' '.join(chunk_surface[st - offset:ed - offset])
chunk_score = ner_model.chunking(output) chunk_loss = crit_chunk(chunk_score, chunk_label) type_score = ner_model.typing(output, type_mask) type_loss = crit_type(type_score, type_label) loss = type_loss + chunk_loss loss.backward() torch.nn.utils.clip_grad_norm_(ner_model.parameters(), args.clip) optimizer.step() batch_index += 1 if 0 == batch_index % args.interval: pw.add_loss_vs_batch({'loss_chunk': utils.to_scalar(chunk_loss), 'loss_type': utils.to_scalar(type_loss)}, batch_index, use_logger = False) if 0 == batch_index % args.check: # NER evaluation pre_dev, rec_dev, f1_dev, type2pre_dev, type2rec_dev, type2f1_dev = utils.evaluate_ner(dev_loader.get_tqdm(device), ner_model, tl_map['None'], id2label) pw.add_loss_vs_batch({'dev_pre': pre_dev, 'dev_rec': rec_dev}, batch_index, use_logger = False) pw.add_loss_vs_batch({'dev_f1': f1_dev}, batch_index, use_logger = True) pw.save_checkpoint(model = ner_model, is_best = f1_dev > best_eval, s_dict = {'config': ner_config, 'w_map': w_map, 'c_map': c_map, 'tl_map': tl_map, 'cl_map': cl_map}) if f1_dev > best_eval: best_eval = f1_dev # best_f1, best_pre, best_rec, best_type2pre, best_type2rec, best_type2f1 = utils.evaluate_ner(test_loader.get_tqdm(device), ner_model, tl_map['None']) best_pre, best_rec, best_f1, best_type2pre, best_type2rec, best_type2f1 = utils.evaluate_ner(test_loader.get_tqdm(device), ner_model, tl_map['None'], id2label)
type_score = ner_model.typing(output, type_mask) type_loss = crit_type(type_score, type_label) loss = type_loss + chunk_loss loss.backward() torch.nn.utils.clip_grad_norm_(ner_model.parameters(), args.clip) optimizer.step() batch_index += 1 if 0 == batch_index % args.interval: pw.add_loss_vs_batch( { 'loss_chunk': utils.to_scalar(chunk_loss), 'loss_type': utils.to_scalar(type_loss) }, batch_index, use_logger=False) if 0 == batch_index % args.check: # NER evaluation pre_dev, rec_dev, f1_dev, type2pre_dev, type2rec_dev, type2f1_dev = utils.evaluate_ner( dev_loader.get_tqdm(device), ner_model, tl_map['None'], id2label) pw.add_loss_vs_batch( { 'dev_pre': pre_dev, 'dev_rec': rec_dev