Example #1
0
def start():
    # produce_data()
    model = Bert_CRF()
    model.load_state_dict(load_model(args.output_dir))
    device = torch.device(args.device if torch.cuda.is_available()
                          and not args.no_cuda else "cpu")
    model.to(device)
    print('create_iter')
    eval_iter = create_batch_iter("valid")
    print('create_iter finished')
    # ------------------判断CUDA模式----------------------
    device = torch.device(args.device if torch.cuda.is_available()
                          and not args.no_cuda else "cpu")

    # -----------------------验证----------------------------
    model.eval()
    count = 0
    y_predicts, y_labels = [], []
    eval_loss, eval_acc, eval_f1 = 0, 0, 0
    with torch.no_grad():
        for step, batch in enumerate(eval_iter):
            batch = tuple(t.to(device) for t in batch)
            input_ids, input_mask, segment_ids, label_ids, output_mask = batch
            bert_encode = model(input_ids, segment_ids, input_mask).cpu()
            eval_los = model.loss_fn(bert_encode=bert_encode,
                                     tags=label_ids,
                                     output_mask=output_mask)
            eval_loss = eval_los + eval_loss
            count += 1
            predicts = model.predict(bert_encode, output_mask)
            predict_tensor = predicts.cpu()
            label_tensor = label_ids.cpu()
            y_predicts.append(predicts)
            y_labels.append(label_ids)
            entity_precision, entity_recall, entity_f1 = score_predict(
                label_tensor, predict_tensor)
            print(
                '\n step :%d - eval_loss: %4f - ent_p:%4f - ent_r:%4f - ent_f1:%4f\n'
                % (step, eval_loss.item() / count, entity_precision,
                   entity_recall, entity_f1))

            label_ids = label_ids.view(1, -1).squeeze()
            predicts = predicts.view(1, -1).squeeze()
            label_ids = label_ids[label_ids != -1]
            predicts = predicts[predicts != -1]
            assert len(label_ids) == len(predicts)

        eval_predicted = torch.cat(y_predicts, dim=0).cpu()
        eval_labeled = torch.cat(y_labels, dim=0).cpu()
        entity_precision, entity_recall, entity_f1 = score_predict(
            eval_labeled, eval_predicted)
        print(
            '\n\n- eval_loss: %4f - eval_acc:%4f - eval_f1:%4f - ent_p:%4f - ent_r:%4f - ent_f1:%4f\n'
            % (eval_loss.item() / count, eval_acc, eval_f1, entity_precision,
               entity_recall, entity_f1))
Example #2
0
 def __init__(self):
     print('[INFO]加载分词器')
     self.processor, self.bertTokenizer = init_params()
     label_list = self.processor.get_labels()
     self.label_map = {label: i for i, label in enumerate(label_list)}
     self.tokenizer = BasicTokenizer()
     print('[INFO]分词器加载完毕')
     print('[INFO]加载模型')
     self.model = Bert_CRF()
     self.model.load_state_dict(load_model(args.output_dir))
     self.device = torch.device(args.device if torch.cuda.is_available()
                                and not args.no_cuda else "cpu")
     self.model.to(self.device)
     self.model.eval()
     print('[INFO]模型加载完毕')
Example #3
0
def start():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--do_not_train_ernie",
        default=False,
        action='store_true',
    )
    parser.add_argument(
        "--do_CRF",
        default=False,
        action='store_true',
    )
    arg = parser.parse_args()
    args.do_not_train_ernie = arg.do_not_train_ernie
    args.do_CRF = arg.do_CRF

    produce_data()
    train_iter, num_train_steps = create_batch_iter("train")
    eval_iter = create_batch_iter("dev")

    epoch_size = num_train_steps * args.train_batch_size * args.gradient_accumulation_steps / args.num_train_epochs

    pbar = ProgressBar(epoch_size=epoch_size, batch_size=args.train_batch_size)
    if args.load_weight:
        model = load_model(args.output_dir)
    else:
        model = Bert_CRF.from_pretrained(args.bert_model,
                                         num_tag=len(args.labels))

    for name, param in model.named_parameters():
        if param.requires_grad:
            print(name)

    fit(model=model,
        training_iter=train_iter,
        eval_iter=eval_iter,
        num_epoch=args.num_train_epochs,
        pbar=pbar,
        num_train_steps=num_train_steps,
        verbose=1)
