当前位置:   article > 正文

BERT实现简单问答_bertforquestionanswering

bertforquestionanswering

本文主要介绍BERT中BertForQuestionAnswering的简单使用,并附带一些有助于新手理解的网站

下载模型

BERT模型下载地址
在这里插入图片描述
下载后解压,本文解压路径如下:
在这里插入图片描述

模型转换

由于需要使用pytorch进行训练,所以需要将模型转换成pytorch可运行的形式。
转换的代码为:

import argparse
import logging

import torch

from transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert

logging.basicConfig(level=logging.INFO)

def convert_tf_checkpoint_to_pytorch(tf_checkpoint_path, bert_config_file, pytorch_dump_path):
    # Initialise PyTorch model
    config = BertConfig.from_json_file(bert_config_file)
    print("Building PyTorch model from configuration: {}".format(str(config)))
    model = BertForPreTraining(config)

    # Load weights from tf checkpoint
    load_tf_weights_in_bert(model, config, tf_checkpoint_path)

    # Save pytorch-model
    print("Save PyTorch model to {}".format(pytorch_dump_path))
    torch.save(model.state_dict(), pytorch_dump_path)


if __name__ == "__main__":
    convert_tf_checkpoint_to_pytorch(r'E:\paperCode\BERT\uncased_L-12_H-768_A-12\bert_model.ckpt', r'E:\paperCode\BERT\uncased_L-12_H-768_A-12\bert_config.json', r'E:\paperCode\BERT\uncased_L-12_H-768_A-12\pytorch_model.bin')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

模型转换成功后,将文件放入一个新的文件夹,config.json是Transformer要求的文件名,由bert_config重命名得到。
在这里插入图片描述

QA

具体的问答代码如下:

import torch
import os
# 使用 transformers的模型库需要将bert模型目录下的 bert_config.json 改为 config.json
from transformers import BertTokenizer,BertForQuestionAnswering    # 这个要从 transformers 导入

bert_path = r"E:\paperCode\BERT\qa"
## 上面我们自定义的保留最后3个模型的路径
model = BertForQuestionAnswering.from_pretrained(bert_path)
tokenizer = BertTokenizer.from_pretrained(bert_path)

question, doc = "Who is Lyon" , "Lyon is a killer"
encoding = tokenizer.encode_plus(text = question,text_pair = doc,  verbose=False)
inputs = encoding['input_ids']  #Token embeddings
sentence_embedding = encoding['token_type_ids']  #Segment embeddings
tokens = tokenizer.convert_ids_to_tokens(inputs)  #input tokens
print("tokens: ", tokens)

outputs = model(input_ids=torch.tensor([inputs]),
                                  token_type_ids=torch.tensor([sentence_embedding]))
#BertForQuestionAnswering返回一个QuestionAnsweringModelOutput对象。
#由于将BertForQuestionAnswering的输出设置为start_scores, end_scores,
# 因此返回的QuestionAnsweringModelOutput对象被强制转换为字符串的元组('start_logits', 'end_logits'),从而导致类型不匹配错误。
# torch.argmax(dim)会返回dim维度上张量最大值的索引
start_index = torch.argmax(outputs.start_logits)
end_index = torch.argmax(outputs.end_logits)
# start_index = torch.argmax(start_scores)
# end_index = torch.argmax(end_scores)
#print("start_index:%d, end_index %d"%(start_index, end_index))
answer = ' '.join(tokens[start_index:end_index + 1])
print(answer)
# 每次执行的结果不一致,这里因为模型没有经过训练,所以效果不好,输出结果不佳
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

注意点:config.json的重命名、start_index和end_index的格式问题。

几个网站

huggingface

https://huggingface.co/

Hugging Face专注于NLP技术,拥有大型的开源社区。尤其是在github上开源的自然语言处理,预训练模型库 Transformers,已被下载超过一百万次,github上超过24000个star。Transformers 提供了NLP领域大量state-of-art的 预训练语言模型结构的模型和调用框架。以下是repo的链接(https://github.com/huggingface/transformers)

Transformer:

简介:

https://www.cnblogs.com/panchuangai/p/12567853.html

https://www.cnblogs.com/panchuangai/p/12567851.html

BERT介绍

https://zhuanlan.zhihu.com/p/113639892

参考:

本文在下文的基础上进行实验,对文中一些在现版本下不可运行的部分进行调整。

https://blog.csdn.net/weixin_46425692/article/details/109184688

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/645255
推荐阅读
相关标签
  

闽ICP备14008679号