Exemple #1
0
def generate_ner_model(config, token_embed, voc_iv_size, voc_ooev_size, char_size, label_size, embedd_word):
    ner_model = BiRecurrentConvCRF4NestedNER(token_embed, voc_iv_size, voc_ooev_size, config['char_embed'], char_size, 
                                             config['num_filters'], config['kernel_size'], label_size, embedd_word,
                                             hidden_size=config['hidden_size'], layers=config['layers'],
                                             word_dropout=config['word_dropout'], char_dropout=config['char_dropout'],
                                             lstm_dropout=config['lstm_dropout'])
    return ner_model
Exemple #2
0
def get_f1(model: BiRecurrentConvCRF4NestedNER,
           mode: str,
           file_path: str = None) -> float:
    with torch.no_grad():
        model.eval()

        pred_all, pred, recall_all, recall = 0, 0, 0, 0
        gold_cross_num = 0
        pred_cross_num = 0
        if mode == 'dev':
            batch_zip = zip(dev_token_iv_batches, dev_token_ooev_batches,
                            dev_char_batches, dev_label_batches,
                            dev_mask_batches)
        elif mode == 'test':
            batch_zip = zip(test_token_iv_batches, test_token_ooev_batches,
                            test_char_batches, test_label_batches,
                            test_mask_batches)
        else:
            raise ValueError

        f = None
        if file_path is not None:
            f = open(file_path, 'w')

        for token_iv_batch, token_ooev_batch, char_batch, label_batch, mask_batch in batch_zip:
            token_iv_batch_var = torch.LongTensor(np.array(token_iv_batch))
            token_ooev_batch_var = torch.LongTensor(np.array(token_ooev_batch))
            char_batch_var = torch.LongTensor(np.array(char_batch))
            mask_batch_var = torch.ByteTensor(
                np.array(mask_batch, dtype=np.uint8))
            if config.if_gpu:
                token_iv_batch_var = token_iv_batch_var.cuda()
                token_ooev_batch_var = token_ooev_batch_var.cuda()
                char_batch_var = char_batch_var.cuda()
                mask_batch_var = mask_batch_var.cuda()

            pred_sequence_entities = model.predict(token_iv_batch_var,
                                                   token_ooev_batch_var,
                                                   char_batch_var,
                                                   mask_batch_var)
            pred_entities = unpack_prediction(model, pred_sequence_entities)
            p_a, p, r_a, r = evaluate(label_batch, pred_entities)

            gold_cross_num += 0
            pred_cross_num += 0

            pred_all += p_a
            pred += p
            recall_all += r_a
            recall += r

            if file_path is not None:
                for token_iv, token_ooev, mask, label, preds \
                        in zip(token_iv_batch, token_ooev_batch, mask_batch, label_batch, pred_entities):
                    words = []
                    for t_iv, t_ooev, m in zip(token_iv, token_ooev, mask):
                        if not m:
                            break
                        if t_iv > 0:
                            words.append(voc_iv_dict.get_instance(t_iv))
                        else:
                            words.append(voc_ooev_dict.get_instance(t_ooev))
                    f.write(' '.join(words) + '\n')

                    labels = []
                    for l in sorted(label, key=lambda x: (x[0], x[1], x[2])):
                        labels.append("{},{} {}".format(
                            l[0], l[1], label_dict.get_instance(l[2])))
                    f.write('|'.join(labels) + '\n')

                    labels = []
                    for p in sorted(preds, key=lambda x: (x[0], x[1], x[2])):
                        labels.append("{},{} {}".format(
                            p[0], p[1], label_dict.get_instance(p[2])))
                    f.write('|'.join(labels) + '\n')

                    f.write('\n')

        if file_path is not None:
            f.close()

        pred = pred / pred_all if pred_all > 0 else 1.
        recall = recall / recall_all if recall_all > 0 else 1.
        f1 = 2 / ((1. / pred) +
                  (1. / recall)) if pred > 0. and recall > 0. else 0.
        logger.info(
            "{} precision: {:.2f}%, recall: {:.2f}%, F1: {:.2f}%".format(
                mode, pred * 100., recall * 100., f1 * 100.))
        # logger.info("Prediction Crossing: ", pred_cross_num)
        # logger.info("Gold Crossing: ", gold_cross_num)

        return f1