Example #4
0
def start():
    train_iter, num_train_steps = create_batch_iter("train")
    eval_iter = create_batch_iter("dev")

    epoch_size = num_train_steps * args.train_batch_size * args.gradient_accumulation_steps / args.num_train_epochs

    pbar = ProgressBar(epoch_size=epoch_size, batch_size=args.train_batch_size)

    model = load_model(args.output_dir)
    #model = QaExtract.from_pretrained(args.bert_model)  #QaExtract(args)

    for name, param in model.named_parameters():
        if param.requires_grad:
            print(name)

    fit(model=model,
        training_iter=train_iter,
        eval_iter=eval_iter,
        num_epoch=args.num_train_epochs,
        pbar=pbar,
        num_train_steps=num_train_steps,
        verbose=1)
from Io.data_loader import create_batch_iter
from preprocessing.data_processor import produce_data
import torch
import os
import json
import config.args as args
from util.model_util import load_model

args.do_inference = True
produce_data()

test_iter = create_batch_iter("inference")
epoch_size = args.train_batch_size * args.gradient_accumulation_steps / args.num_train_epochs
model = load_model(args.output_dir)

num_epoch = args.num_train_epochs
device = torch.device(
    args.device if torch.cuda.is_available() and not args.no_cuda else "cpu")

param_optimizer = list(model.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':
Example #6
0
                        default=-1)
    parser.add_argument(
        '--transform-sequences',
        help=
        'For DFN, determines if FFT is computed over sequences along with tokens.',
        default=False,
        action='store_true')

    args = parser.parse_args()
    data, label_to_id = load_data(args.train, args.dev, args.labels)
    train_data = data['train']
    validation_data = data['dev']
    vocab = None
    model = None
    if args.pretrained_model is not None:
        model, model_config, vocab, reverse_vocab = load_model(
            args.pretrained_model)
    print('\nLoading training data...')
    train_X, train_Y, vocab, reverse_vocab = process_data(
        train_data,
        label_to_id,
        vocab=vocab,
        vocab_size=args.vocab_size,
        max_tokens=args.sequence_length)
    print('Training data loaded.')
    print('\nLoading validation data...')
    validation_X, validation_Y, _, _ = process_data(
        validation_data,
        label_to_id,
        vocab=vocab,
        max_tokens=args.sequence_length)
    print('Validation data loaded.')
Example #7
0
def main(data_dir):
    model = load_model(args.output_dir)
    model.to(device)
    tokenizer = BertTokenizer(args.VOCAB_FILE)
    make_predict(model, tokenizer, data_dir)
Example #8
0
             (index + 1), total_correct_predictions / total_predictions))
        generator_tqdm.set_description(description, refresh=False)
    average_loss = total_eval_loss / len(eval_batches)
    eval_accuracy = total_correct_predictions / total_predictions
    print('Final evaluation accuracy: %.4f loss: %.4f' %
          (eval_accuracy, average_loss))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="""Script to evaluate a trained model on data.""")
    parser.add_argument('model', help='Path to trained model directory')
    parser.add_argument('--test',
                        help='Path to evaluation data.',
                        default=r'./data/test.csv')
    parser.add_argument('--labels',
                        help='Path to label dictionary.',
                        default=r'./data/answers.json')

    args = parser.parse_args()
    data, label_to_id = load_eval_data(args.test, args.labels)
    print('\nLoading test data...')
    model, model_config, vocab, reverse_vocab = load_model(args.model)
    test_X, test_Y, vocab, reverse_vocab = process_data(
        data, label_to_id, vocab=vocab, vocab_size=model_config['vocab_size'])
    print('Test data loaded.')
    batch_size = 32
    batches = generate_batches(test_X, test_Y, batch_size)
    print('Batches finished generating.')
    train_result = eval(model, batches)