Exemple #1
0
def do_inference(args):
    prepare_output_path(args.output_dir, args.overwrite_output_dir)
    device, n_gpus = setup_backend(args.no_cuda)
    args.task_name = args.task_name.lower()
    task = get_glue_task(args.task_name, data_dir=args.data_dir)
    args.batch_size = args.per_gpu_eval_batch_size * max(1, n_gpus)
    classifier = TransformerSequenceClassifier.load_model(
        model_path=args.model_path,
        model_type=args.model_type,
        task_type=task.task_type,
        metric_fn=get_metric_fn(task.name),
        do_lower_case=args.do_lower_case,
        load_quantized=args.load_quantized_model,
    )
    classifier.to(device, n_gpus)
    examples = task.get_dev_examples(
    ) if args.evaluate else task.get_test_examples()
    preds = classifier.inference(examples,
                                 args.max_seq_length,
                                 args.batch_size,
                                 evaluate=args.evaluate)
    with io.open(os.path.join(args.output_dir, "output.txt"),
                 "w",
                 encoding="utf-8") as fw:
        for p in preds:
            fw.write("{}\n".format(p))
Exemple #2
0
def do_inference(args):
    prepare_output_path(args.output_dir, args.overwrite_output_dir)
    device, n_gpus = setup_backend(args.no_cuda)
    args.task_name = args.task_name.lower()
    task = get_glue_task(args.task_name, data_dir=args.data_dir)
    args.batch_size = args.per_gpu_eval_batch_size * max(1, n_gpus)
    classifier = TransformerSequenceClassifier.load_model(
        model_path=args.model_path, model_type=args.model_type)
    classifier.to(device, n_gpus)
    preds = classifier.inference(task.get_test_examples(), args.batch_size)
    with io.open(os.path.join(args.output_dir, "output.txt"),
                 "w",
                 encoding="utf-8") as fw:
        for p in preds:
            fw.write("{}\n".format(p))
Exemple #3
0
def do_training(args):
    prepare_output_path(args.output_dir, args.overwrite_output_dir)
    device, n_gpus = setup_backend(args.no_cuda)
    # Set seed
    args.seed = set_seed(args.seed, n_gpus)
    # Prepare GLUE task
    args.task_name = args.task_name.lower()
    task = get_glue_task(args.task_name, data_dir=args.data_dir)
    classifier = TransformerSequenceClassifier(
        model_type=args.model_type,
        model_name_or_path=args.model_name_or_path,
        labels=task.get_labels(),
        task_type=task.task_type,
        metric_fn=get_metric_fn(task.name),
        config_name=args.config_name,
        tokenizer_name=args.tokenizer_name,
        do_lower_case=args.do_lower_case,
        output_path=args.output_dir,
        device=device,
        n_gpus=n_gpus,
    )

    train_batch_size = args.per_gpu_train_batch_size * max(1, n_gpus)

    train_ex = task.get_train_examples()
    dev_ex = task.get_dev_examples()
    train_dataset = classifier.convert_to_tensors(train_ex,
                                                  args.max_seq_length)
    dev_dataset = classifier.convert_to_tensors(dev_ex, args.max_seq_length)
    train_sampler = RandomSampler(train_dataset)
    dev_sampler = SequentialSampler(dev_dataset)
    train_dl = DataLoader(train_dataset,
                          sampler=train_sampler,
                          batch_size=train_batch_size)
    dev_dl = DataLoader(dev_dataset,
                        sampler=dev_sampler,
                        batch_size=args.per_gpu_eval_batch_size)

    total_steps, _ = classifier.get_train_steps_epochs(
        args.max_steps, args.num_train_epochs, args.per_gpu_train_batch_size,
        len(train_dataset))
    classifier.setup_default_optimizer(
        weight_decay=args.weight_decay,
        learning_rate=args.learning_rate,
        adam_epsilon=args.adam_epsilon,
        warmup_steps=args.warmup_steps,
        total_steps=total_steps,
    )
    classifier.train(
        train_dl,
        dev_dl,
        None,
        gradient_accumulation_steps=args.gradient_accumulation_steps,
        per_gpu_train_batch_size=args.per_gpu_train_batch_size,
        max_steps=args.max_steps,
        num_train_epochs=args.num_train_epochs,
        max_grad_norm=args.max_grad_norm,
        logging_steps=args.logging_steps,
        save_steps=args.save_steps,
    )
    classifier.save_model(args.output_dir, args=args)