Exemple #3
0
with open(config.embed_path, 'rb') as f:
    vectors: List[np.ndarray] = pickle.load(f)
    config.token_embed = vectors[0].size
    embedd_word: Tensor = Tensor(vectors)

config.if_gpu = config.if_gpu and torch.cuda.is_available()

logger.info(config)  # print training setting

ner_model = BiRecurrentConvCRF4NestedNER(config.token_embed,
                                         config.voc_iv_size,
                                         config.voc_ooev_size,
                                         config.char_embed,
                                         config.char_size,
                                         config.num_filters,
                                         config.kernel_size,
                                         config.label_size,
                                         embedd_word,
                                         hidden_size=config.hidden_size,
                                         layers=config.layers,
                                         word_dropout=config.word_dropout,
                                         char_dropout=config.char_dropout,
                                         lstm_dropout=config.lstm_dropout)
if config.if_gpu:
    ner_model = ner_model.cuda()

parameters = filter(lambda p: p.requires_grad, ner_model.parameters())
optimizer, lr_scheduler = create_opt(parameters,
                                     config.opt,
                                     lr=config.lr,
                                     l2=config.l2,
                                     lr_patience=config.lr_patience)
f.close()

# misc info
misc_config: Dict[str,
                  Alphabet] = pickle.load(open(config.config_data_path, 'rb'))
label_dict = load_dynamic_config(misc_config)
config.label_size = label_dict.size()

config.if_gpu = config.if_gpu and torch.cuda.is_available()

logger.info(config)  # print training setting

ner_model = BiRecurrentConvCRF4NestedNER(config.embed_path,
                                         config.char_embed,
                                         config.num_filters,
                                         config.label_size,
                                         hidden_size=config.hidden_size,
                                         layers=config.layers,
                                         word_dropout=config.word_dropout,
                                         lstm_dropout=config.lstm_dropout)
if config.if_gpu:
    ner_model = ner_model.cuda()

parameters = filter(lambda p: p.requires_grad, ner_model.parameters())
optimizer, lr_scheduler = create_opt(parameters,
                                     config.opt,
                                     lr=config.lr,
                                     l2=config.l2,
                                     lr_patience=config.lr_patience)

train_sequence_label_batches = [
    pack_target(ner_model, train_label_batch, train_mask_batch)
Exemple #5
0
# misc info
misc_config: Dict[str,
                  Alphabet] = pickle.load(open(config.config_data_path, 'rb'))
voc_dict = misc_config.get("voc_dict")
label_dict = misc_config.get("label_dict")
config.voc_size = voc_dict.size()
config.label_size = label_dict.size()
config.bert_model = misc_config.get("bert_model", config.bert_model)
config.if_gpu = config.if_gpu and torch.cuda.is_available()

logger.info(config)  # print training setting

ner_model = BiRecurrentConvCRF4NestedNER(config.bert_model,
                                         config.label_size,
                                         hidden_size=config.hidden_size,
                                         layers=config.layers,
                                         lstm_dropout=config.lstm_dropout)
if config.if_gpu:
    ner_model = ner_model.cuda()

parameters = filter(lambda p: p.requires_grad, ner_model.parameters())
optimizer, lr_scheduler = create_opt(parameters,
                                     config.opt,
                                     lr=config.lr,
                                     l2=config.l2,
                                     lr_patience=config.lr_patience)

train_sequence_label_batches = [
    pack_target(ner_model, train_label_batch, train_mask_batch)
    for train_label_batch, train_mask_batch in zip(train_label_batches,