当前位置: 首页>>代码示例>>Python>>正文


Python BertForQuestionAnswering.from_pretrained方法代码示例

本文整理汇总了Python中pytorch_pretrained_bert.modeling.BertForQuestionAnswering.from_pretrained方法的典型用法代码示例。如果您正苦于以下问题:Python BertForQuestionAnswering.from_pretrained方法的具体用法?Python BertForQuestionAnswering.from_pretrained怎么用?Python BertForQuestionAnswering.from_pretrained使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pytorch_pretrained_bert.modeling.BertForQuestionAnswering的用法示例。


在下文中一共展示了BertForQuestionAnswering.from_pretrained方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test

# 需要导入模块: from pytorch_pretrained_bert.modeling import BertForQuestionAnswering [as 别名]
# 或者: from pytorch_pretrained_bert.modeling.BertForQuestionAnswering import from_pretrained [as 别名]
def test(args):  # Load a trained model that you have fine-tuned (we assume evaluate on cpu)
    tokenizer = BertTokenizer.from_pretrained(modelconfig.MODEL_ARCHIVE_MAP[args.bert_model])
    
    eval_examples = data_utils.read_squad_examples(os.path.join(args.data_dir,"test.json"), is_training=False)

    eval_features = data_utils.convert_examples_to_features(eval_examples, tokenizer, args.max_seq_length, args.doc_stride, args.max_query_length, is_training=False)
    
    logger.info("***** Running evaluation *****")
    logger.info("  Num examples = %d", len(eval_examples))
    logger.info("  Batch size = %d", args.eval_batch_size)
    all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
    all_segment_ids = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
    all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
    all_example_index = torch.arange(all_input_ids.size(0), dtype=torch.long)
    
    eval_data = TensorDataset(all_input_ids, all_segment_ids, all_input_mask, all_example_index)
    # Run prediction for full data
    eval_sampler = SequentialSampler(eval_data)
    eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)

    model = torch.load(os.path.join(args.output_dir, "model.pt") )
    model.cuda()
    model.eval()
    all_results = []
    for step, batch in enumerate(eval_dataloader):
        example_indices = batch[-1]
        batch = tuple(t.cuda() for t in batch[:-1])
        input_ids, segment_ids, input_mask= batch
        
        with torch.no_grad():
            batch_start_logits, batch_end_logits = model(input_ids, segment_ids, input_mask)

        for i, example_index in enumerate(example_indices):
            start_logits = batch_start_logits[i].detach().cpu().tolist()
            end_logits = batch_end_logits[i].detach().cpu().tolist()
            eval_feature = eval_features[example_index.item()]
            unique_id = int(eval_feature.unique_id)
            all_results.append(data_utils.RawResult(unique_id=unique_id,
                                         start_logits=start_logits,
                                         end_logits=end_logits))
    output_prediction_file = os.path.join(args.output_dir, "predictions.json")
    output_nbest_file = os.path.join(args.output_dir, "nbest_predictions.json")
    data_utils.write_predictions(eval_examples, eval_features, all_results, args.n_best_size, args.max_answer_length,
                      True, output_prediction_file, output_nbest_file, False) 
开发者ID:howardhsu,项目名称:BERT-for-RRC-ABSA,代码行数:46,代码来源:run_rrc.py


注:本文中的pytorch_pretrained_bert.modeling.BertForQuestionAnswering.from_pretrained方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。