官网首页 › 研究观点 › 观点正文

Hugging Face的BERT源码框架图文详解

发布于 2020-11-30

<section data-tools="135编辑器" data-id="98856" data-mpa-powered-by="yiban.io"><section><section style="margin: 10px auto;text-align: center;"><section style="display: flex;flex-direction: column;justify-content: center;align-items: center;"><section data-tools="135编辑器" data-id="95952" style="text-align: justify;"><section style="margin-right: auto;margin-left: auto;"><section style="padding: 2px 10px;display: flex;justify-content: flex-end;"><section style="margin-right: 10px;width: 50px;height: 50px;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);border-radius: 100%;background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZ1F7icKAWGqxkM3zy2Dzmia9UwuBJYpMp9dP4XgNlXTCFiazruuTYOU0cw/640?wx_fmt=jpeg&quot;);background-position: center center;background-size: cover;background-repeat: no-repeat;background-attachment: initial;background-origin: initial;background-clip: initial;"><img data-ratio="1" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZ1F7icKAWGqxkM3zy2Dzmia9UwuBJYpMp9dP4XgNlXTCFiazruuTYOU0cw/640?wx_fmt=jpeg" data-type="jpeg" data-w="750" data-width="100%" style="width: 100%;border-radius: 50%;opacity: 0;" title="" /></section></section><section style="margin: 5px;border-width: initial;border-style: none;border-color: initial;"><section style="margin-left: 454.391px;width: 0px;border-bottom: 0.6em solid rgb(0, 0, 0);border-top-color: rgb(0, 0, 0);height: 10px;color: inherit;overflow: hidden;border-left: 0.7em solid transparent !important;border-right: 0.7em solid transparent !important;"><br /></section><section style="margin-top: -8px;margin-left: 454.391px;width: 0px;border-bottom: 0.6em solid rgb(254, 254, 254);border-top-color: rgb(254, 254, 254);height: 10px;color: inherit;float: left;overflow: hidden;border-left: 0.7em solid transparent !important;border-right: 0.7em solid transparent !important;"><br /></section><section style="margin-right: auto;margin-bottom: -2px;margin-left: auto;text-align: center;border-width: 2px;border-style: solid;border-color: rgb(0, 0, 0);border-radius: 5px;"><section data-autoskip="1" style="padding: 1em;font-size: 14px;text-align: justify;letter-spacing: 1.5px;line-height: 1.75em;color: rgb(62, 62, 62);"><p hm_fix="374:371" style="text-align: left;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><span style="color: rgb(0, 0, 0);text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 0, 0);font-size: 15px;outline: none 0px !important;">市面上没看到</span><strong style="color: rgb(0, 0, 0);text-align: justify;max-inline-size: 100%;cursor: text;font-size: medium;caret-color: rgb(0, 0, 0);font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">图文结合的huggingface&nbsp;transformers框架bert源码解说</span></strong><span style="color: rgb(0, 0, 0);text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 0, 0);font-size: 15px;outline: none 0px !important;">,笔者也在看源码,所以顺手自己做个笔记。</span></span></p></section></section></section></section></section><section data-tools="135编辑器" data-id="101126"><section style="margin: 10px auto;text-align: center;"><section style="background: rgb(0, 0, 0);padding: 1em;margin-left: 8px;margin-right: 8px;"><section style="display: flex;justify-content: space-between;align-items: center;" hm_fix="353:417"><section data-role="outer" label="Powered by 135editor.com"><section data-tools="135编辑器" data-id="101126"><section style="margin: 10px auto;text-align: center;"><section style="background: #000;padding: 1em;"><section style="display: flex;justify-content: space-between;align-items: center;" hm_fix="353:417"><section style="width: 30px;"><img data-ratio="0.6585365853658537" data-src="https://mmbiz.qpic.cn/mmbiz_png/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZv49X4QS9RpUCDgXXicZZhS2N52yzyzOkEMYVkVicIibwEsPE4XELRHia2Q/640?wx_fmt=png" data-type="png" data-w="41" data-width="100%" style="width: 100%;display: block;" /></section><section data-brushtype="text" style="font-size: 16px;letter-spacing: 1.5px;padding: 4px 1em;color: #fff;font-weight: bold;background: #007aaa;"><p style="line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><span style="font-size: 17px;">应用范围</span></p></section></section><section data-autoskip="1" style="text-align: justify;line-height: 1.75em;letter-spacing: 1.5px;font-size: 14px;color: rgb(255, 255, 255);margin-top: 2em;margin-bottom: 1em;"><p style="line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><span style="font-size: 15px;">BERT可应用在搜索引擎,智能客服,聊天机器人,语音助手,推荐系统,知识图谱的NLP下游任务如:阅读理解,知识抽取,文本分类,语言生成</span></p></section></section></section></section></section></section></section><section data-autoskip="1" style="text-align: justify;line-height: 1.75em;letter-spacing: 1.5px;font-size: 14px;color: rgb(255, 255, 255);margin-top: 2em;margin-bottom: 1em;"><section style="line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><br /></section></section></section></section><p style="font-weight: bold;color: rgb(0, 122, 170);font-size: 60px;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">1</span></p><section data-width="15%" style="margin-top: -35px;width: 15%;height: 1px;background: rgb(0, 122, 170);overflow: hidden;"><br /></section></section><section data-brushtype="text" hm_fix="318:337" style="padding: 8px;font-size: 16px;font-weight: bold;letter-spacing: 1.5px;background: rgb(255, 255, 255);transform: rotate(0deg);"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">文章内容</span></section></section></section></section><section data-role="paragraph"><section style="margin-right: 8px;margin-left: 8px;max-inline-size: 100%;cursor: text;line-height: 1.75em;outline: none 0px !important;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: 15px;outline: none 0px !important;">从宏观到微观讲解huggingface transformers框架的bert源码</span></strong><span style="max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: 15px;">,部分工程细节如降低训练内存使用的util,一些参数可选项都不会涉及,只讲主干。</span></span></section></section><section data-tools="135编辑器" data-id="98856"><section><section style="margin: 10px auto;text-align: center;"><section style="display: flex;flex-direction: column;justify-content: center;align-items: center;"><section data-original-title="" title="" data-num="2" style="font-weight: bold;color: rgb(0, 122, 170);font-size: 60px;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">2</span></section><section data-width="15%" style="margin-top: -35px;width: 15%;height: 1px;background: rgb(0, 122, 170);overflow: hidden;"><br /></section></section><section data-brushtype="text" hm_fix="341:328" style="padding: 8px;font-size: 16px;font-weight: bold;letter-spacing: 1.5px;background: rgb(255, 255, 255);transform: rotate(0deg);"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">BERT概要</span></section></section></section></section><section data-role="paragraph"><section style="margin: 1.4em 8px;caret-color: rgb(255, 0, 0);max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><span style="max-inline-size: 100%;cursor: text;color: rgb(0, 122, 170);"><strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;outline: none 0px !important;">BERT是Google在2018年10月提出来的一个文本语言预训练方法</span></strong></span><span style="max-inline-size: 100%;cursor: text;font-size: 15px;">,他是基于Transformer的实现。在bert之前还有诸如Glov、Word2vec、ULMFiT、Elmo、GPT等预训练方法,但是bert在各种任务上的表现有着显著提升,微调即可在一些任务上轻松达到state of the art,对工业界NLP落地有着极大的推动作用。</span></span></section><section style="margin: 1.4em 8px;caret-color: rgb(255, 0, 0);max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">深度学习非常需要预训练模型。有了好的预训练模型,大家可以更专注于自己的实验,而不是先去得到语义表征再去做下游任务。</span></section><section style="margin: 1.4em 8px;caret-color: rgb(255, 0, 0);max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">废话太多了……开撕</span></section><section data-tools="135编辑器" data-id="98856"><section><section style="margin: 10px auto;text-align: center;"><section hm_fix="339:315" style="display: flex;flex-direction: column;justify-content: center;align-items: center;"><section data-original-title="" title="" data-num="3" style="font-weight: bold;color: rgb(0, 122, 170);font-size: 60px;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">3</span></section><section data-width="15%" style="margin-top: -35px;width: 15%;height: 1px;background: rgb(0, 122, 170);overflow: hidden;"><br /></section></section><section data-brushtype="text" style="padding: 8px;font-size: 16px;font-weight: bold;letter-spacing: 1.5px;background: rgb(255, 255, 255);transform: rotate(0deg);"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">BERT的全貌</span></section></section></section></section><figure data-size="small" style="margin-top: 1.4em;margin-bottom: 1.4em;max-inline-size: 100%;clear: both;color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="777" data-backw="578" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZjnEWIdoogDibQuJepFyiaxVER8CJ6FicZIStYqFq4TLkYKaPNsMsw7zdA/640?wx_fmt=jpeg" data-type="jpeg" width="1303" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 578px;display: block;cursor: zoom-in;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="1.3444444444444446" data-w="720" /><section style="margin-top: 0.66667em;margin-right: 8px;margin-left: 8px;padding-right: 1em;padding-left: 1em;text-align: center;max-inline-size: 100%;cursor: text;font-size: 0.9em;color: rgb(153, 153, 153);line-height: 1.75em;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">transformer的encoder,图片来自transformer原文</span></section></figure><section style="margin: 1.4em 8px;max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;outline: none 0px !important;">红色方框就是bert的主要架构</span></strong><span style="max-inline-size: 100%;cursor: text;font-size: 15px;">,我们的输入就是句子,输出的是这个句子对应的语义表示的向量。对于一个文本句子,我们需要把句子分词,然后再转换成向量输入到bert,bert的分词基本原理是byte pair encoding(BPE),得到的token叫wordpiece,每个token对应一个id,一个句子是一个二维矩阵(vocab_size,sentence_len)。</span></span></section><section style="margin: 1.4em 8px;padding-left: 1em;max-inline-size: 100%;cursor: text;color: rgb(100, 100, 100);border-left: 3px solid rgb(211, 211, 211);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 12px;color: rgb(127, 127, 127);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">Input:句子的wordpiece的id ,Output:句子的语义表征向量</span></section><section style="margin: 1.4em 8px;max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;outline: none 0px !important;">下面看代码的架构。</span></strong></span></section><figure data-size="normal" style="margin-top: 1.4em;margin-bottom: 1.4em;max-inline-size: 100%;color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="145" data-backw="578" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZc2Jmyeh5VMFa5yGtrDalutyCKnFOicJyFyzgxF5xyBl3PrtMTRGyaVA/640?wx_fmt=jpeg" data-type="jpeg" width="3797" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 100%;display: block;cursor: zoom-in;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="0.2513888888888889" data-w="720" /></figure><section style="margin: 1.4em 8px;max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">初始化有三个块,<strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;">BertEmbeddings</strong>,<strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;">BertEncoder</strong>,<strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;">BertPooler</strong>,向量也是依次往下传递。</span></section><section style="margin-top: 1em;margin-bottom: 1em;max-inline-size: 100%;color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><pre style="padding: 0.88889em;overflow-wrap: normal;max-inline-size: 100%;font-size: 0.9em;word-break: normal;overflow: auto;background: rgb(246, 246, 246);border-radius: 4px;outline: none 0px !important;"><section style="margin-right: 8px;margin-left: 8px;max-inline-size: 100%;cursor: text;line-height: 1.75em;outline: none 0px !important;"><code style="max-inline-size: 100%;border-radius: 0px;font-size: inherit;background-color: inherit;font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 12px;color: rgb(127, 127, 127);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">class BertModel(BertPreTrainedModel): &nbsp; &nbsp;def __init__(self, config): &nbsp; &nbsp; &nbsp; &nbsp;super().__init__(config) &nbsp; &nbsp; &nbsp; &nbsp;self.config = config &nbsp; &nbsp; &nbsp; &nbsp;self.embeddings = BertEmbeddings(config) &nbsp; &nbsp; &nbsp; &nbsp;self.encoder = BertEncoder(config) &nbsp; &nbsp; &nbsp; &nbsp;self.pooler = BertPooler(config) &nbsp; &nbsp; &nbsp; &nbsp;self.init_weights()</span></code></section></pre></section><section style="margin: 1.4em 8px;max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">对应</span></section><figure data-size="small" style="margin-top: 1.4em;margin-bottom: 1.4em;max-inline-size: 100%;clear: both;color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="210" data-backw="186" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZHSvB7P35sicSAcpZ8NKBvCSREYaTWVCu0z4bKAthOczBCQXu6icrRVDQ/640?wx_fmt=jpeg" data-type="jpeg" width="1011" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 100%;display: block;cursor: zoom-in;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="1.1291666666666667" data-w="720" /></figure><section style="margin: 1.4em 8px;max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">BertPooler则是取了CLS token的hidden states做tanh,下面我们一个一个讲解。</span></section><figure data-size="small" style="margin-top: 1.4em;margin-bottom: 1.4em;max-inline-size: 100%;clear: both;color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="76" data-backw="186" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZCFA2ZGfRicibplia2oIUq8NuglT1KBWtxEoYREpiaXQwNzdCk41BJvgjZg/640?wx_fmt=jpeg" data-type="jpeg" height="76" width="186" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 578px;display: block;cursor: zoom-in;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="0.4111111111111111" data-w="720" /><section style="margin-top: 0.66667em;margin-right: 8px;margin-left: 8px;padding-right: 1em;padding-left: 1em;text-align: center;max-inline-size: 100%;cursor: text;font-size: 0.9em;color: rgb(153, 153, 153);line-height: 1.75em;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">pooler</span></section></figure></section><section data-tools="135编辑器" data-id="101124"><section style="margin: 10px auto;text-align: center;"><section style="padding: 1em;border-width: 1px;border-style: solid;border-color: rgb(0, 122, 170);"><section style="display: inline-block;"><section data-brushtype="text" style="font-size: 16px;letter-spacing: 1.5px;padding: 6px 1.5em;color: #fff;font-weight: bold;background: #007aaa;border-radius: 30px;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="caret-color: rgb(0, 122, 170);font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;"><span style="font-size: 17px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">BertEmbeddings</span></strong></span></section></section><section style="margin-right: 1em;margin-left: 1em;"><section data-autoskip="1" style="text-align: justify;line-height: 1.75em;letter-spacing: 1.5px;font-size: 14px;margin-top: 1.5em;"><section style="margin-top: 1em;margin-bottom: 1em;color: rgb(18, 18, 18);font-size: medium;background-color: rgb(255, 255, 255);font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;"><pre style="padding: 0.88889em;font-size: 0.9em;word-break: normal;overflow-wrap: normal;overflow: auto;background: rgb(246, 246, 246);border-radius: 4px;"><code style="border-radius: 0px;font-size: inherit;background-color: inherit;font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace;"><span style="color: rgb(127, 127, 127);font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">class BertEmbeddings(nn.Module):<br /> &nbsp; &nbsp;"""Construct the embeddings from word, position and token_type embeddings.<br /> &nbsp; &nbsp;"""<br /> &nbsp; &nbsp;def __init__(self, config):<br /> &nbsp; &nbsp; &nbsp; &nbsp;super().__init__()<br /> &nbsp; &nbsp; &nbsp; &nbsp;self.word_embeddings = nn.Embedding(config.vocab_size, config.hidden_size, padding_idx=config.pad_token_id)<br /> &nbsp; &nbsp; &nbsp; &nbsp;self.position_embeddings = nn.Embedding(config.max_position_embeddings, config.hidden_size)<br /> &nbsp; &nbsp; &nbsp; &nbsp;self.token_type_embeddings = nn.Embedding(config.type_vocab_size, config.hidden_size)<br /><br /> &nbsp; &nbsp; &nbsp; &nbsp;self.LayerNorm = BertLayerNorm(config.hidden_size, eps=config.layer_norm_eps)<br /> &nbsp; &nbsp; &nbsp; &nbsp;self.dropout = nn.Dropout(config.hidden_dropout_prob)<br /><br /> &nbsp; &nbsp; &nbsp; &nbsp;self.register_buffer("position_ids", torch.arange(config.max_position_embeddings).expand((1, -1)))<br /><br /> &nbsp; &nbsp;def forward(self, input_ids=None, token_type_ids=None, position_ids=None, inputs_embeds=None):<br /> &nbsp; &nbsp; &nbsp; &nbsp;#三个embedding使用了加法<br /> &nbsp; &nbsp; &nbsp; &nbsp;embeddings = inputs_embeds + position_embeddings + token_type_embeddings<br /> &nbsp; &nbsp; &nbsp; &nbsp;embeddings = self.LayerNorm(embeddings)<br /> &nbsp; &nbsp; &nbsp; &nbsp;embeddings = self.dropout(embeddings)<br /> &nbsp; &nbsp; &nbsp; &nbsp;return embeddings</span></code></pre></section><figure data-size="small" style="margin-top: 1.4em;margin-bottom: 1.4em;clear: both;color: rgb(18, 18, 18);font-size: medium;background-color: rgb(255, 255, 255);font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;"><figure data-size="small" style="margin-top: 1.4em;margin-bottom: 1.4em;clear: both;"><img data-ratio="1.4333333333333333" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZLDic8gywvzxQiaLncjBfNGxDYpbMnIJ32tiaYiboCh0PUzkLH9M6OyCNDQ/640?wx_fmt=jpeg" data-type="jpeg" data-w="720" style="margin-right: auto;margin-left: auto;width: 100%;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;" width="952" /></figure><figure data-size="normal" style="margin-top: 2.24em;margin-bottom: 1.4em;"><img data-ratio="0.3486111111111111" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZl9J889Ql7icsHDskfvppDPiaOTY6FOMjPTufHQKrzYyhdx9vJVkPBUFQ/640?wx_fmt=jpeg" data-type="jpeg" data-w="720" style="margin-right: auto;margin-left: auto;width: 100%;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;" width="2532" /></figure><section style="margin: 1.4em 8px;caret-color: rgb(0, 122, 170);max-inline-size: 100%;cursor: text;line-height: 1.75em;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">从上图可以看到,embedding的主要几个embedding,<strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;">为什么需要这些embedding?为什么要用加法?</strong></span></section><section style="margin: 1.4em 8px;caret-color: rgb(0, 122, 170);max-inline-size: 100%;cursor: text;line-height: 1.75em;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">从代码可以看到这里用了三个embedding层(transformer原文的position encoding用的是正余弦函数),最后三个相加。</span></section></figure><section style="margin-top: 1em;margin-bottom: 1em;color: rgb(18, 18, 18);font-size: medium;background-color: rgb(255, 255, 255);font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;"><pre style="padding: 0.88889em;font-size: 0.9em;word-break: normal;overflow-wrap: normal;overflow: auto;background: rgb(246, 246, 246);border-radius: 4px;"><code style="border-radius: 0px;font-size: inherit;background-color: inherit;font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace;"><span style="font-size: 12px;color: rgb(127, 127, 127);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">def forward(self, input_ids=None, token_type_ids=None, position_ids=None, inputs_embeds=None):<br /> &nbsp; &nbsp; &nbsp; &nbsp;#三个embedding使用了加法<br /> &nbsp; &nbsp; &nbsp; &nbsp;embeddings = inputs_embeds + position_embeddings + token_type_embeddings<br /> &nbsp; &nbsp; &nbsp; &nbsp;embeddings = self.LayerNorm(embeddings)<br /> &nbsp; &nbsp; &nbsp; &nbsp;embeddings = self.dropout(embeddings)<br /> &nbsp; &nbsp; &nbsp; &nbsp;return embeddings</span></code></pre></section><section style="margin: 1.4em 8px;background-color: rgb(255, 255, 255);caret-color: rgb(0, 122, 170);max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;outline: none 0px !important;">答</span></strong><span style="max-inline-size: 100%;cursor: text;font-size: 15px;">:在encoder之前,我们需要用一些信号来增加input携带的信息,关于为什么用加法这个回答很不错,概括就是<strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;">从声波频率的不同考虑这个问题</strong>,叠加的这些信息,模型可以根据目标问题用不同的滤波方法对信号滤波,来获得想要的或者显性的特征。</span></span></section><section style="margin: 1.4em 8px;background-color: rgb(255, 255, 255);caret-color: rgb(0, 122, 170);max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;outline: none 0px !important;">三个 embedding 携带了什么信号呢?</span></strong></span></section><section style="margin: 1.4em 8px;background-color: rgb(255, 255, 255);caret-color: rgb(0, 122, 170);max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;color: rgb(0, 122, 170);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;"><strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;outline: none 0px !important;">token embedding:每个token的词表示信息<br style="max-inline-size: 100%;outline: none 0px !important;" />segment embedding:区分句子,bert的NSP<br style="max-inline-size: 100%;outline: none 0px !important;" />position embedding :区分每个词的在句子的位置</span></strong></span></section><p style="margin-right: 8px;margin-left: 8px;background-color: rgb(255, 255, 255);color: rgb(255, 255, 255);caret-color: rgb(0, 122, 170);line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;"><span style="color: rgb(18, 18, 18);font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">下面到encoder部分</span></p><p style="margin-right: 8px;margin-left: 8px;background-color: rgb(255, 255, 255);color: rgb(255, 255, 255);caret-color: rgb(0, 122, 170);line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;"><br /></p></section></section></section></section></section><section data-tools="135编辑器" data-id="101124"><section style="margin: 10px auto;text-align: center;"><section style="padding: 1em;border-width: 1px;border-style: solid;border-color: rgb(0, 122, 170);"><section style="display: inline-block;"><section data-brushtype="text" style="font-size: 16px;letter-spacing: 1.5px;padding: 6px 1.5em;color: #fff;font-weight: bold;background: #007aaa;border-radius: 30px;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="caret-color: rgb(0, 122, 170);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;"><span style="font-size: 17px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">encoder</span></strong></span></section></section><section style="margin-right: 1em;margin-left: 1em;"><figure data-size="small" style="margin-top: 1.4em;margin-bottom: 1.4em;color: rgb(18, 18, 18);font-size: medium;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);text-align: left;max-inline-size: 100%;clear: both;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZ9Xic5ApMPqRdYWE1xm3vnWKqVah4CiaErypyjQZYhdpEf2bVk1ia4aD1g/640?wx_fmt=jpeg" data-type="jpeg" width="600" data-backw="279" data-backh="511" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 100%;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="1.8316666666666668" data-w="600" /></figure><section style="margin: 1.4em 8px;color: rgb(18, 18, 18);font-size: medium;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);text-align: left;max-inline-size: 100%;cursor: text;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;outline: none 0px !important;">红色方框就是bert的encoder</span></strong><span style="max-inline-size: 100%;cursor: text;font-size: 15px;">,对于BertEncoder,我们需要先传入它的超参数,可以看到第一个被用到的就是:</span></span></section><section style="margin-top: 1em;margin-bottom: 1em;color: rgb(18, 18, 18);font-size: medium;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);text-align: left;max-inline-size: 100%;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><pre style="padding: 0.88889em;overflow-wrap: normal;max-inline-size: 100%;font-size: 0.9em;word-break: normal;overflow: auto;background: rgb(246, 246, 246);border-radius: 4px;outline: none 0px !important;"><section style="margin-right: 8px;margin-left: 8px;max-inline-size: 100%;cursor: text;line-height: 1.75em;outline: none 0px !important;"><code style="max-inline-size: 100%;border-radius: 0px;font-size: inherit;background-color: inherit;font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 12px;color: rgb(127, 127, 127);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">class BertEncoder(nn.Module): &nbsp; &nbsp;def __init__(self, config): &nbsp; &nbsp; &nbsp; &nbsp;super().__init__()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.config = config &nbsp; &nbsp; &nbsp; &nbsp;#BertLayer就是一个encoder,这里用nn.ModuleList放进了num_hidden_layers个初始化的encoder &nbsp; &nbsp; &nbsp; &nbsp;self.layer = nn.ModuleList([BertLayer(config) for _ in range(config.num_hidden_layers)])</span></code></section></pre></section><section style="margin: 1.4em 8px;padding-left: 1em;font-size: medium;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);text-align: left;max-inline-size: 100%;cursor: text;color: rgb(100, 100, 100);border-left: 3px solid rgb(211, 211, 211);line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 12px;color: rgb(127, 127, 127);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">config.num_hidden_layer</span></section><section style="margin: 1.4em 8px;color: rgb(18, 18, 18);font-size: medium;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);text-align: left;max-inline-size: 100%;cursor: text;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">这个参数就是图中的N,这里要区别于multi head self attention的h,这里的N只是表示要encode几次,每次encode的输入输出向量形状保持不变,如果N=3,则</span></section><section style="margin: 1.4em 8px;padding-left: 1em;font-size: medium;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);text-align: left;max-inline-size: 100%;cursor: text;color: rgb(100, 100, 100);border-left: 3px solid rgb(211, 211, 211);line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 12px;color: rgb(127, 127, 127);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">hs &gt; encoder&gt; hs &gt; encoder &gt; hs &gt; encoder &gt; hs</span></section><section style="margin: 1.4em 8px;color: rgb(18, 18, 18);font-size: medium;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);text-align: left;max-inline-size: 100%;cursor: text;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">encode了三次,看代码,可以看到有一个ModuleList装了。num_hidden_layers个初始化过的BertLayer,forward就是上面说的hs &gt; encoder的传递过程。</span></section></section></section></section></section><section data-tools="135编辑器" data-id="101124"><section style="margin: 10px auto;text-align: center;"><section style="padding: 1em;border-width: 1px;border-style: solid;border-color: rgb(0, 122, 170);"><section style="display: inline-block;"><section data-brushtype="text" style="font-size: 16px;letter-spacing: 1.5px;padding: 6px 1.5em;color: #fff;font-weight: bold;background: #007aaa;border-radius: 30px;"><span style="font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong style="font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;"><span style="font-size: 17px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">encoder拆解</span></strong></span></section></section><section style="margin-right: 1em;margin-left: 1em;"><section style="margin: 1.4em 8px;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">下面我们要分解encoder的内部结构,看bertlayer里有啥,可以看到有BertAttention,BertIntermediate。</span></section><section style="margin-top: 1em;margin-bottom: 1em;text-align: justify;max-inline-size: 100%;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><pre style="padding: 0.88889em;overflow-wrap: normal;max-inline-size: 100%;font-size: 0.9em;word-break: normal;overflow: auto;background: rgb(246, 246, 246);border-radius: 4px;outline: none 0px !important;"><section style="margin-right: 8px;margin-left: 8px;max-inline-size: 100%;cursor: text;line-height: 1.75em;outline: none 0px !important;"><code style="max-inline-size: 100%;border-radius: 0px;font-size: inherit;background-color: inherit;font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 12px;color: rgb(127, 127, 127);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">class BertLayer(nn.Module): &nbsp; &nbsp;def __init__(self, config): &nbsp; &nbsp; &nbsp; &nbsp;super().__init__() &nbsp; &nbsp; &nbsp; &nbsp;self.attention = BertAttention(config) &nbsp; &nbsp; &nbsp; &nbsp;self.intermediate = BertIntermediate(config)</span></code></section></pre></section><section style="margin: 1.4em 8px;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">分别对应</span></section><figure data-size="small" style="margin-top: 1.4em;margin-bottom: 1.4em;text-align: justify;max-inline-size: 100%;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);clear: both;color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="193" data-backw="186" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZZuosUWSC9myXQQK2WZcKQhwJMUjjYg6oUeF5QKUA6gMkbzYauIDB4w/640?wx_fmt=jpeg" data-type="jpeg" width="1394" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 100%;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="1.0402777777777779" data-w="720" /></figure><h3 style="margin: 1.90909em 8px 1.27273em;font-weight: bold;font-size: 1.1em;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);font-variant-numeric: inherit;font-variant-east-asian: inherit;font-stretch: inherit;clear: left;color: rgb(18, 18, 18);line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">BertAttention(Multi-Head Self-Attention)</span></h3><section style="margin: 1.4em 8px;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">其中的<span style="max-inline-size: 100%;cursor: text;font-weight: 600;">BertAttention</span>就是论文中的<span style="max-inline-size: 100%;cursor: text;font-weight: 600;">Multi-Head Self-Attention</span></span></section><figure data-size="normal" style="margin-top: 1.4em;margin-bottom: 1.4em;text-align: justify;max-inline-size: 100%;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="369" data-backw="532" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZRIWD1sK2icvzoSHIP32F0tAcGmslxzDwnC3cxRpiaUib9wTPXWE9lXoLw/640?wx_fmt=jpeg" data-type="jpeg" width="1930" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 532px;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="0.6930555555555555" data-w="720" /><section style="margin-top: 0.66667em;margin-right: 8px;margin-left: 8px;padding-right: 1em;padding-left: 1em;text-align: center;max-inline-size: 100%;cursor: text;font-size: 0.9em;color: rgb(153, 153, 153);line-height: 1.75em;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;font-size: 12px;outline: none 0px !important;">Self-Attention</span></section></figure><figure data-size="normal" style="margin-top: 2.24em;margin-bottom: 1.4em;text-align: justify;max-inline-size: 100%;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="163" data-backw="532" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZEMG0UoTM6A6hslgqIsLp0xOP0esaoaTuEJ46y9c87sKnLn8upe8WlQ/640?wx_fmt=jpeg" data-type="jpeg" width="2508" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 532px;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="0.30694444444444446" data-w="720" /><section style="margin-top: 0.66667em;margin-right: 8px;margin-left: 8px;padding-right: 1em;padding-left: 1em;text-align: center;max-inline-size: 100%;cursor: text;font-size: 0.9em;color: rgb(153, 153, 153);line-height: 1.75em;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;font-size: 12px;outline: none 0px !important;">Multi-Head Attention</span></section></figure><h3 style="margin: 1.90909em 8px 1.27273em;font-weight: bold;font-size: 1.1em;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);font-variant-numeric: inherit;font-variant-east-asian: inherit;font-stretch: inherit;clear: left;color: rgb(18, 18, 18);line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">BertIntermediate(Position-wise Feed-Forward Networks)</span></h3><section style="margin: 1.4em 8px;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-weight: 600;">BertIntermediate</span>就是论文中的<span style="max-inline-size: 100%;cursor: text;font-weight: 600;">Position-wise Feed-Forward Networks</span></span></section><figure data-size="normal" style="margin-top: 1.4em;margin-bottom: 1.4em;text-align: justify;max-inline-size: 100%;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZTVd4Cjnb32AnZW6ricdiammLibrcnPW3sibEF88FkMtynHRFyCqWQriam0Q/640?wx_fmt=jpeg" data-type="jpeg" width="2376" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 2376px;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="0.3388888888888889" data-w="720" /><section style="margin-top: 0.66667em;margin-right: 8px;margin-left: 8px;padding-right: 1em;padding-left: 1em;text-align: center;max-inline-size: 100%;cursor: text;font-size: 0.9em;color: rgb(153, 153, 153);line-height: 1.75em;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">Position-wise Feed-Forward Networks</span></section></figure><section style="margin: 1.4em 8px;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">我们来看BertAttention</span></section><section style="margin-top: 1em;margin-bottom: 1em;text-align: justify;max-inline-size: 100%;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><pre style="padding: 0.88889em;overflow-wrap: normal;max-inline-size: 100%;font-size: 0.9em;word-break: normal;overflow: auto;background: rgb(246, 246, 246);border-radius: 4px;outline: none 0px !important;"><section style="margin-right: 8px;margin-left: 8px;max-inline-size: 100%;cursor: text;line-height: 1.75em;outline: none 0px !important;"><code style="max-inline-size: 100%;border-radius: 0px;font-size: inherit;background-color: inherit;font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;color: rgb(127, 127, 127);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;font-size: 12px;outline: none 0px !important;">class BertAttention(nn.Module): &nbsp; &nbsp;def __init__(self, config): &nbsp; &nbsp; &nbsp; &nbsp;super().__init__() &nbsp; &nbsp; &nbsp; &nbsp;self.self = BertSelfAttention(config) &nbsp; &nbsp; &nbsp; &nbsp;self.output = BertSelfOutput(config) &nbsp; &nbsp; &nbsp; &nbsp;self.pruned_heads = set()</span></code></section></pre></section><section style="margin: 1.4em 8px;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">其实就是下面这个图了:</span></section><figure data-size="normal" style="margin-top: 1.4em;margin-bottom: 1.4em;text-align: justify;max-inline-size: 100%;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="294" data-backw="532" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZldrMknUaZ6gfVicaImCGty3dRo4ZzjtfwlxHk5Lt6khFGS8GPjnXkqQ/640?wx_fmt=jpeg" data-type="jpeg" width="2032" style="margin-right: auto;margin-left: auto;max-inline-size: 100%;width: 100%;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="0.5527777777777778" data-w="720" /></figure><section style="margin: 1.4em 8px;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">我们主要讲BertSelfAttention,这是bert最主要的特征抽取部分</span></section><section style="margin-top: 1em;margin-bottom: 1em;text-align: justify;max-inline-size: 100%;caret-color: rgb(0, 122, 170);background-color: rgb(255, 255, 255);color: rgb(18, 18, 18);font-size: medium;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><pre style="padding: 0.88889em;overflow-wrap: normal;max-inline-size: 100%;font-size: 0.9em;word-break: normal;overflow: auto;background: rgb(246, 246, 246);border-radius: 4px;outline: none 0px !important;"><section style="margin-right: 8px;margin-left: 8px;max-inline-size: 100%;cursor: text;line-height: 1.75em;outline: none 0px !important;"><code style="max-inline-size: 100%;border-radius: 0px;font-size: inherit;background-color: inherit;font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;color: rgb(127, 127, 127);font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">class BertSelfAttention(nn.Module): &nbsp; &nbsp;def __init__(self, config):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super().__init__() &nbsp; &nbsp; &nbsp; &nbsp;if config.hidden_size % config.num_attention_heads != 0 and not hasattr(config, "embedding_size"): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise ValueError( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"The hidden size (%d) is not a multiple of the number of attention " &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"heads (%d)" % (config.hidden_size, config.num_attention_heads) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) &nbsp; &nbsp; &nbsp; &nbsp;self.num_attention_heads = config.num_attention_heads &nbsp; &nbsp; &nbsp; &nbsp;self.attention_head_size = int(config.hidden_size / config.num_attention_heads) &nbsp; &nbsp; &nbsp; &nbsp;self.all_head_size = self.num_attention_heads * self.attention_head_size &nbsp; &nbsp; &nbsp; &nbsp;self.query = nn.Linear(config.hidden_size, self.all_head_size) &nbsp; &nbsp; &nbsp; &nbsp;self.key = nn.Linear(config.hidden_size, self.all_head_size) &nbsp; &nbsp; &nbsp; &nbsp;self.value = nn.Linear(config.hidden_size, self.all_head_size) &nbsp; &nbsp; &nbsp; &nbsp;self.dropout = nn.Dropout(config.attention_probs_dropout_prob) &nbsp; &nbsp;def transpose_for_scores(self, x): &nbsp; &nbsp; &nbsp; &nbsp;new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.attention_head_size) &nbsp; &nbsp; &nbsp; &nbsp;x = x.view(*new_x_shape) &nbsp; &nbsp; &nbsp; &nbsp;return x.permute(0, 2, 1, 3) &nbsp; &nbsp;def forward( &nbsp; &nbsp; &nbsp; &nbsp;self, &nbsp; &nbsp; &nbsp; &nbsp;hidden_states, &nbsp; &nbsp; &nbsp; &nbsp;attention_mask=None, &nbsp; &nbsp; &nbsp; &nbsp;head_mask=None, &nbsp; &nbsp; &nbsp; &nbsp;encoder_hidden_states=None, &nbsp; &nbsp; &nbsp; &nbsp;encoder_attention_mask=None, &nbsp; &nbsp; &nbsp; &nbsp;output_attentions=False, &nbsp; &nbsp;): &nbsp; &nbsp; &nbsp; &nbsp;mixed_query_layer = self.query(hidden_states) &nbsp; &nbsp; &nbsp; &nbsp;# If this is instantiated as a cross-attention module, the keys &nbsp; &nbsp; &nbsp; &nbsp;# and values come from an encoder; the attention mask needs to be &nbsp; &nbsp; &nbsp; &nbsp;# such that the encoder's padding tokens are not attended to. &nbsp; &nbsp; &nbsp; &nbsp;if encoder_hidden_states is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mixed_key_layer = self.key(encoder_hidden_states) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mixed_value_layer = self.value(encoder_hidden_states) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;attention_mask = encoder_attention_mask &nbsp; &nbsp; &nbsp; &nbsp;else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mixed_key_layer = self.key(hidden_states) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mixed_value_layer = self.value(hidden_states) &nbsp; &nbsp; &nbsp; &nbsp;query_layer = self.transpose_for_scores(mixed_query_layer) &nbsp; &nbsp; &nbsp; &nbsp;key_layer = self.transpose_for_scores(mixed_key_layer) &nbsp; &nbsp; &nbsp; &nbsp;value_layer = self.transpose_for_scores(mixed_value_layer) &nbsp; &nbsp; &nbsp; &nbsp;# Take the dot product between "query" and "key" to get the raw attention scores. &nbsp; &nbsp; &nbsp; &nbsp;attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) &nbsp; &nbsp; &nbsp; &nbsp;attention_scores = attention_scores / math.sqrt(self.attention_head_size) &nbsp; &nbsp; &nbsp; &nbsp;if attention_mask is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Apply the attention mask is (precomputed for all layers in BertModel forward() function) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;attention_scores = attention_scores + attention_mask &nbsp; &nbsp; &nbsp; &nbsp;# Normalize the attention scores to probabilities. &nbsp; &nbsp; &nbsp; &nbsp;attention_probs = nn.Softmax(dim=-1)(attention_scores) &nbsp; &nbsp; &nbsp; &nbsp;# This is actually dropping out entire tokens to attend to, which might &nbsp; &nbsp; &nbsp; &nbsp;# seem a bit unusual, but is taken from the original Transformer paper. &nbsp; &nbsp; &nbsp; &nbsp;attention_probs = self.dropout(attention_probs) &nbsp; &nbsp; &nbsp; &nbsp;# Mask heads if we want to&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if head_mask is not None: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;attention_probs = attention_probs * head_mask &nbsp; &nbsp; &nbsp; &nbsp;context_layer = torch.matmul(attention_probs, value_layer) &nbsp; &nbsp; &nbsp; &nbsp;context_layer = context_layer.permute(0, 2, 1, 3).contiguous() &nbsp; &nbsp; &nbsp; &nbsp;new_context_layer_shape = context_layer.size()[:-2] + (self.all_head_size,) &nbsp; &nbsp; &nbsp; &nbsp;context_layer = context_layer.view(*new_context_layer_shape) &nbsp; &nbsp; &nbsp; &nbsp;outputs = (context_layer, attention_probs) if output_attentions else (context_layer,) &nbsp; &nbsp; &nbsp; &nbsp;return outputs</span></code></section></pre></section><section style="margin: 1.4em 8px;text-align: justify;max-inline-size: 100%;cursor: text;caret-color: rgb(0, 122, 170);color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;">讲道理,就是论文的公式,你们细品,在实现的时候特别要注意一下<strong style="max-inline-size: 100%;cursor: text;outline: none 0px !important;">shape变化的细节</strong>,permute,transpose,matmul,Softmax的位置要验证清楚,如果不清楚可以看这篇译制文。</span></section></section></section></section></section><section data-role="paragraph"><section style="margin: 1.4em 8px;max-inline-size: 100%;cursor: text;color: rgb(18, 18, 18);font-size: medium;line-height: 1.75em;font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><span style="max-inline-size: 100%;cursor: text;font-size: 15px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;outline: none 0px !important;"><span style="color: rgb(18, 18, 18);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;font-size: 15px;">最后</span>,我们再看一次架构图作为总结吧。</span></section><figure data-size="normal" style="margin-top: 1.4em;margin-bottom: 1.4em;max-inline-size: 100%;color: rgb(18, 18, 18);font-size: medium;background-color: rgb(255, 255, 255);font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif;outline: none 0px !important;"><img data-backh="145" data-backw="578" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/AJkYaTAujIxnncYia9efBib5j4ia3yRX2WZc2Jmyeh5VMFa5yGtrDalutyCKnFOicJyFyzgxF5xyBl3PrtMTRGyaVA/640?wx_fmt=jpeg" data-type="jpeg" width="3797" style="max-inline-size: 100%;width: 100%;display: block;cursor: zoom-in;background-color: transparent;animation: 0.5s ease-in 0s 1 normal none running fxRichTextFadeIn;box-sizing: border-box !important;outline: none 0px !important;" data-ratio="0.2513888888888889" data-w="720" /></figure><section data-role="paragraph" style="max-inline-size: 100%;border-width: 0px;border-style: none;border-color: initial;z-index: 0;outline: none 0px !important;"><section style="margin-right: 8px;margin-left: 8px;text-align: center;line-height: 1.75em;"><br /></section><hr style="border-style: solid;border-right-width: 0px;border-bottom-width: 0px;border-left-width: 0px;border-color: rgba(0, 0, 0, 0.1);transform-origin: 0px 0px;transform: scale(1, 0.5);" /><section style="margin-right: 8px;margin-left: 8px;text-align: center;line-height: 1.75em;"><br /></section><section style="margin-right: 8px;margin-left: 8px;text-align: center;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(0, 0, 0);font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong>#2020,别把生命浪费在PPT上,去创造价值#</strong><br /></span></section><section style="margin-right: 8px;margin-left: 8px;text-align: center;letter-spacing: 0.54px;min-height: 1em;line-height: 1.75em;"><br /></section><section style="margin-right: 8px;margin-left: 8px;text-align: center;line-height: 1.75em;"><img data-type="png" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/AJkYaTAujIx4ElfytiaoGBKLyptvqklwdgJkaI2n0WsBFiaRYwfs1HZ7SAORAKgoNYn4wo5CcusJXAD7XcwZq5Xw/640?wx_fmt=png" style="width: 250px !important;visibility: visible !important;" data-ratio="1.4916666666666667" data-w="1080" /></section><section style="margin-right: 8px;margin-left: 8px;text-align: center;letter-spacing: 0.54px;min-height: 1em;line-height: 1.75em;"><span style="color: rgb(178, 178, 178);font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><br />——&nbsp; END&nbsp; ——<br /><br /></span></section><section style="margin-right: 8px;margin-left: 8px;text-align: center;letter-spacing: 0.54px;min-height: 1em;line-height: 1.75em;"><span style="font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">猜你想再看<br /></span></section><section style="margin-right: 8px;margin-left: 8px;text-indent: 0em;text-align: center;letter-spacing: 0.54px;background-color: rgb(255, 255, 255);line-height: 1.75em;"><br /></section><section style="margin-right: 8px;margin-left: 8px;text-indent: 0em;text-align: left;letter-spacing: 0.54px;background-color: rgb(255, 255, 255);line-height: 1.75em;"><span style="font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong>城市:</strong>|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831649&amp;idx=1&amp;sn=191239418c15b0c6f395c843415da5fd&amp;chksm=8351793eb426f028970ac64031955167bfde997cdc96375bfb3bce25b5076f1458e11be2be27&amp;token=217203186&amp;lang=zh_CN&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">徐州</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831649&amp;idx=4&amp;sn=3a8acfb05820bbd38cd7b15a58e92ec9&amp;chksm=8351793eb426f028dfd96ec6ffc5239a84aaf25b6643813aa6b4a597b9a3215e7ab25e2b4eaf&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">宿迁</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831649&amp;idx=3&amp;sn=1fcdc66fac79d5ba9504bc24314a14f9&amp;chksm=8351793eb426f02829eef9ba1dfcb8c3e91edb05d3c1d34c548b82f618928834f1da5907d2e7&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">淮北</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831649&amp;idx=2&amp;sn=18d65f24543e71ff6bc70eb1cb0a7bde&amp;chksm=8351793eb426f028b98805f0f7555bfc57e3aefe6c76e7ca8df0369ad5dcc4a593a011a9b029&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">唐山</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831649&amp;idx=5&amp;sn=9f550e5d1f6bd3becc92df723adedd61&amp;chksm=8351793eb426f0289523b13ecd4fcb3d227a3cb259a09e504d388ace5a5eb25ece3aecfe8d46&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">天津</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831491&amp;idx=4&amp;sn=4c4fb5ce9d25c1fe6012d719c5878c82&amp;chksm=8351799cb426f08a8a6b9f35c8db84ceac56401b6da4a6429d1bc5e3339251db372a5045c242&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">深汕</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831603&amp;idx=5&amp;sn=c2d8bae147d4a2d86ec992c50c39ddad&amp;chksm=8351796cb426f07a50b1acbe92593aabba12c3fb95e9966d93ecad99918dbcf654b9ce767405&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">哈尔滨</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831491&amp;idx=3&amp;sn=1e787f725d62f227dbc6c33d0b9ea3eb&amp;chksm=8351799cb426f08accd8896c160b37e7a4436d21e1fbf2cc596763115db5ef6e04dc914ce478&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">兰州</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831603&amp;idx=4&amp;sn=048088acd651254d77cc238d71b1af04&amp;chksm=8351796cb426f07a7c36e33460f244db68a856502151c73820b314470b7e24412b9a8b6dcd5b&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">常德</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831491&amp;idx=2&amp;sn=b6295a149a38a026e886d81014b568f3&amp;chksm=8351799cb426f08a8a8d7adbd8721956fd91c59be8bd1ec35d4f9963a11e7e67bee72cd49405&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">南宁</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831603&amp;idx=8&amp;sn=0efaff09c26e604d7aa5c29376c129ad&amp;chksm=8351796cb426f07abb37dd11a0cfe941868be2b543feebf288460418759254327292658947ae&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">桂林</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831603&amp;idx=3&amp;sn=12f08ffb076544edcbc24e91bc3d87d7&amp;chksm=8351796cb426f07a1cd4530a9033d462d4732c500dcdda723bdc3a87f4d55f1c62f657a4cb77&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">柳州</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831603&amp;idx=6&amp;sn=0a89c4ab17039cec25754aa111b87308&amp;chksm=8351796cb426f07a6fdc14a1c0e877928f2fa46798a8b592b2b2023ad1c65081bbc80b2fafad&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">蚌埠</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831603&amp;idx=7&amp;sn=1bc72e377ffc3b621c2886d68c437515&amp;chksm=8351796cb426f07adc8c50a3e1409197892c508ad9c27a6b52f8e4dde12740baf5674f5a7fa6&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">合肥</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831491&amp;idx=1&amp;sn=10e3d481f7fd0ff46dafe2648cd3ddfb&amp;chksm=8351799cb426f08a5118705d6b85487fa97dd013a2aee842be719ee13086aab8dee370b54185&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">南京</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831603&amp;idx=1&amp;sn=9ffda0c7d9667d6700966a8bab12eccf&amp;chksm=8351796cb426f07a4e94a2471b2d59264ed4460e0567c63095fad9c5704541a546d7bc23584a&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">上海临港</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831065&amp;idx=1&amp;sn=2ffb60d98d02ebcab655c55d34d0acbf&amp;chksm=83517746b426fe509cc721e0e3acf05fd1f60b8695dc3b66c08c0a7aa917e7471b013b02c2c2&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">太原</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830804&amp;idx=1&amp;sn=f571d8218b897b21571a6b2f497d3a1b&amp;chksm=8351744bb426fd5d509591673f4082563a02342283f53dfda255ad33304e6746d6448051b9ea&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">温州</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830454&amp;idx=1&amp;sn=4e7c3289190e2a69c5fb20f2ae8a6d94&amp;chksm=835175e9b426fcffa5f65cafe5274b1c296caf1a51e13ef4c7920c5a18847bef3c7b13c2bfd0&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">昆明</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830295&amp;idx=1&amp;sn=f680ffa2ddf52783e9a7730971520d48&amp;chksm=83517248b426fb5e0efc1e5c62d15b173e4d23ab7aafaab82c6ce861e98d96271fe5dc7db798&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">嘉兴</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830278&amp;idx=1&amp;sn=8441ac780477cd9def8d4e9d7866f143&amp;chksm=83517259b426fb4f93ff50f1c07d7b830f16ec4d1e0c23f64ef15a78a056ae301274ffbb8ec3&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">重庆</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830196&amp;idx=1&amp;sn=7a599cf338734c1d41c6fa16005b1641&amp;chksm=835172ebb426fbfdda6873adbeddc124ae670de675a7c36d11977106150a1d924572ea70f299&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">成都</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830221&amp;idx=1&amp;sn=f5994dbfe9fa00ffce4b5be1a69f3244&amp;chksm=83517292b426fb8419e771cf48ea94de01fc7dc909b4e8c2759c6423b8be6080bc5bd1c248cc&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">深圳</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649829995&amp;idx=1&amp;sn=cd78421e4f2e724f552f8eefc65e0236&amp;chksm=835173b4b426faa26aed46ed566bc1f8ffa11498370fed14fa9d0690795a3eff5a186c63a03d&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">上海</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649829512&amp;idx=1&amp;sn=421f2b766d75009082de8fc1e81cd8d2&amp;chksm=83517157b426f84197c93fd852a3e3f93019c9d81e099784ad0aa09f326ca9dd33a0edb7cfd7&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">珠三角</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649829480&amp;idx=1&amp;sn=c77baf137c109f1017170a708548d37b&amp;chksm=835171b7b426f8a1d942708988b8f4bd875366f5928d1db99470ccad6958aa0c8cd47b292c23&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">雄安新区</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=401964235&amp;idx=1&amp;sn=8839375241e6a7e69f49aec108ca1ad6&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">无锡</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=203026648&amp;idx=1&amp;sn=466dca69ff3eb746e2e5c21831d57f0f&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">北京</a>&nbsp;|&nbsp;<br /></span></section><section style="margin-right: 8px;margin-left: 8px;text-indent: 0em;text-align: left;letter-spacing: 0.54px;background-color: rgb(255, 255, 255);line-height: 1.75em;"><span style="font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong>热点:</strong>|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzIxMTUxMzE2Mw==&amp;mid=2247485505&amp;idx=1&amp;sn=90a4482d0ec14f8a4d441f77c653806c&amp;chksm=97557265a022fb7385559a6648c54242c721582b3811ad664360ea1e6f562f0f62264852c20c&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" hasload="1" data-itemshowtype="0" style="cursor: pointer;">深圳土拍</a><strong>&nbsp;</strong>|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831957&amp;idx=1&amp;sn=a889b497d60264ef90aea585e4d4556f&amp;chksm=83517bcab426f2dc5e2b3778bc8e8834b453c4eac98c490ebeb525c4a5b01093fe602418b8fb&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">美国地产</a>&nbsp;|&nbsp;</span><a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831073&amp;idx=1&amp;sn=b9e813dced68e2e1397bd2cc2b6d05c7&amp;chksm=8351777eb426fe68993e996d9312897701ad0e93a7703c5d719dc20ebf09a1b0c6715f502e4d&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;font-size: 12px;text-decoration: underline;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">长租公寓</a><span style="font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;">&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830578&amp;idx=1&amp;sn=73f5149b299d5ce19a433c365eedee83&amp;chksm=8351756db426fc7b173558a1765830766ec5f7fb387e652250f10a106ef6490821f809722cfb&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">中美贸易战</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830503&amp;idx=1&amp;sn=2fae9eec64a6ed71f0c4ee9e4e9bafe7&amp;chksm=835175b8b426fcaec41bb2a484ef939ac78474c77acde2d96a6f7f8b61a0f4ade630c13e7f90&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">粤港澳</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830398&amp;idx=1&amp;sn=ffd40b9cb7730bd712d8f053280d6fa9&amp;chksm=83517221b426fb37e97b45db61547227724f5a09e6a2a0484a55475066ac41fc1b0165954d0d&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">万科商业</a>&nbsp;|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830360&amp;idx=1&amp;sn=3cea24af7ebf848fde74c52538f111e5&amp;chksm=83517207b426fb11a1ae1c301117e9626ba09012aa9d8b4d8f722e45423ecb7e63987f50bec5&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">2018年走势</a>&nbsp;|<br /></span></section><section style="margin-right: 8px;margin-left: 8px;text-indent: 0em;text-align: left;letter-spacing: 0.54px;background-color: rgb(255, 255, 255);line-height: 1.75em;"><span style="font-size: 12px;font-family: 微软雅黑, &quot;Microsoft YaHei&quot;;"><strong>专业:</strong>|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831987&amp;idx=1&amp;sn=8553537eb64de48a9b7eb16564fe8988&amp;chksm=83517becb426f2fa0c63ef59ace65085334609b45795331170f0edb7f455d154c06d53aa85f7&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">穿越熊市</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831371&amp;idx=1&amp;sn=9b15fe316680aabc64e2b7c37e5767d8&amp;chksm=83517614b426ff0229de8ab490c6a7fb33f9e7de34bae145abd3565818c5965c1eca5a4b0029&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">地产研报</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831103&amp;idx=1&amp;sn=6ae2c7ba214abd19c76ed907399b77b7&amp;chksm=83517760b426fe767422812a59d53a334c00664a2cc146d59a076bc0e59a062f518c7df00861&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">投资</a>&nbsp;|<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649831043&amp;idx=1&amp;sn=dcae4bb0d9c69a6aefeb48f7447dff10&amp;chksm=8351775cb426fe4a32f43b1fe3a11ebffb7d381291adc0746d6d0ed9f3efb882617e0407aa10&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;">&nbsp;思考</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830765&amp;idx=1&amp;sn=d21dbefeb5e1aea1599bcec8fae7a7d4&amp;chksm=835174b2b426fda4b66776ffb3da368f2b71e44233e17581d647b246e52aa53255140e3adac0&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">客研</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830698&amp;idx=1&amp;sn=27ffa7f097500e9e7fb23f99fdce51ef&amp;chksm=835174f5b426fde3854d2eec6dc6fd1c0c182faa0218d1b0ce833e50c860c23b062b88f914e0&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">高周转</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830656&amp;idx=1&amp;sn=dc61474f9d09133fcce2934476ea10c1&amp;chksm=835174dfb426fdc9575a9a316aef5c25d051f574d0cd96ae3d1cda189bca7c7b0b7447e403e4&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">房价预测</a>&nbsp;|&nbsp;<a href="http://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830457&amp;idx=1&amp;sn=37a5c454028fb849c2c5fce3b3575756&amp;chksm=835175e6b426fcf07c9d8fdee249e33edf0ae7f5684be2290c3145624ad430d880a10a654ebd&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">2018房地产报告</a>&nbsp;|&nbsp;&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649830201&amp;idx=1&amp;sn=1816623486b879233513443f1df6ec59&amp;chksm=835172e6b426fbf07aa0f023c3ff79ca1cf11606fc2f2b66cfe773222e3fa1df7f92affa90b6&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">远郊定位&nbsp;</a>|&nbsp;<a href="https://mp.weixin.qq.com/s?__biz=MzAxMDEzOTgyNA==&amp;mid=2649829710&amp;idx=1&amp;sn=58d632f4b07dc0796127ef5df605bb36&amp;chksm=83517091b426f9871fa25aed40ade8d757557347ecf505d8b9510645b5adcc6d703caed90b31&amp;scene=21#wechat_redirect" target="_blank" data-linktype="2" style="cursor: pointer;outline: currentcolor none 0px;">时势蓝图</a>&nbsp;|</span></section></section><p><br /></p></section><p><br /></p>

← 返回观点全库