知行编程网知行编程网  2022-05-05 20:00 知行编程网 隐藏边栏 |   抢沙发  25 
文章评分 0 次,平均分 0.0
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%
原作 | Jack Pertschuk   转自 | 量子位 
在各大NLP榜单大杀四方的BERT,自打正式加入搜索引擎中,用户搜索体验确实更丝滑了。
不过,强大往往也意味着复杂。
BERT对算力、内存要求很高,为了使其顺利应对成百万次的搜索,谷歌不得不搭建起专门的Cloud TPU来满足其计算量。
那么,问题来了。不像谷歌那样财大气粗,就用不上BERT搜索了呗?
不知道你是否听说过,华为诺亚方舟实验室和华中科技大学联手打造的TinyBERT
现在,就有人用这个Transformer蒸馏方法,让BERT搜索变快6倍,模型大小则只有原来的1/20
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%

TinyBERT搜索

首先,用微软的MS Marco训练三元组,在PyTorch中训练一个基于BERT的教师网络。
注:MS Marco:微软机器阅读理解数据集,由10万个问答组成
然后,让教师网络训练一个较小的学生BERT网络,这个学生网络只有4个隐藏层,而非标准的12层。
每一层的大小仅为312,以使模型更为轻巧。
在整个模型的最后,采用前馈二进制分类层来生成得分,进行搜索排名。
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%
模型的具体参数如下,加粗部分是与标准BERT不同的部分。
<section style="padding: 0.5em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(51, 51, 51);background: rgb(248, 248, 248);margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;">{ <br  />“ attention_probs_dropout_prob”:<span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">0.1</span>,<br  />“单元格”:{},<br  /><strong> “ emb_size”:</strong><strong><span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">312</span>,</strong><br  /> “ hidden_act”:“ gelu”,<br  />“ hidden_dropout_prob”:<span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">0.1</span>,<br  />“ hidden_size”:<span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">312</span>,<br  />“ initializer_range”:<span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">0.02</span>,<br  /><strong>“ intermediate_size”:</strong><strong> <span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">1200</span>,</strong><br  /> “ max_position_embeddings”:<span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">512</span>,<br  />“ num_attention_heads”:<span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">12</span>,<br  /><strong>“ num_hidden_layers”:</strong><strong><span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">4</span>,</strong><br  /> “ pre_trained”:“”,<br  />“ structure”:[],<br  />“ type_vocab_size”:<span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">2</span>,<br  />“ vocab_size”:<span style="font-size: inherit;line-height: inherit;color: rgb(0, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">30522</span> <br  />}<br  /></section>
那么,用TinyBERT来做搜索,效果如何呢?
作者用MS Marco数据集对其进行检验。这一数据集采集自Bing的实际使用情况,是最大的公共搜索引擎使用情况数据集。
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%
虽然在搜索效果的改进上,TinyBERT的性能只达到了BERT的90%,但速度提升了5倍,并且模型大小仅为BERT的1/20。

谷歌:用BERT做搜索

2019年10月底,谷歌将BERT加入到他们的搜索引擎当中。
谷歌搜索副总裁Pandu Nayak表示:
对于比较长、会话性比较强的查询,或者在“for”、“to”这样的介词比较重要的语句中,使用了BERT的搜索引擎能够更好地理解查询中单词的上下文。
比如,当输入“2019 brazil traveler to usa need a visa”(2019年前往美国的巴西旅行者需要签证)这样一条查询语句时,“to”这个词起到了关键的联系作用(巴西人→美国)
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%
BERT未加入前,搜索引擎的算法无法理解这种关联,因此返回了有关前往巴西的美国公民(美国人→巴西)的结果。
而有了BERT,搜索引擎就能把握住其中细微而关键的差别了。
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%

TinyBERT

而针对BERT这样的庞大模型难以部署到算力、内存有限的设备中的问题,华为诺亚方舟实验室和华中科技大学合作开源了TinyBERT
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%
应用知识蒸馏(knowledge distillation,KD)的方法,大型教师模型BERT中编码的大量知识,能被很好地迁移到小型学生模型TinyBERT中。
研究人员还提出了一种专门用于TinyBERT的两段式学习框架,分别在预训练阶段和特定任务学习阶段进行transformer蒸馏。
这样,就能确保TinyBERT可以获取教师模型在通用和特定任务知识上的经验。
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%
在GLUE基准上,TinyBERT的性能与BERT相当,仅下降了3个百分点。
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%
并且,模型大小不到不到BERT的1/7,推理速度是BERT的9.4倍。
速度×6,模型缩小20倍:用华为&华科的TinyBERT做搜索,性能达BERT 90%

传送门

博客:
https://towardsdatascience.com/tinybert-for-search-10x-faster-and-20x-smaller-than-bert-74cd1b6b5aec
TinyBERT论文:
https://arxiv.org/abs/1909.10351
TinyBERT开源地址:
https://github.com/huawei-noah/Pretrained-Language-Model/tree/master/TinyBERT
作者系网易新闻·网易号“各有态度”签约作者
<pre style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-right: 8px;margin-left: 8px;max-width: 100%;white-space: normal;color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-align: center;widows: 1;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br  /></section><section style="margin-right: 8px;margin-left: 8px;max-width: 100%;white-space: normal;color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-align: center;widows: 1;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong>完<strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong></span></strong></span></strong></section><section style="max-width: 100%;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-align: center;widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-right: 8px;margin-bottom: 15px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></section><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">“12306”的架构到底有多牛逼?</span></p><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">中国程序员34岁生日当天在美国遭抢笔记本电脑,追击歹徒被拖行后身亡,为什么会发生此类事件?</span></p><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">阿里如何抗住90秒100亿?看这篇你就明白了!</span></p><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(87, 107, 149);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">60个Chrome神器插件大收集:助你快速成为老司机,一键分析网站技术栈</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></p><p style="margin-right: 8px;margin-bottom: 5px;margin-left: 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 1.75em;letter-spacing: 0px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">深度学习必懂的13种概率分布</span></p></section></section></section></section></section></section></section></section>

本篇文章来源于: 深度学习这件小事

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享