首页/文章/ 详情

阅读理解回答问题(Question Answering)---一个更强的BERT预训练模型

1年前浏览2720

1 引言

在<Transformers之问题对答(Question Answering)>中, 使用了mrm8488/bert-multi-cased-finetuned-xquadv1数据集回答问题, 这个数据集是一个多语言预训练模型: BERT(base-multilingual-cased) fine-tuned for multilingual Q&A. 并且使用了最简单的管道pipeline()调用方法. 就像我们已经看到的一样, 这个模型得出的结果不甚理想, 因此本文探索了一个更高级的BERT预训练模型.

2 模型描述

本文的试验模型采用了bert-large-uncased-whole-word-masking-finetuned-squad数据集作为问题回答模型。在默认状态下, 这个模型保存在C:\Users\m\.cache\huggingface\transformers文件夹内. 该模型不区分字母大小写, 使用了屏蔽语言模拟masked language modeling (MLM) 目标对英语语言进行预训练。可以在问题回答管道中使用它,或者使用它来输出给定查询和上下文的原始结果。BERT模型在BookCorpus上进行了预训练,该数据集由11,038本未出版的书籍和英文维 基 百 科组成(不包括列表、表格和标题)。

与其他BERT模型不同的是,这个模型使用了全词屏蔽Whole Word Masking技术进行训练。在这种情况下,一个词所对应的所有标记(tokens)都会被一次性屏蔽掉, 而整体屏蔽率保持不变。训练是相同的 -- 每个被屏蔽的WordPiece标记都是独立预测的。在预训练之后,这个模型在SQuAD数据集上用一个微调脚本进行了微调。

BERT是一个以自我监督方式在大型英语数据语料库上预训练的transformers 模型。这意味着它只对原始文本进行了预训练,没有人以任何方式给它们贴标签(这就是为什么它可以使用大量公开可用的数据),并通过一个自动过程从这些文本中生成输入和标签。更确切地说,它的预训练有两个目标: 

(1) Masked language modeling (MLM): 掩蔽语言模拟(MLM)---取一个句子,模型随机掩蔽输入中15%的单词,然后通过模型运行整个掩蔽的句子预测掩蔽的单词。这与传统的递归神经网络RNN不同,RNN通常是一个接一个地看单词,或者与自回归模型GPT不同,GPT在内部屏蔽未来的标记。而MLM允许模型学习句子的双向表示。

(2) Next sentence prediction (NSP): 下一句预测(NSP)---模型在预训练期间将两个被掩盖的句子连接起来作为输入。作为随机变量, 有时它们对应于原文中彼此相邻的句子,有时则不是。然后,该模型预测这两个句子是否彼此相接。

这样一来,该模型就学会了英语的内在表示,然后可以用来提取对下游任务有用的特征, 例如,如果有一个标记的句子数据集,就可以使用BERT模型产生的特征作为输入来训练一个标准分类器。这个模型有以下配置: 24层, 1024个隐藏维度; 16个注意头和336M参数。

3 调用方法

<Transformers之问题对答(Question Answering)>[transformers-pipeline-question-answering.py]使用了管道pipeline方法,而本例使用AutoTokenizer方法[Transformers-AutoModelForQuestionAnswering.py]。

    from transformers import AutoTokenizer, AutoModelForQuestionAnsweringimport torchtokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")

    4 测试结果

    我们使用与前文内容相同的句子作为比较对象,提出以下四个问题::

    内容:  '''The development of a step-path failure surface is mainly controlled by the orientation and spatial characteristics of the present major rock structure including major joints sets, shear planes and fault planes. '''

    (1) 问题: '''What kinds of factors controlled the development of a step-path failure surface?'''

    回答: orientation and spatial characteristics of the present major rock structure including major joints sets, shear planes and fault planes

    这个回答更精确和完善.  
    (2) 问题: '''Please describe the major rock structure.''' 

    回答: major joints sets, shear planes and fault planes

    这个回答正确.

    (3) 问题: What is rock structure?

    回答: major joints sets, shear planes and fault planes

    这个回答正确.

    (4) How many kinds of present major rock structure?

    回答: major joints sets, shear planes and fault planes (0.003)

    这个回答正确.

    可以看出,就英语语言来说,这个模型比上一个模型的效果好很多。

    5 新的测试

    内容: '''The Chuquicamata mine in northern Chile has one of the largest open pits in the world, measuring approximately 4 km long, 3 km wide, and 1 km deep. Removing ore and waste from the mine on conveyors or by truck, using the haul roads such as that illustrated in Fig. 25, is a complex and expensive process. Hence, planning started more than 10 years ago for a transition from open pit to block caving underground as the mining method.''' [智利北部的丘基卡马塔矿是世界上最大的露天矿之一,长约4公里,宽3公里,深1公里。用传送带或卡车将矿石和矸石从矿井中运出,使用如图25所示的运输道路,这是一个复杂而昂贵的过程。因此,10多年前就开始规划采矿方法,从露天矿过渡到地下块体崩落法。]

    根据上面的描述,提出以下四个问题:

    (1) 问题: Where is Chuquicamata mine located?

    回答: northern chile

    (2) 问题: What size is Chuquicamata mine?

    回答: one of the largest open pits in the world

    (3) 问题: What method is used in Chuquicamata mine?

    回答: block caving underground

    (4) How depth is Chuquicamata mine?

    回答: 1 km

    来源:计算岩土力学
    AutoMod试验
    著作权归作者所有,欢迎分享,未经许可,不得转载
    首次发布时间:2022-11-20
    最近编辑:1年前
    计算岩土力学
    传播岩土工程教育理念、工程分析...
    获赞 119粉丝 871文章 1721课程 0
    点赞
    收藏
    未登录
    还没有评论

    课程
    培训
    服务
    行家

    VIP会员 学习 福利任务 兑换礼品
    下载APP
    联系我们
    帮助与反馈