首页/文章/ 详情

完形填空---遮蔽式语言模拟(Masked Language Modeling)

1年前浏览2971

1 引言

计算语言学其中一项具有挑战性的任务就是语言模拟,语言模拟是将一个模型与语料库相匹配,语料库可以是特定领域的,就像我们的GeotechSet。所有流行的基于transformer的模型都是用语言模拟的变体来训练的,例如BERT用屏蔽语言建模(masked language modeling),而GPT-2用因果语言建模(causal language modeling)。除了预训练之外,语言模拟可以将模型的分布迁移到特定领域, 例如使用一个在非常大的语料库上训练出来语言模型,然后将其微调到新闻数据集或科学论文上。

这个笔记着重强调屏蔽式语言模拟(MLM, masked language modeling),类似于我们在英语考试中经常遇到的完形填空。屏蔽式语言模拟的任务是用一个屏蔽标记(token)来屏蔽一个序列中的标记,并提示模型用一个适当的标记来填充该屏蔽。这使得模型能够同时注意到右边的语境(掩码右边的标记)和左边的语境(掩码左边的标记)。MLM允许模型学习句子的双向表示, 这样的训练为需要双向语境的下游任务,如SQuAD(问题回答Question Answering)奠定了坚实基础。取一个句子,模型随机掩蔽输入中15%的单词,然后通过模型运行整个掩蔽的句子预测掩蔽的单词,参看《阅读理解回答问题(Question Answering)---一个更强的BERT预训练模型》和《Transformers之问题对答(Question Answering)》。此外,可以利用run_mlm.py脚本在屏蔽的语言模拟任务上微调模型。

2 管道(Pipeline)方法

在前面的文章中,已经使用过如下两个管道:

    from transformers import pipelinesummarizer = pipeline("summarization")question_answering = pipeline("question-answering") 

    MLM使用的管道名称是fill-mask

      unmasker = pipeline("fill-mask")

      unmasker.tokenizer.mask_token为需要填充的词汇。目前只能填充一个单词,如果发展成bigram和trigram就更好了。

      (1) 填充名词

      试验的句子为Shear failure commonly takes place along the {  } at the lower part of rock slope.

      计算选项: slope, ridge, slopes, glacier, cliffs

      正确答案: joint fissure

      (2) 填充动词

      试验的句子为continuum models cannot adequately {   } the relative displacement of blocks, interlocking, internal moments produced by block rotations, and fracturing of the intact rock material.

      计算选项: capture, explain, quantify, simulate, describe

      正确答案: simulate

      (3) 填充形容词

      试验的句子为failure of intact rock bridges would develop in the form of {   } damage over a specific area.

      计算选项: structural, catastrophic, extensive, permanent, seismic

      正确答案: progressive 

      3 模型和标记方法

      下面使用模型和标记的方法来进行填充任务。采用的模型是distilbert-base-cased。

        from transformers import AutoModelWithLMHead, AutoTokenizertokenizer = AutoTokenizer.from_pretrained("distilbert-base-cased")model = AutoModelWithLMHead.from_pretrained("distilbert-base-cased")

        句子和填充与上面Pipeline方法中所示的一样。现在我们来运行这个模型,并与上面的结果做比较。

        (1) 填充名词

        试验的句子为Shear failure commonly takes place along the {  } at the lower part of rock slope.

        计算选项: ridge, slope, slopes, ridges, fault 【slope, ridge, slopes, glacier, cliffs】

        正确答案: joint fissure

        这五个选项中的fault与答案joint最为接近。

        (2) 填充动词

        试验的句子为continuum models cannot adequately {   } the relative displacement of blocks, interlocking, internal moments produced by block rotations, and fracturing of the intact rock material.

        计算选项: predict, describe, explain, estimate, determine 【capture, explain, quantify, simulate, describe】

        正确答案: simulate

        这些选项其实都可以代替标准答案。

        (3) 填充形容词

        试验的句子为failure of intact rock bridges would develop in the form of {   } damage over a specific area.

        计算选项: structural, catastrophic, landslide, flood, significant【structural, catastrophic, extensive, permanent, seismic】

        正确答案: progressive

        没有上下文,这个填空是很难猜出的。 

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

        课程
        培训
        服务
        行家

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