<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title></title>
 <link href="http://daiwk.github.io/atom.xml" rel="self"/>
 <link href="http://daiwk.github.io"/>
 <updated>2024-03-10T13:57:55+00:00</updated>
 <author>
   <name>daiwk</name>
   <email>daiwk#foxmail.com</email>
 </author>

 
 <entry>
   <title>video caption</title>
   <link href="http://daiwk.github.io/posts/video-video-caption.html"/>
   <updated>2019-10-17T00:00:00+00:00</updated>
   <id>/posts/video-video-caption</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/dlFpxfUywYFNlLglPMxc-g&quot;&gt;CVPR 2019视频描述（video caption）相关论文总结&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/sinat_35177634/article/details/89601950&quot;&gt;https://blog.csdn.net/sinat_35177634/article/details/89601950&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;前几年的相关论文&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/sinat_35177634/article/details/88568491&quot;&gt;https://blog.csdn.net/sinat_35177634/article/details/88568491&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>cortex</title>
   <link href="http://daiwk.github.io/posts/platform-cortex.html"/>
   <updated>2019-10-17T00:00:00+00:00</updated>
   <id>/posts/platform-cortex</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/f4ZVo0i3Kt89c2iXPHiVzg&quot;&gt;机器学习算法部署平台Cortex更新，支持TensorFlow、Pytorch等主流框架&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>numnet plus</title>
   <link href="http://daiwk.github.io/posts/nlp-numnet-plus.html"/>
   <updated>2019-10-17T00:00:00+00:00</updated>
   <id>/posts/nlp-numnet-plus</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/3DPNC2bwmg8veH-XtVAGGA&quot;&gt;腾讯提出NumNet+模型，超越谷歌登DROP排行榜第一名 | 微信AI专访&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>hichnet</title>
   <link href="http://daiwk.github.io/posts/dl-hichnet.html"/>
   <updated>2019-10-17T00:00:00+00:00</updated>
   <id>/posts/dl-hichnet</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/jnyuE0hOZZV4To1AdmyL6Q&quot;&gt;“玩转标签，发现层次的力量！”：跨模态哈希方法研究&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://xuemengsong.github.io/SIGIR2019_HiCHNet.pdf&quot;&gt;Supervised Hierarchical Cross-Modal Hashing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;作者希望利用层次标签信息来监督跨模态哈希映射的过程，使得到的哈希码更多地保留数据在原始空间中的相似性关系。具体地，作者提出了一个新颖的有监督层次跨模态哈希模型 HiCHNet，统一了层次判别性学习和正则化跨模态哈希两个主要过程，&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>youtube multitask</title>
   <link href="http://daiwk.github.io/posts/dl-youtube-multitask.html"/>
   <updated>2019-10-13T00:00:00+00:00</updated>
   <id>/posts/dl-youtube-multitask</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;recsys2019上&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/B109cC_hf3XXsKRAw-0_xA&quot;&gt;YouTube视频推荐系统为什么那么强？看了这篇文章你就知道了&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=3346997&quot;&gt;Recommending what video to watch next: a multitask ranking system&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;..要钱，不要钱的在这里：&lt;a href=&quot;https://daiwk.github.io/assets/youtube-multitask.pdf&quot;&gt;https://daiwk.github.io/assets/youtube-multitask.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/82584437&quot;&gt;https://zhuanlan.zhihu.com/p/82584437&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://medium.com/vantageai/how-youtube-is-recommending-your-next-video-7e5f1a6bd6d9&quot;&gt;https://medium.com/vantageai/how-youtube-is-recommending-your-next-video-7e5f1a6bd6d9&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ngboost</title>
   <link href="http://daiwk.github.io/posts/dl-ngboost.html"/>
   <updated>2019-10-13T00:00:00+00:00</updated>
   <id>/posts/dl-ngboost</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/U59PEPSJlhqUMOjMnqzlgg&quot;&gt;斯坦福吴恩达团队提出NGBoost：用于概率预测的自然梯度提升&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1910.03225v1.pdf&quot;&gt;NGBoost: Natural Gradient Boosting for Probabilistic Prediction&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>MLIR</title>
   <link href="http://daiwk.github.io/posts/platform-mlir.html"/>
   <updated>2019-10-12T00:00:00+00:00</updated>
   <id>/posts/platform-mlir</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/3vHeHh8iwKxeymBr8yXGCQ&quot;&gt;MLIR：借助开源基础架构加快 AI 发展&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://medium.com/tensorflow/mlir-a-new-intermediate-representation-and-compiler-framework-beba999ed18d&quot;&gt;MLIR: A new intermediate representation and compiler framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/mlir&quot;&gt;https://github.com/tensorflow/mlir&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MLIR，这是一款开源的机器学习编译器架构，它可解决软硬件日益碎片化所导致的复杂性，降低 AI 应用的构建难度。该工具提供全新基础架构，其设计理念是让用户在任意类型的硬件上统一地表示和执行机器学习模型。&lt;/p&gt;

&lt;p&gt;借助 MLIR，AI 将助力研究人员训练和部署更大规模的模型，提升应用于不同硬件时的一致性、速度和简洁性，从而实现更快发展。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>nlp+gnn</title>
   <link href="http://daiwk.github.io/posts/nlp-nlp-gnn.html"/>
   <updated>2019-10-12T00:00:00+00:00</updated>
   <id>/posts/nlp-nlp-gnn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#hgat&quot;&gt;HGAT&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h3 id=&quot;hgat&quot;&gt;HGAT&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1903.07293&quot;&gt;Heterogeneous Graph Attention Network&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://shichuan.org/doc/74.pdf&quot;&gt;Heterogeneous Graph Attention Networks for Semi-supervised Short Text Classification&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是北京邮电大学和南洋理工发表于 EMNLP 2019 的工作，文章创新性地将短文本分类问题建模为异质图（多种类型的节点和边）并提出一种端到端的异质图神经网络。短文本分类往往会遇到稀疏性问题（文本过短，无法提供足够的信息）。本文通过构图可以可以的丰富短文本之间的联系，进而较好的解决稀疏性问题。&lt;/p&gt;

&lt;p&gt;另外，本文创新地提出一种异质图神经网络 HGAT。HGAT 分别从节点级别和类型级别聚合信息来更新节点表示。最后本文在 6 个数据集上做了大量的实验来验证 HGAT 的优越性。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>m4</title>
   <link href="http://daiwk.github.io/posts/nlp-m4.html"/>
   <updated>2019-10-12T00:00:00+00:00</updated>
   <id>/posts/nlp-m4</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/zsudXELPj3W49otDh8nwsA&quot;&gt;500亿参数，支持103种语言：谷歌推出「全球文字翻译」模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1907.05019&quot;&gt;Massively Multilingual Neural Machine Translation in the Wild: Findings and Challenges&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在这篇论文和后续几篇论文中，谷歌的研究者们在超过 250 亿的句子对上训练了一个 NMT 模型，这些句子是 100 多种语言与英语的互译，参数量超过 500 亿。他们得到了一种大规模多语言、大规模神经机器翻译方法 M4，在数据丰富和匮乏的语言中都实现了显著的性能提升，可以轻松适应单个领域/语言，同时能够有效应用于跨语言下游迁移任务。&lt;/p&gt;

&lt;p&gt;EMNLP2019上，&lt;a href=&quot;https://arxiv.org/abs/1909.02197&quot;&gt;Investigating Multilingual NMT Representations at Scale&lt;/a&gt;，谷歌的研究者比较了多语言模型在多种语言中的表征能力。他们发现，多语言模型无需外部限制就能学习在语言学上相似的语言的共享表征，验证了长期以来利用这些相似性的直觉和实验结果。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.00437&quot;&gt;Evaluating the Cross-Lingual Effectiveness of Massively Multilingual Neural Machine Translation&lt;/a&gt;中，研究者进一步证明了这些学习到的表征在下游任务中跨语言迁移的有效性。&lt;/p&gt;

&lt;p&gt;构建大规模神经网络&lt;/p&gt;

&lt;p&gt;在增加数据匮乏的语种数量之后，数据丰富的语种翻译质量开始下降。这种下降在多任务设置中可以被观察到，由任务间的竞争和迁移的单向性引起（即从数据丰富的语言到数据匮乏的语言）。研究人员探索了能够更好地学习和实现能力控制的算法，以此来解决这种负迁移问题。在此过程中，他们还通过增加神经网络模型的参数量来提高其表征能力，以此来提高数据丰富语言的翻译质量。&lt;/p&gt;

&lt;p&gt;提高神经网络的能力还有其他几种方法，包括添加层数、增加隐藏表征的宽度等。为了训练更深的翻译模型，研究者利用 GPipe 来训练 128 层、参数超过 60 亿的 Transformer。模型能力的提高使得所有语言的翻译质量都得到了显著提升，BLEU 分数平均提高了 5 分。他们还研究了深度网络的其他性质，包括深度-宽度权衡、可训练性难题以及将 transformer 扩展到 1500 多层、840 亿参数的设计选择等。&lt;/p&gt;

&lt;p&gt;尽管扩展深度是提高模型能力的一种方法，探索能够利用问题多任务特性的架构也是一种非常可行的补充方法。研究者通过用稀疏门控专家混合层（sparsely-gated mixture of experts）替代原始的前馈层修改 transformer 的架构，显著提高了模型能力，使得我们可以成功地训练和传递 500 亿参数，从而进一步提高了翻译质量。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>RSGAN</title>
   <link href="http://daiwk.github.io/posts/dl-rsgan.html"/>
   <updated>2019-10-12T00:00:00+00:00</updated>
   <id>/posts/dl-rsgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.03529&quot;&gt;Generating Reliable Friends via Adversarial Training to Improve Social Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://github.com/Coder-Yu/RecQ&quot;&gt;http://github.com/Coder-Yu/RecQ&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文为发表在 ICDM 2019 的工作。论文提出了一种基于生成对抗训练框架的 social recommendation 模型，指出了现有社会化推荐模型中的关系缺失、噪声问题，并利用对抗训练改善了这些问题，提高了社会化推荐的效果。此外，以往基于 Policy Gradient 的离散 GAN 在训练时存在 reward 函数设计困难，收敛难等问题，本文利用重参数技巧，对模型进行了 End-to-End 的训练，绕开了 reward 函数，取得了很好的效果。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Animating Face using Disentangled Audio Representations</title>
   <link href="http://daiwk.github.io/posts/video-animate-face.html"/>
   <updated>2019-10-11T00:00:00+00:00</updated>
   <id>/posts/video-animate-face</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/VgW0yvQDRpJeX7J_BVBu-Q&quot;&gt;微软AI：一张面部照片一段音频，完美生成头像演讲视频&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1910.00726&quot;&gt;Animating Face using Disentangled Audio Representations&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>flowseq</title>
   <link href="http://daiwk.github.io/posts/nlp-flowseq.html"/>
   <updated>2019-10-11T00:00:00+00:00</updated>
   <id>/posts/nlp-flowseq</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.02480&quot;&gt;FlowSeq: Non-Autoregressive Conditional Sequence Generation with Generative Flow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是 CMU 和 Facebook AI 联合发表于 EMNLP 2019 的工作。为了解决自自回归模型（auto regressive）在 Seq2Seq 问题上解码速度慢，只能利用一侧上下文信息等问题，提出了利用 generative flow 的非自回归模型（non-autoregressive）FlowSeq。在机器翻译任务上面的相比于之前的非自回归模型有显著提高，大大缩小了与自回归模型的差距。同时解码速度比自回归模型明显加快。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/XuezheMax/flowseq&quot;&gt;https://github.com/XuezheMax/flowseq&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>MixHop</title>
   <link href="http://daiwk.github.io/posts/dl-mixhop.html"/>
   <updated>2019-10-11T00:00:00+00:00</updated>
   <id>/posts/dl-mixhop</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.00067&quot;&gt;MixHop: Higher-Order Graph Convolutional Architectures via Sparsified Neighborhood Mixing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/samihaija/mixhop&quot;&gt;https://github.com/samihaija/mixhop&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是图分析大牛 Bryan Perozzi 组发表于 ICML 2019 的工作。本文 argue 现有的 GNN 模型无法学习到一种很通用的邻居混合信息，然后提出了 MixHop 来混合不同阶邻居的信息并学习节点表示。&lt;/p&gt;

&lt;p&gt;MixHop 非常的高效并且有很强的理论背景（MixHop 与 delta operators 之间的联系）。另外，通过混合各阶信息，MixHop 一定程度上避免了 GNN 过平滑问题。GNN 的过平滑问题：随着层数的增加，GNN 所学习到的节点表示变的没有区分度。最后作者通过大量的试验验证了 MixHop 的效果。&lt;/p&gt;

&lt;p&gt;在 Citeseer，Cora 和 Pubmed 上，MixHop 都取得了大量提升。例如，虽然 MixHop 没有使用注意力机制来学习邻居的重要性，但其表现依然大幅超过 GAT。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>avm</title>
   <link href="http://daiwk.github.io/posts/audio-avm.html"/>
   <updated>2019-10-11T00:00:00+00:00</updated>
   <id>/posts/audio-avm</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/UalvXsMnKdDDf84CYVUm7Q&quot;&gt;平安首创AVM自动变奏模型，探秘全球首部AI交响变奏曲背后硬核技术&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Arror数据集+tf</title>
   <link href="http://daiwk.github.io/posts/dl-arrow-tf.html"/>
   <updated>2019-10-10T00:00:00+00:00</updated>
   <id>/posts/dl-arrow-tf</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/O2mfxaczHQYHeUsdf4Ul1w&quot;&gt;TensorFlow 与 Apache Arrow 数据集搭配简介&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apache Arrow 本质上是一种基于内存的列式数据的标准格式，旨在提高系统之间的效率和互操作性。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先，作为一种数据标准，无论数据来源为何，Apache Arrow 均能确保数据类型安全性和数据完整性。&lt;/li&gt;
  &lt;li&gt;其次，作为一种内存格式，Apache Arrow 允许系统之间进行数据交换，而无需为不同文件格式进行序列化或转换。&lt;/li&gt;
  &lt;li&gt;最后，Arrow 始终针对数据处理进行全面优化，无论是零拷贝中读取还是现代硬件上的加速操作支持均涵盖在内。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;因此，这确保了您可以高效地处理数据，同时与不同规模的各类系统无缝集成。&lt;/p&gt;

&lt;p&gt;Arrow 数据集是 tf.data.Dataset 的扩展，因此两者可利用相同的 API 与 tf.data 流水线集成，并可作为 tf.keras 的输入。TensorFlow I/O 目前提供 3 种 Arrow 数据集，按名称排序如下：ArrowDataset、ArrowFeatherDataset 和 ArrowStreamDataset。这三种数据集均由相同的底层 Arrow 数据馈送，且此类底层数据具有两个重要特征：结构化 和 批量化。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/5UtpFg7Zmm6WY0OOxzeueQ&quot;&gt;TensorFlow 与 Apache Arrow 数据集搭配最佳实践&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>情感计算</title>
   <link href="http://daiwk.github.io/posts/dl-affective-computing.html"/>
   <updated>2019-10-10T00:00:00+00:00</updated>
   <id>/posts/dl-affective-computing</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/A9BUL_AvcbSwdRF08QDOBQ&quot;&gt;情感计算：让机器更加智能&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Recurrent Independent Mechanisms</title>
   <link href="http://daiwk.github.io/posts/dl-recurrent-independent-mechanisms.html"/>
   <updated>2019-10-08T00:00:00+00:00</updated>
   <id>/posts/dl-recurrent-independent-mechanisms</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/ALa9rXXIBEH9v585u1riWA&quot;&gt;Bengio等人提出新型循环架构，大幅提升模型泛化性能&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.10893&quot;&gt;Recurrent Independent Mechanisms&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>deep image prior用于视频修复</title>
   <link href="http://daiwk.github.io/posts/video-deep-image-prior.html"/>
   <updated>2019-10-07T00:00:00+00:00</updated>
   <id>/posts/video-deep-image-prior</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/JahD1ON5ym0_ldc7MiJz7A&quot;&gt;让视频里的你完全消失，Adobe最新SOTA模型实现无痕修图，无需先验知识&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;用各种修图技术 P 掉图片里的指定内容往往很难不着痕迹，抹掉视频里的流动内容就更难了。但近日，Adobe 提出了一种基于 Deep Image Prior 的新型视频修图算法，可以同时修复缺失图像和移动（光流）信息，增强视频的时间和空间连贯性，使得去掉某些内容之后的视频依然自然、流畅，毫无修图痕迹。而且，该方法无需外部数据库，仅通过视频内部学习即可实现。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.07957v1&quot;&gt;An Internal Learning Approach to Video Inpainting&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tsm</title>
   <link href="http://daiwk.github.io/posts/video-tsm.html"/>
   <updated>2019-10-05T00:00:00+00:00</updated>
   <id>/posts/video-tsm</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#tsm&quot;&gt;TSM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%8a%a0%e9%80%9f&quot;&gt;加速&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;tsm&quot;&gt;TSM&lt;/h2&gt;

&lt;p&gt;ICCV2019上，&lt;a href=&quot;https://arxiv.org/abs/1811.08383&quot;&gt;TSM: Temporal Shift Module for Efficient Video Understanding&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;加速&quot;&gt;加速&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/a_cXMashqCrD-b65rusTLQ&quot;&gt;15分钟完成Kinetics视频识别训练，除了超级计算机你还需要TSM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1910.00932&quot;&gt;Training Kinetics in 15 Minutes: Large-scale Distributed Training on Videos&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/mit-han-lab/temporal-shift-module&quot;&gt;https://github.com/mit-han-lab/temporal-shift-module&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://hanlab.mit.edu/projects/tsm/&quot;&gt;https://hanlab.mit.edu/projects/tsm/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;深度视频识别的计算成本比图像识别更高，尤其是在 Kinetics 等大规模数据集上。因此，为了处理大量视频，可扩展性训练是至关重要的。这篇论文研究了影响视频网络的可扩展性的因素。研究者认定了三个瓶颈，包括数据加载（从磁盘向 GPU 移动数据）、通信（在网络中移动数据）和计算速度（FLOPs）。&lt;/p&gt;

&lt;p&gt;针对这些瓶颈，研究者提出了三种可以提升可扩展性的设计原则：（1）使用 FLOPs 更低且对硬件友好的算子来提升计算效率；（2）降低输入帧数以减少数据移动和提升数据加载效率，（3）减少模型大小以降低网络流量和提升网络效率。&lt;/p&gt;

&lt;p&gt;基于这些原则，研究者设计了一种新型的算子「时间位移模块（TSM：Temporal Shift Module）」，能够实现高效且可扩展的分布式训练。相比于之前的 I3D 模型，TSM 模型的吞吐量可以高出 1.8 倍。&lt;/p&gt;

&lt;p&gt;研究者也通过实验测试了新提出的 TSM 模型。将 TSM 模型的训练扩展到了 1536 个 GPU 上，使用了包含 12288 个视频片段/ 98304 张图像的 minibatch，没有造成准确度损失。使用这样的硬件友好的模型设计，研究者成功地扩展了在 Summit 超级计算机上的训练，将在 Kinetics 数据集上的训练时间从 49 小时 55 分减少到了 14 分 13 秒，同时实现了 74.0% 的 top-1 准确度，这在准确度更高的同时还比之前的 I3D 视频模型快 1.6 和 2.9 倍。&lt;/p&gt;

&lt;p&gt;在计算机视觉领域，视频识别是一个至关重要的分支。视频识别问题的难度更高，但得到的研究更少：（1）相比于 2D 图像模型，视频模型的计算成本通常高一个数量级。举个例子，很常见的 ResNet-50 模型的速度大约是 4G FLOPs，而 ResNet-50 I3D 则要消耗 33G FLOPs，多过 8 倍；（2）视频数据集比 2D 图像数据集大得多，而且数据 I/O 也比图像高很多。举个例子，ImageNet 有 128 万张训练图像，而视频数据集 Kinetics-400 有 6300 万训练帧，大约是前者的 50 倍；（3）视频模型的模型大小通常更大，因此需要更高的网络带宽来交换梯度。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>gan-tts</title>
   <link href="http://daiwk.github.io/posts/audio-gan-tts.html"/>
   <updated>2019-10-05T00:00:00+00:00</updated>
   <id>/posts/audio-gan-tts</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/GucJq-45QfvlCbE33nsmVQ&quot;&gt;DeepMind推出GAN-TTS：用生成对抗网络实现高保真语音&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1909.11646.pdf&quot;&gt;High fidelity speech synthesis with adversarial networks&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorflow 2.0</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-2.0.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-2.0</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#tf20%e6%95%99%e7%a8%8b&quot;&gt;tf2.0教程&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%9f%ba%e7%a1%80&quot;&gt;基础&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%a8%a1%e5%9e%8b&quot;&gt;模型&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/bjQoJ7hqxXJJ70Y6a0Yp2g&quot;&gt;谷歌重磅发布TensorFlow 2.0正式版，高度集成Keras，大量性能改进&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;安装：&lt;a href=&quot;https://www.tensorflow.org/install&quot;&gt;https://www.tensorflow.org/install&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;datasets：&lt;a href=&quot;https://www.tensorflow.org/guide/data&quot;&gt;https://www.tensorflow.org/guide/data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;autograph：&lt;a href=&quot;https://www.tensorflow.org/guide/function&quot;&gt;https://www.tensorflow.org/guide/function&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1.x-&amp;gt;2.0迁移：&lt;a href=&quot;https://www.tensorflow.org/guide/migrate&quot;&gt;https://www.tensorflow.org/guide/migrate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;deeplearning.ai的教程：&lt;a href=&quot;https://www.coursera.org/learn/introduction-tensorflow&quot;&gt;https://www.coursera.org/learn/introduction-tensorflow&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;tf20教程&quot;&gt;tf2.0教程&lt;/h2&gt;

&lt;h3 id=&quot;基础&quot;&gt;基础&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/6DmpLZ3Nklo17WY84hJYGA&quot;&gt;TensorFlow 2.0 基础：张量、自动求导与优化器&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;模型&quot;&gt;模型&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/xBZoD8V7gTZaBRff19gPUA&quot;&gt;TensorFlow 2.0 模型：Keras 训练流程及自定义组件&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/VZBAevM1y-FxTUrFCI6oEQ&quot;&gt;​TensorFlow 2.0 模型：卷积神经网络&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;推荐教程：&lt;a href=&quot;https://colab.research.google.com/drive/1UCJt8EYjlzCs1H1d1X0iDGYJsHKwu-NO#scrollTo=88ExjKfCo7aP&quot;&gt;https://colab.research.google.com/drive/1UCJt8EYjlzCs1H1d1X0iDGYJsHKwu-NO#scrollTo=88ExjKfCo7aP&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;拷了一份过来：&lt;a href=&quot;https://colab.research.google.com/drive/1GCWbk5pInyXSrJitM5dZfGxVT9ZWQsnM&quot;&gt;https://colab.research.google.com/drive/1GCWbk5pInyXSrJitM5dZfGxVT9ZWQsnM&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>InterpretML</title>
   <link href="http://daiwk.github.io/posts/platform-interpret-ml.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/platform-interpret-ml</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#interpretml&quot;&gt;InterpretML&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%b6%e4%bb%96%e5%ba%93&quot;&gt;其他库&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#lime&quot;&gt;LIME&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rslime&quot;&gt;RSLIME&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#h2o&quot;&gt;H2O&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#acecv%e7%9b%b8%e5%85%b3&quot;&gt;ACE(cv相关)&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;interpretml&quot;&gt;InterpretML&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.09223v1&quot;&gt;InterpretML: A Unified Framework for Machine Learning Interpretability&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/microsoft/interpret&quot;&gt;https://github.com/microsoft/interpret&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;InterpretML 是一个为实践者和研究者提供机器学习可解释性算法的开源 Python 软件包。InterpretML 能提供以下两种类型的可解释性：（1）明箱（glassbox），这是针对可解释性设计的机器学习模型（比如线性模型、规则列表、广义相加模型）；（2）黑箱（blackbox）可解释技术，用于解释已有的系统（比如部分依赖、LIME）。这个软件包可让实践者通过在一个统一的 API 下，借助内置的可扩展可视化平台，使用多种方法来轻松地比较可解释性算法。InterpretML 也包含了可解释 Boosting 机（Explanable Boosting Machine，EBM）的首个实现，这是一种强大的可解释明箱模型，可以做到与许多黑箱模型同等准确的性能。&lt;/p&gt;

&lt;h2 id=&quot;其他库&quot;&gt;其他库&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/76112940&quot;&gt;吐血整理！绝不能错过的24个顶级Python库&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;lime&quot;&gt;LIME&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1602.04938.pdf&quot;&gt;“Why Should I Trust You?” Explaining the Predictions of Any Classifier&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/marcotcr/lime&quot;&gt;https://github.com/marcotcr/lime&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;LIME是一种算法（库），可以解释任何分类器或回归量的预测。LIME是如何做到的呢？通过可解释的模型在局部不断接近预测值，这个模型解释器可用于生成任何分类算法的解释。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.analyticsvidhya.com/blog/2017/06/building-trust-in-machine-learning-models/&quot;&gt;在机器学习模型中建立信任（在Python中使用LIME）&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;rslime&quot;&gt;RSLIME&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://ieeexplore.ieee.org/document/8852034&quot;&gt;RSLIME: An Efficient Feature Importance Analysis Approach for Industrial Recommendation Systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/mazNz9sGyxH-oASqg80Pgw&quot;&gt;为什么刷小视频停不下来？爱奇艺用这篇论文告诉你答案&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;相比于传统的视频推荐系统，爱奇艺的 UGC 推荐和小视频分发有四个极其困难的方面：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;新鲜度：爱奇艺的小视频应用的语料库非常动态，用户每天都会上传数十万条新视频。推荐系统应具有足够的响应能力，以便建模新上传的视频和最新的用户行为。&lt;/li&gt;
  &lt;li&gt;冷启动：鉴于小视频有更高的及时性要求和更低的用户黏性，推荐系统面临着严重的用户和项目冷启动问题，这会有损基于协同过滤（CF）的方法的性能。&lt;/li&gt;
  &lt;li&gt;多样性：由于视频类型和用户人口统计分布的多样性，爱奇艺的视频标签系统以及用户兴趣画像都比传统的视频推荐复杂得多，这也使得相关特征极其分散。内容和用户的多样性也会使得小视频推荐系统的结果不能稳健地应对输入中的错误。&lt;/li&gt;
  &lt;li&gt;兴趣转移：历史用户行为并不总是可靠的。每位用户在一个小时内就可能浏览数十个小视频，他们感兴趣的内容也会发生巨大的变化。一旦用户对之前的视频感到厌烦，他们就会渴望探索新的类别。因此，把握短期和长期用户偏好之间的平衡是至关重要的。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;针对这些难题，爱奇艺的研究者提出了一种遵循多阶段流程的模型，其由三个模块构成，即用户画像（User Profile）、召回（Recall）和排序 Ranking）。为了提升小视频推荐系统的表现，每个模块中都使用了广泛的模型集成方法。下面简要介绍了其系统结构：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;用户画像：对用户的人口统计属性、历史行为、兴趣和偏好的多维度分析。用户画像可用作实现个性化推荐的基石。&lt;/li&gt;
  &lt;li&gt;召回：多种协同过滤（CF）算法（基于物品的 CF、基于用户的 CF、矩阵分解、Item2Vec 等）和多种基于内容的过滤（CBF）方法的组合。这些模型的结果会被聚合起来，为每个请求构建一个视频候选项语料库，其中通常包含数百条视频。&lt;/li&gt;
  &lt;li&gt;排序：一个用于评估视频候选项的分数的点击率预估模型，然后将少量最佳推荐的视频推送到用户界面。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;为了监控排序模型的工作方式是否有如预期以及是否能加速模型的迭代过程，爱奇艺提出了一种与模型无关的推荐系统局部可解释方法 Recommendation System Boosted Local Interpretable Model-Agnostic Explanations Method（RSLIME，），可为其排序模型提供特征重要度分析。RSLIME 有望为推荐系统中的特征选取过程提供参照，并帮助推荐系统开发者不必太过麻烦就能调整他们的排序模型。RSLIME 具有以下特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对于单个输入样本，RSLIME 可以生成特征重要度的估计，而不管排序模块中所用的架构为何。然后可以基于这些特征重要度解释推荐结果。&lt;/li&gt;
  &lt;li&gt;对于多个输入样本，RSLIME 可以结合多个样本的预测结果进行整体分析，并给出准确的特征重要度估计。&lt;/li&gt;
  &lt;li&gt;RSLIME 可对稀疏特征的影响进行高效的分析，从而指导模型的优化和特征的选择&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其中：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;DNN：DNN 使用的是一个带有三个隐藏层的全连接网络，其维度分别为 1024、512 和 256。DNN 的输入是用户和视频的预训练的特征嵌入，这基于用户行为和视频语义内容。爱奇艺的模型使用了 DNN 来提升排序模块的准确度以及在线 A/B 测试中的泛化能力。&lt;/li&gt;
  &lt;li&gt;GBDT：GBDT 是指多个决策树的基于提升（boosting）的集成。GBDT 的叶节点自动表示所选择的重要特征，其可被用于提升 FM 的性能。GBDT 先要单独进行预训练，之后才会与 FM 和 DNN 进行联合训练。由于 GBDT 对非归一化的特征的存在而言是稳健的，所以其输入中归一化和非归一化的稠密特征都可以有。&lt;/li&gt;
  &lt;li&gt;FM：FM 可自动执行特征组合和二阶交叉特征计算。因此，FM 可以执行特征融合和在 GBDT 输出和稀疏特征上执行交叉，由此能在推荐点击率（CTR）预估方面取得当前最佳的结果。&lt;/li&gt;
  &lt;li&gt;Sigmoid：Sigmoid 能为 DNN 和 FM 的输出加权并在其结果上执行 sigmoid 变换。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这里使用 X 表示输入数据，x 表示单个输入样本。样本 x 的 n 维输入特征表示为 Zn 或 (z1…zn)。z 表示单个特征组合，推理模型表示为 f。线性回归模型 g ∈ G 等可解释的模型经过训练后用于执行单个案例的特征重要度分析。&lt;/p&gt;

&lt;p&gt;RSLIME 是局部可解释的与模型无关的解释（LIME）方法的一种扩展。LIME 使用了一种可解释的模型来评估推理模型 f 在单个输入样本 x 上的特征重要度。设有 100 个特征 (z1…z100) 的一个输入样本 x，f(x) 是 x 的推理结果。&lt;/p&gt;

&lt;p&gt;LIME 首先会自动生成数千个不同的特征组合（比如 z1…z99、z2…z100）。然后对于每个特征组合，LIME 根据这个组合内部的特征（同时掩盖其它所有特征）计算该推理模型 f 的预测结果。为了说明清楚，使用特征组合 z1…z99 时，表示 z100 被 0 掩码掩盖。&lt;/p&gt;

&lt;p&gt;给定每个特征组合 z 和对应的预测结果 f(z)，LIME 会计算 z 和 x 的距离并将其作为 z 的权重，然后会训练一个可解释的模型（以线性回归模型为例）g 将 z 映射到 f(z) 和 f(x) 之间的绝对差值，然后用作单个案例特征重要度的直接指示。这个可解释模型中每个特征的最终权重都会被用作特征重要度。因此，LIME 的局部特征诊断算法可以表示为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\varepsilon(x)=\operatorname{argmin}_{g \in G} L\left(f, g, \pi_{x}(z)\right)+\Omega(g)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Omega(g)\)&lt;/code&gt;表示模型复杂度，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi_{x}(z)\)&lt;/code&gt;表示样本x的特征组合。&lt;/p&gt;

&lt;h3 id=&quot;h2o&quot;&gt;H2O&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/h2oai/mli-resources&quot;&gt;https://github.com/h2oai/mli-resources&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;H2O的无人驾驶AI，提供简单的数据可视化技术，用于表示高度特征交互和非线性模型行为，通过可视化提供机器学习可解释性（MLI），说明建模结果和模型中特征的影响。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.h2o.ai/wp-content/uploads/2018/01/Machine-Learning-Interpretability-MLI_datasheet_v4-1.pdf&quot;&gt;机器学习可解释性&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;acecv相关&quot;&gt;ACE(cv相关)&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1902.03129.pdf&quot;&gt;Towards Automated Concept-based Explanation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/JXJYvnLqLLuSclsd4ZTySA&quot;&gt;AI眼中的世界是什么样子？谷歌新研究找到了机器的视觉概念&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/amiratag/ACE&quot;&gt;https://github.com/amiratag/ACE&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>各种概率分布</title>
   <link href="http://daiwk.github.io/posts/ml-distributions.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/ml-distributions</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/vcgZ2ncZDlXp4SAExyptnQ&quot;&gt;Distribution is all you need：这里有12种做ML不可不知的分布&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/graykode/distribution-is-all-you-need&quot;&gt;https://github.com/graykode/distribution-is-all-you-need&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/all-distributions.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>量子计算相关</title>
   <link href="http://daiwk.github.io/posts/dl-quantum-computing.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/dl-quantum-computing</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#量子霸权&quot;&gt;量子霸权&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#强化学习量子计算&quot;&gt;强化学习+量子计算&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;量子计算介绍：&lt;a href=&quot;https://arxiv.org/abs/1708.03684&quot;&gt;An Introduction to Quantum Computing, Without the Physics&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;量子霸权&quot;&gt;量子霸权&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://drive.google.com/file/d/19lv8p1fB47z1pEZVlfDXhop082Lc-kdD/view&quot;&gt;Quantum Supremacy Using a Programmable Superconducting Processor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/njhMSPlO2_fNbi70QzmRZQ&quot;&gt;谷歌实现量子霸权论文曝光，圈内人士：量子计算的里程碑事件&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/Y3lA6-o1ZhTAxWGyH5QR5Q&quot;&gt;谷歌开启量子计算“hello world”时代！费曼预言将成真&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;量子霸权（quantum supremacy）是指量子计算在某些任务上拥有超越所有传统计算机的计算能力。&lt;/p&gt;

&lt;h2 id=&quot;强化学习量子计算&quot;&gt;强化学习+量子计算&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/ErhF8DNtHE03-Bk5FwDBnw&quot;&gt;通往量子霸权之路：谷歌用强化学习优化量子门控制策略&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.nature.com/articles/s41534-019-0141-3.pdf&quot;&gt;Universal Quantum Control through Deep Reinforcement Learning&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>one-shot模仿学习</title>
   <link href="http://daiwk.github.io/posts/dl-one-shot-imitation-learning.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/dl-one-shot-imitation-learning</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/IAK5OA-GFIWBd3FkRU2A2Q&quot;&gt;让机器人看一眼就能模仿：One-Shot模仿学习发展情况&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3篇：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Yu, Tianhe, et al. &lt;a href=&quot;https://arxiv.org/pdf/1810.11043.pdf&quot;&gt;One-shot hierarchical imitation learning of compound visuomotor tasks&lt;/a&gt;，基于元学习的单样本模仿学习。&lt;/li&gt;
  &lt;li&gt;De-An Huang, et al. &lt;a href=&quot;https://arxiv.org/abs/1908.06769&quot;&gt;Continuous Relaxation of Symbolic Planner for One-Shot Imitation Learning&lt;/a&gt; (2019)，利用符号规划问题的单样本模仿学习。&lt;/li&gt;
  &lt;li&gt;Leo Pauly, et al.&lt;a href=&quot;https://arxiv.org/pdf/1810.07483.pdf&quot;&gt;One-Shot Observation Learning Using Visual Activity Features&lt;/a&gt;，基于视觉活动特征的单样本观察学习。&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>gate decorator</title>
   <link href="http://daiwk.github.io/posts/dl-gate-decorator.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/dl-gate-decorator</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/kgl7mz4bK7SywkbViY_qhQ&quot;&gt;利用LSTM思想来做CNN剪枝，北大提出Gate Decorator&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>可解释性CNN</title>
   <link href="http://daiwk.github.io/posts/cv-interpretable-cnn.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/cv-interpretable-cnn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/UOebwF_sJJGqUX027kUfnw&quot;&gt;CVPR 2019 | 基于可解释性以及细粒度的可视化解释卷积神经网络&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1908.02686&quot;&gt;Interpretable and Fine-Grained Visual Explanations for Convolutional Neural Networks&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>gan剪枝</title>
   <link href="http://daiwk.github.io/posts/cv-gan-pruning.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/cv-gan-pruning</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/kgl7mz4bK7SywkbViY_qhQ&quot;&gt;ICCV 2019：华为、北大等首创GAN剪枝算法，线上加速 3 倍以上&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1907.10804&quot;&gt;Co-Evolutionary Compression for Unpaired Image Translation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/huawei-noah/GAN-pruning&quot;&gt;https://github.com/huawei-noah/GAN-pruning&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>语音生成评价</title>
   <link href="http://daiwk.github.io/posts/audio-evaluation.html"/>
   <updated>2019-10-01T00:00:00+00:00</updated>
   <id>/posts/audio-evaluation</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/Qq7EqNE7nVSVXorOwiZvYg&quot;&gt;语音生成效果评价不再逐句进行：谷歌提出长文本语音生成评价新系统&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>rl for recommendation</title>
   <link href="http://daiwk.github.io/posts/rl-rl-recommendation.html"/>
   <updated>2019-09-20T00:00:00+00:00</updated>
   <id>/posts/rl-rl-recommendation</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#drn&quot;&gt;drn&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#top-k-off-policy&quot;&gt;top-k off-policy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#slateq&quot;&gt;slateq&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#dear&quot;&gt;DEAR&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#dear%e6%a8%a1%e5%9e%8b%e7%bb%93%e6%9e%84&quot;&gt;dear模型结构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dear%e5%ae%9e%e9%aa%8c&quot;&gt;dear实验&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;drn&quot;&gt;drn&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/rl-drn.html&quot;&gt;https://daiwk.github.io/posts/rl-drn.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;top-k-off-policy&quot;&gt;top-k off-policy&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-topk-off-policy-correction.html&quot;&gt;https://daiwk.github.io/posts/dl-topk-off-policy-correction.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;slateq&quot;&gt;slateq&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/rl-slateq.html&quot;&gt;https://daiwk.github.io/posts/rl-slateq.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;dear&quot;&gt;DEAR&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/85417314&quot;&gt;今日头条最新论文，首次改进DQN网络解决推荐中的在线广告投放问题&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.03602&quot;&gt;Deep Reinforcement Learning for Online Advertising in Recommender Systems&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;dear模型结构&quot;&gt;dear模型结构&lt;/h3&gt;

&lt;p&gt;在给定推荐列表前提下，本文提出了一种基于DQN的创新架构来同时解决三个任务：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;是否&lt;/strong&gt;插入广告&lt;/li&gt;
  &lt;li&gt;如果插入，插入&lt;strong&gt;哪一条&lt;/strong&gt;广告&lt;/li&gt;
  &lt;li&gt;插入广告在推荐列表的哪个&lt;strong&gt;位置&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DQN的两种经典结构：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;输入state&lt;/strong&gt;，输出是&lt;strong&gt;所有可能action对应的Q&lt;/strong&gt;；&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;输入state&lt;/strong&gt;以及某&lt;strong&gt;一个action&lt;/strong&gt;，输出是&lt;strong&gt;对应的Q&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这两种经典架构的最主要的问题是只能将action定义为插入哪一条广告，或者插入广告在列表的哪个位置，无法同时解决上述提到的三个任务。&lt;/p&gt;

&lt;p&gt;当然，&lt;strong&gt;插入位置&lt;/strong&gt;与&lt;strong&gt;插入哪一条广告&lt;/strong&gt;通过某种表示形式，如one-hot编码来建模action，那么action空间就是O(A*L)，其中A是广告数量，L是位置数量。要实际应用是不可能的。。&lt;/p&gt;

&lt;p&gt;本文融合了上述提到了两种经典DQN结构的结合，会&lt;strong&gt;同时&lt;/strong&gt;针对&lt;strong&gt;所有可能的插入位置&lt;/strong&gt;的Q进行预估。&lt;/p&gt;

&lt;p&gt;如下图的左边部分，输入层包含&lt;strong&gt;State&lt;/strong&gt;以及&lt;strong&gt;Action（插入哪条广告）&lt;/strong&gt;，输出层则是广告插入推荐列表的&lt;strong&gt;L+1位置对应的Q&lt;/strong&gt;-（假设推荐列表长度为L，则可以插入广告的位置为L+1种可能）。与此同时，使用一个&lt;strong&gt;特殊插入位置0&lt;/strong&gt;用来表示&lt;strong&gt;不进行广告插入&lt;/strong&gt;，因此输出层的长度扩展成为&lt;strong&gt;L+2&lt;/strong&gt;。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/dear-arch.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;然后看右边部分，参考dueling dqn，输出层Q函数被拆解成两部分：只由state决定的V(s)函数；以及由state和action同时决定的A(s,a)函数。其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;state是把下面3部分concate到一起的一个向量：
    &lt;ul&gt;
      &lt;li&gt;使用GRU针对用户的推荐结果点击历史和广告点击历史进行用户序列偏好建模的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p^{rec}_t\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p^{ad}_t\)&lt;/code&gt;；&lt;/li&gt;
      &lt;li&gt;当前用户请求的上下文信息&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;，例如操作系统，app版本号，上下滑动等；&lt;/li&gt;
      &lt;li&gt;当前请求展示的推荐列表item的特征进行concat(当然也可以是cnn等)形成的低维稠密向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(rec_{t}\)&lt;/code&gt;；&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;action包含两部分：
    &lt;ul&gt;
      &lt;li&gt;一部分是候选插入广告ad的特征&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t^{ad}\)&lt;/code&gt;，作为输入的一部分&lt;/li&gt;
      &lt;li&gt;另一部分则是广告插入的位置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t^{loc}\)&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;reward函数。Reward函数也包含两部分：
    &lt;ul&gt;
      &lt;li&gt;一部分是广告的的收入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r^{ad}_t\)&lt;/code&gt;；&lt;/li&gt;
      &lt;li&gt;另一部分则是用户是否继续往下刷的奖励&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r^{ex}_t\)&lt;/code&gt;，继续刷新是1，离开是-1。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;最优的Q函数策略便可以通过Bellman等式求得：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q^{*}\left(s_{t}, a_{t}\right)=\mathbb{E}_{s_{t+1}}\left[r_{t}+\gamma \max _{a_{t+1}} Q^{*}\left(s_{t+1}, a_{t+1}\right) | s_{t}, a_{t}\right]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;基于用户交互历史的离线日志，采用 Off-policy的方式进行训练得到最优的投放策略。&lt;/p&gt;

&lt;p&gt;针对每一次迭代训练：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;针对用户请求构建状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t=concat(p^{rec}_t,p^{ad}_t,c_t,rec_t)\)&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;根据off-policy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(b(s_t)\)&lt;/code&gt;(即离线日志里的当前广告策略)执行action &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;，也就是选取特定ad；&lt;/li&gt;
  &lt;li&gt;更新状态为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{t+1}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;计算reward &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t=r_t^{ad}+\alpha r_t^{ex}\)&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;将状态转移信息&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((s_{t}，a_{t}，r_{t}，s_{t+1})\)&lt;/code&gt;存储到replay buffer；&lt;/li&gt;
  &lt;li&gt;从replay buffer中取出mini-batch的状态转移信息&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s,a,r,s'\)&lt;/code&gt;，来最小化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((y-Q(s, a ; \theta))^{2}\)&lt;/code&gt;训练得到最优的Q函数参数。&lt;/li&gt;
  &lt;li&gt;其中，&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
y=\left\{\begin{array}{ll}{r} &amp;amp; {\text { terminal } s^{\prime}} \\ {r+\gamma \max _{a^{\prime}} Q\left(s^{\prime}, a^{\prime} ; \theta\right)} &amp;amp; {\text { non - terminal } s^{\prime}}\end{array}\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/off-policy-dear.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在线test：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;得到状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t=concat(p^{rec}_t,p^{ad}_t,c_t,rec_t)\)&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;通过最优策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^*(s_t,a_t)\)&lt;/code&gt;执行动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;得到用户反馈的reward &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t(s_t,a_t)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;将状态从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;更新到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{t+1}\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/test-dear.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;dear实验&quot;&gt;dear实验&lt;/h3&gt;

&lt;p&gt;100w的session，涉及到18w的user，1700w的自然结果视频，1000w的广告。&lt;/p&gt;

&lt;p&gt;平均每个session里有55个自然结果视频,55%的推荐列表里有广告，平均每个session的时长是17min。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>The Lottery Ticket Hypothesis</title>
   <link href="http://daiwk.github.io/posts/dl-lottery-ticket-hypothesis.html"/>
   <updated>2019-09-18T00:00:00+00:00</updated>
   <id>/posts/dl-lottery-ticket-hypothesis</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/Qxeh89AADpZZomizurgpEw&quot;&gt;训练网络像是买彩票？神经网络剪枝最新进展之彩票假设解读&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ICLR2019，&lt;a href=&quot;https://arxiv.org/abs/1803.03635&quot;&gt;The Lottery Ticket Hypothesis: Finding Sparse, Trainable Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;彩票假设的正式定义为：一个随机初始化的密集神经网络包含一个初始化的子网络，在单独训练时，最多经过相同的迭代次数，可以达到和原始网络一样的测试准确率。&lt;/p&gt;

&lt;p&gt;我们将一个复杂网络的所有参数当做奖池，上述一组子参数对应的子网络就是中奖彩票。&lt;/p&gt;

&lt;p&gt;作者提出了彩票假设并给出一种寻找中奖彩票的方法，通过迭代非结构化剪枝的方式可以找到一个子网络，用原始网络的初始化参数来初始化，可以在性能不下降的情况下更快的训练这个子网络，但是如果用随机初始化方法却达不到同样的性能。&lt;/p&gt;

&lt;p&gt;作者也在文章中指出这项工作存在的一些问题。例如，迭代剪枝的计算量太大，需要对一个网络进行连续 15 次或 15 次以上的多次训练。未来可以探索更加高效的寻找中奖彩票的方法。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.01067&quot;&gt;Deconstructing Lottery Tickets: Zeros, Signs, and the Supermask&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>SpeedTorch</title>
   <link href="http://daiwk.github.io/posts/platform-pytorch-cpu2gpu.html"/>
   <updated>2019-09-17T00:00:00+00:00</updated>
   <id>/posts/platform-pytorch-cpu2gpu</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/NupSd4e01cvQ3CRnjy1npw&quot;&gt;超原版速度110倍，针对PyTorch的CPU到GPU张量迁移工具开源&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/Santosh-Gupta/SpeedTorch&quot;&gt;https://github.com/Santosh-Gupta/SpeedTorch&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>可视化工具</title>
   <link href="http://daiwk.github.io/posts/others-visual-tools.html"/>
   <updated>2019-09-17T00:00:00+00:00</updated>
   <id>/posts/others-visual-tools</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#netron&quot;&gt;netron&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;netron&quot;&gt;netron&lt;/h2&gt;

&lt;p&gt;官网：&lt;a href=&quot;https://github.com/lutzroeder/netron&quot;&gt;https://github.com/lutzroeder/netron&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;可以装个mac的app&lt;/p&gt;

&lt;p&gt;也可以直接python装了，然后：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;netron xxx.pb &lt;span class=&quot;nt&quot;&gt;--host&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;aa.bbbbb.cc.com&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--p&lt;/span&gt; 8999
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;接下来就可以在chrome里通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://aa.bbbbb.cc.com:8999&lt;/code&gt;来看了&lt;/p&gt;

&lt;p&gt;tf要生成pb的一个最简单的demo，紧跟着save加一句write_graph就行：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## ...
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;## save
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;models/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write_graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_def&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'./'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'model.pb'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以搜索的呢。。掉渣天&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/netron-demo.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

</content>
 </entry>
 
 <entry>
   <title>hypergraph相关</title>
   <link href="http://daiwk.github.io/posts/dl-hypergraph.html"/>
   <updated>2019-09-17T00:00:00+00:00</updated>
   <id>/posts/dl-hypergraph</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/nGPUcDHTrG6KwAqDCkfA1w&quot;&gt;IJCAI 2019 论文解读 | 基于超图网络模型的图网络进化算法&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;现实生活中很多的数据可以用图（graph）来建模，比如社交网络数据，paper 引用数据等。对于 AI 而言，一个常见的任务是半监督分类，即对图中的每一个点进行分类，在仅有部分点有标注的情况下。&lt;/p&gt;

&lt;p&gt;处理理此类问题，比较经典的方法是 GCN，通过对相邻节点的特征聚合操作来对每个节点进行特征提取。GCN 等 GNN 模型对于节点之间的关系表征是二元的，即仅能表征两个节点 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;e1,e2&amp;gt;&lt;/code&gt; 之间的关系，对于大于二元的关系组只能通过多个二元关系的方式去近似。&lt;/p&gt;

&lt;p&gt;超图模型（Hypergraph）就是针对这种情况提出的一种网络结构。不同类型的数据中都存在着多元关系，超图模型的基本设定就是一个边可以包含大于 2 个点，去拟合多元关系。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Listener-Decoder-Speller(LDS)</title>
   <link href="http://daiwk.github.io/posts/audio-lds.html"/>
   <updated>2019-09-15T00:00:00+00:00</updated>
   <id>/posts/audio-lds</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/Cu2z2tVVk0ubY6g7xIfm2A&quot;&gt;Interspeech 2019 | 阿里达摩院语音实验室：联合CTC和Transformer的自动中文纠错模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1904.10045.pdf&quot;&gt;Automatic Spelling Correction with Transformer for CTC-based End-to-End Speech Recognition&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>learn2learn</title>
   <link href="http://daiwk.github.io/posts/platform-learn2learn.html"/>
   <updated>2019-09-14T00:00:00+00:00</updated>
   <id>/posts/platform-learn2learn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/AH35EGTH1YDSx4WzUwY15g&quot;&gt;三四行代码打造元学习核心，PyTorch元学习库L2L现已开源&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;文档：&lt;a href=&quot;http://learn2learn.net/docs/learn2learn/&quot;&gt;http://learn2learn.net/docs/learn2learn/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github: &lt;a href=&quot;https://github.com/learnables/learn2learn&quot;&gt;https://github.com/learnables/learn2learn&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;learn2learn
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>neural style transfer相关</title>
   <link href="http://daiwk.github.io/posts/cv-style-transfer.html"/>
   <updated>2019-09-14T00:00:00+00:00</updated>
   <id>/posts/cv-style-transfer</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/-HBMKBQKXzRugKn5OLgiPg&quot;&gt;10秒搞定建筑日夜景转换，还自带中秋月光&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>美图的MML</title>
   <link href="http://daiwk.github.io/posts/platform-meitu-mml.html"/>
   <updated>2019-09-09T00:00:00+00:00</updated>
   <id>/posts/platform-meitu-mml</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#召回排序&quot;&gt;召回+排序&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#多目标&quot;&gt;多目标&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#样本reweight&quot;&gt;样本reweight&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#多目标模型&quot;&gt;多目标模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#多模型&quot;&gt;多模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#多个多目标模型&quot;&gt;多个多目标模型&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/Eih4J51C8Eh-cuZ8vznESg&quot;&gt;当推荐遇到社交：美图的推荐算法设计优化实践&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MML 机器学习平台包括三个主要模块：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Spark Feature：负责数据分析、特征工程，以及样本拼接。Spark Feature 基于 Spark SQL 进行开发，用户通过编写 SQL 以及配置样本拼接 JSON，即可实现特征以及样本生产的工作；&lt;/li&gt;
  &lt;li&gt;Bamboo：基于 tensorflow 开发，负责模型训练、离线效果评估。Bamboo 实现了推荐领域大量的 State of the Art 的模型，并且提供了丰富的 Layers，以简化算法同学的建模工作。在训练方面支持多种并行训练方式，同时通过对代码的优化实现了较高的训练效率；&lt;/li&gt;
  &lt;li&gt;MML Serving：负责模型的在线服务。底层通过 C++ 实现，在内存和并发上做了大量的优化，支持同时请求多个模型，以及在线热更。灵活的架构让我们能够很方便地接入各种机器学习框架训练的模型。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bamboo 具有以下优点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;便捷：内置了近几年推荐领域的 SOTA 模型，以及建模常用的 Layers，并且内置了部分公共数据集的访问接口，能够支持从本地磁盘，以及 HDFS 读取训练数据。数据、训练、模型评估、模型导出通过配置化实现，算法同学可以专注于模型的设计；&lt;/li&gt;
  &lt;li&gt;高效：采用 tensorflow 底层 API 和 Estimator 来实现，并遵循 tensorflow 官方性能优化指南，最大限度提升模型训练效率，相比 Keras 以及内部未优化版本，单卡训练效率有数倍提升。同时，能够支持同步、异步等多种并行训练方案；&lt;/li&gt;
  &lt;li&gt;可扩展：Bamboo 的最初的设计目标是作为 tensorflow 的补充，因此在整个设计过程充分考虑了扩展性，能够支持采用 Bamboo 提供的 API 或者使用 tensorflow 原生 API。良好的分层设计，方便使用方进行模块的复用和重构。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MML Serving 决定了模型能否上线提供服务以及在线服务的效率。去年下半年，我们上线了采用 C++ 开发的新版 MML Serving，通过内存和并发的优化，让我们整体预估耗时减少了 50%，服务初始化耗时减少了 50%，内存使用量降低了 77%。通过压测发现，服务在高并发下，整体表现稳定。另外良好的架构设计，可以很方便接入各种第三方机器学习库，目前已经内置了对 tensorflow 和 xgboost 模型的支持。&lt;/p&gt;

&lt;p&gt;2018 年，我们上线了第一个基于何向南在 SIGIR 2017 发表的《Neural Factorization Machines for Sparse Predictive Analytics》改进的模型——NFM-v4。相比原论文，我们的主要改进点是通过一个线性变换，将变长稀疏的原始高维特征压缩到一个定长稠密的低维实数空间，从而屏蔽了模型在输入特征处理上的差异，可以将精力更多放在特征的挖掘上。&lt;/p&gt;

&lt;p&gt;但是，将几十万维的高维空间直接压缩到几百维，存在一定的信息损失，因此，在 NFM-v4 的基础上，我们通过将部分高维 id 特征单独建模，比较好的解决了这个问题，在业务指标上，也有不错的效果提升，美拍的人均播放时长增加了 4.75%，人均有效行为数增加了 3.45%。&lt;/p&gt;

&lt;p&gt;不过，NFM 存在的一个问题是，bi-interaction pooling 认为特征二阶交叉的权重是相等的，这种假设在多数场景下并不符合数据的真实分布。因此，在 NFM 的基础上，我们提出了 Neural Field weighted Factorization Machines（NFwFM）模型，通过引入一个权重向量，来建模二阶交叉特征的权重。通过二阶向量不等权相加，业务指标整体提升较为明显。其中美拍人均播放时长增加 3.78%，播放用户数增加 1.74%，美图秀秀点击率提升了 5.689%，人均使用时长增加 2.53%，新用户点击率增加 2.701%。&lt;/p&gt;

&lt;p&gt;目前，我们主要尝试了三种用户行为序列建模的方法，包括 Sum/Mean Pooling 、 RNN 、 Attention 等。在我们的业务场景下，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;RNN 的离线效果并不理想，原因推测是用户点击 feed 的先后顺序并不存在某种固定的模式，而主要取决于用户对所推荐 feed 的偏好，此外，RNN 的训练耗时也增加比较明显。&lt;/li&gt;
  &lt;li&gt;Sum/Mean Pooling 的方式虽然简单，但是在长行为序列建模上，效果相比其它两种方式表现得更加优异，因此是我们目前线上建模用户长序列特征的主要手段。&lt;/li&gt;
  &lt;li&gt;我们也对比了基于 Attention 的方法，离线效果相比 Sum/Mean Pooling 有略微提升，但是考虑到计算复杂度，Attention 只适合于序列长度较短的场景。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在美拍，美图秀秀社区，以及 push 业务都尝试了用户行为序列特征建模，各项业务指标均有较大幅度的提升，美拍人均时长提升了 12%，秀秀的点击率提升了 5%，push 的到达点击率提升了 10%。&lt;/p&gt;

&lt;h2 id=&quot;召回排序&quot;&gt;召回+排序&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU1NTMyOTI4Mw==&amp;amp;mid=2247494558&amp;amp;idx=1&amp;amp;sn=10a6332147d713230d0fa4dbf9cdc897&amp;amp;chksm=fbd759f2cca0d0e4f5d39887a86b1d6efae13ad0540dc191fdbc8bb29120d0081a90c97b35f6&amp;amp;scene=27#wechat_redirect&quot;&gt;深度学习技术在美图个性化推荐的应用实践&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;多目标&quot;&gt;多目标&lt;/h2&gt;

&lt;p&gt;整个多目标优化的路线，大概经历了四个阶段：样本 reweight，多目标模型，多模型，多个多目标模型。&lt;/p&gt;

&lt;h3 id=&quot;样本reweight&quot;&gt;样本reweight&lt;/h3&gt;

&lt;p&gt;样本 reweight 是一种简单轻量的可用于解决多目标问题的做法，它借鉴了 imbalanced data 的典型做法，在保持模型优化的主目标不变的情况下，通过提高次要目标的正样本占比，来模拟多目标的联合概率分布。&lt;/p&gt;

&lt;p&gt;我们在美拍和美图秀秀社区上，对增加关注目标进行了尝试。美拍在播放时长略微上涨的情况下，实现了人均关注 10.06% 的提升。美图秀秀社区关注转化率提升了 12.03%，不过点击率也有略微的下降。&lt;/p&gt;

&lt;p&gt;样本 reweight 的方式改变了样本的原始分布，导致主目标存在比较大的预估偏差。同时，因为次要目标是通过主目标的网络结构来实现，无法对各个目标的模型分别进行调优，模型结构优化存在比较大的局限性。&lt;/p&gt;

&lt;h3 id=&quot;多目标模型&quot;&gt;多目标模型&lt;/h3&gt;

&lt;p&gt;多目标模型通过共享底层的网络输入，实现信息共享，再根据每个目标的数据特点，分别构建各个目标的输出网络，得到每个目标的输出。&lt;/p&gt;

&lt;p&gt;在美图的多个社交场景中，我们进行了尝试，并取得了比较大的在线提升。其中，在美拍双列 feed 流场景下，人均关注提升 11.43%，人均播放时长提升 12.45%。美图秀秀首页 feed 流，点击率提升 1.93%，关注率提升 2.9%。美图秀秀下滑 feed 流，关注率提升 9.3%，人均时长提升 10.33%。&lt;/p&gt;

&lt;h3 id=&quot;多模型&quot;&gt;多模型&lt;/h3&gt;

&lt;p&gt;虽然多目标模型在业务上取得了比较大的提升，但是仍然存在一些问题。典型的问题包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;当不同任务的目标相关性较弱，或者损失函数的输出值范围差异较大时，多目标模型的调优存在比较大的困难；&lt;/li&gt;
  &lt;li&gt;使用多目标模型，会导致不同目标的优化存在比较大的耦合，延迟整体优化进度，在产品要求快速迭代的场景下，这种技术手段不一定能够很好的满足业务需求。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;为了解决多目标模型存在的一些问题，我们通过拆分多目标模型的各个目标，得到多个单目标模型，并对每个单目标模型分别进行优化。在美拍双列 feed 流场景下，我们进行了相应的尝试，在人均时长不变的情况下，人均关注提升了 2.98%。通过进一步调整模型的优化目标，人均时长再次提升了 19.37%，人均关注提升了 14.1%。&lt;/p&gt;

&lt;h3 id=&quot;多个多目标模型&quot;&gt;多个多目标模型&lt;/h3&gt;

&lt;p&gt;当推荐场景的优化目标增加，多模型的方案会存在维护成本高，线上资源开销大，各个任务的模型无法利用其它任务的数据等问题。&lt;/p&gt;

&lt;p&gt;综合多模型和多目标模型的优点，采用多个多目标模型是解决多目标任务的有效手段。在美拍场景下，通过同时优化关注、时长、播放等目标，人均关注提升 12.18%，活跃留存提升 25.67%。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>非独立同分布的机器学习方法</title>
   <link href="http://daiwk.github.io/posts/dl-non-iid.html"/>
   <updated>2019-09-08T00:00:00+00:00</updated>
   <id>/posts/dl-non-iid</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;联邦学习相关可以看：&lt;a href=&quot;https://daiwk.github.io/posts/dl-federated-learning.html&quot;&gt;https://daiwk.github.io/posts/dl-federated-learning.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/BCQYdpgZdzdNLXZadptP1w&quot;&gt;若DL没了独立同分布假设，样本不独立的机器学习方法综述&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://people.ee.duke.edu/~lcarin/IJCAI07-121.pdf&quot;&gt;Learning Classifiers When The Training Data Is Not IID&lt;/a&gt;，ijcai，主要解决经典统计分析进行分类器预测过程中针对 Non-IID 数据的处理方法&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://arxiv.org/abs/1602.05629&quot;&gt;Communication-Efficient Learning of Deep Networks from Decentralized Data&lt;/a&gt; 为解决联邦学习中 Non-IID 数据问题，提出一种基于迭代模型平均的深层网络联合学习方法（Federated Averaging，FedAvg）&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1806.00582&quot;&gt;Federated Learning with Non-IID Data&lt;/a&gt;是针对（2）的分析和改进，使用客户端数据分布和中央服务器数据总体分布之间的土方运距 (earth mover』s distance, EMD) 计算权重散度，同时提出了一种数据共享（Data-Sharing）策略改进 FedAvg 的性能&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1907.02189&quot;&gt;On the Convergence of FedAvg on Non-IID Data&lt;/a&gt;重点讨论联邦学习问题中 FedAvg 在处理 Non-IID 数据时的收敛性问题，从理论角度证明了 FedAvg 的有效性&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://arxiv.org/abs/1811.12629v2&quot;&gt;LoAdaBoost:Loss-Based AdaBoost Federated Machine Learning on medical data&lt;/a&gt;基于 FedAvg 和数据共享策略提出了一种针对医学数据的提高联邦学习效率的自适应增强方法&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>neural structured learning(NSL)</title>
   <link href="http://daiwk.github.io/posts/platform-neural-structured-learning.html"/>
   <updated>2019-09-04T00:00:00+00:00</updated>
   <id>/posts/platform-neural-structured-learning</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/CvV16eK9EUm148dOw0EEcA&quot;&gt;五行代码用图提升模型表现，TensorFlow开源NSL神经结构学习框架&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/neural-structured-learning&quot;&gt;https://github.com/tensorflow/neural-structured-learning&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>tf-gan</title>
   <link href="http://daiwk.github.io/posts/platform-tf-gan.html"/>
   <updated>2019-08-31T00:00:00+00:00</updated>
   <id>/posts/platform-tf-gan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/pLpux2I6pLZZCqIqr7e9lg&quot;&gt;谷歌TF-GAN大升级！云TPU支持，兼容TF 2.0，全新免费教程！&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/gan/tree/master/tensorflow_gan/examples/self_attention_estimator&quot;&gt;https://github.com/tensorflow/gan/tree/master/tensorflow_gan/examples/self_attention_estimator&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/57i5mxK9oKAb9k77iElFBA&quot;&gt;TF - GAN入门：TensorFlow 2.0 的轻量级 GAN 库&lt;/a&gt;【这个东西不少，可以去看看。。】&lt;/p&gt;

&lt;p&gt;使用tf-gan的论文：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/gan#who-uses-tf-gan&quot;&gt;https://github.com/tensorflow/gan#who-uses-tf-gan&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>多gpu并行</title>
   <link href="http://daiwk.github.io/posts/platform-multigpu-parallel.html"/>
   <updated>2019-08-31T00:00:00+00:00</updated>
   <id>/posts/platform-multigpu-parallel</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1907.13257.pdf&quot;&gt;Optimizing Multi-GPU Parallelization Strategies for Deep Learning Training&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/P67T7uKNzXN3SpfhFv61WQ&quot;&gt;分布式训练中数据并行远远不够，「模型并行+数据并行」才是王道&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;数据并行化（Data parallelism，DP）是应用最为广泛的并行策略，但随着数据并行训练设备数量的增加，设备之间的通信开销也在增长。&lt;/p&gt;

&lt;p&gt;此外，每一个训练步中批大小规模的增加，使得模型统计效率（statistical efficiency）出现损失，即获得期望准确率所需的训练 epoch 增加。这些因素会影响整体的训练时间，而且当设备数超出一定量后，利用 DP 获得的加速无法实现很好的扩展。除 DP 以外，训练加速还可以通过模型并行化（model parallelism，MP）实现。&lt;/p&gt;

&lt;p&gt;来自加州大学洛杉矶分校和英伟达的研究人员探索了混合并行化方法，即每一个数据并行化 worker 包含多个设备，利用模型并行化分割模型数据流图（model dataflow graph，DFG）并分配至多个设备上。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>autogan</title>
   <link href="http://daiwk.github.io/posts/cv-autogan.html"/>
   <updated>2019-08-25T00:00:00+00:00</updated>
   <id>/posts/cv-autogan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/323RA6UtnyTySoXVXcJ2vA&quot;&gt;华人团队打造：AutoML + GAN = AutoGAN！AI设计GAN模型比人类更好&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1908.03835v1.pdf&quot;&gt;AutoGAN: Neural Architecture Search for Generative Adversarial Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github：&lt;a href=&quot;https://github.com/TAMU-VITA/AutoGAN&quot;&gt;https://github.com/TAMU-VITA/AutoGAN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/VdjHp5Tb1fSyV3CQblPQmw&quot;&gt;利用NAS寻找最佳GAN：AutoGAN架构搜索方案专为GAN打造&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>mrqa</title>
   <link href="http://daiwk.github.io/posts/nlp-mrqa.html"/>
   <updated>2019-08-19T00:00:00+00:00</updated>
   <id>/posts/nlp-mrqa</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/_eH1JKWNGsRxKi3KPeMOdw&quot;&gt;百度夺冠MRQA 2019，包揽测试集10项第一，飞桨加持&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/d-net.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>说话人分类模型</title>
   <link href="http://daiwk.github.io/posts/audio-speech-recognition-and-speaker-diarization.html"/>
   <updated>2019-08-17T00:00:00+00:00</updated>
   <id>/posts/audio-speech-recognition-and-speaker-diarization</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/g2GI11GJaAaRwe5TkA017w&quot;&gt;是谁在说话？谷歌多人语音识别新系统错误率降至2%&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1907.05337.pdf&quot;&gt;Joint Speech Recognition and Speaker Diarization via Sequence Transductio&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>HSIC-bottleneck</title>
   <link href="http://daiwk.github.io/posts/dl-hsic-bottleneck.html"/>
   <updated>2019-08-16T00:00:00+00:00</updated>
   <id>/posts/dl-hsic-bottleneck</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/jT6WU-XlcvP-n428oWyHQQ&quot;&gt;彻底解决梯度爆炸问题，新方法不用反向传播也能训练ResNet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1908.01580v1.pdf&quot;&gt;The HSIC bottleneck: deep learning without back-propagation&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>CorrNet</title>
   <link href="http://daiwk.github.io/posts/dl-corrnet.html"/>
   <updated>2019-08-11T00:00:00+00:00</updated>
   <id>/posts/dl-corrnet</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/Lx1mCVtuFKkeuP_ovFJopg&quot;&gt;基于Keras的关联神经网络CorrNet综合指南&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在机器学习中，这些来自不同异构源的数据类型称为多模态数据，如语音识别中的音频和视觉信息。由于多模态数据可能由不同维度和类型的数据构成，往往难以在训练中直接使用。因此许多研究致力于解决多模态数据的通用表示。&lt;/p&gt;

&lt;p&gt;学习多视图数据的通用表示有助于若干下游应用，如对视频及其对应的音频的通用表示进行学习，相比于单纯使用音频而言可以生成更加精确的字幕。&lt;/p&gt;

&lt;p&gt;尝试学习两种数据视图的共同表示，并且从该编码表示重构两种视图。&lt;/p&gt;

&lt;p&gt;相关神经网络（CorrNet）是学习通用表示的一种方法。它的架构与传统的单视图深度自动编码器几乎相同。但它为每种数据模态都构建了编解码对。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>concept activation vector(概念激活向量)</title>
   <link href="http://daiwk.github.io/posts/dl-concept-activation-vector.html"/>
   <updated>2019-08-10T00:00:00+00:00</updated>
   <id>/posts/dl-concept-activation-vector</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/QVXPr_lm7zcYf2LwsrhlfQ&quot;&gt;谷歌大脑提出概念激活向量，助力神经网络可解释性研究&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/tcav&quot;&gt;https://github.com/tensorflow/tcav&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1711.11279.pdf&quot;&gt;Interpretability Beyond Feature Attribution: Quantitative Testing with Concept Activation Vectors&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>pytorch-kaldi</title>
   <link href="http://daiwk.github.io/posts/audio-pytorch-kaldi.html"/>
   <updated>2019-08-01T00:00:00+00:00</updated>
   <id>/posts/audio-pytorch-kaldi</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766934&amp;amp;idx=4&amp;amp;sn=f165427c87ca1db28f252c8c50eac1c4&amp;amp;chksm=871aba28b06d333e3d9c4b8f963a191478c68ad04b9ea7955800c49ba6d99c280458a8534f4d&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=zAXdHORK5tTx549e9RwAgNcm7bjJrH4ENwbbTYVrAZDqpsE%2Fu1hY63b%2FoRfnZQdM#rd&quot;&gt;语音识别开源工具PyTorch-Kaldi：兼顾Kaldi效率与PyTorch灵活性&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github：&lt;a href=&quot;https://github.com/mravanelli/pytorch-kaldi&quot;&gt;https://github.com/mravanelli/pytorch-kaldi&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/abs/1811.07453&quot;&gt;The PyTorch-Kaldi Speech Recognition Toolkit&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>生成模型小结</title>
   <link href="http://daiwk.github.io/posts/dl-generative-models.html"/>
   <updated>2019-07-29T00:00:00+00:00</updated>
   <id>/posts/dl-generative-models</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652050247&amp;amp;idx=5&amp;amp;sn=6c4f22467360e3f0d57e3dc6ab5e018a&amp;amp;chksm=f12067b6c657eea0e328299e52b05d3a1603733300aac146cdac5ed237817831642b9b4c290e&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;O’Reilly新书：《生成式深度学习》，近5年最先进GAN一网打全！&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;电子书：&lt;a href=&quot;https://www.oreilly.com/library/view/generative-deep-learning/9781492041931/&quot;&gt;https://www.oreilly.com/library/view/generative-deep-learning/9781492041931/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/davidADSP/GDL_code&quot;&gt;https://github.com/davidADSP/GDL_code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766858&amp;amp;idx=3&amp;amp;sn=e4db96f69dd6a56e952db33254b61f1a&amp;amp;chksm=871aba74b06d33628d24a900e4e2c2088f34d471538215848645ce5e32aa0feb1e56d4e68ff0&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;生成式模型入门：训练似然模型的技巧&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原文：&lt;a href=&quot;https://blog.evjang.com/2019/07/likelihood-model-tips.html&quot;&gt;https://blog.evjang.com/2019/07/likelihood-model-tips.html&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SLATEQ</title>
   <link href="http://daiwk.github.io/posts/rl-slateq.html"/>
   <updated>2019-07-24T00:00:00+00:00</updated>
   <id>/posts/rl-slateq</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%83%8c%e6%99%af&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%bb%93%e5%90%88top-k-off-policy%e4%b8%80%e8%b5%b7%e7%9c%8b&quot;&gt;结合top-k off-policy一起看&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1905.12767.pdf&quot;&gt;Reinforcement Learning for Slate-based Recommender Systems: A Tractable Decomposition and Practical Methodology&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这个比较长。。38页&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://storage.googleapis.com/pub-tools-public-publication-data/pdf/9f91de1fa0ac351ecb12e4062a37afb896aa1463.pdf&quot;&gt;SLATEQ: A Tractable Decomposition for Reinforcement Learning with Recommendation Sets&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这个比较短。。&lt;/p&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

&lt;p&gt;xxx&lt;/p&gt;

&lt;h2 id=&quot;结合top-k-off-policy一起看&quot;&gt;结合top-k off-policy一起看&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/77494496&quot;&gt;以 YouTube 论文学习如何在推荐场景应用强化学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;一个是 off-policy，一个是 value-base，用 on-policy。&lt;/p&gt;

&lt;p&gt;好在推荐场景的样本收集成本低，量级比较大，但问题是存在较为&lt;strong&gt;严重的Bias&lt;/strong&gt;。即只有被系统&lt;strong&gt;展示过的物料才有反馈&lt;/strong&gt;，而且，还会有源源不断的&lt;strong&gt;新物料和用户&lt;/strong&gt;加入。&lt;/p&gt;

&lt;p&gt;off-policy的特点是，使用了两个policy，一个是用户behavior的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta\)&lt;/code&gt;，代表产生用户行为Trajectory：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((s_0,A_0,s_1, ..., )\)&lt;/code&gt;的策略，另一个是系统决策的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;，代表系统是如何在面对用户a在状态s下选择某个action的。&lt;/p&gt;

&lt;p&gt;off-policy的好处是一定程度上带了exploration，但也带来了问题。因此，常见的是引入importance weighting来解决。&lt;/p&gt;

&lt;p&gt;和标准的objective比，多了一个因子，因为这个&lt;strong&gt;因子是连乘&lt;/strong&gt;和rnn的问题类似，&lt;strong&gt;梯度容易爆炸或消失&lt;/strong&gt;。论文中用了一个近似解，并有人证明了是ok的。&lt;/p&gt;

&lt;p&gt;value-base虽然直观容易理解，但一直被质疑不能稳定的收敛。&lt;/p&gt;

&lt;p&gt;而policy-base则有较好的收敛性质，所以在很多推荐场景的RL应用，大部分会选择policy-base。当然现在也很有很多二者融合的策略，比如A3C、DDPG这种，也是比较流行的。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;的训练是比较常规的，有意思的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta\)&lt;/code&gt;的学习。用户的behavior是很难建模的，我们还是用nn的方式去学一个出来，这里有一个单独的分支去预估&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta\)&lt;/code&gt;，和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;是一个网络，但是它的梯度不回传。&lt;/p&gt;

&lt;p&gt;listwise的loss并不容易优化，复杂度较高。RL在推荐场景，也会遇到相同的问题。但直接做list推荐是不现实的，假设我们一次推荐K个物料，总共有N个物料，那么我们能选择的action就是一个排列组合问题，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C_N^K * K!\)&lt;/code&gt;个，当N是百万级时，量级非常夸张。&lt;/p&gt;

&lt;p&gt;youtube的两篇论文，都将问题从listwise（他们叫slatewise）转化成了itemwise。但这个itemwise和我们常规理解的pointwise的个性化技术还是有区别的。在于这个wise是reward上的表达，同时要引申出user choice model。&lt;/p&gt;

&lt;p&gt;pointwise的方法只考虑单个item的概率，论文中提出的itemwise，虽然也是认为最后的reward只和每个被选中的item有关，且item直接不互相影响，但它有对user choice做假设。比如论文[2]还做了更详细的假设，将目标函数的优化变成一个多项式内可解的问题&lt;/p&gt;

&lt;p&gt;SC是指用户一次指选择一个item，RTDS是指reward只和当前选择的item有关。&lt;/p&gt;

&lt;p&gt;有不少研究是专门针对user choice model的，一般在经济学中比较多。推荐中常见的有cascade model和mutilnomial logit model，比如cascade model，会认为用户选择某个item的概率是p，那么在一个list下滑的过程中，点击了第j个item的概率是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((1-p(i))^j * p(j)\)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;论文1中最后的objective中有一个因子，表达了user choice的假设：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\lambda_{K}\left(s_{t}, a_{t}\right)=\frac{\partial \alpha\left(a_{t} | s_{t}\right)}{\partial \pi\left(a_{t} | s_{t}\right)}=K\left(1-\pi_{\theta}\left(a_{t} | s_{t}\right)\right)^{K-1}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;简单理解就是，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;当做用户每次选择的概率，那上面就是K-1不选择a概率的连乘。而论文2中，RL模型和现有的监督模型是融合在一起的，直接用pCTR模型预估的pctr来当这个user choice的概率。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Large Memory Layers with Product Keys</title>
   <link href="http://daiwk.github.io/posts/dl-product-key-memory-layer.html"/>
   <updated>2019-07-13T00:00:00+00:00</updated>
   <id>/posts/dl-product-key-memory-layer</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652049263&amp;amp;idx=2&amp;amp;sn=32b5629eb8fd0619cc4d6fad9a854ef9&amp;amp;chksm=f1207b9ec657f288b392a0d49932211de93372dc133596d4cd5cae9aa1a9f9a83690b57beb98&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=zzUnWIgdqTLvX39vSLCKaOJN8KVDYuvxPgj7h5mQNNMiTnEMdrWSwBJSd3ch3aLL#rd&quot;&gt;LeCun力荐：Facebook推出十亿参数超大容量存储器&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1907.05242.pdf&quot;&gt;Large Memory Layers with Product Keys&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>label smoothing</title>
   <link href="http://daiwk.github.io/posts/dl-label-smoothing.html"/>
   <updated>2019-07-13T00:00:00+00:00</updated>
   <id>/posts/dl-label-smoothing</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI5MDUyMDIxNA==&amp;amp;mid=2247489604&amp;amp;idx=3&amp;amp;sn=227b76801ccb50fd7e565f5ed3ace402&amp;amp;chksm=ec1ff5bddb687cab29912fae97569b850b1dcb2227b4875ef32d0db8a0b8b99c538e6bacb120&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=zzUnWIgdqTLvX39vSLCKaOJN8KVDYuvxPgj7h5mQNNMiTnEMdrWSwBJSd3ch3aLL#rd&quot;&gt;Hinton等人最新研究：大幅提升模型准确率，标签平滑技术到底怎么用?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1906.02629&quot;&gt;When Does Label Smoothing Help?&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>layer rotation</title>
   <link href="http://daiwk.github.io/posts/dl-layer-rotation.html"/>
   <updated>2019-07-12T00:00:00+00:00</updated>
   <id>/posts/dl-layer-rotation</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650765628&amp;amp;idx=4&amp;amp;sn=5c73ad8f52558048f0f8d4759c0072f8&amp;amp;chksm=871abd42b06d34544e575a381843cdf6ac8c9d867925e091bf3872b9f62c5430a002403d7bef&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=zzUnWIgdqTLvX39vSLCKaOJN8KVDYuvxPgj7h5mQNNMiTnEMdrWSwBJSd3ch3aLL#rd&quot;&gt;层旋转：超强的深度网络泛化评价标准？&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>dlrm</title>
   <link href="http://daiwk.github.io/posts/platform-dlrm.html"/>
   <updated>2019-07-04T00:00:00+00:00</updated>
   <id>/posts/platform-dlrm</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650765233&amp;amp;idx=4&amp;amp;sn=3542b0b86ec78a81fef75feb39f2dfac&amp;amp;chksm=871ab3cfb06d3ad9999ed019ae623fff59db8ea77881bfc908f3d41432e8d7c9f0d56b7b58c6&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=g0RhlU91yTm4YwdL6HxxS6fDU%2FNvWsf8uqd5BGk9%2Fewn4u2UU5gMclDp6uVTk%2Bm3#rd&quot;&gt;想知道Facebook怎样做推荐？FB开源深度学习推荐模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/7YuLBli0_pz7eE4I6PpbyQ&quot;&gt;Facebook 面向个性化推荐系统的深度学习推荐模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebookresearch/dlrm&quot;&gt;https://github.com/facebookresearch/dlrm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1906.00091&quot;&gt;Deep Learning Recommendation Model for Personalization and Recommendation Systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原始博客：&lt;a href=&quot;https://ai.facebook.com/blog/dlrm-an-advanced-open-source-deep-learning-recommendation-model/&quot;&gt;https://ai.facebook.com/blog/dlrm-an-advanced-open-source-deep-learning-recommendation-model/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DLRM模型有两大类特征：连续（dense）特征和类别（sparse）特征。使用emb处理类别特征，使用下方的多层感知机（MLP）处理连续特征。然后显式地计算不同特征的二阶交互（second-order interaction）。最后，使用顶部的多层感知机处理结果，并输入sigmoid函数中，得出点击的概率。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/dlrm.gif&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

</content>
 </entry>
 
 <entry>
   <title>multi-sample dropout</title>
   <link href="http://daiwk.github.io/posts/dl-multi-sample-dropout.html"/>
   <updated>2019-06-07T00:00:00+00:00</updated>
   <id>/posts/dl-multi-sample-dropout</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1905.09788.pdf&quot;&gt;Multi-Sample Dropout for Accelerated Training and Better Generalization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650763415&amp;amp;idx=5&amp;amp;sn=9c7ccac83e5da883ffb05ca5b6d20954&amp;amp;chksm=871ab4e9b06d3dff984df2ffa38520a6ca6cee546919f527045467c12a26053c2a00fedadf1c&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;大幅减少训练迭代次数，提高泛化能力：IBM提出「新版Dropout」&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;简单地说，假设dropout的比例是0.5，那么dropout会在每轮训练中随机忽略（即 drop）&lt;strong&gt;50%的神经元&lt;/strong&gt;，以避免过拟合的发生。如此一来，神经元之间无法相互依赖，从而保证了神经网络的泛化能力。在infer时，会&lt;strong&gt;用到所有的神经元&lt;/strong&gt;，因此所有的信息都被保留；但&lt;strong&gt;输出值会乘0.5&lt;/strong&gt;，使&lt;strong&gt;平均值与训练时间一致&lt;/strong&gt;。这种推理网络可以看作是训练过程中随机生成的&lt;strong&gt;多个子网络的集合&lt;/strong&gt;。后来有一些变形，例如DropConnect，也就是&lt;a href=&quot;http://yann.lecun.com/exdb/publis/pdf/wan-icml-13.pdf&quot;&gt;Regularization of Neural Networks using DropConnect&lt;/a&gt;，随机忽略的是神经元之间的部分连接，而不是神经元。&lt;/p&gt;

&lt;p&gt;本文阐述的也是一种 dropout 技术的变形——multi-sample dropout。传统 dropout 在每轮训练时会从输入中随机选择一组样本（称之为 dropout 样本），而 multi-sample dropout 会创建多个 dropout 样本，然后平均所有样本的损失，从而得到最终的损失。这种方法只要在 dropout 层后复制部分训练网络，并在这些复制的全连接层之间共享权重就可以了，无需新运算符。&lt;/p&gt;

&lt;p&gt;通过综合 M 个 dropout 样本的损失来更新网络参数，使得最终损失比任何一个 dropout 样本的损失都低。这样做的效果类似于对一个 minibatch 中的每个输入重复训练 M 次。因此，它大大减少了训练迭代次数。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>GAN-CDQN</title>
   <link href="http://daiwk.github.io/posts/dl-gan-rl-recommend.html"/>
   <updated>2019-06-05T00:00:00+00:00</updated>
   <id>/posts/dl-gan-rl-recommend</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%ae%80%e4%bb%8b&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#setting%e5%92%8crl-formulation&quot;&gt;Setting和RL Formulation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#generative-adversarial-user-model&quot;&gt;Generative Adversarial User Model&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#user-behavior-as-reward-maximization&quot;&gt;User Behavior As Reward Maximization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#model-parameterization&quot;&gt;Model Parameterization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#generative-adversarial-training&quot;&gt;Generative Adversarial Training&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cascading-rl-policy-for-recommendation&quot;&gt;Cascading RL Policy for Recommendation&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#cascading-q-networks&quot;&gt;Cascading Q-Networks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#parameterization-and-estimation&quot;&gt;Parameterization and Estimation&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650763260&amp;amp;idx=3&amp;amp;sn=ae589196211189a8aba6f56a11e2cccb&amp;amp;chksm=871aab82b06d22942d37c9b6efe33cd9647050599f293e59ffc8ababeedb9f8c80afbd80b509&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;ICML 2019 | 强化学习用于推荐系统，蚂蚁金服提出生成对抗用户模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1812.10613.pdf&quot;&gt;Generative Adversarial User Model for Reinforcement Learning Based Recommendation System&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/xinshi-chen/GenerativeAdversarialUserModel&quot;&gt;https://github.com/xinshi-chen/GenerativeAdversarialUserModel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ppt：&lt;a href=&quot;https://icml.cc/media/Slides/icml/2019/201(11-14-00)-11-14-25-4831-generative_adve.pdf&quot;&gt;https://icml.cc/media/Slides/icml/2019/201(11-14-00)-11-14-25-4831-generative_adve.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;本文提出利用&lt;strong&gt;生成对抗网络&lt;/strong&gt;同时学习&lt;strong&gt;用户行为模型transition&lt;/strong&gt;以及&lt;strong&gt;奖励函数reward&lt;/strong&gt;。将该&lt;strong&gt;用户模型&lt;/strong&gt;作为强化学习的&lt;strong&gt;模拟环境&lt;/strong&gt;，研究者开发了全新的&lt;strong&gt;Cascading-DQN&lt;/strong&gt;算法，从而得到了可以&lt;strong&gt;高效处理大量候选&lt;/strong&gt;物品的&lt;strong&gt;组合推荐&lt;/strong&gt;策略。&lt;/p&gt;

&lt;p&gt;本文用真实数据进行了实验，发现和其它相似的模型相比，这一&lt;strong&gt;生成对抗用户模型&lt;/strong&gt;可以&lt;strong&gt;更好地解释用户行为&lt;/strong&gt;，而基于该模型的RL策略可以给用户带来&lt;strong&gt;更好的长期收益&lt;/strong&gt;，并给系统提供&lt;strong&gt;更高的点击率&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;RL在推荐场景中有以下问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先，驱动用户行为的兴趣点&lt;strong&gt;（奖励函数）一般是未知的&lt;/strong&gt;，但它对于 RL 算法的使用来说至关重要。在用于推荐系统的现有RL算法中，奖励函数一般是&lt;strong&gt;手动设计的（例如用 ±1 表示点击或不点击）&lt;/strong&gt;，这可能无法反映出用户对不同项目的偏好如何(如&lt;a href=&quot;https://arxiv.org/abs/1805.02343&quot;&gt;Deep Reinforcement Learning for Page-wise Recommendations&lt;/a&gt;)。&lt;/li&gt;
  &lt;li&gt;其次，&lt;strong&gt;无模型&lt;/strong&gt;RL一般都需要&lt;strong&gt;和环境（在线用户）进行大量的交互&lt;/strong&gt;才能学到良好的策略。但这在推荐系统设置中是不切实际的。如果推荐看起来比较随机或者推荐结果不符合在线用户兴趣，他会很快&lt;strong&gt;放弃&lt;/strong&gt;!!这一服务。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;为了解决&lt;strong&gt;无模型&lt;/strong&gt;方法&lt;strong&gt;样本复杂度大&lt;/strong&gt;的问题，&lt;strong&gt;基于模型&lt;/strong&gt;的RL方法更为可取。近期有一些研究，在robotics applications中，在&lt;strong&gt;相关但不相同&lt;/strong&gt;的&lt;strong&gt;环境&lt;/strong&gt;设置中训练机器人策略，结果表明&lt;strong&gt;基于模型&lt;/strong&gt;的RL&lt;strong&gt;采样效率更高&lt;/strong&gt;。如&lt;a href=&quot;https://arxiv.org/abs/1708.02596&quot;&gt;Neural Network Dynamics for Model-Based Deep Reinforcement Learning with Model-Free Fine-Tuning&lt;/a&gt;，还有&lt;a href=&quot;https://www.doc.ic.ac.uk/~mpd37/publications/pami_final_w_appendix.pdf&quot;&gt;Gaussian Processes for Data-Efficient Learning in Robotics and Control&lt;/a&gt;，还有&lt;a href=&quot;https://arxiv.org/abs/1803.11347&quot;&gt; Learning to adapt: Meta-learning for model-based control&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;基于模型的方法的优势在于可以&lt;strong&gt;池化&lt;/strong&gt;大量的&lt;strong&gt;off-policy数据&lt;/strong&gt;，而且可以用这些数据学习良好的环境动态模型，而无模型方法只能用&lt;strong&gt;昂贵的on-policy&lt;/strong&gt;数据学习。但之前基于模型的方法一般都是根据物理或高斯过程设计的，而&lt;strong&gt;不是根据用户行为的复杂序列&lt;/strong&gt;定制的。&lt;/p&gt;

&lt;p&gt;本文的框架用统一的minimax框架学习&lt;strong&gt;用户行为模型&lt;/strong&gt;和相关的&lt;strong&gt;奖励函数&lt;/strong&gt;，然后再用这个模型&lt;strong&gt;学习RL策略&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;主要贡献如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;开发了生成对抗学习（GAN）方法来对用户行为动态性(dynamics)建模，并recover奖励函数。可以通过&lt;strong&gt;联合的minimax优化算法&lt;/strong&gt;同时评估这两个组件。该方法的优势在于：
    &lt;ul&gt;
      &lt;li&gt;可以得到&lt;strong&gt;更predictive(可预测的？)的用户模型&lt;/strong&gt;，而且可以用&lt;strong&gt;与用户模型一致&lt;/strong&gt;的方法&lt;strong&gt;学习奖励函数&lt;/strong&gt;；&lt;/li&gt;
      &lt;li&gt;相较于手动设计的简单奖励函数，&lt;strong&gt;从用户行为中学习到的奖励函数&lt;/strong&gt;更&lt;strong&gt;有利于&lt;/strong&gt;后面的&lt;strong&gt;强化学习&lt;/strong&gt;；&lt;/li&gt;
      &lt;li&gt;学习到的用户模型使研究者能够为&lt;strong&gt;新用户&lt;/strong&gt;执行基于模型的RL和在线适应从而实现更好的结果。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;用这一模型作为模拟环境，研究者还开发了&lt;strong&gt;级联DQN&lt;/strong&gt;(cascade dqn)算法来获得组合推荐策略。&lt;strong&gt;动作-值函数&lt;/strong&gt;的&lt;strong&gt;级联设计&lt;/strong&gt;允许其在&lt;strong&gt;大量候选物品&lt;/strong&gt;中找到要展示的物品的&lt;strong&gt;最佳子集&lt;/strong&gt;，其&lt;strong&gt;时间复杂度&lt;/strong&gt;和&lt;strong&gt;候选物品的数量&lt;/strong&gt;呈&lt;strong&gt;线性关系&lt;/strong&gt;，大大减少了计算难度。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;用真实数据进行实验得到的结果表明，从保留似然性和点击预测的角度来说，这种生成对抗模型可以&lt;strong&gt;更好地拟合用户行为&lt;/strong&gt;。根据学习到的用户模型和奖励，研究者发现评估推荐策略可以给用户带来&lt;strong&gt;更好的长期累积奖励&lt;/strong&gt;。此外，在&lt;strong&gt;模型不匹配&lt;/strong&gt;的情况下，基于模型的策略也能够&lt;strong&gt;很快地适应新动态&lt;/strong&gt;（和无模型方法相比，&lt;strong&gt;和用户交互的次数要少得多&lt;/strong&gt;）。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/gan-cdqn-illustration.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;图中绿线是推荐的信息流，黄线是用户的信息流。&lt;/p&gt;

&lt;h2 id=&quot;setting和rl-formulation&quot;&gt;Setting和RL Formulation&lt;/h2&gt;

&lt;p&gt;setting：给用户展示了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个item，然后他点了&lt;strong&gt;1个或者0个&lt;/strong&gt;，然后展示后&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个item。&lt;/p&gt;

&lt;p&gt;简单来讲，RL框架就是，推荐系统会在用户状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{s}\)&lt;/code&gt;下，采用策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(\boldsymbol{s}, \mathcal{I})\)&lt;/code&gt;来从集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}\)&lt;/code&gt;中进行选择，使得如下的用户长期累积reward最大：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
 \pi^{*}=\underset{\pi\left(\boldsymbol{s}^{t}, \mathcal{I}^{t}\right)}{\arg \max } \mathbb{E}\left[\sum_{t=0}^{\infty} \gamma^{t} r\left(\boldsymbol{s}^{t}, a^{t}\right)\right]
 \]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s^{0} \sim p^{0}\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{t} \sim \pi\left(s^{t}, \mathcal{I}^{t}\right)\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{s}^{t+1} \sim P\left(\cdot | \boldsymbol{s}^{t}, \mathcal{A}^{t}\right)\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{t} \in \mathcal{A}^{t}\)&lt;/code&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;环境：在推荐的每一页的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个item中可以点击其中一个的用户&lt;/li&gt;
  &lt;li&gt;状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{s}^{t} \in \mathcal{S}\)&lt;/code&gt;：用户&lt;strong&gt;历史点击&lt;/strong&gt;的一个&lt;strong&gt;有序序列&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{t} \in\left(\begin{array}{c}{\mathcal{I}^{t}} \\ {k}\end{array}\right)\)&lt;/code&gt;：推荐系统从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}^{t}\)&lt;/code&gt;个候选中选择&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个候选的&lt;strong&gt;子集&lt;/strong&gt;。其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left(\begin{array}{c}{\mathcal{I}^{t}} \\ {k}\end{array}\right)\)&lt;/code&gt;表示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}^{t}\)&lt;/code&gt;中的所有的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;元素子集。而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}^{t} \subset \mathcal{I}\)&lt;/code&gt;是所有候选&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}\)&lt;/code&gt;在时间步&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;的候选子集。&lt;/li&gt;
  &lt;li&gt;状态转移&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\left(\cdot | s^{t}, \mathcal{A}^{t}\right) : \mathcal{S} \times\left(\begin{array}{l}{\mathcal{I}} \\ {k}\end{array}\right) \mapsto \mathcal{P}(\mathcal{S})\)&lt;/code&gt;：给定状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{s}^{t}\)&lt;/code&gt;，以及展示的集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{t}\)&lt;/code&gt;的情况下，转移到状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{s}^{t+1}\)&lt;/code&gt;的转移概率。等价于&lt;a href=&quot;#user-behavior-as-reward-maximization&quot;&gt;下文&lt;/a&gt;提到的在用户行为上的分布&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi\left(s^{t}, \mathcal{A}^{t}\right)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;奖励函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\left(\boldsymbol{s}^{t}, \mathcal{A}^{t}, a^{t}\right) : \mathcal{S} \times\left(\begin{array}{l}{\mathcal{I}} \\ {k}\end{array}\right) \times \mathcal{I} \mapsto \mathbb{R}\)&lt;/code&gt;：用户在状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{s}^{t}\)&lt;/code&gt;下，采用动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{t} \in \mathcal{A}^{t}\)&lt;/code&gt;得到的回报。在这里假设推荐系统得到的回报和用户得到的回报一样，所以长期回报也是一样。&lt;/li&gt;
  &lt;li&gt;策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{t} \sim \pi\left(s^{t}, \mathcal{I}^{t}\right) : \mathcal{S} \times 2^{\mathcal{I}} \mapsto \mathcal{P}\left(\left(\begin{array}{c}{\mathcal{I}_{k}} \\ {k}\end{array}\right)\right)\)&lt;/code&gt;：在用户状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{s}^{t}\)&lt;/code&gt;下，从集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}^{t}\)&lt;/code&gt;中选择子集&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{t}\)&lt;/code&gt;进行展示的概率。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;可见，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;环境&lt;/strong&gt;、&lt;strong&gt;状态&lt;/strong&gt;、&lt;strong&gt;状态转移&lt;/strong&gt;与&lt;strong&gt;用户&lt;/strong&gt;有关，&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;行为&lt;/strong&gt;、&lt;strong&gt;策略&lt;/strong&gt;与&lt;strong&gt;推荐系统&lt;/strong&gt;有关。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;回报&lt;/strong&gt;与&lt;strong&gt;推荐系统&lt;/strong&gt;和&lt;strong&gt;用户&lt;/strong&gt;均有关。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\left(\boldsymbol{s}^{t}, \mathcal{A}^{t}, a^{t}\right)\)&lt;/code&gt;来强调回报对推荐的action的依赖，也就是说，用户只能从展示的结果集中进行选择。其实，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\left(\boldsymbol{s}^{t}, \mathcal{A}^{t}, a^{t}\right)=r\left(\boldsymbol{s}^{t}, a^{t}\right) \cdot \mathbf{1}\left(a^{t} \in \mathcal{A}^{t}\right)\)&lt;/code&gt;。所以&lt;a href=&quot;#user-behavior-as-reward-maximization&quot;&gt;下文&lt;/a&gt;在讲用户模型的时候，就使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\left(\boldsymbol{s}^{t}, a^{t}\right)=r\left(\boldsymbol{s}^{t}, \mathcal{A}^{t}, a^{t}\right)\)&lt;/code&gt;来表示了，假设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{t} \in \mathcal{A}^{t}\)&lt;/code&gt;是true。&lt;/p&gt;

&lt;p&gt;reward函数和transition都是未知的，可以从数据中学习。只要这两个学好了，那么就可以通过使用例如Q-learrning等算法，不断地对模型进行query，来估计上文提到的最优的策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\( \pi^{*}=\underset{\pi\left(\boldsymbol{s}^{t}, \mathcal{I}^{t}\right)}{\arg \max } \mathbb{E}\left[\sum_{t=0}^{\infty} \gamma^{t} r\left(\boldsymbol{s}^{t}, a^{t}\right)\right]\)&lt;/code&gt;。&lt;/p&gt;

&lt;h2 id=&quot;generative-adversarial-user-model&quot;&gt;Generative Adversarial User Model&lt;/h2&gt;

&lt;p&gt;找reward的过程，其实是一个逆向强化学习的过程，可以参考&lt;a href=&quot;https://blog.csdn.net/philthinker/article/details/79778271&quot;&gt;漫谈逆向强化学习 - A Brief Review to Inverse Reinforcement Learning&lt;/a&gt;：&lt;/p&gt;

&lt;p&gt;什么是逆向强化学习呢？当完成复杂的任务时，强化学习的回报函数很难指定，我们希望有一种方法找到一种高效可靠的回报函数，这种方法就是逆向强化学习。我们假设专家在完成某项任务时，其决策往往是最优的或接近最优的，当所有的策略产生的累积汇报函数期望都不比专家策略产生的累积回报期望大时，强化学习所对应的回报函数就是根据示例学到的回报函数。即逆向强化学习就是&lt;strong&gt;从专家示例中学习回报函数&lt;/strong&gt;。当需要基于最优序列样本学习策略时，我们可以结合逆向强化学习和强化学习共同提高回报函数的精确度和策略的效果。&lt;/p&gt;

&lt;p&gt;而paper的话，可以看吴恩达的&lt;a href=&quot;https://ai.stanford.edu/~ang/papers/icml00-irl.pdf&quot;&gt;Algorithms for Inverse Reinforcement Learning&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;受imitation learning的启发，通过&lt;strong&gt;expert demonstration&lt;/strong&gt;来学习&lt;strong&gt;序列决策策略(sequential decision-making policies)&lt;/strong&gt;，参考Abbeel和吴恩达的&lt;a href=&quot;https://ai.stanford.edu/~ang/papers/icml04-apprentice.pdf&quot;&gt;Apprenticeship learning via inverse reinforcement learning&lt;/a&gt;【其中的”Apprenticeship”的意思就是学徒】，还有&lt;a href=&quot;https://arxiv.org/abs/1605.08478&quot;&gt;Model-free imitation learning with policy optimization&lt;/a&gt;，还有&lt;a href=&quot;https://arxiv.org/abs/1606.03476&quot;&gt;Generative adversarial imitation learning&lt;/a&gt;，还有&lt;a href=&quot;https://arxiv.org/abs/1805.01954&quot;&gt;Behavioral Cloning from Observation&lt;/a&gt;。因此，本文提出了一个unified mini-max optimization来基于sample trajectories(轨迹)来同时学习用户行为模型和回报函数。&lt;/p&gt;

&lt;h3 id=&quot;user-behavior-as-reward-maximization&quot;&gt;User Behavior As Reward Maximization&lt;/h3&gt;

&lt;p&gt;基于如下两个现实(realistic)的假设来对用户行为建模：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;用户不是消极的(passive)。当用户看到展现的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个item时，会做出令他自己的回报最大的决定。回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;意味着这个用户对这个item有多感兴趣或者多满意。而如果他都不感兴趣，可以选择都不点。&lt;/li&gt;
  &lt;li&gt;reward不仅和当前这个被选择的item有关，也和用户的历史有关。例如，一个用户听了a的某一首歌，可能他会对a的其他歌也感兴趣；而如果他听了很多a的歌，可能他也会感到厌倦了。这些都是和personal experience有关的。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;把点击的item(视为用户的action &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{t}\)&lt;/code&gt;)还有用户的历史(视为状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{s}^{t}\)&lt;/code&gt;)都作为reward函数的输入：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\left(s^{t}, a^{t}\right)\)&lt;/code&gt;。而没有点击的item会被视为special item或者action。&lt;/p&gt;

&lt;p&gt;假设在session &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;，展示给用户&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个item &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{t}=\left\{a_{1}, \cdots, a_{k}\right\}\)&lt;/code&gt;，而他们对应的特征是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left\{\boldsymbol{f}_{1}^{t}, \cdots, \boldsymbol{f}_{k}^{t}\right\}\)&lt;/code&gt;。然后他采用可以使自己的期望reward最大的策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi^{*}\)&lt;/code&gt;来做出action &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{t} \in \mathcal{A}^{t}\)&lt;/code&gt;。这个策略可以看成是在一个候选action集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{t}\)&lt;/code&gt;上的概率分布：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\phi^{*}\left(\boldsymbol{s}^{t}, \mathcal{A}^{t}\right)=\arg \max _{\phi \in \Delta^{k-1}} \mathbb{E}_{\phi}\left[r\left(\boldsymbol{s}^{t}, a^{t}\right)\right]-R(\phi) / \eta
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Delta^{k-1}\)&lt;/code&gt;是probability simplex，也就是概率&lt;a href=&quot;https://zh.wikipedia.org/wiki/%E5%8D%95%E7%BA%AF%E5%BD%A2&quot;&gt;单纯形&lt;/a&gt;，参考&lt;a href=&quot;https://juejin.im/entry/58e09c2cda2f60005fcd5573&quot;&gt;https://juejin.im/entry/58e09c2cda2f60005fcd5573&lt;/a&gt;，简单理解好像。。就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个元素，和为1，所以可以看成是一个概率分布。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R(\phi)\)&lt;/code&gt;是一个凸的正则函数。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;能控制正则化的强度&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;引理1：假设正则项是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R(\phi)=\sum_{i=1}^{k} \phi_{i} \log \phi_{i}\)&lt;/code&gt;，也就是negative Shannon entropy，而且&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi \in \Delta^{k-1}\)&lt;/code&gt;是任意一种mapping。然后这个最优策略有如下逼近形式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\phi^{*}\left(\boldsymbol{s}^{t}, \mathcal{A}^{t}\right)_{i}=\exp \left(\eta r\left(\boldsymbol{s}^{t}, a_{i}\right)\right) / \sum_{a_{j} \in \mathcal{A}^{t}} \exp \left(\eta r\left(\boldsymbol{s}^{t}, a_{j}\right)\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;进一步地，在每一个session &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;中，用户的最优策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi^{*}\)&lt;/code&gt;与如下离散的choice model是等价的，其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon^{t}\)&lt;/code&gt;服从Gumbel分布（参考&lt;a href=&quot;https://blog.csdn.net/JackyTintin/article/details/79364490&quot;&gt;【Learning Notes】Gumbel 分布及应用浅析&lt;/a&gt;），wikipedia的解释&lt;a href=&quot;https://en.wikipedia.org/wiki/Gumbel_distribution&quot;&gt;https://en.wikipedia.org/wiki/Gumbel_distribution&lt;/a&gt;，简单来说是一个极值分布，比如每个点是周围若干个点的max或者min这种。。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
a^{t}=\arg \max _{a \in \mathcal{A}^{t}} \eta r\left(\boldsymbol{s}^{t}, a\right)+\varepsilon^{t}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如上引理说明了，用户根据reward function去greedily地选择一个item(exploitation)，而其中的Gumbel noise &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon^{t}\)&lt;/code&gt;使得用户可以去deviate(偏差)和explore其他reward相对小一点的item。在经济学模型中，已经有类似的方法了，例如&lt;a href=&quot;https://www.sciencedirect.com/science/article/pii/0304407675900329&quot;&gt;Maximum score estimation of the stochastic utility model of choice&lt;/a&gt;，还有&lt;a href=&quot;https://eml.berkeley.edu/reprints/mcfadden/zarembka.pdf&quot;&gt;Conditional logit analysis of qualitative choice behaviour&lt;/a&gt;，但之前的经济学模型并没有把多样的特征还有用户状态的演变考虑进去。可见，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;越小，越偏向explore。不过，因为每个人的reward也不一样，所以实际应用的时候，简单地设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta=1\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;注意：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;其他的正则&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R(\phi)\)&lt;/code&gt;也可以用，这样&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi^{*}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;的关系也会变，也就不一定会有那个逼近的形式了&lt;/li&gt;
  &lt;li&gt;对于用户没有点击任意一个item这种情况，可以看成一直在展现集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{t}\)&lt;/code&gt;中的一个特殊的item。这个item的feature vector可以都搞成0，或者可以把reward定义成一个常量。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;model-parameterization&quot;&gt;Model Parameterization&lt;/h3&gt;

&lt;p&gt;使用用户在session &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;之前历史点击的embedding来表示状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{s}^{t}\)&lt;/code&gt;，然后基于状态和当前action &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{t}\)&lt;/code&gt;的embedding来定义reward函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\left(\boldsymbol{s}^{t}, a^{t}\right)\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;定义用户的状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{s}^{t} :=h\left(\boldsymbol{F}_{*}^{1 : t-1} :=\left[\boldsymbol{f}_{*}^{1}, \cdots, \boldsymbol{f}_{*}^{t-1}\right]\right)\)&lt;/code&gt;，其中每一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{f}_{*}^{\tau} \in \mathbb{R}^{d}\)&lt;/code&gt;是session &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau\)&lt;/code&gt;的点击item的特征向量，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h(\cdot)\)&lt;/code&gt;是一个embedding函数，本文提出了一种简单且有效的position weighting scheme。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{W} \in \mathbb{R}^{m \times n}\)&lt;/code&gt;是一个行数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m\)&lt;/code&gt;是一个固定的历史的时间步数，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;列里每一列与positions上的importance weights的集合有关。所以embedding函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h \in \mathbb{R}^{d n \times 1}\)&lt;/code&gt;可以设计成如下形式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\boldsymbol{s}^{t}=h\left(\boldsymbol{F}_{*}^{t-m : t-1}\right) :=\operatorname{vec}\left[\sigma\left(\boldsymbol{F}_{*}^{t-m : t-1} \boldsymbol{W}+\boldsymbol{B}\right)\right]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{B} \in \mathbb{R}^{d \times n}\)&lt;/code&gt;是一个bias矩阵。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma(\cdot)\)&lt;/code&gt;是非线性变换。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\operatorname{vec}[\cdot]\)&lt;/code&gt;把输入矩阵的列concate到一起，形成一个长向量。当然，也可以使用LSTM来对历史进行建模。但position weighting scheme是浅层网络，比RNN在前向计算和反向传播上都更加高效。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/gan-cdqn.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;接下来，定义reward函数还有用户行为模型。&lt;/p&gt;

&lt;p&gt;用户的选择&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{t} \in \mathcal{A}^{t}\)&lt;/code&gt;和特征是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{f}_{a^{t}}^{t}\)&lt;/code&gt;的item有关，所以，reward定义如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
r\left(\boldsymbol{s}^{t}, a^{t}\right) :=\boldsymbol{v}^{\top} \sigma\left(\boldsymbol{V}\left[\begin{array}{c}{\boldsymbol{s}^{t}} \\ {\boldsymbol{f}_{a^{t}}^{t}}\end{array}\right]+\boldsymbol{b}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;用户行为模型如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\phi\left(s, \mathcal{A}^{t}\right) \propto \exp \left(\boldsymbol{v}^{\prime \top} \sigma\left(\boldsymbol{V}^{\prime}\left[\begin{array}{c}{\boldsymbol{s}^{t}} \\ {\boldsymbol{f}_{a^{t}}^{t}}\end{array}\right]+\boldsymbol{b}^{\prime}\right)\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{V}, \boldsymbol{V}^{\prime} \in \mathbb{R}^{\ell \times(d n+d)}\)&lt;/code&gt;是权重矩阵，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{b}, \boldsymbol{b}^{\prime} \in \mathbb{R}^{\ell \times 1}\)&lt;/code&gt;是bias&lt;strong&gt;向量&lt;/strong&gt;(原文写错了，和作者邮件确认了应该是这个维数)，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{v}, \boldsymbol{v}^{\prime} \in \mathbb{R}^{\ell}\)&lt;/code&gt;是最终的regression参数。&lt;/p&gt;

&lt;p&gt;为了简化一点，把reward的所有参数定义为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，而用户模型的所有参数定义为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha\)&lt;/code&gt;，因此，reward就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_{\theta}\)&lt;/code&gt;，而用户模型就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi_{\alpha}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;自己来梳理一下。。有m个时间步，每个时间步的f是dx1维的，所以F是dxm，而w是mxn，所以乘完后是个dxn，然后这个vec的操作就是把n列竖着叠到一起，变成一个dnx1的向量。这就是s。然后那个s,f呢，f只是一个item，所以是dx1维，而s是dnx1，把这两个竖着叠在一起就是(n+1)xd=dn+d这么多行，所以V就是lx(dn+d)。V乘以s和f的那个，出来就是一个lx1的。最后的r是一个标量吧。&lt;/p&gt;

&lt;h3 id=&quot;generative-adversarial-training&quot;&gt;Generative Adversarial Training&lt;/h3&gt;

&lt;p&gt;上面提到的reward函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\left(s^{t}, a^{t}\right)\)&lt;/code&gt;和用户行为模型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi\left(s^{t}, \mathcal{A}^{t}\right)\)&lt;/code&gt;均是未知的，需要从数据中学习。用户行为模型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi\)&lt;/code&gt;试图模仿真实用户最大化其reward &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;的真实action序列。根据gan的术语，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi\)&lt;/code&gt;可以看做是一个generator。基于用户的历史，产生用户的下一个行为。参数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha\)&lt;/code&gt;，要让把假的当成真实最大，所以在下面的式子里，需要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha\)&lt;/code&gt;最大！&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;可以看做是一个discriminator，试图分辨出用户的真实行为与generator产生的用户行为。参数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，要让把真实的当成真实最大，而下面的式子第二项前有个负号，所以要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;最小。。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;给定一个有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T\)&lt;/code&gt;个已观测action的轨迹(trajectory)&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left\{a_{\text {true}}^{1}, a_{\text {true}}^{2}, \ldots, a_{\text {true}}^{T}\right\}\)&lt;/code&gt;，以及对应的点击item的特征&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left\{\boldsymbol{f}_{*}^{1}, \boldsymbol{f}_{*}^{2}, \ldots, \boldsymbol{f}_{*}^{T}\right\}\)&lt;/code&gt;，解决如下mini-max的优化问题：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} \min _{\theta} \max _{\alpha}\left(\mathbb{E}_{\phi_{\alpha}}\right.&amp;amp;\left[\sum_{t=1}^{T} r_{\theta}\left(\boldsymbol{s}_{\text {true}}^{t}, a^{t}\right)\right]-R\left(\phi_{\alpha}\right) / \eta ) -\sum_{t=1}^{T} r_{\theta}\left(\boldsymbol{s}_{\text {true}}^{t}, a_{\text {true}}^{t}\right) \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{s}_{\text {true}}^{t}\)&lt;/code&gt;用来强调这是观测到的数据。上式前面那项是基于真实state使用用户模型产出的action得到的reward，也就是正常gan里的D(G(z))，后面一项是真实的state下真实action的reward，也就是正常gan里的D(x)。&lt;/p&gt;

&lt;p&gt;对于一般化的正则项&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\left(\phi_{\alpha}\right)\)&lt;/code&gt;，mini-max的优化问题并没有一个逼近形式，所以需要通过交替更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi_{\alpha}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_{\theta}\)&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\left\{\begin{array}{l}{\alpha \leftarrow \alpha+\gamma_{1} \nabla_{\alpha} \mathbb{E}_{\phi_{\alpha}}\left[\sum_{t=1}^{T} r_{\theta}\right]-\gamma_{1} \nabla_{\alpha} R\left(\phi_{\alpha}\right) / \eta} \\ {\theta \leftarrow \theta-\gamma_{2} \mathbb{E}_{\phi_{\alpha}}\left[\sum_{t=1}^{T} \nabla_{\theta} r_{\theta}\right]+\gamma_{2} \sum_{t=1}^{T} \nabla_{\theta} r_{\theta}}\end{array}\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这个更新过程可能不一定会stable，因为这本身可能是一个非凸问题。所以可以在初始化的时候加个特殊的正则。对于entropy的正则，有个如下引理2的逼近形式：&lt;/p&gt;

&lt;p&gt;引理2：假设正则项是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R(\phi)=\sum_{i=1}^{k} \phi_{i} \log \phi_{i}\)&lt;/code&gt;，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Phi\)&lt;/code&gt;包含了所有的从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{S} \times\left(\begin{array}{l}{\mathcal{I}} \\ {k}\end{array}\right)\)&lt;/code&gt;映射到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Delta^{k-1}\)&lt;/code&gt;的mapping。那么如上的优化问题可以等价为如下最大化likelihood的估计：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\max _{\theta \in \Theta} \prod_{t=1}^{T} \frac{\exp \left(\eta r_{\theta}\left(s_{t r u e}^{t}, a_{t r u e}^{t}\right)\right)}{\sum_{a^{t} \in \mathcal{A}^{t}} \exp \left(\eta r_{\theta}\left(s_{t r u e}^{t}, a^{t}\right)\right)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;当entropy正则的reward函数学习完了之后，能用来对其他形式的正则进行初始化。&lt;/p&gt;

&lt;h2 id=&quot;cascading-rl-policy-for-recommendation&quot;&gt;Cascading RL Policy for Recommendation&lt;/h2&gt;

&lt;p&gt;推荐策略需要处理&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left(\begin{array}{l}{\mathcal{I}} \\ {k}\end{array}\right)\)&lt;/code&gt;（组合数）这么一个combinatorial action space，其中每个action是从有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;个候选的大集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}\)&lt;/code&gt;中挑选出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个item的子集。有两个挑战：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;在combinatorial action space这个空间上的可能的很高的计算复杂度&lt;/li&gt;
  &lt;li&gt;对某种item组合的长期reward(Q)的预估也需要一个复杂度较高的框架&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;cascading-q-networks&quot;&gt;Cascading Q-Networks&lt;/h3&gt;

&lt;p&gt;用户的一次请求，系统需要从有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;个候选的大集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}\)&lt;/code&gt;中挑选出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个item的子集&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;最优的Q如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q^{*}\left(s^{t}, \mathcal{A}^{t}\right)=\mathbb{E}\left[r\left(s^{t}, \mathcal{A}^{t}, a^{t}\right)+\gamma \max _{\mathcal{A}^{\prime} \subset \mathcal{I}} Q^{*}\left(s^{t+1}, \mathcal{A}^{\prime}\right)\right], a^{t} \in \mathcal{A}^{t}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而学到了这个最优的Q之后，最优的推荐策略就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi^{*}\left(\boldsymbol{s}^{t}, \mathcal{I}^{t}\right)=\arg \max _{\mathcal{A}^{t} \subset \mathcal{I}^{t}} Q^{*}\left(\boldsymbol{s}^{t}, \mathcal{A}^{t}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{I}^{t} \subset \mathcal{I}\)&lt;/code&gt;是在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;时候的item候选集合。挑战就是，组合数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left(\begin{array}{l}{K} \\ {k}\end{array}\right)\)&lt;/code&gt;是非常巨大的，即使是不大的K=1000, k=5，组合数也有1.6亿！。。而且，同一个item，在不同的组合里，被点击的概率也会因不同用户在不同时刻而不同。&lt;/p&gt;

&lt;p&gt;因此，本文用&lt;strong&gt;不止一个Q函数&lt;/strong&gt;，而是使用&lt;strong&gt;k个&lt;/strong&gt;相关的&lt;strong&gt;Q函数&lt;/strong&gt;来进行建模。&lt;/p&gt;

&lt;p&gt;定义推荐的action为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}=\left\{a_{1: k}\right\} \subset \mathcal{I}\)&lt;/code&gt;，最优的action为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{A}^{*}=\left\{a_{1: k}^{*}\right\}=\arg \max _{\mathcal{A}} Q^{*}(s, \mathcal{A})\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;也就是说，给定一组当前推荐系统推荐的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_1,...,a_k\)&lt;/code&gt;，需要得到一组使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^*(s,a_{1:k})\)&lt;/code&gt;最大的最优的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^*_1,...,a^*_k\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;可以这么拆解，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_1\)&lt;/code&gt;，找到使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{1*}(s,a_1)\)&lt;/code&gt;最大的动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_1^*\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_1^*\)&lt;/code&gt;还有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_2\)&lt;/code&gt;，找到使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{2*}(s,a_1^*,a_2)\)&lt;/code&gt;最大的动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_2^*\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;…&lt;/li&gt;
  &lt;li&gt;输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{1:k-1}^*\)&lt;/code&gt;还有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_k\)&lt;/code&gt;，找到使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{k*}(s,a_{1:k-1}^*,a_k)\)&lt;/code&gt;最大的动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_k^*\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一步的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{1*}(s,a_1)\)&lt;/code&gt;可以看成是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^*(s,a_{1:k})\)&lt;/code&gt;当动作取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{2:k}\)&lt;/code&gt;的时候取得最大值&lt;/li&gt;
  &lt;li&gt;第二步的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{2*}(s,a_1^*,a_2)\)&lt;/code&gt;可以看成是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^*(s,a_{1:k})\)&lt;/code&gt;当动作取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{3:k}\)&lt;/code&gt;的时候取得最大值&lt;/li&gt;
  &lt;li&gt;…&lt;/li&gt;
  &lt;li&gt;第k步的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{k*}(s,a_{1:k-1}^*,a_1)\)&lt;/code&gt;可以看成是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^*(s,a_{1:k})\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;于是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{array}{l}{\text { Cascading Q-Networks: }} \\ {\qquad \begin{aligned} a_{1}^{*} &amp;amp;=\arg \max _{a_{1}}\left\{Q^{1 *}\left(s, a_{1}\right):=\max _{a_{2: k}} Q^{*}\left(s, a_{1: k}\right)\right\} \\ a_{2}^{*} &amp;amp;=\arg \max _{a_{2}}\left\{Q^{2 *}\left(s, a_{1}^{*}, a_{2}\right):=\max _{a_{3: k}} Q^{*}\left(s, a_{1: k}\right)\right\} \\ \cdots &amp;amp; \\ a_{k}^{*} &amp;amp;=\arg \max _{a_{k}}\left\{Q^{k *}\left(s, a_{1: k-1}^{*}, a_{k}\right):=Q^{*}\left(s, a_{1: k}\right)\right\} \end{aligned}}\end{array}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;看ppt。。。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/cdqn-decompose.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;画成网络图就是：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/cdqn-arch.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;parameterization-and-estimation&quot;&gt;Parameterization and Estimation&lt;/h3&gt;

&lt;p&gt;每一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{j*}\)&lt;/code&gt;通过神经网络来定义：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\boldsymbol{q}_{j}^{\top} \sigma\left(\boldsymbol{L}_{j}\left[\boldsymbol{s}^{\top}, \boldsymbol{f}_{a_{i}}^{\top}, \ldots, \boldsymbol{f}_{a_{j-1}^{\prime}}^{\top}, \boldsymbol{f}_{a_{j}}^{\top}\right]^{\top}+\boldsymbol{c}_{j}\right), \forall j
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Theta_{j}\)&lt;/code&gt;包括了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{L}_{j} \in \mathbb{R}^{\ell \times(d n+d j)}\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{c}_{j} \in \mathbb{R}^{\ell}\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{q}_{j} \in \mathbb{R}^{\ell}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;这里的state和前面用户模型的是共享的，即&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\boldsymbol{s}^{t}:=h\left(\boldsymbol{F}_{*}^{1: t-1}:=\left[\boldsymbol{f}_{*}^{1}, \cdots, \boldsymbol{f}_{*}^{t-1}\right]\right)\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;还有&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\boldsymbol{s}^{t}=h\left(\boldsymbol{F}_{*}^{t-m: t-1}\right):=\operatorname{vec}\left[\sigma\left(\boldsymbol{F}_{*}^{t-m: t-1} \boldsymbol{W}+\boldsymbol{B}\right)\right]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;理论上，结果要是最优的话，那么要求&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{j*}\)&lt;/code&gt;恰好就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^*\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q^{j *}\left(s, a_{1}^{*}, \cdots, a_{j}^{*}\right)=Q^{*}\left(s, a_{1}^{*}, \cdots, a_{k}^{*}\right), \quad \forall j
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;但严格要求相等是很难的，作者做了如下近似。&lt;/p&gt;

&lt;p&gt;定义loss为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{array}{l}{\left(y-Q^{j}\right)^{2}, \text { where }} \\ {y=r\left(s^{t}, \mathcal{A}^{t}, a^{t}\right)+\gamma Q^{k}\left(s^{t+1}, a_{1: k}^{*} ; \Theta_{k}\right), \forall j}\end{array}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;也就是说，所有的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^j\)&lt;/code&gt;个网络都拟合&lt;strong&gt;同一个目标y&lt;/strong&gt;。这样，参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Theta_{k}\)&lt;/code&gt;就可以通过对上述loss进行梯度下降来更新了。&lt;/p&gt;

&lt;p&gt;整体的训练方法：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/cdqn-train.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>VQ-VAE</title>
   <link href="http://daiwk.github.io/posts/cv-vq-vae.html"/>
   <updated>2019-06-05T00:00:00+00:00</updated>
   <id>/posts/cv-vq-vae</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650763312&amp;amp;idx=1&amp;amp;sn=55dd0a33237837e8b586bede45ace8ec&amp;amp;chksm=871ab44eb06d3d5883462170aa30212061c9b6c8fbd3a95629cb788218f12fd8241106a44575&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;超越BigGAN，DeepMind提出「史上最强非GAN生成器」VQ-VAE-2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1906.00446.pdf&quot;&gt;Generating Diverse High-Fidelity Images with VQ-VAE-2&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>EfficientNet</title>
   <link href="http://daiwk.github.io/posts/cv-efficientnet.html"/>
   <updated>2019-06-01T00:00:00+00:00</updated>
   <id>/posts/cv-efficientnet</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#efficientnet&quot;&gt;efficientnet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#改进版&quot;&gt;改进版&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;efficientnet&quot;&gt;efficientnet&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652046349&amp;amp;idx=4&amp;amp;sn=721193942579e26cbc65e026db548674&amp;amp;chksm=f12070fcc657f9eada2bf6f1ccd7ed199b36380d79ce3bb11c916a92491ee0c6cd3daf184954&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;谷歌出品EfficientNet：比现有卷积网络小84倍，比GPipe快6.1倍&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.11946&quot;&gt;EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet&quot;&gt;https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;目前提高CNN精度的方法，主要是通过任意增加CNN&lt;strong&gt;深度&lt;/strong&gt;或&lt;strong&gt;宽度&lt;/strong&gt;，或使用更大的输入图像分辨率进行训练和评估。&lt;/p&gt;

&lt;p&gt;以固定的资源成本开发，然后按比例放大，以便在获得更多资源时实现更好的准确性。例如ResNet可以通过增加层数从ResNet-18扩展到ResNet-200。&lt;/p&gt;

&lt;p&gt;劣势就是，往往需要进行繁琐的微调。一点点的摸黑去试、还经常的徒劳无功&lt;/p&gt;

&lt;p&gt;作者发现只要对网络的深度、宽度和分辨率进行合理地平衡，就能带来更好的性能。基于这一观察，科学家提出了一种新的缩放方法，使用简单但高效的复合系数均匀地缩放深度、宽度和分辨率的所有尺寸。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一步是执行网格搜索，在固定资源约束下找到基线网络的不同缩放维度之间的关系（例如，2倍FLOPS），这样做的目的是为了找出每个维度的适当缩放系数。&lt;/li&gt;
  &lt;li&gt;然后应用这些系数，将基线网络扩展到所需的目标模型大小或算力预算。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;与传统的缩放方法相比，这种复合缩放方法可以持续提高扩展模型的准确性和效率，和传统方法对比结果：MobileNet（+ 1.4％ imagenet精度），ResNet（+ 0.7％）。&lt;/p&gt;

&lt;p&gt;新模型缩放的有效性，很大程度上也依赖基线网络。&lt;/p&gt;

&lt;p&gt;为了进一步提高性能，研究团队还通过使用AutoML MNAS框架执行神经架构搜索来开发新的基线网络，该框架优化了准确性和效率（FLOPS）。&lt;/p&gt;

&lt;p&gt;由此产生的架构使用移动倒置瓶颈卷积（MBConv），类似于MobileNetV2和MnasNet，但由于FLOP预算增加而略大。然后，通过扩展基线网络以获得一系列模型，被称为EfficientNets。&lt;/p&gt;

&lt;h2 id=&quot;改进版&quot;&gt;改进版&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650767463&amp;amp;idx=3&amp;amp;sn=d37de5f41347916496881d10bf76a8be&amp;amp;chksm=871a4419b06dcd0f8c4119dfeed6a93afcc7a3a925905945ad058bbc51368e063cf01f2c05f9&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;AutoML构建加速器优化模型首尝试，谷歌发布EfficientNet-EdgeTPU&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet/edgetpu&quot;&gt;https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet/edgetpu&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://coral.withgoogle.com/docs/&quot;&gt;https://coral.withgoogle.com/docs/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原文：&lt;a href=&quot;https://ai.googleblog.com/2019/08/efficientnet-edgetpu-creating.html&quot;&gt;https://ai.googleblog.com/2019/08/efficientnet-edgetpu-creating.html&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>fastspeech</title>
   <link href="http://daiwk.github.io/posts/audio-fastspeech.html"/>
   <updated>2019-06-01T00:00:00+00:00</updated>
   <id>/posts/audio-fastspeech</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1905.09263.pdf&quot;&gt;FastSpeech: Fast, Robust and Controllable Text to Speech&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650763134&amp;amp;idx=5&amp;amp;sn=47795c4f17d677fb591689db5bef6729&amp;amp;chksm=871aab00b06d2216b21034bfbfb6c53351a479dc2af74bfd052526c50b699d24d2420ab03945&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;将文本转语音速度提高38倍，这个FastSpeech真的很fast&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;基于神经网络的端到端文本语音转换（TTS）显著改善了合成语音的质量。一些主要方法（如 Tacotron 2）通常首先从文本生成梅尔频谱（mel-spectrogram），然后使用诸如 WaveNet 的声码器从梅尔频谱合成语音。&lt;/p&gt;

&lt;p&gt;与基于连接和统计参数的传统方法相比，基于神经网络的端到端模型有一些不足之处，包括推理速度较慢，合成语音不稳健（即某些词被跳过或重复），且缺乏可控性（语音速度或韵律控制）。&lt;/p&gt;

&lt;p&gt;本文提出了一种基于 Transformer 的新型前馈网络，用于为 TTS 并行生成梅尔频谱。具体来说就是，从基于编码器-解码器的教师模型中提取注意力对齐（attention alignments），用于做音素（phoneme）持续时间预测。长度调节器利用这一预测来扩展源音素序列，以匹配目标梅尔频谱序列的长度，从而并行生成梅尔频谱。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>对抗学习进展</title>
   <link href="http://daiwk.github.io/posts/dl-adversarial-machine-learning.html"/>
   <updated>2019-05-31T00:00:00+00:00</updated>
   <id>/posts/dl-adversarial-machine-learning</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652045933&amp;amp;idx=5&amp;amp;sn=245e97bc4c7754400c0ad69c4667dc79&amp;amp;chksm=f120769cc657ff8ad6a578ae6bc04f3d2c2afd2d1cc4bf33027b035cea5d8c2c08cb45ec9fd3&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;Ian GoodFellow最新演讲：对抗机器学习的进展与挑战（附视频、PPT下载）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://pan.baidu.com/s/1S3g77xA2qtRFHZ9zFqRWsg&quot;&gt;https://pan.baidu.com/s/1S3g77xA2qtRFHZ9zFqRWsg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;提取码: 51j1&lt;/p&gt;

&lt;p&gt;另外，参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247497582&amp;amp;idx=1&amp;amp;sn=92fb9107b44fc9cc26e96d6b478021ec&amp;amp;chksm=96ea28eea19da1f85251634b680868c4c8e1278914b3a1aa9025ece7a304fe949d446971eb1e&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;一文读懂文本处理中的对抗训练&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Top-K Off-Policy Correction for a REINFORCE Recommender System</title>
   <link href="http://daiwk.github.io/posts/dl-topk-off-policy-correction.html"/>
   <updated>2019-05-28T00:00:00+00:00</updated>
   <id>/posts/dl-topk-off-policy-correction</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1812.02353.pdf&quot;&gt;Top-K Off-Policy Correction for a REINFORCE Recommender System&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;视频链接：&lt;a href=&quot;https://www.youtube.com/watch?v=HEqQ2_1XRTs&quot;&gt;Reinforcement Learning for Recommender Systems: A Case Study on Youtube&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/69559974&quot;&gt;Youtube推荐已经上线RL了，强化学习在推荐广告工业界大规模应用还远吗？&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;google的ai blog也说到了off-policy的分类方法，可以预测出哪种机器学习模型会产生最好结果。参考&lt;a href=&quot;https://ai.googleblog.com/2019/06/off-policy-classification-new.html&quot;&gt;https://ai.googleblog.com/2019/06/off-policy-classification-new.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Youtube推荐系统架构主要分为两层：召回和排序。本文中的算法应用在&lt;strong&gt;召回&lt;/strong&gt;侧。&lt;/p&gt;

&lt;p&gt;建模思路是给定用户的&lt;strong&gt;行为历史&lt;/strong&gt;，预测用户&lt;strong&gt;下一次的点击item&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;受限于On-Policy方法对系统训练架构要求复杂，所以本文采用Off-Policy的训练策略，即，并不是根据用户的交互进行实时的策略更新，而是根据&lt;strong&gt;收集到日志中用户反馈&lt;/strong&gt;进行&lt;strong&gt;模型训练&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;假设同时展示K个不重复item的reward奖励等于每个item的reward的之和，&lt;/p&gt;

&lt;p&gt;而offpolicy的训练方式，对policy gradient类的模型训练会带来如下问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;策略梯度是由不同的policy计算出来的&lt;/li&gt;
  &lt;li&gt;同一个用户的行为历史也收集了其他召回策略的数据。（没看懂。。）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;然后作者就提出了基于importance weighting的Off-Policy修正方案，对pg的计算进行一阶逼近：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\prod_{t^{\prime}=0}^{|\tau|} \frac{\pi\left(a_{t^{\prime}} | s_{t^{\prime}}\right)}{\beta\left(a_{t^{\prime}} | s_{t^{\prime}}\right)} \approx \prod_{t^{\prime}=0}^{t} \frac{\pi\left(a_{t^{\prime}} | s_{t^{\prime}}\right)}{\beta\left(a_{t^{\prime}} | s_{t^{\prime}}\right)}=\frac{P_{\pi_{\theta}}\left(s_{t}\right)}{P_{\beta}\left(s_{t}\right)} \frac{\pi\left(a_{t} | s_{t}\right)}{\beta\left(a_{t} | s_{t}\right)} \approx \frac{\pi\left(a_{t} | s_{t}\right)}{\beta\left(a_{t} | s_{t}\right)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;最终可以得到一个低variance的策略梯度的biased estimator：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\sum_{\tau \sim \beta}\left[\sum_{t=0}^{|\tau|} \frac{\pi_{\theta}\left(a_{t} | s_{t}\right)}{\beta\left(a_{t} | s_{t}\right)} R_{t} \nabla_{\theta} \log \pi_{\theta}\left(a_{t} | s_{t}\right)\right]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;注：随机策略梯度就有这么个重要性权重，应该是类似的思路吧。。&lt;a href=&quot;https://daiwk.github.io/posts/rl-stepbystep-chap9.html#31-%E9%9A%8F%E6%9C%BA%E7%AD%96%E7%95%A5ac%E6%96%B9%E6%B3%95&quot;&gt;https://daiwk.github.io/posts/rl-stepbystep-chap9.html#31-%E9%9A%8F%E6%9C%BA%E7%AD%96%E7%95%A5ac%E6%96%B9%E6%B3%95&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;因为是基于用户的交互历史预测下一个用户点击的item，所以文中也采用RNN针对用户State的转换进行建模。文中提到实验了包括LSTM、GRU等RNN单元，发现Chaos Free的RNN单元(&lt;a href=&quot;https://arxiv.org/abs/1612.06212&quot;&gt;A recurrent neural network without chaos&lt;/a&gt;)因为稳定高效而使用起来效果最好。&lt;/p&gt;

&lt;p&gt;上面那个公式里，最难获取到的是&lt;strong&gt;用户的行为策略&lt;/strong&gt;，理想情况下是收集日志的时候同时把用户相应的用户策略也就是点击概率给收集下来，但由于策略不同等客观原因文中针对用户的行为策略使用另外一组&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta '\)&lt;/code&gt;参数进行预估，而且&lt;strong&gt;防止它的梯度回传&lt;/strong&gt;影响主RNN网络的训练。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/topk-off-policy-another-theta.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在推荐系统中，用户可以同时看到&lt;strong&gt;k个&lt;/strong&gt;展示给用户的&lt;strong&gt;候选item&lt;/strong&gt;，用户可能同时与一次展示出来的多个item进行交互。因此需要扩展策略根据用户的行为历史&lt;strong&gt;预测下一次&lt;/strong&gt;用户&lt;strong&gt;可能点击的top-K个item&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;假设&lt;strong&gt;同时展示K个不重复&lt;/strong&gt;item的reward奖励等于&lt;strong&gt;每个item的reward的之和&lt;/strong&gt;，这样，Top-K的Off-Policy修正的策略梯度如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} &amp;amp; \sum_{\tau \sim \beta}\left[\sum_{t=0}^{|\tau|} \frac{\alpha_{\theta}\left(a_{t} | s_{t}\right)}{\beta\left(a_{t} | s_{t}\right)} R_{t} \nabla_{\theta} \log \alpha_{\theta}\left(a_{t} | s_{t}\right)\right] \\=&amp;amp; \sum_{\tau \sim \beta}\left[\sum_{t=0}^{|\tau|} \frac{\pi_{\theta}\left(a_{t} | s_{t}\right)}{\beta\left(a_{t} | s_{t}\right)} \frac{\partial \alpha\left(a_{t} | s_{t}\right)}{\partial \pi\left(a_{t} | s_{t}\right)} R_{t} \nabla_{\theta} \log \pi_{\theta}\left(a_{t} | s_{t}\right)\right] \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;与上面Top 1的修正公式相比主要是多了一个包含K的系数。也就是说，&lt;strong&gt;随着K的增长&lt;/strong&gt;，策略梯度会比原来的公式&lt;strong&gt;更快地降到0&lt;/strong&gt;。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>mixmatch</title>
   <link href="http://daiwk.github.io/posts/dl-mixmatch.html"/>
   <updated>2019-05-21T00:00:00+00:00</updated>
   <id>/posts/dl-mixmatch</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.02249&quot;&gt;MixMatch: A Holistic Approach to Semi-Supervised Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文来自 Google Research，这一方法综合了&lt;strong&gt;自洽正则化（Consistency Regularization）&lt;/strong&gt;，&lt;strong&gt;最小化熵（Entropy Minimization）&lt;/strong&gt;以及&lt;strong&gt;传统正则化（Traditional Regularization）&lt;/strong&gt;，取三者之长，并补三者之短，提出了 MixMatch 这一方法。在 CIFAR10 上，仅仅使用 250 个标签数据就达到 11% 的错误率，远超其他主流方法。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/66281890&quot;&gt;https://zhuanlan.zhihu.com/p/66281890&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652045372&amp;amp;idx=4&amp;amp;sn=780cb3cb33f7e30ef9de59d7ca1b6177&amp;amp;chksm=f12074cdc657fddb7fff685107b70883d40c42d43e2665a938599d524ebec1f11da7122b90a8&amp;amp;scene=4&quot;&gt;谷歌首席科学家：半监督学习的悄然革命&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原始blog：&lt;a href=&quot;https://towardsdatascience.com/the-quiet-semi-supervised-revolution-edec1e9ad8c&quot;&gt;https://towardsdatascience.com/the-quiet-semi-supervised-revolution-edec1e9ad8c&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/google-research/mixmatch&quot;&gt;https://github.com/google-research/mixmatch&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>强化学习进展（持续更新）</title>
   <link href="http://daiwk.github.io/posts/rl-rl-progresses.html"/>
   <updated>2019-05-11T00:00:00+00:00</updated>
   <id>/posts/rl-rl-progresses</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#beyond-dqna3c-a-survey-in-advanced-reinforcement-learning2018&quot;&gt;Beyond DQN/A3C: A Survey in Advanced Reinforcement Learning(2018)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#modern-deep-reinforcement-learning-algorithms2019&quot;&gt;Modern Deep Reinforcement Learning Algorithms(2019)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#evolution-strategies-as-a-scalable-alternative-to-reinforcement-learning&quot;&gt;Evolution Strategies as a Scalable Alternative to Reinforcement Learning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%a8%a1%e6%8b%9f%e5%99%a8%e7%9b%b8%e5%85%b3&quot;&gt;模拟器相关&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e4%bb%a3%e7%a0%81%e5%ba%93&quot;&gt;代码库&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%bf%81%e7%a7%bb%e5%ad%a6%e4%b9%a0%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0&quot;&gt;迁移学习+强化学习&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;梳理rl的一些新进展&lt;/p&gt;

&lt;h2 id=&quot;beyond-dqna3c-a-survey-in-advanced-reinforcement-learning2018&quot;&gt;Beyond DQN/A3C: A Survey in Advanced Reinforcement Learning(2018)&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650751210&amp;amp;idx=4&amp;amp;sn=a1d4c4c2b27a7f62a4edbd74f626f23c&amp;amp;chksm=871a8494b06d0d82e3dc22b8a591cde7449d845576bbbdecf98ad513a4326cfe9b34eb6272b4&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=050208JkhftrjXIfdxVcWRZi&amp;amp;pass_ticket=csFmp%2BqPqpbOEtBCr9byDm0vHyp83ccxf21EyZaHyV%2BoFQOLINXIlgzuTkVvCg24#rd&quot;&gt;深度 | 超越DQN和A3C：深度强化学习领域近期新进展概览&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原blog：&lt;a href=&quot;https://towardsdatascience.com/advanced-reinforcement-learning-6d769f529eb3&quot;&gt;https://towardsdatascience.com/advanced-reinforcement-learning-6d769f529eb3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DQN&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q\left(s_{t}, a_{t} ; \theta\right) \leftarrow Q\left(s_{t}, a_{t} ; \theta\right)+\alpha[\underbrace{\underbrace{(r_{t}+\max _{a} \hat{Q}\left(s_{t+1}, a ; \theta^{\prime}\right))}_{\text { target }}-Q\left(s_{t}, a_{t} ; \theta\right) )}_{\text {TD-error}}]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;ac&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
d \theta_{v} \leftarrow d \theta_{v}+\partial{\underbrace{\left(R-V\left(s_{i} ; \theta_{v}\right)\right)}_{\text{advantage}}}^{2} / \partial \theta_{v}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;modern-deep-reinforcement-learning-algorithms2019&quot;&gt;Modern Deep Reinforcement Learning Algorithms(2019)&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1906.10025v1.pdf&quot;&gt;Modern Deep Reinforcement Learning Algorithms&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原论文有点大。。打开太慢。。转存一份：&lt;a href=&quot;https://daiwk.github.io/assets/Modern%20Deep%20Reinforcement%20Learning%20Algorithms.pdf&quot;&gt;https://daiwk.github.io/assets/Modern%20Deep%20Reinforcement%20Learning%20Algorithms.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;evolution-strategies-as-a-scalable-alternative-to-reinforcement-learning&quot;&gt;Evolution Strategies as a Scalable Alternative to Reinforcement Learning&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.03864&quot;&gt;Evolution Strategies as a Scalable Alternative to Reinforcement Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;blog：&lt;a href=&quot;https://openai.com/blog/evolution-strategies/&quot;&gt;https://openai.com/blog/evolution-strategies/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/openai/evolution-strategies-starter&quot;&gt;https://github.com/openai/evolution-strategies-starter&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;模拟器相关&quot;&gt;模拟器相关&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1805.10000.pdf&quot;&gt;Virtual-Taobao: Virtualizing Real-world Online Retail Environment for Reinforcement Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1906.11462.pdf&quot;&gt;Simulating User Feedback for Reinforcement Learning Based Recommendations&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;代码库&quot;&gt;代码库&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/p-christ/Deep-Reinforcement-Learning-Algorithms-with-PyTorch&quot;&gt;https://github.com/p-christ/Deep-Reinforcement-Learning-Algorithms-with-PyTorch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/BtNH2_s4l1Kcwe82y-oEkA&quot;&gt;17种深度强化学习算法用Pytorch实现&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;迁移学习强化学习&quot;&gt;迁移学习+强化学习&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/Rj55EoopzlR71DZ5XrvH_w&quot;&gt;八千字长文深度解读，迁移学习在强化学习中的应用及最新进展&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorlayer</title>
   <link href="http://daiwk.github.io/posts/platform-tensorlayer.html"/>
   <updated>2019-05-11T00:00:00+00:00</updated>
   <id>/posts/platform-tensorlayer</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650761885&amp;amp;idx=5&amp;amp;sn=d01731043d03423d1b1afe5522dec47f&amp;amp;chksm=871aaee3b06d27f5e64a72ee9d2944be2efcb5d6c976911a8059872beae549e418cb862a346d&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=csFmp%2BqPqpbOEtBCr9byDm0vHyp83ccxf21EyZaHyV%2BoFQOLINXIlgzuTkVvCg24#rd&quot;&gt;TensorLayer 2.0：保有最多官方神经网络层的通用Python库&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>图神经网络+推荐</title>
   <link href="http://daiwk.github.io/posts/dl-graph-recommendations.html"/>
   <updated>2019-05-07T00:00:00+00:00</updated>
   <id>/posts/dl-graph-recommendations</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#diffnet&quot;&gt;DiffNet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#dgrec&quot;&gt;DGRec&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#igpl&quot;&gt;IGPL&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#graphrec&quot;&gt;GraphRec&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#danser&quot;&gt;DANSER&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ngcf&quot;&gt;NGCF&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#meirec&quot;&gt;MEIrec&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU2OTA0NzE2NA==&amp;amp;mid=2247510850&amp;amp;idx=1&amp;amp;sn=222f99d740acd50bcbcf5a5745c5d938&amp;amp;chksm=fc864a51cbf1c34739a348177e109d4ff4323f313208c410a44ac6af4cc4e32ad66f6275b235&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=yi5ku1%2Fs0oomHKKecAzMcpWLxtfI6PDYKYJn%2BWzsyCs3SOPlL0ZxjXuFZUZ2FS5h#rd&quot;&gt;近期必读的6篇【图神经网络的推荐（GNN+R）】相关论文和代码（WWW、SIGIR、WSDM）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://tianchi.aliyun.com/course/video?spm=5176.12586971.1001.62.55e2194dAHpQl4&amp;amp;liveId=41071&quot;&gt;CIKM 2019 EComm AI：用户行为预测 赛题解读与阿里GNN推荐结合实践分享&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;diffnet&quot;&gt;DiffNet&lt;/h2&gt;

&lt;p&gt;SIGIR ’19，&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1904.10322.pdf&quot;&gt;A Neural Influence Diffusion Model for Social Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;社交推荐系统利用每个用户的局部邻居偏好(local neighbors’ preferences)来缓解数据稀疏性，从而更好地进行用户emb建模。对于每一个社交平台的用户，其潜在的嵌入是受他信任的用户影响的，而这些他信任的用户也被他们自己的社交联系所影响。随着社交影响在社交网络中递归传播和扩散（diffuse），每个用户的兴趣在递归过程中发生变化。然而，目前的社交推荐模型只是利用每个用户的&lt;strong&gt;局部邻居&lt;/strong&gt;来构建&lt;strong&gt;静态模型&lt;/strong&gt;，没有模拟全局社交网络中的&lt;strong&gt;递归扩散&lt;/strong&gt;，导致推荐性能不理想。&lt;/p&gt;

&lt;p&gt;本文提出了一个deep influence propagation model。对于每个用户，扩散过程（diffusion）用融合了相关特征和一个caputure了latent behavior preference的free的用户隐向量。本文的key idea是，设计了一个layer-wise的influence propagation结构，可以随着social diffusion process的进行，对用户emb进行演化。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/diffnet.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;dgrec&quot;&gt;DGRec&lt;/h2&gt;

&lt;p&gt;WSDM ’19，&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1902.09362v2&quot;&gt;Session-based Social Recommendation via Dynamic Graph Attention Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/DeepGraphLearning/RecommenderSystems/tree/master/socialRec&quot;&gt;https://github.com/DeepGraphLearning/RecommenderSystems/tree/master/socialRec&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;igpl&quot;&gt;IGPL&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1904.12058v1&quot;&gt;Inductive Graph Pattern Learning for Recommender Systems Based on a Graph Neural Network&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;graphrec&quot;&gt;GraphRec&lt;/h2&gt;

&lt;p&gt;WWW’19，&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1902.07243.pdf&quot;&gt;Graph Neural Networks for Social Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;danser&quot;&gt;DANSER&lt;/h2&gt;

&lt;p&gt;WWW’19 Oral&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1903.10433&quot;&gt;Dual Graph Attention Networks for Deep Latent Representation of Multifaceted Social Effects in Recommender Systems&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;ngcf&quot;&gt;NGCF&lt;/h2&gt;

&lt;p&gt;SIGIR’19&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.08108&quot;&gt;Neural Graph Collaborative Filtering&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jianshu.com/p/16c8973ef8ff&quot;&gt;https://www.jianshu.com/p/16c8973ef8ff&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;meirec&quot;&gt;MEIrec&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://shichuan.org/doc/67.pdf&quot;&gt;Metapath-guided Heterogeneous Graph Neural Network for Intent Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是北京邮电大学和阿里巴巴发表于 KDD 2019 的工作。针对手机淘宝的用户意图推荐，本文设计了基于异质图神经网络的意图推荐模型 MEIRec。&lt;/p&gt;

&lt;p&gt;传统商品推荐为用户推荐商品，而意图推荐则关注于预测用户的意图。本文将意图推荐的业务场景建模为异质图（包含多种类型节点和关系的图），然后设计了 metapath-guided heterogeneous Graph Neural Network 来学习该业务场景下多种不同目标的表示。同时，本文也提出一种 term embedding mechanism 来降低大规模异质图场景下的参数量。最后，在淘宝真实场景下的 AB test 证明了 MEIRec 算法的优越性。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/googlebaba/KDD2019-MEIRec&quot;&gt;https://github.com/googlebaba/KDD2019-MEIRec&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>chatbot</title>
   <link href="http://daiwk.github.io/posts/nlp-chatbot.html"/>
   <updated>2019-04-29T00:00:00+00:00</updated>
   <id>/posts/nlp-chatbot</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%b0%8f%e5%86%b0&quot;&gt;小冰&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#space-fusion&quot;&gt;space fusion&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#style-fusion&quot;&gt;style fusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650761302&amp;amp;idx=3&amp;amp;sn=879c2289b422313908ca31214a5f96d1&amp;amp;chksm=871aac28b06d253ebc991f2f17ff2d508e213e09b97be0d0039ccd08b42db060278f751c24b4&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0806FV1ysWjPk9GNKVFxYbGM&amp;amp;sharer_sharetime=1565082872880&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;专栏 | 聊天机器人：困境和破局&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247498768&amp;amp;idx=1&amp;amp;sn=fcfe600c5424d0574bb2388bb84f6201&amp;amp;chksm=96ea2390a19daa86945c6770eae0d901461012a2d3f0db0e9d588b2890a4dc56cf0f44484065&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1565003633118&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;最新综述：对话系统之用户模拟器&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/uI1yCNPN5eb1_cdB2RvT6g&quot;&gt;AI更懂人话：谷歌发布全新对话数据集，模仿智能助理&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;谷歌发布了Coached Conversational Preference Elicitation（CCPE）和Taskmaster-1 对话数据集，在设计中独特地模仿当今基于语音的数字助理，在自动化系统的环境中保留口语对话的特征。&lt;/p&gt;

&lt;h2 id=&quot;小冰&quot;&gt;小冰&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/h8kHxiJVb9TdVirPUiJmvg&quot;&gt;第七代微软小冰现身：史上最大升级，订制私人AI不是梦&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/8l-MkmoeEqtK75P6CrpKBA&quot;&gt;DIY自己的AI助理，萝莉御姐暖男霸道总裁全凭你定义，微软小冰团队发布新框架&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/L_8Uj-LeJdGjc7Qsi1_PUA&quot;&gt;开放框架，进驻OV手机，五岁的微软小冰已经学会了养活自己&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;space-fusion&quot;&gt;space fusion&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1902.11205&quot;&gt;Jointly optimizing diversity and relevance in neural response generation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NACCL 2019&lt;/p&gt;

&lt;h2 id=&quot;style-fusion&quot;&gt;style fusion&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.05361&quot;&gt;Structuring Latent Spaces for Stylized Response Generation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/golsun/StyleFusion&quot;&gt;https://github.com/golsun/StyleFusion&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是微软发表于 EMNLP 2019 的工作，这是 SpaceFusion  的后续之作。SpaceFusion 尝试将对话系统中的 source 和 target 映射到同一隐空间上，从而转换生成对话的问题成为生成隐空间向量的问题。本文（StyleFusion）进一步将额外的风格化文本的特征迁移到生成回复中，从而达到对话个性化和风格化的效果。在若干标准数据集上取得了很好的效果。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>bert加速</title>
   <link href="http://daiwk.github.io/posts/platform-bert-speedup.html"/>
   <updated>2019-04-03T00:00:00+00:00</updated>
   <id>/posts/platform-bert-speedup</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#lamb&quot;&gt;LAMB&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cubert&quot;&gt;cuBERT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#distill&quot;&gt;distill&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%83%8c%e6%99%af%e4%b8%8e%e7%9b%b8%e5%85%b3%e5%b7%a5%e4%bd%9c&quot;&gt;背景与相关工作&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%a8%a1%e5%9e%8b%e5%8e%8b%e7%bc%a9&quot;&gt;模型压缩&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e7%bd%91%e7%bb%9c%e7%bb%93%e6%9e%84%e4%b8%8e%e6%96%b9%e6%b3%95&quot;&gt;网络结构与方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#distill%e7%9b%ae%e6%a0%87&quot;&gt;distill目标&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e7%94%a8%e4%ba%8edistill%e7%9a%84augmentation&quot;&gt;用于distill的augmentation&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#masking&quot;&gt;Masking&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#pos-guided-word-replacement&quot;&gt;POS-guided word replacement&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#n-gram-sampling&quot;&gt;n-gram sampling&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%95%b4%e4%b8%aaaugmentation%e7%9a%84%e6%b5%81%e7%a8%8b&quot;&gt;整个augmentation的流程&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%9c%9f%e8%b1%aa%e7%89%88megatron&quot;&gt;土豪版：Megatron&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;更多参考：&lt;a href=&quot;https://daiwk.github.io/posts/nlp-paddle-lark.html&quot;&gt;https://daiwk.github.io/posts/nlp-paddle-lark.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;lamb&quot;&gt;LAMB&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650759697&amp;amp;idx=1&amp;amp;sn=fe72aacd339307eca4ca79da19375526&amp;amp;chksm=871aa66fb06d2f796cfda4649fc084f31ec74281a0862af59a42b8ccacb57f4bbdce257de29e&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=7xdQ1FGMEYoqdGrXJFR19sivsW6Crb87wS1sHZF8wBLs7ph%2Fdoe7AQvztk%2FA3Q8h#rd&quot;&gt;76分钟训练BERT！谷歌大脑新型优化器LAMB加速大批量训练&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/abs/1904.00962&quot;&gt;Reducing BERT Pre-Training Time from 3 Days to 76 Minutes&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;cubert&quot;&gt;cuBERT&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/zhihu/cuBERT&quot;&gt;https://github.com/zhihu/cuBERT&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;distill&quot;&gt;distill&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1903.12136.pdf&quot;&gt;Distilling Task-Specific Knowledge from BERT into Simple Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;首先，distill相关的可以参考&lt;a href=&quot;https://daiwk.github.io/posts/dl-knowledge-distill.html&quot;&gt;https://daiwk.github.io/posts/dl-knowledge-distill.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;背景与相关工作&quot;&gt;背景与相关工作&lt;/h3&gt;

&lt;p&gt;要进行distill就需要大量的无标签数据，然后让大模型给出logits，但nlp领域不像图像，可以简单地data augmentation。传统的nlp的data augmentation通常是task-specific的（例如&lt;a href=&quot;https://arxiv.org/abs/1804.07461&quot;&gt;GLUE: A multi-task benchmark and analysis platform for natural language understanding&lt;/a&gt;，或者&lt;a href=&quot;https://www.aclweb.org/anthology/P16-1056&quot;&gt;Generating factoid questions with recurrent neural networks: The 30m factoid question-answer corpus&lt;/a&gt;），本文提出了一个rule-based的用来构建transfer set的data augmentation方法。&lt;/p&gt;

&lt;h4 id=&quot;模型压缩&quot;&gt;模型压缩&lt;/h4&gt;

&lt;p&gt;开山之作是lecun在1990年的&lt;a href=&quot;http://yann.lecun.com/exdb/publis/pdf/lecun-90b.pdf&quot;&gt;Optimal brain damage&lt;/a&gt;，提出了一个local error-based的方法来剪枝不重要的weight。&lt;/p&gt;

&lt;p&gt;2015年的&lt;a href=&quot;https://arxiv.org/abs/1510.00149&quot;&gt;Compressing deep neural networks with pruning, trained quantization and Huffman coding&lt;/a&gt;提出了一个简单的压缩pipeline，可以把模型大小缩小40倍，且不对准确率造成损伤。&lt;/p&gt;

&lt;p&gt;但上面的这些方法会引入高度不规则的稀疏性（highly irregular sparsity），阻止了高度优化的计算routines。&lt;/p&gt;

&lt;p&gt;所以其他人会使用一些以设备为中心的指标（ targeting device-centric metrics），例如浮点数运算（&lt;a href=&quot;https://arxiv.org/pdf/1811.03060.pdf&quot;&gt;FLOPs as a direct optimization objective for learning sparse neural networks&lt;/a&gt;），或者响应时间（&lt;a href=&quot;http://openaccess.thecvf.com/content_ECCV_2018/papers/Changan_Chen_Constraints_Matter_in_ECCV_2018_paper.pdf&quot;&gt;Constraint-aware deep neural network compression&lt;/a&gt;），来直接把整个filters剪枝掉（如&lt;a href=&quot;https://arxiv.org/abs/1608.08710&quot;&gt;Pruning filters for efficient convnets&lt;/a&gt;，或者&lt;a href=&quot;https://arxiv.org/abs/1708.06519&quot;&gt;Learning efficient convolutional networks through network slimming&lt;/a&gt;）。&lt;/p&gt;

&lt;p&gt;还有些人直接研究量子神经网络（如&lt;a href=&quot;https://arxiv.org/abs/1802.04680&quot;&gt;Training and inference with integers in deep neural networks&lt;/a&gt;）。甚至还有提出用二进制权重和激活的二进制网络来搞，&lt;a href=&quot;https://arxiv.org/abs/1602.02830&quot;&gt;Binarized neural networks: Training deep neural networks with weights and activations constrained to +1 or -1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;而distill的方法能将大模型(teacher)的知识transfer到一个完全不同结构的小模型(student)中，在nlp领域已经有两个例子了：机器翻译的&lt;a href=&quot;https://arxiv.org/abs/1606.07947&quot;&gt;Sequence level knowledge distillation&lt;/a&gt;，还有语言模型的&lt;a href=&quot;https://aclweb.org/anthology/C18-2028&quot;&gt;On-device neural language model based word prediction&lt;/a&gt;。&lt;/p&gt;

&lt;h3 id=&quot;网络结构与方法&quot;&gt;网络结构与方法&lt;/h3&gt;

&lt;p&gt;拆成两大步：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;加上logits-regression objective&lt;/li&gt;
  &lt;li&gt;建立transfer set，对训练集做augmentation从而可以让knowledge transfer更有效&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;bert模型对一个句子或者pair，会产出一个特征向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{h} \in \mathbb{R}^{d}\)&lt;/code&gt;，我们基于这个向量来构建分类器：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;单个句子的分类&lt;/strong&gt;：直接加一个softmax层，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{y}^{(B)}=\operatorname{softmax}(W \boldsymbol{h})\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W \in \mathbb{R}^{k \times d}\)&lt;/code&gt;是softmax层的权重矩阵，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;是label数&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;句子pair的任务&lt;/strong&gt;：将两句话的feature进行concat，然后扔给一个softmax层&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;训练阶段，同时finetune bert的参数和softmax层的参数，目标是label的cross-entropy loss。&lt;/p&gt;

&lt;p&gt;student模型&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;单个句子的分类&lt;/strong&gt;：如下图，word emb之后接一个双向lstm，两个方向的输出concat到一起，然后接全连接(用的Relu)，然后再接softmax。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/distill-bert-1sentence.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;句子pair的任务&lt;/strong&gt;：如下图，对两个sentence encoder使用siamese结构我双向lstm，分别产生&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{h}_{s_1}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{h}_{s_2}\)&lt;/code&gt;，然后做一个标准的concatenate–compare操作（参考&lt;a href=&quot;https://arxiv.org/abs/1804.07461&quot;&gt;GLUE: A multi-task benchmark and analysis platform for natural language understanding&lt;/a&gt;），即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f\left(\boldsymbol{h}_{s 1}, \boldsymbol{h}_{s 2}\right)=\left[\boldsymbol{h}_{s 1}, \boldsymbol{h}_{s 2}, \boldsymbol{h}_{s 1} \odot\right. \boldsymbol{h}_{s 2},\left|\boldsymbol{h}_{s 1}-\boldsymbol{h}_{s 2}\right| ]\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\odot\)&lt;/code&gt;是element wise乘积，然后把这个结果给一个relu的分类器。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/distill-bert-sentence-pair.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;distill目标&quot;&gt;distill目标&lt;/h3&gt;

&lt;p&gt;softmax的输入也就是logits是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z=w^Th\)&lt;/code&gt;，输出是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\widetilde{y}_{i}=\operatorname{softmax}(\boldsymbol{z})=\frac{\exp \left\{\boldsymbol{w}_{i}^{\top} \boldsymbol{h}\right\}}{\sum_{j} \exp \left\{\boldsymbol{w}_{j}^{\top} \boldsymbol{h}\right\}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;本文使用Caruana等人的&lt;a href=&quot;https://arxiv.org/abs/1312.6184&quot;&gt;Do deep nets really need to be deep?&lt;/a&gt;方法，distillation objective是student和teacher的logits的mse：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\mathcal{L}_{\text { distill }}=\left\|\boldsymbol{z}^{(B)}-\boldsymbol{z}^{(S)}\right\|_{2}^{2}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{z}^{(B)}\)&lt;/code&gt;是teacher的logit，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{z}^{(S)}\)&lt;/code&gt;是student的logit。其他方法比如hinton在2015年的&lt;a href=&quot;https://arxiv.org/abs/1503.02531&quot;&gt;Distilling the knowledge in a neural network&lt;/a&gt;使用soft target的cross entropy也是可行的，但在这个任务上效果还是mse稍好一点。&lt;/p&gt;

&lt;p&gt;训练的时候，这个distillation objective可以和传统的针对one-hot的label的cross-entropy一起用：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} \mathcal{L} &amp;amp;=\alpha \cdot \mathcal{L}_{\mathrm{CE}}+(1-\alpha) \cdot \mathcal{L}_{\mathrm{distill}} \\=&amp;amp;-\alpha \sum_{i} \log y_{i}^{(S)}-(1-\alpha)\left\|z^{(B)}-z^{(S)}\right\|_{2}^{2} \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;实验结果显示，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha\)&lt;/code&gt;设成0的时候，也就是&lt;strong&gt;只使用distill的loss，效果最好。&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对于有label的数据集，one-hot target &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;直接用ground-truth的label&lt;/li&gt;
  &lt;li&gt;对于无label的数据集，可以直接用teacher的预估label：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_{i}=1\ if\ i=\operatorname{argmax} y^{(B)}\)&lt;/code&gt;，否则就是0&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;用于distill的augmentation&quot;&gt;用于distill的augmentation&lt;/h3&gt;

&lt;p&gt;随机使用以下几个操作之一：&lt;/p&gt;

&lt;h4 id=&quot;masking&quot;&gt;Masking&lt;/h4&gt;

&lt;p&gt;以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\text { mask }}\)&lt;/code&gt;的概率，随机地将一个词替换为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[MASK]&lt;/code&gt;，在student模型里就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[UNK]&lt;/code&gt;，而在bert中就是mask。这个规则能够clarify每个词对label的贡献，例如，teacher网络对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I [MASK] the comedy&lt;/code&gt;产生的logits比&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;“I loved the comedy&lt;/code&gt;产出的logits要低。&lt;/p&gt;

&lt;h4 id=&quot;pos-guided-word-replacement&quot;&gt;POS-guided word replacement&lt;/h4&gt;

&lt;p&gt;以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\text { pos }}\)&lt;/code&gt;的概率，随机地把一个词替换成相同POS(part-of-speech) tag的另一个词（如，把how替换成what）。为了保持原始的训练集的分布，新词从使用POS tag进行re-normalize的unigram的分布中采样出来。&lt;/p&gt;

&lt;h4 id=&quot;n-gram-sampling&quot;&gt;n-gram sampling&lt;/h4&gt;

&lt;p&gt;以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\text { ng }}\)&lt;/code&gt;的概率，从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\{1,2, \dots, 5\}\)&lt;/code&gt;中随机选一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;，然后随机采样出一个ngram。这种方法相当于随机扔掉句子的其他部分，是一种更aggressive的masking。&lt;/p&gt;

&lt;h4 id=&quot;整个augmentation的流程&quot;&gt;整个augmentation的流程&lt;/h4&gt;

&lt;p&gt;给定一个训练样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left\{w_{1}, \dots w_{n}\right\}\)&lt;/code&gt;，整个augmentation的过程如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对每个词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i\)&lt;/code&gt;从uniform distribution &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X_{i} \sim \mathrm{UNIFORM}[0,1]\)&lt;/code&gt;中采样出一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X_i\)&lt;/code&gt;如下操作
    &lt;ul&gt;
      &lt;li&gt;如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X_{i}&amp;lt;p_{\mathrm{mask}}\)&lt;/code&gt;，对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i\)&lt;/code&gt;进行masking&lt;/li&gt;
      &lt;li&gt;如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\text { mask }} \leq X_{i}&amp;lt;p_{\mathrm{mask}}+p_{\mathrm{pos}}\)&lt;/code&gt;，那么对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i\)&lt;/code&gt;使用POS-guided word replacement&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;对这个样本的每个词处理完之后，以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\text { ng }}\)&lt;/code&gt;的概率，使用n-gram sampling，然后把结果加到augmented数据集中（无标签）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;然后使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n_{\text { iter }}\)&lt;/code&gt;次如上操作，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n_{\text { iter }}\)&lt;/code&gt;个augmented的sample。&lt;/p&gt;

&lt;h2 id=&quot;土豪版megatron&quot;&gt;土豪版：Megatron&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/NVIDIA/Megatron-LM&quot;&gt;https://github.com/NVIDIA/Megatron-LM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/8qJqP9gWook8VhtQQnWDxw&quot;&gt;有钱任性：英伟达训练80亿参数量GPT-2，1475块V100 53分钟训练BERT&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;将BERT的训练时间缩短到了53分钟；&lt;/li&gt;
  &lt;li&gt;将BERT的推理时间缩短到了2.2毫秒（10 毫秒已经是业界公认的高水平）；&lt;/li&gt;
  &lt;li&gt;将GPT-2的参数量推向80亿（以前OpenAI GPT-2最大为15亿参数量）。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;最大配置：72层、每层隐藏单元都是3072&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/MWaJP283qQrhs1VvyF0SrQ&quot;&gt;想效仿英伟达50分钟训练 BERT？只有GPU还不够……&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/abs/1909.08053v1&quot;&gt;Megatron-LM: Training Multi-Billion Parameter Language Models Using GPU Model Parallelism&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;该方法无需新的编译器或库更改，它与 pipeline 模型并行正交且互补，只需在 PyTorch 中嵌入几个通信操作即可完整实现。利用该方法，研究者使用 512 个 GPU 收敛了一个具备 83 亿参数的 transformer 语言模型，该模型是目前最大的 transformer 模型，其规模是 BERT 的 24 倍，GPT-2 的 5.6 倍。&lt;/p&gt;

&lt;p&gt;为了展示该方法的可扩展性，研究者建立了一个基线：他们在单个 NVIDIA V100 32GB GPU 上训练了一个具备 12 亿参数的模型，整个训练应用维持 39 TeraFLOPs/秒的性能，是单个 GPU 在 DGX-2H 服务器上运行的理论峰值 FLOPS 的 30%，因此这是一个非常强大的基线模型。将该模型扩展至 83 亿参数，并使用 8-way 模型并行化在 512 个 GPU 上进行训练，达到了 15.1 PetaFLOPs/秒的性能。与单个 GPU 的情况相比，它实现了 76% 的扩展效率。在 174 GB 文本数据上收敛该模型需要以 12 ZettaFLOPs 训练 9.2 天。&lt;/p&gt;

&lt;p&gt;研究者利用 transformer 网络的结构，仅添加几个同步基元（synchronization primitives）即创建出一个简单的模型并行化实现。他们对 transformer 中的自注意力模块和多层感知机（MLP）模块均采用了模型并行化。&lt;/p&gt;

&lt;p&gt;模型并行与数据并行是正交的，因此我们可以同时使用二者在合理时间内训练大型模型。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>paddle的LARK(ERNIE/BERT等)+bert的各种变种</title>
   <link href="http://daiwk.github.io/posts/nlp-paddle-lark.html"/>
   <updated>2019-03-20T00:00:00+00:00</updated>
   <id>/posts/nlp-paddle-lark</id>
   <content type="html">&lt;h2 id=&quot;小结&quot;&gt;小结&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/-U_Lu2MMr5QRNe1xpv-_Xg&quot;&gt;预训练语言模型关系图+必读论文列表，清华荣誉出品&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/thunlp/PLMpapers&quot;&gt;https://github.com/thunlp/PLMpapers&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/PLMfamily.jpg&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;paddle的LARK里包含了一些nlp模型&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/xtv8T-X8OddhF5wDasR3NA&quot;&gt;强力推荐！飞桨产业级PaddleNLP最新全景图发布&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;bert&quot;&gt;bert&lt;/h2&gt;

&lt;h3 id=&quot;finetune和跑预测并save模型&quot;&gt;finetune和跑预测并save模型&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;BERT_BASE_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./pretrained_model/chinese_L-12_H-768_A-12/
&lt;span class=&quot;nv&quot;&gt;TASK_NAME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;XNLI&quot;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#DATA_PATH=./data/XNLI-1.0-demo/&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;DATA_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./data/XNLI-MT-1.0-dwk/
&lt;span class=&quot;nv&quot;&gt;INIT_CKPT_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./output/step_50
&lt;span class=&quot;nv&quot;&gt;SAVE_INFERENCE_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./output/infer_step_50 &lt;span class=&quot;c&quot;&gt;## 这个目录下会有个__model__文件，给在线infer用的，注意paddle的版本要用1.3.1以上的，1.3.0生成的这个目录有bug&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;python&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;../../python-2.7.14-paddle-1.3.1/bin/python


&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FLAGS_enable_parallel_graph&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;FLAGS_sync_nccl_allreduce&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;CUDA_VISIBLE_DEVICES&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0,1,2,3,4,5,6,7
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;CPU_NUM&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3 &lt;span class=&quot;c&quot;&gt;## 设置跑的cpu核数&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;TASK_NAME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'XNLI'&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;CKPT_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./output/

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;finetune_xnli&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;### 如果有56核的cpu，会占114g。。；而如果是12核的cpu，只会占25g内存&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;DATA_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./data/XNLI-MT-1.0-dwk/
&lt;span class=&quot;nv&quot;&gt;$python&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; run_classifier.py &lt;span class=&quot;nt&quot;&gt;--task_name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TASK_NAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--use_cuda&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--do_train&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--do_val&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--do_test&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--batch_size&lt;/span&gt; 1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--in_tokens&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--init_pretraining_params&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;BERT_BASE_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/params &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;DATA_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--vocab_path&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;BERT_BASE_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/vocab.txt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--checkpoints&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;CKPT_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--save_steps&lt;/span&gt; 50 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--weight_decay&lt;/span&gt;  0.01 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--warmup_proportion&lt;/span&gt; 0.0 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--validation_steps&lt;/span&gt; 2500 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--epoch&lt;/span&gt; 1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--max_seq_len&lt;/span&gt; 8 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--bert_config_path&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;BERT_BASE_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/bert_config.json &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--learning_rate&lt;/span&gt; 1e-4 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--skip_steps&lt;/span&gt; 1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--random_seed&lt;/span&gt; 1
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;save_inference_model&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;### 如果是56核cpu，会占22g内存..&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;DATA_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./data/XNLI-1.0-demo/
&lt;span class=&quot;nv&quot;&gt;$python&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; predict_classifier.py &lt;span class=&quot;nt&quot;&gt;--task_name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TASK_NAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--use_cuda&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--batch_size&lt;/span&gt; 1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;DATA_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--vocab_path&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;BERT_BASE_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/vocab.txt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--do_lower_case&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--init_checkpoint&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;INIT_CKPT_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--max_seq_len&lt;/span&gt; 8 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--bert_config_path&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;BERT_BASE_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/bert_config.json &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--do_predict&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--save_inference_model_path&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SAVE_INFERENCE_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;main&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    finetune_xnli
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$?&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;1
    save_inference_model
    &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$?&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;1
    &lt;span class=&quot;k&quot;&gt;return &lt;/span&gt;0
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

main 2&amp;gt;&amp;amp;1 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;线上infer部分&quot;&gt;线上infer部分&lt;/h3&gt;

&lt;p&gt;参考这个readme:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/models/tree/develop/PaddleNLP/language_representations_kit/BERT/inference&quot;&gt;https://github.com/PaddlePaddle/models/tree/develop/PaddleNLP/language_representations_kit/BERT/inference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;生成demo文件可以参考&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;TASK_NAME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;xnli&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;DATA_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;../data/XNLI-1.0/
&lt;span class=&quot;nv&quot;&gt;BERT_BASE_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;../pretrained_model/chinese_L-12_H-768_A-12/
&lt;span class=&quot;nv&quot;&gt;python&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;../../../python-2.7.14-paddle-1.3.1/bin/python
&lt;span class=&quot;nv&quot;&gt;$python&lt;/span&gt; gen_demo_data.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--task_name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TASK_NAME&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--data_path&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;DATA_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--vocab_path&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;BERT_BASE_PATH&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/vocab.txt&quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;nt&quot;&gt;--batch_size&lt;/span&gt; 1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
           &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; bert_data.demo

&lt;span class=&quot;c&quot;&gt;#           --in_tokens \&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;运行示例：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;INFERENCE_MODEL_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./output/infer_step_50
&lt;span class=&quot;nv&quot;&gt;DATA_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./bert_data.demo
&lt;span class=&quot;nv&quot;&gt;REPEAT_TIMES&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
./bin/bert_demo &lt;span class=&quot;nt&quot;&gt;--logtostderr&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;--model_dir&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$INFERENCE_MODEL_PATH&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;--data&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$DATA_PATH&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;--repeat&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$REPEAT_TIMES&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;--output_prediction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>BAM</title>
   <link href="http://daiwk.github.io/posts/nlp-bam.html"/>
   <updated>2019-03-19T00:00:00+00:00</updated>
   <id>/posts/nlp-bam</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#模型结构&quot;&gt;模型结构&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#方法&quot;&gt;方法&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#多任务训练&quot;&gt;多任务训练&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#知识蒸馏&quot;&gt;知识蒸馏&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650758780&amp;amp;idx=4&amp;amp;sn=55fc290af6341d11184793ebcbbd02ca&amp;amp;chksm=871a9a02b06d13145f6b53cfb3900de5136fcfad6d88c0c7c93859bb383abf541122c5089c68&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=OMkeru0hKbW%2FzHQFUoZof%2F%2BUDb%2BCgp5lNk%2B8t%2FD%2F3LWLuutOtmN3PBqbGS0R1bbs#rd&quot;&gt;BAM！利用知识蒸馏和多任务学习构建的通用语言模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文地址：&lt;a href=&quot;https://openreview.net/pdf?id=SylnYlqKw4&quot;&gt;BAM! Born-Again Multi-Task Networks for Natural Language Understanding&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;构建能够执行多个任务的单个模型一直是NLP领域的难题。多任务NLP对很多应用而言是无效的，多任务模型通常比单任务模型性能差。但是该研究提出利用知识蒸馏方法，让单任务模型高效教导多任务模型，从而在不同单任务上都有很好的表现。&lt;/p&gt;

&lt;p&gt;知识蒸馏即将知识&lt;strong&gt;从「教师」模型迁移到「学生」模型&lt;/strong&gt;，执行方式为训练&lt;strong&gt;学生&lt;/strong&gt;模型&lt;strong&gt;模仿教师&lt;/strong&gt;模型的预测。在&lt;a href=&quot;https://arxiv.org/pdf/1805.04770.pdf&quot;&gt;Born-Again Neural Networks&lt;/a&gt;中，教师和学生具备&lt;strong&gt;同样的神经网络架构和模型大小&lt;/strong&gt;，然而令人惊讶的是，学生网络的性能超越了教师网络。该研究将这一想法扩展到多任务模型训练环境中。&lt;/p&gt;

&lt;p&gt;研究者使用多个变体对比Single-&amp;gt;Multi born-again知识蒸馏，这些变体包括single-&amp;gt;single的知识蒸馏和multi-&amp;gt;multi的知识蒸馏，还有single-&amp;gt;multi-&amp;gt;single-&amp;gt;multi。此外，该研究还提出了一个简单的教师退火（teacher annealing）方法，帮助学生模型超越教师模型，大幅改善预测结果。&lt;/p&gt;

&lt;h2 id=&quot;模型结构&quot;&gt;模型结构&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/bam.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;上面是整体模型的结构，其采用&lt;strong&gt;多个任务&lt;/strong&gt;的&lt;strong&gt;单模型&lt;/strong&gt;与&lt;strong&gt;对应标签&lt;/strong&gt;作为输入。其中&lt;strong&gt;多任务模型&lt;/strong&gt;主要&lt;strong&gt;基于BERT&lt;/strong&gt;，因此该多任务模型能通过知识蒸馏学习到各单任务模型的语言知识。模型会有一个教师退火的过程，即最开始由多个单任务模型教多任务模型学习，而随着&lt;strong&gt;训练的进行&lt;/strong&gt;，多任务模型将&lt;strong&gt;更多使用真实任务标签&lt;/strong&gt;进行训练(即图中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\lambda\)&lt;/code&gt;从0慢慢变到1)。&lt;/p&gt;

&lt;h2 id=&quot;方法&quot;&gt;方法&lt;/h2&gt;

&lt;h3 id=&quot;多任务训练&quot;&gt;多任务训练&lt;/h3&gt;

&lt;p&gt;模型：该研究所有模型均基于BERT构建。该模型将byte-pair-tokenized的输入句子传输到Transformer网络，为每个token生成语境化的表征。最后一层的特征向量用token &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLS&lt;/code&gt;的向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;来表示。对于分类任务，研究者使用标准softmax层直接分类，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(softmax(Wc)\)&lt;/code&gt;。对于回归任务，接一个size是1的nn并使用sigmoid激活函数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sigmoid(w^Tc)\)&lt;/code&gt;。在该研究开发的多任务模型中，除了基于BERT的分类器，&lt;strong&gt;所有模型参数在所有任务上共享&lt;/strong&gt;，这意味着不到 0.01% 的参数是task-specific的。和BERT一样，字符级词嵌入和Transformer使用「masked LM」预训练阶段的权重进行初始化。&lt;/p&gt;

&lt;p&gt;训练：单任务训练按照BERT原文的研究来执行。至于多任务训练，研究者将&lt;strong&gt;打乱不同任务的顺序&lt;/strong&gt;，即使在&lt;strong&gt;小批量内也会进行shuffle&lt;/strong&gt;。最后模型的训练为&lt;strong&gt;最小化所有任务上的（未加权）损失和&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;知识蒸馏&quot;&gt;知识蒸馏&lt;/h3&gt;

&lt;p&gt;使用知识蒸馏方法，让单任务模型来教多任务模型。这里学生网络和教师网络具备同样的模型架构。&lt;/p&gt;

&lt;p&gt;知识蒸馏中&lt;strong&gt;学生&lt;/strong&gt;网络要&lt;strong&gt;模仿教师&lt;/strong&gt;网络，这有可能导致学生网络受限于教师网络的性能，无法超过教师网络。为了解决该问题，该研究提出&lt;strong&gt;teacher annealing&lt;/strong&gt;，在训练过程中混合教师预测和gold label。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SimPLe</title>
   <link href="http://daiwk.github.io/posts/rl-simple.html"/>
   <updated>2019-03-12T00:00:00+00:00</updated>
   <id>/posts/rl-simple</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%A6%82%E8%BF%B0&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650758447&amp;amp;idx=4&amp;amp;sn=4161f81dfd48317ae37e4cae1318edfe&amp;amp;chksm=871a9951b06d1047a214467941b60d87e8a27f7146a58bf092d2a2573df91b07d293294f3680&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=7RpWEE9pG33VI7QrEdlUejKpWhD51hkOFZcYbWAKlJdi8gHu1RETFQwn8uhoCs2u#rd&quot;&gt;仅需2小时学习，基于模型的强化学习方法可以在Atari上实现人类水平&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文地址：&lt;a href=&quot;https://arxiv.org/pdf/1903.00374.pdf&quot;&gt;Model Based Reinforcement Learning for Atari&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;无模型强化学习能够用于在复杂任务（如雅达利游戏，甚至基于图像观测）中学习非常有效的策略。但是，这通常需要&lt;strong&gt;非常大量的交互&lt;/strong&gt;——事实上，比人类掌握相同游戏需要的次数更多。为什么人类可以学习如此之快？部分原因可能是，人类能够学习游戏原理，并预测出哪个动作会带来想要的结果。在本文中，我们探索了如何&lt;strong&gt;基于视频预测模型&lt;/strong&gt;来达到类似效果，让智能体能够通过&lt;strong&gt;更少的交互（相较于无模型方法而言，次数降低了几个数量级）&lt;/strong&gt;，通过雅达利游戏。本文讨论了模拟策略学习（SimPLe），一个基于视频预测模型的完全基于模型的深度强化学习算法，并比较了几种模型架构，包括一个在本文设定下得出最优结果的全新架构。我们在一系列雅达利游戏上测试评估了SimPLe，实验结果显示，仅通过10万次智能体和环境之间的交互（40万帧），约2小时的实时学习，SimPLe 就可获得有竞争力的结果。&lt;/p&gt;

&lt;p&gt;效果最好的模型是前馈卷积神经网络。它利用一组卷积对一系列输入帧进行编码，并给定智能体采取的行动，然后利用一组反卷积对下一帧进行解码。奖励是基于瓶颈表征（bottleneck representation）预测的。&lt;/p&gt;

&lt;p&gt;将&lt;strong&gt;随机性&lt;/strong&gt;引入模型会带来不错的效果，可以让策略在训练阶段尝试更多不同的场景。为此，研究人员添加了一个&lt;strong&gt;隐变量&lt;/strong&gt;，而来自隐变量的样本被添加至瓶颈表征。在&lt;strong&gt;离散变量&lt;/strong&gt;该设定下效果最优，被&lt;strong&gt;编码为比特序列&lt;/strong&gt;。模型的整体架构类似于&lt;strong&gt;变分自编码器&lt;/strong&gt;，其中隐变量上的后验是基于&lt;strong&gt;整个序列（输入帧+目标帧）近似&lt;/strong&gt;得到，从该后验中&lt;strong&gt;抽取一个值&lt;/strong&gt;，并将该值与输入帧和action一起用于预测下一帧。在&lt;strong&gt;推断阶段&lt;/strong&gt;，latent code由&lt;strong&gt;自回归 LSTM&lt;/strong&gt;网络生成。&lt;/p&gt;

&lt;p&gt;网络结构如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/SimPLe-arch.png&quot; style=&quot;max-height: 380px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;模型输入是 4 个堆叠的帧（以及智能体选择的策略），输出则是预测的下一帧及预期奖励。&lt;/p&gt;

&lt;p&gt;用全连接层嵌入输入像素和action，在输出中有像素级的softmax（256 色）函数。&lt;/p&gt;

&lt;p&gt;该模型有两个主要组成部分。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先，网络底部由带有&lt;strong&gt;残差连接&lt;/strong&gt;的&lt;strong&gt;卷积编码器和解码器(反卷积)&lt;/strong&gt;组成。为了根据智能体的行动调节输出，&lt;strong&gt;解码器&lt;/strong&gt;中&lt;strong&gt;每一层的输出&lt;/strong&gt;都&lt;strong&gt;乘以learned embedded action&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;模型的第二部分是&lt;strong&gt;卷积推断网络&lt;/strong&gt;，类似于 Babaeizadeh 等人 (2017) &lt;a href=&quot;https://arxiv.org/abs/1710.11252&quot;&gt;Stochastic variational video prediction&lt;/a&gt;的观点，它在给定下一帧的条件下&lt;strong&gt;近似估计后验&lt;/strong&gt;。
    &lt;ul&gt;
      &lt;li&gt;在训练阶段(左上角的@training)，从&lt;strong&gt;近似后验抽样&lt;/strong&gt;得到的&lt;strong&gt;隐变量&lt;/strong&gt;值将&lt;strong&gt;离散化&lt;/strong&gt;为比特。为使模型&lt;strong&gt;可微&lt;/strong&gt;，&lt;strong&gt;反向传播&lt;/strong&gt;根据 Kaiser &amp;amp; Bengio (2018) &lt;a href=&quot;https://arxiv.org/abs/1801.09797&quot;&gt;Discrete autoencoders for sequence models&lt;/a&gt;的方法&lt;strong&gt;避开离散化&lt;/strong&gt;，并训练第三个基于LSTM的网络，以在给定先前比特时近似估计当前比特。&lt;/li&gt;
      &lt;li&gt;在推断阶段(右上角的@inference)，利用该网络&lt;strong&gt;自回归地(auto-regressively)&lt;/strong&gt;预测latent bits。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;确定性模型（deterministic model）与上图架构相同，但&lt;strong&gt;不包含推断网络&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor/rl&quot;&gt;https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor/rl&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;包含视频的url：&lt;a href=&quot;https://sites.google.com/view/modelbasedrlatari/home&quot;&gt;https://sites.google.com/view/modelbasedrlatari/home&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>parl</title>
   <link href="http://daiwk.github.io/posts/rl-parl.html"/>
   <updated>2019-03-01T00:00:00+00:00</updated>
   <id>/posts/rl-parl</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8&quot;&gt;简单使用&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#parl%E6%A1%86%E6%9E%B6&quot;&gt;PARL框架&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#quickstart%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90&quot;&gt;quickstart代码解析&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;简单使用&quot;&gt;简单使用&lt;/h2&gt;

&lt;p&gt;代码&lt;a href=&quot;https://github.com/PaddlePaddle/PARL&quot;&gt;https://github.com/PaddlePaddle/PARL&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;parl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以从源码安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;paddlepaddle

pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;gym
git clone https://github.com/PaddlePaddle/PARL.git
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;PARL
pip &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后跑quickstart：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;examples/QuickStart/
python train.py
&lt;span class=&quot;c&quot;&gt;# &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者将评估过程可视化：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python train.py &lt;span class=&quot;nt&quot;&gt;--eval_vis&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就可以得到下图的结果&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/parl-quickstart.png&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;parl框架&quot;&gt;PARL框架&lt;/h2&gt;

&lt;p&gt;主要抽象为Model、Algorithm、Agent三大模块：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Model：定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;policy network&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;critic network&lt;/code&gt;，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt;作为输入，建立一个前向网络&lt;/li&gt;
  &lt;li&gt;Algorithm：定义更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Model&lt;/code&gt;中的参数的机制，经常有不止一个model&lt;/li&gt;
  &lt;li&gt;Agent：是环境和Algorithm间的桥梁。负责与外界的数据I/O，并在把数据feed给训练process之前进行数据预处理&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;quickstart代码解析&quot;&gt;quickstart代码解析&lt;/h2&gt;

&lt;p&gt;我们看一下quickstart的例子，这是一个policygradient：&lt;/p&gt;

&lt;p&gt;Model如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;parl.layers&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;parl.framework.model_base&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CartpoleModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;hid1_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hid1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'tanh'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'softmax'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Algorithm如下，直接用的pg：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;parl.algorithms&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PolicyGradient&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;alg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PolicyGradient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hyperparas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'lr'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LEARNING_RATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Agent就有点复杂啦：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;parl.layers&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;parl.framework.agent_base&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Agent&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CartpoleAgent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Agent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;algorithm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs_dim&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CartpoleAgent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;algorithm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred_program&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_program&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_startup_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;program_guard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# obs相当于是state
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;define_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;program_guard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# obs相当于是state
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'act'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'int64'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'reward'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;define_learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expand_dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fluid_executor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)},&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;fetch_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 从数组的形状中删除单维度条目，即把shape中为1的维度去掉，参考https://daiwk.github.io/posts/knowledge-numpy-usage.html#npsqueeze
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squeeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expand_dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fluid_executor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)},&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;fetch_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 从数组的形状中删除单维度条目，即把shape中为1的维度去掉，参考https://daiwk.github.io/posts/knowledge-numpy-usage.html#npsqueeze
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squeeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 取出概率最大的那个动作
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 在act的最后面加多一维，why?...
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expand_dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'act'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'int64'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'reward'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fluid_executor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fetch_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用时，先定义训练方法（reset环境，然后根据当前state采样出一个action，然后在环境中执行这个action，得到reward，这些(s,a,r)都存起来，如果done了，就返回这些(s,a,r)）：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run_train_episode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;agent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;obs_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;obs_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;agent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;action_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后定义计算discounted reward的函数，不断地乘gamma，再做个归一化&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;calc_discount_norm_reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gamma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;discount_cumulative_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;discount_cumulative_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;gamma&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discount_cumulative_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discount_cumulative_reward&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;discount_norm_reward&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后定义evaluate的方法，其实就是先reset环境，然后根据s，通过agent的predict得到一个a，然后执行这个a，得到reward，累加这个reward（&lt;strong&gt;注意！！！这里没有gamma了！！！&lt;/strong&gt;），直到done，返回当前的累积reward：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run_evaluate_episode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;agent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;all_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eval_vis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;agent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;all_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_reward&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后就是整体流程啦：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;CartPole-v0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SEED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SEED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CartpoleModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ACT_DIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;alg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PolicyGradient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hyperparas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'lr'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LEARNING_RATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;agent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CartpoleAgent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obs_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OBS_DIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ACT_DIM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SEED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;obs_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_train_episode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;agent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Episode {}, Reward Sum {}.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;batch_obs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obs_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calc_discount_norm_reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reward_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GAMMA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;agent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_obs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;all_reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;run_evaluate_episode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;agent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Test reward: {}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>lingvo</title>
   <link href="http://daiwk.github.io/posts/platform-lingvo.html"/>
   <updated>2019-02-26T00:00:00+00:00</updated>
   <id>/posts/platform-lingvo</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#安装及基本使用&quot;&gt;安装及基本使用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#安装&quot;&gt;安装&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#跑lenet5&quot;&gt;跑lenet5&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#跑机器翻译&quot;&gt;跑机器翻译&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#自定义参数和模型&quot;&gt;自定义参数和模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#自定义参数&quot;&gt;自定义参数&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#现有模型&quot;&gt;现有模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#语音识别asr-automatic-speech-recogition&quot;&gt;语音识别(asr, Automatic Speech Recogition)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#语言模型&quot;&gt;语言模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#机器翻译&quot;&gt;机器翻译&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/tensorflow/lingvo&quot;&gt;https://github.com/tensorflow/lingvo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/abs/1902.08295&quot;&gt;Lingvo: a Modular and Scalable Framework for Sequence-to-Sequence Modeling&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lingvo 是一个能够为协作式深度学习研究提供完整解决方案的 Tensorflow 框架，尤其关注序列到序列模型。Lingvo 模型由模块化构件组成，这些构件灵活且易于扩展，实验配置集中且可定制。分布式训练和量化推理直接在框架内得到支持，框架内包含大量 utilities、辅助函数和最新研究思想的现有实现。&lt;/p&gt;

&lt;p&gt;设计原则如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;单个代码块应该精细且&lt;strong&gt;模块化&lt;/strong&gt;，它们会使用相同的接口，同时也&lt;strong&gt;容易扩展&lt;/strong&gt;；&lt;/li&gt;
  &lt;li&gt;实验应该是共享的、可比较的、可复现的、可理解的和正确的；&lt;/li&gt;
  &lt;li&gt;性能应该可以&lt;strong&gt;高效地扩展到生产规模的数据集&lt;/strong&gt;，或拥有&lt;strong&gt;数百个加速器的分布式训练系统&lt;/strong&gt;；&lt;/li&gt;
  &lt;li&gt;当模型从&lt;strong&gt;研究转向产品&lt;/strong&gt;时应该尽可能共享代码。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;我们可以发现好多paper都是基于lingvo跑的呢：&lt;a href=&quot;https://github.com/tensorflow/lingvo/blob/master/PUBLICATIONS.md&quot;&gt;https://github.com/tensorflow/lingvo/blob/master/PUBLICATIONS.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;api文档：&lt;a href=&quot;https://tensorflow.github.io/lingvo/&quot;&gt;https://tensorflow.github.io/lingvo/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;安装及基本使用&quot;&gt;安装及基本使用&lt;/h2&gt;

&lt;h3 id=&quot;安装&quot;&gt;安装&lt;/h3&gt;

&lt;p&gt;首先的首先，需要安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;tf-nightly
pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;tensorflow &lt;span class=&quot;nt&quot;&gt;-U&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# 至少要是2.0的哦，后面会讲原因，可以自己编个whl出来，再pip本地安装&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;首先下载数据集(如果遇到下载的ssl问题，可以参考&lt;a href=&quot;https://daiwk.github.io/posts/knowledge-tf-usage.html#%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98&quot;&gt;https://daiwk.github.io/posts/knowledge-tf-usage.html#%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98&lt;/a&gt;)&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /tmp/mnist
bazel run &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; opt //lingvo/tools:keras2ckpt &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--dataset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;mnist &lt;span class=&quot;nt&quot;&gt;--out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/mnist/mnist
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后build一个trainer，如果出现如下错误。。把装了tf的py扔到PATH里就行。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; opt //lingvo:trainer
ERROR: /home/disk2/daiwenkai/workspaces/tf/lingvo/lingvo/core/ops/BUILD:24:1: no such package &lt;span class=&quot;s1&quot;&gt;'@tensorflow_solib//'&lt;/span&gt;: Traceback &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;most recent call last&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:
        File &lt;span class=&quot;s2&quot;&gt;&quot;/home/disk2/daiwenkai/workspaces/tf/lingvo/lingvo/repo.bzl&quot;&lt;/span&gt;, line 88
                _find_tf_lib_path&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;repo_ctx&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        File &lt;span class=&quot;s2&quot;&gt;&quot;/home/disk2/daiwenkai/workspaces/tf/lingvo/lingvo/repo.bzl&quot;&lt;/span&gt;, line 30, &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;_find_tf_lib_path
                fail&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Could not locate tensorflow ins...&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Could not locate tensorflow installation path. and referenced by &lt;span class=&quot;s1&quot;&gt;'//lingvo/core/ops:x_ops'&lt;/span&gt;
ERROR: Analysis of target &lt;span class=&quot;s1&quot;&gt;'//lingvo:trainer'&lt;/span&gt; failed&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; build aborted: no such package &lt;span class=&quot;s1&quot;&gt;'@tensorflow_solib//'&lt;/span&gt;: Traceback &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;most recent call last&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:
        File &lt;span class=&quot;s2&quot;&gt;&quot;/home/disk2/daiwenkai/workspaces/tf/lingvo/lingvo/repo.bzl&quot;&lt;/span&gt;, line 88
                _find_tf_lib_path&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;repo_ctx&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        File &lt;span class=&quot;s2&quot;&gt;&quot;/home/disk2/daiwenkai/workspaces/tf/lingvo/lingvo/repo.bzl&quot;&lt;/span&gt;, line 30, &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;_find_tf_lib_path
                fail&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Could not locate tensorflow ins...&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Could not locate tensorflow installation path.
INFO: Elapsed &lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt;: 5.916s
INFO: 0 processes.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;想看详细日志，可以：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; opt //lingvo:trainer &lt;span class=&quot;nt&quot;&gt;--sandbox_debug&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;想把当前的PATH加进去，可以：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; opt //lingvo:trainer &lt;span class=&quot;nt&quot;&gt;--sandbox_debug&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--action_env&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;PATH
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样，就会显示：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ERROR: /home/disk2/daiwenkai/workspaces/tf/lingvo/lingvo/core/ops/BUILD:304:1: Executing genrule //lingvo/core/ops:hyps_proto_gencc failed &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Exit 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; process-wrapper failed: error executing &lt;span class=&quot;nb&quot;&gt;command&lt;/span&gt; 
  &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /home/work/.cache/bazel/_bazel_work/6019387a2835fd5f247d5cbc29b5ee5a/execroot/__main__ &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;exec env&lt;/span&gt; - &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/opt/compiler/gcc-4.8.2/bin/:/home/disk2/daiwenkai/tools/python-2.7.14/bin/:/home/disk2/daiwenkai/workspaces/tf/prepare/:/home/disk2/daiwenkai/workspaces/tf/prepare/jdk1.8.0_152/bin:/home/work/.hmpclient/bin:/home/work/.BCloud/bin:/home/work/.hmpclient/bin:/home/work/.jumbo/opt/sun-java6/bin:/home/work/.jumbo/opt/sun-java6/jre/bin:/home/work/.jumbo/opt/sun-java6/db/bin:/home/work/.jumbo/bin/core_perl:/home/work/.jumbo/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/bin:/home/opt/bin &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;TMPDIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  /home/work/.cache/bazel/_bazel_work/install/8e122cdaf21df7dee88c59e8c0fa6061/_embedded_binaries/process-wrapper &lt;span class=&quot;s1&quot;&gt;'--timeout=0'&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'--kill_delay=15'&lt;/span&gt; /bin/bash &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'source external/bazel_tools/tools/genrule/genrule-setup.sh; 
          mkdir -p bazel-out/k8-opt/genfiles/lingvo/core/ops/tf_proto.$$;
          tar -C bazel-out/k8-opt/genfiles/lingvo/core/ops/tf_proto.$$ -xf bazel-out/host/genfiles/lingvo/tf_protos.tar;
          external/protobuf_protoc/bin/protoc --proto_path=bazel-out/k8-opt/genfiles/lingvo/core/ops/tf_proto.$$  --proto_path=. --cpp_out=bazel-out/k8-opt/genfiles lingvo/core/ops/hyps.proto;
          rm -rf bazel-out/k8-opt/genfiles/lingvo/core/ops/tf_proto.$$
        '&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
external/protobuf_protoc/bin/protoc: /lib64/tls/libc.so.6: version &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;GLIBC_2.4&lt;span class=&quot;s1&quot;&gt;' not found (required by external/protobuf_protoc/bin/protoc)
Target //lingvo:trainer failed to build
Use --verbose_failures to see the command lines of failed build steps.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个时候我们会发现，，因为依赖的是用gcc4.8编译好的protoc，而下下来本地跑bazel的时候，会用本地默认的lib64，所以不行，所以我们用trick来搞，对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lingvo/lingvo.bzl&lt;/code&gt;进行如下修改，也就是&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一方面把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/compiler/gcc-4.8.2/lib/ld-linux-x86-64.so.2 --library-path /opt/compiler/gcc-4.8.2/lib&lt;/code&gt;加到protoc前面，&lt;/li&gt;
  &lt;li&gt;另一方面，手动把protc的zip下载下来，例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lingvo/repos.bzl&lt;/code&gt;里要求的是3.6.1版本，然后解压到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./protobuf3.6.1_dir&lt;/code&gt;目录，所以咱们把里面的include目录(里面有一堆.proto文件)加进来『&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-I/home/disk2/daiwenkai/workspaces/tf/lingvo/protobuf3.6.1_dir/include&lt;/code&gt;』&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_proto_gen_cc_src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;genrule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;srcs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.proto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;outs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.pb.cc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.pb.h&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tools&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;@protobuf_protoc//:protoc_bin&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;//lingvo:tf_dot_protos&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;      
        &lt;span class=&quot;c1&quot;&gt;# TODO(drpng): only unpack if tf_proto dependency is requested.
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
          mkdir -p $(@D)/tf_proto.$$$$;
          tar -C $(@D)/tf_proto.$$$$ -xf $(location //lingvo:tf_dot_protos);
          /opt/compiler/gcc-4.8.2/lib/ld-linux-x86-64.so.2 --library-path /opt/compiler/gcc-4.8.2/lib $(location @protobuf_protoc//:protoc_bin) --proto_path=$(@D)/tf_proto.$$$$ -I/home/disk2/daiwenkai/workspaces/tf/lingvo/protobuf3.6.1_dir/include --proto_path=. --cpp_out=$(GENDIR) $(&amp;lt;);
          rm -rf $(@D)/tf_proto.$$$$
        &quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_proto_gen_py_src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;genrule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;srcs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.proto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;outs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_pb2.py&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tools&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;@protobuf_protoc//:protoc_bin&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;//lingvo:tf_dot_protos&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;      
        &lt;span class=&quot;c1&quot;&gt;# TODO(drpng): only unpack if tf_proto dependency is requested.
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
          mkdir -p $(@D)/tf_proto.$$$$;
          tar -C $(@D)/tf_proto.$$$$ -xf $(location //lingvo:tf_dot_protos);
          /opt/compiler/gcc-4.8.2/lib/ld-linux-x86-64.so.2 --library-path /opt/compiler/gcc-4.8.2/lib $(location @protobuf_protoc//:protoc_bin) --proto_path=$(@D)/tf_proto.$$$$ -I/home/disk2/daiwenkai/workspaces/tf/lingvo/protobuf3.6.1_dir/include --proto_path=. --python_out=$(GENDIR) $(&amp;lt;);
          rm -rf $(@D)/tf_proto.$$$$
        &quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意！！！这里的protobuf版本要求是3.6.1！！！&lt;/p&gt;

&lt;p&gt;为什么呢。。。发现我们从tf1.3源码build出来的include里会出现这种问题&lt;a href=&quot;https://github.com/tensorflow/lingvo/issues/39&quot;&gt;https://github.com/tensorflow/lingvo/issues/39&lt;/a&gt;，于是。。从源码编个tf2.0就行啦！！！！！&lt;/p&gt;

&lt;p&gt;因为你的tf1.3的，那么protobuf是3.6.0的，所以是不行的呢！！&lt;/p&gt;

&lt;p&gt;也就是说，把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lingvo/repo.bzl&lt;/code&gt;必须是原来的3.6.1！！不要乱改成3.6.0。。。：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lingvo_protoc_deps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;http_archive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;protobuf_protoc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;build_file_content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
filegroup(
    name = &quot;protoc_bin&quot;,
    srcs = [&quot;bin/protoc&quot;],
    visibility = [&quot;//visibility:public&quot;],
)
&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;urls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#&quot;https://github.com/google/protobuf/releases/download/v3.6.0/protoc-3.6.0-linux-x86_64.zip&quot;,
&lt;/span&gt;            &lt;span class=&quot;s&quot;&gt;&quot;https://github.com/google/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;      
        &lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;6003de742ea3fcf703cfec1cd4a3380fd143081a2eb0e559065563496af27807&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#sha256 = &quot;84e29b25de6896c6c4b22067fb79472dac13cf54240a7a210ef1cac623f5231d&quot;,
&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;跑lenet5&quot;&gt;跑lenet5&lt;/h3&gt;

&lt;p&gt;然后就可以跑啦&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel-bin/lingvo/trainer &lt;span class=&quot;nt&quot;&gt;--run_locally&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;cpu &lt;span class=&quot;nt&quot;&gt;--mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sync&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;image.mnist.LeNet5 &lt;span class=&quot;nt&quot;&gt;--logdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/mnist/log &lt;span class=&quot;nt&quot;&gt;--logtostderr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果出现：『locale.Error: unsupported locale setting』这种报错！！&lt;/p&gt;

&lt;p&gt;那么！！&lt;a href=&quot;https://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting&quot;&gt;https://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting&lt;/a&gt;这个解答是完美的！！&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;LC_ALL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;C
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意，跑的时候要装matplotlib，而且要用tk的东西（记得把libtk*，还有libtcl*拷到python的lib-dynload目录下去）~&lt;/p&gt;

&lt;p&gt;当然，我们可以看tensorboard，使用&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;tensorboard &lt;span class=&quot;nt&quot;&gt;--logdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/mnist/log/ &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8023
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果提示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;E0309 04:14:00.829349 139808152127232 program.py:232] Tensorboard could not &lt;span class=&quot;nb&quot;&gt;bind &lt;/span&gt;to unsupported address family ::
ERROR: Tensorboard could not &lt;span class=&quot;nb&quot;&gt;bind &lt;/span&gt;to unsupported address family ::
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;那么，我们可以加上host参数：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;tensorboard &lt;span class=&quot;nt&quot;&gt;--logdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/mnist/log/ &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8023 &lt;span class=&quot;nt&quot;&gt;--host&lt;/span&gt; bj01-aaa.bbb.bj01
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以看到：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/lingvo-lenet5.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;这样，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/mnist/log/control&lt;/code&gt;目录下，就有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;params.txt: hyper-parameters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;add_summary : True
allow_implicit_capture : NoneType
cls : &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;/lingvo.core.base_model/SingleTaskModel
cluster.add_summary : NoneType
cluster.cls : &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;/lingvo.core.cluster/_Cluster
cluster.controller.devices_per_split : 1
cluster.controller.gpus_per_replica : 0
cluster.controller.name : &lt;span class=&quot;s1&quot;&gt;'/job:local'&lt;/span&gt;
cluster.controller.num_tpu_hosts : 0
cluster.controller.replicas : 1
cluster.controller.tpus_per_replica : 0
cluster.decoder.devices_per_split : 1
cluster.decoder.gpus_per_replica : 0
cluster.decoder.name : &lt;span class=&quot;s1&quot;&gt;'/job:local'&lt;/span&gt;

...

task.train.max_steps : 4000000
task.train.optimizer.add_summary : True
task.train.optimizer.allow_implicit_capture : NoneType
task.train.optimizer.beta1 : 0.9
task.train.optimizer.beta2 : 0.999
task.train.optimizer.cls : &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;/lingvo.core.optimizer/Adam
task.train.optimizer.dtype : float32
task.train.optimizer.epsilon : 1e-06
task.train.optimizer.fprop_dtype : NoneType
task.train.optimizer.inference_driver_name : NoneType
task.train.optimizer.is_eval : NoneType
task.train.optimizer.is_inference : NoneType
task.train.optimizer.name : &lt;span class=&quot;s1&quot;&gt;'Adam'&lt;/span&gt;
task.train.optimizer.params_init.method : &lt;span class=&quot;s1&quot;&gt;'xavier'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;model_analysis.txt: model sizes for each layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;_task.conv[0].w           &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;5, 5, 1, 20&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;               500 lenet5/conv0/w/var
_task.conv[1].w           &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;5, 5, 20, 50&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;            25000 lenet5/conv1/w/var
_task.fc.b                &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;300,&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                      300 lenet5/fc/b/var
_task.fc.w                &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;2450, 300&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;              735000 lenet5/fc/w/var
_task.softmax.bias_0      &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;10,&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                        10 lenet5/softmax/bias_0/var
_task.softmax.weight_0    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;300, 10&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                  3000 lenet5/softmax/weight_0/var
&lt;span class=&quot;o&quot;&gt;====================================================================================================&lt;/span&gt;
total &lt;span class=&quot;c&quot;&gt;#params:     763810&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;train.pbtxt: the training tf.GraphDef.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;node &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  name: &lt;span class=&quot;s2&quot;&gt;&quot;global_step/Initializer/zeros&quot;&lt;/span&gt;
  op: &lt;span class=&quot;s2&quot;&gt;&quot;Const&quot;&lt;/span&gt; 
  attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;  
    key: &lt;span class=&quot;s2&quot;&gt;&quot;_class&quot;&lt;/span&gt;
    value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
      list &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;  
        s: &lt;span class=&quot;s2&quot;&gt;&quot;loc:@global_step&quot;&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;  
    key: &lt;span class=&quot;s2&quot;&gt;&quot;dtype&quot;&lt;/span&gt; 
    value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_INT64
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;  
    key: &lt;span class=&quot;s2&quot;&gt;&quot;value&quot;&lt;/span&gt; 
    value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
      tensor &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        dtype: DT_INT64
        tensor_shape &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;       
        int64_val: 0
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
node &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  name: &lt;span class=&quot;s2&quot;&gt;&quot;global_step&quot;&lt;/span&gt;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;events.*: a tensorboard events file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;而在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/mnist/log/train&lt;/code&gt;目录下，有&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ckpt-*: the checkpoint files.&lt;/li&gt;
  &lt;li&gt;checkpoint: a text file containing information about the checkpoint files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;model_checkpoint_path: &lt;span class=&quot;s2&quot;&gt;&quot;/tmp/mnist/log/train/ckpt-00397115&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;/tmp/mnist/log/train/ckpt-00391858&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;/tmp/mnist/log/train/ckpt-00391915&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;/tmp/mnist/log/train/ckpt-00391973&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;/tmp/mnist/log/train/ckpt-00392030&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;跑机器翻译&quot;&gt;跑机器翻译&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/tensorflow/lingvo/tree/master/lingvo/tasks/mt&quot;&gt;https://github.com/tensorflow/lingvo/tree/master/lingvo/tasks/mt&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;自定义参数和模型&quot;&gt;自定义参数和模型&lt;/h2&gt;

&lt;h3 id=&quot;自定义参数&quot;&gt;自定义参数&lt;/h3&gt;

&lt;p&gt;例如，我们想基于lenet5的模型，改一下训练时间，那么需要参考&lt;a href=&quot;https://github.com/tensorflow/lingvo/issues/40&quot;&gt;https://github.com/tensorflow/lingvo/issues/40&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;自己写一个：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_registry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterSingleTaskModel&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LeNet5Custom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LeNet5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;LeNet params for MNIST classification custom.&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;classmethod&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LeNet5Custom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_steps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后跑一遍全流程：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ps aux| &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;tensorboard| &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $2}'&lt;/span&gt;| xargs &lt;span class=&quot;nb&quot;&gt;kill&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-9&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;tensorboard &lt;span class=&quot;nt&quot;&gt;--logdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/mnist/log/ &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8023 &lt;span class=&quot;nt&quot;&gt;--host&lt;/span&gt; xxx-xx-xx &amp;amp;

bazel build &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; opt //lingvo:trainer &lt;span class=&quot;nt&quot;&gt;--sandbox_debug&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--action_env&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;PATH
bazel-bin/lingvo/trainer &lt;span class=&quot;nt&quot;&gt;--run_locally&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;cpu &lt;span class=&quot;nt&quot;&gt;--mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sync&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;image.mnist.LeNet5Custom &lt;span class=&quot;nt&quot;&gt;--logdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/mnist/log &lt;span class=&quot;nt&quot;&gt;--logtostderr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;现有模型&quot;&gt;现有模型&lt;/h2&gt;

&lt;h3 id=&quot;语音识别asr-automatic-speech-recogition&quot;&gt;语音识别(asr, Automatic Speech Recogition)&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/lingvo/blob/master/lingvo/tasks/asr/params/librispeech.py&quot;&gt;asr.librispeech.Librispeech960Grapheme&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/lingvo/blob/master/lingvo/tasks/asr/params/librispeech.py&quot;&gt;asr.librispeech.Librispeech960Wpm&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参考&lt;a href=&quot;https://arxiv.org/pdf/1508.01211.pdf&quot;&gt;Listen, Attend and Spell&lt;/a&gt;以及&lt;a href=&quot;https://arxiv.org/pdf/1412.1602.pdf&quot;&gt;End-to-end Continuous Speech Recognition using Attention-based Recurrent NN: First Results&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;语言模型&quot;&gt;语言模型&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tensorflow/lingvo/blob/master/lingvo/tasks/lm/params/one_billion_wds.py&quot;&gt;lm.one_billion_wds.WordLevelOneBwdsSimpleSampledSoftmax&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参考&lt;a href=&quot;https://arxiv.org/pdf/1602.02410.pdf&quot;&gt;Exploring the Limits of Language Modeling&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;机器翻译&quot;&gt;机器翻译&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tensorflow/lingvo/blob/master/lingvo/tasks/mt/params/wmt14_en_de.py&quot;&gt;mt.wmt14_en_de.WmtEnDeTransformerBase&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tensorflow/lingvo/blob/master/lingvo/tasks/mt/params/wmt14_en_de.py&quot;&gt;mt.wmt14_en_de.WmtEnDeRNMT&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tensorflow/lingvo/blob/master/lingvo/tasks/mt/params/wmtm16_en_de.py&quot;&gt;mt.wmtm16_en_de.WmtCaptionEnDeTransformer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参考ACL2018的&lt;a href=&quot;http://aclweb.org/anthology/P18-1008&quot;&gt;The Best of Both Worlds: Combining Recent Advances in Neural Machine Translation&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>deepgan</title>
   <link href="http://daiwk.github.io/posts/cv-deepgan.html"/>
   <updated>2019-02-24T00:00:00+00:00</updated>
   <id>/posts/cv-deepgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650757542&amp;amp;idx=1&amp;amp;sn=a039ec0bc3421d4814a10e5c71fa8593&amp;amp;chksm=871a9dd8b06d14ce2ef8f9c9b6c3f16d22187e6ee1e35630b03aae06581c869f9a35f83fbb89&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0227MOv2Ys20o1BTIFeHvjOD&amp;amp;pass_ticket=4hmIYvO6GJcf2XjDjrkA6v22Y3ZUCDsA30spOD3nAyih4OfDpXcZPiTcotPvF%2FnT#rd&quot;&gt;史上最强图像生成器BigGAN变身DeepGAN？四倍深度实现更强效果&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>multitask nlu</title>
   <link href="http://daiwk.github.io/posts/platform-multitask-nlu.html"/>
   <updated>2019-02-13T00:00:00+00:00</updated>
   <id>/posts/platform-multitask-nlu</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650756988&amp;amp;idx=5&amp;amp;sn=c4f13f7ef8086fc92fd19542dcce68ff&amp;amp;chksm=871a9302b06d1a1424ab72319f439c1a5898a46c6c92409f75059f1c2de5ba1379052072d814&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0317GFYlOPPmTLikracyQqtE&amp;amp;pass_ticket=y2lMhYAQVbFKus2ttT2YnbLDZ%2FT1PnWsH8UiDzyqogeTkFzFq2AWwaJREF2dZmiv#rd&quot;&gt;GLUE排行榜上全面超越BERT的模型近日公布了！&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1901.11504.pdf&quot;&gt;Multi-Task Deep Neural Networks for Natural Language Understanding&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/multitask-nlu.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;较低层（即文本编码层）在所有任务中共享，而顶层是任务特定的，组合不同类型的 NLU 任务，如单句分类、pair文本分类、文本相似性和相关性排序。与BERT模型类似，MT-DNN分两个阶段进行训练：预训练和微调。与BERT不同的是，MT-DNN在微调阶段使用MTL，在其模型架构中具有多个任务特定层。&lt;/p&gt;

&lt;p&gt;单词序列（可以是一句话或者打包在一起的句子对）作为输入X，先表示为一个嵌入向量序列，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l_1\)&lt;/code&gt;中一个向量对应一个单词。然后 Transformer编码器通过自注意机制捕捉每个单词的语境信息，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l_2\)&lt;/code&gt;中生成语境嵌入序列。这就是多任务目标函数训练得到的共享语义表征。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>meta learning& auto-ml</title>
   <link href="http://daiwk.github.io/posts/dl-meta-learning.html"/>
   <updated>2019-02-02T00:00:00+00:00</updated>
   <id>/posts/dl-meta-learning</id>
   <content type="html">&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#meta-learning&quot;&gt;meta-learning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#auto-ml&quot;&gt;auto-ml&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#fairnas&quot;&gt;fairnas&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#efficientnet&quot;&gt;efficientnet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#adanet&quot;&gt;adanet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#mdenas&quot;&gt;mdenas&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#proxylessnas--haq&quot;&gt;ProxylessNAS &amp;amp; HAQ&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%bf%9b%e5%8c%96%e8%ae%ba%e6%96%b9%e6%b3%95&quot;&gt;进化论方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#autogan&quot;&gt;autogan&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#once-for-all&quot;&gt;once for all&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%ba%94%e7%94%a8&quot;&gt;应用&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;meta-learning&quot;&gt;meta-learning&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://lilianweng.github.io/lil-log/2018/11/30/meta-learning.html&quot;&gt;https://lilianweng.github.io/lil-log/2018/11/30/meta-learning.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;auto-ml&quot;&gt;auto-ml&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.automl.org/wp-content/uploads/2018/12/AutoML-Tutorial-NeurIPS2018-MetaLearning.pdf&quot;&gt;https://www.automl.org/wp-content/uploads/2018/12/AutoML-Tutorial-NeurIPS2018-MetaLearning.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650761726&amp;amp;idx=4&amp;amp;sn=0ce08475039d9c890a6ab8fe5bf85d6f&amp;amp;chksm=871aad80b06d2496af89ecd96c986dbcf419c4083032c6b6bc85a692f96a5996fe19025b3c2b&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=csFmp%2BqPqpbOEtBCr9byDm0vHyp83ccxf21EyZaHyV%2BoFQOLINXIlgzuTkVvCg24#rd&quot;&gt;AutoML研究综述：让AI学习设计AI&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650747841&amp;amp;idx=5&amp;amp;sn=5391948dfdd125be21f7cdd12a6318d1&amp;amp;chksm=871af7bfb06d7ea9e6b1e98aad6a9d1b1083e63ba940decee7e9290c807c73ecefe84e5c7694&amp;amp;scene=21#wechat_redirect&quot;&gt;专栏 | 神经网络架构搜索（NAS）综述（附AutoML资料推荐）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247498834&amp;amp;idx=1&amp;amp;sn=f16cb40771137d0c609bc420b556ee69&amp;amp;chksm=96ea23d2a19daac49f3cb84e44a7198f779a77daa84dbb71d9e93aa8b8949f874565da9f4c72&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1565158428082&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;KDD Cup 2019 AutoML Track冠军深兰科技DeepBlueAI团队技术分享 | 开源代码&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://arxiv.org/pdf/1908.00709v1&quot;&gt;AutoML: A Survey of the State-of-the-Art&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在特定领域构建高质量的深度学习系统不仅耗时，而且需要大量的资源和人类的专业知识。为了缓解这个问题，许多研究正转向自动机器学习。本文是一个全面的 AutoML 论文综述文章，介绍了最新的 SOTA 成果。首先，文章根据机器学习构建管道的流程，介绍了相应的自动机器学习技术。然后总结了现有的神经架构搜索（NAS）研究。论文作者同时对比了 NAS 算法生成的模型和人工构建的模型。最后，论文作者介绍了几个未来研究中的开放问题。&lt;/p&gt;

&lt;h3 id=&quot;fairnas&quot;&gt;fairnas&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650765320&amp;amp;idx=3&amp;amp;sn=76f3d759c7a970950a476ad938816b66&amp;amp;chksm=871abc76b06d3560c4ad5b3978aa9338f067fdaf22eb763a6269780878c1227ed22085c7edd4&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=g0RhlU91yTm4YwdL6HxxS6fDU%2FNvWsf8uqd5BGk9%2Fewn4u2UU5gMclDp6uVTk%2Bm3#rd&quot;&gt;超越MnasNet、Proxyless：小米开源全新神经架构搜索算法FairNAS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1907.01845.pdf&quot;&gt;FairNAS: Rethinking Evaluation Fairness of Weight Sharing Neural Architecture Search&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;源码：&lt;a href=&quot;https://github.com/fairnas/FairNAS&quot;&gt;https://github.com/fairnas/FairNAS&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;efficientnet&quot;&gt;efficientnet&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/cv-efficientnet.html&quot;&gt;https://daiwk.github.io/posts/cv-efficientnet.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;adanet&quot;&gt;adanet&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/platform-adanet.html&quot;&gt;https://daiwk.github.io/posts/platform-adanet.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;mdenas&quot;&gt;mdenas&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650768900&amp;amp;idx=4&amp;amp;sn=d61886200c847f7beca7b68ad1416539&amp;amp;chksm=871a427ab06dcb6c91d4d579cd9e7a95fc3dd08d93110f7ab495fb31d02fc693421869888975&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=mmBhl6hER5JU9q0KMKTTFnbwPDksdn18kk%2FlW9Ih3p2TCzi4%2BlfisKHhCysHq%2Bou#rd&quot;&gt;ICCV 2019 | 四小时搜索NN结构，厦大提出快速NAS检索方法&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.07529&quot;&gt;Multinomial Distribution Learning for Effective Neural Architecture Search&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tanglang96/MDENAS&quot;&gt;https://github.com/tanglang96/MDENAS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;近年来，通过神经架构搜索（NAS）算法生成的架构在各种计算机视觉任务中获得了极强的的性能。然而，现有的 NAS 算法需要再上百个 GPU 上运行 30 多天。在本文中，我们提出了一种基于多项式分布估计快速 NAS 算法，它将搜索空间视为一个多项式分布，我们可以通过采样-分布估计来优化该分布，从而将 NAS 可以转换为分布估计/学习。&lt;/p&gt;

&lt;p&gt;除此之外，本文还提出并证明了一种保序精度排序假设，进一步加速学习过程。在 CIFAR-10 上，通过我们的方法搜索的结构实现了 2.55％的测试误差，GTX1080Ti 上仅 4 个 GPU 小时。在 ImageNet 上，我们实现了 75.2％的 top1 准确度。&lt;/p&gt;

&lt;h3 id=&quot;proxylessnas--haq&quot;&gt;ProxylessNAS &amp;amp; HAQ&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/ulrPhfsPunKAWYohBhkh9w&quot;&gt;寻找最佳的神经网络架构，韩松组两篇论文解读&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1812.00332v2.pdf&quot;&gt;ProxylessNAS: Direct Neural Architecture Search on Target Task and Hardware&lt;/a&gt;,  ICLR2019&lt;/p&gt;

&lt;p&gt;github: &lt;a href=&quot;https://github.com/MIT-HAN-LAB/ProxylessNAS&quot;&gt;https://github.com/MIT-HAN-LAB/ProxylessNAS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1811.08886.pdf&quot;&gt;HAQ: Hardware-Aware Automated Quantization with Mixed Precision&lt;/a&gt;, CVPR2019&lt;/p&gt;

&lt;h3 id=&quot;进化论方法&quot;&gt;进化论方法&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/xVhaIEuWUgPP8Va-hkYjFg&quot;&gt;亚马逊：自动选择AI模型，进化论方法效率更高！&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;来自亚马逊的研究人员探索出了一种可适用于任何计算模型的技术，条件是该模型可以计算出与图灵机相同的功能。（这里的“图灵机”是指定义抽象机的模型，可以根据规则来操纵符号。）&lt;/p&gt;

&lt;p&gt;“无论使用哪种学习算法，选择哪种体系结构或调整训练参数（例如批规模或学习率），选择神经体系结构都不可能为给定的机器学习问题提供最佳解决方案，”Alexa AI机器学习平台服务组织的研究工程师，论文的主要作者温特表示。“只有考虑到尽可能多的可能性，才能确定一种在理论上保证计算准确性的体系结构。”&lt;/p&gt;

&lt;p&gt;为此，研究团队评估了函数逼近问题的解决方案，这是AI算法搜索参数以逼近目标函数输出的方式的数学抽象方法。研究人员将其重新制定为发现一个估计目标函数输出的已知函数序列的问题，以获取更大的系统建模优势。&lt;/p&gt;

&lt;p&gt;研究人员的研究表明，应该选择AI模型的组成部分，以确保它们具有“图灵等效性”。研究人员认为，最好通过自动搜索来识别模型，使用程序来设计特定任务的AI模型架构。这种搜索中的算法会首先生成用于解决问题的其他候选算法，然后将性能最佳的候选者彼此组合并再次进行测试。&lt;/p&gt;

&lt;p&gt;“本文中……可立即应用的结果是鉴定遗传算法，更具体地说，是协同进化算法，其性能指标取决于彼此之间的相互作用，这是寻找最佳（或接近最佳）架构的最实用方法，”论文作者写道。“基于经验，许多研究人员得出的结论是，协同进化算法提供了构建机器学习系统的最佳方法。但是本文中的函数逼近框架有助于为他们的直觉提供更安全的理论基础。”&lt;/p&gt;

&lt;p&gt;亚马逊并不是唯一一个倡导采用进化方法进行AI架构搜索的机构。今年7月，Uber为名为EvoGrad的进化算法开源了开发资源库。去年10月，Google推出了AdaNet，这是一种用于组合机器学习算法以获得更好的预测观点的工具。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1908.09942&quot;&gt;On the Bounds of Function Approximations&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;autogan&quot;&gt;autogan&lt;/h3&gt;

&lt;p&gt;[[https://daiwk.github.io/posts/cv-autogan.html](https://daiwk.github.io/posts/cv-autogan.html](&lt;a href=&quot;https://daiwk.github.io/posts/cv-autogan.html&quot;&gt;https://daiwk.github.io/posts/cv-autogan.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;once-for-all&quot;&gt;once for all&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650769015&amp;amp;idx=5&amp;amp;sn=8e490488bdb2cd6a0aa28fa89083d55f&amp;amp;chksm=871a4209b06dcb1f1ec06b3a74bffad1eade7f69e790516bfa9f7c332fe22bddb83df90d9e41&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=mmBhl6hER5JU9q0KMKTTFnbwPDksdn18kk%2FlW9Ih3p2TCzi4%2BlfisKHhCysHq%2Bou#rd&quot;&gt;韩松等人提出NN设计新思路：训练一次，全平台应用&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1908.09791.pdf&quot;&gt;Once for All: Train One Network and Specialize it for Efficient Deployment&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如要有效地部署深度学习模型，需要专门的神经网络架构，以便最好地适应不同的硬件平台和效率限制条件（定义为部署场景（deployment scenario））。传统的方法要么是人工设计，要么就是使用 AutoML（自动机器学习）来搜索特定的神经网络，再针对每个案例从头开始训练。这些方法的成本高昂，而且难以扩展，因为它们的训练成本与部署场景的数量有关。&lt;/p&gt;

&lt;p&gt;本研究为高效神经网络设计引入了一种 Once for All（OFA/一劳永逸）方法，可处理很多部署场景。这种新方法的特点是分离了模型训练与架构搜索过程。这种方法不会针对每种案例都训练一个专用模型，而是训练一个支持多种不同架构设置（深度、宽度、核大小和分辨率）的 OFA 网络。&lt;/p&gt;

&lt;p&gt;然后给定一个部署场景，再通过选择 OFA 网络中特定的子网络来搜索合适的结果，这个过程无需训练。因此，专用模型的训练成本就从 O(N) 降到了 O(1)。但是，我们却难以防止许多不同子网络之间的干扰。针对这一问题，MIT 的这些研究者提出了渐进式收束算法（progressive shrinking algorithm）。该算法能够训练支持超过 10^19 个子网络的 OFA 网络，同时还能保持与独立训练的网络一样的准确度，从而节省非重复性工程开发（non-recurring engineering/NRE）成本。&lt;/p&gt;

&lt;p&gt;研究者在多种不同的硬件平台（移动平台/CPU/GPU）和效率限制条件上进行了广泛的实验，结果表明：相比于当前最佳（SOTA）的神经架构搜索（NAS）方法，OFA 能稳定地取得同等水平（或更好）的 ImageNet 准确度。值得注意的是，OFA 在处理多部署场景（N）时的速度比 NAS 快几个数量级。当 N=40 时，OFA 所需的 GPU 工作小时数比 ProxylessNAS 少 14 倍、比 FBNet 少 16 倍、比 MnasNet 少 1142 倍。部署场景越多，则相比于 NAS 就节省越多。&lt;/p&gt;

&lt;h2 id=&quot;应用&quot;&gt;应用&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/85810706?utm_source=wechat_session&amp;amp;utm_medium=social&amp;amp;s_r=0&amp;amp;from=timeline&amp;amp;isappinstalled=0#showWechatShareTip&quot;&gt;AutoTiKV：基于机器学习的数据库调优&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>graph representation</title>
   <link href="http://daiwk.github.io/posts/dl-graph-representations.html"/>
   <updated>2019-02-02T00:00:00+00:00</updated>
   <id>/posts/dl-graph-representations</id>
   <content type="html">&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#part1-node-representation-learning&quot;&gt;part1-Node Representation Learning&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#node-representation-methods&quot;&gt;Node Representation Methods&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#line&quot;&gt;LINE&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#%e4%b8%80%e9%98%b6%e7%9b%b8%e4%bc%bc%e5%ba%a6&quot;&gt;一阶相似度&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%e4%ba%8c%e9%98%b6%e7%9b%b8%e4%bc%bc%e5%ba%a6&quot;&gt;二阶相似度&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%e4%bc%98%e5%8c%96trick&quot;&gt;优化trick&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%e8%ae%a8%e8%ae%ba&quot;&gt;讨论&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%e5%ae%9e%e9%aa%8c&quot;&gt;实验&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#deepwalk&quot;&gt;DeepWalk&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#node2vec&quot;&gt;Node2vec&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#graph-and-high-dimensional-data-visualization&quot;&gt;Graph and High-dimensional Data Visualization&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#t-sne&quot;&gt;t-SNE&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#visualizing-large-scale-and-high-dimensional-data&quot;&gt;Visualizing Large-scale and High-dimensional Data&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#learning-the-layout-of-knn-graph&quot;&gt;Learning the Layout of KNN Graph&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#a-probabilistic-model-for-graph-layout&quot;&gt;A Probabilistic Model for Graph Layout&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#knowledge-graph-embedding&quot;&gt;Knowledge Graph Embedding&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#relation-patterns&quot;&gt;relation patterns&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#rotate&quot;&gt;RotatE&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#relation-as-elementwise-rotation-in-complex-space&quot;&gt;Relation as Elementwise Rotation in Complex Space&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#rotee%e7%9a%84%e4%bc%98%e5%8c%96&quot;&gt;RoteE的优化&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#a-high-performance-node-representation-system&quot;&gt;A High-performance Node Representation System&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#part2-graph-neural-networks&quot;&gt;part2-Graph Neural Networks&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86&quot;&gt;基础知识&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#neighborhood-aggregation&quot;&gt;Neighborhood Aggregation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#graph-convolutional-networksgcn&quot;&gt;Graph Convolutional Networks(GCN)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#graphsage&quot;&gt;GraphSAGE&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gated-graph-neural-networks&quot;&gt;Gated Graph Neural Networks&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#gated-graph-neural-networks%e4%bb%8b%e7%bb%8d&quot;&gt;Gated Graph Neural Networks介绍&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#message-passing-neural-networks%e4%bb%8b%e7%bb%8d&quot;&gt;Message-Passing Neural Networks介绍&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#graph-attention-networksgat&quot;&gt;Graph Attention Networks(GAT)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#subgraph-embeddings&quot;&gt;Subgraph Embeddings&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%85%b6%e4%bb%96gnn&quot;&gt;其他GNN&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%9b%b4%e6%b7%b1%e7%9a%84gcn&quot;&gt;更深的GCN&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#part3-deep-generative-models-for-graph-generation&quot;&gt;part3-Deep Generative Models for Graph Generation&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%b7%b1%e5%ba%a6%e7%94%9f%e6%88%90%e6%a8%a1%e5%9e%8b&quot;&gt;深度生成模型&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#variational-autoencoders-vaes&quot;&gt;Variational Autoencoders (VAEs)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#generative-adversarial-networks-gans&quot;&gt;Generative Adversarial Networks (GANs)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#deep-auto-regressive-models&quot;&gt;Deep Auto-regressive Models&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#graphvae&quot;&gt;GraphVAE&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#jtvae&quot;&gt;JTVAE&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#molgan&quot;&gt;MolGAN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gcpn&quot;&gt;GCPN&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%9c%aa%e6%9d%a5%e6%96%b9%e5%90%91&quot;&gt;未来方向&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%87%aa%e5%b7%b1%e7%9a%84%e5%b0%8f%e7%bb%93&quot;&gt;自己的小结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%b6%e4%bb%96&quot;&gt;其他&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#google%e7%9a%84%e4%b8%80%e4%ba%9b%e8%bf%9b%e5%b1%95&quot;&gt;Google的一些进展&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e7%90%86%e8%a7%a3gat&quot;&gt;理解gat&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;入门可以看看&lt;a href=&quot;https://mp.weixin.qq.com/s/edrh-HXqW01Yx7c8tQ8UxA&quot;&gt;从数据结构到算法：图网络方法初探&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;再来另一个KDD2019的图相关的turorial：“Learning From Networks——Algorithms, Theory, &amp;amp; Applications”：&lt;/p&gt;

&lt;p&gt;链接:&lt;a href=&quot;https://pan.baidu.com/s/1uLXThmqVgS3P18i2PPBI9g&quot;&gt;https://pan.baidu.com/s/1uLXThmqVgS3P18i2PPBI9g&lt;/a&gt;  密码:9ls2&lt;/p&gt;

&lt;p&gt;以下主要参考自AAAI2019的tutorial：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652037887&amp;amp;idx=4&amp;amp;sn=730e6bd0cce6c3e40f4aec2de190f99d&amp;amp;chksm=f121960ec6561f1842640aff963fbe609e52fdd695386a442a9726016551c0a3697a196fd5a1&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0202IYksgop7SmowGXrFkpMV&amp;amp;pass_ticket=IHFhi51iDztgrpluZGaofR7zoGSfaB%2F4Y6iACEc6lvxZ3KgGvbkA%2Fhp2MAVH09RS#rd&quot;&gt;AAAI2019《图表示学习》Tutorial, 180 页 PPT 带你从入门到精通（下载）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ppt下载：&lt;a href=&quot;https://pan.baidu.com/s/1hRjm1nbMcj4_ynZ0niE2JA&quot;&gt;https://pan.baidu.com/s/1hRjm1nbMcj4_ynZ0niE2JA&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;传统的机器学习方法依赖于用户定义的启发式模型来提取关于图的结构信息的特征编码 (例如，degree statistics或核函数)。然而，近年来，使用基于深度学习和非线性降维的技术，自动学习将&lt;strong&gt;图结构&lt;/strong&gt;编码为&lt;strong&gt;低维embedding&lt;/strong&gt;的方法激增。&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;graph的几大传统ml任务：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Node classification&lt;/strong&gt;：预测给定的结点的type&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Link prediction&lt;/strong&gt;：预测两个结点是否有边相连&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Community detection&lt;/strong&gt;：发现联系紧密的nodes的clusters&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Network similarity&lt;/strong&gt;：两个（子）网是否相似&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目前的深度学习：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;cnn：固定大小的图片/网格&lt;/li&gt;
  &lt;li&gt;rnn/w2v：文本/序列&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;图更加复杂：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;复杂的拓扑结构（例如，不像网格那样有spatial locality(空间局部性，在最近的将来将用到的信息很可能与现在正在使用的信息在&lt;strong&gt;空间地址上是临近的&lt;/strong&gt;。)）&lt;/li&gt;
  &lt;li&gt;没有固定的结点顺序或者参考点（reference point）(例如，isomorphism（同构）问题)&lt;/li&gt;
  &lt;li&gt;经常是动态的并且有multimodal（多模态）的features&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;part1-node-representation-learning&quot;&gt;part1-Node Representation Learning&lt;/h2&gt;

&lt;h3 id=&quot;node-representation-methods&quot;&gt;Node Representation Methods&lt;/h3&gt;

&lt;p&gt;问题定义：给定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G=(V,E,W)\)&lt;/code&gt;，其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;是结点集合，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E\)&lt;/code&gt;是边的集合，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W\)&lt;/code&gt;是&lt;strong&gt;边&lt;/strong&gt;的权重集合。所谓的node embedding就是对结点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;学习一个向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\in R^d\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;相关工作：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;传统graph embedding算法：MDS, IsoMap, LLE, Laplacian Eigenmap, …。缺点：hard to scale up&lt;/li&gt;
  &lt;li&gt;Graph factorization(Ahmed et al. 2013)：只适用于无向图，并非专门为网络表示而设计&lt;/li&gt;
  &lt;li&gt;Neural word embeddings(Bengio et al. 2003)：Neural language model；word2vec (skipgram), paragraph vectors, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;line&quot;&gt;LINE&lt;/h4&gt;

&lt;p&gt;WWW2015上的&lt;a href=&quot;http://www.www2015.it/documents/proceedings/proceedings/p1067.pdf&quot;&gt;LINE: Large-scale Information Network Embedding&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;LINE代码（c++）：&lt;a href=&quot;https://github.com/tangjianpku/LINE&quot;&gt;https://github.com/tangjianpku/LINE&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;任意类型的网络（有向图、无向图、有/无权重）&lt;/li&gt;
  &lt;li&gt;明确的目标函数（一阶和二阶相似性（first/second proximity））&lt;/li&gt;
  &lt;li&gt;可扩展性
    &lt;ul&gt;
      &lt;li&gt;异步sgd&lt;/li&gt;
      &lt;li&gt;百万级的结点和十亿级别的边：单机数小时&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;一阶相似度&quot;&gt;一阶相似度&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;First-order Proximity（一阶相似度）&lt;/strong&gt;：两个&lt;strong&gt;顶点之间&lt;/strong&gt;的&lt;strong&gt;自身&lt;/strong&gt;相似（不考虑其他顶点）。因为有些结点的link并没有被观测到，所以一阶相似度不足以保存网络结构。&lt;/p&gt;

&lt;p&gt;分布：(定义在&lt;strong&gt;无向&lt;/strong&gt;边&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i-j\)&lt;/code&gt;上)&lt;/p&gt;

&lt;p&gt;一阶相似度的经验分布：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{p}_1(v_i,v_j)=\frac{w_{ij}}{\sum_{(m,n)\in E}w_{mn}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;一阶相似度的模型分布：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p_1(v_i,v_j)=\frac{\exp(\vec{u_i}^T\vec{u_j})}{\sum_{(m,n)\in V\times V}\exp(\vec{u_m}^T\vec{u_n})}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\vec{u_i}\)&lt;/code&gt;是节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;的embedding，其实就是sigmoid：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p_1(v_i,v_j)=\frac{1}{1+\exp(-\vec{u_i}^T\vec{u_j})}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;目标函数是&lt;strong&gt;KL散度&lt;/strong&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
O_1=KL(\hat{p}_1,p_1)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;干掉常量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum_{(m,n)\in E}w_{mn}\)&lt;/code&gt;，还有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum _{(i,j)\in E}w_{ij}\log w_{ij}\)&lt;/code&gt;之后：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
O_1=\sum _{(i,j)\in E}w_{ij}\log w_{ij}-\sum _{(i,j)\in E}w_{ij}\log p_1(v_i,v_j)\approx -\sum _{(i,j)\in E}w_{ij}\log p_1(v_i,v_j)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;只考虑一阶相似度的情况下，改变同一条边的方向对于最终结果没有什么影响。因此一阶相似度只能用于&lt;strong&gt;无向图&lt;/strong&gt;，不能用于有向图。&lt;/p&gt;

&lt;h5 id=&quot;二阶相似度&quot;&gt;二阶相似度&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;Second-order Proximity（二阶相似度）&lt;/strong&gt;：网络中一对顶点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((u,v)\)&lt;/code&gt;之间的二阶相似度是它们&lt;strong&gt;邻近网络结构&lt;/strong&gt;之间的相似性。&lt;/p&gt;

&lt;p&gt;分布：(定义在&lt;strong&gt;有向&lt;/strong&gt;边&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\rightarrow j\)&lt;/code&gt;上)&lt;/p&gt;

&lt;p&gt;邻近网络的经验分布：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{p}_2(v_j|v_i)=\frac{w_{ij}}{\sum_{k\in V}w_{ik}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;邻近网络的模型分布，其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v_i\)&lt;/code&gt;被视为顶点时的表示，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u'_i\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v_i\)&lt;/code&gt;被视为”context”时的表示：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p_2(v_j|v_i)=\frac{\exp(\vec{u'_j}^T\vec{u_i})}{\sum_{k\in V}\exp(\vec{u'_k}^T\vec{u_i})}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;目标函数是&lt;strong&gt;KL散度&lt;/strong&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
O_2=\sum_i KL(\hat{p}_2(\cdot |v_i),p_2(\cdot|v_i))=-\sum _{(i,j)\in E}w_{ij}\log p_2(v_j|v_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;h5 id=&quot;优化trick&quot;&gt;优化trick&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;sgd+negative sampling：随机sample一条边，以及多个negative的边&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如针对二阶的，对每条边&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((i,j)\)&lt;/code&gt;来说，它的目标函数就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\log \sigma(\vec{u'_j}^T\vec{u'_i})+\sum ^K_{i=1}E_{v_n\sim P_n(v)}[\log \sigma (-\vec{u'_n}^T\vec{u_i})]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma(x)=1/(1+\exp(-x))\)&lt;/code&gt;，设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P_n(v)\propto d_v^{3/4}\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_v\)&lt;/code&gt;是节点的出度（即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_i=\sum _{k\in N(i)}w_{ik}\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N(i)\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v_i\)&lt;/code&gt;的为起点的邻居的集合）。&lt;/p&gt;

&lt;p&gt;针对一阶的，把上面式子里的第一项里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\vec{u'_j}^T\)&lt;/code&gt;换成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\vec{u_j}^T\)&lt;/code&gt;就行啦~&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;边&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((i,j)\)&lt;/code&gt;的embedding的梯度：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial O_2}{\partial \vec{u_i}}=w_{ij}\frac{\partial \log \hat{p}_2(v_j|v_i)}{\partial \vec{u_i}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;当边的权重方差很大的时候，从上式可知，目标函数的梯度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_2\)&lt;/code&gt;的梯度再乘以边权重，所以目标函数的梯度的方差也会很大，这样会有问题。&lt;/li&gt;
  &lt;li&gt;解决方法：&lt;strong&gt;edge sampling&lt;/strong&gt;：根据边的权重来采样边，然后把采样到的边当成binary的，也就是把每条边的权重看成一样的！(例如一个边的权重是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;，那么拆成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;条binary的边)&lt;/li&gt;
  &lt;li&gt;复杂度：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(d\times K \times |E|)\)&lt;/code&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;是embedding的维数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;是负样本的个数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(|E|\)&lt;/code&gt;是边的总数&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;讨论&quot;&gt;讨论&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;对只有少量邻居的节点（low degree vertices）进行embed：
    &lt;ul&gt;
      &lt;li&gt;通过增加高阶邻居来扩展邻居&lt;/li&gt;
      &lt;li&gt;BFS(breadth-first search)，使用广度优先搜索策略扩展每个顶点的邻域，即递归地添加邻居的邻居&lt;/li&gt;
      &lt;li&gt;在大部分场景下，只增加二阶邻居就足够了&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;对新节点进行emb（如果新节点和已有节点有边相连，可以如下方式来搞；否则，future work…）:
    &lt;ul&gt;
      &lt;li&gt;保持现有节点的embedding不变&lt;/li&gt;
      &lt;li&gt;根据新节点的embedding求经验分布和模型分布，从而优化目标函数 w.r.t. 新node的embedding&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所以，对于新节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;，直接最小化如下目标函数：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
-\sum_{j\in N(i)}w_{ji}\log p_1(v_j,v_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;或者&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
-\sum _{j\in N(i)}w_{ji}\log p_2(v_j|v_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;h5 id=&quot;实验&quot;&gt;实验&lt;/h5&gt;

&lt;p&gt;LINE(1st)只适用于无向图，LINE(2nd)适用于各种图。&lt;/p&gt;

&lt;p&gt;LINE (1st+2nd)：同时考虑一阶相似度和二阶相似度。将由LINE（1st）和LINE（2nd）学习得到的两个向量表示，&lt;strong&gt;连接成一个更长的向量&lt;/strong&gt;。在连接之后，对维度&lt;strong&gt;重新加权&lt;/strong&gt;以&lt;strong&gt;平衡两个表示&lt;/strong&gt;。因为在无监督的任务中，设定权重很困难，所以&lt;strong&gt;只应用于监督学习&lt;/strong&gt;的场景。&lt;/p&gt;

&lt;p&gt;更适合的方法是共同训练一阶相似度和二阶相似度的目标函数，比较复杂，文章中没有实现。&lt;/p&gt;

&lt;h4 id=&quot;deepwalk&quot;&gt;DeepWalk&lt;/h4&gt;

&lt;p&gt;KDD14上的&lt;a href=&quot;http://www.perozzi.net/publications/14_kdd_deepwalk.pdf&quot;&gt;DeepWalk: Online Learning of Social Representations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;使用学习word representation的方法来学习node representation（例如skip gram）&lt;/p&gt;

&lt;p&gt;将网络上的随机游走视为句子。&lt;/p&gt;

&lt;p&gt;分成两步：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;通过随机游走生成结点的context&lt;/li&gt;
  &lt;li&gt;预测周围的节点:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(v_j|v_i)=\frac{\exp(\vec{u'_i}^T \vec{u_j})}{\sum _{k\in V}\exp(\vec{u'_k}^T\vec{u_i})}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;node2vec&quot;&gt;Node2vec&lt;/h4&gt;

&lt;p&gt;KDD16上的&lt;a href=&quot;https://cs.stanford.edu/people/jure/pubs/node2vec-kdd16.pdf&quot;&gt;node2vec: Scalable Feature Learning for Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;通过如下混合策略去寻找一个node的context：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Breadth-firstSampling(BFS): homophily（同质性）&lt;/li&gt;
  &lt;li&gt;Depth-firstSampling(DFS): structuralequivalence（结构等价）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用带有参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q\)&lt;/code&gt;的&lt;strong&gt;Biased Random Walk&lt;/strong&gt;来进行context的扩展，在BFS和DFS中达到一个平衡，同时考虑到微观局部(BFS)和宏观全局(DFS)的信息，并且具有很高的适应性：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p\)&lt;/code&gt;：Return parameter，控制在walk的过程中，&lt;strong&gt;revisit&lt;/strong&gt;一个节点的概率，对应BFS&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q\)&lt;/code&gt;：In-out parameter，控制探索”&lt;strong&gt;outward&lt;/strong&gt;“节点的概率，对应DFS&lt;/li&gt;
  &lt;li&gt;在有标签的数据上，用&lt;strong&gt;cross validation&lt;/strong&gt;来寻找最优的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/node2vec.png&quot; style=&quot;max-height: 100px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;刚从edge&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((t,v)\)&lt;/code&gt;过来，现在在节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;上，要决定下一步&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((v,x)\)&lt;/code&gt;怎么走：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\alpha _{pq}(t,x)=\left\{\begin{matrix}
\frac{1}{p}&amp;amp;if\ d_{tx}=0\\ 
1 &amp;amp;if\ d_{tx}=1\\ 
\frac{1}{q}&amp;amp;if\ d_{tx}=2
\end{matrix}\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{tx}\)&lt;/code&gt;表示节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;到节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;间的最短路径：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;为0表示回到节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;本身&lt;/li&gt;
  &lt;li&gt;为1表示节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;和节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;直接相连，但上一步却选择了节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;为2表示节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;不直接相连，但节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;和节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;直接相连&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;最简单的给random walk加上bias的方法就是转移概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi _{vx}=w_{vx}\)&lt;/code&gt;，而我们的方法就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi _{vx}=\alpha _{pq}(t,x)w_{vx}\)&lt;/code&gt;，相当于还考虑了跳到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;之前的节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;优化目标和LINE的&lt;strong&gt;一阶相似度&lt;/strong&gt;类似&lt;/p&gt;

&lt;p&gt;LINE、DeepWalk、Node2vec的对比：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/graph-embedding-compare.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;node representation的应用：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Node &lt;strong&gt;classification&lt;/strong&gt; (Perozzi et al. 2014, Tang et al. 2015a, Grover et al. 2015 )&lt;/li&gt;
  &lt;li&gt;Node &lt;strong&gt;visualization&lt;/strong&gt; (Tang et al. 2015a)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Link&lt;/strong&gt; prediction (Grover et al. 2015)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Recommendation&lt;/strong&gt; (Zhao et al. 2016)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Text&lt;/strong&gt; representation (Tang et al. 2015a, Tang et al. 2015b)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;node representation的扩展：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Leverage &lt;strong&gt;global structural information&lt;/strong&gt; (Cao et al. 2015)&lt;/li&gt;
  &lt;li&gt;Non-linear methods based on &lt;strong&gt;autoencoders&lt;/strong&gt; (Wang et al. 2016) • Matrix-factorization based approaches (Qiu et al. 2018)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Directed&lt;/strong&gt; network embedding (Ou et al. 2016)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Signed&lt;/strong&gt; network embedding (Wang et al. 2017)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Multi-view&lt;/strong&gt; networks ( Qu and Tang et al. 2017)&lt;/li&gt;
  &lt;li&gt;Networks with &lt;strong&gt;node attributes&lt;/strong&gt; (Yang et al. 2015)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Heterogeneous(异构)&lt;/strong&gt; networks (Chang et al. 2015)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Task-specific&lt;/strong&gt; network embedding (Chen et al. 2017)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;graph-and-high-dimensional-data-visualization&quot;&gt;Graph and High-dimensional Data Visualization&lt;/h3&gt;

&lt;h4 id=&quot;t-sne&quot;&gt;t-SNE&lt;/h4&gt;

&lt;p&gt;高维数据可视化的一个state-of-the-art的方法，tensorboard就用的这个。&lt;/p&gt;

&lt;p&gt;缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;K-NNG(K-Nearest Neighbor Graph) construction: 复杂度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(NlogN)\)&lt;/code&gt;，假设图中有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;个数据点&lt;/li&gt;
  &lt;li&gt;Graph layout: 复杂度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(NlogN)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;对参数非常敏感（Very sensitive parameters）&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;visualizing-large-scale-and-high-dimensional-data&quot;&gt;Visualizing Large-scale and High-dimensional Data&lt;/h4&gt;

&lt;p&gt;www16的best paper提名&lt;a href=&quot;https://arxiv.org/abs/1602.00370&quot;&gt;Visualizing Large-scale and High-dimensional Data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;largevis代码（c++&amp;amp;python）：&lt;a href=&quot;https://github.com/lferry007/LargeVis&quot;&gt;https://github.com/lferry007/LargeVis&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;K-NNG construction的高效近似：
    &lt;ul&gt;
      &lt;li&gt;比t-SNE的速度快30倍（300w的数据点）&lt;/li&gt;
      &lt;li&gt;更好的time-accuracy tradeoff&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;graph layout的高效的probabilistic model
    &lt;ul&gt;
      &lt;li&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(NlogN)\)&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(N)\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;比t-SNE快7倍（300w的数据点）&lt;/li&gt;
      &lt;li&gt;更好的visualization layouts&lt;/li&gt;
      &lt;li&gt;在不同数据集间有更stable的参数&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;learning-the-layout-of-knn-graph&quot;&gt;Learning the Layout of KNN Graph&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;保持2D/3D空间的节点的相似度
    &lt;ul&gt;
      &lt;li&gt;对每个节点使用一个2D/3D的向量来表示&lt;/li&gt;
      &lt;li&gt;保持相似的数据距离近而不相似的距离远&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;观测节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((i,j)\)&lt;/code&gt;间的一条&lt;strong&gt;binary&lt;/strong&gt;的边的概率：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(e_{ij}=1)=\frac{1}{1+\left \|\vec{y_i}-\vec{y_j}\right \|^2}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;观测节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((i,j)\)&lt;/code&gt;间的一条&lt;strong&gt;有权重&lt;/strong&gt;的边的likelihood：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(e_{ij}=w_{ij})=p(e_{ij}=1)^{w_{ij}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h5 id=&quot;a-probabilistic-model-for-graph-layout&quot;&gt;A Probabilistic Model for Graph Layout&lt;/h5&gt;

&lt;p&gt;目标函数：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
O=\prod _{(i,j)\in E}p(e_{ij}=w_{ij})\prod _{(i,j)\in \bar{E}}(1-p(e_{ij}=w_{ij})^{\gamma }
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;是给&lt;strong&gt;negative edge&lt;/strong&gt;赋值的&lt;strong&gt;unified weight&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;随机sample一些negative edges&lt;/li&gt;
  &lt;li&gt;使用异步sgd来优化&lt;/li&gt;
  &lt;li&gt;时间复杂度：与数据点数是&lt;strong&gt;线性&lt;/strong&gt;关系&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;knowledge-graph-embedding&quot;&gt;Knowledge Graph Embedding&lt;/h3&gt;

&lt;p&gt;知识图谱是异构图，有多种类型的relations&lt;/p&gt;

&lt;p&gt;用(head entity, relation, tail entity)的三元组来表示facts的集合。&lt;/p&gt;

&lt;p&gt;related works：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;将entities用embeddings来表示&lt;/li&gt;
  &lt;li&gt;将relations用embeddings或者matrices来表示&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/knowledge-graph-embedding.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;kg的核心任务：预测missing links&lt;/p&gt;

&lt;p&gt;kg的核心idea：根据观测到的knowledge facts，对kg中的relation patterns进行建模和infer。也就是学习&lt;strong&gt;relations的relations&lt;/strong&gt;。&lt;/p&gt;

&lt;h4 id=&quot;relation-patterns&quot;&gt;relation patterns&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;对称和反对称：
    &lt;ul&gt;
      &lt;li&gt;对称(Symmetric)：例如，marriage&lt;/li&gt;
      &lt;li&gt;反对称(Antisymmetric)：例如，Filiation(父子关系)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;形式化定义：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
r\ is\ Symmetric &amp;amp; r(x,y)\Rightarrow r(y,x)\ if\ \forall x,y\\ 
r\ is\ Antisymmetric &amp;amp; r(x,y)\Rightarrow \neg r(y,x)\ if\ \forall x,y\\ 
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Inverse relations:
    &lt;ul&gt;
      &lt;li&gt;Hypernym(上位词) and hyponym(下位词)：花是鲜花的上位词，鲜花是花的下位词&lt;/li&gt;
      &lt;li&gt;丈夫和妻子&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;形式化定义：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
r_1\ is\ inverse\ to\ relation\ r_2:\ r_2(x,y)\Rightarrow r_1(y,x)\ if\ \forall x,y
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Composition Relations
    &lt;ul&gt;
      &lt;li&gt;My mother’s husband is my father&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;形式化定义：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
r_1\ is\ a\ composition\ of\ relation\ r_2\ and\ relation\ r_3: &amp;amp; \ r_2(x,y)\wedge r_3(y,z) \Rightarrow r_1(x,z)\ if\ \forall x,y,z
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;目前的方法没有一种能同时infer上面这所有3种relation patterns，只有RotatE可以！！&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/infer-relation-patterns.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h4 id=&quot;rotate&quot;&gt;RotatE&lt;/h4&gt;

&lt;p&gt;ICLR19 &lt;a href=&quot;https://openreview.net/pdf?id=HkgEQnRqYQ&quot;&gt;RotatE: Knowledge Graph Embedding by Relational Rotation in Complex Space.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RotatE代码（pytorch）:&lt;a href=&quot;https://github.com/DeepGraphLearning/KnowledgeGraphEmbedding&quot;&gt;https://github.com/DeepGraphLearning/KnowledgeGraphEmbedding&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;每一个relation可以看成是从source entity到target entity在complex(复数)向量空间上的elementwise rotation&lt;/p&gt;

&lt;p&gt;RotatE可以同时建模和infer上面这所有3种relation patterns&lt;/p&gt;

&lt;p&gt;优化RotatE可以用高效的negative sampling&lt;/p&gt;

&lt;p&gt;在kg的link prediction的benchmarks中能达到state-of-the-art的效果&lt;/p&gt;

&lt;h5 id=&quot;relation-as-elementwise-rotation-in-complex-space&quot;&gt;Relation as Elementwise Rotation in Complex Space&lt;/h5&gt;

&lt;p&gt;head entity：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\in \mathbb{C}^k\)&lt;/code&gt;；tail entity：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\in \mathbb{C}^k\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;relation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;：是一个从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;的elementwise rotation：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
t=h\circ r,\ where\ |r_i|=1
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\circ\)&lt;/code&gt;是element-wise product，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_i=h_ir_i\)&lt;/code&gt;，其中&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
r_i=e^{\mathbf{i}\theta _{r,i}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;里面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta _{r,i}\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;的第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;维的phase angle，e的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{i}\theta_{r,i}\)&lt;/code&gt;的第一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{i}\)&lt;/code&gt;是虚数单位，第二个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;是第i维。&lt;/p&gt;

&lt;p&gt;定义distance function：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
d_r(h,t)=\left \| h\circ r-t \right \|
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如左图，transE建模的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h+r\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;的距离，也就是在&lt;strong&gt;实数直线&lt;/strong&gt;上以&lt;strong&gt;translation&lt;/strong&gt;的方式建模&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;如右图，RotatE建模的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\circ r\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;的距离，也就是在&lt;strong&gt;复平面&lt;/strong&gt;上以&lt;strong&gt;rotation&lt;/strong&gt;的方式建模&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/illustration-rotate-1.png&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;先科普一下，在复变函数中，自变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z\)&lt;/code&gt;可以写成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z=r\times (\cos \theta + \mathbf{i}\sin \theta)\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z\)&lt;/code&gt;的模，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r=|z|\)&lt;/code&gt;；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z\)&lt;/code&gt;的辐角，记作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Arg(z)\)&lt;/code&gt;。在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(-\pi\)&lt;/code&gt;到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;间的辐角称为辐角主值，记作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(arg(z)\)&lt;/code&gt;。指数形式&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z=r(\cos \theta + i\sin \theta)=re^{\mathbf{i}\theta}\)&lt;/code&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;relation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;是对称的，当且仅当，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_i=\pm 1\)&lt;/code&gt;，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{r,i}=0\ or\ \pi\)&lt;/code&gt;，例如下图，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_i=-1\)&lt;/code&gt;也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta _{r,i}=\pi\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/illustration-rotate-2.png&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;relation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;是反对称的，当且仅当，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\circ r\neq 1\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;relation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_1\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_2\)&lt;/code&gt;是inverse，当且仅当，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_2=r^{-1}_1\)&lt;/code&gt;，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta _{2,i}=-\theta _{1,i}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;relation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_3=e^{\mathbf{i}\theta_3}\)&lt;/code&gt;是两个relation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_1=e^{\mathbf{i}\theta_1}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_2=e^{\mathbf{i}\theta_2}\)&lt;/code&gt;的composition，当且仅当，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_3=r_1\circ r_2\)&lt;/code&gt;，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta _3=\theta _1 + \theta _2\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;rotee的优化&quot;&gt;RoteE的优化&lt;/h5&gt;

&lt;p&gt;Negative sampling loss如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L=-\log\sigma (\gamma -d_r(h,t))-\sum ^k_{i=1}\frac{1}{k}\log \sigma (d_r(h'_i,t'_i)-\gamma)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;是一个fixed margin，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma\)&lt;/code&gt;是sigmoid，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((h'_i,r,t'_i)\)&lt;/code&gt;是第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;个negative三元组。&lt;/p&gt;

&lt;p&gt;然后我们要变成self-adversarial negative sampling：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;传统地，负样本通过uniform的方式（均匀分布，即等概率）来采样
    &lt;ul&gt;
      &lt;li&gt;随着训练的继续，因为很多样本是obviously false了，所以这种采样是inefficient的&lt;/li&gt;
      &lt;li&gt;没有提供有用的信息&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;self-adversarial negative sampling：
    &lt;ul&gt;
      &lt;li&gt;根据当前的embedding model来进行negative三元组的采样&lt;/li&gt;
      &lt;li&gt;从更简单的samples开始，逐步变难&lt;/li&gt;
      &lt;li&gt;Curriculum Learning（递进学习，课程学习，可以参考&lt;a href=&quot;https://blog.csdn.net/qq_25011449/article/details/82914803&quot;&gt;https://blog.csdn.net/qq_25011449/article/details/82914803&lt;/a&gt;），从如下分布中进行采样：&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(h'_j,r,t'_j|\{(h_i,r_i,t_i)\})=\frac{\exp \alpha f_r(h'_j,t'_j)}{\sum _i \exp \alpha f_r(h'_i,t'_i)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha\)&lt;/code&gt;是sampling的temperature，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_r(h'_j,t'_j)\)&lt;/code&gt;衡量三元组的salience(突出程度)&lt;/p&gt;

&lt;p&gt;但在实际应用中，从上面这个分布去sample的代价是很大的，所以我们把这个概率直接作为负样本的权重，所以最终的loss如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L=-\log\sigma (\gamma -d_r(h,t))-\sum ^k_{i=1}p(h'_i,r,t'_i)\log \sigma (d_r(h'_i,t'_i)-\gamma)
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;a-high-performance-node-representation-system&quot;&gt;A High-performance Node Representation System&lt;/h3&gt;

&lt;p&gt;A High-Performance CPU-GPU Hybrid System for Node Embedding，投稿www19&lt;/p&gt;

&lt;p&gt;algorithm and system co-design的一个node embeddings的系统&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;CPUs: online random walk generation&lt;/li&gt;
  &lt;li&gt;GPUs: training node embeddings&lt;/li&gt;
  &lt;li&gt;Efficient and effective collaboration strategies between CPUs and GPUs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;比现有的系统快50倍，一个有100w节点的网络只要1min&lt;/p&gt;

&lt;h2 id=&quot;part2-graph-neural-networks&quot;&gt;part2-Graph Neural Networks&lt;/h2&gt;

&lt;h3 id=&quot;基础知识&quot;&gt;基础知识&lt;/h3&gt;

&lt;p&gt;通过一个encoder函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(ENC\)&lt;/code&gt;，把原始网络的结点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u\)&lt;/code&gt;和结点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;映射到embedding space的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;维向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_u\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_v\)&lt;/code&gt;，然后希望原空间的相似度和embedding space的相似度（例如内积）接近：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
similarity(u,v)\approx z_v^Tz_u
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;之前的encoder是shallow的，也就是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Z\)&lt;/code&gt;矩阵，使用embedding lookup，矩阵大小是node_num * emb_dim。缺点如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;需要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(|V|)\)&lt;/code&gt;的参数&lt;/strong&gt;：每个node有自己的unique的embedding vector，&lt;strong&gt;没有参数共享&lt;/strong&gt;！！&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Inherently “transductive”&lt;/strong&gt;：固有的『直推式』。也就是说，对于&lt;strong&gt;训练中没有见过的结点&lt;/strong&gt;，不可能生成一个embedding&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;没有包含节点feature&lt;/strong&gt;：很多图有一些我们必须要考虑和利用好的feature。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;因此需要使用deeper的encoder，而这些更复杂的encoder也自带了similarity函数。&lt;/p&gt;

&lt;p&gt;参考2017年的综述&lt;a href=&quot;https://arxiv.org/abs/1709.05584&quot;&gt;Representation Learning on Graphs: Methods and Applications&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;还有2005年的&lt;a href=&quot;https://ieeexplore.ieee.org/document/4700287/&quot;&gt;The Graph Neural Network Model&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;定义：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;：图&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;：节点集合&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;：邻接矩阵(假设是binary的)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\in R ^{m\times |V|}\)&lt;/code&gt;：&lt;strong&gt;节点features&lt;/strong&gt;的矩阵
    &lt;ul&gt;
      &lt;li&gt;类别型的特征、文本、图像数据等&lt;/li&gt;
      &lt;li&gt;节点度数、clustering coefficients(聚集系数，参考&lt;a href=&quot;https://blog.csdn.net/pennyliang/article/details/6838956&quot;&gt;https://blog.csdn.net/pennyliang/article/details/6838956&lt;/a&gt;)等&lt;/li&gt;
      &lt;li&gt;Indicator vectors(例如，每个节点的one-hot vector)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;neighborhood-aggregation&quot;&gt;Neighborhood Aggregation&lt;/h3&gt;

&lt;p&gt;核心思想：使用nn对节点的邻居的信息进行汇聚，生成这个节点的embedding&lt;/p&gt;

&lt;p&gt;如下图：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;node在每一层都有embedding&lt;/li&gt;
  &lt;li&gt;模型的depth可以任意&lt;/li&gt;
  &lt;li&gt;节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u\)&lt;/code&gt;在第0层的embedding是它的input-feature &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_u\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/neighborhood-aggregation.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;neighborhood aggregateion其实数学上和spectral graph convolutions(参考&lt;a href=&quot;https://arxiv.org/abs/1611.08097&quot;&gt;Geometric deep learning: going beyond Euclidean data&lt;/a&gt;)很像，可以看成是一种center-surround filter。&lt;/p&gt;

&lt;p&gt;关键在于上图的layer1和layer2用什么样的网络结构，一种basic的方法就是，layer2先average，然后再接一个神经网络：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
h^0_v &amp;amp;=x_v \\ 
h^k_v &amp;amp;=\sigma (W_k\sum _{u\in N(v)}\frac{h^{k-1}_u}{|N(v)|}+B_kh^{k-1}_v) ,\forall k&amp;gt;0\\ 
z_v&amp;amp;=h^K_v\\
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h^0_v\)&lt;/code&gt;：第0层的embedding就是node的特征&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h^k_v\)&lt;/code&gt;：第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;层的embedding，包括的两项分别是邻居节点的前一层的emb的平均，还有当前节点的前一层的emb&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma\)&lt;/code&gt;：非线性，可以是relu/tanh等&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_k\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(B_k\)&lt;/code&gt;是两个待训练的矩阵&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_v\)&lt;/code&gt;：最终的输出结果，也就是第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;层的输出&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;训练可以使用无监督的方法，loss可以是前面讲到的任意的node embedding的方法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Random walks (node2vec, DeepWalk)&lt;/li&gt;
  &lt;li&gt;Graph factorization&lt;/li&gt;
  &lt;li&gt;或者直接训练保证相似的node有相似的embedding&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;也可以直接用监督学习的方法来训（例如是一个node的分类问题），其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;是classification weights：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L=\sum _{v\in V}y_v\log (\sigma (z^T_v\theta )+(1-y_v)\log(1-\sigma (z^T_v\theta)))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;归纳能力（inductive capability）：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;所有节点共享相同的aggregation parameters&lt;/li&gt;
  &lt;li&gt;模型参数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(|V|\)&lt;/code&gt;的sublinear，而且可以对没见过的node生成embed&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;graph-convolutional-networksgcn&quot;&gt;Graph Convolutional Networks(GCN)&lt;/h3&gt;

&lt;p&gt;参考ICLR17的&lt;a href=&quot;https://arxiv.org/abs/1609.02907&quot;&gt;Semi-Supervised Classification with Graph Convolutional Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在neighborhood aggregation上有一些小改动：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
h^k_v=\sigma(W_k\sum_{u\in N(v)\cup v}\frac{h^{k-1}_u}{\sqrt{|N(u)||N(v)|}})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;和普通gnn的区别：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;self和neighbor的embedding共用同一个权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_k\)&lt;/code&gt;，而普通的gnn是两个权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(B_k\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_k\)&lt;/code&gt;，好处就是有&lt;strong&gt;更多的参数共享&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;每一个neighbor都有normalization(即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sqrt{|N(u)||N(v)|}\)&lt;/code&gt;)，好处就是可以&lt;strong&gt;减小度数多的邻居的权重&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;graphsage&quot;&gt;GraphSAGE&lt;/h3&gt;

&lt;p&gt;参考NIPS17的&lt;a href=&quot;https://arxiv.org/abs/1706.02216&quot;&gt;Inductive Representation Learning on Large Graphs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;出发点：把上面在aggregate之后使用的神经网络换成任意一个可以把一堆vectors映射成一个单独的vector的可微函数（也就是下面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(AGG(\{h^{k-1}_u,\forall u\in N(v)\})\)&lt;/code&gt;）：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
h^k_v=\sigma ([A_k\cdot AGG(\{h^{k-1}_u,\forall u\in N(v)\}),B_kh^{k-1}_v])
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\([A_k\cdot AGG(\{h^{k-1}_u,\forall u\in N(v)\}),B_kh^{k-1}_v]\)&lt;/code&gt;是把这self embedding和neighbor embedding这两个向量&lt;strong&gt;concate&lt;/strong&gt;到一起。&lt;/p&gt;

&lt;p&gt;AGG的变种：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;mean：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
AGG=\sum _{u\in N(v)}\frac{h^{k-1}_u}{|N(v)|}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;pool：对neighbor vectors进行转换（例如下面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;），并进行symmetric vector函数变换（例如下面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;就是element-wise mean/max）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
AGG=\gamma (\{Qh^{k-1}_u,\forall u \in N(v)\})
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;lstm：对neighbor的一个&lt;strong&gt;随机排列&lt;/strong&gt;（random permutation）使用lstm&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
AGG=LSTM([h^{k-1}_u, \forall u\in \pi(N(v))])
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;gated-graph-neural-networks&quot;&gt;Gated Graph Neural Networks&lt;/h3&gt;

&lt;p&gt;参考ICLR16的&lt;a href=&quot;https://arxiv.org/abs/1511.05493&quot;&gt;Gated Graph Sequence Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考ICML17的&lt;a href=&quot;https://arxiv.org/pdf/1704.01212.pdf&quot;&gt;Neural Message Passing for Quantum Chemistry&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GCNs和GraphSAGE大部分情况下只有&lt;strong&gt;2-3层&lt;/strong&gt;深，层数加深有如下挑战：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;参数太多导致过拟合&lt;/li&gt;
  &lt;li&gt;bp的过程中出现梯度消失/爆炸&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;思路：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;层间参数共享&lt;/li&gt;
  &lt;li&gt;Recurrent state update：各层的神经网络使用RNN。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;gated-graph-neural-networks介绍&quot;&gt;Gated Graph Neural Networks介绍&lt;/h4&gt;

&lt;p&gt;Recurrent state update这种方法分成两步：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;在step &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;从neighbors获取”message”，&lt;strong&gt;这个聚合函数与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;无关&lt;/strong&gt;：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
m^k_v=W\sum _{u\in N(v)}h^{k-1}_u
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;通过gru来更新节点的”state”。新节点的state依赖old state以及neighbors的”message”：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
h^k_v=GRU(h^{k-1}_v,m^k_v)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;优点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;可以处理&lt;strong&gt;20+的层数&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;绝大部分真实世界的网络有比较小的diameters（直径，放大倍率），&lt;strong&gt;大部分小于等于7&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;能够将&lt;strong&gt;global的图结构&lt;/strong&gt;的复杂信息传播给所有结点&lt;/li&gt;
  &lt;li&gt;对复杂网络的表示很有用（例如Logical formulas，或者程序）&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;message-passing-neural-networks介绍&quot;&gt;Message-Passing Neural Networks介绍&lt;/h4&gt;

&lt;p&gt;从以下两个方面来对gated graph neural networks进行泛化：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;在step &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;从neighbors获取”message”：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;可以是一个一般(generic)的”message”函数，例如sum或者MLP。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{u,v}\)&lt;/code&gt;把&lt;strong&gt;边的信息&lt;/strong&gt;考虑进来了！&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
m^k_v=\sum _{u\in N(v)}M(h^{k-1}_u,h^{k-1}_v,e_{u,v})
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;更新node的”state”：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(U\)&lt;/code&gt;可以是一个一般(generic)的”update”函数，例如LSTM或者GRU&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
h^k_v=U(h^{k-1}_v,m^k_v)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，其实这是一个通用的conceptual（概念性的） framework，可以归纳大部分GNNs。&lt;/p&gt;

&lt;h3 id=&quot;graph-attention-networksgat&quot;&gt;Graph Attention Networks(GAT)&lt;/h3&gt;

&lt;p&gt;参考ICLR18的&lt;a href=&quot;https://arxiv.org/pdf/1710.10903.pdf&quot;&gt;Graph Attention Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;key idea：某些neighbor更重要，所以可以使用attention机制来搞&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
h^k_v=\sigma (\sum _{u\in N(v)\cup \{v\}}\alpha _{v,u}W^kh^{k-1}_u)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma\)&lt;/code&gt;是非线性；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum _{u\in N(v)\cup \{v\}}\)&lt;/code&gt;意味着把所有neighbor(包括节点自己！！)都加起来&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha _{v,u}\)&lt;/code&gt;是学习到的attention权重&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;各种attention都是可以的，原始GAT用的是如下attention权重：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\alpha _{v,u}=\frac{\exp(LeakyReLU(a^T[Qh_v,Qh_u]))}{\sum _{u'\in N(v)\cup \{v\}}\exp(LeakyReLU(a^T[Qh_v,Qh_{u'}]))}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;对照上面讲到的通用的conceptual（概念性的） framework，其实就是&lt;strong&gt;把attention加到获取”message”那步里去&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;其他新的东西：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Generalizations based on spectral convolutions:
    &lt;ul&gt;
      &lt;li&gt;Geometric Deep Learning (Bronstein et al., 2017, &lt;a href=&quot;https://arxiv.org/abs/1611.08097&quot;&gt;Geometric deep learning: going beyond Euclidean data&lt;/a&gt;)&lt;/li&gt;
      &lt;li&gt;Mixture Model CNNs (Monti et al., 2017, &lt;a href=&quot;https://arxiv.org/pdf/1611.08402.pdf&quot;&gt;Geometric deep learning on graphs and manifolds using mixture model CNNs&lt;/a&gt;)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Speed improvements via subsampling:
    &lt;ul&gt;
      &lt;li&gt;FastGCNs (Chen et al., 2018, &lt;a href=&quot;https://arxiv.org/abs/1801.10247&quot;&gt;FastGCN: Fast Learning with Graph Convolutional Networks via Importance Sampling&lt;/a&gt;)&lt;/li&gt;
      &lt;li&gt;Stochastic GCNs (Chen et al., 2017, &lt;a href=&quot;https://arxiv.org/abs/1710.10568&quot;&gt;Stochastic Training of Graph Convolutional Networks with Variance Reduction&lt;/a&gt;)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;还可以参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650757332&amp;amp;idx=3&amp;amp;sn=4a66ac808542af2617be5621a4a56e5d&amp;amp;chksm=871a9caab06d15bc29f72e3d2c78355cd226d65907486a4c4acbc3f6c60713192908b70eadb4&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=4hmIYvO6GJcf2XjDjrkA6v22Y3ZUCDsA30spOD3nAyih4OfDpXcZPiTcotPvF%2FnT#rd&quot;&gt;专栏 | 深入理解图注意力机制&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;subgraph-embeddings&quot;&gt;Subgraph Embeddings&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;方法一：直接把子图中的node的emb进行sum或者avg&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
z_S=\sum _{v\in S}z_v
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;见2016年的&lt;a href=&quot;https://arxiv.org/abs/1509.09292&quot;&gt;Convolutional Networks on Graphs for Learning Molecular Fingerprints&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;方法二：引入”virtual node”来表示子图，并走一个完整的gnn&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如下图&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/subgraph-embedding-approach2.png&quot; style=&quot;max-height: 100px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;见2016年的&lt;a href=&quot;https://arxiv.org/abs/1511.05493&quot;&gt;Gated Graph Sequence Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;方法三：对节点进行层次聚类&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;见2018年的&lt;a href=&quot;https://arxiv.org/pdf/1806.08804.pdf&quot;&gt;Hierarchical Graph Representation Learning with Differentiable Pooling&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/subgraph-embedding-approach3.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;大致流程如下：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;在图上跑GNN，得到node的embeddings&lt;/li&gt;
  &lt;li&gt;对node embeddings进行聚类，得到一个“coarsened” graph（粗糙的）&lt;/li&gt;
  &lt;li&gt;在“coarsened” graph上跑GNN&lt;/li&gt;
  &lt;li&gt;重复&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;学习clustering的不同方式：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;使用softmax weight的soft clustering（2018年的&lt;a href=&quot;https://arxiv.org/pdf/1806.08804.pdf&quot;&gt;Hierarchical Graph Representation Learning with Differentiable Pooling&lt;/a&gt;）&lt;/li&gt;
  &lt;li&gt;使用hard clustering（2018年的&lt;a href=&quot;https://arxiv.org/pdf/1811.01287.pdf&quot;&gt;Towards Sparse Hierarchical Graph Classifiers&lt;/a&gt;和2018年的&lt;a href=&quot;https://openreview.net/pdf?id=HJePRoAct7&quot;&gt;GRAPH U-NET&lt;/a&gt;）&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;其他gnn&quot;&gt;其他GNN&lt;/h3&gt;

&lt;h4 id=&quot;更深的gcn&quot;&gt;更深的GCN&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650767026&amp;amp;idx=3&amp;amp;sn=9bcb11b0355f366a5f8cbdf886061223&amp;amp;chksm=871abaccb06d33da62147dac4bf87fb68c9deadd7f46b8e91aa141b3b4b24b7a2899c9b92e63&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=zAXdHORK5tTx549e9RwAgNcm7bjJrH4ENwbbTYVrAZDqpsE%2Fu1hY63b%2FoRfnZQdM#rd&quot;&gt;​ICCV 2019 Oral论文：KAUST提出大幅加深图卷积网络的新方法&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1904.03751.pdf&quot;&gt;Can GCNs Go as Deep as CNNs?&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;part3-deep-generative-models-for-graph-generation&quot;&gt;part3-Deep Generative Models for Graph Generation&lt;/h2&gt;

&lt;h3 id=&quot;深度生成模型&quot;&gt;深度生成模型&lt;/h3&gt;

&lt;p&gt;深度生成模型的目标：为数据分布&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(x)\)&lt;/code&gt;隐式或者显式地建模，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;是一个高维随机变量&lt;/p&gt;

&lt;h4 id=&quot;variational-autoencoders-vaes&quot;&gt;Variational Autoencoders (VAEs)&lt;/h4&gt;

&lt;p&gt;原始论文：2014年Kingma et al.的&lt;a href=&quot;https://arxiv.org/pdf/1312.6114.pdf&quot;&gt;Auto-Encoding Variational Bayes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Latent variable model:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一个encoder &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q_{\phi}(z|x)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;一个decoder &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q_{\theta}(x|z)\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;最大化log likelihood &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\log p(x)\)&lt;/code&gt;：inference是intractable（棘手）的，因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z\)&lt;/code&gt;是连续的&lt;/p&gt;

&lt;p&gt;最大化variational的下界&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L(\phi, \theta;x)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;通过reparametrization trick来jointly优化encoder和decoder：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(\phi, \theta;x)=E_{q_{\phi}(z|x)}\log p_{\theta }(x|z)-KL[q_{\phi}(z|x)||p(z)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E_{q_{\phi}(z|x)}\log p_{\theta }(x|z)\)&lt;/code&gt;是reconstruction，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(KL[q_{\phi}(z|x)||p(z)]\)&lt;/code&gt;是regularization&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/vaes.png&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;小结一下，encoder是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q_{\phi}\)&lt;/code&gt;，decoder是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\theta}\)&lt;/code&gt;，encoder根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;生成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z\)&lt;/code&gt;，decoder根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z\)&lt;/code&gt;生成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;可以参考&lt;a href=&quot;https://blog.csdn.net/antkillerfarm/article/details/80648805&quot;&gt;https://blog.csdn.net/antkillerfarm/article/details/80648805&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;重构的过程是希望没噪声的，而KL loss则希望有高斯噪声的，两者是对立的。所以，VAE跟GAN一样，内部其实是包含了一个对抗的过程，只不过它们两者是混合起来，共同进化的。&lt;/p&gt;

&lt;p&gt;公式推导可以看&lt;a href=&quot;https://blog.csdn.net/weixin_40955254/article/details/82315909&quot;&gt;https://blog.csdn.net/weixin_40955254/article/details/82315909&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;generative-adversarial-networks-gans&quot;&gt;Generative Adversarial Networks (GANs)&lt;/h4&gt;

&lt;p&gt;原始论文：2014年Goodfellow et al.的&lt;a href=&quot;https://arxiv.org/abs/1406.2661&quot;&gt;Generative Adversarial Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;一个两个玩家的Minimax游戏：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Generator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G: z\rightarrow x\)&lt;/code&gt;。目标是迷惑discriminator&lt;/li&gt;
  &lt;li&gt;Discriminator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D: x\rightarrow \{0,1\}\)&lt;/code&gt;。目标是区分真实数据和生成的数据&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\underset{G}{\min}\underset{D}{\max}V(D,G)=E_{x\sim p_{data}(x)}[\log D(x)]+E_{z\sim p_z(z)}[\log (1-D(G(z)))]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;直观地理解，这个式子包括两部分，一部分是判别真实数据是正例的概率，另一部分是判别生成的数据是负例的的概率，对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;来讲，期望这个式子min，而对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;来讲，期望这个式子max&lt;/p&gt;

&lt;h4 id=&quot;deep-auto-regressive-models&quot;&gt;Deep Auto-regressive Models&lt;/h4&gt;

&lt;p&gt;深度自回归模型：例如RNN&lt;/p&gt;

&lt;p&gt;例如，PixelRNN（2016年Oort et al.的&lt;a href=&quot;https://arxiv.org/abs/1601.06759&quot;&gt;Pixel Recurrent Neural Networks&lt;/a&gt;）和PixelCNN（2016年也是Oort et al.的&lt;a href=&quot;https://arxiv.org/pdf/1606.05328v2.pdf&quot;&gt;Conditional Image Generation with PixelCNN Decoders&lt;/a&gt;）：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一个pixel一个pixel地生成图像&lt;/li&gt;
  &lt;li&gt;通过一个神经网络来对条件概率分布建模&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;WaveNet（2017年Oort et al.的&lt;a href=&quot;https://arxiv.org/abs/1609.03499&quot;&gt;WaveNet: A Generative Model for Raw Audio&lt;/a&gt;）&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(x)=\prod ^{n^2}_{i=1}p(x_i|x_1,...,x_{i-1})
 \]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/wavenet.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;但如果要用在图上，有以下几个挑战：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;图的structures和size是不一样的&lt;/li&gt;
  &lt;li&gt;node之间并没有顺序&lt;/li&gt;
  &lt;li&gt;离散&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;graphvae&quot;&gt;GraphVAE&lt;/h3&gt;

&lt;p&gt;2018年Simonovsky和Komodakis的&lt;a href=&quot;https://arxiv.org/abs/1802.03480&quot;&gt;GraphVAE: Towards Generation of Small Graphs Using Variational Autoencoders&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;提出了生成图的VAE的框架：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输入graph&lt;/li&gt;
  &lt;li&gt;encoder：gnn+gated pooling=&amp;gt;graph representation，参考Li et al.在2015的&lt;/li&gt;
  &lt;li&gt;decoder：输出一个预先定义好max size的probalistic fully-connected graph
    &lt;ul&gt;
      &lt;li&gt;对节点、边、节点和边的属性的存在性单独建模&lt;/li&gt;
      &lt;li&gt;graph matching是必须的&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/graphvae.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;输入的graph是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G=(A,E,F)\)&lt;/code&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;是邻接矩阵；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E\)&lt;/code&gt;是边的属性的tensor；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(F\)&lt;/code&gt;是节点的属性的矩阵&lt;/p&gt;

&lt;p&gt;decoder的输出：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;限制domain在最多max k个节点的所有graphs的集合中（k一般是10左右）&lt;/li&gt;
  &lt;li&gt;一次输出一个k个节点的probalistic fully-connected graph &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{G}=(\tilde{A},\tilde{E},\tilde{F})\)&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;以&lt;strong&gt;bernoulli variables&lt;/strong&gt;建模nodes和edges的&lt;strong&gt;existence&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;以&lt;strong&gt;multinomial variables&lt;/strong&gt;建模nodes和edges的&lt;strong&gt;attributes&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{A}\in [0,1]^{k\times k}\)&lt;/code&gt;：同时包括node probabilities &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{A}_{aa}\)&lt;/code&gt;和edge probabilities &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{A}_{ab}\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\neq b\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{E}\in [0,1]^{k\times k\times d_e}\)&lt;/code&gt;：表示edge attributes的probabilities&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{F}\in [0,1]^{k\times d_e}\)&lt;/code&gt;：表示node attributes的probabilities&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;inference：在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{A}\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{E}\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{F}\)&lt;/code&gt;中使用edge-wise和node-wise的argmax&lt;/li&gt;
  &lt;li&gt;计算reconstruction loss的时候，需要使用graph matching&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;graph的max size必须是预先定义好的&lt;/li&gt;
  &lt;li&gt;graph matching是必须的&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;jtvae&quot;&gt;JTVAE&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1802.04364.pdf&quot;&gt;Junction Tree Variational Autoencoder for Molecular Graph Generation&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;利用了化学领域的知识
    &lt;ul&gt;
      &lt;li&gt;每个molecule(分子)可以表示为化学substructures(如环、键(bond))的树状的scaffold(骨架、支架)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;生成一个树状结构的object
    &lt;ul&gt;
      &lt;li&gt;用来表示subgraph components的scaffold&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;将substructure组装成一个coherent(连贯的) molecular graph&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;molgan&quot;&gt;MolGAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1805.11973.pdf&quot;&gt;MolGAN: An implicit generative model for small molecular graphs&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一个implicit, likelihood-free的生成模型：用于分子生成&lt;/li&gt;
  &lt;li&gt;结合了强化学习来encourage生成的带有化学属性的分子&lt;/li&gt;
  &lt;li&gt;Generator：从先验分布中生成分子&lt;/li&gt;
  &lt;li&gt;Discriminator：区分生成的sample和真实的sample&lt;/li&gt;
  &lt;li&gt;Reward network：
    &lt;ul&gt;
      &lt;li&gt;学习给每个分子赋值一个reward，这个reward要和external software提供的score进行match&lt;/li&gt;
      &lt;li&gt;invalid的分子通常得到的reward是0&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;整体架构图如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/molgan.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;Generator：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;生成一个probabilistic fully-connected graph：
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\in R^{N\times T}\)&lt;/code&gt;：atom types&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A\in R^{N\times N\times Y}\)&lt;/code&gt;：bond types&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;目标函数：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(\theta)=\lambda L_{WGAN}+(1-\lambda)L_RL
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Discriminator &amp;amp; Reward network：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;通过neural message passing algorithm的一个变种-Relational-GCN，Schlichtkrull et al. 2017的&lt;a href=&quot;https://arxiv.org/abs/1703.06103&quot;&gt;Modeling relational data with graph convolutional networks&lt;/a&gt;来学习分子/graph的表示&lt;/li&gt;
  &lt;li&gt;discriminator和reward network用相同的网络结构（但参数不共享）&lt;/li&gt;
  &lt;li&gt;reward network用来近似external software的打分(使用真实的samples和生成的samples进行训练)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;优缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;不需要graph matching&lt;/li&gt;
  &lt;li&gt;graphs/分子的max size仍然需要预先定义&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;gcpn&quot;&gt;GCPN&lt;/h3&gt;

&lt;p&gt;You et al.在2018的&lt;a href=&quot;https://arxiv.org/pdf/1806.02473.pdf&quot;&gt;Graph Convolutional Policy Network for Goal-Directed Molecular Graph Generation&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;将分子的生成看成序列决策问题
    &lt;ul&gt;
      &lt;li&gt;增加节点和边&lt;/li&gt;
      &lt;li&gt;一个马尔可夫决策过程&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;目标：发现分子式，能优化融入了chemical rules的特定的properties&lt;/li&gt;
  &lt;li&gt;GCPN：一个结合了RL的面向目标（goal-directed）的通用的model
    &lt;ul&gt;
      &lt;li&gt;使用policy gradients来优化adversarial loss和domain-specific rewards&lt;/li&gt;
      &lt;li&gt;能够在一个融入了domain-specific rules的environment中生效&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;整体架构图如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/gcpn.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M=(S,A,P,R,\gamma)\)&lt;/code&gt;：
    &lt;ul&gt;
      &lt;li&gt;states &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S=\{s_i\}\)&lt;/code&gt;：包括所有intermediat和final graphs&lt;/li&gt;
      &lt;li&gt;actions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A=\{a_i\}\)&lt;/code&gt;：每一个step对当前graph进行的修改&lt;/li&gt;
      &lt;li&gt;状态转移概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;reward函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;discount factor &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;状态空间：
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;是中间生成的图&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_t\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_0\)&lt;/code&gt;包括一个single node，表示一个carbon atom(碳原子)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;动作空间：
    &lt;ul&gt;
      &lt;li&gt;每个step将要添加的一个atoms的集合：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C=\cup ^S_{i=1}C_i\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;具体的actions:
        &lt;ul&gt;
          &lt;li&gt;把一个新的atom &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C_i\)&lt;/code&gt;连接到现有的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_t\)&lt;/code&gt;中的一个节点上去&lt;/li&gt;
          &lt;li&gt;连接&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_t\)&lt;/code&gt;内退出(exiting)的节点&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;state transition dynamics:
    &lt;ul&gt;
      &lt;li&gt;在state transition dynamics中融入了domain-specific rules，只执行遵守规则的actions&lt;/li&gt;
      &lt;li&gt;policy network产生的infeasible(不可实行的)动作会被rejected，而state保持不变&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Reward设计
    &lt;ul&gt;
      &lt;li&gt;final rewards：domain-specific rewards之和（例如，最终的property scores，对不真实的分子的惩罚，adversirial rewards）&lt;/li&gt;
      &lt;li&gt;intermediate rewards：step-wise validity(有效性) rewards和adversirial rewards&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;GCPN
    &lt;ul&gt;
      &lt;li&gt;使用neural message passing算法计算节点的embeddings&lt;/li&gt;
      &lt;li&gt;预测action：
        &lt;ul&gt;
          &lt;li&gt;挑选两个节点&lt;/li&gt;
          &lt;li&gt;预测边的类型&lt;/li&gt;
          &lt;li&gt;预测是否结束（termination）&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;整体公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
a_t=CONCAT(a_{first},a_{second},a_{edge},a_{stop})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
f_{first}(s_t)=SOFTMAX(m_f(X)), &amp;amp; a_{first}\sim f_{first}(s_t)\in \{0,1\}^n \\ 
f_{second}(s_t)=SOFTMAX(m_s(X_{a_{first}},X)), &amp;amp; a_{second}\sim f_{second}(s_t)\in \{0,1\}^{n+c} \\ 
f_{edge}(s_t)=SOFTMAX(m_e(X_{a_{first}},X_{a_{second}})), &amp;amp; a_{edge}\sim f_{edge}(s_t)\in \{0,1\}^b \\ 
f_{stop}(s_t)=SOFTMAX(m_t(AGG(X))), &amp;amp; a_{stop}\sim f_{stop}(s_t)\in \{0,1\} \\ 
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;未来方向&quot;&gt;未来方向&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/38142339&quot;&gt;https://zhuanlan.zhihu.com/p/38142339&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要想法：将relational的关系转化成attention，利用attention来代表两个entity的关系。隐式地将relational引入NN结构中&lt;/p&gt;

&lt;p&gt;Zambaldi et al.在2018的&lt;a href=&quot;https://arxiv.org/abs/1806.01830&quot;&gt;Relational deep reinforcement learning&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;自己的小结&quot;&gt;自己的小结&lt;/h2&gt;

&lt;p&gt;某个时候整理了个ppt：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/gnn-my-understanding1.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/gnn-my-understanding2.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;其他&quot;&gt;其他&lt;/h2&gt;

&lt;h3 id=&quot;google的一些进展&quot;&gt;Google的一些进展&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://ai.googleblog.com/2019/06/innovations-in-graph-representation.html&quot;&gt;https://ai.googleblog.com/2019/06/innovations-in-graph-representation.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650767026&amp;amp;idx=4&amp;amp;sn=b1f5c9eb2e060c51b882584ee3a24de9&amp;amp;chksm=871abaccb06d33da8a05c7e6afa794763c9e85a75f783a26ef53d3ac8f2cc164cd18dc9926e9&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=zAXdHORK5tTx549e9RwAgNcm7bjJrH4ENwbbTYVrAZDqpsE%2Fu1hY63b%2FoRfnZQdM#rd&quot;&gt;谷歌图表征学习创新：学习单个节点多个嵌入&amp;amp;自动学习最优超参数&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码地址：&lt;a href=&quot;https://github.com/google-research/google-research/tree/master/graph_embedding&quot;&gt;https://github.com/google-research/google-research/tree/master/graph_embedding&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;理解gat&quot;&gt;理解gat&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.02850&quot;&gt;Understanding Attention and Generalization in Graph Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/bknyaz/graph_attention_pool&quot;&gt;https://github.com/bknyaz/graph_attention_pool&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是圭尔夫大学发表于 NeurIPS 2019 的工作。本文的目标是更好地理解图神经网络（GNN）中节点的注意力，并确定影响其有效性的因素。本文特别关注将注意力 GNN 泛化到更大，更复杂或更嘈杂的图的能力。受图同构网络工作的启发，本文设计了简单的图推理任务，使本文能够在受控环境中研究注意力机制。本文发现在典型条件下，注意力的影响可以忽略甚至是有害的，但在某些条件下，它在一些分类任务中提供超过 60％ 的特殊性能提升。在实践中满足这些条件是具有挑战性的，并且通常需要对注意力机制进行监督训练。本文提出了一种替代方法，并以弱监督的方式训练注意力，以接近监督模型的性能，并且与无监督模型相比，改进了几个合成数据集和真实数据集的结果。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>txt2img gan</title>
   <link href="http://daiwk.github.io/posts/cv-gan-txt2img.html"/>
   <updated>2019-02-01T00:00:00+00:00</updated>
   <id>/posts/cv-gan-txt2img</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;paper：&lt;a href=&quot;https://arxiv.org/pdf/1803.04469.pdf&quot;&gt;An Introduction to Image Synthesis with Generative Adversarial Nets&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.twistedwg.com/2019/01/23/GAN_image_generation.html&quot;&gt;GAN在图像生成应用综述（论文解读）&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>smlta</title>
   <link href="http://daiwk.github.io/posts/audio-smlta.html"/>
   <updated>2019-01-19T00:00:00+00:00</updated>
   <id>/posts/audio-smlta</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650755848&amp;amp;idx=3&amp;amp;sn=857b3c5431f9a4a716e856190ef3296a&amp;amp;chksm=871a9776b06d1e60f7c26e57f7eb7f95b6d79fb2ab663dfbf786d33d24f32159599a3c021ab2&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0119iTrsYigomRKoWRJcDojE&amp;amp;pass_ticket=aXFgueLD%2BxWMZCVaAWnXvtEoEpLmG3oXS8xAbcmCXkgLYGoJ9eVER7nGjjDlztRz#rd&quot;&gt;超越整句的流式多级Attention：解密百度输入法背后的语音识别模型&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>序列生成学习算法通用框架(ERPO)</title>
   <link href="http://daiwk.github.io/posts/platform-erpo.html"/>
   <updated>2019-01-14T00:00:00+00:00</updated>
   <id>/posts/platform-erpo</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650755575&amp;amp;idx=4&amp;amp;sn=dabe0c6c036905a81b82effdc0dc0979&amp;amp;chksm=871a9589b06d1c9f028cdf42ab7b101eafb844aa0f9c5e6a1a8f472f59e74e52838980bb1ca0&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0114RQr28a9xQjVoi8NG46kf&amp;amp;pass_ticket=aXFgueLD%2BxWMZCVaAWnXvtEoEpLmG3oXS8xAbcmCXkgLYGoJ9eVER7nGjjDlztRz#rd&quot;&gt;Petuum提出序列生成学习算法通用框架&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650749003&amp;amp;idx=2&amp;amp;sn=a7ebe3049eceda4762ad3733dbe24829&amp;amp;chksm=871afc35b06d7523abd31e0a506343c2c1feafcf1164d17a4c20b8b9390a3f2dbf7dfd47f39e&amp;amp;scene=21#wechat_redirect&quot;&gt;资源 | Petuum&amp;amp;CMU开源Texar：一个模块化、多功能、可扩展的文本生成工具包&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>transformer-xl</title>
   <link href="http://daiwk.github.io/posts/nlp-transformer-xl.html"/>
   <updated>2019-01-12T00:00:00+00:00</updated>
   <id>/posts/nlp-transformer-xl</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#对比论文&quot;&gt;对比论文&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#模型架构&quot;&gt;模型架构&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#原始transformer&quot;&gt;原始transformer&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#transformer-xl&quot;&gt;transformer-xl&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652036193&amp;amp;idx=3&amp;amp;sn=9d2953603e864f124d96944964adf7a4&amp;amp;chksm=f121a890c65621869557432255957ece686a434d0f30c67b8dbc18efdc306f73fad9850da944&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0113pKJ1LJ0Kc8iZHYGhjsZF&amp;amp;pass_ticket=GbqnkzYDgSDQxJoviNYzckA8ZJ6bULsWpoyug4CHgCsT0B80C5nEC38bRj4CywCT#rd&quot;&gt;谷歌、CMU重磅论文：Transformer升级版，评估速度提升超1800倍！&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1901.02860.pdf&quot;&gt;Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github：&lt;a href=&quot;https://github.com/kimiyoung/transformer-xl&quot;&gt;https://github.com/kimiyoung/transformer-xl&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Transformer网络具有学习较长期依赖关系的潜力，但是在语言建模的设置中受到&lt;strong&gt;固定长度上下文&lt;/strong&gt;(fixed-length context)的限制。&lt;/p&gt;

&lt;p&gt;Transformer-XL使Transformer能够在不破坏时间一致性的情况下学习固定长度以外的依赖性。它由一个&lt;strong&gt;segment-level&lt;/strong&gt;的&lt;strong&gt;递归机制&lt;/strong&gt;和一种&lt;strong&gt;新的位置编码&lt;/strong&gt;方案组成。这一方法不仅能够捕获长期依赖关系，而且&lt;strong&gt;解决了上下文碎片&lt;/strong&gt;的问题。&lt;/p&gt;

&lt;p&gt;Transformer-XL学习的依赖关系比RNN长80%，比vanilla Transformer长450%，在&lt;strong&gt;短序列和长序列上都获得了更好的性能&lt;/strong&gt;，并且在&lt;strong&gt;评估&lt;/strong&gt;中比&lt;strong&gt;vanilla Transformer快1800+倍&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;Transformer-XL在5个数据集上都获得了强大的结果。研究人员在enwiki8上将bpc/perplexity的最新 state-of-the-art(SoTA)结果从1.06提高到0.99，在text8上从1.13提高到1.08，在WikiText-103上从20.5提高到18.3，在One Billion Word, 上从23.7提高到21.8，在Penn Treebank上从55.3提高到54.5。&lt;/p&gt;

&lt;h2 id=&quot;对比论文&quot;&gt;对比论文&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1808.04444.pdf&quot;&gt;Character-level language modeling with deeper self-attention&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这篇论文在char-level的语言模型中，用了很深的transformer(64层)…&lt;/p&gt;

&lt;h2 id=&quot;模型架构&quot;&gt;模型架构&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;不再从头开始计算&lt;/strong&gt;每个&lt;strong&gt;新的段(segment)&lt;/strong&gt;的隐藏状态，而是&lt;strong&gt;重用&lt;/strong&gt;(reuse)在&lt;strong&gt;前一段中获得的隐藏状态&lt;/strong&gt;。被重用的隐藏状态用作当前段的memory，这就在段之间建立一个&lt;strong&gt;循环连接&lt;/strong&gt;。提出了一个简单但更有效的&lt;strong&gt;相对位置编码公式&lt;/strong&gt;，该公式可以推广到比训练中观察到的更长的attention lengths。&lt;/p&gt;

&lt;p&gt;因此：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;建模&lt;strong&gt;非常长期的依赖关系&lt;/strong&gt;成为可能，因为信息可以通过循环连接传播&lt;/li&gt;
  &lt;li&gt;从上一段传递信息也可以&lt;strong&gt;解决上下文碎片&lt;/strong&gt;(context fragmentation)的问题。&lt;/li&gt;
  &lt;li&gt;展示了使用&lt;strong&gt;相对位置编码&lt;/strong&gt;而不是绝对位置编码的必要性，以便在不造成时间混乱的情况下&lt;strong&gt;实现状态重用&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;原始transformer&quot;&gt;原始transformer&lt;/h3&gt;

&lt;p&gt;为了将Transformer或self-attention应用到语言建模中，核心问题是如何训练Transformer有效地将&lt;strong&gt;任意长的上下文&lt;/strong&gt;编码为&lt;strong&gt;固定大小的表示&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;给定&lt;strong&gt;无限内存和计算&lt;/strong&gt;，一个简单的解决方案是使用无条件Transformer解码器&lt;strong&gt;处理整个上下文序列&lt;/strong&gt;，类似于前馈神经网络。然而，在实践中，由于资源有限，这通常是不可行的。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/transformer-xl-old.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;一种可行但比较粗略的近似方法是将&lt;strong&gt;整个语料库&lt;/strong&gt;分割成&lt;strong&gt;可管理大小&lt;/strong&gt;的&lt;strong&gt;更短的片段&lt;/strong&gt;，只在&lt;strong&gt;每个片段中训练模型&lt;/strong&gt;，&lt;strong&gt;忽略&lt;/strong&gt;来自&lt;strong&gt;前一段的所有上下文信息&lt;/strong&gt;。这是上面讲到的&lt;a href=&quot;https://daiwk.github.io/posts/nlp-transformer-xl.html#%E5%AF%B9%E6%AF%94%E8%AE%BA%E6%96%87&quot;&gt;对比论文&lt;/a&gt;提出的想法，我们称之为原始模型(vanilla model)，它的图示如图1a。&lt;/p&gt;

&lt;p&gt;在&lt;strong&gt;评估&lt;/strong&gt;过程中，vanilla 模型在每个步骤都消耗与训练期间相同长度的一个segment，但是&lt;strong&gt;在最后一个位置&lt;/strong&gt;只是进行&lt;strong&gt;一次&lt;/strong&gt;预测。然后，在&lt;strong&gt;下一步&lt;/strong&gt;中，这个segment&lt;strong&gt;只向右移动一个位置&lt;/strong&gt;，&lt;strong&gt;新的segment必须从头开始&lt;/strong&gt;处理。&lt;/p&gt;

&lt;p&gt;如图1b所示，该过程保证了每个预测在训练过程中&lt;strong&gt;利用的上下文尽可能长&lt;/strong&gt;，同时也&lt;strong&gt;缓解了&lt;/strong&gt;训练过程中遇到的&lt;strong&gt;上下文碎片&lt;/strong&gt;问题。然而，这个&lt;strong&gt;评估过程成本是非常高的&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;transformer-xl&quot;&gt;transformer-xl&lt;/h3&gt;

&lt;p&gt;为了解决固定长度上下文的局限性，我们在Transformer架构中引入一种&lt;strong&gt;递归机制(recurrence mechanism)&lt;/strong&gt;。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/transformer-xl-new.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在训练过程中，对&lt;strong&gt;上一个segment&lt;/strong&gt;计算的&lt;strong&gt;隐藏状态序列&lt;/strong&gt;进行&lt;strong&gt;fixed和cached&lt;/strong&gt;，并在模型&lt;strong&gt;处理下一个新的segment时&lt;/strong&gt;将其缓存为可重用的扩展上下文，如图2a所示。&lt;/p&gt;

&lt;p&gt;这种递归机制应用于整个语料库的&lt;strong&gt;每两个连续的segment&lt;/strong&gt;，它本质上是在隐藏状态中创建一个&lt;strong&gt;segment-level的递归&lt;/strong&gt;。因此，所使用的有效上下文可以远远超出两个segments。&lt;/p&gt;

&lt;p&gt;除了实现超长的上下文和解决碎片问题外，这种递归方案的另一个好处是&lt;strong&gt;显著加快了评估速度&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;具体地说，在评估期间，可以重用前面部分的表示，而不是像普通模型那样从头开始计算。在enwiki8数据集的实验中，Transformer-XL在评估过程比普通模型快1800倍以上。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>分词工具</title>
   <link href="http://daiwk.github.io/posts/nlp-wordseg-tools.html"/>
   <updated>2019-01-09T00:00:00+00:00</updated>
   <id>/posts/nlp-wordseg-tools</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650755353&amp;amp;idx=3&amp;amp;sn=12aacb01478eb362584581383117200f&amp;amp;chksm=871a9567b06d1c71dba040d3614dbae2b07ad693865c18556240d7bf958a3e026a298cf26853&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0113bqxHuiWfIiVCVso4KrY1&amp;amp;pass_ticket=GbqnkzYDgSDQxJoviNYzckA8ZJ6bULsWpoyug4CHgCsT0B80C5nEC38bRj4CywCT#rd&quot;&gt;北大开源全新中文分词工具包：准确率远超THULAC、结巴分词&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github：&lt;a href=&quot;https://github.com/lancopku/PKUSeg-python&quot;&gt;https://github.com/lancopku/PKUSeg-python&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;高分词准确率&lt;/strong&gt;：相比于其他的分词工具包，该工具包在不同领域的数据上都大幅提高了分词的准确度。根据北大研究组的测试结果，pkuseg 分别在示例数据集（MSRA 和 CTB8）上降低了 79.33% 和 63.67% 的分词错误率。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;多领域分词&lt;/strong&gt;：研究组训练了多种不同领域的分词模型。根据待分词的领域特点，用户可以自由地选择不同的模型。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;支持用户自训练模型&lt;/strong&gt;：支持用户使用全新的标注数据进行训练。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目前仅支持python3…不支持python2&lt;/p&gt;

&lt;p&gt;安装&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/lancopku/pkuseg-python.git
python setup.py &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>graph embedding</title>
   <link href="http://daiwk.github.io/posts/dl-graphembedding.html"/>
   <updated>2019-01-05T00:00:00+00:00</updated>
   <id>/posts/dl-graphembedding</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot;&gt;概述&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#deepwalk&quot;&gt;DeepWalk&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#line&quot;&gt;LINE&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#node2vec&quot;&gt;node2vec&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sdne&quot;&gt;SDNE&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#其他embedding&quot;&gt;其他embedding&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tweet2vec&quot;&gt;tweet2vec&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u013527419/article/details/76017528/&quot;&gt;https://blog.csdn.net/u013527419/article/details/76017528/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;deepwalk&quot;&gt;DeepWalk&lt;/h3&gt;

&lt;h3 id=&quot;line&quot;&gt;LINE&lt;/h3&gt;

&lt;h3 id=&quot;node2vec&quot;&gt;node2vec&lt;/h3&gt;

&lt;p&gt;开源代码实现：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://snap.stanford.edu/node2vec/&quot;&gt;http://snap.stanford.edu/node2vec/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://arxiv.org/abs/1607.00653&quot;&gt;node2vec: Scalable Feature Learning for Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1301.3781&quot;&gt;Efficient Estimation of Word Representations in Vector Space&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在计算广告、推荐领域中，围绕着node2vec有俩很有意思的应用：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Facebook：&lt;a href=&quot;http://geek.csdn.net/news/detail/200138&quot;&gt;http://geek.csdn.net/news/detail/200138&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Tencent：&lt;a href=&quot;http://www.sohu.com/a/124091440_355140&quot;&gt;http://www.sohu.com/a/124091440_355140&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;sdne&quot;&gt;SDNE&lt;/h3&gt;

&lt;h2 id=&quot;其他embedding&quot;&gt;其他embedding&lt;/h2&gt;

&lt;h3 id=&quot;tweet2vec&quot;&gt;tweet2vec&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://lsm.media.mit.edu/papers/tweet2vec_vvr.pdf&quot;&gt;Tweet2Vec: Learning Tweet Embeddings Using Character-level CNN-LSTM Encoder-Decoder&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>图神经网络</title>
   <link href="http://daiwk.github.io/posts/dl-gnn.html"/>
   <updated>2019-01-05T00:00:00+00:00</updated>
   <id>/posts/dl-gnn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#引言&quot;&gt;引言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#模型&quot;&gt;模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#原始gnn&quot;&gt;原始GNN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gnn变种&quot;&gt;GNN变种&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#图类型&quot;&gt;图类型&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#directed-graphs&quot;&gt;Directed Graphs&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#heterogeneous-graphs&quot;&gt;Heterogeneous Graphs&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#graphs-with-edge-information&quot;&gt;Graphs with Edge Information&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#传播类型propagation-types&quot;&gt;传播类型(propagation types)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#训练方法&quot;&gt;训练方法&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#general-frameworks&quot;&gt;General Frameworks&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#message-passing-neural-networksmpnn&quot;&gt;Message Passing Neural Networks(MPNN)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#non-local-neural-networksnlnn&quot;&gt;Non-local Neural Networks(NLNN)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#graph-networksgn&quot;&gt;Graph Networks(GN)&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#应用&quot;&gt;应用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#结构化场景&quot;&gt;结构化场景&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#非结构化场景&quot;&gt;非结构化场景&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#其他场景&quot;&gt;其他场景&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#开放性问题&quot;&gt;开放性问题&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gnn的普适与局限性&quot;&gt;GNN的普适与局限性&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#kdd-2019&quot;&gt;kdd 2019&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;最近图神经网络有点火。。先mark一下：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650754602&amp;amp;idx=2&amp;amp;sn=7d55e6c20bbf4d85d19d62fc67045b85&amp;amp;chksm=871a8a54b06d03429b5fb811c7ef7f22d368c3c2db2ebb71b3abd3d6a9737088e54a402c5ac2&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1228olcW24R33IxsQlPTzwYg&amp;amp;pass_ticket=SzwEuJ2YzNx6f6qRO8kkEzvCJzmwLfJnp2tSTApgym2Eec7lBnwb3SzGw6NeSiqz#rd&quot;&gt;清华大学孙茂松组：图神经网络必读论文列表&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github链接：&lt;a href=&quot;https://github.com/thunlp/GNNPapers&quot;&gt;https://github.com/thunlp/GNNPapers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;综述&lt;a href=&quot;https://arxiv.org/pdf/1812.08434&quot;&gt;Graph Neural Networks: A Review of Methods and Applications&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;解读：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247493906&amp;amp;idx=1&amp;amp;sn=15c9f18a1ce6baa15dc85ecb52e799f6&amp;amp;chksm=96ea3692a19dbf847c1711e6e194ad60d80d11138daf0938f90489a054d77cfd523bee2dc1d2&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1226x1tdeDKTzwcuszAuh1O0&amp;amp;pass_ticket=SzwEuJ2YzNx6f6qRO8kkEzvCJzmwLfJnp2tSTApgym2Eec7lBnwb3SzGw6NeSiqz#rd&quot;&gt;图神经网络综述：模型与应用&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;又一篇综述：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650755237&amp;amp;idx=1&amp;amp;sn=2dd0468552e69057681eec58fd265cbb&amp;amp;chksm=871a94dbb06d1dcd90451b17cc94a38811619fd49c07f1d1cf1909436746bae9b79717c345b2&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=01078WbXsi7srXvp72V6GitX&amp;amp;pass_ticket=mPnDPDR4heSU20VXT7N8W622Cb1dZmIzkNcF8BygI%2Bp60d7GrSesIej%2FlrFbnO84#rd&quot;&gt;图神经网络概述第三弹：来自IEEE Fellow的GNN综述&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;又一个文献合集&lt;a href=&quot;https://github.com/DeepGraphLearning/LiteratureDL4Graph&quot;&gt;https://github.com/DeepGraphLearning/LiteratureDL4Graph&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;引言&quot;&gt;引言&lt;/h2&gt;

&lt;p&gt;图是一种数据结构，它对一组对象 &lt;strong&gt;（节点）&lt;/strong&gt; 及其关系 &lt;strong&gt;（边）&lt;/strong&gt; 进行建模。图神经网络（GNN）是一类基于深度学习的&lt;strong&gt;处理图域信息&lt;/strong&gt;的方法。由于其较好的&lt;strong&gt;性能&lt;/strong&gt;和&lt;strong&gt;可解释性&lt;/strong&gt;，GNN 最近已成为一种广泛应用的图分析方法。&lt;/p&gt;

&lt;p&gt;GNN的第一个动机源于&lt;strong&gt;卷积神经网络（CNN）&lt;/strong&gt;：CNN只能在规则的Euclidean数据上运行，如图像（2维网格）和文本（1维序列）。如何将CNN应用于&lt;strong&gt;图结构这一非欧几里德空间&lt;/strong&gt;，成为GNN模型重点解决的问题。&lt;/p&gt;

&lt;p&gt;GNN的另一个动机来自&lt;strong&gt;图嵌入（Graph Embedding）&lt;/strong&gt;，它学习图中&lt;strong&gt;节点&lt;/strong&gt;、&lt;strong&gt;边&lt;/strong&gt;或&lt;strong&gt;子图&lt;/strong&gt;的低维向量空间表示。DeepWalk、node2vec、LINE、SDNE等方法在网络表示学习领域取得了很大的成功。然而，这些方法在&lt;strong&gt;计算上较为复杂&lt;/strong&gt;并且在&lt;strong&gt;大规模上的图上&lt;/strong&gt;并&lt;strong&gt;不是最优&lt;/strong&gt;的，GNN旨在解决这些问题。&lt;/p&gt;

&lt;h2 id=&quot;模型&quot;&gt;模型&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/gnn-overview.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;原始gnn&quot;&gt;原始GNN&lt;/h3&gt;

&lt;p&gt;2009年的第一篇GNN的文章&lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.1015.7227&amp;amp;rep=rep1&amp;amp;type=pdf&quot;&gt;The graph neural network model&lt;/a&gt;。GNN的target是要学习包括了每个节点的邻居的信息的state embedding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_v\in R^s\)&lt;/code&gt;。这里的state embedding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_v\)&lt;/code&gt;是节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;的一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;维的向量，能用来产生输出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(o_v\)&lt;/code&gt;，例如节点的label。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f\)&lt;/code&gt;是一个带有参数的函数，称为local transition function，被所有的节点共享，能根据输入的邻居来更新节点状态。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g\)&lt;/code&gt;是local output function，表示如何产生output。所以：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
h_v=f(x_v,x_{co[v]},h_{ne[v]},x_{ne[v]})\\
o_v=g(h_v,x_v)\\
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_v\)&lt;/code&gt;：节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;的特征。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{co[v]}\)&lt;/code&gt;：节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;的边的特征。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{ne[v]}\)&lt;/code&gt;：节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;的邻居节点的state。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{ne[v]}\)&lt;/code&gt;：节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v\)&lt;/code&gt;的邻居节点的特征。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;把所有状态、输出、特征、所有节点的特征stack到一起，就有了向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X_N\)&lt;/code&gt;。所以有：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
H=F(H,X)\\
O=G(H,X_N)\\
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;使用如下方式对状态进行迭代：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
H^{t+1}=F(H^t,X)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g\)&lt;/code&gt;都可以用神经网络来表示。loss如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
loss=\sum ^p_{i=1}(t_i-o_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;gnn变种&quot;&gt;GNN变种&lt;/h3&gt;

&lt;h4 id=&quot;图类型&quot;&gt;图类型&lt;/h4&gt;

&lt;h5 id=&quot;directed-graphs&quot;&gt;Directed Graphs&lt;/h5&gt;

&lt;p&gt;有向图。如图中的ADGPM【&lt;a href=&quot;https://arxiv.org/abs/1805.11724&quot;&gt;Rethinking knowledge graph propagation for zero-shot learning&lt;/a&gt;】，有两种权重矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_p\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_c\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[H^t=\sigma(D^{-1}_pA_p\sigma(D^{-1}_cA_cH^{t-1}W_c)W_p)\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D^{-1}_pA_p\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D^{-1}_cA_c\)&lt;/code&gt;分别是parent和children的normalized adjacency matrix。&lt;/p&gt;

&lt;h5 id=&quot;heterogeneous-graphs&quot;&gt;Heterogeneous Graphs&lt;/h5&gt;

&lt;p&gt;异构图。如图中的GraphInception。&lt;/p&gt;

&lt;h5 id=&quot;graphs-with-edge-information&quot;&gt;Graphs with Edge Information&lt;/h5&gt;

&lt;p&gt;边带有信息的图，如图中的G2S以及R-GCN。&lt;/p&gt;

&lt;h4 id=&quot;传播类型propagation-types&quot;&gt;传播类型(propagation types)&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;propagation step&lt;/strong&gt;和&lt;strong&gt;output step&lt;/strong&gt;这两部是很重要的，主要分为以下类别：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;卷积&lt;/strong&gt;：Graph Convolutional Network（GCN）希望将卷积操作应用在图结构数据上，主要分为Spectral Method和Spatial Method（Non-spectral Method）两类。
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Spectral Method&lt;/strong&gt;：希望使用&lt;strong&gt;谱分解&lt;/strong&gt;的方法，应用图的&lt;strong&gt;拉普拉斯矩阵分解&lt;/strong&gt;进行节点的信息收集。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Spatial Method&lt;/strong&gt;：&lt;strong&gt;直接使用图的拓扑结构&lt;/strong&gt;，根据图的&lt;strong&gt;邻居信息&lt;/strong&gt;进行信息收集。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;注意力机制&lt;/strong&gt;：Graph Attention Network 致力于将注意力机制应用在图中的&lt;strong&gt;信息收集阶段&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;门机制&lt;/strong&gt;：门机制应用于&lt;strong&gt;节点更新阶段&lt;/strong&gt;。Gated graph neural network 将&lt;strong&gt;GRU&lt;/strong&gt;机制应用于节点更新。很多工作致力于将 &lt;strong&gt;LSTM&lt;/strong&gt;应用于不同类型的图上，主要包括&lt;strong&gt;Tree LSTM&lt;/strong&gt;、&lt;strong&gt;Graph LSTM&lt;/strong&gt;和&lt;strong&gt;Sentence LSTM&lt;/strong&gt;等&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;残差连接&lt;/strong&gt;：堆叠多层图神经网络可能引起噪声在指数级增加的邻居中传播，所以很多工作将残差机制应用于图神经网络中。主要包括&lt;strong&gt;Highway GNN&lt;/strong&gt;和&lt;strong&gt;Jump Knowledge Network&lt;/strong&gt;两种不同的处理方式。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;信息传递函数主要包括&lt;strong&gt;信息收集（agggregation）&lt;/strong&gt;和节点&lt;strong&gt;信息更新（update）&lt;/strong&gt;两个部分，公式见：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/gnn-formulas.png&quot; style=&quot;max-height: 500px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h4 id=&quot;训练方法&quot;&gt;训练方法&lt;/h4&gt;

&lt;p&gt;譬如&lt;strong&gt;GraphSAGE&lt;/strong&gt;从&lt;strong&gt;附近的邻居&lt;/strong&gt;收集信息，并且能够应用于&lt;strong&gt;inductive learning&lt;/strong&gt;领域；&lt;strong&gt;FastGCN&lt;/strong&gt;使用了&lt;strong&gt;importance sampling&lt;/strong&gt;的方法，使用&lt;strong&gt;采样&lt;/strong&gt;替代使用节点所有的邻居信息，加快了训练过程。&lt;/p&gt;

&lt;h3 id=&quot;general-frameworks&quot;&gt;General Frameworks&lt;/h3&gt;

&lt;p&gt;图神经网络通用框架主要包括MPNN（Message Passing Neural Network）、NLNN（Non-local Neural Network）以及 Deepmind 的 GN（Graph Network）。&lt;/p&gt;

&lt;h4 id=&quot;message-passing-neural-networksmpnn&quot;&gt;Message Passing Neural Networks(MPNN)&lt;/h4&gt;

&lt;p&gt;MPNN将模型总结为信息传递阶段和节点更新阶段，概括了多种图神经网络和图卷积神经网络方法。&lt;/p&gt;

&lt;h4 id=&quot;non-local-neural-networksnlnn&quot;&gt;Non-local Neural Networks(NLNN)&lt;/h4&gt;

&lt;p&gt;NLNN总结了很多种基于自注意力机制的方法。&lt;/p&gt;

&lt;h4 id=&quot;graph-networksgn&quot;&gt;Graph Networks(GN)&lt;/h4&gt;

&lt;p&gt;GN提出了更加通用的模型，能够总结概括几乎所有文中提到的框架，并且拥有更加灵活的表示能力、易于配置的块内结构以及易于组合的多模块架构。&lt;/p&gt;

&lt;h2 id=&quot;应用&quot;&gt;应用&lt;/h2&gt;

&lt;p&gt;应用见：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/gnn-applications.png&quot; style=&quot;max-height: 500px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;结构化场景&quot;&gt;结构化场景&lt;/h3&gt;

&lt;p&gt;GNN 被广泛应用在社交网络、推荐系统、物理系统、化学分子预测、知识图谱等领域。&lt;/p&gt;

&lt;h3 id=&quot;非结构化场景&quot;&gt;非结构化场景&lt;/h3&gt;

&lt;p&gt;在图像和文本中的应用&lt;/p&gt;

&lt;h3 id=&quot;其他场景&quot;&gt;其他场景&lt;/h3&gt;

&lt;p&gt;图生成模型以及使用GNN来解决组合优化问题的场景。&lt;/p&gt;

&lt;h2 id=&quot;开放性问题&quot;&gt;开放性问题&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;浅层结构&lt;/strong&gt;。经验上使用更多参数的神经网络能够得到更好的实验效果，然而堆叠多层的GNN却会产生&lt;strong&gt;over-smoothing&lt;/strong&gt;的问题。具体来说，堆叠层数越多，节点考虑的&lt;strong&gt;邻居个数也会越多&lt;/strong&gt;，导致最终&lt;strong&gt;所有节点的表示&lt;/strong&gt;会&lt;strong&gt;趋向于一致&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;动态图&lt;/strong&gt;。目前大部分方法关注于在静态图上的处理，对于如何处理&lt;strong&gt;节点信息&lt;/strong&gt;和&lt;strong&gt;边信息&lt;/strong&gt;随着时间步&lt;strong&gt;动态变化&lt;/strong&gt;的图仍是一个开放问题。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;非结构化场景&lt;/strong&gt;。虽然很多工作应用于非结构化的场景（比如文本），然而并没有&lt;strong&gt;通用的方法&lt;/strong&gt;用于处理非结构化的数据。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;扩展性&lt;/strong&gt;。虽然已经有一些方法尝试解决这个问题，将图神经网络的方法&lt;strong&gt;应用于大规模数据&lt;/strong&gt;上仍然是一个开放性问题。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;gnn的普适与局限性&quot;&gt;GNN的普适与局限性&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766858&amp;amp;idx=5&amp;amp;sn=c2791f28bdbd31c03960e1aa90a06855&amp;amp;chksm=871aba74b06d33627009959b85b478bf5bbad95fde2216a9ba7290d0ab4657b9590e7dc1e7f7&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1564377881319&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;图神经网络火了？谈下它的普适性与局限性&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;kdd-2019&quot;&gt;kdd 2019&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU2OTA0NzE2NA==&amp;amp;mid=2247513927&amp;amp;idx=1&amp;amp;sn=3c5d4c709dac346fcb339d47a33279e9&amp;amp;chksm=fc865654cbf1df42cf7ceb8649f32095b67fd9d34b0ccc4b779983c3c05f62f1cef1bcf44590&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1565122204533&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;近期必读的12篇KDD 2019【图神经网络（GNN）】相关论文&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>drn</title>
   <link href="http://daiwk.github.io/posts/rl-drn.html"/>
   <updated>2019-01-01T00:00:00+00:00</updated>
   <id>/posts/rl-drn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E8%83%8C%E6%99%AF&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%A8%A1%E5%9E%8B&quot;&gt;模型&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%85%B7%E4%BD%93%E5%AE%9E%E7%8E%B0&quot;&gt;具体实现&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#dueling-double-dqn&quot;&gt;dueling double DQN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#user-activeness&quot;&gt;User activeness&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#exploration&quot;&gt;Exploration&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/58280384&quot;&gt;强化学习系列一——基于深度强化学习的新闻推荐模型DRN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.personal.psu.edu/~gjz5038/paper/www2018_reinforceRec/www2018_reinforceRec.pdf&quot;&gt;DRN: A Deep Reinforcement Learning Framework for News Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

&lt;p&gt;现有推荐模型的几大问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;新闻推荐中的&lt;strong&gt;动态变化&lt;/strong&gt;难以处理&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;动态变化表现在两种层次：一个是新闻的&lt;strong&gt;时效性&lt;/strong&gt;，可能会很快过时；另一个是&lt;strong&gt;用户的浏览兴趣&lt;/strong&gt;，随着时间会动态变化。虽然通过在线学习的方式可以捕获新闻特征和用户兴趣的动态变化，但是这些方法&lt;strong&gt;只注重短期回报，并未考虑长期回报&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;仅使用&lt;strong&gt;是否点击&lt;/strong&gt;作为用户反馈&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;现有方法只考虑是否点击或者打分来作为用户的反馈。作者提出引入&lt;strong&gt;用户返回APP的时间&lt;/strong&gt;，因为该指标也反映了用户对推荐的&lt;strong&gt;满意度&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;现有方法推荐结果&lt;strong&gt;重复&lt;/strong&gt;较多&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;现有方法倾向于给用户推荐相似item，而这将降低用户对相似topic的兴趣，也不利于探索用户新的兴趣。&lt;/p&gt;

&lt;h2 id=&quot;模型&quot;&gt;模型&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/drn-framework.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;将用户和news的特征表示输入给DQN来预测可能的reward，也就是用户&lt;strong&gt;点击news的概率&lt;/strong&gt;。该框架通过DQN的&lt;strong&gt;在线更新&lt;/strong&gt;来处理新闻推荐中的动态变化，并&lt;strong&gt;注重长期回报&lt;/strong&gt;。框架除了用户点击的反馈之外，引入了user activeness（用户在一次推荐后返回到APP的频率）作为回报。另外为避免传统的exploration策略比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon-greedy\)&lt;/code&gt;或UCB可能带来的不准确推荐，作者引入&lt;strong&gt;Dueling Bandit Gradient Descent&lt;/strong&gt;策略来改善推荐的&lt;strong&gt;多样性&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;离线部分：主要是使用离线的用户点击日志来训练，给点击和未点击的news不同的奖励，然后训练一个Q网络，其实就是一个监督学习的过程。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;在线部分：涉及推荐agent和用户的交互过程，按下面的流程对网络进行更新：&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;Push：每当用户发送一个请求，推荐智能体G会根据&lt;strong&gt;用户和候选news集合&lt;/strong&gt;的特征表示，综合exploitation network和exploration network两个网络的结果，&lt;strong&gt;生成top-k&lt;/strong&gt;的news列表L&lt;/li&gt;
      &lt;li&gt;FeedBack：用户针对接受的列表L将会有&lt;strong&gt;点击行为&lt;/strong&gt;。&lt;/li&gt;
      &lt;li&gt;Minor Update：在&lt;strong&gt;每个时间戳&lt;/strong&gt;后，根据user、news列表L和反馈B，智能体G将会比较&lt;strong&gt;exploitation network&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;和&lt;strong&gt;exploration network&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{Q}\)&lt;/code&gt;的推荐效果来&lt;strong&gt;更新模型&lt;/strong&gt;。如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{Q}\)&lt;/code&gt;给出的推荐结果最终效果更好，则将Q往 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{Q}\)&lt;/code&gt;的方向更新，否则&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;保持不变。&lt;strong&gt;每次推荐发生之后都会进行Minor update&lt;/strong&gt;。&lt;/li&gt;
      &lt;li&gt;Major Update：在一个&lt;strong&gt;特定时期&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T_R\)&lt;/code&gt;后，智能体G将使用&lt;strong&gt;用户反馈B&lt;/strong&gt;和&lt;strong&gt;用户activeness&lt;/strong&gt;来更新网络Q。这里使用&lt;strong&gt;经验回放&lt;/strong&gt;来实现，智能体G保存近期历史click和用户activeness记录，当触发Major Update时，智能体&lt;strong&gt;随机选择一个batch&lt;/strong&gt;来更新模型。Major Update通常&lt;strong&gt;间隔比如一个小时&lt;/strong&gt;来进行，在这段时间会收集很多次的展现和反馈的记录。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;具体实现&quot;&gt;具体实现&lt;/h2&gt;

&lt;h3 id=&quot;dueling-double-dqn&quot;&gt;dueling double DQN&lt;/h3&gt;

&lt;p&gt;使用DQN来建模用户是否点击某一news的概率，而这一概率也是智能体能得到的奖励即reward：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
y_{s,a}=Q(s,a)=r_{immediate}+\gamma r_{future}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;状态state&lt;/strong&gt;：context feature和user feature&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;动作action&lt;/strong&gt;：news feature和user-news交互特征&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_{immediate}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_{future}\)&lt;/code&gt;分别表示&lt;strong&gt;当前情况&lt;/strong&gt;下奖励（也就是用户是否点击新闻）和&lt;strong&gt;未来回报&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/drn-ddqn.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;给定当前状态s、动作a和时间戳t，使用DDQN来对total reward进行预测：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
y_{s,a,t}=r_{a,t+1}+\gamma Q(s_{a,t+1},\mathop{\arg\max}_{a^{'}}Q(s_{a,t+1},a^{'};w_t);w_{t}^{'})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_{a,t+1}\)&lt;/code&gt;表示采取&lt;strong&gt;动作a的立即回报&lt;/strong&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t^{'}\)&lt;/code&gt;表示DQN中的两个&lt;strong&gt;不同参数集合&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先&lt;strong&gt;给定候选动作集&lt;/strong&gt;，在参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;的设置下根据&lt;strong&gt;长期回报最大&lt;/strong&gt;选择出动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{'}\)&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;然后&lt;strong&gt;估计&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{a,t+1}\)&lt;/code&gt;采取动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^{'}\)&lt;/code&gt;所得到的&lt;strong&gt;长期回报&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;每几轮迭代之后&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t^{'}\)&lt;/code&gt;将互换。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DDQN(double DQN)可解决Q值过高估计的问题。通过这个过程，模型可兼顾total reward进行决策。&lt;/p&gt;

&lt;p&gt;所以看起来，是先dueling dqn算q，再double dqn算reward&lt;/p&gt;

&lt;p&gt;Q网络底层将四类特征输入到网络中，user feature和context feature作为&lt;strong&gt;state feature&lt;/strong&gt;，而user-news feature和news feature作为&lt;strong&gt;action feature&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一方面，在一个特定状态采用某个动作的&lt;strong&gt;reward和所有特征均有关&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;另一方面，reward&lt;strong&gt;受user本身特征的影响（例如user是否active，user今天是否看了足够多的News了已经）&lt;/strong&gt;要&lt;strong&gt;大于&lt;/strong&gt;&lt;strong&gt;只&lt;/strong&gt;用用户和context特征。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;基于此，文中类似Dueling Network将Q函数分解成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V(s)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A(s,a)\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V(s)\)&lt;/code&gt;&lt;strong&gt;只受state&lt;/strong&gt;特征(即user+context)影响，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A(s,a)\)&lt;/code&gt;则会&lt;strong&gt;受到state特征和action特征&lt;/strong&gt;（即user+context+item+user-item-cross）影响。&lt;/p&gt;

&lt;h3 id=&quot;user-activeness&quot;&gt;User activeness&lt;/h3&gt;

&lt;p&gt;采用&lt;strong&gt;留存模型&lt;/strong&gt;来对用户activeness来建模。时间t后用户返回App的概率定义为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s(t)=e^{-\int_{0}^{t}\lambda(x)dx}\)&lt;/code&gt;，其中一个生命周期&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T_{0}=\int_{0}^{\infty}s(t)dt\)&lt;/code&gt;（文中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T_0\)&lt;/code&gt;设为一天）。每次检测到一个&lt;strong&gt;用户返回App&lt;/strong&gt;，我们将使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s(t)=s(t)+s_a\)&lt;/code&gt;。如下图，开始时&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_0=0.5\)&lt;/code&gt;，然后activeness衰减，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_1\)&lt;/code&gt;时刻用户返回App，使得activeness增加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_a=0.32\)&lt;/code&gt;，后续以此类推。整个过程保证活跃度保持在0-1之间，&lt;strong&gt;如果超过1的话直接截断成1&lt;/strong&gt;。这里&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_0\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_a\)&lt;/code&gt;等值的设定都是数据中的真实用户行为模式确定的。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/drn-activeness.png&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;exploration&quot;&gt;Exploration&lt;/h3&gt;

&lt;p&gt;常见的探索方法包括&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon-greedy\)&lt;/code&gt;和UCB等，其中前者会随机推荐一些新item，后者会选择一些未探索充分的item，这些都会导致推荐效果有一定下降。因此作者使用了Dueling Bandit Gradient Descent算法(最早来自online LTR领域)来进行exploration。智能体G使用当前网络Q产出推荐列表L，并使用explore网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{Q}\)&lt;/code&gt;产生另一个推荐列表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{L}\)&lt;/code&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{Q}\)&lt;/code&gt;的参数可以通过&lt;strong&gt;Q的参数基础上增加微小扰动得到&lt;/strong&gt;。然后使用L和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{L}\)&lt;/code&gt;进行interleave来产生一个&lt;strong&gt;融合的列表&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat L\)&lt;/code&gt;。如果探索网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{Q}\)&lt;/code&gt;产生的item得到更好的反馈，则智能体G将网络Q&lt;strong&gt;向&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{Q}\)&lt;/code&gt;的方向更新&lt;/strong&gt;，表示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w^{'}=w+\eta \tilde{w}\)&lt;/code&gt;，否则Q的参数保持不变。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/drn-bandit-gd.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

</content>
 </entry>
 
 <entry>
   <title>bert as service</title>
   <link href="http://daiwk.github.io/posts/platform-bert-as-service.html"/>
   <updated>2019-01-01T00:00:00+00:00</updated>
   <id>/posts/platform-bert-as-service</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8&quot;&gt;基本使用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%AE%89%E8%A3%85&quot;&gt;安装：&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%90%AF%E5%8A%A8server&quot;&gt;启动server&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%90%AF%E5%8A%A8client&quot;&gt;启动client&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/hanxiao/bert-as-service&quot;&gt;https://github.com/hanxiao/bert-as-service&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;官网：&lt;a href=&quot;https://bert-as-service.readthedocs.io/en/latest/&quot;&gt;https://bert-as-service.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;基本使用&quot;&gt;基本使用&lt;/h2&gt;

&lt;h3 id=&quot;安装&quot;&gt;安装：&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-U&lt;/span&gt; bert-serving-server bert-serving-client
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;要求：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;server的py要&amp;gt;=3.5,tf要&amp;gt;=1.10&lt;/li&gt;
  &lt;li&gt;client可以是py2或者py3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外需要把预训练好的模型下下来~&lt;/p&gt;

&lt;h3 id=&quot;启动server&quot;&gt;启动server&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$workspace&lt;/span&gt;/bin/python3.6 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$workspace&lt;/span&gt;/bin/bert-serving-start &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;-model_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$workspace&lt;/span&gt;/../chinese_L-12_H-768_A-12/ &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;-num_worker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;启动client&quot;&gt;启动client&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;bert_serving.client&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BertClient&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;remote_ip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'xx.xx.xx.xx'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BertClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'First do it'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'then do it right'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'then do it better'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>Pointer Networks和组合优化</title>
   <link href="http://daiwk.github.io/posts/dl-pointer-networks-for-combinational-optimization.html"/>
   <updated>2019-01-01T00:00:00+00:00</updated>
   <id>/posts/dl-pointer-networks-for-combinational-optimization</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#pointer-networks&quot;&gt;Pointer Networks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pointer-networks-in-rl&quot;&gt;Pointer Networks in RL&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;pointer-networks&quot;&gt;Pointer Networks&lt;/h2&gt;

&lt;p&gt;发表在NIPS2015上&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1506.03134.pdf&quot;&gt;Pointer Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jianshu.com/p/2ad389e91467&quot;&gt;Pointer-network理论及tensorflow实战&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pointer-networks-in-rl&quot;&gt;Pointer Networks in RL&lt;/h2&gt;

&lt;p&gt;发表在ICLR2017上&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1611.09940.pdf&quot;&gt;Neural Combinatorial Optimization with Reinforcement Learning&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tf-ranking</title>
   <link href="http://daiwk.github.io/posts/platform-tf-ranking.html"/>
   <updated>2018-12-21T00:00:00+00:00</updated>
   <id>/posts/platform-tf-ranking</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#安装&quot;&gt;安装&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot;&gt;简介&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#定义&quot;&gt;定义&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#打分函数&quot;&gt;打分函数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#损失函数&quot;&gt;损失函数&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU1OTMyNDcxMQ==&amp;amp;mid=2247485361&amp;amp;idx=1&amp;amp;sn=fe8dad5e5dfe8baabe4d60af68b32415&amp;amp;chksm=fc184cf9cb6fc5ef562e20ade54be893cf5f8ffbe89c07ed9e5d6b25cac9bb17fa7a73b52865&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0118obA2nmTOwM9mkfe8HZmY&amp;amp;pass_ticket=yoIK672aXk4WPiJRK3zkCxK5C5wwnua1%2B%2F115s%2FKJyXjdHQlvctIkGZpDsP%2FPVPZ#rd&quot;&gt;TF-Ranking：为 Learning-to-Rank 打造的可扩展 TensorFlow 库&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/ranking&quot;&gt;https://github.com/tensorflow/ranking&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/ranking/tree/master/tensorflow_ranking/examples&quot;&gt;https://github.com/tensorflow/ranking/tree/master/tensorflow_ranking/examples&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1812.00073&quot;&gt;TF-Ranking: Scalable TensorFlow Library for Learning-to-Rank&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build //tensorflow_ranking/tools/pip_package:build_pip_package
bazel-bin/tensorflow_ranking/tools/pip_package/build_pip_package /tmp/ranking_pip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就产生了一个whl文件（要求tf1.12.0+）&lt;/p&gt;

&lt;p&gt;TF-Ranking 能通过嵌入和扩展至数百亿个训练实例来处理稀疏特征（如原始文本）&lt;/p&gt;

&lt;p&gt;TF-Ranking 支持许多常用的排名指标，包括平均倒序排名 (MRR) 和标准化折扣累积收益 (NDCG)。&lt;/p&gt;

&lt;p&gt;TF-Ranking 支持一种新颖的评分机制，可以对多个项目（例如网页）执行联合评分，这是对传统评分范例（对单个项目进行独立评分）的扩展。关于多项目评分，我们经常面临的一个挑战是，难以判断需将哪些项目进行分组并在子组中进行评分。然后，累计每个项目的评分并用于排序。为了让用户免于接触这些复杂原理，TF-Ranking 提供了一个&lt;strong&gt;List-In-List-Out (LILO) API&lt;/strong&gt;，将所有逻辑封装于导出的 TensorFlow 模型内。&lt;/p&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;h3 id=&quot;定义&quot;&gt;定义&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;打分函数&quot;&gt;打分函数&lt;/h3&gt;

&lt;h3 id=&quot;损失函数&quot;&gt;损失函数&lt;/h3&gt;

</content>
 </entry>
 
 <entry>
   <title>pytext</title>
   <link href="http://daiwk.github.io/posts/nlp-pytext.html"/>
   <updated>2018-12-16T00:00:00+00:00</updated>
   <id>/posts/nlp-pytext</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#安装&quot;&gt;安装&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#训练&quot;&gt;训练&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#导出模型&quot;&gt;导出模型&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#c的predictor部分&quot;&gt;c++的predictor部分&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#thrift-api&quot;&gt;thrift api&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#实现server&quot;&gt;实现server&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#编译&quot;&gt;编译&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#部署predictor服务&quot;&gt;部署predictor服务&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

&lt;p&gt;使用python3&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;pytext-nlp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意：&lt;a href=&quot;https://github.com/facebookresearch/pytext/issues/115&quot;&gt;https://github.com/facebookresearch/pytext/issues/115&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;训练&quot;&gt;训练&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pytext train &amp;lt; demo/configs/docnn.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;导出模型&quot;&gt;导出模型&lt;/h2&gt;

&lt;p&gt;训练完了后，可以导出模型&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; ./models
pytext &lt;span class=&quot;nb&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--output-path&lt;/span&gt; ./models/demo.c2 &amp;lt; ./demo/configs/docnn.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;c的predictor部分&quot;&gt;c++的predictor部分&lt;/h2&gt;

&lt;h3 id=&quot;thrift-api&quot;&gt;thrift api&lt;/h3&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;predictor.thrift&lt;/code&gt;如下&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cpp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictor_service&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;service&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Predictor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;// Returns list of scores for each label&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;实现server&quot;&gt;实现server&lt;/h3&gt;

&lt;p&gt;完整代码见&lt;a href=&quot;https://github.com/facebookresearch/pytext/blob/master/demo/predictor_service/server.cpp&quot;&gt;https://github.com/facebookresearch/pytext/blob/master/demo/predictor_service/server.cpp&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PredictorHandler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PredictorIf&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;private:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;NetDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mPredictNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Workspace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mWorkspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;NetDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loadAndInitModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Workspace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modelFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unique_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DBReader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DBReader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;minidb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modelFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metaNetDef&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;runGlobalInitialization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictInitNet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;metaNetDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;PredictorConsts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict_init_net_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;CAFFE_ENFORCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RunNetOnce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictInitNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictNet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NetDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;metaNetDef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;PredictorConsts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict_net_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;CAFFE_ENFORCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CreateNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;PredictorHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;modelFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mWorkspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;workspace&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;mPredictNet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loadAndInitModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mWorkspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modelFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;实现predict函数&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PredictorHandler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PredictorIf&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
  &lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// Pre-process: tokenize input doc&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;docCopy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tokenize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;docCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Feed input to model as tensors&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valTensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorCPUFromValues&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int64_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int64_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;BlobGetMutableTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mWorkspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CreateBlob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;tokens_vals_str:value&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CPU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CopyFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lensTensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorCPUFromValues&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int64_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;BlobGetMutableTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mWorkspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CreateBlob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;tokens_lens&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CPU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CopyFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lensTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Run the model&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;CAFFE_ENFORCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mWorkspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RunNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mPredictNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()));&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Extract and populate results into the response&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mPredictNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;external_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mPredictNet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;external_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scoresTensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mWorkspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetBlob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scoresTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scoresTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;_return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;编译&quot;&gt;编译&lt;/h3&gt;

&lt;p&gt;需要有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;libthrift.so, libcaffe2.so, libprotobuf.so and libc10.so&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;Makefile文件如下&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CPPFLAGS +&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;c++11 &lt;span class=&quot;nt&quot;&gt;-std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;c++14 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt;./gen-cpp &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt;/pytorch &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt;/pytorch/build &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt;/pytorch/aten/src/ &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt;/pytorch/third_party/protobuf/src/
CLIENT_LDFLAGS +&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-lthrift&lt;/span&gt;
SERVER_LDFLAGS +&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt;/pytorch/build/lib &lt;span class=&quot;nt&quot;&gt;-lthrift&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-lcaffe2&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-lprotobuf&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-lc10&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;

server: server.o gen-cpp/Predictor.o
      g++ &lt;span class=&quot;nv&quot;&gt;$^&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;SERVER_LDFLAGS&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$@&lt;/span&gt;

clean:
      &lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.o server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在Dockerfile中，有如下命令&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Copy local files to /app&lt;/span&gt;
COPY &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; /app
WORKDIR /app

&lt;span class=&quot;c&quot;&gt;# Compile app&lt;/span&gt;
RUN thrift &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--gen&lt;/span&gt; cpp predictor.thrift
RUN make

&lt;span class=&quot;c&quot;&gt;# Add library search paths&lt;/span&gt;
RUN &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/pytorch/build/lib/'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/ld.so.conf.d/local.conf
RUN &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/usr/local/lib/'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/ld.so.conf.d/local.conf
RUN ldconfig
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;部署predictor服务&quot;&gt;部署predictor服务&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://pytext-pytext.readthedocs-hosted.com/en/latest/serving_models_in_production.html#test-run-the-server&quot;&gt;https://pytext-pytext.readthedocs-hosted.com/en/latest/serving_models_in_production.html#test-run-the-server&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;可以自己build一个镜像，&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;demo/predictor_service/
docker build &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; predictor_service &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当然也可以用我编好的啦&lt;a href=&quot;https://hub.docker.com/r/daiwk/caffe2&quot;&gt;https://hub.docker.com/r/daiwk/caffe2&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker pull daiwk/caffe2
docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; ~/git_daiwk/pytext/models/:/models &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 8080:8080 daiwk/caffe2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在container中&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/app/server /models/demo.c2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后新开一个窗口直接curl就行：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-G&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;http://localhost:8080&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--data-urlencode&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;doc=Flights from Seattle to San Francisco&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;会得到输出：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;doc_scores:alarm/modify_alarm:-2.13494
doc_scores:alarm/set_alarm:-2.02492
doc_scores:alarm/show_alarms:-2.05924
doc_scores:alarm/snooze_alarm:-2.02332
doc_scores:alarm/time_left_on_alarm:-2.11147
doc_scores:reminder/set_reminder:-2.00476
doc_scores:reminder/show_reminders:-2.21686
doc_scores:weather/find:-2.07725
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>gnn frameworks</title>
   <link href="http://daiwk.github.io/posts/platform-gnn-frameworks.html"/>
   <updated>2018-12-10T00:00:00+00:00</updated>
   <id>/posts/platform-gnn-frameworks</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#graph-netstf&quot;&gt;graph-nets(tf)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#dglmxnetpytorch&quot;&gt;DGL(mxnet+pytorch)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pygpytorch&quot;&gt;PyG(pytorch)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#graphvite&quot;&gt;GraphVite&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pgl&quot;&gt;pgl&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#angel-30&quot;&gt;Angel 3.0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;graph-netstf&quot;&gt;graph-nets(tf)&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/deepmind/graph_nets&quot;&gt;https://github.com/deepmind/graph_nets&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;..好像并没有多少功能。。。&lt;/p&gt;

&lt;h2 id=&quot;dglmxnetpytorch&quot;&gt;DGL(mxnet+pytorch)&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650753378&amp;amp;idx=2&amp;amp;sn=66cd0204e55439476745b751da8ecd61&amp;amp;chksm=871a8d1cb06d040a59581226fabbbfcdc2b70e8adcdfa33ee91662dbd99112cda94408858cc7&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0309z4wr6KBh1FsmzZqQGJxC&amp;amp;pass_ticket=%2BP%2FN5cYeG852O%2FSNu1NE1SPUA8ubUIDrdxe7yapmhw5xuyc6UadTW4Gqxrxrq2TY#rd&quot;&gt;NYU、AWS联合推出：全新图神经网络框架DGL正式发布&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650756252&amp;amp;idx=3&amp;amp;sn=195a715f1e977a342b32137abe0a27d0&amp;amp;chksm=871a90e2b06d19f4c441859a50753466eda9971863ee3a69bcc198c06dc26469df603a4411c8&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0309NNAbBhsEKRdQmH5T9ncP&amp;amp;pass_ticket=%2BP%2FN5cYeG852O%2FSNu1NE1SPUA8ubUIDrdxe7yapmhw5xuyc6UadTW4Gqxrxrq2TY#rd&quot;&gt;专栏 | 手把手教你用DGL框架进行批量图分类&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650761921&amp;amp;idx=3&amp;amp;sn=6ae05951d1f1dc119e3efabcdaee80a0&amp;amp;chksm=871aaebfb06d27a9f4c3cbde3fc0220959987ac41e7985be74de71b20614c47280dfeb2186d0&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=OEoJxI2kFvfmi6pDQlY3W%2FGC2MeNgyiIRuMCWgKgSHf5DYmZLcpg4jkhV1VOz5EE#rd&quot;&gt;性能提升19倍，DGL重大更新支持亿级规模图神经网络训练&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pygpytorch&quot;&gt;PyG(pytorch)&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650758310&amp;amp;idx=1&amp;amp;sn=64655e53fddb1f9492b8b6a1fe3a382b&amp;amp;chksm=871a98d8b06d11ce8292442045b293b2819f5057b726e594a1896ea7c2ae0355949e9df7bd03&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=%2BP%2FN5cYeG852O%2FSNu1NE1SPUA8ubUIDrdxe7yapmhw5xuyc6UadTW4Gqxrxrq2TY#rd&quot;&gt;比DGL快14倍：PyTorch图神经网络库PyG上线了&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;graphvite&quot;&gt;GraphVite&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU1NTUxNTM0Mg==&amp;amp;mid=2247491610&amp;amp;idx=1&amp;amp;sn=1188f6e0707031ee0a58ed10b89cb9d8&amp;amp;chksm=fbd18cbbcca605addf50058077ad5689bf3182b92e7302dacb6c55b2761bd518e33435848827&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1565179278897&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;已开源！GraphVite 超高速图表示学习系统，1 分钟可学百万节点&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;单机支持最大20亿边的图。&lt;/p&gt;

&lt;p&gt;GraphVite 框架由两个部分组成，核心库和 Python wrapper。Python wrapper 可以为核心库中的类提供自动打包功能，并为应用程序和数据集提供了实现。&lt;/p&gt;

&lt;p&gt;核心库用 C+11 和 CUDA 实现，并使用 pybind11 绑定到 python 中。它涵盖了 GraphVite 中所有与计算相关类的实现，例如图、求解器和优化器。所有这些成分都可以打包成类，这类似于 Python 接口。&lt;/p&gt;

&lt;p&gt;在 C+实现中，Python 有一些不同之处。图和求解器由底层数据类型和嵌入向量长度实现。该设计支持 Python 接口中的动态数据类型，以及对最大化优化编译时（compile-time）。为了方便了对 GraphVite 的进一步开发，开发者还对 C+接口进行了高度抽象。通过连接核心接口，用户可以实现图形的深度学习例程，而无需关注调度细节。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;include/base/实现基本数据结构&lt;/li&gt;
  &lt;li&gt;include/util/实现基本用途&lt;/li&gt;
  &lt;li&gt;include/core/实现优化器、图和求解器的核心接口&lt;/li&gt;
  &lt;li&gt;include/gpu/实现所有模型的前向和后向传播&lt;/li&gt;
  &lt;li&gt;include/instance/实现图和求解器的实例&lt;/li&gt;
  &lt;li&gt;include/bind.h 实现Python绑定&lt;/li&gt;
  &lt;li&gt;src/graphvite.cu 实例化所有Python类&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://graphvite.io/&quot;&gt;https://graphvite.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/DeepGraphLearning/graphvite&quot;&gt;https://github.com/DeepGraphLearning/graphvite&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pgl&quot;&gt;pgl&lt;/h2&gt;

&lt;p&gt;paddle的graph learning&lt;/p&gt;

&lt;p&gt;可以从这里搞些examples来试试：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/PGL&quot;&gt;https://github.com/PaddlePaddle/PGL&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;安装不赘述了，还有官方文档：&lt;a href=&quot;https://pgl.readthedocs.io/en/latest/instruction.html&quot;&gt;https://pgl.readthedocs.io/en/latest/instruction.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;看看这个demo：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pgl&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pgl&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# import pgl module
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build_graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# define the number of nodes; we can use number to represent every node
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;num_node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# add edges, we represent all edges as a list of tuple (src, dst)
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;edge_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
             &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
             &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Each node can be represented by a d-dimensional feature vector, here for simple, the feature vectors are randomly generated.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;float32&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# each edge also can be represented by a feature vector
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;edge_feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edge_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;float32&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# create a graph
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_nodes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;edges&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edge_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;node_feat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'feature'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;edge_feat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'edge_feature'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edge_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# create a graph object for saving graph data
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;build_graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'There are %d nodes in the graph.'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_nodes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'There are %d edges in the graph.'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_edges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Out:
# There are 10 nodes in the graph.
# There are 14 edges in the graph.
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;paddle.fluid&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;use_cuda&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;place&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUPlace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_cuda&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CPUPlace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# use GraphWrapper as a container for graph data to construct a graph neural network
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pgl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph_wrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GraphWrapper&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'graph'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;place&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;node_feat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_feat_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;



&lt;span class=&quot;c1&quot;&gt;# define GCN layer function
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gcn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# gw is a GraphWrapper；feature is the feature vectors of nodes
&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# define message function
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dst_feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edge_feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# In this tutorial, we return the feature vector of the source node as message
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'h'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# define reduce function
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;recv_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# we sum the feature vector of the source node
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence_pool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pool_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sum'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# trigger message to passing
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nfeat_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'h'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# recv funciton receives message and trigger reduce funcition to handle message
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recv_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gcn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_feat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'feature'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'gcn_layer_1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gcn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'gcn_layer_2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;float32&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expand_dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# create a label layer as a container
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;node_label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;float32&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;append_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# using cross-entropy with sigmoid layer as the loss function
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid_cross_entropy_with_logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# calculate the mean loss
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# choose the Adam optimizer and set the learning rate to be 0.01
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adam&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Adam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;adam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# create the executor
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Executor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_startup_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_feed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# gets graph data
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'node_label'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;train_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_main_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fetch_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;return_numpy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Epoch %d | Loss: %f'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;angel-30&quot;&gt;Angel 3.0&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/bSpm72WIx061cFMFgkaMlw&quot;&gt;腾讯开源全栈机器学习平台 Angel 3.0，支持三大类型图计算算法&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/Angel-ML/angel&quot;&gt;https://github.com/Angel-ML/angel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;腾讯Angel在稀疏数据高维模型的训练上具有独特优势，擅长推荐模型和图网络模型相关领域。当前业界主流的大规模图计算系统主要有Facebook的Big Graph、Power graph、Data bricks的 Spark GraphX等，但这些系统并不都支持图挖掘、图表示学习、图神经网络的三大类型算法。&lt;/p&gt;

&lt;p&gt;从性能上来看，Angel优于现有图计算系统，能够支持十亿级节点、千亿级边的传统图挖掘算法，以及百亿边的图神经网络算法需求。Angel可运行于多任务集群以及公有云环境，具备高效容错恢复机制，能够进行端到端的训练，新算法容易支持，同时，Angel能够支持图挖掘、图表示、图神经网络算法，具备图学习的能力。&lt;/p&gt;

&lt;p&gt;Angel的PS是针对高维稀疏模型设计的, 而大图是非常高维、有多达十亿的节点，也是稀疏的, 因此PS架构也适合处理图数据。图算法有多种类型，如图挖掘算法、图表示学习、图神经网络。由于Angel的PS有自定义接口, 可以灵活地应对这几类算法，整个平台不需要改动，只要实现所需接口即可。关于可靠性问题，Angel从一开始就是针对共享集群、公有云环境设计的, 并与Spark的结合. Spark也具有很强的稳定性。易用性主要指与上下游是否完整配套。Spark On Angel可以与大数据处理结合，PyTorch On Angel可以跟深度学习结合，将把大数据计算、深度学习统一起来，用户不用借助第三方平台就能完成整个流程, 易用性好。&lt;/p&gt;

&lt;p&gt;Angel可以运行在Yarn/Kubernetes环境上，它上面现在支持三类算法&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;图挖掘: PageRank、Kcore、Closeness，共同好友、三角结构、社团发现、其他；&lt;/li&gt;
  &lt;li&gt;图神经网络: GCN、GraphSage、DGI等神经网络算法；&lt;/li&gt;
  &lt;li&gt;图表示学习: LINE、Node2Vec算法。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;图算法比较多，先将这些算法分类，每一类采取不同的优化方式去实现和优化。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一类是&lt;strong&gt;三角结构&lt;/strong&gt;类，数三角形。这类算法是&lt;strong&gt;暴力算法&lt;/strong&gt;, 没有捷径可走。例如共同好友就是三角结构。基于三角结构可以实现一系列算法, 如Cluster Rank, Clustering coefficient, Ego Network.&lt;/li&gt;
  &lt;li&gt;第二类算法是&lt;strong&gt;连通分量&lt;/strong&gt;，有WCC和SCC。这类算法核心的思想是要做图的&lt;strong&gt;折叠&lt;/strong&gt;或者图的&lt;strong&gt;压缩&lt;/strong&gt;。这类算法有一定的捷径可走，发现连通结点后，就可以进行合并,迭代时图在会不断变小，就可以加快迭代速度。&lt;/li&gt;
  &lt;li&gt;第三类算法是节点的排序。比如PageRank、KCore、Closeness，这类算法的迭代轮数较多，可能好几百轮。有一些方法加速它，主要有两种，一种是让它的&lt;strong&gt;迭代变得少一点&lt;/strong&gt;，另一种是让它&lt;strong&gt;每一轮迭代越来越快&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;第四类算法是&lt;strong&gt;图表示&lt;/strong&gt;学习的算法, 也是没有捷径可走, 主要考虑一些图的划分策略。像GNN，也归为一类&lt;/li&gt;
  &lt;li&gt;最后一类算法是&lt;strong&gt;图神经网络&lt;/strong&gt;。图有很多节点，每个节点都有自己的特征。经过一层层的图卷积，每个节点上的特征就输出一个表示，再经过一层图卷积，又输出另外一层表示，不断的改变图每一个节点的表示，最后根据任务类型需求，对每一个节点的表示把它都加起来，再做softmax，对&lt;strong&gt;全图&lt;/strong&gt;做分类。对任何两个节点，算他们俩俩相交，计算它们的概率，预测它们俩是不是有边。它的核心是一个图，一层卷积，两层卷积，然后输出。图神经网络的问题是图数据规模比较大，需要做深度学习。&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>seqgan</title>
   <link href="http://daiwk.github.io/posts/rl-seqgan.html"/>
   <updated>2018-12-02T00:00:00+00:00</updated>
   <id>/posts/rl-seqgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot;&gt;概述&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#seqgan-via-policy-gradient&quot;&gt;SeqGAN via Policy Gradient&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#生成器相关&quot;&gt;生成器相关&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#判别器相关&quot;&gt;判别器相关&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#算法流程&quot;&gt;算法流程&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#the-generative-model-for-sequences&quot;&gt;The Generative Model for Sequences&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#the-discriminative-model-for-sequences&quot;&gt;The Discriminative Model for Sequences&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1609.05473&quot;&gt;SeqGAN: Sequence Generative Adversarial Nets with Policy Gradient&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jianshu.com/p/e1b87286bfae&quot;&gt;https://www.jianshu.com/p/e1b87286bfae&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/36880287&quot;&gt;https://zhuanlan.zhihu.com/p/36880287&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;GAN 在生成&lt;strong&gt;连续离散序列&lt;/strong&gt;时会遇到两个问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一是因为生成器的输出是离散的，&lt;strong&gt;梯度更新&lt;/strong&gt;从&lt;strong&gt;判别器传到生成器&lt;/strong&gt;比较困难；&lt;/li&gt;
  &lt;li&gt;二是&lt;strong&gt;判别器&lt;/strong&gt;只有当&lt;strong&gt;序列被完全生成后&lt;/strong&gt;才能进行判断，但此刻&lt;strong&gt;指导用处已不太大&lt;/strong&gt;，而如果&lt;strong&gt;生成器生成序列的同时判别器来判断&lt;/strong&gt;，&lt;strong&gt;如何平衡当前序列的分数和未来序列的分数&lt;/strong&gt;又是一个难题。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在这篇论文中：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;将生成器看作是强化学习中的&lt;strong&gt;随机policy&lt;/strong&gt;，这样就可以&lt;strong&gt;直接通过gradient policy&lt;/strong&gt;的更新来避免生成器中的求导问题。&lt;/li&gt;
  &lt;li&gt;判别器对&lt;strong&gt;整个序列的评分&lt;/strong&gt;作为强化学习的reward信号可以通过&lt;strong&gt;Monte Carlo搜索&lt;/strong&gt;传递到序列生成的&lt;strong&gt;中间时刻&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/seqgan.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;G和D的定义如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;通过生成器&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{\theta}\)&lt;/code&gt;产生序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y_{1 : T}=\left(y_{1}, \ldots, y_{t}, \ldots, y_{T}\right), y_{t} \in \mathcal{Y}\)&lt;/code&gt;，其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal{Y}\)&lt;/code&gt;是整个词表。在时间步&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;时，
    &lt;ul&gt;
      &lt;li&gt;状态是前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t-1\)&lt;/code&gt;的序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left(y_{1}, \ldots, y_{t-1}\right)\)&lt;/code&gt;，&lt;/li&gt;
      &lt;li&gt;动作是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_{t}\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{\theta}\left(y_{t} | Y_{1 : t-1}\right)\)&lt;/code&gt;是随机策略&lt;/li&gt;
      &lt;li&gt;状态转移是确定性的&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;通过判别器&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D_{\phi}\left(Y_{1 : T}\right)\)&lt;/code&gt;判断序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y_{1 : T}\)&lt;/code&gt;是真实序列的概率。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;训练过程如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;通过正样本（真实序列）和负样本（生成的序列）来训练判别器&lt;/li&gt;
  &lt;li&gt;基于判别器给出的reward，通过policy gradient和MC搜索来训练生成器。其中的reward是通过生成器能欺骗判别器的likelihood来估计的。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;seqgan-via-policy-gradient&quot;&gt;SeqGAN via Policy Gradient&lt;/h3&gt;

&lt;h4 id=&quot;生成器相关&quot;&gt;生成器相关&lt;/h4&gt;

&lt;p&gt;对于生成器&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{\theta}\left(y_{t} | Y_{1 : t-1}\right)\)&lt;/code&gt;来讲，其实没有中间的reward，只有整个sequence生成之后的final reward，所以目标就是最大化：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
J(\theta)=\mathbb{E}\left[R_{T} | s_{0}, \theta\right]=\sum_{y_{1} \in \mathcal{Y}} G_{\theta}\left(y_{1} | s_{0}\right) \cdot Q_{D_{\phi}}^{G_{\theta}}\left(s_{0}, y_{1}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;上式中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R_{T}\)&lt;/code&gt;是完整的长度为T的序列的reward。&lt;/p&gt;

&lt;p&gt;而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q_{D_{\phi}}^{G_{\theta}}(s, a)\)&lt;/code&gt;是一个序列的action-value function。指的是从状态s开始，执行动作a，再&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{\theta}\)&lt;/code&gt;，再得到的累积期望reward。&lt;/p&gt;

&lt;p&gt;对于倒数第二个状态，执行一步就是长度为T的序列了，那么，这个reward就是判别器的输出：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q_{D_{\phi}}^{G_{\theta}}\left(a=y_{T}, s=Y_{1 : T-1}\right)=D_{\phi}\left(Y_{1 : T}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而对于中间状态呢，就要用MC了。&lt;/p&gt;

&lt;p&gt;先看下面这个式子，给定一个长度为t-1的序列，使用带有roll-out policy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{\beta}\)&lt;/code&gt;的MC搜索采样N次，每次采样T-t个token，得到一个长度为T的序列。所以产生的就是N个长度为T的序列。在这里，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{\beta}\)&lt;/code&gt;和生成器&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_\theta\)&lt;/code&gt;一样。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\left\{Y_{1 : T}^{1}, \ldots, Y_{1 : T}^{N}\right\}=\mathrm{MC}^{G_{\beta}}\left(Y_{1 : t} ; N\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;于是，t&amp;lt;T时，就是采样出N个长度为T的结果，然后每个结果算一下D，再对这N个结果取平均&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q_{D_{\phi}}^{G_{\theta}}\left(s=Y_{1 : t-1}, a=y_{t}\right)=\left\{\begin{array}{ll}{\frac{1}{N} \sum_{n=1}^{N} D_{\phi}\left(Y_{1 : T}^{n}\right), Y_{1 : T}^{n} \in \mathrm{MC}^{G_{\beta}}\left(Y_{1 : t} ; N\right)} &amp;amp; {\text { for } t&amp;lt;T} \\ {D_{\phi}\left(Y_{1 : t}\right)} &amp;amp; {\text { for } t=T}\end{array}\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后生成器的目标函数的导数就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\nabla_{\theta} J(\theta)=\sum_{t=1}^{T} \mathbb{E}_{Y_{1 : t-1} \sim G_{\theta}}\left[\sum_{y_{t} \in \mathcal{Y}} \nabla_{\theta} G_{\theta}\left(y_{t} | Y_{1 : t-1}\right) \cdot Q_{D_{\phi}}^{G_{\theta}}\left(Y_{1 : t-1}, y_{t}\right)\right]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;近似一下就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{array}{l}{\nabla_{\theta} J(\theta) \simeq \sum_{t=1}^{T} \sum_{y_{t} \in \mathcal{Y}} \nabla_{\theta} G_{\theta}\left(y_{t} | Y_{1 : t-1}\right) \cdot Q_{D_{\phi}}^{G_{\theta}}\left(Y_{1 : t-1}, y_{t}\right)} \\ {=\sum_{t=1}^{T} \sum_{y_{t} \in \mathcal{Y}} G_{\theta}\left(y_{t} | Y_{1 : t-1}\right) \nabla_{\theta} \log G_{\theta}\left(y_{t} | Y_{1 : t-1}\right) \cdot Q_{D_{\phi}}^{G_{\theta}}\left(Y_{1 : t-1}, y_{t}\right)} \\ {=\sum_{t=1}^{T} \mathbb{E}_{y_{t} \sim G_{\theta}\left(y_{t} | Y_{1 : t-1}\right)}\left[\nabla_{\theta} \log G_{\theta}\left(y_{t} | Y_{1 : t-1}\right) \cdot Q_{D_{\phi}}^{G_{\theta}}\left(Y_{1 : t-1}, y_{t}\right)\right]}\end{array}    
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y_{1 : t-1}\)&lt;/code&gt;是从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{\theta}\)&lt;/code&gt;里sample出来的observed的中间状态。然后通过上面这个梯度来对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;进行梯度下降更新。&lt;/p&gt;

&lt;h4 id=&quot;判别器相关&quot;&gt;判别器相关&lt;/h4&gt;

&lt;p&gt;训练判别器使用的损失函数则是&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\min _{\phi}-\mathbb{E}_{Y \sim p_{\text {data }}}\left[\log D_{\phi}(Y)\right]-\mathbb{E}_{Y \sim G_{\theta}}\left[\log \left(1-D_{\phi}(Y)\right)\right]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;把负号取出来，第一项就是要让真实分布采样出来的最大，第二项就是让从G出来的最小也就是1-D最大。&lt;/p&gt;

&lt;h4 id=&quot;算法流程&quot;&gt;算法流程&lt;/h4&gt;

&lt;h3 id=&quot;the-generative-model-for-sequences&quot;&gt;The Generative Model for Sequences&lt;/h3&gt;

&lt;p&gt;使用rnn&lt;/p&gt;

&lt;h3 id=&quot;the-discriminative-model-for-sequences&quot;&gt;The Discriminative Model for Sequences&lt;/h3&gt;

&lt;p&gt;使用cnn&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Horizon</title>
   <link href="http://daiwk.github.io/posts/platform-horizon.html"/>
   <updated>2018-11-01T00:00:00+00:00</updated>
   <id>/posts/platform-horizon</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/cDfuasM7CzQxIefjFNNt6Q&quot;&gt;资源 | Facebook开源首个适应大规模产品的强化学习平台Horizon，基于PyTorch 1.0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码地址：&lt;a href=&quot;https://github.com/facebookresearch/Horizon&quot;&gt;https://github.com/facebookresearch/Horizon&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/abs/1811.00260&quot;&gt;Horizon: Facebook’s Open Source Applied Reinforcement Learning Platform&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>adanet</title>
   <link href="http://daiwk.github.io/posts/platform-adanet.html"/>
   <updated>2018-11-01T00:00:00+00:00</updated>
   <id>/posts/platform-adanet</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#快速易用&quot;&gt;快速易用&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#学习保证&quot;&gt;学习保证&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#可扩展&quot;&gt;可扩展&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650751000&amp;amp;idx=2&amp;amp;sn=62814e8e19f53655bc75c83f84b0b9e1&amp;amp;chksm=871a8466b06d0d701ce5a35c4ee13eeeab5f9a155bd28474aa745b3938099df7e35d43ee754d&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1101yG3KXEYIqzbTknSmIRQu&amp;amp;pass_ticket=zX4VgptyVP3Mvtqcew6Og6Jwwl4pq45hepOQUq59s21SZj%2BEuwQXeeT1wVMIDy0b#rd&quot;&gt;资源 | 谷歌开源AdaNet：基于TensorFlow的AutoML框架&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;相关论文： &lt;a href=&quot;http://proceedings.mlr.press/v70/cortes17a/cortes17a.pdf&quot;&gt;AdaNet: Adaptive Structural Learning of Artificial Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Github 项目地址：&lt;a href=&quot;https://github.com/tensorflow/adanet&quot;&gt;https://github.com/tensorflow/adanet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;教程 notebook：&lt;a href=&quot;https://github.com/tensorflow/adanet/tree/v0.1.0/adanet/examples/tutorials&quot;&gt;https://github.com/tensorflow/adanet/tree/v0.1.0/adanet/examples/tutorials&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;基于 TensorFlow 的轻量级框架 AdaNet，该框架可以使用少量专家干预来自动学习高质量模型。AdaNet 在谷歌近期的强化学习和基于进化的 AutoML 的基础上构建，快速灵活同时能够提供学习保证（learning guarantee）。重要的是，AdaNet 提供通用框架，不仅能用于学习神经网络架构，还能学习集成架构以获取更好的模型。&lt;/p&gt;

&lt;p&gt;AdaNet 易于使用，能够创建高质量模型，节省 ML 从业者在选择最优神经网络架构上所花费的时间，实现学习神经架构作为集成子网络的自适应算法。AdaNet 能够添加不同深度、宽度的子网络，从而创建不同的集成，并在性能改进和参数数量之间进行权衡。&lt;/p&gt;

&lt;h3 id=&quot;快速易用&quot;&gt;快速易用&lt;/h3&gt;

&lt;p&gt;AdaNet 实现了 TensorFlow Estimator 接口，通过压缩训练、评估、预测和导出极大地简化了机器学习编程。它整合如 TensorFlow Hub modules、TensorFlow Model Analysis、Google Cloud』s Hyperparameter Tuner 这样的开源工具。它支持分布式训练，极大减少了训练时间，使用可用 CPU 和加速器（例如 GPU）实现线性扩展。&lt;/p&gt;

&lt;p&gt;AdaNet 将 TensorBoard 无缝集成，以监控子网络的训练、集成组合和性能。AdaNet 完成训练后将导出一个 SavedModel，可使用 TensorFlow Serving 进行部署。&lt;/p&gt;

&lt;h3 id=&quot;学习保证&quot;&gt;学习保证&lt;/h3&gt;

&lt;p&gt;构建神经网络集成存在多个挑战：最佳子网络架构是什么？重复使用同样的架构好还是鼓励差异化好？虽然具备更多参数的复杂子网络在训练集上表现更好，但也因其极大的复杂性它们难以泛化到未见过的数据上。这些挑战源自对模型性能的评估。我们可以在训练集分留出的数据集上评估模型表现，但是这么做会降低训练神经网络的样本数量。&lt;/p&gt;

&lt;p&gt;不同的是，AdaNet 的方法是优化一个目标函数，在神经网络集成在训练集上的表现与泛化能力之间进行权衡。直观上，即仅在候选子网络改进网络集成训练损失的程度超过其对泛化能力的影响时，选择该候选子网络。这保证了：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;集成网络的泛化误差受训练误差和复杂度的约束。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;通过优化这一目标函数，能够直接最小化这一约束。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;优化这一目标函数的实际收益是它能减少选择哪个候选子网络加入集成时对留出数据集的需求。另一个益处是允许使用更多训练数据来训练子网络。&lt;/p&gt;

&lt;h3 id=&quot;可扩展&quot;&gt;可扩展&lt;/h3&gt;

&lt;p&gt;谷歌认为，创建有用的 AutoML 框架的关键是：研究和产品使用方面不仅能够提供合理的默认设置，还要让用户尝试自己的子网络/模型定义。这样，机器学习研究者、从业者、喜爱者都能够使用 tf.layers 这样的 API 定义自己的 AdaNet adanet.subnetwork.Builder。&lt;/p&gt;

&lt;p&gt;已在自己系统中融合 TensorFlow 模型的用户可以轻松将 TensorFlow 代码转换到 AdaNet 子网络中，并使用 adanet.Estimator 来提升模型表现同时获取学习保证。AdaNet 将探索他们定义的候选子网络搜索空间，并学习集成这些子网络。例如，采用 NASNet-A CIFAR 架构的开源实现，把它迁移到一个子网络，经过 8 次 AdaNet 迭代后提高其在 CIFAR-10 上的当前最优结果。&lt;/p&gt;

&lt;p&gt;通过固定或自定义 tf.contrib.estimator.Heads，用户可以使用自己定义的损失函数作为 AdaNet 目标函数的一部分来训练回归、分类和多任务学习问题。&lt;/p&gt;

&lt;p&gt;用户也可以通过拓展 adanet.subnetwork.Generator 类别，完全定义要探索的候选子网络搜索空间。这使得用户能够基于硬件扩大或缩小搜索空间范围。子网络的搜索空间可以简单到复制具备不同随机种子的同一子网络配置，从而训练数十种具备不同超参数组合的子网络，并让 AdaNet 选择其中一个进入最终的集成模型。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>bert代码</title>
   <link href="http://daiwk.github.io/posts/nlp-bert-code.html"/>
   <updated>2018-11-01T00:00:00+00:00</updated>
   <id>/posts/nlp-bert-code</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#pretrained-model&quot;&gt;pretrained model&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#sentence-and-sentence-pair-classification-tasks&quot;&gt;Sentence (and sentence-pair) classification tasks&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#glue-data%e6%95%b0%e6%8d%ae%e9%9b%86&quot;&gt;glue data数据集&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%bf%90%e8%a1%8c&quot;&gt;运行&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pretraining&quot;&gt;pretraining&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#step1-create-pretraining-data&quot;&gt;step1. create-pretraining-data&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#step2-run-pretraining&quot;&gt;step2. run-pretraining&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pretrain-tips-and-caveats&quot;&gt;pretrain tips and caveats&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%8a%bd%e5%8f%96feature-vector%e7%b1%bb%e4%bc%bcelmo&quot;&gt;抽取feature vector(类似ELMo)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%87%aa%e5%b7%b1%e5%b0%9d%e8%af%95&quot;&gt;自己尝试&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%b3%a8%e6%84%8f&quot;&gt;注意&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;北京时间10.31 23时许，bert官方版代码正式出炉~&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/google-research/bert&quot;&gt;https://github.com/google-research/bert&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原文的解读和pytorch版本的解读参考：&lt;a href=&quot;https://daiwk.github.io/posts/nlp-bert.html&quot;&gt;https://daiwk.github.io/posts/nlp-bert.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考参考机器之心发的&lt;a href=&quot;https://mp.weixin.qq.com/s/vFdm-UHns7Nhbmdoiu6jWg&quot;&gt;谷歌终于开源BERT代码：3 亿参数量，机器之心全面解读&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码结构：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;`-- bert
    |-- CONTRIBUTING.md
    |-- create_pretraining_data.py
    |-- extract_features.py
    |-- __init__.py
    |-- LICENSE
    |-- modeling.py
    |-- modeling_test.py
    |-- optimization.py
    |-- optimization_test.py
    |-- README.md
    |-- run_classifier.py
    |-- run_pretraining.py
    |-- run_squad.py
    |-- sample_text.txt
    |-- tokenization.py
    `-- tokenization_test.py

1 directory, 16 files
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;pretrained-model&quot;&gt;pretrained model&lt;/h2&gt;

&lt;p&gt;有这几个版本（在进行WordPiece分词之前是否区分大小写：是：cased，否：uncased(即全部转为小写)）：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;BERT-Base, Uncased: 12-layer, 768-hidden, 12-heads, 110M parameters&lt;/li&gt;
  &lt;li&gt;BERT-Large, Uncased: 24-layer, 1024-hidden, 16-heads, 340M parameters&lt;/li&gt;
  &lt;li&gt;BERT-Base, Cased: 12-layer, 768-hidden, 12-heads , 110M parameters&lt;/li&gt;
  &lt;li&gt;BERT-Large, Cased: 24-layer, 1024-hidden, 16-heads, 340M parameters (Not available yet. Needs to be re-generated).&lt;/li&gt;
  &lt;li&gt;BERT-Base, Multilingual: 102 languages, 12-layer, 768-hidden, 12-heads, 110M parameters&lt;/li&gt;
  &lt;li&gt;BERT-Base, Chinese: Chinese Simplified and Traditional, 12-layer, 768-hidden, 12-heads, 110M parameters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;每个zip中包含如下三个文件：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一个TensorFlow checkpoint (bert_model.ckpt) ：包含pre-trained weights（里面有3个文件）&lt;/li&gt;
  &lt;li&gt;一个vocab文件(vocab.txt)：将WordPiece映射成word id&lt;/li&gt;
  &lt;li&gt;一个config file (bert_config.json) ：存储hyperparameters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;uncased_L-12_H-768_A-12
|-- bert_config.json
|-- bert_model.ckpt.data-00000-of-00001
|-- bert_model.ckpt.index
|-- bert_model.ckpt.meta
|-- checkpoint
&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; vocab.txt

0 directories, 6 files
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;sentence-and-sentence-pair-classification-tasks&quot;&gt;Sentence (and sentence-pair) classification tasks&lt;/h2&gt;

&lt;h3 id=&quot;glue-data数据集&quot;&gt;glue data数据集&lt;/h3&gt;

&lt;p&gt;下载glue数据，使用&lt;a href=&quot;https://gist.github.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e&quot;&gt;https://gist.github.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e&lt;/a&gt;的py，执行【记住要是python3!!!!!】。。不过在墙内好像怎么都下不下来。。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python download_glue_data.py &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt; glue_data &lt;span class=&quot;nt&quot;&gt;--tasks&lt;/span&gt; all
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;官方文档：&lt;a href=&quot;https://github.com/nyu-mll/GLUE-baselines&quot;&gt;https://github.com/nyu-mll/GLUE-baselines&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如果是国内，先把这个clone下来：&lt;a href=&quot;https://github.com/wasiahmad/paraphrase_identification&quot;&gt;https://github.com/wasiahmad/paraphrase_identification&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python download_glue_data.py &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt; glue_data &lt;span class=&quot;nt&quot;&gt;--tasks&lt;/span&gt; all &lt;span class=&quot;nt&quot;&gt;--path_to_mrpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;paraphrase_identification/dataset/msr-paraphrase-corpus
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意，如果要用glove，从&lt;a href=&quot;https://nlp.stanford.edu/projects/glove/&quot;&gt;https://nlp.stanford.edu/projects/glove/&lt;/a&gt;下载下来的840B版本的zip就有2G多，直接unzip解压不了。。可以&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;7z x glove.840B.300d.zip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后就很风骚。。。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;7z x glove.840B.300d.zip

7-Zip &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;64] 16.02 : Copyright &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;locale&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;en_US,Utf16&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;on,HugeFiles&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;on,64 bits,56 CPUs x64&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

Scanning the drive &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;archives:
1 file, 2176768927 bytes &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;2076 MiB&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

Extracting archive: glove.840B.300d.zip
&lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;
Path &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; glove.840B.300d.zip
Type &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; zip
Physical Size &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2176768927
64-bit &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; +

Everything is Ok

Size:       5646236541
Compressed: 2176768927
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/nyu-mll/GLUE-baselines&quot;&gt;https://github.com/nyu-mll/GLUE-baselines&lt;/a&gt;，装allennlp==0.7.0，torch&amp;gt;=0.4.1，可以跑glue数据集的baseline：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/home/xxx/python-3-tf-cpu/bin/python3.6
&lt;span class=&quot;nb&quot;&gt;alias &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;superhead&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'/opt/compiler/gcc-4.8.2/lib/ld-linux-x86-64.so.2 --library-path /opt/compiler/gcc-4.8.2/lib:$LD_LIBRARY_PATH '&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;alias &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;python&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'superhead $py'&lt;/span&gt;
python main.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--exp_dir&lt;/span&gt; EXP_DIR &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--run_dir&lt;/span&gt; RUN_DIR &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--train_tasks&lt;/span&gt; all &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--cove&lt;/span&gt; 0 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--cuda&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--eval_tasks&lt;/span&gt; all &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--glove&lt;/span&gt; 1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--word_embs_file&lt;/span&gt; ./emb_dir/glove.840B.300d.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;运行&quot;&gt;运行&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;BERT_BASE_DIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/path/to/bert/uncased_L-12_H-768_A-12
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;GLUE_DIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/path/to/glue

python run_classifier.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--task_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;MRPC &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--do_train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--do_eval&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$GLUE_DIR&lt;/span&gt;/MRPC &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--vocab_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/vocab.txt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--bert_config_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/bert_config.json &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--init_checkpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/bert_model.ckpt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--train_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;32 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2e-5 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--num_train_epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3.0 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--output_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/mrpc_output/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;***** Eval results *****
  eval_accuracy = 0.845588
  eval_loss = 0.505248
  global_step = 343
  loss = 0.505248
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;表示dev set上有84.55%的准确率，像MRPC（glue_data中的一个数据集）这样的小数据集，即使从pretrained的checkpoint开始，仍然可能在dev set的accuracy上会有很高的variance（跑多次，可能结果在84-88%之间）。&lt;/p&gt;

&lt;h2 id=&quot;pretraining&quot;&gt;pretraining&lt;/h2&gt;

&lt;h3 id=&quot;step1-create-pretraining-data&quot;&gt;step1. create-pretraining-data&lt;/h3&gt;

&lt;p&gt;paper的源码是用c++写的，这里用py又实现了一遍。。实现masked lm和next sentence prediction。&lt;/p&gt;

&lt;p&gt;输入文件的格式：一行一句话（对于next sentence prediction这很重要），不同文档间用空行分隔。例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sample_text.txt&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Something glittered in the nearest red pool before him.
Gold, surely!
But, wonderful to relate, not an irregular, shapeless fragment of crude ore, fresh from Nature's crucible, but a bit of jeweler's handicraft in the form of a pla
in gold ring.
Looking at it more attentively, he saw that it bore the inscription, &quot;May to Cass.&quot;
Like most of his fellow gold-seekers, Cass was superstitious.

The fountain of classic wisdom, Hypatia herself.
As the ancient sage--the name is unimportant to a monk--pumped water nightly that he might study by day, so I, the guardian of cloaks and parasols, at the sacred
 doors of her lecture-room, imbibe celestial knowledge.
From my youth I felt in me a soul above the matter-entangled herd.
She revealed to me the glorious fact, that I am a spark of Divinity itself.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出是一系列的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TFRecord&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.train.Example&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;注意：这个脚本把整个输入文件都放到内存里了，所以对于大文件，你可能需要把文件分片，然后跑多次这个脚本，得到一堆&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf_examples.tf_record*&lt;/code&gt;，然后把这些文件都作为下一个脚本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run_pretraining.py&lt;/code&gt;的输入。&lt;/p&gt;

&lt;p&gt;参数：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;max_predictions_per_seq：每个序列里最大的masked lm predictions。建议设置为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_seq_length*masked_lm_prob&lt;/code&gt;（这个脚本不会自动设置）&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python create_pretraining_data.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--input_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./sample_text.txt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--output_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/tf_examples.tfrecord &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--vocab_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/vocab.txt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--do_lower_case&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;True &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--masked_lm_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.15 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--random_seed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;12345 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--dupe_factor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;INFO:tensorflow:*** Example ***
INFO:tensorflow:tokens: [CLS] indeed , it was recorded in [MASK] star that a fortunate early [MASK] ##r had once picked up on the highway a solid chunk [MASK] gold quartz which the [MASK] had freed from its inc [MASK] ##ing soil , and washed into immediate and [MASK] popularity . [SEP] rainy season , [MASK] insult show habit of body , and seldom lifted their eyes to the rift ##ed [MASK] india - ink washed skies [MASK] them . &quot; cass &quot; beard [MASK] elliot early that morning , but not with a view to [MASK] . a leak in his [MASK] roof , - - quite [MASK] with his careless , imp ##rov ##ide ##nt habits , - - had rouse ##d him at 4 a [MASK] m [SEP]
INFO:tensorflow:input_ids: 101 5262 1010 2009 2001 2680 1999 103 2732 2008 1037 19590 2220 103 2099 2018 2320 3856 2039 2006 1996 3307 1037 5024 20000 103 2751 20971 2029 1996 103 2018 10650 2013 2049 4297 103 2075 5800 1010 1998 8871 2046 6234 1998 103 6217 1012 102 16373 2161 1010 103 15301 2265 10427 1997 2303 1010 1998 15839 4196 2037 2159 2000 1996 16931 2098 103 2634 1011 10710 8871 15717 103 2068 1012 1000 16220 1000 10154 103 11759 2220 2008 2851 1010 2021 2025 2007 1037 3193 2000 103 1012 1037 17271 1999 2010 103 4412 1010 1011 1011 3243 103 2007 2010 23358 1010 17727 12298 5178 3372 14243 1010 1011 1011 2018 27384 2094 2032 2012 1018 1037 103 1049 102
INFO:tensorflow:input_mask: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
INFO:tensorflow:segment_ids: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
INFO:tensorflow:masked_lm_positions: 7 12 13 25 30 36 45 52 53 54 68 74 81 82 93 99 103 105 125 0
INFO:tensorflow:masked_lm_ids: 17162 2220 4125 1997 4542 29440 20332 4233 1037 16465 2030 2682 2018 13763 5456 6644 1011 8335 1012 0
INFO:tensorflow:masked_lm_weights: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0
INFO:tensorflow:next_sentence_labels: 0
INFO:tensorflow:*** Example ***
INFO:tensorflow:tokens: [CLS] and there burst on phil ##am ##mon ' s astonished eyes a vast semi ##ci ##rcle of blue sea [MASK] ring ##ed with palaces and towers [MASK] [SEP] like most of [MASK] fellow gold - seekers , cass was super ##sti [MASK] . [SEP]
INFO:tensorflow:input_ids: 101 1998 2045 6532 2006 6316 3286 8202 1005 1055 22741 2159 1037 6565 4100 6895 21769 1997 2630 2712 103 3614 2098 2007 22763 1998 7626 103 102 2066 2087 1997 103 3507 2751 1011 24071 1010 16220 2001 3565 16643 103 1012 102 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
INFO:tensorflow:input_mask: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
INFO:tensorflow:segment_ids: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
INFO:tensorflow:masked_lm_positions: 10 20 23 27 32 39 42 0 0 0 0 0 0 0 0 0 0 0 0 0
INFO:tensorflow:masked_lm_ids: 22741 1010 2007 1012 2010 2001 20771 0 0 0 0 0 0 0 0 0 0 0 0 0
INFO:tensorflow:masked_lm_weights: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
INFO:tensorflow:next_sentence_labels: 1
INFO:tensorflow:Wrote 60 total instances
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;step2-run-pretraining&quot;&gt;step2. run-pretraining&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;如果你是从头开始pretrain，不要include init_checkpoint&lt;/li&gt;
  &lt;li&gt;模型配置（包括vocab size）在bert_config_file中设置&lt;/li&gt;
  &lt;li&gt;num_train_steps在现实中一般要设置10000以上&lt;/li&gt;
  &lt;li&gt;max_seq_length和max_predictions_per_seq要和传给create_pretraining_data的一样&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python run_pretraining.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--input_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/tf_examples.tfrecord &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--output_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/pretraining_output &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--do_train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;True &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--do_eval&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;True &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--bert_config_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/bert_config.json &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--init_checkpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/bert_model.ckpt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--train_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;32 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--num_train_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--num_warmup_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2e-5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;跑的时候发现会充分利用显存，具体不是特别清楚，显存太小应该也跑不了吧。由于sample_text.txt很小，所以会overfit。log如下(最后会生成一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eval_results.txt&lt;/code&gt;文件，记录&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;***** Eval results *****&lt;/code&gt;部分)：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Evaluation [10/100]
INFO:tensorflow:Evaluation [20/100]
INFO:tensorflow:Evaluation [30/100]
INFO:tensorflow:Evaluation [40/100]
INFO:tensorflow:Evaluation [50/100]
INFO:tensorflow:Evaluation [60/100]
INFO:tensorflow:Evaluation [70/100]
INFO:tensorflow:Evaluation [80/100]
INFO:tensorflow:Evaluation [90/100]
INFO:tensorflow:Evaluation [100/100]
INFO:tensorflow:Finished evaluation at 2018-10-31-18:13:12
INFO:tensorflow:Saving dict for global step 20: global_step = 20, loss = 0.27842212, masked_lm_accuracy = 0.94665253, masked_lm_loss = 0.27976906, next_sentence_accuracy = 1.0, next_sentence_loss = 0.0002133457
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 20: ./pretraining_output/model.ckpt-20
INFO:tensorflow:***** Eval results *****
INFO:tensorflow:  global_step = 20
INFO:tensorflow:  loss = 0.27842212
INFO:tensorflow:  masked_lm_accuracy = 0.94665253
INFO:tensorflow:  masked_lm_loss = 0.27976906
INFO:tensorflow:  next_sentence_accuracy = 1.0
INFO:tensorflow:  next_sentence_loss = 0.0002133457
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;具体可以看对应的tensorboard，比较卡，猜测是模型比较大，截图如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert-pretraining.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;还有个projector，如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert-pretraining-projector.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;左边可以选哪个模型的哪一层&lt;/p&gt;

&lt;p&gt;然后在中间的图中可以选中一个点，这样在最右边会显示出与这个点最近的n个点，度量方式可以选择cos或者欧氏距离。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert-projector-ann.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;pretrain-tips-and-caveats&quot;&gt;pretrain tips and caveats&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;如果你的任务有很大的domain-specific语料，最好从bert的checkpoint开始，在你的语料上进行多一些的pre-train&lt;/li&gt;
  &lt;li&gt;paper中的学习率设为1e-4，如果基于已有bert checkpoint继续pretrain，建议把学习率调小（如2e-5）&lt;/li&gt;
  &lt;li&gt;当前的bert模型只是English的，2018年11月底会放出更多语言的！！&lt;/li&gt;
  &lt;li&gt;更长的序列的计算代价会非常大，因为attention是序列长度平方的复杂度。例如，一个长度是512的minibatch-size=64的batch，比一个长度为128的minibatch-size=256的batch计算代码要大得多。对于全连接或者cnn来讲，其实这个计算代价是一样的。但对attention而言，长度是512的计算代价会大得多。所以，建议对长度为128的序列进行9w个step的预训练，然后对长度为512的序列再做1w个step的预训练是更好的~对于非常长的序列，最需要的是学习positional embeddings，这是很快就能学到的啦。注意，这样做就需要使用不同的max_seq_length来生成两次数据。&lt;/li&gt;
  &lt;li&gt;如果你从头开始pretrain，计算代价是很大的，特别是在gpu上。建议的是在一个preemptible Cloud TPU v2上pretrain一个bert-base（2周要500美刀…）。如果在一个single cloud TPU上的话，需要把batchsize scale down。建议使用能占满TPU内存的最大batchsize…&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;抽取feature-vector类似elmo&quot;&gt;抽取feature vector(类似ELMo)&lt;/h2&gt;

&lt;p&gt;输入文件&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;input.txt&lt;/code&gt;格式：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果是两个句子，那就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sentence A ||| sentence B&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;如果是一个句子，那就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sentence A&lt;/code&gt;，不要分隔符&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python extract_features.py &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--input_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;input.txt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--output_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/output.json &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--vocab_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/vocab.txt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--bert_config_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/bert_config.json &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--init_checkpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BERT_BASE_DIR&lt;/span&gt;/bert_model.ckpt &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;,-2,-3,-4 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;例如输入的内容是『大家』，那么输出的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;output.json&lt;/code&gt;格式如下：&lt;/p&gt;

&lt;p&gt;其中的”linex_index”表示第几行&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;linex_index&quot;&lt;/span&gt;: 0,
  &lt;span class=&quot;s2&quot;&gt;&quot;features&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;token&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;[CLS]&quot;&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;layers&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;1.507966, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.155272, 0.108119, ..., 0.111],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-2&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;1.39443, 0.307064, 0.483496, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0.961682, 0.757408, 0.720898, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-4&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.275457, 0.632056, 1.063737, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;token&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;大&quot;&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;layers&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0.326004, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.313136, 0.233399, ..., 0.111],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-2&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0.795364, 0.361322, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.116774, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0.807957, 0.206743, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.359639, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-4&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.226106, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.129655, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.128466, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;token&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;家&quot;&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;layers&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;1.768678, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.814265, 0.016321, ..., 0.111],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-2&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;1.76887, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.020193, 0.44832, 0.193271, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;1.695086, 0.050979, 0.188321, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.537057, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-4&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0.745073, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.09894, 0.166217, &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;.045382, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;token&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;[SEP]&quot;&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;layers&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0.881939, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.34753, 0.210375, ..., 0.111],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-2&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.047698, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.030813, 0.041558, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.049113, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.067705, 0.018293, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;index&quot;&lt;/span&gt;: &lt;span class=&quot;nt&quot;&gt;-4&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;values&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;0.000215, &lt;span class=&quot;nt&quot;&gt;-0&lt;/span&gt;.057331, &lt;span class=&quot;nt&quot;&gt;-3&lt;/span&gt;.2e-05, ..., 0.332],
    &lt;span class=&quot;o&quot;&gt;}]&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;自己尝试&quot;&gt;自己尝试&lt;/h2&gt;

&lt;p&gt;基于预训练的中文模型中的vocab，把网络改小，基于190w的中文语料（还是用默认的wordpiece分词）进行单机cpu训练，一个句子当成一篇文档，这个句子当成sentence2，这个句子的tag当成sentence1：&lt;/p&gt;

&lt;p&gt;模型配置如下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;s2&quot;&gt;&quot;attention_probs_dropout_prob&quot;&lt;/span&gt;: 0.1, 
  &lt;span class=&quot;s2&quot;&gt;&quot;directionality&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;bidi&quot;&lt;/span&gt;, 
  &lt;span class=&quot;s2&quot;&gt;&quot;hidden_act&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;gelu&quot;&lt;/span&gt;, 
  &lt;span class=&quot;s2&quot;&gt;&quot;hidden_dropout_prob&quot;&lt;/span&gt;: 0.1, 
  &lt;span class=&quot;s2&quot;&gt;&quot;hidden_size&quot;&lt;/span&gt;: 64, 
  &lt;span class=&quot;s2&quot;&gt;&quot;initializer_range&quot;&lt;/span&gt;: 0.02, 
  &lt;span class=&quot;s2&quot;&gt;&quot;intermediate_size&quot;&lt;/span&gt;: 3072, 
  &lt;span class=&quot;s2&quot;&gt;&quot;max_position_embeddings&quot;&lt;/span&gt;: 512, 
  &lt;span class=&quot;s2&quot;&gt;&quot;num_attention_heads&quot;&lt;/span&gt;: 8, 
  &lt;span class=&quot;s2&quot;&gt;&quot;num_hidden_layers&quot;&lt;/span&gt;: 2, 
  &lt;span class=&quot;s2&quot;&gt;&quot;pooler_fc_size&quot;&lt;/span&gt;: 64, 
  &lt;span class=&quot;s2&quot;&gt;&quot;pooler_num_attention_heads&quot;&lt;/span&gt;: 12, 
  &lt;span class=&quot;s2&quot;&gt;&quot;pooler_num_fc_layers&quot;&lt;/span&gt;: 3, 
  &lt;span class=&quot;s2&quot;&gt;&quot;pooler_size_per_head&quot;&lt;/span&gt;: 32, 
  &lt;span class=&quot;s2&quot;&gt;&quot;pooler_type&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;first_token_transform&quot;&lt;/span&gt;, 
  &lt;span class=&quot;s2&quot;&gt;&quot;type_vocab_size&quot;&lt;/span&gt;: 2, 
  &lt;span class=&quot;s2&quot;&gt;&quot;vocab_size&quot;&lt;/span&gt;: 21128
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参数设置如下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;## g_max_predictions_per_seq approx_to g_max_seq_length * g_masked_lm_prob&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# online or offline&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;train_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;offline
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;param_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;param1
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_train_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_num_train_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10000
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_masked_lm_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.15
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_dupe_factor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3

sh &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; scripts/run_train_bert.sh  &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; log/&lt;span class=&quot;nv&quot;&gt;$param_name&lt;/span&gt;.log &amp;amp;

&lt;span class=&quot;c&quot;&gt;# online or offline&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;train_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;offline
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;param_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;param2
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_train_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;64
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_num_train_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10000
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_masked_lm_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.15
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_dupe_factor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3

sh &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; scripts/run_train_bert.sh  &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; log/&lt;span class=&quot;nv&quot;&gt;$param_name&lt;/span&gt;.log &amp;amp;

&lt;span class=&quot;c&quot;&gt;# online or offline&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;train_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;offline
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;param_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;param3
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_train_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_num_train_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10000
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_masked_lm_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.05
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_dupe_factor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5

sh &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; scripts/run_train_bert.sh  &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; log/&lt;span class=&quot;nv&quot;&gt;$param_name&lt;/span&gt;.log &amp;amp;

&lt;span class=&quot;c&quot;&gt;# online or offline&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;train_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;offline
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;param_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;param4
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_train_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;64
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_num_train_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10000
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_masked_lm_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.05
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_dupe_factor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5

sh &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; scripts/run_train_bert.sh  &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; log/&lt;span class=&quot;nv&quot;&gt;$param_name&lt;/span&gt;.log &amp;amp;

&lt;span class=&quot;c&quot;&gt;# online or offline&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;train_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;offline
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;param_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;param5
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_train_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;32
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_num_train_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10000
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_masked_lm_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.15
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_dupe_factor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3

sh &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; scripts/run_train_bert.sh  &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; log/&lt;span class=&quot;nv&quot;&gt;$param_name&lt;/span&gt;.log &amp;amp;

&lt;span class=&quot;c&quot;&gt;# online or offline&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;train_mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;offline
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;param_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;param6
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_train_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;32
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_num_train_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10000
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;128
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_masked_lm_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.05
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g_dupe_factor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5

sh &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; scripts/run_train_bert.sh  &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; log/&lt;span class=&quot;nv&quot;&gt;$param_name&lt;/span&gt;.log &amp;amp;

&lt;span class=&quot;nb&quot;&gt;wait&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;跑1w个step，效果如下（图中训了2w步的那个忘了是啥配置了…）：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert-self-train-loss.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;可见，同为1w个step，参数1训练时间最久，但loss最低&lt;/p&gt;

&lt;p&gt;每秒的example数：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert-self-train-loss-examples-per-second.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;每秒的global-steps：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert-self-train-global-steps-per-second.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;拿来eval时，next sentence的准确率：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert-self-train-next-sentence-acc.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;拿来eval时，masked lm的准确率就比较。。。了：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert-self-train-maskedlm-acc.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;注意&quot;&gt;注意&lt;/h2&gt;

&lt;p&gt;我们发现，代码里没看到tf.summary相关的代码，却可以看到tensorboard…&lt;/p&gt;

&lt;p&gt;是因为用了tpuestimator。。。”TPUEstimator API 不支持 tensorboard 的自定义摘要。但是，基本摘要会自动记录到模型目录中的事件文件中。”&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://cloud.google.com/tpu/docs/tutorials/migrating-to-tpuestimator-api&quot;&gt;https://cloud.google.com/tpu/docs/tutorials/migrating-to-tpuestimator-api&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>bert代码解读——framework</title>
   <link href="http://daiwk.github.io/posts/nlp-bert-code-annotated-framework.html"/>
   <updated>2018-11-01T00:00:00+00:00</updated>
   <id>/posts/nlp-bert-code-annotated-framework</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#modelingpy&quot;&gt;modeling.py&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%85%AC%E5%85%B1%E5%87%BD%E6%95%B0&quot;&gt;公共函数&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#get-assignment-map-from-checkpoint&quot;&gt;get-assignment-map-from-checkpoint&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#reshape-to-matrix&quot;&gt;reshape-to-matrix&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#reshape-from-matrix&quot;&gt;reshape-from-matrix&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#assert-rank&quot;&gt;assert-rank&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#get-shape-list&quot;&gt;get-shape-list&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#gelu&quot;&gt;gelu&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#create-initializer&quot;&gt;create-initializer&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#dropout&quot;&gt;dropout&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#layer-norm&quot;&gt;layer-norm&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#layer-norm-and-dropout&quot;&gt;layer-norm-and-dropout&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#embedding-lookup&quot;&gt;embedding-lookup&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#embedding-postprocessor&quot;&gt;embedding-postprocessor&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#create-attention-mask-from-input-mask&quot;&gt;create-attention-mask-from-input-mask&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#attention-layer&quot;&gt;attention-layer&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#transformer-model&quot;&gt;transformer-model&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bertconfig&quot;&gt;BertConfig&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#bertconfig%E5%88%9D%E5%A7%8B%E5%8C%96&quot;&gt;BertConfig初始化&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#bertconfig%E6%96%B9%E6%B3%95&quot;&gt;BertConfig方法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#from-dictclassmethod&quot;&gt;from-dict(classmethod)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#from-json-fileclassmethod&quot;&gt;from-json-file(classmethod)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#to-dict&quot;&gt;to-dict&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#to-json-string&quot;&gt;to-json-string&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bertmodel&quot;&gt;BertModel&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%88%9D%E5%A7%8B%E5%8C%96&quot;&gt;初始化&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#get-pooled-output&quot;&gt;get-pooled-output&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#get-sequence-output&quot;&gt;get-sequence-output&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#get-all-encoder-layers&quot;&gt;get-all-encoder-layers&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#get-embedding-output&quot;&gt;get-embedding-output&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#get-embedding-table&quot;&gt;get-embedding-table&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#optimizationpy&quot;&gt;optimization.py&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tokenizationpy&quot;&gt;tokenization.py&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0&quot;&gt;常用函数&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#convert-to-unicode&quot;&gt;convert-to-unicode&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#printable-text&quot;&gt;printable-text&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#load-vocab&quot;&gt;load-vocab&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#xxx&quot;&gt;xxx&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%B0%8F%E7%BB%93&quot;&gt;小结&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#embedding%E9%83%A8%E5%88%86&quot;&gt;embedding部分&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#transformer%E9%83%A8%E5%88%86&quot;&gt;transformer部分&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pool%E9%83%A8%E5%88%86&quot;&gt;pool部分&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;strong&gt;注：由于标题不能有下划线，所以把函数名/文件名里的_换成了-&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;modelingpy&quot;&gt;modeling.py&lt;/h2&gt;

&lt;p&gt;高仿&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/layers/transformer_layers.py#L99&quot;&gt;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/layers/transformer_layers.py#L99&lt;/a&gt;的transformer_encoder部分。&lt;/p&gt;

&lt;h3 id=&quot;公共函数&quot;&gt;公共函数&lt;/h3&gt;

&lt;h4 id=&quot;get-assignment-map-from-checkpoint&quot;&gt;get-assignment-map-from-checkpoint&lt;/h4&gt;

&lt;p&gt;从checkpoint的list_variables中，获取tvars（一般是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tvars = tf.trainable_variables()&lt;/code&gt;）中的变量&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_assignment_map_from_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tvars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Compute the union of the current variables and checkpoint variables.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;assignment_map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;initialized_variable_names&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;name_to_variable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tvars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;^(.*):&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;d+$&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name_to_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 返回一个list，每个元素是(name, shape)
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;assignment_map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name_to_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assignment_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;initialized_variable_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;initialized_variable_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;:0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assignment_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initialized_variable_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;reshape-to-matrix&quot;&gt;reshape-to-matrix&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reshape_to_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Reshapes a &amp;gt;= rank 2 tensor to a rank 2 tensor (i.e., a matrix).&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ndims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndims&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ndims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Input tensor must have at least rank 2. Shape = %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ndims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;reshape-from-matrix&quot;&gt;reshape-from-matrix&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reshape_from_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orig_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Reshapes a rank 2 tensor back to its original rank &amp;gt;= 2 tensor.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;orig_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;output_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;orig_dims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orig_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;orig_dims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;assert-rank&quot;&gt;assert-rank&lt;/h4&gt;

&lt;p&gt;注意：&lt;/p&gt;

&lt;p&gt;tensor的rank表示一个tensor&lt;strong&gt;需要的索引数目&lt;/strong&gt;来唯一表示任何一个元素。也就是通常所说的 “order”, “degree”或”ndims”，不是矩阵的秩。。参考&lt;a href=&quot;https://blog.csdn.net/lenbow/article/details/52152766&quot;&gt;https://blog.csdn.net/lenbow/article/details/52152766&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#’t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
# shape of tensor ‘t’ is [2, 2, 3]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;函数的功能：如果输入tensor的rank和预期的不一样，就抛异常&lt;/p&gt;

&lt;p&gt;参数：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;tensor：输入的tf.Tensor&lt;/li&gt;
  &lt;li&gt;expected_rank：Python integer or list of integers，期望的rank&lt;/li&gt;
  &lt;li&gt;name：error message中的tensor的名字&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;assert_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;expected_rank_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;six&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integer_types&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;expected_rank_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;expected_rank_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;actual_rank&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndims&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actual_rank&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;scope_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;For the tensor `%s` in scope `%s`, the actual rank &quot;&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;`%d` (shape = %s) is not equal to the expected rank `%s`&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actual_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;get-shape-list&quot;&gt;get-shape-list&lt;/h4&gt;

&lt;p&gt;参数：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;tensor：一个需要返回shape的tf.Tensor&lt;/li&gt;
  &lt;li&gt;expected_rank：int或者是一个int的list。输入tensor期望的rank，如果输入tensor的rank不等于这个数，或者不是这个list的元素之一，会抛异常&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assert_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;non_static_indexes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;non_static_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;non_static_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;dyn_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;non_static_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dyn_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;gelu&quot;&gt;gelu&lt;/h4&gt;

&lt;p&gt;其中的tf.erf是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/python/ops/gen_math_ops.py&lt;/code&gt;中的函数，计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Gauss error function of `x` element-wise.&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gelu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Gaussian Error Linear Unit.

  This is a smoother version of the RELU.
  Original paper: https://arxiv.org/abs/1606.08415

  Args:
    input_tensor: float Tensor to perform activation.

  Returns:
    `input_tensor` with the GELU activation applied.
  &quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;erf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cdf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;create-initializer&quot;&gt;create-initializer&lt;/h4&gt;

&lt;p&gt;对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.truncated_normal_initializer&lt;/code&gt;的简单封装&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.02&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Creates a `truncated_normal_initializer` with the given range.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;truncated_normal_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;dropout&quot;&gt;dropout&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Perform dropout.

  Args:
    input_tensor: float Tensor.
    dropout_prob: Python float. The probability of dropping out a value (NOT of
      *keeping* a dimension as in `tf.nn.dropout`).

  Returns:
    A version of `input_tensor` with dropout applied.
  &quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;layer-norm&quot;&gt;layer-norm&lt;/h4&gt;

&lt;p&gt;只在shape的最后一维做layer norm&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;layer_norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Run layer normalization on the last dimension of the tensor.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;begin_norm_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;begin_params_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;layer-norm-and-dropout&quot;&gt;layer-norm-and-dropout&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;layer_norm_and_dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Runs layer normalization followed by dropout.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;embedding-lookup&quot;&gt;embedding-lookup&lt;/h4&gt;

&lt;p&gt;返回一个shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length, embedding_size]&lt;/code&gt;的tensor，还有shape为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[vocab_size, embedding_size]&lt;/code&gt;的整个embedding_table&lt;/p&gt;

&lt;p&gt;参数：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;input_ids：shape为包含了word ids的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length]&lt;/code&gt;的tensor&lt;/li&gt;
  &lt;li&gt;vocab_size：embedding vocabulary的size&lt;/li&gt;
  &lt;li&gt;embedding_size：word embeddings的width&lt;/li&gt;
  &lt;li&gt;initializer_range：Embedding初始化的range&lt;/li&gt;
  &lt;li&gt;word_embedding_name：embedding table的名字&lt;/li&gt;
  &lt;li&gt;use_one_hot_embeddings：true: 使用one-hot的embedding；false：使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.embedding_lookup()&lt;/code&gt;，如下所述，tpu用one-hot好，cpu/gpu用非one-hot好&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.02&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;word_embedding_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;word_embeddings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;## 此函数假设输入的shape是[batch_size, seq_length, num_inputs]。如果是[batch_size, seq_length]，会reshape成[batch_size, seq_length, 1]
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## tf.expand_dims在axis处插入维度1进入一个tensor中
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expand_dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;## shape是[vocab_size, embedding_size]的embedding table
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;embedding_table&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_embedding_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## 把[batch_size, seq_length, 1]的input_ids变成[batch_size*seq_length]的一个tensor
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;flat_input_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## 变成一个[batch_size*seq_length, vocab_size]的one-hot的tensor，depth参数的含义就是vocab_size
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;one_hot_input_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_hot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flat_input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## [batch_size*seq_length, vocab_size]的one_hot_input_ids和[vocab_size, embedding_size]的embedding_table矩阵相乘，得到[batch_size*seq_length,embedding_size]的output
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_hot_input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## [batch_size, seq_length, 1]的input_ids去[vocab_size, embedding_size]的embedding_table中lookup，得到一个[batch_size, seq_length, 1, embedding_size]的output
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;## reshape成[batch_size, seq_length, 1 * embedding_size]的输出
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;embedding-postprocessor&quot;&gt;embedding-postprocessor&lt;/h4&gt;

&lt;p&gt;参数：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;input_tensor：shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length, embedding_size]&lt;/code&gt;的float Tensor&lt;/li&gt;
  &lt;li&gt;use_token_type：是否要为embedding加上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;token_type_ids&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;token_type_ids：shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length]&lt;/code&gt;的int32 Tensor&lt;/li&gt;
  &lt;li&gt;token_type_vocab_size：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;token_type_ids&lt;/code&gt;的vocabulary size&lt;/li&gt;
  &lt;li&gt;token_type_embedding_name：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;token_type_ids&lt;/code&gt;的embedding_table的名字，默认”token_type_embeddings”&lt;/li&gt;
  &lt;li&gt;use_position_embeddings：是否要为序列里每个token的位置加上position embeddings&lt;/li&gt;
  &lt;li&gt;position_embedding_name：position_embeddings的embedding_table的名字，默认”position_embeddings”&lt;/li&gt;
  &lt;li&gt;initializer_range：权重初始化的range&lt;/li&gt;
  &lt;li&gt;max_position_embeddings：最大的sequence length，可以比输入的sequence length长，但不能比它短&lt;/li&gt;
  &lt;li&gt;dropout_prob：&lt;strong&gt;最终输出tensor&lt;/strong&gt;的dropout rate&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;embedding_postprocessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;use_token_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;token_type_vocab_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;token_type_embedding_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;token_type_embeddings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;use_position_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;position_embedding_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position_embeddings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.02&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;## input_shape是[batch_size, seq_length, embedding_size]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;The seq length (%d) cannot be greater than &quot;&lt;/span&gt;
                     &lt;span class=&quot;s&quot;&gt;&quot;`max_position_embeddings` (%d)&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_token_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;`token_type_ids` must be specified if&quot;&lt;/span&gt;
                       &lt;span class=&quot;s&quot;&gt;&quot;`use_token_type` is True.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## 搞一个[token_type_vocab_size, embedding_size]的token_type的embedding_table
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;token_type_table&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_type_embedding_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_type_vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 由于token_type的vocab很小，所以直接用one-hot，这样能更快，这个用法和上面的embedding_lookup函数一样，不再赘述
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;flat_token_type_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;one_hot_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_hot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flat_token_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_type_vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;token_type_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_hot_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token_type_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;token_type_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_type_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                       &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token_type_embeddings&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_position_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;full_position_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position_embedding_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# position embedding table是一个learned variable，对[0, 1, 2, ..., max_position_embeddings-1]来讲，
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# full_position_embeddings的shape就是[max_position_embeddings, width]
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 而当前的序列长度是seq_length，所以针对[0, 1, 2, ... seq_length-1], 可以对full_position_embeddings做个slice
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 传入给slice的begin是[0,0]，size是[seq_length,-1]，所以是对输入的shape取[0:seq_len, 0:-1]，所以
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# slice的结果position_embeddings的shape是[seq_length, width]
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;position_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;full_position_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                                     &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;position_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;full_position_embeddings&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# output.shape是[batch_size, seq_length, embedding_size]，num_dims是3
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;num_dims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;## 其实这里就是把position_broadcast_shape写成[1, seq_length, width]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;position_broadcast_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_dims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;position_broadcast_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;position_broadcast_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 把position_embeddings从[seq_length, width]给reshape成[1, seq_length, width]，方便和output相加
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 第一维是1，是因为batch里的每一条数据，相同position加的position embedding是一样的
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;position_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                     &lt;span class=&quot;n&quot;&gt;position_broadcast_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position_embeddings&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 注意,layer_norm里只对最后一维做norm，即只对embedding_size这一维做norm
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_norm_and_dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;create-attention-mask-from-input-mask&quot;&gt;create-attention-mask-from-input-mask&lt;/h4&gt;

&lt;p&gt;输入：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;from_tensor：2D或者3D的Tensor，shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, from_seq_length]&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, from_seq_length, xxx]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;to_mask：int32的Tensor，shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, to_seq_length]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_attention_mask_from_input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Create 3D attention mask from a 2D tensor mask.

  Args:
    from_tensor: 2D or 3D Tensor of shape [batch_size, from_seq_length, ...].
    to_mask: int32 Tensor of shape [batch_size, to_seq_length].

  Returns:
    float Tensor of shape [batch_size, from_seq_length, to_seq_length].
  &quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 确保输入的tensor是2D或者3D，前两维是batch_size和from_seq_length
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# 确保to_mask是2D的，shape是[batch_size, to_seq_length]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;to_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;to_seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# 1. 把to_mask的shape从[batch_size, to_seq_length]转成[batch_size, 1, to_seq_length]
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 2. 把to_mask的数据类型从int32转成float
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;to_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# We don't assume that `from_tensor` is a mask (although it could be). We
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# don't actually care if we attend *from* padding tokens (only *to* padding)
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# tokens so we create a tensor of all ones.
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;#
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# `broadcast_ones` = [batch_size, from_seq_length, 1]
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# broadcast_ones是一个float32的[batch_size, from_seq_length, 1]的全1 tensor
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;broadcast_ones&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# [batch_size, from_seq_length, 1]和[[batch_size, 1, from_seq_length]相乘(element-wise乘积)，经过broadcast后
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 得到[batch_size, from_seq_length, from_seq_length]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;broadcast_ones&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_mask&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mask = broadcast_ones * to_mask&lt;/code&gt;的理解如下：&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/yangmang/p/7125458.html&quot;&gt;https://www.cnblogs.com/yangmang/p/7125458.html&lt;/a&gt;提到的广播原则：如果两个数组的后缘维度(即：&lt;strong&gt;从末尾开始算起的维度&lt;/strong&gt;)的&lt;strong&gt;轴长相符&lt;/strong&gt;或&lt;strong&gt;其中一方的长度为1&lt;/strong&gt;，则认为它们是广播兼容的，广播会在&lt;strong&gt;缺失和(或)长度为1的轴上进行&lt;/strong&gt;。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.79036561&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.6795738&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.80898213&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;

       &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.03638711&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.34853504&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.48699898&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;

       &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.79036561&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.6795738&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.80898213&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.79036561&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.6795738&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.80898213&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.79036561&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.6795738&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.80898213&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;

       &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.03638711&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.34853504&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.48699898&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.03638711&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.34853504&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.48699898&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.03638711&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.34853504&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.48699898&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;attention-layer&quot;&gt;attention-layer&lt;/h4&gt;

&lt;p&gt;如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;from_tensor&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_tensor&lt;/code&gt;一样，那就是self-attention。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;from_tensor&lt;/code&gt;的每一个timestep会attend to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_tensor&lt;/code&gt;的对应序列，然后返回一个fixed-with vector。&lt;/p&gt;

&lt;p&gt;首先，将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;from_tensor&lt;/code&gt;映射到一个”query” tensor，并把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_tensor&lt;/code&gt;映射成”key” tensors和”value” tensors。这些是一个长度为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;num_attention_heads&lt;/code&gt;的list的tensors，每个tensor的shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length, size_per_head]&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;然后，对query和key tensors进行dot-product，然后scale。这是通过softmax来获得attention probabilities。然后把value tensors通过这些probabilities进行interpolate(插值？。。)，再然后concate到一起形成一个single tensor并返回。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;实践中，multi-head attention通过transpose和reshape，而非真正地将tensors进行separate。。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;参数如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;from_tensor：float Tensor，shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, from_seq_length, from_width]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;to_tensor：float Tensor，shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, to_seq_length, to_width]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;attention_mask：int32 Tensor，shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, from_seq_length, to_seq_length]&lt;/code&gt;，每个元素的值要是0/1。如果mask的值是0，那它对应的attention score会被设成&lt;strong&gt;-infinity&lt;/strong&gt;，如果是mask的值是1，那么attention score不变。&lt;/li&gt;
  &lt;li&gt;num_attention_heads：attention heads的个数&lt;/li&gt;
  &lt;li&gt;size_per_head：每个attention head的size&lt;/li&gt;
  &lt;li&gt;query_act：query transform的激活函数&lt;/li&gt;
  &lt;li&gt;key_act：key transform的激活函数&lt;/li&gt;
  &lt;li&gt;value_act：value transform的激活函数&lt;/li&gt;
  &lt;li&gt;attention_probs_dropout_prob：attention probabilities的dropout rate&lt;/li&gt;
  &lt;li&gt;initializer_range：weight初始化的range&lt;/li&gt;
  &lt;li&gt;do_return_2d_tensor：是否返回2d tensor。具体取值和对应的返回shape如下所述&lt;/li&gt;
  &lt;li&gt;batch_size：如果输入是2D，这个参数是3D版本的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;from_tensor&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_tensor&lt;/code&gt;的batch_size&lt;/li&gt;
  &lt;li&gt;from_seq_length：如果输入是2D，这个参数是3D版本的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;from_tensor&lt;/code&gt;的seq_length&lt;/li&gt;
  &lt;li&gt;to_seq_length：如果输入是2D，这个参数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_tensor&lt;/code&gt;的seq_length&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;返回值：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do_return_2d_tensor&lt;/code&gt;是true，那么返回一个shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, from_seq_length,num_attention_heads * size_per_head]&lt;/code&gt;的float Tensor&lt;/li&gt;
  &lt;li&gt;反之，返回一个shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size * from_seq_length, num_attention_heads * size_per_head]&lt;/code&gt;的float Tensor。&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;attention_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;to_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;query_act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;key_act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;value_act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.02&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;do_return_2d_tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;to_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;transpose_for_scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_tensor&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# from_tensor和to_tensor的shape要么是2([from_seq_length * seq_length, from_width])，要么是3([batch_size, seq_length, from_width]) --&amp;gt;详见reshape_to_matrix和reshape_from_matrix，，应该是这样吧2D的时候。。
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;to_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;The rank of `from_tensor` must match the rank of `to_tensor`.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;to_seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_seq_length&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&quot;When passing in rank 2 tensors to attention_layer, the values &quot;&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&quot;for `batch_size`, `from_seq_length`, and `to_seq_length` &quot;&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&quot;must all be specified.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Scalar dimensions referenced here:
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;#   B = batch size (number of sequences)
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;#   F = `from_tensor` sequence length
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;#   T = `to_tensor` sequence length
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;#   N = `num_attention_heads`
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;#   H = `size_per_head`
&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;from_tensor_2d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reshape_to_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;to_tensor_2d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reshape_to_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `query_layer` = [B*F, N*H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;query_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;from_tensor_2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query_act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;query&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;kernel_initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `key_layer` = [B*T, N*H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;key_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;to_tensor_2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key_act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;kernel_initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `value_layer` = [B*T, N*H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;value_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;to_tensor_2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value_act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;kernel_initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `query_layer` = [B, N, F, H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;query_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transpose_for_scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                     &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                     &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `key_layer` = [B, N, T, H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;key_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transpose_for_scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                   &lt;span class=&quot;n&quot;&gt;to_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Take the dot product between &quot;query&quot; and &quot;key&quot; to get the raw
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# attention scores.
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# `attention_scores` = [B, N, F, T]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;attention_scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transpose_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;attention_scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                 &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# `attention_mask` = [B, 1, F, T]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expand_dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Since attention_mask is 1.0 for positions we want to attend and 0.0 for
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# masked positions, this operation will create a tensor which is 0.0 for
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# positions we want to attend and -10000.0 for masked positions.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;adder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;10000.0&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Since we are adding it to the raw scores before the softmax, this is
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# effectively the same as removing these entirely.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;attention_scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adder&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Normalize the attention scores to probabilities.
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# `attention_probs` = [B, N, F, T]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;attention_probs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# This is actually dropping out entire tokens to attend to, which might
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# seem a bit unusual, but is taken from the original Transformer paper.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;attention_probs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_probs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `value_layer` = [B, T, N, H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;value_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;value_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `value_layer` = [B, N, T, H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;value_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `context_layer` = [B, N, F, H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;context_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_probs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# `context_layer` = [B, F, N, H]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;context_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;do_return_2d_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# `context_layer` = [B*F, N*V]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;context_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;context_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# `context_layer` = [B, F, N*V]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;context_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;context_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context_layer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;transformer-model&quot;&gt;transformer-model&lt;/h4&gt;

&lt;p&gt;最后一个隐层的shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length, hidden_size]&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;参数：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;input_tensor：shape为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length, hidden_size]&lt;/code&gt;的float Tensor&lt;/li&gt;
  &lt;li&gt;attention_mask：shape为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length, seq_length]&lt;/code&gt;的int32 Tensor（实际上是float??）。1表示可以被attended to的positions，0表示不能&lt;/li&gt;
  &lt;li&gt;hidden_size：Transformer的hidden size&lt;/li&gt;
  &lt;li&gt;num_hidden_layers：Transformer中的layers (blocks)个数&lt;/li&gt;
  &lt;li&gt;num_attention_heads：Transformer中的attention heads数&lt;/li&gt;
  &lt;li&gt;intermediate_size：”intermediate”层(例如feed-forward)的size&lt;/li&gt;
  &lt;li&gt;intermediate_act_fn：”intermediate”层输出的激活函数&lt;/li&gt;
  &lt;li&gt;hidden_dropout_prob：隐层的dropout rate&lt;/li&gt;
  &lt;li&gt;attention_probs_dropout_prob：attention probabilities的dropout rate&lt;/li&gt;
  &lt;li&gt;initializer_range：初始化权重的range&lt;/li&gt;
  &lt;li&gt;do_return_all_layers：返回所有层或者只返回最后一层&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;transformer_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;num_hidden_layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;intermediate_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3072&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;intermediate_act_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gelu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.02&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;do_return_all_layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;## 首先保证hidden_size能被num_attention_heads整除，因为后面要hidden_size / num_attention_heads
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;The hidden size (%d) is not a multiple of the number of attention &quot;&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;heads (%d)&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;attention_head_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;## 输入的shape是[batch_size, seq_length, hidden_size]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;input_width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# The Transformer performs sum residuals on all layers so the input needs
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# to be the same as the hidden size.
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;The width of the input tensor (%d) != hidden size (%d)&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# We keep the representation as a 2D tensor to avoid re-shaping it back and
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# forth from a 3D tensor to a 2D tensor. Re-shapes are normally free on
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# the GPU/CPU but may not be free on the TPU, so we want to minimize them to
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# help the optimizer.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;prev_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reshape_to_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;all_layer_outputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_idx&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_hidden_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer_%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;layer_input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prev_output&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;attention&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;attention_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;self&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;attention_head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attention_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;from_tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;to_tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;size_per_head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_head_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;do_return_2d_tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;from_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;to_seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;# In the case where we have other sequences, we just concatenate
&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# them to the self-attention head before the projection.
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Run a linear projection of `hidden_size` then add a residual
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# with `layer_input`.
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;kernel_initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;# The activation is only applied to the &quot;intermediate&quot; hidden layer.
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;intermediate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;intermediate_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;intermediate_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;intermediate_act_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kernel_initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;# Down-project back to `hidden_size` then add the residual.
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;intermediate_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kernel_initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attention_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;prev_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;all_layer_outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;do_return_all_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;final_outputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_layer_outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;## 将输出reshape成和input_shape一样的shape，即[batch_size, seq_length, hidden_size]
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;final_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reshape_from_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;final_outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;final_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;final_outputs&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## 将输出reshape成和input_shape一样的shape，即[batch_size, seq_length, hidden_size]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;final_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reshape_from_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prev_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;final_output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;bertconfig&quot;&gt;BertConfig&lt;/h3&gt;

&lt;h4 id=&quot;bertconfig初始化&quot;&gt;BertConfig初始化&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BertConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Configuration for `BertModel`.&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;num_hidden_layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;intermediate_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3072&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;hidden_act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;gelu&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;type_vocab_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.02&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_hidden_layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_hidden_layers&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_act&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_act&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;intermediate_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;intermediate_size&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type_vocab_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type_vocab_size&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参数如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;vocab_size：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inputs_ids&lt;/code&gt;的vocabulary size&lt;/li&gt;
  &lt;li&gt;hidden_size：encoder layers和pooler layer的size&lt;/li&gt;
  &lt;li&gt;num_hidden_layers：Transformer encoder的hidden layer数&lt;/li&gt;
  &lt;li&gt;num_attention_heads：Transformer encoder的每个attention layer的attention heads数&lt;/li&gt;
  &lt;li&gt;intermediate_size：Transformer encoder的”intermediate” layer(例如feed-forward)的size&lt;/li&gt;
  &lt;li&gt;hidden_act：encoder and pooler的激活函数&lt;/li&gt;
  &lt;li&gt;hidden_dropout_prob：embeddings, encoder, 和pooler的所有全连接的dropout rate&lt;/li&gt;
  &lt;li&gt;attention_probs_dropout_prob：attention probabilities的dropout rate&lt;/li&gt;
  &lt;li&gt;max_position_embeddings：最大的sequence长度，通常设得比较大（512 or 1024 or 2048）&lt;/li&gt;
  &lt;li&gt;type_vocab_size：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;token_type_ids&lt;/code&gt;的vocabulary size&lt;/li&gt;
  &lt;li&gt;initializer_range：所有权重矩阵的truncated_normal_initializer的stdev&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;bertconfig方法&quot;&gt;BertConfig方法&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;classmethod&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;注：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;classmethod&lt;/code&gt;修饰符对应的函数&lt;strong&gt;不需要实例化&lt;/strong&gt;，&lt;strong&gt;不需要self参数&lt;/strong&gt;，但第一个参数需要是表示自身类的cls参数，可以来&lt;strong&gt;调用类的属性，类的方法，实例化对象等&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;  
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'foo'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
    &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;classmethod&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'func2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# 调用 foo 方法
&lt;/span&gt; 
&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;# 不需要实例化
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__dict__&lt;/code&gt;变量&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;实例的__dict__仅存储与该实例相关的实例属性，&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BertConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__dict__&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'type_vocab_size'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'vocab_size'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'num_attention_heads'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'num_hidden_layers'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'attention_probs_dropout_prob'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'max_position_embeddings'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'initializer_range'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.02&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'hidden_act'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'gelu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'hidden_size'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'intermediate_size'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3072&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'hidden_dropout_prob'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;类的__dict__存储所有实例共享的变量和函数(类属性，方法等)，类的__dict__并不包含其父类的属性。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;

    &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;classmethod&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mm&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ccc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ccc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__dict__&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__dict__&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 输出
&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'func2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;classmethod&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x100812478&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__module__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__main__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'func1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x1007fc758&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__dict__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attribute&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__dict__'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'A'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__weakref__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attribute&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__weakref__'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'A'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objects&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__doc__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__init__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;at&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x1007fc140&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看看BertConfig的方法们：&lt;/p&gt;

&lt;h4 id=&quot;from-dictclassmethod&quot;&gt;from-dict(classmethod)&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;classmethod&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;from_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Constructs a `BertConfig` from a Python dictionary of parameters.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BertConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;six&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iteritems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__dict__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;six.iteritems&lt;/code&gt;函数如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;iteritems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;six&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iteritems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;from-json-fileclassmethod&quot;&gt;from-json-file(classmethod)&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;classmethod&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;from_json_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Constructs a `BertConfig` from a json file of parameters.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;to-dict&quot;&gt;to-dict&lt;/h4&gt;

&lt;p&gt;关于deepcopy，参考&lt;a href=&quot;https://daiwk.github.io/posts/knowledge-python.html#copy-deepcopy&quot;&gt;https://daiwk.github.io/posts/knowledge-python.html#copy-deepcopy&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Serializes this instance to a Python dictionary.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deepcopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__dict__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;to-json-string&quot;&gt;to-json-string&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_json_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Serializes this instance to a JSON string.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;indent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sort_keys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;bertmodel&quot;&gt;BertModel&lt;/h3&gt;

&lt;h4 id=&quot;初始化&quot;&gt;初始化&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BertModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;is_training&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参数如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;config：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BertConfig&lt;/code&gt;instance.&lt;/li&gt;
  &lt;li&gt;is_training：true: training model；false：eval model。用于控制是否dropout。&lt;/li&gt;
  &lt;li&gt;input_ids：shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length]&lt;/code&gt;的int32 Tensor。&lt;/li&gt;
  &lt;li&gt;input_mask：shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length]&lt;/code&gt;的int32 Tensor。&lt;/li&gt;
  &lt;li&gt;token_type_ids：shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length]&lt;/code&gt;的int32 Tensor。&lt;/li&gt;
  &lt;li&gt;use_one_hot_embeddings：使用one-hot embedding，还是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.embedding_lookup()&lt;/code&gt;。TPU上设成True会更快，cpu/gpu上设成False更快。&lt;/li&gt;
  &lt;li&gt;scope：variable scope，默认是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bert&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;实现分成以下几步：&lt;/p&gt;

&lt;p&gt;首先是input_mask/token_type_ids/batch_size/seq_length的确定：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deepcopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_training&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 期望input_ids的shape是两维，即[batch_size, seq_length]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_shape_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expected_rank&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;## 默认input_mask全是1
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;## 默认token_type_ids全是0
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后确定网络结构：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bert&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;embeddings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 对输入的word ids进行emb
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;word_embedding_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;word_embeddings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 对wordid的emb结果，加上type embed和position emb，然后normalize并dropout输出
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_postprocessor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;use_token_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;token_type_vocab_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type_vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;token_type_embedding_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;token_type_embeddings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;use_position_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;position_embedding_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;position_embeddings&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_position_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;encoder&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 将shape是[batch_size, seq_length]的input_ids和
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# shape是[batch_size, seq_length]的input_mask转成
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# shape是[batch_size, seq_length, seq_length]的3D mask，给attention scores用
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_attention_mask_from_input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Run the stacked transformer.
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# `sequence_output` shape = [batch_size, seq_length, hidden_size].
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_encoder_layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transformer_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input_tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;num_hidden_layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_hidden_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_attention_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;intermediate_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;intermediate_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;intermediate_act_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention_probs_dropout_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;do_return_all_layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;# sequence_output只取all_encoder_layers的最后一个元素，shape是[batch_size, seq_length, hidden_size]
&lt;/span&gt;      &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_encoder_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# 对于segment-level或者segment-pair-level的分类任务，我们需要对segment的一个
&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# fixed dimensional representation，所以需要这么一个&quot;pool&quot;操作，
&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# 把shape是[batch_size, seq_length, hidden_size]的tensor变成
&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# shape是[batch_size, hidden_size]的输出
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;pooler&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# We &quot;pool&quot; the model by simply taking the hidden state corresponding
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# to the first token. We assume that this has been pre-trained
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# 假设已经pretrained:
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# 只拿出batchsize个序列的每个序列的第一个token的向量(下面的[:,0:1,:])出来
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# 从[batch_size, seq_length, hidden_size]变成了[batch_size, hidden_size]
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;first_token_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squeeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 接一个size是hidden_size的fc，输出的shape还是[batch_size, hidden_size]
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pooled_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;first_token_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tanh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;kernel_initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;get-pooled-output&quot;&gt;get-pooled-output&lt;/h4&gt;

&lt;p&gt;返回pooled_output&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_pooled_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pooled_output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;get-sequence-output&quot;&gt;get-sequence-output&lt;/h4&gt;

&lt;p&gt;返回encoder的最后一个隐层&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_sequence_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Gets final hidden layer of encoder.

    Returns:
      float Tensor of shape [batch_size, seq_length, hidden_size] corresponding
      to the final hidden of the transformer encoder.
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence_output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;get-all-encoder-layers&quot;&gt;get-all-encoder-layers&lt;/h4&gt;

&lt;p&gt;返回all_encoder_layers&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_all_encoder_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_encoder_layers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;get-embedding-output&quot;&gt;get-embedding-output&lt;/h4&gt;

&lt;p&gt;返回embedding_output，shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch_size, seq_length, hidden_size]&lt;/code&gt;，是加好了positional embeddings和token type embeddings，然后过了layer norm的结果，即transformer的input。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_embedding_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Gets output of the embedding lookup (i.e., input to the transformer).

    Returns:
      float Tensor of shape [batch_size, seq_length, hidden_size] corresponding
      to the output of the embedding layer, after summing the word
      embeddings with the positional embeddings and the token type embeddings,
      then performing layer normalization. This is the input to the transformer.
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;get-embedding-table&quot;&gt;get-embedding-table&lt;/h4&gt;

&lt;p&gt;返回embedding_table&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_embedding_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_table&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;optimizationpy&quot;&gt;optimization.py&lt;/h2&gt;

&lt;h2 id=&quot;tokenizationpy&quot;&gt;tokenization.py&lt;/h2&gt;

&lt;h3 id=&quot;常用函数&quot;&gt;常用函数&lt;/h3&gt;

&lt;h4 id=&quot;convert-to-unicode&quot;&gt;convert-to-unicode&lt;/h4&gt;

&lt;p&gt;从utf8转成unicode&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;convert_to_unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Converts `text` to Unicode (if it's not already), assuming utf-8 input.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;six&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PY3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ignore&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Unsupported string type: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;six&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PY2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ignore&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Unsupported string type: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Not running on Python2 or Python 3?&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;printable-text&quot;&gt;printable-text&lt;/h4&gt;

&lt;p&gt;将输入变成tf.logging可以打印的类型：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果是python3，那就是str或者unicode&lt;/li&gt;
  &lt;li&gt;如果是python2，那就是str或者utf8&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;printable_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Returns text encoded in a way suitable for print or `tf.logging`.&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# These functions want `str` for both Python2 and Python3, but in one case
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# it's a Unicode string and in the other it's a byte string.
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;six&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PY3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ignore&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Unsupported string type: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;six&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PY2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Unsupported string type: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Not running on Python2 or Python 3?&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;load-vocab&quot;&gt;load-vocab&lt;/h4&gt;

&lt;p&gt;加载vocab，每行是一词，转成unicode，行号是词的id&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Loads a vocabulary file into a dictionary.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;convert_to_unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;xxx&quot;&gt;xxx&lt;/h4&gt;

&lt;h2 id=&quot;小结&quot;&gt;小结&lt;/h2&gt;

&lt;h3 id=&quot;embedding部分&quot;&gt;embedding部分&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert_flow_embedding.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;transformer部分&quot;&gt;transformer部分&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert_flow_transformer.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;pool部分&quot;&gt;pool部分&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bert_flow_pool.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>bert代码解读——application</title>
   <link href="http://daiwk.github.io/posts/nlp-bert-code-annotated-application.html"/>
   <updated>2018-11-01T00:00:00+00:00</updated>
   <id>/posts/nlp-bert-code-annotated-application</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#basics&quot;&gt;basics&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tpuestimator&quot;&gt;TPUEstimator&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#train&quot;&gt;train&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#predict&quot;&gt;predict&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#evaluate&quot;&gt;evaluate&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#extract-features&quot;&gt;extract-features&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#extract-featurespy&quot;&gt;extract-features.py&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#inputexample&quot;&gt;InputExample&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#inputfeatures&quot;&gt;InputFeatures&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#truncate-seq-pairextract-feature%e4%b8%ad&quot;&gt;truncate-seq-pair(extract-feature中)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#read-examples&quot;&gt;read-examples&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#convert-examples-to-features&quot;&gt;convert-examples-to-features&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#input-fn-builder&quot;&gt;input-fn-builder&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#model-fn-builder&quot;&gt;model-fn-builder&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#main&quot;&gt;main&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pretrain&quot;&gt;pretrain&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#create-pretraining-datapy&quot;&gt;create-pretraining-data.py&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%b7%a5%e5%85%b7%e5%87%bd%e6%95%b0%e4%b8%8e%e7%b1%bb&quot;&gt;工具函数与类&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#traininginstance&quot;&gt;TrainingInstance&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#create-int-feature&quot;&gt;create-int-feature&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#create-float-feature&quot;&gt;create-float-feature&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#write-instance-to-example-files&quot;&gt;write-instance-to-example-files&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#main-1&quot;&gt;main&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#create-training-instances&quot;&gt;create-training-instances&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#truncate-seq-paircreate-pretrain%e4%b8%ad&quot;&gt;truncate-seq-pair(create-pretrain中)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#create-instances-from-document&quot;&gt;create-instances-from-document&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#create-masked-lm-predictions&quot;&gt;create-masked-lm-predictions&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#run-pretrainingpy&quot;&gt;run-pretraining.py&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#classification&quot;&gt;classification&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#run-classifierpy&quot;&gt;run-classifier.py&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#squad&quot;&gt;squad&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#run-squadpy&quot;&gt;run-squad.py&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%8f%af%e8%a7%86%e5%8c%96%e5%88%86%e6%9e%90&quot;&gt;可视化分析&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;basics&quot;&gt;basics&lt;/h2&gt;

&lt;h3 id=&quot;tpuestimator&quot;&gt;TPUEstimator&lt;/h3&gt;

&lt;p&gt;类的定义在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;site-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py&lt;/code&gt;中&lt;/p&gt;

&lt;h4 id=&quot;train&quot;&gt;train&lt;/h4&gt;

&lt;p&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py&lt;/code&gt;文件中（如果装的是cpu/gpu版的，好像没这个函数）：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;max_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;saving_listeners&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error_handling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ErrorRendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_sources&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_fn_lib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TRAIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TPUEstimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_steps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;saving_listeners&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saving_listeners&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# pylint: disable=broad-except
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'training_loop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'training_loop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raise_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;predict&quot;&gt;predict&lt;/h4&gt;

&lt;p&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py&lt;/code&gt;文件中（如果装的是cpu/gpu版的，好像没这个函数）：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;predict_keys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;checkpoint_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;yield_single_examples&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error_handling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ErrorRendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_sources&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_fn_lib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PREDICT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TPUEstimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;predict_keys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;checkpoint_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpoint_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;yield_single_examples&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yield_single_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# pylint: disable=broad-except
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'prediction_loop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'prediction_loop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raise_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'prediction_loop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raise_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;evaluate&quot;&gt;evaluate&lt;/h4&gt;

&lt;p&gt;看tf的源码，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py&lt;/code&gt;文件中（如果装的是cpu/gpu版的，好像没这个函数）：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;checkpoint_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error_handling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ErrorRendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_sources&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_fn_lib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EVAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TPUEstimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hooks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;checkpoint_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkpoint_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# pylint: disable=broad-except
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'evaluation_loop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exc_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;record_done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'evaluation_loop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;rendezvous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raise_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;extract-features&quot;&gt;extract-features&lt;/h2&gt;

&lt;h3 id=&quot;extract-featurespy&quot;&gt;extract-features.py&lt;/h3&gt;

&lt;h4 id=&quot;inputexample&quot;&gt;InputExample&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;InputExample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;inputfeatures&quot;&gt;InputFeatures&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;InputFeatures&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;A single set of features of data.&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;truncate-seq-pairextract-feature中&quot;&gt;truncate-seq-pair(extract-feature中)&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_truncate_seq_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Truncates a sequence pair in place to the maximum length.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 保证tokens_a+tokens_b的总长度小于等于max_length
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 如果不满足，把比较长的那个list的最后一个元素删了，然后循环，直到满足为止
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 当一个句子很短时，这样做与对每个句子删掉相同比例的token要更make sense，
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 因为短句子中的token信息量应该会比长句子更大
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;total_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 把tokens_a的最后一个元素删了
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 把tokens_b的最后一个元素删了
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;read-examples&quot;&gt;read-examples&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Read a list of `InputExample`s from an input file.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;examples&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# 将每行转成unicode
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert_to_unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# 以『|||』进行分隔，前面是句子A，后面是句子B
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;^(.*) \|\|\| (.*)$&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# 使用InputExample类封装一下
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; 
          &lt;span class=&quot;n&quot;&gt;InputExample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;examples&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;convert-examples-to-features&quot;&gt;convert-examples-to-features&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;convert_examples_to_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Loads a data file into a list of `InputFeatures`s.&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 对句子a进行分词
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 对句子b进行分词
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# 对tokens_a和tokens_b进行裁剪，保证总长度不大于seq_length - 3
&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# -3是因为有[CLS], [SEP], [SEP]
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;_truncate_seq_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# -2是因为没有tokens_b的时候，只有[CLS], [SEP]
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# The convention in BERT is:
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# (a) For sequence pairs:
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#  tokens:   [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#  type_ids: 0     0  0    0    0     0       0 0     1  1  1  1   1 1
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# (b) For single sequences:
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#  tokens:   [CLS] the dog is hairy . [SEP]
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#  type_ids: 0     0   0   0  0     0 0
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 其中，&quot;type_ids&quot;表示是第一句(0)还是第二句(1)
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 对于classification tasks, [CLS]的向量可以被当做是&quot;sentence vector&quot;. 当然，只有当整个model已经fine-tuned的时候，这才make sense
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[CLS]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[SEP]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[SEP]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 将token转成id（在tokenizer中，读vocab文件，行号就是其id，所以不能简单地增量训）
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert_tokens_to_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 因为seq_len可能比实际输入的序列长，所以需要padding
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 实际输入的mask是1
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 比实际输入长，到seq_length的部分，用0进行padding，mask也写成0
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 注意，vocab文件中，第0行，也就是第0个token，是[PAD]，专门用来padding的
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ex_index是第几个输入example，只有前5个example打这个日志
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;*** Example ***&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;unique_id: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;tokens: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printable_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input_ids: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input_mask: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&quot;input_type_ids: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;InputFeatures&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;input-fn-builder&quot;&gt;input-fn-builder&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;input_fn_builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Creates an `input_fn` closure to be passed to TPUEstimator.&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;all_unique_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;all_input_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;all_input_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;all_input_type_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;all_unique_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;all_input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;all_input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;all_input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;The actual input function.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;batch_size&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;num_examples&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# This is for demo purposes and does NOT scale to large data sets. We do
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# not use Dataset.from_generator() because that uses tf.py_func which is
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# not TPU compatible. The right way to load data is with TFRecordReader.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_tensor_slices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;unique_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_unique_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;all_input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;input_mask&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;all_input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;input_type_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;all_input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;drop_remainder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;model-fn-builder&quot;&gt;model-fn-builder&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;model_fn_builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Returns `model_fn` closure for TPUEstimator.&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;model_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# pylint: disable=unused-argument
&lt;/span&gt;    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;The `model_fn` for TPUEstimator.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 这里的features是input_fn_builder的输出，格式如上
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;unique_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;unique_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input_mask&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input_type_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modeling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BertModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;is_training&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;token_type_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_type_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PREDICT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Only PREDICT modes are supported: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;tvars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainable_variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;scaffold_fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# initialized_variable_names: 有哪些变量在checkpoint中已经初始化了
&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assignment_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;initialized_variable_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modeling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_assignment_map_from_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;tvars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tpu_scaffold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_from_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignment_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Scaffold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;scaffold_fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tpu_scaffold&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_from_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignment_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;**** Trainable Variables ****&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tvars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;init_string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initialized_variable_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 这些变量在checkpoint中已经init了
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;init_string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;, *INIT_FROM_CKPT*&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;  name = %s, shape = %s%s&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;init_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 是一个list，每个元素的shape是[batch_size, seq_length, hidden_size]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;all_layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_all_encoder_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;unique_id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unique_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;## 对于需要输出的indexes，从all_layers里取出来
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer_output_%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;output_spec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TPUEstimatorSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scaffold_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scaffold_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_spec&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_fn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;main&quot;&gt;main&lt;/h4&gt;

&lt;p&gt;首先，定义如下几个变量：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_verbosity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;INFO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 期望输出的layer_index们，例如: -1,-2,-3
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;layer_indexes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;bert_config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modeling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BertConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_json_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert_config_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 切词类
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullTokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;vocab_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;do_lower_case&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;do_lower_case&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;is_per_host&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InputPipelineConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PER_HOST_V2&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# tpu run_config
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;run_config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RunConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;master&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;master&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tpu_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TPUConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;num_shards&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_tpu_cores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;per_host_input_for_training&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_per_host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;# 读文件
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;examples&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 切词，并保证句子a+句子b再加上padding和[CLS]/[SEP]等的总长度不大于max_seq_length
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 把unique_id/oken/input_ids/mask/input_type_ids存在features中
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;convert_examples_to_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;examples&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# unique_id就是输入样本的行号，把每行对应的具体feature存到dict里
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;unique_id_to_feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;unique_id_to_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;model_fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_fn_builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;bert_config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_checkpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;layer_indexes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;use_tpu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_one_hot_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# If TPU is not available, this will fall back to normal Estimator on CPU
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# or GPU.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TPUEstimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;use_tpu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_tpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;model_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;predict_batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_fn_builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;codecs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getwriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                               &lt;span class=&quot;s&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# yield_single_examples参数是True时，会把一个batch的结果拆成batch条结果。
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 如果是False，不分解，当结果的第一维不是batch_size时要这么用~
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yield_single_examples&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;unique_id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unique_id_to_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;output_json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;output_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;linex_index&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unique_id&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 第几个样本
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;all_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;all_layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer_output_%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;index&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_index&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;values&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
              &lt;span class=&quot;nb&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flat&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;all_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;token&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layers&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_layers&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;all_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;output_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;features&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_features&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;pretrain&quot;&gt;pretrain&lt;/h2&gt;

&lt;h3 id=&quot;create-pretraining-datapy&quot;&gt;create-pretraining-data.py&lt;/h3&gt;

&lt;p&gt;将输入文件转换成tfrecords格式&lt;/p&gt;

&lt;h4 id=&quot;工具函数与类&quot;&gt;工具函数与类&lt;/h4&gt;

&lt;h5 id=&quot;traininginstance&quot;&gt;TrainingInstance&lt;/h5&gt;

&lt;p&gt;有以下几个成员变量：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;tokens&lt;/li&gt;
  &lt;li&gt;segment_ids&lt;/li&gt;
  &lt;li&gt;is_random_next&lt;/li&gt;
  &lt;li&gt;masked_lm_positions&lt;/li&gt;
  &lt;li&gt;masked_lm_labels&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TrainingInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;A single training instance (sentence pair).&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__str__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tokens: %s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printable_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;segment_ids: %s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;is_random_next: %s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;masked_lm_positions: %s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;masked_lm_labels: %s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printable_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__repr__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__str__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;create-int-feature&quot;&gt;create-int-feature&lt;/h5&gt;

&lt;p&gt;生成int特征&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_int_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int64_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Int64List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;create-float-feature&quot;&gt;create-float-feature&lt;/h5&gt;

&lt;p&gt;生成float特征&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_float_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FloatList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;write-instance-to-example-files&quot;&gt;write-instance-to-example-files&lt;/h5&gt;

&lt;p&gt;将结果落盘&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;write_instance_to_example_files&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                    &lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_files&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Create TF example files from `TrainingInstance`s.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;writers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_file&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_files&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;writers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;python_io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TFRecordWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;writer_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;total_written&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inst_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 使用convert_tokens_to_ids将tokens转换为对应的input_ids
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert_tokens_to_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;masked_lm_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert_tokens_to_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;masked_lm_weights&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;masked_lm_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;masked_lm_weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;next_sentence_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_int_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input_mask&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_int_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;segment_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_int_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;masked_lm_positions&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_int_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;masked_lm_ids&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_int_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;masked_lm_weights&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_float_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;next_sentence_labels&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_int_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_sentence_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;tf_example&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;writers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writer_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf_example&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SerializeToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;writer_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writer_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;total_written&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inst_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;*** Example ***&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;tokens: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printable_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int64_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int64_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;&quot;%s: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])))&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;writers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Wrote %d total instances&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_written&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;main-1&quot;&gt;main&lt;/h4&gt;

&lt;p&gt;首先建立tokenizer&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullTokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;vocab_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;do_lower_case&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;do_lower_case&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后create_training_instances&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_training_instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;input_files&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dupe_factor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;short_seq_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后把结果落盘：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;write_instance_to_example_files&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                  &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_files&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;create-training-instances&quot;&gt;create-training-instances&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_training_instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_files&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                              &lt;span class=&quot;n&quot;&gt;dupe_factor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_seq_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                              &lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Create `TrainingInstance`s from raw text.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[]]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Input file format:
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# (1) One sentence per line. These should ideally be actual sentences, not
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# entire paragraphs or arbitrary spans of text. (Because we use the
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# sentence boundaries for the &quot;next sentence prediction&quot; task).
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# (2) Blank lines between documents. Document boundaries are needed so
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# that the &quot;next sentence prediction&quot; task doesn't span between documents.
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# (1) 一行一句话，最好就是一句完整的话，而不是一段话或者半句话（因为会使用句子边界来给next sentence prediction任务用）
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# (2) 文档间用空行隔开
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_files&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert_to_unicode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Empty lines are used as document delimiters
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# 空行被视为文档的分隔符
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Remove empty documents
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 全部load到内存，所以如果有非常多的训练语料，建议在外面拆分成多个小文件，然后多进程调用这个函数。
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;vocab_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dupe_factor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;document_index&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;## 调用这个函数生成一条ins
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;create_instances_from_document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;document_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_seq_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;masked_lm_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;truncate-seq-paircreate-pretrain中&quot;&gt;truncate-seq-pair(create-pretrain中)&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;truncate_seq_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_num_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Truncates a pair of sequences to a maximum sequence length.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;total_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_num_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## a和b两个部分，每次对比较长的那个进行trunc，当两部分差不多长时，交替trunc，保证不要一直对一个部分去trunc。。
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;trunc_tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trunc_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# We want to sometimes truncate from the front and sometimes from the
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# back to add more randomness and avoid biases.
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 一半的概率扔掉头，一半概率扔掉尾
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trunc_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;trunc_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;create-instances-from-document&quot;&gt;create-instances-from-document&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_instances_from_document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;document_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_seq_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;masked_lm_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Creates `TrainingInstance`s for a single document.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 获取当前文档
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;document&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;document_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Account for [CLS], [SEP], [SEP]
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;max_num_tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# We *usually* want to fill up the entire sequence since we are padding
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# to `max_seq_length` anyways, so short sequences are generally wasted
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# computation. However, we *sometimes*
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# (i.e., short_seq_prob == 0.1 == 10% of the time) want to use shorter
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# sequences to minimize the mismatch between pre-training and fine-tuning.
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# The `target_seq_length` is just a rough target however, whereas
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# `max_seq_length` is a hard limit.
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 有一定的比例，如10%的概率，我们使用比较短的序列长度，以缓解预训练的长序列和finetune阶段（可能的）短序列的不一致情况
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;target_seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_num_tokens&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_seq_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;target_seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_num_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# We DON'T just concatenate all of the tokens from a document into a long
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# sequence and choose an arbitrary split point because this would make the
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# next sentence prediction task too easy. Instead, we split the input into
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# segments &quot;A&quot; and &quot;B&quot; based on the actual &quot;sentences&quot; provided by the user
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# input.
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# A和B都使用完整的句子，而非半句话
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;current_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#获取当前的一句话，并扔到current_chunk里
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;current_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# curren_length是这些句子的总长度
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;## 要么遍历完文档了，要么句子总长度已经超过target_seq_length了（够一条样本了）
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target_seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# `a_end` is how many segments from `current_chunk` go into the `A`
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# (first) sentence.
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# a_end是当前chunk的多少句话可以放到A的候选中
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;a_end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;a_end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 从第0句到第a_end句，首尾相连！！扔到tokens_a里
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Random next
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 当文档只有一句话，或者以0.5的概率，从其他文档随机采样
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;target_b_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target_seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

          &lt;span class=&quot;c1&quot;&gt;# This should rarely go for more than one iteration for large
&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# corpora. However, just to be careful, we try to make sure that
&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# the random document is not the same as the document
&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# we're processing.
&lt;/span&gt;          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;random_document_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_document_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;document_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
              &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;# 随机一个文档出来
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;random_document&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_documents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_document_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;random_start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;# 在这篇随机出的文档中，随机出一个位置，这个位置开始的后面所有句子，首尾相连地连起来！！作为tokens_b
&lt;/span&gt;          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target_b_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
              &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;# We didn't actually use these segments so we &quot;put them back&quot; so
&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# they don't go to waste.
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;num_unused_segments&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_end&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_unused_segments&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Actual next
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;truncate_seq_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_num_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[CLS]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[SEP]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;[SEP]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_masked_lm_predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TrainingInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_random_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;current_chunk&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;current_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;create-masked-lm-predictions&quot;&gt;create-masked-lm-predictions&lt;/h4&gt;

&lt;p&gt;首先定义了一个namedtuple：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;MaskedLmInstance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;namedtuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;MaskedLmInstance&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                          &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;index&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后是这个函数的实现：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_masked_lm_predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                 &lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Creates the predictions for the masked LM objective.&quot;&quot;&quot;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;cand_indexes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;[CLS]&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;[SEP]&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# wordpieces涉及到Whole Word Masking(WWM)。wordpieces中，同一个词的第一个token没有marker，中间的token以##开头，所以见到##开头的话，就和前一个词的下标拼到一起去！！
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 如果是中文，可能参考albert_zh的实现：https://github.com/daiwk/dl-frame/blob/43d120ad5cb6e1766b0a0b26f8f5c5376ec36069/demos/thirdparty/albert_zh/resources/create_pretraining_data_roberta.py#L261
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Whole Word Masking means that if we mask all of the wordpieces
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# corresponding to an original word. When a word has been split into
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# WordPieces, the first token does not have any marker and any subsequence
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# tokens are prefixed with ##. So whenever we see the ## token, we
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# append it to the previous set of word indexes.
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Note that Whole Word Masking does *not* change the training code
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# at all -- we still predict each WordPiece independently, softmaxed
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# over the entire vocabulary.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;do_whole_word_mask&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cand_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startswith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;##&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;cand_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;cand_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cand_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;output_tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;num_to_predict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_predictions_per_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                       &lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;masked_lms&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;covered_indexes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_set&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cand_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_to_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# If adding a whole-word mask would exceed the maximum number of
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# predictions, then just skip this candidate.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_to_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;is_any_index_covered&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;covered_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;is_any_index_covered&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_any_index_covered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;covered_indexes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;masked_token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# 80% of the time, replace with [MASK]
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;masked_token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;[MASK]&quot;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 10% of the time, keep original
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;masked_token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 10% of the time, replace with random word
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;masked_token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;output_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_token&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;masked_lms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MaskedLmInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_to_predict&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;masked_lms&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_lms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;masked_lm_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;run-pretrainingpy&quot;&gt;run-pretraining.py&lt;/h3&gt;

&lt;p&gt;读入tfrecords格式的训练样本，进行训练&lt;/p&gt;

&lt;h2 id=&quot;classification&quot;&gt;classification&lt;/h2&gt;

&lt;p&gt;使用自己的数据集基于现有模型进行finetune&lt;/p&gt;

&lt;h3 id=&quot;run-classifierpy&quot;&gt;run-classifier.py&lt;/h3&gt;

&lt;h2 id=&quot;squad&quot;&gt;squad&lt;/h2&gt;

&lt;h3 id=&quot;run-squadpy&quot;&gt;run-squad.py&lt;/h3&gt;

&lt;h2 id=&quot;可视化分析&quot;&gt;可视化分析&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://towardsdatascience.com/deconstructing-bert-part-2-visualizing-the-inner-workings-of-attention-60a16d86b5c1&quot;&gt;https://towardsdatascience.com/deconstructing-bert-part-2-visualizing-the-inner-workings-of-attention-60a16d86b5c1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github：&lt;a href=&quot;https://github.com/jessevig/bertviz&quot;&gt;https://github.com/jessevig/bertviz&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>googlecloud k8s</title>
   <link href="http://daiwk.github.io/posts/platform-googlecloud-k8s.html"/>
   <updated>2018-10-16T00:00:00+00:00</updated>
   <id>/posts/platform-googlecloud-k8s</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650749988&amp;amp;idx=3&amp;amp;sn=3a890b8ca2dab7cb3808b1cdd0a9d976&amp;amp;chksm=871af85ab06d714c02be8194c44722c6d789085919960e7f55a1353e983ba17b0d60a48e39bd&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1017sZUAdSKXL1wB7wZJPkPI&amp;amp;pass_ticket=SszanYxD5OWcgQT%2B0DDH7ckogHOwJlBON1eETcKdrqvcCwwsdmrx3DePGS5Lponk#rd&quot;&gt;教程 | 如何使用 Kubernetes 轻松部署深度学习模型&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>bert</title>
   <link href="http://daiwk.github.io/posts/nlp-bert.html"/>
   <updated>2018-10-13T00:00:00+00:00</updated>
   <id>/posts/nlp-bert</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%A6%82%E8%BF%B0&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#bert&quot;&gt;BERT&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%A8%A1%E5%9E%8B%E6%9E%B6%E6%9E%84&quot;&gt;模型架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#input-representation&quot;&gt;Input Representation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pre-training-tasks&quot;&gt;Pre-training Tasks&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#task-1-masked-lm&quot;&gt;Task #1: Masked LM&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#task-2-next-sentence-prediction&quot;&gt;Task #2: Next Sentence Prediction&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pre-training-procedure&quot;&gt;Pre-training Procedure&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#fine-tuning-procedure&quot;&gt;Fine-tuning Procedure&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#comparison-of-bert-and-openai-gpt&quot;&gt;Comparison of BERT and OpenAI GPT&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%AE%9E%E9%AA%8C&quot;&gt;实验&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#glue-datasets&quot;&gt;GLUE Datasets&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#glue-results&quot;&gt;GLUE Results&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#squad-v11&quot;&gt;SQuAD v1.1&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#named-entity-recognition&quot;&gt;Named Entity Recognition&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#swag&quot;&gt;SWAG&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ablation-studies&quot;&gt;Ablation Studies&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#effect-of-pre-training-tasks&quot;&gt;Effect of Pre-training Tasks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#effect-of-model-size&quot;&gt;Effect of Model Size&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#effect-of-number-of-training-steps&quot;&gt;Effect of Number of Training Steps&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#feature-based-approach-with-bert&quot;&gt;Feature-based Approach with BERT&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0&quot;&gt;代码实现&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#pytorch%E7%89%88%E6%9C%AC&quot;&gt;pytorch版本&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E4%BB%A3%E7%A0%81%E8%A7%A3%E8%AF%BB&quot;&gt;代码解读&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86&quot;&gt;基础知识&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#position-encoding&quot;&gt;position encoding&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#position-wise-feed-forward&quot;&gt;position-wise feed forward&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#attention%E5%92%8Cmulti-head-attention&quot;&gt;attention和Multi-head attention&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#layernorm%E5%92%8Csublayer&quot;&gt;layernorm和sublayer&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%AE%8C%E6%95%B4%E7%9A%84bert&quot;&gt;完整的bert&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#vocab%E5%92%8Cdataset&quot;&gt;vocab和dataset&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#main%E5%87%BD%E6%95%B0&quot;&gt;main函数&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%AE%98%E6%96%B9%E7%89%88&quot;&gt;官方版&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jiqizhixin.com/articles/2018-10-12-13&quot;&gt;最强NLP预训练模型！谷歌BERT横扫11项NLP任务记录&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.zhihu.com/question/298203515/answer/509703208&quot;&gt;https://www.zhihu.com/question/298203515/answer/509703208&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;本文介绍了一种新的语言表征模型BERT——来自&lt;strong&gt;Transformer的双向编码器表征&lt;/strong&gt;。与最近的语言表征模型不同，BERT旨在基于&lt;strong&gt;所有层&lt;/strong&gt;的&lt;strong&gt;左、右语境&lt;/strong&gt;来预训练深度双向表征。BERT是首个在&lt;strong&gt;大批句子层面&lt;/strong&gt;和&lt;strong&gt;token层面&lt;/strong&gt;任务中取得当前最优性能的&lt;strong&gt;基于微调的表征模型&lt;/strong&gt;，其性能超越许多使用任务特定架构的系统，刷新了11项NLP任务的当前最优性能记录。&lt;/p&gt;

&lt;p&gt;目前将预训练语言表征应用于下游任务存在两种策略：feature-based的策略和fine-tuning策略。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;feature-based策略（如 ELMo）使用&lt;strong&gt;将预训练表征&lt;/strong&gt;作为&lt;strong&gt;额外特征&lt;/strong&gt;的任务专用架构。&lt;/li&gt;
  &lt;li&gt;fine-tuning策略（如生成预训练 Transformer (OpenAI GPT)）引入了&lt;strong&gt;任务特定最小参数&lt;/strong&gt;，通过&lt;strong&gt;简单地微调预训练参数&lt;/strong&gt;在下游任务中进行训练。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在之前的研究中，两种策略在&lt;strong&gt;预训练期间使用相同的目标函数&lt;/strong&gt;，利用&lt;strong&gt;单向语言模型&lt;/strong&gt;来学习通用语言表征。&lt;/p&gt;

&lt;p&gt;作者认为现有的技术严重制约了预训练表征的能力，微调策略尤其如此。其主要局限在于&lt;strong&gt;标准语言模型是单向&lt;/strong&gt;的，这&lt;strong&gt;限制了可以在预训练期间使用的架构类型&lt;/strong&gt;。例如，OpenAI GPT使用的是&lt;strong&gt;从左到右&lt;/strong&gt;的架构，其中&lt;strong&gt;每个token只能注意Transformer自注意力层中的先前token&lt;/strong&gt;。这些局限对于&lt;strong&gt;句子层面的任务&lt;/strong&gt;而言不是最佳选择，对于&lt;strong&gt;token级任务&lt;/strong&gt;（如 SQuAD 问答）则可能是毁灭性的，&lt;strong&gt;因为在这种任务中，结合两个方向的语境至关重要&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;BERT（Bidirectional Encoder Representations from Transformers）改进了&lt;strong&gt;基于微调的策略&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;BERT提出一种新的&lt;strong&gt;预训练目标&lt;/strong&gt;——&lt;strong&gt;遮蔽语言模型（masked language model，MLM）&lt;/strong&gt;，来克服上文提到的单向局限。MLM 的灵感来自 Cloze 任务（Taylor, 1953）。MLM&lt;strong&gt;随机遮蔽输入中的一些token&lt;/strong&gt;，目标在于&lt;strong&gt;仅基于遮蔽词的语境&lt;/strong&gt;来&lt;strong&gt;预测其原始词汇id&lt;/strong&gt;。与从左到右的语言模型预训练不同，MLM目标&lt;strong&gt;允许表征融合左右两侧的语境&lt;/strong&gt;，从而&lt;strong&gt;预训练一个深度双向Transformer&lt;/strong&gt;。除了 MLM，我们还引入了一个&lt;strong&gt;“下一句预测”（next sentence prediction）任务&lt;/strong&gt;，该任务&lt;strong&gt;联合预训练&lt;/strong&gt;文本对表征。&lt;/p&gt;

&lt;p&gt;贡献：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;展示了&lt;strong&gt;双向预训练&lt;/strong&gt;语言表征的重要性。不同于 Radford 等人（2018）使用&lt;strong&gt;单向语言模型进行预训练&lt;/strong&gt;，BERT使用MLM预训练深度双向表征。本研究与 Peters 等人（2018）的研究也不同，后者使用的是&lt;strong&gt;独立训练&lt;/strong&gt;的&lt;strong&gt;从左到右和从右到左LM&lt;/strong&gt;的&lt;strong&gt;浅层级联&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;证明了&lt;strong&gt;预训练表征&lt;/strong&gt;可以&lt;strong&gt;消除对许多精心设计的任务特定架构的需求&lt;/strong&gt;。BERT是首个在大批句子层面和token层面任务中取得当前最优性能的基于微调的表征模型，其性能超越许多使用任务特定架构的系统。&lt;/li&gt;
  &lt;li&gt;BERT 刷新了11项NLP任务的当前最优性能记录。本论文还报告了BERT的模型简化测试（ablation study），证明该模型的&lt;strong&gt;双向特性是最重要的一项新贡献&lt;/strong&gt;。代码和预训练模型将发布在&lt;a href=&quot;goo.gl/language/bert&quot;&gt;goo.gl/language/bert&lt;/a&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bert&quot;&gt;BERT&lt;/h2&gt;

&lt;h3 id=&quot;模型架构&quot;&gt;模型架构&lt;/h3&gt;

&lt;p&gt;BERT 旨在基于所有层的左、右语境来预训练深度双向表征。因此，预训练的 BERT 表征可以仅用一个额外的输出层进行微调，进而为很多任务（如问答和语言推断任务）创建当前最优模型，无需对任务特定架构做出大量修改。&lt;/p&gt;

&lt;p&gt;BERT 的模型架构是一个多层双向Transformer编码器，基于Vaswani 等人 (2017)描述的原始实现，在tensor2tensor库中发布(当然，可以抽空看看&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&quot;&gt;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&lt;/a&gt;和&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensor-to-tensor-coding.html&quot;&gt;https://daiwk.github.io/posts/platform-tensor-to-tensor-coding.html&lt;/a&gt;)。&lt;/p&gt;

&lt;p&gt;本文中，我们将&lt;strong&gt;层数（即Transformer块）&lt;/strong&gt;表示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L\)&lt;/code&gt;，将&lt;strong&gt;隐层的size&lt;/strong&gt;表示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;、&lt;strong&gt;自注意力头数&lt;/strong&gt;表示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;。在所有实验中，我们将feed-forward/filter的size设置为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(4H\)&lt;/code&gt;，即H=768时为3072，H=1024时为4096。我们主要看下在两种模型尺寸上的结果：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(BERT_{BASE}\)&lt;/code&gt;&lt;/strong&gt;: L=12, H=768, A=12, Total Parameters=110M&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(BERT_{LARGE}\)&lt;/code&gt;&lt;/strong&gt;:  L=24, H=1024, A=16, Total Parameters=340M&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(BERT_{BASE}\)&lt;/code&gt;和OpenAI GPT的大小是一样的。BERT Transformer使用双向自注意力机制，而GPT Transformer使用受限的自注意力机制，导致每个token只能关注其左侧的语境。&lt;strong&gt;双向Transformer&lt;/strong&gt;在文献中通常称为&lt;strong&gt;“Transformer 编码器”&lt;/strong&gt;，而只&lt;strong&gt;关注左侧语境的版本&lt;/strong&gt;则因能用于文本生成而被称为&lt;strong&gt;“Transformer 解码器”&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;下图显示了BERT/GPT Transformer/ELMo的结构区别：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/bert-gpt-transformer-elmo.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;BERT 使用双向Transformer&lt;/li&gt;
  &lt;li&gt;OpenAI GPT 使用从左到右的Transformer&lt;/li&gt;
  &lt;li&gt;ELMo 使用独立训练的从左到右和从右到左LSTM的级联来生成下游任务的特征。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;三种模型中，只有BERT表征会基于&lt;strong&gt;所有层中的左右两侧语境&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;input-representation&quot;&gt;Input Representation&lt;/h3&gt;

&lt;p&gt;论文的输入表示（input representation）能够在一个token序列中明确地表示&lt;strong&gt;单个文本句子&lt;/strong&gt;或&lt;strong&gt;一对文本句子&lt;/strong&gt;（例如， [Question, Answer]）。对于给定token，其输入表示通过对相应的token、segment和position embeddings进行求和来构造：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/bert-input-representation.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;使用WordPiece嵌入【GNMT，&lt;a href=&quot;https://arxiv.org/abs/1609.08144&quot;&gt;Google’s neural machine translation system: Bridging the gap between human and machine translation&lt;/a&gt;】和30,000个token的词汇表。用##表示分词。&lt;/li&gt;
  &lt;li&gt;使用learned positional embeddings，支持的序列长度最多为512个token。&lt;/li&gt;
  &lt;li&gt;每个序列的第一个token始终是特殊分类嵌入（[CLS]）。对应于该token的最终隐藏状态（即，Transformer的输出）被用作分类任务的聚合序列表示。对于非分类任务，将忽略此向量。&lt;/li&gt;
  &lt;li&gt;句子对被打包成一个序列。以两种方式区分句子。
    &lt;ul&gt;
      &lt;li&gt;首先，用特殊标记（[SEP]）将它们分开。&lt;/li&gt;
      &lt;li&gt;其次，添加一个learned sentence A嵌入到第一个句子的每个token中，一个sentence B嵌入到第二个句子的每个token中。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;对于单个句子输入，只使用 sentence A嵌入。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;pre-training-tasks&quot;&gt;Pre-training Tasks&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;它在训练双向语言模型时以减小的概率把少量的词替成了Mask或者另一个随机的词。感觉其目的在于使模型被迫增加对上下文的记忆。（知乎的回答）&lt;/li&gt;
  &lt;li&gt;增加了一个预测下一句的loss。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;task-1-masked-lm&quot;&gt;Task #1: Masked LM&lt;/h4&gt;

&lt;p&gt;标准条件语言模型只能从左到右或从右到左进行训练，因为双向条件作用将允许每个单词在多层上下文中间接地“see itself”。&lt;/p&gt;

&lt;p&gt;为了训练一个深度双向表示（deep bidirectional representation），研究团队采用了一种简单的方法，即随机屏蔽（masking）部分输入token，然后只预测那些被屏蔽的token。论文将这个过程称为“masked LM”(MLM)，尽管在文献中它经常被称为Cloze任务(Taylor, 1953)。&lt;/p&gt;

&lt;p&gt;在这个例子中，与masked token对应的最终隐藏向量被输入到词汇表上的输出softmax中，就像在标准LM中一样。在团队所有实验中，随机地屏蔽了每个序列中15%的WordPiece token。与去噪的自动编码器（Vincent et al.， 2008）相反，只预测masked words而不是重建整个输入。&lt;/p&gt;

&lt;p&gt;虽然这确实能让团队获得双向预训练模型，但这种方法有两个缺点。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;缺点1：预训练和finetuning之间不匹配，因为在finetuning期间从未看到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[MASK]&lt;/code&gt;token。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;为了解决这个问题，团队并不总是用实际的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[MASK]&lt;/code&gt;token替换被“masked”的词汇。相反，训练数据生成器&lt;strong&gt;随机选择15％的token&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;例如在这个句子“my dog is hairy”中，它选择的token是“hairy”。然后，执行以下过程：&lt;/p&gt;

&lt;p&gt;数据生成器将执行以下操作，而不是始终用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[MASK]&lt;/code&gt;替换所选单词：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;80％的时间：用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[MASK]&lt;/code&gt;标记替换单词，例如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my dog is hairy → my dog is [MASK]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;10％的时间：用一个&lt;strong&gt;随机的单词&lt;/strong&gt;替换该单词，例如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my dog is hairy → my dog is apple&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;10％的时间：&lt;strong&gt;保持单词不变&lt;/strong&gt;，例如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my dog is hairy → my dog is hairy&lt;/code&gt;. 这样做的目的是将表示偏向于实际观察到的单词。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Transformer encoder不知道它将被要求预测哪些单词或哪些单词已被随机单词替换，因此它&lt;strong&gt;被迫保持每个输入token的分布式上下文表示&lt;/strong&gt;。此外，因为随机替换只发生在所有token的1.5％（即&lt;strong&gt;15％的10％&lt;/strong&gt;），这似乎不会损害模型的语言理解能力。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;缺点2：每个batch只预测了15％的token，这表明模型可能需要更多的预训练步骤才能收敛。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;团队证明MLM的收敛速度略慢于 left-to-right的模型（预测每个token），但MLM模型在实验上获得的提升远远超过增加的训练成本。&lt;/p&gt;

&lt;h4 id=&quot;task-2-next-sentence-prediction&quot;&gt;Task #2: Next Sentence Prediction&lt;/h4&gt;

&lt;p&gt;在为了训练一个理解句子的模型关系，预先训练一个二分类的下一句测任务，这一任务可以从任何单语语料库中生成。具体地说，当选择句子A和B作为预训练样本时，B有50％的可能是A的下一个句子，也有50％的可能是来自语料库的随机句子。例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Input &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;CLS] the man went to &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;MASK] store &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;SEP]
he bought a gallon &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;MASK] milk &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;SEP]
Label &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; IsNext

Input &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;CLS] the man &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;MASK] to the store &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;SEP]
penguin &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;MASK] are flight &lt;span class=&quot;c&quot;&gt;##less birds [SEP]&lt;/span&gt;
Label &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; NotNext
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;完全随机地选择了NotNext语句，最终的预训练模型在此任务上实现了97％-98％的准确率。&lt;/p&gt;

&lt;h3 id=&quot;pre-training-procedure&quot;&gt;Pre-training Procedure&lt;/h3&gt;

&lt;p&gt;使用gelu激活函数（&lt;a href=&quot;http://arxiv.org/abs/1606.08415&quot;&gt;Bridging nonlinearities and stochastic regularizers with gaus- sian error linear units&lt;/a&gt;），在pytorch里实现如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GELU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    Paper Section 3.4, last paragraph notice that BERT used the GELU instead of RELU
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tanh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.044715&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;fine-tuning-procedure&quot;&gt;Fine-tuning Procedure&lt;/h3&gt;

&lt;h3 id=&quot;comparison-of-bert-and-openai-gpt&quot;&gt;Comparison of BERT and OpenAI GPT&lt;/h3&gt;

&lt;h2 id=&quot;实验&quot;&gt;实验&lt;/h2&gt;

&lt;p&gt;网络结构如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/bert-specific-models.png&quot; style=&quot;max-height: 500px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;glue-datasets&quot;&gt;GLUE Datasets&lt;/h3&gt;

&lt;h4 id=&quot;glue-results&quot;&gt;GLUE Results&lt;/h4&gt;

&lt;h3 id=&quot;squad-v11&quot;&gt;SQuAD v1.1&lt;/h3&gt;

&lt;h3 id=&quot;named-entity-recognition&quot;&gt;Named Entity Recognition&lt;/h3&gt;

&lt;h3 id=&quot;swag&quot;&gt;SWAG&lt;/h3&gt;

&lt;h2 id=&quot;ablation-studies&quot;&gt;Ablation Studies&lt;/h2&gt;

&lt;h3 id=&quot;effect-of-pre-training-tasks&quot;&gt;Effect of Pre-training Tasks&lt;/h3&gt;

&lt;h3 id=&quot;effect-of-model-size&quot;&gt;Effect of Model Size&lt;/h3&gt;

&lt;h3 id=&quot;effect-of-number-of-training-steps&quot;&gt;Effect of Number of Training Steps&lt;/h3&gt;

&lt;h3 id=&quot;feature-based-approach-with-bert&quot;&gt;Feature-based Approach with BERT&lt;/h3&gt;

&lt;h2 id=&quot;代码实现&quot;&gt;代码实现&lt;/h2&gt;

&lt;h3 id=&quot;pytorch版本&quot;&gt;pytorch版本&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/codertimo/BERT-pytorch&quot;&gt;https://github.com/codertimo/BERT-pytorch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;fork了一份：&lt;a href=&quot;https://github.com/daiwk/BERT-pytorch&quot;&gt;https://github.com/daiwk/BERT-pytorch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data/corpus.small&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Welcome to the &lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt; the jungle &lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;
I can stay &lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt; here all night &lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可视化，需要：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;brew &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;graphviz &lt;span class=&quot;c&quot;&gt;# mac&lt;/span&gt;
pip3 &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;git+https://github.com/szagoruyko/pytorchviz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;画出bert的架构图的方法(先生成vocab，如果机器的dot不支持pdf，只支持png/jpg等，需要在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib/python3.6/site-packages/torchviz/dot.py&lt;/code&gt;中把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dot = Digraph(node_attr=node_attr, graph_attr=dict(size=&quot;12,12&quot;))&lt;/code&gt;改成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dot = Digraph(node_attr=node_attr, graph_attr=dict(size=&quot;12,12&quot;), format=&quot;png&quot;)&lt;/code&gt;)：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;torch&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;torch&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;torchviz&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make_dot_from_trace&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./bert_pytorch-0.0.1a4.src/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#from trainer import BERTTrainer
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTLM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERT&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTDataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WordVocab&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;torch.utils.data&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataLoader&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;demo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lstm_cell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LSTMCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lstm_cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lstm_cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;named_parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;file_out&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;xx&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bert_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_dataset_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data/bert_train_data.xxx&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data/vocab.all.xxx&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WordVocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;train_dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTDataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_dataset_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


    &lt;span class=&quot;n&quot;&gt;train_data_loader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataLoader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_workers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cpu&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mymodel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTLM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_data_loader&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out_idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out_idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;make_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mymodel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bert_input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;segment_label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mymodel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;named_parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./bert-arch&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;bert_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以画出这么个图。。图太大，自己下载看看&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/bert-arch.jpeg&quot;&gt;https://daiwk.github.io/assets/bert-arch.jpeg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对应的pdf如&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/bert-arch.pdf&quot;&gt;https://daiwk.github.io/assets/bert-arch.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对应的dot文件&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/bert-arch&quot;&gt;https://daiwk.github.io/assets/bert-arch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;把dot文件转换成其他格式的方式：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./bert-arch
&lt;span class=&quot;nv&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./bert-arch
dot &lt;span class=&quot;nv&quot;&gt;$input&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-Tjpeg&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$output&lt;/span&gt;.jpeg
dot &lt;span class=&quot;nv&quot;&gt;$input&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-Tpdf&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$output&lt;/span&gt;.pdf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;设置一个layer的简单版pdf如下：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/bert-arch-1layer.pdf&quot;&gt;https://daiwk.github.io/assets/bert-arch-1layer.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;代码解读&quot;&gt;代码解读&lt;/h4&gt;

&lt;p&gt;transformer部分参考&lt;a href=&quot;http://nlp.seas.harvard.edu/2018/04/03/attention.htm&quot;&gt;http://nlp.seas.harvard.edu/2018/04/03/attention.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;可以学习下&lt;a href=&quot;https://blog.csdn.net/stupid_3/article/details/83184691&quot;&gt;https://blog.csdn.net/stupid_3/article/details/83184691&lt;/a&gt;，讲得很细致呢！&lt;/p&gt;

&lt;h5 id=&quot;基础知识&quot;&gt;基础知识&lt;/h5&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/knowledge-pytorch-usage.html]&quot;&gt;https://daiwk.github.io/posts/knowledge-pytorch-usage.html&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;position-encoding&quot;&gt;position encoding&lt;/h5&gt;

&lt;p&gt;代码&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PositionalEncoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Implement the PE function.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PositionalEncoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# Compute the positional encodings once in log space.
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;div_term&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
                             &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;10000.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;div_term&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;div_term&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'pe'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt; 
                         &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输入是shape为(max_len, d_model)的矩阵，d_model是emb的size。如下图，输入是一个max_len=100，d_model=20的矩阵，图中画的是这20维里的4、5、6、7每一维在100个position的取值。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/position-encoding-torch.png&quot; style=&quot;max-height: 500px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;bert里改名了一下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PositionalEmbedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Compute the positional encodings once in log space.
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;require_grad&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;div_term&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;10000.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;div_term&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;div_term&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'pe'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而bert还有另外两个embedding，就是segment和token，这里用很简单的实现：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SegmentEmbedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;### 输入是segment_label，表示是第1句话，第2句话，还是padding，所以num_embeddings是3
&lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding_idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TokenEmbedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding_idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;用的时候是把三者加起来：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BERTEmbedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    BERT Embedding which is consisted with under features
        1. TokenEmbedding : normal embedding matrix
        2. PositionalEmbedding : adding positional information using sin, cos
        2. SegmentEmbedding : adding sentence segment info, (sent_A:1, sent_B:2)

        sum of all these features are output of BERTEmbedding
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :param vocab_size: total vocab size
        :param embed_size: embedding size of token embedding
        :param dropout: dropout rate
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TokenEmbedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PositionalEmbedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SegmentEmbedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这部分画出来的图就应该是下面这个了：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/bert-emb-part.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h5 id=&quot;position-wise-feed-forward&quot;&gt;position-wise feed forward&lt;/h5&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PositionwiseFeedForward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Implements FFN equation.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PositionwiseFeedForward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在bert中，把relu改为gelu，所以：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GELU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    Paper Section 3.4, last paragraph notice that BERT used the GELU instead of RELU
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tanh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.044715&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PositionwiseFeedForward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Implements FFN equation.&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PositionwiseFeedForward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GELU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;attention和multi-head-attention&quot;&gt;attention和Multi-head attention&lt;/h5&gt;

&lt;p&gt;代码如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Compute 'Scaled Dot Product Attention'&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;d_k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; \
             &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MultiHeadedAttention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;Take in model size and number of heads.&quot;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MultiHeadedAttention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# We assume d_v always equals d_k
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linears&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;Implements Figure 2&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# Same mask applied to all h heads.
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;nbatches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# 1) Do all the linear projections in batch from d_model =&amp;gt; h x d_k 
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; \
            &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nbatches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
             &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linears&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# 2) Apply attention on all the projected vectors in batch. 
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                                 &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# 3) &quot;Concat&quot; using a view and apply a final linear. 
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contiguous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; \
             &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nbatches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linears&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注：&lt;/p&gt;

&lt;p&gt;画出来的图可以参考[&lt;a href=&quot;https://daiwk.github.io/assets/bert-arch-1layer.pdf&quot;&gt;https://daiwk.github.io/assets/bert-arch-1layer.pdf&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;有4个Linear，其中三个分别和q,k,v相乘，最后一个和concat后的相乘。大小都是d_model,d_model。因为d_k=d_v=d_model/h，对于q来讲，有h个(d_k, d_model)，所以一个(d_model, d_model)就行了。k,v同理。当然，后面还搞了下batches，所以画出来的图是q和k先bmm一下，再和v去bmm一下，最后的concat是就是view一下，然后再和最后那个linear去mm一下。&lt;/p&gt;

&lt;p&gt;封装一下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    Compute 'Scaled Dot Product Attention
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; \
                 &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masked_fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scores&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p_attn&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MultiHeadedAttention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    Take in model size and number of heads.
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# We assume d_v always equals d_k
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear_layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModuleList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_linear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# 1) Do all the linear projections in batch from d_model =&amp;gt; h x d_k
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                             &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# 2) Apply attention on all the projected vectors in batch.
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# 3) &quot;Concat&quot; using a view and apply a final linear.
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contiguous&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;layernorm和sublayer&quot;&gt;layernorm和sublayer&lt;/h5&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LayerNorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Construct a layernorm module (See citation for details).&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LayerNorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keepdim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keepdim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_2&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SublayerConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    A residual connection followed by a layer norm.
    Note for code simplicity the norm is first as opposed to last.
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SublayerConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LayerNorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;Apply residual connection to any sublayer with the same size.&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;transformer里的encoder：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EncoderLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Encoder is made up of self-attn and feed forward (defined below)&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EncoderLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self_attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self_attn&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SublayerConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;Follow Figure 1 (left) for connections.&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;decoder部分：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Generic N layer decoder with masking.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LayerNorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DecoderLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Decoder is made of self-attn, src-attn, and feed forward (defined below)&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DecoderLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self_attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self_attn&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_attn&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SublayerConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;Follow Figure 1 (right) for connections.&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中的mask部分：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;subsequent_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Mask out subsequent positions.&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attn_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## np.triu：一个上三角矩阵（注意：这里是一个方阵）右上角都是1，左下角都是0
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;subsequent_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;triu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'uint8'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_numpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subsequent_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Object for holding a batch of data with mask during training.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; \
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_std_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ntokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    
    &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;staticmethod&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;make_std_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;Create a mask to hide padding and future words.&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;subsequent_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type_as&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在产出数据时把mask加上：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;data_gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nbatches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Generate random data for a src-tgt copy task.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nbatches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_numpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;V&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;src&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;整个模型：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EncoderDecoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    A standard Encoder-Decoder architecture. Base for this and many 
    other models.
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EncoderDecoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoder&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_embed&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tgt_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_embed&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;generator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generator&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;Take in and process masked src and target sequences.&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tgt_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tgt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Generator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Define standard linear + softmax generation step.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Generator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;make_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
               &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2048&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Helper: Construct a model from hyperparameters.&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deepcopy&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MultiHeadedAttention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PositionwiseFeedForward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PositionalEncoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EncoderDecoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Encoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EncoderLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DecoderLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; 
                             &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Generator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tgt_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# This was important from their code. 
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Initialize parameters with Glorot / fan_avg.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xavier_uniform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;bert中的transformerblock(相当于只有encoder，但是加入了自己的mask)：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TransformerBlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    Bidirectional Encoder = Transformer (self-attention)
    Transformer = MultiHead_Attention + Feed_Forward with sublayer connection
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_forward_hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :param hidden: hidden size of transformer
        :param attn_heads: head sizes of multi-head attention
        :param feed_forward_hidden: feed_forward_hidden, usually 4*hidden_size
        :param dropout: dropout rate
        &quot;&quot;&quot;&lt;/span&gt;

        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MultiHeadedAttention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PositionwiseFeedForward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_ff&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_forward_hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_sublayer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SublayerConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_sublayer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SublayerConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_sublayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;完整的bert&quot;&gt;完整的bert&lt;/h5&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BERT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    BERT model : Bidirectional Encoder Representations from Transformers.
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :param vocab_size: vocab_size of total words
        :param hidden: BERT model hidden size
        :param n_layers: numbers of Transformer blocks(layers)
        :param attn_heads: number of attention heads
        :param dropout: dropout rate
        &quot;&quot;&quot;&lt;/span&gt;

        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_layers&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# paper noted they used 4*hidden_size for ff_network_hidden_size
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_forward_hidden&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# embedding for BERT, sum of positional, segment, token embeddings
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTEmbedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# multi-layers transformer blocks, deep network
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transformer_blocks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModuleList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TransformerBlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# attention masking for padded token
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# torch.ByteTensor([batch_size, 1, seq_len, seq_len)
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# embedding the indexed sequence to sequence of vectors
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# running over multiple transformer blocks
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transformer&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transformer_blocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transformer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;对于pretrain来讲：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BERTLM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    BERT Language Model
    Next Sentence Prediction Model + Masked Language Model
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :param bert: BERT model which should be trained
        :param vocab_size: total vocab size for masked_lm
        &quot;&quot;&quot;&lt;/span&gt;

        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_sentence&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NextSentencePrediction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask_lm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MaskedLanguageModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask_lm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;NextSentencePrediction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    2-class classification model : is_next, is_not_next
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :param hidden: BERT model output size
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LogSoftmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MaskedLanguageModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    predicting origin token from masked input sequence
    n-class classification problem, n-class = vocab_size
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :param hidden: output size of BERT model
        :param vocab_size: total vocab size
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LogSoftmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;整个训练过程：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BERTTrainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    BERTTrainer make the pretrained BERT model with two LM training method.

        1. Masked Language Model : 3.3.1 Task #1: Masked LM
        2. Next Sentence prediction : 3.3.2 Task #2: Next Sentence Prediction

    please check the details on README.md with simple example.

    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;train_dataloader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataLoader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test_dataloader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataLoader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;betas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.999&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight_decay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warmup_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;with_cuda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cuda_devices&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        :param bert: BERT model which you want to train
        :param vocab_size: total word vocab size
        :param train_dataloader: train dataset data loader
        :param test_dataloader: test dataset data loader [can be None]
        :param lr: learning rate of optimizer
        :param betas: Adam optimizer betas
        :param weight_decay: Adam optimizer weight decay param
        :param with_cuda: traning with cuda
        :param log_freq: logging frequency of the batch iteration
        &quot;&quot;&quot;&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Setup cuda device for BERT training, argument -c, --cuda should be true
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;cuda_condition&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cuda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_available&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_cuda&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cuda:0&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cuda_condition&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;cpu&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# This BERT model will be saved every epoch
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Initialize the BERT Language Model, with BERT model
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTLM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Distributed GPU training if CUDA can detect more than 1 GPU
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_cuda&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cuda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Using %d GPUS for BERT&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cuda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataParallel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device_ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cuda_devices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Setting the train and test data loader
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_dataloader&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test_dataloader&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Setting the Adam optimizer with hyper-param
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Adam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;betas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;betas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight_decay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight_decay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optim_schedule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ScheduledOptim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_warmup_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warmup_steps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Using Negative Log Likelihood Loss function for predicting the masked_token
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterion&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NLLLoss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ignore_index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log_freq&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Total Parameters:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nelement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]))&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;iteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_loader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        loop over the data_loader for training or testing
        if on train status, backward operation is activated
        and also auto save the model every peoch

        :param epoch: current epoch index
        :param data_loader: torch.utils.data.DataLoader for iteration
        :param train: boolean value of is train or test
        :return: None
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;str_code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;train&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;test&quot;&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# Setting the tqdm progress bar
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;data_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tqdm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tqdm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_loader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                              &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;EP_%s:%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                              &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_loader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                              &lt;span class=&quot;n&quot;&gt;bar_format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{l_bar}{r_bar}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;avg_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;total_correct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;total_element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# 0. batch_data will be sent into the device(GPU or cpu)
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# 1. forward the next_sentence_prediction and masked_lm model
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;next_sent_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask_lm_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bert_input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;segment_label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# 2-1. NLL(negative log likelihood) loss of is_next classification result
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;next_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_sent_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;is_next&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# 2-2. NLLLoss of predicting masked token word
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;mask_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask_lm_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bert_label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# 2-3. Adding next_loss and mask_loss : 3.4 Pre-training Procedure
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask_loss&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# 3. backward and optimization only in train
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optim_schedule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optim_schedule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step_and_update_lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# next sentence prediction accuracy
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;correct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next_sent_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;is_next&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;avg_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;total_correct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;correct&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;total_element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;is_next&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nelement&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;post_fix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;s&quot;&gt;&quot;epoch&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;s&quot;&gt;&quot;iter&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;s&quot;&gt;&quot;avg_loss&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;s&quot;&gt;&quot;avg_acc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_correct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;s&quot;&gt;&quot;loss&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;data_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;post_fix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;EP%d_%s, avg_loss=&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;total_acc=&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;total_correct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;100.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;output/bert_trained.model&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        Saving the current BERT model on file_path

        :param epoch: current epoch number
        :param file_path: model output path which gonna be file_path+&quot;ep%d&quot; % epoch
        :return: final_output_path
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;output_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.ep%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cpu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;EP:%d Model Saved on:&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_path&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;vocab和dataset&quot;&gt;vocab和dataset&lt;/h5&gt;

&lt;p&gt;vocab部分：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TorchVocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Defines a vocabulary object that will be used to numericalize a field.
    Attributes:
        freqs: A collections.Counter object holding the frequencies of tokens
            in the data used to build the Vocab.
        stoi: A collections.defaultdict instance mapping token strings to
            numerical identifiers.
        itos: A list of token strings indexed by their numerical identifiers.
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;specials&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;pad&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'&amp;lt;oov&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;vectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unk_init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vectors_cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Create a Vocab object from a collections.Counter.
        Arguments:
            counter: collections.Counter object holding the frequencies of
                each value found in the data.
            max_size: The maximum size of the vocabulary, or None for no
                maximum. Default: None.
            min_freq: The minimum frequency needed to include a token in the
                vocabulary. Values less than 1 will be set to 1. Default: 1.
            specials: The list of special tokens (e.g., padding or eos) that
                will be prepended to the vocabulary in addition to an &amp;lt;unk&amp;gt;
                token. Default: ['&amp;lt;pad&amp;gt;']
            vectors: One of either the available pretrained vectors
                or custom pretrained vectors (see Vocab.load_vectors);
                or a list of aforementioned vectors
            unk_init (callback): by default, initialize out-of-vocabulary word vectors
                to zero vectors; can be any function that takes in a Tensor and
                returns a Tensor of the same size. Default: torch.Tensor.zero_
            vectors_cache: directory for cached vectors. Default: '.vector_cache'
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freqs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;specials&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# frequencies of special tokens are not counted when building vocabulary
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# in frequency order
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;specials&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# sort by frequency, then alphabetically
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;words_and_frequencies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;words_and_frequencies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words_and_frequencies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# stoi is simply a reverse dict for itos
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tok&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vectors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vectors&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load_vectors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vectors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unk_init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unk_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vectors_cache&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unk_init&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vectors_cache&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__eq__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freqs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freqs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vectors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vectors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__len__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;vocab_rerank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TorchVocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pad_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unk_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eos_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sos_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;specials&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;pad&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;unk&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;eos&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;sos&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;mask&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                         &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentece&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_eos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_sos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;from_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_pad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;

    &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;staticmethod&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Vocab'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pickle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;save_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;pickle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;# Building Vocab with text files
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WordVocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;texts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Building Vocab&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tqdm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tqdm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;texts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_eos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_sos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unk_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_eos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eos_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# this would be index 1
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_sos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sos_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;origin_seq_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pad_index&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin_seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_len&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;from_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_pad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                 &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;%d&amp;gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;
                 &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;
                 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with_pad&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pad_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;join&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;

    &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;staticmethod&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'WordVocab'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pickle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;argparse&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argparse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ArgumentParser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-c&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;--corpus_path&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;required&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-o&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;--output_path&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;required&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-s&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;--vocab_size&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-e&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;--encoding&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-m&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;--min_freq&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WordVocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;VOCAB SIZE:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;main函数&quot;&gt;main函数&lt;/h5&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Loading Vocab&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WordVocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load_vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Vocab Size: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Loading Train Dataset&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTDataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Loading Test Dataset&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;test_dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTDataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_dataset&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Creating Dataloader&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_data_loader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataLoader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_workers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_workers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;test_data_loader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataLoader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_workers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_workers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; \
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test_dataset&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Building BERT model&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Creating BERT Trainer&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;trainer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BERTTrainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_dataloader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_data_loader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test_dataloader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_data_loader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                          &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;betas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adam_beta1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adam_beta2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight_decay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adam_weight_decay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                          &lt;span class=&quot;n&quot;&gt;with_cuda&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;with_cuda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cuda_devices&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cuda_devices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Training Start&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;trainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;trainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test_data_loader&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;trainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;dataset部分：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;torch.utils.data&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dataset&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tqdm&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;torch&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BERTDataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tqdm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tqdm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Loading Dataset&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                              &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tqdm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tqdm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Loading Dataset&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__len__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__getitem__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_next_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_sent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t1_random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t1_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t2_random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# [CLS] tag = SOS tag, [SEP] tag = EOS tag
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sos_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t1_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eos_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eos_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;t1_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pad_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t1_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pad_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t2_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pad_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;segment_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))])[:&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bert_input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[:&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bert_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t1_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[:&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pad_index&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bert_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bert_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bert_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bert_input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bert_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;s&quot;&gt;&quot;bert_label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bert_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;s&quot;&gt;&quot;segment_label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segment_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;s&quot;&gt;&quot;is_next&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_next_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;output_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.15&lt;/span&gt;

                &lt;span class=&quot;c1&quot;&gt;# 80% randomly change token to mask token
&lt;/span&gt;                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mask_index&lt;/span&gt;

                &lt;span class=&quot;c1&quot;&gt;# 10% randomly change token to random token
&lt;/span&gt;                &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

                &lt;span class=&quot;c1&quot;&gt;# 10% randomly change token to current token
&lt;/span&gt;                &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unk_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

                &lt;span class=&quot;n&quot;&gt;output_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unk_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unk_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;output_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_label&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random_sent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_corpus_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# output_text, label(isNotNext:0, isNext:1)
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_random_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_corpus_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t2&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_random_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;on_memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;corpus_lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;官方版&quot;&gt;官方版&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/google-research/bert&quot;&gt;https://github.com/google-research/bert&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;详见&lt;a href=&quot;https://daiwk.github.io/posts/nlp-bert-code.html&quot;&gt;https://daiwk.github.io/posts/nlp-bert-code.html&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>TensorFlow Serving的深度学习在线预估优化</title>
   <link href="http://daiwk.github.io/posts/platform-tf-serving-optimization.html"/>
   <updated>2018-10-11T00:00:00+00:00</updated>
   <id>/posts/platform-tf-serving-optimization</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%A6%82%E8%BF%B0&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B&quot;&gt;应用案例&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MjM5NjQ5MTI5OA==&amp;amp;mid=2651748960&amp;amp;idx=2&amp;amp;sn=4c637290b0bd35dc5b541d01d76ce574&amp;amp;chksm=bd12a32d8a652a3b24e159f352c835fd7ff4eedead31dd5b0d0ae2c21da4a0fc587f69a99aef&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1203KFLFFRacUEMmeCOpN2wn&amp;amp;pass_ticket=rvAT7iKQaRz0ACeQIDUC6RZ4afT%2ByLa0tHoA%2BZhdh2qN9dgNEcAfTFve673tHOcV#rd&quot;&gt;基于TensorFlow Serving的深度学习在线预估&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;h2 id=&quot;应用案例&quot;&gt;应用案例&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MjM5NjQ5MTI5OA==&amp;amp;mid=2651749434&amp;amp;idx=2&amp;amp;sn=343e811408542dd1984582b8639240a6&amp;amp;chksm=bd12a5778a652c61ed4297f1a17582cad4ca6b8e8d4d66843f169e0eda9f6aede988bc675743&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1127cg905I2SRL4WpqvTo2mJ&amp;amp;pass_ticket=rvAT7iKQaRz0ACeQIDUC6RZ4afT%2ByLa0tHoA%2BZhdh2qN9dgNEcAfTFve673tHOcV#rd&quot;&gt;强化学习在美团“猜你喜欢”的实践&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>treegan</title>
   <link href="http://daiwk.github.io/posts/nlp-treegan.html"/>
   <updated>2018-10-06T00:00:00+00:00</updated>
   <id>/posts/nlp-treegan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650749532&amp;amp;idx=3&amp;amp;sn=16cad5039f02255a6bf9bb7059eaad88&amp;amp;chksm=871afe22b06d77345b186159de9e8803749b3024df173ebafc23f41b681ec675be66f88a1544&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1006oVlYpuXj2BLG934rPBEJ&amp;amp;pass_ticket=QxrSV3DS%2FiS9KVGB05ngctujF130h1%2BGnKxMMG4QijDKs3TyHWK%2Fx1yQCCmdpdq%2B#rd&quot;&gt;学界 | TreeGAN：为序列生成任务构建有句法意识的GAN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1808.07582&quot;&gt;TreeGAN: Syntax-Aware Sequence Generation with Generative Adversarial Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;生成对抗网络是由生成网络和判别网络组成的无监督学习框架。我们将它们称为生成器（G）和判别器（D）。D 学着去区分某个数据实例是来自真实世界还是人为合成的。G 试图通过生成高质量的合成实例来迷惑 D。在 GAN 框架中，D 和 G 被不断地轮流训练直到它们达到纳什均衡。训练好的 GAN 会得到一个能够产生看起来与真实数据十分相似的高质量数据实例的生成器。&lt;/p&gt;

&lt;p&gt;受到其在图像生成和相关领域取得的巨大成功的启发，GAN[1] 最近已经被扩展到序列生成任务中 [2，3]。用于序列生成的 GAN 在现实世界中有许多重要的应用。例如，为了给一个数据库构建一个良好的查询优化器，研究人员可能希望生成大量高质量的合成 SQL 查询语句对优化器进行基准对比测试。不同于图像生成任务，大多数语言都有其固有的语法或句法。现有的用于序列生成的 GAN 模型 [2，3，7] 主要着眼于如图 1a 所示的句法无关（grammar-free）的环境。这些方法试图从数据中学习到复杂的底层句法和语法模式，这通常是非常具有挑战性的，需要大量的真实数据样本才能取得不错的性能。在许多形式语言中，语法规则或句法（例如，SQL 句法，Python PL 句法）是预定义好的。将这样的句法引入到 GAN 的训练中，应该会得出一个具有句法意识的更好的序列生成器，并且在训练阶段显著缩小搜索空间。有句法意识的现有序列生成模型 [4] 主要是通过极大似然估计（MLE）进行训练的，它们高度依赖于真实数据样本的质量和数量。一些研究 [2，5] 表明，对抗性训练可以进一步提高基于极大似然估计的序列生成性能。即使有句法意识的现有序列生成方法引入了语法信息，其生成结果也可能不是最好的。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>stackGAN</title>
   <link href="http://daiwk.github.io/posts/cv-stackgan.html"/>
   <updated>2018-09-30T00:00:00+00:00</updated>
   <id>/posts/cv-stackgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://www.sohu.com/a/128047728_505880&quot;&gt;https://www.sohu.com/a/128047728_505880&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;输入文本–》图片：stack gan (反img caption)&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>deblurGAN</title>
   <link href="http://daiwk.github.io/posts/cv-deblurgan.html"/>
   <updated>2018-09-30T00:00:00+00:00</updated>
   <id>/posts/cv-deblurgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;用GAN使模糊图片变清晰(ECCV2018)&lt;/p&gt;

&lt;p&gt;g: resnet+反卷积&lt;/p&gt;

&lt;p&gt;7x7conv,3x3conv,9个resblock,再接convtranspose(反卷积)&lt;/p&gt;

&lt;p&gt;d: 简单的cnn&lt;/p&gt;

&lt;p&gt;loss：adversarial_loss + lambda * content_loss&lt;/p&gt;

&lt;p&gt;content_loss: 生成的图片和真实图片过vgg，得到第一层的输出，算perceptual loss(本质是l2 loss)&lt;/p&gt;

&lt;p&gt;adversarial_loss: wasserstein distance&lt;/p&gt;

&lt;p&gt;数据集：gopro有1k数据，成对的数据：效果好，但会产生伪影和亮点。因为都是运动图片&lt;/p&gt;

&lt;p&gt;还会生成一些棋盘图（相邻像素灰度值一个高一个低）==》因为图片size不同，所以在采样时会出现重叠，然后在重叠处==》把一个反卷积改成上采样(邻近插值)&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jianshu.com/p/36ff39344de5&quot;&gt;Deconvolution and Checkerboard Artifacts&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pytorch的修改方式参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/pull/64/commits/3b6a5bb36b018ffc6dd43833c5d31af1e7a5b770&quot;&gt;https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/pull/64/commits/3b6a5bb36b018ffc6dd43833c5d31af1e7a5b770&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nn.UpsamplingNearest2d(scale_factor=2)&lt;/code&gt;替换&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2)&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>cycleGAN</title>
   <link href="http://daiwk.github.io/posts/cv-cyclegan.html"/>
   <updated>2018-09-30T00:00:00+00:00</updated>
   <id>/posts/cv-cyclegan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;模糊-》清晰&lt;/p&gt;

&lt;p&gt;清晰-》模糊&lt;/p&gt;

&lt;p&gt;一组到另一组的标记，不需要像素级相似（背景、姿态之类的）&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>BigGAN</title>
   <link href="http://daiwk.github.io/posts/cv-biggan.html"/>
   <updated>2018-09-30T00:00:00+00:00</updated>
   <id>/posts/cv-biggan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#biggan&quot;&gt;biggan&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#bigbigan&quot;&gt;bigbigan&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;biggan&quot;&gt;biggan&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650749368&amp;amp;idx=2&amp;amp;sn=4b970da824cc7c6fb0fa3014315da7fa&amp;amp;chksm=871afdc6b06d74d04c8d98f5d86d8361ea67227a5998db8e810284662eb784a44149644c2eb1&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0930f4H8KxbDJuf5iRVKCfIO&amp;amp;pass_ticket=qICBkLpFa%2FoJ1ivCCZZivOyb5EzGCcK5DM3u9g4RzsnN5IM45qwu8G%2Fb1FFf1SAd#rd&quot;&gt;学界 | 史上最强GAN图像生成器，Inception分数提高两倍&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://openreview.net/pdf?id=B1xsqj09Fm&quot;&gt;https://openreview.net/pdf?id=B1xsqj09Fm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;升级版：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650757542&amp;amp;idx=1&amp;amp;sn=a039ec0bc3421d4814a10e5c71fa8593&amp;amp;scene=21#wechat_redirect&quot;&gt;史上最强图像生成器BigGAN变身DeepGAN？四倍深度实现更强效果&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;bigbigan&quot;&gt;bigbigan&lt;/h2&gt;

&lt;p&gt;又升级。。BigBiGAN：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650765569&amp;amp;idx=3&amp;amp;sn=5c0d3e8be875b76bc50bfb8f5a7b0b0c&amp;amp;chksm=871abd7fb06d3469db975a886b18e770d840c4b1d70e384d2fd115f845eb3f5a4c502a4dcf55&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=zzUnWIgdqTLvX39vSLCKaOJN8KVDYuvxPgj7h5mQNNMiTnEMdrWSwBJSd3ch3aLL#rd&quot;&gt;GAN用于无监督表征学习，效果依然惊人……&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652048875&amp;amp;idx=1&amp;amp;sn=2ab6d108c7d8dd17e5ecab31c5e5af8a&amp;amp;chksm=f120791ac657f00c78f4440d7b9569dac39d188e08fbf45fcf2e5f867f20f488676bea5a3649&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=zzUnWIgdqTLvX39vSLCKaOJN8KVDYuvxPgj7h5mQNNMiTnEMdrWSwBJSd3ch3aLL#rd&quot;&gt;DeepMind爆出无监督表示学习模型BigBiGAN，GAN之父点赞！&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766563&amp;amp;idx=4&amp;amp;sn=e204d4ca4bfb09137947f4630da78f5e&amp;amp;chksm=871ab89db06d318b10fa70659af21f789a0be93d711c77d7ab6e8ed43251507b8e5266971465&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;BigBiGAN的前世今生，走向成熟的特征抽取器&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/ZC-vCRWDzsnYkyTnvXQ-Jw&quot;&gt;DeepMind开源最牛无监督学习BigBiGAN预训练模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1907.02544.pdf&quot;&gt;Large Scale Adversarial Representation Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;预训练模型地址：&lt;a href=&quot;https://tfhub.dev/s?publisher=deepmind&amp;amp;q=bigbigan&quot;&gt;https://tfhub.dev/s?publisher=deepmind&amp;amp;q=bigbigan&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>match for search recommendation（深度学习部分）</title>
   <link href="http://daiwk.github.io/posts/dl-match-for-search-recommendation.html"/>
   <updated>2018-09-09T00:00:00+00:00</updated>
   <id>/posts/dl-match-for-search-recommendation</id>
   <content type="html">&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%A6%82%E8%BF%B0&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%90%9C%E7%B4%A2%E9%A2%86%E5%9F%9F%E7%9A%84deep-match&quot;&gt;搜索领域的deep match&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%AD%A6%E4%B9%A0%E6%90%9C%E7%B4%A2representation&quot;&gt;学习搜索representation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%AD%A6%E4%B9%A0%E6%90%9C%E7%B4%A2match%E5%87%BD%E6%95%B0&quot;&gt;学习搜索match函数&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%AD%A6%E4%B9%A0query-document%E7%9A%84matching-matrix&quot;&gt;学习query-document的matching matrix&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#ARC-II&quot;&gt;ARC-II&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#MatchPyramid&quot;&gt;MatchPyramid&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#Match-SRNN&quot;&gt;Match-SRNN&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#K-NRM&quot;&gt;K-NRM&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#Conv-KNRM&quot;&gt;Conv-KNRM&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E4%BD%BF%E7%94%A8attention-model%E8%BF%9B%E8%A1%8Cmatch&quot;&gt;使用attention model进行match&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%8E%A8%E8%8D%90%E7%9A%84deep-match&quot;&gt;推荐的deep match&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%AD%A6%E4%B9%A0%E6%8E%A8%E8%8D%90representation&quot;&gt;学习推荐representation&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#Pure-CF-models&quot;&gt;Pure CF models&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#DeepMF&quot;&gt;DeepMF&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#AutoRec&quot;&gt;AutoRec&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#CDAE&quot;&gt;CDAE&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#CF-with-side-information&quot;&gt;CF with side information&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#DCF&quot;&gt;DCF&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#DUIF&quot;&gt;DUIF&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#ACF&quot;&gt;ACF&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#CKB&quot;&gt;CKB&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%AD%A6%E4%B9%A0%E6%8E%A8%E8%8D%90match%E5%87%BD%E6%95%B0&quot;&gt;学习推荐match函数&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#Pure-CF-models%E7%9A%84match%E5%AD%A6%E4%B9%A0&quot;&gt;Pure CF models的match学习&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8ENeural-Collaborative-Filtering%E6%A1%86%E6%9E%B6&quot;&gt;基于Neural Collaborative Filtering框架&lt;/a&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#NeuMF&quot;&gt;NeuMF&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#NNCF&quot;&gt;NNCF&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#ConvNCF&quot;&gt;ConvNCF&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8ETranslation%E6%A1%86%E6%9E%B6&quot;&gt;基于Translation框架&lt;/a&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#TransRec&quot;&gt;TransRec&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#LRML&quot;&gt;LRML&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#Feature-based-models%E7%9A%84match%E5%AD%A6%E4%B9%A0&quot;&gt;Feature-based models的match学习&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8EMLP&quot;&gt;基于MLP&lt;/a&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#WideDeep&quot;&gt;Wide&amp;amp;Deep&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8EFM&quot;&gt;基于FM&lt;/a&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#Neural-FM&quot;&gt;Neural FM&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#Attentional-FM&quot;&gt;Attentional FM&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8E%E6%A0%91&quot;&gt;基于树&lt;/a&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#GB-CENT&quot;&gt;GB-CENT&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#DEF&quot;&gt;DEF&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#TEM&quot;&gt;TEM&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;有两个版本，一个是www18的：&lt;a href=&quot;https://www.comp.nus.edu.sg/~xiangnan/papers/www18-tutorial-deep-matching.pdf&quot;&gt;https://www.comp.nus.edu.sg/~xiangnan/papers/www18-tutorial-deep-matching.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;一个是sigir18的：&lt;a href=&quot;https://www.comp.nus.edu.sg/~xiangnan/sigir18-deep.pdf&quot;&gt;https://www.comp.nus.edu.sg/~xiangnan/sigir18-deep.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;好像都403了。。可以看这个&lt;a href=&quot;http://www.hangli-hl.com/uploads/3/4/4/6/34465961/wsdm_2019_tutorial.pdf&quot;&gt;http://www.hangli-hl.com/uploads/3/4/4/6/34465961/wsdm_2019_tutorial.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;sigir的这个比较新。。看之&lt;/p&gt;

&lt;p&gt;这里讲深度学习部分，传统部分见：&lt;a href=&quot;https://daiwk.github.io/posts/dl-match-for-search-recommendation-traditional.html&quot;&gt;https://daiwk.github.io/posts/dl-match-for-search-recommendation-traditional.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;h2 id=&quot;搜索领域的deep-match&quot;&gt;搜索领域的deep match&lt;/h2&gt;

&lt;h3 id=&quot;学习搜索representation&quot;&gt;学习搜索representation&lt;/h3&gt;

&lt;h3 id=&quot;学习搜索match函数&quot;&gt;学习搜索match函数&lt;/h3&gt;

&lt;h4 id=&quot;学习query-document的matching-matrix&quot;&gt;学习query-document的matching matrix&lt;/h4&gt;

&lt;h5 id=&quot;arc-ii&quot;&gt;ARC-II&lt;/h5&gt;

&lt;p&gt;AAAI’16 &lt;a href=&quot;http://www.hangli-hl.com/uploads/3/1/6/8/3168008/hu-etal-nips2014.pdf&quot;&gt;Convolutional Neural Network Architectures for Matching Natural Language Sentences&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;matchpyramid&quot;&gt;MatchPyramid&lt;/h5&gt;

&lt;p&gt;AAAI’16 &lt;a href=&quot;https://arxiv.org/pdf/1602.06359.pdf&quot;&gt;Text Matching as Image Recognition&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;match-srnn&quot;&gt;Match-SRNN&lt;/h5&gt;

&lt;p&gt;IJCAI’16 &lt;a href=&quot;https://arxiv.org/pdf/1604.04378.pdf&quot;&gt;Match-SRNN: Modeling the Recursive Matching Structure with Spatial RNN&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;k-nrm&quot;&gt;K-NRM&lt;/h5&gt;

&lt;p&gt;SIGIR’17 &lt;a href=&quot;http://www.cs.cmu.edu/~zhuyund/papers/end-end-neural.pdf&quot;&gt;End-to-End Neural Ad-hoc Ranking with Kernel Pooling&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;conv-knrm&quot;&gt;Conv-KNRM&lt;/h5&gt;

&lt;p&gt;WSDM’18 &lt;a href=&quot;http://www.cs.cmu.edu/~zhuyund/papers/WSDM_2018_Dai.pdf&quot;&gt;Convolutional Neural Networks for So-Matching N-Grams in Ad-hoc Search&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;使用attention-model进行match&quot;&gt;使用attention model进行match&lt;/h4&gt;

&lt;p&gt;EMNLP 2016 &lt;a href=&quot;https://aclweb.org/anthology/D16-1244&quot;&gt;A Decomposable Attention Model for Natural Language Inference&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;推荐的deep-match&quot;&gt;推荐的deep match&lt;/h2&gt;

&lt;h3 id=&quot;学习推荐representation&quot;&gt;学习推荐representation&lt;/h3&gt;

&lt;h4 id=&quot;pure-cf-models&quot;&gt;Pure CF models&lt;/h4&gt;

&lt;h5 id=&quot;deepmf&quot;&gt;DeepMF&lt;/h5&gt;

&lt;p&gt;Deep Matrix Factorization&lt;/p&gt;

&lt;h5 id=&quot;autorec&quot;&gt;AutoRec&lt;/h5&gt;

&lt;p&gt;Autoencoders Meeting CF&lt;/p&gt;

&lt;h5 id=&quot;cdae&quot;&gt;CDAE&lt;/h5&gt;

&lt;p&gt;Collaborative Denoising Autoencoder&lt;/p&gt;

&lt;h4 id=&quot;cf-with-side-information&quot;&gt;CF with side information&lt;/h4&gt;

&lt;h5 id=&quot;dcf&quot;&gt;DCF&lt;/h5&gt;

&lt;p&gt;Deep Collaborative Filtering via Marginalized DAE&lt;/p&gt;

&lt;h5 id=&quot;duif&quot;&gt;DUIF&lt;/h5&gt;

&lt;p&gt;Deep User-Image Feature&lt;/p&gt;

&lt;h5 id=&quot;acf&quot;&gt;ACF&lt;/h5&gt;

&lt;p&gt;Attentive Collaborative Filtering&lt;/p&gt;

&lt;h5 id=&quot;ckb&quot;&gt;CKB&lt;/h5&gt;

&lt;p&gt;Collaborative Knowledge Base Embeddings&lt;/p&gt;

&lt;h3 id=&quot;学习推荐match函数&quot;&gt;学习推荐match函数&lt;/h3&gt;

&lt;h4 id=&quot;pure-cf-models的match学习&quot;&gt;Pure CF models的match学习&lt;/h4&gt;

&lt;h5 id=&quot;基于neural-collaborative-filtering框架&quot;&gt;基于Neural Collaborative Filtering框架&lt;/h5&gt;

&lt;h6 id=&quot;neumf&quot;&gt;NeuMF&lt;/h6&gt;

&lt;h6 id=&quot;nncf&quot;&gt;NNCF&lt;/h6&gt;

&lt;h6 id=&quot;convncf&quot;&gt;ConvNCF&lt;/h6&gt;

&lt;h5 id=&quot;基于translation框架&quot;&gt;基于Translation框架&lt;/h5&gt;

&lt;h6 id=&quot;transrec&quot;&gt;TransRec&lt;/h6&gt;

&lt;p&gt;要求&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Head + Relation \approx Tail\)&lt;/code&gt;，也就是说，想让这两个向量尽量是同一个向量，那cos相似度就没啥用了，因为cos只能表示夹角尽量小，可能向量的长度会差很远，所以呢，可以用L1(曼哈顿距离)或者L2距离(欧几里得距离)！！！&lt;/p&gt;

&lt;h6 id=&quot;lrml&quot;&gt;LRML&lt;/h6&gt;

&lt;p&gt;直接用欧氏距离，relation向量是通过attention学到的&lt;/p&gt;

&lt;h4 id=&quot;feature-based-models的match学习&quot;&gt;Feature-based models的match学习&lt;/h4&gt;

&lt;h5 id=&quot;基于mlp&quot;&gt;基于MLP&lt;/h5&gt;

&lt;h6 id=&quot;widedeep&quot;&gt;Wide&amp;amp;Deep&lt;/h6&gt;

&lt;h5 id=&quot;基于fm&quot;&gt;基于FM&lt;/h5&gt;

&lt;h6 id=&quot;neural-fm&quot;&gt;Neural FM&lt;/h6&gt;

&lt;h6 id=&quot;attentional-fm&quot;&gt;Attentional FM&lt;/h6&gt;

&lt;h5 id=&quot;基于树&quot;&gt;基于树&lt;/h5&gt;

&lt;h6 id=&quot;gb-cent&quot;&gt;GB-CENT&lt;/h6&gt;

&lt;h6 id=&quot;def&quot;&gt;DEF&lt;/h6&gt;

&lt;h6 id=&quot;tem&quot;&gt;TEM&lt;/h6&gt;
</content>
 </entry>
 
 <entry>
   <title>match for search recommendation（传统部分）</title>
   <link href="http://daiwk.github.io/posts/dl-match-for-search-recommendation-traditional.html"/>
   <updated>2018-09-09T00:00:00+00:00</updated>
   <id>/posts/dl-match-for-search-recommendation-traditional</id>
   <content type="html">&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%A6%82%E8%BF%B0&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%90%9C%E7%B4%A2%E9%A2%86%E5%9F%9F%E7%9A%84%E4%BC%A0%E7%BB%9F%E5%8C%B9%E9%85%8D%E6%A8%A1%E5%9E%8B&quot;&gt;搜索领域的传统匹配模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E4%BD%BF%E7%94%A8%E6%9C%BA%E5%99%A8%E7%BF%BB%E8%AF%91%E5%8C%B9%E9%85%8D&quot;&gt;使用机器翻译匹配&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#Statistical-Machine-Translation-SMT&quot;&gt;Statistical Machine Translation (SMT)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#Word-based-Model-IBM-Model-One&quot;&gt;Word-based Model: IBM Model One&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E4%BD%BF%E7%94%A8Word-based-Translation-Models%E8%BF%9B%E8%A1%8C%E5%8C%B9%E9%85%8D&quot;&gt;使用Word-based Translation Models进行匹配&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E4%BD%BF%E7%94%A8Phrase-based-translation-models%E8%BF%9B%E8%A1%8C%E5%8C%B9%E9%85%8D&quot;&gt;使用Phrase-based translation models进行匹配&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%9C%A8latent-space%E4%B8%AD%E5%8C%B9%E9%85%8D&quot;&gt;在latent space中匹配&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#Partial-Least-Square-PLS&quot;&gt;Partial Least Square (PLS)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#Regularized-Mapping-to-Latent-SpaceRMLS&quot;&gt;Regularized Mapping to Latent Space(RMLS)&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%8E%A8%E8%8D%90%E9%A2%86%E5%9F%9F%E7%9A%84%E4%BC%A0%E7%BB%9F%E5%8C%B9%E9%85%8D%E6%A8%A1%E5%9E%8B&quot;&gt;推荐领域的传统匹配模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#Collaborative-Filtering-Models&quot;&gt;Collaborative Filtering Models&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#Memory-based-CF&quot;&gt;Memory-based CF&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#Model-based-CF&quot;&gt;Model-based CF&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#Item-based-CF-in-Latent-Space&quot;&gt;Item-based CF in Latent Space&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#Fusing-User-based-and-Item-based-CF-in-Latent-Space&quot;&gt;Fusing User-based and Item-based CF in Latent Space&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Generic-Feature-based-Models&quot;&gt;Generic Feature-based Models&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#Factorization-Machine&quot;&gt;Factorization Machine&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E9%9A%90%E5%BC%8F%E5%8F%8D%E9%A6%88vs-%E6%98%BE%E5%BC%8F%E5%8F%8D%E9%A6%88&quot;&gt;隐式反馈vs 显式反馈&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#top-k-%E6%8E%A8%E8%8D%90&quot;&gt;top-k 推荐&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;这里讲传统部分，深度学习部分见：&lt;a href=&quot;https://daiwk.github.io/posts/dl-match-for-search-recommendation.html&quot;&gt;https://daiwk.github.io/posts/dl-match-for-search-recommendation.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;h2 id=&quot;搜索领域的传统匹配模型&quot;&gt;搜索领域的传统匹配模型&lt;/h2&gt;

&lt;h3 id=&quot;使用机器翻译匹配&quot;&gt;使用机器翻译匹配&lt;/h3&gt;

&lt;h4 id=&quot;statistical-machine-translation-smt&quot;&gt;Statistical Machine Translation (SMT)&lt;/h4&gt;

&lt;h4 id=&quot;word-based-model-ibm-model-one&quot;&gt;Word-based Model: IBM Model One&lt;/h4&gt;

&lt;h4 id=&quot;使用word-based-translation-models进行匹配&quot;&gt;使用Word-based Translation Models进行匹配&lt;/h4&gt;

&lt;h4 id=&quot;使用phrase-based-translation-models进行匹配&quot;&gt;使用Phrase-based translation models进行匹配&lt;/h4&gt;

&lt;h3 id=&quot;在latent-space中匹配&quot;&gt;在latent space中匹配&lt;/h3&gt;

&lt;h4 id=&quot;partial-least-square-pls&quot;&gt;Partial Least Square (PLS)&lt;/h4&gt;

&lt;h4 id=&quot;regularized-mapping-to-latent-spacermls&quot;&gt;Regularized Mapping to Latent Space(RMLS)&lt;/h4&gt;

&lt;h2 id=&quot;推荐领域的传统匹配模型&quot;&gt;推荐领域的传统匹配模型&lt;/h2&gt;

&lt;h3 id=&quot;collaborative-filtering-models&quot;&gt;Collaborative Filtering Models&lt;/h3&gt;

&lt;h4 id=&quot;memory-based-cf&quot;&gt;Memory-based CF&lt;/h4&gt;

&lt;h4 id=&quot;model-based-cf&quot;&gt;Model-based CF&lt;/h4&gt;

&lt;h4 id=&quot;item-based-cf-in-latent-space&quot;&gt;Item-based CF in Latent Space&lt;/h4&gt;

&lt;h4 id=&quot;fusing-user-based-and-item-based-cf-in-latent-space&quot;&gt;Fusing User-based and Item-based CF in Latent Space&lt;/h4&gt;

&lt;p&gt;MF&lt;/p&gt;

&lt;p&gt;FISM&lt;/p&gt;

&lt;p&gt;SVD++&lt;/p&gt;

&lt;h3 id=&quot;generic-feature-based-models&quot;&gt;Generic Feature-based Models&lt;/h3&gt;

&lt;h4 id=&quot;factorization-machine&quot;&gt;Factorization Machine&lt;/h4&gt;

&lt;p&gt;Rendle, ICDM’10&lt;/p&gt;

&lt;p&gt;FM使得矩阵分解更容易加特征了，所以，可以模仿以下模型的效果：&lt;/p&gt;

&lt;p&gt;MF,SVD++,timeSVD(Koren,KDD’09),PIFT(Rendle,WSDM’10)etc.&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;p&gt;当xxxx时，FM和MF+bias是等价的：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fm-equal-mf-with-bias.png&quot; style=&quot;max-width: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;当xxxx时，FM和SVD++是等价的：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fm-equal-svdpp.png&quot; style=&quot;max-width: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h4 id=&quot;隐式反馈vs-显式反馈&quot;&gt;隐式反馈vs 显式反馈&lt;/h4&gt;

&lt;h4 id=&quot;top-k-推荐&quot;&gt;top-k 推荐&lt;/h4&gt;

</content>
 </entry>
 
 <entry>
   <title>autokeras</title>
   <link href="http://daiwk.github.io/posts/platform-autokeras.html"/>
   <updated>2018-08-04T00:00:00+00:00</updated>
   <id>/posts/platform-autokeras</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/NAjaco_dC1y3Eo_-UfAsbg&quot;&gt;https://mp.weixin.qq.com/s/NAjaco_dC1y3Eo_-UfAsbg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650749988&amp;amp;idx=1&amp;amp;sn=59b5486b3c70489d91712bc438ce3961&amp;amp;chksm=871af85ab06d714c26f3e2a44385b5513d3da24ef268ff61e2d2a0fc6fc429a54530d37721a0&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1017LFiazN82maBp8SiVlofH&amp;amp;pass_ticket=SszanYxD5OWcgQT%2B0DDH7ckogHOwJlBON1eETcKdrqvcCwwsdmrx3DePGS5Lponk#rd&quot;&gt;超参数搜索不够高效？这几大策略了解一下&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;官网：&lt;a href=&quot;http://autokeras.com/&quot;&gt;http://autokeras.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/jhfjhfj1/autokeras&quot;&gt;https://github.com/jhfjhfj1/autokeras&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;另一篇enas的论文
&lt;a href=&quot;https://arxiv.org/abs/1802.03268&quot;&gt;Efficient Neural Architecture Search via Parameter Sharing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;enas对应的tf和pytorch实现：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;tf：&lt;a href=&quot;https://github.com/melodyguan/enas&quot;&gt;https://github.com/melodyguan/enas&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;pytorch：&lt;a href=&quot;ttps://github.com/carpedm20/ENAS-pytorch&quot;&gt;https://github.com/carpedm20/ENAS-pytorch&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;NAS：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1611.01578&quot;&gt;Neural Architecture Search with Reinforcement Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;神经架构搜索（NAS）是自动机器学习中一种有效的计算工具，旨在为给定的学习任务搜索最佳的神经网络架构。然而，现有的 NAS 算法通常计算成本很高。另一方面，网络态射（network morphism）已经成功地应用于神经架构搜索。网络态射是一种改变神经网络架构但保留其功能的技术。因此，我们可以利用网络态射操作将训练好的神经网络改成新的体系架构，如，插入一层或添加一个残差连接。然后，只需再加几个 epoch 就可以进一步训练新架构以获得更好的性能。&lt;/p&gt;

&lt;p&gt;基于网络态射的 NAS 方法要解决的最重要问题是运算的选择，即从网络态射运算集里进行选择，将现有的架构改变为一种新的架构。基于最新网络态射的方法使用深度强化学习控制器，这需要大量的训练样例。另一个简单的方法是使用随机算法和爬山法，这种方法每次只能探索搜索区域的邻域，并且有可能陷入局部最优值。&lt;/p&gt;

&lt;p&gt;贝叶斯优化已被广泛用于基于观察有限数据的寻找函数最优值过程。它经常被用于寻找黑箱函数的最优点，其中函数的观察值很难获取。贝叶斯优化的独特性质启发了研究者探索它在指导网络态射减少已训练神经网络数量的能力，从而使搜索更加高效。&lt;/p&gt;

&lt;p&gt;为基于网络态射的神经架构搜索设计贝叶斯优化方法是很困难的，因为存在如下挑战：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先，其潜在的高斯过程（GP）在传统上是用于欧氏空间的，为了用观察数据更新贝叶斯优化，潜在高斯过程将使用搜索到的架构和它们的性能来训练。然而，神经网络架构并不位于欧氏空间，并且很难参数化为固定长度的向量。&lt;/li&gt;
  &lt;li&gt;其次，采集函数需要进行优化以生成下一个架构用于贝叶斯优化。然而，这个过程不是最大化欧氏空间里的一个函数来态射神经架构，而是选择一个节点在一个树架构搜索空间中扩展，其中每个节点表示一个架构，且每条边表示一个态射运算。传统的类牛顿或基于梯度的方法不能简单地进行应用。第三，网络态射运算改变神经架构的的一个层可能会导致其它层的很多变化，以保持输入和输出的一致性，这在以前的研究中并没有定义。网络态射运算在结合了跳过连接的神经架构搜索空间中是很复杂的。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在 AutoKeras 作者提交的论文中，研究人员们提出了一种带有网络态射的高效神经架构搜索，它利用贝叶斯优化通过每次选择最佳运算来引导搜索空间。为应对上述挑战，研究者创建了一种基于编辑距离（edit-distance）的神经网络核函数。与网络态射的关键思路一致，它给出了将一个神经网络转化为另一个神经网络需要多少运算。此外，研究者为树形架构搜索空间专门设计了一种新的采集函数（acquisition function）优化器，使贝叶斯优化能够从运算中进行选择。优化方法可以在优化过程中平衡探索和利用。此外，作者还定义了一个网络级态射，以解决基于前一层网络态射的神经架构中的复杂变化。该方法被封装成一个开源软件，即 AutoKeras，在基准数据集上进行评估，并与最先进的基线方法进行比较。&lt;/p&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/abs/1806.10282&quot;&gt;Efficient Neural Architecture Search with Network Morphism&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;之前还有一篇enas的文章：&lt;/p&gt;

&lt;p&gt;安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;autokeras
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果装完后提示libgcc_s.so里的GCC_VERSION找不到啥的，可以试一下用root：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; /opt/compiler/gcc-4.8.2/lib/libgcc_s.so&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /lib64/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>tf与开源框架的集成</title>
   <link href="http://daiwk.github.io/posts/platform-tf-frameworks.html"/>
   <updated>2018-07-20T00:00:00+00:00</updated>
   <id>/posts/platform-tf-frameworks</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#kubeflow&quot;&gt;kubeflow&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%AE%89%E8%A3%85&quot;&gt;安装&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%AE%89%E8%A3%85kubernets&quot;&gt;安装kubernets&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E4%BB%8Ebootstrapper%E5%AE%89%E8%A3%85kubeflow&quot;&gt;从bootstrapper安装kubeflow&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tf-on-k8s&quot;&gt;tf on k8s&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tf-on-marathon&quot;&gt;tf on marathon&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tf-on-hadoop&quot;&gt;tf on hadoop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tf-on-spark&quot;&gt;tf on spark&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E9%99%84%E5%BD%95&quot;&gt;附录&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E4%BD%BF%E7%94%A8minikube%E6%B2%A1%E6%88%90%E5%8A%9F%E8%BF%87%E2%80%A6&quot;&gt;使用minikube(没成功过…)&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%AE%89%E8%A3%85kubectl&quot;&gt;安装kubectl&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%AE%89%E8%A3%85minikubelocal%08%E5%AE%89%E8%A3%85&quot;&gt;安装minikube(local安装)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%90%AF%E5%8A%A8minikube&quot;&gt;启动minikube&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/ecosystem&quot;&gt;https://github.com/tensorflow/ecosystem&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;kubeflow&quot;&gt;kubeflow&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/kubeflow/kubeflow&quot;&gt;https://github.com/kubeflow/kubeflow&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;安装&quot;&gt;安装&lt;/h3&gt;

&lt;h4 id=&quot;安装kubernets&quot;&gt;安装kubernets&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.sina.com.cn/s/blog_48c95a190102wqpq.html&quot;&gt;http://blog.sina.com.cn/s/blog_48c95a190102wqpq.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;首先安装go：直接去官网&lt;a href=&quot;https://www.golangtc.com/download&quot;&gt;https://www.golangtc.com/download&lt;/a&gt;搞一个下来，然后解压，然后设置一下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export GOROOT=xxxxx&lt;/code&gt;，再把bin目录下的go*丢到/usr/local/bin下面就行了。&lt;/p&gt;

&lt;p&gt;然后从&lt;a href=&quot;https://github.com/kubernetes/kubernetes/releases&quot;&gt;https://github.com/kubernetes/kubernetes/releases&lt;/a&gt;这里找一个版本的源码进行下载，例如kubernetes-1.9.7.tar.gz这个版本，然后解压&lt;/p&gt;

&lt;p&gt;然后进入解压后的目录，直接make就行了，这样会自己把最必需的kubectl、kubelet、kubeadm放到/usr/local/bin下，而且在_output目录下生成kube-apiserver、kube-proxy、kube-controller-manager、kube-scheduler等各种bin。&lt;/p&gt;

&lt;p&gt;然后把etcd和flannel搞下来&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://github.com/coreos/etcd/releases/download/v2.3.8/etcd-v2.3.8-linux-amd64.tar.gz
wget https://github.com/coreos/flannel/releases/download/v0.6.2/flannel-v0.6.2-linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;放到/usr/local/bin下面去，然后修改 /etc/sysctl.conf&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;net.bridge.bridge-nf-call-ip6tables &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1
net.bridge.bridge-nf-call-iptables &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1
net.bridge.bridge-nf-call-arptables &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;执行&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sysctl &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;11.11.2.3

&lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;etcd &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt; infra0 &lt;span class=&quot;nt&quot;&gt;--initial-advertise-peer-urls&lt;/span&gt; http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:2380,http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:7001 &lt;span class=&quot;nt&quot;&gt;--listen-peer-urls&lt;/span&gt; http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:2380,http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:7001 &lt;span class=&quot;nt&quot;&gt;--listen-client-urls&lt;/span&gt; http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:2379,http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:4001 &lt;span class=&quot;nt&quot;&gt;--advertise-client-urls&lt;/span&gt; http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:2379,http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:4001 &lt;span class=&quot;nt&quot;&gt;--initial-cluster-token&lt;/span&gt; etcd-cluster &lt;span class=&quot;nt&quot;&gt;--initial-cluster&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;infra0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:2380,infra0&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:7001 &lt;span class=&quot;nt&quot;&gt;--data-dir&lt;/span&gt; /root/data/etcd/data &lt;span class=&quot;nt&quot;&gt;--initial-cluster-state&lt;/span&gt; new &amp;amp;

&lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;etcdctl &lt;span class=&quot;nt&quot;&gt;--endpoints&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:2379,http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:4001 mk /coreos.com/network/config &lt;span class=&quot;s1&quot;&gt;'{&quot;Network&quot;:&quot;172.17.0.0/16&quot;, &quot;SubnetMin&quot;: &quot;172.17.1.0&quot;, &quot;SubnetMax&quot;: &quot;172.17.254.0&quot;}'&lt;/span&gt; &amp;amp;

&lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;flanneld &lt;span class=&quot;nt&quot;&gt;-etcd-endpoints&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:2379,http://&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;master_hostname&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;:4001 &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;从bootstrapper安装kubeflow&quot;&gt;从bootstrapper安装kubeflow&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-O&lt;/span&gt; https://raw.githubusercontent.com/kubeflow/kubeflow/master/bootstrap/bootstrapper.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; bootstrapper.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;tf-on-k8s&quot;&gt;tf on k8s&lt;/h2&gt;

&lt;h2 id=&quot;tf-on-marathon&quot;&gt;tf on marathon&lt;/h2&gt;

&lt;p&gt;marathon是基于mesos的&lt;/p&gt;

&lt;h2 id=&quot;tf-on-hadoop&quot;&gt;tf on hadoop&lt;/h2&gt;

&lt;h2 id=&quot;tf-on-spark&quot;&gt;tf on spark&lt;/h2&gt;

&lt;h2 id=&quot;附录&quot;&gt;附录&lt;/h2&gt;

&lt;h3 id=&quot;使用minikube没成功过&quot;&gt;使用minikube(没成功过…)&lt;/h3&gt;

&lt;h4 id=&quot;安装kubectl&quot;&gt;安装kubectl&lt;/h4&gt;

&lt;p&gt;kubectl即kubernetes的客户端，通过他可以进行类似docker run等容器管理操作&lt;/p&gt;

&lt;p&gt;+ ubuntu&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; apt-transport-https
curl &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; https://packages.cloud.google.com/apt/doc/apt-key.gpg | &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-key add -
&lt;span class=&quot;nb&quot;&gt;sudo touch&lt;/span&gt; /etc/apt/sources.list.d/kubernetes.list
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;deb http://apt.kubernetes.io/ kubernetes-xenial main&quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;sudo tee&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; /etc/apt/sources.list.d/kubernetes.list
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get update
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; kubectl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;centos&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;EOF&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt; &amp;gt; /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;EOF

&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;yum &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; kubectl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;mac&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;brew &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;kubectl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;安装minikubelocal安装&quot;&gt;安装minikube(local安装)&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;ubuntu/centos&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-Lo&lt;/span&gt; minikube https://storage.googleapis.com/minikube/releases/v0.28.0/minikube-linux-amd64
&lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x minikube
&lt;span class=&quot;nb&quot;&gt;sudo mv &lt;/span&gt;minikube /usr/local/bin/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;mac&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;先从官网安装virtualbox &lt;a href=&quot;https://www.virtualbox.org/wiki/Downloads&quot;&gt;https://www.virtualbox.org/wiki/Downloads&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-Lo&lt;/span&gt; minikube https://storage.googleapis.com/minikube/releases/v0.28.0/minikube-darwin-amd64
&lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x minikube
&lt;span class=&quot;nb&quot;&gt;sudo mv &lt;/span&gt;minikube /usr/local/bin/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;启动minikube&quot;&gt;启动minikube&lt;/h4&gt;

&lt;p&gt;以下参数是kubeflow的最低配置&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;minikube start &lt;span class=&quot;nt&quot;&gt;--cpus&lt;/span&gt; 4 &lt;span class=&quot;nt&quot;&gt;--memory&lt;/span&gt; 8096 &lt;span class=&quot;nt&quot;&gt;--disk-size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;40g 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果在linux下，可以指定不要虚拟机：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;minikube start &lt;span class=&quot;nt&quot;&gt;--cpus&lt;/span&gt; 4 &lt;span class=&quot;nt&quot;&gt;--memory&lt;/span&gt; 8096 &lt;span class=&quot;nt&quot;&gt;--disk-size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;40g &lt;span class=&quot;nt&quot;&gt;--vm-driver&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;none
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;mac&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果发现被墙，下不下来…就手动开浏览器下载提示出错的url，例如&lt;a href=&quot;ttps://storage.googleapis.com/minikube/iso/minikube-v0.28.0.iso&quot;&gt;ttps://storage.googleapis.com/minikube/iso/minikube-v0.28.0.iso&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mv git_daiwk/minikube-v0.28.0.iso ~/.minikube/cache/iso/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果kubelet下载超时，如&lt;a href=&quot;https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubelet&quot;&gt;https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubelet&lt;/a&gt;和&lt;a href=&quot;https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubeadm&quot;&gt;https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubeadm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mv &lt;/span&gt;kubelet ~/.minikube/cache/v1.10.0/
&lt;span class=&quot;nb&quot;&gt;mv &lt;/span&gt;kubeadm ~/.minikube/cache/v1.10.0/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;ubuntu/centos&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt; ./k8s_version_stable.txt https://storage.googleapis.com/kubernetes-release/release/stable.txt &lt;span class=&quot;c&quot;&gt;# v1.11.1&lt;/span&gt;
curl &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt; ./kubectl &lt;span class=&quot;s2&quot;&gt;&quot;https://storage.googleapis.com/kubernetes-release/release/&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /tmp/kubectl_version&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/bin/linux/amd64/kubectl&quot;&lt;/span&gt;
curl &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt; ./kubelet &lt;span class=&quot;s2&quot;&gt;&quot;https://storage.googleapis.com/kubernetes-release/release/&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /tmp/kubectl_version&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/bin/linux/amd64/kubelet&quot;&lt;/span&gt;
curl &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt; ./kubeadm &lt;span class=&quot;s2&quot;&gt;&quot;https://storage.googleapis.com/kubernetes-release/release/&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /tmp/kubectl_version&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/bin/linux/amd64/kubeadm&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>超分辨率</title>
   <link href="http://daiwk.github.io/posts/cv-super-resolution.html"/>
   <updated>2018-07-16T00:00:00+00:00</updated>
   <id>/posts/cv-super-resolution</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#srcnn&quot;&gt;SRCNN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fsrcnn&quot;&gt;FSRCNN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#espcn&quot;&gt;ESPCN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#vdsr&quot;&gt;VDSR&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#drcn&quot;&gt;DRCN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#red&quot;&gt;RED&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#drrn&quot;&gt;DRRN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#lapsrn&quot;&gt;LapSRN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#srdensenet&quot;&gt;SRDenseNet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#srgansrresnet&quot;&gt;SRGAN(SRResNet)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#edsr&quot;&gt;EDSR&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deblurgan&quot;&gt;deblurGAN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%ae%a8%e8%ae%ba&quot;&gt;讨论&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e4%bc%98%e9%85%b7%e6%95%b0%e6%8d%ae%e9%9b%86&quot;&gt;优酷数据集&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;综述：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650757585&amp;amp;idx=3&amp;amp;sn=10b7f6aa96e5a7717eaa079f05696935&amp;amp;chksm=871a9dafb06d14b9a99edc0070caac73d22a5febed2045ac103419903909cff833a38089e577&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=4hmIYvO6GJcf2XjDjrkA6v22Y3ZUCDsA30spOD3nAyih4OfDpXcZPiTcotPvF%2FnT#rd&quot;&gt;从网络设计到实际应用，深度学习图像超分辨率综述&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/YCTf__0LjGzn2BA414OCMQ&quot;&gt;基于深度学习的超分辨率图像技术一览&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/sinat_39372048/article/details/81628945&quot;&gt;https://blog.csdn.net/sinat_39372048/article/details/81628945&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;可以自己搞&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;找图，然后加运动模糊（随机添加运动方向和位移）&lt;/li&gt;
  &lt;li&gt;视频压缩所导致的模糊，自己压缩构造样本（边缘块状）&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;图片物理变化：用pil直接调锐度、亮度、对比度（有参照的，在原图基础上）之类的，其中亮度、锐度可以有个整体平均灰度值，可以往这个均值附近靠&lt;/p&gt;

&lt;h2 id=&quot;srcnn&quot;&gt;SRCNN&lt;/h2&gt;

&lt;h2 id=&quot;fsrcnn&quot;&gt;FSRCNN&lt;/h2&gt;

&lt;h2 id=&quot;espcn&quot;&gt;ESPCN&lt;/h2&gt;

&lt;h2 id=&quot;vdsr&quot;&gt;VDSR&lt;/h2&gt;

&lt;h2 id=&quot;drcn&quot;&gt;DRCN&lt;/h2&gt;

&lt;h2 id=&quot;red&quot;&gt;RED&lt;/h2&gt;

&lt;h2 id=&quot;drrn&quot;&gt;DRRN&lt;/h2&gt;

&lt;h2 id=&quot;lapsrn&quot;&gt;LapSRN&lt;/h2&gt;

&lt;h2 id=&quot;srdensenet&quot;&gt;SRDenseNet&lt;/h2&gt;

&lt;h2 id=&quot;srgansrresnet&quot;&gt;SRGAN(SRResNet)&lt;/h2&gt;

&lt;h2 id=&quot;edsr&quot;&gt;EDSR&lt;/h2&gt;

&lt;h2 id=&quot;deblurgan&quot;&gt;deblurGAN&lt;/h2&gt;

&lt;h2 id=&quot;讨论&quot;&gt;讨论&lt;/h2&gt;

&lt;p&gt;最新的能精确控制某个神经元对应哪一部分（比如眼睛 鼻子之类的）&lt;/p&gt;

&lt;p&gt;视频：用gan时，细节并没有损失，切帧，每一帧去做传统超分，可以。但做清晰度重建不行，因为运动的图片模糊部分比如帧1补了个脚。。可能帧2会补个手。。&lt;/p&gt;

&lt;p&gt;传统超分可以先超分成大图，再下采样变小。可以省带宽，传输时下采样变小。&lt;/p&gt;

&lt;p&gt;分辨率重建，是本身图片大小不变，直接改图。&lt;/p&gt;

&lt;p&gt;还有另一个相似的课题：图像补全(image inpainting)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/gavinmiaoc/article/details/80802967&quot;&gt;https://blog.csdn.net/gavinmiaoc/article/details/80802967&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;优酷数据集&quot;&gt;优酷数据集&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/tsJOP1bHiFV1ZI_jYRE92g&quot;&gt;优酷发布最大工业级超高清视频数据集，超分辨率算法大赛落幕&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://tianchi.aliyun.com/dataset/dataDetail?datald=39568&quot;&gt;https://tianchi.aliyun.com/dataset/dataDetail?datald=39568&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>glow</title>
   <link href="http://daiwk.github.io/posts/cv-glow.html"/>
   <updated>2018-07-16T00:00:00+00:00</updated>
   <id>/posts/cv-glow</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247490358&amp;amp;idx=1&amp;amp;sn=b4b5d6014bdd365456d500537ba5bcad&amp;amp;chksm=96e9c4b6a19e4da08710a55935dc2e15b00838d5395fdf2a424c50dedff9af7fa11441741b9d&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0713uWBQxj4Q5wCZwuphDYZN&amp;amp;pass_ticket=qgYs5vOKlc87Cj4B5uTln9ELDfWQJnTqwJO%2B5ipNoI6K7VStQ9djW9PXdfzSwMD3#rd&quot;&gt;换脸效果媲美GAN！一文解析OpenAI最新流生成模型「Glow」&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1807.03039&quot;&gt;Glow: Generative Flow with Invertible 1x1 Convolutions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;虽然基于流的生成模型在 2014 年就已经提出来了，但是一直没有得到重视。Glow 的作者在之前已经在基于流的生成模型上提出了&lt;a href=&quot;https://arxiv.org/abs/1410.8516&quot;&gt;NICE: Non-linear Independent Components Estimation&lt;/a&gt;和 &lt;a href=&quot;https://arxiv.org/abs/1605.08803&quot;&gt;Density estimation using Real NVP&lt;/a&gt;，Glow 正是在这两个模型基础加入可逆 1 x 1 卷积进行扩展，精确的潜在变量推断在人脸属性上展示了惊艳的实验效果，具体效果可在 OpenAI 放出的 Demo(&lt;a href=&quot;https://blog.openai.com/glow/&quot;&gt;https://blog.openai.com/glow/&lt;/a&gt;)下查看。&lt;/p&gt;

&lt;p&gt;目前已有的生成模型除了 Glow 外包括三大类，&lt;strong&gt;GAN、VAE 和 Autoregressive Model（自回归模型）&lt;/strong&gt;。 其中&lt;strong&gt;自回归模型和VAE是基于似然的方法&lt;/strong&gt;，&lt;strong&gt;GAN则是通过缩小样本和生成之间的分布实现数据的生成&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;自回归模型（Autoregressive Model）&lt;/strong&gt;：自回归模型在&lt;strong&gt;PixelCNN和PixelRNN&lt;/strong&gt;上展示了很不错的实验效果，但是由于是&lt;strong&gt;按照像素点去生成图像&lt;/strong&gt;导致计算成本高， 在&lt;strong&gt;可并行性上受限&lt;/strong&gt;，在处理大型数据如大型图像或视频是具有一定麻烦的。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;变分自编码器（VAE）&lt;/strong&gt;：VAE是在Autoencoder的基础上&lt;strong&gt;让图像编码的潜在向量服从高斯分布&lt;/strong&gt;从而实现图像的生成，&lt;strong&gt;优化了数据对数似然的下界&lt;/strong&gt;，VAE 在图像生成上是&lt;strong&gt;可并行的&lt;/strong&gt;， 但是VAE存在着&lt;strong&gt;生成图像模糊&lt;/strong&gt;的问题，Glow 文中称之为优化相对具有挑战性。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;生成对抗网络（GAN）&lt;/strong&gt;：GAN 的思想就是利用博弈不断的优化生成器和判别器从而使得生成的图像与真实图像在分布上越来越相近。&lt;strong&gt;GAN 生成的图像比较清晰&lt;/strong&gt;， 在很多 GAN 的拓展工作中也取得了很大的提高。但是&lt;strong&gt;GAN生成中的多样性不足&lt;/strong&gt;以及&lt;strong&gt;训练过程不稳定&lt;/strong&gt;是GAN一直以来的问题，同时GAN&lt;strong&gt;没有潜在空间编码器，从而缺乏对数据的全面支持。&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Glow 在图像的生成，尤其是在&lt;strong&gt;图像编码得到的潜在向量精确推断&lt;/strong&gt;上展示了很好的效果。&lt;/p&gt;

&lt;p&gt;基于流的生成模型总结一下具有以下优点：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;精确的潜在变量推断和对数似然评估&lt;/strong&gt;。在 VAE 中编码后&lt;strong&gt;只能推理出对应于数据点的潜在变量的近似值&lt;/strong&gt;，GAN 根本就没有编码器更不用谈潜在变量的推断了。在 Glow 这样的&lt;strong&gt;可逆&lt;/strong&gt;生成模型中，&lt;strong&gt;可以在没有近似的情况下实现潜在变量的精确的推理&lt;/strong&gt;，还可以&lt;strong&gt;优化数据的精确对数似然，而不是其下限。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;高效的推理和合成&lt;/strong&gt;。自回归模型如 PixelCNN，也是&lt;strong&gt;可逆&lt;/strong&gt;的，然而这样的模型合成难以实现并行化，并且通常在并行硬件上效率低下。而基于流的生成模型如 Glow 和 RealNVP 都能有效实现推理与合成的并行化。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;对下游任务有用的潜在空间&lt;/strong&gt;。&lt;strong&gt;自回归模型的隐藏层有未知的边际分布&lt;/strong&gt;，使其执行有效的数据操作上很困难；在 GAN 中，由于模型没有编码器使得&lt;strong&gt;数据点通常不能在潜在空间中直接被表征&lt;/strong&gt;，并且&lt;strong&gt;表征完整的数据分布也是不容易的&lt;/strong&gt;。而在可逆生成模型和 VAE 中不会如此，它们&lt;strong&gt;允许多种应用，例如数据点之间的插值，和已有数据点的有目的修改。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;内存的巨大潜力&lt;/strong&gt;。如 RevNet 论文所述，在可逆神经网络中&lt;strong&gt;计算梯度需要一定量的内存，而不是线性的深度。&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

</content>
 </entry>
 
 <entry>
   <title>gan landscape</title>
   <link href="http://daiwk.github.io/posts/cv-gan-overview.html"/>
   <updated>2018-07-16T00:00:00+00:00</updated>
   <id>/posts/cv-gan-overview</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-%E6%A6%82%E8%BF%B0&quot;&gt;1. 概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#x-%E4%BB%A3%E7%A0%81&quot;&gt;x. 代码&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;1-概述&quot;&gt;1. 概述&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650745273&amp;amp;idx=1&amp;amp;sn=b057305f7431423adebaa519dcd23547&amp;amp;chksm=871aedc7b06d64d1f66d78687a721b7360f0b11f20b44b00c7d92047f137e75be20d9ffb2009&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0715c3IDUOvpvCyTHgZhMDMV&amp;amp;pass_ticket=qgYs5vOKlc87Cj4B5uTln9ELDfWQJnTqwJO%2B5ipNoI6K7VStQ9djW9PXdfzSwMD3#rd&quot;&gt;谷歌大脑发布GAN全景图：看百家争鸣的生成对抗网络&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;有以下两篇：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1711.10337&quot;&gt;Are GANs Created Equal? A Large-Scale Study&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1807.04720&quot;&gt;The GAN Landscape: Losses, Architectures, Regularization, and Normalization&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;训练 GAN 需要在生成器和判别器的参数上求解一个&lt;strong&gt;极小极大问题&lt;/strong&gt;。由于生成器和判别器通常被参数化为深度卷积神经网络，这个极小极大问题在实践中非常困难。&lt;/p&gt;

&lt;p&gt;作者主要从损失函数、判别器的正则化与归一化、生成器与判别器的架构、评估度量与数据集等 5 个方面进行了讨论。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;损失函数：&lt;/strong&gt;作者讨论了原版 GAN 的 JS 距离、WGAN 的 Wasserstein 距离和最小二乘等损失函数。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;判别器的正则化与归一化&lt;/strong&gt;：
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;判别器的正则化&lt;/strong&gt;主要为梯度范数罚项，例如在 WGAN 中，这种梯度范数惩罚主要体现在对违反 1-Lipschitzness 平滑的软惩罚。此外，模型还能根据数据流形评估梯度范数惩罚，并鼓励判别器在该数据流形上成分段线性。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;判别器的归一化&lt;/strong&gt;主要体现在最优化与表征上，即归一化能获得更高效的梯度流与更稳点的优化过程，以及修正各权重矩阵的谱结构而获得更更丰富的层级特征。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;生成器与判别器架构&lt;/strong&gt;：
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;深度卷积生成对抗网络&lt;/strong&gt;：生成器与判别器分别包含 5 个卷积层，且带有谱归一化的变体称为 SNDCGAN。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;残差网络&lt;/strong&gt;：ResNet19 的生成器包含 5 个残差模块，判别器包含 6 个残差模块。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;评估度量&lt;/strong&gt;：包括 Inception Score (IS)、Frechet Inception Distance (FID) 和 Kernel Inception distance (KID) 等，它们都提供生成样本质量的定量分析&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;数据集&lt;/strong&gt;：IFAR10、CELEBA-HQ-128 和 LSUN-BEDROOM。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;x-代码&quot;&gt;x. 代码&lt;/h2&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/google/compare_gan&quot;&gt;https://github.com/google/compare_gan&lt;/a&gt;
安装： clone下来&lt;/p&gt;

&lt;p&gt;然后需要修改一下setup.py，改为：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;nv&quot;&gt;scripts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=[&lt;/span&gt;
        &lt;span class=&quot;s1&quot;&gt;'compare_gan/bin/compare_gan_generate_tasks'&lt;/span&gt;,
        &lt;span class=&quot;s1&quot;&gt;'compare_gan/bin/compare_gan_prepare_datasets.sh'&lt;/span&gt;,
        &lt;span class=&quot;s1&quot;&gt;'compare_gan/bin/compare_gan_run_one_task'&lt;/span&gt;,
        &lt;span class=&quot;s1&quot;&gt;'compare_gan/bin/compare_gan_run_test.sh'&lt;/span&gt;,
    &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后安装&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后运行下面的代码，把数据集准备好&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;bin &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; bash &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; compare_gan_prepare_datasets.sh
&lt;span class=&quot;c&quot;&gt;## 可能需要修改一下t2t_datagen的路径，例如：&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#T2T_DATAGEN=&quot;$HOME/.local/bin/t2t-datagen&quot;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#T2T_DATAGEN=&quot;/usr/local/lib/python3.6/site-packages/tensor2tensor/bin/t2t_datagen.py&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意，这两个数据集没装：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Lsun bedrooms dataset: If you want to install lsun-bedrooms you need to run t2t-datagen yourself (this dataset will take couple hours to download and unpack).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;CelebaHQ dataset: currently it is not available in tensor2tensor. Please use the ProgressiveGAN &lt;a href=&quot;https://github.com/tkarras/progressive_growing_of_gans&quot;&gt;https://github.com/tkarras/progressive_growing_of_gans&lt;/a&gt; for instructions on how to prepare it.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;然后就可以跑了(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compare_gan_generate_tasks&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compare_gan_run_one_task&lt;/code&gt;是安装的两个bin)&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Create tasks for experiment &quot;test&quot; in directory /tmp/results. See &quot;src/generate_tasks_lib.py&quot; to see other possible experiments.&lt;/span&gt;
compare_gan_generate_tasks &lt;span class=&quot;nt&quot;&gt;--workdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/results &lt;span class=&quot;nt&quot;&gt;--experiment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Run task 0 (training and eval)&lt;/span&gt;
compare_gan_run_one_task &lt;span class=&quot;nt&quot;&gt;--workdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/results &lt;span class=&quot;nt&quot;&gt;--task_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 &lt;span class=&quot;nt&quot;&gt;--dataset_root&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/datasets

&lt;span class=&quot;c&quot;&gt;# Run task 1 (training and eval)&lt;/span&gt;
compare_gan_run_one_task &lt;span class=&quot;nt&quot;&gt;--workdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/results &lt;span class=&quot;nt&quot;&gt;--task_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1 &lt;span class=&quot;nt&quot;&gt;--dataset_root&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/datasets
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>分布式深度学习</title>
   <link href="http://daiwk.github.io/posts/platform-distributed-dl.html"/>
   <updated>2018-07-09T00:00:00+00:00</updated>
   <id>/posts/platform-distributed-dl</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#111的文章&quot;&gt;11.1的文章&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#79的文章&quot;&gt;7.9的文章&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#dc-asgd算法补偿异步通信中梯度的延迟&quot;&gt;DC-ASGD算法：补偿异步通信中梯度的延迟&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ensemble-compression算法改进非凸模型的聚合方法&quot;&gt;Ensemble-Compression算法：改进非凸模型的聚合方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#随机重排下算法的收敛性分析改进分布式深度学习理论&quot;&gt;随机重排下算法的收敛性分析：改进分布式深度学习理论&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;111的文章&quot;&gt;11.1的文章&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650751264&amp;amp;idx=4&amp;amp;sn=0f5dfff2ebd1b37babd63b73be4863fc&amp;amp;chksm=871a855eb06d0c481fe419d9e6c1d5b3f62894d3c2cfe645f8209b0161d9a33dd66b7f27d8f0&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1105caQR87JgQQ5dm4fQjKEc&amp;amp;pass_ticket=sEqx376QcMxu0d9OAIUzH9EA050c09ikuLsDrlgH%2FaJhPa6Mv9arV5AxJ2UkeNs2#rd&quot;&gt;学界 | 深度神经网络的分布式训练概述：常用方法和技巧全面总结&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;79的文章&quot;&gt;7.9的文章&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652021689&amp;amp;idx=4&amp;amp;sn=a32b3a89ec9b27fb3ae0f14636aebac5&amp;amp;chksm=f121d748c6565e5ea1c22496a8b8b58029b50b23e7ba4f44fd15e98fb4225f129506b1b681c3&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=07086KpHqOtQ4edEpGgxvRyJ&amp;amp;pass_ticket=MbycpNZtuIW06kKTrmdAWLocG4d06ASGMcDZkz7VwgG9rlL9Wj9iLFK58BCCqJP6#rd&quot;&gt;分布式深度学习新进展：让“分布式”和“深度学习”真正深度融合&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;dc-asgd算法补偿异步通信中梯度的延迟&quot;&gt;DC-ASGD算法：补偿异步通信中梯度的延迟&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Asynchronous Stochastic Gradient Descent with Delay Compensation, ICML2017&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;其实这个在tensorRS里提到了呢&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensorflow-optimizations.html#%E6%A2%AF%E5%BA%A6%E8%A1%A5%E5%81%BF&quot;&gt;https://daiwk.github.io/posts/platform-tensorflow-optimizations.html#%E6%A2%AF%E5%BA%A6%E8%A1%A5%E5%81%BF&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;ensemble-compression算法改进非凸模型的聚合方法&quot;&gt;Ensemble-Compression算法：改进非凸模型的聚合方法&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Ensemble-Compression: A New Method for Parallel Training of Deep Neural Networks, ECML 2017&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;随机重排下算法的收敛性分析改进分布式深度学习理论&quot;&gt;随机重排下算法的收敛性分析：改进分布式深度学习理论&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Convergence Analysis of Distributed Stochastic Gradient Descent with Shuffling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1709.10432&quot;&gt;https://arxiv.org/abs/1709.10432&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>fastai</title>
   <link href="http://daiwk.github.io/posts/platform-fastai.html"/>
   <updated>2018-07-01T00:00:00+00:00</updated>
   <id>/posts/platform-fastai</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#使用多个而不是单一学习率&quot;&gt;使用多个而不是单一学习率&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#如何找到合适的学习率&quot;&gt;如何找到合适的学习率&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cosine-annealing&quot;&gt;cosine annealing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#带重启的sgd算法&quot;&gt;带重启的SGD算法&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/gf6Ebj9Nnh-QH7fLurH_WA&quot;&gt;称霸Kaggle的十大深度学习技巧&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原文：&lt;a href=&quot;https://blog.floydhub.com/ten-techniques-from-fast-ai/&quot;&gt;https://blog.floydhub.com/ten-techniques-from-fast-ai/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;安装直接(目前这个版本要求torch&amp;lt;0.4，而autokeras要求&amp;gt;=0.4.0，会有点小diff咯)&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;fastai
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用多个而不是单一学习率&quot;&gt;使用多个而不是单一学习率&lt;/h2&gt;

&lt;p&gt;差分学习率（Differential Learning rates）意味着在训练时&lt;strong&gt;变换网络层&lt;/strong&gt;比提高网络深度更重要。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/fastai/fastai/blob/master/courses/dl1/lesson1-vgg.ipynb&quot;&gt;https://github.com/fastai/fastai/blob/master/courses/dl1/lesson1-vgg.ipynb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;fastai.imports&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;fastai.transforms&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;fastai.conv_learner&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;fastai.model&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;fastai.dataset&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;fastai.sgdr&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;fastai.plots&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# import library for creating learning object for convolutional #networks
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sz&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;224&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vgg16&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# assign model to resnet, vgg, or even your own custom model
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'./imgs'&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;##文件夹要是imgs/train/1/xx.jpg, images/valid/1/xx.jpg
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageClassifierData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_paths&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tfms&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tfms_from_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# create fast ai data object, in this method we use from_paths where 
# inside PATH each image class is separated into different folders
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ConvLearner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pretrained&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;precompute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# create a learn object to quickly utilise state of the art
# techniques from the fast ai library
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后，冻结前面网络层并微调后面网络层：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# freeze layers up to the last one, so weights will not be updated.
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# train only the last layer for a few epochs
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当后面的网络效果比较好的时候，可以用差分学习率来改变前面的网络层，实践中，一般将学习率的缩小倍数设置为10倍：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unfreeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# set requires_grads to be True for all layers, so they can be updated
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# learning rate is set so that deepest third of layers have a rate of 0.001, # middle layers have a rate of 0.01, and final layers 0.1.
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# train model for three epoch with using differential learning rates
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;如何找到合适的学习率&quot;&gt;如何找到合适的学习率&lt;/h2&gt;

&lt;p&gt;一篇周期性学习率的paper:&lt;a href=&quot;https://arxiv.org/abs/1506.01186&quot;&gt;Cyclical Learning Rates for Training Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;用较低的学习率来训练，但在每个batch中以指数形式增加：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lr_find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# run on learn object where learning rate is increased  exponentially
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sched&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot_lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# plot graph of learning rate against iterationslr
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/cycle_lr.png&quot; style=&quot;max-height: 200px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;然后可以看看学习率和loss的关系&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sched&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# plots the loss against the learning rate
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/lr_loss.png&quot; style=&quot;max-height: 200px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;通过找出&lt;strong&gt;学习率最高&lt;/strong&gt;且&lt;strong&gt;Loss值仍在下降的值&lt;/strong&gt;来确定最佳学习率。在上述情况中，该值将为0.01。&lt;/p&gt;

&lt;h2 id=&quot;cosine-annealing&quot;&gt;cosine annealing&lt;/h2&gt;

&lt;p&gt;当逐渐接近loss最小值时，学习率应该变得更小来使得模型不会超调且尽可能接近这一点。余弦退火（Cosine annealing）利用余弦函数来降低学习率。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/cos_annealing.png&quot; style=&quot;max-height: 200px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;从上图可以看出，随着x的增加，余弦值&lt;strong&gt;首先缓慢下降&lt;/strong&gt;，然后&lt;strong&gt;加速下降&lt;/strong&gt;，再次&lt;strong&gt;缓慢下降&lt;/strong&gt;。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Calling learn fit automatically takes advantage of cosine annealing
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Fast.ai库中的learn.fit()函数，来快速实现这个算法，在整个周期中不断降低学习率，如下图所示：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/cos_annealing_fastai.png&quot; style=&quot;max-height: 200px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;带重启的sgd算法&quot;&gt;带重启的SGD算法&lt;/h2&gt;

&lt;p&gt;梯度下降算法可能陷入局部最小值，而不是全局最小值。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/local_minima.png&quot; style=&quot;max-height: 200px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;可以通过&lt;strong&gt;突然提高学习率&lt;/strong&gt;，来&lt;strong&gt;跳出局部最小值&lt;/strong&gt;并找到通向全局最小值的路径。这种方式称为&lt;strong&gt;带重启&lt;/strong&gt;的随机梯度下降方法（stochastic gradient descent with restarts, SGDR），这个方法在Loshchilov和Hutter的ICLR论文中展示出了很好的效果。&lt;a href=&quot;https://arxiv.org/abs/1608.03983&quot;&gt;SGDR: Stochastic Gradient Descent with Warm Restarts&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;当调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;learn.fit(learning_rate, epochs)&lt;/code&gt;函数时，学习率在每个周期开始时&lt;strong&gt;重置为参数输入时的初始值&lt;/strong&gt;，然后像上面&lt;strong&gt;余弦退火&lt;/strong&gt;部分描述的那样，逐渐减小。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/warm_restart.png&quot; style=&quot;max-height: 200px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;每当学习率下降到最小点，在上图中为每100次迭代，我们称为一个循环。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;cycle_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# decide how many epochs it takes for the learning rate to fall to
# its minimum point. In this case, 1 epoch
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cycle_mult&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# at the end of each cycle, multiply the cycle_len value by 2
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cycle_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cycle_mult&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# in this case there will be three restarts. The first time with
# cycle_len of 1, so it will take 1 epoch to complete the cycle.
# cycle_mult=2 so the next cycle with have a length of two epochs, 
# and the next four.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果我们把cycle_mult设成2：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/warm_restart_twice.png&quot; style=&quot;max-height: 200px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;关于这两个cycle_mult和cycle_len的函数，可以参考&lt;a href=&quot;http://forums.fast.ai/t/understanding-cycle-len-and-cycle-mult/9413/8&quot;&gt;http://forums.fast.ai/t/understanding-cycle-len-and-cycle-mult/9413/8&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>openai gpt</title>
   <link href="http://daiwk.github.io/posts/nlp-openai-gpt.html"/>
   <updated>2018-07-01T00:00:00+00:00</updated>
   <id>/posts/nlp-openai-gpt</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#gpt&quot;&gt;gpt&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gpt-2&quot;&gt;gpt-2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;gpt&quot;&gt;gpt&lt;/h2&gt;

&lt;p&gt;openai的gpt(generative pre-training)：&lt;a href=&quot;https://s3-us-west-2.amazonaws.com/openai-assets/research-covers/language-unsupervised/language_understanding_paper.pdf&quot;&gt;Improving language understanding with unsupervised learning&lt;/a&gt;。。改名字了『Improving Language Understanding by Generative Pre-Training』&lt;/p&gt;

&lt;p&gt;代码&lt;a href=&quot;https://github.com/openai/finetune-transformer-lm&quot;&gt;https://github.com/openai/finetune-transformer-lm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jiqizhixin.com/articles/2018-06-12-6&quot;&gt;https://www.jiqizhixin.com/articles/2018-06-12-6&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;blog原文：&lt;a href=&quot;https://blog.openai.com/language-unsupervised/&quot;&gt;https://blog.openai.com/language-unsupervised/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;该方法主要结合了两个已存的研究，即 Transformer 和无监督预训练。实验结果提供了非常令人信服的证据，其表明联合监督学习方法和无监督预训练能够得到非常好的性能。&lt;/p&gt;

&lt;p&gt;研究者开发的这种方式借鉴了他们关于 Sentiment Neuron，即&lt;a href=&quot;https://blog.openai.com/unsupervised-sentiment-neuron/&quot;&gt;https://blog.openai.com/unsupervised-sentiment-neuron/&lt;/a&gt;方面的研究成果，他们发现无监督学习技术在足够多的数据集上训练能产生令人惊讶的可区分特征。&lt;/p&gt;

&lt;p&gt;主要分为两个阶段：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先以无监督的方式在大型数据集上训练一个 Transformer，即使用语言建模作为训练信号，&lt;/li&gt;
  &lt;li&gt;然后在小得多的有监督数据集上精调模型以解决具体任务。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/openai-gpt.png&quot; style=&quot;max-height: 200px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;左图是transformer的结构，有两个目标&lt;/li&gt;
  &lt;li&gt;右图是不同应用场景下的fine-tuning方法&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这一项研究任务建立在&lt;a href=&quot;https://arxiv.org/abs/1511.01432&quot;&gt;Semi-supervised Sequence Learning&lt;/a&gt;论文中所提出的方法，该方法展示了如何通过无监督预训练的 LSTM 与有监督的精调提升文本分类性能。这一项研究还扩展了论文&lt;a href=&quot;https://arxiv.org/abs/1801.06146&quot;&gt;Universal Language Model Fine-tuning for Text Classification&lt;/a&gt;所提出的 ULMFiT 方法，它展示了单个与数据集无关的 LSTM 语言模型如何进行精调以在各种文本分类数据集上获得当前最优的性能。&lt;/p&gt;

&lt;p&gt;OpenAI 的研究工作展示了如何使用基于 Transformer 的模型，并在精调后能适应于除文本分类外其它更多的任务，例如常识推理、语义相似性和阅读理解。&lt;/p&gt;

&lt;p&gt;该方法与 ELMo 相似但更加通用，ELMo 同样也结合了预训练，但需要使用为任务定制的架构以在各种任务中取得当前顶尖的性能。&lt;/p&gt;

&lt;p&gt;OpenAI 只需要很少的调整就能实现最后的结果。所有数据集都使用单一的前向语言模型，且不使用任何集成方法，超参配置也与大多数研究成果相同。&lt;/p&gt;

&lt;p&gt;缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;计算需求：很多之前的解决 NLP 任务的方法可以在单块 GPU 上从零开始训练相对较小的模型。OpenAI 的方法在&lt;strong&gt;预训练步骤中需要很高的成本——在 8 块 GPU 上训练 1 个月&lt;/strong&gt;。幸运的是，这仅需要做一次，OpenAI 会发布他们的模型，从而其他人可以避免这一步。它也是一个很大的模型（相比于之前的工作），因而需要更多的计算和内存。OpenAI 使用了一个&lt;strong&gt;37 层（12 个模块）的 Tranformer&lt;/strong&gt;架构，并且在达到&lt;strong&gt;512 个 token的序列&lt;/strong&gt;上训练。多数实验都是在 4 和 8 块 GPU 的系统上构建的。该模型确实能很快速地精调到新的任务上，这缓解了额外的资源需求。&lt;/li&gt;
  &lt;li&gt;通过文本学习而导致的对世界理解的局限和偏差：在互联网上可用的书籍和文本并没有囊括关于世界的完整甚至是准确的信息。近期的研究表明特定类型的信息很难仅通过文本学习到，其它研究表明模型会从数据分布中学习和利用偏差。&lt;/li&gt;
  &lt;li&gt;泛化能力仍然很脆弱：虽然 OpenAI 的方法在多种任务中提高了性能，目前的深度学习 NLP 模型仍然展现出令人吃惊的和反直觉的行为，特别是当在系统化、对抗性或超出数据分布的方式进行评估的时候。OpenAI 的方法相比于之前的纯神经网络的方法在文本蕴涵任务上展示了提升的词法鲁棒性。在 Glockner 等人的论文《Breaking NLI Systems with Sentences that Require Simple Lexical Inferences》引入的数据集中，他们的模型达到了 83.75% 的准确率，和 KIM（Knowledge-based Inference Model，来自《NATURAL LANGUAGE INFERENCE WITH EXTERNAL KNOWLEDGE》）的性能相近（通过 WordNet 整合了外部知识）。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;未来方向：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;扩展该方法：研究者观察到语言模型性能的提高和下游任务的提高有很大关联。他们目前使用的是&lt;strong&gt;商用 GPU（单个 8GPU 机器）以及仅包含数千本书籍的训练数据集（约 5GB 的文本）。&lt;/strong&gt;这意味着如果使用验证效果好的方法以及更多的计算资源和数据，该模型还有很大的提升空间。&lt;/li&gt;
  &lt;li&gt;改善精调过程：研究者的方法目前还很简单。有可能使用更复杂的适应和迁移技术例如在ULMFiT中所探索的方法，可以让该模型获得显著的提升。&lt;/li&gt;
  &lt;li&gt;更好地理解生成式预训练的有效性：虽然本文中片面地讨论了一些思想，更多的目标指向的实验和研究将帮助分辨不同的解释。例如，实验中观察到的性能增益有多少是由于处理更广泛上下文的能力的提高，有多少是由于世界知识的提高。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;gpt-2&quot;&gt;gpt-2&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/67Z_dslvwTyRl3OMrArhCg&quot;&gt;完全图解GPT-2：看完这篇就够了（一）&lt;/a&gt;
&lt;a href=&quot;https://mp.weixin.qq.com/s/xk5fWrSBKErH8tvl-3pgtg&quot;&gt;完全图解GPT-2：看完这篇就够了（二）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://jalammar.github.io/illustrated-gpt2/&quot;&gt;https://jalammar.github.io/illustrated-gpt2/&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Deep contextualized word representations(ELMo)</title>
   <link href="http://daiwk.github.io/posts/nlp-elmo.html"/>
   <updated>2018-07-01T00:00:00+00:00</updated>
   <id>/posts/nlp-elmo</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#elmo&quot;&gt;ELMo&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#代码&quot;&gt;代码&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#pytorch版本&quot;&gt;pytorch版本&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tensorflow版本&quot;&gt;tensorflow版本&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/37915351&quot;&gt;https://zhuanlan.zhihu.com/p/37915351&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://cstsunfu.github.io/2018/06/ELMo/&quot;&gt;https://cstsunfu.github.io/2018/06/ELMo/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/triplemeng/article/details/82380202&quot;&gt;https://blog.csdn.net/triplemeng/article/details/82380202&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1802.05365&quot;&gt;Deep contextualized word representations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://allennlp.org/elmo&quot;&gt;https://allennlp.org/elmo&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;之前的glove以及word2vec的word embedding在nlp任务中都取得了最好的效果, 现在几乎没有一个NLP的任务中不加word embedding.&lt;/p&gt;

&lt;p&gt;我们常用的获取embedding方法都是通过训练language model, 将language model中预测的hidden state做为word的表示, 给定N个tokens的序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((t_1, t_2,...,t_n)\)&lt;/code&gt;, 前向language model就是通过前k-1个输入序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((t_1, t_2, ...,t_k)\)&lt;/code&gt;的hidden表示, 预测第k个位置的token, 反向的language model就是给定后面的序列, 预测之前的, 然后将language model的第k个位置的hidden输出做为word embedding。&lt;/p&gt;

&lt;p&gt;之前的做法的缺点是对于每一个单词都有唯一的一个embedding表示, 而对于多义词显然这种做法不符合直觉, 而单词的意思又和上下文相关, ELMo的做法是我们&lt;strong&gt;只预训练language model&lt;/strong&gt;, 而&lt;strong&gt;word embedding是通过输入的句子实时输出&lt;/strong&gt;的, 这样单词的意思就是上下文相关的了, 这样就很大程度上缓解了歧义的发生。且ELMo输出多个层的embedding表示, 试验中已经发现每层LM输出的信息对于不同的任务效果不同, 因此&lt;strong&gt;对每个token用不同层的embedding表示会提升效果&lt;/strong&gt;。&lt;/p&gt;

&lt;h2 id=&quot;elmo&quot;&gt;ELMo&lt;/h2&gt;

&lt;p&gt;使用双向的language model, 给定N个tokens&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((t1, t2,…,tN)\)&lt;/code&gt;, language model通过给定前面的k-1个位置的token序列计算第k个token的出现的概率:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(t_1, t_2, ..., t_N) = \prod_{k=1}^N p(t_k|t_1, t_2, ..., t_{k-1})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;后向的计算方法与前向相似:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(t_1, t_2, ..., t_N) = \prod_{k=1}^N p(t_k\vert t_{k+1}, t_{k+2}, ..., t_{N})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;双向的biLM训练过程中的目标就是最大化:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\sum^N_{k=1}(\log p(t_k| t_1, ...,t_{k-1};\Theta _x, \overrightarrow{\Theta}_{LSTM}, \Theta _s) + \log p(t_k\vert t_{k+1}, ...,t_{N}; \Theta _x, \overleftarrow{\Theta}_{LSTM}, \Theta _s))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;ELMo对于每一个token &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_k\)&lt;/code&gt;，通过一个L层的biLM得到2L+1个表示：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
R_k = \{x_k^{LM}, \overrightarrow{h}_{k,j}^{LM}, \overleftarrow{h}_{k, j}^{LM} \vert j=1, ..., L\} = \{h_{k,j}^{LM} \vert j=0,..., L\}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[h_{k,0}^{LM}\]&lt;/code&gt;是对token直接编码的结果((这里是对字符进行CNN编码)，即上式的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[x_k^{LM}\]&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{k,j}^{LM} = [\overrightarrow{h}_{k,j}^{LM}; \overleftarrow{h}_{k, j}^{LM}]\)&lt;/code&gt;是每个biLSTM层输出的结果&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在应用中，将ELMo中所有层的输出R压缩为单个向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(ELMo_k = E(R_k;\Theta _\epsilon)\)&lt;/code&gt;， 最简单的压缩方法是取最后一层的输出做为token的表示: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E(R_k) = h_{k,L}^{LM}\)&lt;/code&gt;。更通用的做法是通过一些参数来联合所有层的信息:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
ELMo_k^{task} = E(R_k;\Theta ^{task}) = \gamma ^{task} \sum _{j=0}^L s_j^{task}h_{k,j}^{LM}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_j\)&lt;/code&gt;是一个softmax出来的结果&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;是任务相关的scale参数&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pre-trained的language model是用了两层的biLM, 对token进行上下文无关的编码是通过CNN对字符进行编码, 然后将三层的输出scale到1024维,最后对每个token输出3个1024维的向量表示。&lt;/p&gt;

&lt;p&gt;这里之所以将3层的输出都作为token的embedding表示是因为实验已经证实不同层的LM输出的信息对于不同的任务作用是不同的, 也就是所不同层的输出捕捉到的token的信息是不相同的.&lt;/p&gt;

&lt;p&gt;ELMo只是提供了word级别的解决方案：利用它的pretrained biLM来产生word embedding,然后提供给下游的模型。这里的模型往往是sequence model，其效果已经在相应的NLP任务上得到验证。&lt;/p&gt;

&lt;p&gt;如何使用ElMo产生的表征呢？对于一个supervised NLP任务，可以分以下三步:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;产生pre-trained biLM模型。模型由两层bi-LSTM组成，之间用residual connection连接起来。&lt;/li&gt;
  &lt;li&gt;在任务语料上(注意是语料，忽略label)fine tuning上一步得到的biLM模型。可以把这一步看为biLM的domain transfer。&lt;/li&gt;
  &lt;li&gt;利用ELMo的word embedding来对任务进行训练。通常的做法是把它们作为输入加到已有的模型中，一般能够明显的提高原模型的表现。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在QA，Textual entailment，Semanic role labeling, Coreference resolution, NER，和 Sentiment analysis上的表现：&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Textual entailment(文本蕴含)：给定一个前提文本（premise），根据这个前提去推断假说文本（hypothesis）与premise的关系，一般分为蕴含关系（entailment）和矛盾关系（contradiction），蕴含关系（entailment）表示从premise中可以推断出hypothesis；矛盾关系（contradiction）即hypothesis与premise矛盾。文本蕴含的结果就是这几个概率值。使用的语料是Stanford Natural Language Inference (SNLI) corpus。&lt;/li&gt;
  &lt;li&gt;Coreference resolution(共指解析)：目的在于自动识别表示同一个实体的名词短语或代词，并将他们归类&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/elmo-experiment1.png&quot; style=&quot;max-height: 200px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;和state of art比基本上每个任务都有明显的改善。表中的OUR BASELINE在论文中有详细介绍，它指的是作者&lt;strong&gt;选定的某些已有的模型&lt;/strong&gt;。ELMo+BASELINE指的是作者把&lt;strong&gt;ELMo的word representation作为输入&lt;/strong&gt;提供给选定的模型。&lt;/p&gt;

&lt;h2 id=&quot;代码&quot;&gt;代码&lt;/h2&gt;

&lt;h3 id=&quot;pytorch版本&quot;&gt;pytorch版本&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/allenai/allennlp/blob/master/tutorials/how_to/elmo.md&quot;&gt;https://github.com/allenai/allennlp/blob/master/tutorials/how_to/elmo.md&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;tensorflow版本&quot;&gt;tensorflow版本&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/allenai/bilm-tf&quot;&gt;https://github.com/allenai/bilm-tf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码解析：&lt;a href=&quot;https://blog.csdn.net/jeryjeryjery/article/details/81183433&quot;&gt;https://blog.csdn.net/jeryjeryjery/article/details/81183433&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;看下cnn部分：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/char-cnn-1.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/char-cnn-2.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;正向lstm：每个时间步预测下一个词：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/elmo-lstm.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;反向lstm：输入序列逆序，每个时间步还是预测下一个词：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/elmo-reverse-lstm.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap13 值迭代网络</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap13.html"/>
   <updated>2018-06-11T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap13</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-背景&quot;&gt;1. 背景&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-dqn的缺陷&quot;&gt;1.1 DQN的缺陷&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-具有规划能力的策略网络&quot;&gt;2.2 具有规划能力的策略网络&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-值迭代网络&quot;&gt;2. 值迭代网络&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-代码解读&quot;&gt;3. 代码解读&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-基础知识&quot;&gt;3.1 基础知识：&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#python的slice函数&quot;&gt;python的slice函数&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tfnnconv2d&quot;&gt;tf.nn.conv2d&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tfreduce_&quot;&gt;tf.reduce_*&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-代码&quot;&gt;3.2 代码&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-背景&quot;&gt;1. 背景&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://papers.nips.cc/paper/6046-value-iteration-networks.pdf&quot;&gt;Value Iteration Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://zhuanlan.zhihu.com/p/25515755&quot;&gt;https://zhuanlan.zhihu.com/p/25515755&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;slide：&lt;a href=&quot;https://daiwk.github.io/assets/value-iteration-networks-slide.pdf&quot;&gt;https://daiwk.github.io/assets/value-iteration-networks-slide.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;11-dqn的缺陷&quot;&gt;1.1 DQN的缺陷&lt;/h3&gt;

&lt;p&gt;先从以下几个角度来理解DQN：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;DQN是一个深度神经网络：是一个由3个卷积层和2个全连接层组成的深度神经网络&lt;/li&gt;
  &lt;li&gt;DQN的训练方法是强化学习：调整神经网络权值有很多方法，只是在DQN中使用的是强化学习。详见第6章（&lt;a href=&quot;https://daiwk.github.io/posts/rl-stepbystep-chap6.html&quot;&gt;https://daiwk.github.io/posts/rl-stepbystep-chap6.html&lt;/a&gt;）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tamar等发现，&lt;strong&gt;已经调优的深度神经网络，很难泛化到其他游戏中，即，该网络并没有学到本质。。。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;原因就在于，DQN的网络结构是前向的多层神经网络。&lt;strong&gt;输入是状态，输出是动作，也就是策略。&lt;/strong&gt;Tamar等人称这种策略为&lt;strong&gt;『reactive policy(反应式策略)』&lt;/strong&gt;。也就是给定一个状态，得到一个反应动作。&lt;/p&gt;

&lt;p&gt;从强化学习要解决的任务来看，强化学习要解决的是序贯决策问题，&lt;strong&gt;即当前的决策要考虑后续的决策，使得整个策略总体最优&lt;/strong&gt;，而反应式策略并不能表达后续策略对当前策略的影响。。&lt;/p&gt;

&lt;h3 id=&quot;22-具有规划能力的策略网络&quot;&gt;2.2 具有规划能力的策略网络&lt;/h3&gt;

&lt;p&gt;所谓的规划就是&lt;strong&gt;考虑后续的回报&lt;/strong&gt;。目前大部分强化学习所用的深度网络都是反应是网络，缺少显式的规划计算。但由于&lt;strong&gt;训练方法用的是强化学习训练方法，在训练时考虑了规划问题&lt;/strong&gt;，所以很多网络还是比较成功的。但由于网络本身没有规划模块，所以运用到新环境时，大部分需要重新训练，即泛化能力差。&lt;/p&gt;

&lt;p&gt;如果训练策略本身有规划模块，有以下两个好处：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;可以利用&lt;strong&gt;已经训练好的规划模块&lt;/strong&gt;规划新的任务，泛化能力很强&lt;/li&gt;
  &lt;li&gt;训练方法可以更灵活，不必依赖强化学习算法。&lt;strong&gt;可以利用成熟的监督学习方法和模仿学习方法&lt;/strong&gt;。而，&lt;strong&gt;如果没有数据标签时，仍然要用强化学习的训练方法&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-值迭代网络&quot;&gt;2. 值迭代网络&lt;/h2&gt;

&lt;p&gt;最常用的规划算法是值迭代算法，第3章讲了动态规划的思想。规划实际蕴含的是一个优化问题，基于贝尔曼优化原理：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon ^*(s)=\underset{a}{max}R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon ^*(s')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;基于该原理，具体的算法实现是值迭代算法，在第3章中提到了&lt;a href=&quot;https://daiwk.github.io/posts/rl-stepbystep-chap3.html#13-%E5%80%BC%E5%87%BD%E6%95%B0%E8%BF%AD%E4%BB%A3%E7%AE%97%E6%B3%95&quot;&gt;https://daiwk.github.io/posts/rl-stepbystep-chap3.html#13-%E5%80%BC%E5%87%BD%E6%95%B0%E8%BF%AD%E4%BB%A3%E7%AE%97%E6%B3%95&lt;/a&gt;：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;输入：状态转移概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;，回报函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R^a_{s}\)&lt;/code&gt;，折扣因子&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;，初始化值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon(s)=0\)&lt;/code&gt;，初始化策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi_0\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Repeat &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l=0,1,...\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;for every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt; do&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon _{l+1}(s)=\underset{a}{max}R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon _l(s')\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Until &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon _{l+1}=\upsilon _l\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;输出：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(s)=argmax_aR^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon _l(s')\)&lt;/code&gt;&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;由于值迭代的计算过程与CNN的传播过程很相似，所以可以&lt;strong&gt;利用CNN来表示值迭代过程。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;先看看值迭代计算过程与CNN传播过程的相似之处：&lt;/p&gt;

&lt;p&gt;值迭代中，最关键的公式是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon _{l+1}(s)=\underset{a}{max}R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon _l(s')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可以分解为两个步骤：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;遍历动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;，得到不同动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;对应的值函数更新，即：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon _{l+1}(s)=R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon _l(s')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这一步相当于CNN中的卷积操作。相当于图中的，输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\bar{R}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Prev.\ Value\)&lt;/code&gt;，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\bar{Q}\)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;遍历动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;，找到最大的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon_{l+1}(s,a)\)&lt;/code&gt;：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon _{l+1}(s)=\underset{a}{max}\upsilon _{l+1}(s,a)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这一步相当于CNN中的池化操作。相当于图中的，输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\bar{Q}\)&lt;/code&gt;，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(New\ Value\ \bar{V}\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，可以将值迭代的过程用CNN嵌入到策略网络中，而与CNN有如下的不同：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;偏移量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R^a_s\)&lt;/code&gt;对应着每个像素的偏移量(bias)&lt;/li&gt;
  &lt;li&gt;状态转移函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;对应着卷积核，因为它要和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\bar{V}\)&lt;/code&gt;进行点积再求和&lt;/li&gt;
  &lt;li&gt;卷积核的个数对应着动作空间的维数&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/value-iteration-network-1.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;这个网络迭代k次，可以理解为网络往后多看了k步之后的值函数。【相当于一个k个时间步的cnn(conv+maxpooling)，然后再和原始输入做个attention？】&lt;/p&gt;

&lt;p&gt;k步之后得到的最优策略为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi ^*(s)=argmax_aR(s,a)+\gamma \sum _{s'} P(s'|s,a)V^*(s')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;处的策略只和它的邻域的值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V^*(s')\)&lt;/code&gt;有关。而在nn中，当给定的标签只与输入特征的一个局部相关时，就是attention机制。所以，VIN在VI模块后，加了一个attention网络。&lt;/p&gt;

&lt;p&gt;训练方法的话，可以采用模仿学习（IL），也可以采用强化学习（RL）。模仿学习就是利用专家数据对网络参数进行训练，例如针对导航任务，专家数据可以来自传统的规划算法，如Dijkstra算法或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A^*\)&lt;/code&gt;算法。&lt;/p&gt;

&lt;h2 id=&quot;3-代码解读&quot;&gt;3. 代码解读&lt;/h2&gt;

&lt;p&gt;tf：&lt;a href=&quot;https://github.com/TheAbhiKumar/tensorflow-value-iteration-networks&quot;&gt;https://github.com/TheAbhiKumar/tensorflow-value-iteration-networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pytorch：&lt;a href=&quot;https://github.com/kentsommer/pytorch-value-iteration-networks&quot;&gt;https://github.com/kentsommer/pytorch-value-iteration-networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;tf版本&lt;/p&gt;

&lt;h3 id=&quot;31-基础知识&quot;&gt;3.1 基础知识：&lt;/h3&gt;

&lt;h4 id=&quot;python的slice函数&quot;&gt;python的slice函数&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;### start -- 起始位置
### stop -- 结束位置
### step -- 间距
## 示例
&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myslice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 设置截取5个元素的切片
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;myslice&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myslice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;# 截取 5 个元素
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;tfnnconv2d&quot;&gt;tf.nn.conv2d&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/knowledge-tf-usage.html#tfnnconv2d&quot;&gt;https://daiwk.github.io/posts/knowledge-tf-usage.html#tfnnconv2d&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;tfreduce_&quot;&gt;tf.reduce_*&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/knowledge-tf-usage.html#tfreduce_&quot;&gt;https://daiwk.github.io/posts/knowledge-tf-usage.html#tfreduce_&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;32-代码&quot;&gt;3.2 代码&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;flipkernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;conv2d_flipkernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flipkernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;strides&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'SAME'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;VI_Block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Number of value iterations performed
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;ch_i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch_i&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Channels in input layer
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;ch_h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch_h&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Channels in initial hidden layer
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;ch_q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch_q&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Channels in q layer (~actions)
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;state_batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;statebatchsize&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# k+1 state inputs for each channel
&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# weights from inputs to q layer (~reward in Bellman equation)
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;w0&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 从X到h？
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;w1&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 从h到reward？
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;     &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 从reward到q？
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# feedback weights from v layer into q layer (~transition probabilities in Bellman equation)
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;w_fb&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;w_o&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch_q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# initial conv layer over image+reward prior
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv2d_flipkernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;h0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv2d_flipkernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv2d_flipkernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;q&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_dims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 相当于maxpooling
&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# reward和上一步的v一起，接下来进行卷积
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;wwfb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w_fb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv2d_flipkernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wwfb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;q&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_dims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# do one last convolution
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv2d_flipkernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                          &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w_fb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;q&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# CHANGE TO THEANO ORDERING
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Since we are selecting over channels, it becomes easier to work with
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# the tensor when it is in NCHW format vs NHWC
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Select the conv-net channels at the state position (S1,S2).
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# This intuitively corresponds to each channel representing an action, and the convnet the Q function.
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# The tricky thing is we want to select the same (S1,S2) position *for each* channel and for each sample
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# TODO: performance can be improved here by substituting expensive
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#       transpose calls with better indexing for gather_nd
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;bs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rprn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state_batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ins1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ins2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;idx_in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ins1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ins2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rprn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;q_out&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gather_nd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;q_out&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# add logits
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w_o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# softmax output weights
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>ray</title>
   <link href="http://daiwk.github.io/posts/platform-ray.html"/>
   <updated>2018-06-05T00:00:00+00:00</updated>
   <id>/posts/platform-ray</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ray的ui&quot;&gt;ray的ui&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#使用&quot;&gt;使用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#简单的数据并行&quot;&gt;简单的数据并行&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#有任务依赖的并行&quot;&gt;有任务依赖的并行&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ray-rllib&quot;&gt;ray-rllib&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jiqizhixin.com/articles/2018-01-10-2&quot;&gt;伯克利AI分布式框架Ray，兼容TensorFlow、PyTorch与MXNet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github: &lt;a href=&quot;https://github.com/ray-project&quot;&gt;https://github.com/ray-project&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;tutorials: &lt;a href=&quot;http://ray.readthedocs.io/en/latest/tutorial.html&quot;&gt;http://ray.readthedocs.io/en/latest/tutorial.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;ray的ui&quot;&gt;ray的ui&lt;/h2&gt;

&lt;p&gt;首先参考&lt;a href=&quot;http://ipywidgets.readthedocs.io/en/latest/user_install.html&quot;&gt;http://ipywidgets.readthedocs.io/en/latest/user_install.html&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ipywidgets
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后设置一下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jupyter nbextension &lt;span class=&quot;nb&quot;&gt;enable&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--py&lt;/span&gt; widgetsnbextension
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再然后，启动jupyter:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;jupyter notebook &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;还需要装个chrome的插件trace-viewer：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/catapult-project/catapult&quot;&gt;https://github.com/catapult-project/catapult&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/catapult-project/catapult.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后还要安装bokeh&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;bokeh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在我们执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ray.init(num_cpus=4, redirect_output=True)&lt;/code&gt;的时候会有提示，例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;View the web UI at http://localhost:8889/notebooks/ray_ui82961.ipynb?token&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;43724f05dc0a2b1897bf50e9c9d01541a1bfef8ba9030eac
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;点开这个url，就可以看到ui的几个用法了：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ray.experimental.ui&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ui&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;redis_address&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;environ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;REDIS_ADDRESS&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;## 从刚init的那个环境再init一下下
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ui.object_search_bar()&lt;/code&gt;可以查看objectid的信息，例如：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Search&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;95&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f3bf18ee668c11d8f3d2c51861718daea2240d&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;## 输出
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'DataSize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;516&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'Hash'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'725b5c6d41876612000000001400000095f3bf18ee668c11d8f3d2c51861718d'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'IsPut'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'ManagerIDs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'ba72fda097848a8638b6b6d66a7eb58d65748e88'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'TaskID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'94f3bf18ee668c11d8f3d2c51861718daea2240d'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ui.task_search_bar()&lt;/code&gt;可以查看taskid的信息，例如：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Search&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;94&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f3bf18ee668c11d8f3d2c51861718daea2240d&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'ExecutionDependencies'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'ExecutionDependenciesString'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x0c\x00\x00\x00\x00\x00\x06\x00&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;
                                &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x08\x00\x04\x00\x06\x00\x00\x00&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;
                                &lt;span class=&quot;sa&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x04\x00\x00\x00\x00\x00\x00\x00&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'LocalSchedulerID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'d70027558552b191028c0fde93adc09114eb4c52'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'SpillbackCount'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'State'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'TaskSpec'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'ActorCounter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'ActorCreationDummyObjectID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'ffffffffffffffffffffffffffffffffffffffff'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'ActorCreationID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'ffffffffffffffffffffffffffffffffffffffff'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'ActorID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'ffffffffffffffffffffffffffffffffffffffff'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'Args'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'DriverID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'6ed2713441d1e526abb1f1bc591727511c627f4b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'FunctionID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'e7ebc6e9f04fcacac3d998ac5c3b611067cc9b8d'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'ParentCounter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'ParentTaskID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'b256658bf683d1525d52fb4a6b6faed08fba35ac'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'RequiredResources'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'CPU'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'ReturnObjectIDs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;95&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f3bf18ee668c11d8f3d2c51861718daea2240d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt;
              &lt;span class=&quot;s&quot;&gt;'TaskID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'94f3bf18ee668c11d8f3d2c51861718daea2240d'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ui.task_timeline()&lt;/code&gt;则稍微有点复杂。。点击”View task timeline”，会生成一个json文件，例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/folders/9q/91xmxq4d1zl__l2w9lsp22mj6x47pl/T/tmpr6x81_js.json&lt;/code&gt;，然后就需要执行：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;catapult/tracing/bin/trace2html /var/folders/9q/91xmxq4d1zl__l2w9lsp22mj6x47pl/T/tmpr6x81_js.json &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;my_trace.html &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; open my_trace.html
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就可以在浏览器中打开啦~&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ray-ui-trace-viewer.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://daiwk.github.io/assets/my_trace.html&quot;&gt;https://daiwk.github.io/assets/my_trace.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;从图中可以看出，我跑了三次，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一次是20多个tasks，分布在4个不同的worker上并行执行&lt;/li&gt;
  &lt;li&gt;第二次1个task，建了一个新的worker&lt;/li&gt;
  &lt;li&gt;第三次7个task，分布在4个worker上并行执行&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外，下面这几个是基于bokeh的，可以直接在jupyter里看：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task_completion_time_distribution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cpu_usage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cluster_usage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用&quot;&gt;使用&lt;/h2&gt;

&lt;h3 id=&quot;简单的数据并行&quot;&gt;简单的数据并行&lt;/h3&gt;

&lt;p&gt;定义：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# A regular Python function.
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;regular_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# A Ray remote function.
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;remote_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;运行时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;remote_function.remote()&lt;/code&gt;返回的是一个objectID，然后create了一个task。想要拿到结果，就要执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ray.get&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;regular_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
 &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;remote_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;ObjectID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c80d6937802cd7786ad25e50caf2f023c95e350&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
 &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;数据并行：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;slow_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;会发现最后的结果是保持了原来的顺序的，应该是并行执行，然后最后会按先后顺序聚合。&lt;/p&gt;

&lt;h3 id=&quot;有任务依赖的并行&quot;&gt;有任务依赖的并行&lt;/h3&gt;

&lt;p&gt;只要不get，就可以把objectid一路传下去，最后get一下就行，这样多个remote间就是有依赖关系的了，例如(只get了一次：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ray.get(losses)&lt;/code&gt;)：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;normalize_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;extract_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normalized_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hstack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normalized_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;normalized_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;compute_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;num_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;start_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;losses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'file1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'file2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'file3'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'file4'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;normalized_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;normalize_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;extract_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normalized_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;compute_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;losses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'The losses are {}.'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;losses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;losses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;end_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;duration&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start_time&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'The loss is {}. This took {} seconds. Run the next cell to see '&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;'if the exercise was done correctly.'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ray-dependency-ui-trace-viewer.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;可见一个task里，是串行地执行load_data-&amp;gt;normalize_data-&amp;gt;extract_features-&amp;gt;compute_loss的。而每一个函数内部又分为get_arguments-&amp;gt;execute-&amp;gt;store_outputs三个阶段。&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://daiwk.github.io/assets/my_trace.dependency.html&quot;&gt;https://daiwk.github.io/assets/my_trace.dependency.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;ray-rllib&quot;&gt;ray-rllib&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/platform-ray-rllib.html&quot;&gt;https://daiwk.github.io/posts/platform-ray-rllib.html&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ray-tune</title>
   <link href="http://daiwk.github.io/posts/platform-ray-tune.html"/>
   <updated>2018-06-05T00:00:00+00:00</updated>
   <id>/posts/platform-ray-tune</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E7%AE%80%E4%BB%8B&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E4%BD%BF%E7%94%A8&quot;&gt;使用&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1807.05118v1.pdf&quot;&gt;Tune: A Research Platform for Distributed Model Selection and Training&lt;/a&gt;
&lt;a href=&quot;https://ray.readthedocs.io/en/latest/tune.html&quot;&gt;https://ray.readthedocs.io/en/latest/tune.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;一个常见的例子涉及到模型的建立。 数据科学家要花费相当多的时间进行实验，其中许多涉及调整他们最爱的机器学习算法的参数。 随着深度学习和RL变得越来越流行，数据科学家将需要某种软件工具来进行高效的超参数调整和其他形式的实验和模拟。 RayTune是一个新的&lt;strong&gt;深度学习和RL分布式超参数搜索框架&lt;/strong&gt;。 它建立在Ray之上，与RLlib紧密结合。 RayTune基于网格搜索，并使用early stopping的想法，包括&lt;strong&gt;中位数停止&lt;/strong&gt;规则和&lt;strong&gt;HyperBand&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;有越来越多的开放源代码软件工具可用于希望深入学习和RL的公司。我们处于经验时代，我们需要能够实现快速并行实验的工具，同时让我们能够利用流行的软件库，算法和组件。 Ray刚刚添加了两个库，让公司可以进行强化学习，并有效搜索神经网络架构的空间。&lt;/p&gt;

&lt;p&gt;强化学习应用程序涉及多个组件，每个组件提供分布式计算的机会。 Ray RLlib采用了一种编程模型，可以轻松组合和重用组件，并利用多层次并行性和物理设备的并行性。在短期内，RISE实验室计划添加更多的RL算法，用于与在线服务集成的API，支持多智能体场景，以及一组扩展的优化策略。&lt;/p&gt;

&lt;h2 id=&quot;使用&quot;&gt;使用&lt;/h2&gt;

&lt;p&gt;首先import&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ray&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ray.tune&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tune&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后对想要tune的函数，加一个reporter参数，并把metrics传给reporter:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;train_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reporter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# add a reporter arg
&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SGD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;momentum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;momentum&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

     &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;accuracy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;reporter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean_accuracy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;accuracy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# report metrics
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;最后，设置搜索范围并执行：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;all_trials&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tune&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run_experiments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;my_experiment&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;run&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;stop&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mean_accuracy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;config&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;momentum&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tune&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid_search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>ray-rllib</title>
   <link href="http://daiwk.github.io/posts/platform-ray-rllib.html"/>
   <updated>2018-06-05T00:00:00+00:00</updated>
   <id>/posts/platform-ray-rllib</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E7%AE%80%E4%BB%8B&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tune&quot;&gt;tune&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E4%BD%BF%E7%94%A8&quot;&gt;使用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#gym%E8%87%AA%E5%B8%A6%E7%9A%84%E7%8E%AF%E5%A2%83&quot;&gt;gym自带的环境&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1712.09381&quot;&gt;RLlib: Abstractions for Distributed Reinforcement Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;部分参考自：&lt;a href=&quot;https://blog.csdn.net/zkh880loLh3h21AJTH/article/details/79620311&quot;&gt;https://blog.csdn.net/zkh880loLh3h21AJTH/article/details/79620311&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;现有的强化学习libs：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rl-related-libs.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;几个出发点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;需要支持现有的机器学习库。&lt;/strong&gt;因为RL通常使用基于梯度下降或进化算法来学习和拟合策略函数，所以您需要它支持您最喜欢的库（TensorFlow，Keras，PyTorch等）。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;可扩展性。&lt;/strong&gt;RL是&lt;strong&gt;计算密集型&lt;/strong&gt;的，并且在关键应用程序中开始使用RL时，可以选择以分布式方式运行。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;可组合性。&lt;/strong&gt;RL算法通常涉及模拟和许多其他组件。 您将需要一个库，允许您&lt;strong&gt;重用与多种深度学习框架兼容的RL算法组件（例如策略图，走子演算[这是啥。。]等）&lt;/strong&gt;，并提供&lt;strong&gt;可组合的分布式执行原语（嵌套并行）&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tune&quot;&gt;tune&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/platform-ray-tune.html&quot;&gt;https://daiwk.github.io/posts/platform-ray-tune.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;使用&quot;&gt;使用&lt;/h2&gt;

&lt;p&gt;最基础用法(使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib/python2.7/site-packages/ray/rllib/train.py&lt;/code&gt;)：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python ./train.py &lt;span class=&quot;nt&quot;&gt;--run&lt;/span&gt; DQN &lt;span class=&quot;nt&quot;&gt;--env&lt;/span&gt; CartPole-v0 
python ./train.py &lt;span class=&quot;nt&quot;&gt;--run&lt;/span&gt; APEX &lt;span class=&quot;nt&quot;&gt;--env&lt;/span&gt; CartPole-v0
python ./train.py &lt;span class=&quot;nt&quot;&gt;--run&lt;/span&gt; APEX_DDPG &lt;span class=&quot;nt&quot;&gt;--env&lt;/span&gt; Pendulum-v0
python ./train.py &lt;span class=&quot;nt&quot;&gt;--run&lt;/span&gt; DDPG &lt;span class=&quot;nt&quot;&gt;--env&lt;/span&gt; Pendulum-v0
python ./train.py &lt;span class=&quot;nt&quot;&gt;--run&lt;/span&gt; DDPG2 &lt;span class=&quot;nt&quot;&gt;--env&lt;/span&gt; Pendulum-v0
python ./train.py &lt;span class=&quot;nt&quot;&gt;--run&lt;/span&gt; A3C &lt;span class=&quot;nt&quot;&gt;--env&lt;/span&gt; CartPole-v0 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rllib-stack.svg&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rllib-api.svg&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;gym自带的环境&quot;&gt;gym自带的环境&lt;/h3&gt;

&lt;p&gt;gym的所有自带的环境（注意，rllib里的ddpg适用的问题是Box的，Discrete的不能解）&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openai/gym/wiki/Table-of-environments&quot;&gt;https://github.com/openai/gym/wiki/Table-of-environments&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>bazel</title>
   <link href="http://daiwk.github.io/posts/platform-bazel.html"/>
   <updated>2018-06-02T00:00:00+00:00</updated>
   <id>/posts/platform-bazel</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#c&quot;&gt;c++&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;c&quot;&gt;c++&lt;/h2&gt;

&lt;p&gt;下载demo&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/bazelbuild/examples/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;examples
└── cpp-tutorial
    ├──stage1
    │  ├── main
    │  │   ├── BUILD
    │  │   └── hello-world.cc
    │  └── WORKSPACE
    ├──stage2
    │  ├── main
    │  │   ├── BUILD
    │  │   ├── hello-world.cc
    │  │   ├── hello-greet.cc
    │  │   └── hello-greet.h
    │  └── WORKSPACE
    └──stage3
       ├── main
       │   ├── BUILD
       │   ├── hello-world.cc
       │   ├── hello-greet.cc
       │   └── hello-greet.h
       ├── lib
       │   ├── BUILD
       │   ├── hello-time.cc
       │   └── hello-time.h
       └── WORKSPACE
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;在project的根目录有一个WORKSPACE文件&lt;/li&gt;
  &lt;li&gt;有一个BUILD文件的目录是一个package&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;常用命令&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;build WORKSPACE 下面所有的 target，会扫所有的目录&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build //...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;单独的 build 的 target 则直接 //:demo， 这里 :demo 是 target name&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build //:demo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;执行 :demo&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel run //:demo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;跑 demo_test 测试&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;baze &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; //:demo_test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>tf serving docker+k8s</title>
   <link href="http://daiwk.github.io/posts/dl-tf-serving-docker.html"/>
   <updated>2018-06-02T00:00:00+00:00</updated>
   <id>/posts/dl-tf-serving-docker</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#tf-serving&quot;&gt;tf-serving&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#基本流程&quot;&gt;基本流程&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#将graph进行freeze&quot;&gt;将graph进行freeze&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#调整推断代码&quot;&gt;调整推断代码&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#容器化&quot;&gt;容器化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#添加api层&quot;&gt;添加API层&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#部署可以运行推断脚本的扩展容器&quot;&gt;部署可以运行推断脚本的扩展容器&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#部署运行api层的扩展容器&quot;&gt;部署运行API层的扩展容器&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#缓解计算成本的累积&quot;&gt;缓解计算成本的累积&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#重复使用会话&quot;&gt;重复使用会话&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#缓存输入&quot;&gt;缓存输入&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#使用任务队列&quot;&gt;使用任务队列&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#部署小结&quot;&gt;部署小结&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker--tf-servingfrom-阿里云&quot;&gt;docker + tf-serving[from 阿里云]&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker--k8s--tf-serving自己搞&quot;&gt;docker + k8s + tf-serving[自己搞]&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#dockertf-serving&quot;&gt;docker+tf-serving&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#创建docker镜像&quot;&gt;创建docker镜像&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#编译server&quot;&gt;编译server&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#编译examples&quot;&gt;编译examples&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#mnist-example&quot;&gt;mnist example&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#inception-example&quot;&gt;inception example&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#commit-container&quot;&gt;commit container&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#在2个容器间进行访问&quot;&gt;在2个容器间进行访问&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#k8s&quot;&gt;k8s&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;tf-serving&quot;&gt;tf-serving&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/serving&quot;&gt;https://github.com/tensorflow/serving&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;安装方式见&lt;a href=&quot;https://github.com/tensorflow/serving/blob/master/tensorflow_serving/g3doc/setup.md&quot;&gt;https://github.com/tensorflow/serving/blob/master/tensorflow_serving/g3doc/setup.md&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;基本流程&quot;&gt;基本流程&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650755184&amp;amp;idx=4&amp;amp;sn=f597d101749e9bbc1186a95900f4a26d&amp;amp;chksm=871a940eb06d1d18070d8c8dadb9e2dd36397a1734da8932776a1b8358b35af694814865a01d&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0107WNE0JGfEKwXhHPQWbClR&amp;amp;pass_ticket=mPnDPDR4heSU20VXT7N8W622Cb1dZmIzkNcF8BygI%2Bp60d7GrSesIej%2FlrFbnO84#rd&quot;&gt;没人告诉你的大规模部署AI高效流程！&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;将graph&lt;strong&gt;固化&lt;/strong&gt;为Protobuf二进制文件&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;调整推断代码&lt;/strong&gt;，使它可以&lt;strong&gt;处理固化的图&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;容器化&lt;/strong&gt;应用程序&lt;/li&gt;
  &lt;li&gt;在最上面加上&lt;strong&gt;API层&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;将graph进行freeze&quot;&gt;将graph进行freeze&lt;/h3&gt;

&lt;p&gt;「固化」graph要用所有命名节点、权重、架构和检查点元数据，并创建一个protobuf二进制文件。最常用的是tf自己的工具，它可以固化任何给定输出节点名字的graph。参考&lt;a href=&quot;https://www.tensorflow.org/guide/extend/model_files#freezing&quot;&gt;https://www.tensorflow.org/guide/extend/model_files#freezing&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;调整推断代码&quot;&gt;调整推断代码&lt;/h3&gt;

&lt;p&gt;在大多数情况下，feed_dict 是不变的，主要区别在于添加了加载模型的代码，也许还有输出节点的规范。&lt;/p&gt;

&lt;h3 id=&quot;容器化&quot;&gt;容器化&lt;/h3&gt;

&lt;p&gt;只要在 Dockerfile 中设置环境即可&lt;/p&gt;

&lt;h3 id=&quot;添加api层&quot;&gt;添加API层&lt;/h3&gt;

&lt;p&gt;两种通用的方法：&lt;/p&gt;

&lt;h4 id=&quot;部署可以运行推断脚本的扩展容器&quot;&gt;部署可以运行推断脚本的扩展容器&lt;/h4&gt;

&lt;p&gt;这些容器根据输入运行脚本，脚本启动一个会话并执行推断，再&lt;strong&gt;通过管道&lt;/strong&gt;返回输出结果。这种方法效率是很低的：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对大多数云供应商而言添加一个可以操纵容器和管道进出的 API 层并不容易&lt;/li&gt;
  &lt;li&gt;在启动容器、分配硬件、启动会话以及推断时会损失宝贵的时间&lt;/li&gt;
  &lt;li&gt;你让stdin开着并保持管道输出，那么你的脚本就会加速但是会失去可扩展性&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;部署运行api层的扩展容器&quot;&gt;部署运行API层的扩展容器&lt;/h4&gt;

&lt;p&gt;这种方法效率更高:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;虽然这需要更多资源，但它已经用了最少资源而且没有垂直扩展&lt;/li&gt;
  &lt;li&gt;允许每个容器保持运行状态&lt;/li&gt;
  &lt;li&gt;由于这种情况下 API 是分散的，因此可以将特定的stdin/stout连接到主要的请求路由器上&lt;/li&gt;
  &lt;li&gt;省去了启动时间，可以在服务多个请求的同时维持速度并保证水平扩展&lt;/li&gt;
  &lt;li&gt;可以用负载平衡器集中容器，并用Kubernetes保证近乎100%的运行时间并管理集群&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;缓解计算成本的累积&quot;&gt;缓解计算成本的累积&lt;/h3&gt;

&lt;p&gt;通过容器集群分散 API 的主要缺点在于计算成本会相对较快地累积起来。这在AI中是不可避免的，但有一些方法可以缓解这一问题。&lt;/p&gt;

&lt;h4 id=&quot;重复使用会话&quot;&gt;重复使用会话&lt;/h4&gt;

&lt;p&gt;集群会根据负载成比例地增长和收缩，因此你的目标是&lt;strong&gt;最小化执行推断的时间&lt;/strong&gt;，使&lt;strong&gt;容器&lt;/strong&gt;可以&lt;strong&gt;释放出来&lt;/strong&gt;处理另外的请求。&lt;/p&gt;

&lt;p&gt;所以可以初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.Session&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.Graph&lt;/code&gt;后就将它们&lt;strong&gt;存储起来&lt;/strong&gt;并将它们&lt;strong&gt;作为全局变量&lt;/strong&gt;传递，以达到重复使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.Session&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.Graph&lt;/code&gt;的目的。&lt;/p&gt;

&lt;p&gt;这样做可以减少启动会话和构建图的时间，从而大大提高推断任务的速度，即便是单个容器，这个方法也是有效的。这一技术被广泛用于资源再分配最小化和效率最大化。&lt;/p&gt;

&lt;h4 id=&quot;缓存输入&quot;&gt;缓存输入&lt;/h4&gt;

&lt;p&gt;如果可能的话还要缓存输出。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;动态规划范式&lt;/strong&gt;在AI中是最重要的。缓存输入，你可以节省预处理输入或从远程获得输入的时间；缓存输出，你可以节省运行推断的时间。&lt;/p&gt;

&lt;p&gt;通常，你的模型会随着时间的推移变得更好，但这会很大程度上影响你的&lt;strong&gt;输出缓存机制&lt;/strong&gt;。例如，可以使用80-20原则，当模型准确率低于80% 时，不会缓存任何输出；一旦准确率到了80%，就&lt;strong&gt;开始缓存&lt;/strong&gt;并设置为在&lt;strong&gt;准确率到一定值&lt;/strong&gt;（而不是某个时间点）的时候&lt;strong&gt;停止缓存&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;随着模型变得越来越准确，输出也会发生变化，但是在「80-20」缓存中，性能和速度之间存在的权衡更少。&lt;/p&gt;

&lt;h4 id=&quot;使用任务队列&quot;&gt;使用任务队列&lt;/h4&gt;

&lt;p&gt;一般需要运行或大或小的推断任务，对UX来说，使用&lt;strong&gt;堆队列&lt;/strong&gt;（heap queue）可能更好，它会优先处理小一些的任务，这样，要运行简单步骤的用户只要等这一步结束就行了，而不必等另一个用户的更大推断任务先完成。&lt;/p&gt;

&lt;p&gt;在带有任务队列的专用GPU上训练模型。如果你要将每个交互返回到模型中进行训练，请考虑在单独的服务器或GPU上运行。一旦训练结束，你就可以将模型（在AWS中，你可以将模型repo集中在S3中）部署到容器中了。&lt;/p&gt;

&lt;h3 id=&quot;部署小结&quot;&gt;部署小结&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;固化图并将推断封装在API下&lt;/li&gt;
  &lt;li&gt;重复使用会话和图，缓存输入和输出&lt;/li&gt;
  &lt;li&gt;用Docker容器化应用程序（包括API层）&lt;/li&gt;
  &lt;li&gt;将大规模应用程序与Kubernetes一起部署在你选择的云上&lt;/li&gt;
  &lt;li&gt;将训练从推断中分离出来&lt;/li&gt;
  &lt;li&gt;建立任务队列，提高较小的任务的运行优先级&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;docker--tf-servingfrom-阿里云&quot;&gt;docker + tf-serving[from 阿里云]&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;注意：1.6版本的docker不行（没有–link参数，-p参数不是port），亲测1.9.1的docker可以。。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://yq.aliyun.com/articles/60894&quot;&gt;https://yq.aliyun.com/articles/60894&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这两个镜像[发现bazel是0.3.0，而serving是2016.9时的版本]：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;registry.cn-hangzhou.aliyuncs.com/denverdino/tensorflow-serving : TensorFlow Serving的基础镜像&lt;/li&gt;
  &lt;li&gt;registry.cn-hangzhou.aliyuncs.com/denverdino/inception-serving : 基于上述基础镜像添加Inception模型实现的服务镜像&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;docker--k8s--tf-serving自己搞&quot;&gt;docker + k8s + tf-serving[自己搞]&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/serving/serving_inception&quot;&gt;https://www.tensorflow.org/serving/serving_inception&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;dockertf-serving&quot;&gt;docker+tf-serving&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.tensorflow.org/serving/docker&quot;&gt;https://www.tensorflow.org/serving/docker&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;创建docker镜像&quot;&gt;创建docker镜像&lt;/h4&gt;

&lt;p&gt;首先把这个搞下来&lt;a href=&quot;https://github.com/tensorflow/serving/blob/master/tensorflow_serving/tools/docker/Dockerfile.devel&quot;&gt;https://github.com/tensorflow/serving/blob/master/tensorflow_serving/tools/docker/Dockerfile.devel&lt;/a&gt;：&lt;/p&gt;

&lt;p&gt;然后，把bazel的version改成0.11.0（编译1.7版本的serving需要），另外，我还补充了automake/libtool【&lt;strong&gt;当然，目前可以work的是1.4版本的serving，要把bazel版本改成0.5.4&lt;/strong&gt;】：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;FROM ubuntu:16.04

MAINTAINER Jeremiah Harmsen &amp;lt;jeremiah@google.com&amp;gt;

RUN apt-get update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        build-essential &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        curl &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        git &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        libfreetype6-dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        libpng12-dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        libzmq3-dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        mlocate &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        pkg-config &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        python-dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        python-numpy &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        python-pip &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        software-properties-common &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        swig &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        zip &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        zlib1g-dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        libcurl3-dev &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        openjdk-8-jdk&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        openjdk-8-jre-headless &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        wget &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        automake &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        libtool &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    apt-get clean &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Set up grpc&lt;/span&gt;

RUN pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;mock grpcio

&lt;span class=&quot;c&quot;&gt;# Set up Bazel.&lt;/span&gt;

ENV BAZELRC /root/.bazelrc
&lt;span class=&quot;c&quot;&gt;# Install the most recent bazel release.&lt;/span&gt;
ENV BAZEL_VERSION 0.11.0
WORKDIR /
RUN &lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; /bazel &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /bazel &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    curl &lt;span class=&quot;nt&quot;&gt;-fSsL&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-O&lt;/span&gt; https://github.com/bazelbuild/bazel/releases/download/&lt;span class=&quot;nv&quot;&gt;$BAZEL_VERSION&lt;/span&gt;/bazel-&lt;span class=&quot;nv&quot;&gt;$BAZEL_VERSION&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-installer-linux-x86_64&lt;/span&gt;.sh &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    curl &lt;span class=&quot;nt&quot;&gt;-fSsL&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; /bazel/LICENSE.txt https://raw.githubusercontent.com/bazelbuild/bazel/master/LICENSE &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x bazel-&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.sh &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    ./bazel-&lt;span class=&quot;nv&quot;&gt;$BAZEL_VERSION&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-installer-linux-x86_64&lt;/span&gt;.sh &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; / &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; /bazel/bazel-&lt;span class=&quot;nv&quot;&gt;$BAZEL_VERSION&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-installer-linux-x86_64&lt;/span&gt;.sh

CMD &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;/bin/bash&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker build &lt;span class=&quot;nt&quot;&gt;--pull&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$USER&lt;/span&gt;/tensorflow-serving-devel &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; Dockerfile.tf-serving &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动docker并进入&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run &lt;span class=&quot;nt&quot;&gt;-idt&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /home/disk1/tf_space:/home/work/data &lt;span class=&quot;nv&quot;&gt;$USER&lt;/span&gt;/tensorflow-serving-devel /bin/bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;编译server&quot;&gt;编译server&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@cb189256755# &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /home/work/data/serving/serving_1.4/
root@0cb189256755:/home/work/data/serving/serving_1.4# git clone &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; r1.4 &lt;span class=&quot;nt&quot;&gt;--recurse-submodules&lt;/span&gt; https://github.com/tensorflow/serving
root@0cb189256755:/home/work/data/serving/serving_1.4# &lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;serving/tensorflow
root@0cb189256755:/home/work/data/serving/serving_1.4/serving/tensorflow# ./configure
root@0cb189256755:/home/work/data/serving/serving_1.4/serving# &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ..
root@0cb189256755:/home/work/data/serving/serving_1.4/serving# bazel build &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; opt tensorflow_serving/model_servers:tensorflow_model_server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样，生成的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server&lt;/code&gt;就是我们想要的啦&lt;/p&gt;

&lt;h4 id=&quot;编译examples&quot;&gt;编译examples&lt;/h4&gt;

&lt;p&gt;编译一下tf-serving的example:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@0cb189256755:/home/work/data/serving/serving_1.4/serving# bazel build &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; opt tensorflow_serving/example/...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;mnist-example&quot;&gt;mnist example&lt;/h5&gt;

&lt;p&gt;训练并export一个模型&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@0cb189256755:/home/work/data/serving/serving_1.4/serving# rm /tmp/mnist_model/ -rf
root@0cb189256755:/home/work/data/serving/serving_1.4/serving# bazel-bin/tensorflow_serving/example/mnist_saved_model /tmp/mnist_model
Training model...
Extracting /tmp/train-images-idx3-ubyte.gz
Extracting /tmp/train-labels-idx1-ubyte.gz
Extracting /tmp/t10k-images-idx3-ubyte.gz
Extracting /tmp/t10k-labels-idx1-ubyte.gz
2018-06-03 10:29:58.144101: I external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
training accuracy 0.9092
Done training!
Exporting trained model to /tmp/mnist_model/1
Done exporting!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/mnist_model&lt;/code&gt;下面有一个文件夹1，就代表version，下面有两部分：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;saved_model.pb： 序列化后的 tensorflow::SavedModel. It includes one or more graph definitions of the model, as well as metadata of the model such as signatures.&lt;/li&gt;
  &lt;li&gt;variables: files that hold the serialized variables of the graphs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;启动server&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@0cb189256755:/home/work/data/serving/serving_1.4/serving# &lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;9000 &lt;span class=&quot;nt&quot;&gt;--model_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;mnist &lt;span class=&quot;nt&quot;&gt;--model_base_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/mnist_model/ &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动client&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@0cb189256755:/home/work/data/serving/serving_1.4/serving# bazel-bin/tensorflow_serving/example/mnist_client &lt;span class=&quot;nt&quot;&gt;--num_tests&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000 &lt;span class=&quot;nt&quot;&gt;--server&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;localhost:9000
Extracting /tmp/train-images-idx3-ubyte.gz
Extracting /tmp/train-labels-idx1-ubyte.gz
Extracting /tmp/t10k-images-idx3-ubyte.gz
Extracting /tmp/t10k-labels-idx1-ubyte.gz
........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Inference error rate: 10.4%
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;inception-example&quot;&gt;inception example&lt;/h5&gt;

&lt;p&gt;export一个训练好的模型并存储到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/inception-export&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@0cb189256755:/home/work/data/serving/serving_1.4/serving# curl &lt;span class=&quot;nt&quot;&gt;-O&lt;/span&gt; http://download.tensorflow.org/models/image/imagenet/inception-v3-2016-03-01.tar.gz
root@0cb189256755:/home/work/data/serving/serving_1.4/serving# &lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;xzf inception-v3-2016-03-01.tar.gz
root@0cb189256755:/home/work/data/serving/serving_1.4/serving# &lt;span class=&quot;nb&quot;&gt;ls &lt;/span&gt;inception-v3
README.txt  checkpoint  model.ckpt-157585
root@0cb189256755:/home/work/data/serving/serving_1.4/serving# bazel-bin/tensorflow_serving/example/inception_saved_model &lt;span class=&quot;nt&quot;&gt;--checkpoint_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;inception-v3 &lt;span class=&quot;nt&quot;&gt;--output_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/inception-export
Successfully loaded model from inception-v3/model.ckpt-157585 at &lt;span class=&quot;nv&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;157585.
Successfully exported model to /tmp/inception-export
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动server&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@0cb189256755:/home/work/data/serving/serving_1.4/serving# &lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;9000 &lt;span class=&quot;nt&quot;&gt;--model_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;inception &lt;span class=&quot;nt&quot;&gt;--model_base_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/inception-export &amp;amp;&amp;gt; inception_log &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动client：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@0cb189256755:/home/work/data/serving/serving_1.4/serving# bazel-bin/tensorflow_serving/example/inception_client &lt;span class=&quot;nt&quot;&gt;--server&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;localhost:9000 &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;persian_cat_image.jpg   
outputs &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  key: &lt;span class=&quot;s2&quot;&gt;&quot;classes&quot;&lt;/span&gt;
  value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    dtype: DT_STRING
    tensor_shape &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      dim &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        size: 1
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      dim &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        size: 5
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    string_val: &lt;span class=&quot;s2&quot;&gt;&quot;Persian cat&quot;&lt;/span&gt;
    string_val: &lt;span class=&quot;s2&quot;&gt;&quot;lynx, catamount&quot;&lt;/span&gt;
    string_val: &lt;span class=&quot;s2&quot;&gt;&quot;Egyptian cat&quot;&lt;/span&gt;
    string_val: &lt;span class=&quot;s2&quot;&gt;&quot;tabby, tabby cat&quot;&lt;/span&gt;
    string_val: &lt;span class=&quot;s2&quot;&gt;&quot;Angora, Angora rabbit&quot;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
outputs &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  key: &lt;span class=&quot;s2&quot;&gt;&quot;scores&quot;&lt;/span&gt;
  value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    dtype: DT_FLOAT
    tensor_shape &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      dim &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        size: 1
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      dim &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        size: 5
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    float_val: 9.48267459869
    float_val: 3.10385608673
    float_val: 2.89405298233
    float_val: 2.83001184464
    float_val: 2.81639647484
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;大功告成咯~~&lt;/p&gt;

&lt;h4 id=&quot;commit-container&quot;&gt;commit container&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker commit 0cb189256755 root/tf_serving_1.4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后就可以看到&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker images
REPOSITORY                                                        TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
root/tf_serving_1.4                                               latest              e6eae34c8754        41 seconds ago      3.014 GB
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;在2个容器间进行访问&quot;&gt;在2个容器间进行访问&lt;/h4&gt;

&lt;p&gt;然后启动两个容器：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;## 谨慎使用，删除现有所有容器&lt;/span&gt;
docker ps &lt;span class=&quot;nt&quot;&gt;-aq&lt;/span&gt;| xargs docker stop | xargs docker &lt;span class=&quot;nb&quot;&gt;rm

&lt;/span&gt;docker run &lt;span class=&quot;nt&quot;&gt;-dti&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 9001:9000 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt; inception-serving &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        root/tf_serving_1.4 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        /bin/bash


docker run &lt;span class=&quot;nt&quot;&gt;-dti&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt; client &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;--link&lt;/span&gt; inception-serving:serving &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /home/disk1/tf_space:/home/work/data &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        root/tf_serving_1.4 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
        /bin/bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动客户端，并定义容器link，允许在容器内部通过“serving”别名来访问“inception-serving”容器&lt;/p&gt;

&lt;p&gt;此时，&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; docker ps   
CONTAINER ID        IMAGE                           COMMAND             CREATED             STATUS              PORTS                    NAMES
910de959f1ae        root/tf_serving_1.4             &lt;span class=&quot;s2&quot;&gt;&quot;/bin/bash&quot;&lt;/span&gt;         41 seconds ago      Up 40 seconds                                client
3e3134158f9c        root/tf_serving_1.4             &lt;span class=&quot;s2&quot;&gt;&quot;/bin/bash&quot;&lt;/span&gt;         41 seconds ago      Up 40 seconds       0.0.0.0:9001-&amp;gt;9000/tcp   inception-serving
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;进入server &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3e3134158f9c&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@3e3134158f9c:~# &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /home/work/data/serving/serving_1.4/serving/
root@3e3134158f9c:/home/work/data/serving/serving_1.4/serving# &lt;span class=&quot;nb&quot;&gt;nohup &lt;/span&gt;bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;9000 &lt;span class=&quot;nt&quot;&gt;--model_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;inception &lt;span class=&quot;nt&quot;&gt;--model_base_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/inception-export &amp;amp;&amp;gt; inception_log &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;进入client &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;910de959f1ae&lt;/code&gt;，注意，这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--server=serving:9000&lt;/code&gt;，serving就是刚刚–link取的别名~&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@910de959f1ae:~# &lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /home/work/data/serving/serving_1.4/serving/
root@910de959f1ae:/home/work/data/serving/serving_1.4/serving# bazel-bin/tensorflow_serving/example/inception_client &lt;span class=&quot;nt&quot;&gt;--server&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;serving:9000 &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;persian_cat_image.jpg 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;k8s&quot;&gt;k8s&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>crf v.s. softmax</title>
   <link href="http://daiwk.github.io/posts/ml-crf-softmax.html"/>
   <updated>2018-05-30T00:00:00+00:00</updated>
   <id>/posts/ml-crf-softmax</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#softmax%E4%B8%8Ecrf%E5%AF%B9%E6%AF%94&quot;&gt;softmax与crf对比&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#optimal-hyperparameters-for-deep-lstm-networks-for-sequence-labeling-tasks&quot;&gt;Optimal Hyperparameters for Deep LSTM-Networks for Sequence Labeling Tasks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;softmax与crf对比&quot;&gt;softmax与crf对比&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jiqizhixin.com/articles/2018-05-23-3&quot;&gt;https://www.jiqizhixin.com/articles/2018-05-23-3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;摘一句重点：&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;逐帧 softmax 和 CRF 的根本不同：前者将序列标注看成是 n 个 k 分类问题，后者将序列标注看成是 1 个 k^n 分类问题。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/bobobe/article/details/80489303&quot;&gt;https://blog.csdn.net/bobobe/article/details/80489303&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;optimal-hyperparameters-for-deep-lstm-networks-for-sequence-labeling-tasks&quot;&gt;Optimal Hyperparameters for Deep LSTM-Networks for Sequence Labeling Tasks&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1707.06799&quot;&gt;Optimal Hyperparameters for Deep LSTM-Networks for Sequence Labeling Tasks&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>knowledge distill</title>
   <link href="http://daiwk.github.io/posts/dl-knowledge-distill.html"/>
   <updated>2018-05-22T00:00:00+00:00</updated>
   <id>/posts/dl-knowledge-distill</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#introduction&quot;&gt;introduction&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#distillation&quot;&gt;distillation&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#matching-logits是distillation的一个特例&quot;&gt;Matching logits是distillation的一个特例&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#在mnist上的初步实验&quot;&gt;在MNIST上的初步实验&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#在speech-recognition上的实验&quot;&gt;在speech recognition上的实验&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#training-ensembles-of-specialists-on-very-big-datasets&quot;&gt;training ensembles of specialists on very big datasets&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#jft-dataset&quot;&gt;JFT dataset&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#specialist-models&quot;&gt;specialist models&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#assigning-classes-to-specialists&quot;&gt;assigning classes to specialists&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#performing-inference-with-ensumbles-of-specialists&quot;&gt;performing inference with ensumbles of specialists&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#结果&quot;&gt;结果&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#soft-targets-as-regularizers&quot;&gt;soft targets as regularizers&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#使用soft-targets以阻止specialists过拟合&quot;&gt;使用soft targets以阻止specialists过拟合&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#与mixtures-of-experts的关系&quot;&gt;与mixtures of experts的关系&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/50519680&quot;&gt;https://www.zhihu.com/question/50519680&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原始paper：&lt;a href=&quot;https://arxiv.org/abs/1503.02531&quot;&gt;Distilling the Knowledge in a Neural Network&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/zhongshaoyy/article/details/53582048&quot;&gt;蒸馏神经网络(Distill the Knowledge in a Neural Network)&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;introduction&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;核心思想&lt;/strong&gt;：一个复杂的网络结构模型是若干个单独模型组成的集合，或者是一些很强的约束条件下（比如dropout率很高）训练得到的一个很大的网络模型。一旦复杂网络模型训练完成，我们便可以用另一种训练方法：“蒸馏”，把复杂模型中的knowledge transfer到一个更易于部署的小模型中。&lt;/p&gt;

&lt;p&gt;“蒸馏”的难点在于如何&lt;strong&gt;缩减网络结构&lt;/strong&gt;但是把网络中的知识保留下来。知识就从输入向量到输出向量的一个learned mapping。做复杂网络的训练时，目标是将&lt;strong&gt;正确答案的概率最大化&lt;/strong&gt;，但这引入了一个副作用：这种网络为&lt;strong&gt;所有错误答案分配了概率&lt;/strong&gt;，即使这些概率非常小。而这些错误答案间也有的比别的大，例如一辆宝马车，错误答案货车的概率就比错误答案胡萝卜的概率要大得多，这也是模型泛化能力的体现。&lt;/p&gt;

&lt;p&gt;将复杂模型转化为小模型时需要注意保留模型的泛化能力：&lt;/p&gt;

&lt;p&gt;一种方法是利用由复杂模型产生的&lt;strong&gt;分类概率&lt;/strong&gt;作为&lt;strong&gt;软目标&lt;/strong&gt;来训练小模型。在transfer阶段，我们可以用同样的训练集或者是单独的transfer set。当复杂模型是由简单模型复合而成时，我们可以用各自的概率分布的算术平均或者几何平均作为软目标。当&lt;strong&gt;软目标的熵值较高&lt;/strong&gt;时，相对硬目标，它每次训练&lt;strong&gt;可以提供更多的信息和更小的梯度方差&lt;/strong&gt;，因此&lt;strong&gt;小模型&lt;/strong&gt;可以&lt;strong&gt;用更少的数据&lt;/strong&gt;和&lt;strong&gt;更高的学习率&lt;/strong&gt;进行训练。&lt;/p&gt;

&lt;p&gt;像MNIST这种任务，复杂模型可以给出很完美的结果，大部分信息分布在小概率的软目标中。比如一张2的图片被认为是3的概率为0.000001，被认为是7的概率是0.000000001，但对于cross entropy的损失函数的值来讲，就没什么区分性了，因为他们都接近0。&lt;/p&gt;

&lt;p&gt;Caruana用logits（&lt;strong&gt;softmax层的输入&lt;/strong&gt;）而不是softmax层的输出作为“软目标”。他们目标是是的&lt;strong&gt;复杂模型的logits和小模型的logits的平方差最小&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;distillation：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一步，&lt;strong&gt;提升&lt;/strong&gt;final softmax中的调节参数&lt;strong&gt;T&lt;/strong&gt;，使得复杂模型&lt;strong&gt;产生合适的『软目标』&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;第二步，采用&lt;strong&gt;同样的T&lt;/strong&gt;来&lt;strong&gt;训练小模型&lt;/strong&gt;，让它去&lt;strong&gt;匹配『软目标』&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;第三步，训练完成之后，&lt;strong&gt;T变回1&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;后面发现，匹配复杂模型的logits其实就是distillation的一个special case。&lt;/p&gt;

&lt;p&gt;transfer set可以由无标签数据组成(&lt;a href=&quot;https://www.cs.cornell.edu/~caruana/compression.kdd06.pdf&quot;&gt;Model Compression&lt;/a&gt;)，也可以用原训练集。我们发现使用&lt;strong&gt;原训练集&lt;/strong&gt;效果很好，特别是我们在&lt;strong&gt;目标函数&lt;/strong&gt;中加了一项目之后，这一项会encourage小模型&lt;strong&gt;预测真实目标&lt;/strong&gt;，同时&lt;strong&gt;尽量匹配『软目标』&lt;/strong&gt;。要注意的是，小模型并不能完全无误的匹配“软目标”，而正确结果的犯错方向(erring in the direction of the correct answer)是有帮助的。&lt;/p&gt;

&lt;h2 id=&quot;distillation&quot;&gt;distillation&lt;/h2&gt;

&lt;p&gt;softmax层公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q_i=\frac{\exp(z_i/T)}{\sum _j \exp(z_j/T)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;：logit，也就是softmax层的输入&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q_i\)&lt;/code&gt;：softmax层算出的分类概率&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T\)&lt;/code&gt;：temperature，就是调节参数，一般设为1。&lt;strong&gt;T越大，分类的概率分布越『软』&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;复制粘贴一下上面提到的：&lt;/p&gt;

&lt;p&gt;distillation：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一步，&lt;strong&gt;提升&lt;/strong&gt;final softmax中的调节参数&lt;strong&gt;T&lt;/strong&gt;，使得复杂模型&lt;strong&gt;产生合适的『软目标』&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;第二步，采用&lt;strong&gt;同样的T&lt;/strong&gt;来&lt;strong&gt;训练小模型&lt;/strong&gt;，让它去&lt;strong&gt;匹配『软目标』&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;第三步，训练完成之后，&lt;strong&gt;T变回1&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;当transfer set中部分或者所有数据都有标签时，这种方式可以通过同时训练模型使得模型得到正确的标签来大大提升效果。&lt;/p&gt;

&lt;p&gt;一种实现方法是用正确标签来修正『软目标』，但一种更好的方法是：对两个目标函数进行加权平均。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一个目标函数是两个模型的&lt;strong&gt;『软目标』&lt;/strong&gt;的交叉熵，这个交叉熵用开始的那个&lt;strong&gt;比较大的T&lt;/strong&gt;来计算。&lt;/li&gt;
  &lt;li&gt;第二个目标函数是&lt;strong&gt;正确标签&lt;/strong&gt;的交叉熵，这个交叉熵用&lt;strong&gt;小模型softmax层的logits&lt;/strong&gt;来计算且&lt;strong&gt;T等于1&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;发现当&lt;strong&gt;第二个目标函数权重较低&lt;/strong&gt;时可以得到最好的结果。&lt;/p&gt;

&lt;p&gt;因为梯度的量级(magnitude)被『软目标』缩放了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1/T^2\)&lt;/code&gt;(下面有讲)，所以同时使用hard和soft target的时候，需要乘以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T^2\)&lt;/code&gt;，这样可以保证即使T在实验的过程中改了，hard和soft targets的贡献程度相对不变。&lt;/p&gt;

&lt;h3 id=&quot;matching-logits是distillation的一个特例&quot;&gt;Matching logits是distillation的一个特例&lt;/h3&gt;

&lt;p&gt;那么，我们先看一下交叉熵的求导(参考&lt;a href=&quot;https://blog.csdn.net/qian99/article/details/78046329&quot;&gt;简单易懂的softmax交叉熵损失函数求导&lt;/a&gt;)，假设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;是logit，经过softmax后得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_i\)&lt;/code&gt;，label是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_i\)&lt;/code&gt;，那么，由于n个类，只有一个类是1，其他都是0，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum_j y_j = 0\)&lt;/code&gt;，所以：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial C}{\partial z_i}=\frac{\partial C}{\partial a_i}\frac{\partial a_i}{\partial z_i}=...=a_i\sum_j y_j-y_i=a_i-y_i
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后看回这个distill模型&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;大模型的logit是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v_i\)&lt;/code&gt;，算出来的soft target的probability是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_i\)&lt;/code&gt;，&lt;/li&gt;
  &lt;li&gt;小模型的logit是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;，算出来的soft target的probability是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q_i\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;交叉熵损失函数对小模型的logit，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;进行求导（把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_i\)&lt;/code&gt;看成一个常量），得到的梯度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(dC/dz_i\)&lt;/code&gt;如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial C}{\partial z_i}=\frac{1}{T}(q_i-p_i)=\frac{1}{T}(\frac{e^{z_i/T}}{\sum_je^{z_j/T}}-\frac{e^{v_i/T}}{\sum_je^{v_j/T}})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后，如果temperature T比logits的量级（magnitude）要大得多，那么，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i/T\)&lt;/code&gt;趋向于0（是一个很小的数），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i&amp;lt;0\)&lt;/code&gt;的时候是从左边趋向于0，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\&amp;gt;0\)&lt;/code&gt;的时候是从右边趋向于0，所以，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e^{z_i/T}\approx e^0+z_i/T\)&lt;/code&gt;。因此，可以如下方式近似：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial C}{\partial z_i}\approx \frac{1}{T}(\frac{1+z_i/T}{N+\sum _jz_j/T}-\frac{1+v_i/T}{N+\sum_jv_j/T})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;假设对于每一个transfer case，都有logits的均值为0，那么就有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum_jz_j=\sum_jv_j=0\)&lt;/code&gt;，所以上式可以简化为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial C}{\partial z_i}\approx \frac{1}{T}(\frac{1+z_i/T}{N}-\frac{1+v_i/T}{N})=\frac{1}{NT^2}(z_i-v_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，如果temperature T很高，如果对于每一个transfer case，都有logits的均值为0，那么distillation就等价于最小化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1/2(z_i-v_i)^2\)&lt;/code&gt;，也就是Caruana提出的使得复杂模型的logits和小模型的logits的平方差最小。&lt;/p&gt;

&lt;p&gt;而对于比较低的temperature T来讲，distillation对那些比平均值negative很多的logits的matching，会给予更少的关注。因为这样的logits在大模型的损失函数中几乎是unconstrained，也就是noisy的，所以这是potentially advantageous的。另一方面，这些很negative的logits可能可以传递大模型学到的知识中的很有用的信息。上面的这些效果哪个起了决定性作用其实是一个empirical(经验主义) question。当distilled model比大模型小太多，以至于无法捕捉到大模型的所有知识时，intermediate（中间的）的temperature效果最好，强烈建议把large negative logits直接忽略掉是很有用的。&lt;/p&gt;

&lt;h2 id=&quot;在mnist上的初步实验&quot;&gt;在MNIST上的初步实验&lt;/h2&gt;

&lt;h2 id=&quot;在speech-recognition上的实验&quot;&gt;在speech recognition上的实验&lt;/h2&gt;

&lt;h2 id=&quot;training-ensembles-of-specialists-on-very-big-datasets&quot;&gt;training ensembles of specialists on very big datasets&lt;/h2&gt;

&lt;h3 id=&quot;jft-dataset&quot;&gt;JFT dataset&lt;/h3&gt;

&lt;h3 id=&quot;specialist-models&quot;&gt;specialist models&lt;/h3&gt;

&lt;h3 id=&quot;assigning-classes-to-specialists&quot;&gt;assigning classes to specialists&lt;/h3&gt;

&lt;h3 id=&quot;performing-inference-with-ensumbles-of-specialists&quot;&gt;performing inference with ensumbles of specialists&lt;/h3&gt;

&lt;h3 id=&quot;结果&quot;&gt;结果&lt;/h3&gt;

&lt;h2 id=&quot;soft-targets-as-regularizers&quot;&gt;soft targets as regularizers&lt;/h2&gt;

&lt;h3 id=&quot;使用soft-targets以阻止specialists过拟合&quot;&gt;使用soft targets以阻止specialists过拟合&lt;/h3&gt;

&lt;h2 id=&quot;与mixtures-of-experts的关系&quot;&gt;与mixtures of experts的关系&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>rl summary</title>
   <link href="http://daiwk.github.io/posts/rl-summary.html"/>
   <updated>2018-05-14T00:00:00+00:00</updated>
   <id>/posts/rl-summary</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#概览&quot;&gt;概览&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#基于值函数的强化学习方法&quot;&gt;基于值函数的强化学习方法&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#策略搜索方法&quot;&gt;策略搜索方法&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#自己的小结&quot;&gt;自己的小结&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;概览&quot;&gt;概览&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rl-overview.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;基于值函数的强化学习方法&quot;&gt;基于值函数的强化学习方法&lt;/h2&gt;

&lt;p&gt;基于值函数的方法是间接方法，即通过学习值函数（value function）或者动作值函数（action-value function）来得到policy。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rl-overview-value-function.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;策略搜索方法&quot;&gt;策略搜索方法&lt;/h2&gt;

&lt;p&gt;直接对policy进行建模和学习&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rl-overview-policy-search.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;自己的小结&quot;&gt;自己的小结&lt;/h2&gt;

&lt;p&gt;某个时候整理了个ppt：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rl-my-understanding1.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rl-my-understanding2.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rl-my-understanding3.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

</content>
 </entry>
 
 <entry>
   <title>rl tutorial-pytorch</title>
   <link href="http://daiwk.github.io/posts/rl-pytorch-rl-tutorial.html"/>
   <updated>2018-05-05T00:00:00+00:00</updated>
   <id>/posts/rl-pytorch-rl-tutorial</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#dqn&quot;&gt;DQN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pytorch-tutorial&quot;&gt;pytorch tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;dqn&quot;&gt;DQN&lt;/h2&gt;

&lt;p&gt;原文是DeepMind发表在Nature上的文章&lt;a href=&quot;https://daiwk.github.io/assets/dqn.pdf&quot;&gt;Human-level control through deep reinforcement learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25703659&quot;&gt;DQN起源《Human-level control through deep reinforcement learning》论文笔记&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://jikaichen.com/2016/11/18/notes-on-atari/&quot;&gt;论文笔记 Human-Level control through deep reinforcement learning&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pytorch-tutorial&quot;&gt;pytorch tutorial&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html&quot;&gt;https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>mlperf</title>
   <link href="http://daiwk.github.io/posts/platform-mlperf.html"/>
   <updated>2018-05-05T00:00:00+00:00</updated>
   <id>/posts/platform-mlperf</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;代码地址：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/mlperf/reference&quot;&gt;https://github.com/mlperf/reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;目前有7个任务：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;image_classification - Resnet-50 v1 applied to Imagenet.(tensorflow)&lt;/li&gt;
  &lt;li&gt;object_detection - Mask R-CNN applied to COCO.(caffe2)&lt;/li&gt;
  &lt;li&gt;speech_recognition - DeepSpeech2 applied to Librispeech.(pytorch)&lt;/li&gt;
  &lt;li&gt;translation - Transformer applied to WMT English-German.(tensorflow)&lt;/li&gt;
  &lt;li&gt;recommendation - Neural Collaborative Filtering applied to MovieLens 20 Million (ml-20m).(pytorch)&lt;/li&gt;
  &lt;li&gt;sentiment_analysis - Seq-CNN applied to IMDB dataset.(paddle)&lt;/li&gt;
  &lt;li&gt;reinforcement - Mini-go applied to predicting pro game moves.(tensorflow)&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>分布式强化学习框架</title>
   <link href="http://daiwk.github.io/posts/rl-distributed-rl.html"/>
   <updated>2018-05-01T00:00:00+00:00</updated>
   <id>/posts/rl-distributed-rl</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-%e6%a6%82%e8%bf%b0&quot;&gt;1. 概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-a3c&quot;&gt;2. A3C&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-ppo&quot;&gt;3. PPO&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-rainbow&quot;&gt;4. rainbow&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-ape-x&quot;&gt;5. APE-X&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#51-%e7%ae%80%e4%bb%8b&quot;&gt;5.1 简介&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#52-actor%e7%9a%84%e7%ae%97%e6%b3%95&quot;&gt;5.2 Actor的算法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#53-learner%e7%9a%84%e7%ae%97%e6%b3%95&quot;&gt;5.3 Learner的算法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e4%bb%a3%e7%a0%81&quot;&gt;代码&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-rudder&quot;&gt;6. rudder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-impala&quot;&gt;7. IMPALA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#xx-%e8%87%aa%e5%b7%b1%e6%90%9e%e4%b8%80%e4%b8%8b&quot;&gt;xx. 自己搞一下&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%b6%e4%bb%96rl%e5%b9%b3%e5%8f%b0&quot;&gt;其他RL平台&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#open-spiel&quot;&gt;open spiel&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rlpyt&quot;&gt;rlpyt&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fen&quot;&gt;FEN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;1-概述&quot;&gt;1. 概述&lt;/h2&gt;

&lt;p&gt;略
大部分在ray中都已经有啦，还有openai的baselines&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/ray-project/ray/blob/master/doc/source/rllib-algorithms.rst&quot;&gt;https://github.com/ray-project/ray/blob/master/doc/source/rllib-algorithms.rst&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openai/baselines&quot;&gt;https://github.com/openai/baselines&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;2-a3c&quot;&gt;2. A3C&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/jinzhuojun/article/details/72851548&quot;&gt;https://blog.csdn.net/jinzhuojun/article/details/72851548&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ICML2016，提出了&lt;a href=&quot;https://arxiv.org/abs/1602.01783&quot;&gt;Asynchronous Methods for Deep Reinforcement Learning&lt;/a&gt;，即A3C（asynchronous advantage actor-critic）算法。&lt;/p&gt;

&lt;p&gt;A3C不仅适用于&lt;strong&gt;离散&lt;/strong&gt;也适用于&lt;strong&gt;连续动作空间&lt;/strong&gt;的控制问题。DRL领域也确实有很成功的分布式机器学习系统，比如Google的Gorila。这篇文章另辟蹊径，发明了“平民版”的DRL算法，而且效果还不比前者差。&lt;/p&gt;

&lt;p&gt;传统经验认为，online的RL算法在和DNN简单结合后会不稳定。主要原因是观察数据往往波动很大且前后sample相互关联。像Neural fitted Q iteration和TRPO方法通过&lt;strong&gt;将经验数据batch&lt;/strong&gt;，或者像DQN中通过&lt;strong&gt;experience replay memory对之随机采样&lt;/strong&gt;，这些方法有效解决了前面所说的两个问题，但是也&lt;strong&gt;将算法限定在了off-policy方法中&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;而A3C是通过创建多个agent，在多个环境实例中并行且异步的执行和学习。于是，在DNN下，解锁了一大批online/offline的RL算法（如Sarsa, AC, Q-learning）。A3C不那么依赖于GPU或大型分布式系统，可以跑在一个多核CPU上。&lt;/p&gt;

&lt;p&gt;将value function的估计作为baseline可以使得PG方法有更低的variance。这种设定下，就有了所谓的A - advantage，即，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R_t−b_t(s_t)\)&lt;/code&gt;，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A(s_t,s_t)=Q(a_t,s_t)−V(s_t)\)&lt;/code&gt;，就是advantage function的估计。&lt;/p&gt;

&lt;p&gt;将&lt;strong&gt;one-step Sarsa&lt;/strong&gt;, &lt;strong&gt;one-step Q-learning&lt;/strong&gt;, &lt;strong&gt;n-step Q-learning&lt;/strong&gt;和&lt;strong&gt;advantage AC&lt;/strong&gt;扩展至多线程异步架构。&lt;/p&gt;

&lt;p&gt;注意，该框架是具有通用性的，例如，AC是on-policy的policy搜索方法，而Q-learning是off-policy value-based方法。&lt;/p&gt;

&lt;p&gt;每个线程都有agent运行在环境的拷贝中，每一步生成一个参数的梯度，多个线程的这些梯度累加起来，一定步数后一起更新共享参数。&lt;/p&gt;

&lt;p&gt;优点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;它运行在&lt;strong&gt;单个机器的多个CPU线程&lt;/strong&gt;上，而非使用parameter server的分布式系统，这样就可以&lt;strong&gt;避免通信开销&lt;/strong&gt;和&lt;strong&gt;利用lock-free的高效数据同步方法&lt;/strong&gt;（Hogwild!方法）【2011年的&lt;a href=&quot;https://people.eecs.berkeley.edu/~brecht/papers/hogwildTR.pdf&quot;&gt;Hogwild ! : A Lock-Free Approach to Parallelizing Stochastic Gradient Descent&lt;/a&gt;是一种并行SGD方法。该方法在多个CPU时间进行并行。处理器通过共享内存来访问参数，并且这些参数不进行加锁。它为每一个cpu分配不重叠的一部分参数（分配互斥），每个cpu只更新其负责的参数。该方法只适合处理数据特征是稀疏的。该方法几乎可以达到一个最优的收敛速度，因为cpu之间不会进行相同信息重写。】&lt;/li&gt;
  &lt;li&gt;多个并行的actor可以有助于exploration。在不同线程上使用不同的探索策略，使得经验数据在时间上的相关性很小。这样&lt;strong&gt;不需要DQN中的experience replay&lt;/strong&gt;也可以起到&lt;strong&gt;稳定学习过程&lt;/strong&gt;的作用，意味着学习过程&lt;strong&gt;可以是on-policy的&lt;/strong&gt;，所以可以使用on-policy方法（如Sarsa），且能保证稳定。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A3C和DDPG类似，通过DNN维护了policy和value function的估计，但它&lt;strong&gt;没用deterministic policy&lt;/strong&gt;。在学习过程中&lt;strong&gt;使用n-step回报&lt;/strong&gt;来&lt;strong&gt;同时更新policy和value function&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;网络结构使用了CNN，其中一个softmax output作为policy，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(a_t|s_t;\theta)\)&lt;/code&gt;，另一个linear output为value function，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V(s_t;\theta _v)\)&lt;/code&gt;，其余layer都共享。&lt;/p&gt;

&lt;p&gt;作者还发现一个古老的技巧，即&lt;strong&gt;将policy的entropy加到目标函数&lt;/strong&gt;可以避免收敛到次优确定性解。直观上，加上该正则项后&lt;strong&gt;目标函数更鼓励找entropy大的&lt;/strong&gt;，即形状“扁平”的分布，这样就不容易在训练过程中聚集到某一个动作上去。&lt;/p&gt;

&lt;p&gt;在优化方法上，作者使用了基于RPMProp的一种变体。&lt;/p&gt;

&lt;p&gt;ray的a3c代码：&lt;a href=&quot;https://github.com/ray-project/ray/tree/master/python/ray/rllib/agents/a3c&quot;&gt;https://github.com/ray-project/ray/tree/master/python/ray/rllib/agents/a3c&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;3-ppo&quot;&gt;3. PPO&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650729155&amp;amp;idx=4&amp;amp;sn=476365254e360e88a9d0d4aea9e86875&amp;amp;chksm=871b2ebdb06ca7abbcc0c513806844bd30f686fe637c1f2f52484516560e2f57d73a28a510cd&amp;amp;scene=0#rd&quot;&gt;业界 | OpenAI 提出强化学习近端策略优化，可替代策略梯度法&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://openai-public.s3-us-west-2.amazonaws.com/blog/2017-07/ppo/ppo-arxiv.pdf&quot;&gt;Proximal Policy Optimization Algorithms&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;openai的blog：&lt;a href=&quot;https://blog.openai.com/openai-baselines-ppo/&quot;&gt;https://blog.openai.com/openai-baselines-ppo/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;策略梯度法（Policy gradient methods）是近来使用深度神经网络进行控制的突破基础，不论是视频游戏还是   3D   移动或者围棋控制等，它们都基于策略梯度法。但但是通过策略梯度法获得优秀的结果是十分困难的，policy gradient有以下几点不足：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;它对步长大小的选择非常敏感。如果迭代步长太小，那么训练进展会非常慢，但如果迭代步长太大，那么信号将受到噪声的强烈干扰，因此我们会看到性能会急剧降低。&lt;/li&gt;
  &lt;li&gt;策略梯度法有非常低的样本效率，它需要数百万（或数十亿）的时间步骤来学习一个简单的任务。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;研究人员希望能通过约束或其他优化策略更新（policy update）大小的方法来消除这些缺陷，如 TRPO 和 ACER 等方法。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ACER（&lt;a href=&quot;https://arxiv.org/abs/1611.01224&quot;&gt;Sample Efficient Actor-Critic with Experience Replay&lt;/a&gt;）方法要比PPO方法复杂得多，需要额外添加代码来&lt;strong&gt;修正off-policy&lt;/strong&gt;和&lt;strong&gt;重构缓冲器&lt;/strong&gt;，但它在Atari基准上仅仅比PPO好一点点&lt;/li&gt;
  &lt;li&gt;TRPO（&lt;a href=&quot;https://arxiv.org/abs/1502.05477&quot;&gt;Trust region policy optimization&lt;/a&gt;，置信域策略优化）虽然对&lt;strong&gt;连续&lt;/strong&gt;控制任务非常有用，但它并不容易与那些在&lt;strong&gt;策略&lt;/strong&gt;和&lt;strong&gt;值函数或辅助损失函数&lt;/strong&gt;（auxiliary losses）间&lt;strong&gt;共享参数&lt;/strong&gt;的算法兼容，即那些用于解决 Atari 和其他视觉输入很重要领域的算法。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PPO 算法很好地权衡了实现简单性、样本复杂度和调参难度，它尝试在每一迭代步计算一个更新以最小化成本函数，在计算梯度时还需要确保与先前策略有相对较小的偏差。&lt;/p&gt;

&lt;p&gt;之前介绍过一个 PPO 变体（在NIPS2016上有一个talk &lt;a href=&quot;https://channel9.msdn.com/Events/Neural-Information-Processing-Systems-Conference/Neural-Information-Processing-Systems-Conference-NIPS-2016/Deep-Reinforcement-Learning-Through-Policy-Optimization&quot;&gt;Deep Reinforcement Learning Through Policy Optimization&lt;/a&gt;），即使用一个&lt;strong&gt;适应性 KL 惩罚项&lt;/strong&gt;来控制&lt;strong&gt;每一次迭代中的策略改变&lt;/strong&gt;。这次的目标函数实现了一种&lt;strong&gt;与随机梯度下降相匹配的置信域（Trust Region）更新&lt;/strong&gt;方法，它同时还&lt;strong&gt;移除了 KL 惩罚项&lt;/strong&gt;以简化算法和构建适应性更新。在测试中该算法在连续控制任务中取得了最好的性能，并且尽管实现起来非常简单，但它同样在 Atari 上获得了与 ACER 算法相匹配的性能。&lt;/p&gt;

&lt;h2 id=&quot;4-rainbow&quot;&gt;4. rainbow&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1710.02298&quot;&gt;Rainbow: Combining improvements in deep reinforcement learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jiqizhixin.com/articles/2017-10-10-2&quot;&gt;DeepMind提出Rainbow：整合DQN算法中的六种变体&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Double DQN（DDQN；van Hasselt、Guez&amp;amp;Silver；2016）通过解耦选择（decoupling selection）和引导行动评估解决了Q-learning过度估计偏差的问题。&lt;/li&gt;
  &lt;li&gt;Prioritized experience replay（Schaul 等人；2015）通过重放（replay）学习到更频繁的转换，提升了数据效率。&lt;/li&gt;
  &lt;li&gt;dueling 网络架构（Wang 等人；2016）可以通过分别表示状态值和动作奖励来概括各种动作。&lt;/li&gt;
  &lt;li&gt;从多步骤引导程序目标中学习（Sutton；1988；Sutton &amp;amp; Barto 1998）如 A3C（Mnih 等人；2016）中使用偏差-方差权衡，而帮助将最新观察到的奖励快速传播到旧状态中。&lt;/li&gt;
  &lt;li&gt;分布式 Q-learning（Bellemare、Dabney &amp;amp; Munos；2017）学习了折扣回报（discounted returns）的分类分布（代替了估计平均值）。&lt;/li&gt;
  &lt;li&gt;Noisy DQN（Fortunato 等人；2017）使用随机网络层进行exploration。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以上这些算法各自都可以提升 DQN 性能的某个方面，因为它们都着力于解决不同的问题，而且都构建在同一个框架上，所以能够被我们整合起来。&lt;/p&gt;

&lt;h2 id=&quot;5-ape-x&quot;&gt;5. APE-X&lt;/h2&gt;

&lt;h3 id=&quot;51-简介&quot;&gt;5.1 简介&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/36375292&quot;&gt;最前沿：当我们以为Rainbow就是Atari游戏的巅峰时，Ape-X出来把Rainbow秒成了渣！&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://openreview.net/pdf?id=H1Dy---0Z&quot;&gt;Distributed Prioritized Experience Replay&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;只使用一个learner和一个Replay buffer，但是分布式的使用了多个Actor来生成数据，paper中实验使用了360个Actor（一个Actor一个CPU）。DeepMind产生专门做这个的想法主要是从Rainbow的研究中发现：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Prioritization was found to be the most important ingredient contributing
to the agent’s performance.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Replay的优先级对于性能影响是最大的&lt;/strong&gt;，而之前的Prioritised Replay Buffer只用单一Actor来采集数据，效率偏低。那么这篇文章中，加上分布式，并且&lt;strong&gt;让每一个Actor都不完全一样&lt;/strong&gt;，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon -greedy\)&lt;/code&gt;采样时的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon \)&lt;/code&gt;不一样。这样可以更好的做explore，并且更全面的寻找优先级最高的replay来训练。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;比以前方法&lt;strong&gt;大得多得多的Replay Buffer&lt;/strong&gt;，毕竟同时用几百个actor来采集数据，充分挖掘了计算资源。这个本身可以大幅度加快训练速度。&lt;/li&gt;
  &lt;li&gt;通过&lt;strong&gt;不同的Actor得到不同优先级Priority的Replay&lt;/strong&gt;，如上面所说，&lt;strong&gt;大幅度提升explore的能力，防止过拟合&lt;/strong&gt;。这是为什么Ape-X效果提升的最主要原因。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/apex-arch.png&quot; style=&quot;max-width: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;整个算法也就是训练架构上发生改变，算法实质并没有变化。同时，由于&lt;strong&gt;使用Replay Buffer是Off-Policy独有&lt;/strong&gt;，因此，这篇paper就在DQN和DDPG上进行改变验证。&lt;/p&gt;

&lt;p&gt;如上图，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;多个actor，每个有自己的环境，并且可以产生experience，并将其写入一个共享的experience replay memory，并且能计算initial priorities&lt;/li&gt;
  &lt;li&gt;一个learner，从memory中sample，然后更新memory中的experience的priorities，并更新网络参数&lt;/li&gt;
  &lt;li&gt;每个actor的网络定期地从learner获取最新的网络参数&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;52-actor的算法&quot;&gt;5.2 Actor的算法&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;procedure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(ACTOR(B, T)\)&lt;/code&gt; // 在environment instance中运行agent，并存储experiences
      &lt;ol&gt;
        &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_0\leftarrow LEARNER.PARAMETERS()\)&lt;/code&gt; // remote call以获取最新的网络参数&lt;/li&gt;
        &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_0\leftarrow ENVIRONMENT.INITIALIZE() \)&lt;/code&gt; // 从环境中获取初始状态&lt;/li&gt;
        &lt;li&gt;for t = 1 to T do
          &lt;ol&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{t-1}\leftarrow \pi \theta _{t-1}(s_{t-1})\)&lt;/code&gt; // 使用当前policy选择一个动作&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t,\gamma_t,s_t\leftarrow ENVIRONMENT.STEP(a_{t-1})\)&lt;/code&gt; // 在环境中执行这个动作&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(LOCALBUFFER.ADD((s_{t-1},a_{t-1},r_t,\gamma_t))\)&lt;/code&gt; // 将data放入local buffer中&lt;/li&gt;
            &lt;li&gt;if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(LOCALBUFFER.SIZE() \gt B\)&lt;/code&gt; then // 在一个后台线程中，定期地send data to replay
              &lt;ol&gt;
                &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau \leftarrow LOCALBUFFER.GET(B)\)&lt;/code&gt; // 获取buffered data(例如，batch of multi-step transitions)&lt;/li&gt;
                &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p \leftarrow COMPUTEPRIORITIES(\tau)\)&lt;/code&gt; // 计算experience的优先级（例如，绝对TD error）&lt;/li&gt;
                &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(REPLAY.ADD(\tau,p)\)&lt;/code&gt; // remote call以将experience加入replay memory中&lt;/li&gt;
              &lt;/ol&gt;
            &lt;/li&gt;
            &lt;li&gt;endif&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(PERIODICALLY(\theta_t\leftarrow LEARNER.PARAMETERS())\)&lt;/code&gt; // 获取最新的网络参数&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
        &lt;li&gt;endfor&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li&gt;end procedure&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;53-learner的算法&quot;&gt;5.3 Learner的算法&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;procedure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(LEARNER(T)\)&lt;/code&gt; // 使用从memory中sampled的batches来更新网络
      &lt;ol&gt;
        &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_0\leftarrow INITIALIZENETWORK()\)&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;for t = 1 to T do // 更新参数T次
          &lt;ol&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(id,\tau \leftarrow REPLAY.SAMPLE()\)&lt;/code&gt; // 在后台线程中sample一个 prioritized batch的transitions&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l_t \leftarrow COMPUTELOSS(\tau;\theta_t)\)&lt;/code&gt; // Apply learning rule，例如double Q-learning或者DDPG&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{t+1}\leftarrow UPDATEPARAMETERS(l_t;\theta_t)\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p \leftarrow COMPUTEPRIORITIES()\)&lt;/code&gt; // 计算experience的优先级（例如，绝对TD error）【和Actor一样】&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(REPLAY.SETPRIORITY(id,p)\)&lt;/code&gt; // remote call以更新优先级&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(PERIODICALLY(REPLAY.REMOVETOFIT())\)&lt;/code&gt; // 从replay memory中删掉旧的experience&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
        &lt;li&gt;endfor&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li&gt;end procedure&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;效果：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ape-x.png&quot; style=&quot;max-width: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;代码&quot;&gt;代码&lt;/h3&gt;

&lt;p&gt;ray的ape-x代码：&lt;a href=&quot;https://github.com/ray-project/ray/blob/master/python/ray/rllib/agents/dqn/apex.py&quot;&gt;https://github.com/ray-project/ray/blob/master/python/ray/rllib/agents/dqn/apex.py&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;6-rudder&quot;&gt;6. rudder&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jiqizhixin.com/articles/2018-06-22-3&quot;&gt;比TD、MC、MCTS指数级快，性能超越A3C、DDQN等模型，这篇RL算法论文在Reddit上火了&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在强化学习中，&lt;strong&gt;延迟奖励&lt;/strong&gt;的存在会严重影响性能，主要表现在随着延迟步数的增加，&lt;strong&gt;对时间差分（TD）估计偏差的纠正时间的指数级增长&lt;/strong&gt;，和&lt;strong&gt;蒙特卡洛（MC）估计方差的指数级增长&lt;/strong&gt;。针对这一问题，来自奥地利约翰开普勒林茨大学 LIT AI Lab 的研究者提出了一种&lt;strong&gt;基于返回值分解&lt;/strong&gt;的新方法 RUDDER。实验表明，RUDDER 的速度是 TD、MC 以及 MC 树搜索（MCTS）的指数级，并在特定 Atari 游戏的训练中很快超越 rainbow、A3C、DDQN 等多种著名强化学习模型的性能。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1806.07857&quot;&gt;RUDDER: Return Decomposition for Delayed Rewards&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;源码：&lt;a href=&quot;https://github.com/ml-jku/baselines-rudder&quot;&gt;https://github.com/ml-jku/baselines-rudder&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;7-impala&quot;&gt;7. IMPALA&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737603&amp;amp;idx=4&amp;amp;sn=216d3980555ab9e6e8365b6e2f83cad4&amp;amp;chksm=871acfbdb06d46abe8c47f0cb2b7190e765d66fb0104e0beeafd1db156b2a36ac3698409b46d&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0502Z61wBOXN5SEc1P4P7f2w&amp;amp;pass_ticket=csFmp%2BqPqpbOEtBCr9byDm0vHyp83ccxf21EyZaHyV%2BoFQOLINXIlgzuTkVvCg24#rd&quot;&gt;前沿 | DeepMind提出新型架构IMPALA：帮助实现单智能体的多任务强化学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;IMPALA 受流行的 A3C 架构的启发，&lt;strong&gt;A3C&lt;/strong&gt;架构使用&lt;strong&gt;多个分布式actor&lt;/strong&gt;来学习智能体的参数。在此类模型中，每个actor使用策略参数在环境中动作。&lt;strong&gt;actor周期性地暂停探索&lt;/strong&gt;，和&lt;strong&gt;中央参数服务器共享它们计算出的梯度&lt;/strong&gt;，用于&lt;strong&gt;梯度更新&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;IMPALA的&lt;strong&gt;actor不用于计算梯度，而是用于收集经验&lt;/strong&gt;，然后传输至可&lt;strong&gt;计算梯度的中央学习器&lt;/strong&gt;，生成一个具备完全独立的actor和learner的模型。为了利用现代计算系统，IMPALA可使用&lt;strong&gt;单个学习器&lt;/strong&gt;或执行&lt;strong&gt;同步更新的多个学习器&lt;/strong&gt;来实现。用这种方式分离学习和动作可以有效地&lt;strong&gt;提高整个系统的吞吐量&lt;/strong&gt;，因为 actor不再需要等待学习步（像 batched A2C 架构中那样）。这使得我们可以在多个有趣的环境中训练IMPALA，无需经历帧渲染时间的变动或耗时的任务重启。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1802.01561&quot;&gt;IMPALA: Scalable Distributed Deep-RL with Importance Weighted Actor-Learner Architectures&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;xx-自己搞一下&quot;&gt;xx. 自己搞一下&lt;/h2&gt;

&lt;p&gt;参考一个简单的例子：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA5MDMwMTIyNQ==&amp;amp;mid=2649294666&amp;amp;idx=1&amp;amp;sn=ef88c31c17a1ed6f6511713664a0e60f&amp;amp;chksm=8810110cbf67981a7b8d2680f57240d561b3b52eb364d674766b4f91937d652e12e44ba7e457&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1565122185223&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;强化学习异步分布式训练实现&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;其他rl平台&quot;&gt;其他RL平台&lt;/h2&gt;

&lt;h3 id=&quot;open-spiel&quot;&gt;open spiel&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650768900&amp;amp;idx=3&amp;amp;sn=e214fc23d26cdd4eb94f5e6ea5feb7a2&amp;amp;chksm=871a427ab06dcb6ccadc9ed0ec3f57127f27f91dda1ba3ff4700a380f3dc1cb215dfd36522e8&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=mmBhl6hER5JU9q0KMKTTFnbwPDksdn18kk%2FlW9Ih3p2TCzi4%2BlfisKHhCysHq%2Bou#rd&quot;&gt;DeepMind开源强化学习游戏框架，25款线上游戏等你来挑战&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/deepmind/open_spiel&quot;&gt;https://github.com/deepmind/open_spiel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;目前大多是棋牌、博弈相关游戏。&lt;/p&gt;

&lt;h3 id=&quot;rlpyt&quot;&gt;rlpyt&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/astooke/rlpyt&quot;&gt;https://github.com/astooke/rlpyt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/zKVF0AmQgop4i9j04PPyqw&quot;&gt;集合三大类无模型强化学习算法，BAIR开源RL代码库rlpyt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.01500&quot;&gt;rlpyt: A Research Code Base for Deep Reinforcement Learning in PyTorch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;rlpyt 库的重要特征和能力包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;以串行模式运行实验（对 debug 有帮助）；&lt;/li&gt;
  &lt;li&gt;以并行模式运行实验，具备并行采样和/或多 GPU 优化的选项；&lt;/li&gt;
  &lt;li&gt;同步或异步采样-优化（异步模式通过 replay buffer 实现）；&lt;/li&gt;
  &lt;li&gt;在环境采样中，使用 CPU 或 GPU 进行训练和/或分批动作选择；&lt;/li&gt;
  &lt;li&gt;全面支持循环智能体；&lt;/li&gt;
  &lt;li&gt;在训练过程中，执行在线或离线评估，以及智能体诊断日志记录；&lt;/li&gt;
  &lt;li&gt;在本地计算机上，启动对实验进行栈/队列（stacking / queueing）设置的程序；&lt;/li&gt;
  &lt;li&gt;模块化：易于修改和对已有组件的重用；&lt;/li&gt;
  &lt;li&gt;兼容 OpenAI Gym 环境接口。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;rlpyt 库中的已实现算法包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;策略梯度：A2C、PPO&lt;/li&gt;
  &lt;li&gt;DQN及其变体：Double、Dueling、Categorical、Rainbow minus Noisy Nets、Recurrent (R2D2-style)&lt;/li&gt;
  &lt;li&gt;QPG：DDPG、TD3、SAC&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;fen&quot;&gt;FEN&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/Wy9xpN-EqKybtrbYe5xLVQ&quot;&gt;兼顾公平与效率？北大NeurIPS 19论文提出多智能体强化学习方法FEN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;公平有助于人类社会的稳定和生产力的提高，同样对于多智能体系统也十分重要。然而让一组智能体学习提升系统效率并同时保持公平是一个复杂的、多目标的、联合策略优化问题。目前主流的多智能体强化学习算法没有考虑公平性的问题，一些针对特定情景公平性的方法又依赖专家知识，这对于一般性情景并不适用。&lt;/p&gt;

&lt;p&gt;作者提出一种分层多智能体强化学习方法 Fair-Efficient Network（FEN，「分」），从三个方面解决这一问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;提出 fair-efficient reward，用于学习效率与公平。&lt;/li&gt;
  &lt;li&gt;提出一种 hierarchy 架构，降低学习难度。&lt;/li&gt;
  &lt;li&gt;提出 FEN 的分布式训练方法。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://z0ngqing.github.io/paper/nips-jiechuan19.pdf&quot;&gt;Learning Fairness in Multi-Agent Systems&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorflow优化</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-optimizations.html"/>
   <updated>2018-04-29T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-optimizations</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#阿里的tensortlowrs&quot;&gt;阿里的TensorTlowRS&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tensorflowrs分布式架构&quot;&gt;TensorFlowRS分布式架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ps-plus&quot;&gt;PS-Plus&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#智能参数分配&quot;&gt;智能参数分配&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#去id化特征支持&quot;&gt;去ID化特征支持&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#通信层优化&quot;&gt;通信层优化&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#性能&quot;&gt;性能&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#在线学习&quot;&gt;在线学习&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#非id化特征支持&quot;&gt;非ID化特征支持&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#特征动态增删&quot;&gt;特征动态增删&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#模型增量实时导出&quot;&gt;模型增量实时导出&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#auc-decay&quot;&gt;AUC Decay&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#大规模训练场景下的收敛效果优化&quot;&gt;大规模训练场景下的收敛效果优化&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#问题阐述&quot;&gt;问题阐述&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#梯度补偿&quot;&gt;梯度补偿&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#实验结果&quot;&gt;实验结果&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#高级训练模式&quot;&gt;高级训练模式&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#graph-embedding&quot;&gt;Graph Embedding&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#memory-network&quot;&gt;Memory Network&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#可视化模型分析系统deepinsight&quot;&gt;可视化模型分析系统DeepInsight&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;阿里的tensortlowrs&quot;&gt;阿里的TensorTlowRS&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650741534&amp;amp;idx=4&amp;amp;sn=605076cdf0f45812c8f0fafd5e4e01b5&amp;amp;chksm=871adf60b06d5676852cb30c2c5cad45d5b74fc072dcad6e9f28c86245fa97426c9a47b676a8&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0429va5QwRaha7z2IE3w6B7l&amp;amp;pass_ticket=INCrGaryVZRn7Xp0qFQ7uod1VN14o8mkpvq1bswtroEgKQavvDm7mmg4E7yTOH6d#rd&quot;&gt;专栏 | 阿里妈妈基于TensorFlow做了哪些深度优化？TensorFlowRS架构解析&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;简介&quot;&gt;简介&lt;/h3&gt;

&lt;p&gt;相比图像、语音、视频等领域，搜索、广告、推荐等场景有着独特的场景特点: &lt;strong&gt;样本规模&lt;/strong&gt;和&lt;strong&gt;特征空间&lt;/strong&gt;通常非常巨大，千亿样本、百亿特征并不罕见，同时存在&lt;strong&gt;大量的稀疏特征&lt;/strong&gt;作为Embedding输入。&lt;/p&gt;

&lt;p&gt;阿里妈妈的优化：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;解决了原生TF&lt;strong&gt;水平扩展能力&lt;/strong&gt;不足的问题。在我们的测试中，绝大多数搜索广告模型的训练性能提升在十倍以上，某些模型的极限性能最高可提升百倍。&lt;/li&gt;
  &lt;li&gt;支持完备的&lt;strong&gt;在线学习语义&lt;/strong&gt;，&lt;strong&gt;模型变更实时写出&lt;/strong&gt;；&lt;strong&gt;稀疏特征无需做连续ID化&lt;/strong&gt;，可以直接使用原始特征表征进行训练，大幅简化了特征工程的复杂度。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;异步训练&lt;/strong&gt;的&lt;strong&gt;梯度修正优化器&lt;/strong&gt;（grad-compensation optimizer），有效减少了异步大规模并发引起的训练效果损失。&lt;/li&gt;
  &lt;li&gt;集成了高效的&lt;strong&gt;Graph Embedding&lt;/strong&gt;、&lt;strong&gt;Memory Network&lt;/strong&gt;、&lt;strong&gt;Cross Media&lt;/strong&gt;等多种高级训练模式。&lt;/li&gt;
  &lt;li&gt;模型&lt;strong&gt;可视化系统&lt;/strong&gt;DeepInSight提供深度模型训练的多维度可视化分析。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;tensorflowrs分布式架构&quot;&gt;TensorFlowRS分布式架构&lt;/h3&gt;

&lt;p&gt;TF作为一个分布式训练系统有两个主要的问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;水平扩展能力差&lt;/strong&gt;：在大部分模型的性能测试中,我们发现随着&lt;strong&gt;数据并行度的增加&lt;/strong&gt;，&lt;strong&gt;单个worker的样本处理QPS急剧下降&lt;/strong&gt;。当&lt;strong&gt;worker数量&lt;/strong&gt;增大到一定规模的时候，系统&lt;strong&gt;整体QPS不再有增长甚至有所下降&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;缺乏&lt;strong&gt;完备的分布式Failover机制&lt;/strong&gt;：
    &lt;ul&gt;
      &lt;li&gt;TF基于&lt;strong&gt;静态拓扑配置&lt;/strong&gt;来构建cluster，&lt;strong&gt;不支持动态组网&lt;/strong&gt;，这就意味着当&lt;strong&gt;某个ps或者worker挂掉重启之后，如果ip或者端口发生变化（例如机器crash），训练将无法继续。&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;另外，TF的&lt;strong&gt;checkpoint&lt;/strong&gt;只包含&lt;strong&gt;server存储的参数&lt;/strong&gt;信息，&lt;strong&gt;不包含worker端的状态&lt;/strong&gt;，&lt;strong&gt;不是全局一致性&lt;/strong&gt;的checkpoint，&lt;strong&gt;无法实现Exactly-Once等基本的Failover语义&lt;/strong&gt;。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TensorFlowRS的解决方案：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;通过&lt;strong&gt;对接独立参数服务器&lt;/strong&gt;提升水平扩展能力&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在对TF做过细致的profiling之后，我们发现&lt;strong&gt;TF原生的PS&lt;/strong&gt;由于设计和实现方面的多种原因（&lt;strong&gt;grpc&lt;/strong&gt;，&lt;strong&gt;lock&lt;/strong&gt;，&lt;strong&gt;graph-engine&lt;/strong&gt;），很难达良好的水平扩展能力。于是我们决定丢掉TF-PS的包袱，&lt;strong&gt;重新实现一个高性能的参数服务器&lt;/strong&gt;：PS-Plus。此外我们提供了完整的TF on PS-Plus方案，可以支持用户在&lt;strong&gt;Native-PS&lt;/strong&gt;和&lt;strong&gt;PS-Plus&lt;/strong&gt;之间&lt;strong&gt;自由切换&lt;/strong&gt;，并且&lt;strong&gt;完全兼容&lt;/strong&gt;TensorFlow的&lt;strong&gt;Graph语义&lt;/strong&gt;和&lt;strong&gt;所有API&lt;/strong&gt;。用户可以在深度网络代码一行不改的情况下，将参数分布和运行在PS-Plus上，享受高性能的参数交换和良好的水平扩展能力。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;重新设计Failover机制，支持&lt;strong&gt;动态组网&lt;/strong&gt;和&lt;strong&gt;Exactly-Once&lt;/strong&gt;的Failover&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TensorFlowRS&lt;strong&gt;引入了worker state&lt;/strong&gt;，在checkpoint中存储了worker的状态信息，&lt;strong&gt;worker重启后&lt;/strong&gt;，会&lt;strong&gt;从接着上次的进度继续训练&lt;/strong&gt;。此外TensorFlowRS通过&lt;strong&gt;zk&lt;/strong&gt;生成&lt;strong&gt;cluster配置&lt;/strong&gt;，支持了&lt;strong&gt;动态组网&lt;/strong&gt;的Failover。新的Failover机制可以保证&lt;strong&gt;任意角色挂掉&lt;/strong&gt;的情况下，系统都能在&lt;strong&gt;分钟级完成Failover&lt;/strong&gt;，并且&lt;strong&gt;不多算&lt;/strong&gt;和&lt;strong&gt;漏算&lt;/strong&gt;数据。&lt;/p&gt;

&lt;p&gt;所谓的exactly-once，参考：&lt;a href=&quot;https://www.jianshu.com/p/5d889a67dcd3&quot;&gt;https://www.jianshu.com/p/5d889a67dcd3&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;在分布式系统中，构成系统的&lt;strong&gt;任何节点&lt;/strong&gt;都是被定义为&lt;strong&gt;可以彼此独立失败的&lt;/strong&gt;。比如在 Kafka中，broker可能会crash，在producer推送数据至topic的过程中也可能会遇到网络问题。根据producer处理此类故障所采取的提交策略类型，我们可以获得不同的语义：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;at-least-once&lt;/strong&gt;：如果&lt;strong&gt;producer收到&lt;/strong&gt;来自Kafka &lt;strong&gt;broker的确认（ack）或者acks = all&lt;/strong&gt;，则表示该消息&lt;strong&gt;已经写入&lt;/strong&gt;到Kafka。但如果producer &lt;strong&gt;ack超时或收到错误&lt;/strong&gt;，则可能会&lt;strong&gt;重试发送消息&lt;/strong&gt;，客户端会认为该消息未写入Kafka。如果&lt;strong&gt;broker&lt;/strong&gt;在&lt;strong&gt;发送Ack之前失败&lt;/strong&gt;，但在消息成功写入Kafka之后，此重试将&lt;strong&gt;导致该消息被写入两次&lt;/strong&gt;，因此消息会被不止一次地传递给最终consumer，这种策略可能导致重复的工作和不正确的结果。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;at-most-once&lt;/strong&gt;：如果在&lt;strong&gt;ack超时或返回错误时&lt;/strong&gt;producer&lt;strong&gt;不重试&lt;/strong&gt;，则该消息&lt;strong&gt;可能最终不会写入&lt;/strong&gt;Kafka，因此不会传递给consumer。在大多数情况下，这样做是为了&lt;strong&gt;避免重复&lt;/strong&gt;的可能性，业务上必须接受&lt;strong&gt;数据传递可能的丢失&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;exactly-once&lt;/strong&gt;：&lt;strong&gt;即使producer重试&lt;/strong&gt;发送消息，消息也会&lt;strong&gt;保证最多一次地传递&lt;/strong&gt;给最终consumer。该语义是最理想的，但也难以实现，这是因为它需要&lt;strong&gt;消息系统本身&lt;/strong&gt;与&lt;strong&gt;生产&lt;/strong&gt;和&lt;strong&gt;消费&lt;/strong&gt;消息的应用程序进行&lt;strong&gt;协作&lt;/strong&gt;。例如如果在&lt;strong&gt;消费&lt;/strong&gt;消息&lt;strong&gt;成功后&lt;/strong&gt;，将Kafka &lt;strong&gt;consumer的偏移量rollback&lt;/strong&gt;，我们将会再次&lt;strong&gt;从该偏移量开始接收&lt;/strong&gt;消息。这表明&lt;strong&gt;消息传递系统&lt;/strong&gt;和&lt;strong&gt;客户端应用程序&lt;/strong&gt;必须&lt;strong&gt;配合调整&lt;/strong&gt;才能实现excactly-once。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tensorflowRS.webp&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;ps-plus&quot;&gt;PS-Plus&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;高性能&lt;/strong&gt;：PS-Plus通过&lt;strong&gt;智能参数分配&lt;/strong&gt;，&lt;strong&gt;零拷贝&lt;/strong&gt;，&lt;strong&gt;seastar&lt;/strong&gt;等多项技术，进一步提升了&lt;strong&gt;单台server的服务能力&lt;/strong&gt;和系统&lt;strong&gt;整体的水平扩展能力&lt;/strong&gt;。在实测中，在64core的机器上单个server能轻松用满55+的核心，在dense场景下io能打满双25G网卡，系统整体在&lt;strong&gt;1~4000 worker&lt;/strong&gt;的范围内都具有&lt;strong&gt;近似线性&lt;/strong&gt;的&lt;strong&gt;水平扩展能力&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;高度灵活&lt;/strong&gt;：PS-Plus拥有完善的&lt;strong&gt;UDF接口&lt;/strong&gt;，用户可使用&lt;strong&gt;SDK&lt;/strong&gt;开发定制化的UDF插件，并且可以通过简单的&lt;strong&gt;C++&lt;/strong&gt;以及&lt;strong&gt;Python&lt;/strong&gt;接口进行调用。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;完备的在线学习支持&lt;/strong&gt;：PS-Plus支持&lt;strong&gt;非ID化特征训练&lt;/strong&gt;，&lt;strong&gt;特征动态增删&lt;/strong&gt;，以及&lt;strong&gt;模型增量实时导出&lt;/strong&gt;等支撑在线学习的重要特性。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;智能参数分配&quot;&gt;智能参数分配&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;参数分配策略&lt;/strong&gt;(variable &lt;strong&gt;placement&lt;/strong&gt;)，决定了如何将一个参数&lt;strong&gt;切分&lt;/strong&gt;并&lt;strong&gt;放置&lt;/strong&gt;到不同的server上。placement策略的好坏在&lt;strong&gt;高并发&lt;/strong&gt;的情况下对&lt;strong&gt;PS的整体性能&lt;/strong&gt;有着重大的影响。传统ParameterServer的placement方案是由系统&lt;strong&gt;预先&lt;/strong&gt;实现几种常见的placement算法（比如&lt;strong&gt;平均切分+roundrobin&lt;/strong&gt;），或者由&lt;strong&gt;用户&lt;/strong&gt;在&lt;strong&gt;创建参数的时候手工划分&lt;/strong&gt;，往往没有综合考虑&lt;strong&gt;全局的参数规模&lt;/strong&gt;、&lt;strong&gt;Server的负载&lt;/strong&gt;等。&lt;/p&gt;

&lt;p&gt;PS-Plus实现了&lt;strong&gt;基于模拟退火算法&lt;/strong&gt;的&lt;strong&gt;启发式&lt;/strong&gt;参数分配策略，后续也在考虑实现&lt;strong&gt;基于运行时负载&lt;/strong&gt;，&lt;strong&gt;动态rebalance&lt;/strong&gt;的placement策略(是不是可以试试基于rl的呢，手动滑稽。。。)。PS-Plus的placement设计有如下优点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;综合考虑了&lt;strong&gt;全局参数&lt;/strong&gt;的&lt;strong&gt;shape&lt;/strong&gt;信息，在&lt;strong&gt;cpu&lt;/strong&gt;，&lt;strong&gt;内存&lt;/strong&gt;，&lt;strong&gt;网络带宽&lt;/strong&gt;等限制条件下给出了&lt;strong&gt;近似最优&lt;/strong&gt;的placement方案，避免了手工分配造成的不均匀、热点等问题。&lt;/li&gt;
  &lt;li&gt;整个&lt;strong&gt;参数分配过程&lt;/strong&gt;由&lt;strong&gt;系统内部自动完成&lt;/strong&gt;，用户无需配置即可获得接近最优的性能，用户无需了解PS底层实现的具体细节。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Partition由框架自动完成&lt;/strong&gt;，在上层算法代码，如TF代码中，&lt;strong&gt;不需要额外使用PartitionedVariable&lt;/strong&gt;等机制，使用简单方便。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;去id化特征支持&quot;&gt;去ID化特征支持&lt;/h4&gt;

&lt;p&gt;目前主流的深度学习框架都是以&lt;strong&gt;连续的内存&lt;/strong&gt;来存储训练参数，通过&lt;strong&gt;偏移量（ID值）&lt;/strong&gt;来&lt;strong&gt;寻址&lt;/strong&gt;到具体的权重。为了&lt;strong&gt;避免内存的浪费&lt;/strong&gt;，需要对&lt;strong&gt;特征&lt;/strong&gt;做&lt;strong&gt;从0开始&lt;/strong&gt;的&lt;strong&gt;连续ID化编码&lt;/strong&gt;，这一过程我们称之为&lt;strong&gt;特征ID化&lt;/strong&gt;。特征ID化是一个非常复杂的过程，尤其是&lt;strong&gt;当样本和特征数量非常庞大&lt;/strong&gt;的时候，特征ID化会&lt;strong&gt;占用大量的时间和机器资源&lt;/strong&gt;，给样本构建带来了很大的复杂度。&lt;/p&gt;

&lt;p&gt;PS-Plus内部实现了一个&lt;strong&gt;定制化&lt;/strong&gt;的&lt;strong&gt;hashmap&lt;/strong&gt;，针对参数交换场景做了专门的优化，在&lt;strong&gt;支持特征动态增删&lt;/strong&gt;的同时提供了超高的性能。通过hashmap，PS-Plus直接实现了&lt;strong&gt;对非ID特征的支持&lt;/strong&gt;，极大的简化了样本构建的复杂度。&lt;/p&gt;

&lt;h4 id=&quot;通信层优化&quot;&gt;通信层优化&lt;/h4&gt;

&lt;p&gt;对于Parameter Server架构，延迟是影响整体性能的重要原因。尤其是在模型复杂度不高的情况下，模型计算部分往往在10~100ms量级，那么&lt;strong&gt;总体通信的延迟&lt;/strong&gt;就成为一个关键因素。&lt;/p&gt;

&lt;p&gt;在传统的&lt;strong&gt;pipeline线程模型&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;高并发&lt;/strong&gt;情况下&lt;strong&gt;中断&lt;/strong&gt;和&lt;strong&gt;线程上下文切换&lt;/strong&gt;会导致&lt;strong&gt;很大的开销&lt;/strong&gt;，同时会引起&lt;strong&gt;大量的cache-line miss&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;此外，&lt;strong&gt;高频&lt;/strong&gt;的&lt;strong&gt;锁竞争&lt;/strong&gt;是带来延迟的最主要原因之一，即便是各类&lt;strong&gt;SpinLock&lt;/strong&gt;、&lt;strong&gt;读写锁&lt;/strong&gt;等优化也并不能有效消除这个问题。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;我们认为&lt;strong&gt;polling&lt;/strong&gt; + &lt;strong&gt;run to completion&lt;/strong&gt;是一个正确的选择，并且设计了我们的整体通信层架构。&lt;/p&gt;

&lt;p&gt;在新的通信层中，我们使用了&lt;strong&gt;Seastar&lt;/strong&gt;作为底层的框架。对于&lt;strong&gt;Server&lt;/strong&gt;、&lt;strong&gt;Worker&lt;/strong&gt;上的&lt;strong&gt;connection&lt;/strong&gt;，都严格保证&lt;strong&gt;connection绑定到固定的线程&lt;/strong&gt;，同时&lt;strong&gt;线程与CPU核心绑定&lt;/strong&gt;。Request、response直接采用&lt;strong&gt;run to completion&lt;/strong&gt;的方式在&lt;strong&gt;当前线程&lt;/strong&gt;处理。整体架构如下图所示：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tensorflowRS-communication.webp&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在Seastar的基础上，做了很多功能、性能的改进和优化&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;外部线程交互队列&lt;/strong&gt;。我们借鉴Seastar核心之间的交互机制，提供了一个&lt;strong&gt;M:N无锁生产者消费者队列&lt;/strong&gt;，用于&lt;strong&gt;外部线程&lt;/strong&gt;与&lt;strong&gt;Seastar内部线程&lt;/strong&gt;进行&lt;strong&gt;交互&lt;/strong&gt;。相比传统队列性能有极大的提升。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;写请求顺序调度&lt;/strong&gt;。&lt;strong&gt;从外部线程poll到的写请求&lt;/strong&gt;，如果直接调用Seastar的写接口，会导致&lt;strong&gt;写buffer无法保证有序&lt;/strong&gt;。我们通过队列机制的改造，&lt;strong&gt;自动保证了写顺序&lt;/strong&gt;，同时&lt;strong&gt;基本不损失多connection的并发写&lt;/strong&gt;的性能。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;灵活的编解码层&lt;/strong&gt;。我们提供了一套&lt;strong&gt;编解码层的抽象接口&lt;/strong&gt;，方便用户使用，从而&lt;strong&gt;不需要借助protobuf等传统的序列化、反序列化的第三方库&lt;/strong&gt;，同时也&lt;strong&gt;避免了protobuf的一些性能问题&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;注：更多关于SeaStar可以参考：&lt;a href=&quot;https://daiwk.github.io/posts/platform-seastar.html&quot;&gt;https://daiwk.github.io/posts/platform-seastar.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;关于run to completion：&lt;a href=&quot;https://en.wikipedia.org/wiki/Run_to_completion_scheduling&quot;&gt;https://en.wikipedia.org/wiki/Run_to_completion_scheduling&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;性能&quot;&gt;性能&lt;/h3&gt;

&lt;p&gt;在Dense以及WDE(Wide-Deep-Embedding)两种经典模型的性能指标：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;dense:
    &lt;ul&gt;
      &lt;li&gt;Batch-size: 100&lt;/li&gt;
      &lt;li&gt;Input-dimension: 1130&lt;/li&gt;
      &lt;li&gt;Hidden-units: 5层全连接：256,128,64,32,1&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;wde:
    &lt;ul&gt;
      &lt;li&gt;Batch-size: 100&lt;/li&gt;
      &lt;li&gt;Deep:
        &lt;ul&gt;
          &lt;li&gt;Input-dimension:310&lt;/li&gt;
          &lt;li&gt;Hidden-units:256,128,64,32,1&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Wide:
        &lt;ul&gt;
          &lt;li&gt;Input-dimension:0.2B&lt;/li&gt;
          &lt;li&gt;Output-dimension: 1&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Embedding:
        &lt;ul&gt;
          &lt;li&gt;Input-dimension:0.5B / 7.5B&lt;/li&gt;
          &lt;li&gt;Output-dimension: 8&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tensorflowRS-qps-dense-wde.webp&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;水平扩展能力比较:&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tensorflowRS-scalable.webp&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;在线学习&quot;&gt;在线学习&lt;/h3&gt;

&lt;p&gt;TensorFlowRS通过对接PS-Plus，给出了一套完整的端到端的在线学习解决方案，赋予了TF支持&lt;strong&gt;千亿&lt;/strong&gt;规模&lt;strong&gt;非ID化特征&lt;/strong&gt;在线训练的能力。&lt;/p&gt;

&lt;h4 id=&quot;非id化特征支持&quot;&gt;非ID化特征支持&lt;/h4&gt;

&lt;p&gt;在在线学习的场景下做&lt;strong&gt;特征实时ID化&lt;/strong&gt;是比较复杂的，需要一个&lt;strong&gt;超高性能&lt;/strong&gt;的&lt;strong&gt;全局&lt;/strong&gt;的ID生成器，这给样本生成带来了很大的复杂度。TensorFlowRS利用PS-Plus&lt;strong&gt;直接实现&lt;/strong&gt;了对&lt;strong&gt;非ID特征&lt;/strong&gt;的支持，极大的简化了&lt;strong&gt;实时样本构建&lt;/strong&gt;的复杂度。&lt;/p&gt;

&lt;h4 id=&quot;特征动态增删&quot;&gt;特征动态增删&lt;/h4&gt;

&lt;p&gt;在在线训练的场景下，&lt;strong&gt;训练任务&lt;/strong&gt;会以&lt;strong&gt;service&lt;/strong&gt;的形式&lt;strong&gt;长期运行&lt;/strong&gt;，在训练过程中，不断会有新特征加入到模型中，为了保证训练可以长期进行而不会因为新特征的不断加入导致OOM，PS-Plus在&lt;strong&gt;支持特征动态添加&lt;/strong&gt;的同时，还提供了&lt;strong&gt;默认的特征删除策略&lt;/strong&gt;，可以将&lt;strong&gt;低频或者低权重&lt;/strong&gt;的特征&lt;strong&gt;删除&lt;/strong&gt;掉，用户还可以通过&lt;strong&gt;UDF定制&lt;/strong&gt;符合自身业务需求的删除策略&lt;/p&gt;

&lt;h4 id=&quot;模型增量实时导出&quot;&gt;模型增量实时导出&lt;/h4&gt;

&lt;p&gt;在线学习模型更新的常见方式有全量和增量两种。在模型参数较多的情况下，全量更新的方式会对在线系统的带宽带来巨大的压力，而降低更新频率又会使模型的实效性降低。PS-Plus支持以&lt;strong&gt;任意频率&lt;/strong&gt;将模型&lt;strong&gt;增量部分实时写出到消息队列&lt;/strong&gt;，在&lt;strong&gt;大幅减少网络IO&lt;/strong&gt;的同时实现了真正意义上的模型实时更新。&lt;/p&gt;

&lt;h4 id=&quot;auc-decay&quot;&gt;AUC Decay&lt;/h4&gt;

&lt;p&gt;在在线学习的场景下，我们希望在&lt;strong&gt;训练的过程中&lt;/strong&gt;就可以尽快的&lt;strong&gt;发现模型本身的异常&lt;/strong&gt;，而不是等模型更新到线上之后。因此我们需要有一些方法来评估模型在训练过程中的 AUC等指标。TF里&lt;strong&gt;默认的streaming auc&lt;/strong&gt;的实现在&lt;strong&gt;历史数据累积了一定量&lt;/strong&gt;的时候，&lt;strong&gt;无法及时反应当前模型的状态&lt;/strong&gt;，反馈有很大的&lt;strong&gt;滞后性&lt;/strong&gt;。因此我们引入了新的AUC计算机制：&lt;strong&gt;AUC Decay&lt;/strong&gt;。AUC Decay本质上是一种&lt;strong&gt;特殊的Moving Average&lt;/strong&gt;，通过&lt;strong&gt;基于时间的减益方式&lt;/strong&gt;，&lt;strong&gt;弱化历史样本和模型&lt;/strong&gt;在&lt;strong&gt;当前AUC&lt;/strong&gt;计算中的&lt;strong&gt;比重&lt;/strong&gt;，以达到更快反应模型变化的目的。&lt;/p&gt;

&lt;h3 id=&quot;大规模训练场景下的收敛效果优化&quot;&gt;大规模训练场景下的收敛效果优化&lt;/h3&gt;

&lt;h4 id=&quot;问题阐述&quot;&gt;问题阐述&lt;/h4&gt;

&lt;p&gt;大数据模型引入了分布式并行训练，同步并行训练受长尾worker的制约，并发数容易受限。&lt;strong&gt;异步并行&lt;/strong&gt;是快速训练的主流。异步并行训练打破了普通SGD训练的串行性，计算的梯度与更新的模型不是严格一致，引入了梯度delay的问题。&lt;/p&gt;

&lt;p&gt;在基于ParameterServer的训练框架里，系统分为两个角色：worker和ps。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;ps&lt;/strong&gt;的职责是模型的&lt;strong&gt;切块存储&lt;/strong&gt;与&lt;strong&gt;更新&lt;/strong&gt;；&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;worker&lt;/strong&gt;的职责是&lt;strong&gt;加载从ps端获得最新的模型&lt;/strong&gt;，读取数据进行&lt;strong&gt;模型训练&lt;/strong&gt;，最后把学到&lt;strong&gt;梯度发送给ps&lt;/strong&gt;，由ps更新模型。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;异步并发训练&lt;strong&gt;打破&lt;/strong&gt;了普通SGD训练的&lt;strong&gt;串行性&lt;/strong&gt;，引入了梯度delay的问题。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tensorflowRS-gradient-delay.webp&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;如图，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(worker_m\)&lt;/code&gt;拿到了模型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;，计算得到梯度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g_t\)&lt;/code&gt;，但传给ps时，已经有r个worker向ps提交了梯度更新，所以，这个时候应用到的是模型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{t+r}\)&lt;/code&gt;。虽然梯度更新的大方向可能偏差不大，但与模型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{t+r}\)&lt;/code&gt;期望的梯度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g_{t+r}\)&lt;/code&gt;相比，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g_t\)&lt;/code&gt;是存在细微偏差的。&lt;/p&gt;

&lt;h4 id=&quot;梯度补偿&quot;&gt;梯度补偿&lt;/h4&gt;

&lt;p&gt;微软在ICML2017提出过&lt;strong&gt;DC-ASGD optimizer&lt;/strong&gt;【&lt;a href=&quot;https://arxiv.org/abs/1609.08326&quot;&gt;
Asynchronous Stochastic Gradient Descent with Delay Compensation&lt;/a&gt;】，使用&lt;strong&gt;泰勒展开&lt;/strong&gt;来&lt;strong&gt;近似梯度补偿&lt;/strong&gt;。我们测试在&lt;strong&gt;50并发以内收益良好&lt;/strong&gt;。可是在&lt;strong&gt;上百并发&lt;/strong&gt;训练里，&lt;strong&gt;泰勒展开超过了近似收敛半径&lt;/strong&gt;，导致的误差增大，效果下降。&lt;/p&gt;

&lt;p&gt;各框架实现dc-asgd的issue：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;tf: &lt;a href=&quot;https://github.com/tensorflow/tensorflow/issues/8744&quot;&gt;https://github.com/tensorflow/tensorflow/issues/8744&lt;/a&gt;，但后来被removed了【&lt;a href=&quot;https://github.com/tensorflow/tensorflow/pull/9551&quot;&gt;https://github.com/tensorflow/tensorflow/pull/9551&lt;/a&gt;】，因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;it was lacking an _apply_dense() implementation, there were typos that caused failures when used, etc.The commit is obviously in the history for somebody to re-use and fix, if they wanted, but they'd need to write more tests and probably refactor it to make it something that the team could accept to TensorFlow.&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;mxnet: &lt;a href=&quot;https://github.com/apache/incubator-mxnet/pull/3614&quot;&gt;https://github.com/apache/incubator-mxnet/pull/3614&lt;/a&gt;，代码在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mxnet.optimizer.DCASGD&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;改进：引入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Delta w\)&lt;/code&gt;【maybe就是下面第一篇里讲到的隐式动量？，，再看看】与g的&lt;strong&gt;相关因子&lt;/strong&gt;来衡量&lt;strong&gt;梯度delay的严重程度&lt;/strong&gt;，从而用来boost&lt;strong&gt;主流&lt;/strong&gt;的&lt;strong&gt;sgd-based optimizer&lt;/strong&gt;。在每一维度上：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果大部分&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Delta w\)&lt;/code&gt;与-g是&lt;strong&gt;正相关&lt;/strong&gt;，说明大部分worker都在&lt;strong&gt;往同方向更新&lt;/strong&gt;，模型w&lt;strong&gt;在这个方向上已经走的挺远了&lt;/strong&gt;，继续前进需要&lt;strong&gt;谨慎&lt;/strong&gt;。这个时候，&lt;strong&gt;保持g的方向不变，但减少g的绝对值。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;如果大部分&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Delta w\)&lt;/code&gt;与-g是&lt;strong&gt;负相关&lt;/strong&gt;，说明大部分worker都在往反方向更新，此时&lt;strong&gt;g是一个比较强烈的转折信号&lt;/strong&gt;，揭示了模型w的更新方向要发生变化，我们需要重视这个信号，所以我们&lt;strong&gt;保持g的方向不变，但增大了g的绝对值&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;引入相关因子是基于以下原因：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;异步训练时，存在&lt;strong&gt;隐式&lt;/strong&gt;的&lt;strong&gt;梯度动量加速&lt;/strong&gt;情况。参见&lt;a href=&quot;https://arxiv.org/abs/1605.09774&quot;&gt;Asynchrony begets(产生) Momentum, with an Application to Deep Learning&lt;/a&gt;，&lt;strong&gt;并发越大&lt;/strong&gt;，&lt;strong&gt;隐式动量越大&lt;/strong&gt;，造成&lt;strong&gt;梯度往一个方向过度前进&lt;/strong&gt;的情况。此文的abstract里说：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;For convolutional neural networks, we experimentally validate that the degree of asynchrony directly correlates with the momentum, confirming our main result. An important implication is that tuning the momentum parameter is important when considering different levels of asynchrony.&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;如果&lt;strong&gt;不是很老的w&lt;/strong&gt;，&lt;strong&gt;相关因子&lt;/strong&gt;是&lt;strong&gt;转折信号&lt;/strong&gt;，暗示模型在&lt;strong&gt;多个worker的动量累积推动&lt;/strong&gt;下&lt;strong&gt;前进的有些过度&lt;/strong&gt;了。&lt;/li&gt;
  &lt;li&gt;存在着tradeoff，&lt;strong&gt;太老的w&lt;/strong&gt;，&lt;strong&gt;信号准确率会下降&lt;/strong&gt;，这时要&lt;strong&gt;控制(调小)&lt;/strong&gt;系数&lt;strong&gt;lambda&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Delta w\)&lt;/code&gt;与g的相关性具备普适性，可以和主流的sgd-based optimizer结合，适应不同场景的不同优化器并发训练需求。&lt;/p&gt;

&lt;h4 id=&quot;实验结果&quot;&gt;实验结果&lt;/h4&gt;

&lt;p&gt;用相关性因子boost了SGD、Momentum、AdaGrad三种算法，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;WDE模型&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;并行度&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Boosted-sgd auc&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Boosted-moment auc&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Boosted-adagrad auc&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;100&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.012%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.01%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.012%&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;200&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.028%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.045%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.051%&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;400&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.043%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.064%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.058%&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;Cifar10 Alexnet模型&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;并行度&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Boosted-sgd accuracy&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Boosted-moment accuracy&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Boosted-adagrad accuracy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;30&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.43%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.2%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.25%&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;60&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.56%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.25%&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;+0.46%&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;h3 id=&quot;高级训练模式&quot;&gt;高级训练模式&lt;/h3&gt;

&lt;p&gt;TFRS中集成了多种高阶训练模式，例如Graph Embedding，Memory Network，Cross Media Training等。&lt;/p&gt;

&lt;h4 id=&quot;graph-embedding&quot;&gt;Graph Embedding&lt;/h4&gt;

&lt;p&gt;Graph Embedding，图是一种表征能力极强的数据结构，但是&lt;strong&gt;无法直接作为神经网络的输入&lt;/strong&gt;。TFRS&lt;strong&gt;支持样本以图的形式进行输入&lt;/strong&gt;，并支持&lt;strong&gt;多种随机游走算法动态生成正负样本&lt;/strong&gt;。目前Graph Embedding已经应用在了淘宝搜索直通车的向量化召回等多个项目里，通过&lt;strong&gt;在User-Query-Item&lt;/strong&gt;三种节点的&lt;strong&gt;异构有向图&lt;/strong&gt;中随机游走，&lt;strong&gt;生成&lt;/strong&gt;深度神经网络能够处理的&lt;strong&gt;稀疏特征&lt;/strong&gt;。最终学习出User，Query和Item三者的&lt;strong&gt;高维向量化的表示&lt;/strong&gt;，用于线上广告的向量化召回。&lt;/p&gt;

&lt;p&gt;值得一提的是，除了Graph Embedding，我们同样&lt;strong&gt;支持对图的结构进行学习&lt;/strong&gt;，例如在&lt;strong&gt;训练过程&lt;/strong&gt;中&lt;strong&gt;反馈调整图中的边的权重&lt;/strong&gt;等。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tensorflowRS-graph-embedding.webp&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h4 id=&quot;memory-network&quot;&gt;Memory Network&lt;/h4&gt;

&lt;p&gt;Memory Network记忆网络最早由Facebook在2015年提出，用于QA系统中。在本模型出现之前，机器学习的模型都缺乏可以读取和写入外部知识的组件。对很多任务来说，这是一个很强的限制。比如，给定一系列事实或故事，然后要求回答关于该主题的问题，虽然原则上这可以用&lt;strong&gt;RNN等模型&lt;/strong&gt;进行处理，然而&lt;strong&gt;它们的记忆（隐藏状态和权重编码）通常太小&lt;/strong&gt;，并且&lt;strong&gt;不能精确地记住过去的事实&lt;/strong&gt;。在阿里妈妈搜索广告场景下，我们使用记忆网络对用户行为进行建模。&lt;/p&gt;

&lt;p&gt;相比一般的在样本组织阶段进行记忆体生成的方式，TFRS通过在&lt;strong&gt;训练过程中&lt;/strong&gt;引入&lt;strong&gt;动态记忆存储模块&lt;/strong&gt;，&lt;strong&gt;支持长短期记忆&lt;/strong&gt;，大幅提高了序列化类行为数据的训练效率。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/dl-ntm-memory-networks.html&quot;&gt;https://daiwk.github.io/posts/dl-ntm-memory-networks.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;可视化模型分析系统deepinsight&quot;&gt;可视化模型分析系统DeepInsight&lt;/h3&gt;

&lt;p&gt;DeepInsight是一个深度学习可视化质量评估系统，支持&lt;strong&gt;训练阶段&lt;/strong&gt;模型&lt;strong&gt;内部数据&lt;/strong&gt;的&lt;strong&gt;全面透出&lt;/strong&gt;与&lt;strong&gt;可视化分析&lt;/strong&gt;，用以解决模型评估、分析、调试等一系列问题，提高深度模型的可解释性。&lt;/p&gt;

&lt;p&gt;下面我们通过一个&lt;strong&gt;过拟合&lt;/strong&gt;的例子来说明DeepInsight在模型质量分析和问题定位方面发挥的作用：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tensorflowRS-deepinsight.webp&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;上图是通过DeepInsight生成的特征权重分布,从图中我们可以看到&lt;strong&gt;右侧过拟合&lt;/strong&gt;模型的&lt;strong&gt;边权重大小分布很不均匀&lt;/strong&gt;，出现了&lt;strong&gt;大量权重极大的边&lt;/strong&gt;，且&lt;strong&gt;集中&lt;/strong&gt;在一条&lt;strong&gt;带状区域&lt;/strong&gt;内，其为&lt;strong&gt;某一组特征&lt;/strong&gt;输入所&lt;strong&gt;连接的所有边&lt;/strong&gt;，这表明模型&lt;strong&gt;过度拟合&lt;/strong&gt;了&lt;strong&gt;该组特征&lt;/strong&gt;的信息。在使用正则项和dropout之后，过拟合的问题仍然没解决，因此我们最终&lt;strong&gt;定位到问题出现在该组特征的输入&lt;/strong&gt;上。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>目标检测汇总</title>
   <link href="http://daiwk.github.io/posts/cv-object-detection.html"/>
   <updated>2018-04-29T00:00:00+00:00</updated>
   <id>/posts/cv-object-detection</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%8e%86%e5%8f%b2%e5%9b%9e%e9%a1%be&quot;&gt;历史回顾&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%bc%80%e6%ba%90%e5%ba%93detectron&quot;&gt;开源库：Detectron&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#yolo-nano&quot;&gt;YOLO nano&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gaussian-yolov3&quot;&gt;gaussian yolov3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;历史回顾&quot;&gt;历史回顾&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650741534&amp;amp;idx=1&amp;amp;sn=02dc164ffcedbf22124b97841ba67fe5&amp;amp;chksm=871adf60b06d567690fa2328b161c012a464687768e50f812a51b5533a7d68b99af1cf8f02b8&amp;amp;scene=0&amp;amp;pass_ticket=INCrGaryVZRn7Xp0qFQ7uod1VN14o8mkpvq1bswtroEgKQavvDm7mmg4E7yTOH6d#rd&quot;&gt;从RCNN到SSD，这应该是最全的一份目标检测算法盘点&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;同时参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU1NTUxNTM0Mg==&amp;amp;mid=2247489070&amp;amp;idx=1&amp;amp;sn=d7b00a6e66de9191d898ee654be448cc&amp;amp;chksm=fbd27a8fcca5f39950a9ef8d423afcc39f4ad5ba01f84bedca905975b53f34817f660fca1795&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0723zzseCRdY3SSK8GKzxTvO&amp;amp;pass_ticket=dxpPUDHz41aBj1bP227WEg1oWcCDfep3IeGSCzYrlaZP9ZqENPugpUQrWsVELUK8#rd&quot;&gt;一文读懂目标检测：R-CNN、Fast R-CNN、Faster R-CNN、YOLO、SSD&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;传统的目标检测算法：Cascade + HOG/DPM + Haar/SVM以及上述方法的诸多改进、优化；&lt;/li&gt;
  &lt;li&gt;候选区域/框 + 深度学习分类：通过提取候选区域，并对相应区域进行以深度学习方法为主的分类的方案，如：
    &lt;ul&gt;
      &lt;li&gt;R-CNN（Selective Search + CNN + SVM）&lt;/li&gt;
      &lt;li&gt;SPP-net（ROI Pooling）&lt;/li&gt;
      &lt;li&gt;Fast R-CNN（Selective Search + CNN + ROI）&lt;/li&gt;
      &lt;li&gt;Faster R-CNN（RPN + CNN + ROI）&lt;/li&gt;
      &lt;li&gt;R-FCN等系列方法；&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;基于深度学习的回归方法：YOLO/SSD/DenseBox 等方法；以及最近出现的结合RNN算法的RRC detection；结合DPM的Deformable CNN等&lt;/li&gt;
&lt;/ol&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rcnns.webp&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;开源库detectron&quot;&gt;开源库：Detectron&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebookresearch/Detectron&quot;&gt;https://github.com/facebookresearch/Detectron&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如果你正在寻找最先进的物体检测算法，那么你可以使用Detectron。&lt;/p&gt;

&lt;p&gt;它由Facebook开发，是AI Research软件系统的一部分。它利用Caffe2深度学习框架和Python。&lt;/p&gt;

&lt;h2 id=&quot;yolo-nano&quot;&gt;YOLO nano&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/cl3HBzt_u1Edp35YLmBVsw&quot;&gt;比Tiny YOLOv3小8倍，性能提升11个点，4MB的网络也能做目标检测&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1910.01271&quot;&gt;YOLO Nano: a Highly Compact You Only Look Once Convolutional Neural Network for Object Detection&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;研究者提出了名为 YOLO Nano 的网络。这一模型的大小在 4.0MB 左右，比 Tiny YOLOv2 和 Tiny YOLOv3 分别小了 15.1 倍和 8.3 倍。在计算上需要 4.57B 次推断运算，比后两个网络分别少了 34% 和 17%。&lt;/p&gt;

&lt;p&gt;在性能表现上，在 VOC2007 数据集取得了 69.1% 的 mAP，准确率比后两者分别提升了 12 个点和 10.7 个点。研究者还在 Jetson AGX Xavier 嵌入式模块上，用不同的能源预算进行了测试，进一步说明 YOLO Nano 非常适合边缘设备与移动端。&lt;/p&gt;

&lt;h2 id=&quot;gaussian-yolov3&quot;&gt;gaussian yolov3&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1904.04620&quot;&gt;Gaussian YOLOv3: An Accurate and Fast Object Detector Using Localization Uncertainty for Autonomous Driving&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>rl代码集合</title>
   <link href="http://daiwk.github.io/posts/rl-rl-codes.html"/>
   <updated>2018-04-27T00:00:00+00:00</updated>
   <id>/posts/rl-rl-codes</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#tensorflow&quot;&gt;tensorflow&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#keras&quot;&gt;keras&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pytorch&quot;&gt;pytorch&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#mxnet&quot;&gt;mxnet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#paddle&quot;&gt;paddle&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;tensorflow&quot;&gt;tensorflow&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/carpedm20/deep-rl-tensorflow&quot;&gt;https://github.com/carpedm20/deep-rl-tensorflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/rlcode/reinforcement-learning&quot;&gt;https://github.com/rlcode/reinforcement-learning&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;keras&quot;&gt;keras&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/keras-rl/keras-rl&quot;&gt;https://github.com/keras-rl/keras-rl&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pytorch&quot;&gt;pytorch&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html&quot;&gt;https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openai/baselines&quot;&gt;https://github.com/openai/baselines&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;mxnet&quot;&gt;mxnet&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/apache/incubator-mxnet/tree/master/example/reinforcement-learning&quot;&gt;https://github.com/apache/incubator-mxnet/tree/master/example/reinforcement-learning&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;paddle&quot;&gt;paddle&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://paddlepaddle.org/paddle/ModelPARL&quot;&gt;http://paddlepaddle.org/paddle/ModelPARL&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>GAN pytorch+keras实现</title>
   <link href="http://daiwk.github.io/posts/platform-pytorch-keras-gan.html"/>
   <updated>2018-04-26T00:00:00+00:00</updated>
   <id>/posts/platform-pytorch-keras-gan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650741360&amp;amp;idx=1&amp;amp;sn=75f2679c86e4d360d4f63715267c1f2d&amp;amp;chksm=871ade0eb06d5718704dade576bdabcaea1a14144a9bcc28c3a9673e008c514ed7b2e313a572&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0424nMoEL8MYr51O1xqlR17x&amp;amp;pass_ticket=xVD6tFMOcBuMj0CEBB2IP92A%2B%2BkSgCQGEGdSpEO0%2BVgLTzc%2F18xnZ6S7alwGIXho#rd&quot;&gt;这些资源你肯定需要！超全的GAN PyTorch+Keras实现集合&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>ResNet及其变种</title>
   <link href="http://daiwk.github.io/posts/cv-resnet-etc.html"/>
   <updated>2018-04-26T00:00:00+00:00</updated>
   <id>/posts/cv-resnet-etc</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650741211&amp;amp;idx=1&amp;amp;sn=fa2229f97a63977853d7ebcea7858076&amp;amp;chksm=871adda5b06d54b34389edac855f55b4163b81ffc25c38f26cc12e99822d87c3e28c459d78f9&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0427EnjJp1O10ZxsV6cjzN4h&amp;amp;pass_ticket=xVD6tFMOcBuMj0CEBB2IP92A%2B%2BkSgCQGEGdSpEO0%2BVgLTzc%2F18xnZ6S7alwGIXho#rd&quot;&gt;一文简述ResNet及其多种变体&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>深度神经网络的模型压缩和加速方法</title>
   <link href="http://daiwk.github.io/posts/dl-dl-compression-speedup.html"/>
   <updated>2018-04-24T00:00:00+00:00</updated>
   <id>/posts/dl-dl-compression-speedup</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%83%8c%e6%99%af&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%a0%94%e7%a9%b6%e7%8e%b0%e7%8a%b6&quot;&gt;研究现状&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#paddle%e7%9a%84slim&quot;&gt;paddle的slim&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#dafl&quot;&gt;DAFL&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247488630&amp;amp;idx=1&amp;amp;sn=894b06c31b37ccdad3e9bfdd7323a33f&amp;amp;chksm=96e9cbf6a19e42e0c666d6727430a39fe4e09db047c3cfc0465a34923b87a36dfbe7585fe339&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0424GVwdeHItD7HnyFTeDqEK&amp;amp;pass_ticket=ELDwWNrhj5p4OHcAdT%2BpNCVXGsuewWdPEQ9M8Z%2BwtE9PmYD%2Bq%2FrOC6%2BTyLhZIu%2Bd#rd&quot;&gt;当前深度神经网络模型压缩和加速都有哪些方法？&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/pdf/1710.09282.pdf&quot;&gt;A Survey of Model Compression and Acceleration for Deep Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

&lt;p&gt;在2012年，Krizhevsky等人提出的alexnet（&lt;a href=&quot;https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf&quot;&gt;ImageNet Classification with Deep Convolutional&lt;/a&gt;），以超过第二名以传统人工设计特征方法准确率 10% 的巨大领先取得了 ImageNet图像分类比赛冠军。&lt;/p&gt;

&lt;p&gt;alexnet是1个包&lt;strong&gt;含5个卷积层&lt;/strong&gt;和&lt;strong&gt;3个全连接层&lt;/strong&gt;的&lt;strong&gt;6000万参数&lt;/strong&gt;的网络。通常情况下，即使使用当时性能顶级的GPU NVIDIA &lt;strong&gt;K40&lt;/strong&gt;来训练整个模型仍需要花费&lt;strong&gt;两到三天&lt;/strong&gt;时间。对于使用全连接的大规模网络，其参数规模甚至可以达到数十亿量级。&lt;/p&gt;

&lt;p&gt;为了解决全连接层参数规模的问题，人们转而考虑&lt;strong&gt;增加卷积层&lt;/strong&gt;，使全连接参数降低。随之带来的负面影响便是&lt;strong&gt;大大增长了计算时间与能耗&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;Krizhevsky在2014年的文章中，提出的两点观察结论为后来的研究深度模型的压缩与加速提供了统计依据：&lt;/p&gt;

&lt;p&gt;现代卷积神经网络主要由两种层构成，他们具有不一样的属性和性能(!!!!!!参考&lt;a href=&quot;https://blog.csdn.net/xsc_c/article/details/42420167&quot;&gt;卷积神经网络的并行化模型——One weird trick for parallelizing convolutional neural networks&lt;/a&gt;)：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;卷积层&lt;/strong&gt;，占据了&lt;strong&gt;90% ~ 95%的计算量&lt;/strong&gt;，&lt;strong&gt;5%的参数&lt;/strong&gt;，但是对结果具有&lt;strong&gt;很大的表达能力&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;全连接层&lt;/strong&gt;，占据了&lt;strong&gt;5% ~ 10%的计算量&lt;/strong&gt;，&lt;strong&gt;95%的参数&lt;/strong&gt;，但是对于结果具有相对&lt;strong&gt;较小的表达的能力&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;综上：卷积层计算量大，所需参数系数 W 少，全连接层计算量小，所需参数系数 W 多。因此对于&lt;strong&gt;卷积层&lt;/strong&gt;适合使用&lt;strong&gt;数据并行&lt;/strong&gt;，对于&lt;strong&gt;全连接层&lt;/strong&gt;适合使用&lt;strong&gt;模型并行&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;一个典型的例子是具有50个卷积层的ResNet-50需要超过 95MB的存储器以及38亿次浮点运算。在丢弃了一些冗余的权重后，网络仍照常工作，但节省了超过75％的参数和50％的计算时间。&lt;/p&gt;

&lt;h2 id=&quot;研究现状&quot;&gt;研究现状&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;参数修剪和共享&lt;/strong&gt;（parameter pruning and sharing)：针对&lt;strong&gt;模型参数的冗余&lt;/strong&gt;性，试图去除冗余和不重要的项。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;低秩因子分解&lt;/strong&gt;（low-rank factorization）：使用&lt;strong&gt;矩阵/张量分解&lt;/strong&gt;来&lt;strong&gt;估计深度学习模型的信息参数&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;转移/紧凑卷积滤波器&lt;/strong&gt;（transferred/compact convolutional filters）：设计了&lt;strong&gt;特殊的结构卷积滤波器&lt;/strong&gt;来降低存储和计算复杂度。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;知识蒸馏&lt;/strong&gt;（knowledge distillation）：通过学习一个&lt;strong&gt;蒸馏模型&lt;/strong&gt;，训练一个&lt;strong&gt;更紧凑的神经网络&lt;/strong&gt;来重现一个更大的网络的输出。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参数修剪和共享、低秩分解和知识蒸馏方法可以用于&lt;strong&gt;全连接层&lt;/strong&gt;和&lt;strong&gt;卷积层&lt;/strong&gt;的CNN，但另一方面，使用转移/紧凑型卷积核的方法&lt;strong&gt;仅支持卷积层&lt;/strong&gt;(因为只是修改卷积filter)。&lt;/p&gt;

&lt;p&gt;低秩因子分解和基于转换/紧凑型卷积核的方法提供了一个&lt;strong&gt;端到端&lt;/strong&gt;的流水线，可以很容易地在 CPU/GPU 环境中实现。&lt;/p&gt;

&lt;p&gt;参数修剪和共享使用不同的方法，如矢量量化，二进制编码和稀疏约束来执行任务，这导致常&lt;strong&gt;需要几个步骤&lt;/strong&gt;才能达到目标。&lt;/p&gt;

&lt;p&gt;关于训练协议，基于参数修剪/共享、低秩分解的模型&lt;strong&gt;可以从预训练模型&lt;/strong&gt;或者&lt;strong&gt;从头开始&lt;/strong&gt;训练，因此灵活而有效。然而转移/紧凑的卷积核和知识蒸馏模型&lt;strong&gt;只能支持从零开始&lt;/strong&gt;训练。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/4ways-for-compression-dnn.webp&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;paddle的slim&quot;&gt;paddle的slim&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650765628&amp;amp;idx=2&amp;amp;sn=62b7034b2a68422f0b434f423e157306&amp;amp;chksm=871abd42b06d3454a083d54b4c25d8bdf973dd1183e471b586e59a8c7cf016b95de53868e7d5&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=zzUnWIgdqTLvX39vSLCKaOJN8KVDYuvxPgj7h5mQNNMiTnEMdrWSwBJSd3ch3aLL#rd&quot;&gt;自动模型压缩与架构搜索，这是飞桨PaddleSlim最全的解读&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;dafl&quot;&gt;DAFL&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/jMuIfkxvpYNGP9lqWUvIzg&quot;&gt;重磅开源！ ICCV 2019，华为诺亚提出无需数据网络压缩技术&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1904.01186&quot;&gt;DAFL：Data-Free Learning of Student Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github: &lt;a href=&quot;https://github.com/huawei-noah/DAFL&quot;&gt;https://github.com/huawei-noah/DAFL&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;绝大多数的神经网络压缩算法都假设训练数据是可以获得的。然而，在现实生活应用中，数据集往往由于隐私、法律或传输限制等原因是不可获得的。&lt;/p&gt;

&lt;p&gt;有很少的工作关注在无数据情况下的网络压缩，然而，这些方法得到的压缩后的网络准确率下降很多，这是因为这些方法没有利用待压缩网络中的信息。&lt;/p&gt;

&lt;p&gt;提出了一个新的无需训练数据的网络压缩方法。具体的，我们把给定的待压缩网络看作一个固定的判别器，接着，我们设计了一系列的损失函数来训练生成网络，使得生成图片可以代替训练数据集进行训练，最后，我们使用生成数据结合蒸馏算法得到压缩后的网络。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorflow probability</title>
   <link href="http://daiwk.github.io/posts/platform-tf-probability.html"/>
   <updated>2018-04-22T00:00:00+00:00</updated>
   <id>/posts/platform-tf-probability</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650741211&amp;amp;idx=2&amp;amp;sn=760f7041d77ad19d9da27c16fd1eff40&amp;amp;chksm=871adda5b06d54b3982d54a17eab28a599f6921e3fff2b49f3465fcb2eca788a46947ee50d26&amp;amp;scene=0&amp;amp;pass_ticket=EUwwyIFBVe43KU36qUZYEq7h15BWgwS7KrwLya%2BB%2B1HtzW6kqMdyvHJs1asAuyqo#rd&quot;&gt;资源 | 概率编程工具：TensorFlow Probability官方简介&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原文：&lt;a href=&quot;https://medium.com/tensorflow/introducing-tensorflow-probability-dca4c304e245&quot;&gt;https://medium.com/tensorflow/introducing-tensorflow-probability-dca4c304e245&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TensorFlow Probability 适用于以下需求：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;希望建立一个&lt;strong&gt;生成模型&lt;/strong&gt;，推理其hidden processes。&lt;/li&gt;
  &lt;li&gt;需要&lt;strong&gt;量化&lt;/strong&gt;预测结果的&lt;strong&gt;不确定性（uncertainty）&lt;/strong&gt;，而不是预测单个值。&lt;/li&gt;
  &lt;li&gt;训练集具有大量&lt;strong&gt;与数据点数量相关的特征&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;训练数据是&lt;strong&gt;结构化的&lt;/strong&gt;（例如，使用分组，空间，图表或语义），并且想&lt;strong&gt;使用先验信息（prior information）&lt;/strong&gt;来捕捉其中的结构。&lt;/li&gt;
  &lt;li&gt;存在一个inverse problem - 参考 &lt;a href=&quot;https://www.youtube.com/watch?v=Bb1_zlrjo1c&quot;&gt;TFDS’18 演讲视频&lt;/a&gt;以重建测量中的融合等离子体。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--user&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--upgrade&lt;/span&gt; tfp-nightly
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;源码：&lt;a href=&quot;https://github.com/tensorflow/probability&quot;&gt;https://github.com/tensorflow/probability&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>talk to book</title>
   <link href="http://daiwk.github.io/posts/dl-talk-to-book.html"/>
   <updated>2018-04-18T00:00:00+00:00</updated>
   <id>/posts/dl-talk-to-book</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#实验效果&quot;&gt;实验效果：&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#准确率&quot;&gt;准确率&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#性能&quot;&gt;性能&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#计算复杂度&quot;&gt;计算复杂度&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#内存占用&quot;&gt;内存占用&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650740857&amp;amp;idx=1&amp;amp;sn=be95f7c9f04c1cd8b4a8b4e5b6b5de5b&amp;amp;chksm=871adc07b06d55118fc6d66522a2e7e3e6fff3231775b050842a70b9df1326ae9e9a128c4702&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0414zG2CmGMaxS6Oo7jDtihI&amp;amp;pass_ticket=HoHoizuZ8hXEv%2BpUKGlCMf6B4i260rN3vajF9BgQ0sjDtoVg7DAO2SlGSICkvRb1#rd&quot;&gt;人人都可参与的AI技术体验：谷歌发布全新搜索引擎Talk to Books&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1803.11175&quot;&gt;Universal Sentence Encoder&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pre-trained model:
&lt;a href=&quot;https://tfhub.dev/google/universal-sentence-encoder/1&quot;&gt;https://tfhub.dev/google/universal-sentence-encoder/1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;使用示例：
&lt;a href=&quot;https://www.tensorflow.org/hub/modules/google/universal-sentence-encoder/1&quot;&gt;https://www.tensorflow.org/hub/modules/google/universal-sentence-encoder/1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;my notebook: &lt;a href=&quot;https://github.com/daiwk/workspace_tf/blob/master/talk_to_books/Semantic_Similarity_with_TF_Hub_Universal_Encoder.ipynb&quot;&gt;https://github.com/daiwk/workspace_tf/blob/master/talk_to_books/Semantic_Similarity_with_TF_Hub_Universal_Encoder.ipynb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;项目链接：&lt;a href=&quot;https://research.google.com/semanticexperiences/&quot;&gt;https://research.google.com/semanticexperiences/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要的两篇参考文献：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;attention is all you need，解读可以参考&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&quot;&gt;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;DAN，即&lt;a href=&quot;https://www.cs.umd.edu/~miyyer/pubs/2015_acl_dan.pdf&quot;&gt;Deep Unordered Composition Rivals Syntactic Methods for Text Classification&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;实验效果&quot;&gt;实验效果：&lt;/h2&gt;

&lt;h3 id=&quot;准确率&quot;&gt;准确率&lt;/h3&gt;

&lt;p&gt;在transfer learning的任务SST上，USE_T（Universal Sentence Encoder + Transformer）只需要1k的训练数据（这个数据集总共有67.3k的训练数据），就能够达到其他很多模型使用全量训练数据得到的准确率。&lt;/p&gt;

&lt;h3 id=&quot;性能&quot;&gt;性能&lt;/h3&gt;

&lt;p&gt;假设句子长度为n。&lt;/p&gt;

&lt;h4 id=&quot;计算复杂度&quot;&gt;计算复杂度&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;Transformer: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(n^2)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;DAN: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(n)\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;内存占用&quot;&gt;内存占用&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;Transformer: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(n^2)\)&lt;/code&gt;。但对于短句，因为Transformer只要存储unigram的embedding，所以占用的内存几乎是DAN的一半。&lt;/li&gt;
  &lt;li&gt;DAN: 与句子长度无关，由用来存储unigram和bigram的embedding的参数决定&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap9 基于确定性策略搜索的强化学习方法</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap9.html"/>
   <updated>2018-04-12T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap9</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-%e6%a6%82%e8%bf%b0&quot;&gt;1. 概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-%e9%9a%8f%e6%9c%ba%e7%ad%96%e7%95%a5%e4%b8%8e%e7%a1%ae%e5%ae%9a%e6%80%a7%e7%ad%96%e7%95%a5&quot;&gt;2. 随机策略与确定性策略&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-%e9%9a%8f%e6%9c%ba%e7%ad%96%e7%95%a5&quot;&gt;2.1 随机策略&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-%e7%a1%ae%e5%ae%9a%e6%80%a7%e7%ad%96%e7%95%a5&quot;&gt;2.2 确定性策略&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#23-%e5%af%b9%e6%af%94&quot;&gt;2.3 对比&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-ac%e6%a1%86%e6%9e%b6&quot;&gt;3. AC框架&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-%e9%9a%8f%e6%9c%ba%e7%ad%96%e7%95%a5ac%e6%96%b9%e6%b3%95&quot;&gt;3.1 随机策略AC方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-%e7%a1%ae%e5%ae%9a%e6%80%a7%e7%ad%96%e7%95%a5ac%e6%96%b9%e6%b3%95dpg&quot;&gt;3.2 确定性策略AC方法（DPG）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#33-%e6%b7%b1%e5%ba%a6%e7%a1%ae%e5%ae%9a%e6%80%a7%e7%ad%96%e7%95%a5%e6%a2%af%e5%ba%a6%e6%96%b9%e6%b3%95ddpg&quot;&gt;3.3 深度确定性策略梯度方法（DDPG）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#33-a3casynchronous-advantage-actor-critic&quot;&gt;3.3 A3C(asynchronous advantage actor-critic)&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-概述&quot;&gt;1. 概述&lt;/h2&gt;

&lt;p&gt;model-free的策略搜索方法可以分为随机策略搜索方法和确定性策略搜索方法。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;2014年以前，学者们都在发展随机策略搜索方法。因为大家认为确定性策略梯度是不存在的。&lt;/li&gt;
  &lt;li&gt;2014年Silver在论文&lt;a href=&quot;http://proceedings.mlr.press/v32/silver14.pdf&quot;&gt;Deterministic Policy Gradient Algorithms&lt;/a&gt;中提出了&lt;strong&gt;确定性策略理论&lt;/strong&gt;，即DPG。&lt;/li&gt;
  &lt;li&gt;2015年DeepMind又将DPG与DQN的成功经验相结合，提出了&lt;a href=&quot;https://arxiv.org/abs/1509.02971&quot;&gt;Continuous Control with Deep Reinforcement Learning&lt;/a&gt;，即&lt;strong&gt;DDPG&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;ICML2016，提出了&lt;a href=&quot;https://arxiv.org/abs/1602.01783&quot;&gt;Asynchronous Methods for Deep Reinforcement Learning&lt;/a&gt;，即A3C（asynchronous advantage actor-critic）算法。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-随机策略与确定性策略&quot;&gt;2. 随机策略与确定性策略&lt;/h2&gt;

&lt;h3 id=&quot;21-随机策略&quot;&gt;2.1 随机策略&lt;/h3&gt;

&lt;p&gt;随机策略公式为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi_{\theta}(a|s)=P[a|s;\theta]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;含义为，在状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;时，动作符合参数为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;的概率分布，例如常用的高斯策略：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi_{\theta}(a|s)=\frac{1}{\sqrt{2\pi \sigma}}exp(-\frac{(a-f_{\theta}(s))}{2\sigma ^2})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;在状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;处，采取的动作服从均值为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_{\theta}(s)\)&lt;/code&gt;，方差为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma ^2\)&lt;/code&gt;的正态分布。所以，即使在相同的状态下，每次采取的动作也可能不一样。&lt;/p&gt;

&lt;h3 id=&quot;22-确定性策略&quot;&gt;2.2 确定性策略&lt;/h3&gt;

&lt;p&gt;确定性策略的公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
a=\mu_{\theta}(s)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;相同的策略（即相同&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;），在状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;时，动作是唯一确定的。&lt;/p&gt;

&lt;h3 id=&quot;23-对比&quot;&gt;2.3 对比&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;确定性策略的优点在于：&lt;strong&gt;需要采样的数据少&lt;/strong&gt;，算法效率高&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;随机策略的梯度计算公式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown _{\theta}J(\pi _{\theta})=E_{s\sim \rho ^{\pi},a\sim \pi_{\theta}}[\triangledown _{\theta}log\pi_{\theta}(a|s)Q^{\pi}(s,a)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{\pi}(s,a)\)&lt;/code&gt;是状态-行为值函数。可见，策略梯度是关于状态和动作的期望，在求期望时，需要对状态分布和动作分布求积分，需要在状态空间和动作空间内大量采样，这样求出来的均值才能近似期望。&lt;/p&gt;

&lt;p&gt;而确定性策略的动作是确定的，所以，如果存在确定性策略梯度，其求解&lt;strong&gt;不需要在动作空间采样&lt;/strong&gt;，所以需要的样本数更少。对于动作空间很大的智能体（如多关节机器人），动作空间维数很大，有优势。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;随机策略的优点：随机策略可以将&lt;strong&gt;探索和改善&lt;/strong&gt;集成到&lt;strong&gt;一个策略&lt;/strong&gt;中&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;随机策略本身自带探索，可以通过探索产生各种数据（有好有坏），好的数据可以让强化学习算法改进当前策略。&lt;/p&gt;

&lt;p&gt;而确定性策略给定状态和策略参数时，动作是固定的，所以无法探索其他轨迹或者访问其他状态。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;确定性策略&lt;/strong&gt;无法探索环境，所以需要通过&lt;strong&gt;异策略&lt;/strong&gt;（off-policy）方法来进行学习，即行动策略和评估策略不是同一个策略。&lt;strong&gt;行动策略采用随机策略，而评估策略要用确定性策略&lt;/strong&gt;。而整个确定性策略的学习框架采用的是AC方法。&lt;/p&gt;

&lt;h2 id=&quot;3-ac框架&quot;&gt;3. AC框架&lt;/h2&gt;

&lt;p&gt;这里会参考&lt;a href=&quot;https://blog.csdn.net/jinzhuojun/article/details/72851548&quot;&gt;https://blog.csdn.net/jinzhuojun/article/details/72851548&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Actor-Critic（AC）方法其实是policy-based和value-based方法的结合。因为它本身是一种PG方法，同时又结合了value estimation方法，所以有些地方将之归为PG方法的一种，有些地方把它列为policy-based和value-based以外的另一种方法。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Actor指的是行动策略，负责policy gradient学习策略&lt;/li&gt;
  &lt;li&gt;Critic指的是评估策略，负责policy evaluation估计value function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所以，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一方面actor学习策略，而策略更新依赖critic估计的value function；&lt;/li&gt;
  &lt;li&gt;另一方面critic估计value function，而value function又是策略的函数。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果是Actor-only，那就是policy gradient，而如果是Critic-only，那就是Q-learning。&lt;/p&gt;

&lt;h3 id=&quot;30-pg&quot;&gt;3.0 pg&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.jianshu.com/p/2ccbab48414b&quot;&gt;https://www.jianshu.com/p/2ccbab48414b&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/qq_30615903/article/details/80747380&quot;&gt;https://blog.csdn.net/qq_30615903/article/details/80747380&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如果一个动作得到的reward多，那么我们就使其出现的概率增加，如果一个动作得到的reward少，我们就使其出现的概率减小。&lt;/p&gt;

&lt;p&gt;根据这个思想，我们构造如下的损失函数：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
loss= -log(prob)*v_t
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;上式中log(prob)表示在状态s对所选动作a的吃惊度, 如果概率越小, 反向的log(prob)反而越大. 而vt代表的是当前状态s下采取动作a所能得到的奖励，这是当前的奖励和未来奖励的贴现值的求和。也就是说，我们的策略梯度算法必须要完成一个完整的eposide才可以进行参数更新，而不是像值方法那样，每一个(s,a,r,s’)都可以进行参数更新。如果在prob很小的情况下, 得到了一个大的Reward, 也就是大的vt, 那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(log(prob)*v_t\)&lt;/code&gt;就更大, 表示更吃惊, (我选了一个不常选的动作, 却发现原来它能得到了一个好的reward, 那我就得对我这次的参数进行一个大幅修改)。&lt;/p&gt;

&lt;p&gt;Policy Gradient的核心思想是更新参数时有两个考虑：如果这个回合选择某一动作，下一回合选择该动作的概率大一些，然后再看奖惩值，如果奖惩是正的，那么会放大这个动作的概率，如果奖惩是负的，就会减小该动作的概率。&lt;/p&gt;

&lt;p&gt;注意点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;算法输出的是动作的概率，而不是Q值。&lt;/li&gt;
  &lt;li&gt;损失函数的形式为：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(loss= -log(prob)*v_t\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;需要一次完整的episode才可以进行参数的更新&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;31-随机策略ac方法&quot;&gt;3.1 随机策略AC方法&lt;/h3&gt;

&lt;p&gt;随机策略的梯度为&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown _{\theta}J(\pi _{\theta})=E_{s\sim \rho ^{\pi},a\sim \pi_{\theta}}[\triangledown _{\theta}log\pi_{\theta}(a|s)Q^{\pi}(s,a)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中Actor方法用来调整&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;值，&lt;/p&gt;

&lt;p&gt;Critic方法逼近值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{w}(s,a)\approx Q^{\pi}(s,a)\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;为待逼近的参数，可以用TD学习的方法来评估值函数。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;异策略&lt;/strong&gt;随机梯度为&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown _{\theta}J_{\beta}(\pi _{\theta})=E_{s\sim \rho ^{\pi},a\sim \beta}[\frac{\pi_{\theta}(a|s)}{\beta_{\theta}(a|s)}\triangledown _{\theta}log\pi_{\theta}(a|s)Q^{\pi}(s,a)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;和原公式的区别在于&lt;strong&gt;采样策略为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta\)&lt;/code&gt;&lt;/strong&gt;，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\sim \beta\)&lt;/code&gt;，与行动策略不同，所以叫异策略。从而，多了一项&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\pi_{\theta}(a|s)}{\beta_{\theta}(a|s)}\)&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;32-确定性策略ac方法dpg&quot;&gt;3.2 确定性策略AC方法（DPG）&lt;/h3&gt;

&lt;p&gt;确定性的策略梯度为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown _{\theta}J(\mu _{\theta})=E_{s\sim \rho ^{\mu}}[\triangledown _{\theta}\mu_{\theta}(s)\triangledown _{a}Q^{\mu}(s,a)|_{a=\mu_{\theta}(s)}]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可见，区别如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi_{\theta}\)&lt;/code&gt;变成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu_{\theta}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;原来的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{\pi}(s,a)\)&lt;/code&gt;改成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{\mu}(s,a)|_{a=\mu_{\theta}(s)}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;原来的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\sim \rho ^{\pi}\)&lt;/code&gt;变成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\sim \rho ^{\mu}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;去掉了对于动作的采样&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\sim \pi _{\theta}\)&lt;/code&gt;，而改成确定性的动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a=\mu_{\theta}(s)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;原来对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;的梯度，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\triangledown _{\theta}log\pi_{\theta}(a|s)\)&lt;/code&gt;改成了对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu\)&lt;/code&gt;的梯度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\triangledown _{\theta}\mu_{\theta}(s)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;也要求一次关于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;的梯度，即：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\triangledown _{a}Q^{\mu}(s,a)|_{a=\mu_{\theta}(s)}\)&lt;/code&gt;，即回报函数对动作的导数&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所以&lt;strong&gt;异策略&lt;/strong&gt;确定性策略梯度为&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown _{\theta}J_{\beta}(\mu _{\theta})=E_{s\sim \rho ^{\beta}}[\triangledown _{\theta}\mu_{\theta}(s)\triangledown _{a}Q^{\mu}(s,a)|_{a=\mu_{\theta}(s)}]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;与异策略的随机策略梯度进行对比，可以发现少了重要性权重，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\pi_{\theta}(a|s)}{\beta_{\theta}(a|s)}\)&lt;/code&gt;。因为重要性采样是用简单的概率分布估计复杂的概率分布，而确定性策略的动作是确定值；&lt;/p&gt;

&lt;p&gt;此外，确定性策略的值函数评估用的是Q-learning方法，也就是用TD(0)估计动作值函数，并且忽略重要性权重。&lt;/p&gt;

&lt;p&gt;然后看一下确定性策略异策略AC算法的更新过程：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
\delta _t=r_t+ \gamma Q^{w}(s_{t+1},\mu_{\theta}(s_{t+1}))-Q^{w}(s_t,a_t)\\ 
w_{t+1}=w_t+\alpha _w\delta_t\triangledown _wQ^w(s_t,a_t)\\ 
\theta _{t+1}=\theta _t+\alpha _\theta \triangledown _{\theta} \mu _{\theta}(s_t)\triangledown _aQ^w(s_t,a_t)|_{a=\mu_{\theta}(s)}
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;前两行是利用值函数逼近的方法更新值函数参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;，使用的是TD，用Q-learning。&lt;/p&gt;

&lt;p&gt;第3行是用确定性策略梯度方法更新策略参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;33-深度确定性策略梯度方法ddpg&quot;&gt;3.3 深度确定性策略梯度方法（DDPG）&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1509.02971&quot;&gt;Continuous Control with Deep Reinforcement Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DDPG是深度确定性策略，复用DNN逼近行为值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^w(s,a)\)&lt;/code&gt;和确定性策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu_\theta (s)\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;在讲DQN时，当利用DNN进行函数逼近时，强化学习算法常常不稳定。因为训练nn时往往假设输入数据是独立同分布的，而强化学习的数据 是顺序采集的，数据间存在马尔科夫性，所以这些数据并非独立同分布。&lt;/p&gt;

&lt;p&gt;为了打破数据间的相关性，DQN使用了两个技巧，经验回放和独立的目标网络。&lt;/p&gt;

&lt;p&gt;DDPG就是将这两个技巧用到DPG算法中，DDPG的经验回放和DQN完全相同，这里介绍DDPG中的独立目标网络。&lt;/p&gt;

&lt;p&gt;DDPG的目标值是上式中第一行的前两项，即&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
r_t+ \gamma Q^{w}(s_{t+1},\mu_{\theta}(s_{t+1}))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而所谓的独立目标网络，就是将上式的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;单独拿出来，利用独立的网络对其进行更新，所以DDPG的更新公式为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
\delta _t=r_t+ \gamma Q^{w^-}(s_{t+1},\mu_{\theta^-}(s_{t+1}))-Q^{w}(s_t,a_t)\\ 
w_{t+1}=w_t+\alpha _w\delta_t\triangledown _wQ^w(s_t,a_t)\\ 
\theta _{t+1}=\theta _t+\alpha _\theta \triangledown _{\theta} \mu _{\theta}(s_t)\triangledown _aQ^w(s_t,a_t)|_{a=\mu_{\theta}(s)}
\\ \theta^-=\tau \theta +(1-\tau)\theta^-
\\w^-=\tau w+(1-\tau)w^-
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DDPG的整体流程如下：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;使用权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^Q\)&lt;/code&gt;随机初始化critic网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s,a|\theta ^Q)\)&lt;/code&gt;，使用权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^{\mu}\)&lt;/code&gt;随机初始化actor&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu (s|\theta ^{\mu})\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;使用权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\({\theta ^{Q'}} \leftarrow \theta ^Q\)&lt;/code&gt;初始化目标网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q'\)&lt;/code&gt;，使用权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\({\theta ^{\mu'}} \leftarrow \theta ^{\mu}\)&lt;/code&gt;初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu'\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;初始化replay buffer &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(episode = [1,...,M]\)&lt;/code&gt; do
      &lt;ol&gt;
        &lt;li&gt;初始化一个随机过程&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathcal {N}\)&lt;/code&gt;，即noise，以用于action exploration&lt;/li&gt;
        &lt;li&gt;获取初始化的可观测状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_1\)&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t=[1,...T]\)&lt;/code&gt; do
          &lt;ol&gt;
            &lt;li&gt;根据当前的policy以用exploration noise，选择动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t=\mu(s_t|\theta^{\mu})+\mathcal {N}_t\)&lt;/code&gt;【这里体现了随机策略作为行动策略】&lt;/li&gt;
            &lt;li&gt;执行动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;，得到回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t\)&lt;/code&gt;以及新的状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{t+1}\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;将transition &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((s_t,a_t,r_t,s_{t+1})\)&lt;/code&gt;存入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;。&lt;/li&gt;
            &lt;li&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;中随机sample出一个minibatch(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;个)的transitions，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((s _i,a_i,r_i,s_{i+1})\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_i=r_i+\gamma {Q'}{(s_{i+1},{\mu'}(s_{i+1}|\theta ^{\mu'})|\theta ^{Q'}})\)&lt;/code&gt;【即使用两个目标网络得predict的值&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_i\)&lt;/code&gt;】&lt;/li&gt;
            &lt;li&gt;通过最小化loss&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L=\frac{1}{N}\sum_i(y_i-Q(s_i,a_i|\theta ^Q))^2\)&lt;/code&gt;对critic &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;进行更新&lt;/li&gt;
            &lt;li&gt;通过采样的梯度，对actor policy&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu\)&lt;/code&gt;进行更新：
 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\triangledown _{\theta ^\mu} {J}\approx \frac{1}{N}\sum_i\triangledown_aQ(s,a|\theta ^Q)|_{s=s_i,a=\mu(s_i)}\triangledown _{\theta ^\mu} {\mu(s|\theta ^\mu)|_{s_i}}\]&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;更新critic的目标网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q'\)&lt;/code&gt;和actor的目标网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu'\)&lt;/code&gt;： 
 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\begin{matrix}
\theta^{Q'}\leftarrow\tau \theta ^Q +(1-\tau)\theta^{Q'}
\\\theta^{\mu'}\leftarrow\tau \theta ^{\mu}+(1-\tau)\theta^{\mu'}
\end{matrix}\]&lt;/code&gt;&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
        &lt;li&gt;End For&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li&gt;End For&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;注：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;critic是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;，critic的目标网络是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q'\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;actor是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu\)&lt;/code&gt;，actor的目标网络是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu'\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;critic的参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^Q\)&lt;/code&gt;就是前面讲的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;critic的目标网络的参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^{Q'}\)&lt;/code&gt;就是前面讲的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w^-\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;actor的参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^\mu\)&lt;/code&gt;就是前面讲的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;actor的目标网络的参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^{\mu'}\)&lt;/code&gt;就是前面讲的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^-\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;来看看ddpg的代码：&lt;a href=&quot;https://github.com/MorvanZhou/Reinforcement-learning-with-tensorflow/blob/master/contents/9_Deep_Deterministic_Policy_Gradient_DDPG/DDPG_update.py&quot;&gt;https://github.com/MorvanZhou/Reinforcement-learning-with-tensorflow/blob/master/contents/9_Deep_Deterministic_Policy_Gradient_DDPG/DDPG_update.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码里有几个点可以注释下咯：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;需要求导的参数定义&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ae_params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GraphKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GLOBAL_VARIABLES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Actor/eval'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;at_params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GraphKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GLOBAL_VARIABLES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Actor/target'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ce_params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GraphKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GLOBAL_VARIABLES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Critic/eval'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ct_params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_collection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GraphKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GLOBAL_VARIABLES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Critic/target'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;actor和critic的定义&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Actor'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_build_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'eval'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 是用来训练的
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;a_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_build_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'target'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 目标网络只是隔tau后会直接更新参数
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Critic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# assign self.a = a in memory when calculating q for td_error,
&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# otherwise the self.a is from Actor when updating Actor
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_build_c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'eval'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 是用来训练的
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;q_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_build_c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;S_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'target'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 目标网络只是隔tau后会直接更新参数
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;actor网络，输入状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;，输出动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;，由于是连续动作空间，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a_dim&lt;/code&gt;维的向量，在tanh后，是-1到1之间，乘一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a_bound&lt;/code&gt;把输出值缩放到正确的值域里。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_build_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'l1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tanh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'scaled_a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;critic网络，输入有两个参数，状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;和动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;，输出q值(是一个数字)：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_build_c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;n_l1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;w1_s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'w1_s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;w1_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'w1_a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;b1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w1_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w1_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Q(s,a)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;critic的loss：当前reward加上gamma乘以目标critic网络输出的q，是我们的td目标（即上面提到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_i\)&lt;/code&gt;），而critic网络输出的q，则是我们当前critic网络的输出，两者之差的mse就是td_error，而我们需要对critic的参数求导，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ar_list=self.ce_params&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;n&quot;&gt;q_target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GAMMA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q_&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# in the feed_dic for the td_error, the self.a should change to actions in memory
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;td_error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;losses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean_squared_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctrain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdamOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LR_C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;td_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ce_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;actor的loss：可以发现上面讲的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\triangledown_aQ(s,a|\theta ^Q)|_{s=s_i,a=\mu(s_i)}\triangledown _{\theta ^\mu} {\mu(s|\theta ^\mu)|_{s_i}}\)&lt;/code&gt;，其实就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\partial Q(s,a)}{\partial a} \frac{\partial a}{\partial \theta ^\mu}\)&lt;/code&gt;，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\partial Q(s,a)}{\partial \theta ^\mu}\)&lt;/code&gt;，所以就是以critic网络的输出q，对actor的参数进行求导，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var_list=self.ae_params&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;n&quot;&gt;a_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# maximize the q
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;atrain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdamOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LR_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ae_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;更新目标网络的操作&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;c1&quot;&gt;# target net replacement
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;soft_replace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TAU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TAU&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                             &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;at_params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ct_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ae_params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ce_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;33-a3casynchronous-advantage-actor-critic&quot;&gt;3.3 A3C(asynchronous advantage actor-critic)&lt;/h3&gt;

&lt;p&gt;详见：&lt;a href=&quot;https://daiwk.github.io/posts/rl-distributed-rl.html#2-a3c&quot;&gt;https://daiwk.github.io/posts/rl-distributed-rl.html#2-a3c&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap8 基于置信域策略搜索的强化学习方法</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap8.html"/>
   <updated>2018-04-12T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap8</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80&quot;&gt;1. 理论基础&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-理论基础&quot;&gt;1. 理论基础&lt;/h2&gt;

&lt;p&gt;策略梯度方法的参数更新公式为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\theta_{new}=\theta_{old}+\alpha \triangledown_{\theta}J
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;策略梯度方法最大的问题是步长的选取问题，如果步长太长，策略很容易发散；如果步长太短，收敛速度很慢。&lt;/p&gt;

&lt;p&gt;合适的步长&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha\)&lt;/code&gt;指的是当策略更新后，回报函数的值不能更差。因些TRPO要解决的就是如何找到新的策略，使得新的回报函数的值单调增长，或者单调不减。&lt;/p&gt;

&lt;p&gt;用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau\)&lt;/code&gt;表示一组状态行为序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_0,u_0,...,s_H,u_H\)&lt;/code&gt;，强化学习的回报函数为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\eta (\tilde{\pi})=E_{\tau|\tilde{\pi}}[\sum _{t=0}^{\infty }\gamma^t(r(s_t))]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{\pi}\)&lt;/code&gt;表示策略。如何找到新策略，使回报函数单调不减呢？一个自然想法就是把新策略对应的回报函数分解成旧策略对应的回报函数，加上其他项。保证这个其他项大于等于0就行了。2002年Sham Kakade就提出了这么一个等式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\eta (\tilde{\pi})=\eta (\pi)+E_{s_0,a_0,...\sim \tilde{\pi}}[\sum _{t=0}^{\infty }\gamma^t(A_{\pi}(s_t,a_t))]  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;表示旧策略，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{\pi}\)&lt;/code&gt;表示新策略。其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A_{\pi}(s,a)\)&lt;/code&gt;是优势函数。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
A_{\pi}(s,a)=Q_\pi(s,a)-V_\pi(s)=E_{s'\sim P(s'|s,a)}[r(s)+\gamma V^{\pi}(s')-V^{\pi}(s)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V_\pi(s)=\sum _i\pi(a_i|s)Q_{\pi}(s,a_i)\)&lt;/code&gt;，也就是说值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V(s)\)&lt;/code&gt;可以理解为该状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;下，所有可能动作所对应的动作值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q_{\pi}(s,a_i)\)&lt;/code&gt;与采取该动作的概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(a_i|s)\)&lt;/code&gt;的乘积之和，相当于是该状态下所有动作的值函数的平均。而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q_{\pi}(s,a)\)&lt;/code&gt;是单个动作所对应的值函数，所以所谓的优势就是当前动作值函数 对于平均值函数的优势。&lt;/p&gt;

&lt;p&gt;然后。。好复杂&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap7 基于策略梯度的强化学习方法</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap7.html"/>
   <updated>2018-04-12T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap7</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#问题定义&quot;&gt;问题定义&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#似然率的角度&quot;&gt;似然率的角度&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#重要性采样的角度&quot;&gt;重要性采样的角度&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#如何求解log梯度&quot;&gt;如何求解log梯度&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#reinforce算法&quot;&gt;REINFORCE算法&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;问题定义&quot;&gt;问题定义&lt;/h2&gt;

&lt;h3 id=&quot;似然率的角度&quot;&gt;似然率的角度&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau\)&lt;/code&gt;表示一组状态-行为序列（轨迹）&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_0,u_0,...,s_H,u_H\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R(\tau) = \sum_{t=0}^{H}R(s_t,u_t)\)&lt;/code&gt;表示这条轨迹的回报&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(\tau;\theta)\)&lt;/code&gt;表示轨迹&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau\)&lt;/code&gt;出现的概率&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;那么，强化学习的&lt;strong&gt;优化目标&lt;/strong&gt;就是长期累积期望回报：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
U(\theta) = E(\sum^{H}_{t=0}R(s_t,u_t);\pi_{\theta})=\sum_{\tau}P(\tau;\theta)R(\tau)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以强化学习就是要找到最优参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\max_{\theta}U(\theta)=\max_{\theta}\sum_{\tau}P(\tau;\theta)R(\tau)\)&lt;/code&gt;，那就可以用&lt;strong&gt;梯度上升&lt;/strong&gt;（因为是&lt;strong&gt;求max&lt;/strong&gt;）来解了。&lt;/p&gt;

&lt;p&gt;关键在于如何对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(U(\theta)\)&lt;/code&gt;求导：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{split}
\\ \triangledown _{\theta}(U_{\theta})&amp;amp;=\triangledown_{\theta}\sum_{\tau}P(\tau;\theta)R(\tau)
\\&amp;amp;=\sum_{\tau}\triangledown_{\theta}P(\tau;\theta)R(\tau)
\\&amp;amp;=\sum_{\tau} \frac{P(\tau;\theta)}{P(\tau;\theta)} \triangledown_{\theta}P(\tau;\theta)R(\tau)
\\&amp;amp;=\sum_{\tau} P(\tau;\theta)\frac{\triangledown_{\theta}P(\tau;\theta)R(\tau)}{P(\tau;\theta)}
\\&amp;amp;=\sum_{\tau} P(\tau;\theta)\triangledown_{\theta}\log P(\tau;\theta)R(\tau)
\end{split}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\triangledown_x\log P(x)=\frac{1}{P(x)}\triangledown_xP(x)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因此，策略梯度最终变成求&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\triangledown_{\theta}\log P(\tau;\theta)\)&lt;/code&gt;的期望。而这期望，可以通过利用当前策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi_{\theta}\)&lt;/code&gt;采样m条轨迹&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau_1,...,\tau_m\)&lt;/code&gt;之后，求平均来近似：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown _{\theta}(U_{\theta})\approx \frac{1}{m}\sum_{i=1}^{m}\triangledown_{\theta}\log P(\tau_i;\theta)R(\tau_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;也从另一个角度来证明，因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R(\tau)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;无关，所以其实也可以这么看：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown_{\theta}\log P(\tau;\theta)=\frac{1}{P(\tau;\theta)}\triangledown_{\theta}P(\tau;\theta)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown_{\theta}P(\tau;\theta)=P(\tau;\theta)\triangledown_{\theta}\log P(\tau;\theta)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{split}
\\ \triangledown _{\theta}(U_{\theta})&amp;amp;=\triangledown_{\theta}\sum_{\tau}P(\tau;\theta)R(\tau)
\\&amp;amp;=\sum_{\tau}[\triangledown_{\theta}P(\tau;\theta)]R(\tau)
\\&amp;amp;=\sum_{\tau} [P(\tau;\theta)\triangledown_{\theta}\log P(\tau;\theta)]R(\tau)
\end{split}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;重要性采样的角度&quot;&gt;重要性采样的角度&lt;/h3&gt;

&lt;p&gt;暂时略&lt;/p&gt;

&lt;h2 id=&quot;如何求解log梯度&quot;&gt;如何求解log梯度&lt;/h2&gt;

&lt;p&gt;上面推导出了策略梯度的公式是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\triangledown _{\theta}(U_{\theta})\approx \frac{1}{m}\sum_{i=1}^{m}\triangledown_{\theta}\log P(\tau_i;\theta)R(\tau_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如何求解&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\triangledown_{\theta}\log P(\tau;\theta)\)&lt;/code&gt;呢？&lt;/p&gt;

&lt;h3 id=&quot;reinforce算法&quot;&gt;REINFORCE算法&lt;/h3&gt;

&lt;p&gt;首先，轨迹&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau=s_0,u_0,...,s_H,u_H\)&lt;/code&gt;，那么：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(\tau;\theta)=P(s_0)\prod ^{H-1}_{i=0}P(s_{i+1}|s_{i},u_{i})\pi_{\theta}(u_i|s_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi_{\theta}(u_i|s_i)\)&lt;/code&gt;是策略，也就是在状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_i\)&lt;/code&gt;下，采用动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;的概率。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap6 基于值函数逼近的强化学习方法</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap6.html"/>
   <updated>2018-04-12T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap6</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-基于值函数逼近的理论讲解&quot;&gt;1. 基于值函数逼近的理论讲解&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-dqn及其变种&quot;&gt;2. DQN及其变种&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-dqn方法&quot;&gt;2.1 DQN方法&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#211-利用深度卷积神经网络逼近行为值函数&quot;&gt;2.1.1 利用深度卷积神经网络逼近行为值函数&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#212-在强化学习的学习过程中引入经验回放&quot;&gt;2.1.2 在强化学习的学习过程中引入经验回放&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#213-设立了独立的目标网络单独处理时间差分算法中的td偏差&quot;&gt;2.1.3 设立了独立的目标网络单独处理时间差分算法中的TD偏差&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#dqn小结by-daiwk&quot;&gt;DQN小结（by daiwk）&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-double-dqn&quot;&gt;2.2 Double DQN&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#221-qlearning的过估计&quot;&gt;2.2.1 Qlearning的过估计&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#23-优先回放prioritized-replay&quot;&gt;2.3 优先回放(Prioritized Replay)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#24-dueling-dqn&quot;&gt;2.4 Dueling DQN&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-函数逼近方法&quot;&gt;3. 函数逼近方法&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-基于非参数的函数逼近&quot;&gt;3.1 基于非参数的函数逼近&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#基于核函数的方法&quot;&gt;基于核函数的方法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#基于高斯过程的函数逼近方法&quot;&gt;基于高斯过程的函数逼近方法&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-基于参数的函数逼近&quot;&gt;3.2 基于参数的函数逼近&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#33-卷积神经网络&quot;&gt;3.3 卷积神经网络&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#331-卷积运算&quot;&gt;3.3.1 卷积运算&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#稀疏连接&quot;&gt;稀疏连接&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#权值共享&quot;&gt;权值共享&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#332-池化&quot;&gt;3.3.2 池化&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-各种其他优化&quot;&gt;4. 各种其他优化&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-基于值函数逼近的理论讲解&quot;&gt;1. 基于值函数逼近的理论讲解&lt;/h2&gt;

&lt;h2 id=&quot;2-dqn及其变种&quot;&gt;2. DQN及其变种&lt;/h2&gt;

&lt;h3 id=&quot;21-dqn方法&quot;&gt;2.1 DQN方法&lt;/h3&gt;

&lt;p&gt;DeepMind发表在Nature上的文章&lt;a href=&quot;https://daiwk.github.io/assets/dqn.pdf&quot;&gt;Human-level control through deep reinforcement learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;最主要创新点是两个：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;经验回放&lt;/li&gt;
  &lt;li&gt;设立单独的目标网络&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;大体框架是复用&lt;strong&gt;传统强化学习&lt;/strong&gt;里的&lt;strong&gt;Qlearning&lt;/strong&gt;方法。Qlearning包括两个关键点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;异策略：&lt;strong&gt;行动策略&lt;/strong&gt;与 &lt;strong&gt;要评估的策略&lt;/strong&gt;不是同一个策略。
    &lt;ul&gt;
      &lt;li&gt;行动策略（用来选择行动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;的策略）是&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon -greedy\)&lt;/code&gt;策略&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;要评估和改进的策略是&lt;strong&gt;贪婪策略&lt;/strong&gt;（即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(max_aQ(s_{t+1},a)\)&lt;/code&gt;，当前状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{t+1}\)&lt;/code&gt;下，使用各种a使&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s_{t+1},a)\)&lt;/code&gt;达到的&lt;strong&gt;最大值&lt;/strong&gt;），即，是最大的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;，与行动策略里随机选一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;得到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;不同&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;时间差分（temporal difference, TD）：使用时间差分目标（即，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t+\gamma max_aQ(s_{t+1},a)\)&lt;/code&gt;）来更新当前的行为值函数&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;注意，时间差分方法其实包括了异策略和同策略两种，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;异策略(即off-policy)&lt;/strong&gt;，如Qlearning，&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;同策略&lt;/strong&gt;如Sarsa强化学习算法 &lt;strong&gt;（行为策略和目标策略都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon \) -greedy&lt;/code&gt;策略）&lt;/strong&gt;(书p77-79)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Qlearning的算法流程如下：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;初始化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s,a),\forall s\in S,a\in A(s)\)&lt;/code&gt;，给定参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha, \gamma\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Repeat
      &lt;ol&gt;
        &lt;li&gt;给定起始状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;，并根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon -greedy\)&lt;/code&gt;策略在状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;选择动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;Repeat
          &lt;ol&gt;
            &lt;li&gt;根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon -greedy\)&lt;/code&gt;策略选择动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;，得到回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t\)&lt;/code&gt;和下一个状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{t+1}\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;使用时间差分方法更新行为值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s_t,a_t)\leftarrow Q(s_t,a_t)+\alpha [r_t+\gamma max_a(Q(s_{t+1},a))-Q(s_t,a_t)]\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s=s'\)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a=a'\)&lt;/code&gt;&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
        &lt;li&gt;Until &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;是最终状态&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li&gt;Until 所有的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s,a)\)&lt;/code&gt;收敛&lt;/li&gt;
    &lt;li&gt;输出最终策略：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi (s)=argmax_aQ(s,a)\)&lt;/code&gt;&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;DQN对Qlearning主要进行了如下三个修改：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;利用深度卷积神经网络逼近行为值函数&lt;/li&gt;
  &lt;li&gt;在强化学习的学习过程中引入经验回放&lt;/li&gt;
  &lt;li&gt;设立了独立的目标网络单独处理时间差分算法中的TD偏差&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;211-利用深度卷积神经网络逼近行为值函数&quot;&gt;2.1.1 利用深度卷积神经网络逼近行为值函数&lt;/h4&gt;

&lt;p&gt;行为值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s,a)\)&lt;/code&gt;用参数为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;的非线性参数逼近&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s,a;\theta)\)&lt;/code&gt;来近似&lt;/p&gt;

&lt;h4 id=&quot;212-在强化学习的学习过程中引入经验回放&quot;&gt;2.1.2 在强化学习的学习过程中引入经验回放&lt;/h4&gt;

&lt;p&gt;其实20世纪90年代就已经有用神经网络逼近行为值函数的方法了，但常常出现&lt;strong&gt;不稳定不收敛&lt;/strong&gt;的情况。&lt;/p&gt;

&lt;p&gt;deepmind的创始人Hassabis是神经科学的博士，他是研究人类大脑中负责记忆和学习的主要部分——海马体的。&lt;strong&gt;在人类睡觉时，海马体会把一天的记忆重放给大脑皮层。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;训练&lt;strong&gt;神经网络&lt;/strong&gt;时，存在的&lt;strong&gt;假设&lt;/strong&gt;是&lt;strong&gt;训练数据是独立同分布&lt;/strong&gt;的，但通过强化学习采集的&lt;strong&gt;数据间存在关联性&lt;/strong&gt;，利用这些数据进行训练，神经网络就不会稳定。而经验回放就可以打破数据间的关联。&lt;/p&gt;

&lt;h4 id=&quot;213-设立了独立的目标网络单独处理时间差分算法中的td偏差&quot;&gt;2.1.3 设立了独立的目标网络单独处理时间差分算法中的TD偏差&lt;/h4&gt;

&lt;p&gt;经验回放的技巧可以打破数据间的关联性，这个技巧在2013年的NIPS已经发布了，而2015年的Nature论文进一步提出了&lt;strong&gt;目标网络&lt;/strong&gt;，&lt;strong&gt;进一步降低数据间的关联性&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;正常的Qlearning更新值函数的方法如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q(s_t,a_t)\leftarrow Q(s_t,a_t)+\alpha [r_t+\gamma max_a(Q(s_{t+1},a))-Q(s_t,a_t)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;但现在使用卷积神经网络来逼近，所以需要更新的是参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，使用的方式自然是梯度下降：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\theta_{t+1}=\theta _t+\alpha [r+\gamma max_{a'}(Q(s',a';\theta))-Q(s',a';\theta)]\nabla Q(s,a;\theta)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r+\gamma max_{a'}(Q(s',a';\theta))\)&lt;/code&gt;是TD目标，在计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(max_{a'}(Q(s',a';\theta))\)&lt;/code&gt;时用到的网络参数为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;在DQN之前，计算TD目标的动作值函数所用的网络参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;与梯度计算中要逼近的动作值函数所用的网络参数相同，这样就容易导致数据间存在关联性，从而使训练不稳定。&lt;/p&gt;

&lt;p&gt;所以DQN的做法就是将TD目标的网络表示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^{-}\)&lt;/code&gt;，用于动作值函数逼近的网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;每一步都更新，而用于计算TD目标的网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^{-}\)&lt;/code&gt;则是每固定的步数才更新一次【这就可以使得在一段时间里目标Q值是保持不变的，从而使得在一段时间里能集中力量去优化真正的目标&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，等真正的目标好一点了，再去更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^-\)&lt;/code&gt;】：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\theta_{t+1}=\theta _t+\alpha [r+\gamma max_{a'}(Q(s',a';\theta ^{-}))-Q(s,a;\theta)]\nabla Q(s,a;\theta)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;DQN的训练算法如下：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;初始化replay memory &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;，capacity设为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;使用随机权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;初始化动作值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;使用权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^{-}=\theta\)&lt;/code&gt;初始化目标动作值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat {Q}\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(episode = [1,...,M]\)&lt;/code&gt; do
      &lt;ol&gt;
        &lt;li&gt;初始化事件的第一个状态(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_1\)&lt;/code&gt;是第一张图片)&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_1=\{x_1\}\)&lt;/code&gt;，并通过预处理得到状态对应的特征输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi _1=\phi(s_1)\)&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t=[1,...T]\)&lt;/code&gt; do
          &lt;ol&gt;
            &lt;li&gt;根据概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon\)&lt;/code&gt;随机选择一个动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;如果小概率事件没有发生，就用贪婪策略选择当前行为值函数最大的那个动作：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t=argmax_a(Q(\phi(s_t),a;\theta))\)&lt;/code&gt;【上面那行和这行就是所谓的行动策略，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon -greedy\)&lt;/code&gt;策略】&lt;/li&gt;
            &lt;li&gt;在模拟器中执行动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;，得到回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t\)&lt;/code&gt;以及图片&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{t+1}\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{t+1}=s_t,a_t,x_{t+1}\)&lt;/code&gt;，然后预处理&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi _{t+1}=\phi(s_{t+1})\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;将transition &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((\phi_t,a_t,r_t,\phi _{t+1})\)&lt;/code&gt;存入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;。&lt;/li&gt;
            &lt;li&gt;从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;中随机sample出一个minibatch的transitions，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((\phi _j,a_j,r_j,\phi _{j+1})\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_j=\left\{\begin{matrix}r_j &amp;amp; if\ episode\ terminates\ at\ step\ j+1\\ r_j+\gamma max_{a'}\hat {Q}(\phi_{j+1},a';\theta^{-}) &amp;amp; otherwise \end{matrix}\right.\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((y_j-Q(\phi _j,a_j;\theta))^2\)&lt;/code&gt;的参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;进行一个梯度下降step的更新，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{t+1}=\theta _t+\alpha [r+\gamma max_{a'}(\hat {Q}(s',a';\theta ^{-}))-Q(s,a;\theta)]\nabla Q(s,a;\theta)\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;每&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;个step，令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat {Q}=Q\)&lt;/code&gt;，即令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^{-}=\theta \)&lt;/code&gt;&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
        &lt;li&gt;End For&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li&gt;End For&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;dqn小结by-daiwk&quot;&gt;DQN小结（by daiwk）&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_i\)&lt;/code&gt;的时候用的是目标网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{Q}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^-\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;loss是目标网络算出来的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r+\gamma max \hat{Q}\)&lt;/code&gt;与原网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;之差&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_i\)&lt;/code&gt;是当前reward，基于状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{t+1}\)&lt;/code&gt;再执行一个action得到的新的reward。而原网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;是当前的状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;总共有这么几大步：
    &lt;ul&gt;
      &lt;li&gt;当前状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;，处理后是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi _t\)&lt;/code&gt;，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon -greedy\)&lt;/code&gt;得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;最大的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi _t, a_t,r_t,\phi_{t+1}\)&lt;/code&gt;，扔进replay buffer中&lt;/li&gt;
      &lt;li&gt;采样出一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi _j,a_j,r_j,\phi_{j+1}\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;计算&lt;strong&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi _{j+1}\)&lt;/code&gt;上&lt;/strong&gt;再执行一个使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat {Q}\)&lt;/code&gt;(注意，这里不是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;)最大的动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a'\)&lt;/code&gt;得到的reward：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_j=r_j+\gamma max \hat {Q}(\phi_{j+1},a')\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_i\)&lt;/code&gt;和当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;的残差，梯度下降，更新当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;的参数：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta += \alpha (y_j-Q)\nabla Q\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;步之后，把当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;的参数赋值给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat {Q}\)&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;再次强调
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(maxQ\)&lt;/code&gt;的是&lt;strong&gt;当前&lt;/strong&gt;状态+&lt;strong&gt;当前&lt;/strong&gt;动作；&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(max\hat {Q}\)&lt;/code&gt;是&lt;strong&gt;下一&lt;/strong&gt;状态+&lt;strong&gt;下一&lt;/strong&gt;动作；&lt;/li&gt;
      &lt;li&gt;用来算残差的也是&lt;strong&gt;当前&lt;/strong&gt;状态+&lt;strong&gt;当前&lt;/strong&gt;动作得到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;22-double-dqn&quot;&gt;2.2 Double DQN&lt;/h3&gt;

&lt;h4 id=&quot;221-qlearning的过估计&quot;&gt;2.2.1 Qlearning的过估计&lt;/h4&gt;

&lt;p&gt;DQN无法克服Qlearning本身固有的缺点：过估计&lt;/p&gt;

&lt;p&gt;过估计指的是&lt;strong&gt;估计的值函数比真实值函数大&lt;/strong&gt;，Qlearning出现过估计的问题，根源在于其中的&lt;strong&gt;最大化操作&lt;/strong&gt;：&lt;/p&gt;

&lt;p&gt;对于表格型，值函数评估的更新公式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q(s_t,a_t)\leftarrow Q(s_t,a_t)+\alpha [r_t+\gamma \underset{a}{max}Q(s_{t+1},a)-Q(s_t,a_t)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;对于基于函数逼近的方法，值函数更新公式为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\theta _{t+1}=\theta_t+\alpha(R_{t+1}+\gamma \underset{a}{max}Q(S_{t+1},a;\theta_t)-Q(S_t,A_t;\theta_t))\triangledown _{\theta_t}Q(S_t,A_t;\theta_t)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可以发现不管是表格型，还是基于值函数逼近的方法，更新公式中都有max操作，使得估计的值函数比值函数的真实值大。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;如果过估计是均匀的&lt;/strong&gt;，即值函数的每一点的值都被过估计了相同的幅度，那么由于最优策略是贪婪策略，即找最在的值函数所对应的动作，在这种情况下一不会影响最优策略（因为每个值函数都变大一样的幅度，所以原来最大的那个还是最大），这样因为强化学习的目标是找到最优策略，所以不会影响我们解决问题~&lt;/p&gt;

&lt;p&gt;但实际情况中，过估计量并不是均匀的，所以会影响最终策略，使得最终策略并非最优！！&lt;/p&gt;

&lt;p&gt;在Qlearning的值函数更新中，TD目标为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Y^Q_t=R_{t+1}+\gamma max_aQ(S_{t+1},a;\theta_t)
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;动作选择&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在求TD目标&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y^Q_t\)&lt;/code&gt;时，首先需要选择一个动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^*\)&lt;/code&gt;，该动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^*\)&lt;/code&gt;应该满足在状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S_{t+1}\)&lt;/code&gt;处使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s_{t+1},a)\)&lt;/code&gt;最大，这就是&lt;strong&gt;动作选择&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;动作评估&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;选出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^*\)&lt;/code&gt;后，利用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^*\)&lt;/code&gt;处的动作值函数构造TD目标。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;一般的Qlearning使用同一个参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_t\)&lt;/code&gt;来选择和评估动作。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Double Qlearning将动作的选择和动作的评估分别用不同的值函数来实现，从而其TD目标为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Y^{DoubleQ}_t=R_{t+1}+\gamma Q(S_{t+1},argmax_aQ(S_{t+1},a;\theta_t);\theta_t')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，我们可以看出，动作的选择所选择的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^*\)&lt;/code&gt;为&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
a^*=argmax_aQ(S_{t+1},s;\theta_t)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;动作值函数网络的参数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_t\)&lt;/code&gt;。当选出最大动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a^*\)&lt;/code&gt;之后，动作评估的公式为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Y^{DoubleQ}_t=R_{t+1}+\gamma Q(S_{t+1},a^*;\theta_t')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，引入DQN就是Double DQN：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1509.06461&quot;&gt;Deep Reinforcement Learning with Double Q-learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;将TD error修改为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
r+\gamma Q(s',argmax_{a'}Q(s',a',\theta),\theta^-)-Q(s,a,\theta)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;是当前的网络参数，用来选择动作&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^-\)&lt;/code&gt;是前一步的网络参数（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(delayed \theta\)&lt;/code&gt;），用来评估动作&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;因为原来的DQN就已经引入了目标网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^-\)&lt;/code&gt;，所以其实改动不大。
但注意！！区别在于，原来DQN的TD error是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r+\gamma Q(s',argmax_{a'}Q(s',a',\theta^-),\theta^-)\)&lt;/code&gt;，即动作选择和动作评估都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^-\)&lt;/code&gt;。而Double DQN动作选择是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，动作评估是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^-\)&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;23-优先回放prioritized-replay&quot;&gt;2.3 优先回放(Prioritized Replay)&lt;/h3&gt;

&lt;h3 id=&quot;24-dueling-dqn&quot;&gt;2.4 Dueling DQN&lt;/h3&gt;

&lt;p&gt;参考书本，以及&lt;a href=&quot;https://blog.csdn.net/u013236946/article/details/73161586&quot;&gt;https://blog.csdn.net/u013236946/article/details/73161586&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;竞争网络（dueling net）从网络结构上改进了DQN，将&lt;strong&gt;动作值函数分解&lt;/strong&gt;为&lt;strong&gt;状态值函数&lt;/strong&gt;和&lt;strong&gt;优势函数&lt;/strong&gt;，即：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q^{\pi}(s,a)=V^{\pi}(s)+A^{\pi}(s,a)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;以往的DQN都是直接用神经网络逼近&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q^{\pi}(s,a)\)&lt;/code&gt;，而Dueling DQN则是对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V^{\pi}(s)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A^{\pi}(s,a)\)&lt;/code&gt;分别用神经网络来逼近。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;状态值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V^{\pi}(s)\)&lt;/code&gt;表示静态的状态环境本身具有的价值。是一个scalar。&lt;/li&gt;
  &lt;li&gt;依赖状态的动作优势函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A^{\pi}(s,a)\)&lt;/code&gt;(advantage function)，表示选择某个Action额外带来的价值。是一个vector。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;更详细地，&lt;strong&gt;状态价值函数&lt;/strong&gt;表示为&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
V(s;\theta,\beta)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;动作优势函数&lt;/strong&gt;表示为&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
A(s,a;\theta,\alpha)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;动作Q值为两者相加&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q(s,a;\theta,\alpha,\beta) = V(s;\theta,\beta)+A(s,a;\theta,\alpha)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;是卷积层的参数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha\)&lt;/code&gt;是两个支路全连接层的参数。&lt;/p&gt;

&lt;p&gt;而在实际中，一般要将动作优势流设置为&lt;strong&gt;单独动作优势&lt;/strong&gt;函数&lt;strong&gt;减去&lt;/strong&gt;某状态下&lt;strong&gt;所有动作优势函数的平均值&lt;/strong&gt;，这样做可以保证该状态下各动作的优势函数&lt;strong&gt;相对排序不变&lt;/strong&gt;，而且可以&lt;strong&gt;缩小Q值的范围&lt;/strong&gt;，去除多余的自由度，提高算法&lt;strong&gt;稳定性&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Q(s,a;\theta,\alpha,\beta) = V(s;\theta,\beta)+(A(s,a;\theta,\alpha)-\frac{1}{|A|}\sum_{a'}A(s,a';\theta,\alpha))
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/dueling-dqn.jpg&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;这种竞争结构能学到在&lt;strong&gt;没有动作的影响&lt;/strong&gt;下&lt;strong&gt;环境状态&lt;/strong&gt;的价值&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V(s)\)&lt;/code&gt;。如下图，在训练过程中，上下两行图表示不同时刻，左右两列表示属于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V(s)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A(a)\)&lt;/code&gt;，（通过某种方法处理后）图中红色区域代表&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V(s)\)&lt;/code&gt; 和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A(a)\)&lt;/code&gt;所关注的地方。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V(s)\)&lt;/code&gt;关注于地平线上&lt;strong&gt;是否有车辆出现&lt;/strong&gt;（此时动作的选择影响不大）以及分数；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A(a)\)&lt;/code&gt;则更关心&lt;strong&gt;会立即造成碰撞的车辆&lt;/strong&gt;，此时动作的选择很重要。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/dueling-dqn-attend.jpeg&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;论文中dueling net结合了DDQN(double dqn)以及优先级采样（Prioritized Experience Replay）的训练方式。&lt;/p&gt;

&lt;h2 id=&quot;3-函数逼近方法&quot;&gt;3. 函数逼近方法&lt;/h2&gt;

&lt;h3 id=&quot;31-基于非参数的函数逼近&quot;&gt;3.1 基于非参数的函数逼近&lt;/h3&gt;

&lt;h4 id=&quot;基于核函数的方法&quot;&gt;基于核函数的方法&lt;/h4&gt;

&lt;h4 id=&quot;基于高斯过程的函数逼近方法&quot;&gt;基于高斯过程的函数逼近方法&lt;/h4&gt;

&lt;h3 id=&quot;32-基于参数的函数逼近&quot;&gt;3.2 基于参数的函数逼近&lt;/h3&gt;

&lt;h3 id=&quot;33-卷积神经网络&quot;&gt;3.3 卷积神经网络&lt;/h3&gt;

&lt;h4 id=&quot;331-卷积运算&quot;&gt;3.3.1 卷积运算&lt;/h4&gt;

&lt;h5 id=&quot;稀疏连接&quot;&gt;稀疏连接&lt;/h5&gt;

&lt;h5 id=&quot;权值共享&quot;&gt;权值共享&lt;/h5&gt;

&lt;h4 id=&quot;332-池化&quot;&gt;3.3.2 池化&lt;/h4&gt;

&lt;h2 id=&quot;4-各种其他优化&quot;&gt;4. 各种其他优化&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/21547911&quot;&gt;https://zhuanlan.zhihu.com/p/21547911&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;几个问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;目标Q值的计算准确吗？全部通过max Q来计算有没有问题？&lt;/li&gt;
  &lt;li&gt;随机采样的方法好吗？按道理不同样本的重要性是不一样的&lt;/li&gt;
  &lt;li&gt;Q值代表状态，动作的价值，那么单独动作价值的评估会不会更准确？&lt;/li&gt;
  &lt;li&gt;DQN中使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon-greedy\)&lt;/code&gt;的方法来探索状态空间，有没有更好的做法？&lt;/li&gt;
  &lt;li&gt;使用卷积神经网络的结构是否有局限？加入RNN呢？&lt;/li&gt;
  &lt;li&gt;DQN无法解决一些高难度的Atari游戏比如Montezuma’s Revenge，如何处理这些游戏？&lt;/li&gt;
  &lt;li&gt;DQN训练时间太慢了，跑一个游戏要好几天，有没有办法更快？&lt;/li&gt;
  &lt;li&gt;DQN训练是单独的，也就是一个游戏弄一个网络进行训练，有没有办法弄一个网络同时掌握多个游戏，或者训练某一个游戏后将知识迁移到新的游戏？&lt;/li&gt;
  &lt;li&gt;DQN能否用在连续动作输出问题？&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;解法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;改进目标Q值计算：&lt;a href=&quot;https://arxiv.org/abs/1509.06461&quot;&gt;Deep Reinforcement Learning with Double Q-learning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;改进随机采样：&lt;a href=&quot;https://arxiv.org/abs/1511.05952&quot;&gt;Prioritized Experience Replay&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;改进网络结构，评估单独动作价值：&lt;a href=&quot;https://arxiv.org/abs/1511.06581&quot;&gt;Dueling Network Architectures for Deep Reinforcement Learning&lt;/a&gt; ( 本文为ICML最佳论文之一）&lt;/li&gt;
  &lt;li&gt;改进探索状态空间方式：（1）&lt;a href=&quot;https://arxiv.org/abs/1602.04621&quot;&gt;Deep Exploration via Bootstrapped DQN&lt;/a&gt; （2）&lt;a href=&quot;https://arxiv.org/abs/1507.00814&quot;&gt;Incentivizing Exploration In Reinforcement Learning With Deep Predictive Models&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;改变网络结构，增加RNN：&lt;a href=&quot;https://arxiv.org/abs/1507.06527&quot;&gt;Deep Recurrent Q-Learning for Partially Observable MDPs&lt;/a&gt;（非DeepMind出品，效果很一般，谈不上改进，不考虑讲解）&lt;/li&gt;
  &lt;li&gt;实现DQN训练的迁移学习：（1）&lt;a href=&quot;https://arxiv.org/abs/1511.06295&quot;&gt;Policy Distillation&lt;/a&gt; （2） &lt;a href=&quot;https://arxiv.org/abs/1511.06342&quot;&gt;Actor-Mimic: Deep Multitask and Transfer Reinforcement Learning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;解决高难度游戏Montezuma‘s Revenge：&lt;a href=&quot;https://arxiv.org/abs/1606.01868&quot;&gt;Unifying Count-Based Exploration and Intrinsic Motivation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;加快DQN训练速度：&lt;a href=&quot;https://arxiv.org/abs/1602.01783&quot;&gt;Asynchronous Methods for Deep Reinforcement Learning&lt;/a&gt;（这篇文章还引出了可以替代DQN的A3C算法，效果4倍Nature DQN）&lt;/li&gt;
  &lt;li&gt;改变DQN使之能够应用在连续控制上面：&lt;a href=&quot;https://arxiv.org/abs/1603.00748&quot;&gt;Continuous Deep Q-Learning with Model-based Acceleration&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>multi-task learning</title>
   <link href="http://daiwk.github.io/posts/dl-mtl.html"/>
   <updated>2018-04-12T00:00:00+00:00</updated>
   <id>/posts/dl-mtl</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e4%b8%a4%e5%a4%a7%e7%b1%bbmtl%e6%96%b9%e6%b3%95&quot;&gt;两大类mtl方法&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#hard%e5%8f%82%e6%95%b0%e5%85%b1%e4%ba%ab&quot;&gt;hard参数共享&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#soft%e5%8f%82%e6%95%b0%e5%85%b1%e4%ba%ab&quot;&gt;soft参数共享&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#mtl%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9awork&quot;&gt;mtl为什么会work&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e9%9a%90%e5%bc%8f%e6%95%b0%e6%8d%ae%e5%a2%9e%e5%bc%ba&quot;&gt;隐式数据增强&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#attention-focusing&quot;&gt;Attention focusing&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#eavesdropping&quot;&gt;Eavesdropping&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#representation-bias&quot;&gt;Representation bias&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#regularization&quot;&gt;Regularization&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e9%9d%9e%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e4%b8%ad%e7%9a%84mtl&quot;&gt;非神经网络中的mtl&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#block-sparse-regularization&quot;&gt;Block-sparse regularization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#learning-task-relationships&quot;&gt;Learning task relationships&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#mtl-deep-learning%e7%9a%84recent-work&quot;&gt;MTL Deep Learning的Recent work&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#deep-relationship-networks&quot;&gt;Deep Relationship Networks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#fully-adaptive-feature-sharing&quot;&gt;Fully-Adaptive Feature Sharing&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cross-stitch-networks&quot;&gt;Cross-stitch Networks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#low-supervision&quot;&gt;Low supervision&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#a-joint-many-task-model&quot;&gt;A Joint Many-Task model&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#weighting-losses-with-uncertainty&quot;&gt;Weighting losses with uncertainty&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tensor-factorisation-for-mtl&quot;&gt;Tensor factorisation for MTL&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sluice-networks&quot;&gt;Sluice Networks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#what-should-i-share-in-my-model&quot;&gt;What should I share in my model?&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#auxiliary-tasks&quot;&gt;Auxiliary tasks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://ruder.io/multi-task/&quot;&gt;http://ruder.io/multi-task/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;阿里天池的一个讲解&lt;a href=&quot;https://tianchi.aliyun.com/course/video?spm=5176.12586971.1001.19.55e2194dAHpQl4&amp;amp;liveId=10562&quot;&gt;【深度学习系列09】Multi-Task Learning for E-commerce&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;两大类mtl方法&quot;&gt;两大类mtl方法&lt;/h2&gt;

&lt;h3 id=&quot;hard参数共享&quot;&gt;hard参数共享&lt;/h3&gt;

&lt;p&gt;所有task共享大的隐层表示，然后每个task有自己的output层。可以极大地减少过拟合，因为大的隐层表示的参数量比每个task自己的小output的参数量要大得多。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/mtl-hard.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;soft参数共享&quot;&gt;soft参数共享&lt;/h3&gt;

&lt;p&gt;每个task有自己的模型和自己的参数。模型参数间的距离一般会使用&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\ell_2\)&lt;/code&gt;正则&lt;/strong&gt;进行正则化，目的是为了让参数间更相似。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/mtl-soft.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;mtl为什么会work&quot;&gt;mtl为什么会work&lt;/h2&gt;

&lt;p&gt;假设task A和task B，有共同的隐层表示F。&lt;/p&gt;

&lt;h3 id=&quot;隐式数据增强&quot;&gt;隐式数据增强&lt;/h3&gt;

&lt;h3 id=&quot;attention-focusing&quot;&gt;Attention focusing&lt;/h3&gt;

&lt;h3 id=&quot;eavesdropping&quot;&gt;Eavesdropping&lt;/h3&gt;

&lt;h3 id=&quot;representation-bias&quot;&gt;Representation bias&lt;/h3&gt;

&lt;h3 id=&quot;regularization&quot;&gt;Regularization&lt;/h3&gt;

&lt;h2 id=&quot;非神经网络中的mtl&quot;&gt;非神经网络中的mtl&lt;/h2&gt;

&lt;h3 id=&quot;block-sparse-regularization&quot;&gt;Block-sparse regularization&lt;/h3&gt;

&lt;h3 id=&quot;learning-task-relationships&quot;&gt;Learning task relationships&lt;/h3&gt;

&lt;h2 id=&quot;mtl-deep-learning的recent-work&quot;&gt;MTL Deep Learning的Recent work&lt;/h2&gt;

&lt;h3 id=&quot;deep-relationship-networks&quot;&gt;Deep Relationship Networks&lt;/h3&gt;

&lt;h3 id=&quot;fully-adaptive-feature-sharing&quot;&gt;Fully-Adaptive Feature Sharing&lt;/h3&gt;

&lt;h3 id=&quot;cross-stitch-networks&quot;&gt;Cross-stitch Networks&lt;/h3&gt;

&lt;h3 id=&quot;low-supervision&quot;&gt;Low supervision&lt;/h3&gt;

&lt;h3 id=&quot;a-joint-many-task-model&quot;&gt;A Joint Many-Task model&lt;/h3&gt;

&lt;h3 id=&quot;weighting-losses-with-uncertainty&quot;&gt;Weighting losses with uncertainty&lt;/h3&gt;

&lt;h3 id=&quot;tensor-factorisation-for-mtl&quot;&gt;Tensor factorisation for MTL&lt;/h3&gt;

&lt;h3 id=&quot;sluice-networks&quot;&gt;Sluice Networks&lt;/h3&gt;

&lt;h3 id=&quot;what-should-i-share-in-my-model&quot;&gt;What should I share in my model?&lt;/h3&gt;

&lt;h2 id=&quot;auxiliary-tasks&quot;&gt;Auxiliary tasks&lt;/h2&gt;
&lt;p&gt;Related task
Adversarial
Hints
Focusing attention
Quantization smoothing
Predicting inputs
Using the future to predict the present
Representation learning
What auxiliary tasks are helpful?
Conclusion&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorflow代码——数据持久化</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-code-data-persistence.html"/>
   <updated>2018-04-07T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-code-data-persistence</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#metagraphdef&quot;&gt;MetaGraphDef&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#metainfodef&quot;&gt;meta_info_def&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#graphdef&quot;&gt;graph_def&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#saverdef&quot;&gt;saver_def&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#collectiondef&quot;&gt;collection_def&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：《TensorFlow实战Google深度学习框架（第2版）》第5章&lt;/p&gt;

&lt;h2 id=&quot;metagraphdef&quot;&gt;MetaGraphDef&lt;/h2&gt;

&lt;p&gt;tf通过&lt;strong&gt;元图（MetaGraph）&lt;/strong&gt;记录计算图中&lt;strong&gt;节点的信息&lt;/strong&gt;及运行计算图中&lt;strong&gt;节点所需的元数据&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/protobuf/meta_graph.proto&lt;/code&gt;中定义了：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MetaGraphDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Meta information regarding the graph to be exported.  To be used by users&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// of this protocol buffer to encode information regarding their meta graph.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MetaInfoDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// User specified Version string. Can be the name of the model and revision,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// steps this model has been trained to, etc.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meta_graph_version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// A copy of the OpDefs used by the producer of this graph_def.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Descriptions and Ops not used in graph_def are stripped out.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;OpList&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stripped_op_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// A serialized protobuf. Can be the time this meta graph is created, or&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// modified, or name of the model.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;protobuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Any&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any_info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User supplied tag(s) on the meta_graph and included graph_def.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// MetaGraphDefs should be tagged with their capabilities or use-cases.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Examples: &quot;train&quot;, &quot;serve&quot;, &quot;gpu&quot;, &quot;tpu&quot;, etc.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// These tags enable loaders to access the MetaGraph(s) appropriate for a&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// specific use-case or runtime environment.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// The __version__ string of the tensorflow build used to write this graph.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// This will be populated by the framework, which will overwrite any user&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// supplied value.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow_version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// The __git_version__ string of the tensorflow build used to write this&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// graph. This will be populated by the framework, which will overwrite any&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// user supplied value.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow_git_version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// A flag to denote whether default-valued attrs have been stripped from&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// the nodes in this graph_def.&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stripped_default_attrs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MetaInfoDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meta_info_def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// GraphDef.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;GraphDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;graph_def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// SaverDef.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;SaverDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;saver_def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// collection_def: Map from collection name to collections.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// See CollectionDef section for details.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CollectionDef&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collection_def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// signature_def: Map from user supplied key for a signature to a single&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// SignatureDef.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SignatureDef&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;signature_def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Asset file def to be used with the defined graph.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AssetFileDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asset_file_def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;保存MetaGraphDef的文件默认以.meta结尾，是二进制文件。tf有export_meta_graph函数，可以以json格式导出MetaGraphDef：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;13.8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;init_op&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ckpt_json_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./demo/model/model.ckpt.meta.json&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;export_meta_graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ckpt_json_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;as_text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ckpt_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./demo/model/model.ckpt&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;init_op&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    
    &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ckpt_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;ttps://daiwk.github.io/assets/tf.saver.demo.meta.json&quot;&gt;https://daiwk.github.io/assets/tf.saver.demo.meta.json&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;meta_info_def&quot;&gt;meta_info_def&lt;/h3&gt;

&lt;p&gt;如上，MetaInfoDef中包含如下信息：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MetaInfoDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// User specified Version string. Can be the name of the model and revision,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// steps this model has been trained to, etc.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;meta_graph_version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// A copy of the OpDefs used by the producer of this graph_def.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Descriptions and Ops not used in graph_def are stripped out.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;OpList&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stripped_op_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// A serialized protobuf. Can be the time this meta graph is created, or&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// modified, or name of the model.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;protobuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Any&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any_info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User supplied tag(s) on the meta_graph and included graph_def.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// MetaGraphDefs should be tagged with their capabilities or use-cases.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Examples: &quot;train&quot;, &quot;serve&quot;, &quot;gpu&quot;, &quot;tpu&quot;, etc.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// These tags enable loaders to access the MetaGraph(s) appropriate for a&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// specific use-case or runtime environment.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// The __version__ string of the tensorflow build used to write this graph.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// This will be populated by the framework, which will overwrite any user&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// supplied value.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow_version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// The __git_version__ string of the tensorflow build used to write this&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// graph. This will be populated by the framework, which will overwrite any&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// user supplied value.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow_git_version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// A flag to denote whether default-valued attrs have been stripped from&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// the nodes in this graph_def.&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stripped_default_attrs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中的meta_graph_version(计算图的版本号)、tags(用户指定的一些标签)，如果没在saver中指定，都默认是空。stripped_op_list属性记录了计算图上使用的所有&lt;strong&gt;运算方法&lt;/strong&gt;的信息。如果某个运算在计算图中出现多次，则其在stripped_op_list中也&lt;strong&gt;只会出现一次&lt;/strong&gt;。stripped_op_list的类型是OpList其定义在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/framework/op_def.proto&lt;/code&gt;中，如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;syntax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cc_enable_arenas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_outer_classname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;OpDefProtos&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_multiple_files&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_package&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;org.tensorflow.framework&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;go_package&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;github.com/tensorflow/tensorflow/tensorflow/go/core/framework&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tensorflow/core/framework/attr_value.proto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tensorflow/core/framework/types.proto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Defines an operation. A NodeDef in a GraphDef specifies an Op by&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// using the &quot;op&quot; field which should match the name of a OpDef.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// LINT.IfChange&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OpDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Op names starting with an underscore are reserved for internal use.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Names should be CamelCase and match the regexp &quot;[A-Z][a-zA-Z0-9_]*&quot;.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// For describing inputs and outputs.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArgDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Name for the input/output.  Should match the regexp &quot;[a-z][a-z0-9_]*&quot;.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Human readable description.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;description&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Describes the type of one or more tensors that are accepted/produced&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// by this input/output arg.  The only legal combinations are:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// * For a single tensor: either the &quot;type&quot; field is set or the&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//   &quot;type_attr&quot; field is set to the name of an attr with type &quot;type&quot;.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// * For a sequence of tensors with the same type: the &quot;number_attr&quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//   field will be set to the name of an attr with type &quot;int&quot;, and&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//   either the &quot;type&quot; or &quot;type_attr&quot; field will be set as for&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//   single tensors.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// * For a sequence of tensors, the &quot;type_list_attr&quot; field will be set&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//   to the name of an attr with type &quot;list(type)&quot;.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;DataType&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type_attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// if specified, attr must have type &quot;type&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number_attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// if specified, attr must have type &quot;int&quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// If specified, attr must have type &quot;list(type)&quot;, and none of&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// type, type_attr, and number_attr may be specified.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type_list_attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// For inputs: if true, the inputs are required to be refs.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//   By default, inputs can be either refs or non-refs.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// For outputs: if true, outputs are refs, otherwise they are not.&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_ref&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Description of the input(s).&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArgDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Description of the output(s).&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArgDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Description of the graph-construction-time configuration of this&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Op.  That is to say, this describes the attr fields that will&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// be specified in the NodeDef.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AttrDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// A descriptive name for the argument.  May be used, e.g. by the&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Python client, as a keyword argument name, and so should match&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// the regexp &quot;[a-z][a-z0-9_]+&quot;.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// One of the type names from attr_value.proto (&quot;string&quot;, &quot;list(string)&quot;,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// &quot;int&quot;, etc.).&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// A reasonable default for this attribute if the user does not supply&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// a value.  If not specified, the user must supply a value.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;AttrValue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default_value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Human-readable description.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;description&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// TODO(josh11b): bool is_optional?&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// --- Constraints ---&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// These constraints are only in effect if specified.  Default is no&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// constraints.&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// For type == &quot;int&quot;, this is a minimum value.  For &quot;list(___)&quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// types, this is the minimum length.&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;has_minimum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;int64&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;minimum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// The set of allowed values.  Has type that is the &quot;list&quot; version&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// of the &quot;type&quot; field above (uses the &quot;list&quot; field of AttrValue).&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// If type == &quot;type&quot; or &quot;list(type)&quot; above, then the &quot;type&quot; field&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// of &quot;allowed_values.list&quot; has the set of allowed DataTypes.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// If type == &quot;string&quot; or &quot;list(string)&quot;, then the &quot;s&quot; field of&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// &quot;allowed_values.list&quot; has the set of allowed strings.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;AttrValue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allowed_values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AttrDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Optional deprecation based on GraphDef versions.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;OpDeprecation&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deprecation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// One-line human-readable description of what the Op does.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Additional, longer human-readable description of what the Op does.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;description&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// -------------------------------------------------------------------------&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Which optimizations this operation can participate in.&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// True if the operation is commutative (&quot;op(a,b) == op(b,a)&quot; for all inputs)&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_commutative&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// If is_aggregate is true, then this operation accepts N &amp;gt;= 2&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// inputs and produces 1 output all of the same type.  Should be&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// associative and commutative, and produce output with the same&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// shape as the input.  The optimizer may replace an aggregate op&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// taking input from multiple devices with a tree of aggregate ops&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// that aggregate locally within each device (and possibly within&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// groups of nearby devices) before communicating.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// TODO(josh11b): Implement that optimization.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_aggregate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// for things like add&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Other optimizations go here, like&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   can_alias_input, rewrite_when_output_unused, partitioning_strategy, etc.&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// -------------------------------------------------------------------------&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Optimization constraints.&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Ops are marked as stateful if their behavior depends on some state beyond&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// their input tensors (e.g. variable reading op) or if they have&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// a side-effect (e.g. printing or asserting ops). Equivalently, stateless ops&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// must always produce the same output for the same input and have&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// no side-effects.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// By default Ops may be moved between devices.  Stateful ops should&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// either not be moved, or should only be moved if that state can also&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// be moved (e.g. via some sort of save / restore).&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Stateful ops are guaranteed to never be optimized away by Common&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Subexpression Elimination (CSE).&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_stateful&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// for things like variables, queue&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// -------------------------------------------------------------------------&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Non-standard options.&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// By default, all inputs to an Op must be initialized Tensors.  Ops&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// that may initialize tensors for the first time should set this&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// field to true, to allow the Op to take an uninitialized Tensor as&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// input.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allows_uninitialized_input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// for Assign, etc.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// LINT.ThenChange(&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//     https://www.tensorflow.org/code/tensorflow/core/framework/op_def_util.cc)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Information about version-dependent deprecation of an op&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OpDeprecation&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// First GraphDef version at which the op is disallowed.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Explanation of why it was deprecated and what to use instead.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;explanation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// A collection of OpDefs&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OpList&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OpDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;例如，如下就是名为Add的运算。两个input_arg，一个output_arg，它们都有type_attr，且值均为T。所以在attr中，必须出现name是T的属性，及其allow_values。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    op &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      name: &lt;span class=&quot;s2&quot;&gt;&quot;Add&quot;&lt;/span&gt;
      input_arg &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        name: &lt;span class=&quot;s2&quot;&gt;&quot;x&quot;&lt;/span&gt;
        type_attr: &lt;span class=&quot;s2&quot;&gt;&quot;T&quot;&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      input_arg &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        name: &lt;span class=&quot;s2&quot;&gt;&quot;y&quot;&lt;/span&gt;
        type_attr: &lt;span class=&quot;s2&quot;&gt;&quot;T&quot;&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      output_arg &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        name: &lt;span class=&quot;s2&quot;&gt;&quot;z&quot;&lt;/span&gt;
        type_attr: &lt;span class=&quot;s2&quot;&gt;&quot;T&quot;&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        name: &lt;span class=&quot;s2&quot;&gt;&quot;T&quot;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;
        allowed_values &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          list &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_BFLOAT16
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_HALF
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_FLOAT
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_DOUBLE
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_UINT8
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_INT8
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_INT16
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_INT32
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_INT64
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_COMPLEX64
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_COMPLEX128
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_STRING
          &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;另外，在meta_info_def中，还有如下记录生成当前计算图的tensorflow版本的属性：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;nl&quot;&gt;tensorflow_version:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;1.9.0&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;tensorflow_git_version&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;v1.9.0-0-g25c197e023&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;graph_def&quot;&gt;graph_def&lt;/h3&gt;

&lt;p&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/framework/graph.proto&lt;/code&gt;中定义了GraphDef，用于记录计算图上的节点信息。每个节点对应一个运算。在meta_info_def中已包含了所有运算的信息，所以graph_def&lt;strong&gt;只关注运算的连接结构&lt;/strong&gt;。GraphDef中的versions比较简单，主要存储tf的版本号，主要信息都在NodeDef类型的node中。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;syntax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cc_enable_arenas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_outer_classname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;GraphProtos&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_multiple_files&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_package&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;org.tensorflow.framework&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;go_package&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;github.com/tensorflow/tensorflow/tensorflow/go/core/framework&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tensorflow/core/framework/node_def.proto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tensorflow/core/framework/function.proto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tensorflow/core/framework/versions.proto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Represents the graph of operations&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GraphDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NodeDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Compatibility versions of the graph.  See core/public/version.h for version&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// history.  The GraphDef version is distinct from the TensorFlow version, and&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// each release of TensorFlow will support a range of GraphDef versions.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;VersionDef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;versions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Deprecated single version field; use versions above instead.  Since all&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// GraphDef changes before &quot;versions&quot; was introduced were forward&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// compatible, this field is entirely ignored.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deprecated&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// EXPERIMENTAL. DO NOT USE OR DEPEND ON THIS YET.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// &quot;library&quot; provides user-defined functions.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Naming:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   * library.function.name are in a flat namespace.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     NOTE: We may need to change it to be hierarchical to support&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     different orgs. E.g.,&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     { &quot;/google/nn&quot;, { ... }},&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     { &quot;/google/vision&quot;, { ... }}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     { &quot;/org_foo/module_bar&quot;, { ... }}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     map&amp;lt;string, FunctionDefLib&amp;gt; named_lib;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   * If node[i].op is the name of one function in &quot;library&quot;,&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     node[i] is deemed as a function call. Otherwise, node[i].op&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     must be a primitive operation supported by the runtime.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Function call semantics:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   * The callee may start execution as soon as some of its inputs&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     are ready. The caller may want to use Tuple() mechanism to&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     ensure all inputs are ready in the same time.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   * The consumer of return values may start executing as soon as&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     the return values the consumer depends on are ready.  The&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     consumer may want to use Tuple() mechanism to ensure the&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     consumer does not start until all return values of the callee&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     function are ready.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;FunctionDefLibrary&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;library&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中的NodeDef在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/framework/node_def.proto&lt;/code&gt;中定义如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;syntax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cc_enable_arenas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_outer_classname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;NodeProto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_multiple_files&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_package&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;org.tensorflow.framework&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;go_package&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;github.com/tensorflow/tensorflow/tensorflow/go/core/framework&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tensorflow/core/framework/attr_value.proto&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NodeDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// The name given to this operator. Used for naming inputs,&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// logging, visualization, etc.  Unique within a single GraphDef.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Must match the regexp &quot;[A-Za-z0-9.][A-Za-z0-9_./]*&quot;.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// The operation name.  There may be custom parameters in attrs.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Op names starting with an underscore are reserved for internal use.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Each input is &quot;node:src_output&quot; with &quot;node&quot; being a string name and&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// &quot;src_output&quot; indicating which output tensor to use from &quot;node&quot;. If&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// &quot;src_output&quot; is 0 the &quot;:0&quot; suffix can be omitted.  Regular inputs&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// may optionally be followed by control inputs that have the format&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// &quot;^node&quot;.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// A (possibly partial) specification for the device on which this&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// node should be placed.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// The expected syntax for this string is as follows:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// DEVICE_SPEC ::= PARTIAL_SPEC&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// PARTIAL_SPEC ::= (&quot;/&quot; CONSTRAINT) *&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// CONSTRAINT ::= (&quot;job:&quot; JOB_NAME)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//              | (&quot;replica:&quot; [1-9][0-9]*)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//              | (&quot;task:&quot; [1-9][0-9]*)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//              | (&quot;device:&quot; [A-Za-z]* &quot;:&quot; ([1-9][0-9]* | &quot;*&quot;) )&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Valid values for this string include:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// * &quot;/job:worker/replica:0/task:1/device:GPU:3&quot;  (full specification)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// * &quot;/job:worker/device:GPU:3&quot;                   (partial specification)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// * &quot;&quot;                                    (no specification)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// If the constraints do not resolve to a single device (or if this&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// field is empty or not present), the runtime will attempt to&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// choose a device automatically.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Operation-specific graph-construction-time configuration.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Note that this should include all attrs defined in the&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// corresponding OpDef, including those with a value matching&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// the default -- this allows the default to change and makes&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// NodeDefs easier to interpret on their own.  However, if&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// an attr with a default is not specified in this list, the&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// default will be used.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// The &quot;names&quot; (keys) must match the regexp &quot;[a-z][a-z0-9_]+&quot; (and&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// one of the names from the corresponding OpDef's attr field).&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// The values must have a type matching the corresponding OpDef&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// attr's type field.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// TODO(josh11b): Add some examples here showing best practices.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AttrValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;name是节点名称，是一个节点的唯一标识符。tf中可以通过节点名称来获取相应的节点。&lt;/li&gt;
  &lt;li&gt;op属性给出了该节点使用的tf运算方法的名称，通过此名称可以在计算图元图的meta_info_def中找到该运算的具体信息。&lt;/li&gt;
  &lt;li&gt;input属性中每个字符串的取值格式为node:src_output
    &lt;ul&gt;
      &lt;li&gt;node部分给出一个节点的名称&lt;/li&gt;
      &lt;li&gt;src_output部分表明这个输入是指定节点的&lt;strong&gt;第几个输出&lt;/strong&gt;。src_output为0时可以省略，即node:0可以记为node。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;device属性指定了处理这个运算的设备。设备可以是本地cpu/gpu，也可以是远程的cpu/gpu。当device为空时，tf在运行时会选一个最合适的设备来运行此运算。&lt;/li&gt;
  &lt;li&gt;attr指定了和当前运算相关的配置信息。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;graph_def &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
...
  node &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    name: &lt;span class=&quot;s2&quot;&gt;&quot;Variable_1&quot;&lt;/span&gt;
    op: &lt;span class=&quot;s2&quot;&gt;&quot;VariableV2&quot;&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;_output_shapes&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        list &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          shape &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            dim &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
              size: 1
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;container&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        s: &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;dtype&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_FLOAT
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;shape&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        shape &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          dim &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            size: 1
          &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;shared_name&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        s: &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
...
  node &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    name: &lt;span class=&quot;s2&quot;&gt;&quot;add&quot;&lt;/span&gt;
    op: &lt;span class=&quot;s2&quot;&gt;&quot;Add&quot;&lt;/span&gt;
    input: &lt;span class=&quot;s2&quot;&gt;&quot;Variable/read&quot;&lt;/span&gt;
    input: &lt;span class=&quot;s2&quot;&gt;&quot;Variable_1/read&quot;&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;T&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_FLOAT
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;_output_shapes&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        list &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          shape &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            dim &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
              size: 1
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
...
  node &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    name: &lt;span class=&quot;s2&quot;&gt;&quot;save/control_dependency&quot;&lt;/span&gt;
    op: &lt;span class=&quot;s2&quot;&gt;&quot;Identity&quot;&lt;/span&gt;
    input: &lt;span class=&quot;s2&quot;&gt;&quot;save/Const&quot;&lt;/span&gt;
    input: &lt;span class=&quot;s2&quot;&gt;&quot;^save/SaveV2&quot;&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;T&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: DT_STRING
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;_class&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        list &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          s: &lt;span class=&quot;s2&quot;&gt;&quot;loc:@save/Const&quot;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    attr &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      key: &lt;span class=&quot;s2&quot;&gt;&quot;_output_shapes&quot;&lt;/span&gt;
      value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        list &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          shape &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
...
  versions &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    producer: 26
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;主要有以下几种节点：&lt;/p&gt;

&lt;p&gt;节点1：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;变量定义的运算：名称为Variable_1，&lt;/li&gt;
  &lt;li&gt;运算方法名：VariableV2&lt;/li&gt;
  &lt;li&gt;与运算相关的属性，例如Variable_1中定义了dtype、shape等&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;节点2：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;定义的运算：名称为add&lt;/li&gt;
  &lt;li&gt;运算方法名：Add&lt;/li&gt;
  &lt;li&gt;输入：Variable/read和Variable_1/read。因为Variable_1的值是Variable_1/read的第一个输出，所以:0可以省略&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;节点3：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;名称：save/control_dependency&lt;/li&gt;
  &lt;li&gt;在系统完成模型持久化过程中自动生成的一个运算&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;最后，versions是生成文件时使用的tf版本号&lt;/p&gt;

&lt;h3 id=&quot;saver_def&quot;&gt;saver_def&lt;/h3&gt;

&lt;p&gt;记录了持久化模型时需要用到的一些参数 ，例如保存到文件的文件名、保存操作和加载操作的名称、保存频率、清理历史记录等。在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/protobuf/saver.proto&lt;/code&gt;中定义如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;syntax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cc_enable_arenas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_outer_classname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;SaverProtos&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_multiple_files&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;java_package&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;org.tensorflow.util&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;go_package&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;github.com/tensorflow/tensorflow/tensorflow/go/core/protobuf&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Protocol buffer representing the configuration of a Saver.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SaverDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// The name of the tensor in which to specify the filename when saving or&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// restoring a model checkpoint.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename_tensor_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// The operation to run when saving a model checkpoint.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;save_tensor_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// The operation to run when restoring a model checkpoint.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;restore_op_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Maximum number of checkpoints to keep.  If 0, no checkpoints are deleted.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_to_keep&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Shard the save files, one per device that has Variable nodes.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sharded&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// How often to keep an additional checkpoint. If not specified, only the last&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// &quot;max_to_keep&quot; checkpoints are kept; if specified, in addition to keeping&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// the last &quot;max_to_keep&quot; checkpoints, an additional checkpoint will be kept&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// for every n hours of training.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_checkpoint_every_n_hours&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// A version number that identifies a different on-disk checkpoint format.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Usually, each subclass of BaseSaverBuilder works with a particular&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// version/format.  However, it is possible that the same builder may be&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// upgraded to support a newer checkpoint format in the future.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CheckpointFormatVersion&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Internal legacy format.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LEGACY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Deprecated format: tf.Saver() which works with tensorflow::table::Table.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;V1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Current format: more efficient.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;V2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;CheckpointFormatVersion&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;saver_def &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  filename_tensor_name: &lt;span class=&quot;s2&quot;&gt;&quot;save/Const:0&quot;&lt;/span&gt;
  save_tensor_name: &lt;span class=&quot;s2&quot;&gt;&quot;save/control_dependency:0&quot;&lt;/span&gt;
  restore_op_name: &lt;span class=&quot;s2&quot;&gt;&quot;save/restore_all&quot;&lt;/span&gt;
  max_to_keep: 5
  keep_checkpoint_every_n_hours: 10000.0
  version: V2
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;filename_tensor_name给出了保存文件名的张量名称，此张量是节点save/Const的第一个输出&lt;/li&gt;
  &lt;li&gt;save_tensor_name：给出了持久化模型的运算所对应的节点名称&lt;/li&gt;
  &lt;li&gt;restore_op_name：持久化模型对应的加载模型的运算名称&lt;/li&gt;
  &lt;li&gt;max_to_keep：设定tf.train.Saver类清理之前保存模型的策略。设为n时，第n+1次调用saver.save时，第一次保存的模型就会自动删掉&lt;/li&gt;
  &lt;li&gt;keep_checkpoint_every_n_hours：同样设定tf.train.Saver类清理之前保存模型的策略。设为n时，表示每n小时可以在max_to_keep的基础上多保存一个模型。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;collection_def&quot;&gt;collection_def&lt;/h3&gt;

&lt;p&gt;在计算图（tf.Graph）中可以维护不同集合，而维护这些集合的底层实现就是collection_def这个属性。这是一个&lt;strong&gt;从集合名称到集合内容的映射&lt;/strong&gt;，集合名称是字符串，集合内容是CollectionDef，定义在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/protobuf/meta_graph.proto&lt;/code&gt;中：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CollectionDef&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// NodeList is used for collecting nodes in graph. For example&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// collection_def {&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   key: &quot;summaries&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   value {&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     node_list {&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//       value: &quot;input_producer/ScalarSummary:0&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//       value: &quot;shuffle_batch/ScalarSummary:0&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//       value: &quot;ImageSummary:0&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     }&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   }&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NodeList&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// BytesList is used for collecting strings and serialized protobufs. For&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// example:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// collection_def {&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   key: &quot;trainable_variables&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   value {&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     bytes_list {&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//       value: &quot;\n\017conv1/weights:0\022\024conv1/weights/Assign&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//              \032\024conv1/weights/read:0&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//       value: &quot;\n\016conv1/biases:0\022\023conv1/biases/Assign\032&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//              \023conv1/biases/read:0&quot;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//     }&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//   }&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// }&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BytesList&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Int64List is used for collecting int, int64 and long values.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Int64List&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;int64&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;packed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// FloatList is used for collecting float values.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FloatList&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;packed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// AnyList is used for collecting Any protos.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AnyList&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;protobuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Any&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;oneof&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;NodeList&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BytesList&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytes_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Int64List&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;int64_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;FloatList&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;float_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;AnyList&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;any_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可见，计算图上主要维护4类不同的集合：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;NodeList用于维护计算图上节点的集合&lt;/li&gt;
  &lt;li&gt;BytesList可以维护字符串或序列化之后的pb的集合&lt;/li&gt;
  &lt;li&gt;Int64List用于维护整数集合&lt;/li&gt;
  &lt;li&gt;FloatList用于维护实数集合&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;collection_def &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  key: &lt;span class=&quot;s2&quot;&gt;&quot;trainable_variables&quot;&lt;/span&gt;
  value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    bytes_list &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      value: &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Variable:0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;17Variable/Assign&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;17Variable/read:02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04v1:08&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&quot;&lt;/span&gt;
      value: &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;14Variable_1:0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;21Variable_1/Assign&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;21Variable_1/read:02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04v2:08&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
collection_def &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  key: &lt;span class=&quot;s2&quot;&gt;&quot;variables&quot;&lt;/span&gt;
  value &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    bytes_list &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      value: &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Variable:0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;17Variable/Assign&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;17Variable/read:02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04v1:08&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&quot;&lt;/span&gt;
      value: &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;14Variable_1:0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;21Variable_1/Assign&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;21Variable_1/read:02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;04v2:08&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;01&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可见，维护了两个集合，一个是所有变量的集合『variables』，另一个是可训练变量的集合『trainable_variables』。&lt;/p&gt;

&lt;p&gt;持久化得到以下5个文件：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;demo/model
├── checkpoint
├── model.ckpt.data-00000-of-00001
├── model.ckpt.index
├── model.ckpt.meta
└── model.ckpt.meta.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面讲到的都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model.ckpt.meta&lt;/code&gt;的明文文件即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model.ckpt.meta.json&lt;/code&gt;的内容，持久化了计算图的结构。&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model.ckpt.index&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model.ckpt.data-xxxx-of-xxxx&lt;/code&gt;文件保存了&lt;strong&gt;变量的取值&lt;/strong&gt;，其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model.ckpt.data-xxxx-of-xxxx&lt;/code&gt;是用SSTable格式存储的，大致理解为一个kv列表。可以通过tf.train.NewCheckpointReader类来查看保存的变量：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ckpt_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./demo/model/model.ckpt&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NewCheckpointReader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ckpt_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;global_variables&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable_to_shape_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;variable_name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;global_variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Value for variable v1 is &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;v1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 输出：
&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'''
v2
[1]
v1
[1]
Value for variable v1 is  [1.]
'''&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;checkpoint&lt;/code&gt;文件维护了一个由tf.train.Saver类持久化的所有tf模型文件的文件名。当某个文件被删除时，此模型对应的文件名也会从checkpoint文件中删除。checkpoint文件的格式为CheckpointState，定义在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/python/training/checkpoint_state.proto&lt;/code&gt;中：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;syntax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensorflow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cc_enable_arenas&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Protocol buffer representing the checkpoint state.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CheckpointState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Path to the most-recent model checkpoint.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_checkpoint_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Paths to all not-yet-deleted model checkpoints, sorted from oldest to&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// newest.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Note that the value of model_checkpoint_path should be the last item in&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// this list.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_model_checkpoint_paths&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Unix timestamps corresponding to all_model_checkpoint_paths, indicating&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// when each checkpoint was created.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;repeated&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_model_checkpoint_timestamps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Unix timestamp indicating the creation time for the last preserved&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// checkpoint.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_preserved_timestamp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;文件内容为：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;model_checkpoint_path: &lt;span class=&quot;s2&quot;&gt;&quot;model.ckpt&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;model.ckpt&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;model_checkpoint_path属性保存了最新模型文件的文件名&lt;/li&gt;
  &lt;li&gt;all_model_checkpoint_paths列出了当前还没有被删除的所有模型文件的文件名&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果有多个checkpoint，则文件内容如下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;model_checkpoint_path: &lt;span class=&quot;s2&quot;&gt;&quot;model.ckpt-29001&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;model.ckpt-25001&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;model.ckpt-26001&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;model.ckpt-27001&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;model.ckpt-28001&quot;&lt;/span&gt;
all_model_checkpoint_paths: &lt;span class=&quot;s2&quot;&gt;&quot;model.ckpt-29001&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorflow代码解析——概览</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-code-analysis-overview.html"/>
   <updated>2018-04-07T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-code-analysis-overview</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E7%AE%80%E4%BB%8B&quot;&gt;简介&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%80%BB%E4%BD%93%E7%BB%93%E6%9E%84&quot;&gt;总体结构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E4%BB%A3%E7%A0%81%E7%BB%93%E6%9E%84&quot;&gt;代码结构&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#tensorflowcore&quot;&gt;tensorflow/core&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tensorflowstreamexecutor&quot;&gt;tensorflow/stream_executor&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tensorflowcontrib&quot;&gt;tensorflow/contrib&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tensroflowpython&quot;&gt;tensroflow/python&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#thirdparty&quot;&gt;third_party&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tf%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5&quot;&gt;tf核心概念&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tensor&quot;&gt;Tensor&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#tensor-contraction&quot;&gt;tensor contraction&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tensor%E5%AE%9E%E7%8E%B0&quot;&gt;Tensor实现&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#tensor%E7%9A%84%E4%B8%A4%E4%B8%AA%E4%B8%BB%E8%A6%81%E7%9A%84%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F&quot;&gt;Tensor的两个主要的成员变量&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#tensor%E7%9A%84%E4%B8%BB%E8%A6%81%E5%87%BD%E6%95%B0&quot;&gt;Tensor的主要函数&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#eigentensor&quot;&gt;Eigen::Tensor&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E7%AC%A6%E5%8F%B7%E7%BC%96%E7%A8%8B&quot;&gt;符号编程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%A2%AF%E5%BA%A6%E8%AE%A1%E7%AE%97&quot;&gt;梯度计算&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%8E%A7%E5%88%B6%E6%B5%81&quot;&gt;控制流&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;强行置顶！！！！&lt;/strong&gt;&lt;a href=&quot;https://github.com/horance-liu/tensorflow-internals&quot;&gt;https://github.com/horance-liu/tensorflow-internals&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://public.kevinrobinsonblog.com/docs/A%20tour%20through%20the%20TensorFlow%20codebase%20-%20v4.pdf&quot;&gt;A tour through the TensorFlow codebase&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25646408&quot;&gt;『深度长文』Tensorflow代码解析（一）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25927956&quot;&gt;『深度长文』Tensorflow代码解析（二）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25929909&quot;&gt;『深度长文』Tensorflow代码解析（三）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25932160&quot;&gt;『深度长文』Tensorflow代码解析（四）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/26031658&quot;&gt;『深度长文』Tensorflow代码解析（五）&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;h3 id=&quot;总体结构&quot;&gt;总体结构&lt;/h3&gt;

&lt;p&gt;从底向上分为&lt;strong&gt;设备管理&lt;/strong&gt;和&lt;strong&gt;通信&lt;/strong&gt;层、&lt;strong&gt;数据操作&lt;/strong&gt;层、&lt;strong&gt;图计算&lt;/strong&gt;层、&lt;strong&gt;API接口&lt;/strong&gt;层、&lt;strong&gt;应用&lt;/strong&gt;层。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;底层设备通信层负责网络通信和设备管理。
    &lt;ul&gt;
      &lt;li&gt;设备管理可以实现TF&lt;strong&gt;设备异构&lt;/strong&gt;的特性，支持CPU、GPU、Mobile等不同设备。&lt;/li&gt;
      &lt;li&gt;网络通信依赖&lt;strong&gt;gRPC&lt;/strong&gt;通信协议实现不同设备间的数据传输和更新。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;数据操作层是Tensor的&lt;strong&gt;OpKernels&lt;/strong&gt;实现。这些OpKernels&lt;strong&gt;以Tensor为处理对象&lt;/strong&gt;，依赖&lt;strong&gt;网络通信和设备内存分配&lt;/strong&gt;，实现了各种Tensor操作或计算。Opkernels不仅包含MatMul等&lt;strong&gt;计算操作&lt;/strong&gt;，还包含Queue等&lt;strong&gt;非计算操作&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;图计算层（Graph），包含&lt;strong&gt;本地计算流图&lt;/strong&gt;和&lt;strong&gt;分布式计算流图&lt;/strong&gt;的实现。Graph模块包含Graph的&lt;strong&gt;创建&lt;/strong&gt;、&lt;strong&gt;编译&lt;/strong&gt;、&lt;strong&gt;优化&lt;/strong&gt;和&lt;strong&gt;执行&lt;/strong&gt;等部分，Graph中&lt;strong&gt;每个节点都是OpKernels类型表示&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;API接口层。&lt;strong&gt;Tensor C API&lt;/strong&gt;是对TF功能模块的接口封装，便于其他语言平台调用。&lt;/li&gt;
  &lt;li&gt;应用层。不同编程语言在应用层通过API接口层调用TF核心功能实现相关实验和应用。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;代码结构&quot;&gt;代码结构&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;以2018.09.23的master为基准&lt;/strong&gt;:&lt;/p&gt;

&lt;h4 id=&quot;tensorflowcore&quot;&gt;tensorflow/core&lt;/h4&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core&lt;/code&gt;目录包含了TF核心模块代码：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public&lt;/code&gt;: API接口头文件目录，用于外部接口调用的API定义，主要是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session.h&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;client&lt;/code&gt;: API接口实现文件目录。（目前已经没有这个目录了…）&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;platform&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OS系统&lt;/code&gt;相关&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;接口文件&lt;/code&gt;，如file system, env等。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;protobuf&lt;/code&gt;: 均为.proto文件，用于数据传输时的结构序列化。（都是proto3的语法）&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;common_runtime&lt;/code&gt;: 公共运行库，包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;executor&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;threadpool&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rendezvous&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memory管理&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;设备分配算法&lt;/code&gt;等。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;distributed_runtime&lt;/code&gt;: 分布式执行模块，如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpc session&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpc master&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rpc worker&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;graph manager&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;framework&lt;/code&gt;: 包含基础功能模块，如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;memory&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensor&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;graph&lt;/code&gt;: &lt;strong&gt;计算流图&lt;/strong&gt;相关操作，如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;construct&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;partition&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;optimize&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;execute&lt;/code&gt;等&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kernels&lt;/code&gt;: 核心Op，如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;matmul&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;conv2d&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;argmax&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;batch_norm&lt;/code&gt;等&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib&lt;/code&gt;: 公共基础库，如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gif&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gtl(google模板库)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hash&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;histogram&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jpeg&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;png&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wav&lt;/code&gt;等。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ops&lt;/code&gt;: &lt;strong&gt;基本&lt;/strong&gt;ops运算(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xxx_ops.cc&lt;/code&gt;)，ops&lt;strong&gt;梯度&lt;/strong&gt;运算（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xxx_grad.cc&lt;/code&gt;），&lt;strong&gt;io相关的ops&lt;/strong&gt;&lt;em&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;io_ops.cc&lt;/code&gt;），&lt;/em&gt;&lt;strong&gt;控制流和数据流&lt;/strong&gt;*操作（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;control_flow_ops.cc&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data_flow_ops.cc&lt;/code&gt;）&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;tensorflowstream_executor&quot;&gt;tensorflow/stream_executor&lt;/h4&gt;

&lt;p&gt;tensorflow/stream_executor目录是并行计算框架，由google stream executor团队开发。&lt;/p&gt;

&lt;h4 id=&quot;tensorflowcontrib&quot;&gt;tensorflow/contrib&lt;/h4&gt;

&lt;p&gt;tensorflow/contrib目录是contributor开发目录。&lt;/p&gt;

&lt;h4 id=&quot;tensroflowpython&quot;&gt;tensroflow/python&lt;/h4&gt;

&lt;p&gt;tensroflow/python目录是python API客户端脚本&lt;/p&gt;

&lt;h4 id=&quot;third_party&quot;&gt;third_party&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;eigen3：eigen矩阵运算库，tf基础ops调用&lt;/li&gt;
  &lt;li&gt;gpus: 封装了cuda/cudnn编程库&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tf核心概念&quot;&gt;tf核心概念&lt;/h2&gt;

&lt;p&gt;tf的核心是围绕Graph展开的，简而言之，就是&lt;strong&gt;Tensor沿着Graph传递闭包完成Flow&lt;/strong&gt;的过程。&lt;/p&gt;

&lt;h3 id=&quot;tensor&quot;&gt;Tensor&lt;/h3&gt;

&lt;p&gt;Matrix表示二维线性映射，Tensor表示多维线性映射。TF中Tensor的维数描述为阶，数值是0阶，向量是1阶，矩阵是2阶，以此类推，可以表示n阶高维数据。&lt;/p&gt;

&lt;h4 id=&quot;tensor-contraction&quot;&gt;tensor contraction&lt;/h4&gt;

&lt;p&gt;matrix的product和tensor的contract运算如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/matrix_tensor.jpg&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;可见，一个4x2的A与一个2x1的B矩阵相乘，可以变成两个矩阵相加，每一个是一个1x1与一个4x1相乘，就是A矩阵的两列分别与B的两列分别相乘，再相加&lt;/p&gt;

&lt;p&gt;contract是tensor的运算，python实现可以看：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/tensorflow/python/ops/math_ops.py&lt;/code&gt;，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensordot&lt;/code&gt;（也称为张量收缩）对从a和b所指定的索引a_axes和b_axes&lt;strong&gt;的元素的乘积&lt;/strong&gt;进行&lt;strong&gt;求和&lt;/strong&gt;。a_axes和b_axes是两个数组，指定沿其收缩张量的那些轴对。对于所有range(0, len(a_axes))中的i，a的轴a_axes[i]必须与b的轴b_axes[i]具有相同的维度。列表a_axes和b_axes必须具有相同的长度，并由唯一的整数组成，用于为每个张量指定有效的坐标轴。&lt;/p&gt;

&lt;p&gt;该操作对应于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;numpy.tensordot(a, b, axes)&lt;/code&gt;，&lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.tensordot.html&quot;&gt;numpy.tensordot文档&lt;/a&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;示例1：当a和b是矩阵（2阶）时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;axes = 1&lt;/code&gt;相当于矩阵乘法。&lt;/li&gt;
  &lt;li&gt;示例2：当a和b是矩阵（2阶）时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;axes = [[1], [0]]&lt;/code&gt;相当于矩阵乘法。&lt;/li&gt;
  &lt;li&gt;示例3：假设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_ {ijk}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(b_ {lmn}\)&lt;/code&gt;表示3阶的两个张量。那么，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contract(a, b, [[0], [2]])&lt;/code&gt;是4阶张量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_ {jklm}\)&lt;/code&gt;，其条目对应于索引&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((j,k,l,m)\)&lt;/code&gt;由下式给出：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
c_{jklm} = \sum_i a_{ijk} b_{lmi}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可见，因为传入的是[[0],[2]]，所以ijk的第0维，即i，和lmn的第2维，即n，都变成了i，然后求和~&lt;/p&gt;

&lt;p&gt;一般来说，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;order(c) = order(a) + order(b) - 2*len(axes[0])&lt;/code&gt;。&lt;/p&gt;

&lt;h4 id=&quot;tensor实现&quot;&gt;Tensor实现&lt;/h4&gt;

&lt;p&gt;Tensor在高维空间数学运算比Matrix计算复杂，计算量也非常大，&lt;strong&gt;加速张量并行运算&lt;/strong&gt;是TF优先考虑的问题，如&lt;strong&gt;add&lt;/strong&gt;, &lt;strong&gt;contract&lt;/strong&gt;, &lt;strong&gt;slice&lt;/strong&gt;, &lt;strong&gt;reshape&lt;/strong&gt;, &lt;strong&gt;reduce&lt;/strong&gt;, &lt;strong&gt;shuffle&lt;/strong&gt;等运算。&lt;/p&gt;

&lt;p&gt;TF中Tensor支持的数据类型有很多，如&lt;strong&gt;tf.float16&lt;/strong&gt;, &lt;strong&gt;tf.float32&lt;/strong&gt;, &lt;strong&gt;tf.float64&lt;/strong&gt;, &lt;strong&gt;tf.uint8&lt;/strong&gt;, &lt;strong&gt;tf.int8&lt;/strong&gt;, &lt;strong&gt;tf.int16&lt;/strong&gt;, &lt;strong&gt;tf.int32&lt;/strong&gt;, &lt;strong&gt;tf.int64&lt;/strong&gt;, &lt;strong&gt;tf.string&lt;/strong&gt;, &lt;strong&gt;tf.bool&lt;/strong&gt;, &lt;strong&gt;tf.complex64&lt;/strong&gt;等，所有Tensor运算都使用&lt;em&gt;*泛化的数据类型&lt;/em&gt;`*(可以重载*和+运算咯)表示。&lt;/p&gt;

&lt;p&gt;Tensor定义和运算主要是&lt;strong&gt;调用Eigen&lt;/strong&gt;矩阵计算库完成的。&lt;/p&gt;

&lt;p&gt;Tensor的定义在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/framework/tensor.h&lt;/code&gt;中。&lt;/p&gt;

&lt;h5 id=&quot;tensor的两个主要的成员变量&quot;&gt;Tensor的两个主要的成员变量&lt;/h5&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;TensorShape&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TensorBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf_code_tensorshape.png&quot; style=&quot;max-height: 200px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TensorShape&lt;/code&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/framework/tensor_shape.h&lt;/code&gt;中定义，基类是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TensorShapeBase&lt;/code&gt;：&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TensorShape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorShapeBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorShape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;TensorShapeBase如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;/// Represents the shape of a Tensor.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;///&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// A tensor's shape is denoted by its number of dimensions and a size for each&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// dimension.  For example, a Tensor represented by a 3 x 4 matrix would have&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// a shape of 2-D, [3,4].&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;///&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// If you know the exact shape of your Tensor when you create the TensorShape&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// object, you can specify it then, or you can create a TensorShape with&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// zero dimensions and one element, and call AddDim() to add dimensions later.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TensorShape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorShapeBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorShape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorShapeBase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorShape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorShapeBase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;/// Allow a TensorShape to be used as a PartialTensorShape without copying&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PartialTensorShape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// NOLINT(runtime/explicit)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;/// Returns true if `*this` and `b` have the same sizes. Ignores&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;/// dimension names.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsSameSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorShape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorShape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsSameSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorShape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsSameSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;/// Fill `*dsizes` from `*this`.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DSizes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DenseIndex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AsEigenDSizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;/// Same as `AsEigenDSizes()` but allows for `NDIMS &amp;gt; dims()` -- in&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;/// which case we pad the rest of the sizes with 1.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DSizes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DenseIndex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AsEigenDSizesWithPadding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

 &lt;span class=&quot;nl&quot;&gt;private:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// These CHECK fail to ease debugging.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// REQUIRES: dims() == NDIMS&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CheckDimsEqual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// REQUIRES: dims() &amp;gt;= NDIMS&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CheckDimsAtLeast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TensorShapeBase&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TensorShapeRep&lt;/code&gt;的子类&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;/// Base class for TensorShape and PartialTensorShape.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// The class is templatized by either TensorShape or PartialTensorShape to&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// allow skipping known/unknown checks in the TensorShape case, but the&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;/// representation is shared exactly for fast conversion.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TensorShapeBase&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TensorShapeRep&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TensorBuffer&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/lib/core/refcount.h&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RefCounted&lt;/code&gt;(引用计数器)的子类&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TensorBuffer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RefCounted&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;tensor的主要函数&quot;&gt;Tensor的主要函数&lt;/h5&gt;

&lt;p&gt;返回Eigen::Tensor类型的主要函数：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 其中，typedef Eigen::TensorMap&amp;lt;Eigen::Tensor&amp;lt;T, 1, Eigen::RowMajor, IndexType&amp;gt;, Eigen::Aligned&amp;gt; Vec;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Matrix&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 其中，typedef Eigen::TensorMap&amp;lt;Eigen::Tensor&amp;lt;T, 2, Eigen::RowMajor, IndexType&amp;gt;, Eigen::Aligned&amp;gt; Matrix;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// 其中，typedef Eigen::TensorMap&amp;lt;Eigen::Tensor&amp;lt;T, NDIMS, Eigen::RowMajor, IndexType&amp;gt;, Eigen::Aligned&amp;gt; Tensor;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TTypes&lt;/code&gt;定义在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/core/framework/tensor_types.h&lt;/code&gt;中，如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Helper to define Tensor types given that the scalar is of type T.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IndexType&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DenseIndex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TTypes&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Rank-&amp;lt;NDIMS&amp;gt; tensor of scalar type T.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Unaligned Rank-&amp;lt;NDIMS&amp;gt; tensor of scalar type T.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedConstTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDIMS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Tensor32Bit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Scalar tensor (implemented as a rank-0 tensor) of scalar type T.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorFixedSize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sizes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorFixedSize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sizes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                                  &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ConstScalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Unaligned Scalar tensor of scalar type T.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorFixedSize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sizes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedScalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorFixedSize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sizes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                                  &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedConstScalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Rank-1 tensor (vector) of scalar type T.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Flat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ConstFlat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ConstVec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Unaligned Rank-1 tensor (vector) of scalar type T.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedFlat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedConstFlat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedVec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedConstVec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Rank-2 tensor (matrix) of scalar type T.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Aligned&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ConstMatrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Unaligned Rank-2 tensor (matrix) of scalar type T.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedMatrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TensorMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RowMajor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IndexType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;UnalignedConstMatrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;用法如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(...&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;built&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}...);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// 2D Eigen::Tensor, 3 x 5.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 2D Eigen::Tensor, 3 x 5.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// CHECK fails as my_mat is 2D.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// CHECK fails as my_mat is 2D.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// CHECK fails as type mismatch.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;eigentensor&quot;&gt;Eigen::Tensor&lt;/h5&gt;

&lt;p&gt;eigen源码：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://bitbucket.org/eigen/eigen/src/8dd2d6552a87?at=default&quot;&gt;https://bitbucket.org/eigen/eigen/src/8dd2d6552a87?at=default&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;git上的源码：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/eigenteam/eigen-git-mirror&quot;&gt;https://github.com/eigenteam/eigen-git-mirror&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Eigen::Tensor不属于Eigen官方维护的程序，由贡献者提供文档和维护，所以Tensor定义在Eigen unsupported模块中(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#include &quot;third_party/eigen3/unsupported/Eigen/CXX11/Tensor&quot;&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://eigen.tuxfamily.org/dox-devel/unsupported/classEigen_1_1Tensor.html&quot;&gt;http://eigen.tuxfamily.org/dox-devel/unsupported/classEigen_1_1Tensor.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://eigen.tuxfamily.org/dox-devel/unsupported/Tensor_8h_source.html&quot;&gt;Tensor&lt;/a&gt;主要包含一个变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TensorStorage&amp;lt;Scalar, Dimensions, Options&amp;gt; m_storage&lt;/code&gt;，而&lt;a href=&quot;https://bitbucket.org/eigen/eigen/src/8dd2d6552a872b778fe46aca4033e294ff82fb86/unsupported/Eigen/CXX11/src/Tensor/TensorStorage.h?at=default&amp;amp;fileviewer=file-view-default&quot;&gt;TensorStorage&lt;/a&gt;里有两个变量m_data和m_dimensions，m_data保存了Tensor的数据块，T是泛化的数据类型，m_dimensions保存了Tensor的维度信息。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;EIGEN_ALIGN_MAX&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MinSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;FixedDimensions&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m_dimensions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Eigen::Tensor的成员变量很简单，却支持非常多的基本运算，再借助Eigen的加速机制实现快速计算。Eigen::Tensor主要包含了&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一元运算（Unary），如sqrt、square、exp、abs等。&lt;/li&gt;
  &lt;li&gt;二元运算（Binary），如add，sub，mul，div等&lt;/li&gt;
  &lt;li&gt;选择运算（Selection），即if / else条件运算&lt;/li&gt;
  &lt;li&gt;归纳运算（Reduce），如reduce_sum， reduce_mean等&lt;/li&gt;
  &lt;li&gt;几何运算（Geometry），如reshape，slice，shuffle，chip，reverse，pad，concatenate，extract_patches，extract_image_patches等&lt;/li&gt;
  &lt;li&gt;张量积（Contract）和卷积运算（Convolve）是重点运算，后续会详细讲解。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;符号编程&quot;&gt;符号编程&lt;/h3&gt;

&lt;p&gt;编程模式通常分为命令式编程（imperative style programs）和符号式编程（symbolic style programs）。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;命令式编程&lt;/strong&gt;：&lt;strong&gt;容易理解和调试&lt;/strong&gt;，命令语句基本没有优化，按&lt;strong&gt;原有逻辑&lt;/strong&gt;执行。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;符号式编程&lt;/strong&gt;：涉及较多的&lt;strong&gt;嵌入和优化&lt;/strong&gt;，不容易理解和调试，但&lt;strong&gt;运行速度&lt;/strong&gt;有同比提升。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;命令式编程明确输入变量，并根据程序逻辑逐步运算，这种模式非常在调试程序时进行单步跟踪，分析中间变量。&lt;/p&gt;

&lt;p&gt;符号式编程将计算过程抽象为&lt;strong&gt;计算图&lt;/strong&gt;，计算流图可以方便的描述计算过程，所有&lt;strong&gt;输入节点&lt;/strong&gt;、&lt;strong&gt;运算节点&lt;/strong&gt;、&lt;strong&gt;输出节点&lt;/strong&gt;均&lt;strong&gt;符号化处理&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;和目前的符号语言比起来，TF最大的特点是强化了数据流图，引入了&lt;strong&gt;mutation&lt;/strong&gt;的概念。所谓mutation，就是可以在&lt;strong&gt;计算的过程中更改一个变量的值&lt;/strong&gt;，而这个变量在计算的过程中会&lt;strong&gt;被带入到下一轮迭代里面去&lt;/strong&gt;。mutation是机器学习优化算法几乎必须要引入的东西（虽然也可以通过immutable replacement来代替，但是会有效率的问题），这一点会导致最后的API设计和使用需要特别小心，把mutation引入到数据流图中会带来一些新的问题，比如，&lt;strong&gt;如何处理写与写之间的依赖&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;梯度计算&quot;&gt;梯度计算&lt;/h3&gt;

&lt;p&gt;梯度计算涉及每个计算节点，每个自定义的前向计算图都包含一个&lt;strong&gt;隐式的反向计算图&lt;/strong&gt;。从数据流向上看，正向计算图是数据从输入节点到输出节点的流向过程，反向计算图是数据&lt;strong&gt;从输出节点到输入节点的流向过程&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;可以参考&lt;a href=&quot;https://daiwk.github.io/posts/platform-tf-step-by-step-1-basics.html#231-%E8%87%AA%E5%8A%A8%E6%B1%82%E5%AF%BC&quot;&gt;tensorflow实战 chap1的自动求导部分&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;反向计算&lt;strong&gt;限制了符号编程中内存空间复用的优势&lt;/strong&gt;，因为在&lt;strong&gt;正向计算中的计算数据&lt;/strong&gt;在&lt;strong&gt;反向计算中也可能要用到&lt;/strong&gt;。从这一点上讲，粗粒度的计算节点比细粒度的计算节点更有优势，而TF大部分为细粒度操作，虽然&lt;strong&gt;灵活性很强&lt;/strong&gt;，但&lt;strong&gt;细粒度操作涉及到更多的优化方案&lt;/strong&gt;，在&lt;strong&gt;工程实现上开销较大&lt;/strong&gt;，不及粗粒度简单直接。在神经网络模型中，TF将逐步侧重粗粒度运算。&lt;/p&gt;

&lt;h3 id=&quot;控制流&quot;&gt;控制流&lt;/h3&gt;

&lt;p&gt;TF的计算图如同数据流一样，数据流向表示计算过程。数据流图可以很好的表达计算过程，为了扩展TF的表达能力，TF中引入控制流。编程语言中，if…else…是最常见的逻辑控制，在TF的数据流中也可以通过这种方式控制数据流向。接口函数如下，pred为判别表达式，fn1和fn2为运算表达式。当pred为true是，执行fn1操作；当pred为false时，执行fn2操作。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fn1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;TF还可以协调&lt;strong&gt;多个数据流&lt;/strong&gt;，在存在&lt;strong&gt;依赖节点&lt;/strong&gt;的场景下非常有用，例如节点B要读取模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;更新后的值，而节点A负责更新参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，则节点B必须等节点A完成后才能执行，否则读取的参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;为更新前的数值，这时需要一个运算控制器。接口函数如下，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.control_dependencies&lt;/code&gt;函数可以控制&lt;strong&gt;多个数据流执行完成后才能执行接下来的操作&lt;/strong&gt;，通常与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.group&lt;/code&gt;函数结合使用。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;control_dependencies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;control_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;TF支持的控制算子有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Switch&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Merge&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enter&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Leave&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NextIteration&lt;/code&gt;等。&lt;/p&gt;

&lt;p&gt;TF不仅支持逻辑控制，还支持&lt;strong&gt;循环控制&lt;/strong&gt;。TF使用和MIT Token-Tagged machine（即&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=78583.&quot;&gt;Executing a program
on the MIT tagged-token dataflow architecture.&lt;/a&gt;）相似的表示系统，将循环的&lt;strong&gt;每次迭代标记为一个tag&lt;/strong&gt;，&lt;strong&gt;迭代的执行状态标记为一个frame&lt;/strong&gt;，但迭代&lt;strong&gt;所需的数据准备好的时候&lt;/strong&gt;，就可以开始计算，从而&lt;strong&gt;多个迭代可以同时执行&lt;/strong&gt;。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>自然语言处理中的自注意力机制（Self-Attention Mechanism）</title>
   <link href="http://daiwk.github.io/posts/nlp-self-attention-models.html"/>
   <updated>2018-04-04T00:00:00+00:00</updated>
   <id>/posts/nlp-self-attention-models</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%b3%a8%e6%84%8f&quot;&gt;注意&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#attention%e7%9a%84%e6%9c%ac%e8%b4%a8&quot;&gt;attention的本质&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%87%aa%e5%b7%b1%e7%9a%84%e5%b0%8f%e7%bb%93&quot;&gt;自己的小结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#multi-head-attention&quot;&gt;multi-head attention&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#self-attention&quot;&gt;self-attention&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%af%b9%e6%af%94rnncnntransformer&quot;&gt;对比rnn/cnn/transformer&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#rnn&quot;&gt;rnn&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#sru&quot;&gt;SRU&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#srnn&quot;&gt;SRNN&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cnn&quot;&gt;cnn&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#transformer&quot;&gt;transformer&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%9c%ba%e5%99%a8%e7%bf%bb%e8%af%91%e5%b0%8f%e7%bb%bc%e8%bf%b0&quot;&gt;机器翻译小综述&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%9c%ba%e5%99%a8%e7%bf%bb%e8%af%91%e7%9a%84%e6%8c%91%e6%88%98&quot;&gt;机器翻译的挑战&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%bc%8f%e8%af%91&quot;&gt;漏译&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%95%b0%e6%8d%ae%e7%a8%80%e7%96%8f&quot;&gt;数据稀疏&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%bc%95%e5%85%a5%e7%9f%a5%e8%af%86&quot;&gt;引入知识&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%8f%af%e8%a7%a3%e9%87%8a%e6%80%a7&quot;&gt;可解释性&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e8%af%ad%e7%af%87%e7%bf%bb%e8%af%91&quot;&gt;语篇翻译&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%b8%85%e5%8d%8e%e5%88%98%e6%b4%8b%e7%9a%84talk%e6%9c%ba%e5%99%a8%e7%bf%bb%e8%af%91%e7%9a%84%e4%b8%89%e5%a4%a7%e6%8c%91%e6%88%98&quot;&gt;清华刘洋的talk——机器翻译的三大挑战&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e7%9f%a5%e8%af%86%e6%95%b4%e5%90%88&quot;&gt;知识整合&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%8f%af%e8%a7%a3%e9%87%8a%e5%8f%af%e8%a7%86%e5%8c%96&quot;&gt;可解释/可视化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e9%b2%81%e6%a3%92%e6%80%a7&quot;&gt;鲁棒性&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#attention%e7%9a%84%e5%85%b6%e4%bb%96%e5%a5%87%e6%80%aa%e5%ba%94%e7%94%a8&quot;&gt;attention的其他奇怪应用&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;attention is all you need的解读可以参考&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&quot;&gt;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;各种attention model可以参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-attention-models.html&quot;&gt;https://daiwk.github.io/posts/dl-attention-models.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247488035&amp;amp;idx=1&amp;amp;sn=9d0568f58cd85d628fa60ddc33d266e9&amp;amp;chksm=96e9cda3a19e44b5e7ce784d08508ad6d03dcd93c96491dd660af4312b9c67b67457486475ea&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0328RMAtTkf2hZSuXZD5vJBR&amp;amp;pass_ticket=tNNNXIGOajFyoVTQkCkEGcrVM4xaK5lnuItOaXnqkjfkBuTkVoKCva7UoF68PTww#rd&quot;&gt;自然语言处理中的自注意力机制（Self-Attention Mechanism）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文作者之一Lukasz Kaiser的ppt：&lt;a href=&quot;https://daiwk.github.io/assets/attention-is-all-you-need-lkaiser.pdf&quot;&gt;https://daiwk.github.io/assets/attention-is-all-you-need-lkaiser.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;注意&quot;&gt;注意&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.jianshu.com/p/48e71b72ca67&quot;&gt;https://www.jianshu.com/p/48e71b72ca67&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Adam 优化器可以说是目前使用最广泛、收敛速度较快且收敛过程较稳定的优化器。Adam 的计算公式如图所示。可以看到公式中梯度的计算使用了动量原理，每一轮用于梯度下降的梯度是当前计算的真实梯度与上一轮用于梯度下降的梯度的加权和。这样动量的引入可以防止训练时产生震荡。Adam 优化器的学习率对于不同参数也是不同的，由该参数历史每一轮的真实梯度的大小决定。好处是对于 NLP 这种输入极度稀疏且输入特征极度不平衡（例如整个预料库中“我”这样的词经常出现，而“拉姆塞”这样的词只出现几次）的任务，学习率是自适应的，一些在一次训练 epoch 中只更新几次的 embedding，在训练后期还是会有较大的学习率。&lt;/p&gt;

&lt;p&gt;NLP 输入稀疏的特点与 Adam 使用动量计算梯度的特点相结合就引入了麻烦。每一轮更新参数时，只有极少数 embedding 的梯度是非 0 的，大部分 embedding 的梯度是 0 即上图公式中的 gt 是 0。但是，计算了动量之后，这些原本梯度都应该是 0 的 embedding 有了非零梯度 mt 用于梯度下降更新。想象一个极端的例子，“拉姆塞”这个词在一个 epoch 中只在第一个 batch 出现了，于是第一个 batch 计算了“拉姆塞”这个 embedding 的真实梯度 g0 用于更新参数，在以后的每个 batch 中虽然“拉姆塞”这个词没有出现过，Adam 都会计算它的动量梯度 mt，并用于更新“拉姆塞”这个 embedding，实际上方向与 g0 完全相同，只是每一轮做一次 β1 倍的衰减。这样的做法就相当于对这些出现次数较少的低频词的 embedding，每次梯度下降的等效学习率是非常大的，容易引起类似过拟合的问题。&lt;/p&gt;

&lt;p&gt;每轮迭代只更新这个 batch 中出现过的词的 embedding 即可。TensorFlow 中可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.contrib.opt.LazyAdamOptimizer&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.zhihu.com/question/265357659/answer/580469438&quot;&gt;https://www.zhihu.com/question/265357659/answer/580469438&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;和图像等领域不同，对 NLU 之类的任务，每个 batch 采样到的词有限，每次更新对 Embedding 的梯度估计都是稀疏的。非 momentum-based 的 Optimizer 每步只会更新采样到的词，而对于 momentum-based 的 Optimizer，现在所有框架的实现都会用当前的 momentum 去更新所有的词，即使这些词在连续的几十步更新里都没有被采样到。这可能会使 Embedding 过拟合。&lt;/p&gt;

&lt;h2 id=&quot;attention的本质&quot;&gt;attention的本质&lt;/h2&gt;

&lt;p&gt;Attention函数的本质可以被描述为&lt;strong&gt;一个查询（query）与一系列（键key-值value）对一起映射成一个输出&lt;/strong&gt;。分为以下3步：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;将&lt;strong&gt;query&lt;/strong&gt;和&lt;strong&gt;每个key&lt;/strong&gt;进行&lt;strong&gt;相似度&lt;/strong&gt;计算得到权重，常用的相似度函数有点积，拼接，感知机等&lt;/li&gt;
  &lt;li&gt;使用一个&lt;strong&gt;softmax&lt;/strong&gt;(因为是一系列的k/v，所以类似多分类，要用softmax)函数对这些&lt;strong&gt;权重进行归一化&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;将&lt;strong&gt;权重&lt;/strong&gt;和&lt;strong&gt;相应的键值value&lt;/strong&gt;进行&lt;strong&gt;加权求和&lt;/strong&gt;得到最后的Attention&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
attention(Q,K,V)=softmax(\frac{QK^T}{\sqrt {d_k}})V
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;目前在&lt;strong&gt;NLP研究&lt;/strong&gt;中，key和value常常都是同一个，即 &lt;strong&gt;key=value&lt;/strong&gt;(如下例中的&lt;strong&gt;源&lt;/strong&gt;语言的编码器输出)。&lt;/p&gt;

&lt;p&gt;对比&lt;a href=&quot;https://daiwk.github.io/posts/nlp-nmt.html#4-%E6%B3%A8%E6%84%8F%E5%8A%9B%E6%9C%BA%E5%88%B6&quot;&gt;https://daiwk.github.io/posts/nlp-nmt.html#4-%E6%B3%A8%E6%84%8F%E5%8A%9B%E6%9C%BA%E5%88%B6&lt;/a&gt;以及&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensor-to-tensor.html#422-attention&quot;&gt;https://daiwk.github.io/posts/platform-tensor-to-tensor.html#422-attention&lt;/a&gt;可以发现：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;机器翻译里的&lt;strong&gt;源&lt;/strong&gt;语言的编码器输出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;就是&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;机器翻译里的&lt;strong&gt;源&lt;/strong&gt;语言的编码器输出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;同样是&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;机器翻译里的&lt;strong&gt;目标&lt;/strong&gt;语言的隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;就是&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;机器翻译里的目标语言和源语言的匹配程度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{QK^T}{\sqrt {d_k}}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;机器翻译里的归一化后的目标语言和源语言的匹配程度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{ij}\)&lt;/code&gt;就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(softmax(\frac{QK^T}{\sqrt {d_k}})\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;机器翻译里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;就是最终的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(attention(Q,K,V)\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所以说，机器翻译的attention，本质就是想给源语言的encoder输出的每一个元素&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt; &lt;strong&gt;(即V)&lt;/strong&gt; 搞一个权重，然后加权求和。而这个权重是通&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt; 它自己 &lt;strong&gt;(即K=V)&lt;/strong&gt; 与目标语言的隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt; &lt;strong&gt;(即Q)&lt;/strong&gt; 进行变换得到的。所以：&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k=v=源语言的encoder输出，q=目标语言的隐层状态。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;再理解回nmt里（不理那个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sqrt{d_k}\)&lt;/code&gt;）。假设emb是d维，那么 Q是mxd，K是nxd，i=1-&amp;gt;m，j=1-&amp;gt;n，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{ij}=QK^T\)&lt;/code&gt;是一个m行目标语言，n列源语言的矩阵，那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{ij}\)&lt;/code&gt;是对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;求softmax，分母就是第i行每个元素(即这第i个目标语言的词，对应的所有源语言词)的exp之和，分子就是第i行第j列(第i个目标语言词对应的第j个源语言词)的exp，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{ij}=\frac {exp(e_{ij})}{\sum _{k=1}^Texp(e_{ik})}\)&lt;/code&gt;看分母，是固定i，遍历j，也就是固定第i行，求这行每一列的和。&lt;/p&gt;

&lt;p&gt;再强调一次，一定要记得i、Q、m是目标语言的，j、K、n是源语言的，所以是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(QK^T\)&lt;/code&gt;，我们要拿attention来对源语言也就是V=K来做加权。做加权这步，就是一个mxn的矩阵，乘一个nxd的矩阵，得到最后mxd矩阵。就是第i行和nxd的一列对应相乘相加，得到一个元素，最后总共得到m个元素。相当于对于目标语言的第i个词来讲，他和源语言的每个词（共n个词）分别有个相关性（mxn矩阵的一行），然后作为这次输入的n个词的权重，求个和，当做这第i个目标语言的词的表示，即最终mxd的第i行的一个元素。而可以看到这个权重，对d维的emb的每一维的作用都是一样的（emb的第1维，即nxd的第1列和mxn的第i行相乘相加得到一个元素；emb的第2维，即nxd的第2列，和mxn的第i行相乘相加得到一个元素，这次运算和上一次运算，对于这一列的每一行来讲，它乘的mxn里的那一行是一样的，也就是说对一个给定的目标语言的词语，即mxn的第i行来讲，他对每一个源语言的词的影响权重在每一维源语言的emb（例如nxd的第1列和第2列）上都是一样的）。&lt;/p&gt;

&lt;p&gt;另外，得到的context只是作为产出下一个目标语言词的输入之一，还有前一个目标语言的词，及前一个目标语言的隐层状态。&lt;/p&gt;

&lt;p&gt;再来理解一下其中的softmax那步：&lt;/p&gt;

&lt;p&gt;类比一个分类任务，m行n列，m个样本，n个类别，每一行就是对这个样本而言，他在这个分类的概率，所以分子是这个类别，分母是所有类别（这一行求和）&lt;/p&gt;

&lt;p&gt;类似地，对于这个attention矩阵，m行n列，m个目标语言，n个源语言，一行就是一个目标语言的词，表示这个词和源语言每个词的相关程度，所以分母是所有源语言的词（这一行求和）。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/attention-explanation.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;自己的小结&quot;&gt;自己的小结&lt;/h2&gt;

&lt;p&gt;某个时候整理了个ppt：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/attention-my-understanding.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;&lt;strong&gt;注意：输出的attention维度和Q一样，都是mxn&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;multi-head-attention&quot;&gt;multi-head attention&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Query，Key，Value最开始都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维，各自通过h个线性变换(即每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W^Q,W^K,W^V\)&lt;/code&gt;)拆成h部分，每一部分的大小是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_v\)&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k=d_v=d_{model}/h\)&lt;/code&gt;）。&lt;/li&gt;
  &lt;li&gt;然后这h部分，每部分各自的q/k/v做一个attention的操作得到对应的attention&lt;/li&gt;
  &lt;li&gt;最后把这h个attention给concat到一起&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;不同之处在于进行了h次计算而不仅仅算一次，论文中说到这样的好处是可以&lt;strong&gt;允许模型在不同的表示子空间里学习到相关的信息&lt;/strong&gt;。&lt;/p&gt;

&lt;h2 id=&quot;self-attention&quot;&gt;self-attention&lt;/h2&gt;

&lt;p&gt;Self-Attention即&lt;strong&gt;K=V=Q&lt;/strong&gt;，例如输入一个句子，那么里面的&lt;strong&gt;每个词都要和该句子中的所有词&lt;/strong&gt;进行Attention计算。目的是学习句子内部的词依赖关系，捕获句子的内部结构。&lt;/p&gt;

&lt;p&gt;使用self-attention的原因：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每一层的复杂度：
    &lt;ul&gt;
      &lt;li&gt;如果输入序列n小于表示维度d的话，每一层的时间复杂度Self-Attention是比较有优势的。&lt;/li&gt;
      &lt;li&gt;当n比较大时，作者也给出了一种解决方案Self-Attention(restricted)即每个词不是和所有词计算Attention，而是只与限制的r个词去计算Attention。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;是否可以并行: multi-head Attention和CNN一样不依赖于前一时刻的计算，可以很好的并行，优于 RNN。&lt;/li&gt;
  &lt;li&gt;长距离依赖: 由于Self-Attention是每个词和所有词都要计算Attention，所以不管他们中间有多长距离，最大的路径长度也都只是 1。可以捕获长距离依赖关系。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;decoder中的masked att：&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/79872507&quot;&gt;https://zhuanlan.zhihu.com/p/79872507&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;每个词只能看到他前面的词，后面的要mask掉&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/mask-att-1.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在gpt-2中，就是把要mask的乘以负无穷。qk如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/mask-att-2.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;乘以mask矩阵后如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/mask-att-3.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;计算softmax后如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/mask-att-4.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;对比rnncnntransformer&quot;&gt;对比rnn/cnn/transformer&lt;/h2&gt;

&lt;p&gt;当然，在讲t2t的时候，就讲到了&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensor-to-tensor.html#426-why-self-attention&quot;&gt;https://daiwk.github.io/posts/platform-tensor-to-tensor.html#426-why-self-attention&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;不过，我们可以参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650755575&amp;amp;idx=2&amp;amp;sn=86fe97ff3d3b345eaeba92fbcf74ca99&amp;amp;chksm=871a9589b06d1c9fa48f2a471f50777980472464d576440d76d858cae6a9f1a305a9489de091&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0114HOKLy2c4NGYKZEsHUxA8&amp;amp;pass_ticket=aXFgueLD%2BxWMZCVaAWnXvtEoEpLmG3oXS8xAbcmCXkgLYGoJ9eVER7nGjjDlztRz#rd&quot;&gt;放弃幻想，全面拥抱Transformer：自然语言处理三大特征抽取器（CNN/RNN/TF）比较&lt;/a&gt;再来对比一下~&lt;/p&gt;

&lt;p&gt;nlp问题的特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输入是&lt;strong&gt;一维线性序列&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;输入是&lt;strong&gt;不定长&lt;/strong&gt;的&lt;/li&gt;
  &lt;li&gt;单词或子句的&lt;strong&gt;相对位置&lt;/strong&gt;关系很重要&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;主要的nlp任务：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;序列标注&lt;/strong&gt;：句子中&lt;strong&gt;每一个单词&lt;/strong&gt;要求模型根据上下文给出一个&lt;strong&gt;分类类别&lt;/strong&gt;。如中文分词、词性标注、命名实体识别、语义角色标注。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;分类任务&lt;/strong&gt;：整个句子输出一个分类值。如文本分类。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;句子关系推断&lt;/strong&gt;：给定&lt;strong&gt;两个句子&lt;/strong&gt;，判断这两个句子是否具备某种名义关系。例如entilment、QA、语义改写、自然语言推断&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;生成式任务&lt;/strong&gt;：输出一段文本，生成另一段文本。如机器翻译、文本摘要、写诗造句、看图说话。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;rnn&quot;&gt;rnn&lt;/h3&gt;

&lt;p&gt;基本原理不再赘述，看看rnn并行化的两个主要套路&lt;/p&gt;

&lt;h4 id=&quot;sru&quot;&gt;SRU&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1709.02755.pdf&quot;&gt;Simple Recurrent Units for Highly Parallelizable Recurrence&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在nmt上，transformer+sru比单纯transformer的bleu提升约0.7个点。&lt;/p&gt;

&lt;p&gt;SRU的核心思想就是仍然保留任意连续时间步（T-1和T）之间的隐层连接，然后在网络结构上做了一些改变，可以参考知乎的讨论：&lt;a href=&quot;https://www.zhihu.com/question/65244705&quot;&gt;如何评价新提出的RNN变种SRU?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;先看下原始的gru：&lt;a href=&quot;https://daiwk.github.io/posts/nlp-nmt.html#12-gru&quot;&gt;https://daiwk.github.io/posts/nlp-nmt.html#12-gru&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\ z_t=\sigma(W_zx_t+U_zh_{t-1}+b_z)
\\ r_t=\sigma(W_rx_t+U_rh_{t-1}+b_r)
\\ h_t=z_t \circ h_{t-1}+(1-z_t) \circ tanh(W_hx_t+ U_h(r_t \circ h_{t-1}) + b_h)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后从知乎偷个图：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/sru.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在gru的基础上进行修改，将各个时间步的&lt;strong&gt;gate&lt;/strong&gt;和&lt;strong&gt;transformed input&lt;/strong&gt;的计算&lt;strong&gt;只依赖&lt;/strong&gt;于&lt;strong&gt;当前时间步&lt;/strong&gt;的输入，然后在recurrent layers之间添加了&lt;strong&gt;skip
connections（严格来说是highway connections）&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;所以，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;各个时间步的transformed input、forget gate 以及reset gate的值可以并行处理，因为不依赖上一个时间步了。&lt;/li&gt;
  &lt;li&gt;还&lt;strong&gt;存在时间步依赖&lt;/strong&gt;的计算也只是比较简单和快速的&lt;strong&gt;element-wise&lt;/strong&gt;操作，它们还可以在dimension上进行并行。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;因此这个RNN单元现在的计算瓶颈就在三个矩阵乘法了，最后将这三个矩阵乘法可以归并成一个矩阵乘法。&lt;/p&gt;

&lt;p&gt;作者为了进一步的加速，将上面的各个时间步之间的element-wise的操作优化实现成了CUDA kernel functions。&lt;/p&gt;

&lt;h4 id=&quot;srnn&quot;&gt;SRNN&lt;/h4&gt;

&lt;h3 id=&quot;cnn&quot;&gt;cnn&lt;/h3&gt;

&lt;h3 id=&quot;transformer&quot;&gt;transformer&lt;/h3&gt;

&lt;h2 id=&quot;机器翻译小综述&quot;&gt;机器翻译小综述&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIxNTgyMDMwMw==&amp;amp;mid=2247486312&amp;amp;idx=1&amp;amp;sn=e3d9f43a101dd21ab92acf91e6f00ee3&amp;amp;chksm=97933a08a0e4b31eb1d903ffcfc0dedfc83f4839aae80c274f1a8b988be96794968bb4cb1810&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=KX13tEs%2BqQgwQ%2B1WWjoUQ1lrrkEScP2JOwqFfDxz8jLhwJXggm7HrOeHlTxg7Of1#rd&quot;&gt;神经网络机器翻译技术及应用（上）&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;机器翻译的挑战&quot;&gt;机器翻译的挑战&lt;/h3&gt;

&lt;h4 id=&quot;漏译&quot;&gt;漏译&lt;/h4&gt;

&lt;p&gt;翻译模型把原文句子整体读进去以后形成了&lt;strong&gt;一个向量&lt;/strong&gt;，然后再对这个向量进行解码。翻译模型&lt;strong&gt;认为有些词不应该产生&lt;/strong&gt;，从而漏掉了译文。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.aaai.org/Papers/AAAI/2019/AAAI-ZhaoYang.2930.pdf&quot;&gt;Addressing the Under-translation Problem from the Entropy Perspective&lt;/a&gt;这篇就发现漏译与&lt;strong&gt;词语的熵&lt;/strong&gt;成&lt;strong&gt;正相关&lt;/strong&gt;关系，这个词的&lt;strong&gt;熵越大&lt;/strong&gt;，&lt;strong&gt;漏译的可能性越大&lt;/strong&gt;。它所对应的目标语言词越多，概率越分散（熵越大），越有可能被漏译。&lt;/p&gt;

&lt;p&gt;例如源语言的一个词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s1&lt;/code&gt;对应3种不同的翻译，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(s1,t1),(s1,t2),(s1,t3 t4)&lt;/code&gt;，它的熵就比较大。我们把所有对应的翻译统一替换为一个特殊词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stoken4s1&lt;/code&gt;，以降低词语翻译的熵值。然后文章提出了pre-training, multitask learning, two-pass decoding三种方法，来改善翻译结果。&lt;/p&gt;

&lt;h4 id=&quot;数据稀疏&quot;&gt;数据稀疏&lt;/h4&gt;

&lt;p&gt;相比于统计机器翻译，这个问题对神经网络翻译而言更严重。实验表明，&lt;strong&gt;神经网络对于数据量更敏感&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://aclweb.org/anthology/P15-1166&quot;&gt;Multi-Task Learning for Multiple Language Translation&lt;/a&gt;在进行多语言翻译的时候，源语言&lt;strong&gt;共享编码器&lt;/strong&gt;，在解码端，&lt;strong&gt;不同的语言，使用不同的解码器&lt;/strong&gt;。这样在源语言端就会共享编码器的信息，从而缓解数据稀疏问题。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://aclweb.org/anthology/D18-1549&quot;&gt;Phrase-Based &amp;amp; Neural Unsupervised Machine Translation&lt;/a&gt;是EMNLP’18的best paper，提出了一个统一的框架，通过两种单语言，来构建翻译系统。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先构建一个词典，把这两种语言之间的词做一下对齐&lt;/li&gt;
  &lt;li&gt;然后为两个单语言分别训练一个语言模型&lt;/li&gt;
  &lt;li&gt;然后使用&lt;strong&gt;back translation&lt;/strong&gt;，先基于词的翻译从语言a翻译成语言b，然后挑出得分高的译文，再翻译回去，再用源语言的语言模型来判别好不好。一轮轮地迭代，就可以得到比较好的翻译结果&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;引入知识&quot;&gt;引入知识&lt;/h4&gt;

&lt;p&gt;我们引入了几种知识，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;短语表或者词表：如果发现『XXX』这个词没有被翻译出来，我们就去查这个词典，这个词典的内容是：『XXX: oo1 0.7 oo2:0.3』这样的，也就是XXX翻译成oo1和oo2的概率&lt;/li&gt;
  &lt;li&gt;语言模型：衡量目标语言的这个句子是不是流畅&lt;/li&gt;
  &lt;li&gt;长度奖励特征：奖励长句子，因为句子越长，可能漏掉的信息就越少。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;但这样还可能有歧义问题，因为比如中巴关系，可以是中国和巴基斯坦，中国和巴西，中国和巴勒斯坦等的缩写，如果限定了『金砖框架』下的，那就只能是巴西了，所以这还是有挑战的。&lt;/p&gt;

&lt;h4 id=&quot;可解释性&quot;&gt;可解释性&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;http://nlp.csai.tsinghua.edu.cn/~ly/papers/acl2017_dyz.pdf&quot;&gt;Visualizing and Understanding Neural Machine Translation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;左边的例子，出现了一个UNK，它虽然没有被翻译出来，但是出现在正确的位置，占了一个位置。通过Attention对应关系，可以看到这个UNK对应到『债务国』。&lt;/p&gt;

&lt;p&gt;右边例子是一个重复翻译的现象。神经网络机器翻译除了经常漏翻译之外，还会经常重复翻译。比如说出现了两个“history”。那么通过这个对应关系我们就可以看到，第6个位置上的“history”是重复出现的，它的出现不仅跟第一个位置“美国人”和第二个位置“历史”相关，还跟第5个位置“the”相关。因为产生了一个定冠词“the”，模型认为这个地方应该出现一个“history”。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/visualizing-and-understanding-of-mt.png&quot; style=&quot;max-height: 300px;&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;语篇翻译&quot;&gt;语篇翻译&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1811.05683&quot;&gt;Modeling Coherence for Discourse Neural Machine Translation&lt;/a&gt;提出了一个两步解码的方法。在第一轮解码中单独生成每个句子的初步翻译结果，在第二轮解码中利用第一轮翻译的结果进行翻译内容润色，并且提出使用&lt;strong&gt;增强式学习&lt;/strong&gt;模型来奖励模型产生更流畅的译文。&lt;/p&gt;

&lt;h2 id=&quot;清华刘洋的talk机器翻译的三大挑战&quot;&gt;清华刘洋的talk——机器翻译的三大挑战&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652046349&amp;amp;idx=2&amp;amp;sn=3bdb889aa84b90f0c687fb2eb52bd8b1&amp;amp;chksm=f12070fcc657f9ea22310cd663feb21e2fa0e2c2b92b52bfd2b949eb0d9e677d52947beaecd7&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;清华刘洋《基于深度学习的机器翻译》，突破知识整合、可解释和鲁棒性三大难关&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;知识整合&quot;&gt;知识整合&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1811.01100.pdf&quot;&gt;Prior Knowledge Integration for Neural Machine Translation using Posterior Regularization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在这项工作中，建议使用后验正则化来提供一个将先验知识整合到神经机器翻译中的通用框架。将先验知识来源表示为一个对数线性模型的特征，该模型指导神经翻译模型的学习过程。汉英翻译实验表明，该方法取得了显著的改进。&lt;/p&gt;

&lt;h3 id=&quot;可解释可视化&quot;&gt;可解释/可视化&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://aclweb.org/anthology/P17-1106&quot;&gt;Visualizing and Understanding Neural Machine Translation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这项工作主要的贡献包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;利用层级相关性传播算法可视化分析神经机器翻译；&lt;/li&gt;
  &lt;li&gt;能够计算任意隐状态和任意contextual words的相关性，同时不要求神经网络中的函数必须可求偏导，不同于之前只有encoder和decoder隐层之间的对应信息；&lt;/li&gt;
  &lt;li&gt;能够针对机器翻译中出错的例子，进行分析。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;鲁棒性&quot;&gt;鲁棒性&lt;/h3&gt;

&lt;p&gt;输入中的小扰动会严重扭曲中间表示，从而影响神经机器翻译（NMT）模型的翻译质量。&lt;/p&gt;

&lt;h2 id=&quot;attention的其他奇怪应用&quot;&gt;attention的其他奇怪应用&lt;/h2&gt;

&lt;p&gt;机器翻译里，假设emb是d维，正常机器翻译里，源语言K=V是nxd，目标语言Q是mxd&lt;/p&gt;

&lt;p&gt;softmax(QK^T)是mxn，第i行第j列是源语言的第j个词在所有源语言的词里，对第i个目标语言词的重要程度&lt;/p&gt;

&lt;p&gt;假设有一个rank问题，m个队列，n个位置，每个队列有n条结果，每条结果是一个d维向量&lt;/p&gt;

&lt;p&gt;Q是(mxd)xn，姑且把mxd这个向量看成一维的，即d=1，&lt;/p&gt;

&lt;p&gt;K是nxn，表示每个位置间两两的关系&lt;/p&gt;

&lt;p&gt;QK^T是(mxd)xn，第i行第j列是第j个位置在所有位置里，对第i个队列的重要程度&lt;/p&gt;

&lt;p&gt;。。真绕&lt;/p&gt;

&lt;p&gt;从我自己的角度，yy了一下：&lt;/p&gt;

&lt;p&gt;m个队列，n个位置，每个队列有k条结果，每个结果是一个d维向量&lt;/p&gt;

&lt;p&gt;Q: (mxd)xk，m行表示m个队列，k列表示k条结果&lt;/p&gt;

&lt;p&gt;K: nxk，n行表示n个位置，k列表示k条结果，也就是每个位置与每一条结果间的权重&lt;/p&gt;

&lt;p&gt;V: nxk，同K&lt;/p&gt;

&lt;p&gt;softmax(QK^T)：(mxd)xn维，第i行第j列表示，第j个位置在所有位置里，对第i个队列的重要程度&lt;/p&gt;

&lt;p&gt;softmax(QK^T)V：(mxd)xk维，&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/att-rank-yy1.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;有点奇怪。。如果把k和n互换呢。。。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/att-rank-yy2.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;再想想。。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>linux内核</title>
   <link href="http://daiwk.github.io/posts/platform-zookeeper.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-zookeeper</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《Linux内核设计与实现（原书第3版）》&lt;/strong&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>thrift</title>
   <link href="http://daiwk.github.io/posts/platform-thrift.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-thrift</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/apache/thrift&quot;&gt;https://github.com/apache/thrift&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>storm</title>
   <link href="http://daiwk.github.io/posts/platform-storm.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-storm</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/apache/storm&quot;&gt;https://github.com/apache/storm&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>seastar</title>
   <link href="http://daiwk.github.io/posts/platform-seastar.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-seastar</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;Seastar 是一个开源，基于c++ 11/14 feature，支持&lt;strong&gt;高并发&lt;/strong&gt;和&lt;strong&gt;低延迟&lt;/strong&gt;的&lt;strong&gt;异步编程&lt;/strong&gt;高性能库。参考：&lt;a href=&quot;http://www.sohu.com/a/190357942_617676&quot;&gt;现代硬件上的高性能C++异步框架 - SeaStar&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/scylladb/seastar&quot;&gt;https://github.com/scylladb/seastar&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>protobufrpc</title>
   <link href="http://daiwk.github.io/posts/platform-protobufrpc.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-protobufrpc</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.codedump.info/?p=169&quot;&gt;http://www.codedump.info/?p=169&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对于 &lt;strong&gt;service&lt;/strong&gt; EchoService而言,会对应的生成两个类:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;EchoService&lt;/strong&gt;类&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;EchoService_Stub&lt;/strong&gt;类&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>paddle fluid</title>
   <link href="http://daiwk.github.io/posts/platform-paddle-fluid.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-paddle-fluid</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E7%AE%80%E4%BB%8B&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5&quot;&gt;核心概念&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E7%BC%96%E8%AF%91%E6%97%B6%E6%A6%82%E5%BF%B5&quot;&gt;编译时概念&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E8%BF%90%E8%A1%8C%E6%97%B6%E6%A6%82%E5%BF%B5&quot;&gt;运行时概念&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tensorlodlevel-of-detailtensor&quot;&gt;Tensor/LoD(Level-of-Detail)Tensor&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#demo&quot;&gt;demo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensorflow-folding.html&quot;&gt;https://daiwk.github.io/posts/platform-tensorflow-folding.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;先了解下eager execution的优点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;快速调试即刻的运行错误并通过 Python 工具进行整合&lt;/li&gt;
  &lt;li&gt;借助易于使用的 Python 控制流支持动态模型&lt;/li&gt;
  &lt;li&gt;为自定义和高阶梯度提供强大支持&lt;/li&gt;
  &lt;li&gt;适用于几乎所有可用的 TensorFlow 运算&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;fluid也有点类似，分为编译时和运行时。&lt;/p&gt;

&lt;p&gt;编译时：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;创建变量描述Variable&lt;/li&gt;
  &lt;li&gt;创建operators的描述OpDesc&lt;/li&gt;
  &lt;li&gt;创建operator的属性&lt;/li&gt;
  &lt;li&gt;推断变量的类型和形状，进行静态检查：InferShape&lt;/li&gt;
  &lt;li&gt;规划变量的内存复用&lt;/li&gt;
  &lt;li&gt;创建反向计算&lt;/li&gt;
  &lt;li&gt;添加优化相关的Operators&lt;/li&gt;
  &lt;li&gt;(可选)添加多卡/多机相关的Operator，生成在多卡/多机上运行的程序&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;运行时：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;创建Executor&lt;/li&gt;
  &lt;li&gt;为将要执行的一段计算，在层级式的Scope空间中创建Scope&lt;/li&gt;
  &lt;li&gt;创建Block，依次执行Block&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/fluid-compile-run.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;另外，fluid自己封装了各种switch/ifelse/while_op等。&lt;/p&gt;

&lt;h2 id=&quot;核心概念&quot;&gt;核心概念&lt;/h2&gt;

&lt;h3 id=&quot;编译时概念&quot;&gt;编译时概念&lt;/h3&gt;

&lt;p&gt;program就是一个nn的训练/预测任务，由多个可嵌套的Block组成，而每个Block中包含了Variable和Operator：&lt;/p&gt;

&lt;p&gt;VarDesc + TensorDesc + OpDesc ==&amp;gt; BlockDesc ==&amp;gt; ProgramDesc&lt;/p&gt;

&lt;p&gt;transpiler将一个ProgramDesc转成另一个ProgramDesc，有以下两种：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Memory optimization transpiler: 在原始ProgramDesc中插入FreeMemoryOps，在一次迭代结束前提前释放内存，使得能够维持较小的memory footprint&lt;/li&gt;
  &lt;li&gt;distributed training transpiler: 将原始ProgramDesc转化为对应的分布式版本，包括：
    &lt;ul&gt;
      &lt;li&gt;trainer进程执行的ProgramDesc&lt;/li&gt;
      &lt;li&gt;parameter server执行的ProgramDesc&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;WIP: 输入ProgramDesc，生成可以直接被gcc/nvcc/icc等编译的代码，编译得到可执行文件。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'x'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_predict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'y'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square_error_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;avg_cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sgd_optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SGD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;optimize_ops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params_grads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sgd_optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;use_cuda&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;place&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CUDAPlace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;use_cuda&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CPUPlace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;training_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;TRAINING_ROLE&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;TRAINER&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;training_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;PSERVER&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;place&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CPUPlace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Executor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个时候可以打出来：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; print fluid.default_startup_program().to_string(True)
blocks {
  idx: 0
  parent_idx: -1
  vars {
    name: &quot;learning_rate_0&quot;
    type {
      type: LOD_TENSOR
      lod_tensor {
        tensor {
          data_type: FP32
          dims: 1
        }
      }
    }
    persistable: true
  }
  vars {
    name: &quot;fc_0.b_0&quot;
    type {
      type: LOD_TENSOR
      lod_tensor {
        tensor {
          data_type: FP32
          dims: 1
        }
      }
    }
    persistable: true
  }
  vars {
    name: &quot;fc_0.w_0&quot;
    type {
      type: LOD_TENSOR
      lod_tensor {
        tensor {
          data_type: FP32
          dims: 13
          dims: 1
        }
      }
    }
    persistable: true
  }
  ops {
    outputs {
      parameter: &quot;Out&quot;
      arguments: &quot;learning_rate_0&quot;
    }
    type: &quot;fill_constant&quot;
    attrs {
      name: &quot;op_role_var&quot;
      type: STRINGS
    }
    attrs {
      name: &quot;op_role&quot;
      type: INT
      i: 0
    }
    attrs {
      name: &quot;force_cpu&quot;
      type: BOOLEAN
      b: false
    }
    attrs {
      name: &quot;value&quot;
      type: FLOAT
      f: 0.0010000000475
    }
    attrs {
      name: &quot;shape&quot;
      type: INTS
      ints: 1
    }
    attrs {
      name: &quot;dtype&quot;
      type: INT
      i: 5
    }
  }
  ops {
    outputs {
      parameter: &quot;Out&quot;
      arguments: &quot;fc_0.b_0&quot;
    }
    type: &quot;fill_constant&quot;
    attrs {
      name: &quot;op_role_var&quot;
      type: STRINGS
    }
    attrs {
      name: &quot;op_role&quot;
      type: INT
      i: 0
    }
    attrs {
      name: &quot;force_cpu&quot;
      type: BOOLEAN
      b: false
    }
    attrs {
      name: &quot;value&quot;
      type: FLOAT
      f: 0.0
    }
    attrs {
      name: &quot;shape&quot;
      type: INTS
      ints: 1
    }
    attrs {
      name: &quot;dtype&quot;
      type: INT
      i: 5
    }
  }
  ops {
    outputs {
      parameter: &quot;Out&quot;
      arguments: &quot;fc_0.w_0&quot;
    }
    type: &quot;uniform_random&quot;
    attrs {
      name: &quot;op_role_var&quot;
      type: STRINGS
    }
    attrs {
      name: &quot;op_role&quot;
      type: INT
      i: 0
    }
    attrs {
      name: &quot;dtype&quot;
      type: INT
      i: 5
    }
    attrs {
      name: &quot;seed&quot;
      type: INT
      i: 0
    }
    attrs {
      name: &quot;min&quot;
      type: FLOAT
      f: -0.654653668404
    }
    attrs {
      name: &quot;max&quot;
      type: FLOAT
      f: 0.654653668404
    }
    attrs {
      name: &quot;shape&quot;
      type: INTS
      ints: 13
      ints: 1
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再接下来，可以打出更多的信息(会发现多了梯度等)：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_main_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;persistable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOD_TENSOR&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lod_tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FP32&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lod_level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;persistable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOD_TENSOR&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lod_tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FP32&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lod_level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;persistable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;learning_rate_0&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOD_TENSOR&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lod_tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FP32&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;persistable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOD_TENSOR&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lod_tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FP32&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_1@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOD_TENSOR&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lod_tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FP32&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOD_TENSOR&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lod_tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FP32&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lod_level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;persistable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mean_0.tmp_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LOD_TENSOR&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lod_tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FP32&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;persistable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mul&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;y_num_col_dims&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;x_num_col_dims&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;elementwise_add&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;use_mkldnn&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BOOLEAN&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;axis&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;elementwise_sub&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;use_mkldnn&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BOOLEAN&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;axis&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;use_mkldnn&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BOOLEAN&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mean_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mean&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mean_0.tmp_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fill_constant&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;257&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;value&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLOAT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;force_cpu&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;shape&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INTS&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;dtype&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mean_0.tmp_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_1@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mean_grad&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_1@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_grad&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;use_mkldnn&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BOOLEAN&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square_error_cost_0.tmp_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;y&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_1@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;elementwise_sub_grad&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;use_mkldnn&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BOOLEAN&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;axis&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_1&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_1@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;elementwise_add_grad&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;use_mkldnn&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BOOLEAN&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;axis&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Out@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.tmp_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;x&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;X@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Y@GRAD&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mul_grad&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;y_num_col_dims&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;x_num_col_dims&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Grad&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;LearningRate&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;learning_rate_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Param&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ParamOut&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sgd&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.b_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Grad&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;LearningRate&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;learning_rate_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Param&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ParamOut&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sgd&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role_var&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;STRINGS&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0&quot;&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;strings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc_0.w_0@GRAD&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;attrs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;op_role&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;还可以用debugger模块：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;paddle.fluid&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debugger&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;paddle.fluid&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;framework&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debugger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pprint_program_codes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;framework&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_main_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;variables&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;13L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;13L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square_error_cost_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate_0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square_error_cost_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;il&quot;&gt;1L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;operators&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_role_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_num_col_dims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_num_col_dims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elementwise_add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_role_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_mkldnn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;square_error_cost_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elementwise_sub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_role_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use_mkldnn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ymean_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square_error_cost_0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_role_var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_role&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;运行时概念&quot;&gt;运行时概念&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;数据相关：
    &lt;ul&gt;
      &lt;li&gt;Tensor/LoDtensor/Variable&lt;/li&gt;
      &lt;li&gt;Scope&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;计算相关：
    &lt;ul&gt;
      &lt;li&gt;Block&lt;/li&gt;
      &lt;li&gt;Kernel/OpWithKernel/OpWithoutKernel&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;执行相关：Executor&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;tensorlodlevel-of-detailtensor&quot;&gt;Tensor/LoD(Level-of-Detail)Tensor&lt;/h2&gt;

&lt;p&gt;Tensor是n维array的推广，LoDTensor是在Tensor基础上加了&lt;strong&gt;序列信息&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fluid中输入、输出以及所有可学习参数都用LodTensor表示&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;一个mini-batch的输入数据是一个LoDTensor:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;rnn处理变长序列不需要padding&lt;/li&gt;
  &lt;li&gt;LoD可以理解为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;对于非序列数据，LoD信息为空&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/LoDTensor.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;图中，&lt;/p&gt;

&lt;h2 id=&quot;demo&quot;&gt;demo&lt;/h2&gt;

&lt;p&gt;定义以下训练流程：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;train_loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    train_loop
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_reader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cluster_data_reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cluster_train_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BATCH_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;feeder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFeeder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_startup_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;batch_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;PASS_NUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pass_id&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PASS_NUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                      &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feeder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                                      &lt;span class=&quot;n&quot;&gt;fetch_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;visualdl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show_fluid_trend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'epoch'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
                    &lt;span class=&quot;n&quot;&gt;pass_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'{&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;avg_loss&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;:%d}'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;batch_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;save_dirname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_inference_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_dirname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'x'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                                                  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isnan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;got NaN loss, training failed.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;visualdl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show_fluid_trend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'batch'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
                &lt;span class=&quot;n&quot;&gt;pass_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'{&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;avg_loss&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;:%d}'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;save_dirname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_inference_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_dirname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'x'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;train_loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    train_loop
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_reader&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cluster_data_reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cluster_train_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BATCH_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;feeder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFeeder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;place&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_startup_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;batch_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;PASS_NUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pass_id&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PASS_NUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                      &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feeder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                                      &lt;span class=&quot;n&quot;&gt;fetch_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;visualdl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show_fluid_trend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'epoch'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
                    &lt;span class=&quot;n&quot;&gt;pass_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'{&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;avg_loss&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;:%d}'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;batch_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;save_dirname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_inference_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_dirname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'x'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                                                  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isnan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;got NaN loss, training failed.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;visualdl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show_fluid_trend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'batch'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
                &lt;span class=&quot;n&quot;&gt;pass_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'{&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;avg_loss&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;:%d}'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avg_loss_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;save_dirname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_inference_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save_dirname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'x'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果是local，直接调用上面的函数&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;train_loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default_main_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果是分布式：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PADDLE_PORT&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;6174&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pserver_ips&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PADDLE_PSERVERS&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# ip,ip...
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eplist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ip&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pserver_ips&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;eplist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;':'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pserver_endpoints&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eplist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# ip:port,ip:port...
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PADDLE_TRAINERS_NUM&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;current_endpoint&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;POD_IP&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;trainer_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PADDLE_TRAINER_ID&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DistributeTranspiler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;optimize_ops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;params_grads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;trainer_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pservers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pserver_endpoints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;trainers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trainers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;training_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;PSERVER&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pserver_prog&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_pserver_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_endpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pserver_startup&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_startup_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_endpoint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                            &lt;span class=&quot;n&quot;&gt;pserver_prog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pserver_startup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;exe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pserver_prog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;training_role&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;TRAINER&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_trainer_program&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;完整的logistic regression的demo: &lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/tests/book/test_fit_a_line.py&quot;&gt;https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/tests/book/test_fit_a_line.py&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>paddle fluid分布式cpu相关</title>
   <link href="http://daiwk.github.io/posts/platform-paddle-fluid-code-distributed-cpu.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-paddle-fluid-code-distributed-cpu</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650767419&amp;amp;idx=2&amp;amp;sn=178f83aaae05eb31b7edfbc78be0be3a&amp;amp;chksm=871a4445b06dcd53f58476f35bcada44c7e7f7d874374e1c8b78e3ee14d93308d08cfccacd2b&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;ERNIE2.0背后的神助攻：飞桨高性能分布式训练引擎&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./paddle/fluid/framework/dist_multi_trainer.cc&lt;/code&gt;：入口&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./paddle/fluid/framework/downpour_worker.cc&lt;/code&gt;：pull push sparse&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./paddle/fluid/framework/pull_dense_worker.cc&lt;/code&gt;：pull，push dense&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./paddle/fluid/framework/data_feed.cc&lt;/code&gt;：解析ins&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>nginx</title>
   <link href="http://daiwk.github.io/posts/platform-nginx.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-nginx</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/nginx/nginx&quot;&gt;https://github.com/nginx/nginx&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>微服务</title>
   <link href="http://daiwk.github.io/posts/platform-microservices.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-microservices</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#从单体服务迁移成微服务&quot;&gt;从单体服务迁移成微服务&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#设计时考虑失败&quot;&gt;设计时考虑失败&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#去中心化数据管理&quot;&gt;去中心化数据管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#服务发现&quot;&gt;服务发现&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#服务通信&quot;&gt;服务通信&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#k8s微服务&quot;&gt;k8s+微服务&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#saas的12要素&quot;&gt;SaaS的12要素&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#kubernetes的设计适合微服务&quot;&gt;Kubernetes的设计适合微服务&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#搞&quot;&gt;搞&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;微服务是一种工程方法，聚焦在将应用&lt;strong&gt;拆解&lt;/strong&gt;为具备&lt;strong&gt;良好接口设计&lt;/strong&gt;的&lt;strong&gt;单一功能&lt;/strong&gt;的模块集上。模块集可以&lt;strong&gt;独立&lt;/strong&gt;部署，由掌管&lt;strong&gt;整个生命周期&lt;/strong&gt;的&lt;strong&gt;小团队&lt;/strong&gt;维护。&lt;/p&gt;

&lt;p&gt;微服务&lt;strong&gt;最小化&lt;/strong&gt;了人与人之间的沟通和协作，进而&lt;strong&gt;减小&lt;/strong&gt;应用范围和变更的&lt;strong&gt;风险&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;抽几个来讲一下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;单一功能：一个服务聚焦一个”类型的”功能上&lt;/li&gt;
  &lt;li&gt;良好接口设计：服务间的通信依赖接口，简单清晰的接口是基础&lt;/li&gt;
  &lt;li&gt;独立：服务具备独立运行工作的能力，包括流水线、监控、上线等&lt;/li&gt;
  &lt;li&gt;整个生命周期：团队负责从开发-测试-staging-部署-维护的整个阶段&lt;/li&gt;
  &lt;li&gt;语言无关：服务间通信使用语言无关的API，通常由RESTful的接口、RPC等实现&lt;/li&gt;
  &lt;li&gt;有限上下文：松耦合，服务单元不需要了解其他服务单元如何运作&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;随着时间演进，大型、复杂的单体应用逐步演化出多层架构（MVC等），但按照业务功能的分层仍然笨拙、复杂、难以控制。&lt;/p&gt;

&lt;h2 id=&quot;从单体服务迁移成微服务&quot;&gt;从单体服务迁移成微服务&lt;/h2&gt;

&lt;p&gt;核心就是面向业务拆分服务&lt;/p&gt;

&lt;h3 id=&quot;设计时考虑失败&quot;&gt;设计时考虑失败&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;熔断机制
    &lt;ul&gt;
      &lt;li&gt;设计初衷是保证一个服务故障不会影响整个系统&lt;/li&gt;
      &lt;li&gt;当某个服务调用失败比例提高，合理的使用其他方式提供服务&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;隔离
    &lt;ul&gt;
      &lt;li&gt;单个服务造成的负载问题不影响其他服务&lt;/li&gt;
      &lt;li&gt;设计服务的资源用量&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;去中心化数据管理&quot;&gt;去中心化数据管理&lt;/h3&gt;

&lt;p&gt;单体应用的事务管理更容易。相较ACID，微服务架构更偏重BASE&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;BASE
    &lt;ul&gt;
      &lt;li&gt;BA：Basically Available， 基本可用&lt;/li&gt;
      &lt;li&gt;S：Soft state，无状态&lt;/li&gt;
      &lt;li&gt;E：Eventual consistency，最终一致性。中间可以失败重试，但最终结果要保持一致。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;尽可能的避免分布式事务&lt;/strong&gt;，因为BASE还是会有各种坑&lt;/li&gt;
  &lt;li&gt;理想情况下每个微服务单元管理自己的数据&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;服务发现&quot;&gt;服务发现&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;微服务架构要求服务可靠和容错&lt;/li&gt;
  &lt;li&gt;云计算和容器化的微服务，服务配置动态化。新服务单元创建后，需要网络中的其他服务快速找到并相互调用&lt;/li&gt;
  &lt;li&gt;使用注册服务中心管理服务状态(例如使用zk，etcd等搞一个&lt;strong&gt;配置中心&lt;/strong&gt;)&lt;/li&gt;
  &lt;li&gt;使用服务内通信设置，更少的依赖外部环境：IP\域名\hostname...&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;服务通信&quot;&gt;服务通信&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;API调用
    &lt;ul&gt;
      &lt;li&gt;HTTP RESTful API&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;RPC&lt;/li&gt;
  &lt;li&gt;消息机制：mq，劣势是实时性，优势是可以延迟处理&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;k8s微服务&quot;&gt;k8s+微服务&lt;/h2&gt;

&lt;h3 id=&quot;saas的12要素&quot;&gt;SaaS的12要素&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://12factor.net/zh_cn/&quot;&gt;https://12factor.net/zh_cn/&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;基准代码：一分代码多处部署&lt;/li&gt;
  &lt;li&gt;依赖：显示声明依赖关系&lt;/li&gt;
  &lt;li&gt;配置：在环境中存储配置&lt;/li&gt;
  &lt;li&gt;后端服务：把后端服务当做附加资源&lt;/li&gt;
  &lt;li&gt;构建、发布、运行：严格分离构建和运行&lt;/li&gt;
  &lt;li&gt;进程：一个或多个无状态进程运行应用&lt;/li&gt;
  &lt;li&gt;端口绑定：通过端口绑定提供服务&lt;/li&gt;
  &lt;li&gt;并发：通过进程模型进行扩展&lt;/li&gt;
  &lt;li&gt;易处理：快速启动和优雅终止可最大化健壮性&lt;/li&gt;
  &lt;li&gt;开发与线上环境等价：尽可能的保持开发，预发布，线上环境相同&lt;/li&gt;
  &lt;li&gt;日志：把日志当作事件流&lt;/li&gt;
  &lt;li&gt;管理进程：后台管理任务当作一次性进程运行&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;kubernetes的设计适合微服务&quot;&gt;Kubernetes的设计适合微服务&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;服务发现Service&lt;/li&gt;
  &lt;li&gt;服务编排与弹性伸缩&lt;/li&gt;
  &lt;li&gt;统一配置中心：对于配置中心，K8S 提供了 configMap，可以在容器启动的时候，将配置注入到环境变量或者 Volume 里面。但是唯一的缺点是，注入到环境变量中的配置不能动态改变了，好在 Volume 里面的可以，只要容器中的进程有 reload 机制，就可以实现配置的动态下发了。&lt;/li&gt;
  &lt;li&gt;统一日志与监控：统一日志和监控往往需要在 Node 上部署 Agent，来对日志和指标进行收集，当然每个 Node 上都有，daemonset 的设计，使得更容易实现
    &lt;ul&gt;
      &lt;li&gt;Container Liveness&lt;/li&gt;
      &lt;li&gt;Readness Probe&lt;/li&gt;
      &lt;li&gt;Node-Problem-Detector&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;滚动更新&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;搞&quot;&gt;搞&lt;/h3&gt;

&lt;p&gt;设置账号密码&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;root&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; username
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'root123!'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; password
kubectl create secret generic user-password &lt;span class=&quot;nt&quot;&gt;--from-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;username &lt;span class=&quot;nt&quot;&gt;--from-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;password
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>memcached</title>
   <link href="http://daiwk.github.io/posts/platform-memcached.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-memcached</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/memcached/memcached&quot;&gt;https://github.com/memcached/memcached&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>linux内存管理</title>
   <link href="http://daiwk.github.io/posts/platform-linux-memory.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-linux-memory</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://lib.csdn.net/article/linux/33242&quot;&gt;http://lib.csdn.net/article/linux/33242&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>linux内核</title>
   <link href="http://daiwk.github.io/posts/platform-linux-kernel.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-linux-kernel</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考《linux内核设计与实现》&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>leveldb rocksdb anna等</title>
   <link href="http://daiwk.github.io/posts/platform-leveldb-rocksdb.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-leveldb-rocksdb</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#leveldb&quot;&gt;leveldb&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rocksdb&quot;&gt;rocksdb&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ssd%E7%AE%80%E4%BB%8B&quot;&gt;ssd简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cassandra&quot;&gt;Cassandra&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#anna&quot;&gt;anna&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;leveldb&quot;&gt;leveldb&lt;/h2&gt;

&lt;p&gt;memtable + wal文件 +l0-7&lt;/p&gt;

&lt;h2 id=&quot;rocksdb&quot;&gt;rocksdb&lt;/h2&gt;

&lt;h2 id=&quot;ssd简介&quot;&gt;ssd简介&lt;/h2&gt;

&lt;p&gt;flash:写page 擦除block，512k一个block。擦除目前最好的是3w次&lt;/p&gt;

&lt;p&gt;ssd中大多数厂商是通过一个映射表，把物理映射到逻辑上，将随机读改成顺序读&lt;/p&gt;

&lt;p&gt;每个ssd会留出一块block做缓存，用户不可见。当用户区写满时，会把有用的数据移到这块block，然后移回去。如果失效数据很少，写放大很严重（目前写放大是10倍左右）&lt;/p&gt;

&lt;p&gt;ssd写性能: 顺序500m/s，随机一般要/10
ssd读性能：约3G/s&lt;/p&gt;

&lt;p&gt;xxxx存储系统针对ssd设计：有个全内存的index，是个hash，然后也有个磁盘的wal。内存里只记录偏移。设计上是顺序写&lt;/p&gt;

&lt;p&gt;id是64int，其中前几位是标记offset，导致只有约16个bit可用==》最多只能用128g磁盘。但现在磁盘是800-1024t，所以要充分利用得部署多个引擎。但这样就变成了随机写&lt;/p&gt;

&lt;p&gt;结合：
单机引擎部分：用leveldb只做index，存储key+offset，所以不受限于内存，解决xxx的问题。&lt;/p&gt;

&lt;p&gt;分布式：主从复制，写只写master，从只读&lt;/p&gt;

&lt;p&gt;新硬件AEP：往内存口插&lt;/p&gt;

&lt;h2 id=&quot;cassandra&quot;&gt;Cassandra&lt;/h2&gt;

&lt;h2 id=&quot;anna&quot;&gt;anna&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MjM5MDE0Mjc4MA==&amp;amp;mid=2651009148&amp;amp;idx=2&amp;amp;sn=a42a12c16c3e08bfe8a06b0ac347663f&amp;amp;chksm=bdbec82f8ac941390b86eea1a438383c823054b173857e3299794d2098b32bbefb6a5562cde8&amp;amp;scene=27#wechat_redirect&quot;&gt;秒杀Redis的KVS上云了！伯克利重磅开源Anna 1.0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;berkely anna kv: 针对分布式的改造。&lt;/p&gt;

&lt;p&gt;正常主-从group一般500个，但一开始hash要搞3w多个，方便扩容&lt;/p&gt;

&lt;p&gt;节点没有主从概念，但每个slot有主从
一致性：通过client端做一致性，很容易支持多种一致性（最终一致、强一致等）&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>kubernetes</title>
   <link href="http://daiwk.github.io/posts/platform-k8s.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-k8s</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#背景&quot;&gt;背景&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#物理机时代&quot;&gt;物理机时代&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#虚拟机时代&quot;&gt;虚拟机时代&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#前容器时代&quot;&gt;前容器时代&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#后容器时代&quot;&gt;后容器时代&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#k8s起源&quot;&gt;k8s起源&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#borg&quot;&gt;borg&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#omega&quot;&gt;omega&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#mesos&quot;&gt;mesos&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#nomad&quot;&gt;nomad&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cncf基金会&quot;&gt;CNCF基金会&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#k8s现状&quot;&gt;k8s现状&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#例子&quot;&gt;例子&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#架构组件&quot;&gt;架构&amp;amp;组件&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#k8s--docker&quot;&gt;k8s &amp;amp; docker&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#k8s--node&quot;&gt;k8s &amp;amp; Node&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#架构&quot;&gt;架构&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#基础概念术语&quot;&gt;基础概念&amp;amp;术语&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#pod&quot;&gt;Pod&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#replicationset&quot;&gt;ReplicationSet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#label&quot;&gt;Label&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#deployment&quot;&gt;deployment&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rolling-update&quot;&gt;rolling update&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#statefulset&quot;&gt;StatefulSet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#daemonset&quot;&gt;daemonSet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#jobs&quot;&gt;Jobs&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#service&quot;&gt;service&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#headless-service&quot;&gt;headless service&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#k8s-service对外提供访问&quot;&gt;k8s service对外提供访问&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dns&quot;&gt;DNS&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#native-network-model&quot;&gt;native network model&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#volumes&quot;&gt;Volumes&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#inf-k8s&quot;&gt;inf k8s&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#k8s示例&quot;&gt;k8s示例&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#minikube&quot;&gt;minikube&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#katacoda&quot;&gt;katacoda&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#其他&quot;&gt;其他&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;中文网：&lt;a href=&quot;https://www.kubernetes.org.cn&quot;&gt;https://www.kubernetes.org.cn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/kubernetes/kubernetes&quot;&gt;https://github.com/kubernetes/kubernetes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;百度前员工刘杰写的《分布式系统原理介绍》【&lt;a href=&quot;https://daiwk.github.io/assets/分布式系统原理介绍.pdf&quot;&gt;https://daiwk.github.io/assets/分布式系统原理介绍.pdf&lt;/a&gt;】。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://kubernetes.kansea.com/docs/whatisk8s/&quot;&gt;http://kubernetes.kansea.com/docs/whatisk8s/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://queue.acm.org/detail.cfm?id=2898444&quot;&gt;Borg, Omega, and Kubernetes Lessons learned from three container-management systems over a decade&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MjM5MDE0Mjc4MA==&amp;amp;mid=2651017395&amp;amp;idx=4&amp;amp;sn=9c025e9ac1d66b254cf0f81a6d79d0ee&amp;amp;chksm=bdbea8e08ac921f6ea24b322144147383aed99b7d7596ff2958f57504cebdf8fd118aea1286f&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=zzUnWIgdqTLvX39vSLCKaOJN8KVDYuvxPgj7h5mQNNMiTnEMdrWSwBJSd3ch3aLL#rd&quot;&gt;Kubernetes 五周年回顾与展望&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI5ODQ2MzI3NQ==&amp;amp;mid=2247487599&amp;amp;idx=1&amp;amp;sn=b7866e9c9dc925072caad6592be86d3d&amp;amp;chksm=eca4292bdbd3a03d78838d65644ca8f25446c3876c746559b0f22a28ba38530fc79932215fd0&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1565173432767&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;Docker 5年，架构演进之路&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

&lt;h3 id=&quot;物理机时代&quot;&gt;物理机时代&lt;/h3&gt;

&lt;h3 id=&quot;虚拟机时代&quot;&gt;虚拟机时代&lt;/h3&gt;

&lt;p&gt;openstack&lt;/p&gt;

&lt;h3 id=&quot;前容器时代&quot;&gt;前容器时代&lt;/h3&gt;

&lt;p&gt;docker，基于LXC为基础构建的容器引擎，通过namespace和cgroup实现&lt;strong&gt;资源&lt;/strong&gt;隔离和调配。将os和应用捆绑，使得应用系统环境标准化、集装箱化。主要问题：缺乏完整的调度部署管理能力。&lt;/p&gt;

&lt;h3 id=&quot;后容器时代&quot;&gt;后容器时代&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;原生docker swarm&lt;/li&gt;
  &lt;li&gt;google的kubernetes(2015年)：容器集群管理系统。可以管理跨机器容器化。&lt;/li&gt;
  &lt;li&gt;apache的mesos。优势在于离线集群任务。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;k8s起源&quot;&gt;k8s起源&lt;/h3&gt;

&lt;p&gt;2004年google开始使用容器，06年cgroup
内部集群资源管理平台borg和omega
k8s起源于borg，参考了omega的经验和教训。borg和omega的创始人都加入k8s。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/Borg_Omega_Kubernetes.pdf&quot;&gt;https://daiwk.github.io/assets/Borg_Omega_Kubernetes.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;borg&quot;&gt;borg&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/Borg.pdf&quot;&gt;https://daiwk.github.io/assets/Borg.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;omega&quot;&gt;omega&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/Omega.pdf&quot;&gt;https://daiwk.github.io/assets/Omega.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;mesos&quot;&gt;mesos&lt;/h4&gt;

&lt;h4 id=&quot;nomad&quot;&gt;nomad&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/Nomad.pdf&quot;&gt;https://daiwk.github.io/assets/Nomad.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;cncf基金会&quot;&gt;CNCF基金会&lt;/h3&gt;

&lt;p&gt;Cloud Native Computing Foundation ==&amp;gt;生态绑定、法律保护、推广、培训等。&lt;/p&gt;

&lt;h3 id=&quot;k8s现状&quot;&gt;k8s现状&lt;/h3&gt;

&lt;p&gt;5w+commits，2.5w stars&lt;/p&gt;

&lt;h3 id=&quot;例子&quot;&gt;例子&lt;/h3&gt;

&lt;p&gt;通过kubectl提交，应用描述文件（json/yaml）&lt;/p&gt;

&lt;p&gt;一个app下可以有多个实例。&lt;/p&gt;

&lt;p&gt;使用label和selector进行deployment和service的对应，nodeport虚拟端口nodeport，对外暴露的端口targetport&lt;/p&gt;

&lt;h2 id=&quot;架构组件&quot;&gt;架构&amp;amp;组件&lt;/h2&gt;

&lt;h3 id=&quot;k8s--docker&quot;&gt;k8s &amp;amp; docker&lt;/h3&gt;

&lt;p&gt;k8s会调度相应的app到对应的docker host上去运行&lt;/p&gt;

&lt;h3 id=&quot;k8s--node&quot;&gt;k8s &amp;amp; Node&lt;/h3&gt;

&lt;p&gt;调度和pack各种xxx到不同的nodes上&lt;/p&gt;

&lt;h3 id=&quot;架构&quot;&gt;架构&lt;/h3&gt;

&lt;p&gt;可以参考&lt;a href=&quot;https://www.kubernetes.org.cn/kubernetes%E8%AE%BE%E8%AE%A1%E6%9E%B6%E6%9E%84&quot;&gt;https://www.kubernetes.org.cn/kubernetes%E8%AE%BE%E8%AE%A1%E6%9E%B6%E6%9E%84&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/k8s-architecture.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;单集群可以一次5000台机器上线&lt;/p&gt;

&lt;p&gt;有一个中心化的api server，然后把状态存储在etcd中
主节点有scheduler和controller mgr，worker节点有kubelet和service proxy。&lt;/p&gt;

&lt;p&gt;api server中有所有资源的增删改查的接口，还有authorization，可以按namespace来划分权限。&lt;/p&gt;

&lt;p&gt;scheduler中把定义的pod按照策略调度到相应节点&lt;/p&gt;

&lt;p&gt;controller manager和apiserver通信，获取集群的特定信息，然后做出响应的反馈动作。由多个controller组成&lt;/p&gt;

&lt;p&gt;etcd是分布式k-v你在什么地方，所有集群数据存放在etcd中，实现组件的无状态化。可以通过watch的方式监听变化，并触发相应动作。&lt;/p&gt;

&lt;p&gt;worker节点:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;kubelet：是节点的agent，接收描述的manifest并处理&lt;/li&gt;
  &lt;li&gt;kubeproxy: 网络agent，部署在各个node，简单的tcp/udp转发，简单的round-robin负载均衡。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;基础概念术语&quot;&gt;基础概念&amp;amp;术语&lt;/h2&gt;

&lt;h3 id=&quot;pod&quot;&gt;Pod&lt;/h3&gt;
&lt;p&gt;pod是若干&lt;strong&gt;相关容器&lt;/strong&gt;的组合。pod包含的容器运行在同一台宿主机上，它们使用相同的pid/network/ipc/uts命名空间/ip地址和端口，相互之前能通过localhost来发现和通信，还可以共享一块存储volume空间。其实是容器的更高层次的抽象。&lt;/p&gt;

&lt;h3 id=&quot;replicationset&quot;&gt;ReplicationSet&lt;/h3&gt;
&lt;p&gt;控制管理pod副本。确保任何时候k8s集群中有足够v几个的pod副本在运行。如果少于，会自动启动。&lt;/p&gt;

&lt;h3 id=&quot;label&quot;&gt;Label&lt;/h3&gt;
&lt;p&gt;区分pod/service/replication/controller的k/v对。每个api对象可以有多个label，但每个label的k只能对应一个v。&lt;/p&gt;

&lt;h3 id=&quot;deployment&quot;&gt;deployment&lt;/h3&gt;
&lt;p&gt;只描述集群的期望状态&lt;/p&gt;

&lt;h3 id=&quot;rolling-update&quot;&gt;rolling update&lt;/h3&gt;
&lt;p&gt;升级时，旧实例下一下，上一个新实例这样。但如果是大更新，只能停机更新，旧的全下了再上新的。
参数：
maxSurge:不容许实例减少，且集群资源充裕
maxUnavaliable:集群资源照张，可以先缩容，再更新&lt;/p&gt;

&lt;h3 id=&quot;statefulset&quot;&gt;StatefulSet&lt;/h3&gt;

&lt;p&gt;部署带持久化数据的服务&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;identity严格区分, uniq ordinal value&lt;/li&gt;
  &lt;li&gt;index min会被选举为master&lt;/li&gt;
  &lt;li&gt;nfs是底层存储&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;搭建分布式db集群&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;storage class用于vpc申请&lt;/li&gt;
  &lt;li&gt;headless service用于维护db的endpoints&lt;/li&gt;
  &lt;li&gt;statefulset&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;daemonset&quot;&gt;daemonSet&lt;/h3&gt;
&lt;p&gt;每个host部署一个服务，也有rolling update&lt;/p&gt;

&lt;h3 id=&quot;jobs&quot;&gt;Jobs&lt;/h3&gt;

&lt;p&gt;parallelism：最大的共有并行数
completions: 完成了多少个job就算完成了&lt;/p&gt;

&lt;h3 id=&quot;service&quot;&gt;service&lt;/h3&gt;

&lt;p&gt;service是真实应用服务的抽象，定义了pod的逻辑集合和访问这个pod集合的策略。
将代理pod对外表现为一个单一的访问接口，外部不需要了解后端pod是如何运行的。&lt;/p&gt;

&lt;p&gt;实现原理：kube-proxy维护一个iptables，会去watch apiserver，然后更新这个table，有dns解析功能&lt;/p&gt;

&lt;h3 id=&quot;headless-service&quot;&gt;headless service&lt;/h3&gt;

&lt;p&gt;不提供round-robin
显式返回所有endpoints
可以自定义策略（如选主）
最佳实践：cacendera的去中心化选主&lt;/p&gt;

&lt;h3 id=&quot;k8s-service对外提供访问&quot;&gt;k8s service对外提供访问&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;nodeport&lt;/li&gt;
  &lt;li&gt;loadbalancer
    &lt;ul&gt;
      &lt;li&gt;blvxxxxx: 类似bfe
+ingress: 7层的routing，nginx的实现(可以访问内部节点，然后开个外网ip，让其他k8s也能访问它内部的节点)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;dns&quot;&gt;DNS&lt;/h3&gt;

&lt;p&gt;skyDNS-&amp;gt;KubeDNS。可以对特定的domain自定义上游nameserver。&lt;/p&gt;

&lt;p&gt;优先级：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;kubedns&lt;/li&gt;
  &lt;li&gt;特定domain的自定义上游namesever&lt;/li&gt;
  &lt;li&gt;默认的upstream nameserver&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;native-network-model&quot;&gt;native network model&lt;/h3&gt;

&lt;p&gt;每个pod有一个ip，需要容器网络&lt;/p&gt;

&lt;h3 id=&quot;volumes&quot;&gt;Volumes&lt;/h3&gt;

&lt;p&gt;可以将存储在pod上进行挂载，可以直接挂载基于fuse共享存储NFS等。&lt;/p&gt;

&lt;h2 id=&quot;inf-k8s&quot;&gt;inf k8s&lt;/h2&gt;

&lt;p&gt;微服务
自动伸缩
使用supervisor,如果主进程被Kill了，会把这个信号量传给每个子进程，避免孤儿进程
节点亲和&amp;amp;应用亲和：例如混布cpu+gpu（后来，1.6的k8s原生支持gpu了）&lt;/p&gt;

&lt;h2 id=&quot;k8s示例&quot;&gt;k8s示例&lt;/h2&gt;

&lt;h3 id=&quot;minikube&quot;&gt;minikube&lt;/h3&gt;

&lt;p&gt;使用minikube:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://qii404.me/2018/01/06/minukube.html&quot;&gt;https://qii404.me/2018/01/06/minukube.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;katacoda&quot;&gt;katacoda&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.katacoda.com/&quot;&gt;https://www.katacoda.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;有状态的容器：例如&lt;/p&gt;

&lt;p&gt;监控：普罗米修斯&lt;/p&gt;

&lt;h3 id=&quot;其他&quot;&gt;其他&lt;/h3&gt;

&lt;p&gt;建一个k8s集群的时候，有n+3台机器做master，因为用的是etcd，选主至少要3台机器。&lt;/p&gt;

&lt;p&gt;service如何访问到pods:&lt;/p&gt;

&lt;p&gt;可以参考&lt;a href=&quot;https://blog.csdn.net/yuanjunlai141/article/details/79487381&quot;&gt;https://blog.csdn.net/yuanjunlai141/article/details/79487381&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;在宿主机创建iptables&lt;/li&gt;
  &lt;li&gt;xxx&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>kubernetes-container</title>
   <link href="http://daiwk.github.io/posts/platform-k8s-container.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-k8s-container</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#namespace&quot;&gt;Namespace&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#pid-namespace&quot;&gt;PID Namespace&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E4%B8%8E%E8%99%9A%E6%8B%9F%E6%9C%BA%E7%9A%84%E5%8C%BA%E5%88%AB&quot;&gt;与虚拟机的区别&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cgroups&quot;&gt;Cgroups&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#namespace%E7%9A%84%E9%97%AE%E9%A2%98&quot;&gt;Namespace的问题&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cgroups%E7%AE%80%E4%BB%8B&quot;&gt;cgroups简介&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cgroups%E7%BC%BA%E7%82%B9&quot;&gt;cgroups缺点&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rootfs%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F&quot;&gt;rootfs文件系统&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;容器技术的核心功能，就是通过&lt;strong&gt;约束和修改进程&lt;/strong&gt;的动态表现，从而为其创造一个&lt;strong&gt;『边界』&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Namespace&lt;/strong&gt;：&lt;strong&gt;修改进程视图&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cgroups&lt;/strong&gt;：&lt;strong&gt;制造约束&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;namespace&quot;&gt;Namespace&lt;/h2&gt;

&lt;p&gt;可以参考&lt;a href=&quot;https://coolshell.cn/articles/17010.html&quot;&gt;https://coolshell.cn/articles/17010.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://time.geekbang.org/column/article/14642&quot;&gt;05 | 白话容器基础（一）：从进程说开去&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;pid-namespace&quot;&gt;PID Namespace&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;进程&lt;/strong&gt;：一个程序运行后的计算机执行环境（磁盘上的可执行文件、内存中的数据、寄存器中的值、堆栈中的指令、被打开的文件、各种设备的状态信息等）的总和。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; busybox /bin/sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;-it参数指的是，在启动容器后，&lt;strong&gt;分配一个文本输入/输出环境，即tty&lt;/strong&gt;，与容器的标准输入相关联。&lt;/p&gt;

&lt;p&gt;linux的Namespace机制，其实就是Linux新建进程的一个可选参数，在Linux系统中创建线程的系统调用是clone()，如：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SIGCHLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个系统调用会创建一个新进程，并返回其进程号pid（其中，SIGCHLD表示在&lt;strong&gt;一个进程终止或者停止时&lt;/strong&gt;，将&lt;strong&gt;SIGCHLD信号发送给其父进程&lt;/strong&gt;，按系统默认将忽略此信号，如果父进程希望被告知其子系统的这种状态，则应捕捉此信号）。可以指定CLONE_NEWPID参数，这样就会创建一个&lt;strong&gt;新的PID Namespace&lt;/strong&gt;，clone出来的&lt;strong&gt;新进程将成为Namespace里&lt;/strong&gt;的&lt;strong&gt;第一个进程&lt;/strong&gt;。如：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CLONE_NEWPID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SIGCHLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果多次执行如上的clone()调用，就会创建多个PID Namespace，&lt;strong&gt;每个Namespace里的应用进程，都会认为自己是当前容器里的第1号进程&lt;/strong&gt;，看不到宿主机里真正的进程空间，也看不到其他PID Namespace里的具体情况。&lt;/p&gt;

&lt;p&gt;除了PID Namespace，Linux中还有Mount、UTS、IPC、Network和User这些Namespace。&lt;/p&gt;

&lt;h3 id=&quot;与虚拟机的区别&quot;&gt;与虚拟机的区别&lt;/h3&gt;

&lt;p&gt;下图左边是虚拟机的工作原理，Hypervisor是虚拟机最主要部分，通过硬件虚拟化功能，模拟出运行一个操作系统需要的各种硬件（CPU、内存、I/O设备等），然后在这些虚拟的硬件上安装了一个新的操作系统Guest OS。所以，这个Hypervisor负责创建虚拟机，会有额外资源消耗和占用，本身虚拟机还会占用内存，对宿主机操作系统的调用也要经过虚拟化软件的拦截和处理，对计算资源、网络和磁盘的I/O损耗也非常大。&lt;/p&gt;

&lt;p&gt;右边的Docker在运行时，并没有一个真正的『docker容器』运行在宿主机中，只是一个正常的应用进程，只是在创建时，加上了各种Namespace参数。&lt;/p&gt;

&lt;p&gt;『敏捷』和『高性能』是容器相对于虚拟机的最大优势，也是它能够在PaaS这种更细粒度的资源管理平台上大行其道的重要原因。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/k8s-container-virtual-tech-compare.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;cgroups&quot;&gt;Cgroups&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://time.geekbang.org/column/article/14653&quot;&gt;06 | 白话容器基础（二）：隔离与限制&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;namespace的问题&quot;&gt;Namespace的问题&lt;/h3&gt;

&lt;p&gt;但基于linux namespace的隔离机制有一个主要问题：&lt;strong&gt;隔离得不彻底&lt;/strong&gt;，体现为以下两方面：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;容器只是运行在宿主机上的一种特殊进程，多个容器之间用的还是&lt;strong&gt;同一个宿主机的操作系统内核&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;可以在容器中通过Mount Namespace单独挂载其他不同版本的操作系统文件，如CentOS或Ubuntu，但不能改变共享宿主机内核的事实。所以，&lt;strong&gt;要在Windows宿主机主运行Linux容器，或者在低版本的Linux宿主机上运行高版本的Linux容器，都是不行的&lt;/strong&gt;（Docker on Mac或者windows，实际上都是基于虚拟化技术实现的，和这里要讲的linux容器不同）&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Linux内核中，&lt;strong&gt;很多资源和对象是不能被Namespace化的&lt;/strong&gt;，典型例子就是『时间』&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如，如果在容器中使用了settimeofday(可以设置系统的秒数，以及微秒数)修改了时间，那么宿主机的时间也会被修改。。。所以，在容器里部署应用时，『什么能做，什么不能做』，是用户必须考虑的。所以容器给应用暴露出的攻击面是很大的，尽管在实践中可以用&lt;strong&gt;Seccomp&lt;/strong&gt;等技术，对容器内部发起的所有系统调用进行过滤和甄别以进行安全加固，但这加多了一层对系统调用的过滤，会拖累容器的性能。所以在生产环境中，&lt;strong&gt;不能把运行在物理机上的Linux容器直接暴露到公网上&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;注：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;seccomp 是 secure computing 的缩写，其是 Linux kernel 从2.6.23版本引入的一种简洁的 sandboxing 机制。在 Linux 系统里，大量的系统调用（system call）直接暴露给用户态程序。但是，并不是所有的系统调用都被需要，而且不安全的代码滥用系统调用会对系统造成安全威胁。seccomp安全机制能使一个进程进入到一种“安全”运行模式，该模式下的进程只能调用4种系统调用（system call），即 read(), write(), exit() 和 sigreturn()，否则进程便会被终止。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;当然，后续讲的&lt;strong&gt;基于虚拟化或者独立内核技术的容器实现&lt;/strong&gt;，可以较好地在隔离和性能间做平衡。&lt;/p&gt;

&lt;h3 id=&quot;cgroups简介&quot;&gt;cgroups简介&lt;/h3&gt;

&lt;p&gt;可以参考&lt;a href=&quot;https://coolshell.cn/articles/17049.html&quot;&gt;https://coolshell.cn/articles/17049.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Linux Cgroups是Linux内核中用来&lt;strong&gt;为进程设置资源限制&lt;/strong&gt;的一个重要功能，可以限制一个进程能够使用的资源上限，包括CPU、内存、磁盘、网络带宽等。&lt;/p&gt;

&lt;p&gt;如果是centos，先&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yum install libcgroup&lt;/code&gt;，然后&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service cgconfig start&lt;/code&gt;，可以发现&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@xx:ll /cgroup/
total 0
drwxr-xr-x 4 root root 0 Jul 19  2017 blkio
drwxr-xr-x 7 root root 0 Jul 19  2017 cpu
drwxr-xr-x 4 root root 0 Jul 19  2017 cpuacct
drwxr-xr-x 4 root root 0 Jul 19  2017 cpuset
drwxr-xr-x 4 root root 0 Jun 21 20:25 devices
drwxr-xr-x 5 root root 0 Jul 19  2017 freezer
drwxr-xr-x 5 root root 0 Jul 19  2017 memory
drwxr-xr-x 2 root root 0 Oct  8 20:34 net_cls
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;类似的目录，ubuntu是在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/sys/fs/cgroup/&lt;/code&gt;目录下。例如，cpu目录下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@xx:ll /cgroup/cpu
total 0
drwxr-xr-x 2 root root 0 May 25 07:02 agent-webdir
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Jul 19  2017 cgroup.clone_children
&lt;span class=&quot;nt&quot;&gt;--w--w--w-&lt;/span&gt; 1 root root 0 Jul 19  2017 cgroup.event_control
&lt;span class=&quot;nt&quot;&gt;-r--r--r--&lt;/span&gt; 1 root root 0 Jul 19  2017 cgroup.procs
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Jul 19  2017 cpu.cfs_period_us
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Jul 19  2017 cpu.cfs_quota_us
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Jul 19  2017 cpu.shares
&lt;span class=&quot;nt&quot;&gt;-r--r--r--&lt;/span&gt; 1 root root 0 Jul 19  2017 cpu.stat
drwxr-xr-x 7 root root 0 Jul 19  2017 idle
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Jul 19  2017 notify_on_release
drwxr-xr-x 7 root root 0 Jul 19  2017 offline
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Jul 19  2017 release_agent
drwxr-xr-x 2 root root 0 Oct  8 20:30 small
drwxr-xr-x 2 root root 0 Jul 19  2017 system
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:06 tasks
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;cfs_quota和cfs_period是组合使用的参数，限制进程在&lt;strong&gt;长度为cfs_period的一段时间内&lt;/strong&gt;，只能被分配到&lt;strong&gt;总量为cfs_quota的cpu时间&lt;/strong&gt;。试着建一个文件夹，就会生成一堆文件，这个目录称为一个&lt;strong&gt;『控制组』&lt;/strong&gt;：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@xx:mkdir /cgroup/cpu/container
root@xx:ll /cgroup/cpu/container/  
total 0
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:43 cgroup.clone_children
&lt;span class=&quot;nt&quot;&gt;--w--w--w-&lt;/span&gt; 1 root root 0 Oct  8 20:43 cgroup.event_control
&lt;span class=&quot;nt&quot;&gt;-r--r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:43 cgroup.procs
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:43 cpu.cfs_period_us
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:43 cpu.cfs_quota_us
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:43 cpu.shares
&lt;span class=&quot;nt&quot;&gt;-r--r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:43 cpu.stat
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:43 notify_on_release
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:43 tasks
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以看下默认值：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; /cgroup/cpu/container/cpu.cfs_period_us &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
100000

&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; /cgroup/cpu/container/cpu.cfs_quota_us &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;文件&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cgroup/cpu/container/cpu.cfs_quota_us&lt;/code&gt;的默认值是-1，如果改成20000（即20000us，也就是20ms），那么，在每100ms的时间内(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cgroup/cpu/container/cpu.cfs_period_us&lt;/code&gt;文件指定)，被这个控制组限制的进程只能用20%的CPU带宽。如何指定进程呢？把pid写到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tasks&lt;/code&gt;文件里就行啦。&lt;/p&gt;

&lt;p&gt;几个cgroups常用的子系统：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;blkio：为&lt;strong&gt;块设备&lt;/strong&gt;设定&lt;strong&gt;I/O限制&lt;/strong&gt;，一般用于磁盘等设备&lt;/li&gt;
  &lt;li&gt;cpuset：为进程分配单独的&lt;strong&gt;CPU核&lt;/strong&gt;和对应的&lt;strong&gt;内存节点&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;memory：为进程设定&lt;strong&gt;内存使用&lt;/strong&gt;的限制&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;对于docker等linux容器项目来说，只需要在每个子系统下，为每个容器创建一个控制组（即创建一个新目录），然后在启动容器进程后，把这个进程的pid写入对应控制组的tasks文件中就行啦~&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--cpu-period&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;100000 &lt;span class=&quot;nt&quot;&gt;--cpu-quota&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20000 ubuntu /bin/bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后可以看到：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@xx:docker ps
CONTAINER ID        IMAGE                                  COMMAND             CREATED             STATUS              PORTS                              NAMES
edc3dad98449        ubuntu                                 &lt;span class=&quot;s2&quot;&gt;&quot;/bin/bash&quot;&lt;/span&gt;         14 minutes ago      Up 14 minutes                                          evil_shaw
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们拿containerid去找：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@xx:ll /cgroup/cpu/docker/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;edc3dad98449&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
total 0
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 cgroup.clone_children
&lt;span class=&quot;nt&quot;&gt;--w--w--w-&lt;/span&gt; 1 root root 0 Oct  8 20:59 cgroup.event_control
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 cgroup.procs
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 cpu.cfs_period_us
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 cpu.cfs_quota_us
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 cpu.rt_period_us
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 cpu.rt_runtime_us
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 cpu.shares
&lt;span class=&quot;nt&quot;&gt;-r--r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 cpu.stat
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 notify_on_release
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt; 1 root root 0 Oct  8 20:59 tasks
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;进一步地，可以发现cfs_period_us和cfs_quota_us确实都设成了我们想要的值啦：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@xx:head /cgroup/cpu/docker/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;edc3dad98449&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;cpu&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; /cgroup/cpu/docker/edc3dad98449c401fc53e684bb14242dcba2ccd5ac56024ed330aa54d5b3642d/cpu.cfs_period_us &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
100000

&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; /cgroup/cpu/docker/edc3dad98449c401fc53e684bb14242dcba2ccd5ac56024ed330aa54d5b3642d/cpu.cfs_quota_us &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
20000

&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; /cgroup/cpu/docker/edc3dad98449c401fc53e684bb14242dcba2ccd5ac56024ed330aa54d5b3642d/cpu.rt_period_us &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
1000000

&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; /cgroup/cpu/docker/edc3dad98449c401fc53e684bb14242dcba2ccd5ac56024ed330aa54d5b3642d/cpu.rt_runtime_us &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
0

&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; /cgroup/cpu/docker/edc3dad98449c401fc53e684bb14242dcba2ccd5ac56024ed330aa54d5b3642d/cpu.shares &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
1024

&lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; /cgroup/cpu/docker/edc3dad98449c401fc53e684bb14242dcba2ccd5ac56024ed330aa54d5b3642d/cpu.stat &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
nr_periods 5
nr_throttled 1
throttled_time 27232386
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;总之，容器是一个&lt;strong&gt;『单进程』模型&lt;/strong&gt;。所以，在&lt;strong&gt;一个容器中&lt;/strong&gt;，&lt;strong&gt;不能同时运行两个不同的应用&lt;/strong&gt;，除非可以&lt;strong&gt;事先找到一个公共的pid=1&lt;/strong&gt;来充当两个不同应用的父进程。所以很多人会用systemd或者supervisord等软件来代替应用本身作为容器的启动进程。当然，还有其他的解决方法，使&lt;strong&gt;容器&lt;/strong&gt;和&lt;strong&gt;应用&lt;/strong&gt;能&lt;strong&gt;同生命周期&lt;/strong&gt;，因为如果『容器正常运行，但里面的应用已经挂了』这种情况出现，容器编排就很麻烦了。。&lt;/p&gt;

&lt;h3 id=&quot;cgroups缺点&quot;&gt;cgroups缺点&lt;/h3&gt;

&lt;p&gt;提及最多的是&lt;strong&gt;/proc文件系统的问题&lt;/strong&gt;。/proc目录存储的是记录&lt;strong&gt;当前内核运行状态&lt;/strong&gt;的一系列特殊文件，也是top命令查看系统信息的主要数据来源。所以，在容器中执行top命令，会发现&lt;strong&gt;显示的是宿主机的cpu和内存数据&lt;/strong&gt;！因为&lt;strong&gt;/proc并不知道cgroups限制的存在。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;解决方法：&lt;strong&gt;lxcfs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;top是从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/proc/stats&lt;/code&gt;目录下获取数据，所以道理上来讲，容器不挂载宿主机的该目录就可以了。lxcfs就是来实现这个功能的，做法是把宿主机的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/lib/lxcfs/proc/memoinfo&lt;/code&gt;文件挂载到Docker容器的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/proc/meminfo&lt;/code&gt;位置后。容器中进程读取相应文件内容时，LXCFS的FUSE实现会从容器对应的Cgroup中读取正确的内存限制。从而使得应用获得正确的资源约束设定。kubernetes环境下，也能用，以ds 方式运行 lxcfs ，自动给容器注入争取的 proc 信息。&lt;/p&gt;

&lt;p&gt;详见&lt;a href=&quot;https://blog.csdn.net/shida_csdn/article/details/79196258&quot;&gt;https://blog.csdn.net/shida_csdn/article/details/79196258&lt;/a&gt;，从&lt;a href=&quot;https://copr-be.cloud.fedoraproject.org/results/ganto/lxd/epel-7-x86_64/00486278-lxcfs/&quot;&gt;https://copr-be.cloud.fedoraproject.org/results/ganto/lxd/epel-7-x86_64/00486278-lxcfs/&lt;/a&gt;下载&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lxcfs-2.0.5-3.el7.centos.x86_64.rpm&lt;/code&gt;，然后(注意，要求GLIBC_2.17。。关于glibc的问题可以参考&lt;a href=&quot;https://daiwk.github.io/posts/knowledge-gcc48.html&quot;&gt;https://daiwk.github.io/posts/knowledge-gcc48.html&lt;/a&gt;)：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yum &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;lxcfs-2.0.5-3.el7.centos.x86_64.rpm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后启动的时候加如下参数就行：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; 300m  &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo:rw &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /var/lib/lxcfs/proc/diskstats:/proc/diskstats:rw &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /var/lib/lxcfs/proc/meminfo:/proc/meminfo:rw &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /var/lib/lxcfs/proc/stat:/proc/stat:rw &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /var/lib/lxcfs/proc/swaps:/proc/swaps:rw &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /var/lib/lxcfs/proc/uptime:/proc/uptime:rw &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
	  ubuntu:14.04 /bin/bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;rootfs文件系统&quot;&gt;rootfs文件系统&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://time.geekbang.org/column/article/17921&quot;&gt;07 | 白话容器基础（三）：深入理解容器镜像&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mount Namespace和其他Namespace的不同之处在于：它对容器进程视图的改变，一定要伴随着挂载操作(mount)才能生效。&lt;/p&gt;

&lt;p&gt;xxx&lt;/p&gt;

&lt;p&gt;Union Mount FS可以参考&lt;a href=&quot;https://en.wikipedia.org/wiki/Union_mount&quot;&gt;https://en.wikipedia.org/wiki/Union_mount&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;实现：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AU FS&lt;/li&gt;
  &lt;li&gt;Overlay FS&lt;/li&gt;
  &lt;li&gt;Devicemapper&lt;/li&gt;
  &lt;li&gt;btrfs&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>grpc</title>
   <link href="http://daiwk.github.io/posts/platform-grpc.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-grpc</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;安装&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#c-tutorial&quot;&gt;c++ tutorial&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#c&quot;&gt;快速c++入门&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;进阶&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/grpc/grpc&quot;&gt;https://github.com/grpc/grpc&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/grpc/grpc/blob/master/INSTALL.md&quot;&gt;https://github.com/grpc/grpc/blob/master/INSTALL.md&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;curl &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt; https://grpc.io/release&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; https://github.com/grpc/grpc
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;grpc
git submodule update &lt;span class=&quot;nt&quot;&gt;--init&lt;/span&gt;
make
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;h2 id=&quot;c-tutorial&quot;&gt;c++ tutorial&lt;/h2&gt;

&lt;h3 id=&quot;快速c入门&quot;&gt;快速c++入门&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/grpc/grpc/tree/master/examples/cpp&quot;&gt;https://github.com/grpc/grpc/tree/master/examples/cpp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;先安装protoc&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;third_party/protobuf/
make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后编译demo&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;examples/cpp/helloworld
make
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动server&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./greeter_server
./greeter_async_server &lt;span class=&quot;c&quot;&gt;# 异步&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动client&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;greeter_client
greeter_async_client &lt;span class=&quot;c&quot;&gt;#异步&lt;/span&gt;
greeter_async_client2 &lt;span class=&quot;c&quot;&gt;#异步&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;进阶&quot;&gt;进阶&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/grpc/grpc/blob/master/examples/cpp/cpptutorial.md&quot;&gt;https://github.com/grpc/grpc/blob/master/examples/cpp/cpptutorial.md&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>brpc-rdma</title>
   <link href="http://daiwk.github.io/posts/platform-brpc-rdma.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-brpc-rdma</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#brpc&quot;&gt;brpc&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%86%85%E9%83%A8%E5%AE%9E%E7%8E%B0&quot;&gt;内部实现&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%97%A0%E9%94%81%E8%AF%BB%E5%86%99&quot;&gt;无锁读写&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bthread&quot;&gt;bthread&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#iobuf&quot;&gt;iobuf&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rdma&quot;&gt;rdma&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#rdma-vs-tcp&quot;&gt;rdma vs tcp&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rdma%E5%AE%9E%E7%8E%B0&quot;&gt;rdma实现&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#brpc-rdma&quot;&gt;brpc-rdma&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E9%9B%B6%E6%8B%B7%E8%B4%9D&quot;&gt;零拷贝&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%86%85%E5%AD%98%E6%B1%A0&quot;&gt;内存池&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;brpc&quot;&gt;brpc&lt;/h2&gt;

&lt;h3 id=&quot;内部实现&quot;&gt;内部实现&lt;/h3&gt;

&lt;h3 id=&quot;无锁读写&quot;&gt;无锁读写&lt;/h3&gt;

&lt;h3 id=&quot;bthread&quot;&gt;bthread&lt;/h3&gt;

&lt;h3 id=&quot;iobuf&quot;&gt;iobuf&lt;/h3&gt;

&lt;h2 id=&quot;rdma&quot;&gt;rdma&lt;/h2&gt;

&lt;h3 id=&quot;rdma-vs-tcp&quot;&gt;rdma vs tcp&lt;/h3&gt;

&lt;h3 id=&quot;rdma实现&quot;&gt;rdma实现&lt;/h3&gt;

&lt;h2 id=&quot;brpc-rdma&quot;&gt;brpc-rdma&lt;/h2&gt;

&lt;h3 id=&quot;零拷贝&quot;&gt;零拷贝&lt;/h3&gt;

&lt;h3 id=&quot;内存池&quot;&gt;内存池&lt;/h3&gt;

</content>
 </entry>
 
 <entry>
   <title>baidurpc</title>
   <link href="http://daiwk.github.io/posts/platform-baidurpc.html"/>
   <updated>2018-03-28T00:00:00+00:00</updated>
   <id>/posts/platform-baidurpc</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot;&gt;简介&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#baidurpc的动机&quot;&gt;baidurpc的动机&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#key-components&quot;&gt;key components&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#rpcsocket&quot;&gt;rpc::socket&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#使fd-aba-free且能原子地写入消息&quot;&gt;使fd ABA-free，且能&lt;strong&gt;原子&lt;/strong&gt;地&lt;strong&gt;写入消息&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#wait-free-write&quot;&gt;wait-free write&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#bthread_id&quot;&gt;bthread_id&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#rpcloadbalance&quot;&gt;rpc::LoadBalance&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#调度并发&quot;&gt;调度并发&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#locality-aware&quot;&gt;locality aware&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#consistent-hashing&quot;&gt;consistent hashing&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#rpcnamingservice&quot;&gt;rpc::NamingService&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rpcchannel&quot;&gt;rpc::Channel&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#支持协议&quot;&gt;支持协议：&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#特性&quot;&gt;特性：&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rpcserver&quot;&gt;rpc::Server&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#同端口多协议&quot;&gt;同端口多协议&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#特性&quot;&gt;特性&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#内置服务&quot;&gt;内置服务&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#status&quot;&gt;/status&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#connections&quot;&gt;/connections&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#flags&quot;&gt;/flags&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#vars&quot;&gt;/vars&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#rpcz&quot;&gt;/rpcz&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#pprofprofiler&quot;&gt;/pprof/profiler&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#pprofheap&quot;&gt;/pprof/heap&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#baidurpc的性能&quot;&gt;baidurpc的性能&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#其他语言实现&quot;&gt;其他语言实现&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bthread&quot;&gt;bthread&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#eventloop&quot;&gt;eventloop&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#eventloopthreadpool&quot;&gt;eventloop+threadpool&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#bthread-1&quot;&gt;bthread&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bvar&quot;&gt;bvar&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#base&quot;&gt;base&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/brpc/brpc&quot;&gt;https://github.com/brpc/brpc&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;h3 id=&quot;baidurpc的动机&quot;&gt;baidurpc的动机&lt;/h3&gt;

&lt;p&gt;现有rpc不好用的原因&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;不透明：没开源，所有有一点相关的问题都想找rpc团队跟进&lt;/li&gt;
  &lt;li&gt;难扩展：很难被使用超过2年&lt;/li&gt;
  &lt;li&gt;性能差：在机器较忙或者混部环境下波动很大&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;baidurpc的解决方法&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;/vars, /flags, /rpcz等http内置服务，方便快速排查问题&lt;/li&gt;
  &lt;li&gt;重视接口设计。支持了百度所有rpc协议和多种外部协议，共12种&lt;/li&gt;
  &lt;li&gt;充分考虑多线程，尽量规避全局竞争。较其他rpc大大提升了性能。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;key-components&quot;&gt;key components&lt;/h3&gt;

&lt;h4 id=&quot;rpcsocket&quot;&gt;rpc::socket&lt;/h4&gt;

&lt;h5 id=&quot;使fd-aba-free且能原子地写入消息&quot;&gt;使fd ABA-free，且能&lt;strong&gt;原子&lt;/strong&gt;地&lt;strong&gt;写入消息&lt;/strong&gt;&lt;/h5&gt;

&lt;p&gt;这里的fd指的是socket&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;使fd ABA-free：
linux管理fd时用的是32位的整数，但关掉之后就释放了，可能被另一个进程打开，而指向了另一个设备。所以多线程时，可能同一个整数被多个线程使用，用的时候可能fd已经被关掉或者被重复打开（ABA-连续看到两个值是a，无法判断是之前那个a，还是开始是a，后来变b，又变回a）&lt;/li&gt;
  &lt;li&gt;能原子地写入消息：
在操作系统中，fd是可以原子地写入消息的，但这里的原子指的是byte。例如，有两个线程要往一个fd写消息，但一个消息的长度肯定不止一个byte，所以结果会是两个线程交错地写这个fd。&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;wait-free-write&quot;&gt;wait-free write&lt;/h5&gt;

&lt;p&gt;使每个线程同时能做有用的事，而不是因为锁被一个线程抢占了，其他线程就只能空等(one-free)，而如果用了锁，就两者都不能保证。每秒可以写入500w个16字节的消息。
解决：使用一个64-bid的id。多线程的时候，不能传指针之类的，因为不知道什么时候就被free了，可能访问的是一个非法内存。所以用一个64位的id，并且被一个类似sharedptr的包住，保证不会在中间被析构，当有一个地方将其标记为失效时，其他用到的地方也都会原子地失效&lt;/p&gt;

&lt;h4 id=&quot;bthread_id&quot;&gt;bthread_id&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;ABA-free: 同上，是个64bit的id&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其他rpc存在的问题：
例1：当往一个连接开始写一个request之后，可能response在write这个函数完成之前就回来了。而如果write的时候会访问一个数据，而拿到response后也会访问同一个数据，那就会有问题
例2：当超时时间设置得很短，线程还在写连接的时候，超时时间就到了，而连接里的东西不完整，可能别人会去读它。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;checking every RPC call without global contention(??)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;rpcloadbalance&quot;&gt;rpc::LoadBalance&lt;/h4&gt;

&lt;p&gt;详见&lt;a href=&quot;https://github.com/brpc/brpc/blob/master/docs/cn/lalb.md#doublybuffereddata&quot;&gt;https://github.com/brpc/brpc/blob/master/docs/cn/lalb.md#doublybuffereddata&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;例如，round-robin（rr）。&lt;/p&gt;

&lt;h5 id=&quot;调度并发&quot;&gt;调度并发&lt;/h5&gt;

&lt;p&gt;例如对于rr算法而言，因为naming service对应的下游可能会变，所以每个线程在访问rr的这个列表时，常规的解决方法之一就是用读写锁。例如在查时，用的是读锁，而想要修改时，会用写锁。但在POSIX中，读写锁的性能会特别差。如果临界区不是特别大（例如rr其实临界区非常小），会发现直接用mutex反而比读写锁还要快…&lt;/p&gt;

&lt;p&gt;brpc的解决方法：用一个特殊的双buffer读写锁，读时读前台的buffer，写时写后台的buffer，定时同步两个buffer（前后台切换）。但有一个限制就是前后台不能切换得太快，不然可能出现类似前面讲到的ABA的情况，可能一个线程正在读前台，但中间后台和前台切换了，然后又切换一次。所以会有一个切换频率，最低也要2s，一般是5-10s。但在loadbalance这个场景，这个等待时间是不可接受的。所以这里是一个特殊的双buffer读写锁，只需要和每一个前台线程抢一把thread-local锁就行了。&lt;/p&gt;

&lt;h5 id=&quot;locality-aware&quot;&gt;locality aware&lt;/h5&gt;

&lt;p&gt;最适合混部的分流算法。是一个动态的迭代型的算法，总是能选择期望延时最低的server。例如，优先把流量分给同机房的机器，只有当同机房的机器或者负载达到一定临界值，或者出现故障的时候，才会导流给邻近机房的机器。&lt;/p&gt;

&lt;h5 id=&quot;consistent-hashing&quot;&gt;consistent hashing&lt;/h5&gt;

&lt;p&gt;实现了多种一致性hash，便于各类caching使用。主要是两种：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;基于memcached的&lt;/li&gt;
  &lt;li&gt;基于md5的&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;rpcnamingservice&quot;&gt;rpc::NamingService&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;统一形式： protocol://url&lt;/li&gt;
  &lt;li&gt;例如：bns://node, http://node, file://path, list://server1,server2,…, servername&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;rpcchannel&quot;&gt;rpc::Channel&lt;/h3&gt;

&lt;p&gt;与服务器通信&lt;/p&gt;

&lt;h4 id=&quot;支持协议&quot;&gt;支持协议：&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;hulu-pbrpc,sofa-pbrpc,public/pbrpc,nova-pbrpc,ubrpc(idl/mcpack/compack)&lt;/li&gt;
  &lt;li&gt;http 1.1, hadoop-rpc, memcached&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;特性&quot;&gt;特性：&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;线程安全，都支持异步
也就是说，不需要每个线程都建一个channel。很多开源的都是线程不安全的&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;超时，重试，backup request， 取消
    &lt;ul&gt;
      &lt;li&gt;超时是严格的超时，只要超时就一定结束，结束就一定失败，错误码是timeout。而其他rpc，可能有很多种不同的超时。&lt;/li&gt;
      &lt;li&gt;重试只会发生在连接断开时，而不会在各种超时的时候去不断开连接继续重试（这种情况就是backup request了）&lt;/li&gt;
      &lt;li&gt;backup request就是比如设置了5ms没回来，就再try一下，再等5ms&lt;/li&gt;
      &lt;li&gt;取消，可以随时取消。如果是异步调用，取消后仍然会调用done，返回的错误码是cancel&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;单连接，连接池，短连接，连接caching，连接认证
    &lt;ul&gt;
      &lt;li&gt;单连接，不管建立多少个channel，两点之前都只会建立一个连接。所以就会使用前面讲到的wait-free的fd的socket。&lt;/li&gt;
      &lt;li&gt;连接池，ub、http1.1（keep-alive）、public/pbrpc就是基于这种方式的，相当于一个池子里有n个连接，一个请求想发送时，就从这n个连接里拿一个来用，用完就放回池子里，并不需要大量地重新建立连接。但，如果在一个集群里，如果两两间要建立连接，而单点和单点之间假设要保持很多个连接，那fd、端口号可能就会被打爆了，所以就需要单连接的设计&lt;/li&gt;
      &lt;li&gt;短连接，用完就释放掉，适用于频率非常低的，比如一秒来一次的&lt;/li&gt;
      &lt;li&gt;连接caching，server端可以配一个参数，例如某个连接大于5s，没有任何东西写入，就把连接关掉。client端也有&lt;/li&gt;
      &lt;li&gt;连接认证，支持认证&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;ParallelChannel：更简单的并发访问方式。是一个combo-channel，可以加入一系列的子channel，每个Channel对应一个子分库，这样，对一个parallellchannel的访问，就会变成对这一系列子channel的并发访问（全异步）。可以parallelchannel套parallelchannel&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;channel的析构是不影响这次rpc_call的。所以如果是点对点的channel，那可以在栈上直接声明一个channel变量，然后去异步地rpc_call，再做别的。但如果是在bns上的channel，因为本身这个init是比较重量级的，就不太好这么用，最好还是用一个类成员变量之类的。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Channel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;10.1.1.1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;la&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;rpcserver&quot;&gt;rpc::Server&lt;/h3&gt;

&lt;h4 id=&quot;同端口多协议&quot;&gt;同端口多协议&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;hulu-pbrpc,sofa-pbrpc,public/pbrpc,nova-pbrpc,ubrpc(with adapter,将idl适配成pb，server中还是pb，但client可以仍然用idl)&lt;/li&gt;
  &lt;li&gt;http 1.1, hadoop-rpc, https&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;特性-1&quot;&gt;特性&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;高度并发：只要能并发就并发。除了从一个fd读数据之外，因为操作系统要求读一个fd只能是一个线程读，其实是一个线程不安全的设计。例如一个fd里传来了两个pb，如果pb很大，普通的rpc会等第一个parsefrom完再去解第二个，而brpc可以并发解析。&lt;/li&gt;
  &lt;li&gt;没有io线程和worker线程之分，首先，是没有io线程的，只有worker线程，其次，默认取cpu的核数，会在/flags里记录，可以通过内置的html页面去动态地修改&lt;/li&gt;
  &lt;li&gt;同进程内所有Server/Channel默认共享工作线程。为了提高线程的利用率&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_OWNS_SERVICE&lt;/code&gt;指的是server析构的时候，一起把service给干掉；反之，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SERVER_DOESNT_OWN_SERVICE&lt;/code&gt;指的是service析构时，不把service干掉&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;MyService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SERVER_OWNS_SERVICE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8010&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;内置服务&quot;&gt;内置服务&lt;/h3&gt;

&lt;p&gt;方便监控和调试&lt;/p&gt;

&lt;h4 id=&quot;status&quot;&gt;/status&lt;/h4&gt;

&lt;p&gt;因为同端口支持多协议，所以可以用同一个server端口在浏览器打开。可以看到支持的所有service,还有每个method的对应的指标，各种时间维度的流量、平响之类的&lt;/p&gt;

&lt;h4 id=&quot;connections&quot;&gt;/connections&lt;/h4&gt;

&lt;p&gt;精确到微秒，有各种对端的ip之类的&lt;/p&gt;

&lt;h4 id=&quot;flags&quot;&gt;/flags&lt;/h4&gt;

&lt;p&gt;进程内所有的gflags，有（R）的，就表示可以在浏览器里动态修改，改过的会高亮。如果一个gflag有检查函数，就会动态reload(gflag的特性)&lt;/p&gt;

&lt;h4 id=&quot;vars&quot;&gt;/vars&lt;/h4&gt;

&lt;p&gt;所有用到bvar的都会显示在这里，bvar可以算各种cnt,max,min,90perlatency,80perlatency之类的，类似ubmonitor，但性能好很多，会定时写，noah会动态地读&lt;/p&gt;

&lt;h4 id=&quot;rpcz&quot;&gt;/rpcz&lt;/h4&gt;

&lt;p&gt;会把进程内所有rpc_call列出来，会分配一个唯一的traceid，会有每一次rpc_call的详情&lt;/p&gt;

&lt;h4 id=&quot;pprofprofiler&quot;&gt;/pprof/profiler&lt;/h4&gt;

&lt;p&gt;支持远程做profiling，可以在线做cpu /heap profiling，&lt;/p&gt;

&lt;h4 id=&quot;pprofheap&quot;&gt;/pprof/heap&lt;/h4&gt;

&lt;p&gt;同上&lt;/p&gt;

&lt;h3 id=&quot;baidurpc的性能&quot;&gt;baidurpc的性能&lt;/h3&gt;

&lt;p&gt;好于ub/hulu/sofa/thrift/zeromq…&lt;/p&gt;

&lt;p&gt;why：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;从sys_read开始就是并发的，只要cpu有富余，请求总会在O(1)时间内开始处理&lt;/li&gt;
  &lt;li&gt;wait-free write 写出总会在O(1)时间内返回，高吞吐，特别是包大的时候&lt;/li&gt;
  &lt;li&gt;高并发的loadbalancer（因为locality aware比较复杂，所以要求localbalancer不能全局加锁）&lt;/li&gt;
  &lt;li&gt;没有全局竞争的request tracking(不需要一个全局hash表来区分每次请求，有bthread-id就行了)&lt;/li&gt;
  &lt;li&gt;不区分io与worker，可以减少一次上下文切换&lt;/li&gt;
  &lt;li&gt;dedicated memory allocator: 专门写的针对多线程的内存分配，比tcmalloc更快&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;其他语言实现&quot;&gt;其他语言实现&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;python:
    &lt;ul&gt;
      &lt;li&gt;基于c++包装，通过动态pb互动&lt;/li&gt;
      &lt;li&gt;尽量规避GIL(GIL并不是Python的特性，它是在实现Python解析器(CPython)时所引入的一个概念。参考&lt;a href=&quot;http://python.jobbole.com/81822/&quot;&gt;http://python.jobbole.com/81822/&lt;/a&gt;)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;java：完全用java写的&lt;/li&gt;
  &lt;li&gt;其他脚本语言：推荐用http+json访问baidurpc，因为baidurpc默认开启http&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;bthread&quot;&gt;bthread&lt;/h3&gt;

&lt;p&gt;M:N的线程库，同步的代码可以获得异步的性能，和pthread接口同构&lt;/p&gt;

&lt;p&gt;butex使bthread阻塞函数可同时被bthread和pthread调用，分别阻塞bthread和pthread，相互可唤醒。mutex/semaphore/condition之类的，都是基于futex（POSIX最底层的）的，而butex就相当于futex。&lt;/p&gt;

&lt;p&gt;超快的创建：每个rpc请求建一个bthread,请求结束，bthread就结束。如果一个channel里有3个请求，会用一个bthread读进来，然后建两个bthread去处理后面两个请求，读数据的那个bthread处理第一个请求&lt;/p&gt;

&lt;p&gt;超快的scheduler&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;更好的cache locality: 允许新线程在当前cpu core上执行&lt;/li&gt;
  &lt;li&gt;减少全局竞争：取线程任务靠steal，而非pull&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;eventloop&quot;&gt;eventloop&lt;/h4&gt;

&lt;p&gt;从epoll-wait开始，后面的callback会等上面的结束了再执行，会进到epoll-wait状态，延时不可控，有的callback很慢，所以会等很久。&lt;/p&gt;

&lt;p&gt;适用于高度定制的&lt;/p&gt;

&lt;p&gt;ub_aserver用的就是eventloop，有n个eventloop。&lt;/p&gt;

&lt;h4 id=&quot;eventloopthreadpool&quot;&gt;eventloop+threadpool&lt;/h4&gt;

&lt;p&gt;io线程+worker线程&lt;/p&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;io线程的竞争非常激烈&lt;/li&gt;
  &lt;li&gt;io线程到worker线程有拷贝的开销&lt;/li&gt;
  &lt;li&gt;epoll-control在linux中的实现，时间复杂度要O(n)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;bthread-1&quot;&gt;bthread&lt;/h4&gt;

&lt;p&gt;需要执行callback时，复用io线程&lt;/p&gt;

&lt;h3 id=&quot;bvar&quot;&gt;bvar&lt;/h3&gt;

&lt;p&gt;修改是thread-local的，写数据的时候并不急着读，所以写自己的thread-local就行，不需要全局竞争，只在需要读的时候汇总就行了&lt;/p&gt;

&lt;h3 id=&quot;base&quot;&gt;base&lt;/h3&gt;

&lt;p&gt;基于chromium【&lt;a href=&quot;https://chromium.googlesource.com/chromium/src/+/master/docs/linux_build_instructions.md&quot;&gt;https://chromium.googlesource.com/chromium/src/+/master/docs/linux_build_instructions.md&lt;/a&gt;】和百度的一些公共库写的&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>cv tips</title>
   <link href="http://daiwk.github.io/posts/cv-tips.html"/>
   <updated>2018-03-27T00:00:00+00:00</updated>
   <id>/posts/cv-tips</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;图片分类&lt;/p&gt;

&lt;p&gt;提特征：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;对空间建模&lt;/li&gt;
  &lt;li&gt;各像素当做bag of words&lt;/li&gt;
  &lt;li&gt;对图片做金字塔分割&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;处理特征：&lt;/p&gt;

&lt;p&gt;pca 降维
bootstrap 增维&lt;/p&gt;

&lt;p&gt;视频分类：
提取视频内的interest point&lt;/p&gt;

&lt;p&gt;特征：有区分性和鲁棒性，合适的特征是任务驱动的&lt;/p&gt;

&lt;p&gt;普通的nn是不具备平移不变性的，而cnn可以，因为同一个卷积核在不同位置的权重是一样的&lt;/p&gt;

&lt;p&gt;imgnet:
top5:打5个标签 有一个对就算对&lt;/p&gt;

&lt;p&gt;vgg：不断用3*3的卷积核&lt;/p&gt;

&lt;p&gt;inception: google系的，用各种不同大小的卷积核，然后再拼起来&lt;/p&gt;

&lt;p&gt;分组卷积：嵌入式的地方常用，mobilenet，因为速度快。例如256层，变成8组，每组32层，在cpu上加速很快，在gpu一般&lt;/p&gt;

&lt;p&gt;残差：2015年的paper，在所有网络都有提升&lt;/p&gt;

&lt;p&gt;se-net：引入se-block，在所有网络都有提升==》浅层、中层、深层 分别去做分类，然后把结果merge到一起&lt;/p&gt;

&lt;p&gt;视频分类：先对每个图做光流，然后把rgb和光流图一起去做分类&lt;/p&gt;

&lt;p&gt;用上lstm:
1.拿出最后一个隐状态的结果做分类
2.把每一个时间步的状态做pooling，再分类，会稍微好一点&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;attention : 每一帧做卷积 再做softmax 得到一个一维的权重==》aaai 2018&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;做3d的卷积，直接输入32张图，32*3个channel，然后分块卷积，参数变大了&lt;/p&gt;

&lt;p&gt;空间维度，先用一个cnn提取出1024维，t个时间步，看成1024个channel的图像，高1，宽是t，就变成了一个图像分类问题&lt;/p&gt;

&lt;p&gt;与3d卷积相比，参数少很多&lt;/p&gt;

&lt;p&gt;训练时 会采样，例如只采样10帧（不用采太多），采一样多zhen，只要样本够多；测试可以所有帧&lt;/p&gt;

&lt;p&gt;分类
欠拟合：区分性不足，
解决：把类别分细一点，类间不要有重合
加batchnorm&lt;/p&gt;

&lt;p&gt;过拟合：对于hardsample,不容易区分是哪一类，人工加入正确的样本&lt;/p&gt;

&lt;p&gt;交叉熵等价mle
加入weightdecay等价于map&lt;/p&gt;

&lt;p&gt;学习率优化(2个很有效的方法）：&lt;/p&gt;

&lt;p&gt;cos annealing sgd+nestrov momentom&lt;/p&gt;

&lt;p&gt;plateau anealing + rmsprop&lt;/p&gt;

&lt;p&gt;图文相关性&lt;/p&gt;

&lt;p&gt;百度去搜图 搜出top10的 提出来 当做这个文本的表示&lt;/p&gt;

&lt;p&gt;ocr：一般先做检测再做分类&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>tensorflow基础用法</title>
   <link href="http://daiwk.github.io/posts/platform-tf-basics.html"/>
   <updated>2018-03-21T00:00:00+00:00</updated>
   <id>/posts/platform-tf-basics</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#创建和操作张量&quot;&gt;创建和操作张量&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#矢量加法&quot;&gt;矢量加法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#张量形状&quot;&gt;张量形状&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#广播&quot;&gt;广播&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#矩阵乘法&quot;&gt;矩阵乘法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#张量变形&quot;&gt;张量变形&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#变量初始化和赋值&quot;&gt;变量、初始化和赋值&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pandas&quot;&gt;pandas&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#基本概念&quot;&gt;基本概念&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#访问数据&quot;&gt;访问数据&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#操控数据&quot;&gt;操控数据&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#索引&quot;&gt;索引&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#线性回归&quot;&gt;线性回归&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#定义特征并配置特征列&quot;&gt;定义特征并配置特征列&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#定义目标&quot;&gt;定义目标:&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#配置-linearregressor&quot;&gt;配置 LinearRegressor&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#定义输入函数&quot;&gt;定义输入函数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#训练模型&quot;&gt;训练模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评估模型&quot;&gt;评估模型&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#重新训练&quot;&gt;重新训练&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://developers.google.cn/machine-learning/crash-course/exercises&quot;&gt;https://developers.google.cn/machine-learning/crash-course/exercises&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;创建和操作张量&quot;&gt;创建和操作张量&lt;/h2&gt;

&lt;h3 id=&quot;矢量加法&quot;&gt;矢量加法&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create a six-element vector (1-D tensor).
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Create another six-element vector. Each element in the vector will be
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# initialized to 1. The first argument is the shape of the tensor (more
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# on shapes below).
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Add the two vectors. The resulting tensor is a six-element vector.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;just_beyond_primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Create a session to run the default graph.
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;just_beyond_primes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#[ 3  4  6  8 12 14]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;张量形状&quot;&gt;张量形状&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# A scalar (0-D tensor).
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([])&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# A vector with 3 elements.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# A matrix with 2 rows and 3 columns.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'scalar has shape'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'and value:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 0.0
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'vector has shape'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'and value:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# [0. 0. 0.]
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'matrix has shape'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'and value:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#[[0. 0. 0.]
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# [0. 0. 0.]]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;广播&quot;&gt;广播&lt;/h3&gt;

&lt;p&gt;利用广播，元素级运算中的较小数组会增大到与较大数组具有相同的形状。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果指令需要大小为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[6]&lt;/code&gt; 的张量，则大小为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1]&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt; 的张量可以作为运算数。&lt;/li&gt;
  &lt;li&gt;如果指令需要大小为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[4, 6]&lt;/code&gt; 的张量，则以下任何大小的张量都可以作为运算数。
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;如果指令需要大小为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[3, 5, 6]&lt;/code&gt; 的张量，则以下任何大小的张量都可以作为运算数。
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 5, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[3, 1, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[3, 5, 1]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 1, 1]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[5, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;当张量被广播时，从概念上来说，系统会复制其条目（出于性能考虑，&lt;strong&gt;实际并不复制&lt;/strong&gt;。广播专为实现性能优化而设计）。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create a six-element vector (1-D tensor).
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Create a constant scalar with value 1.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Add the two tensors. The resulting tensor is a six-element vector.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;just_beyond_primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;just_beyond_primes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;矩阵乘法&quot;&gt;矩阵乘法&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create a matrix (2-d tensor) with 3 rows and 4 columns.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Create a matrix with 4 rows and 2 columns.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Multiply `x` by `y`. 
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# The resulting matrix will have 3 rows and 2 columns.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;matrix_multiply_result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matrix_multiply_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# [[35 58]
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#  [35 33]
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#  [ 1 -4]]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;张量变形&quot;&gt;张量变形&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create an 8x2 matrix (2-D tensor).
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Reshape the 8x2 matrix into a 2x8 matrix.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;reshaped_2x8_matrix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;# Reshape the 8x2 matrix into a 4x4 matrix
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;reshaped_4x4_matrix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Original matrix (8x2):&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Reshaped matrix (2x8):&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reshaped_2x8_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Reshaped matrix (4x4):&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reshaped_4x4_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create an 8x2 matrix (2-D tensor).
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Reshape the 8x2 matrix into a 3-D 2x2x4 tensor.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;reshaped_2x2x4_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;# Reshape the 8x2 matrix into a 1-D 16-element tensor.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;one_dimensional_vector&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Original matrix (8x2):&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Reshaped 3-D tensor (2x2x4):&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reshaped_2x2x4_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;1-D vector:&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one_dimensional_vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;变量初始化和赋值&quot;&gt;变量、初始化和赋值&lt;/h3&gt;

&lt;p&gt;创建变量时，可以明确设置一个初始值，也可以使用初始化程序（例如分布）:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Create a variable with the initial value 3.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Create a variable of shape [1], with a random initial value,
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# sampled from a normal distribution with mean 1 and standard deviation 0.35.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.35&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;TensorFlow 的一个特性是变量初始化不是自动进行的。例如，以下代码块会导致错误：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FailedPreconditionError&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Caught expected error: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# Caught expected error:  Attempting to use uninitialized value Variable
&lt;/span&gt;	    &lt;span class=&quot;c1&quot;&gt;#[[Node: _retval_Variable_0_0 = _Retval[T=DT_INT32, index=0, _device=&quot;/job:localhost/replica:0/task:0/device:CPU:0&quot;](Variable)]]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;最简单的方法，是调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;global_variables_initializer&lt;/code&gt;，初始化后，变量的值保留在&lt;strong&gt;同一会话中&lt;/strong&gt;（不过，当&lt;strong&gt;启动新会话时，需要重新初始化&lt;/strong&gt;）&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;initialization&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initialization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Now, variables can be accessed normally, and have values assigned to them.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;要更改变量的值，使用&lt;strong&gt;assign&lt;/strong&gt;指令。请注意，仅创建 assign 指令不会起到任何作用。和初始化一样，&lt;strong&gt;必须运行赋值指令&lt;/strong&gt;才能更新变量值：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# This should print the variable's initial value.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;assignment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# The variable has not been changed yet!
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Execute the assignment op.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Now the variable is updated.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;练习：&lt;/p&gt;

&lt;p&gt;创建一个骰子模拟，在模拟中生成一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10x3&lt;/code&gt; 二维张量，其中：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;列 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; 均存储一个骰子的一次投掷值。&lt;/li&gt;
  &lt;li&gt;列 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt; 存储同一行中列 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; 的值的总和。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如，第一行中可能会包含以下值：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;列 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; 存储 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;列 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; 存储 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;列 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt; 存储 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Task 2: Simulate 10 throws of two dice. Store the results
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# in a 10x3 matrix.
&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# We're going to place dice throws inside two separate
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 10x1 matrices. We could have placed dice throws inside
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# a single 10x2 matrix, but adding different columns of
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# the same matrix is tricky. We also could have placed
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# dice throws inside two 1-D tensors (vectors); doing so
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# would require transposing the result.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;dice1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_uniform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                                        &lt;span class=&quot;n&quot;&gt;minval&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxval&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                        &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;dice2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_uniform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                                        &lt;span class=&quot;n&quot;&gt;minval&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxval&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                        &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# We may add dice1 and dice2 since they share the same shape
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# and size.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;dice_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dice1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dice2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# We've got three separate 10x1 matrices. To produce a single
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 10x3 matrix, we'll concatenate them along dimension 1.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;resulting_matrix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dice1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dice2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dice_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# The variables haven't been initialized within the graph yet,
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# so let's remedy that.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resulting_matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;pandas&quot;&gt;pandas&lt;/h2&gt;

&lt;p&gt;pandas官网：&lt;a href=&quot;http://pandas.pydata.org/pandas-docs/stable/index.html&quot;&gt;http://pandas.pydata.org/pandas-docs/stable/index.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;基本概念&quot;&gt;基本概念&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;pandas&lt;/em&gt; 中的主要数据结构被实现为以下两类：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DataFrame&lt;/code&gt;&lt;/strong&gt;，您可以将它想象成一个关系型数据&lt;strong&gt;表格&lt;/strong&gt;，其中包含多个行和已命名的列。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Series&lt;/code&gt;&lt;/strong&gt;，它是单&lt;strong&gt;一列&lt;/strong&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DataFrame&lt;/code&gt; 中包含一个或多个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Series&lt;/code&gt;，每个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Series&lt;/code&gt; 均有一个名称。&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;city_names&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'San Francisco'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'San Jose'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Sacramento'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;population&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;852469&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1015785&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;485199&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'City name'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Population'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;population&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;加载整个文件，并显示一些统计信息（如：count/mean/std/min/25%/50%/75%/max）：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://storage.googleapis.com/mledu-datasets/california_housing_train.csv&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示前几条记录：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;绘制图表（某一列的值的分布）：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'housing_median_age'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;访问数据&quot;&gt;访问数据&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'City name'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;city_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Population'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;population&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'City name'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 直接访问某一列
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'City name'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 访问某一列的某一行
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 整个表格的第0-1行的所有列的数据
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;pandas的&lt;a href=&quot;http://pandas.pydata.org/pandas-docs/stable/indexing.html&quot;&gt;索引和选择官方文档&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;操控数据&quot;&gt;操控数据&lt;/h3&gt;

&lt;p&gt;可以直接进行算术运算：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;population&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1000.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以通过numpy的函数进行操作&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;population&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以通过apply以及lambda进行操作：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;population&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;dataframe的修改，可以直接操作：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Area square miles'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;46.87&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;176.53&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;97.92&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Population density'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Population'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Area square miles'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Is wide and has saint name'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Area square miles'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'City name'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startswith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'San'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;索引&quot;&gt;索引&lt;/h3&gt;

&lt;p&gt;Series 和 DataFrame 对象也定义了 index 属性，该属性会向每个 Series 项或 DataFrame 行赋一个标识符值。&lt;/p&gt;

&lt;p&gt;默认情况下，在构造时，pandas 会赋可反映源数据顺序的索引值。&lt;strong&gt;索引值在创建后是稳定的&lt;/strong&gt;；也就是说，它们不会因为数据重新排序而发生改变。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;city_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# RangeIndex(start=0, stop=3, step=1)，city_names这一列有0,1,2总共3行
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# RangeIndex(start=0, stop=3, step=1)，整个dataframe有0,1,2总共3行
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;调用 DataFrame.reindex 以手动重新排列各行的顺序。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#City name	Population	Area square miles	Population density	Is wide and has saint name
#2	Sacramento	485199	97.92	4955.055147	False
#0	San Francisco	852469	46.87	18187.945381	False
#1	San Jose	1015785	176.53	5754.177760	True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;重建索引是一种随机排列 DataFrame 的绝佳方式。(参考&lt;a href=&quot;https://blog.csdn.net/you_are_my_dream/article/details/70165384&quot;&gt;https://blog.csdn.net/you_are_my_dream/article/details/70165384&lt;/a&gt;，np.random.permutation与np.random.shuffle有两处不同：
如果传给permutation一个矩阵，它会返回一个洗牌后的矩阵副本；而shuffle只是对一个矩阵进行洗牌，无返回值。 如果传入一个整数，它会返回一个洗牌后的arange。)&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permutation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意：
如果您的 reindex 输入数组包含原始 DataFrame 索引值中没有的值，reindex 会为此类“丢失的”索引添加新行，并在所有对应列中填充 NaN 值：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reindex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#	City name	Population	Area square miles	Population density	Is wide and has saint name
#0	San Francisco	852469.0	46.87	18187.945381	False
#4	NaN	NaN	NaN	NaN	NaN
#5	NaN	NaN	NaN	NaN	NaN
#2	Sacramento	485199.0	97.92	4955.055147	False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;线性回归&quot;&gt;线性回归&lt;/h2&gt;

&lt;h3 id=&quot;定义特征并配置特征列&quot;&gt;定义特征并配置特征列&lt;/h3&gt;

&lt;p&gt;在 TensorFlow 中，我们使用一种称为&lt;strong&gt;“特征列”&lt;/strong&gt;的结构来表示特征的&lt;strong&gt;数据类型&lt;/strong&gt;。特征列仅存储对特征数据的描述；&lt;strong&gt;不包含特征数据&lt;/strong&gt;本身。&lt;/p&gt;

&lt;p&gt;主要有两类数据：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;分类数据&lt;/strong&gt;：一种文字数据。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;数值数据&lt;/strong&gt;：一种数字（整数或浮点数）数据以及您希望视为数字的数据。有时您可能会希望将数值数据（例如邮政编码）视为分类数据。&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Define the input feature: total_rooms.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;total_rooms&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;​&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Configure a numeric feature column for total_rooms.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numeric_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;total_rooms&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;定义目标&quot;&gt;定义目标:&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;median_house_value&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;配置-linearregressor&quot;&gt;配置 LinearRegressor&lt;/h3&gt;

&lt;p&gt;使用```clip_gradients_by_norm``将梯度裁剪应用到我们的优化器。梯度裁剪可确保梯度大小在训练期间不会变得过大，梯度过大会导致梯度下降法失败。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Use gradient descent as the optimizer for training the model.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GradientDescentOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0000001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clip_gradients_by_norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Configure the linear regression model with our feature columns and optimizer.
# Set a learning rate of 0.0000001 for Gradient Descent.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linear_regressor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LinearRegressor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feature_columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_optimizer&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;定义输入函数&quot;&gt;定义输入函数&lt;/h3&gt;

&lt;p&gt;将 Pandas 特征数据转换成 NumPy 数组字典，例如，本例中就是把pandas的一列”total_rooms”:[3.,20.,…,42.]变成一个字典{“total_rooms”:array([3.,20.,…42.])}&lt;/p&gt;

&lt;p&gt;使用 TensorFlow Dataset API 根据我们的数据构建 Dataset 对象，并&lt;strong&gt;将数据拆分成大小为 batch_size 的多批数据&lt;/strong&gt;，以&lt;strong&gt;按照指定周期数 (num_epochs) 进行重复&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;如果 shuffle 设置为 True，则我们会对数据进行随机处理，以便数据在训练期间以随机方式传递到模型。buffer_size 参数会指定 shuffle 将&lt;strong&gt;从中随机抽样的数据集的大小&lt;/strong&gt;(representing the number of elements from this dataset from which the new dataset will sample)。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Trains a linear regression model of one feature.
  
    Args:
      features: pandas DataFrame of features
      targets: pandas DataFrame of targets
      batch_size: Size of batches to be passed to the model
      shuffle: True or False. Whether to shuffle the data.
      num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely
    Returns:
      Tuple of (features, labels) for next data batch
    &quot;&quot;&quot;&lt;/span&gt;
  
    &lt;span class=&quot;c1&quot;&gt;# Convert pandas data into a dict of np arrays.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()}&lt;/span&gt;                                           
 
    &lt;span class=&quot;c1&quot;&gt;# Construct a dataset, and configure batching/repeating
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from_tensor_slices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# warning: 2GB limit
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_epochs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Shuffle the data, if specified
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Return the next batch of data
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_one_shot_iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;训练模型&quot;&gt;训练模型&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear_regressor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;评估模型&quot;&gt;评估模型&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Create an input function for predictions.
# Note: Since we're making just one prediction for each example, we don't 
# need to repeat or shuffle the data here.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prediction_input_fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Call predict() on the linear_regressor to make predictions.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear_regressor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prediction_input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Format predictions as a NumPy array, so we can calculate error metrics.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'predictions'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Print Mean Squared Error and Root Mean Squared Error.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean_squared_error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean_squared_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;root_mean_squared_error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean_squared_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Mean Squared Error (on training data): %0.3f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean_squared_error&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Root Mean Squared Error (on training data): %0.3f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root_mean_squared_error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;RMSE 的一个很好的特性是，它可以在与原目标相同的规模下解读。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;min_house_value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;median_house_value&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;max_house_value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;median_house_value&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;min_max_difference&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_house_value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_house_value&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Min. Median House Value: %0.3f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_house_value&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Max. Median House Value: %0.3f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_house_value&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Difference between Min. and Max.: %0.3f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;min_max_difference&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Root Mean Squared Error: %0.3f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root_mean_squared_error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以通过以下方法获得均匀分布的随机数据样本：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;重新训练&quot;&gt;重新训练&lt;/h2&gt;

&lt;p&gt;在 10 个等分的时间段内使用此函数，以便观察模型在每个时间段的改善情况。&lt;/p&gt;

&lt;p&gt;对于每个时间段，我们都会计算训练损失并绘制相应图表。这可以帮助您判断模型收敛的时间，或者模型是否需要更多迭代。&lt;/p&gt;

&lt;p&gt;此外，我们还会绘制模型随着时间的推移学习的特征权重和偏差项值的曲线图。您还可以通过这种方式查看模型的收敛效果。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;train_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_feature&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;total_rooms&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Trains a linear regression model of one feature.
  
  Args:
    learning_rate: A `float`, the learning rate.
    steps: A non-zero `int`, the total number of training steps. A training step
      consists of a forward and backward pass using a single batch.
    batch_size: A non-zero `int`, the batch size.
    input_feature: A `string` specifying a column from `california_housing_dataframe`
      to use as input feature.
  &quot;&quot;&quot;&lt;/span&gt;
  
  &lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;steps_per_period&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_feature&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;my_feature_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;my_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;median_house_value&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Create feature columns
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;feature_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numeric_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;# Create input functions
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;training_input_fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;prediction_input_fn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  
  &lt;span class=&quot;c1&quot;&gt;# Create a linear regressor object.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;my_optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GradientDescentOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;my_optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clip_gradients_by_norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;linear_regressor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LinearRegressor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;feature_columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_optimizer&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Set up to plot the state of our model's line each period.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Learned Line by Period&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;california_housing_dataframe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coolwarm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Train the model, but do so inside a loop so that we can periodically assess
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# loss metrics.
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Training model...&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;RMSE (on training data):&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;root_mean_squared_errors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;period&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;periods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Train the model, starting from the prior state.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;linear_regressor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;training_input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steps_per_period&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Take a break and compute predictions.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear_regressor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prediction_input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'predictions'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Compute loss.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;root_mean_squared_error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean_squared_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Occasionally print the current loss.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;  period %02d : %0.2f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;period&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root_mean_squared_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Add the loss metrics from this period to our list.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;root_mean_squared_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root_mean_squared_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Finally, track the weights and biases over time.
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Apply some math to ensure that the data and line are plotted neatly.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;y_extents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()])&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear_regressor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'linear/linear_model/%s/weights'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;linear_regressor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'linear/linear_model/bias_weights'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;x_extents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_extents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x_extents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maximum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_extents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                      &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y_extents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_extents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_extents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_extents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;period&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; 
  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Model training finished.&quot;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Output a graph of loss metrics over periods.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'RMSE'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Periods'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Root Mean Squared Error vs. Periods&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tight_layout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root_mean_squared_errors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Output a table with calibration data.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;calibration_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;calibration_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;predictions&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;calibration_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;targets&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calibration_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;describe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Final RMSE (on training data): %0.2f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root_mean_squared_error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;train_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.00002&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tf-basic-rmse-1.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tf-basic-rmse-2.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>IndRNN</title>
   <link href="http://daiwk.github.io/posts/dl-indrnn.html"/>
   <updated>2018-03-21T00:00:00+00:00</updated>
   <id>/posts/dl-indrnn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：
&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650739594&amp;amp;idx=3&amp;amp;sn=91c8462a57965bddeab01e3299ef7669&amp;amp;chksm=871ad7f4b06d5ee2aa1fb37d410d530d80d33c82f4f155782b30c83b51c82f2ba6d55fe208b1&amp;amp;scene=0&amp;amp;pass_ticket=tbnVq1TrpflpVcTq7fMUU%2Fsr7OcKdxw7WB2otHwDM5bdv%2FOKgcgVhENYBFC%2BhG79#rd&quot;&gt;学界 | 新型循环神经网络IndRNN：可构建更长更深的RNN（附GitHub实现）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/batzner/indrnn&quot;&gt;https://github.com/batzner/indrnn&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;澳大利亚伍伦贡大学联合电子科技大学提出一种新型的循环神经网络IndRNN，不仅可以解决传统RNN所存在的&lt;strong&gt;梯度消失和梯度爆炸&lt;/strong&gt;问题，还&lt;strong&gt;学习长期依赖&lt;/strong&gt;关系；此外，借助&lt;strong&gt;relu&lt;/strong&gt;等非饱和激活函数，训练之后IndRNN会变得非常&lt;strong&gt;鲁棒&lt;/strong&gt;，并且通过&lt;strong&gt;堆叠多层IndRNN&lt;/strong&gt;还可以构建比现有RNN&lt;strong&gt;更深&lt;/strong&gt;的网络。实验结果表明，与传统的RNN和LSTM相比，使用IndRNN可以在各种任务中取得更好的结果。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>deeplearning.ai笔记</title>
   <link href="http://daiwk.github.io/posts/dl-dl-ai-notes.html"/>
   <updated>2018-03-09T00:00:00+00:00</updated>
   <id>/posts/dl-dl-ai-notes</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#资料1&quot;&gt;资料1&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#资料2by-mbadry1&quot;&gt;资料2：by mbadry1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;资料1&quot;&gt;资料1&lt;/h2&gt;
&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650738808&amp;amp;idx=1&amp;amp;sn=950b3a51af28a0434018343a2810bc82&amp;amp;chksm=871ad406b06d5d1000a246ea73acb9bec34e9b1de8f19fc90e473f071a2c40667676a174eb1b&amp;amp;scene=0&amp;amp;pass_ticket=UMTz5Pq1gpNBExzY%2BETXpe50iIGGCVgdqjdO7JSm2INe6uUJece4gIrHttgI89bP#rd&quot;&gt;这是一份优美的信息图，吴恩达点赞的deeplearning.ai课程总结&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;下载地址
&lt;a href=&quot;https://daiwk.github.io/assets/deeplearning.ai-notes.pdf&quot;&gt;https://daiwk.github.io/assets/deeplearning.ai-notes.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;资料2by-mbadry1&quot;&gt;资料2：by mbadry1&lt;/h2&gt;

&lt;p&gt;github地址：&lt;a href=&quot;https://github.com/mbadry1/DeepLearning.ai-Summary&quot;&gt;https://github.com/mbadry1/DeepLearning.ai-Summary&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pdf打包下载地址(&lt;strong&gt;!!大赞!!&lt;/strong&gt;)：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/lecture_notes_dl.ai_by_mbadry1.tar.gz&quot;&gt;https://daiwk.github.io/assets/lecture_notes_dl.ai_by_mbadry1.tar.gz&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>机器学习速成课程</title>
   <link href="http://daiwk.github.io/posts/ml-google-tutorial-tf-ml.html"/>
   <updated>2018-03-01T00:00:00+00:00</updated>
   <id>/posts/ml-google-tutorial-tf-ml</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考
&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650738445&amp;amp;idx=1&amp;amp;sn=b2dbe3aa45c253e37b4a81a0ba3dc4a0&amp;amp;chksm=871acb73b06d426503929c75ff2c8112e04ed8c0acfc80e03d64f36010da255b90e3ea9e9628&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0301be8Bpd4zfBivY8JzrFcg&amp;amp;pass_ticket=5SrYQ%2FpXD7KSyz%2F6zJ88Oww%2F%2BOVrTzMYGKLCTqOeAYXx7g%2F0un258aZ0W3JSn10l#rd&quot;&gt;学习了！谷歌今日上线基于TensorFlow的机器学习速成课程（中文版）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;课程地址：&lt;a href=&quot;https://developers.google.cn/machine-learning/crash-course/&quot;&gt;https://developers.google.cn/machine-learning/crash-course/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;机器学习从入门到放弃：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU2ODAyNTM1Mw==&amp;amp;mid=2247483676&amp;amp;idx=1&amp;amp;sn=e1bba917297d7ef62b3c6e794245ecdb&amp;amp;chksm=fc95097acbe2806ce68f7f2cb76277c8e94d07717ffe110f5109092023cf63bf8db96ddc892e&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0430JFZlK1yQIS8hUz1qlAGC&amp;amp;pass_ticket=rZ%2B02uOGACG2lwYRNxG50ushV5eVdE7rvTmUhNnfkKINrCbptwr%2FplKNjNApCJy9#rd&quot;&gt;机器学习- 从入门到放弃&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;百度云地址：&lt;a href=&quot;https://pan.baidu.com/s/1tNXYQNadAsDGfPvuuj7_Tw&quot;&gt;https://pan.baidu.com/s/1tNXYQNadAsDGfPvuuj7_Tw&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap5 基于时间差分的强化学习方法</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap5.html"/>
   <updated>2018-02-19T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap5</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-%E5%9F%BA%E4%BA%8E%E6%97%B6%E9%97%B4%E5%B7%AE%E5%88%86%E7%9A%84%E5%BC%BA%E5%8C%96%E5%AD%A6%E4%B9%A0%E6%96%B9%E6%B3%95&quot;&gt;1. 基于时间差分的强化学习方法&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-python%E5%92%8Cgym%E7%9A%84%E5%AE%9E%E4%BE%8B&quot;&gt;2. python和gym的实例&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-基于时间差分的强化学习方法&quot;&gt;1. 基于时间差分的强化学习方法&lt;/h2&gt;

&lt;p&gt;sarsa和Qlearning的最大区别在于:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;sarsa是用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon -greedy\)&lt;/code&gt;得到动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;和下一个状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s'\)&lt;/code&gt;，并对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s'\)&lt;/code&gt;也使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon -greedy\)&lt;/code&gt;得到动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a'\)&lt;/code&gt;和状态行为值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s',a')\)&lt;/code&gt;，并计算TD目标&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r+\gamma Q(s',a')\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Qlearning是用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon -greedy\)&lt;/code&gt;得到动作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;和下一个状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s'\)&lt;/code&gt;【这部分和sarsa一样】，然后计算TD目标&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r+\gamma max_{a'}Q(s',a')\)&lt;/code&gt;，可见这里不再是通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon-greedy\)&lt;/code&gt;选出的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a'\)&lt;/code&gt;来算的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s',a')\)&lt;/code&gt;，而是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(max_{a'}Q(s',a')\)&lt;/code&gt;，也就是强制选使Q最大的那个action带来的Q，而非随机策略。&lt;/li&gt;
  &lt;li&gt;注意，这里二者的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s',a')\)&lt;/code&gt;都是基于第一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon-greedy\)&lt;/code&gt;得到的新状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s'\)&lt;/code&gt;来搞的。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-python和gym的实例&quot;&gt;2. python和gym的实例&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/daiwk/reinforcement-learning-code/blob/master/qlearning.py&quot;&gt;https://github.com/daiwk/reinforcement-learning-code/blob/master/qlearning.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gym&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'GridWorld-v0'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#grid=env.env                     #创建网格世界
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;states&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getStates&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;#获得网格世界的状态空间
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getAction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;#获得网格世界的动作空间
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gamma&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getGamma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;#获得折扣因子
#计算当前策略和最优策略之间的差
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;best&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#储存最优行为值函数
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_best&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;best_qfunc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;eles&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;best&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#计算值函数的误差
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;compute_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sum1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;best&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sum1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum1&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#  贪婪策略
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;greedy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;amax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;qmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 扫描动作空间得到最大动作值函数Q(s,a)
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;qmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;amax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;amax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;#######epsilon贪婪策略
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;epsilon_greedy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;amax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;qmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#扫描动作空间得到最大动作值函数
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;qmax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;amax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#概率部分，除了max的为加上1-epsilon，其他的概率一样
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pro&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pro&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;amax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pro&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;##选择动作
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pro&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;qlearning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_iter1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;#行为值函数为字典
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#初始化行为值函数为0
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iter1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_iter1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compute_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;#初始化初始状态
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 应该改成epsilon-greedy?
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#与环境进行一次交互，从环境中得到新的状态及回报
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;key1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#s1处的最大动作
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;greedy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;key1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 这个时候的qfunc[key1]就是max的
&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;#利用qlearning方法更新值函数，注意！！这里更新的是key，而不是key1
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gamma&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#转到下一个状态
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon_greedy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-.,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;q alpha=%2.1f epsilon=%2.1f&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;主流程的代码在&lt;a href=&quot;https://github.com/daiwk/reinforcement-learning-code/blob/master/learning_and_test.py&quot;&gt;https://github.com/daiwk/reinforcement-learning-code/blob/master/learning_and_test.py&lt;/a&gt;中。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gym&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;qlearning&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gym&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrappers&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#main函数
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;# grid = grid_mdp.Grid_Mdp()  # 创建网格世界
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#states = grid.getStates()  # 获得网格世界的状态空间
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#actions = grid.getAction()  # 获得网格世界的动作空间
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sleeptime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;terminate_states&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getTerminate_states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#读入最优值函数
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;read_best&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    plt.figure(figsize=(12,6))
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#训练
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qlearning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_iter1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#画图
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;number of iterations&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;square errors&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;# 显示误差图像
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleeptime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#学到的值函数
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;the qfunc of key (%s) is %f&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#学到的策略为：
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;the learned policy is:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;terminate_states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;the state %d is terminate_states&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;the policy of state %d is (%s)&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;greedy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 设置系统初始状态
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setAction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 对训练好的策略进行测试
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrappers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Monitor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'./robotfindgold'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;force&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 记录回放动画
&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;#随机初始化，寻找金币的路径
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#随机初始化
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleeptime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#判断随机状态是否在终止状态中
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;terminate_states&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;reach the terminate state %d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;greedy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qfunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleeptime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%d_%s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# 与环境进行一次交互，从环境中得到新的状态及回报
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;c1&quot;&gt;#打印终止状态
&lt;/span&gt;                    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleeptime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;reach the terminate state %d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# s1处的最大动作
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;s0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap3 基于模型的动态规划方法</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap3.html"/>
   <updated>2018-02-19T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap3</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-基于模型的动态方法理论&quot;&gt;1. 基于模型的动态方法理论&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-动态规划&quot;&gt;1.1 动态规划&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-策略迭代算法&quot;&gt;1.2 策略迭代算法&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#121-策略评估算法&quot;&gt;1.2.1 策略评估算法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#122-贪婪策略&quot;&gt;1.2.2 贪婪策略&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#123-策略迭代算法&quot;&gt;1.2.3 策略迭代算法&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-值函数迭代算法&quot;&gt;1.3 值函数迭代算法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#14-解决最优控制问题的三种算法&quot;&gt;1.4 解决最优控制问题的三种算法&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#141-变分法原理&quot;&gt;1.4.1 变分法原理&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#142-庞特里亚金最大值原理&quot;&gt;1.4.2 庞特里亚金最大值原理&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#143-动态规划方法&quot;&gt;1.4.3 动态规划方法&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-动态规划中的数学基础&quot;&gt;2. 动态规划中的数学基础&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-线性方程组的迭代解法&quot;&gt;2.1 线性方程组的迭代解法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-压缩映射证明策略评估的收敛性&quot;&gt;2.2 压缩映射证明策略评估的收敛性&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-基于gym的编程实例&quot;&gt;3. 基于gym的编程实例&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-最优控制与强化学习比较&quot;&gt;4. 最优控制与强化学习比较&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-基于模型的动态方法理论&quot;&gt;1. 基于模型的动态方法理论&lt;/h2&gt;

&lt;p&gt;一个完整的已知模型的马尔科夫决策过程可以用元组&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S,A,P,r,\gamma\)&lt;/code&gt;表示。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;为状态集，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;为动作集，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;为&lt;strong&gt;转移概率&lt;/strong&gt;【对应环境和智能体的&lt;strong&gt;模型&lt;/strong&gt;】，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;为回报函数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;为折扣因子，用于计算累积回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R=\sum_{t=0}^T\gamma ^tr_t\)&lt;/code&gt;。若&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T\)&lt;/code&gt;为有限值，强化学习过程称为有限范围强化学习。若&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T=\infty\)&lt;/code&gt;，称为无限范围强化学习。下面以有限范围强化学习为例。&lt;/p&gt;

&lt;p&gt;强化学习的目标是找到最优策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;，使得累积回报的期望最大。策略是状态到动作的映射&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi:s\rightarrow a\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau \)&lt;/code&gt;表示从状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;到最终状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_T\)&lt;/code&gt;的一个序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau:s_t,s_{t+1},...,s_T\)&lt;/code&gt;，目标函数是累积回报函数的期望&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\int R(\tau)p_{\pi}(\tau)d\tau\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;所以强化学习的目标是：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\underset{\pi}{max}\int R(\tau)p_{\pi}(\tau)d\tau\)&lt;/code&gt;，最终目标是找到最优策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi ^*:s\rightarrow u^*\)&lt;/code&gt;。这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u\)&lt;/code&gt;指的是决策序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_0^*\rightarrow u_1^*\rightarrow ...u_T^*\)&lt;/code&gt;，所以，广义上来讲，强化学习就是&lt;strong&gt;找到一个决策序列&lt;/strong&gt;，使得&lt;strong&gt;目标函数最优&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;强化学习的分类如下图所示：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/rl-classifications.jpg&quot; style=&quot;max-height: 220px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;根据转移概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;是否已知，可以分为&lt;strong&gt;基于模型&lt;/strong&gt;的动态规划方法，和&lt;strong&gt;基于无模型&lt;/strong&gt;的强化学习方法。&lt;/p&gt;

&lt;p&gt;这两种方法都包括&lt;strong&gt;策略迭代&lt;/strong&gt;算法、&lt;strong&gt;值迭代&lt;/strong&gt;算法和&lt;strong&gt;策略搜索&lt;/strong&gt;算法。而基于&lt;strong&gt;无模型&lt;/strong&gt;的强化学习方法中，每类方法又分为&lt;strong&gt;online&lt;/strong&gt;和&lt;strong&gt;offline&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;11-动态规划&quot;&gt;1.1 动态规划&lt;/h3&gt;

&lt;p&gt;基于模型的强化学习可以用动态规划的思想来解决。“动态”，指序列和状态的变化;“规划”，指优化，如线性优化、二次优化或非线性优化。&lt;/p&gt;

&lt;p&gt;利用动态规划可解决的问题需要满足两个条件：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;整个优化问题可以&lt;strong&gt;分解&lt;/strong&gt;为&lt;strong&gt;多个子优化&lt;/strong&gt;问题。&lt;/li&gt;
  &lt;li&gt;子优化问题的&lt;strong&gt;解&lt;/strong&gt;可以&lt;strong&gt;被存储&lt;/strong&gt;和&lt;strong&gt;重复利用&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;第二章说到，强化学习可以利用马尔科夫决策过程来描述，利用贝尔曼最优性原理得到贝尔曼最优化方程：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\\upsilon ^*(s)=\underset{a}{max}R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon ^*(s')
\\q^*(s,a)=R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\underset{a}{max}q^*(s',a')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可见马尔科夫决策问题符合使用动态规划的两个条件，所以可以用动态规划来解决。&lt;/p&gt;

&lt;p&gt;动态规划的核心是&lt;strong&gt;找到最优值函数。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;由上一章可以得到如下状态值函数的计算方法：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon_{\pi}(s)=\sum_{a\in A}\pi(a|s)(R^a_{s}+\gamma \sum_{s'\in S}P^a_{ss'}\upsilon_{\pi}(s'))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;从上式中可见，&lt;strong&gt;状态s处的值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon_{\pi}(s)\)&lt;/code&gt;&lt;/strong&gt;可以用&lt;strong&gt;后继状态的值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon_{\pi}(s')\)&lt;/code&gt;&lt;/strong&gt;来表示，而&lt;strong&gt;后继状态的值函数&lt;/strong&gt;是&lt;strong&gt;未知&lt;/strong&gt;的，所以这就是&lt;strong&gt;bootstrap算法&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;上式中，对于&lt;strong&gt;模型已知&lt;/strong&gt;的强化学习算法，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R^a_{s}\)&lt;/code&gt;都是已知的，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(a|s)\)&lt;/code&gt;是要评估的策略，是指定的，也是已知的。所以&lt;strong&gt;未知数&lt;/strong&gt;就是&lt;strong&gt;值函数&lt;/strong&gt;，未知数的个数为状态的总数，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(|S|\)&lt;/code&gt;表示。&lt;/p&gt;

&lt;p&gt;下面介绍如何求解上述公式(其实就是&lt;strong&gt;关于值函数&lt;/strong&gt;的&lt;strong&gt;线性方程组&lt;/strong&gt;)&lt;/p&gt;

&lt;h3 id=&quot;12-策略迭代算法&quot;&gt;1.2 策略迭代算法&lt;/h3&gt;

&lt;h4 id=&quot;121-策略评估算法&quot;&gt;1.2.1 策略评估算法&lt;/h4&gt;

&lt;p&gt;使用高斯-赛德尔迭代算法：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon_{k+1}(s)=\sum_{a\in A}\pi(a|s)(R^a_{s}+\gamma \sum_{s'\in S}P^a_{ss'}\upsilon _{k}(s'))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;算法步骤如下：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;输入：需要评估的策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;状态转移概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;回报函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R^a_s\)&lt;/code&gt;，折扣因子&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;初始化值函数：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon(s)=0\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;repeat k=0,1,…&lt;/li&gt;
    &lt;li&gt;for s in S:&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon_{k+1}(s)=\sum _{a\in A}\pi(a|s)(R^a_{s}+\gamma \sum_{s'\in S}P^a_{ss'}\upsilon_k(s'))\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;until &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon_{k+1}=\upsilon_k\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;输出：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon(s)\)&lt;/code&gt;&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;注意：&lt;strong&gt;每次迭代&lt;/strong&gt;都需要&lt;strong&gt;对状态集进行一次遍历&lt;/strong&gt;，以评估每个状态的值函数。&lt;/p&gt;

&lt;h4 id=&quot;122-贪婪策略&quot;&gt;1.2.2 贪婪策略&lt;/h4&gt;

&lt;h4 id=&quot;123-策略迭代算法&quot;&gt;1.2.3 策略迭代算法&lt;/h4&gt;

&lt;h3 id=&quot;13-值函数迭代算法&quot;&gt;1.3 值函数迭代算法&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;输入：状态转移概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;，回报函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R^a_{s}\)&lt;/code&gt;，折扣因子&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;，初始化值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon(s)=0\)&lt;/code&gt;，初始化策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi_0\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Repeat &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l=0,1,...\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;for every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt; do&lt;/li&gt;
    &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon _{l+1}(s)=\underset{a}{max}R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon _l(s')\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Until &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon _{l+1}=\upsilon _l\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;输出：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(s)=argmax_aR^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon _l(s')\)&lt;/code&gt;&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;14-解决最优控制问题的三种算法&quot;&gt;1.4 解决最优控制问题的三种算法&lt;/h3&gt;

&lt;h4 id=&quot;141-变分法原理&quot;&gt;1.4.1 变分法原理&lt;/h4&gt;

&lt;h4 id=&quot;142-庞特里亚金最大值原理&quot;&gt;1.4.2 庞特里亚金最大值原理&lt;/h4&gt;

&lt;h4 id=&quot;143-动态规划方法&quot;&gt;1.4.3 动态规划方法&lt;/h4&gt;

&lt;h2 id=&quot;2-动态规划中的数学基础&quot;&gt;2. 动态规划中的数学基础&lt;/h2&gt;

&lt;h3 id=&quot;21-线性方程组的迭代解法&quot;&gt;2.1 线性方程组的迭代解法&lt;/h3&gt;

&lt;h3 id=&quot;22-压缩映射证明策略评估的收敛性&quot;&gt;2.2 压缩映射证明策略评估的收敛性&lt;/h3&gt;

&lt;h2 id=&quot;3-基于gym的编程实例&quot;&gt;3. 基于gym的编程实例&lt;/h2&gt;

&lt;h2 id=&quot;4-最优控制与强化学习比较&quot;&gt;4. 最优控制与强化学习比较&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>TVM</title>
   <link href="http://daiwk.github.io/posts/platform-TVM.html"/>
   <updated>2018-02-17T00:00:00+00:00</updated>
   <id>/posts/platform-TVM</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737992&amp;amp;idx=1&amp;amp;sn=5cb431b3789e18ee99445c2cb1a6c418&amp;amp;chksm=871ac936b06d40201051c1974c37895d61f7e043a136a60d3b97c56fb37e4d4bd91edd41ef08&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0217596nfHqrHXbfBXpEuthY&amp;amp;pass_ticket=30u4uwMKJ%2BI0sZ9LYTxHtdIL%2FC2GRriWYYpDd0Au%2FAR%2BCjnjRgfEgecp4k4pwcLr#rd&quot;&gt;陈天奇等人提出TVM：深度学习自动优化代码生成器&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/hec4TbUDsKLxtncZqMsqAw&quot;&gt;陈天奇任CTO，TVM团队成立OctoML：让任何硬件都能部署机器学习模型&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap2 马尔科夫决策过程</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap2.html"/>
   <updated>2018-02-16T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap2</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-马尔科夫决策过程理论&quot;&gt;1. 马尔科夫决策过程理论&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-马尔科夫性&quot;&gt;1.1 马尔科夫性&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-马尔科夫过程&quot;&gt;1.2 马尔科夫过程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-马尔科夫决策过程&quot;&gt;1.3 马尔科夫决策过程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#14-状态值函数与状态-行为值函数&quot;&gt;1.4 状态值函数与状态-行为值函数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#15-状态值函数与状态-行为值函数的贝尔曼方程&quot;&gt;1.5 状态值函数与状态-行为值函数的贝尔曼方程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#16-最优状态值函数与最优状态-行为值函数及其贝尔曼最优方程&quot;&gt;1.6 最优状态值函数与最优状态-行为值函数及其贝尔曼最优方程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#17-强化学习形式化描述&quot;&gt;1.7 强化学习形式化描述&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-mdp中的概率学基础&quot;&gt;2. MDP中的概率学基础&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-期望与方差&quot;&gt;2.1 期望与方差&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#期望&quot;&gt;期望&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#方差&quot;&gt;方差&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-随机策略&quot;&gt;2.2 随机策略&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#221-贪婪策略&quot;&gt;2.2.1 贪婪策略&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#222-\\varepsilon--greedy\策略&quot;&gt;2.2.2 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon -greedy\)&lt;/code&gt;策略&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#223-高斯策略&quot;&gt;2.2.3 高斯策略&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#224-玻尔兹曼分布&quot;&gt;2.2.4 玻尔兹曼分布&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-基于gym的mdp实例&quot;&gt;3. 基于gym的MDP实例&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-马尔科夫决策过程理论&quot;&gt;1. 马尔科夫决策过程理论&lt;/h2&gt;

&lt;h3 id=&quot;11-马尔科夫性&quot;&gt;1.1 马尔科夫性&lt;/h3&gt;

&lt;p&gt;系统的下一个状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{t+1}\)&lt;/code&gt;&lt;strong&gt;仅与当前状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;有关&lt;/strong&gt;，与之前的状态无关。&lt;/p&gt;

&lt;p&gt;定义：状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;是马尔科夫的，当且仅当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P[s_{t+1}|s_t]=P[s_{t+1}|s_1,...,s_t]\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;随机过程就是随机变量序列。如果随机变量序列的&lt;strong&gt;每个状态&lt;/strong&gt;都是&lt;strong&gt;马尔科夫&lt;/strong&gt;的，那么此随机过程就是&lt;strong&gt;马尔科夫随机过程&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;12-马尔科夫过程&quot;&gt;1.2 马尔科夫过程&lt;/h3&gt;

&lt;p&gt;马尔科夫过程是一个二元组&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((S,P)\)&lt;/code&gt;，且满足：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;是有限状态集合，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;是状态转移概率。状态转移概率矩阵如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P=\begin{bmatrix}
P_{11} &amp;amp; ... &amp;amp; P_{1n} \\ 
\vdots  &amp;amp; \vdots  &amp;amp; \vdots \\ 
P_{n1} &amp;amp; ... &amp;amp; P_{nn}
\end{bmatrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;由若干个状态组成的序列称为马尔科夫链。当给定状态转移概率时，从某个状态出发存在多条马尔科夫链。&lt;/p&gt;

&lt;h3 id=&quot;13-马尔科夫决策过程&quot;&gt;1.3 马尔科夫决策过程&lt;/h3&gt;

&lt;p&gt;马尔科夫决策过程由元组&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((S,A,P,R,\gamma)\)&lt;/code&gt;描述，其中：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;为有限的状态集&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;为有限的动作集&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;为状态转移概率&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;为回报函数&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;为&lt;strong&gt;折扣因子&lt;/strong&gt;，用于计算&lt;strong&gt;累积回报&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;区别于马尔科夫过程，马尔科夫决策过程的&lt;strong&gt;态转移概率&lt;/strong&gt;是&lt;strong&gt;包含动作&lt;/strong&gt;的，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P^a_{ss'}=P[S_{t+1}=s'|S_t=s,A_t=a]\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;强化学习的目标是给定一个马尔科夫决策过程，寻找最优策略。&lt;strong&gt;策略&lt;/strong&gt;指的是，&lt;strong&gt;状态到动作的映射&lt;/strong&gt;，用符号&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;表示，给定状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;时，动作集上的一个分布，即：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi (a|s)=p[A_i=a|S_t=s]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;含义是，策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;在每个状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;指定一个动作概率。如果给出的策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;是确定性的，那么策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;在每个状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;指定一个确定的动作。注，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum _{a\in A}\pi(s,a)=1\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;概率在强化学习中的作用：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;强化学习的策略往往是随机策略。可以将&lt;strong&gt;探索&lt;/strong&gt;耦合到&lt;strong&gt;采样&lt;/strong&gt;的过程中。&lt;strong&gt;探索，指机器人尝试其他动作以便找到更好的策略&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;实际应用中，存在各种噪声，这些噪声大都服从正态分布，可以用概率的知识&lt;strong&gt;去掉噪声&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;给定一个策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;&lt;/strong&gt;时，可以计算&lt;strong&gt;累积回报&lt;/strong&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
G_t=R_{t+1}+\gamma R_{t+2}+...=\sum _{k=0}^{\infty }\gamma ^kR_{i+k+1}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;当给定策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;时，假设从状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;出发，可能有很多个状态序列，所以对应的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_t\)&lt;/code&gt;也有多个可能值。为了评价状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;的价值，需要一个确定的量来描述。而&lt;strong&gt;累积回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_t\)&lt;/code&gt;是一个随机变量&lt;/strong&gt;，不是确定值，但其&lt;strong&gt;期望&lt;/strong&gt;是一个&lt;strong&gt;确定值&lt;/strong&gt;，因此可以做为状态值函数的定义。&lt;/p&gt;

&lt;h3 id=&quot;14-状态值函数与状态-行为值函数&quot;&gt;1.4 状态值函数与状态-行为值函数&lt;/h3&gt;

&lt;p&gt;当智能体&lt;strong&gt;采用策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;时&lt;/strong&gt;，累积回报服从一个分布，&lt;strong&gt;累积回报&lt;/strong&gt;在&lt;strong&gt;状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;处的期望值&lt;/strong&gt;定义&lt;strong&gt;状态值函数&lt;/strong&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon _{\pi}(s)=E_{\pi}[\sum _{k=0}^{\infty }\gamma ^kR_{t+k+1}|S_t=s]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;注：&lt;strong&gt;状态值函数是与策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;相对应的&lt;/strong&gt;，因为策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;决定了累积回报&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;的状态分布。&lt;/p&gt;

&lt;p&gt;状态-行为值函数为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q_{\pi}(s,a)=E_{\pi}[\sum _{k=0}^{\infty}\gamma ^kR_{t+k+1}|S_t=s,A_t=a]
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;15-状态值函数与状态-行为值函数的贝尔曼方程&quot;&gt;1.5 状态值函数与状态-行为值函数的贝尔曼方程&lt;/h3&gt;

&lt;p&gt;贝尔曼方程（Bellman Equation）也被称作动态规划方程（Dynamic Programming Equation）。&lt;/p&gt;

&lt;p&gt;状态值函数的贝尔曼方程：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{split}
\\\upsilon(S_t)&amp;amp;=\upsilon(s)=E[G_t|S_t=s]
\\&amp;amp;=E[R_{t+1}+\gamma R_{t+2}+...|S_t=s]
\\&amp;amp;=E[R_{t+1}+\gamma (R_{t+2}+\gamma R_{t+3}+...)|S_t=s]
\\&amp;amp;=E[R_{t+1}+\gamma G_{t+1}|S_t=s]
\\&amp;amp;=E[R_{t+1}+\gamma \upsilon(S_{t+1})|S_t=s]
\end{split}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;最后一个等号的证明（书P23，有小改……）&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{split}
\\\upsilon(S_t)&amp;amp;=E_{S_t,S_{t+1},...}(R_{t+1}+\gamma G_{t+1}|S_t=s)
\\&amp;amp;=E_{S_t}(R_{t+1})+\gamma E_{S_{t+1},...}(G_{t+1})
\\&amp;amp;=E_{S_t}(R_{t+1}+\gamma \upsilon(S_{t+1}))
\\&amp;amp;=E(R_{t+1}+\gamma\upsilon(S_{t+1}))
\end{split}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;同理，状态-行为值函数的贝尔曼方程：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q_{\pi}(s,a)=E_{\pi}[R_{t+1}+\gamma q(S_{t+1},A_{t+1})|S_t=s,A_t=a]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;状态值函数与状态-行为值函数的关系：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon_{\pi}(s)=\sum_{a\in A}\pi(a|s)q_{\pi}(s,a)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而(其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;表示从状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;开始，采取行动&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;，移动到状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s'\)&lt;/code&gt;的概率)&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q_{\pi}(s,a)=R^a_{s}+\gamma \sum _{s'}P^a_{ss'}\upsilon _{\pi}(s')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因此，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon_{\pi}(s)=\sum_{a\in A}\pi(a|s)(R^a_{s}+\gamma \sum_{s'\in S}P^a_{ss'}\upsilon_{\pi}(s'))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon_{\pi}(s')=\sum _{a'\in A}\pi(a'|s')q_{\pi}(s',a')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，状态-行为值函数是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q_{\pi}(s,a)=R^a_{s}+\gamma \sum_{s'\in S}P^a_{ss'}\sum _{a'\in A}\pi(a'|s')q_{\pi}(s',a')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;示例：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/state-value-function-demo.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;图中，&lt;strong&gt;空心&lt;/strong&gt;圆圈中的数值为该状态下的&lt;strong&gt;值函数&lt;/strong&gt;，&lt;strong&gt;实心&lt;/strong&gt;圆圈表示&lt;strong&gt;状态-行为对&lt;/strong&gt;。除了实心圆圈部分，其他状态转移&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(a|s)=0.5,\gamma =1\)&lt;/code&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_4\)&lt;/code&gt;通过睡觉，可以到达&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_5\)&lt;/code&gt;，而通过发表，可以到达&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_3\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_2\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_4\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{split}
\\\upsilon (s_4)&amp;amp;=\pi(睡觉|s_4)R^{睡觉}_{s_4s_5}+\pi(发表|s_4)(R^{发表}_{s_4s_3}+\gamma (P^{发表}_{s_4s_2}\upsilon (s_2)+P^{发表}_{s_4s_3}\upsilon (s_3)+P^{发表}_{s_4s_4}\upsilon (s_4)))
\\&amp;amp;=0.5*10+0.5*(1+1*(0.2*(-1.3)+0.4*2.7+0.4*7.4))
\\&amp;amp;=7.39
\end{split}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;16-最优状态值函数与最优状态-行为值函数及其贝尔曼最优方程&quot;&gt;1.6 最优状态值函数与最优状态-行为值函数及其贝尔曼最优方程&lt;/h3&gt;

&lt;p&gt;每个策略对应一个状态值函数，最优策略对应着最优状态值函数。&lt;/p&gt;

&lt;p&gt;定义：最优状态值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon ^*(s)\)&lt;/code&gt;为在所有策略中值最大的值函数，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon ^*(s)=\underset{\pi}{max}\upsilon_{\pi}(s)\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;最优状态-行为值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q^*(s,a)\)&lt;/code&gt;为在所有策略中最大的状态-行为值函数，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q^*(s,a)=\underset{\pi}{max}q_{\pi}(s,a)\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;从而，&lt;strong&gt;最优状态值函数&lt;/strong&gt;的&lt;strong&gt;贝尔曼最优方程&lt;/strong&gt;(与前面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\upsilon _{\pi}(s)\)&lt;/code&gt;的区别是，少乘了一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(a|s)\)&lt;/code&gt;)：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\upsilon ^*(s)=\underset{a}{max}R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon ^*(s')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;最优状态-行为值函数&lt;/strong&gt;的&lt;strong&gt;贝尔曼最优方程&lt;/strong&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q^*(s,a)=R^a_{s}+\gamma \sum _{s'\in S}P^a_{ss'}\underset{a'}{max}q^*(s',a')
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;若已各大最优状态-值函数，最优策略可以通过直接最大化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q^*(s,a)\)&lt;/code&gt;来决定：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi_*(a|s)=\left\{\begin{matrix}
1 &amp;amp;if\ a=\underset{a\in A}{argmax}q_*(s,a) \\ 
0 &amp;amp; otherwise
\end{matrix}\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;17-强化学习形式化描述&quot;&gt;1.7 强化学习形式化描述&lt;/h3&gt;

&lt;p&gt;定义一个离散时间有限范围的折扣马尔科夫决策过程&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M=(S,A,P,r,\rho_0,\gamma,T)\)&lt;/code&gt;，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;为&lt;strong&gt;状态集&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;为&lt;strong&gt;动作集&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P:S\times A\times S\rightarrow R\)&lt;/code&gt;为&lt;strong&gt;转移概率&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r:S\times A\rightarrow [-R_{max},R_{max}]\)&lt;/code&gt;为&lt;strong&gt;立即回报函数&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\rho_0:S\rightarrow R\)&lt;/code&gt;是&lt;strong&gt;初始状态分布&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma \in [0,1]\)&lt;/code&gt;是&lt;strong&gt;折扣因子&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T\)&lt;/code&gt;为&lt;strong&gt;水平范围（即步数）&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau \)&lt;/code&gt;为一个&lt;strong&gt;轨迹序列&lt;/strong&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau =(s_0,a_0,s_1,a_1,...)\)&lt;/code&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R=\sum _{t=0}^T\gamma ^tr_t\)&lt;/code&gt;为&lt;strong&gt;累积回报&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;强化学习的目标是找到&lt;strong&gt;最优策略&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;&lt;/strong&gt;，使得该策略下的&lt;strong&gt;累积回报期望最大&lt;/strong&gt;，即&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\underset{\pi}{max}\int R(\tau )p_{\pi}(\tau )d\tau 
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;2-mdp中的概率学基础&quot;&gt;2. MDP中的概率学基础&lt;/h2&gt;

&lt;h3 id=&quot;21-期望与方差&quot;&gt;2.1 期望与方差&lt;/h3&gt;

&lt;h4 id=&quot;期望&quot;&gt;期望&lt;/h4&gt;

&lt;p&gt;函数f(x)关于某分布P(x)的期望指，当x由分布P(x)产生，f作用于x时，f(x)的平均值。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;离散型随机变量的期望：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
E_{x\sim P}[f(x)]=\sum _xP(x)f(x)
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;连续型随机变量的期望通过积分求得：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
E_{x\sim P}[f(x)]=\int p(x)f(x)dx
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;期望的运算是线性的：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
E_x(\alpha f(x)+\beta g(x))=\alpha E_x[f(x)]+\beta E_x[g(x)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;方差&quot;&gt;方差&lt;/h4&gt;

&lt;p&gt;方差衡量利用当前概率分布采样时，采样值差异的大小：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Var(f(x))=E[(f(x)-E[f(x)])^2]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;方差越小，不确定性越小。&lt;/p&gt;

&lt;h3 id=&quot;22-随机策略&quot;&gt;2.2 随机策略&lt;/h3&gt;

&lt;h4 id=&quot;221-贪婪策略&quot;&gt;2.2.1 贪婪策略&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi_*(a|s)=\left\{\begin{matrix}
1 &amp;amp;if\ a=\underset{a\in A}{argmax}q_*(s,a) \\ 
0 &amp;amp; otherwise
\end{matrix}\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;贪婪策略是一个&lt;strong&gt;确定性策略&lt;/strong&gt;，只有动作值函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q^*(s,a)\)&lt;/code&gt;&lt;strong&gt;最大&lt;/strong&gt;的动作时取&lt;strong&gt;概率1&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;222-varepsilon--greedy策略&quot;&gt;2.2.2 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon -greedy\)&lt;/code&gt;策略&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi (a|s)=\left\{\begin{matrix}
1-\varepsilon + \frac{\varepsilon}{|A(s)|} &amp;amp;if\ a=\underset{a\in A}{argmax}q_*(s,a) \\ 
\frac{\varepsilon}{|A(s)|} &amp;amp; otherwise
\end{matrix}\right.
\]&lt;/code&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon -greedy\)&lt;/code&gt;策略是强化学习&lt;strong&gt;最基本最常用&lt;/strong&gt;的&lt;strong&gt;随机策略&lt;/strong&gt;。该策略平衡了e&amp;amp;e，也就是&lt;strong&gt;利用(exploitation)&lt;/strong&gt;和&lt;strong&gt;探索(exploration)&lt;/strong&gt;，其中选取动作值函数最大的部分为利用，其他非最优动作仍然有概率的部分为探索部分。&lt;/p&gt;

&lt;p&gt;也就是说，所有的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi(a|s)\)&lt;/code&gt;都初始化为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\varepsilon}{|A(s)|}\)&lt;/code&gt;，而如果正好这个a能取到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(max_aQ(s,a)\)&lt;/code&gt;，那么给他加上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((1-\varepsilon)\)&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;223-高斯策略&quot;&gt;2.2.3 高斯策略&lt;/h4&gt;

&lt;p&gt;一般的高斯策略都可以写成&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi_{\theta}=\mu_{\theta}+\varepsilon,\varepsilon \sim N(0,\sigma ^2)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu_{\theta}\)&lt;/code&gt;为&lt;strong&gt;确定性部分&lt;/strong&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon\)&lt;/code&gt;为&lt;strong&gt;零均值&lt;/strong&gt;的&lt;strong&gt;高斯随机噪声&lt;/strong&gt;。这种策略也&lt;strong&gt;平衡&lt;/strong&gt;了&lt;strong&gt;利用&lt;/strong&gt;与&lt;strong&gt;探索&lt;/strong&gt;，利用由确定性部分完成，探索由&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\varepsilon\)&lt;/code&gt;完成。&lt;/p&gt;

&lt;h4 id=&quot;224-玻尔兹曼分布&quot;&gt;2.2.4 玻尔兹曼分布&lt;/h4&gt;

&lt;p&gt;对于&lt;strong&gt;动作空间是离散的，或者动作空间并不大&lt;/strong&gt;的情况，可以采用玻尔兹曼分布(吉布斯分布)作为随机策略，即：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\pi(a|s,\theta)=\frac{exp(Q(s,a,\theta))}{\sum_bexp(h(s,b,\theta))}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q(s,a,\theta)\)&lt;/code&gt;是动作值函数。&lt;strong&gt;动作值函数大的动作被选中的概率大，小的动作被选中的概率小。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;类似softmax，可以参考&lt;strong&gt;&lt;a href=&quot;https://www.zhihu.com/question/40403377&quot;&gt;多类分类下为什么用softmax而不是用其他归一化方法?&lt;/a&gt;&lt;/strong&gt;。&lt;/p&gt;

&lt;h2 id=&quot;3-基于gym的mdp实例&quot;&gt;3. 基于gym的MDP实例&lt;/h2&gt;

&lt;p&gt;详见书p29&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/daiwk/reinforcement-learning-code/blob/master/grid_mdp.py&quot;&gt;https://github.com/daiwk/reinforcement-learning-code/blob/master/grid_mdp.py&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Wasserstein自编码器</title>
   <link href="http://daiwk.github.io/posts/dl-wasserstein-autoencoders.html"/>
   <updated>2018-02-16T00:00:00+00:00</updated>
   <id>/posts/dl-wasserstein-autoencoders</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737956&amp;amp;idx=3&amp;amp;sn=d3597e73bd7457d608488cf1087389f4&amp;amp;chksm=871ac95ab06d404c1952b6fd59d0ec59267e5647cd52b39d2a9e8adf070b2c4c372dd8f0e219&amp;amp;scene=0&amp;amp;pass_ticket=52szIHU2nDe0%2FHpNVg6A0uKH8OcCUpRBXt0cv3flp56HR4%2FKKYQVtbTX3H73ePQ7#rd&quot;&gt;ICLR 2018 | 谷歌大脑Wasserstein自编码器：新一代生成模型算法&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;变分自编码器（VAE）与生成对抗网络（GAN）是复杂分布上无监督学习主流的两类方法。近日，谷歌大脑 Ilya Tolstikhin 等人提出了又一种新思路：Wasserstein 自编码器，其不仅具有 VAE 的一些优点，更结合了 GAN 结构的特性，可以实现更好的性能。该研究的论文《Wasserstein Auto-Encoders》已被即将在 4 月 30 日于温哥华举行的 ICLR 2018 大会接收。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Zero to Hero：2017年机器之心AI高分概述文章全集</title>
   <link href="http://daiwk.github.io/posts/dl-2017-introductions.html"/>
   <updated>2018-02-16T00:00:00+00:00</updated>
   <id>/posts/dl-2017-introductions</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737956&amp;amp;idx=1&amp;amp;sn=31804a962ac859cb9c24661e4ad79801&amp;amp;chksm=871ac95ab06d404c91c616236c5b6ae24d4981e790a2fb3471978b1f68e76c0ba32607f1f8cb&amp;amp;scene=0&amp;amp;pass_ticket=52szIHU2nDe0%2FHpNVg6A0uKH8OcCUpRBXt0cv3flp56HR4%2FKKYQVtbTX3H73ePQ7#rd&quot;&gt;Zero to Hero：2017年机器之心AI高分概述文章全集&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap1 绪论</title>
   <link href="http://daiwk.github.io/posts/rl-stepbystep-chap1.html"/>
   <updated>2018-02-13T00:00:00+00:00</updated>
   <id>/posts/rl-stepbystep-chap1</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-强化学习可以解决什么问题&quot;&gt;1. 强化学习可以解决什么问题&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-强化学习如何解决问题&quot;&gt;2. 强化学习如何解决问题&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-强化学习算法分类及发展趋势&quot;&gt;3. 强化学习算法分类及发展趋势&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-gym&quot;&gt;4. gym&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#reset函数&quot;&gt;reset()函数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#render函数&quot;&gt;render()函数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#step函数&quot;&gt;step()函数&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-整体脉络&quot;&gt;5. 整体脉络&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#强化学习的基本算法&quot;&gt;强化学习的基本算法&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#第一部分-强化学习基础&quot;&gt;第一部分 强化学习基础&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#chap2-马尔科夫决策过程&quot;&gt;chap2 马尔科夫决策过程&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#chap3-基于动态规划的强化学习算法&quot;&gt;chap3 基于动态规划的强化学习算法&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#第二部分-基于值函数的强化学习方法&quot;&gt;第二部分 基于值函数的强化学习方法&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#chap4-基于蒙特卡罗的强化学习算法&quot;&gt;chap4 基于蒙特卡罗的强化学习算法&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#chap5-基于时间差分的强化学习算法&quot;&gt;chap5 基于时间差分的强化学习算法&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#chap6-基于值函数逼近的强化学习算法&quot;&gt;chap6 基于值函数逼近的强化学习算法&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#第三部分-基于直接策略搜索的强化学习方法&quot;&gt;第三部分 基于直接策略搜索的强化学习方法&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#chap7-策略梯度理论&quot;&gt;chap7 策略梯度理论&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#chap8-trpo&quot;&gt;chap8 TRPO&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#chap9-确定性策略搜索&quot;&gt;chap9 确定性策略搜索&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#chap10-引导策略搜索的强化学习算法&quot;&gt;chap10 引导策略搜索的强化学习算法&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#第四部分-强化学习研究及前沿&quot;&gt;第四部分 强化学习研究及前沿&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#chap11-逆向强化学习算法&quot;&gt;chap11 逆向强化学习算法&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#chap12-组合策略梯度和值函数方法&quot;&gt;chap12 组合策略梯度和值函数方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#chap13-值迭代网络&quot;&gt;chap13 值迭代网络&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#chap14-pilco方法及其扩展&quot;&gt;chap14 PILCO方法及其扩展&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#强化学习算法所用到的基础知识&quot;&gt;强化学习算法所用到的基础知识&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;strong&gt;《深入浅出强化学习》&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-强化学习可以解决什么问题&quot;&gt;1. 强化学习可以解决什么问题&lt;/h2&gt;

&lt;p&gt;强化学习解决的是&lt;strong&gt;智能决策&lt;/strong&gt;（即&lt;strong&gt;序贯决策&lt;/strong&gt;）问题，也就是说需要&lt;strong&gt;连续不断&lt;/strong&gt;地做出决策，才能实现&lt;strong&gt;最终目标&lt;/strong&gt;。&lt;/p&gt;

&lt;h2 id=&quot;2-强化学习如何解决问题&quot;&gt;2. 强化学习如何解决问题&lt;/h2&gt;

&lt;p&gt;监督学习解决的是&lt;strong&gt;智能感知&lt;/strong&gt;的问题，学习的是输入长得像什么（特征），以及和它对应的是什么（标签）。需要的是多样化的标签数据。&lt;/p&gt;

&lt;p&gt;强化学习不关心输入长什么样，只关心&lt;strong&gt;当前输入下&lt;/strong&gt;应该采取什么&lt;strong&gt;动作&lt;/strong&gt;，才能实现&lt;strong&gt;最终目标&lt;/strong&gt;。需要智能体&lt;strong&gt;不断&lt;/strong&gt;地与&lt;strong&gt;环境&lt;/strong&gt;交互，不断尝试。需要的是&lt;strong&gt;带有回报&lt;/strong&gt;的&lt;strong&gt;交互数据&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;几个关键时间点：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;1998年，Richard S. Sutton出版了《强化学习导论》第一版，总结了1998年以前强化学习的各种进展。关注和发展最多的是&lt;strong&gt;表格型强化学习算法&lt;/strong&gt;。基于&lt;strong&gt;直接策略搜索&lt;/strong&gt;的方法也被提出来了，例如1992年的Rinforce算法，直接对策略梯度进行估计。&lt;/li&gt;
  &lt;li&gt;2013年，deepmind提出了&lt;strong&gt;DQN(deep Q network)&lt;/strong&gt;，将深度学习与强化学习结合形成深度强化学习。&lt;/li&gt;
  &lt;li&gt;2016、2017年，alphago连续两年击败围棋世界冠军。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;3-强化学习算法分类及发展趋势&quot;&gt;3. 强化学习算法分类及发展趋势&lt;/h2&gt;

&lt;p&gt;根据强化学习是否依赖模型：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;基于模型的强化学习：利用与环境交互得到的数据学习系统或者环境模型，再&lt;strong&gt;基于模型&lt;/strong&gt;进行序贯决策。效率会比无模型的高&lt;/li&gt;
  &lt;li&gt;无模型的强化学习：&lt;strong&gt;直接&lt;/strong&gt;利用与环境交互获得的数据改善自身的行为。有些根本无法建模的任务，只能利用无模型的强化学习算法，也更具有通用性。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;根据策略的更新和学习方法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;基于值函数的强化学习：学习&lt;strong&gt;值函数&lt;/strong&gt;，最终的策略根据值函数&lt;strong&gt;贪婪&lt;/strong&gt;得到。任意状态下，&lt;strong&gt;值函数最大的动作就是当前最优策略。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;基于直接策略搜索的强化学习：将策略&lt;strong&gt;参数化&lt;/strong&gt;，学习实现目标的&lt;strong&gt;最优参数&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;基于AC的方法：&lt;strong&gt;联合&lt;/strong&gt;使用值函数和直接策略搜索。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;根据环境返回的回报函数是否已知：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;正向强化学习：&lt;strong&gt;回报函数&lt;/strong&gt;是&lt;strong&gt;人为指定&lt;/strong&gt;的&lt;/li&gt;
  &lt;li&gt;逆向强化学习：通过机器学习的方法由函数自己学出回报，因为很多时候回报无法人为指定，例如无人机的表演&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其他强化学习：分层强化学习、元强化学习、多智能体强化学习、关系强化学习、迁移强化学习等。&lt;/p&gt;

&lt;p&gt;强化学习的发展趋势：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;强化学习和深度学习的结合会更加紧密&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;机器学习算法分为三大类：监督学习、无监督学习、强化学习。三类算法联合使用的效果更好，例如基于深度强化学习的对话生成等。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;强化学习和专业知识的结合会更加紧密&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;对于不同领域，可以重塑回报函数，或者修改网络结构。代表作：NIPS2016的最佳论文：值迭代网络【&lt;a href=&quot;https://arxiv.org/pdf/1602.02867.pdf&quot;&gt;Value Iteration Networks&lt;/a&gt;，github代码：&lt;a href=&quot;https://github.com/TheAbhiKumar/tensorflow-value-iteration-networks&quot;&gt;https://github.com/TheAbhiKumar/tensorflow-value-iteration-networks&lt;/a&gt;】。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;强化学习算法理论分析会更强，算法会更稳定和高效&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;代表作有：基于深度能量的策略方法、值函数与策略方法的等价性等。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;强化学习与脑科学、认知神经科学、记忆的联系会更紧密&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DeepMind和伦敦大学学院是这一流派的代表。&lt;/p&gt;

&lt;h2 id=&quot;4-gym&quot;&gt;4. gym&lt;/h2&gt;

&lt;p&gt;以CartPoleEnv为例：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'CartPole-v0'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;CartPoleEnv的环境文件位于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/gym/gym/envs/classic_control/cartpole.py&lt;/code&gt;
注册的代码在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/gym/gym/envs/__init__.py&lt;/code&gt;中&lt;/p&gt;

&lt;h3 id=&quot;reset函数&quot;&gt;reset()函数&lt;/h3&gt;

&lt;p&gt;智能体需要一次次尝试病积累经验，然后从经验中学到好的动作。&lt;strong&gt;每一次尝试&lt;/strong&gt;称为&lt;strong&gt;一条轨迹&lt;/strong&gt;，或者&lt;strong&gt;一个episode&lt;/strong&gt;。每次尝试都需要达到终止状态，一次尝试结束后，就需要智能体重新初始化。&lt;/p&gt;

&lt;p&gt;reset()是重新初始化函数。实现如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np_random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uniform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;low&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;high&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,))&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steps_beyond_done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;即，利用均匀随机分布初始化环境状态，然后设置当前步数为None，病返回环境的初始化状态。&lt;/p&gt;

&lt;h3 id=&quot;render函数&quot;&gt;render()函数&lt;/h3&gt;

&lt;p&gt;render()扮演图像引擎的角色。为了便于直观显示环境中物理的状态，需要除了物理引擎之外的图像引擎。源码如下:&lt;/p&gt;

&lt;p&gt;注释参考：&lt;a href=&quot;https://github.com/daiwk/reinforcement-learning-code/blob/master/cartpole_notes.py&quot;&gt;https://github.com/daiwk/reinforcement-learning-code/blob/master/cartpole_notes.py&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;viewer&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gym.envs.classic_control&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendering&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;viewer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendering&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Viewer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen_width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;screen_height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# 创建台车
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cartwidth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cartwidth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cartheight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cartheight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;axleoffset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cartheight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;4.0&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendering&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FilledPolygon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;## 填充一个矩形
&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;#添加台车转换矩阵属性
&lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carttrans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendering&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carttrans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#加入几何体台车
&lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;viewer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_geom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#创建摆杆
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polewidth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polewidth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polelen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polewidth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polewidth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;pole&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendering&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FilledPolygon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;pole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#添加摆杆转换矩阵属性
&lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;poletrans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendering&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;translation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axleoffset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;pole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;poletrans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;pole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carttrans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#加入几何体
&lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;viewer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_geom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#创建摆杆和台车之间的连接
&lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendering&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_circle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polewidth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;poletrans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carttrans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;viewer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_geom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;#创建台车来回滑动的轨道，即一条直线
&lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rendering&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen_width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;viewer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_geom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cartx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen_width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# MIDDLE OF CART
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;#设置平移属性
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;carttrans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_translation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cartx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;carty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;poletrans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_rotation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;viewer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;return_rgb_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'rgb_array'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;创建完cart的形状，给cart添加平移属性和旋转属性，将车的位移设置到cart的平移属性中，cart就会根据系统的状态变化左右移动。&lt;/p&gt;

&lt;h3 id=&quot;step函数&quot;&gt;step()函数&lt;/h3&gt;

&lt;p&gt;step()函数扮演物理引擎的角色。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;输入&lt;/strong&gt;：&lt;strong&gt;动作&lt;/strong&gt;a&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;输出&lt;/strong&gt;：&lt;strong&gt;下一步的状态&lt;/strong&gt;、&lt;strong&gt;立即回报&lt;/strong&gt;、&lt;strong&gt;是否终止&lt;/strong&gt;、&lt;strong&gt;调试项&lt;/strong&gt;。调试信息可能为空，但要填默认值{}。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;描述了智能体与环境交互的所有信息。利用智能体的运动学模型和动力学模型计算下一步的状态和立即回报，并判断是否达到终止状态。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%r (%s) invalid&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta_dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 系统的当前状态
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;force&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;force_mag&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;force_mag&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 输入动作，即作用到车上的力
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;costheta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# cos
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;sintheta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# sin
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# 车摆的动力学方程式，即加速度与动作之间的关系
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;force&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polemass_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta_dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta_dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sintheta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_mass&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;thetaacc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gravity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sintheta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costheta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;masspole&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costheta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costheta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_mass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 摆的角加速度
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;xacc&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polemass_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thetaacc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;costheta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_mass&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 小车的平移加速度
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tau&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_dot&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x_dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tau&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xacc&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tau&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta_dot&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;theta_dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta_dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tau&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;thetaacc&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 积分球下一步的状态
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_threshold&lt;/span&gt; \
                &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_threshold&lt;/span&gt; \
                &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta_threshold_radians&lt;/span&gt; \
                &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;theta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theta_threshold_radians&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steps_beyond_done&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# Pole just fell!
&lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steps_beyond_done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steps_beyond_done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;You are calling 'step()' even though this environment has already returned done = True. You should always call 'reset()' once you receive 'done = True' -- any further steps are undefined behavior.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;steps_beyond_done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;5-整体脉络&quot;&gt;5. 整体脉络&lt;/h2&gt;

&lt;h3 id=&quot;强化学习的基本算法&quot;&gt;强化学习的基本算法&lt;/h3&gt;

&lt;h4 id=&quot;第一部分-强化学习基础&quot;&gt;第一部分 强化学习基础&lt;/h4&gt;

&lt;h5 id=&quot;chap2-马尔科夫决策过程&quot;&gt;chap2 马尔科夫决策过程&lt;/h5&gt;

&lt;p&gt;强化学习解决的是序贯决策问题，一般的序贯决策问题可以用&lt;strong&gt;马尔科夫决策过程（MDP）&lt;/strong&gt;的框架来表示。&lt;/p&gt;

&lt;h5 id=&quot;chap3-基于动态规划的强化学习算法&quot;&gt;chap3 基于动态规划的强化学习算法&lt;/h5&gt;

&lt;p&gt;对于&lt;strong&gt;模型已知&lt;/strong&gt;的MDP问题，&lt;strong&gt;动态规划&lt;/strong&gt;是一个不错的解。由此引出&lt;strong&gt;广义策略迭代&lt;/strong&gt;的方法。而广义策略迭代的方法也适用于无模型的方法，是&lt;strong&gt;基于值函数强化学习的基本框架&lt;/strong&gt;。因此有chap4的基于蒙特卡罗方法、chap5的基于时间查分方法、chap6的基于值函数逼近方法。&lt;/p&gt;

&lt;h4 id=&quot;第二部分-基于值函数的强化学习方法&quot;&gt;第二部分 基于值函数的强化学习方法&lt;/h4&gt;

&lt;h5 id=&quot;chap4-基于蒙特卡罗的强化学习算法&quot;&gt;chap4 基于蒙特卡罗的强化学习算法&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;无模型&lt;/strong&gt;的强化学习算法，是整个强化学习算法的&lt;strong&gt;核心&lt;/strong&gt;。基于&lt;strong&gt;值函数&lt;/strong&gt;的强化学习算法的&lt;strong&gt;核心&lt;/strong&gt;是计算&lt;strong&gt;值函数的期望&lt;/strong&gt;。值函数是一个&lt;strong&gt;随机变量&lt;/strong&gt;，其&lt;strong&gt;期望&lt;/strong&gt;的计算可以通过&lt;strong&gt;蒙特卡罗方法&lt;/strong&gt;得到。&lt;/p&gt;

&lt;h5 id=&quot;chap5-基于时间差分的强化学习算法&quot;&gt;chap5 基于时间差分的强化学习算法&lt;/h5&gt;

&lt;p&gt;基于&lt;strong&gt;蒙特卡罗&lt;/strong&gt;的强化学习算法通过蒙特卡罗模拟计算期望，需要等每次&lt;strong&gt;试验结束后&lt;/strong&gt;再&lt;strong&gt;对值函数进行估计&lt;/strong&gt;，&lt;strong&gt;收敛速度漫&lt;/strong&gt;。而时间差分的方法只需要&lt;strong&gt;一步&lt;/strong&gt;便&lt;strong&gt;更新&lt;/strong&gt;，&lt;strong&gt;效率高、收敛速度快&lt;/strong&gt;。&lt;/p&gt;

&lt;h5 id=&quot;chap6-基于值函数逼近的强化学习算法&quot;&gt;chap6 基于值函数逼近的强化学习算法&lt;/h5&gt;

&lt;p&gt;chap4和chap5介绍的是&lt;strong&gt;表格型&lt;/strong&gt;强化学习，即，&lt;strong&gt;状态&lt;/strong&gt;空间和&lt;strong&gt;动作&lt;/strong&gt;空间都是&lt;strong&gt;有限集&lt;/strong&gt;，动作值函数可以用一个表格来描述，表格的索引分别为&lt;strong&gt;状态量&lt;/strong&gt;和&lt;strong&gt;动作量&lt;/strong&gt;。但当状态空间和动作空间都很大时，甚至两个空间都是连续空间时，这就无法用表格表示，可以用&lt;strong&gt;函数逼近理论&lt;/strong&gt;对值函数进行逼近。本章介绍了&lt;strong&gt;DQN&lt;/strong&gt;及其变种（Double DQN、Prioritized Replay DQN、Dueling DQN等）。&lt;/p&gt;

&lt;h4 id=&quot;第三部分-基于直接策略搜索的强化学习方法&quot;&gt;第三部分 基于直接策略搜索的强化学习方法&lt;/h4&gt;

&lt;h5 id=&quot;chap7-策略梯度理论&quot;&gt;chap7 策略梯度理论&lt;/h5&gt;

&lt;p&gt;区别于基于值函数的方法，强化学习的第二大类算法是&lt;strong&gt;直接策略搜索&lt;/strong&gt;方法。就是将策略进行&lt;strong&gt;参数化&lt;/strong&gt;，然后在&lt;strong&gt;参数空间&lt;/strong&gt;直接&lt;strong&gt;搜索最优策略&lt;/strong&gt;。直接策略搜索方法中，最直接最简单的方法是&lt;strong&gt;策略梯度&lt;/strong&gt;方法 。&lt;/p&gt;

&lt;h5 id=&quot;chap8-trpo&quot;&gt;chap8 TRPO&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;基于策略梯度&lt;/strong&gt;方法最具挑战性的是&lt;strong&gt;更新步长的确定&lt;/strong&gt;。TRPO方法通过理论分析得到&lt;strong&gt;单调非递减&lt;/strong&gt;的策略更新方法.&lt;/p&gt;

&lt;h5 id=&quot;chap9-确定性策略搜索&quot;&gt;chap9 确定性策略搜索&lt;/h5&gt;

&lt;p&gt;当&lt;strong&gt;动作空间&lt;/strong&gt;维数很高时，智能体的探索效率会很低，利用&lt;strong&gt;确定性策略&lt;/strong&gt;可以&lt;strong&gt;免除对动作空间的探索&lt;/strong&gt;，提升算法的收敛速度。&lt;/p&gt;

&lt;h5 id=&quot;chap10-引导策略搜索的强化学习算法&quot;&gt;chap10 引导策略搜索的强化学习算法&lt;/h5&gt;

&lt;p&gt;chap7（策略梯度）\8（TRPO）\9（确定性策略搜索）章讲的是&lt;strong&gt;无模型&lt;/strong&gt;的&lt;strong&gt;直接策略搜索方法&lt;/strong&gt;。而对于机器人等复杂系统，无模型的方法随机初始化很难找到成功的解，所以算法很难收敛。此时，可以利用&lt;strong&gt;传统控制器&lt;/strong&gt;来&lt;strong&gt;引导策略进行搜索&lt;/strong&gt;。&lt;/p&gt;

&lt;h4 id=&quot;第四部分-强化学习研究及前沿&quot;&gt;第四部分 强化学习研究及前沿&lt;/h4&gt;

&lt;h4 id=&quot;chap11-逆向强化学习算法&quot;&gt;chap11 逆向强化学习算法&lt;/h4&gt;

&lt;p&gt;很多实际问题，往往不知道回报函数，所以可以通过逆向强化学习来&lt;strong&gt;学习回报函数&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;chap12-组合策略梯度和值函数方法&quot;&gt;chap12 组合策略梯度和值函数方法&lt;/h3&gt;

&lt;p&gt;将策略梯度方法和值函数方法相组合。&lt;/p&gt;

&lt;h3 id=&quot;chap13-值迭代网络&quot;&gt;chap13 值迭代网络&lt;/h3&gt;

&lt;p&gt;先介绍DQN，然后介绍值迭代网络。&lt;/p&gt;

&lt;h3 id=&quot;chap14-pilco方法及其扩展&quot;&gt;chap14 PILCO方法及其扩展&lt;/h3&gt;

&lt;p&gt;PLICO(probalistic inference for learning control)是一种&lt;strong&gt;基于模型&lt;/strong&gt;的强化学习算法，将&lt;strong&gt;模型误差&lt;/strong&gt;纳入考虑范围内，一般只需要训练几次到几十次就可以成功实现对单摆等典型非线性系统的稳定性控制，而基于无模型的强化学习则需要训练上万次。&lt;/p&gt;

&lt;h3 id=&quot;强化学习算法所用到的基础知识&quot;&gt;强化学习算法所用到的基础知识&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;第2章：概率学基础、&lt;strong&gt;随机策略&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;第3章：&lt;strong&gt;模型已知&lt;/strong&gt;时，值函数的求解可转化为线性方程组的求解。&lt;strong&gt;线性方程组的数值求解方法&lt;/strong&gt;——&lt;strong&gt;高斯·赛尔德迭代法&lt;/strong&gt;，并利用时变与泛函分析中的压缩映射证明了算法的收敛性。&lt;/li&gt;
  &lt;li&gt;第4章：值函数是&lt;strong&gt;累积回报的期望&lt;/strong&gt;。统计学中利用&lt;strong&gt;采样数据&lt;/strong&gt;可以用来&lt;strong&gt;计算期望&lt;/strong&gt;：&lt;strong&gt;重要性&lt;/strong&gt;采样、&lt;strong&gt;拒绝性&lt;/strong&gt;采样、&lt;strong&gt;MCMC&lt;/strong&gt;方法。&lt;/li&gt;
  &lt;li&gt;第8章：TRPO中，替代目标函数用了&lt;strong&gt;信息论&lt;/strong&gt;的&lt;strong&gt;熵&lt;/strong&gt;和&lt;strong&gt;相对熵&lt;/strong&gt;的概念，同时TRPO的求解需要用到各种优化算法。&lt;/li&gt;
  &lt;li&gt;第10章：引导策略搜索强化学习的优化目标用到了&lt;strong&gt;KL散度&lt;/strong&gt;和&lt;strong&gt;变分推理&lt;/strong&gt;，及大型的&lt;strong&gt;并行优化算法&lt;/strong&gt;，例如，&lt;strong&gt;LBFGS&lt;/strong&gt;优化算法、&lt;strong&gt;ADMM&lt;/strong&gt;方法（交替方向乘子法）&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>阿里巴巴的强化学习应用</title>
   <link href="http://daiwk.github.io/posts/rl-alibaba-rl.html"/>
   <updated>2018-02-06T00:00:00+00:00</updated>
   <id>/posts/rl-alibaba-rl</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考
&lt;a href=&quot;http://techforum-img.cn-hangzhou.oss-pub.aliyun-inc.com/1517812754285/reinforcement_learning.pdf?Expires=1517898391&amp;amp;OSSAccessKeyId=LTAIAJ2WBIdlRPlb&amp;amp;Signature=A6ZMEEJi16DKLK0tyyBMqoSYLLM%3D&quot;&gt;原文链接&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/reinforcement_learning_alibaba.pdf&quot;&gt;转储&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorflow项目架构模板+常用代码集</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-template.html"/>
   <updated>2018-02-06T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-template</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#tensorflow项目架构模板&quot;&gt;tensorflow项目架构模板&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tensorflow常用代码集&quot;&gt;tensorflow常用代码集&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#oo的dl代码&quot;&gt;oo的dl代码&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#awesome-tensorflow&quot;&gt;awesome tensorflow&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;tensorflow项目架构模板&quot;&gt;tensorflow项目架构模板&lt;/h2&gt;

&lt;p&gt;参考
&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737338&amp;amp;idx=1&amp;amp;sn=34c5cab8287b2138b7f80ff730fe2cd0&amp;amp;chksm=871acec4b06d47d299d3a72ec955d0353b690f8734c61ae864d1054a90c5f07a6cdc0f9fe380&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=020676GzXwFc20a3HPA6Ub4R&amp;amp;pass_ticket=9ERW5CrFxOvS2lhjR8S8LDnjY4q8APAoow%2FlXngRZASrq2yGQ%2BN3nk8bqFjdyz53#rd&quot;&gt;快速开启你的第一个项目：TensorFlow项目架构模板&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;项目代码：
&lt;a href=&quot;https://github.com/Mrgemy95/Tensorflow-Project-Template&quot;&gt;https://github.com/Mrgemy95/Tensorflow-Project-Template&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;tensorflow常用代码集&quot;&gt;tensorflow常用代码集&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/taki0112/Tensorflow-Cookbook&quot;&gt;https://github.com/taki0112/Tensorflow-Cookbook&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;oo的dl代码&quot;&gt;oo的dl代码&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766829&amp;amp;idx=2&amp;amp;sn=bc751e25ac5bbcc0f40cf58f627d3190&amp;amp;chksm=871ab993b06d30851ea30e81e6001e46cee4ca8cc432e993679c3a70c3980e2a08e75967ccc3&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;编程进阶之路：用简单的面向对象编程提升深度学习原型&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;awesome-tensorflow&quot;&gt;awesome tensorflow&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/jtoy/awesome-tensorflow&quot;&gt;https://github.com/jtoy/awesome-tensorflow&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>nested lstm</title>
   <link href="http://daiwk.github.io/posts/dl-nested-lstm.html"/>
   <updated>2018-02-04T00:00:00+00:00</updated>
   <id>/posts/dl-nested-lstm</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考
&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737297&amp;amp;idx=4&amp;amp;sn=075ed17c1fa9ec09309c1bea0f72785e&amp;amp;chksm=871aceefb06d47f9afd7fa28706f660c2dcaec3193575b7a637701ceb5620dcf7173975dcec9&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0204MoahEmvMJaWVufDeThJS&amp;amp;pass_ticket=DtS40xhA8b%2FQB76bC%2FH86g91SmSrUAyY6MHLOfLSEdM7VjdptiHtx9tHknQ1s3BI#rd&quot;&gt;学界 | Nested LSTM：一种能处理更长期信息的新型LSTM扩展&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文地址：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1801.10308.pdf&quot;&gt;Nested LSTMs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;虽然在层级记忆上已有一些研究，LSTM 及其变体仍旧是处理时序任务最流行的深度学习模型，例如字符级的语言建模。特别是默认的堆栈 LSTM 架构使用一系列 LSTM 一层层地堆叠在一起来处理数据，一层的输出成为下一层的输入。在此论文中，研究者们提出并探索了一种全新的&lt;strong&gt;嵌套 LSTM 架构（Nested LSTM，NLSTM)&lt;/strong&gt;，并认为其有潜力&lt;strong&gt;直接取代堆栈 LSTM&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;在 NLSTM 中，LSTM 的&lt;strong&gt;记忆单元&lt;/strong&gt;可以&lt;strong&gt;访问内部记忆&lt;/strong&gt;，使用标准的 LSTM 门选择性地进行读取、编写。相比于传统的堆栈 LSTM，这一关键特征使得模型能&lt;strong&gt;实现更有效的时间层级&lt;/strong&gt;。在 NLSTM 中，&lt;strong&gt;(外部）记忆单元&lt;/strong&gt;可自由选择&lt;strong&gt;读取、编写&lt;/strong&gt;的相关长期信息&lt;strong&gt;到内部单元&lt;/strong&gt;。相比之下，在堆栈 LSTM 中，高层级的激活（类似内部记忆）直接生成输出，因此必须包含所有的与当前预测相关的短期信息。换言之，堆栈 LSTM 与嵌套 LSTM 之间的主要不同是，NLSTM 可以&lt;strong&gt;选择性&lt;/strong&gt;地&lt;strong&gt;访问内部记忆&lt;/strong&gt;。这使得&lt;strong&gt;内部记忆&lt;/strong&gt;可以&lt;strong&gt;免于记住、处理更长时间规模上的事件&lt;/strong&gt;，即使这些事件与当前事件不相关。&lt;/p&gt;

&lt;p&gt;在此论文中，作者们的可视化图证明了，相比于堆栈 LSTM 中的高层级记忆，NLSTM 的&lt;strong&gt;内部记忆&lt;/strong&gt;确实能&lt;strong&gt;在更长的时间规模上操作&lt;/strong&gt;。实验也表明，NLSTM 在多种任务上都超越了堆栈 LSTM。&lt;/p&gt;

&lt;p&gt;直观上，LSTM 中的&lt;strong&gt;输出门&lt;/strong&gt;会编码&lt;strong&gt;仍旧值得记忆的信息&lt;/strong&gt;，这些记忆可能与&lt;strong&gt;当前的时间步&lt;/strong&gt;并&lt;strong&gt;不相关&lt;/strong&gt;。嵌套 LSTM 根据这一直观理解来创造一种记忆的时间层级。访问&lt;strong&gt;内部记忆&lt;/strong&gt;以同样的方式&lt;strong&gt;被门控&lt;/strong&gt;，以便于长期信息只有在情景相关的条件下才能选择性地访问。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/nested lstm.webp&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;计算(outer)memory cell不是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_t^{outer}=f_t\odot c_{t-1}+i_t\odot g_t\)&lt;/code&gt;，而是将concatenation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((f_t\odot c_{t-1}, i_t\odot g_t)\)&lt;/code&gt;作为inner lstm(NLSTM)的memory cell的输入，并指定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c^{outer}_t=h^{inner}_t\)&lt;/code&gt;。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>对抗样本防御</title>
   <link href="http://daiwk.github.io/posts/rl-defend-adversarial-samples.html"/>
   <updated>2018-02-03T00:00:00+00:00</updated>
   <id>/posts/rl-defend-adversarial-samples</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考
&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737254&amp;amp;idx=1&amp;amp;sn=2b9601756abbece51e0cc9b8ef3f653d&amp;amp;chksm=871ace18b06d470e473119f3e56baa868f7437378b4ae46aed5de683ab557e56d15d764a6cd4&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0203lSby7oCQ2NuRZ6356Prh&amp;amp;pass_ticket=KX3Ohz6jGpPev3lTZVba5zbxc4CiKE1LFnsRgVRoVuMDxZW5%2BU%2BIxFNH0FRRUIOv#rd&quot;&gt;搞事！ICLR 2018七篇对抗样本防御论文被新研究攻破，Goodfellow论战&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>capsule</title>
   <link href="http://daiwk.github.io/posts/dl-capsules.html"/>
   <updated>2018-02-02T00:00:00+00:00</updated>
   <id>/posts/dl-capsules</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-cnn-challenges&quot;&gt;1. cnn-challenges&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-equivariance&quot;&gt;2. equivariance&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-capsule&quot;&gt;3. capsule&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-dynamic-routing&quot;&gt;4. dynamic-routing&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-intuition&quot;&gt;4.1 intuition&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-calculating-a-capsule-output&quot;&gt;4.2 calculating-a-capsule-output&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-iterative-dynamic-routing&quot;&gt;5. iterative-dynamic-routing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-max-pooling-shortcoming&quot;&gt;6. max-pooling-shortcoming&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-significant-of-routing-by-agreement-with-capsules&quot;&gt;7. significant-of-routing-by-agreement-with-capsules&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#8-capsnet-architecture&quot;&gt;8. capsnet-architecture&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#9-loss-function-margin-loss&quot;&gt;9. loss-function-margin-loss&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#10-capsnet-model&quot;&gt;10. capsnet-model&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#101-primarycapsules&quot;&gt;10.1 primarycapsules&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#102-squash-function&quot;&gt;10.2 squash-function&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#103-digicaps-with-dynamic-routing&quot;&gt;10.3 digicaps-with-dynamic-routing&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#104-image-reconstruction&quot;&gt;10.4 image-reconstruction&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#105-reconstruction-loss&quot;&gt;10.5 reconstruction-loss&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#11-what-capsule-is-learning&quot;&gt;11. what-capsule-is-learning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#12-sarasra的代码&quot;&gt;12. sarasra的代码&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#121-quick-mnist-test-results&quot;&gt;12.1 Quick mnist test results：&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#122-quick-cifar10-ensemble-test-results&quot;&gt;12.2 Quick CIFAR10 ensemble test results&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#123-训练&quot;&gt;12.3 训练&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#124-训练验证&quot;&gt;12.4 训练+验证&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#13-源码解析&quot;&gt;13. 源码解析&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;strong&gt;代码&lt;/strong&gt;：
&lt;a href=&quot;https://github.com/Sarasra/models/tree/master/research/capsules&quot;&gt;https://github.com/Sarasra/models/tree/master/research/capsules&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;paper:
&lt;a href=&quot;https://arxiv.org/pdf/1710.09829.pdf&quot;&gt;Dynamic Routing Between Capsules&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652012657&amp;amp;idx=1&amp;amp;sn=6e7c8bb25d41e7c73682fc6e539c0e1c&amp;amp;chksm=f121f480c6567d969b1e48f649665d8c5d5122559641b944f37535846c77f645e9f435189b3d&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0201iw6QfDNUP6ejcN6Cp4L5&amp;amp;pass_ticket=qs99iMGY1jAAqC7Y%2F5bWFAKdSVWrNCDdPaLjWTEyZ612ZoU1cwNmbIOusMI23vOr#rd&quot;&gt;Hinton胶囊网络代码正式开源，5天GitHub fork超1.4万&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文主要参考jhui的博客：
&lt;a href=&quot;https://jhui.github.io/2017/11/03/Dynamic-Routing-Between-Capsules/&quot;&gt;https://jhui.github.io/2017/11/03/Dynamic-Routing-Between-Capsules/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-cnn-challenges&quot;&gt;1. cnn-challenges&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;神经元的激活水平&lt;/strong&gt;通常被解释为&lt;strong&gt;检测特定特征&lt;/strong&gt;的可能性【例如，如果图中蓝色的比例很高，可能某个神经元的激活值就特别大】。&lt;/p&gt;

&lt;p&gt;CNN善于&lt;strong&gt;检测特征&lt;/strong&gt;，却在探索&lt;strong&gt;特征（视角，大小，方位）之间&lt;/strong&gt;的&lt;strong&gt;空间关系&lt;/strong&gt;方面效果较差。(一个简单的CNN模型可以正确提取鼻子、眼睛和嘴巴的特征，但如果一张图里，鼻子和眼睛错位了，或者一只眼睛倾斜了一定的角度，那这张图仍然有可能错误地激活神经元导致认为这张图就是人脸)&lt;/p&gt;

&lt;p&gt;假设每个神经元都包含特征的&lt;strong&gt;可能性&lt;/strong&gt;和&lt;strong&gt;属性&lt;/strong&gt;【&lt;strong&gt;这里就叫做胶囊(capsule)了，也就是说，里面包含的不是一个值(a single scaler value)，而是一个向量(vector)&lt;/strong&gt;】。例如，神经元输出的是一个包含&lt;strong&gt;[可能性，方向，大小]&lt;/strong&gt;的向量。利用这种空间信息，就可以检测鼻子、眼睛和耳朵特征之间的方向和大小的一致性。此时，上面那张图对于人脸检测的激活输出就会低很多。&lt;/p&gt;

&lt;h2 id=&quot;2-equivariance&quot;&gt;2. equivariance&lt;/h2&gt;

&lt;p&gt;为了CNN能够处理不同的视角或变体，我们添加了&lt;strong&gt;更多&lt;/strong&gt;的神经元和层。尽管如此，这种方法倾向于记忆数据集，而不是得出一个比较通用的解决方案，它需要&lt;strong&gt;大量的训练数据来覆盖不同的变体，并避免过拟合&lt;/strong&gt;。MNIST数据集包含55,000个训练数据，也即每个数字都有5,500个样本。但是，儿童看过几次就能记住数字。现有的包括CNN在内的深度学习模式在&lt;strong&gt;利用数据方面效率十分低下&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;胶囊网络不是训练来捕捉特定变体的特征，而是捕捉特征及其变体的可能性。所以胶囊的目的不仅在于&lt;strong&gt;检测特征&lt;/strong&gt;，还在于&lt;strong&gt;训练模型&lt;/strong&gt;来&lt;strong&gt;学习变体&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;这样，&lt;strong&gt;相同的胶囊&lt;/strong&gt;就可以检测&lt;strong&gt;不同方向&lt;/strong&gt;的&lt;strong&gt;同一个物体类别&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Invariance&lt;/strong&gt;对应&lt;strong&gt;特征检测&lt;/strong&gt;，特征是不变的。例如，&lt;strong&gt;检测鼻子的神经元不管什么方向，都检测鼻子&lt;/strong&gt;。但是，神经元空间定向的损失最终会损害这种invariance模型的有效性。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Equivariance&lt;/strong&gt;对应&lt;strong&gt;变体检测&lt;/strong&gt;，也即可以相互转换的对象（例如检测不同方向的人脸）。直观地说，胶囊网络&lt;strong&gt;检测到脸部旋转了20°&lt;/strong&gt;，而不是实现与旋转了20°的变体相匹配的脸。通过强制模型学习胶囊中的特征变体，我们可以&lt;strong&gt;用较少的训练数据&lt;/strong&gt;，&lt;strong&gt;更有效地推断可能的变体&lt;/strong&gt;。此外，也可以更有效地&lt;strong&gt;防止对抗攻击&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;3-capsule&quot;&gt;3. capsule&lt;/h2&gt;

&lt;p&gt;胶囊是&lt;strong&gt;一组&lt;/strong&gt;神经元，不仅捕捉&lt;strong&gt;特征的可能性&lt;/strong&gt;，还捕捉&lt;strong&gt;具体特征的参数&lt;/strong&gt;。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/cap1.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;第一行表示神经元检测到数字“7”的概率。2-D胶囊是组合了2个神经元的网络。这个胶囊在检测数字“7”时输出2-D向量。&lt;/p&gt;

&lt;p&gt;第二行中的第一个图像，它输出一个向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v = (0, 0.9)\)&lt;/code&gt;，向量的模是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\| v \| = \sqrt{ 0^2 + 0.9^2 } = 0.9\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;在第三行，旋转图像20°。胶囊将产生具有&lt;strong&gt;相同幅度&lt;/strong&gt;但&lt;strong&gt;不同方向&lt;/strong&gt;的矢量。这里，矢量的角度表示数字“7”的旋转角度。&lt;/p&gt;

&lt;p&gt;最后，还可以添加2个神经元来捕捉大小和笔画的宽度(如下图)。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/cap2.jpg&quot; style=&quot;max-height: 50px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;我们称胶囊的&lt;strong&gt;输出向量&lt;/strong&gt;为活动向量(&lt;strong&gt;activity vector&lt;/strong&gt;) ，其&lt;strong&gt;幅度(模)&lt;/strong&gt;代表&lt;strong&gt;检测特征的概率&lt;/strong&gt;，其&lt;strong&gt;方向&lt;/strong&gt;代表其&lt;strong&gt;参数&lt;/strong&gt;（属性）。&lt;/p&gt;

&lt;h2 id=&quot;4-dynamic-routing&quot;&gt;4. dynamic-routing&lt;/h2&gt;

&lt;h3 id=&quot;41-intuition&quot;&gt;4.1 intuition&lt;/h3&gt;

&lt;p&gt;假设有3张类似但大小、方向不同的人脸图，嘴巴和眼睛的capsule分别记录了嘴巴和眼睛的水平方向的宽度（当然，也可以加上别的，例如高度、颜色等）。在dynamic routing中，将input capsules的vectors通过一个变换矩阵&lt;strong&gt;（transformation matrix）&lt;/strong&gt;转换成一个&lt;strong&gt;vote&lt;/strong&gt;，并且将&lt;strong&gt;相似vote&lt;/strong&gt;的capsules分为&lt;strong&gt;同一组&lt;/strong&gt;。这些&lt;strong&gt;votes&lt;/strong&gt;最终成为&lt;strong&gt;parent capsule&lt;/strong&gt;的&lt;strong&gt;output vector&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;42-calculating-a-capsule-output&quot;&gt;4.2 calculating-a-capsule-output&lt;/h3&gt;

&lt;p&gt;对于capsule网络，一个capsule的输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v_j\)&lt;/code&gt;都是向量。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/capsule-dynamic-routing.jpg&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;我们将一个变换矩阵（transformation matrix）&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{ij}\)&lt;/code&gt;应用到前一层的输出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;上，例如，使用一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m\times k\)&lt;/code&gt;的矩阵，将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\times D\)&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;变成一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m\times D\)&lt;/code&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat u_{j|i}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;然后计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_{ij}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat u_{j|i}\)&lt;/code&gt;的加权和，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_j\)&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\ \hat u_{j|i}=W_{ij}u_i
\\ s_j=\sum_i c_{ij}\hat u_{j|i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_{ij}\)&lt;/code&gt;是迭代动态路由过程（iterative dynamic routing process）训练的&lt;strong&gt;耦合系数&lt;/strong&gt;（coupling coefficients），而且&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum_jc_{ij}=1\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;这里不适用ReLU，而使用一个&lt;strong&gt;挤压函数（squashing function）&lt;/strong&gt;，来缩短0和单位长度之间的向量：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
v_j=\frac{||s_j||^2}{1+||s_j||^2}\frac{s_j}{||s_j||}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;挤压函数能够&lt;strong&gt;将短向量缩小到接近0&lt;/strong&gt;，&lt;strong&gt;将长向量缩小为接近单位向量&lt;/strong&gt;。因此，每个capsule的似然性在&lt;strong&gt;0到1之间&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\ v_j\approx ||s_j||s_j,\ for\ s_j\ is\ short
\\ v_j\approx \frac{s_j}{||s_j||},\ for\ s_j\ is\ long
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;5-iterative-dynamic-routing&quot;&gt;5. iterative-dynamic-routing&lt;/h2&gt;

&lt;p&gt;在深度学习中，我们使用反向传播来训练模型参数。转换矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{ij}\)&lt;/code&gt;在胶囊中仍然用&lt;strong&gt;反向传播&lt;/strong&gt;训练。不过，耦合系数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_{ij}\)&lt;/code&gt; 用新的&lt;strong&gt;迭代动态路由方法&lt;/strong&gt;进行计算。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/capsule-iterative-dynamic-routing.jpg&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;伪代码如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/capsule-iterative-dynamic-routing-algorithm.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在深度学习中，我们使用反向传播来训练基于成本函数的模型参数。这些参数（权重）控制信号从一层到另一层的路由。如果两个神经元之间的权重为零，则神经元的激活不会传播到该神经元。&lt;/p&gt;

&lt;p&gt;迭代动态路由提供了如何&lt;strong&gt;根据特征参数&lt;/strong&gt;来&lt;strong&gt;路由信号&lt;/strong&gt;的替代方案。通过利用特征参数，理论上，可以&lt;strong&gt;更好地将胶囊分组&lt;/strong&gt;，形成一个&lt;strong&gt;高层次的结构&lt;/strong&gt;。例如，胶囊层可能最终表现为&lt;strong&gt;探索“部分-整体”关系的分析树&lt;/strong&gt;。例如，脸部由眼睛、鼻子和嘴组成。迭代动态路由利用变换矩阵、可能性和特征的性质，控制向上传播到上面胶囊的信号的多少。&lt;/p&gt;

&lt;h2 id=&quot;6-max-pooling-shortcoming&quot;&gt;6. max-pooling-shortcoming&lt;/h2&gt;

&lt;h2 id=&quot;7-significant-of-routing-by-agreement-with-capsules&quot;&gt;7. significant-of-routing-by-agreement-with-capsules&lt;/h2&gt;

&lt;h2 id=&quot;8-capsnet-architecture&quot;&gt;8. capsnet-architecture&lt;/h2&gt;

&lt;p&gt;使用CapsNet进行mnist任务&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/capsnet-architecture.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;每一层的说明(capsule层使用convolution kernel来explore locality information)：&lt;/p&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Layer Name&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Apply&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Output shape&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;Image&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Raw image array&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;28x28x1&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;ReLU Conv1&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Convolution layer with 9x9 kernels output 256 channels, stride 1, no padding with ReLU	&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;20x20x256&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;PrimaryCapsules&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Convolution capsule layer with 9x9 kernel output 32x6x6 8-D capsule, stride 2, no padding&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;6x6x32x8&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;DigiCaps&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Capsule output computed from a (16x8 matrix) between and ( from 1 to 32x6x6 and from 1 to 10).&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;10x16&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;FC1&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Fully connected with ReLU&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;512&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;FC2&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Fully connected with ReLU&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;1024&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;Output image&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Fully connected with sigmoid&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;784(28x28)&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;h2 id=&quot;9-loss-function-margin-loss&quot;&gt;9. loss-function-margin-loss&lt;/h2&gt;

&lt;h2 id=&quot;10-capsnet-model&quot;&gt;10. capsnet-model&lt;/h2&gt;

&lt;h3 id=&quot;101-primarycapsules&quot;&gt;10.1 primarycapsules&lt;/h3&gt;

&lt;h3 id=&quot;102-squash-function&quot;&gt;10.2 squash-function&lt;/h3&gt;

&lt;h3 id=&quot;103-digicaps-with-dynamic-routing&quot;&gt;10.3 digicaps-with-dynamic-routing&lt;/h3&gt;

&lt;h3 id=&quot;104-image-reconstruction&quot;&gt;10.4 image-reconstruction&lt;/h3&gt;

&lt;h3 id=&quot;105-reconstruction-loss&quot;&gt;10.5 reconstruction-loss&lt;/h3&gt;

&lt;h2 id=&quot;11-what-capsule-is-learning&quot;&gt;11. what-capsule-is-learning&lt;/h2&gt;

&lt;h2 id=&quot;12-sarasra的代码&quot;&gt;12. sarasra的代码&lt;/h2&gt;

&lt;p&gt;要求：tf/numpy/gpu&lt;/p&gt;

&lt;p&gt;测试：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python layers_test.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;121-quick-mnist-test-results&quot;&gt;12.1 Quick mnist test results：&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;下载tfrecords，并解压到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$DATA_DIR/&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://storage.googleapis.com/capsule_toronto/mnist_data.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;下载model checkpoint，并解压到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$CKPT_DIR&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://storage.googleapis.com/capsule_toronto/mnist_checkpoints.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;测试&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python experiment.py &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt;/mnist_data/ &lt;span class=&quot;nt&quot;&gt;--train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--summary_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/ &lt;span class=&quot;nt&quot;&gt;--checkpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$CKPT_DIR&lt;/span&gt;/mnist_checkpoint/model.ckpt-1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;122-quick-cifar10-ensemble-test-results&quot;&gt;12.2 Quick CIFAR10 ensemble test results&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;下载cifar10 binary version，并解压到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$DATA_DIR/&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget  https://www.cs.toronto.edu/~kriz/cifar.html
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;下载cifar10 model checkpoints，并解压到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$CKPT_DIR&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://storage.googleapis.com/capsule_toronto/cifar_checkpoints.tar.gz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;测试&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python experiment.py &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--dataset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;cifar10 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--hparams_override&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;num_prime_capsules&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;64,padding&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SAME,leaky&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;,remake&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--summary_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/ &lt;span class=&quot;nt&quot;&gt;--checkpoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$CKPT_DIR&lt;/span&gt;/cifar/cifar&lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;/model.ckpt-600000 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--num_trials&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;123-训练&quot;&gt;12.3 训练&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;mnist&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python experiment.py &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--dataset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;cifar10 &lt;span class=&quot;nt&quot;&gt;--max_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;600000&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--hparams_override&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;num_prime_capsules&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;64,padding&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SAME,leaky&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;,remake&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--summary_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;mnist baseline&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python experiment.py &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt;/mnist_data/ &lt;span class=&quot;nt&quot;&gt;--max_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;300000&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--summary_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/attempt1/ &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;baseline
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;cifar&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python experiment.py &lt;span class=&quot;nt&quot;&gt;--ata_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt;/mnist_data/ &lt;span class=&quot;nt&quot;&gt;--max_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;300000&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--summary_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/attempt0/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;124-训练验证&quot;&gt;12.4 训练+验证&lt;/h3&gt;

&lt;p&gt;训练时在验证集上验证：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--validate=true&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;需要两个gpu：一个训练；一个验证&lt;/li&gt;
  &lt;li&gt;如果两个job都在同一台机器，需要限制每个job的RAM大小，因为 TensorFlow will fill all your RAM for the session of your first job and your second job will fail&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python experiment.py &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt;/mnist_data/ &lt;span class=&quot;nt&quot;&gt;--max_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;300000&lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--summary_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/attempt0/ &lt;span class=&quot;nt&quot;&gt;--train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--validate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;测试/训练 MultiMNIST:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;--num_targets&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2
&lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt;/multitest_6shifted_mnist.tfrecords@10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;生成multiMNIST/MNIST records的代码：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;input_data/mnist/mnist_shift.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;generate multiMNIST test split的代码：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python mnist_shift.py &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt;/mnist_data/ &lt;span class=&quot;nt&quot;&gt;--split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--shift&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;6 
&lt;span class=&quot;nt&quot;&gt;--pad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4 &lt;span class=&quot;nt&quot;&gt;--num_pairs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000 &lt;span class=&quot;nt&quot;&gt;--max_shard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;100000 &lt;span class=&quot;nt&quot;&gt;--multi_targets&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;build expanded_mnist for affNIST generalizability：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;--shift&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;6 &lt;span class=&quot;nt&quot;&gt;--pad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;13-源码解析&quot;&gt;13. 源码解析&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737203&amp;amp;idx=1&amp;amp;sn=43c2b6f0e62f8c4aa3f913aa8b9c9620&amp;amp;chksm=871ace4db06d475be8366969d74c4b2250602f5e262a3f97a5faf2183e53474d3f9fd6763308&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0206E7CpxZFlD3UayCkIu2Pk&amp;amp;pass_ticket=eOgGMWWl1n%2B%2FqN%2BBmdlJBRG9MYce7eTK6SlPMr8UGO1IWGU8J0lq%2Bggu9PIzKO2r#rd&quot;&gt;Capsule官方代码开源之后，机器之心做了份核心代码解读&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>cv方面的paper</title>
   <link href="http://daiwk.github.io/posts/cv-cv-papers.html"/>
   <updated>2018-01-31T00:00:00+00:00</updated>
   <id>/posts/cv-cv-papers</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#2017%e5%b9%b4&quot;&gt;2017年&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#mask-r-cnn&quot;&gt;Mask R-CNN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#image-to-image-translation-with-conditional-adversarial-networks&quot;&gt;Image-to-Image Translation with Conditional Adversarial Networks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#a-fast-rcnn-hard-positive-generation-via-adversary-for-object-detection&quot;&gt;A-Fast-RCNN: Hard Positive Generation via Adversary for Object Detection&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bayesian-gan&quot;&gt;Bayesian GAN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#interpretable-r-cnn&quot;&gt;Interpretable R-CNN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#learning-feature-pyramids-for-human-pose-estimation&quot;&gt;Learning Feature Pyramids for Human Pose Estimation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#unpaired-image-to-image-translation-using-cycle-consistent-adversarial-networks&quot;&gt;Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#high-resolution-image-synthesis-and-semantic-manipulation-with-conditional-gans&quot;&gt;High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#triple-generative-adversarial-nets&quot;&gt;Triple Generative Adversarial Nets&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#realtime-multi-person-2d-pose-estimation-using-part-affinity-fields&quot;&gt;Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;2017年&quot;&gt;2017年&lt;/h2&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247487348&amp;amp;idx=1&amp;amp;sn=8ee8bf57418342a419fe73829cb14e75&amp;amp;chksm=96e9d0f4a19e59e288dcb105bd90b1e13f419ee7268ac69eba7cd6dac12e2e64aa84c56e5c07&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0130mjKCPyhYcXPqH6S7PcmU&amp;amp;pass_ticket=ZxuI7pnWcDwwNYf9OiipCrRncMzWnmWp6BRC9ytl30FalvvjUOKKM1gQYP2e0qkU#rd&quot;&gt;2017年度最值得读的AI论文 | CV篇 · 评选结果公布&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;mask-r-cnn&quot;&gt;Mask R-CNN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.06870&quot;&gt;Mask R-CNN&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;image-to-image-translation-with-conditional-adversarial-networks&quot;&gt;Image-to-Image Translation with Conditional Adversarial Networks&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1611.07004&quot;&gt;Image-to-Image Translation with Conditional Adversarial Networks&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;a-fast-rcnn-hard-positive-generation-via-adversary-for-object-detection&quot;&gt;A-Fast-RCNN: Hard Positive Generation via Adversary for Object Detection&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1704.03414&quot;&gt;A-Fast-RCNN: Hard Positive Generation via Adversary for Object Detection&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;bayesian-gan&quot;&gt;Bayesian GAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1705.09558&quot;&gt;Bayesian GAN&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;interpretable-r-cnn&quot;&gt;Interpretable R-CNN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1711.05226&quot;&gt;Towards Interpretable R-CNN by Unfolding Latent Structures&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;learning-feature-pyramids-for-human-pose-estimation&quot;&gt;Learning Feature Pyramids for Human Pose Estimation&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1708.01101&quot;&gt;Learning Feature Pyramids for Human Pose Estimation&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;unpaired-image-to-image-translation-using-cycle-consistent-adversarial-networks&quot;&gt;Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.10593&quot;&gt;Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;high-resolution-image-synthesis-and-semantic-manipulation-with-conditional-gans&quot;&gt;High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1711.11585&quot;&gt;High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;triple-generative-adversarial-nets&quot;&gt;Triple Generative Adversarial Nets&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.02291&quot;&gt;Triple Generative Adversarial Nets&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;realtime-multi-person-2d-pose-estimation-using-part-affinity-fields&quot;&gt;Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1611.08050&quot;&gt;Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>2017年度10大值得读的nlp方面的paper</title>
   <link href="http://daiwk.github.io/posts/nlp-2017-nlp-papers.html"/>
   <updated>2018-01-30T00:00:00+00:00</updated>
   <id>/posts/nlp-2017-nlp-papers</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-attention-is-all-you-need&quot;&gt;1. Attention Is All You Need&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-reinforcement-learning-for-relation-classification-from-noisy-data&quot;&gt;2. Reinforcement Learning for Relation Classification from Noisy Data&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-convolutional-sequence-to-sequence-learning&quot;&gt;3. Convolutional Sequence to Sequence Learning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-zero-shot-relation-extraction-via-reading-comprehension&quot;&gt;4. Zero-Shot Relation Extraction via Reading Comprehension&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-irgan-a-minimax-game-for-unifying-generative-and-discriminative-information-retrieval-models&quot;&gt;5. IRGAN: A Minimax Game for Unifying Generative and Discriminative Information Retrieval Models&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-neural-relation-extraction-with-selective-attention-over-instances&quot;&gt;6. Neural Relation Extraction with Selective Attention over Instances&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-unsupervised-neural-machine-translation&quot;&gt;7. Unsupervised Neural Machine Translation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#8-joint-extraction-of-entities-and-relations-based-on-a-novel-tagging-scheme&quot;&gt;8. Joint Extraction of Entities and Relations Based on a Novel Tagging Scheme&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#9-a-structured-self-attentive-sentence-embedding&quot;&gt;9. A Structured Self-attentive Sentence Embedding&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#10-dialogue-learning-with-human-in-the-loop&quot;&gt;10. Dialogue Learning With Human-In-The-Loop&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247487348&amp;amp;idx=1&amp;amp;sn=8ee8bf57418342a419fe73829cb14e75&amp;amp;chksm=96e9d0f4a19e59e288dcb105bd90b1e13f419ee7268ac69eba7cd6dac12e2e64aa84c56e5c07&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0130mjKCPyhYcXPqH6S7PcmU&amp;amp;pass_ticket=ZxuI7pnWcDwwNYf9OiipCrRncMzWnmWp6BRC9ytl30FalvvjUOKKM1gQYP2e0qkU#rd&quot;&gt;2017年度最值得读的AI论文 | NLP篇 · 评选结果公布&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-attention-is-all-you-need&quot;&gt;1. Attention Is All You Need&lt;/h2&gt;

&lt;h2 id=&quot;2-reinforcement-learning-for-relation-classification-from-noisy-data&quot;&gt;2. Reinforcement Learning for Relation Classification from Noisy Data&lt;/h2&gt;

&lt;h2 id=&quot;3-convolutional-sequence-to-sequence-learning&quot;&gt;3. Convolutional Sequence to Sequence Learning&lt;/h2&gt;

&lt;h2 id=&quot;4-zero-shot-relation-extraction-via-reading-comprehension&quot;&gt;4. Zero-Shot Relation Extraction via Reading Comprehension&lt;/h2&gt;

&lt;h2 id=&quot;5-irgan-a-minimax-game-for-unifying-generative-and-discriminative-information-retrieval-models&quot;&gt;5. IRGAN: A Minimax Game for Unifying Generative and Discriminative Information Retrieval Models&lt;/h2&gt;

&lt;h2 id=&quot;6-neural-relation-extraction-with-selective-attention-over-instances&quot;&gt;6. Neural Relation Extraction with Selective Attention over Instances&lt;/h2&gt;

&lt;h2 id=&quot;7-unsupervised-neural-machine-translation&quot;&gt;7. Unsupervised Neural Machine Translation&lt;/h2&gt;

&lt;h2 id=&quot;8-joint-extraction-of-entities-and-relations-based-on-a-novel-tagging-scheme&quot;&gt;8. Joint Extraction of Entities and Relations Based on a Novel Tagging Scheme&lt;/h2&gt;

&lt;h2 id=&quot;9-a-structured-self-attentive-sentence-embedding&quot;&gt;9. A Structured Self-attentive Sentence Embedding&lt;/h2&gt;

&lt;h2 id=&quot;10-dialogue-learning-with-human-in-the-loop&quot;&gt;10. Dialogue Learning With Human-In-The-Loop&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>pytorch一周年</title>
   <link href="http://daiwk.github.io/posts/platform-pytorch-anniversary.html"/>
   <updated>2018-01-20T00:00:00+00:00</updated>
   <id>/posts/platform-pytorch-anniversary</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#Community&quot;&gt;Community&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#Research-papers-packages-and-Github&quot;&gt;Research papers, packages and Github&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#cycle-GAN&quot;&gt;cycle-GAN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#opennmt&quot;&gt;opennmt&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E8%B6%85%E5%88%86%E8%BE%A8%E7%8E%87&quot;&gt;超分辨率&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#PyTorch-QRNN&quot;&gt;PyTorch-QRNN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#Pyro--ProbTorch&quot;&gt;Pyro &amp;amp; ProbTorch&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#pix2pixHD-sentiment-neuron--flownet2&quot;&gt;pix2pixHD, sentiment neuron &amp;amp; flownet2&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#allenNLP&quot;&gt;allenNLP&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#DSB2017%E5%86%A0%E5%86%9B&quot;&gt;DSB2017冠军&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%8F%AF%E8%A7%86%E5%8C%96&quot;&gt;可视化&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#Facebook-AI-Research&quot;&gt;Facebook AI Research&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Metrics&quot;&gt;Metrics&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Research-Metrics&quot;&gt;Research Metrics&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Courses-Tutorials-and-Books&quot;&gt;Courses, Tutorials and Books&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#Engineering&quot;&gt;Engineering&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#Higher-order-gradients&quot;&gt;Higher-order gradients&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Distributed-PyTorch&quot;&gt;Distributed PyTorch&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Closer-to-NumPy&quot;&gt;Closer to NumPy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Sparse-Tensors&quot;&gt;Sparse Tensors&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Performance&quot;&gt;Performance&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#Reducing-framework-overhead-by-10x-across-board&quot;&gt;Reducing framework overhead by 10x across board&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#ATen&quot;&gt;ATen&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#Exporting-models-to-production--ONNX-Support-and-the-JIT-compiler&quot;&gt;Exporting models to production — ONNX Support and the JIT compiler&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650736406&amp;amp;idx=1&amp;amp;sn=db8da5ddc9a9cf86e804d29eb817f078&amp;amp;chksm=871ac368b06d4a7ec4854a44cc758a2d9610a350569be43209d4f99868e7341e7a551155079e&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0120WbpeJXZUHZpvcIhqA8Xu&amp;amp;pass_ticket=xK%2FffWIobjEWlqRcODdvVXVcND5Es%2FtthdouMWebLV70o%2F654l0vh1big6xAGCGm#rd&quot;&gt;PyTorch一周年战绩总结：是否比TensorFlow来势凶猛？&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原文：&lt;a href=&quot;http://pytorch.org/2018/01/19/a-year-in.html&quot;&gt;http://pytorch.org/2018/01/19/a-year-in.html&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;community&quot;&gt;Community&lt;/h1&gt;

&lt;h2 id=&quot;research-papers-packages-and-github&quot;&gt;Research papers, packages and Github&lt;/h2&gt;

&lt;p&gt;人们一起创建了 &lt;strong&gt;torchtext&lt;/strong&gt;、&lt;strong&gt;torchvision&lt;/strong&gt; 和 &lt;strong&gt;torchaudio&lt;/strong&gt;，以便利化平民化不同领域的研究。&lt;/p&gt;

&lt;p&gt;首个 PyTorch 社区工具包（被命名为 Block）来自 Brandon Amo，有助于更轻松地处理块矩阵（block matrix）。来自 CMU 的 Locus 实验室后来继续公布 PyTorch 工具包及其大部分研究的实现。首个研究论文代码来自 Sergey Zagoruyko，论文名称为《Paying more attention to attention》。&lt;/p&gt;

&lt;h3 id=&quot;cycle-gan&quot;&gt;cycle-GAN&lt;/h3&gt;

&lt;p&gt;来自 U.C.Berkeley 的 Jun-Yan Zhu、Taesung Park、Phillip Isola、Alyosha Efros 及团队发布了非常流行的 &lt;strong&gt;Cycle-GAN&lt;/strong&gt; 和 &lt;strong&gt;pix2pix&lt;/strong&gt;，用于图像转换。&lt;/p&gt;

&lt;p&gt;torch版：&lt;a href=&quot;https://github.com/junyanz/CycleGAN&quot;&gt;https://github.com/junyanz/CycleGAN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pytorch版：&lt;a href=&quot;https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix&quot;&gt;https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文链接：&lt;a href=&quot;https://arxiv.org/abs/1703.10593&quot;&gt;https://arxiv.org/abs/1703.10593&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;介绍：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650725257&amp;amp;idx=4&amp;amp;sn=bf367ff90e03f8189f7c67ae0e5ab76f&amp;amp;chksm=871b1ff7b06c96e1e355d8b360abd0c256af04e2ba72a8d2a3364bfea8ff80b347a734d17e9d&amp;amp;scene=21#wechat_redirect&quot;&gt;让莫奈画作变成照片：伯克利图像到图像翻译新研究&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;opennmt&quot;&gt;opennmt&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650721602&amp;amp;idx=3&amp;amp;sn=4f80dae1dfe49c1151288a60731d4b40&amp;amp;chksm=871b093cb06c802abc2c4f3dc7c2989caad155308667da292888a26c6297be28c37018e40769&amp;amp;scene=21#wechat_redirect&quot;&gt;哈佛大学NLP组开源神经机器翻译工具包OpenNMT：已达到生产可用水平&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HarvardNLP 和 Systran 的研究者开始使用 PyTorch 开发和提升 OpenNMT，它最初开始于 Facebook Adam Lerer 的 [Lua]Torch 代码最初的再实现。&lt;/p&gt;

&lt;p&gt;项目主页：&lt;a href=&quot;http://opennmt.net/&quot;&gt;http://opennmt.net/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pytorch版：&lt;a href=&quot;https://github.com/OpenNMT/OpenNMT-py&quot;&gt;https://github.com/OpenNMT/OpenNMT-py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;tf版：
&lt;a href=&quot;https://github.com/OpenNMT/OpenNMT-tf&quot;&gt;https://github.com/OpenNMT/OpenNMT-tf&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;超分辨率&quot;&gt;超分辨率&lt;/h3&gt;

&lt;p&gt;来自 Twitter 的 MagicPony 团队贡献了其超分辨率研究示例的 PyTorch 实现【直接集成进pytorch的examples中】&lt;a href=&quot;https://github.com/pytorch/examples/tree/master/super_resolution&quot;&gt;https://github.com/pytorch/examples/tree/master/super_resolution&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;pytorch-qrnn&quot;&gt;PyTorch-QRNN&lt;/h3&gt;

&lt;p&gt;Salesforce 发布了若干个工具包，包括其亮点成果 PyTorch-QRNN，这是一种新型 RNN，相比于 CuDNN 优化的标准 LSTM 可提速 2 到 17 倍。James Bradbury 及其团队是 PyTorch 社区中最活跃和最有吸引力的团队之一。&lt;/p&gt;

&lt;p&gt;pytorch：&lt;a href=&quot;https://github.com/salesforce/pytorch-qrnn&quot;&gt;https://github.com/salesforce/pytorch-qrnn&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;pyro--probtorch&quot;&gt;Pyro &amp;amp; ProbTorch&lt;/h3&gt;

&lt;p&gt;来自 Uber、Northeaster、Stanford 的研究者围绕着其工具包 Pyro 和 ProbTorch，形成了一个活跃的概率编程社区。他们正在积极开发 torch.distributions 核心工具包。该社区非常活跃，快速发展，我们联合 Fritz Obermeyer、Noah Goodman、Jan-Willem van de Meent、Brooks Paige、Dustin Tran 及其他 22 名参会者在 NIPS 2017 上举办了首次 PyTorch 概率编程会议，共同探讨如何使世界贝叶斯化。&lt;/p&gt;

&lt;p&gt;pyro：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://pyro.ai/&quot;&gt;http://pyro.ai/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650732815&amp;amp;idx=1&amp;amp;sn=eddc61facb9f25da638be9fc494302e2&amp;amp;chksm=871b3d71b06cb46703d9781830dc6949ab14b531026a91b84366b9c5abd8b59c0f4154e1a115&amp;amp;scene=21#wechat_redirect&quot;&gt;Uber 与斯坦福大学开源深度概率编程语言 Pyro：基于 PyTorch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;probtorch：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/probtorch/probtorch&quot;&gt;https://github.com/probtorch/probtorch&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;pix2pixhd-sentiment-neuron--flownet2&quot;&gt;pix2pixHD, sentiment neuron &amp;amp; flownet2&lt;/h3&gt;

&lt;p&gt;英伟达研究者发布了三个高质量 repo，实现了 pix2pix-HD、Sentiment Neuron 和 FlowNet2。对 PyTorch 中不同数据并行模型的扩展性分析对整个社区都很有益。&lt;/p&gt;

&lt;p&gt;pix2pix-HD：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/NVIDIA/pix2pixHD&quot;&gt;https://github.com/NVIDIA/pix2pixHD&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;sentiment neuron: 
&lt;a href=&quot;https://github.com/NVIDIA/sentiment-discovery&quot;&gt;https://github.com/NVIDIA/sentiment-discovery&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;flownet2: 
&lt;a href=&quot;https://github.com/NVIDIA/flownet2-pytorch&quot;&gt;https://github.com/NVIDIA/flownet2-pytorch&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;allennlp&quot;&gt;allenNLP&lt;/h3&gt;

&lt;p&gt;艾伦人工智能研究所发布 AllenNLP，包括多个 NLP 先进模型：标准 NLP 任务的参考实现和易用 web demo。&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/allenai/allennlp&quot;&gt;https://github.com/allenai/allennlp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;allenNLP: &lt;a href=&quot;http://allennlp.org/&quot;&gt;http://allennlp.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;demo[包括machine-comprehension/textuale-ntailment/semantic-role-labeling/coreference-resolution/named-entity-recognitio]：&lt;a href=&quot;http://demo.allennlp.org/machine-comprehension&quot;&gt;http://demo.allennlp.org/machine-comprehension&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;dsb2017冠军&quot;&gt;DSB2017冠军&lt;/h3&gt;

&lt;p&gt;六月份，我们还首次取得了 Kaggle 竞赛冠军（团队 grt123）。他们获得了 2017 数据科学杯（关于肺癌检测）【DataScience Bowl 2017 on Lung Cancer detection】的冠军，后来公开了其 PyTorch 实现：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/lfz/DSB2017&quot;&gt;https://github.com/lfz/DSB2017&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;可视化&quot;&gt;可视化&lt;/h3&gt;

&lt;p&gt;在可视化方面，Tzu-Wei Huang 实现了 TensorBoard-PyTorch 插件
&lt;a href=&quot;https://github.com/lanpa/tensorboard-pytorch&quot;&gt;https://github.com/lanpa/tensorboard-pytorch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Facebook AI Research 发布了与 PyTorch 兼容的 &lt;strong&gt;visdom&lt;/strong&gt; 可视化包。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebookresearch/visdom&quot;&gt;https://github.com/facebookresearch/visdom&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;facebook-ai-research&quot;&gt;Facebook AI Research&lt;/h3&gt;

&lt;p&gt;Facebook AI Research 发布了多个项目，如 ParlAI、fairseq-py、VoiceLoop 和 FaderNetworks，在多个领域中实现了先进的模型和接口数据集。由于空间有限，这里就不将优秀项目一一列出，详细列表可参阅：&lt;a href=&quot;https://github.com/facebookresearch/&quot;&gt;https://github.com/facebookresearch/&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;ParlAI:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebookresearch/ParlAI&quot;&gt;https://github.com/facebookresearch/ParlAI&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;fairseq-py:
&lt;a href=&quot;https://github.com/facebookresearch/fairseq-py&quot;&gt;https://github.com/facebookresearch/fairseq-py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;VoiceLoop:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebookresearch/loop&quot;&gt;https://github.com/facebookresearch/loop&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;FaderNetworks:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebookresearch/FaderNetworks&quot;&gt;https://github.com/facebookresearch/FaderNetworks&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;metrics&quot;&gt;Metrics&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;在 Github 上有 87769 行代码引入 Torch。&lt;/li&gt;
  &lt;li&gt;在 Github 上有 3983 个 repository 在名字或者描述中提到了 PyTorch。&lt;/li&gt;
  &lt;li&gt;PyTorch binary 下载量超过 50 万，具体数字是 651916。&lt;/li&gt;
  &lt;li&gt;在论坛上，有 5400 名用户发表了 21500 条讨论，涉及 5200 个主题。&lt;/li&gt;
  &lt;li&gt;自发布以来，在 Reddit 上的/r/machinelearning 主题中有 131 条讨论提到了 PyTorch。同期，TensorFlow 被提及的次数为 255。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;pytorch v.s. tensorflow:
&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650723769&amp;amp;idx=1&amp;amp;sn=17565e650771699ceddabb214d485626&amp;amp;chksm=871b11c7b06c98d1c76623f7c90120e363cc43462b74f22c27038e324c2975ec4d0db5b483c1&amp;amp;scene=21#wechat_redirect&quot;&gt;PyTorch和TensorFlow到底哪个更好？看看一线开发者怎么说&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650720407&amp;amp;idx=1&amp;amp;sn=768d7248e0ab5fa469dbae86d11152e1&amp;amp;chksm=871b0ce9b06c85ffefa2e0c8f6fb7ae4cc1c0500cda7bad008fe68ed6b87d7c8765d138e1fd1&amp;amp;scene=21#wechat_redirect&quot;&gt;TensorFlow开源一周年：这可能是一份最完整的盘点&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;research-metrics&quot;&gt;Research Metrics&lt;/h2&gt;

&lt;p&gt;PyTorch 是一个专注于研究的框架。所以与衡量它的指标包括 PyTorch 在机器学习研究论文中的使用。&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;在 ICLR 2018 学术会议提交的论文中，有 87 篇提到了 PyTorch，相比之下 TensorFlow 228 篇，Keras 42 篇，Theano 和 Matlab 是 32 篇。&lt;/li&gt;
  &lt;li&gt;按照月度来看，arXiv 论文提到 PyTorch 框架的有 72 篇，TensorFlow 是 273 篇，Keras 100 篇，Caffe 94 篇，Theano 53 篇。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;courses-tutorials-and-books&quot;&gt;Courses, Tutorials and Books&lt;/h2&gt;

&lt;p&gt;Sasank Chilamkurthy 承担了改进教程的任务，教程详见：&lt;a href=&quot;http://pytorch.org/tutorials/&quot;&gt;http://pytorch.org/tutorials/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sean Robertson 和 Justin Johnson 编写了 NLP 领域的全新教程，还有通过示例学习的教程。&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/spro/practical-pytorch&quot;&gt;https://github.com/spro/practical-pytorch&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/jcjohnson/pytorch-examples&quot;&gt;https://github.com/jcjohnson/pytorch-examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yunjey Choi 写了用 30 行或者更少的代码部署大多数模型的教程。每个新教程都帮助用户用不同的学习方法更快地找到适合自己的学习路径。&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/yunjey/pytorch-tutorial&quot;&gt;https://github.com/yunjey/pytorch-tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goku Mohandas 和 Delip Rao 把正在写的书中的代码做了改变，使用了 PyTorch。&lt;/p&gt;

&lt;p&gt;我们看到，一些大学的机器学习课程是使用 PyTorch 作为主要工具讲授的，例如哈佛 CS 287。为了更进一步方便大众学习，我们还看到三个在线课程使用 PyTorch 讲授。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://harvard-ml-courses.github.io/cs287-web/&quot;&gt;https://harvard-ml-courses.github.io/cs287-web/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fast.ai 的「Deep Learning for Coders」是个流行的在线课程。9 月份，Jeremy 和 Rachel 宣布下一个 fast.ai 的课程将几乎全部基于 PyTorch。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.fast.ai/2017/09/08/introducing-pytorch-for-fastai/&quot;&gt;http://www.fast.ai/2017/09/08/introducing-pytorch-for-fastai/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ritchie Ng，在清华、新加坡国立大学都学习过的研究者，推出了名为「Practical Deep Learning with PyTorch」的 Udemy 课程。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.udemy.com/practical-deep-learning-with-pytorch/&quot;&gt;https://www.udemy.com/practical-deep-learning-with-pytorch/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;来自香港科技大学的 Sung Kim 在 Yotube 上推出了面向普通观众的在线课程「PyTorch Zero to All」。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/playlist?list=PLlMkM4tgfjnJ3I-dbhO9JTw7gNty6o_2m&quot;&gt;https://www.youtube.com/playlist?list=PLlMkM4tgfjnJ3I-dbhO9JTw7gNty6o_2m&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650731685&amp;amp;idx=1&amp;amp;sn=9b8cfdf380ff9c8c91b45ebe7452f4ee&amp;amp;chksm=871b30dbb06cb9cd199412e72d7740970e82c7c61057473871287706a4239f3661eafbfd1630&amp;amp;scene=21#wechat_redirect&quot;&gt;四天速成！香港科技大学 PyTorch 课件分享&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;engineering&quot;&gt;Engineering&lt;/h1&gt;

&lt;h2 id=&quot;higher-order-gradients&quot;&gt;Higher-order gradients&lt;/h2&gt;

&lt;p&gt;随着多篇关于实现&lt;strong&gt;梯度罚项&lt;/strong&gt;的论文的发表，以及&lt;strong&gt;二阶梯度&lt;/strong&gt;法的不断研究发展，高阶梯度成为必需的热门功能。去年 8 月，我们实现了一个通用接口，&lt;strong&gt;可使用 n 阶导数&lt;/strong&gt;，加快&lt;strong&gt;支持高阶梯度函数的收敛&lt;/strong&gt;，截至写作本文时，几乎所有 ops 都支持此界面。&lt;/p&gt;

&lt;h2 id=&quot;distributed-pytorch&quot;&gt;Distributed PyTorch&lt;/h2&gt;

&lt;p&gt;去年 8 月，我们发布了一个小型分布式包，该包使用非常流行的 MPI 集合（MPI-collective）方法。&lt;strong&gt;它有多个后端，如 TCP、MPI、Gloo 和 NCCL2&lt;/strong&gt;，以支持多种&lt;strong&gt;CPU/GPU集合&lt;/strong&gt;操作和用例，这个包整合了 &lt;strong&gt;Infiniband 和 RoCE&lt;/strong&gt; 等分布式技术。分布很难，我们在初始迭代时也有一些 bug。在后续版本中，我们作出了一些改进，使这个包更加稳定，性能也更强。&lt;/p&gt;

&lt;h2 id=&quot;closer-to-numpy&quot;&gt;Closer to NumPy&lt;/h2&gt;

&lt;p&gt;用户最大的一个需求是他们熟悉的 NumPy 功能。&lt;strong&gt;Broadcasting&lt;/strong&gt; 和 &lt;strong&gt;Advanced Indexing&lt;/strong&gt; 等功能方便、简洁，节约用户的时间。我们实现了这些功能，开始使我们的 API 更接近 NumPy。随着时间的进展，我们希望在合适的地方越来越接近 NumPy 的 API。&lt;/p&gt;

&lt;h2 id=&quot;sparse-tensors&quot;&gt;Sparse Tensors&lt;/h2&gt;

&lt;p&gt;In March, we released a small package supporting sparse Tensors and in May we released CUDA support for the sparse package. The package is small and limited in functionality, and is used for &lt;strong&gt;implementing Sparse Embeddings and commonly used sparse paradigms in deep learning&lt;/strong&gt;. This package is still small in scope and there’s demand to expand it — if you are interested in working on expanding the sparse package, reach out to us on our &lt;a href=&quot;https://discuss.pytorch.org/&quot;&gt;Discussion Boards&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;performance&quot;&gt;Performance&lt;/h2&gt;

&lt;p&gt;性能是一场仍在进行中的战斗，尤其对于想要最大化灵活性的动态框架 PyTorch 而言。去年，从核心 Tensor 库到神经网络算子，我们改善了 PyTorch 在 board 上的性能，能在 board 上更快的编写微优化。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;我们添加了专门的 &lt;strong&gt;AVX 和 AVX2 内部函数，用于 Tensor 运算&lt;/strong&gt;；&lt;/li&gt;
  &lt;li&gt;写&lt;strong&gt;更快的 GPU kernel&lt;/strong&gt;，用于常用的工作负载，如级联和 Softmax；&lt;/li&gt;
  &lt;li&gt;为多个神经网络算子重写代码，如 nn.Embedding 和组卷积。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;reducing-framework-overhead-by-10x-across-board&quot;&gt;Reducing framework overhead by 10x across board&lt;/h3&gt;

&lt;p&gt;由于 PyTorch 是动态图框架，我们在训练循环的&lt;strong&gt;每次迭代时都要创建一个新图&lt;/strong&gt;。因此，框架开销必须很低，或者工作负载必须足够大来隐藏框架开销。去年 8 月，DyNet 的作者（Graham Neubig 及其团队）展示了 DyNet 在一些小型 NLP 模型上的速度快于 PyTorch。这是很有意思的一个挑战，我们开始重写 PyTorch 内部构件，&lt;strong&gt;将框架开销从 10 微秒／算子降低到 1 微秒&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;aten&quot;&gt;ATen&lt;/h3&gt;

&lt;p&gt;重新设计 PyTorch 内部构件的同时，我们也构建了 ATen C++11 库，该库现在主导 PyTorch 所有后端。ATen 具备一个类似 PyTorch Python API 的 API，使之成为&lt;strong&gt;便于 Tensor 计算的 C++库&lt;/strong&gt;。ATen 可由 PyTorch 独立构建和使用。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/pytorch/pytorch/tree/master/aten&quot;&gt;https://github.com/pytorch/pytorch/tree/master/aten&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;exporting-models-to-production--onnx-support-and-the-jit-compiler&quot;&gt;Exporting models to production — ONNX Support and the JIT compiler&lt;/h2&gt;

&lt;p&gt;我们收到的一个普遍请求是将 PyTorch 模型输出到另一个框架。&lt;strong&gt;用户使用 PyTorch 进行快速研究，模型完成后，他们想将模型搭载到更大的项目中，而该项目只要求使用 C++。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;因此我们构建了 &lt;a href=&quot;https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/tracer.h&quot;&gt;&lt;strong&gt;tracer&lt;/strong&gt;&lt;/a&gt;，可将 PyTorch 模型输出为中间表示。用户可使用后续的 tracer 更高效地运行当前的 PyTorch 模型，或将其转换成 ONNX 格式以输出至 Caffe2、MXNet、TensorFlow 等其他框架，或直接搭载至硬件加速库，如 CoreML 或 TensorRT。今年，我们将更多地利用 JIT 编译器提升性能。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SQuAD比赛算法综述</title>
   <link href="http://daiwk.github.io/posts/nlp-squad.html"/>
   <updated>2018-01-14T00:00:00+00:00</updated>
   <id>/posts/nlp-squad</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-slqa&quot;&gt;1. SLQA&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-整体思路&quot;&gt;1.1 整体思路&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-slqa架构&quot;&gt;1.2 SLQA架构&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-相关模型&quot;&gt;2. 相关模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-基本模型&quot;&gt;2.1 基本模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-match-lstm4&quot;&gt;2.2 Match-LSTM[4]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#23-bidaf5&quot;&gt;2.3 BiDAF[5]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#24-dynamic-coattention-network6&quot;&gt;2.4 Dynamic Coattention Network[6]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#25-multi-perspective-matching7&quot;&gt;2.5 Multi-Perspective Matching[7]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#26-fastqaext8&quot;&gt;2.6 FastQAExt[8]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#27-jnet9&quot;&gt;2.7 jNet[9]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#28-ruminating-reader10&quot;&gt;2.8 Ruminating Reader[10]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#29-reasonet11&quot;&gt;2.9 ReasoNet[11]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#210-r-net13&quot;&gt;2.10 r-net[13]&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#211-mnemonic-reader14&quot;&gt;2.11 Mnemonic Reader[14]&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-小结&quot;&gt;3. 小结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-参考文献&quot;&gt;4. 参考文献&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#x-qanet&quot;&gt;x. QANet&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#x1-cnnself-attention&quot;&gt;x.1 cnn+self-attention&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#x2-利用机器翻译进行数据增强&quot;&gt;x.2 利用机器翻译进行数据增强&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650736188&amp;amp;idx=2&amp;amp;sn=03793343fabba546cafd9540be0f8277&amp;amp;chksm=871ac242b06d4b54283e7926a91dd4aff8a9480acd4f79a57785822c080dc8e027da96b1d04f&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0114a1kagAyjzKDjM5D8be80&amp;amp;pass_ticket=maZ8vJqYz24CXvI%2FR2qg58OsViKCicnkKXyOdUjO2Iy11TDgRJHxroNhcwnQ9Lne#rd&quot;&gt;机器阅读理解打破人类记录，解读阿里iDST SLQA技术&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SQuAD 数据集是行业内公认的机器阅读理解标准水平测试，也是该领域顶级赛事，被誉为机器阅读理解界的 ImageNet（图像识别领域的顶级赛事）。&lt;/p&gt;

&lt;p&gt;SQuAD 是由 Rajpurkar 等人[1]提出的一个最新的阅读理解数据集。该数据集包含 10 万个（问题，原文，答案）三元组，原文来自于 536 篇维基百科文章，而问题和答案的构建主要是通过众包的方式，让标注人员提出最多 5 个基于文章内容的问题并提供正确答案，且答案出现在原文中。SQuAD 和之前的完形填空类阅读理解数据集如 CNN/DM[2]，CBT[3]等最大的区别在于：&lt;strong&gt;SQuAD 中的答案不在是单个实体或单词，而可能是一段短语&lt;/strong&gt;，这使得其答案更难预测。SQuAD 包含公开的训练集和开发集，以及一个隐藏的测试集，其采用了与 ImageNet 类似的封闭评测的方式，研究人员需提交算法到一个开放平台，并由 SQuAD 官方人员进行测试并公布结果。&lt;/p&gt;

&lt;p&gt;机器阅读理解的评测维度分为 EM（Exact Match，精准匹配分数）和 F1（精确率和召回率的平均，模糊匹配分数）。&lt;/p&gt;

&lt;h2 id=&quot;1-slqa&quot;&gt;1. SLQA&lt;/h2&gt;

&lt;p&gt;本次阿里巴巴参与测评的系统名为 SLQA，Semantic Learning for Question Answering，是 iDST NLP 团队提出的「基于分层融合注意力机制」的深度神经网络系统。评测证明，相比传统方法，SLQA 的效果取得了显著的提升。&lt;/p&gt;

&lt;p&gt;采用传统方法解决机器阅读理解问题，一般会将该过程分为以下几个步骤：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对问题、篇章分别进行词法、句法分析，针对分析结果进行特征提取：&lt;/li&gt;
  &lt;li&gt;基于特征采用诸如 LR、CRF 等模型进行答案边界预测；&lt;/li&gt;
  &lt;li&gt;采用梯度下降类算法在训练集上进行优化，拟合数据分布。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在此过程中，基础语言模型、依存分析等模块的准确率在一定程度上会影响训练效果，特征工程的优劣也同样左右着是否能训练得到可用的模型。&lt;/p&gt;

&lt;h3 id=&quot;11-整体思路&quot;&gt;1.1 整体思路&lt;/h3&gt;

&lt;p&gt;人类在进行阅读理解时，常见思维顺序如下：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;通读篇章，理解文章主题和大体内容；读题，了解提问内容及关注点。&lt;/li&gt;
  &lt;li&gt;带着问题找答案，将问题同篇章做关联，并结合篇章主题，理解问题重点。&lt;/li&gt;
  &lt;li&gt;定位可能的答案范围，并再次重点阅读附近文字。&lt;/li&gt;
  &lt;li&gt;为避免忘记问题，再次审题，并结合 3. 中重点区域进行答案圈选。&lt;/li&gt;
  &lt;li&gt;针对挑出的答案候选进行精筛，确定最正确的答案。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;因此，构建模型的主要思想是在捕捉&lt;strong&gt;问题和文章中特定区域关联&lt;/strong&gt;的同时，借助分层策略，&lt;strong&gt;逐步集中注意力&lt;/strong&gt;，使答案边界清晰。&lt;/p&gt;

&lt;p&gt;同时，为了避免过于关注细节，&lt;strong&gt;采用融合方式将全局信息加入注意力机制&lt;/strong&gt;，进行适度纠正，确保关注点正确。这种逐步聚焦并兼顾全局的方式与其他参赛者已经公布的的做法不太相同，也是团队此次刷榜登顶的关键所在。&lt;/p&gt;

&lt;h3 id=&quot;12-slqa架构&quot;&gt;1.2 SLQA架构&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/slqa_framework.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;目前业界主流的基于 End2End 学习的机器阅读理解模型主要为&lt;strong&gt;Encode-Interaction-Pointer框架&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;SLQA 系统包含如下基本结构：&lt;strong&gt;Encoder Layer（文本表征）&lt;/strong&gt;，&lt;strong&gt;Attention Layer（注意力机制）&lt;/strong&gt;，&lt;strong&gt;Match Layer（问题篇章匹配）&lt;/strong&gt;以及 &lt;strong&gt;Output Layer（答案预测）&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Encoder Layer&lt;/strong&gt;: 用于表示学习，可以理解为语言模型层，用以将篇章及问题从离散字符转变为蕴含语义的表征向量。团队采用了&lt;strong&gt;多层双向 LSTM&lt;/strong&gt; 并分别对篇章和问题进行主题和重点词关注。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Attention Layer&lt;/strong&gt;: 得到有效的问题及篇章表征后，为表达依据问题定位答案过程，缩小备选答案查找范围，将搜索空间通过注意力机制约束，主要进行&lt;strong&gt;多层融合注意力表示&lt;/strong&gt;，对问题和篇章进行相关性对齐（Align），并不断补充全局信息（Fusion），每一次对齐都基于下层信息并在此基础上更加细化（paragraph→sentence→phrase→word），采用的方式分别为 &lt;strong&gt;Co-Attention（篇章到问题，问题到篇章）&lt;/strong&gt;，&lt;strong&gt;Self-Attention（问题自身，篇章自身）&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Match Layer&lt;/strong&gt;: 用于做融合信息后的问题和篇章匹配，团队采用&lt;strong&gt;双线性矩阵&lt;/strong&gt;来学习经过多层信息过滤后的篇章和问题匹配参数，由于在前一阶段无关信息已经被过滤，最后的匹配可完成答案的定位工作。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Output Layer&lt;/strong&gt;: 结合匹配信息对篇章中词汇进行标注，预测相应词汇是答案开始位置或结束位置的概率。之后，模型会抽取可能性最高的一段连续文本作为答案。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;重点探索和研究的Layer是第三层（Hierarchical Attention Fusion Network）。&lt;/p&gt;

&lt;h2 id=&quot;2-相关模型&quot;&gt;2. 相关模型&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/27015318&quot;&gt;PaperWeekly 第38期 | SQuAD综述&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;21-基本模型&quot;&gt;2.1 基本模型&lt;/h3&gt;

&lt;p&gt;由于 SQuAD 的答案限定于来自原文，模型只需要判断原文中哪些词是答案即可，因此是一种抽取式的 QA 任务而不是生成式任务。&lt;/p&gt;

&lt;p&gt;几乎所有做 SQuAD 的模型都可以概括为同一种框架：Embed 层，Encode 层，Interaction 层和 Answer 层。&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Embed 层负责将原文和问题中的 tokens 映射为向量表示；&lt;/li&gt;
  &lt;li&gt;Encode 层主要使用 RNN 来对原文和问题进行编码，这样编码后每个 token 的向量表示就蕴含了上下文的语义信息；&lt;/li&gt;
  &lt;li&gt;Interaction 层是大多数研究工作聚焦的重点，该层主要负责捕捉问题和原文之间的交互关系，并输出编码了问题语义信息的原文表示，即 query-aware 的原文表示；&lt;/li&gt;
  &lt;li&gt;最后 Answer 层则基于 query-aware 的原文表示来预测答案范围。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/qa_framework.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;22-match-lstm4&quot;&gt;2.2 Match-LSTM[4]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/match_lstm.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;Match-LSTM 的 Answer 层包含了两种预测答案的模式，分别为 &lt;strong&gt;Sequence Model&lt;/strong&gt; 和 &lt;strong&gt;Boundary Model&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Sequence Model 将答案看做是一个整数组成的序列，&lt;strong&gt;每个整数表示选中的 token 在原文中的位置&lt;/strong&gt;，因此模型按顺序产生一系列条件概率，每个条件概率表示基于上轮预测的 token 产生的下个 token 的位置概率，&lt;strong&gt;最后答案总概率等于所有条件概率的乘积。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Boundary Model 简化了整个预测答案的过程，&lt;strong&gt;只预测答案开始和答案结束位置&lt;/strong&gt;，相比于 Sequence Model 极大地缩小了搜索答案的空间，最后的实验也显示简化的 Boundary Model 相比于复杂的 Sequence Model 效果更好，因此 Boundary Model 也成为后来的模型用来预测答案范围的标配。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;主要步骤如下：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Embed 层使用词向量表示原文和问题；&lt;/li&gt;
  &lt;li&gt;Encode 层使用单向 LSTM 编码原文和问题 embedding；&lt;/li&gt;
  &lt;li&gt;Interaction 层对&lt;strong&gt;原文中每个词，计算其关于问题的注意力分布&lt;/strong&gt;，并使用该注意力分布汇总问题表示，将原文该词表示和对应问题表示输入另一个 LSTM 编码，得到该词的 query-aware 表示；&lt;/li&gt;
  &lt;li&gt;在反方向重复步骤 2，获得双向 query-aware 表示；&lt;/li&gt;
  &lt;li&gt;Answer 层基于双向 query-aware 表示使用 Sequence Model 或 Boundary Model 预测答案范围。&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;23-bidaf5&quot;&gt;2.3 BiDAF[5]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/bidaf.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;相比于之前工作，BiDAF（Bi-Directional Attention Flow）最大的改进在于 Interaction 层中引入了双向注意力机制，即首先计算一个&lt;strong&gt;原文和问题的 Alignment matrix&lt;/strong&gt;，然后基于该矩阵计算 &lt;strong&gt;Query2Context 和 Context2Query 两种注意力&lt;/strong&gt;，并基于注意力计算 &lt;strong&gt;query-aware 的原文表示&lt;/strong&gt;，接着使用双向 LSTM 进行语义信息的聚合。另外，其 Embed 层中混合了&lt;strong&gt;词级 embedding 和字符级 embedding&lt;/strong&gt;，词级 embedding 使用预训练的词向量进行初始化，而&lt;strong&gt;字符级 embedding 使用 CNN 进一步编码&lt;/strong&gt;，两种 embedding 共同经过 2 层 Highway Network 作为 Encode 层输入。最后，BiDAF 同样使用 &lt;strong&gt;Boundary Model&lt;/strong&gt; 来预测答案开始和结束位置。&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/allenai/bi-att-flow&quot;&gt;https://github.com/allenai/bi-att-flow&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;24-dynamic-coattention-network6&quot;&gt;2.4 Dynamic Coattention Network[6]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/dcn.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;DCN 最大的特点在于 Answer 层，其 &lt;strong&gt;Answer 层使用了一种多轮迭代 pointing 机制&lt;/strong&gt;，&lt;strong&gt;每轮迭代都会产生对答案开始和结束位置的预测&lt;/strong&gt;，并&lt;strong&gt;基于这两个预测使用 LSTM 和 Highway Maxout Network 来更新下一轮的答案范围预测&lt;/strong&gt;。而在 Interaction 层，DCN 使用和 BiDAF 类似的&lt;strong&gt;双向注意力机制计算 query-aware 的原文表示&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;25-multi-perspective-matching7&quot;&gt;2.5 Multi-Perspective Matching[7]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/Multi-Perspective Matching.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;Multi-Perspective Matching 在 Encode 层同样使用 char, word 两个 embedding，只不过 char embedding 使用 LSTM 进行编码。在 Encode 层之前，该模型使用一个过滤操作，作用是&lt;strong&gt;过滤掉那些和问题相关度低的原文词。&lt;/strong&gt;该模型最大的特点在 Interaction 层，该层针对&lt;strong&gt;每个原文词使用一种 multi-perspective 的匹配函数计算其和问题的匹配向量&lt;/strong&gt;，并&lt;strong&gt;使用 BiLSTM 来进一步聚合这些匹配向量&lt;/strong&gt;。匹配的形式包括&lt;strong&gt;每个原文词和整个问题的表示匹配&lt;/strong&gt;，&lt;strong&gt;每个原文词和每个问题词匹配后进行最大池化&lt;/strong&gt;，和&lt;strong&gt;每个原文词和每个问题词匹配后进行平均池化&lt;/strong&gt;。最后在 Answer 层，基于匹配向量聚合表示使用两个前馈网络来预测答案开始和结束位置。&lt;/p&gt;

&lt;h3 id=&quot;26-fastqaext8&quot;&gt;2.6 FastQAExt[8]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/FastQAExt.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;FastQAExt 使用了一种轻量级的架构，其 Embed 层除了 word 和 char 两种 embeeding 作为输入以外，还额外使用了两个特征：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;binary 特征表示&lt;strong&gt;原文词是否出现在问题中&lt;/strong&gt;；&lt;/li&gt;
  &lt;li&gt;weighted 特征表示&lt;strong&gt;原文词对于问题中所有词的相似度。&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;并且这两个特征&lt;strong&gt;同样用在了问题词上&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;在 Interaction 层，FastQAExt 使用了两种轻量级的信息 fusion 策略：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Intra-Fusion&lt;/strong&gt;，即&lt;strong&gt;每个原文词&lt;/strong&gt;和&lt;strong&gt;其他原文词&lt;/strong&gt;计算相似度，并汇总得到&lt;strong&gt;原文总表示&lt;/strong&gt;，接着将&lt;strong&gt;该原文词&lt;/strong&gt;和&lt;strong&gt;对应原文总表示&lt;/strong&gt;输入 &lt;strong&gt;Highway Networks&lt;/strong&gt; 进行聚合，聚合后原文词表示进一步和上下文词表示进行类似的聚合；&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Inter-Fusion&lt;/strong&gt;，即对&lt;strong&gt;每个原文词&lt;/strong&gt;计算和&lt;strong&gt;问题词&lt;/strong&gt;的相似度，并汇总得到&lt;strong&gt;问题总表示&lt;/strong&gt;，接着将&lt;strong&gt;该原文词&lt;/strong&gt;和&lt;strong&gt;对应问题总表示&lt;/strong&gt;输入 &lt;strong&gt;Highway Networks&lt;/strong&gt; 进行聚合，得到 &lt;strong&gt;query-aware&lt;/strong&gt; 原文表示。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;此外，在 Answer 层，FastQAExt 首先计算了一个&lt;strong&gt;问题的总表示&lt;/strong&gt;，接着将 &lt;strong&gt;query-aware 原文表示&lt;/strong&gt;和&lt;strong&gt;问题总表示&lt;/strong&gt;共同输入两个前馈网络产生答案&lt;strong&gt;开始和结束位置概率&lt;/strong&gt;。在确定答案范围时，FastQAExt 使用了 Beam-search。&lt;/p&gt;

&lt;h3 id=&quot;27-jnet9&quot;&gt;2.7 jNet[9]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/jnet.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;jNet 的 baseline 模型和 BiDAF 类似，其在 Interaction 层除了对每个原文词计算一个对应的问题表示以外，还将 &lt;strong&gt;Alignment Matrix&lt;/strong&gt; 按&lt;strong&gt;原文所在维度进行池化（最大池化和平均池化）&lt;/strong&gt;，池化后的值表示原文各词的重要程度，因此&lt;strong&gt;基于该值对原文表示进行过滤&lt;/strong&gt;，剔除不重要的原文词。在 Answer 层，jNet 不仅先预测答案开始位置再预测答案结束位置，还&lt;strong&gt;反向地先预测答案结束位置再预测答案开始位置&lt;/strong&gt;。最后&lt;strong&gt;对两方向概率求平均后作为总概率输出。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;jNet 的最大创新在于对问题的理解和适应。为了在编码问题表示时&lt;strong&gt;考虑句法信息&lt;/strong&gt;，jNet 使用 &lt;strong&gt;TreeLSTM&lt;/strong&gt; 对问题进行编码，并将编码后表示作为 Interaction 层的输入。为了对不同问题进行适应，jNet &lt;strong&gt;首先使用了问题类型的 embedding&lt;/strong&gt;，将该 embeeding 作为 Interaction 层输入。&lt;/p&gt;

&lt;p&gt;另外，jNet 定义了&lt;strong&gt;K 个 cluster 的中心向量，每个 cluster model 了一个特定的问题类型&lt;/strong&gt;比如”when”,”where”等，接下来的适应算法分为两步：adapting 和 updating。&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Adapting 指&lt;strong&gt;根据问题总表示和 K 个 cluster 的相似度来更新出一个新的问题表示&lt;/strong&gt;，并输入 Interaction 层；&lt;/li&gt;
  &lt;li&gt;Updating 层旨在&lt;strong&gt;修改 K 个 cluster 的中心以令每个 cluster 可以 model 不同类型的问题&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;28-ruminating-reader10&quot;&gt;2.8 Ruminating Reader[10]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/Ruminating Reader.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;Ruminating Reader 是 BiDAF 的改进和扩展，它将之前的单 Interaction 层扩展为了&lt;strong&gt;双 Interaction 层&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;第一个 Interaction 层&lt;/strong&gt;和 BiDAF 的 Interaction 层相同，&lt;strong&gt;输出 query-aware 的原文表示&lt;/strong&gt;。query-aware 原文表示经过一个&lt;strong&gt;双向 LSTM&lt;/strong&gt; 编码，其输出的&lt;strong&gt;最后一位隐层状态&lt;/strong&gt;作为 &lt;strong&gt;query-aware 原文表示的总结&lt;/strong&gt;。接着，该&lt;strong&gt;总结向量&lt;/strong&gt;依次与&lt;strong&gt;各原文词表示&lt;/strong&gt;和&lt;strong&gt;各问题词表示&lt;/strong&gt;经过一个 &lt;strong&gt;Highway Network&lt;/strong&gt; 处理，以将总结向量的信息&lt;strong&gt;重新融入原文和问题表示&lt;/strong&gt;当中。&lt;/li&gt;
  &lt;li&gt;最后，基于更新后的原文和问题表示，使用&lt;strong&gt;第二个 Interaction&lt;/strong&gt; 层来捕捉它们之间的交互，并生成&lt;strong&gt;新的 query-aware 的原文表示&lt;/strong&gt;。Ruminating Reader 的 Embed 层，Encode 层和 Answer 层和 BiDAF 相同。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;29-reasonet11&quot;&gt;2.9 ReasoNet[11]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ReasoNet.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;&lt;strong&gt;【使用强化学习进行训练】&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;和之前介绍的 Embed-Encode-Interaction-Answer 框架不同，ReasoNet 使用了 &lt;strong&gt;Memory Networks&lt;/strong&gt; 的框架[12]。在使用 BiRNN 编码问题和原文后，&lt;strong&gt;问题&lt;/strong&gt;的&lt;strong&gt;最后一位隐层状态&lt;/strong&gt;初始化为一个&lt;strong&gt;中间状态 s&lt;/strong&gt;，而&lt;strong&gt;原文和问题表示&lt;/strong&gt;作为 &lt;strong&gt;Memory&lt;/strong&gt;。接下来是一个&lt;strong&gt;多轮迭代&lt;/strong&gt;的过程，在每一轮迭代中，&lt;strong&gt;中间状态 s&lt;/strong&gt; 首先经过一个&lt;strong&gt;逻辑回归函数&lt;/strong&gt;来输出一个 &lt;strong&gt;binary random variable t&lt;/strong&gt;，&lt;strong&gt;t 为真，那么 ReasoNet 停止&lt;/strong&gt;，并且&lt;strong&gt;用当前中间状态 s 输出到 Answer 模块产生对答案的预测&lt;/strong&gt;；否则，&lt;strong&gt;中间状态 s&lt;/strong&gt; 会和 &lt;strong&gt;Memory（原文和问题）&lt;/strong&gt;中&lt;strong&gt;每一位表示&lt;/strong&gt;计算&lt;strong&gt;注意力&lt;/strong&gt;，并&lt;strong&gt;基于注意力&lt;/strong&gt;求&lt;strong&gt;原文&lt;/strong&gt;和&lt;strong&gt;问题&lt;/strong&gt;的&lt;strong&gt;加权表示&lt;/strong&gt; x，x 和 s 共同作为一个 RNN 的输入，&lt;strong&gt;产生新的中间状态 s 并进入下一轮迭代&lt;/strong&gt;。由于出现了 binary random variable，ReasoNet &lt;strong&gt;使用了强化学习的方法&lt;/strong&gt;进行训练。&lt;/p&gt;

&lt;h3 id=&quot;210-r-net13&quot;&gt;2.10 r-net[13]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/r-net.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;r-net 同样使用了&lt;strong&gt;双 Interaction 层&lt;/strong&gt;架构，其第一 Interaction 层负责捕捉&lt;strong&gt;原文和问题之间的交互信息&lt;/strong&gt;，而第二 Interaction 层负责捕捉&lt;strong&gt;原文内部各词之间的交互信息&lt;/strong&gt;。具体来说，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;在第一 Interaction 层，r-net 首先使用了&lt;strong&gt;类似于 Match-LSTM 的方法&lt;/strong&gt;，即对&lt;strong&gt;原文中每个词&lt;/strong&gt;，计算其&lt;strong&gt;关于问题&lt;/strong&gt;的&lt;strong&gt;注意力分布&lt;/strong&gt;，并使用该注意力分布&lt;strong&gt;汇总问题表示&lt;/strong&gt;，将&lt;strong&gt;原文该词表示&lt;/strong&gt;和&lt;strong&gt;对应问题表示&lt;/strong&gt;输入 &lt;strong&gt;RNN&lt;/strong&gt; 编码，得到&lt;strong&gt;该词的 query-aware 表示&lt;/strong&gt;。不同的是，在原文词表示和对应问题表示输入 RNN 之前，r-net &lt;strong&gt;使用了一个额外的门来过滤不重要的信息&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;接着，在第二 Interaction 层，r-net 使用了同样的策略来将 &lt;strong&gt;query-aware 表示&lt;/strong&gt;进一步&lt;strong&gt;和自身进行匹配&lt;/strong&gt;，将&lt;strong&gt;回答答案所需的证据和问题信息进行语义上的融合&lt;/strong&gt;，得到&lt;strong&gt;最终的原文表示&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在其他方面，r-net 的 Embed 层同样使用了 &lt;strong&gt;word 和 char 两种 embedding&lt;/strong&gt; 以丰富输入特征。在 Answer 层，r-net 首先使用一个 &lt;strong&gt;attention-pooling 的问题向量&lt;/strong&gt;作为一个 RNN 的初始状态，该 RNN 的状态和&lt;strong&gt;最终的原文表示&lt;/strong&gt;共同输入一个 &lt;strong&gt;pointer networks&lt;/strong&gt; 以产生答案开始概率，接着&lt;strong&gt;基于开始概率&lt;/strong&gt;和&lt;strong&gt;原文表示&lt;/strong&gt;产生&lt;strong&gt;另一个 attention-pooling 向量&lt;/strong&gt;，该向量和 &lt;strong&gt;RNN 状态&lt;/strong&gt;共同经过一次 &lt;strong&gt;RNN 更新后得到 RNN 的新状态&lt;/strong&gt;，并&lt;strong&gt;基于新状态来预测答案结束概率&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;211-mnemonic-reader14&quot;&gt;2.11 Mnemonic Reader[14]&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/Mnemonic Reader.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;相比于之前的工作，Mnemonic Reader 同样使用了类似于 r-net 和 Ruminating Reader 的&lt;strong&gt;两层 Interaction 层&lt;/strong&gt;设计。其中第一个 Interaction 层负责&lt;strong&gt;捕捉原文和问题之间&lt;/strong&gt;的交互信息，第二 Interaction 层负责&lt;strong&gt;捕捉原文内部的长时依赖&lt;/strong&gt;信息。不同于 r-net 的是，&lt;strong&gt;r-net 使用了单向注意力+门机制&lt;/strong&gt;来编码这些交互信息，而 Mnemonic Reader &lt;strong&gt;使用了双向注意力机制来编码&lt;/strong&gt;交互信息，因此能够捕捉更加细粒度的语义信息。&lt;/p&gt;

&lt;p&gt;在 Answer 层，我们使用&lt;strong&gt;对问题敏感&lt;/strong&gt;的表示方法，具体来说，问题表示分为两种：&lt;strong&gt;显式的问题类型 embedding&lt;/strong&gt; 和&lt;strong&gt;隐式的问题向量&lt;/strong&gt;表示。&lt;/p&gt;

&lt;p&gt;进一步地，我们使用了 &lt;strong&gt;Memory Network&lt;/strong&gt;[12] 的框架来预测答案范围，将&lt;strong&gt;问题表示&lt;/strong&gt;作为一个&lt;strong&gt;可更新的记忆向量&lt;/strong&gt;，在每次预测答案概率后&lt;strong&gt;将候选答案信息更新至记忆向量中&lt;/strong&gt;。该过程&lt;strong&gt;可以持续多轮&lt;/strong&gt;，因此可以&lt;strong&gt;根据之前预测信息来不断修正当前预测，直到产生正确的答案范围。&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;3-小结&quot;&gt;3. 小结&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;大规模语料集&lt;/strong&gt;的构建是推进机器阅读理解发展的重要前提。从 15 年提出的 CNN/DM 完形填空数据集，到近期的 SQuAD 数据集，再到之后的若干新数据集，每一个新数据集都提出了当前方法无法有效解决的新问题，从而促使研究人员不断探索新的模型，促进了该领域的发展。&lt;/li&gt;
  &lt;li&gt;针对&lt;strong&gt;抽取式阅读理解任务&lt;/strong&gt;，可以看到有如下几个技术创新点:
    &lt;ul&gt;
      &lt;li&gt;建立在单向或双向注意力机制上的 Interaction 层对于模型理解原文和问题至关重要，而[10]，[13]和[14]中&lt;strong&gt;更复杂的双 Interaction 层设计&lt;/strong&gt;无疑要优于之前的单 Interaction 层设计，原因是在&lt;strong&gt;问题-原文交互层&lt;/strong&gt;之上的&lt;strong&gt;原文自交互层&lt;/strong&gt;使得更多的语义信息能在原文中流动，因此在某种程度上部分解决了长文本中存在的长时依赖问题。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;多轮推理机制&lt;/strong&gt;如[6]，[11]和[14]对于回答复杂问题具备一定帮助，尤其是针对 SQuAD 中的答案不是一个单词而可能是一个短语的情况，多轮推理机制可以不断缩小预测范围，最终确定正确答案位置。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;对问题敏感的问题表示方法&lt;/strong&gt;[9]，[14]能够更好地 model &lt;strong&gt;各类型问题&lt;/strong&gt;，并根据问题类型聚焦于原文中的特定单词，比如 when 类问题更加聚焦于原文中的时间信息，而 where 类问题更关注空间信息。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;4-参考文献&quot;&gt;4. 参考文献&lt;/h2&gt;

&lt;p&gt;[1] Pranav Rajpurkar, Jian Zhang, Konstantin Lopyrev, and Percy Liang. 2016. Squad: 100,000+ questions for machine comprehension of text. In Proceedings of EMNLP.&lt;/p&gt;

&lt;p&gt;[2] Karl Moritz Hermann, Tomas Kocisky, Edward Grefenstette, Lasse Espeholt, Will Kay, Mustafa Suleyman, , and Phil Blunsom. 2015. Teaching ma- chines to read and comprehend. In Proceedings of NIPS.&lt;/p&gt;

&lt;p&gt;[3] Felix Hill, Antoine Bordes, Sumit Chopra, and Jason Weston. 2016. The goldilocks principle: Reading childrens books with explicit memory representa- tions. In Proceedings of ICLR.&lt;/p&gt;

&lt;p&gt;[4] Shuohang Wang and Jing Jiang. 2017. Machine comprehension using match-lstm and answer pointer. In Proceedings of ICLR.&lt;/p&gt;

&lt;p&gt;[5] Minjoon Seo, Aniruddha Kembhavi, Ali Farhadi, and Hananneh Hajishirzi. 2017. Bidirectional attention flow for machine comprehension. In Proceedings of ICLR.&lt;/p&gt;

&lt;p&gt;[6] Caiming Xiong, Victor Zhong, and Richard Socher. 2017. Dynamic coattention networks for question answering. In Proceedings of ICLR.&lt;/p&gt;

&lt;p&gt;[7] Zhiguo Wang, Haitao Mi, Wael Hamza, and Radu Florian. 2016. Multi-perspective context matching for machine comprehension. arXiv preprint arXiv:1612.04211 .&lt;/p&gt;

&lt;p&gt;[8] Dirk Weissenborn, Georg Wiese, and Laura Seiffe. 2017. Fastqa: A simple and efficient neural architecture for question answering. arXiv preprint arXiv:1703.04816 .&lt;/p&gt;

&lt;p&gt;[9] Junbei Zhang, Xiaodan Zhu, Qian Chen, Lirong Dai, Si Wei, and Hui Jiang. 2017. Exploring question understanding and adaptation in neural- network-based question answering. arXiv preprint arXiv:1703.04617 .&lt;/p&gt;

&lt;p&gt;[10] Yichen Gong and Samuel R. Bowman. 2017. Ruminating reader: Reasoning with gated multi-hop attention. arXiv preprint arXiv:1704.07415 .&lt;/p&gt;

&lt;p&gt;[11] Yelong Shen, Po-Sen Huang, Jianfeng Gao, and Weizhu Chen. 2016. Reasonet: Learning to stop reading in machine comprehension. arXiv preprint arXiv:1609.05284 .&lt;/p&gt;

&lt;p&gt;[12] Sainbayar Sukhbaatar, Arthur Szlam, Jason Weston, and Rob Fergus. 2015. End-to-end memory networks. In Proceedings of NIPS.&lt;/p&gt;

&lt;p&gt;[13] Microsoft Research Asia. 2017. R-NET: MACHINE READING COMPREHENSION WITH SELF-MATCHING NETWORKS. In Proceedings of ACL.&lt;/p&gt;

&lt;p&gt;[14] Minghao Hu, Yuxing Peng, and Xipeng Qiu. 2017. Mnemonic Reader for Machine Comprehension. arXiv preprint arXiv:1705.02798 .&lt;/p&gt;

&lt;h2 id=&quot;x-qanet&quot;&gt;x. QANet&lt;/h2&gt;

&lt;p&gt;参考谷歌大脑团队和 CMU 联合推出的 QANet，发表在ICLR2018：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.toutiao.com/i6554286595137077773/?tt_from=weixin&amp;amp;utm_campaign=client_share&amp;amp;from=groupmessage&amp;amp;timestamp=1526058546&amp;amp;app=news_article&amp;amp;utm_source=weixin&amp;amp;isappinstalled=0&amp;amp;iid=30099372021&amp;amp;utm_medium=toutiao_android&amp;amp;wxshare_count=2&amp;amp;pbid=6531927882050012680&quot;&gt;问答系统冠军之路：用 CNN 做问答任务的 QANet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1804.09541&quot;&gt;QANet: Combining Local Convolution with Global Self-Attention for Reading Comprehension&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;给定一个包含 n 个单词的上下文片段 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C={c_1,c_2,...,c_n}\)&lt;/code&gt;,我们考虑包含 m 个单词的查询语句 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q={q_1,q_2,...,q_m}\)&lt;/code&gt;,模型输出为一个包含 j 个单词的&lt;strong&gt;片段 C 中的答案区间&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S={c_i,c_{i+1},...,c_{i+j}}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;QANet 包含五个主要的组成部分：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;嵌入层 （embedding layer）&lt;/li&gt;
  &lt;li&gt;嵌入编码层（embedding encoder layer）&lt;/li&gt;
  &lt;li&gt;语境-查询注意力层（context-query attention layer）&lt;/li&gt;
  &lt;li&gt;模型编码层（model encoder）&lt;/li&gt;
  &lt;li&gt;输出层（output layer）&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;x1-cnnself-attention&quot;&gt;x.1 cnn+self-attention&lt;/h3&gt;

&lt;p&gt;仅仅使用&lt;strong&gt;卷积&lt;/strong&gt;（convolution）和&lt;strong&gt;自注意力机制&lt;/strong&gt;（self-attention）构建了一个神经网络，使得模型的训练速率和推断速率大大加快，并且可以并行处理输入的单词。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;卷积&lt;/strong&gt;操作可以对&lt;strong&gt;局部相互作用&lt;/strong&gt;建模（捕获文本的局部结构），而使用&lt;strong&gt;自注意力机制&lt;/strong&gt;则可以对&lt;strong&gt;全局交互&lt;/strong&gt;进行建模（学习&lt;strong&gt;每对单词之间&lt;/strong&gt;的相互作用）。据作者们介绍，这也是领域内首次将卷积和自注意力机制相结合。由于卷积层和自注意力机制都没有消耗时间的递归操作，所以作者们不仅大胆地把模型深度增加到了问答任务中史无前例的&lt;strong&gt;超过 130 层&lt;/strong&gt;，同时还在训练、推理中都有数倍的速度提升。&lt;strong&gt;（相较于基于 RNN 的模型，训练速度提升了3-13倍，推理速度提升了 4-9 倍）&lt;/strong&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/qanet.jpeg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;左图为包含多个编码器模块的 QANet 整体架构。&lt;/p&gt;

&lt;p&gt;右图为&lt;strong&gt;基本编码器模块单元&lt;/strong&gt;，QANet 所使用的所有编码器都是按照这个模式构建的，仅仅修改模块中卷积层的数量。(其实前两步都是 attention is all you need里有的，第三步是加上的)&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;QANet 在每一层之间会使用&lt;strong&gt;LayerNorm和残差连接&lt;/strong&gt;技术&lt;/li&gt;
  &lt;li&gt;将编码器结构内&lt;strong&gt;位置编码&lt;/strong&gt;之后的&lt;strong&gt;每个子层（卷积、自注意力、前馈网络等）&lt;/strong&gt;封装在&lt;strong&gt;残差模块内&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;QANet 还&lt;strong&gt;共享&lt;/strong&gt;了&lt;strong&gt;语境、问题、输出编码器之间&lt;/strong&gt;的&lt;strong&gt;部分权重&lt;/strong&gt;，以达到知识共享。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;区别于一般的编码器-解码器结构中使用的 Attention model（输入和输出的内容不同），self attention 机制并不是输入和输出之间的 attention 机制，而是输入内部的单词或者输出内部单词之间的 attention 机制。Self-attention即K=V=Q，在 QANet 中，作者使得原文中每一对单词的相互作用都能够被刻画出来，捕获整篇文章的内部结构。&lt;/p&gt;

&lt;p&gt;使用 self-attention 有以下好处：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;在&lt;strong&gt;并行&lt;/strong&gt;方面，self-attention 和 CNN一样不依赖于前一时刻的计算，可以很好的并行，优于RNN。&lt;/li&gt;
  &lt;li&gt;在&lt;strong&gt;长距离依赖&lt;/strong&gt;上，由于 self-attention 是每个词和所有词都要计算 attention，所以&lt;strong&gt;不管他们中间有多长距离，最大的路径长度也都只是 1。可以高效捕获长距离依赖关系。&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;更多的知识，请移步：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://daiwk.github.io/posts/nlp-self-attention-models.html&quot;&gt;https://daiwk.github.io/posts/nlp-self-attention-models.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&quot;&gt;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-attention-models.html&quot;&gt;https://daiwk.github.io/posts/dl-attention-models.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;x2-利用机器翻译进行数据增强&quot;&gt;x.2 利用机器翻译进行数据增强&lt;/h3&gt;

&lt;p&gt;使用数据增强技术对原始数据集进行了扩充，用更多数据训练了模型。&lt;/p&gt;

&lt;p&gt;具体来说，他们把&lt;strong&gt;英文原文用现有的神经机器翻译器翻译成另一种语言（QANet 使用的是法语）之后再翻译回英语&lt;/strong&gt;。这个过程相当于对样本进行了改写，这样使得训练样本的数量大大增加，句式更加丰富。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>dl中的normalization</title>
   <link href="http://daiwk.github.io/posts/dl-normalization.html"/>
   <updated>2018-01-12T00:00:00+00:00</updated>
   <id>/posts/dl-normalization</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#batch-normalization&quot;&gt;batch normalization&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#layer-normalization&quot;&gt;layer normalization&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;batch-normalization&quot;&gt;batch normalization&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1502.03167.pdf&quot;&gt;Batch Normalization: Accelerating Deep Network Training by  Reducing Internal Covariate Shift&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.csdn.net/hjimce/article/details/50866313&quot;&gt;http://blog.csdn.net/hjimce/article/details/50866313&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要有四个好处：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;快速训练收敛的特性&lt;/strong&gt;。可以选择比较大的初始学习率，让你的训练速度飙涨。以前还需要慢慢调整学习率，甚至在网络训练到一半的时候，还需要想着学习率进一步调小的比例选择多少比较合适。现在我们可以采用初始很大的学习率，然后学习率的衰减速度也很大，因为这个算法收敛很快。即使你选择了较小的学习率，也比以前的收敛速度快。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;提高网络泛化能力的特性&lt;/strong&gt;。不用去理会过拟合中drop out、L2正则项参数的选择问题。移除这两项了参数，或者可以选择更小的L2正则约束参数了。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;本身就是一个归一化网络层&lt;/strong&gt;。不需要使用使用局部响应归一化层（Alexnet网络用到的方法）。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;可以把训练数据彻底打乱&lt;/strong&gt;。防止每批训练的时候，某一个样本都经常被挑选到。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;一般地，在神经网络训练开始前，都要对输入数据做一个归一化处理，原因如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;神经网络学习过程本质就是为了学习数据分布，一旦&lt;strong&gt;训练数据与测试数据的分布&lt;/strong&gt;不同，那么网络的泛化能力也大大降低&lt;/li&gt;
  &lt;li&gt;一旦&lt;strong&gt;每批训练数据的分布&lt;/strong&gt;各不相同(batch 梯度下降)，那么网络就要在每次迭代都去学习适应不同的分布，这样将会大大降低网络的训练速度。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/qq_25737169/article/details/79048516&quot;&gt;https://blog.csdn.net/qq_25737169/article/details/79048516&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;layer-normalization&quot;&gt;layer normalization&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1607.06450v1.pdf&quot;&gt;Layer Normalization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考知乎的讨论：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/59728870&quot;&gt;https://www.zhihu.com/question/59728870&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/48820040&quot;&gt;https://www.zhihu.com/question/48820040&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://skyhigh233.com/blog/2017/07/21/norm/&quot;&gt;https://skyhigh233.com/blog/2017/07/21/norm/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;直观区别：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;bn是“竖”着来的，对于某一个神经元，会拿整个batch所有样本的值来做归一化。&lt;/li&gt;
  &lt;li&gt;ln是“横”着来的，对一个样本，在不同的神经间做归一化。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;好处：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;它的&lt;strong&gt;training和inference没有区别&lt;/strong&gt;，只需要对&lt;strong&gt;当前隐藏层&lt;/strong&gt;计算mean和variance就行&lt;/li&gt;
  &lt;li&gt;不需要保存每层的moving average mean and variance&lt;/li&gt;
  &lt;li&gt;不受batch size的限制，可以通过&lt;strong&gt;online learning&lt;/strong&gt;的方式&lt;strong&gt;一条一条&lt;/strong&gt;地输入训练数据&lt;/li&gt;
  &lt;li&gt;LN可以方便的在RNN中使用&lt;/li&gt;
  &lt;li&gt;LN增加了&lt;strong&gt;gain&lt;/strong&gt;和&lt;strong&gt;bias&lt;/strong&gt;作为学习的参数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma\)&lt;/code&gt;分别是该layer的隐层维度的均值和方差&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bn-ln.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;LayerNorm是Batch Normalization的一个变体，BN针对一个minibatch的输入样本，计算均值和方差，基于计算的均值和方差来对某一层神经网络的输入X中每一个case进行归一化操作。但BN有两个明显不足：1、高度依赖于mini-batch的大小，实际使用中会对mini-Batch大小进行约束，不适合类似在线学习（mini-batch为1）情况；2、不适用于RNN网络中normalize操作：BN实际使用时需要计算并且保存某一层神经网络mini-batch的均值和方差等统计信息，对于对一个固定深度的前向神经网络（DNN，CNN）使用BN，很方便；但对于RNN来说，sequence的长度是不一致的，换句话说RNN的深度不是固定的，不同的time-step需要保存不同的statics特征，可能存在一个特殊sequence比其的sequence长很多，这样training时，计算很麻烦。但LN可以有效解决上面这两个问题。LN中同层神经元输入拥有相同的均值和方差，不同的输入样本有不同的均值和方差；而BN中则针对不同神经元输入计算均值和方差，同一个minibatch中的输入拥有相同的均值和方差。因此，LN不依赖于mini-batch的大小和输入sequence的深度，因此可以用于bath-size为1和RNN中对边长的输入sequence的normalize操作。参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIxNDgzNDg3NQ==&amp;amp;mid=2247483765&amp;amp;idx=1&amp;amp;sn=be24746f5e99058a4b9e8e209848a717&amp;amp;chksm=97a0caa1a0d743b7440c224e17fdc8579a88793940470e6d028b34040f7385f2b99d4da17478&amp;amp;scene=21#wechat_redirect&quot;&gt;深度学习加速器Layer Normalization-LN&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>基于深度学习的ctr预估模型集合（持续更新）</title>
   <link href="http://daiwk.github.io/posts/dl-dl-ctr-models.html"/>
   <updated>2018-01-12T00:00:00+00:00</updated>
   <id>/posts/dl-dl-ctr-models</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#fm&quot;&gt;FM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ffm&quot;&gt;FFM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#embedding--mlp&quot;&gt;embedding + mlp&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fnn-snn&quot;&gt;FNN, SNN&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#fnn&quot;&gt;FNN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#snn&quot;&gt;SNN&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#nfm&quot;&gt;NFM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#afm&quot;&gt;AFM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pnn&quot;&gt;PNN&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#ipnn&quot;&gt;IPNN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#opnn&quot;&gt;OPNN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pnn%e5%b0%8f%e7%bb%93&quot;&gt;PNN小结&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ccpm&quot;&gt;CCPM&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#convolution-layer&quot;&gt;Convolution Layer&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#flexible-p-max-pooling&quot;&gt;Flexible p-Max Pooling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#feature-maps&quot;&gt;feature maps&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ccpm%e5%b0%8f%e7%bb%93&quot;&gt;ccpm小结&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#wide--deep&quot;&gt;Wide &amp;amp; Deep&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deepfm&quot;&gt;DeepFM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deep--cross&quot;&gt;Deep &amp;amp; Cross&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#xdeepfm&quot;&gt;xDeepFM&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%83%8c%e6%99%af&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e9%a2%84%e5%a4%87%e7%9f%a5%e8%af%86&quot;&gt;预备知识&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#embedding&quot;&gt;embedding&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e9%9a%90%e5%bc%8f%e7%9a%84%e9%ab%98%e9%98%b6%e7%89%b9%e5%be%81%e4%ba%a4%e4%ba%92&quot;&gt;隐式的高阶特征交互&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%98%be%e5%bc%8f%e7%9a%84%e9%ab%98%e9%98%b6%e7%89%b9%e5%be%81%e4%ba%a4%e4%ba%92&quot;&gt;显式的高阶特征交互&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cin&quot;&gt;CIN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#xdeepfm-1&quot;&gt;xDeepFM&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#din&quot;&gt;DIN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#dien&quot;&gt;DIEN&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%85%b4%e8%b6%a3%e6%8f%90%e5%8f%96&quot;&gt;兴趣提取&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%85%b4%e8%b6%a3%e6%bc%94%e5%8c%96&quot;&gt;兴趣演化&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#esmm&quot;&gt;ESMM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tdm&quot;&gt;TDM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#deepgbm&quot;&gt;DeepGBM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%90%84%e7%a7%8d%e6%af%94%e8%b5%9b&quot;&gt;各种比赛&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%ba%94%e7%94%a8%e7%a4%ba%e4%be%8b&quot;&gt;应用示例&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.mamicode.com/info-detail-1990002.html&quot;&gt;深度学习在 CTR 中应用&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/32523455&quot;&gt;ctr模型汇总&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;基于lr和gbdt的可以参考&lt;a href=&quot;https://daiwk.github.io/posts/dl-traditional-ctr-models.html&quot;&gt;传统ctr预估模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://www.jiqizhixin.com/articles/2018-07-16-17&quot;&gt;从FM推演各深度CTR预估模型(附代码)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;发现一个不错的合集。。&lt;a href=&quot;https://www.jianshu.com/p/2b1246858e6f&quot;&gt;https://www.jianshu.com/p/2b1246858e6f&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;fm&quot;&gt;FM&lt;/h2&gt;

&lt;p&gt;二阶多项式模型：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\phi(x) = w_0+\sum _{i}w_ix_i+\sum_{i}\sum_{j&amp;lt;i}w_{ij}x_ix_j
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;多项式模型的问题在于二阶项的参数过多，假设特征个数为n，那么二阶项的参数数目为n(n+1)/2，参数太多，而却只有少数模式在样本中能找到，因此模型无法学出对应的权重。&lt;/p&gt;

&lt;p&gt;FM模型：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{y} = w_0+\sum _{i=1}^nw_ix_i+\sum_{i=1}^{n-1}\sum_{j=i+1}^{n}\left \langle \mathbf{v}_i,\mathbf{v}_j  \right \rangle x_ix_j
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_0\in \mathbb{R}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{w}\in \mathbb{R}^n\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{V}\in \mathbb{R}^{n\times k}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{w_{ij}}=\mathbf{v}_i\mathbf{v}_j^T=\sum _{l=1}^kv_{il}v_{jl}\)&lt;/code&gt;
所以上式中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{v}_i\)&lt;/code&gt;就表示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{V}\)&lt;/code&gt;这个矩阵的第i行（有k列），而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left \langle \mathbf{v}_i,\mathbf{v}_j  \right \rangle\)&lt;/code&gt;就表示第i行和和j行这两个向量的内积（得到一个数），而得到的正好就是权重矩阵的第i行第j列的元素&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{w}_{ij}\)&lt;/code&gt;，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{w}\)&lt;/code&gt;这个矩阵是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((n-1)\times(n-1)\)&lt;/code&gt;维的矩阵，刻画的是相邻两个x【&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{i+1}\)&lt;/code&gt;】之间的系数。因此，可以理解为，将这个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((n-1)\times(n-1)\)&lt;/code&gt;维的矩阵用一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\times k\)&lt;/code&gt;维的低秩矩阵来表示。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在计算特征组合的时候，会用如下一个小trick：&lt;/p&gt;

&lt;p&gt;因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((ax+by)^2=a^2x^2+b^2y^2+2axby\)&lt;/code&gt;，所以，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\sum ^n_{i=1}\sum ^n_{j=i+1}x_ix_j=\frac{1}{2}[(\sum ^n_{i=1}x_i)^2-\sum^n_{i=1}x_i^2]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;考虑了v，其实就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\sum xxv=\frac{1}{2}[\sum (xv)^2-\sum x^2v^2]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;tf实现：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factor_order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opt_algo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'gd'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;l2_w&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 一次、二次交叉、偏置项
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'w'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'xavier'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'v'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factor_order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'xavier'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'zero'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_random_seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparse_placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_var_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'w'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'v'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;# [(x1+x2+x3)^2 - (x1^2+x2^2+x3^2)]/2
&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# 先计算所有的交叉项，再减去平方项(自己和自己相乘)
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;X_square&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SparseTensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;indices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_int64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;xv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparse_tensor_dense_matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparse_tensor_dense_matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X_square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;xw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparse_tensor_dense_matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid_cross_entropy_with_logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; \
                        &lt;span class=&quot;n&quot;&gt;l2_w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; \
                        &lt;span class=&quot;n&quot;&gt;l2_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt_algo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;#GPU设定
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConfigProto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gpu_options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;allow_growth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# 图中所有variable初始化
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;将FM的公式稍微变形，写成内积形式，可以发现：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{y} = w_0+\left \langle w,x \right  \rangle + \left \langle xV,xV \right  \rangle
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;发现，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(xV\)&lt;/code&gt;就是将离散稀疏特征&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;降维成一个低维稠密向量，所以&lt;strong&gt;其实就是一种embedding&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;所以，其实FM就是：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;先对离散特征进行embedding&lt;/li&gt;
  &lt;li&gt;然后对embedding后的向量进行内积来做二阶特征组合&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ffm&quot;&gt;FFM&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.csie.ntu.edu.tw/~cjlin/papers/ffm.pdf&quot;&gt;Field-aware Factorization Machines for CTR Prediction&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/john_xyz/article/details/78933253#field-aware-factorization-machinesffm&quot;&gt;https://blog.csdn.net/john_xyz/article/details/78933253#field-aware-factorization-machinesffm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;FFM把&lt;strong&gt;相同性质的特征&lt;/strong&gt;归于&lt;strong&gt;同一个field&lt;/strong&gt;。每一维特征&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;，针对&lt;strong&gt;其他特征&lt;/strong&gt;的每一种field &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_j\)&lt;/code&gt;，都会学习一个隐向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V_{i,f_j}\)&lt;/code&gt;。所以隐向量不仅与特征有关，也与field相关。假设有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;个特征，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f\)&lt;/code&gt;个field，那么FFM的二次项总共有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(nf\)&lt;/code&gt;个隐向量，而FM中，每一维特征的隐向量只有一个。公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
y=w_0+\sum^n_{i=1}w_ix_i+\sum^n_{i=1}\sum ^n_{j=i+1} \left \langle V_{i,f_j}, V_{j,f_i} \right  \rangle x_i x_j
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果隐向量的长度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;，那么FFM的二次参数有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(nfk\)&lt;/code&gt;个，远多于FM的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(nk\)&lt;/code&gt;个，二次项并不能化简，所以时间复杂度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(kn^2)\)&lt;/code&gt;。由于FFM中的latent vector只需要学习特定的field，所以通常要求&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k_{FFM}&amp;lt;&amp;lt; k_{FM}\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;FFM做召回可以参考：&lt;a href=&quot;https://mp.weixin.qq.com/s/4YWId4hRun8z9ACuYeUThA&quot;&gt;推荐系统召回四模型之二：沉重的FFM模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要思想就是把item侧的emb做一下转置，然后就可以各自concat，拼出user的大向量和item的大向量，然后做个内积就是了。要加一阶项或者user内部组合特征或者item内部组合特征，就往对应的大向量后面加，然后另一个大向量对应位置写1，再去搞内积就是了。&lt;/p&gt;

&lt;p&gt;“微博在业务中的实践表明，如果采取FM召回模型，对于有些应用来说，一阶项对于最终效果有明显影响，所以在用FM/FFM做召回的时候，是需要将一阶项考虑进去的，这可能是个别一阶特征比较重要导致的。我们在Criteo数据集合的实验结果也证明：如果是FM模型，一阶项是有用的，去掉一阶项，只保留二阶项，AUC大约会掉1个绝对百分点，对于CTR来说，这个差距还是很明显的；而如果是采用DeepFM模型，则FM部分是否保留一阶项对最终结果没有什么影响，这说明DNN的隐层有效地将一阶项的作用吸收掉了。”&lt;/p&gt;

&lt;p&gt;加场景特征C的时候，先在取用户特征U的时候算出每个域的&amp;lt;U,C&amp;gt;，记为score1。然后把C拼到U的长向量里去，I的长向量后面也拼上I给C准备的emb，然后内积算出score2，score1+score2就是了。&lt;/p&gt;

&lt;p&gt;这样就可能出现向量太长的问题，所以需要提速：&lt;/p&gt;

&lt;p&gt;方法1：U的emb切分成k段，I的emb也切分成k段，U的每一段去annoy找topk的I的那段出来，然后k个子集merge，有交集的item把k段的内积加起来，就是它的score&lt;/p&gt;

&lt;p&gt;方案2：作者提出了一种奇怪的想法，当然得出的结果不是FFM，是变种的FM+FFM：比如I有3个域，U有两个域，那U有2组向量，每组向量里有3个向量，记为(U11,U12,U13), (U21,U22,U23)，同理，I有3组向量，每组向量有2个向量，记为(I11,I12), (I21,I22), (I31,I32)，然后U跨域相加，得到(U11+U21),(U12+U22),(U31+U23)，而I进行域内相加，得到(I11+I12), (I21+I22), (I31+I32)，然后U和I就可以做内积啦。。同理，也可以对I跨域相加，对U域内相加。。&lt;/p&gt;

&lt;h2 id=&quot;embedding--mlp&quot;&gt;embedding + mlp&lt;/h2&gt;

&lt;p&gt;是ctr预估的通用框架，各种field的特征进行emb，然后concat到一起，然后堆一堆mlp。&lt;/p&gt;

&lt;p&gt;缺点：只学习高阶特征组合，对于&lt;strong&gt;低阶&lt;/strong&gt;或者&lt;strong&gt;手动的特征组合&lt;/strong&gt;不够兼容，而且参数较多，学习较困难&lt;/p&gt;

&lt;h2 id=&quot;fnn-snn&quot;&gt;FNN, SNN&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1601.02376.pdf&quot;&gt;Deep Learning over Multi-field Categorical Data - A Case Study on User Response Prediction in Display Ads&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这篇文章里提出了FNN和SNN。&lt;/p&gt;

&lt;h3 id=&quot;fnn&quot;&gt;FNN&lt;/h3&gt;

&lt;p&gt;将&lt;strong&gt;FM与MLP&lt;/strong&gt;进行了结合。它有着十分显著的特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;采用&lt;strong&gt;FM预训练&lt;/strong&gt;得到的隐含层及其权重作为神经网络的&lt;strong&gt;第一层的初始值&lt;/strong&gt;，之后再不断堆叠全连接层，最终输出预测的点击率。&lt;/li&gt;
  &lt;li&gt;可以将FNN理解成一种特殊的embedding+MLP，其要求第一层嵌入后的&lt;strong&gt;各领域特征维度一致&lt;/strong&gt;，并且emb权重的初始化是FM预训练好的。&lt;/li&gt;
  &lt;li&gt;不是一个端到端的训练过程，有&lt;strong&gt;贪心预训练&lt;/strong&gt;的思路。而且如果不考虑预训练过程，模型网络结构也没有考虑低阶特征组合。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/fnn.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;tf代码：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FNN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;field_sizes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_sizes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_acts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;drop_out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;embed_l2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_l2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opt_algo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'gd'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;num_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'embed_%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'xavier'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;node_in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_size&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'w%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'xavier'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'zero'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;node_in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_random_seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparse_placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keep_prob_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;drop_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keep_prob_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;drop_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_keeps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_var_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;w0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'embed_%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;xw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sparse_tensor_dense_matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xw&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;#全连接部分
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;wi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'w%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;activate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;layer_acts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;
                    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_keeps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squeeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid_cross_entropy_with_logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_l2&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_l2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;wi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'w%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt_algo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConfigProto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gpu_options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;allow_growth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;snn&quot;&gt;SNN&lt;/h3&gt;

&lt;p&gt;和FNN的不同就是最底层的结构和预训练方式。&lt;strong&gt;其最底层是全连接的&lt;/strong&gt;，用&lt;strong&gt;RBM和DAE预训练&lt;/strong&gt;。预训练的时候，因为特征数量太多，为了减少计算量，&lt;strong&gt;每个field&lt;/strong&gt;里&lt;strong&gt;值为0的特征&lt;/strong&gt;抽取&lt;strong&gt;m个&lt;/strong&gt;，没有抽中的特征在该次权重更新就会被完全忽略。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/snn.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;FNN比SNN-DAE和SNN-RBM好，两种SNN结果总是差不多，但都比LR和FM好。&lt;/p&gt;

&lt;p&gt;文章还指出一点，钻石型网络结构比常数型结构好，常数型又比增加型，减少型结构好&lt;/p&gt;

&lt;h2 id=&quot;nfm&quot;&gt;NFM&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1708.05027.pdf&quot;&gt;Neural Factorization Machines for Sparse Predictive Analytics&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NFM的基本特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;利用&lt;strong&gt;二阶交互池化层&lt;/strong&gt;（Bi-Interaction Pooling）对FM嵌入后的向量&lt;strong&gt;两两进行元素级别的乘法&lt;/strong&gt;，形成&lt;strong&gt;同维度的向量求和&lt;/strong&gt;后作为前馈神经网络的输入。&lt;/li&gt;
  &lt;li&gt;NFM与DeepFM的区别是&lt;strong&gt;没有单独的FM的浅层网络&lt;/strong&gt;进行联合训练，而是将其整合后&lt;strong&gt;直接输出给前馈神经网络&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;当MLP的全连接层都是恒等变换且最后一层参数全为1时，NFM就退化成了FM。可见，NFM是FM的推广，它推迟了FM的实现过程，并在其中加入了更多非线性运算。&lt;/li&gt;
  &lt;li&gt;NFM与FNN非常相似。它们的主要区别是NFM在embedding之后对特征进行了两两逐元素乘法。因为逐元素相乘的向量维数不变，之后对这些向量求和的维数仍然与embedding的维数一致。因此&lt;strong&gt;输入到MLP的参数比起直接concatenate的FNN更少。&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;tf实现：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;model_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Bulid Model function f(x) for Estimator.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#------hyperparameters----
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;field_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;field_size&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feature_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;feature_size&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;embedding_size&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;l2_reg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;l2_reg&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;learning_rate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#optimizer = params[&quot;optimizer&quot;]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;deep_layers&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;','&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;dropout&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;','&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;#------bulid weights------
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;Global_Bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'bias'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Feat_Bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'linear'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;glorot_normal_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Feat_Emb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'emb'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;glorot_normal_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;#------build feaure-------
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;feat_ids&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'feat_ids'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feat_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feat_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feat_vals&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'feat_vals'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feat_vals&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feat_vals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;#------build f(x)------
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Linear-part&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;feat_wgts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Feat_Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feat_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;# None * F * 1
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;y_linear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feat_wgts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feat_vals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;BiInter-part&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Feat_Emb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feat_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;# None * F * K
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;feat_vals&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feat_vals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;field_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feat_vals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;c1&quot;&gt;# vij * xi
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;sum_square_emb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;square_sum_emb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_square_emb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square_sum_emb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# None * K
&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Deep-part&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TRAIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;train_phase&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;train_phase&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TRAIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;                      &lt;span class=&quot;c1&quot;&gt;# None * K
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fully_connected&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_outputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; \
                &lt;span class=&quot;n&quot;&gt;weights_regularizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_regularizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'mlp%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_norm_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_phase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_phase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope_bn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'bn_%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;#放在RELU之后 https://github.com/ducha-aiki/caffenet-benchmark/blob/master/batchnorm.md#bn----before-or-after-relu
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TRAIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_prob&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;                              &lt;span class=&quot;c1&quot;&gt;#Apply Dropout after all BN layers and set dropout=0.8(drop_ratio=0.2)
&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;#deep_inputs = tf.layers.dropout(inputs=deep_inputs, rate=dropout[i], training=mode == tf.estimator.ModeKeys.TRAIN)
&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;y_deep&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fully_connected&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_outputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation_fn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;identity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
            &lt;span class=&quot;n&quot;&gt;weights_regularizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_regularizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_reg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'deep_out'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;y_d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_deep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variable_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;NFM-out&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#y_bias = Global_Bias * tf.ones_like(labels, dtype=tf.float32)  # None * 1  warning;这里不能用label，否则调用predict/export函数会出错，train/evaluate正常；初步判断estimator做了优化，用不到label时不传
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;y_bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Global_Bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# None * 1
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_linear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_d&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pred&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;prob&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;export_outputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saved_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;signature_constants&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEFAULT_SERVING_SIGNATURE_DEF_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PredictOutput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Provide an estimator spec for `ModeKeys.PREDICT`
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PREDICT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EstimatorSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;export_outputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;export_outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;#------bulid loss------
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid_cross_entropy_with_logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; \
        &lt;span class=&quot;n&quot;&gt;l2_reg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Feat_Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2_reg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l2_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Feat_Emb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Provide an estimator spec for `ModeKeys.EVAL`
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;eval_metric_ops&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;auc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EVAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EstimatorSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;eval_metric_ops&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eval_metric_ops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;#------bulid optimizer------
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Adam'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdamOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beta1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beta2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.999&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epsilon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Adagrad'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdagradOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initial_accumulator_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Momentum'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MomentumOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;momentum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.95&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'ftrl'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FtrlOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;train_op&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;global_step&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_global_step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Provide an estimator spec for `ModeKeys.TRAIN` modes
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TRAIN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EstimatorSpec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;train_op&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;afm&quot;&gt;AFM&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1708.04617.pdf&quot;&gt;Attentional Factorization Machines:Learning theWeight of Feature Interactions via Attention Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NFM的主要创新点是在FM过程中添加了逐元素相乘的运算来增加模型的复杂度。但没有在此基础上添加更复杂的运算过程，比如对加权求和。&lt;/p&gt;

&lt;p&gt;AFM的特点是：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AFM与NFM都是致力于充分利用二阶特征组合的信息，对嵌入后的向量两两进行逐元素乘法，形成同维度的向量。而且&lt;strong&gt;AFM没有MLP部分&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;AFM通过在&lt;strong&gt;逐元素乘法之后&lt;/strong&gt;形成的向量进行&lt;strong&gt;加权求和&lt;/strong&gt;，而且权重是基于网络自身来产生的。其方法是引入一个注意力子网络（Attention Net）。&lt;/li&gt;
  &lt;li&gt;当&lt;strong&gt;权重都相等&lt;/strong&gt;时，AFM退化成&lt;strong&gt;无全连接层的NFM&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;“注意力子网络”的主要操作是进行矩阵乘法，其最终输出结果为softmax，以保证各分量的权重本身是一个概率分布。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;pnn&quot;&gt;PNN&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1611.00144.pdf&quot;&gt;Product-based Neural Networks for User Response Prediction&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/pnn.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;首先对输入数据进行embedding处理，得到一个low-dimensional vector层&lt;/li&gt;
  &lt;li&gt;对该层的任意两个feature进行内积或是外积处理就得到上图的蓝色节点，&lt;/li&gt;
  &lt;li&gt;是把这些Feature直接和1相乘复制到上一层的Z中，&lt;/li&gt;
  &lt;li&gt;然后把Z和P接在一起就可以作为神经网络的输入层，&lt;/li&gt;
  &lt;li&gt;在此基础上我们就可以应用神经网络去模型了。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;假设有N个field，每个field是M维的embedding。&lt;/p&gt;

&lt;h3 id=&quot;ipnn&quot;&gt;IPNN&lt;/h3&gt;

&lt;p&gt;field之间使用内积。左边的z就是一个NxM维的，而对于p来讲，p是NxN的。所以对p来讲，使用矩阵分解来简化问题：&lt;/p&gt;

&lt;p&gt;任意的 N×N 实对称矩阵都有 N 个线性无关的特征向量。并且这些特征向量都可以正交单位化而得到一组正交且模为1的向量。故实对称矩阵A可被分解成 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A=Q\Lambda Q^T\)&lt;/code&gt;。其中，Q为正交矩阵，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Lambda\)&lt;/code&gt;为实对角矩阵。&lt;/p&gt;

&lt;p&gt;由于weight matrix是一个对称方阵，所以，如果进行一阶低秩矩阵分解，那么可以分解为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W^n_p=\theta ^n(\theta ^n)^T\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^N\in R^N\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;而如果进行K阶低秩矩阵分解，就有：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
W^n_p\odot p=\sum ^N_{i=1}\sum ^N_{j=1}\left \langle \theta ^i_n,\theta ^j_n \right \rangle \left \langle f_i,f_j \right \rangle
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^i_n\in R^K\)&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;opnn&quot;&gt;OPNN&lt;/h3&gt;

&lt;p&gt;和IPNN唯一不同的是构造交叉项的方式：&lt;/p&gt;

&lt;h3 id=&quot;pnn小结&quot;&gt;PNN小结&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;利用二阶向量积层（Pair-wisely Connected Product Layer）对FM嵌入后的向量两两进行向量积，形成的结果作为之后MLP的输入。PNN采用的向量积有内积与外积两种形式。&lt;/li&gt;
  &lt;li&gt;PNN中向量与常数1进行的乘法运算其实与FNN类似，不是PNN的主要创新点。&lt;/li&gt;
  &lt;li&gt;对于内积形式的PNN，因为两个向量相乘的结果为标量，可以直接把各个标量“拼接”成一个大向量，就可以作为MLP的输入了。&lt;/li&gt;
  &lt;li&gt;当MLP的全连接层都是恒等变换且最后一层参数全为1时，内积形式的PNN就退化成了FM。&lt;/li&gt;
  &lt;li&gt;对于外积形式的PNN，因为两个向量相乘相当于列向量与行向量进行矩阵相乘，得到的结果为一个矩阵。各个矩阵向之前内积形式的操作一样直接拼接起来维数太多，论文的简化方案是直接对各个矩阵进行求和，得到的新矩阵（可以理解成之后对其拉长成向量）就直接作为MLP的输入。&lt;/li&gt;
  &lt;li&gt;观察计算图发现外积形式的PNN与&lt;strong&gt;NFM&lt;/strong&gt;很像，其实就是PNN把NFM的逐元素乘法换成了外积。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ccpm&quot;&gt;CCPM&lt;/h2&gt;

&lt;p&gt;CIKM2015的文章&lt;a href=&quot;http://nlpr-web.ia.ac.cn/english/irds/People/sw/Liu2015CCPM.pdf&quot;&gt;A Convolutional Click Prediction Model&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;为了充分利用&lt;strong&gt;历史的顺序点击&lt;/strong&gt;的信息，可以有基于RNN的预测模型，把用户所浏览的历史记录作为序列，通过用户在&lt;strong&gt;不同的时间间隔&lt;/strong&gt;内来划分用户的历史点击序列。然而在&lt;strong&gt;真实的场景&lt;/strong&gt;下，用户对商品的&lt;strong&gt;兴趣会随时间而改变&lt;/strong&gt;，RNN模型在此刻的场景下就受到限制。本文用cnn来解决。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;在单条广告展示中（single ad impression），包括许多元素：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;element = (user, query, ad, impression time, site category, device type, etc)&lt;/code&gt;。用户是否点击一个广告与用户的历史ad impression有关。&lt;/li&gt;
  &lt;li&gt;一系列的ad impression组成sequential ad impression。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;基于以上两种情况来预测点击概率。&lt;/p&gt;

&lt;p&gt;ccpm包括convolutional layers和flexible p-max pooling layers两种layer：&lt;/p&gt;

&lt;h3 id=&quot;convolution-layer&quot;&gt;Convolution Layer&lt;/h3&gt;

&lt;p&gt;每个样本有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;个特征，对每个特征使用embeding得到定长为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;的向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_i\in R^d\)&lt;/code&gt;。构成矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\in R^{d\times n}\)&lt;/code&gt;(一列是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;维的向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_i\)&lt;/code&gt;)：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{bmatrix}
\vdots &amp;amp; \vdots  &amp;amp;\vdots \\
e_1 &amp;amp; ... &amp;amp; e_n \\
\vdots &amp;amp; \vdots &amp;amp; \vdots
\end{bmatrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后就可以用cnn了：&lt;/p&gt;

&lt;p&gt;搞一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\in R^{d\times \omega}\)&lt;/code&gt;的权重矩阵进行卷积，卷积完得到的矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\in R^{d\times (n+\omega -1)}\)&lt;/code&gt;。给定各矩阵的第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;行：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i\in R^{\omega}\)&lt;/code&gt;，和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_i\in R^n\)&lt;/code&gt;，还有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_i\in R^{(n+\omega -1)}\)&lt;/code&gt;。那么：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
r_i=w_i^Ts_{i,j-\omega +1:j}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j=1,...,n+\omega -1\)&lt;/code&gt;。将out-of-range的值&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{i,k}\)&lt;/code&gt;，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k&amp;lt;1\ or\ k&amp;gt;n\)&lt;/code&gt;全部置为0(即不要padding)。&lt;/p&gt;

&lt;p&gt;解释一下下：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\in R^{d\times \omega}\)&lt;/code&gt;，所以，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w^T\in R^{\omega \times d}\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i^T\)&lt;/code&gt;就是这个矩阵的第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;行这个长度为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;的向量。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_{i,j-\omega +1:j}\)&lt;/code&gt;指的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;中的大小为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\times \omega\)&lt;/code&gt;的小矩阵的第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;行（因为卷积是&lt;strong&gt;element-wise的乘积&lt;/strong&gt;再相加，也就是两个相同维度的向量做内积，所以这里也是&lt;strong&gt;『行』！&lt;/strong&gt;）,这行有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\omega\)&lt;/code&gt;个元素（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j-(j-\omega+1)+1=\omega\)&lt;/code&gt;），而start的范围是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1-w+1,...,n\)&lt;/code&gt;，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1-\omega+1\le j-\omega -1 \le n\)&lt;/code&gt;，所以，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1\le j \le n+\omega -1\)&lt;/code&gt;：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/ccpm-analysis-cnn.JPG&quot; style=&quot;max-height: 100px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;flexible-p-max-pooling&quot;&gt;Flexible p-Max Pooling&lt;/h3&gt;

&lt;p&gt;由于&lt;strong&gt;输入的长度是可变&lt;/strong&gt;的，为了降低这种影响，对应的池化层的参数应该也是灵活可变的。给定一个vector &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_i\in R^n\)&lt;/code&gt;，所谓的p-max pooling就是取出一个sub-vector &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s^p_i\in R^p\)&lt;/code&gt;，取出原vector里最大的p个值。因为输入的instance是变长的，所以卷积层输出的长度也会随着变化，所以pooling层需要足够灵活地平滑地取出这p个数。因此，定义p为一个与输入数据长度及网络深度有关的参数：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p_i=\left\{\begin{matrix}
(1-(i/l)^{l-i})n, &amp;amp; i = 1,...,l-1 \\
3, &amp;amp; i=l
\end{matrix}\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l\)&lt;/code&gt;代表卷积层的层数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;表示输入的长度（特征数），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_i\)&lt;/code&gt;表示第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;个池化层的参数。例如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n=18\)&lt;/code&gt;，有3个卷积层，那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_1=16,p_2=6,p_3=3\)&lt;/code&gt;。好处：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;最后一个pooling层输出固定是3，不论输入长度怎么变化，都是固定的&lt;/li&gt;
  &lt;li&gt;这是一个power-exponential函数，与线性函数相比，一开始变化很慢，避免了一开始损失太多重要特征&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;我们看一下图像就一目了然了，假设有5层，我们看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;前面的系数，也就是把层数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;当成变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;。可以看到，一开始系数很接近1，所以变化很慢，后面就衰减得比较快，在5的时候衰减到0，所以我们要手动把最后一层设成一个固定的数：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/ccpm-p-max-pooling.png&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;feature-maps&quot;&gt;feature maps&lt;/h3&gt;

&lt;p&gt;pooling完了后，接的是tanh。本文里把经过了卷积、pooling和tanh的结果叫1阶feature map。定义第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;阶feature map为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(F^i\)&lt;/code&gt;。对于中间的某一层，里面其实有很多个feature map，完全可以并行计算。例如，定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(F^i_j\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;阶feature maps里的第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j\)&lt;/code&gt;个feature map，是通过如下方式计算的：将distinct的权重矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w^i_{j,k}\)&lt;/code&gt;和低阶&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i-1\)&lt;/code&gt;的每个feature map &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(F^{i-1}_k\)&lt;/code&gt;的卷积结果加起来：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
F^i_j=\sum ^{m_i}_{k=1}w^i_{j,k}* F^{i-1}_j
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m_i\)&lt;/code&gt;是第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;阶的feature map数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(*\)&lt;/code&gt;是卷积。类似地，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(F^i_j\)&lt;/code&gt;后面可以接pooling。最后接fc再接softmax得到最终输出。&lt;/p&gt;

&lt;h3 id=&quot;ccpm小结&quot;&gt;ccpm小结&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/ccpm.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;如上图，embed的维数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d=4\)&lt;/code&gt;，有2个卷积层，每个卷积层分别生成了2个feature map。第一个卷积层的filter的宽度即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\omega _1=k_1=3\)&lt;/code&gt;，也就是图中左边的蓝色部分，长度为3，第二个卷积层的filter的宽度即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_2=k_2=2\)&lt;/code&gt;，也就是图中中间的蓝色部分长度为2。这里把最后一个pooling层的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_2\)&lt;/code&gt;设成固定的2。&lt;/p&gt;

&lt;h2 id=&quot;wide--deep&quot;&gt;Wide &amp;amp; Deep&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1606.07792.pdf&quot;&gt;Wide &amp;amp; deep learning for recommender systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;LR 对于 DNN 模型的优势是对大规模稀疏特征的容纳能力，包括内存和计算量等方面，工业界都有非常成熟的优化方法； 而 DNN 模型具有自己学习新特征的能力，一定程度上能够提升特征使用的效率， 这使得 DNN 模型在同样规模特征的情况下，更有可能达到更好的学习效果。&lt;/p&gt;

&lt;p&gt;模型结构如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/wide-and-deep-model.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;模型左边的 Wide 部分，可以容纳大规模系数特征，并且对一些特定的信息（比如 ID）有一定的记忆能力； 而模型右边的 Deep 部分，能够学习特征间的隐含关系，在相同数量的特征下有更好的学习和推导能力。&lt;/p&gt;

&lt;p&gt;用于apps推荐：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/wide-and-deep-recommend.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;wide侧：用户安装的app和浏览过的app的cross-product&lt;/li&gt;
  &lt;li&gt;deep侧：每个categorical产出一个32维的emb，然后concate到一起，再和连续值特征concate到一起得到一个约1200维的vec，再接3层fc和relu(1024-&amp;gt;512-&amp;gt;256)&lt;/li&gt;
  &lt;li&gt;wide和deep的输出加起来（如果是2分类问题，wide和deep的输出就都是一维，如果是n分类问题，那就是n维），加完的结果再丢给sigmoid或者softmax，去和label算交叉熵&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;用于ctr预估&lt;a href=&quot;https://github.com/PaddlePaddle/models/tree/develop/ctr&quot;&gt;https://github.com/PaddlePaddle/models/tree/develop/ctr&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;特征的生成：&lt;a href=&quot;https://github.com/PaddlePaddle/models/blob/develop/ctr/dataset.md&quot;&gt;https://github.com/PaddlePaddle/models/blob/develop/ctr/dataset.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;tf代码：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Model directory = %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 对checkpoint去做设定
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;runconfig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RunConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;save_checkpoints_secs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;save_checkpoints_steps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 宽模型
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'WIDE'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LinearClassifier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;model_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;feature_columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wide_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 深度模型
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'DEEP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DNNClassifier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;model_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;feature_columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;hidden_units&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 宽度深度模型
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'WIDE_AND_DEEP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DNNLinearCombinedClassifier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;model_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;linear_feature_columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wide_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;dnn_feature_columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;dnn_hidden_units&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runconfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'estimator built'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意：wide deep在wide侧和deep侧用的是两个优化方法：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/estimator/DNNLinearCombinedClassifier&quot;&gt;https://www.tensorflow.org/api_docs/python/tf/estimator/DNNLinearCombinedClassifier&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;linear_optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Ftrl'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;dnn_optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Adagrad'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以通过如下方式对不同参数指定不同的优化方法~&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/34945554/how-to-set-layer-wise-learning-rate-in-tensorflow&quot;&gt;https://stackoverflow.com/questions/34945554/how-to-set-layer-wise-learning-rate-in-tensorflow&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;deepfm&quot;&gt;DeepFM&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.ijcai.org/proceedings/2017/0239.pdf&quot;&gt;DeepFM: A Factorization-Machine based Neural Network for CTR Prediction&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DeepFM和之前模型相比优势在于两点:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;相对于Wide&amp;amp;Deep不再需要手工构建wide部分&lt;/li&gt;
  &lt;li&gt;另一个相对于FNN，FNN把FM的隐向量参数直接作为网络参数学习。而DeepFM将embedding层结果输入给FM和MLP，两者输出叠加，达到捕捉了低阶和高阶特征交叉的目的。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/deepfm.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;deep--cross&quot;&gt;Deep &amp;amp; Cross&lt;/h2&gt;

&lt;p&gt;论文地址：&lt;a href=&quot;https://arxiv.org/abs/1708.05123&quot;&gt;deep &amp;amp; cross network for ad click predictions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://daiwk.github.io/posts/dl-deep-cross-network.html&quot;&gt;https://daiwk.github.io/posts/dl-deep-cross-network.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DCN的特点如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Deep部分就是普通的MLP网络，主要是全连接。&lt;/li&gt;
  &lt;li&gt;与DeepFM类似，DCN是由embedding+MLP部分与cross部分进行联合训练的。Cross部分是对FM部分的推广。&lt;/li&gt;
  &lt;li&gt;可以证明，cross网络是&lt;strong&gt;FM&lt;/strong&gt;的过程在&lt;strong&gt;高阶特征组合&lt;/strong&gt;的&lt;strong&gt;推广&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;只有两层，且&lt;strong&gt;第一层&lt;/strong&gt;与&lt;strong&gt;最后一层权重参数相&lt;/strong&gt;等时的Cross网络与&lt;strong&gt;简化版FM等价&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;简化版的FM指的是，将拼接好的稠密向量作为输入向量，且不做领域方面的区分（但产生这些稠密向量的过程是考虑领域信息的，相对全特征维度的全连接层减少了大量参数，可以视作稀疏链接思想的体现）。而且之后进行embedding权重矩阵W只有一列——是退化成列向量的情形&lt;/li&gt;
  &lt;li&gt;与MLP网络相比，Cross部分在增加高阶特征组合的同时减少了参数的个数，并省去了非线性激活函数。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;xdeepfm&quot;&gt;xDeepFM&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzAwMTA3MzM4Nw==&amp;amp;mid=2649444578&amp;amp;idx=1&amp;amp;sn=13330325d99eabfb1266bcf59ea21dd3&amp;amp;chksm=82c0b966b5b73070784dd6e4e64cfaf8dbb8e3d700a70e170521e5e36b40e53ac09194968d48&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0822TT1RCK4m3zivGMktMqTt&amp;amp;pass_ticket=Iv7tDFrzxp8iL9atPI0PT3qolxBV3HKdG%2FbQlgIvIJXhk29gJ1G3SogZR0Se77o2#rd&quot;&gt;KDD 2018 | 推荐系统特征构建新进展：极深因子分解机模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;还可以参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/83784018&quot;&gt;XDeepFM—-左边跟我撸个CIN，右边画一个DNN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1803.05170&quot;&gt;xDeepFM: Combining Explicit and Implicit Feature Interactions for Recommender Systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/Leavingseason/xDeepFM&quot;&gt;https://github.com/Leavingseason/xDeepFM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;传统的推荐系统中，&lt;strong&gt;高阶交叉特征&lt;/strong&gt;通常是由工程师手工提取的，这种做法主要有三种缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;重要的特征都是&lt;strong&gt;与应用场景息息相关&lt;/strong&gt;的，针对每一种应用场景，工程师们都需要首先花费大量时间和精力深入了解数据的规律之后才能设计、提取出高效的高阶交叉特征，因此人力成本高昂&lt;/li&gt;
  &lt;li&gt;原始数据中往往包含&lt;strong&gt;大量稀疏的特征&lt;/strong&gt;，例如用户和物品的ID，交叉特征的维度空间是原始特征维度的乘积，因此很容易带来维度灾难的问题&lt;/li&gt;
  &lt;li&gt;人工提取的交叉特征无法泛化到&lt;strong&gt;未曾在训练样本中出现过的模式&lt;/strong&gt;中。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目前大部分相关的研究工作是&lt;strong&gt;基于因子分解机&lt;/strong&gt;的框架，利用多层全连接神经网络去自动学习特征间的高阶交互关系，例如FNN、PNN和DeepFM等。其缺点是模型学习出的是&lt;strong&gt;隐式的交互特征&lt;/strong&gt;，其形式是未知的、不可控的；同时它们的特征交互是发生在&lt;strong&gt;元素级bit-wise&lt;/strong&gt;而不是&lt;strong&gt;特征向量之间vector-wise&lt;/strong&gt;，这一点违背了因子分解机的初衷。&lt;/p&gt;

&lt;p&gt;同时以显式和隐式的方式自动学习高阶的特征交互，使特征交互发生在向量级，还兼具记忆与泛化的学习能力。&lt;/p&gt;

&lt;h3 id=&quot;背景&quot;&gt;背景&lt;/h3&gt;

&lt;p&gt;FM将每个feature映射成一个D维的隐向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v_i=[v_{i1},v_{i2},...,v_{iD}]\)&lt;/code&gt;，然后将pairwise的特征交叉建模为隐向量的内积&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f^{(2)}(i,j)=\left \langle v_i,v_j \right \rangle x_i x_j\)&lt;/code&gt;，本文中&lt;strong&gt;将隐向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v_i\)&lt;/code&gt;的一个元素（例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v_{i1}\)&lt;/code&gt;）称作『bit』。&lt;/strong&gt;。传统FM能扩展到任意高阶的特征交互，但同时对有用和无用的组合都建模了。而这些无用的组合可能会引入噪音并降低精度。&lt;/p&gt;

&lt;p&gt;FNN（ Factorisation-machine supported Neural Network）在使用DNN之前，为field embedding使用pre-trained的FM。&lt;/p&gt;

&lt;p&gt;PNN为embedding layer和DNN layer引入了product layer，从而不需要对pretrained FM的依赖。&lt;/p&gt;

&lt;p&gt;FNN和PNN的缺点是，&lt;strong&gt;他们更多地关注高阶特征组合，而忽视了低阶的特征组合。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;wide&amp;amp;deep和deepFM使用混合架构，在网络中有shallow part和deep part，保证memorization和generalization，从而克服了这个问题。&lt;/p&gt;

&lt;p&gt;而并没有理论上的结论来证明，DNN能够支持的最高阶的特征交互到底有多少。而且，DNN是bit-wise的交互，与FM的vector-wise交互是不同的。&lt;/p&gt;

&lt;p&gt;本文与DCN类似，但把cross network替换成了CIN。&lt;/p&gt;

&lt;h3 id=&quot;预备知识&quot;&gt;预备知识&lt;/h3&gt;

&lt;h4 id=&quot;embedding&quot;&gt;embedding&lt;/h4&gt;

&lt;p&gt;假设有m个field，每个field都从原始特征映射成了一个D维的embedding向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_i\in R^D\)&lt;/code&gt;，那么embedding就是一个concat到一起的vector：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
e=[e_1,e_2,...,e_m]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如下图，就是D=4的embedding layer：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/field-embedding.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;隐式的高阶特征交互&quot;&gt;隐式的高阶特征交互&lt;/h4&gt;

&lt;p&gt;FNN，Deep crossing，wide&amp;amp;deep的deep部分都是直接用DNN来隐式地进行高阶特征交互。这里的交互是bit-wise的，也就是说，&lt;strong&gt;即使是同一个field里的feature，也会互相影响&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;而PNN和DeepFM做的改进就是，加入了vector-wise的交互，区别如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;PNN将product layer的输出直接做为DNN的输入&lt;/li&gt;
  &lt;li&gt;DeepFM将FM layer直接连到了output unit&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/implicit-high-order-interaction.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;显式的高阶特征交互&quot;&gt;显式的高阶特征交互&lt;/h4&gt;

&lt;p&gt;cross network使用如下公式进行高阶特征组合：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x_k=x_0x^T_{k-1}w_k+b_k+x_{k-1}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_k,x_k,b_k\in R^{m\times D}\)&lt;/code&gt;，假设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d=m\times D\)&lt;/code&gt;。可以通过证明发现，cross network其实是一种特殊的高阶特征交互，每一个hidden layer其实是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;乘了一个scalar。。&lt;/p&gt;

&lt;p&gt;证明：&lt;/p&gt;

&lt;p&gt;k=1时，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
x_1 &amp;amp;= x_0(x_0)^Tw_1+x_0 \\ 
 &amp;amp;=x_0(x_0^Tw_1+1) \\ 
 &amp;amp;=\alpha ^1x_0
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;scalar&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha ^1\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;的一个线性变换(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;是dx1，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0^T\)&lt;/code&gt;是1xd，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_1\)&lt;/code&gt;是dx1，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0^Tw_1\)&lt;/code&gt;是一个标量。。然后假设k=i成立，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i=\alpha ^i x_0\)&lt;/code&gt;。对于k=i+1，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
x_{i+1} &amp;amp;= x_0(x_i)^Tw_{i+1}+x_i \\ 
 &amp;amp;=x_0((\alpha ^ix_0)^Tw_1)+ \alpha ^ix_0 \\ 
 &amp;amp;=\alpha ^{i+1}x_0
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha ^{i+1}=\alpha ^i(x^T_0w_{i+1}+1)\)&lt;/code&gt;同样是一个scalar。。&lt;/p&gt;

&lt;p&gt;当然了，这并不意味着&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_k\)&lt;/code&gt;就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;的一个线性变换。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha ^{i+1}\)&lt;/code&gt;对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;是敏感的。&lt;/p&gt;

&lt;p&gt;因此，cross network有两个缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每一层变成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;与一个标量相乘&lt;/li&gt;
  &lt;li&gt;高阶的特征交互变成了bit-wise，而不是FM所期待的vector-wise&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/cross-network.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;cin&quot;&gt;CIN&lt;/h3&gt;

&lt;p&gt;假设总共有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m\)&lt;/code&gt;个field，每个field的embedding是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;维向量。&lt;/p&gt;

&lt;p&gt;压缩交互网络（Compressed Interaction Network， 简称CIN）隐向量是一个单元对象，因此我们将输入的原特征和神经网络中的隐层都分别组织成一个矩阵，记为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X^0\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X^k\)&lt;/code&gt;，CIN中每一层的神经元都是根据&lt;strong&gt;前一层的隐层&lt;/strong&gt;以及&lt;strong&gt;原特征向量&lt;/strong&gt;推算而来，其计算公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
X^k_{h,*}=\sum ^{H_{k-1}}_{i=1}\sum ^{m}_{j=1}W^{k,h}_{ij}(X^{k-1}_{i,*}\circ X^{0}_{j,*})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，第k层隐层含有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H_k\)&lt;/code&gt;条神经元向量。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\circ \)&lt;/code&gt;是Hadamard product，即element-wise product，即，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left \langle a_1,a_2,a_3\right \rangle\circ \left \langle b_1,b_2,b_3\right \rangle=\left \langle a_1b_1,a_2b_2,a_3b_3 \right \rangle\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;隐层的计算可以分成两个步骤：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;根据前一层隐层的状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X^k\)&lt;/code&gt;和原特征矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X^0\)&lt;/code&gt;，计算出一个中间结果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Z^{k+1}\)&lt;/code&gt;，它是一个三维的张量。注意图中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\bigotimes \)&lt;/code&gt;是outer product，其实就是矩阵乘法咯，也就是一个mx1和一个nx1的向量的外积是一个mxn的矩阵：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
u\bigotimes v=uv^T=\begin{bmatrix}
u_1\\ 
u_2\\ 
u_3\\
u_4
\end{bmatrix}\begin{bmatrix}
v_1 &amp;amp; v_2  &amp;amp; v_3  
\end{bmatrix}=\begin{bmatrix}
u_1v_1 &amp;amp;  u_1v_2&amp;amp; u_1v_3 \\ 
u_2v_1 &amp;amp; u_2v_2 &amp;amp; u_2v_3\\ 
u_3v_1 &amp;amp; u_3v_2 &amp;amp; u_3v_3\\ 
u_4v_1 &amp;amp; u_4v_2 &amp;amp; u_4v_3
\end{bmatrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/cin-1.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;而图中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;维，其实就是左边的一行和右边的一行对应相乘，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;接下来，如下图所示：&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/cin-2.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;也就是说，这个时候把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Z^{k+1}\)&lt;/code&gt;看成一个channel数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;的image，而把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W^{k,h}\)&lt;/code&gt;看成一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H^k*m\)&lt;/code&gt;的卷积核（filter），这个卷积核大小和image一样，沿着embedding dimension(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;)进行slide，一个卷积核处理后就映射成一个1x1xD的向量。使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H^{k+1}\)&lt;/code&gt;个的卷积核，就生成一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H^{k+1}*D\)&lt;/code&gt;的矩阵。&lt;/p&gt;

&lt;p&gt;可以复习一下卷积的那些公式：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/convolution-formulas.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;大致逻辑&lt;a href=&quot;https://github.com/Leavingseason/xDeepFM/blob/master/exdeepfm/src/CIN.py#L295&quot;&gt;https://github.com/Leavingseason/xDeepFM/blob/master/exdeepfm/src/CIN.py#L295&lt;/a&gt;：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;                &lt;span class=&quot;n&quot;&gt;split_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_nn_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hparams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;dot_result_m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split_tensor0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;split_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transpose_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;dot_result_o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot_result_m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hparams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;field_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;dot_result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot_result_o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

                &lt;span class=&quot;n&quot;&gt;filters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;f_&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                                         &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;field_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                                         &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;# dot_result = tf.transpose(dot_result, perm=[0, 2, 1])
&lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;curr_out&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv1d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stride&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'VALID'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;CIN的总体框架如下图：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/cin-3.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;最终学习出的特征交互的阶数是由网络的层数决定的，每一层隐层都通过一个pooling操作连接到输出层，从而保证了输出单元可以见到不同阶数的特征交互模式。&lt;/p&gt;

&lt;p&gt;CIN的结构与RNN是很类似的，即每一层的状态是由前一层隐层的值与一个额外的输入数据计算所得。不同的是，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;CIN中不同层的参数是不一样的，而在RNN中是相同的；&lt;/li&gt;
  &lt;li&gt;RNN中每次额外的输入数据是不一样的，而CIN中额外的输入数据是固定的，始终是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X^0\)&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;xdeepfm-1&quot;&gt;xDeepFM&lt;/h3&gt;

&lt;p&gt;CIN+DNN+linear&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/xdeepfm.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;集成的CIN和DNN两个模块能够帮助模型同时以显式和隐式的方式学习高阶的特征交互，而集成的线性模块和深度神经模块也让模型兼具记忆与泛化的学习能力。值得一提的是，为了提高模型的通用性，&lt;strong&gt;xDeepFM中不同的模块共享相同的输入数据&lt;/strong&gt;。而在具体的应用场景下，不同的模块&lt;strong&gt;也可以接入各自不同的输入数据&lt;/strong&gt;，例如，线性模块中依旧可以接入很多根据先验知识提取的交叉特征来提高记忆能力，而在CIN或者DNN中，为了减少模型的计算复杂度，可以只导入一部分稀疏的特征子集。&lt;/p&gt;

&lt;p&gt;一些基本知识可以参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU3OTY2MjQ2NQ==&amp;amp;mid=2247484047&amp;amp;idx=1&amp;amp;sn=93027663ed80d795d705ec06e196e5d5&amp;amp;chksm=fd63e561ca146c7798edbefa7f6b3ed8cc47083e7ed66db9b519cf11e5a11a3ecb2e822fba90&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=8wChBZeeRNV5mWLFKMXfVyWjwTb94XookbbSJiYpmEClqUrpybiGPpfilXkL5UQN#rd&quot;&gt;一镜到底：FM们的原理及在贝壳搜索的实践&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;din&quot;&gt;DIN&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1706.06978&quot;&gt;Deep Interest Network for Click-Through Rate Prediction&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;以上神经网络对同领域离散特征的处理基本是将其&lt;strong&gt;嵌入后直接求和&lt;/strong&gt;，这在一般情况下没太大问题。但其实可以做得更加精细。比如对于历史统计类特征。&lt;/p&gt;

&lt;p&gt;以用户历史浏览的商户id为例，假设用户历史浏览了&lt;strong&gt;10个商户&lt;/strong&gt;，这些商户id的常规处理方法是&lt;strong&gt;作为同一个领域的特征&lt;/strong&gt;嵌入后&lt;strong&gt;直接求和&lt;/strong&gt;得到一个嵌入向量。但这10个商户&lt;strong&gt;只有一两个商户&lt;/strong&gt;与&lt;strong&gt;当前被预测的&lt;/strong&gt;广告所在的&lt;strong&gt;商户相似&lt;/strong&gt;，其他商户关系不大。&lt;strong&gt;增加这两个商户&lt;/strong&gt;在求和过程中的&lt;strong&gt;权重&lt;/strong&gt;，应该能够更好地提高模型的表现力。而增加求和权重的思路就是典型的注意力机制思路。DIN主要关注用户在&lt;strong&gt;同一领域&lt;/strong&gt;的&lt;strong&gt;历史行为特征&lt;/strong&gt;，如浏览了多个商家、多个商品等。DIN可以对这些特征分配不同的权重进行求和。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;用户的每个领域的历史特征权重则由该历史特征及其对应备选广告特征通过一个子网络得到。即用户&lt;strong&gt;历史浏览的商户特征&lt;/strong&gt;与&lt;strong&gt;当前浏览商户特征&lt;/strong&gt;对应，&lt;strong&gt;历史浏览的商品特征&lt;/strong&gt;与&lt;strong&gt;当前浏览商品特征&lt;/strong&gt;对应。&lt;/li&gt;
  &lt;li&gt;权重子网络主要包括特征之间的元素级别的乘法、加法和全连接等操作。&lt;/li&gt;
  &lt;li&gt;AFM也引入了注意力机制。但是AFM是将注意力机制与FM同领域特征&lt;strong&gt;求和之后&lt;/strong&gt;进行结合，DIN直接是将注意力机制与同领域特征&lt;strong&gt;求和之前&lt;/strong&gt;进行结合。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/din.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;dien&quot;&gt;DIEN&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/alibaba/x-deeplearning/wiki/%E7%94%A8%E6%88%B7%E5%85%B4%E8%B6%A3%E6%BC%94%E5%8C%96%E6%A8%A1%E5%9E%8B(DIEN)&quot;&gt;https://github.com/alibaba/x-deeplearning/wiki/%E7%94%A8%E6%88%B7%E5%85%B4%E8%B6%A3%E6%BC%94%E5%8C%96%E6%A8%A1%E5%9E%8B(DIEN)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这篇工作中我们提出用户的兴趣是一个抽象的概念，用户的&lt;strong&gt;历史行为&lt;/strong&gt;只是&lt;strong&gt;抽象的兴趣&lt;/strong&gt;的&lt;strong&gt;一个具体的体现&lt;/strong&gt;。 在DIEN中我们提出了&lt;strong&gt;兴趣抽取&lt;/strong&gt;和&lt;strong&gt;兴趣演化&lt;/strong&gt;两个模块共同组成的CTR预估模型。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/dien-arch.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;兴趣提取&quot;&gt;兴趣提取&lt;/h3&gt;

&lt;p&gt;传统的算法&lt;strong&gt;直接&lt;/strong&gt;将&lt;strong&gt;用户的历史行为&lt;/strong&gt;当做用户的兴趣。同时整个建模过程中的&lt;strong&gt;监督&lt;/strong&gt;信息&lt;strong&gt;全部集中&lt;/strong&gt;于广告&lt;strong&gt;点击样本&lt;/strong&gt;上。而单纯的广告点击样本只能体现用户在决策&lt;strong&gt;是否点击广告时的兴趣&lt;/strong&gt;，&lt;strong&gt;很难&lt;/strong&gt;建模好用户&lt;strong&gt;历史每个行为时刻&lt;/strong&gt;的兴趣。&lt;/p&gt;

&lt;p&gt;本文提出了&lt;strong&gt;auxiliary loss&lt;/strong&gt;用于兴趣提取模块，约束模型在对用户&lt;strong&gt;每一个历史行为时刻&lt;/strong&gt;的隐层表达能够&lt;strong&gt;推测出后续的行为&lt;/strong&gt;，我们希望这样的隐层表达能更好的体现用户在每一个行为时刻的兴趣。&lt;/p&gt;

&lt;p&gt;兴趣提取层部分我们主要采用GRU结构来对用户行为序列进行建模，获取得到用户在不同时刻的兴趣表达。同时我们在&lt;strong&gt;每个时间点&lt;/strong&gt;约束&lt;strong&gt;当前兴趣表达&lt;/strong&gt;可以&lt;strong&gt;预测&lt;/strong&gt;下一个时刻的&lt;strong&gt;点击&lt;/strong&gt;以及用户下时刻&lt;strong&gt;采样的不点击&lt;/strong&gt;行为。我们将这样的约束方式作为模型的辅助loss的方式引入学习。通过加入辅助loss的方式不仅能够&lt;strong&gt;引入用户的反馈信息&lt;/strong&gt;并且还能够帮助长序列的学习，降低梯度回传难度，同时还能够提供更多的语义信息帮助embedding部分的学习。&lt;/p&gt;

&lt;p&gt;看着是不是有点晕，不急，我们来看上面那个图。。其实就是一个gru，然后比如说现在是时间步&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;，那么这个时刻的输出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h(t)\)&lt;/code&gt;：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一方面需要和&lt;strong&gt;下一个时间步&lt;/strong&gt;的&lt;strong&gt;输入&lt;/strong&gt;的&lt;strong&gt;用户点击历史里&lt;/strong&gt;的&lt;strong&gt;点击样本&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e(t+1)\)&lt;/code&gt;去算一个&lt;strong&gt;点击的loss&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;另一方面，需要和&lt;strong&gt;下一个时间步&lt;/strong&gt;的&lt;strong&gt;输入&lt;/strong&gt;的&lt;strong&gt;负采样样本&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e'(t+1)\)&lt;/code&gt;去算另一个&lt;strong&gt;不点击的loss&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;然后，&lt;strong&gt;每一个时间步&lt;/strong&gt;的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h(t)\)&lt;/code&gt;还要再和&lt;strong&gt;所有的目标广告&lt;/strong&gt;去算&lt;strong&gt;attention&lt;/strong&gt;，作为下面的AUGRU的输入。&lt;/p&gt;

&lt;h3 id=&quot;兴趣演化&quot;&gt;兴趣演化&lt;/h3&gt;

&lt;p&gt;传统的RNN类似的方法只能建模&lt;strong&gt;一个单一的序列&lt;/strong&gt;，然而在电商场景 用户&lt;strong&gt;不同的兴趣&lt;/strong&gt;其实有&lt;strong&gt;不同的演化过程&lt;/strong&gt;。本文中提出了AUGRU（Activation Unit GRU），让GRU的&lt;strong&gt;update门&lt;/strong&gt;和&lt;strong&gt;预估的商品相关&lt;/strong&gt;。在建模用户的兴趣演化过程中，AUGRU会&lt;strong&gt;根据不同的预估目标商品&lt;/strong&gt;构建&lt;strong&gt;不同的兴趣演化路径&lt;/strong&gt;，推断出用户和此商品相关的兴趣。&lt;/p&gt;

&lt;p&gt;用户的兴趣是多种多样的，其同时存在多个兴趣轨迹，我们在预测&lt;strong&gt;当前AD&lt;/strong&gt;时，只需要关心和&lt;strong&gt;这个目标AD相关的兴趣的演化状态&lt;/strong&gt;。在DIN算法里我们采用的是attention的方式得到用户和当前ad相关的兴趣状态，但是没有考虑到用户兴趣间的演化关系。所以我们在兴趣演化层部分首先将和&lt;strong&gt;当前ad相关的子兴趣&lt;/strong&gt;提取出来，然后把这些&lt;strong&gt;子兴趣进行序列建模&lt;/strong&gt;，从而能够获取得到和当前ad相关的兴趣演化信息。 在这里我们将GRU结构进行了改进，将&lt;strong&gt;ad和兴趣的相关信息&lt;/strong&gt;引入了门更新，实现了对不同的目标AD，用户都有一条独有的兴趣演化轨迹。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
r_t=\sigma(W^ri_t+U^rh_{t-1}+b^r) \\ 
u_t=\sigma(W^ui_t+U^uh_{t-1}+b^u) \\ 
u'_t=u_t*a_t \\ 
\hat{h_t}=tanh(W^hi_t+r_t\circ U^hh_{t-1}+b^h)\\ 
h_t=(1-u'_t)\circ h_{t-1}+u'_t\circ \hat{h_t}
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;是不是有点晕。。没事，我们对比一下正常gru的公式。。&lt;a href=&quot;https://daiwk.github.io/posts/nlp-nmt.html#12-gru&quot;&gt;https://daiwk.github.io/posts/nlp-nmt.html#12-gru&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\ z_t=\sigma(W_zx_t+U_zh_{t-1}+b_z)
\\ r_t=\sigma(W_rx_t+U_rh_{t-1}+b_r)
\\ h_t=z_t \circ h_{t-1}+(1-z_t) \circ tanh(W_hx_t+ U_h(r_t \circ h_{t-1}) + b_h)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;就会发现其实一毛一样。。只是把更新门多乘了一个attention权重而已。。。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_t\)&lt;/code&gt;是ad和&lt;strong&gt;当前时间点兴趣&lt;/strong&gt;(由兴趣提取层提取得到)的&lt;strong&gt;相关度权重&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
a_i=\frac{exp(h_iWe_{ad})}{\sum^T_{i=0}exp(h_jWe_{ad})}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后，将兴趣演化层的最后一个时刻的兴趣表达&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h'(T)\)&lt;/code&gt;作为用户兴趣，因为它捕捉了用户兴趣的演化信息，并且是和&lt;strong&gt;ad相关的子兴趣表达&lt;/strong&gt;。最后将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h'(T)\)&lt;/code&gt;和ad特征、上下文特征、用户静态信息特征一起拼接在一起，输出多层dnn进行预测。&lt;/p&gt;

&lt;p&gt;代码实现：&lt;a href=&quot;https://github.com/alibaba/x-deeplearning/blob/master/xdl-algorithm-solution/DIEN/script/model.py&quot;&gt;https://github.com/alibaba/x-deeplearning/blob/master/xdl-algorithm-solution/DIEN/script/model.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其中augru的实现如下：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/alibaba/x-deeplearning/blob/master/xdl-algorithm-solution/DIEN/script/utils.py#L139&quot;&gt;https://github.com/alibaba/x-deeplearning/blob/master/xdl-algorithm-solution/DIEN/script/utils.py#L139&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;esmm&quot;&gt;ESMM&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1804.07931&quot;&gt;Entire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conversion Rate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;传统CVR预估模型会有比较明显的样本选择偏差（sample selection bias）和训练数据过于稀疏（data sparsity ）的问题，而ESMM模型利用&lt;strong&gt;用户行为序列数据&lt;/strong&gt;，在完整的样本数据空间&lt;strong&gt;同时学习点击率和转化率&lt;/strong&gt;（post-view clickthrough&amp;amp;conversion rate，CTCVR），在一定程度上解决了这个问题。&lt;/p&gt;

&lt;p&gt;定义：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(pctcvr=pctr*pcvr\)&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/esmm.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;主任务是pCVR。引入两个辅助任务，分别拟合pCTR和pCTCVR，把pCVR当做一个中间变量。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(\theta_{cvr},\theta_{ctr})=\sum ^N_{i=1}l(y_i,f(x_i;\theta_{ctr}))+\sum ^N_{i=1}l(y_i\&amp;amp;z_i,f(x_i;\theta_{ctr})\times f(x_i;\theta_{cvr}))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l\)&lt;/code&gt;是交叉熵。可以参考&lt;a href=&quot;https://github.com/alibaba/x-deeplearning/blob/master/xdl-algorithm-solution/ESMM/script/esmm.py#L269&quot;&gt;https://github.com/alibaba/x-deeplearning/blob/master/xdl-algorithm-solution/ESMM/script/esmm.py#L269&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;实际操作中，由于pCTR通常很小，pCTCVR除这个很小的数，容易溢出。故ESMM采用了乘法的形式，避免了除法。且能够使pCVR的值在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[0,1]&lt;/code&gt;区间。&lt;/p&gt;

&lt;p&gt;ESMM模型是在整个样本空间建模，而不像传统CVR预估模型那样只在点击样本空间建模。&lt;/p&gt;

&lt;p&gt;创新点其实是，一方面在&lt;strong&gt;特征间&lt;/strong&gt;传统mtl的&lt;strong&gt;隐式共享&lt;/strong&gt;，另一方面，在&lt;strong&gt;label间&lt;/strong&gt;找到了&lt;strong&gt;显式的关联&lt;/strong&gt;（在此模型中，就是&lt;strong&gt;连乘关系&lt;/strong&gt;）&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/alibaba/x-deeplearning/blob/master/xdl-algorithm-solution/ESMM/script/esmm.py#L228&quot;&gt;https://github.com/alibaba/x-deeplearning/blob/master/xdl-algorithm-solution/ESMM/script/esmm.py#L228&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;tdm&quot;&gt;TDM&lt;/h2&gt;

&lt;p&gt;严格来说，tdm是个召回模型，这里一起看一看&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1801.02294&quot;&gt;Learning Tree-based Deep Model for Recommender Systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/alibaba/x-deeplearning/wiki/%E6%B7%B1%E5%BA%A6%E6%A0%91%E5%8C%B9%E9%85%8D%E6%A8%A1%E5%9E%8B(TDM)&quot;&gt;https://github.com/alibaba/x-deeplearning/wiki/%E6%B7%B1%E5%BA%A6%E6%A0%91%E5%8C%B9%E9%85%8D%E6%A8%A1%E5%9E%8B(TDM)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://tianchi.aliyun.com/course/video?spm=5176.12586971.1001.57.55e2194dAHpQl4&amp;amp;liveId=41072&quot;&gt;CIKM 2019 EComm AI：超大规模推荐之用户兴趣高效检索 赛题解读及阿里深度树匹配技术实践&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;deepgbm&quot;&gt;DeepGBM&lt;/h2&gt;

&lt;p&gt;KDD2019上，&lt;a href=&quot;http://delivery.acm.org/10.1145/3340000/3330858/p384-ke.pdf?ip=123.116.98.90&amp;amp;id=3330858&amp;amp;acc=OPENTOC&amp;amp;key=4D4702B0C3E38B35%2E4D4702B0C3E38B35%2E4D4702B0C3E38B35%2E9F04A3A78F7D3B8D&amp;amp;__acm__=1567230572_d9273c6128be4b62d2932d75d61e6af9&quot;&gt;DeepGBM: A Deep Learning Framework Distilled by GBDT for Online Prediction Tasks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;而目前广泛用于这些任务的机器学习模型都没有完美适配这两个重要的特性。常用的机器学习模型大致可分为三类：梯度提升树（GBDT）、神经网络（NN）和二者（GBDT+NN）结合的模型。其中 GBDT 可以很好地处理连续的数值特征，但很难处理好稀疏的类别特征，并且 GBDT 是利用全量数据学习的，很难高效地进行在线更新；而 NN（如Wide&amp;amp;Deep、DeepFM等）虽然可以用 embedding 技术处理好稀疏类别特征，且可以高效地在线更新，但其难以很好地处理数值特征；常见的 GBDT+NN 方法会单独使用 GBDT 再使用 NN，但同样由于 GBDT 的缘故，难以在线更新。&lt;/p&gt;

&lt;p&gt;DeepGBM 主要包含两个子模块——面向类别特征的 CatNN 和面向数值特征的 GBDT2NN。CatNN 主要继承了 NN 中对类别特征友好的 Deep 模块和 FM 模块；GBDT2NN 是文章最主要的贡献之一，其基于 GBDT 进行知识蒸馏构造了一个 NN 模块，可以有效地处理类别特征并可以在线更新。综上，DeepGBM 既支持含有类别特征和数值特征的表格型数据输入，还能利用实时产生的数据进行学习和更新。&lt;/p&gt;

&lt;h2 id=&quot;各种比赛&quot;&gt;各种比赛&lt;/h2&gt;

&lt;p&gt;2019腾讯广告大赛：&lt;/p&gt;

&lt;p&gt;冠军方案：&lt;a href=&quot;https://zhuanlan.zhihu.com/p/72762888&quot;&gt;https://zhuanlan.zhihu.com/p/72762888&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/73062485&quot;&gt;https://zhuanlan.zhihu.com/p/73062485&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;初赛代码：&lt;a href=&quot;https://github.com/guoday/Tencent2019_Preliminary_Rank1st&quot;&gt;https://github.com/guoday/Tencent2019_Preliminary_Rank1st&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;包括deepwalk/CIN/key-value memory network等&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1606.03126.pdf&quot;&gt;Key-Value Memory Networks for Directly Reading Documents&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;应用示例&quot;&gt;应用示例&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/w1Kk1hn9LZzy8dgAqGGUtw&quot;&gt;从模型到部署，FPGA该怎样加速广告推荐算法&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>parameter server详解</title>
   <link href="http://daiwk.github.io/posts/dl-pserver.html"/>
   <updated>2018-01-11T00:00:00+00:00</updated>
   <id>/posts/dl-pserver</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%83%8c%e6%99%af&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%8f%91%e5%b1%95%e5%8e%86%e7%a8%8b&quot;&gt;发展历程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%af%b9%e6%af%94parameter-server%e4%b8%8e%e9%80%9a%e7%94%a8%e5%88%86%e5%b8%83%e5%bc%8f%e7%b3%bb%e7%bb%9f&quot;&gt;对比parameter server与通用分布式系统&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#parameter-server%e7%9a%84%e4%bc%98%e5%8a%bf&quot;&gt;parameter server的优势&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#parameter-server%e7%b3%bb%e7%bb%9f%e6%9e%b6%e6%9e%84&quot;&gt;parameter server系统架构&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%80%bb%e4%bd%93%e6%9e%b6%e6%9e%84&quot;&gt;总体架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#kv-range-push--pull&quot;&gt;(k,v), range push &amp;amp; pull&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#asynchronous-tasks-and-dependency&quot;&gt;Asynchronous Tasks and Dependency&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#implementation&quot;&gt;Implementation&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#vector-clock&quot;&gt;Vector Clock&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#messages&quot;&gt;Messages&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#consistent-hashing&quot;&gt;Consistent Hashing&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#replication-and-consistency&quot;&gt;Replication and Consistency&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e9%bb%98%e8%ae%a4%e7%9a%84%e5%a4%8d%e5%88%b6%e6%96%b9%e5%bc%8f-chain-replication-%e5%bc%ba%e4%b8%80%e8%87%b4%e6%80%a7-%e5%8f%af%e9%9d%a0&quot;&gt;默认的复制方式: &lt;strong&gt;Chain replication (强一致性, 可靠)&lt;/strong&gt;：&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#replication-after-aggregation&quot;&gt;Replication after Aggregation&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#server-management&quot;&gt;Server Management&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%b6%e4%bb%96%e8%b5%84%e6%96%99&quot;&gt;其他资料&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.csdn.net/cyh_24/article/details/50545780&quot;&gt;Parameter Server 详解&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考论文：
&lt;a href=&quot;https://www.cs.cmu.edu/~muli/file/parameter_server_osdi14.pdf&quot;&gt;Scaling Distributed Machine Learning with the Parameter Server&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

&lt;p&gt;现实中，训练数据的数量可能达到1TB到1PB之间，而训练过程中的参数可能会达到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(10^9\)&lt;/code&gt;（十亿）到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(10^{12}\)&lt;/code&gt;（千亿）。而往往这些模型的参数需要被所有的worker节点频繁的访问，就有如下问题与挑战：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;需要大量的网络带宽支持&lt;/li&gt;
  &lt;li&gt;很多机器学习算法都是连续型的，&lt;strong&gt;只有上一次迭代完成（各个worker都完成）之后，才能进行下一次迭代，&lt;/strong&gt;这就导致了如果机器之间性能差距大（木桶理论），就会造成性能的极大损失；&lt;/li&gt;
  &lt;li&gt;在分布式中，&lt;strong&gt;容错能力&lt;/strong&gt;是非常重要的。很多情况下，算法都是部署到云环境中的（这种环境下，机器是不可靠的，并且job也是有可能被抢占的）&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;发展历程&quot;&gt;发展历程&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;第一代 parameter server：缺少灵活性和性能 —— 仅使&lt;strong&gt;用memcached(key, value) 键值对存储作为同步机制。&lt;/strong&gt;YahooLDA 通过改进这个机制，增加了一个专门的服务器，提供用户能够自定义的更新操作(set, get, update)。&lt;/li&gt;
  &lt;li&gt;第二代 parameter server：用&lt;strong&gt;bounded delay模型&lt;/strong&gt;来改进YahooLDA，但是却进一步限制了worker线程模型。&lt;/li&gt;
  &lt;li&gt;第三代 parameter server 能够解决这些局限性。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;对比parameter-server与通用分布式系统&quot;&gt;对比parameter server与通用分布式系统&lt;/h2&gt;

&lt;p&gt;通用的分布式系统通常都是：每次迭代都&lt;strong&gt;强制同步&lt;/strong&gt;，通常在几十个节点上，它们的性能可以表现的很好，但是在&lt;strong&gt;大规模集群中，这样的每次迭代强制同步的机制会因为木桶效应变得很慢。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mahout 基于 Hadoop，MLI 基于 Spark，&lt;/strong&gt;它们（Spark与MLI）采用的都是 &lt;strong&gt;Iterative MapReduce 的架构&lt;/strong&gt;。它们能够保持迭代之间的状态，并且执行策略也更加优化了。但是，由于这两种方法都采用&lt;strong&gt;同步迭代的通信方式&lt;/strong&gt;，使得它们很容易因为个别机器的低性能导致全局性能的降低。&lt;/p&gt;

&lt;p&gt;为了解决这个问题，&lt;strong&gt;Graphlab 采用图形抽象的方式进行异步调度通信。&lt;/strong&gt;但是它&lt;strong&gt;缺少了以 MapReduce 为基础架构的弹性扩展性&lt;/strong&gt;，并且它使用&lt;strong&gt;粗粒度的snapshots来进行恢复，这两点都会阻碍到可扩展性。&lt;/strong&gt;parameter server 正是吸取Graphlab异步机制的优势，并且解决了其在可扩展性方面的劣势。&lt;/p&gt;

&lt;h2 id=&quot;parameter-server的优势&quot;&gt;parameter server的优势&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Efficient communication
由于是&lt;strong&gt;异步的通信&lt;/strong&gt;，因此，不需要停下来等一些机器执行完一个iteration（除非有必要），这大大减少了延时。为机器学习任务做了一些优化(后续会细讲)，能够大大减少网络流量和开销&lt;/li&gt;
  &lt;li&gt;Flexible consistency models
&lt;strong&gt;宽松的一致性要求&lt;/strong&gt;进一步减少了同步的成本和延时。parameter server 允许算法设计者根据自身的情况来做算法收敛速度和系统性能之间的trade-off。&lt;/li&gt;
  &lt;li&gt;Elastic Scalability
使用了一个&lt;strong&gt;分布式hash表&lt;/strong&gt;使得&lt;strong&gt;新的server节点可以随时动态的插入到集合中&lt;/strong&gt;；因此，&lt;strong&gt;新增一个节点不需要重新运行系统。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;Fault Tolerance and Durability
节点故障是不可避免的，特别是在大规模商用服务器集群中。&lt;strong&gt;从非灾难性机器故障中恢复，只需要1秒，而且不需要中断计算&lt;/strong&gt;。&lt;strong&gt;Vector clocks&lt;/strong&gt;保证了经历故障之后还是能运行良好&lt;/li&gt;
  &lt;li&gt;Ease of Use
&lt;strong&gt;全局共享的参数&lt;/strong&gt;可以被表示成各种形式：vector，matrices 或者相应的sparse类型，这大大方便了机器学习算法的开发。并且提供的线性代数的数据类型都具有高性能的多线程库。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;parameter-server系统架构&quot;&gt;parameter server系统架构&lt;/h2&gt;

&lt;h3 id=&quot;总体架构&quot;&gt;总体架构&lt;/h3&gt;

&lt;p&gt;在parameter server中，每个 server 实际上都只负责分到的&lt;strong&gt;部分参数&lt;/strong&gt;（servers共同维持一个全局的共享参数），而每个 work 也只分到&lt;strong&gt;部分数据&lt;/strong&gt;和处理任务。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_framework1.gif&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;上图中，每个子节点都只维护自己分配到的参数（黑色），&lt;strong&gt;自己部分更新之后&lt;/strong&gt;，将计算结果（例如，参数）传回到主节点，进行&lt;strong&gt;全局的更新&lt;/strong&gt;（比如平均操作之类的），主节点再向子节点传送新的参数。&lt;/p&gt;

&lt;p&gt;servers 与 workers 之间的通信如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_framework_server_worker_communication_0.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_framework_server_worker_communication.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;server 节点：&lt;/strong&gt;可以跟其他 server 节点通信，每个server负责自己分到的参数，&lt;strong&gt;server group 共同维持所有参数的更新。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;server manager node：&lt;/strong&gt;负责维护一些&lt;strong&gt;元数据的一致性&lt;/strong&gt;，比如各个&lt;strong&gt;节点的状态&lt;/strong&gt;，&lt;strong&gt;参数的分配&lt;/strong&gt;情况等&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;worker 节点：&lt;/strong&gt;worker之间没有通信，&lt;strong&gt;只跟自己对应的server进行通信。&lt;/strong&gt;每个&lt;strong&gt;worker group&lt;/strong&gt;有一个&lt;strong&gt;task scheduler&lt;/strong&gt;，负责向worker&lt;strong&gt;分配任务&lt;/strong&gt;，并且&lt;strong&gt;监控worker的运行情况&lt;/strong&gt;。当有新的worker加入或者退出，task scheduler 负责&lt;strong&gt;重新分配任务&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;kv-range-push--pull&quot;&gt;(k,v), range push &amp;amp; pull&lt;/h3&gt;

&lt;p&gt;parameter server 中，&lt;strong&gt;参数都是可以被表示成(key, value)的集合&lt;/strong&gt;，比如一个最小化损失函数的问题，&lt;strong&gt;key就是feature ID，而value就是它的权值。&lt;/strong&gt;对于&lt;strong&gt;稀疏参数，不存在的key，就可以认为是0。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;workers 跟 servers 之间通过 push 跟 pull 来通信。
&lt;strong&gt;worker 通过 push 将计算好的梯度发送到server，然后通过 pull 从server更新参数。&lt;/strong&gt;
为了提高计算性能和带宽效率，parameter server 允许用户使用&lt;strong&gt;Range Push 跟 Range Pull&lt;/strong&gt;操作。&lt;/p&gt;

&lt;p&gt;range push/pull：发送和接收特定Range中的参数。&lt;/p&gt;

&lt;h3 id=&quot;asynchronous-tasks-and-dependency&quot;&gt;Asynchronous Tasks and Dependency&lt;/h3&gt;

&lt;p&gt;如果 iter1 需要在 iter0 computation，push 跟 pull 都完成后才能开始，那么就是Synchronous，反之就是Asynchronous.&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_asynchronous_tasks.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;Asynchronous Task：能够提高系统的效率（因为节省了很多等待的过程），但是，它的缺点就是容易降低算法的收敛速率。&lt;/p&gt;

&lt;p&gt;系统性能和算法收敛速率的trade-off需要考虑的因素：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;算法对于参数非一致性的敏感度；&lt;/li&gt;
  &lt;li&gt;训练数据特征之间的关联度；&lt;/li&gt;
  &lt;li&gt;硬盘的存储容量&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;考虑到用户使用的时候会有不同的情况，parameter server 为用户提供了多种任务依赖方式：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_dependency.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Sequential&lt;/strong&gt;： 这里其实是 synchronous task，任务之间是有顺序的，只有上一个任务完成，才能开始下一个任务；&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Eventual&lt;/strong&gt;： 跟 sequential 相反，所有任务之间没有顺序，各自独立完成自己的任务，&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bounded Delay&lt;/strong&gt;： 这是sequential 跟 eventual 之间的trade-off，可以设置一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau \)&lt;/code&gt;作为最大的延时时间。也就是说，只有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(&amp;gt;\tau \)&lt;/code&gt;之前的任务都被完成了，才能开始一个新的任务。极端的情况：&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau = 0\)&lt;/code&gt;情况就是 Sequential；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau = \infty \)&lt;/code&gt;情况就是 Eventual&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;一个bounded delay 的 PGD (proximal gradient descent)算法的系统运行流程：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_proximal_gradient_descent.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;如何选择&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau \)&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_bounded_delay_choose.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;implementation&quot;&gt;Implementation&lt;/h2&gt;

&lt;h3 id=&quot;vector-clock&quot;&gt;Vector Clock&lt;/h3&gt;

&lt;p&gt;parameter server 使用 vector clock 来记录&lt;strong&gt;每个节点中参数的时间戳&lt;/strong&gt;，能够用来&lt;strong&gt;跟踪状态&lt;/strong&gt;或&lt;strong&gt;避免数据的重复发送&lt;/strong&gt;。但是，假设有n个节点，m个参数，那么vector clock的空间复杂度就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(n*m)\)&lt;/code&gt;。当有几千个节点和几十亿的参数时，对于内存和带宽来说都是不可实现的。&lt;/p&gt;

&lt;p&gt;parameter server 在push跟pull的时候，都是&lt;strong&gt;rang-based&lt;/strong&gt;，这就带来了一个好处：&lt;strong&gt;这个range里面的参数共享的是同一个时间戳&lt;/strong&gt;，这显然可以大大降低空间复杂度。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_vector_lock.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;每次从一个range里再提取一个range，最多会生成3个新的 vector clocks（一分为三）。假设总共m个参数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;是算法中产生的所有的range，那么空间复杂度就变成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(k*m)\)&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;messages&quot;&gt;Messages&lt;/h3&gt;

&lt;p&gt;一条 message 包括：时间戳，len(range)对k-v：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
[vc(R), (k_1, v_1), . . . , (k_p, v_p)] k_j \in R \; \; and\;\; j \in \{1, . . . p\}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这是parameter server 中&lt;strong&gt;最基本的通信格式&lt;/strong&gt;，不仅仅是&lt;strong&gt;共享的参数&lt;/strong&gt;才有，&lt;strong&gt;task 的message&lt;/strong&gt;也是这样的格式，只要把这里的(key, value) 改成 &lt;strong&gt;(task ID, 参数/返回值)&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;由于机器学习问题通常都需要很高的网络带宽，因此&lt;strong&gt;信息的压缩&lt;/strong&gt;是必须的。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;key的压缩：&lt;/strong&gt; 
  因为训练数据通常在分配之后都不会发生改变，因此
    &lt;ul&gt;
      &lt;li&gt;worker没有必要每次都发送相同的key，只需要&lt;strong&gt;接收方在第一次接收的时候缓存&lt;/strong&gt;起来就行了。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;第二次&lt;/strong&gt;，worker不再需要同时发送key和value，&lt;strong&gt;只需要发送value 和 key list的hash就行&lt;/strong&gt;。这样瞬间减少了一半的通信量。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;value的压缩：&lt;/strong&gt; 假设参数是稀疏的，那么就会有大量的0存在。因此，为了进一步压缩，我们&lt;strong&gt;只需要发送非0值。&lt;/strong&gt;parameter server使用&lt;strong&gt;Snappy快速压缩库来压缩数据、高效去除0值。&lt;/strong&gt;【Snappy 是一个 C++ 的用来压缩和解压缩的开发包。其目标不是最大限度压缩或者兼容其他压缩格式，而是旨在提供&lt;strong&gt;高速压缩速度和合理的压缩率&lt;/strong&gt;。 Snappy在 Google 内部被广泛的使用，从 BigTable 到 MapReduce 以及内部的 RPC 系统&lt;a href=&quot;https://code.google.com/p/snappy/&quot;&gt;https://code.google.com/p/snappy/&lt;/a&gt;】&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;另外，key 的压缩和 value 的压缩可以同时进行。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;另外，还有&lt;strong&gt;用户自定义过滤&lt;/strong&gt;：
对于机器学习优化问题比如梯度下降来说，并不是每次计算的梯度对于最终优化都是有价值的，用户可以通过&lt;strong&gt;自定义的规则过滤一些不必要的传送，再进一步压缩带宽cost&lt;/strong&gt;：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;发送&lt;strong&gt;很小的梯度值&lt;/strong&gt;是低效的&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;更新接近最优情况的值&lt;/strong&gt;是低效的 
因此，只在非最优的情况下发送，可通过&lt;strong&gt;KKT&lt;/strong&gt;来判断&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;consistent-hashing&quot;&gt;Consistent Hashing&lt;/h3&gt;

&lt;p&gt;parameter server 在数据一致性上，使用的是传统的&lt;strong&gt;一致性哈希&lt;/strong&gt;算法，&lt;strong&gt;参数key与server node id被插入到一个hash ring中&lt;/strong&gt;。在分布式系统中，&lt;strong&gt;动态增加和移除节点&lt;/strong&gt;的同时还能&lt;strong&gt;保证系统存储与key分配的性能效率&lt;/strong&gt;。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_hash_ring.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;每个节点都复制了它逆时钟方向的k个节点中的key。图中，k=2，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S_1\)&lt;/code&gt;复制了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S_2\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S_3\)&lt;/code&gt;内的key。&lt;/p&gt;

&lt;h3 id=&quot;replication-and-consistency&quot;&gt;Replication and Consistency&lt;/h3&gt;

&lt;p&gt;两种方式保证slave跟master之间的数据一致性：&lt;/p&gt;

&lt;h4 id=&quot;默认的复制方式-chain-replication-强一致性-可靠&quot;&gt;默认的复制方式: &lt;strong&gt;Chain replication (强一致性, 可靠)&lt;/strong&gt;：&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_chain_replication.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;更新&lt;/strong&gt;：&lt;strong&gt;只能发生在数据头节点&lt;/strong&gt;,然后更新逐步后移，直到更新到达尾节点，并由尾节点向客户确认更新成功；&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;查询&lt;/strong&gt;：为保证强一致性，客户查询&lt;strong&gt;只能在尾节点进行&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;replication-after-aggregation&quot;&gt;Replication after Aggregation&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pserver_replication_after_aggregation.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;两个worker 节点分别向server传送x和y。server 首先通过一定方式（如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f(x+y)\)&lt;/code&gt; ）&lt;strong&gt;进行aggregate&lt;/strong&gt;，然后&lt;strong&gt;再进行复制操作&lt;/strong&gt;；
当有n个worker的时候，复制只需要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k/n\)&lt;/code&gt;的带宽。通常来说，&lt;strong&gt;k（复制次数）是一个很小的常数&lt;/strong&gt;，而&lt;strong&gt;n的值大概是几百到几千&lt;/strong&gt;；&lt;/p&gt;

&lt;h2 id=&quot;server-management&quot;&gt;Server Management&lt;/h2&gt;

&lt;p&gt;要想实现系统的容错以及动态的扩展系统规模，必须要求系统能够支持&lt;strong&gt;动态添加和移除节点。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;当有一个 server节点添加进来时：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;server manager 会对新的节点分配一些range 的key，这会造成其他server节点的key的变化；&lt;/li&gt;
  &lt;li&gt;新节点会获取数据做为训练用，另外会复制k份到slave。&lt;/li&gt;
  &lt;li&gt;server manager 将节点的变化情况广播出去。接收方可能会移除不再属于自己的数据，并且将未完成的任务提交给新来的节点&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;当有一个worker节点W添加进来时：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;task scheduler 为W分配数据；&lt;/li&gt;
  &lt;li&gt;这个 worker 节点通过网络或者文件系统得到分配到的训练数据。接着，W会从服务器pull参数；&lt;/li&gt;
  &lt;li&gt;task scheduler 会广播节点的变化情况，可能会使一些节点释放一部分训练数据&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;其他资料&quot;&gt;其他资料&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/hjApfI-5cfkMCUqcuESnXQ&quot;&gt;参数服务器——分布式机器学习的新杀器&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/EZ0QmoOqSMJcJWmB7tuh-g&quot;&gt;解决大规模机器学习的策略和原则&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/wERMC2jcZS34qtyag5kRzQ&quot;&gt;一个利用参数服务器解决大规模在线学习的实例&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>irgan</title>
   <link href="http://daiwk.github.io/posts/rl-irgan.html"/>
   <updated>2018-01-02T00:00:00+00:00</updated>
   <id>/posts/rl-irgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%a6%82%e8%a7%88&quot;&gt;概览&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%a7%82%e7%82%b93&quot;&gt;观点3&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/27472708&quot;&gt;SIGIR2017 满分论文：IRGAN | 每周一起读&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1705.10513.pdf&quot;&gt;IRGAN: A Minimax Game for Unifying Generative and Discriminative Information Retrieval Models&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在现代信息检索领域一直是两大学派之争的局面。一方面，经典思维流派是假设在文档和信息需求（由查询可知）之间存在着一个&lt;strong&gt;独立的随机生成&lt;/strong&gt;过程。另一方面，现代思维流派则充分利用机器学习的优势，将文档和搜索词联合考虑为特征，并从大量训练数据中&lt;strong&gt;预测其相关性&lt;/strong&gt;或&lt;strong&gt;排序&lt;/strong&gt;顺序标签。&lt;/p&gt;

&lt;p&gt;本篇SIGIR2017的满分论文则首次提出将两方面流派的数据模型通过一种对抗训练的方式统一在一起，使得两方面的模型能够相互提高，最终使得检索到的文档更加精准。文章的实验分别在&lt;strong&gt;搜索&lt;/strong&gt;、&lt;strong&gt;推荐系统&lt;/strong&gt;以及&lt;strong&gt;问答系统&lt;/strong&gt;三个应用场景中实现并验证了结果的有效性。&lt;/p&gt;

&lt;h2 id=&quot;概览&quot;&gt;概览&lt;/h2&gt;

&lt;p&gt;IR 系统，从数据库中检索相似的信息，应对的是离散的数据，而GAN一般在连续情况下容易work。常用的SGD在这里并不 work，作者采用RL的policy gradient作为替代。 IRGAN将generative retrieval model和discriminative retrieval model分别作为GAN的 generator和discriminator（文章里对生成模型和判别模型的提法跟我们通常所说的刚好相反）。所以，IRGAN训练的结果是&lt;strong&gt;两个IR系统&lt;/strong&gt;：一个是生成模型，一个是判别模型。作者开源了代码，做了一些实验实验：web search，item recommendation，question answering，实验结果表明，IRGAN打败了多种strong baselines，带来显著的性能提升。作者认为，这种性能提升得益于GAN的对抗训练机制。两类IR模型统一到GAN框架下，虽然它们的性能不同，但是跟没有采用对抗训练的模型相比，它们之中&lt;strong&gt;至少有一个能够得到显著的性能提升&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;学习一下：&lt;a href=&quot;https://www.cnblogs.com/liaohuiqiang/p/9694277.html&quot;&gt;https://www.cnblogs.com/liaohuiqiang/p/9694277.html&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;判别模型：挖掘标注与非标注的数据中的信息，用于指导训练拟合文档内在相关性分布生成模型；&lt;/li&gt;
  &lt;li&gt;生成模型：生成判别模型难以判别的例子；&lt;/li&gt;
  &lt;li&gt;经典相关性模型着重于，如何从查询生成(相关)文档；独立模型，每个token是独立从相关文档档中生成；统计语言模型一般是从文档中生成查询元素；在词嵌模型中，词从其上下文中生成；在推荐系统中，也有类似的方法，从 item 的上下文中生成 item；&lt;/li&gt;
  &lt;li&gt;模型扩展到 pointwise, pairwise, listwise, 其中 pointwise 基于人的判断来衡量相关性，pairwise 主要是在所有文档对中找出最相关的文档对，listwise 着重于返回最合理的相关性排序；&lt;/li&gt;
  &lt;li&gt;观察到的正例和未观察到的正例之间会存在内在联系，生成器需要基于判别器的信息来快速推动这些未观察到的正例；&lt;/li&gt;
  &lt;li&gt;与 conditional GAN 有些相似；&lt;/li&gt;
  &lt;li&gt;生成模型提供了一种新的负采样方式；&lt;/li&gt;
  &lt;li&gt;使用 IR 的奖励机制，是在传统模型中不可获取的；&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;观点3&quot;&gt;观点3&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;不像传统的 GAN，用噪声信号作为输入做生成，然后判别，而是将 Query 做输入，直接利用 Generative 和 Discriminative IR Models 做 GAN；&lt;/li&gt;
  &lt;li&gt;将 RL 的 Policy Gradient 引入针对离散的输入变量；以上两个步骤使得 GAN 更具推广意义。&lt;/li&gt;
&lt;/ol&gt;

</content>
 </entry>
 
 <entry>
   <title>tagspace</title>
   <link href="http://daiwk.github.io/posts/nlp-tagspaces.html"/>
   <updated>2018-01-02T00:00:00+00:00</updated>
   <id>/posts/nlp-tagspaces</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://research.fb.com/wp-content/uploads/2014/09/tagspace-semantic-embeddings-from-hashtags.pdf&quot;&gt;#TagSpace: Semantic Embeddings from Hashtags&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://research.fb.com/publications/tagspace-semantic-embeddings-from-hashtags/&quot;&gt;https://research.fb.com/publications/tagspace-semantic-embeddings-from-hashtags/&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tagspaces.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;假设一句话有l个词，总词典大小是N，那么大的emb矩阵就是Nxd，而对于这句话来讲，就是一个lxd的矩阵，然后用same padding，并用1D卷积，详细地说，就是先padding成一个(l+K-1)xd的矩阵，然后有H个Kxd的卷积核，这样得到的结果就是一个(l+K-1-K+1)x(d-d+1)xH=lx1xH=lxH的矩阵了，之所以叫same padding，就是因为一开始的第一维是l，最终的第一维仍是l。&lt;/p&gt;

&lt;p&gt;对于maxpooling来说，输入是lxH，按照论文和图中的，应该是l个数取max，得到1xH，但下面的tf的实现，好像不是这样的呢。。。paddle的实现用的是&lt;a href=&quot;https://daiwk.github.io/posts/nlp-nmt.html#8-%E5%85%B6%E4%BB%96&quot;&gt;https://daiwk.github.io/posts/nlp-nmt.html#8-%E5%85%B6%E4%BB%96&lt;/a&gt;这个的sequence_conv_pool函数，实现参考&lt;/p&gt;

&lt;p&gt;参考tf代码&lt;a href=&quot;https://github.com/flrngel/TagSpace-tensorflow/blob/master/model.py&quot;&gt;https://github.com/flrngel/TagSpace-tensorflow/blob/master/model.py&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;      &lt;span class=&quot;n&quot;&gt;doc_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tflearn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lt_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lt_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 在卷积这步这个变量没啥用
&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tflearn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv_1d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;doc_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'tanh'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# conv_1d默认是same padding，卷积核是Kxd，有H个卷积核，输出是(None,l,d)
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tflearn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_pool_1d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# max_pool_1d默认也是same pooling，输出是(None,xxx,H)
&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;## maybe上面那行应该改成tensorflow.keras.layers.GlobalMaxPooling1D(net)# 输出就是(None,H)啦?
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tflearn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tanh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tflearn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fully_connected&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考paddle代码：&lt;a href=&quot;https://github.com/PaddlePaddle/models/blob/develop/fluid/PaddleRec/tagspace/net.py&quot;&gt;https://github.com/PaddlePaddle/models/blob/develop/fluid/PaddleRec/tagspace/net.py&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;text_emb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_text_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;emb_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;text_emb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pos_tag_emb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_tag_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;emb_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;tag_emb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;neg_tag_emb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;neg_tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_tag_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;emb_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;tag_emb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;conv_1d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence_conv_pool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_emb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;num_filters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hid_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;filter_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;win_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;pool_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;max&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;param_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cnn&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;text_hid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fluid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv_1d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emb_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;text_hid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cos_pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cos_sim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_tag_emb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_hid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>c++11新特性</title>
   <link href="http://daiwk.github.io/posts/knowledge-c++11.html"/>
   <updated>2018-01-02T00:00:00+00:00</updated>
   <id>/posts/knowledge-c++11</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-简介&quot;&gt;0. 简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-核心语言的运行期表现强化&quot;&gt;1. 核心语言的运行期表现强化&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-右值引用和move语义&quot;&gt;1.1 右值引用和move语义&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-constexpr--泛化的常量表示式&quot;&gt;1.2 constexpr – 泛化的常量表示式&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-对pod定义的修正&quot;&gt;1.3 对POD定义的修正&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-核心语言构造期表现的加强&quot;&gt;2. 核心语言构造期表现的加强&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-外部模板&quot;&gt;2.1 外部模板&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-核心语言使用性的加强&quot;&gt;3. 核心语言使用性的加强&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-初始化列表&quot;&gt;3.1 初始化列表&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-统一的初始化&quot;&gt;3.2 统一的初始化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#33-类型推导&quot;&gt;3.3 类型推导&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#34-基于范围的for循环&quot;&gt;3.4 基于范围的for循环&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#35-lambda函数与表示式&quot;&gt;3.5 Lambda函数与表示式&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#36-回返类型后置的函数声明&quot;&gt;3.6 回返类型后置的函数声明&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#37-对象构造的改良&quot;&gt;3.7 对象构造的改良&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#38-显式虚函数重载&quot;&gt;3.8 显式虚函数重载&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#39-空指针&quot;&gt;3.9 空指针&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#310-强类型枚举&quot;&gt;3.10 强类型枚举&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#311-角括号&quot;&gt;3.11 角括号&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#312-显式类型转换子&quot;&gt;3.12 显式类型转换子&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#313-模板的别名&quot;&gt;3.13 模板的别名&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#314-无限制的unions&quot;&gt;3.14 无限制的unions&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-核心语言能力的提升&quot;&gt;4. 核心语言能力的提升&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-可变参数模板&quot;&gt;4.1 可变参数模板&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-新的字符串字面值&quot;&gt;4.2 新的字符串字面值&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#43-用户定义字面量&quot;&gt;4.3 用户定义字面量&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#44-多任务内存模型&quot;&gt;4.4 多任务内存模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#45-thread-local的存储期限&quot;&gt;4.5 thread-local的存储期限&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#46-使用或禁用对象的默认函数&quot;&gt;4.6 使用或禁用对象的默认函数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#47-long-long-int类型&quot;&gt;4.7 long long int类型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#48-静态assertion&quot;&gt;4.8 静态assertion&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#49-允许sizeof运算符作用在类别的数据成员上无须明确的对象&quot;&gt;4.9 允许sizeof运算符作用在类别的数据成员上，无须明确的对象&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#410-垃圾回收机制&quot;&gt;4.10 垃圾回收机制&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-c标准程序库的变更&quot;&gt;5. C++标准程序库的变更&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#51-标准库组件上的升级&quot;&gt;5.1 标准库组件上的升级&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#52-线程支持&quot;&gt;5.2 线程支持&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#53-多元组类型&quot;&gt;5.3 多元组类型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#54-散列表&quot;&gt;5.4 散列表&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#55-正则表达式&quot;&gt;5.5 正则表达式&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#56-通用智能指针&quot;&gt;5.6 通用智能指针&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#57-可扩展的随机数功能&quot;&gt;5.7 可扩展的随机数功能&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#58-包装引用&quot;&gt;5.8 包装引用&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#59-多态函数对象包装器&quot;&gt;5.9 多态函数对象包装器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#510-用于元编程的类型属性&quot;&gt;5.10 用于元编程的类型属性&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#511-用于计算函数对象回返类型的统一方法&quot;&gt;5.11 用于计算函数对象回返类型的统一方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#512-iota-函数&quot;&gt;5.12 iota 函数&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#x-示例&quot;&gt;X. 示例&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#shuffle&quot;&gt;shuffle&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#带权随机采样&quot;&gt;带权随机采样&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#均匀随机采样&quot;&gt;均匀随机采样&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#在一个unorder_map里随机抽取元素&quot;&gt;在一个unorder_map里随机抽取元素&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：
&lt;a href=&quot;https://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html&quot;&gt;C++11新特性&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/C++11&quot;&gt;英文维基百科: c++11&lt;/a&gt;
&lt;a href=&quot;https://zh.wikipedia.org/wiki/C%2B%2B11&quot;&gt;中文维基百科：c++11&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;0-简介&quot;&gt;0. 简介&lt;/h2&gt;

&lt;p&gt;C++11,之前被称作C++0x，即ISO/IEC 14882:2011，是目前的C++编程语言的正式标准。它取代第二版标准ISO/IEC 14882:2003(第一版ISO/IEC 14882:1998发布于1998年，第二版于2003年发布，分别通称C++98以及C++03，两者差异很小)。新的标准包含了几个核心语言增加的新特性，而且扩展C++标准程序库，并入了大部分的C++ Technical Report 1程序库(数学的特殊函数除外)。最新的消息被公布在 ISO C++ 委员会网站(英文)。
ISO／IEC JTC1/SC22/WG21 C++ 标准委员会计划在2010年8月之前完成对最终委员会草案的投票，以及于2011年3月召开的标准会议完成国际标准的最终草案。然而，WG21预期ISO将要花费六个月到一年的时间才能正式发布新的C++标准。为了能够如期完成，委员会决定致力于直至2006年为止的提案，忽略新的提案。最终,于2011年8月12日公布，并于2011年9月出版。2012年2月28日的国际标准草案(N3376)是最接近于现行标准的草案，差异仅有编辑上的修正。
像C++这样的编程语言，通过一种演化的过程来发展其定义。这个过程不可避免地将引发与现有代码的兼容问题。不过根据Bjarne Stroustrup(C++的创始人,标准委员会的一员)表示，新的标准将几乎100%兼容现有标准。&lt;/p&gt;

&lt;h2 id=&quot;1-核心语言的运行期表现强化&quot;&gt;1. 核心语言的运行期表现强化&lt;/h2&gt;

&lt;p&gt;提升某些性能表现，例如内存或是速度上的表现。&lt;/p&gt;

&lt;h3 id=&quot;11-右值引用和move语义&quot;&gt;1.1 右值引用和move语义&lt;/h3&gt;

&lt;p&gt;在C++03及之前的标准中，临时对象(称为&lt;strong&gt;右值”R-values”&lt;/strong&gt;，因为通常&lt;strong&gt;位于赋值运算符的右边&lt;/strong&gt;)的值是&lt;strong&gt;不能改变&lt;/strong&gt;的，和C语言一样, 且&lt;strong&gt;无法和 const T&amp;amp; 类型做出区分&lt;/strong&gt;。尽管在某些情况下临时对象的确会被改变，甚至有时还被视为是一个有用的漏洞。C++11新增加了一个非常量引用(non-const reference)类型，称作&lt;strong&gt;右值引用(R-value reference)&lt;/strong&gt;，标记为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T &amp;amp;&amp;amp;&lt;/code&gt;。右值引用所引用的临时对象&lt;strong&gt;可以在该临时对象被初始化之后做修改&lt;/strong&gt;，这是为了&lt;strong&gt;允许 move 语义&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;C++03 性能上被长期诟病的问题之一，就是其&lt;strong&gt;耗时&lt;/strong&gt;且&lt;strong&gt;不必要&lt;/strong&gt;的&lt;strong&gt;深拷贝&lt;/strong&gt;。深拷贝会隐式地发生在&lt;strong&gt;以传值的方式传递对象&lt;/strong&gt;的时候。例如，std::vector内部封装了一个C风格的数组和其元素个数，如果创建或是从函数返回一个std::vector的临时对象，要将其保存起来只能通过&lt;strong&gt;生成新的std::vector对象&lt;/strong&gt;并且把&lt;strong&gt;该临时对象所有的数据复制进去&lt;/strong&gt;。该&lt;strong&gt;临时对象&lt;/strong&gt;和其&lt;strong&gt;拥有的內存&lt;/strong&gt;会被&lt;strong&gt;销毁&lt;/strong&gt;。(为简单起见，这里忽略了编译器的返回值优化)&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zh.cppreference.com/w/cpp/container/vector/vector&quot;&gt;https://zh.cppreference.com/w/cpp/container/vector/vector&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;//移动构造函数。用移动语义构造拥有 other 内容的容器。分配器通过属于 other 的分配器移动构造获得。移动后，保证 other 为 empty() 。&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//(C++11 起) (C++17 前)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//(C++17 起)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 有分配器扩展的移动构造函数。以 alloc 为新容器的分配器，从 other 移动内容；若 alloc != other.get_allocator() ，则它导致逐元素移动。（该情况下，移动后不保证 other 为空）&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Allocator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alloc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//(C++11 起)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在 C++11中，std::vector有一个&lt;strong&gt;“移动构造函数”&lt;/strong&gt;，对&lt;strong&gt;某个vector的右值引用&lt;/strong&gt;可以单纯地&lt;strong&gt;从右值复制&lt;/strong&gt;其内部C风格数组的指针到新的vector中，&lt;strong&gt;然后将右值中的指针置空&lt;/strong&gt;。因为这个临时对象不会再被使用,没代码会再访问这个空指针，而且因为这个临时对象的内部指针是NULL，所以&lt;strong&gt;当这个临时对象离开作用域时它的内存也不会被释放掉&lt;/strong&gt;。所以,这个操作不仅&lt;strong&gt;没有代价高昂的深拷贝&lt;/strong&gt;, 还是安全的,对用户不可见的!&lt;/p&gt;

&lt;p&gt;这个操作不需要数组的复制，而且空的临时对象的析构也不会销毁内存。&lt;strong&gt;返回vector临时对象的函数只需要返回&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&amp;lt;T&amp;gt;&amp;amp;&amp;amp;&lt;/code&gt;。&lt;/strong&gt;如果vector没有move 构造函数，那么就会调用常规拷贝构造函数。&lt;strong&gt;如果有，那么就会优先调用move构造函数&lt;/strong&gt;，这能够避免大量的内存分配和内存拷贝操作。&lt;/p&gt;

&lt;p&gt;右值引用不用对标准库之外的代码做任何改动就可以为已有代码带来性能上的提升. &lt;strong&gt;返回值类型为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&amp;lt;T&amp;gt;&lt;/code&gt;的函数&lt;/strong&gt;返回了一个&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&amp;lt;T&amp;gt;&lt;/code&gt;类型的临时对象&lt;/strong&gt;，为了使用移动构造&lt;strong&gt;不需要显示地将返回值类型改为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&amp;lt;T&amp;gt;&amp;amp;&amp;amp;&lt;/code&gt;&lt;/strong&gt;, 因为&lt;strong&gt;这样的临时对象会被自动当作右值引用&lt;/strong&gt;。但是在c++03中, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&amp;lt;T&amp;gt;&lt;/code&gt;没有移动构造函数, 带有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const std::vector&amp;lt;T&amp;gt;&amp;amp;&lt;/code&gt;参数的&lt;strong&gt;拷贝构造&lt;/strong&gt;会**被调用, 这会导致大量内存分配和拷贝动作.&lt;/p&gt;

&lt;p&gt;出于安全考虑, 需要施加一些限制! &lt;strong&gt;一个已命名的变量即使声明为右值,也不会被视为右值&lt;/strong&gt;。想要&lt;strong&gt;获取一个右值&lt;/strong&gt;，应该使用模板函数&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::move&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/strong&gt;。 右值引用也可以在特定情况下被修改, 主要是为了与移动构造函数一起使用!&lt;/p&gt;

&lt;p&gt;由于”右值引用”这个词的自然语义,以及对”左值引用”(常规引用)这个词的修正, 右值引用可以让开发者提供完美的函数转发! 与可变参数模板结合时, 这个能力让模板函数能够完美地将参数转发给带有这些参数的另一个函数。这&lt;strong&gt;对构造函数的参数转发最为有用&lt;/strong&gt;，创建一个能够根据特定的参数自动调用适当的构造函数的工厂函数.（没太懂…）&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hahaha&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;xx&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 输出：&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 0x7fffd13c0860&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// hahaha&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 0x7fffd13c0860&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 1&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 1&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// xx // ==&amp;gt;move 后变成了empty()，string的内容也清空了，而对于double/int等普通类型，好像没变。。&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;12-constexpr--泛化的常量表示式&quot;&gt;1.2 constexpr – 泛化的常量表示式&lt;/h3&gt;

&lt;h3 id=&quot;13-对pod定义的修正&quot;&gt;1.3 对POD定义的修正&lt;/h3&gt;

&lt;h2 id=&quot;2-核心语言构造期表现的加强&quot;&gt;2. 核心语言构造期表现的加强&lt;/h2&gt;

&lt;h3 id=&quot;21-外部模板&quot;&gt;2.1 外部模板&lt;/h3&gt;

&lt;h2 id=&quot;3-核心语言使用性的加强&quot;&gt;3. 核心语言使用性的加强&lt;/h2&gt;

&lt;h3 id=&quot;31-初始化列表&quot;&gt;3.1 初始化列表&lt;/h3&gt;
&lt;h3 id=&quot;32-统一的初始化&quot;&gt;3.2 统一的初始化&lt;/h3&gt;
&lt;h3 id=&quot;33-类型推导&quot;&gt;3.3 类型推导&lt;/h3&gt;
&lt;h3 id=&quot;34-基于范围的for循环&quot;&gt;3.4 基于范围的for循环&lt;/h3&gt;
&lt;h3 id=&quot;35-lambda函数与表示式&quot;&gt;3.5 Lambda函数与表示式&lt;/h3&gt;
&lt;h3 id=&quot;36-回返类型后置的函数声明&quot;&gt;3.6 回返类型后置的函数声明&lt;/h3&gt;
&lt;h3 id=&quot;37-对象构造的改良&quot;&gt;3.7 对象构造的改良&lt;/h3&gt;
&lt;h3 id=&quot;38-显式虚函数重载&quot;&gt;3.8 显式虚函数重载&lt;/h3&gt;
&lt;h3 id=&quot;39-空指针&quot;&gt;3.9 空指针&lt;/h3&gt;
&lt;h3 id=&quot;310-强类型枚举&quot;&gt;3.10 强类型枚举&lt;/h3&gt;
&lt;h3 id=&quot;311-角括号&quot;&gt;3.11 角括号&lt;/h3&gt;
&lt;h3 id=&quot;312-显式类型转换子&quot;&gt;3.12 显式类型转换子&lt;/h3&gt;
&lt;h3 id=&quot;313-模板的别名&quot;&gt;3.13 模板的别名&lt;/h3&gt;
&lt;h3 id=&quot;314-无限制的unions&quot;&gt;3.14 无限制的unions&lt;/h3&gt;

&lt;h2 id=&quot;4-核心语言能力的提升&quot;&gt;4. 核心语言能力的提升&lt;/h2&gt;

&lt;h3 id=&quot;41-可变参数模板&quot;&gt;4.1 可变参数模板&lt;/h3&gt;

&lt;p&gt;在 C++11 之前, 不论是类模板或是函数模板，都只能按其被声明时所指定的样子，&lt;strong&gt;接受一组数目固定的模板参数&lt;/strong&gt;。C++11 加入新的表示法，允许&lt;strong&gt;任意个数，任意类型的模板参数&lt;/strong&gt;，不必在定义时将参数的个数固定。&lt;/p&gt;

&lt;p&gt;先写：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后，模板类 tuple 的对象，能接受不限个数的 typename 作为它的模板形参(如下面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int/vector&amp;lt;int&amp;gt;/map&amp;lt;std::string, std::vector&amp;lt;int&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;)。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;someInstanceName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;实参的个数也可以是0,所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;class tuple&amp;lt;&amp;gt; someInstanceName&lt;/code&gt;这样的定义也是可以的。&lt;/p&gt;

&lt;p&gt;若不希望产生实参个数为 0 的变长参数模板，则可以采用以下的定义：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;First&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Rest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;举个例子：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OutputType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;baseClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OutputType&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;实现一个子类如下，这样是没有问题的&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;derivedClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;derivedClass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xxx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;但如果把其中的一个const xxx*的指针去掉，如：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;derivedClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;kt&quot;&gt;int32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;derivedClass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xxx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;因为我们写了override标识，所以直接就报错了，一方面是这个函数并非是我们想要重载的基类的纯虚函数，另一方面没有重载纯虚函数的子类是无法被实例化的~&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./tmplt.h:18:13: error: 'int32_t derivedClass::process(int*, std::string, const bool*)' marked override, but does not override
     int32_t process(int* a, const string b, const bool* c) override {
             ^
./main.cpp: In function 'int main()':
./main.cpp:31:18: error: cannot declare variable 'xxx' to be of abstract type 'derivedClass'
     derivedClass xxx;
                  ^
In file included from ./main.cpp:5:0:
./tmplt.h:17:7: note:   because the following virtual functions are pure within 'derivedClass':
 class derivedClass: baseClass&amp;lt;int, string, bool&amp;gt; {
       ^
./tmplt.h:14:21: note:  int32_t baseClass&amp;lt;OutputType, Args&amp;gt;::process(OutputType*, const Args* ...) [with OutputType = int; Args = {std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &amp;gt;, bool}; int32_t = int]
     virtual int32_t process(OutputType *, const Args* ...) = 0;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而如果我们去掉override标识，报错就只剩没有实现纯虚函数的了，因为process函数被当做子类自己的新函数：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./main.cpp: In function 'int main()':
./main.cpp:31:18: error: cannot declare variable 'xxx' to be of abstract type 'derivedClass'
     derivedClass xxx;
                  ^
In file included from ./main.cpp:5:0:
./tmplt.h:15:7: note:   because the following virtual functions are pure within 'derivedClass':
 class derivedClass: baseClass&amp;lt;int, string, bool&amp;gt; {
       ^
./tmplt.h:12:21: note:  int32_t baseClass&amp;lt;OutputType, Args&amp;gt;::process(OutputType*, const Args* ...) [with OutputType = int; Args = {std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &amp;gt;, bool}; int32_t = int]
     virtual int32_t process(OutputType *, const Args* ...) = 0;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;另外，在这个子类里，这个process函数可以自由地声明成private/protected/public，都行~&lt;/p&gt;

&lt;h3 id=&quot;42-新的字符串字面值&quot;&gt;4.2 新的字符串字面值&lt;/h3&gt;
&lt;h3 id=&quot;43-用户定义字面量&quot;&gt;4.3 用户定义字面量&lt;/h3&gt;
&lt;h3 id=&quot;44-多任务内存模型&quot;&gt;4.4 多任务内存模型&lt;/h3&gt;
&lt;h3 id=&quot;45-thread-local的存储期限&quot;&gt;4.5 thread-local的存储期限&lt;/h3&gt;
&lt;h3 id=&quot;46-使用或禁用对象的默认函数&quot;&gt;4.6 使用或禁用对象的默认函数&lt;/h3&gt;
&lt;h3 id=&quot;47-long-long-int类型&quot;&gt;4.7 long long int类型&lt;/h3&gt;
&lt;h3 id=&quot;48-静态assertion&quot;&gt;4.8 静态assertion&lt;/h3&gt;
&lt;h3 id=&quot;49-允许sizeof运算符作用在类别的数据成员上无须明确的对象&quot;&gt;4.9 允许sizeof运算符作用在类别的数据成员上，无须明确的对象&lt;/h3&gt;
&lt;h3 id=&quot;410-垃圾回收机制&quot;&gt;4.10 垃圾回收机制&lt;/h3&gt;

&lt;h2 id=&quot;5-c标准程序库的变更&quot;&gt;5. C++标准程序库的变更&lt;/h2&gt;

&lt;h3 id=&quot;51-标准库组件上的升级&quot;&gt;5.1 标准库组件上的升级&lt;/h3&gt;
&lt;h3 id=&quot;52-线程支持&quot;&gt;5.2 线程支持&lt;/h3&gt;
&lt;h3 id=&quot;53-多元组类型&quot;&gt;5.3 多元组类型&lt;/h3&gt;
&lt;h3 id=&quot;54-散列表&quot;&gt;5.4 散列表&lt;/h3&gt;
&lt;h3 id=&quot;55-正则表达式&quot;&gt;5.5 正则表达式&lt;/h3&gt;
&lt;h3 id=&quot;56-通用智能指针&quot;&gt;5.6 通用智能指针&lt;/h3&gt;
&lt;h3 id=&quot;57-可扩展的随机数功能&quot;&gt;5.7 可扩展的随机数功能&lt;/h3&gt;
&lt;h3 id=&quot;58-包装引用&quot;&gt;5.8 包装引用&lt;/h3&gt;
&lt;h3 id=&quot;59-多态函数对象包装器&quot;&gt;5.9 多态函数对象包装器&lt;/h3&gt;
&lt;h3 id=&quot;510-用于元编程的类型属性&quot;&gt;5.10 用于元编程的类型属性&lt;/h3&gt;
&lt;h3 id=&quot;511-用于计算函数对象回返类型的统一方法&quot;&gt;5.11 用于计算函数对象回返类型的统一方法&lt;/h3&gt;
&lt;h3 id=&quot;512-iota-函数&quot;&gt;5.12 iota 函数&lt;/h3&gt;

&lt;h2 id=&quot;x-示例&quot;&gt;X. 示例&lt;/h2&gt;

&lt;h3 id=&quot;shuffle&quot;&gt;shuffle&lt;/h3&gt;

&lt;p&gt;如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;random&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;score_cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RidTmpInfoPtr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RidTmpInfoPtr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result_end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;advance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;video_rec_buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_result_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mt19937&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exponential_distribution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;distribution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distribution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;generator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res_score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;score_cmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;带权随机采样&quot;&gt;带权随机采样&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://stackoverflow.com/questions/1761626/weighted-random-numbers&quot;&gt;https://stackoverflow.com/questions/1761626/weighted-random-numbers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://c.biancheng.net/view/646.html&quot;&gt;http://c.biancheng.net/view/646.html&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;random&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iterator&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;ctime&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;type_traits&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;cassert&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx_lst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;a2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;a3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;a4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight_lst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2003&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8899&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3344&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;discrete_distribution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight_lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weight_lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mt19937&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx_lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;均匀随机采样&quot;&gt;均匀随机采样&lt;/h3&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;random&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iterator&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;ctime&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;type_traits&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;cassert&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uniform_int_distribution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mt19937&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;在一个unorder_map里随机抽取元素&quot;&gt;在一个unorder_map里随机抽取元素&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/27024269/select-random-element-in-an-unordered-map&quot;&gt;https://stackoverflow.com/questions/27024269/select-random-element-in-an-unordered-map&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;极慢。。。。。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unordered_map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Edge&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_it&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_between&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;返回的是随机一个非空的edge&lt;/p&gt;

&lt;p&gt;更复杂一点：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;random&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iterator&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;ctime&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;type_traits&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;cassert&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;map&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;memory&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;unordered_map&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unordered_map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unordered_map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;aa&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ptr_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unordered_map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uniform_int_distribution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;199&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mt19937&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;idx&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr_x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;!!!快得多的方法：&lt;/p&gt;

&lt;p&gt;建个list，size和map一样大，把map的key扔到这个list里去&lt;/p&gt;

&lt;p&gt;然后随机一个index，取出这个list对应的元素，再去map里get&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>deep & cross network for ad click predictions</title>
   <link href="http://daiwk.github.io/posts/dl-deep-cross-network.html"/>
   <updated>2018-01-02T00:00:00+00:00</updated>
   <id>/posts/dl-deep-cross-network</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%a6%82%e8%bf%b0&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#related-work&quot;&gt;related work&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#embedding%e6%96%b9%e6%b3%95&quot;&gt;embedding方法&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#factorization-machinesfms&quot;&gt;Factorization machines(FMs)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#field-aware-factorization-machinesffms&quot;&gt;Field-aware Factorization Machines(FFMs)&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c&quot;&gt;神经网络&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%b4%a1%e7%8c%ae&quot;&gt;贡献&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%bd%91%e7%bb%9c%e7%bb%93%e6%9e%84&quot;&gt;网络结构&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#embedding-and-stacking-layer&quot;&gt;Embedding and Stacking Layer&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cross-network&quot;&gt;Cross Network&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%a4%8d%e6%9d%82%e5%ba%a6%e5%88%86%e6%9e%90&quot;&gt;复杂度分析&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#deep-network&quot;&gt;Deep Network&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%a4%8d%e6%9d%82%e5%ba%a6%e5%88%86%e6%9e%90-1&quot;&gt;复杂度分析&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#combination-layer&quot;&gt;Combination Layer&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e4%bb%a3%e7%a0%81&quot;&gt;代码&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

&lt;p&gt;简称DCN。&lt;/p&gt;

&lt;p&gt;论文地址：&lt;a href=&quot;https://arxiv.org/abs/1708.05123&quot;&gt;deep &amp;amp; cross network for ad click predictions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在ctr预估这种场景中，有大量的离散和类别特征，所以特征空间非常大且稀疏。线性模型，例如logistic regression，简单，有可解释性，易扩展性，而交叉特征可以显著增加模型的表示能力。但这些组合往往需要人工的特征工程，或者exhaustive searching，而且，产生unseen的特征interactions非常困难。&lt;/p&gt;

&lt;p&gt;本文提出了cross network，可以使用自动的方式实现显式的特征交叉。cross network包括了多种层，其中最高度的interactions是由层的深度决定的。每一层会基于已有的部分产出高阶interactions，并保持前层的interactions。&lt;strong&gt;本文实现了cross network和dnn的联合训练。dnn能捕捉特征间非常复杂的interactions，但相比cross network而言，需要更多参数（几乎差一个数量级？an order of），而且难以显式地产出交叉特征，并且可能难以高效地学习特征的interactions。&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;related-work&quot;&gt;related work&lt;/h2&gt;

&lt;p&gt;为了避免extensive task-specific特征工程，主要有两类方法：embedding方法和神经网络。&lt;/p&gt;

&lt;h3 id=&quot;embedding方法&quot;&gt;embedding方法&lt;/h3&gt;

&lt;h4 id=&quot;factorization-machinesfms&quot;&gt;Factorization machines(FMs)&lt;/h4&gt;

&lt;p&gt;将离散特征映射到低维的稠密vector，然后通过vector的内积学习特征交互。&lt;/p&gt;

&lt;h4 id=&quot;field-aware-factorization-machinesffms&quot;&gt;Field-aware Factorization Machines(FFMs)&lt;/h4&gt;

&lt;p&gt;让每个feature可以学习更多的vectors，而每个vector与一个field有关。&lt;/p&gt;

&lt;h3 id=&quot;神经网络&quot;&gt;神经网络&lt;/h3&gt;

&lt;p&gt;如果有足够的隐层及隐单元，DNN能够近似任意函数。而现实问题所对应的函数往往不是『任意』的。&lt;/p&gt;

&lt;h2 id=&quot;贡献&quot;&gt;贡献&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;在cross network中，每一层都有feature crossing，能够学习交叉特征，并不需要人为设计的特征工程。&lt;/li&gt;
  &lt;li&gt;cross network简单有效。多项式度（polynomial degree）随着网络层数增加而增加&lt;/li&gt;
  &lt;li&gt;十分节约内存，且易于使用&lt;/li&gt;
  &lt;li&gt;DCN相比于其他模型有更出色的效果，与DNN模型相比，较少的参数却取得了较好的效果。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;网络结构&quot;&gt;网络结构&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/deepcross.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;embedding-and-stacking-layer&quot;&gt;Embedding and Stacking Layer&lt;/h3&gt;

&lt;p&gt;将sparse特征（例如类别型的特征，输入就是一个二进制的one-hot，如[0,1,0,0]）embedding成稠密向量：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x_{embed,i}=W_{embed,i}x_i
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{embed,i}\)&lt;/code&gt;是embedding后的向量，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;是第i个类目的二进制输入，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{embed,i}\in R^{n_e\times n_v}\)&lt;/code&gt;是一个embedding矩阵，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n_e\)&lt;/code&gt;是embedding的size，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n_v\)&lt;/code&gt;是词典的size。&lt;/p&gt;

&lt;p&gt;然后将embedding向量和归一化后的dense特征拼接成一个向量：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x_0=[x^T_{embed,1},...x^T_{embed,k},x^T_{dense}]
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;cross-network&quot;&gt;Cross Network&lt;/h3&gt;

&lt;p&gt;显式地进行feature crossing，每一个cross layer的公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x_{l+1}=x_0x^T_lw_l+b_l+x_l=f(x_l,w_l,b_l)+x_l
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_l,x_{l+1}\in R^d\)&lt;/code&gt;是第l和第l+1层的cross layer输出的列向量&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_l,b_l\in R^d\)&lt;/code&gt;是第l层的权重和bias&lt;/li&gt;
  &lt;li&gt;每一层的cross layer在进行完feature crossing的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f\)&lt;/code&gt;之后，又把input加回来&lt;/li&gt;
  &lt;li&gt;借鉴residual network的思想，映射函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f:R^d \mapsto R^d\)&lt;/code&gt;拟合残差&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{l+1}-x_{l}\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/deep-cross-cross-layer.png&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;可见，维数分别是dx1,1xd,dx1，最终输出得到的就是一个dx1的向量。所以第二项需要对上一层的输出转置成1xd。&lt;/p&gt;

&lt;p&gt;相当于考虑了从1到l+1阶的所有特征组合。&lt;/p&gt;

&lt;h4 id=&quot;复杂度分析&quot;&gt;复杂度分析&lt;/h4&gt;

&lt;p&gt;假设cross layer有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L_c\)&lt;/code&gt;层，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;是input的维数，那么，整个cross network的参数数量就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
d\times L_c \times 2
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这里乘了2，因为有w和b两个参数，每个参数在每一层里都是d维。时间和空间复杂度都是线性的。&lt;/p&gt;

&lt;h3 id=&quot;deep-network&quot;&gt;Deep Network&lt;/h3&gt;

&lt;p&gt;dnn部分就是正常的dnn：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
h_{l+1}=f(W_lh_l+b_l)
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_l \in R^{n_l},h_{l+1}\in R^{n_{l+1}}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_l\in R^{n_{l+1}\times n_l},b_l\in R^{n_{l+1}}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f\)&lt;/code&gt;是ReLU&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;复杂度分析-1&quot;&gt;复杂度分析&lt;/h4&gt;

&lt;p&gt;假设所有层的size一样大，都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m\)&lt;/code&gt;。假设层数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L_d\)&lt;/code&gt;，总参数数量为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
d\times m+m+(m^2+m)\times (L_d-1)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，第一层是和embedding的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;相连的，所以是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\times m+m\)&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;combination-layer&quot;&gt;Combination Layer&lt;/h3&gt;

&lt;p&gt;combination layer如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p=\sigma ([x^T_{L_1},h^T_{L_2}]w_{logits})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{L_1}\in R^d\)&lt;/code&gt;是cross network的输出，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{L_2}\in R^m\)&lt;/code&gt;是dnn的输出。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{logits}\in R^{(d+m)}\)&lt;/code&gt;是权重，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma (x)=1/(1+exp(-x))\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;损失函数是log loss加上l2正则：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
loss=-\frac{1}{N}\sum ^N_{i=1}y_ilog(p_i)+(1-y_i)log(1-p_i)+\lambda \sum _l\parallel w_l \parallel ^2
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;代码&quot;&gt;代码&lt;/h2&gt;

&lt;p&gt;看看网友用keras实现的版本&lt;a href=&quot;https://github.com/Nirvanada/Deep-and-Cross-Keras&quot;&gt;https://github.com/Nirvanada/Deep-and-Cross-Keras&lt;/a&gt;：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.backend&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.optimizers&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Adam&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Lambda&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.models&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.utils.vis_utils&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plot_model&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OneHotEncoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StandardScaler&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn.metrics&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accuracy_score&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# similar to https://github.com/jrzaurin/Wide-and-Deep-Keras/blob/master/wide_and_deep_keras.py
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;feature_generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cate_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cont_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;preprocessing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;embeddings_tensors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;continuous_tensors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cate_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;layer_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'_inp'&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# For categorical features, we em-bed the features in dense vectors of dimension 6×(category cardinality)**(1/4)
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nunique&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nunique&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nunique&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; \
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nunique&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t_inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_build&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nunique&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;embeddings_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cc&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cont_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;layer_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'_in'&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t_inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_build&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;continous_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;continuous_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inp_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;et&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embeddings_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inp_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ct&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;continuous_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inp_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;et&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embeddings_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inp_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ct&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;continuous_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inp_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inp_embed&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;embedding_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'int64'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;continous_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'float32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;



&lt;span class=&quot;c1&quot;&gt;# The optimal hyperparameter settings were 8 cross layers of size 54 and 6 deep layers of size 292 for DCN
# Embed &quot;Soil_Type&quot; column (embedding dim == 15), we have 8 cross layers of size 29   
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inp_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inp_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#inp_layer, inp_embed = feature_generate(X, cate_columns, cont_columns)
&lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inp_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'concat'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# deep layer
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;deep&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;272&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;deep&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;272&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# cross layer
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;cross&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CrossLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;cross_layer&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;#concat both layers
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'concat'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'softmax'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inp_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plot_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to_file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'model.png'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;show_shapes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Adam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'categorical_crossentropy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;accuracy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y_pred&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Accuracy: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;# https://keras.io/layers/writing-your-own-keras-layers/
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CrossLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_layer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_layer&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CrossLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initializer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'glorot_uniform'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'w_'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initializer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'zeros'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'b_'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trainable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;built&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;cross&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keepdims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;cross&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keepdims&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cross&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;compute_output_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;



&lt;span class=&quot;c1&quot;&gt;# modify the embedding columns here
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;preprocessing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# inverse transform one-hot to continuous column
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;df_onehot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tolist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Soil_Type&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#for i in df_onehot.columns.tolist():
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#    if df_onehot[i].sum() == 0:
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#        del df_onehot[i]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Soil&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;df_onehot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df_onehot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;drop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tolist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Soil_Type&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OneHotEncoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit_transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Cover_Type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;todense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Cover_Type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cate_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Soil&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cont_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Soil&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Feature normilization
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;scaler&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StandardScaler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data_cont&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scaler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit_transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cont_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cont_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data_cate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cate_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_cate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_cont&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cate_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cont_columns&lt;/span&gt;



&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# data download from https://www.kaggle.com/uciml/forest-cover-type-dataset/data
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./data/covtype.csv&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inp_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inp_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;#random split train and test by 9:1
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;train_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;test_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inp_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inp_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Lambda是把一个表达式转换成一个Layer：&lt;a href=&quot;https://keras.io/layers/core/#lambda&quot;&gt;https://keras.io/layers/core/#lambda&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;batch_dot是有两组vectors，每组有batch个vector,对他们进行点积&lt;a href=&quot;https://keras.io/backend/#backend-functions&quot;&gt;https://keras.io/backend/#backend-functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;简单示例：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x = [[1, 2], [3, 4]]&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y = [[5, 6], [7, 8]]&lt;/code&gt;那么，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;batch_dot(x, y, axes=1) = [[17], [53]]&lt;/code&gt;，也就是说一个2x2和一个2x2，对axes=1进行sum，得到的是一个2x1。如果是3维的，就比较复杂了：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_batch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_batch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xy_batch_dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xy_batch_dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;因为：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;x.shape[0] : 32 : append to output shape&lt;/li&gt;
  &lt;li&gt;x.shape[1] : 20 : do not append to output shape, dimension 1 of x has been summed over. (dot_axes[0] = 1)&lt;/li&gt;
  &lt;li&gt;x.shape[2] : 1 : 为啥被ignore了呢&lt;/li&gt;
  &lt;li&gt;y.shape[0] : 32 : do not append to output shape, always ignore first dimension of y&lt;/li&gt;
  &lt;li&gt;y.shape[1] : 30 : append to output shape&lt;/li&gt;
  &lt;li&gt;y.shape[2] : 20 : do not append to output shape, dimension 2 of y has been summed over. (dot_axes[1] = 2) output_shape = (100, 30)&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>激活函数</title>
   <link href="http://daiwk.github.io/posts/dl-activation-functions.html"/>
   <updated>2018-01-02T00:00:00+00:00</updated>
   <id>/posts/dl-activation-functions</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%B8%B8%E7%94%A8%E6%BF%80%E6%B4%BB%E5%87%BD%E6%95%B0&quot;&gt;常用激活函数&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-step&quot;&gt;1. Step&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-identity&quot;&gt;2. Identity&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3-relu&quot;&gt;3. ReLU&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4-sigmoid&quot;&gt;4. Sigmoid&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#5-tanh&quot;&gt;5. Tanh&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#6-leaky-relu&quot;&gt;6. Leaky Relu&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#7-prelu&quot;&gt;7. PReLU&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#8-rrelu&quot;&gt;8. RReLU&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#9-elu&quot;&gt;9. ELU&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#10-selu&quot;&gt;10. SELU&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#11-srelu&quot;&gt;11. SReLU&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-hard-sigmoid&quot;&gt;12. Hard Sigmoid&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-hard-tanh&quot;&gt;13. Hard Tanh&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#14-lecun-tanh&quot;&gt;14. LeCun Tanh&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#15-arctan&quot;&gt;15. ArcTan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#16-softsign&quot;&gt;16. SoftSign&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#17-softplus&quot;&gt;17. SoftPlus&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#18-signum&quot;&gt;18. Signum&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#19-bent-identity&quot;&gt;19. Bent Identity&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#20-symmetrical-sigmoid&quot;&gt;20. Symmetrical Sigmoid&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#21-log-log&quot;&gt;21. Log Log&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-gaussian&quot;&gt;22. Gaussian&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#23-absolute&quot;&gt;23. Absolute&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#24-sinusoid&quot;&gt;24. Sinusoid&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#25-cos&quot;&gt;25. Cos&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#26-sinc&quot;&gt;26. Sinc&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#nlp%E4%B8%AD%E7%9A%84%E6%BF%80%E6%B4%BB%E5%87%BD%E6%95%B0&quot;&gt;nlp中的激活函数&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jiqizhixin.com/articles/2017-10-10-3&quot;&gt;26种神经网络激活函数可视化&lt;/a&gt;，原文&lt;a href=&quot;https://dashee87.github.io/data%20science/deep%20learning/visualising-activation-functions-in-neural-networks/&quot;&gt;Visualising Activation Functions in Neural Networks&lt;/a&gt;【可以在里面选择不同的激活函数，看图】&lt;/p&gt;

&lt;h2 id=&quot;常用激活函数&quot;&gt;常用激活函数&lt;/h2&gt;

&lt;h3 id=&quot;1-step&quot;&gt;1. Step&lt;/h3&gt;

&lt;p&gt;激活函数 Step 更倾向于理论而不是实际，它模仿了生物神经元要么全有要么全无的属性。它无法应用于神经网络，因为其导数是 0（除了零点导数无定义以外），这意味着基于梯度的优化方法并不可行。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) =\begin{cases}1 &amp;amp; \text{for } x\geq0\\0 &amp;amp; \text{for } x&amp;lt;0\end{cases}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'(x) =\begin{cases}0 &amp;amp; \text{for } x\neq0\\? &amp;amp; \text{for } x=0\end{cases}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;2-identity&quot;&gt;2. Identity&lt;/h3&gt;

&lt;p&gt;通过激活函数 Identity，节点的输入等于输出。它完美适合于潜在行为是线性（与线性回归相似）的任务。当存在非线性，单独使用该激活函数是不够的，但它依然可以在最终输出节点上作为激活函数用于回归任务。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}f(x) = x\end{align*}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}f'(x) = 1\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;3-relu&quot;&gt;3. ReLU&lt;/h3&gt;

&lt;p&gt;修正线性单元（Rectified linear unit，ReLU）是神经网络中最常用的激活函数。它保留了 step 函数的生物学启发（只有输入超出阈值时神经元才激活），不过当输入为正的时候，导数不为零，从而允许基于梯度的学习（尽管在 x=0 的时候，导数是未定义的）。使用这个函数能使计算变得很快，因为无论是函数还是其导数都不包含复杂的数学运算。然而，当输入为负值的时候，ReLU 的学习速度可能会变得很慢，甚至使神经元直接无效，因为此时输入小于零而梯度为零，从而其权重无法得到更新，在剩下的训练过程中会一直保持静默。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) =\begin{cases}x &amp;amp; \text{for } x\geq0\\0 &amp;amp; \text{for } x&amp;lt;0\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'(x) =\begin{cases}1 &amp;amp; \text{for } x\geq0\\0 &amp;amp; \text{for } x&amp;lt;0\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_relu.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;4-sigmoid&quot;&gt;4. Sigmoid&lt;/h3&gt;

&lt;p&gt;Sigmoid 因其在 logistic 回归中的重要地位而被人熟知，值域在 0 到 1 之间。Logistic Sigmoid（或者按通常的叫法，Sigmoid）激活函数给神经网络引进了概率的概念。它的导数是非零的，并且很容易计算（是其初始输出的函数）。然而，在分类任务中，sigmoid 正逐渐被 Tanh 函数取代作为标准的激活函数，因为后者为奇函数（关于原点对称）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}f(x) = \frac{1}{1 + e^{-x}} \end{align*}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}f'(x) &amp;amp;= \frac{e^{-x}}{(1+e^{-x})^2} \\&amp;amp;= f(x)(1-f(x)) \end{align*}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_sigmoid.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;5-tanh&quot;&gt;5. Tanh&lt;/h3&gt;

&lt;p&gt;在分类任务中，双曲正切函数（Tanh）逐渐取代 Sigmoid 函数作为标准的激活函数，其具有很多神经网络所钟爱的特征。它是完全可微分的，反对称，对称中心在原点。为了解决学习缓慢和/或梯度消失问题，可以使用这个函数的更加平缓的变体（log-log、softsign、symmetrical sigmoid 等等）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}f(x)&amp;amp;=\tanh(x)\\&amp;amp;=\frac{2}{1+e^{-2x}}-1\end{align*}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}f'(x)&amp;amp;=1-\tanh^2(x)\\&amp;amp;=1-f(x)^2\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_tanh.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;6-leaky-relu&quot;&gt;6. Leaky Relu&lt;/h3&gt;

&lt;p&gt;经典（以及广泛使用的）ReLU 激活函数的变体，带泄露修正线性单元（Leaky ReLU）的输出对负值输入有很小的坡度。由于导数总是不为零，这能减少静默神经元的出现，允许基于梯度的学习（虽然会很慢）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) =\begin{cases}x &amp;amp; \text{for } x\geq0\\0.01x &amp;amp; \text{for } x&amp;lt;0\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'(x) =\begin{cases}1 &amp;amp; \text{for } x\geq0\\0.01 &amp;amp; \text{for } x&amp;lt;0\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_leaky_relu.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;7-prelu&quot;&gt;7. PReLU&lt;/h3&gt;

&lt;p&gt;参数化修正线性单元（Parameteric Rectified Linear Unit，PReLU）属于 ReLU 修正类激活函数的一员。它和 RReLU 以及 Leaky ReLU 有一些共同点，即为负值输入添加了一个线性项。而最关键的区别是，这个线性项的斜率实际上是在模型训练中学习到的。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f_{\alpha}(x) =\begin{cases}x &amp;amp; \text{for } x\geq0\\\alpha x &amp;amp; \text{for } x&amp;lt;0\end{cases}\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'_{\alpha}(x) =\begin{cases}1 &amp;amp; \text{for } x\geq0\\\alpha &amp;amp; \text{for } x&amp;lt;0\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_prelu.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;8-rrelu&quot;&gt;8. RReLU&lt;/h3&gt;

&lt;p&gt;随机带泄露的修正线性单元（Randomized Leaky Rectified Linear Unit，RReLU）也属于 ReLU 修正类激活函数的一员。和 Leaky ReLU 以及 PReLU 很相似，为负值输入添加了一个线性项。而最关键的区别是，这个线性项的斜率在每一个节点上都是&lt;strong&gt;随机分配&lt;/strong&gt;的（通常服从均匀分布）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) =\begin{cases}x &amp;amp; \text{for } x\geq0\\\alpha x &amp;amp; \text{for } x&amp;lt;0\end{cases}\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'(x) =\begin{cases}1 &amp;amp; \text{for } x\geq0\\\alpha &amp;amp; \text{for } x&amp;lt;0\end{cases}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_rrelu.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;9-elu&quot;&gt;9. ELU&lt;/h3&gt;

&lt;p&gt;指数线性单元（Exponential Linear Unit，ELU）也属于 ReLU 修正类激活函数的一员。和 PReLU 以及 RReLU 类似，为负值输入添加了一个非零输出。和其它修正类激活函数不同的是，它包括一个负指数项，从而防止静默神经元出现，导数收敛为零，从而提高学习效率。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) = \begin{cases}\alpha(e^x - 1) &amp;amp; \text{for } x &amp;lt; 0\\x &amp;amp; \text{for } x \ge 0\end{cases}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'(x) = \begin{cases} \alpha e^{x} = f(x) + \alpha &amp;amp; \text{for } x &amp;lt; 0\\1 &amp;amp; \text{for } x \ge 0\end{cases}    
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha = 0.7\)&lt;/code&gt;：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_elu_alpha0.7.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha = 1\)&lt;/code&gt;：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_elu_alpha1.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;注：
tensorflow中的实现就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha = 1\)&lt;/code&gt;的版本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/tensorflow/core/kernels/relu_op_functor.h&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Functor used by EluOp to do the computations.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Elu&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Computes Elu activation.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// features: any shape.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// activations: same shape as &quot;features&quot;.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// features.constant(?)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Functor used by EluGradOp to do the computations.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EluGrad&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Computes EluGrad backprops.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// gradients: gradients backpropagated to the Elu op.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// activations: outputs of the Elu op.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// backprops: gradients to backpropagate to the Elu inputs.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gradients&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;backprops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;backprops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gradients&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gradients&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;10-selu&quot;&gt;10. SELU&lt;/h3&gt;

&lt;p&gt;参考&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/27362891&quot;&gt;引爆机器学习圈：「自归一化神经网络」提出新型激活函数SELU&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://skyhigh233.com/blog/2017/07/21/norm/&quot;&gt;加速网络收敛——BN、LN、WN与selu&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/60910412&quot;&gt;如何评价 Self-Normalizing Neural Networks 这篇论文?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;paper: &lt;a href=&quot;https://arxiv.org/abs/1706.02515&quot;&gt;Self-Normalizing Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其实就是ELU乘了个lambda，关键在于这个lambda是大于1的。以前relu，prelu，elu这些激活函数，都是在负半轴坡度平缓，这样在activation的方差过大的时候可以让它减小，防止了梯度爆炸，但是正半轴坡度简单的设成了1。而selu的正半轴大于1，在方差过小的的时候可以让它增大，同时防止了梯度消失。这样激活函数就有一个不动点，网络深了以后每一层的输出都是均值为0方差为1。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) = \lambda \begin{cases}\alpha(e^x - 1) &amp;amp; \text{for } x &amp;lt; 0\\x &amp;amp; \text{for } x \ge 0 \end{cases}\\\text{ with } \lambda = 1.0507, \alpha = 1.67326
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'(x) = \begin{cases}\lambda\alpha e^{x} = (f(x) + \lambda \alpha) &amp;amp; \text{for } x &amp;lt; 0\\\lambda &amp;amp; \text{for } x \ge 0\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;tensorflow中的实现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/tensorflow/core/kernels/relu_op_functor.h&lt;/code&gt;(其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.0507 * 1.67326=1.758094&lt;/code&gt;)：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Functor used by SeluOp to do the computations.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Selu&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Computes Selu activation.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// features: any shape.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// activations: same shape as &quot;features&quot;.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// features.constant(?)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0507009873554804934193349852946&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale_alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.7580993408473768599402175208123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zero&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zero&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scale_alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Functor used by SeluGradOp to do the computations.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SeluGrad&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Computes SeluGrad backprops.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// gradients: gradients backpropagated to the Selu op.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// activations: outputs of the Selu op.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// backprops: gradients to backpropagate to the Selu inputs.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gradients&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;backprops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0507009873554804934193349852946&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale_alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.7580993408473768599402175208123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;backprops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;gradients&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale_alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gradients&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;11-srelu&quot;&gt;11. SReLU&lt;/h3&gt;

&lt;p&gt;S 型整流线性激活单元（S-shaped Rectified Linear Activation Unit，SReLU）属于以 ReLU 为代表的整流激活函数族。它由三个分段线性函数组成。其中两种函数的斜度，以及函数相交的位置会在模型训练中被学习。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f_{t_l,a_l,t_r,a_r}(x) = \begin{cases}t_l + a_l (x - t_l) &amp;amp; \text{for } x \le t_l\\x &amp;amp; \text{for } t_l &amp;lt; x &amp;lt; t_r\\t_r + a_r (x - t_r) &amp;amp; \text{for } x \ge t_r\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'_{t_l,a_l,t_r,a_r}(x) = \begin{cases}a_l &amp;amp; \text{for } x \le t_l\\1   &amp;amp; \text{for } t_l &amp;lt; x &amp;lt; t_r\\a_r &amp;amp; \text{for } x \ge t_r\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_srelu.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;12-hard-sigmoid&quot;&gt;12. Hard Sigmoid&lt;/h3&gt;

&lt;p&gt;Hard Sigmoid 是 Logistic Sigmoid 激活函数的分段线性近似。它更易计算，这使得学习计算的速度更快，尽管首次派生值为零可能导致静默神经元/过慢的学习速率（详见 ReLU）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) =\begin{cases}0 &amp;amp; \text{for } x&amp;lt;-2.5\\0.2x + 0.5 &amp;amp; \text{for } -2.5\geq x\leq 2.5 \\1 &amp;amp; \text{for } x&amp;gt;2.5\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) =\begin{cases}0 &amp;amp; \text{for } x&amp;lt;-2.5\\0.2 &amp;amp; \text{for } -2.5\geq x\leq 2.5 \\0 &amp;amp; \text{for } x&amp;gt;2.5\end{cases}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_hard_sigmoid.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;13-hard-tanh&quot;&gt;13. Hard Tanh&lt;/h3&gt;

&lt;p&gt;Hard Tanh 是 Tanh 激活函数的线性分段近似。相较而言，它更易计算，这使得学习计算的速度更快，尽管首次派生值为零可能导致静默神经元/过慢的学习速率（详见 ReLU）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) =\begin{cases}-1 &amp;amp; \text{for } x&amp;lt;-1\\x &amp;amp; \text{for } -1\geq x\leq 1 \\1 &amp;amp; \text{for } x&amp;gt;1\end{cases}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x) =\begin{cases}0 &amp;amp; \text{for } x&amp;lt;-1\\1 &amp;amp; \text{for } -1\geq x\leq 1 \\0 &amp;amp; \text{for } x&amp;gt;1\end{cases}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_hard_tanh.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;14-lecun-tanh&quot;&gt;14. LeCun Tanh&lt;/h3&gt;

&lt;p&gt;LeCun Tanh（也被称作 Scaled Tanh）是 Tanh 激活函数的扩展版本。它具有以下几个可以改善学习的属性：f(± 1) = ±1；二阶导数在 x=1 最大化；且有效增益接近 1。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}f(x)&amp;amp;=1.7519\tanh(\frac{2}{3}x)\end{align*}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}f'(x)&amp;amp;=1.7519*\frac{2}{3}(1-\tanh^2(\frac{2}{3}x))\\&amp;amp;=1.7519*\frac{2}{3}-\frac{2}{3*1.7519}f(x)^2\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_lecun_tanh.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;15-arctan&quot;&gt;15. ArcTan&lt;/h3&gt;

&lt;h3 id=&quot;16-softsign&quot;&gt;16. SoftSign&lt;/h3&gt;

&lt;p&gt;Softsign 是 Tanh 激活函数的另一个替代选择。就像 Tanh 一样，Softsign 是反对称、去中心、可微分，并返回-1 和 1 之间的值。其更平坦的曲线与更慢的下降导数表明它可以更高效地学习。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x)=\frac{x}{1+|x|}, f(x) \in (-1,1)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f'(x)=\frac{1}{(1+|x|)^2}, f'(x) \in (0,1]
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/activations_softsign.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;tensorflow实现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/tensorflow/core/kernels/softsign_op.h&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Functor used by SoftsignOp to do the computations.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Softsign&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Computes Softsign activation.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// features: any shape.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// activations: same shape as &quot;features&quot;.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;activations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Functor used by SoftsignGradOp to do the computations.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SoftsignGrad&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Computes SoftsignGrad backprops.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// gradients: gradients backpropagated to the Softsign op.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// features: inputs that were passed to the Softsign op.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// backprops: gradients to backpropagate to the Softsign inputs.&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Device&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gradients&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConstTensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TTypes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;backprops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;backprops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;gradients&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;17-softplus&quot;&gt;17. SoftPlus&lt;/h3&gt;

&lt;h3 id=&quot;18-signum&quot;&gt;18. Signum&lt;/h3&gt;

&lt;h3 id=&quot;19-bent-identity&quot;&gt;19. Bent Identity&lt;/h3&gt;

&lt;h3 id=&quot;20-symmetrical-sigmoid&quot;&gt;20. Symmetrical Sigmoid&lt;/h3&gt;

&lt;h3 id=&quot;21-log-log&quot;&gt;21. Log Log&lt;/h3&gt;

&lt;h3 id=&quot;22-gaussian&quot;&gt;22. Gaussian&lt;/h3&gt;

&lt;h3 id=&quot;23-absolute&quot;&gt;23. Absolute&lt;/h3&gt;

&lt;h3 id=&quot;24-sinusoid&quot;&gt;24. Sinusoid&lt;/h3&gt;

&lt;h3 id=&quot;25-cos&quot;&gt;25. Cos&lt;/h3&gt;

&lt;p&gt;注意区分：&lt;strong&gt;两个向量的cos相似度（例如作为loss_layer）&lt;/strong&gt;的求导：
&lt;a href=&quot;https://math.stackexchange.com/questions/1923613/partial-derivative-of-cosine-similarity&quot;&gt;https://math.stackexchange.com/questions/1923613/partial-derivative-of-cosine-similarity&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;26-sinc&quot;&gt;26. Sinc&lt;/h3&gt;

&lt;h2 id=&quot;nlp中的激活函数&quot;&gt;nlp中的激活函数&lt;/h2&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650756158&amp;amp;idx=2&amp;amp;sn=90cb49c49be078e7406539eb93561c9e&amp;amp;chksm=871a9040b06d195624b980cd8cac239887ae0b39613a1b7f0418f6228b5e1db00f99577c2d63&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0127lQKCh1CKGvDWJdAWzxYt&amp;amp;pass_ticket=G41MPQCwyEdqZu8V6sE4nTZKPFTsEYiPQ6jf7XdbxoHgTnGkbrCc5Wq3%2FP2Zg38k#rd&quot;&gt;21种NLP任务激活函数大比拼：你一定猜不到谁赢了&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1901.02671.pdf&quot;&gt;Is it Time to Swish? Comparing Deep Learning Activation Functions Across NLP tasks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/UKPLab/emnlp2018-activation-functions&quot;&gt;https://github.com/UKPLab/emnlp2018-activation-functions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在8种不同的NLP任务上进行了21种激活函数的大规模比较。&lt;/p&gt;

&lt;p&gt;对比了21种激活函数，其中 6 种是&lt;a href=&quot;https://arxiv.org/abs/1710.05941&quot;&gt;Searching for Activation Functions&lt;/a&gt;中「全新」提出的。&lt;/p&gt;

&lt;p&gt;另外14种激活函数分别是：tanh、sin、relu、lrelu0.01、lrelu-0.30、maxout-2、maxout-3、maxout4、prelu、linear、elu、cube、penalized tanh、selu。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;lrelu-0.01 和 lrelu0.30 是所谓的 leaky relu（LReLU）函数（Maas et al., 2013），它们背后的思想是避免在 relu 的负区域出现零激活/导数。表 1 也给出了它们的函数形式。&lt;/li&gt;
  &lt;li&gt;prelu（He et al., 2015）是对 LReLU 函数的泛化，是将其负区域中的斜率设置为一个可学习的参数。&lt;/li&gt;
  &lt;li&gt;maxout 函数（Goodfellow et al., 2013）的不同在于其引入了额外的参数，而且并不在单个标量输入上操作。比如 maxout-2 是取两个输入中的最大值的操作：max{xW+b, xV+c}，因此可学习参数的数量多一倍。maxout-3 类似，是取三个输入中的最大值。如 Goodfellow et al. (2013) 所示，maxout 可以近似任意凸函数。&lt;/li&gt;
  &lt;li&gt;sin 是标准的正弦函数，被提出用于神经网络学习，比如 Parascandolo et al. (2016)，其中表明 sin 在特定任务上的学习速度比更有地位的函数快。&lt;/li&gt;
  &lt;li&gt;penalized tanh（Xu et al., 2016）的定义与 LReLU 函数类似，可被看作是「惩罚」负区域中的恒等函数。penalized tanh 在 CIFAR-100 上报告的优良表现（Krizhevsky, 2009）让作者推测：激活函数在原点附近的斜率可能对学习至关重要。&lt;/li&gt;
  &lt;li&gt;linear 是恒等函数 f(x) = x。&lt;/li&gt;
  &lt;li&gt;cube 是立方函数 f(x) = x³，由 Chen and Manning (2014) 为依赖关系分析所用的一个 MLP 提出。&lt;/li&gt;
  &lt;li&gt;elu（Clevert et al., 2015）是 relu 的又一种变体，其假设了负值，使平均激活更以零为中心。&lt;/li&gt;
  &lt;li&gt;selu 是 elu 的一种扩展版，Klambauer et al. (2017) 将其用在了所谓的自归一化神经网络背景中。&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>新型机器翻译</title>
   <link href="http://daiwk.github.io/posts/nlp-new-nmt.html"/>
   <updated>2017-12-26T00:00:00+00:00</updated>
   <id>/posts/nlp-new-nmt</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%9f%ba%e4%ba%8egan%e7%9a%84%e6%9c%ba%e5%99%a8%e7%bf%bb%e8%af%91&quot;&gt;基于GAN的机器翻译&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#seqgan&quot;&gt;seqgan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#adversarial-nmt&quot;&gt;Adversarial-NMT&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%97%a0%e7%9b%91%e7%9d%a3%e6%9c%ba%e5%99%a8%e7%bf%bb%e8%af%91&quot;&gt;无监督机器翻译&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#muse&quot;&gt;MUSE&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%9f%ba%e4%ba%8e%e7%9f%ad%e8%af%ad%e5%92%8c%e7%a5%9e%e7%bb%8f%e7%9a%84%e6%97%a0%e7%9b%91%e7%9d%a3%e6%9c%ba%e5%99%a8%e7%bf%bb%e8%af%91&quot;&gt;基于短语和神经的无监督机器翻译&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;基于gan的机器翻译&quot;&gt;基于GAN的机器翻译&lt;/h2&gt;

&lt;p&gt;汇总&lt;a href=&quot;https://zhuanlan.zhihu.com/p/30788930&quot;&gt;https://zhuanlan.zhihu.com/p/30788930&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;seqgan&quot;&gt;seqgan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1609.05473&quot;&gt;SeqGAN: Sequence Generative Adversarial Nets with Policy Gradient&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/rl-seqgan.html&quot;&gt;https://daiwk.github.io/posts/rl-seqgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;adversarial-nmt&quot;&gt;Adversarial-NMT&lt;/h3&gt;

&lt;p&gt;参考知乎文章：&lt;a href=&quot;https://zhuanlan.zhihu.com/p/26661225&quot;&gt;https://zhuanlan.zhihu.com/p/26661225&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1704.06933.pdf&quot;&gt;Adversarial Neural Machine Translation&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;无监督机器翻译&quot;&gt;无监督机器翻译&lt;/h2&gt;

&lt;h3 id=&quot;muse&quot;&gt;MUSE&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1710.04087&quot;&gt;Word Translation Without Parallel Data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1711.00043&quot;&gt;Unsupervised Machine Translation With Monolingual Data Only&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebookresearch/MUSE&quot;&gt;https://github.com/facebookresearch/MUSE&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;基于短语和神经的无监督机器翻译&quot;&gt;基于短语和神经的无监督机器翻译&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650741621&amp;amp;idx=4&amp;amp;sn=210c9b3694f56508267e95235fb99f3c&amp;amp;chksm=871adf0bb06d561dc99a89a5f56cf835bc92aa25401294216262128402490918d2787fe7fd09&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0429xcZSkAituLbHIsmgNBAb&amp;amp;pass_ticket=INCrGaryVZRn7Xp0qFQ7uod1VN14o8mkpvq1bswtroEgKQavvDm7mmg4E7yTOH6d#rd&quot;&gt;学界 | FAIR新一代无监督机器翻译：模型更简洁，性能更优&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1804.07755.pdf&quot;&gt;Phrase-Based &amp;amp; Neural Unsupervised Machine Translation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;机器翻译系统在某些语言上取得了接近人类水平的性能，但其有效性在很大程度上依赖&lt;strong&gt;大量双语文本&lt;/strong&gt;，这降低了机器翻译系统在大多数语言对中的适用性。本研究探讨了如何在&lt;strong&gt;只有大型单语语料库&lt;/strong&gt;的情况下进行机器翻译。&lt;/p&gt;

&lt;p&gt;在广泛使用的 WMT’14 英法和 WMT’16 德英基准测试中，我们的模型在不使用平行语句的情况下分别获得 27.1 和 23.6 的 BLEU 值，比当前最优技术高 11 个 BLEU 点。&lt;/p&gt;

&lt;p&gt;大量文献研究了在&lt;strong&gt;有限的监督&lt;/strong&gt;下使用&lt;strong&gt;单语数据&lt;/strong&gt;来提升翻译性能的问题。这种有限的监督通常是以下形式：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一小组平行句子（Sennrich et al., 2015a; Gulcehre et al., 2015; He et al., 2016; Gu et al., 2018; Wang et al., 2018）&lt;/li&gt;
  &lt;li&gt;使用其他相关语言的一大组平行句子（Firat et al., 2016; Johnson et al., 2016; Chen et al., 2017; Zheng et al., 2017）&lt;/li&gt;
  &lt;li&gt;双语词典（Klementiev et al., 2012; Irvine and Callison-Burch, 2014, 2016）&lt;/li&gt;
  &lt;li&gt;可比语料库（Munteanu et al., 2004; Irvine and Callison-Burch, 2013）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;最近，研究者提出了两种&lt;strong&gt;完全无监督&lt;/strong&gt;的方法（Lample et al., 2018; Artetxe et al., 2018），&lt;strong&gt;仅依赖于每种语言&lt;/strong&gt;的&lt;strong&gt;单语语料库&lt;/strong&gt;，如 Ravi 和 Knight（2011）的开创性研究。&lt;/p&gt;

&lt;p&gt;虽然这两项研究存在细微的技术差异，但我们发现了它们成功的几个共同因素:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;它们使用&lt;strong&gt;推断的双语词典&lt;/strong&gt;仔细完成模型的&lt;strong&gt;初始化&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;它们利用强大的语言模型，通过训练&lt;strong&gt;序列到序列&lt;/strong&gt;的系统（Sutskever et al., 2014; Bahdanau et al., 2015）作为&lt;strong&gt;去噪自编码器&lt;/strong&gt;（Vincent et al., 2008）&lt;/li&gt;
  &lt;li&gt;通过&lt;strong&gt;回译&lt;/strong&gt;自动&lt;strong&gt;生成句对&lt;/strong&gt;，将无监督问题&lt;strong&gt;转化为监督问题&lt;/strong&gt;（Sennrich et al., 2015a）回译过程的关键是维护&lt;strong&gt;两个模型&lt;/strong&gt;，一个&lt;strong&gt;将源语翻译成目标语言&lt;/strong&gt;，另一个则是&lt;strong&gt;从目标语言生成源语言&lt;/strong&gt;。前者生成数据，用于训练后者，反之亦然。&lt;/li&gt;
  &lt;li&gt;这些模型限制编码器产生的、在两种语言之间共享的潜在表征。将这些片段放在一起，无论输入语言是什么，编码器都会产生类似的表征。解码器既作为语言模型又作为噪声输入的翻译器进行训练，它学习与后向模型（从目标语到源语的操作）一起产生越来越好的译文。这种&lt;strong&gt;迭代过程&lt;/strong&gt;在完全无监督的环境中取得了良好的结果，例如，它在 WMT’14 英法基准测试中的 BLEU 值达到了～15。&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>特征工程</title>
   <link href="http://daiwk.github.io/posts/ml-feature-engineering.html"/>
   <updated>2017-11-21T00:00:00+00:00</updated>
   <id>/posts/ml-feature-engineering</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-工具包&quot;&gt;0. 工具包&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-概述&quot;&gt;1. 概述&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-数据预处理单一特征&quot;&gt;1.1 数据预处理(单一特征)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-特征选择多特征&quot;&gt;1.2 特征选择(多特征)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-降维多特征&quot;&gt;1.3 降维(多特征)&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-数据预处理&quot;&gt;2. 数据预处理&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-特征离散化的好处&quot;&gt;2.1 特征离散化的好处&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-特征常见统计指标&quot;&gt;2.2 特征常见统计指标&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-特征选择&quot;&gt;3. 特征选择&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-特征间的统计指标&quot;&gt;3.1 特征间的统计指标&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-特征选择的形式&quot;&gt;3.2 特征选择的形式&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-降维&quot;&gt;4. 降维&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-sklearn小技巧&quot;&gt;5. sklearn小技巧&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-tf的特征工程&quot;&gt;6. tf的特征工程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-paddle的ctr特征demo&quot;&gt;7. paddle的ctr特征demo&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#sk-dist&quot;&gt;sk-dist&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#自动化特征工程&quot;&gt;自动化特征工程&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;0-工具包&quot;&gt;0. 工具包&lt;/h2&gt;

&lt;p&gt;代码: &lt;a href=&quot;https://github.com/abhayspawar/featexp&quot;&gt;https://github.com/abhayspawar/featexp&lt;/a&gt;
说明: &lt;a href=&quot;https://mp.weixin.qq.com/s/SDCon7Uy-E4NlLN8JxzkFg&quot;&gt;业界 | 如何达到Kaggle竞赛top 2%？这里有一篇特征探索经验帖&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-概述&quot;&gt;1. 概述&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/jasonfreak/p/5448385.html&quot;&gt;https://www.cnblogs.com/jasonfreak/p/5448385.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本质是一项工程活动，目的是最大限度地从原始数据中提取特征以供算法和模型使用。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/feature_engineering.jpg&quot; style=&quot;max-height: 450px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;11-数据预处理单一特征&quot;&gt;1.1 数据预处理(单一特征)&lt;/h3&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;库&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;所属方法&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;说明&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;preprocessing.StandardScaler&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;无量纲(dimension)化&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;标准化，基于特征矩阵的列，将特征值转换至服从标准正态分布&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;preprocessing.MinMaxScaler&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;无量纲(dimension)化&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;区间缩放，基于最大最小值，将特征值转换到[0, 1]区间上&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;preprocessing.Normalizer&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;归一化&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;基于特征矩阵的行，将样本向量转换为『单位向量』&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;preprocessing.Binarizer&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;二值化&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;基于给定阈值，将定量特征按阈值划分&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;preprocessing.OneHotEncoder&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;one-hot编码&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;将定性数据转换为定量数据 &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;preprocessing.Imputer&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;缺失值计算&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;计算缺失值，填充为均值等&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;preprocessing.PolynomialFeatures&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;多项式数据转换&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;多项式数据转换&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;preprocessing.FunctionTransformer&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;自定义单元数据转换&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;使用单变元的函数来转换数据&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;p&gt;简单的方差的方法&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
D(x) = E(x^2)-[E(x)]^2
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以用python来写就是：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;narray&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sum1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;narray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;narray2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;narray&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;narray&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sum2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;narray2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stdv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;12-特征选择多特征&quot;&gt;1.2 特征选择(多特征)&lt;/h3&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;库&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;所属方法&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;说明&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;feature_selection.VarianceThreshold&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Filter&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;方差选择法&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;feature_selection.SelectKBest&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Filter&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;常用函数
&lt;br /&gt;
&lt;li&gt;pearson相关系数（scipy.stats.pearsonr）&lt;/li&gt;
&lt;li&gt;卡方检验(sklearn.feature_selection.chi2)&lt;/li&gt;
&lt;li&gt;互信息(minepy.MINE)&lt;/li&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;feature_selection.RFE&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Wrapper&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;特征递归消除法（递归地训练基模型[例如，sklearn.linear_model.LogisticRegression]，将权值系数较小的特征从特征集合中消除）&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;feature_selection.SelectFromModel&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Embeded&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;训练基模型，选择权值系数较高的特征
&lt;li&gt;基于惩罚项的特征选择(结合sklearn.linear_model.LogisticRegression)&lt;/li&gt;
&lt;li&gt;基于树模型的特征选择(结合sklearn.ensemble.GradientBoostingClassifier)&lt;/li&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;h3 id=&quot;13-降维多特征&quot;&gt;1.3 降维(多特征)&lt;/h3&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;库&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;所属方法&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;说明&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;decomposition.PCA&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;  PCA&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;主成分分析，为了让映射后的样本有最大的发散性，是一种无监督的降维方法。&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;lda.LDA&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;  LDA&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;线性判别分析法，为了让映射后的样本有最好的分类性能，是一种有监督的降维方法。&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;h2 id=&quot;2-数据预处理&quot;&gt;2. 数据预处理&lt;/h2&gt;

&lt;h3 id=&quot;21-特征离散化的好处&quot;&gt;2.1 特征离散化的好处&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;稀疏向量内积乘法运算速度快，计算结果方便存储，易扩展&lt;/li&gt;
  &lt;li&gt;离散化后的特征对异常数据有很强的鲁棒性&lt;/li&gt;
  &lt;li&gt;单变量离散化为n个后，每个特征有独立的权重，相当于多个特征，引入非线性&lt;/li&gt;
  &lt;li&gt;离散化后再进行特征组合，如1-&amp;gt;m，1-&amp;gt;n，那组合就有m*n&lt;/li&gt;
  &lt;li&gt;离散化后，模型会更稳定。和第二点类似。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;22-特征常见统计指标&quot;&gt;2.2 特征常见统计指标&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;数据量大小，最大最小值，distinct(特征下不同值的个数)&lt;/li&gt;
  &lt;li&gt;平均值、中位数、众数&lt;/li&gt;
  &lt;li&gt;方差、变异系数(Coefficient of Variation，标准差/平均值，变异系数是一个无量纲量，因此在比较两组量纲不同或均值不同的数据时，应该用变异系数而不是标准差来作为比较的参考)&lt;/li&gt;
  &lt;li&gt;熵(不确定性的度量，不确定性越强、越混乱，熵越大)、偏度(skewness，所有取值分布的对称性)、峰度(Kurtosis，所有取值分布形态陡缓程度)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;3-特征选择&quot;&gt;3. 特征选择&lt;/h2&gt;

&lt;p&gt;两个考虑点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;特征是否发散：如果不发散，即方差接近0，说明样本在这个特征上基本没差异，这个特征对于样本的区分没什么作用==&amp;gt;特征间彼此相关性弱的是好的特征&lt;/li&gt;
  &lt;li&gt;特征与目标的相关性：除方差法，其他方法均从相关性角度考虑。==&amp;gt;与目标相关性强的特征是好的特征&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;31-特征间的统计指标&quot;&gt;3.1 特征间的统计指标&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;连续x连续：pearson相关系数、spearman相关系数&lt;/li&gt;
  &lt;li&gt;分类x分类：卡方独立性检验(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X^2\)&lt;/code&gt;越小，说明变量越独立，越大越相关)&lt;/li&gt;
  &lt;li&gt;连续x分类：
    &lt;ul&gt;
      &lt;li&gt;针对两组特征：t检验(检测数据的准确度，系统误差，类似bias，和真实值的差距，z检验也类似，但z检验需要知道总体方差，不容易知道，所以常用t检验)、F检验(检测数据的精密度，偶然误差，类似variance，方差)&lt;/li&gt;
      &lt;li&gt;针对多于两组特征：ANOVA&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;最大信息系数（Mutual information and maximal information coefficient，MIC）：缺点：值域[0,1]，当零假设不成立时，MIC的统计会受影响。&lt;/li&gt;
  &lt;li&gt;信息增益(待分类的集合的熵和选定某个特征的条件熵之差，参考&lt;a href=&quot;https://www.cnblogs.com/fantasy01/p/4581803.html&quot;&gt;https://www.cnblogs.com/fantasy01/p/4581803.html&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;多特征距离的计算（欧式距离、标准化欧式距离、余弦、jaccard距离、KL散度）&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;32-特征选择的形式&quot;&gt;3.2 特征选择的形式&lt;/h3&gt;

&lt;p&gt;根据特征选择的形式，可以分为以下三种方法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Filter: 过滤法，按照发散性或者相关性对各个特征进行评分，设定阈值或者待选择阈值的个数，选择特征&lt;/li&gt;
  &lt;li&gt;Wrapper：包装法，根据目标函数（通常是预测效果评分），每次选择若干特征，或者排除若干特征。中间的搜索方法有完全搜索、启发式搜索、随机搜索&lt;/li&gt;
  &lt;li&gt;Embedded：嵌入法，先使用某些机器学习算法或者模型进行训练 ，得到和个特征的权值系数，根据系数从大到小特征特征。
    &lt;ul&gt;
      &lt;li&gt;正则化：L1：不稳定，受噪声影响大，因为表达能力差的特征系数为0；L2：更稳定，因为表达能力强的特征的系数非零&lt;/li&gt;
      &lt;li&gt;树模型&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;比如有10个特征，然后你有一个模型a用这10个特征来预测xxx&lt;/p&gt;

&lt;p&gt;wrapper我理解就是10个特征过这个模型，随机扔掉一些，看单特征的效果（类似所谓的的特征重要度）；&lt;/p&gt;

&lt;p&gt;embedded我理解是另外拿一个模型，目标可能不一定是你要预测的xxx，通过那个模型，来给每个特征学一个权重，然后筛选特征，剩下的特征再用a去走（例如模型是个复杂dnn，可以先用lr/gbdt，输入这些特征，去学某个label，然后学到权重用来筛选特征吧）&lt;/p&gt;

&lt;h2 id=&quot;4-降维&quot;&gt;4. 降维&lt;/h2&gt;

&lt;p&gt;pca.explained_variaance_ratio_：percentage of variance explained by each of the selected components.&lt;/p&gt;

&lt;h2 id=&quot;5-sklearn小技巧&quot;&gt;5. sklearn小技巧&lt;/h2&gt;

&lt;p&gt;参考 &lt;a href=&quot;http://www.cnblogs.com/jasonfreak/p/5448462.html&quot;&gt;http://www.cnblogs.com/jasonfreak/p/5448462.html&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;包&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;类或方法&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;说明&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;sklearn.pipeline&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Pipeline&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;流水线处理&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;sklearn.pipeline&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;FeatureUnion&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;并行处理&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;sklearn.grid_search&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;GridSearchCV&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;网格搜索调参&lt;/td&gt;
&lt;/tr&gt;


&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;h2 id=&quot;6-tf的特征工程&quot;&gt;6. tf的特征工程&lt;/h2&gt;

&lt;p&gt;官方文档：&lt;a href=&quot;https://www.tensorflow.org/guide/feature_columns?hl=zh-cn&quot;&gt;https://www.tensorflow.org/guide/feature_columns?hl=zh-cn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/cjopengler/article/details/78161748&quot;&gt;https://blog.csdn.net/cjopengler/article/details/78161748&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;同样地，参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/41663141&quot;&gt;https://zhuanlan.zhihu.com/p/41663141&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;通过调用tf.feature_column模块来创建feature columns。有两大类feature column&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一类是生成dense tensor的Dense Column。包括：
    &lt;ul&gt;
      &lt;li&gt;numerical_column&lt;/li&gt;
      &lt;li&gt;indicator_column：one-hot或者multi-hot的sparsetensor&lt;/li&gt;
      &lt;li&gt;embedding_column：每个bucket对应一个emb向量，如果是multi-hot可以指定combine方式，如sum，avg等&lt;/li&gt;
      &lt;li&gt;shared_embedding_columns：有多个特征可能需要共享相同的embeding映射空间，比如用户历史行为序列中的商品ID和候选商品ID&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;另一类是生成sparse tensor的Categorical Column。包括：
    &lt;ul&gt;
      &lt;li&gt;categorical_column_with_identity：和bucketize类似，也是用一个单个的唯一值表示bucket。&lt;/li&gt;
      &lt;li&gt;categorical_column_with_vocabulary_file：通过一个文件的名字来one-hot&lt;/li&gt;
      &lt;li&gt;categorical_column_with_vocabulary_list：通过一个list的名字来one-hot&lt;/li&gt;
      &lt;li&gt;categorical_column_with_hash_bucket：通过hash的方式来得到最终的类别ID&lt;/li&gt;
      &lt;li&gt;crossed_column：特征先进行笛卡尔积，再hash&lt;/li&gt;
      &lt;li&gt;weighted_categorical_column：&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;还有bucketized_column，可以生成Categorical Column也可以生成Dense Column：把numeric column的值按照提供的边界（boundaries)离散化为多个值&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用indicator_column能把categorical column得到的稀疏tensor转换为one-hot或者multi-hot形式的稠密tensor&lt;/p&gt;

&lt;p&gt;demo：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow.python.feature_column.feature_column&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_LazyBuilder&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_shared_embedding_column_with_hash_bucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'color'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
                  &lt;span class=&quot;s&quot;&gt;'color2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]}&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 4行样本
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_LazyBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_column&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;categorical_column_with_hash_bucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'color'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_column_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_get_sparse_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_column2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;categorical_column_with_hash_bucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'color2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_column_tensor2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_column2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_get_sparse_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'not use input_layer'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'_'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_column_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_column_tensor2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 将稀疏的转换成dense，也就是one-hot形式，只是multi-hot
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;color_column_embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared_embedding_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_column2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;combiner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sum'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_column_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_dense_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_column_embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'use input_layer'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'_'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_dense_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;test_shared_embedding_column_with_hash_bucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;还有：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_categorical_column_with_vocabulary_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'color'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'G'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'B'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'G'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'A'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'A'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]}&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 4行样本
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_LazyBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_column&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;categorical_column_with_vocabulary_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'color'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'G'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'B'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;color_column_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_get_sparse_tensors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_column_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 将稀疏的转换成dense，也就是one-hot形式，只是multi-hot
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;color_column_identy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;indicator_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;color_dense_tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_column_identy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'use input_layer'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'_'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_dense_tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;test_categorical_column_with_vocabulary_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;对于多个取值特征进行feature crossing。。。参考&lt;a href=&quot;https://github.com/Lapis-Hong/wide_deep/blob/master/python/lib/dataset.py#L152&quot;&gt;https://github.com/Lapis-Hong/wide_deep/blob/master/python/lib/dataset.py#L152&lt;/a&gt;的掉渣天的代码：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;n&quot;&gt;_CSV_COLUMNS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;ad_account_id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;education&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;bid&quot;&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;show&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;_CSV_COLUMN_DEFAULTS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'-1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'-1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ad_product_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;categorical_column_with_hash_bucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;'ad_product_id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_bucket_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;base_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ad_account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;crossed_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;crossed_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'ad_product_id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'education_cross2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;hash_bucket_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;wide_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crossed_columns&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;deep_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;bid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_epochs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;parse_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Parsing {}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decode_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;record_defaults&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_CSV_COLUMN_DEFAULTS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_CSV_COLUMNS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# features = {&quot;bid&quot;: csv_decode_obj, &quot;show&quot;: csv_decode_obj, ...}
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;education_cross2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delimiter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'show'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然而要batch的时候，还是会有点问题的，所以需要改成(注意！！padded_shape如果传入第一个元素的是个字典，会按key排序的，也就是”bid”会排在”age”的后面！！)：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;_CSV_COLUMNS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;ad_account_id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;education&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;age&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;bid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;show&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;_CSV_COLUMN_DEFAULTS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'-1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'-1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'-1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ad_product_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;categorical_column_with_hash_bucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;s&quot;&gt;'ad_product_id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_bucket_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;base_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ad_account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;crossed_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;crossed_column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'ad_product_id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'education'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;hash_bucket_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;wide_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;crossed_columns&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;deep_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;bid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;input_fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_epochs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;parse_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Parsing {}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decode_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;record_defaults&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_CSV_COLUMN_DEFAULTS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_CSV_COLUMNS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# features = {&quot;bid&quot;: csv_decode_obj, &quot;show&quot;: csv_decode_obj, ...}
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;education&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delimiter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;age&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;delimiter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'show'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Extract lines from input files using the Dataset API.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TextLineDataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shuffle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_NUM_EXAMPLES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'train'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse_csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_parallel_calls&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;g_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_CSV_COLUMNS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;padded_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;padded_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;age&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 如果设成某个数，比如我想pad成长度5的之类的。。会出现『Attempted to pad to a smaller size than the input element.』的错。。比较蛋疼
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;padded_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;education&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pred&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
      &lt;span class=&quot;n&quot;&gt;padded_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;padded_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;padded_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;padded_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;padded_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padded_shapes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;padded_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#, drop_remainder=True)#.filter(lambda fea,lab: tf.equal(tf.shape(lab)[0], batch_size))
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# We call repeat after shuffling, rather than before, to prevent separate
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# epochs from blending together.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_epochs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;##  dataset = dataset.batch(batch_size) ## 如果有变长的，就不能用这个啦，要用上面的padded_batch
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;另外，在&lt;a href=&quot;https://github.com/daiwk/grace_t/tree/master/python/grace_t/basic_demos&quot;&gt;https://github.com/daiwk/grace_t/tree/master/python/grace_t/basic_demos&lt;/a&gt;这里有些实践和尝试。。&lt;/p&gt;

&lt;h2 id=&quot;7-paddle的ctr特征demo&quot;&gt;7. paddle的ctr特征demo&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/32699487&quot;&gt;https://zhuanlan.zhihu.com/p/32699487&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/PaddlePaddle/models/blob/develop/PaddleRec/ctr/preprocess.py&quot;&gt;https://github.com/PaddlePaddle/models/blob/develop/PaddleRec/ctr/preprocess.py&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CategoryDictGenerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    Generate dictionary for each of the categorical features
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;defaultdict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datafile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;categorial_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cutoff&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datafile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'r'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;categorial_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;categorial_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cutoff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                   &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;vocabs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocabs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocabs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;unk&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&amp;lt;unk&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dicts_sizes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ContinuousFeatureGenerator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    Normalize the integer features to [0, 1] by min-max normalization
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datafile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;continous_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datafile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'r'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_feature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;continous_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;continous_clip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;continous_clip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;sk-dist&quot;&gt;sk-dist&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/mjQuJoP4VIZ2yHToX19aMA&quot;&gt;将sklearn训练速度提升100多倍，美国「返利网」开源sk-dist框架&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/Ibotta/sk-dist&quot;&gt;https://github.com/Ibotta/sk-dist&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;自动化特征工程&quot;&gt;自动化特征工程&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/hNh1PXKZqeSZ9gDt1uZKwA&quot;&gt;HyperparameterHunter 3.0：一文教你学会自动化特征工程&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HunterMcGushion/hyperparameter_hunter&quot;&gt;https://github.com/HunterMcGushion/hyperparameter_hunter&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>pointer network与copynet</title>
   <link href="http://daiwk.github.io/posts/dl-pointer-network-copynet.html"/>
   <updated>2017-11-21T00:00:00+00:00</updated>
   <id>/posts/dl-pointer-network-copynet</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#copynet&quot;&gt;CopyNet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pointer-network&quot;&gt;Pointer Network&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.sohu.com/a/140364283_297288&quot;&gt;干货 | Pointer Network 与 CopyNet 之上有哪些新颖任务&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;[1]&lt;a href=&quot;http://aclweb.org/anthology/P16-1154&quot;&gt;Incorporating Copying Mechanism in Sequence-to-Sequence Learning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[2]&lt;a href=&quot;https://arxiv.org/abs/1603.08148&quot;&gt;Pointing the Unknown Words&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[3]&lt;a href=&quot;https://arxiv.org/abs/1611.09235&quot;&gt;Joint Copying and Restricted Generation for Paraphrase&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[4]&lt;a href=&quot;http://www.nlpr.ia.ac.cn/cip/shizhuhe/articles/acl2017-coreqa.pdf&quot;&gt;Generating Natural Answer by Incorporating Copying and Retrieving Mechanisms in Sequence-to-Sequence Learning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;[5]&lt;a href=&quot;https://arxiv.org/abs/1705.04146&quot;&gt;Program Induction by Rationale Generation:Learning to Solve and Explain Algebraic Word Problems&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;首先讲前两篇ACL2016的文章，为了解决如何把 sequence2sequence任务（不只是机器翻译 MT）中的某些信息“原封不动”地保留下来（而不是翻译）而提出的。&lt;/p&gt;

&lt;h2 id=&quot;copynet&quot;&gt;CopyNet&lt;/h2&gt;

&lt;p&gt;作者提出的网络叫做 CopyNet，提出的机制叫做 Copying Mechanism。&lt;/p&gt;

&lt;p&gt;其实就是说，比如我们在对话过程中，会出现“你好啊，我叫小S”，然后另一方要回答，“很高兴认识啊小S。”那么这个小S，也就是说&lt;strong&gt;实体信息&lt;/strong&gt;或者&lt;strong&gt;日期信息&lt;/strong&gt;等等，就是在对话的 input-output 中，被“复制”的。这个信息是应该“原封不动”地被保留，从 sequence2sequence 的输入端被复制到输出端的。现有的 end2end sequence2sequence 等模型，即使加了 attention 机制，也很难做到这点。&lt;/p&gt;

&lt;p&gt;难点主要有两个:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一是要判断输入端中&lt;strong&gt;哪个/些信息，即sub-sequences&lt;/strong&gt;是应该被“复制”的&lt;/li&gt;
  &lt;li&gt;二是要决定在&lt;strong&gt;输出端的哪个地方粘贴&lt;/strong&gt;这些信息。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/copynet.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;CopyNet 的框架如上图。主要结构还是基于 Bahdanau 2014 提出的&lt;strong&gt;attention-based encoder-decoder&lt;/strong&gt;框架。但是，在&lt;strong&gt;decoder部分&lt;/strong&gt;，CopyNet 做出了一些改进：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;因为要决定是“粘贴”还是选择“生成”，有一个由概率建模的&lt;strong&gt;generate-mode&lt;/strong&gt;和&lt;strong&gt;copy-mode&lt;/strong&gt;。由于copy-mode的加入，&lt;strong&gt;decoder可以产生出一些 OOV words（直接“复制”过来）&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;可能是收到 Neural Turing Machine（NTM）的启发，他们把&lt;strong&gt;decoder部分的hidden state计算变成了location-specific&lt;/strong&gt;的，这个改进的好处是更能让模型去注意&lt;strong&gt;要『复制』的&lt;/strong&gt;那些信息所在的&lt;strong&gt;输入端的位置&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;如果说把（2）的改进看做一种&lt;strong&gt;seletive read&lt;/strong&gt;，再加上attention-based encoder-decoder本身的&lt;strong&gt;attentive read&lt;/strong&gt;，这两种机制就更需要比较好的&lt;strong&gt;hybrid coordination&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这样一来，CopyNet 就能比较好地『复制』出一条&lt;strong&gt;完整的subsequences&lt;/strong&gt;，而不是只复制了片段。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/copynet-decoder.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在实验部分，CopyNet 被重点应用在了&lt;strong&gt;text summarization&lt;/strong&gt;的任务上。实验结果非常不错，case study 也给出了很多分析，比如它是完美解决 OOV 问题的。从某个角度来说，CopyNet&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一方面可以保留『复制』输入端（原始文章）中的重要信息，&lt;/li&gt;
  &lt;li&gt;另一方面又可以在输出端生成一些和原始文章措辞不同的摘要&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;也就可以把CopyNet认为是一种&lt;strong&gt;extractive和abstractive summarization的结合&lt;/strong&gt;。&lt;/p&gt;

&lt;h2 id=&quot;pointer-network&quot;&gt;Pointer Network&lt;/h2&gt;

&lt;p&gt;然而，其实[1] 的工作暂时有一个比较大的局限是，它因为是“原封不动”地“复制”输入端的信息，CopyNet 还不能被应用到机器翻译 MT 这个&lt;strong&gt;输入端和输出端语言不同&lt;/strong&gt;的任务上。这也是[1] 和 [2] 的一个重大不同。[2] 的工作乍一看复杂一些，但是灵活性可能就相对高一点。如果说 [1] 里的 CopyNet 要解决的是，what to copy 和 where to paste；那么这篇论文[2] 解决的就是&lt;strong&gt;learn to point&lt;/strong&gt; 和&lt;strong&gt;when to point&lt;/strong&gt;。这俩问题都是通过他们[2] 提出的 Pointer Softmax（PS）model 解决的。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/pointer-network.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在PS里，有两个softmax输出层：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;shortlist softmax就是传统的softmax(图中绿色正方形)，对应CopyNet中的generate-mode&lt;/li&gt;
  &lt;li&gt;location softmax则是比较重要的创新，它表示着某个词&lt;strong&gt;在输入端的位置&lt;/strong&gt;(图中蓝色正方形)。对应CopyNet中的copy-mode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;也就是说，当 PS 决定走 shortlist softmax 时，就开启generate-mode，从shortlist中&lt;strong&gt;生成&lt;/strong&gt;一个词给decoder；而如果是相反，则从location softmax获取&lt;strong&gt;要『复制』的词在输入端的位置&lt;/strong&gt;。那么，帮PS决定是&lt;strong&gt;走哪个softmax&lt;/strong&gt;的方式就是通过一个实际上是&lt;strong&gt;MLP的switch network&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;因为&lt;strong&gt;不是直接『复制』输入端&lt;/strong&gt;的具体内容，而是&lt;strong&gt;找到输入端的内容的位置&lt;/strong&gt;，所以 PS 可以完成机器翻译这种输入端和输出端不是同一种语言的任务。在实验中，这篇论文[2] 也分别在 detect rare words，机器翻译MT 和 summarization 上都进行了测试。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Progressive growing of GANs</title>
   <link href="http://daiwk.github.io/posts/rl-pg-gan.html"/>
   <updated>2017-11-15T00:00:00+00:00</updated>
   <id>/posts/rl-pg-gan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIzOTY2NTQ5Mg==&amp;amp;mid=2247484467&amp;amp;idx=1&amp;amp;sn=7b49d80a8e7632022bc39e20cc2898eb&amp;amp;chksm=e927ec08de50651ed0417fdcb704df29364209e44ba7a7b71f9b380cc3561c149ecb8793e7f0&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=11151C8y5OFVdSPnFZiDaaVN&amp;amp;pass_ticket=yKSawm65QTL65XEJd%2BHLs%2BQZu1VFIIkBud5RUYsquqF8oKcNYnxDf5s2TFoxBWK7#rd&quot;&gt;Progressive growing of GANs简介+PyTorch复现&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>文本摘要</title>
   <link href="http://daiwk.github.io/posts/nlp-text-summary.html"/>
   <updated>2017-10-31T00:00:00+00:00</updated>
   <id>/posts/nlp-text-summary</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#抽取式&quot;&gt;抽取式&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#基于排序的方法&quot;&gt;基于排序的方法&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#基于启发式规则&quot;&gt;基于启发式规则&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#基于图排序的方法&quot;&gt;基于图排序的方法&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#基于pagerank算法或相似算法&quot;&gt;基于PageRank算法或相似算法&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#textrank算法的基本思想&quot;&gt;textrank算法的基本思想&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#基于有监督学习的方法&quot;&gt;基于有监督学习的方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#基于神经网络的方法&quot;&gt;基于神经网络的方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#基于整数线性规划ilp的方法&quot;&gt;基于整数线性规划（ILP）的方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#基于次模函数的方法&quot;&gt;基于次模函数的方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#摘要句子排序&quot;&gt;摘要句子排序&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#生成式摘要&quot;&gt;生成式摘要&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#语料&quot;&gt;语料&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#1-complex-attention-model&quot;&gt;1. Complex Attention Model&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-simple-attention-model&quot;&gt;2. Simple Attention Model&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3-attention-based-summarizationabs&quot;&gt;3. Attention-Based Summarization(ABS)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4-abs&quot;&gt;4. ABS+&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#5-recurrent-attentive-summarizerras&quot;&gt;5. Recurrent Attentive Summarizer(RAS)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#6-big-words-lvt2k-1sent模型&quot;&gt;6. big-words-lvt2k-1sent模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#7-words-lvt2k-2sent-hieratt模型&quot;&gt;7. words-lvt2k-2sent-hieratt模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#8-feats-lvt2k-2sent-ptr模型&quot;&gt;8. feats-lvt2k-2sent-ptr模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#9-copynet&quot;&gt;9. COPYNET&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#10-mrtnhg&quot;&gt;10. MRT+NHG&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#效果&quot;&gt;效果&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考抽取式摘要1、2：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/qq_32458499/article/details/78659372&quot;&gt;https://blog.csdn.net/qq_32458499/article/details/78659372&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;和&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/qq_32458499/article/details/78664199&quot;&gt;https://blog.csdn.net/qq_32458499/article/details/78664199&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;抽取式&quot;&gt;抽取式&lt;/h2&gt;

&lt;p&gt;从文档中抽取已有句子形成摘要。实现简单，能保证句子的可读性&lt;/p&gt;

&lt;p&gt;可看作一个组合优化问题，可与语句压缩一并进行（可看作混合式方法）&lt;/p&gt;

&lt;h3 id=&quot;基于排序的方法&quot;&gt;基于排序的方法&lt;/h3&gt;

&lt;h4 id=&quot;基于启发式规则&quot;&gt;基于启发式规则&lt;/h4&gt;

&lt;p&gt;例如centroid-based method，考虑了句子包含词语权重、句子位置、句子与首句相似度等几个因素，加权作为句子的打分，然后排序。&lt;/p&gt;

&lt;h4 id=&quot;基于图排序的方法&quot;&gt;基于图排序的方法&lt;/h4&gt;

&lt;h5 id=&quot;基于pagerank算法或相似算法&quot;&gt;基于PageRank算法或相似算法&lt;/h5&gt;

&lt;p&gt;步骤： 
1、构建图G=(V,E),句子作为顶点，句子之间有关系则构建边 
2、应用PageRank算法或相似算法获得每个顶点的权重 
3、基于句子权重选择句子形成摘要&lt;/p&gt;

&lt;p&gt;如果网页T存在一个指向网页A的连接，则表明T的所有者认为A比较重要，从而把T的一部分重要性得分赋予A。这个重要性得分值为： 
PR（T）/L(T) 
其中PR（T）为T的PageRank值，L(T)为T的出链数 
则A的PageRank值为一系列类似于T的页面重要性得分值的累加。 
　 即一个页面的得票数由所有链向它的页面的重要性来决定，到一个页面的超链接相当于对该页投一票。一个页面的PageRank是由所有链向它的页面（链入页面）的重要性经过递归算法得到的。一个有较多链入的页面会有较高的等级，相反如果一个页面没有任何链入页面，那么它没有等级。&lt;/p&gt;

&lt;h5 id=&quot;textrank算法的基本思想&quot;&gt;textrank算法的基本思想&lt;/h5&gt;

&lt;p&gt;d是阻尼系数，In(Vi)表示指向网页i的链接的网页集合，Out(Vj)指网页j中的链接存在的链接指向的网页的集合。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
WS(V_i)=(1-d)+d*\sum _{V_j\in In(V_i)}\frac{w_{ji}}{\sum _{v_k \in Out(V_j)}w_{jk}}WS(V_j)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;等式左边表示一个句子的权重（WS是weight_sum的缩写），右侧的求和表示每个相邻句子对本句子的贡献程度。与提取关键字的时候不同，一般认为全部句子都是相邻的，不再提取窗口。求和的分母wji表示两个句子的相似程度，分母又是一个weight_sum，而WS(Vj)代表上次迭代j的权重。整个公式是一个迭代的过程。&lt;/p&gt;

&lt;p&gt;基于TextRank的自动文摘属于自动摘录，通过选取文本中重要度较高的句子形成文摘，其主要步骤如下： 
　　（1）预处理：将输入的文本或文本集的内容分割成句子这里写图片描述，构建图G =（V,E），其中V为句子集，对句子进行分词、去除停止词，得这里写图片描述，其中这里写图片描述是保留后的候选关键词。 
　　（2）句子相似度计算：构建图G中的边集E，基于句子间的内容覆盖率，给定两个句子si,sj，采用如下公式进行计算：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
sim(S_i,S_j)=\frac{|\{w_k|w_k \in S_i \&amp;amp; w_k \in S_j\}|}{log(|S_i|)+log(|S_j|)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;若两个句子之间的相似度大于给定的阈值，就认为这两个句子语义相关并将它们连接起来，即边的权值这里写图片描述 
　　（3）句子权重计算：根据公式，迭代传播权重计算各句子的得分； 
　　（4）抽取文摘句：将（3）得到的句子得分进行倒序排序，抽取重要度最高的T个句子作为候选文摘句。 
　　（5）形成文摘：根据字数或句子数要求，从候选文摘句中抽取句子组成文摘。&lt;/p&gt;

&lt;h3 id=&quot;基于有监督学习的方法&quot;&gt;基于有监督学习的方法&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;句子分类 
二类分类：句子是否隶属于摘要 
SVM（支持向量机）&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;序列标注 
为每个句子打上标签 
可考虑相邻句子之间的关系 
HMM（隐马尔科夫模型），CRF（条件随机场）&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;句子回归 
为每个句子预测一个反映重要性的分数 
SVR（支持向量回归）&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;基于神经网络的方法&quot;&gt;基于神经网络的方法&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;基于编码器-解码器框架进行单文档摘要&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;编码器：先对句子编码（利用CNN），再对文档编码（利用RNN） 
解码器：输出一个0/1序列，进行句子抽取（序列标注）&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;摘要冗余去除&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;去除（多文档）摘要中的冗余信息 
选择与摘要中已有句子冗余度小的句子&lt;/p&gt;

&lt;p&gt;文本蕴涵识别技术很适合此目的，但是由于自身性能太差，无法真正应用。一般基于文本相似度来进行判断。&lt;/p&gt;

&lt;h3 id=&quot;基于整数线性规划ilp的方法&quot;&gt;基于整数线性规划（ILP）的方法&lt;/h3&gt;

&lt;p&gt;将摘要看做一个带约束的优化问题 
基于ILP进行求解，可采用现成的ILP求解工具 
比如IBM CPLEX Optimizer 
同时进行句子抽取与冗余去除&lt;/p&gt;

&lt;h3 id=&quot;基于次模函数的方法&quot;&gt;基于次模函数的方法&lt;/h3&gt;

&lt;p&gt;将摘要看做一个预算约束下的次模函数最大化问题 
设计次模函数，然后利用贪心算法进行内容选取&lt;/p&gt;

&lt;p&gt;submodular次模函数
实际上就对“边际效用递减”这个说法的形式化。就是对于一个集合函数，若，那么在S中增加一个元素所增加的收益要小于等于在S的子集中增加一个元素所增加的收益。形式化表述就是：对于函数f而言，若且，则通俗的说就是你把所有商品看成一个集合，随着你所拥有的物品数量的增加，那么你获得剩下物品所得到的满足程度越来越小。&lt;/p&gt;

&lt;p&gt;举例说明： 
A是B的子集，则对于函数f()，如果：f(A+e)-f(A)&amp;gt;=f(B+e)-f(B)成立，则说f()函数是子模的。增益递减。 
例子如下： 
u={1,2,3,4,5,6,7,8} A={1,2,3} B={1,2,3,5,6} 
f(A)=|A| 集合A的个数 
所以：f(A+e)-f(A)&amp;gt;=f(B+e)-f(B)，例如e={3,4,5}&lt;/p&gt;

&lt;h3 id=&quot;摘要句子排序&quot;&gt;摘要句子排序&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;句子顺序直接影响摘要可读性&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;单文档摘要中句子顺序容易确定，依据句子在原文档中的顺序即可 
多文档摘要中句子顺序较难确定&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;来自不同文档中的句子如何确定先后排序？&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;可综合考虑句子所在上下文信息进行排序。 
先确定任何两句之间的先后顺序 机器学习、深度学习 
再确定多个句子之间的整体顺序 贪心搜索&lt;/p&gt;

&lt;h2 id=&quot;生成式摘要&quot;&gt;生成式摘要&lt;/h2&gt;

&lt;p&gt;经典。。&lt;a href=&quot;https://zhuanlan.zhihu.com/p/21426100&quot;&gt;教机器学习摘要&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;文本摘要问题按照文档数量可以分为单文档摘要和多文档摘要问题，按照实现方式可以分为提取式（extractive）和摘要式（abstractive）。摘要问题的特点是输出的文本要比输入的文本少很多很多，但却蕴藏着非常多的有效信息在内。有一点点感觉像是主成分分析（PCA），作用也与推荐系统有一点像，都是为了解决信息过载的问题。现在绝大多数应用的系统都是extractive的，这个方法比较简单但存在很多的问题，简单是因为只需要从原文中找出相对来说重要的句子来组成输出即可，系统只需要用模型来选择出信息量大的句子然后按照自然序组合起来就是摘要了。但是摘要的连贯性、一致性很难保证，比如遇到了句子中包含了代词，简单的连起来根本无法获知代词指的是什么，从而导致效果不佳。研究中随着deep learning技术在nlp中的深入，尤其是seq2seq+attention模型的“横行”，大家将abstractive式的摘要研究提高了一个level，并且提出了copy mechanism等机制来解决seq2seq模型中的OOV问题。&lt;/p&gt;

&lt;h3 id=&quot;语料&quot;&gt;语料&lt;/h3&gt;

&lt;p&gt;这里的语料分为两种，一种是用来训练深度学习模型的大型语料，一种是用来参加评测的小型语料。&lt;/p&gt;

&lt;p&gt;1、DUC&lt;/p&gt;

&lt;p&gt;这个网站提供了文本摘要的比赛，2001-2007年在这个网站，2008年开始换到这个网站TAC。很官方的比赛，各大文本摘要系统都会在这里较量一番，一决高下。这里提供的数据集都是小型数据集，用来评测模型的。&lt;/p&gt;

&lt;p&gt;2、Gigaword&lt;/p&gt;

&lt;p&gt;该语料非常大，大概有950w篇新闻文章，数据集用headline来做summary，即输出文本，用first sentence来做input，即输入文本，属于单句摘要的数据集。&lt;/p&gt;

&lt;p&gt;3、CNN/Daily Mail&lt;/p&gt;

&lt;p&gt;该语料就是我们在机器阅读理解中用到的语料，该数据集属于多句摘要。&lt;/p&gt;

&lt;p&gt;4、Large Scale Chinese Short Text Summarization Dataset（LCSTS）&lt;a href=&quot;http://cn.arxiv.org/pdf/1506.05865&quot;&gt;LCSTS: A Large Scale Chinese Short Text Summarization Dataset&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这是一个中文短文本摘要数据集，数据采集自新浪微博。&lt;/p&gt;

&lt;h3 id=&quot;1-complex-attention-model&quot;&gt;1. Complex Attention Model&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://cn.arxiv.org/pdf/1512.01712&quot;&gt;Generating News Headlines with Recurrent Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;模型中的attention weights是用encoder中每个词最后一层hidden layer的表示与当前decoder最新一个词最后一层hidden layer的表示做点乘，然后归一化来表示的。&lt;/p&gt;

&lt;h3 id=&quot;2-simple-attention-model&quot;&gt;2. Simple Attention Model&lt;/h3&gt;

&lt;p&gt;同样是&lt;a href=&quot;http://cn.arxiv.org/pdf/1512.01712&quot;&gt;Generating News Headlines with Recurrent Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;模型将encoder部分在每个词最后一层hidden layer的表示分为两块，一小块用来计算attention weights的，另一大块用来作为encoder的表示。这个模型将最后一层hidden layer细分了不同的作用。&lt;/p&gt;

&lt;h3 id=&quot;3-attention-based-summarizationabs&quot;&gt;3. Attention-Based Summarization(ABS)&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://cn.arxiv.org/pdf/1509.00685.pdf&quot;&gt;A Neural Attention Model for Abstractive Sentence Summarization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这个模型用了三种不同的encoder，包括：Bag-of-Words Encoder、Convolutional Encoder和Attention-Based Encoder。Rush是HarvardNLP组的，这个组的特点是非常喜欢用CNN来做nlp的任务。这个模型中，让我们看到了不同的encoder，从非常简单的词袋模型到CNN，再到attention-based模型，而不是千篇一律的rnn、lstm和gru。而decoder部分用了一个非常简单的NNLM，就是Bengio[10]于2003年提出来的前馈神经网络语言模型，这一模型是后续神经网络语言模型研究的基石，也是后续对于word embedding的研究奠定了基础。可以说，这个模型用了最简单的encoder和decoder来做seq2seq，是一次非常不错的尝试。&lt;/p&gt;

&lt;h3 id=&quot;4-abs&quot;&gt;4. ABS+&lt;/h3&gt;

&lt;p&gt;同样是&lt;a href=&quot;http://cn.arxiv.org/pdf/1509.00685.pdf&quot;&gt;A Neural Attention Model for Abstractive Sentence Summarization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rush提出了一个纯数据驱动的模型ABS之后，又提出了一个abstractive与extractive融合的模型，在ABS模型的基础上增加了feature function，修改了score function，得到了这个效果更佳的ABS+模型。&lt;/p&gt;

&lt;h3 id=&quot;5-recurrent-attentive-summarizerras&quot;&gt;5. Recurrent Attentive Summarizer(RAS)&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://nlp.seas.harvard.edu/papers/naacl16_summary.pdf&quot;&gt;Abstractive Sentence Summarization with Attentive Recurrent Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;6-big-words-lvt2k-1sent模型&quot;&gt;6. big-words-lvt2k-1sent模型&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://cn.arxiv.org/pdf/1602.06023&quot;&gt;Abstractive Text Summarization using Sequence-to-sequence RNNs and Beyond&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这个模型引入了large vocabulary trick(LVT)技术到文本摘要问题上。本方法中，每个mini batch中decoder的词汇表受制于encoder的词汇表，decoder词汇表中的词由一定数量的高频词构成。这个模型的思路重点解决的是由于decoder词汇表过大而造成softmax层的计算瓶颈。本模型非常适合解决文本摘要问题，因为摘要中的很多词都是来自于原文之中。&lt;/p&gt;

&lt;h3 id=&quot;7-words-lvt2k-2sent-hieratt模型&quot;&gt;7. words-lvt2k-2sent-hieratt模型&lt;/h3&gt;

&lt;p&gt;同样是&lt;a href=&quot;http://cn.arxiv.org/pdf/1602.06023&quot;&gt;Abstractive Text Summarization using Sequence-to-sequence RNNs and Beyond&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;文本摘要中经常遇到这样的问题，一些关键词出现很少但却很重要，由于模型基于word embedding，对低频词的处理并不友好，所以本文提出了一种decoder/pointer机制来解决这个问题。模型中decoder带有一个开关，如果开关状态是打开generator，则生成一个单词；如果是关闭，decoder则生成一个原文单词位置的指针，然后拷贝到摘要中。pointer机制在解决低频词时鲁棒性比较强，因为使用了encoder中低频词的隐藏层表示作为输入，是一个上下文相关的表示，而仅仅是一个词向量。这个pointer机制和后面有一篇中的copy机制思路非常类似。&lt;/p&gt;

&lt;h3 id=&quot;8-feats-lvt2k-2sent-ptr模型&quot;&gt;8. feats-lvt2k-2sent-ptr模型&lt;/h3&gt;

&lt;p&gt;同样是&lt;a href=&quot;http://cn.arxiv.org/pdf/1602.06023&quot;&gt;Abstractive Text Summarization using Sequence-to-sequence RNNs and Beyond&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;数据集中的原文一般都会很长，原文中的关键词和关键句子对于形成摘要都很重要，这个模型使用两个双向RNN来捕捉这两个层次的重要性，一个是word-level，一个是sentence-level，并且该模型在两个层次上都使用attention，权重如下：&lt;/p&gt;

&lt;h3 id=&quot;9-copynet&quot;&gt;9. COPYNET&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://cn.arxiv.org/pdf/1603.06393v2.pdf&quot;&gt;Incorporating Copying Mechanism in Sequence-to-Sequence Learning Training&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;encoder采用了一个双向RNN模型，输出一个隐藏层表示的矩阵M作为decoder的输入。decoder部分与传统的Seq2Seq不同之处在于以下三部分：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;预测：在生成词时存在两种模式，一种是生成模式，一种是拷贝模式，生成模型是一个结合两种模式的概率模型。&lt;/li&gt;
  &lt;li&gt;状态更新：用t-1时刻的预测出的词来更新t时刻的状态，COPYNET不仅仅词向量，而且使用M矩阵中特定位置的hidden state。&lt;/li&gt;
  &lt;li&gt;读取M：COPYNET也会选择性地读取M矩阵，来获取混合了内容和位置的信息。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这个模型与第7个模型思想非常的类似，因为很好地处理了OOV的问题，所以结果都非常好。&lt;/p&gt;

&lt;h3 id=&quot;10-mrtnhg&quot;&gt;10. MRT+NHG&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://cn.arxiv.org/pdf/1604.01904.pdf&quot;&gt;Neural Headline Generation with Minimum Risk Training&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这个模型的特别之处在于用了Minimum Risk Training训练数据，而不是传统的MLE（最大似然估计），将评价指标包含在优化目标内，更加直接地对评价指标做优化，得到了不错的结果。&lt;/p&gt;

&lt;h3 id=&quot;效果&quot;&gt;效果&lt;/h3&gt;

&lt;p&gt;不管是中文数据集还是英文数据集上，最好的结果都是来自于模型10,并且该模型只是采用最普通的seq2seq+attention模型，都没有用到效果更好的copy机制或者pointer机制。&lt;/p&gt;

&lt;p&gt;思考：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;为什么MRT那篇文章的结果会比其他各种各样的模型都要好呢？因为直接将ROUGE指标包含在了待优化的目标中，而不是与其他模型一样，采用传统的MLE来做，传统的目标评价的是你的生成质量如何，但与我们最终评价的指标ROUGE并无直接关系。所以说，换了一种优化目标，直接定位于评价指标上做优化，效果一定会很好。&lt;/li&gt;
  &lt;li&gt;OOV(out of vocabulary)的问题。因为文本摘要说到底，都是一个语言生成的问题，只要是涉及到生成的问题，必然会遇到OOV问题，因为不可能将所有词都放到词表中来计算概率，可行的方法是用选择topn个高频词来组成词表。文章[4]和[8]都采用了相似的思路，从input中拷贝原文到output中，而不仅仅是生成，&lt;strong&gt;这里需要设置一个gate来决定这个词是copy来还是generate出来&lt;/strong&gt;。显然，增加了copy机制的模型会在很大程度上解决了OOV的问题，就会显著地提升评价结果。这种思路不仅仅在文摘问题上适用，在一切生成问题上都适用。&lt;/li&gt;
  &lt;li&gt;关于评价指标的问题。一个评价指标是否科学直接影响了这个领域的发展水平，人工评价我们就不提了，只说自动评价。ROUGE指标在2003年就被Lin提出了[9]，13年过去了，仍然没有一个更加合适的评价体系来代替它。&lt;strong&gt;ROUGE评价太过死板，只能评价出output和target之间的一些表面信息，并不涉及到语义层面上的东西&lt;/strong&gt;，是否可以提出一种更加高层次的评价体系，从语义这个层面来评价摘要的效果。其实技术上问题不大，因为计算两个文本序列之间的相似度有无数种解决方案，有监督、无监督、半监督等等等等。很期待有一种新的体系来评价摘要效果，相信新的评价体系一定会推动自动文摘领域的发展。&lt;/li&gt;
  &lt;li&gt;关于数据集的问题。LCSTS数据集的构建给中文文本摘要的研究奠定了基础，将会很大程度地推动自动文摘在中文领域的发展。现在的互联网最不缺少的就是数据，大量的非结构化数据。但如何构建一个高质量的语料是一个难题，如何尽量避免用过多的人工手段来保证质量，如何用自动的方法来提升语料的质量都是难题。所以，如果能够提出一种全新的思路来构建自动文摘语料的话，将会非常有意义。&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>nmt</title>
   <link href="http://daiwk.github.io/posts/nlp-nmt.html"/>
   <updated>2017-10-31T00:00:00+00:00</updated>
   <id>/posts/nlp-nmt</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-rnn&quot;&gt;0. rnn&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-lstm--gru&quot;&gt;1. LSTM &amp;amp; GRU&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-lstm&quot;&gt;1.1 LSTM&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#111-lstm%e4%b8%ba%e4%bd%95%e5%8f%af%e4%bb%a5%e7%bc%93%e8%a7%a3%e6%a2%af%e5%ba%a6%e6%b6%88%e5%a4%b1&quot;&gt;1.1.1 lstm为何可以缓解梯度消失&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-gru&quot;&gt;1.2 GRU&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-%e5%8f%8c%e5%90%91gru&quot;&gt;2. 双向GRU&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-encoder-decoder%e6%a1%86%e6%9e%b6&quot;&gt;3. encoder-decoder框架&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-encoder&quot;&gt;3.1 encoder&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-decoder&quot;&gt;3.2 decoder&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-%e6%b3%a8%e6%84%8f%e5%8a%9b%e6%9c%ba%e5%88%b6&quot;&gt;4. 注意力机制&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-beam-search&quot;&gt;5. beam search&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-paddle-demo&quot;&gt;6. paddle demo&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#61-%e6%a8%a1%e5%9e%8b%e7%bb%93%e6%9e%84&quot;&gt;6.1 模型结构&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#611-%e7%bc%96%e7%a0%81%e5%99%a8&quot;&gt;6.1.1 编码器&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#612-%e8%a7%a3%e7%a0%81%e5%99%a8&quot;&gt;6.1.2 解码器&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#6121-%e5%be%97%e5%88%b0%e5%af%b9%e9%bd%90%e6%a8%a1%e5%9e%8b&quot;&gt;6.1.2.1 得到对齐模型&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#6122-%e6%9e%84%e9%80%a0%e8%a7%a3%e7%a0%81%e5%99%a8rnn%e7%9a%84%e5%88%9d%e5%a7%8b%e7%8a%b6%e6%80%81&quot;&gt;6.1.2.2 构造解码器RNN的初始状态&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#6123-%e5%ae%9a%e4%b9%89%e8%a7%a3%e7%a0%81%e9%98%b6%e6%ae%b5%e6%af%8f%e4%b8%80%e4%b8%aa%e6%97%b6%e9%97%b4%e6%ad%a5%e7%9a%84rnn%e8%a1%8c%e4%b8%ba&quot;&gt;6.1.2.3 定义解码阶段每一个时间步的RNN行为&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#6124-%e5%ae%9a%e4%b9%89%e8%a7%a3%e7%a0%81%e5%99%a8%e6%a1%86%e6%9e%b6%e5%90%8d%e5%ad%97%e5%8f%8agrudecoderwithattention%e7%9a%84%e5%89%8d%e4%b8%a4%e4%b8%aa%e8%be%93%e5%85%a5&quot;&gt;6.1.2.4 定义解码器框架名字及gru_decoder_with_attention的前两个输入&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#62-%e8%ae%ad%e7%bb%83%e6%a8%a1%e5%bc%8f%e7%9a%84decoder&quot;&gt;6.2 训练模式的decoder&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#63-%e7%94%9f%e6%88%90%e6%a8%a1%e5%9e%8b%e7%9a%84%e8%a7%a3%e7%a0%81%e5%99%a8&quot;&gt;6.3 生成模型的解码器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#64-attention%e7%9a%84%e5%8f%af%e8%a7%86%e5%8c%96&quot;&gt;6.4 attention的可视化&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-%e5%ba%94%e7%94%a8&quot;&gt;7. 应用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#71-backbone%e6%a8%a1%e5%9e%8b&quot;&gt;7.1 Backbone模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#72-%e5%85%b6%e4%bb%96%e4%b8%80%e4%ba%9b%e5%b0%9d%e8%af%95&quot;&gt;7.2 其他一些尝试&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#73-%e4%b8%8a%e8%bf%b0%e6%a8%a1%e5%9e%8b%e5%ad%98%e5%9c%a8%e7%9a%84%e9%97%ae%e9%a2%98&quot;&gt;7.3 上述模型存在的问题&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#74-%e9%87%87%e7%94%a8%e5%a2%9e%e5%bc%ba%e5%ad%a6%e4%b9%a0%e8%bf%9b%e8%a1%8c%e4%bc%98%e5%8c%96&quot;&gt;7.4 采用增强学习进行优化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#75-reranking&quot;&gt;7.5 reranking&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#76-rl%e8%bf%9b%e4%b8%80%e6%ad%a5%e6%94%b9%e8%bf%9b%e7%9a%84%e6%96%b9%e5%90%91&quot;&gt;7.6 rl进一步改进的方向&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#77-%e6%a8%a1%e5%9e%8b%e8%9e%8d%e5%90%88&quot;&gt;7.7 模型融合&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#8-%e5%85%b6%e4%bb%96&quot;&gt;8. 其他&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#81-sequenceconvpool%e7%9b%b8%e5%85%b3&quot;&gt;8.1 sequence_conv_pool相关&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#82-paddle%e7%9a%84lstm%e7%9b%b8%e5%85%b3&quot;&gt;8.2 paddle的lstm相关&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#83-%e8%af%84%e4%bb%b7%e6%8c%87%e6%a0%87&quot;&gt;8.3 评价指标&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e4%b8%8d%e6%ad%a2bleu&quot;&gt;不止bleu&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nmt%e7%9a%84%e6%8c%91%e6%88%98&quot;&gt;nmt的挑战&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;paddle rnn系列文档：
&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/tree/develop/doc/howto/deep_model/rnn&quot;&gt;https://github.com/PaddlePaddle/Paddle/tree/develop/doc/howto/deep_model/rnn &lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;0-rnn&quot;&gt;0. rnn&lt;/h2&gt;

&lt;p&gt;numpy写的rnn：&lt;a href=&quot;https://gist.github.com/karpathy/d4dee566867f8291f086&quot;&gt;https://gist.github.com/karpathy/d4dee566867f8291f086&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;拷了一份过来：&lt;a href=&quot;https://daiwk.github.io/assets/min-char-rnn.py&quot;&gt;https://daiwk.github.io/assets/min-char-rnn.py&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
Minimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy)
BSD License
&quot;&quot;&quot;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# data I/O
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'input.txt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'r'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# should be simple plain text file
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'data has %d characters, %d unique.'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;char_to_ix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ix_to_char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# hyperparameters
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# size of hidden layer of neurons
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# number of steps to unroll the RNN for
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-1&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# model parameters
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Wxh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# input to hidden
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Whh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# hidden to hidden
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Why&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# hidden to output
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# hidden bias
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# output bias
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lossFun&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
  inputs,targets are both list of integers.
  hprev is Hx1 array of initial hidden state
  returns the loss, gradients on model parameters, and last hidden state
  &quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# forward pass
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# encode in 1-of-k representation
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tanh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Wxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Whh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# hidden state
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Why&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# unnormalized log probabilities for next chars
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# probabilities for next chars
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# softmax (cross-entropy loss)
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# backward pass: compute gradients going backwards
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;dWxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Wxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Whh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Why&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;dbh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dby&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;dhnext&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# backprop into y. see http://cs231n.github.io/neural-networks-case-study/#grad if confused here
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;dWhy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dby&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Why&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhnext&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# backprop into h
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;dhraw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dh&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# backprop through tanh nonlinearity
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;dbh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhraw&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dWxh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dhraw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dWhh&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dhraw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dhnext&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Whh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dhraw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dparam&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dWxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dbh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dparam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dparam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# clip to mitigate exploding gradients
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dbh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed_ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot; 
  sample a sequence of integers from the model 
  h is memory state, seed_ix is seed letter for first time step
  &quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed_ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ixes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tanh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Wxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Whh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Why&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ravel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ixes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ixes&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mWxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mWhh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mWhy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Wxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Whh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Why&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mbh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mby&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros_like&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# memory variables for Adagrad
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smooth_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# loss at iteration 0
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# prepare inputs (we're sweeping from left to right in steps seq_length long)
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;hprev&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# reset RNN memory
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# go from start of data
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;char_to_ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;char_to_ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# sample from the model now and then
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sample_ix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;txt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ix_to_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ix&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sample_ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'----&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; %s &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;----'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;txt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# forward seq_length characters through the net and fetch gradient
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dbh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hprev&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lossFun&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hprev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;smooth_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;smooth_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.999&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.001&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'iter %d, loss: %f'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;smooth_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# print progress
&lt;/span&gt;  
  &lt;span class=&quot;c1&quot;&gt;# perform parameter update with Adagrad
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dparam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mem&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Wxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Whh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Why&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
                                &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dWxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dWhy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dbh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
                                &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mWxh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mWhh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mWhy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mbh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dparam&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dparam&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;param&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dparam&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# adagrad update
&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_length&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# move data pointer
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# iteration counter 
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;1-lstm--gru&quot;&gt;1. LSTM &amp;amp; GRU&lt;/h2&gt;

&lt;h3 id=&quot;11-lstm&quot;&gt;1.1 LSTM&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://en.wikipedia.org/wiki/Long_short-term_memory&quot;&gt;https://en.wikipedia.org/wiki/Long_short-term_memory&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;最开始的lstm只有inputgate和outputgate&lt;a href=&quot;http://www.bioinf.jku.at/publications/older/2604.pdf&quot;&gt;Long short-term memory, 1997&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;后来在&lt;a href=&quot;https://pdfs.semanticscholar.org/1154/0131eae85b2e11d53df7f1360eeb6476e7f4.pdf&quot;&gt;Learning to Forget: Continual Prediction with LSTM, 2000&lt;/a&gt;引入了forgetgate。&lt;/p&gt;

&lt;p&gt;公式如下，其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\circ \)&lt;/code&gt;表示element-wise的乘积：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\ f_t=\sigma _g(W_fx_t+U_fh_{t-1}+b_f)
\\ i_t=\sigma _g(W_ix_t+U_ih_{t-1}+b_i)
\\ o_t=\sigma _g(W_ox_t+U_oh_{t-1}+b_o)
\\ c_t=f_t \circ c_{t-1}+i_t \circ \sigma _c(W_cx_t+U_ch_{t-1}+b_c)
\\ h_t=o_t \circ \sigma _h(c_t)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，初始值&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_0=0\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_0=0\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\circ \)&lt;/code&gt;表示element-wise的乘积，也就是图中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\otimes \)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;变量&lt;/strong&gt;：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_t \in R^d\)&lt;/code&gt;：LSTM block的input vector&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_t \in R^h\)&lt;/code&gt;：forgetgate的activation vector&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i_t \in R^h\)&lt;/code&gt;：inputgate的activation vector&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(o_t \in R^h\)&lt;/code&gt;：outputgate的activation vector&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_t \in R^h\)&lt;/code&gt;：LSTM block的output vector&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_t \in R^h\)&lt;/code&gt;：cell state vector&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W \in R^{h \times d}\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(U \in R^{h \times h}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(b \in R^{h}\)&lt;/code&gt;：需要学习的权重和bias&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;激活函数&lt;/strong&gt;：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _g\)&lt;/code&gt;: sigmoid&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _c\)&lt;/code&gt;: tanh&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _h\)&lt;/code&gt;: tanh，但在peephole LSTM中，这个是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _h(x)=x\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在&lt;a href=&quot;ftp://ftp.idsia.ch/pub/juergen/L-IEEE.pdf&quot;&gt;LSTM Recurrent Networks Learn Simple Context Free and Context Sensitive Languages, 2001&lt;/a&gt;以及&lt;a href=&quot;http://www.jmlr.org/papers/volume3/gers02a/gers02a.pdf&quot;&gt;Learning Precise Timing with LSTM Recurrent Networks&lt;/a&gt;引入peephole conneciton：&lt;/p&gt;

&lt;p&gt;和lstm基本一样，区别：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_t\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i_t\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(o_t\)&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;都换成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_{t-1}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;干掉了和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i_t\)&lt;/code&gt;乘的那块的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_t\)&lt;/code&gt;的激活函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _h\)&lt;/code&gt;换成了线性激活，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _h(c_t)=c_t\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;可见这些改变，都是为了加强&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;的影响&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\ f_t=\sigma _g(W_fx_t+U_fc_{t-1}+b_f)
\\ i_t=\sigma _g(W_ix_t+U_ic_{t-1}+b_i)
\\ o_t=\sigma _g(W_ox_t+U_oc_{t-1}+b_o)
\\ c_t=f_t \circ c_{t-1}+i_t \circ \sigma _c(W_cx_t+b_c)
\\ h_t=o_t\circ \sigma _h(c_t) 
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/peephole-lstm.jpg&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;直观对比如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/lstm-peephole-lstm.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;111-lstm为何可以缓解梯度消失&quot;&gt;1.1.1 lstm为何可以缓解梯度消失&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.zhihu.com/question/34878706&quot;&gt;知乎&lt;/a&gt;：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://proceedings.mlr.press/v37/jozefowicz15.pdf&quot;&gt;An Empirical Exploration of Recurrent Network Architectures&lt;/a&gt;的第2节&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/lstm_no_vanishing_1.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;&lt;a href=&quot;https://pdfs.semanticscholar.org/2d9e/3f53fcdb548b0b3c4d4efb197f164fe0c381.pdf&quot;&gt;Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling&lt;/a&gt;的第3.3节&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/lstm_no_vanishing_1.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;传统的RNN总是用“覆写”的方式计算状态，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S_t=f(S_{t-1},x_t)\)&lt;/code&gt;，f是仿射运算后再套sigmoid。所以根据求导的链式法则，梯度会变成连积形式。多个小于1的连乘就会很快接近0，导致梯度消失。&lt;/li&gt;
  &lt;li&gt;引入门控单元的rnn，使用的是”累加”的方式，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S_t=\sum_{\tau=1}^{t}\Delta S_{\tau}\)&lt;/code&gt;，这种累加形式导致导数也是累加形式，因此可以缓解梯度消失。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;也就是说，传统的rnn是对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _c(xxxh_{t-1})\)&lt;/code&gt;【其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _c\)&lt;/code&gt;是tanh】乘以w，然后再算sigmoid。而lstm对这项只是乘了一个(0,1)的系数(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i_t\)&lt;/code&gt;)，然后加上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_t \dot c_{t-1}\)&lt;/code&gt;，再求tanh再乘一个(0,1)的系数（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(o_t\)&lt;/code&gt;），也就是说，并没有乘w，只是和一堆（0,1）的系数进行了线性变换，然后求和。。。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://weberna.github.io/blog/2017/11/15/LSTM-Vanishing-Gradients.html&quot;&gt;https://weberna.github.io/blog/2017/11/15/LSTM-Vanishing-Gradients.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;12-gru&quot;&gt;1.2 GRU&lt;/h3&gt;

&lt;p&gt;GRU是Cho等人在LSTM上提出的简化版本，也是RNN的一种扩展，如下图所示。GRU单元只有两个门：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;重置门（reset gate）：如果重置门关闭，会忽略掉历史信息，即历史不相干的信息不会影响未来的输出。&lt;/li&gt;
  &lt;li&gt;更新门（update gate）：将LSTM的输入门和遗忘门合并，用于控制历史信息对当前时刻隐层输出的影响。如果更新门接近1，会把历史信息传递下去。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_t\)&lt;/code&gt;【更新门】和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t\)&lt;/code&gt;【重置门】都作用(sigmoid)于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\([h_{t-1},x_t]\)&lt;/code&gt;【即上一时刻的隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;和这一时刻的输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_t\)&lt;/code&gt;】。&lt;/p&gt;

&lt;p&gt;而重置门作用于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t*h_{t-1}\)&lt;/code&gt;【注意，是element-wise乘积】再与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_t\)&lt;/code&gt;一起经过tanh得到节点状态。&lt;/p&gt;

&lt;p&gt;最终的输出是更新门与节点状态相乘，再加上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1-z_t\)&lt;/code&gt;与上一时刻的隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;的乘积。&lt;/p&gt;

&lt;p&gt;总结一下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;更新门和重置门都作用于上一时刻的隐层状态和这一时刻的输入。&lt;/li&gt;
  &lt;li&gt;节点状态是本时刻的输入与经过重置门作用了的上一时刻的输出一起决定的。&lt;/li&gt;
  &lt;li&gt;最终输出是更新门作用于节点状态，1-更新门作用于上一时刻隐层状态。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/gru.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;wikipedia的定义：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\ z_t=\sigma _g(W_zx_t+U_zh_{t-1}+b_z)
\\ r_t=\sigma _g(W_rx_t+U_rh_{t-1}+b_r)
\\ h_t=z_t \circ h_{t-1}+(1-z_t) \circ \sigma _h(W_hx_t+ U_h(r_t \circ h_{t-1}) + b_h)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;激活函数&lt;/strong&gt;：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _g\)&lt;/code&gt;: sigmoid&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma _h\)&lt;/code&gt;: tanh&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;小结：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;没有输出门&lt;/li&gt;
  &lt;li&gt;没有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;，这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;就有一部分&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;的作用&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_t\)&lt;/code&gt;类似&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_t\)&lt;/code&gt;的作用&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1-z_t\)&lt;/code&gt;类似&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i_t\)&lt;/code&gt;的作用，但它乘的那个tanh里的元素，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r_t\)&lt;/code&gt;这个门来控制&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-双向gru&quot;&gt;2. 双向GRU&lt;/h2&gt;

&lt;p&gt;我们已经在语义角色标注一章中介绍了一种双向循环神经网络，这里介绍Bengio团队在论文【Bahdanau等人&lt;a href=&quot;https://arxiv.org/pdf/1409.0473.pdf&quot;&gt;Neural Machine Translation by Jointly Learning to Align and Translate, ICLR, 2015&lt;/a&gt;】中提出的另一种结构。该结构的目的是输入一个序列，得到其在每个时刻的特征表示，即输出的每个时刻都用定长向量表示到该时刻的上下文语义信息。&lt;/p&gt;

&lt;p&gt;具体来说，该双向循环神经网络分别在时间维以顺序和逆序——即前向（forward）和后向（backward）——依次处理输入序列，并将每个时间步RNN的输出拼接成为最终的输出层。这样每个时间步的输出节点，都包含了输入序列中当前时刻完整的过去和未来的上下文信息。下图展示的是一个按时间步展开的双向循环神经网络。该网络包含一个前向和一个后向RNN，其中有六个权重矩阵：输入到前向隐层和后向隐层的权重矩阵（W1,W3），隐层到隐层自己的权重矩阵（W2,W5），前向隐层和后向隐层到输出层的权重矩阵（W4,W6）。注意，该网络的前向隐层和后向隐层之间没有连接。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/bi-lstm-bengio.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;3-encoder-decoder框架&quot;&gt;3. encoder-decoder框架&lt;/h2&gt;

&lt;p&gt;编码器-解码器（Encoder-Decoder）(&lt;a href=&quot;http://www.aclweb.org/anthology/D/D14/D14-1179.pdf&quot;&gt;Learning phrase representations using RNN encoder-decoder for statistical machine translation&lt;/a&gt;)框架用于解决由一个任意长度的源序列到另一个任意长度的目标序列的变换问题。即编码阶段将整个源序列编码成一个向量，解码阶段通过最大化预测序列概率，从中解码出整个目标序列。编码和解码的过程通常都使用RNN实现。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/encoder-decoder.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;31-encoder&quot;&gt;3.1 encoder&lt;/h3&gt;

&lt;p&gt;总体流程如下：&lt;/p&gt;
&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/encoder-process.png&quot; style=&quot;max-height: 350px;max-width:600px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;重点在于：
最后对于词xi，通过拼接两个GRU的结果得到它的隐层状态。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/encoder.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;32-decoder&quot;&gt;3.2 decoder&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/decoder-process.png&quot; style=&quot;max-height: 350px;max-width:600px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;关键在于，预测的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;由以下三部分经过&lt;strong&gt;非线性激活&lt;/strong&gt;产生：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;源语言句子的&lt;strong&gt;编码信息&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c=qh\)&lt;/code&gt;，如果不用注意力，可以直接取最后一个时间步的编码&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c=h_T\)&lt;/code&gt;，也可以用时间维上的pooling的结果)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;真实目标语言序列&lt;/strong&gt;的第i个词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_0\)&lt;/code&gt;是开始标志&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;s&amp;gt;&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;i时刻的RNN&lt;strong&gt;隐层状态&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_0\)&lt;/code&gt;是全零向量。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;4-注意力机制&quot;&gt;4. 注意力机制&lt;/h2&gt;

&lt;p&gt;如果在编码阶段的输出是一个固定维度的向量，会有以下两个问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;不论源语言序列长度是5个词还是50个词，如果都用固定长度的向量去编码其中的语义和句法信息，对模型来讲，&lt;strong&gt;是一个非常高的要求，特别对长句子而言。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;直觉上，当人类翻译一句话时，会对&lt;strong&gt;与【当前】译文更相关的源语言片段给予更多关注，且关注点会随翻译的进行而改变。&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;而固定维度的向量相当于，任何时刻都对源语言所有信息给予同等程度的关注，Bahdanau等人&lt;a href=&quot;https://arxiv.org/pdf/1409.0473.pdf&quot;&gt;Neural Machine Translation by Jointly Learning to Align and Translate, ICLR, 2015&lt;/a&gt;引入注意力机制，对编码后的上下文片段进行解码，以此解决长句子的特征学习问题。&lt;/p&gt;

&lt;p&gt;和简单的编码器不同之处在于，前面生成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;用的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;，而这里用的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;，也就是，对每个真实目标语言序列中的词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;【注意！！是目标语言的i，不是源语言的！！】，都有一个特定的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;与之对应。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
z_{i+1}=\phi _{\theta }(c_i, u_i, z_i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
c_i=\sum ^T_{j=1}a_{ij}h_j, a_i=[a_{i1}, a_{i2}, ..., a_{iT}]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可见，注意力机制是通过&lt;strong&gt;对编码器中各时刻的RNN状态&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;进行加权平均实现的。权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{ij}\)&lt;/code&gt;计算方法如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
c_i=\sum ^T_{j=1}a_{ij}h_j, a_i=[a_{i1}, a_{i2}, ..., a_{iT}]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
a_{ij}=\frac {exp(e_{ij})}{\sum _{k=1}^Texp(e_{ik})}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\
e_{ij}=align(z_i,h_j)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，align可以看作一个对齐模型，用于衡量&lt;strong&gt;目标语言第i个词&lt;/strong&gt;（第i个隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;）和&lt;strong&gt;源语言第j个词&lt;/strong&gt;（第j个词的上下文片段&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;）的匹配程度。&lt;/p&gt;

&lt;p&gt;传统的对齐模型中，目标语言的每个词明确对应源语言的一个词或多个词（hard alignment）；而这里用的是soft alignment，即&lt;strong&gt;任何两个目标语言和源语言词间&lt;/strong&gt;均存在一定的关联（模型计算出的实数值）。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/decoder-attention.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;5-beam-search&quot;&gt;5. beam search&lt;/h2&gt;

&lt;p&gt;柱搜索（beam search）算法是一种启发式图搜索算法，用于在图或树中搜索有限集合中的最优扩展节点。通常用在&lt;strong&gt;解空间非常大&lt;/strong&gt;的系统（如机器翻译、语音识别）中，因为内存无法存下图或权中所有展开的解。&lt;/p&gt;

&lt;p&gt;beam search使用广度优先策略建立搜索树，在树的每一层，按照启发代价（heuristic cost, &lt;strong&gt;例如本例中的生成词的log概率之和&lt;/strong&gt;）对节点进行排序，然后仅留下预先确定的个数(即beam width/beam size/柱宽度)的节点。只有这些节点会在下一层继续进行扩展，其他节点被裁剪掉了。可以减少搜索所占用的时间和空间，但无法保证一定获得最优解。&lt;/p&gt;

&lt;p&gt;使用beam search的解码阶段，目标是最大化生成序列的概率，具体思路如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每个时刻，根据源语言句子的编码信息c、生成的第i个真实目标语言序列单词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;，和i时刻RNN的隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;，计算出下一个隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;通过softmax【只针对beamsearch得到的可能的解做处理】归一化，得到的目标语言序列的第i+1个单词的概率分布&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{i+1}\)&lt;/code&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(u_{i+1}|u_{\lt i+1}, x)=softmax(W_sz_{i+1}+b_z)
\]&lt;/code&gt;
其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_sz_{i+1}+b_z\)&lt;/code&gt;是&lt;strong&gt;对每个可能的输出单词进行打分&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{i+1}\)&lt;/code&gt;采样出单词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_{i+1}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;重复前三个步骤，直到&lt;strong&gt;获得句子的结束标记&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;e&amp;gt;&lt;/code&gt;或者超过句子的最大生成长度为止。&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;6-paddle-demo&quot;&gt;6. paddle demo&lt;/h2&gt;

&lt;h3 id=&quot;61-模型结构&quot;&gt;6.1 模型结构&lt;/h3&gt;

&lt;p&gt;一些全局变量&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;dict_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30000&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 字典维度
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source_dict_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict_size&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 源语言字典维度
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_dict_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict_size&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 目标语言字典维度
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_vector_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 词向量维度
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoder_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 编码器中的GRU隐层大小
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;512&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 解码器中的GRU隐层大小
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;beam_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 柱宽度
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 生成句子的最大长度
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;611-编码器&quot;&gt;6.1.1 编码器&lt;/h4&gt;

&lt;p&gt;输入是一个文字序列，被表示成整型的序列。序列中每个元素是文字在字典中的索引。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;src_word_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'source_language_word'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integer_value_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source_dict_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后映射成词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src_embedding&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;src_embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
     &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_word_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_vector_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后使用双向gru进行编码，然后拼接两个gru的输出得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;【解码器中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;】即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encoded_vector&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;src_forward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;networks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simple_gru&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
     &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoder_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;src_backward&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;networks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simple_gru&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
     &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoder_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reverse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;encoded_vector&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;612-解码器&quot;&gt;6.1.2 解码器&lt;/h4&gt;

&lt;h5 id=&quot;6121-得到对齐模型&quot;&gt;6.1.2.1 得到对齐模型&lt;/h5&gt;

&lt;p&gt;首先，对源语言序列编码后的结果【&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;】过一个fc，得到其映射&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encoded_proj&lt;/code&gt;【即，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;的映射，也就是对齐模型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;】。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;encoded_proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoded_vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;6122-构造解码器rnn的初始状态&quot;&gt;6.1.2.2 构造解码器RNN的初始状态&lt;/h5&gt;

&lt;p&gt;由于解码器需要预测时序目标序列，但在0时刻并没有初始值，所以我们希望对其进行初始化。&lt;/p&gt;

&lt;p&gt;这里采用的是将源语言序列逆序编码后（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src_backward&lt;/code&gt;）的最后一个状态(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;backward_first&lt;/code&gt;)进行非线性映射fc，作为源语言上下文向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;的初始值（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;decoder_boot&lt;/code&gt;），即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_0=h_T\)&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;backward_first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;decoder_boot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tanh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward_first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;6123-定义解码阶段每一个时间步的rnn行为&quot;&gt;6.1.2.3 定义解码阶段每一个时间步的RNN行为&lt;/h5&gt;

&lt;p&gt;根据三个输入【当前时刻的源语言上下文向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;、解码器隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;、真实目标语言中的第i个词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;】，来预测第i+1个词的概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{i+1}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;主要过程（输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;【即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enc_vec&lt;/code&gt;】、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;【即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enc_proj&lt;/code&gt;】和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;【即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;current_word&lt;/code&gt;】：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_0\)&lt;/code&gt;的初始化，得到解码器隐层状态&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;【即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;decoder_mem&lt;/code&gt;】&lt;/li&gt;
  &lt;li&gt;然后通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;生成attention&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{ij}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;再将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a_{ij}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;一起得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;【和上一步一起，封装在simple_attention函数中，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;context&lt;/code&gt;】&lt;/li&gt;
  &lt;li&gt;接下来，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;【先&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;context&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;current_word&lt;/code&gt;一起经过一个fc，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;decoder_inputs&lt;/code&gt;，然后和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;decoder_mem&lt;/code&gt;一起给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gru_step&lt;/code&gt;做输入，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gru_step&lt;/code&gt;】&lt;/li&gt;
  &lt;li&gt;最后，对使用softmax归一化计算单词的概率，即通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;full_matrix_projection&lt;/code&gt;+softmax，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;out&lt;/code&gt;【即公式：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(u_{i+1}|u_{\lt i+1})=softmax(W_sz_{i+1}+b_z)\)&lt;/code&gt;】&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gru_decoder_with_attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enc_vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    enc_vec: h_j
    enc_proj: e_{ij}
    current_word: u_i
    &quot;&quot;&quot;&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;decoder_mem&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'gru_decoder'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;boot_layer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_boot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

     &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;networks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simple_attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;encoded_sequence&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enc_vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;encoded_proj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enc_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;decoder_state&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_mem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

     &lt;span class=&quot;n&quot;&gt;decoder_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;layer_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ExtraLayerAttribute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;error_clipping_threshold&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;100.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

     &lt;span class=&quot;n&quot;&gt;gru_step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gru_step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'gru_decoder'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;output_mem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_mem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

     &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_dict_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
         &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;full_matrix_projection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gru_step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;6124-定义解码器框架名字及gru_decoder_with_attention的前两个输入&quot;&gt;6.1.2.4 定义解码器框架名字及gru_decoder_with_attention的前两个输入&lt;/h5&gt;

&lt;p&gt;也就是说，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;需要分别通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StaticInput&lt;/code&gt;进行包装一下。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/blob/develop/doc/howto/deep_model/rnn/recurrent_group_cn.md#输入&quot;&gt;recurrent_group教程&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;recurrent_group处理的输入序列主要分为以下三种类型：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;数据输入&lt;/strong&gt;：一个双层序列进入recurrent_group&lt;strong&gt;会被拆解为一个单层序列&lt;/strong&gt;，一个单层序列进入recurrent_group&lt;strong&gt;会被拆解为非序列&lt;/strong&gt;，然后交给step函数，这一过程对用户是完全透明的。可以有以下两种：1）通过data_layer拿到的用户输入；2）其它layer的输出。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;只读Memory输入&lt;/strong&gt;：StaticInput 定义了一个&lt;strong&gt;只读&lt;/strong&gt;的Memory，由StaticInput指定的输入&lt;strong&gt;不会被recurrent_group拆解&lt;/strong&gt;，recurrent_group 循环展开的每个时间步总是能够引用所有输入，可以是一个非序列，或者一个单层序列。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;序列生成任务的输入&lt;/strong&gt;：GeneratedInput只用于在&lt;strong&gt;序列生成任务中&lt;/strong&gt;指定输入数据。&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;decoder_group_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;decoder_group&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;group_input1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StaticInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoded_vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;group_input2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;StaticInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoded_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;group_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group_input1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;group_input2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;62-训练模式的decoder&quot;&gt;6.2 训练模式的decoder&lt;/h3&gt;

&lt;p&gt;注意，wmt14的reader(paddle/v2/dataset/wmt14.py中)如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reader_creator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tar_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;src_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__read_to_dict__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tar_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tarfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tar_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'r'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;each_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;each_item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;each_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endswith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extractfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;line_split&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;src_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# one source sequence
&lt;/span&gt;                    &lt;span class=&quot;n&quot;&gt;src_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;src_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;src_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UNK_IDX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;START&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

                    &lt;span class=&quot;n&quot;&gt;trg_seq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# one target sequence
&lt;/span&gt;                    &lt;span class=&quot;n&quot;&gt;trg_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg_seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;trg_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UNK_IDX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

                    &lt;span class=&quot;c1&quot;&gt;# remove sequence whose length &amp;gt; 80 in training mode
&lt;/span&gt;                    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;src_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;trg_ids_next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;trg_ids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;START&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg_ids&lt;/span&gt;

                    &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;src_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg_ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trg_ids_next&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reader&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;首先，对目标语言计算embedding，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trg_embedding&lt;/code&gt;，作为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;current_word&lt;/code&gt;，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;，放到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;group_inputs&lt;/code&gt;中&lt;/li&gt;
  &lt;li&gt;然后使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recurrent_group&lt;/code&gt;，step指定为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gru_decoder_with_attention&lt;/code&gt;，input为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;group_inputs&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;使用目标语言的下一个词序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target_language_next_word&lt;/code&gt;作为label&lt;/li&gt;
  &lt;li&gt;对decoder的输出与label做多分类的交叉熵算cost&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;trg_embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'target_language_word'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integer_value_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_dict_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_vector_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;param_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ParamAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_target_language_embedding'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;group_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# For decoder equipped with attention mechanism, in training,
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# target embeding (the groudtruth) is the data input,
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# while encoded source sequence is accessed to as an unbounded memory.
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Here, the StaticInput defines a read-only memory
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# for the recurrent_group.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recurrent_group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_group_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gru_decoder_with_attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;lbl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'target_language_next_word'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integer_value_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_dict_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;classification_cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lbl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;63-生成模型的解码器&quot;&gt;6.3 生成模型的解码器&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;首先，在序列生成任务中，由于解码阶段的RNN总是引用上一时刻生成出的词的词向量，作为当前时刻的输入，因此，使用&lt;strong&gt;GeneratedInput&lt;/strong&gt;来自动完成这一过程。&lt;/li&gt;
  &lt;li&gt;然后，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;beam_search&lt;/code&gt;函数循环调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gru_decoder_with_attention&lt;/code&gt;函数，生成出序列id。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;和训练的区别：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;group_inputs的前两个输入一样，都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;的StaticInput，第三个输入有区别：训练是目标语言序列的embedding，而生成使用的是GeneratedInput&lt;/li&gt;
  &lt;li&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;beam_search&lt;/code&gt;，而训练使用的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recurrent_group&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# In generation, the decoder predicts a next target word based on
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# the encoded source sequence and the previous generated target word.
&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# The encoded source sequence (encoder's output) must be specified by
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# StaticInput, which is a read-only memory.
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Embedding of the previous generated word is automatically retrieved
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# by GeneratedInputs initialized by a start mark &amp;lt;s&amp;gt;.
&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;trg_embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GeneratedInput&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_dict_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;embedding_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_target_language_embedding'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_vector_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;group_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trg_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;beam_gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;beam_search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder_group_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gru_decoder_with_attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bos_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;eos_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;beam_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;beam_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;max_length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;max_length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;64-attention的可视化&quot;&gt;6.4 attention的可视化&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/issues/1269&quot;&gt;https://github.com/PaddlePaddle/Paddle/issues/1269&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;7-应用&quot;&gt;7. 应用&lt;/h2&gt;

&lt;p&gt;在&lt;a href=&quot;https://challenger.ai/competition/translation/subject&quot;&gt;AI Challenger比赛的机器翻译赛题&lt;/a&gt;中，冠军的做法如下：&lt;/p&gt;

&lt;p&gt;用pytorch（轻量，且实现Deliberation networks相对容易），8卡titan xp，一次训练将近一周。&lt;/p&gt;

&lt;p&gt;另外一篇类似的&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247494103&amp;amp;idx=1&amp;amp;sn=fc372862e0369b1f6a943bf997f6fc1b&amp;amp;chksm=96ea3657a19dbf4108bbc4179e779aa04ef05fe84f0013fa6425b0cd7e761e9880917361c4c1&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0108Q62Nm1w6JPdLDll9f1n4&amp;amp;pass_ticket=mPnDPDR4heSU20VXT7N8W622Cb1dZmIzkNcF8BygI%2Bp60d7GrSesIej%2FlrFbnO84#rd&quot;&gt;AI Challenger 2018 机器翻译参赛总结&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;71-backbone模型&quot;&gt;7.1 Backbone模型&lt;/h3&gt;

&lt;p&gt;基于encoder-decoder+attention，其中，rnn使用3层lstm，dim(h)=1024。&lt;/p&gt;

&lt;p&gt;对于处理未登录词：&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.aclweb.org/anthology/P16-1162&quot;&gt;Neural Machine Translation of Rare Words with Subword Units&lt;/a&gt;【NMT中的OOV（集外词）和罕见词（Rare Words）问题通常用back-off 词典的方式来解决，本文尝试用一种更简单有效的方式（Subword Units）来表示开放词表。 本文从命名实体、同根词、外来语、组合词（罕见词有相当大比例是上述几种）的翻译策略中得到启发，认为把这些罕见词拆分为“子词单元”(subword units)的组合，可以有效的缓解NMT的OOV和罕见词翻译的问题。子词单元的拆分策略，则是借鉴了一种数据压缩算法&lt;a href=&quot;https://en.wikipedia.org/wiki/Byte_pair_encoding&quot;&gt;Byte Pair Encoding(BPE)&lt;/a&gt;，这里的压缩算法不是针对于词做变长编码，而是对于子词来操作。这样，即使是训练语料里未见过的新词，也可以通过子词的拼接来生成翻译。本文还探讨了BPE的两种编码方式：一种是源语言词汇和目标语言词汇分别编码，另一种是双语词汇联合编码。前者的优势是让词表和文本的表示更紧凑，后者则可以尽可能保证原文和译文的子词切分方式统一。从实验结果来看，在音译或简单复制较多的情形下（比如英德）翻译，联合编码的效果更佳。github:&lt;a href=&quot;https://github.com/rsennrich/subword-nmt&quot;&gt;https://github.com/rsennrich/subword-nmt&lt;/a&gt;】
解决低频词问题，相比Char/Word混合编码更为高效。源和目标的vocabulary size大约都是3.5w。&lt;/p&gt;

&lt;h3 id=&quot;72-其他一些尝试&quot;&gt;7.2 其他一些尝试&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;结合语法信息的特征：
part-of-speech信息的tagging的信息做为sub-word的一个额外的embedding信息，相当于拿词语的词性来做额外的信息辅助。在200w-400w的小数据集上，使用这个方法，可以提升bleu 0.3左右。part-of-speech可以用nltk，而stanford的nlp的库在英文上的效果会更好。&lt;/li&gt;
  &lt;li&gt;解码的方向：
左-&amp;gt;右 or 右-&amp;gt;左其实是两种条件概率，同时做两个方向的解码并且取最优（平均/max之类的都可以试试），bleu也可以提升0.4左右。最终的模型融合中暂未使用，因为就需要16个模型，而不是8个了。&lt;/li&gt;
  &lt;li&gt;基于非rnn的encoder-decoder：
facebook的conv-seq2seq，单模型比lstm略有提升，但性能相对差一点。t2t效果也略好于baseline。但不太方便扩展（如，加上deliberation network，加入词性信息）。&lt;/li&gt;
  &lt;li&gt;迭代编码：
传统encoder-decoder模型中，一旦decoder output了一个字符，就固定不可修改，希望可以继续迭代，进一步调整。所以，可以先训一个encoder-decoder，然后把output再作为输入，再搞一次encoder-decoder。【msra的nips2017有尝试：&lt;a href=&quot;http://papers.nips.cc/paper/6775-deliberation-networks-sequence-generation-beyond-one-pass-decoding.pdf&quot;&gt;Deliberation Networks: Sequence Generation Beyond One-Pass Decoding&lt;/a&gt;】【编码器-解码器框架在许多序列生成任务中都实现了非常好的性能，包括机器翻译、自动文本摘要、对话系统和图像描述等。这样的框架在解码和生成序列的过程中只采用一次（one-pass）正向传播过程，因此缺乏推敲（deliberation）的过程：即生成的序列直接作为最终的输出而没有进一步打磨的过程。然而推敲是人们在日常生活中的一种常见行为，正如我们在阅读新闻和写论文/文章/书籍一样。在该研究中，我们将推敲过程加入到了编码器-解码器框架中，并提出了用于序列生成的推敲网络（Deliberation networks）。推敲网络具有两阶段解码器，其中第一阶段解码器用于解码生成原始序列，第二阶段解码器通过推敲的过程打磨和润色原始语句。由于第二阶段推敲解码器具有应该生成什么样的语句这一全局信息，因此它能通过从第一阶段的原始语句中观察未来的单词而产生更好的序列。神经机器翻译和自动文本摘要的实验证明了我们所提出推敲网络的有效性。在 WMT 2014 英语到法语间的翻译任务中，我们的模型实现了 41.5 的 BLEU 分值，即当前最优的 BLEU 分值。】&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;73-上述模型存在的问题&quot;&gt;7.3 上述模型存在的问题&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;曝光偏差（Exposure Bias）
训练采用teacher forcing原则，永远假设以正确的答案作为结果进行训练，即output的前缀都是在正确的基础上输出下一个字符，和真实的decoding不太一样。即中间如果有个错了，会不会越错越远。&lt;/li&gt;
  &lt;li&gt;训练与测试失配（loss-evaluation mismatch）
训练使用cross-entropy，但测试的目标却是bleu&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;74-采用增强学习进行优化&quot;&gt;7.4 采用增强学习进行优化&lt;/h3&gt;

&lt;p&gt;目标：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
max_\theta E_{\tau \sim  \pi _\theta }[R(\tau )] 
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;先用cross-entropy训出一个基本可用的，再用增强学习，以bleu为目标，使用梯度上升的方法去改进&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta \)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
g=E_{\tau \sim  \pi _\theta }[\nabla _\theta log\pi _\theta (\tau)R(\tau ))] 
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi _\theta (\cdot )\)&lt;/code&gt;为现有的翻译模型&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tau\)&lt;/code&gt;为根据现有模型所生成的一个翻译结果&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R(\tau)\)&lt;/code&gt;采用句子级别BLEU进行计算&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;75-reranking&quot;&gt;7.5 reranking&lt;/h3&gt;

&lt;p&gt;假设模型基本正确，生成k个可能正确的翻译结果，即，采用beamsearch，得到源语言x的多个翻译结果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\{t_i\}^K_{i=1}\)&lt;/code&gt;，然后根据bleu score计算平均值：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\bar{b}=\frac{1}{K}\sum_iR(t_i)    
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后估算梯度：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{g}=\frac{1}{K}\sum_i\nabla _\theta log\pi _\theta (t_i)[R(t_i)-\bar{b})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;有0.3bleu的提升&lt;/p&gt;

&lt;h3 id=&quot;76-rl进一步改进的方向&quot;&gt;7.6 rl进一步改进的方向&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;MIXER：第一步用cross-entropy，后面直接用bleu，有gap，
[sequence level traing with Recurrent neural networks]&lt;/li&gt;
  &lt;li&gt;learning rate的设置：Trust Region Policy Optimization，&lt;a href=&quot;https://arxiv.org/pdf/1707.06347.pdf&quot;&gt;Proximal Polocy Optimization Algorithms&lt;/a&gt;解决学习率难以调整的问题。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L^{clip}(\theta)=E_\tau [min(r(\theta )R(\tau),clip(r(\theta),1-\epsilon,1+\epsilon)R(\tau))]    
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r(\theta)=\frac{\pi_\theta(\tau)}{\pi_{\theta_{old}}(\tau)}\)&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;77-模型融合&quot;&gt;7.7 模型融合&lt;/h3&gt;

&lt;p&gt;计算算数平均值&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\bar{\pi_\theta}(a;s)=\frac{1}{M}\sum_{j=1}^M\pi^j_\theta(a;s)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后采用上述结果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\bar{\pi_\theta}(a;s)\)&lt;/code&gt;进行beam search，得到最终结果
M=8个模型融合，相比单模型，+1.1 bleu&lt;/p&gt;

&lt;p&gt;beamsearch时，除了max-likelihood外，还加入了对coverage的penalty和对句子长度的normalization计算。&lt;/p&gt;

&lt;h2 id=&quot;8-其他&quot;&gt;8. 其他&lt;/h2&gt;

&lt;h3 id=&quot;81-sequence_conv_pool相关&quot;&gt;8.1 sequence_conv_pool相关&lt;/h3&gt;

&lt;p&gt;注意：
cnn的卷积操作参考&lt;a href=&quot;https://daiwk.github.io/posts/image-classical-cnns.html&quot;&gt;https://daiwk.github.io/posts/image-classical-cnns.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;所以，推荐系统里面cnn的例子里，原矩阵是nxk，卷积核是hxk，所以得到的结果是(n-h+1)x(k-k+1)=(n-h+1)x1.所谓的时间维度上的maxpooling，就是对着这(n-h+1)个数字取max.&lt;/p&gt;

&lt;p&gt;如果有多个(例如，xx个)卷积核，那就有多个(n-h+1)个序列，h和每个卷积核相关，那取pooling就是每个(n-h+1)里取max，最后得到xx个max值。&lt;/p&gt;

&lt;p&gt;在sequence_conv_pool中，本质是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text input =&amp;gt; Context Projection =&amp;gt; FC Layer =&amp;gt; Pooling =&amp;gt; Output.&lt;/code&gt;，&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mixed_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context_proj_layer_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LinearActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;layer_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context_projection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;context_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;context_start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;padding_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context_proj_param_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;### 打平成一个input.size*context_len的了？假设输入是一个序列，那么输出不是序列的序列了？而是一个单层序列？另外注意，这里的输入实际上是embedding，也就是序列的序列.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;fc_layer_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%s_conv_fc&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; \
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc_layer_name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc_layer_name&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_layer_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_act&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;layer_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;param_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_param_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_bias_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pooling_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;pooling_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pool_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pool_bias_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;layer_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pool_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而其中的context_projection如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    For example, origin sequence is [A B C D E F G], context len is 3, then
    after context projection and not set padding_attr, sequence will
    be [ 0AB ABC BCD CDE DEF EFG FG0 ].
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;82-paddle的lstm相关&quot;&gt;8.2 paddle的lstm相关&lt;/h3&gt;

&lt;p&gt;在stacked_lstm的例子中：&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;fc_last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pooling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pooling_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pooling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lstm_last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pooling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pooling_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pooling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fc_last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lstm_last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;class_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paddle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Softmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;param_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;para_attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;另外，&lt;/p&gt;

&lt;p&gt;lstmemory的输出大小固定是输入大小的1/4&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;agg_level=AggregateLevel.TO_NO_SEQUENCE pooling 默认把一个单层序列变成一个0层序列，默认是maxpooling，也就是整个序列取一个最大值当做输出。&lt;/p&gt;

&lt;h3 id=&quot;83-评价指标&quot;&gt;8.3 评价指标&lt;/h3&gt;

&lt;p&gt;在计算BLEU得分的时候，&lt;strong&gt;连续匹配&lt;/strong&gt;的词越多，得分越高。&lt;/p&gt;

&lt;p&gt;BLEU值也有比较明显的缺点。用一个词来举例，比如『你好』，人给出的一个参考译文是『hello』。机器给出的译文是『how are you』，跟这个reference没有一个词匹配上，从BLEU值的角度来看，它得分是零。所以BLEU值的得分，&lt;strong&gt;受reference影响&lt;/strong&gt;。Reference越多样化，匹配上的可能性就会越大。一般来说，用于评价机器翻译质量的测试集有&lt;strong&gt;4个reference&lt;/strong&gt;，也有的有一个reference，也有的有十几个reference。&lt;/p&gt;

&lt;h3 id=&quot;不止bleu&quot;&gt;不止bleu&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.aclweb.org/anthology/P19-1427&quot;&gt;Beyond BLEU: Training Neural Machine Translation with Semantic Similarity&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在本文中，研究者提出以一种可替代的奖励函数（reward function）来优化神经机器翻译（NMT）系统，这种奖励函数基于语义相似性的研究。研究者对四种翻译成英文的语言展开评估，结果发现：利用他们提出的&lt;em&gt;度量&lt;/em&gt;进行训练，可以产生较 BLEU、语义相似性和人工测评等评估标准更好的翻译效果，并且优化过程的收敛速度更快。分析认为，取得更好翻译效果的原因在于研究者提出的度量更有利于优化，同时分配部分分数（partial credit），提供较 BLEU.1 更强的分数多样性。&lt;/p&gt;

&lt;h3 id=&quot;nmt的挑战&quot;&gt;nmt的挑战&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1706.03872.pdf&quot;&gt;Six Challenges for Neural Machine Translation&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>alphago-zero</title>
   <link href="http://daiwk.github.io/posts/rl-alphago-zero.html"/>
   <updated>2017-10-19T00:00:00+00:00</updated>
   <id>/posts/rl-alphago-zero</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#背景&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#论文地址&quot;&gt;论文地址&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考
&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652006424&amp;amp;idx=1&amp;amp;sn=d0be3ac450e735cbd9946de4f155fc42&amp;amp;chksm=f1211ce9c65695ff159a3f44d31abc92ad9494bef122082a09089d3ffeaa9e74678db1256392&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=10192t2Uq9DfDmj8F6gQ652a&amp;amp;pass_ticket=h3hZq0WHA7Cyui0YBndmrxji2MHJPRFf2%2F6zqKyUTOnTIhZZuESFoAbpmgeoETVa#rd&quot;&gt;【21天完虐Master】AlphaGo Zero横空出世，DeepMind Nature论文解密不使用人类知识掌握围棋&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA4NDQwNDQ2Nw==&amp;amp;mid=2650480675&amp;amp;idx=1&amp;amp;sn=9cfb29cb37fa28892cae59775ba6b816&amp;amp;chksm=87e831bfb09fb8a92f24ad7516e51ca215cefa627d3fa0d4a82b334418a8311dd37ee02e9e29&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1019QrErNgHVFjp0D7xARMyj&amp;amp;pass_ticket=h3hZq0WHA7Cyui0YBndmrxji2MHJPRFf2%2F6zqKyUTOnTIhZZuESFoAbpmgeoETVa#rd&quot;&gt;今日Nature: 人工智能从0到1, 无师自通完爆阿法狗100-0 | 深度解析&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MjM5ODIzNDQ3Mw==&amp;amp;mid=2649967631&amp;amp;idx=1&amp;amp;sn=c3ebeaa66be9a920bfdad91020da3ed1&amp;amp;chksm=beca3c0989bdb51f9ff1d3f89cbe05d5b0822db9fa89d912abfe6660e6c070abeabcf44b733f&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1019F9cifstsNiGxC0gaOwGc&amp;amp;pass_ticket=h3hZq0WHA7Cyui0YBndmrxji2MHJPRFf2%2F6zqKyUTOnTIhZZuESFoAbpmgeoETVa#rd&quot;&gt;AlphaGo Zero 没有告诉你的秘密&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

&lt;p&gt;人工智能长期以来的一个目标是创造一个能够在具有挑战性的领域，以超越人类的精通程度学习的算法，“tabula rasa”（译注：一种认知论观念，认为指个体在没有先天精神内容的情况下诞生，所有的知识都来自于后天的经验或感知）。此前，AlphaGo成为首个在围棋中战胜人类世界冠军的系统。AlphaGo的那些神经网络使用人类专家下棋的数据进行监督学习训练，同时也通过自我对弈进行强化学习。&lt;/p&gt;

&lt;p&gt;在这里，我们介绍一种仅基于强化学习的算法，不使用人类的数据、指导或规则以外的领域知识。AlphaGo成了自己的老师。我们训练了一个神经网络来预测AlphaGo自己的落子选择和AlphaGo自我对弈的赢家。这种神经网络提高了树搜索的强度，使落子质量更高，自我对弈迭代更强。从“tabula rasa”开始，我们的新系统AlphaGo Zero实现了超人的表现，以100：0的成绩击败了此前发表的AlphaGo。&lt;/p&gt;

&lt;h2 id=&quot;论文地址&quot;&gt;论文地址&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;../assets/agz_unformatted_nature.pdf&quot;&gt;Mastering the Game of Go without Human Knowledge&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>horovod</title>
   <link href="http://daiwk.github.io/posts/platform-horovod.html"/>
   <updated>2017-10-18T00:00:00+00:00</updated>
   <id>/posts/platform-horovod</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%ae%89%e8%a3%85&quot;&gt;安装&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%9c%89%e4%b8%80%e4%b8%aa%e8%a3%85%e4%ba%86tf%e7%9a%84py&quot;&gt;有一个装了tf的py&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%ae%89%e8%a3%85openmpi&quot;&gt;安装openmpi&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pip%e5%ae%89%e8%a3%85&quot;&gt;pip安装&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://juejin.im/post/5cbc6dbd5188253236619ccb#heading-9&quot;&gt;https://juejin.im/post/5cbc6dbd5188253236619ccb#heading-9&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/zwqjoy/article/details/89552432&quot;&gt;[深度学习] 分布式Horovod介绍（四）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主页：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/uber/horovod&quot;&gt;https://github.com/uber/horovod&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

&lt;h3 id=&quot;有一个装了tf的py&quot;&gt;有一个装了tf的py&lt;/h3&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/home/work/tools/python-2.7.8-tf1.4-gpu
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;安装openmpi&quot;&gt;安装openmpi&lt;/h3&gt;

&lt;p&gt;下载地址：
&lt;a href=&quot;https://www.open-mpi.org/software/ompi/v3.0/&quot;&gt;https://www.open-mpi.org/software/ompi/v3.0/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;解压后&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./configure --prefix=/home/work/tools/openmpi
make &amp;amp;&amp;amp; make install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;pip安装&quot;&gt;pip安装&lt;/h3&gt;

&lt;p&gt;确保PATH里有gcc48，以及&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home/work/tools/openmpi/bin/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果是GPU的，确保&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export LD_LIBRARY_PATH=/home/work/cudnnv6/cuda/lib64/:$LD_LIBRARY_PATH&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因为安装时要用到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-lpython2.7&lt;/code&gt;，如果没有root权限，去报错的gcc命令里找&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-L&lt;/code&gt;的路径，发现最简单粗暴的方法就是&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cp  /home/work/tools/python-2.7.8-tf1.4-gpu/lib/libpython2.7.so* /home/work/tools/openmpi/lib
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cp /home/work/tools/python-2.7.8-tf1.4-gpu/lib/ /home/work/tools/python-2.7.8-tf1.4-gpu/lib/python2.7/site-packages/tensorflow
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后pip安装：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/home/work/tools/python-2.7.8-tf1.4-gpu/bin/python  /home/work/tools/python-2.7.8-tf1.4-gpu/bin/pip install horovod
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>gan with the wind</title>
   <link href="http://daiwk.github.io/posts/cv-gan-with-the-wind.html"/>
   <updated>2017-10-18T00:00:00+00:00</updated>
   <id>/posts/cv-gan-with-the-wind</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#背景&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考文献&quot;&gt;参考文献&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3NTM4MzY1Mg==&amp;amp;mid=2650814302&amp;amp;idx=1&amp;amp;sn=87642b1c0662b3b71666e45a1cb2a0b3&amp;amp;chksm=8485c155b3f248433dbdb33d92d36851d25791a261c7ae09164155314e1cee7d578140ef0456&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1018lhAqPZOlGOOGCzx0bqrH&amp;amp;pass_ticket=h3hZq0WHA7Cyui0YBndmrxji2MHJPRFf2%2F6zqKyUTOnTIhZZuESFoAbpmgeoETVa#rd&quot;&gt;虚构的对抗，GAN with the wind&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Goodfellow 【1】于2014年提出了GAN的概念，他的解释如下：GAN的核心思想是构造两个深度神经网络：判别器D和生成器G，用户为GAN提供一些真实货币作为训练样本，生成器G生成假币来欺骗判别器D，判别器D判断一张货币是否来自真实样本还是G生成的伪币；判别器和生成器交替训练，能力在博弈中同步提高，最后达到平衡点的时候判别器无法区分样本的真伪，生成器的伪造功能炉火纯青，生成的货币几可乱真。这种阴阳互补，相克相生的设计理念为GAN的学说增添了魅力。&lt;/p&gt;

&lt;p&gt;GAN模型的优点来自于自主生成数据。机器学习的关键在于海量的数据样本，GAN自身可以生成不尽的样本，从而极大地减少了对于训练数据的需求，因此极其适合无监督学习；GAN的另外一个优点是对于所学习的概率分布要求非常宽泛，这一概率分布由训练数据的测度分布来表达，不需要有显式的数学表示。&lt;/p&gt;

&lt;p&gt;GAN虽然在工程实践中取得巨大的成功，但是缺乏严格的理论基础。大量的几何假设，都是仰仗似是而非的观念；其运作的内在机理，也是依据肤浅唯像的经验解释。丘成桐先生率领团队在学习算法的基础理论方面进行着不懈的探索。我们用最优传输（Optimal mass Transportation）理论框架来阐释对抗生成模型，同时用凸几何（Convex Geometry）的基础理论来为最优传输理论建立几何图景。通过理论验证，我们发现关于对抗生成模型的一些基本观念有待商榷：理论上，Wasserstein GAN中生成器和识别器的竞争是没有必要的，生成器网络和识别器网络的交替训练是徒劳的，此消彼长的对抗是虚构的。最优的识别器训练出来之后，生成器可以由简单的数学公式所直接得到。详细的数学推导和实验结果可以在【7】中找到。&lt;/p&gt;

&lt;h2 id=&quot;参考文献&quot;&gt;参考文献&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Goodfellow, Ian J.; Pouget-Abadie, Jean; Mirza, Mehdi; Xu, Bing; Warde-Farley, David; Ozair, Sherjil; Courville, Aaron; Bengio, Yoshua (2014). “Generative Adversarial Networks”. arXiv:1406.2661 &lt;/li&gt;
  &lt;li&gt;A. D. Alexandrov. “Convex polyhedra” Translated from the 1950 Russian edition by N. S. Dairbekov, S. S. Kutateladze and A. B. Sossinsky. Springer Monographs in Mathematics. Springer-Verlag, Berlin, 2005.&lt;/li&gt;
  &lt;li&gt;Martin Arjovsky, Soumith Chintala, and Léon Bottou. Wasserstein generative adversarial networks. In International Conference on Machine Learning, pages 214–223, 2017.&lt;/li&gt;
  &lt;li&gt;Yann Brenier. Polar factorization and monotone rearrangement of vector-valued functions. Comm. Pure Appl. Math., 44(4):375–417, 1991.&lt;/li&gt;
  &lt;li&gt;Xianfeng Gu, Feng Luo, Jian Sun, and Tianqi Wu. A discrete uniformization theorem for polyhedral surfaces. Journal of Differential Geometry (JDG), 2017.&lt;/li&gt;
  &lt;li&gt;Xianfeng Gu, Feng Luo, Jian Sun, and Shing-Tung Yau. Variational principles for minkowski type problems, discrete optimal transport, and discrete monge-ampere equations. Asian Journal of Mathematics (AJM), 20(2):383 C 398, 2016.&lt;/li&gt;
  &lt;li&gt;Na Lei,Kehua Su,Li Cui,Shing-Tung Yau,David Xianfeng Gu, A Geometric View of Optimal Transportation and Generative Model, arXiv:1710.05488.&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>从2017年顶会论文看Attention Model</title>
   <link href="http://daiwk.github.io/posts/dl-attention-models.html"/>
   <updated>2017-10-16T00:00:00+00:00</updated>
   <id>/posts/dl-attention-models</id>
   <content type="html">&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/R59A_qviCMjklIlmTE-qww&quot;&gt;从2017年顶会论文看Attention Model - PaperWeekly 第50期&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#引言&quot;&gt;引言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#单层attention-model&quot;&gt;单层Attention Model&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#多注意力机制hierarchical-attention--dual-attention&quot;&gt;多注意力机制（Hierarchical Attention &amp;amp; Dual Attention）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#基于知识图谱或者领域知识的注意力机制knowledge-base-attention&quot;&gt;基于知识图谱或者领域知识的注意力机制（Knowledge-base Attention）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考文献&quot;&gt;参考文献&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;引言&quot;&gt;引言&lt;/h2&gt;

&lt;p&gt;Attention Model 在 Image Caption、Machine Translation、Speech Recognition 等领域上有着不错的结果。那么什么是 Attention Model 呢？&lt;/p&gt;

&lt;p&gt;举个例子，给下面这张图片加字幕（Image Caption）：一只黄色的小猫带着一个鹿角帽子趴在沙发上。可以发现在翻译的过程中我们的注意力由小猫到鹿角帽子再到沙发（小猫→鹿角帽子→沙发）。其实在很多和时序有关的事情上，人类的注意力都不是一成不变的，随着事情（时间）发展，我们的注意力不断改变。&lt;/p&gt;

&lt;p&gt;这篇文章的预备知识是 Decoder-Encoder 模型。本文主要做一个介绍，基本不会有公式推导，旨在让大家对 Attention Model 的变型和应用有一个大概的印象。&lt;/p&gt;

&lt;h2 id=&quot;单层attention-model&quot;&gt;单层Attention Model&lt;/h2&gt;

&lt;p&gt;2014 年发表的文章 Neural Machine Translation by Jointly Learning to Align and Translate 使用了单层 Attention Model 解决机器翻译中不同长度的源语言对齐问题。使用 Attention Model 的基本思想是目标语言端的词往往只与源语言端部分词相关。而这个相关度通常用概率的形式表示。&lt;/p&gt;

&lt;p&gt;这个过程基本为：首先计算当前上下文的环境与每个源语言词语的相关度（一个得分），然后使用 softmax 公式，将这个相关度转化为概率的形式，最后用得到的概率乘以对应源语言端词的隐含表示作为该词对预测目标的贡献，将所有源语言端的词贡献加起来作为预测下一个词的部分输入。&lt;/p&gt;

&lt;p&gt;其中计算上下文环境与源语言词语的相关得分，是根据语言特性设计的一个对齐模型（Alignment Model）。通常情况下，单层 Attention Model 的不同之处主要在于相关分数计算方式的不同，接下来我们介绍三种通用的计算方式。同时在后文中，不再重复叙述 Attention Model 中根据相关分数计算输出向量的过程。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/single-attention.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;论文 Dipole: Diagnosis Prediction in Healthcare via Attention-based Bidirectional Recurrent Neural Networks，介绍了单个 Attention Model 在医疗诊断预测中的应用。&lt;/p&gt;

&lt;p&gt;这个模型的输入是用户前 t 次的医疗代码（每次的医疗代码用 one-hot 的形式表示），输出是用户下一时刻的医疗诊断类型。使用 Attention Model 的思想是：用户下一时刻被诊断的疾病类型可能更与前面某一次或某几次的医疗诊断相关。论文模型框架如下。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/attention-bi-rnn.jpg&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;论文 Dynamic Attention Deep Model for Article Recommendation by Learning Human Editors’ Demonstration 介绍了单个 Attention Model 在新闻推荐/筛选领域的应用。该模型的输入是一个文章的文本和种类信息，输出是0/1，表示输入的新闻是否被选中（二分类问题）。&lt;/p&gt;

&lt;p&gt;下图展示的是该模型的 Attention Model 部分，未展示的部分是处理输入数据的过程，该过程是通过 CNN 等模型将文本和种类特征处理成固定维度的隐含向量表示。&lt;/p&gt;

&lt;p&gt;论文的整体思路为：根据新闻的文本信息和种类信息，考虑新闻的时效性和时间特征。它主要是基于两个假设：第一，用户在不同的日子可能会有不同的喜好，比如用户在周一更喜欢金融类的新闻，而在周五可能更喜欢娱乐相关的论文；第二，不同类型的新闻时效性也不同，金融相关的新闻时效性短，而娱乐类的新闻时效性长。&lt;/p&gt;

&lt;p&gt;该论文中 Attention Model 的作用是，在第 t 时刻，给定新闻的文本和种类信息，不仅仅考虑第 t 时刻的模型（假设考虑一周中不同日子的影响，则有 7 个模型），我们也考虑该新闻在其他时刻的影响，然后根据新闻在各个时刻下的影响度随着时间的变化，综合考虑该新闻被选择的概率。 但是这个模型人工干涉的部分太多，随着假设的增加，模型会越来越复杂。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/dynamic-attention-deep-model.jpg&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;论文 Enhancing Recurrent Neural Networks with Positional Attention for Question Answering 将 Attention Model 变形，然后应用于问答系统（Question Answer）。该模型输入为“问题”句子中所有词语的词向量，输出“回答”句子中所有词语的词向量。&lt;/p&gt;

&lt;p&gt;该论文使用 Attention Model 的假设为：如果“问题”句子中某个单词出现在“回答”句子中，则在“回答”句子中该单词周围的词语影响度更大，且该影响度随距离变化呈高斯分布。&lt;/p&gt;

&lt;p&gt;通过拟合高斯分布得到所有输入词的影响概率，然后将影响概率作为权重，和输入向量加权求和得到基于位置的影响向量，最后将得到的影响向量作为指定向量，计算所有输入单词的隐含向量与该指定向量的相关度，通过 Attention Model 计算得到的向量作为输入帮助完成预测。&lt;/p&gt;

&lt;p&gt;这篇论文中，相关度的计算并没有使用上面提到的三种方法，而是根据问题的实际情况以及模型的前提假设进行变型。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/positional-attention-for-qa.jpg&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;论文 Video Question Answering via Attribute-Augmented Attention Network Learning 使用基于属性扩展的 Attention Model 来解决以给定视频作为背景材料的问题。&lt;/p&gt;

&lt;p&gt;本文的基本思路是根据问题发现哪一部分视频和这个问题相关，从而能生成更加相关答案。本文的输入包括，多帧视频信息（视频每一帧通过 CNN 等模型得到固定维度的隐含向量表示）以及处理问题（文本信息）得到隐含向量表示，输出为回答中的多个单词。&lt;/p&gt;

&lt;p&gt;其实这篇论文只简单地将每一帧视频处理成一个固定向量，且 multi-modal 部分的求和取平均有些过于简单。如果能更精确地分析每一帧画面相关的重点信息，结果应该会更加优化。&lt;/p&gt;

&lt;p&gt;而接下来，我们要介绍多注意力机制（Hierarchical Attention 以及 Dual Attention）是如何更加精确地完成任务。&lt;/p&gt;

&lt;h2 id=&quot;多注意力机制hierarchical-attention--dual-attention&quot;&gt;多注意力机制（Hierarchical Attention &amp;amp; Dual Attention）&lt;/h2&gt;

&lt;p&gt;论文 A Context-aware Attention Network for Interactive Interactive Question Answering 介绍了如何用 Hierarchical Attention Model 完成 QA 任务。&lt;/p&gt;

&lt;p&gt;该任务的输入是：给定文档（多个句子，每个句子由多个单词组成）的所有词向量，以及给定问题的隐含向量表示；输出是：回答句子中的一个个单词。&lt;/p&gt;

&lt;p&gt;本文 Attention Model 部分的基本思路为：首先在给定的文档集中的每一个句子中，计算该句子中每一个单词和给定问题的相关度，通过这一层的 Attention Model 计算得到的向量作为每个句子的隐含向量表示；然后在给定的文档集中，计算每一个句子和给定问题的相关度，然后利用第二层 Attention Model 计算得到的向量作为 Context vector，用以生成回答。&lt;/p&gt;

&lt;p&gt;论文 Leveraging Contextual Sentence Relations for Extractive Summarization Using a Neural Attention Model 介绍了如何使用 Hierarchical Attention Model 完成文章的摘要提取。&lt;/p&gt;

&lt;p&gt;摘要提取这类任务的输入是一个文档中所有词向量，输出是 0/1，表示每一个句子是否作为摘要。摘要提取的一个关键是判断哪些词语或者哪些句子更具有代表性，更能代表其他词语或者句子，而通常的做法就是对于句子 t ，计算 [1,t-1] 以及 [t+1,] 与 t 的cosine相似度，作为判断 t 是否作为摘要的依据。&lt;/p&gt;

&lt;p&gt;首先，对每一个词计算其对该句子的贡献率（影响概率），通过单词级别（word-level）的 Attention Model 计算生成的向量作为句子的隐含向量；而在句子级别计算每个句子和其前面 t-1 个句子的相似度，利用第二层 Attention 生成的上文相关向量，同时计算每个句子和其后面所有句子的相似度，利用第二层 Attention 生成的下文相关向量；最后将上文下文相关向量作为最终预测的一部分输入。&lt;/p&gt;

&lt;p&gt;上述文章中的 Attention Model 与前一篇论文类似，都是用了单词级别（word-level）和句子级别（sentence-level）的 Hierarchical Attention Model。&lt;/p&gt;

&lt;p&gt;当然，这种 Hierarchical Attention Model 不仅可以使用在 NLP 领域，它在很多领域都有不错的发展。接下来这篇文章就介绍了 Hierarchical Attention Model 在医疗领域的应用。&lt;/p&gt;

&lt;p&gt;论文 Learning to Generate Rock Descriptions from Multivariate Well Logs with Hierarchical Attention 介绍了如何利用 Hierarchical Attention Model 帮助生成岩石描述。&lt;/p&gt;

&lt;p&gt;该模型的输入是钻井得到的多类数据（每类数据的数值随时间或者其他维度不断变化）以及词典集中所有单词的词向量，输出为岩石描述的文本。它第一层 Attention Model 是为了判断，在预测下一个描述词时，每类数据中具体哪个数值与该描述词相关；而第二层 Attention Model 的意义在于判断在预测下一个描述词时，哪一类数据与该描述词相关。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/hierarchical-attention.jpg&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;前三篇论文都是关于 Hierarchical Attention Model，接下来介绍一篇关于 Dual Attention 的论文。论文 Interpretable Convolutional Neural Networks with Dual Local and Global Attention for Review Rating Prediction 介绍了如何将 Dual Attention Model 用于推荐系统。&lt;/p&gt;

&lt;p&gt;这个模型有两个输入：User Network 的输入是该用户给出的所有评论；Item Network 的输入是该产品收到的所有评论。输出是该用户对给定产品的评分。&lt;/p&gt;

&lt;p&gt;本文有一个假设：不同用户对喜爱或者厌恶情感的表达是不同的，比如一个用户喜欢打高分，不喜欢的产品他可能打 4 分，但是另一个用户喜欢打低分，他喜欢的产品也仅仅给 4 分，因此要根据用户真正的喜好来推荐，而不能仅仅把用户的评分作为依据。&lt;/p&gt;

&lt;p&gt;本文的 Dual Attention Model，是一个平行的模型，不同于前三篇文章第一层 Attention Model 的输出是第二层 Attention Model 的输入。这个 Dual Attention 由一个 Local-Attention（L-Attn）和一个 Global-Attention（G-Attn）组成。L-Attn 旨在在滑动窗口中发掘出一些有意义的关键词（可以根据这些关键词发掘用户的喜好，比如“喜欢”、“裙子”）；而 G-Attn 旨在抓住用户的整体情感表达情况（假设中提到的）。&lt;/p&gt;

&lt;p&gt;同理，在 Item Network 中也使用 L- 和 G-Attn 用来抓住一些和该产品有关的关键词以及其他用户对这个产品的一个整体喜好情况。然后将 Dual Attention Model 得到的输出拼接起来作为完成最终任务的输入。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/dual-local-global-attention.jpg&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;基于知识图谱或者领域知识的注意力机制knowledge-base-attention&quot;&gt;基于知识图谱或者领域知识的注意力机制（Knowledge-base Attention）&lt;/h2&gt;

&lt;p&gt;论文 Attentive Collaborative Filtering: Multimedia Recommendation with Item- and Component-Level Attention 介绍了基于领域知识的注意力机制如何用于推荐系统的。其中的 Attention Model 框架和上一节中介绍的 Hierarchical Attention Model 十分类似，唯一的不同就是它的输入使用了其他领域信息。&lt;/p&gt;

&lt;p&gt;不同于传统的 CF 推荐模型，本文有如下假设：两个用户同时喜欢一件衣服，有人因为它的颜色，有人因为它的裁剪，在实际推荐中，精准分析将提升推荐准确性。因此某一个产品的隐含向量表示不再是固定不变的了，它会根据用户的喜好有不同的表现。模型的输入是用户之前购买的所有产品以及每个产品的多个特征表示，输出是用户喜欢该产品的可能性。&lt;/p&gt;

&lt;p&gt;首先，对于每一个输入的产品，计算用户特征向量和该产品每一个特征的相关度，通过第一层 Attention Model 计算得到的向量作为该产品的隐含表示；然后计算用户特征向量和所有输入产品的相关度，通过第二层 Attention Model 计算得到的向量作为用户的喜好特征，从而完成最终的推荐任务。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/item-component-level-attention.jpg&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;当我们要引入知识图谱或者领域知识时，并不代表模型会十分复杂，也不是说 Attention 的层数越高，模型越好。论文 GRAM: Graph-based Attention Model For Healthcare Representation Learning 就用了一个简单漂亮的模型完成了预测任务。&lt;/p&gt;

&lt;p&gt;和小节 2 中提到的 Dipole 模型一样，这是一个医疗诊断预测问题。这个模型的输入是用户前 t 次的医疗代码（每次的医疗代码用 one-hot 的形式表示），输出是用户下一时刻的医疗诊断类型。但是有很多医疗代码数据稀疏甚至缺失，导致该医疗代码的隐含向量表示不能很好地完成预测任务。本文通过引入知识图谱信息一定程度地改善了这种情况。&lt;/p&gt;

&lt;p&gt;首先引入外部知识图谱，这个知识图谱是一个有向无环图，每个节点的父节点表示了一种相关但更宽泛的定义/概念。当医疗代码 c_i 信息稀少或者缺失时，可以考虑使用更加宽泛的定义。而 Attention Model 就是用来判断 c_i,c_g,c_c,c_a（从叶子节点到根结点上所有节点）对预测任务的贡献度。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/graph-based-attention.jpg&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;参考文献&quot;&gt;参考文献&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;KDD-2017 
[1] Dipole: Diagnosis Prediction in Healthcare via Attention-based Bidirectional Recurrent Neural Networks
[2] A Context-aware Attention Network for Interactive Interactive Question Answering
[3] Dynamic Attention Deep Model for Article Recommendation by Learning Human Editors’ Demonstration
[4] GRAM: Graph-based Attention Model For Healthcare Representation Learning
[5] Learning to Generate Rock Descriptions from Multivariate Well Logs with Hierarchical Attention&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;SIGIR-2017 
[6] Enhancing Recurrent Neural Networks with Positional Attention for Question Answering
[7] Attentive Collaborative Filtering: Multimedia Recommendation with Item- and Component-Level Attention
[8] Video Question Answering via Attribute-Augmented Attention Network Learning
[9] Leveraging Contextual Sentence Relations for Extractive Summarization Using a Neural Attention Model&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Recsys-2017 
[10] Interpretable Convolutional Neural Networks with Dual Local and Global Attention for Review Rating Prediction&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorflow 1.4 & tensorflow Lattice</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-1.4-lattice.html"/>
   <updated>2017-10-01T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-1.4-lattice</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考
&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650731830&amp;amp;idx=2&amp;amp;sn=143336c52be3eb01ac6c89cd791a639e&amp;amp;chksm=871b3148b06cb85e406e48eae6931f275cbe3bc3928a1194be0bd2976fafe00e81da49e48099&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1012Efo9go9G5puRyX9xUoC3&amp;amp;pass_ticket=U2f1rbLl%2BClvuwLAUQglXTKBs0vaj7Eioq3h0UjYuRGC2ba08n9JXiZRnTbI8gcr#rd&quot;&gt;谷歌发布TensorFlow 1.4与TensorFlow Lattice：利用先验知识提升模型准确度&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TensorFlow 1.4.0 先行版更新说明：https://github.com/tensorflow/tensorflow/releases/tag/v1.4.0-rc0
TensorFlow Lattice 项目链接：https://github.com/tensorflow/lattice&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>pytorch 4天速成</title>
   <link href="http://daiwk.github.io/posts/platform-pytorch-4-days.html"/>
   <updated>2017-10-01T00:00:00+00:00</updated>
   <id>/posts/platform-pytorch-4-days</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考:&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650731685&amp;amp;idx=1&amp;amp;sn=9b8cfdf380ff9c8c91b45ebe7452f4ee&amp;amp;chksm=871b30dbb06cb9cd199412e72d7740970e82c7c61057473871287706a4239f3661eafbfd1630&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1009pcuLshsQk7zryuXLSiTD&amp;amp;pass_ticket=Z%2FYgUmrXU%2Bh9nF6dN6bKAV4NtEhXP4axlulYipjx5EaDwwTIipn36q7i7UJgdRtV#rd&quot;&gt;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650731685&amp;amp;idx=1&amp;amp;sn=9b8cfdf380ff9c8c91b45ebe7452f4ee&amp;amp;chksm=871b30dbb06cb9cd199412e72d7740970e82c7c61057473871287706a4239f3661eafbfd1630&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1009pcuLshsQk7zryuXLSiTD&amp;amp;pass_ticket=Z%2FYgUmrXU%2Bh9nF6dN6bKAV4NtEhXP4axlulYipjx5EaDwwTIipn36q7i7UJgdRtV#rd&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/hunkim/PyTorchZeroToAll&quot;&gt;https://github.com/hunkim/PyTorchZeroToAll&lt;/a&gt;
PPT：&lt;a href=&quot;http://bit.ly/PyTorchZeroAll&quot;&gt;http://bit.ly/PyTorchZeroAll&lt;/a&gt;
百度云盘：&lt;a href=&quot;https://pan.baidu.com/s/1cpoyXw&quot;&gt;https://pan.baidu.com/s/1cpoyXw&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Gluon</title>
   <link href="http://daiwk.github.io/posts/platform-gluon.html"/>
   <updated>2017-10-01T00:00:00+00:00</updated>
   <id>/posts/platform-gluon</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#接口更简洁&quot;&gt;接口更简洁&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#速度更快&quot;&gt;速度更快&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#即是文档又是教材&quot;&gt;即是文档，又是教材&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gluon与其他框架的对比&quot;&gt;Gluon与其他框架的对比&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#一系列链接&quot;&gt;一系列链接&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/28648399?from=timeline&amp;amp;isappinstalled=0&quot;&gt;https://zhuanlan.zhihu.com/p/28648399?from=timeline&amp;amp;isappinstalled=0&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;接口更简洁&quot;&gt;接口更简洁&lt;/h2&gt;

&lt;p&gt;Gluon采用Keras和Numpy风格API，并且Layer可以自动判断输入长度。用过Chainer和Pytorch的人想必都体会过每一层都要记住前一层输出长度的麻烦，从卷积层到全联接层过度时长度计算更是痛苦，往往要运行一遍才知道。在Gluon里则没有这种问题，每层只要指定输出长度，输入长度则可以由系统自动计算。&lt;/p&gt;

&lt;h2 id=&quot;速度更快&quot;&gt;速度更快&lt;/h2&gt;

&lt;p&gt;深度学习框架大体分为两类：以TensorFlow，caffe2为代表的静态图（Symbolic）框架和以Chainer，Pytorch为代表的动态图（Imperative）框架。静态图的优势在于速度快，省内存，便于线上部署。而动态图框架的优势是灵活，易用，debug方便，特别是在自然语言处理和增强学习等领域，比起静态图框架有显著优势。&lt;/p&gt;

&lt;p&gt;Gluon同时支持灵活的动态图和高效的静态图，让你在享受动态编程的灵活易用的同时最小化性能的损失。而Gluon的HybridBlock和hybridize接口让你可以在静态动态间一键切换。0.11版Gluon比0.20版Pytorch快10%以上，在未来的一两个月我们会加入更多优化，再提高10%以上的性能。&lt;/p&gt;

&lt;h2 id=&quot;即是文档又是教材&quot;&gt;即是文档，又是教材&lt;/h2&gt;

&lt;p&gt;Gluon教程包括深度学习理论讲解和代码实践。前五章每个例子都包括了两个版本。从零开始（from scratch）版本深入讲解所有细节，Gluon版本则着重演示高级封装的灵活高效。建议刚开始接触深度学习的同学从头开始顺序阅读，而已经有一定经验的同学可以跳过基础教程只看Gluon版。这套教程现在在Github上公开写作，共计划18章，已经完成了前五章。印刷出版和中文翻译也在计划中。&lt;/p&gt;

&lt;h2 id=&quot;gluon与其他框架的对比&quot;&gt;Gluon与其他框架的对比&lt;/h2&gt;

&lt;p&gt;Tensorflow：Gluon同时支持静态图和动态图，在灵活性和速度上都有优势。但由于Gluon刚刚面市，在成熟度和线上部署方便还有不足。总的来说在做深度学习研究的同学不妨一试。&lt;/p&gt;

&lt;p&gt;Pytorch：Gluon与Pytorch的相似度很高，而Gluon独特的静、动态图混合功能可以在不牺牲灵活性的前提下提高性能。如果你喜欢pytorch的简单易用又在乎性能，那么强烈建议你试一试Gluon。&lt;/p&gt;

&lt;h2 id=&quot;一系列链接&quot;&gt;一系列链接&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;文档+教材：&lt;a href=&quot;https://github.com/zackchase/mxnet-the-straight-dope&quot;&gt;https://github.com/zackchase/mxnet-the-straight-dope&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Gluon教程：&lt;a href=&quot;http://gluon.mxnet.io/&quot;&gt;http://gluon.mxnet.io/&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;安装教程：&lt;a href=&quot;https://mxnet.incubator.apache.org/versions/master/get_started/install.html&quot;&gt;https://mxnet.incubator.apache.org/versions/master/get_started/install.html&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;mxnet官网：&lt;a href=&quot;https://mxnet.incubator.apache.org/&quot;&gt;https://mxnet.incubator.apache.org/&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>关闭正在运行的程序正在使用的文件句柄</title>
   <link href="http://daiwk.github.io/posts/other-close-fd.html"/>
   <updated>2017-10-01T00:00:00+00:00</updated>
   <id>/posts/other-close-fd</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/323146/how-to-close-a-file-descriptor-from-another-process-in-unix-systems&quot;&gt;https://stackoverflow.com/questions/323146/how-to-close-a-file-descriptor-from-another-process-in-unix-systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;一个爬虫程序写了for循环，但中间一个网络请求卡住了，后面的一直排不上，需要把这个请求kill掉，让后面的继续：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ps aux | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;new_image_spider.py| &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $2}'&lt;/span&gt; 

&lt;span class=&quot;c&quot;&gt;## 得到的进程号是1234&lt;/span&gt;
ll /proc/1234/fd
total 0
lr-x------  1 work work 64 Oct 12 17:58 0 -&amp;gt; /dev/null
l-wx------  1 work work 64 Oct 12 17:58 1 -&amp;gt; /home/disk1/xxx/4.txt
l-wx------  1 work work 64 Oct 12 17:58 2 -&amp;gt; /home/disk1/xxx/3.txt
lr-x------  1 work work 64 Oct 12 17:58 3 -&amp;gt; /home/disk1/xxx/2.txt
l-wx------  1 work work 64 Oct 12 17:58 4 -&amp;gt; /home/disk1/xxx/1.txt
lrwx------  1 work work 64 Oct 12 17:58 5 -&amp;gt; socket:[4273883397]

&lt;span class=&quot;c&quot;&gt;## 发现5这个fd，也就是socket是红的且卡住了，那么&lt;/span&gt;
gdb &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 1234

&lt;span class=&quot;c&quot;&gt;## 然后&lt;/span&gt;
p close&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;5&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;## 再check:&lt;/span&gt;
ll /proc/6099/fd
total 0
lr-x------  1 work work 64 Oct 12 17:58 0 -&amp;gt; /dev/null
l-wx------  1 work work 64 Oct 12 17:58 1 -&amp;gt; /home/disk1/xxx/4.txt
l-wx------  1 work work 64 Oct 12 17:58 2 -&amp;gt; /home/disk1/xxx/3.txt
lr-x------  1 work work 64 Oct 12 17:58 3 -&amp;gt; /home/disk1/xxx/2.txt
l-wx------  1 work work 64 Oct 12 17:58 4 -&amp;gt; /home/disk1/xxx/1.txt
lrwx------  1 work work 64 Oct 12 17:58 5 -&amp;gt; socket:[444444555]

&lt;span class=&quot;c&quot;&gt;## 发现5这个fd换了，成功了！。。&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>GAN推导</title>
   <link href="http://daiwk.github.io/posts/cv-gan-theories.html"/>
   <updated>2017-10-01T00:00:00+00:00</updated>
   <id>/posts/cv-gan-theories</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650731540&amp;amp;idx=1&amp;amp;sn=193457603fe11b89f3d298ac1799b9fd&amp;amp;chksm=871b306ab06cb97c502af9552657b8e73f1f5286bc4cc71b021f64604fd53dae3f026bc9ac69&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1001meNovATH1QnSTK2He8NG&amp;amp;pass_ticket=n3jVQ%2FEZ0xsOw3vx8T46nbM61%2Fap%2FBbN9DkzqI1A3fbBBmn5cAb1lxv7pphcVAcw#rd&quot;&gt;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650731540&amp;amp;idx=1&amp;amp;sn=193457603fe11b89f3d298ac1799b9fd&amp;amp;chksm=871b306ab06cb97c502af9552657b8e73f1f5286bc4cc71b021f64604fd53dae3f026bc9ac69&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1001meNovATH1QnSTK2He8NG&amp;amp;pass_ticket=n3jVQ%2FEZ0xsOw3vx8T46nbM61%2Fap%2FBbN9DkzqI1A3fbBBmn5cAb1lxv7pphcVAcw#rd&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github地址：&lt;a href=&quot;https://github.com/jiqizhixin/ML-Tutorial-Experiment&quot;&gt;https://github.com/jiqizhixin/ML-Tutorial-Experiment&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>occf</title>
   <link href="http://daiwk.github.io/posts/ml-occf.html"/>
   <updated>2017-09-09T00:00:00+00:00</updated>
   <id>/posts/ml-occf</id>
   <content type="html">&lt;p&gt;目录
&lt;!-- TOC --&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#aaa&quot;&gt;aaa&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#bbb&quot;&gt;bbb&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://pdfs.semanticscholar.org/7d54/15874e0f8b8f9eda49187a9030ce6bca41b1.pdf&quot;&gt;One-Class Collaborative Filtering&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在很多推荐系统中，并不像netflix是可以拿到用户的评分数据（例如，1-5分）的，像新闻推荐、书签推荐等，训练数据往往只有是否点击/是否收藏这种0/1的数据。而且，这些数据特别稀疏（正例很少），而且非正例也不一定是负例（一篇文章没点击，可能是确实不感兴趣，也可能是感兴趣但之前系统没有给他推荐过[unlabeled positive]）。之前的文章多将这种one-class problem看成一个分类问题，本文从cf的角度，提出了weighted low-rank approximation和negative example sampling两种framework来解决occf。&lt;/p&gt;

&lt;h2 id=&quot;aaa&quot;&gt;aaa&lt;/h2&gt;

&lt;h2 id=&quot;bbb&quot;&gt;bbb&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>tensorflow分布式部署</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-distributed-deploy.html"/>
   <updated>2017-09-06T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-distributed-deploy</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%8d%95%e6%9c%ba%e5%a4%9agpu&quot;&gt;单机多gpu&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%88%86%e5%b8%83%e5%bc%8f%e5%a4%9a%e6%9c%ba%e5%a4%9agpu%e8%ae%ad%e7%bb%83&quot;&gt;分布式多机多GPU训练&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%8f%82%e6%95%b0%e6%9c%8d%e5%8a%a1%e5%99%a8&quot;&gt;参数服务器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tensorflow%e7%9a%84%e5%88%86%e5%b8%83%e5%bc%8f&quot;&gt;tensorflow的分布式&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#in-graph-%e6%a8%a1%e5%bc%8f&quot;&gt;in-graph 模式&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#between-graph%e6%a8%a1%e5%bc%8f&quot;&gt;between-graph模式&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%90%8c%e6%ad%a5%e6%9b%b4%e6%96%b0%e5%92%8c%e5%bc%82%e6%ad%a5%e6%9b%b4%e6%96%b0&quot;&gt;同步更新和异步更新&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#replicadevicesetter&quot;&gt;replica_device_setter&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#round-robin%e6%a8%a1%e5%bc%8f&quot;&gt;round-robin模式：&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#greedystrategy&quot;&gt;greedystrategy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#partition&quot;&gt;partition&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#sessionis-and-servers&quot;&gt;Sessionis and servers&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fault-tolerance&quot;&gt;fault tolerance&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%95%85%e9%9a%9c%e6%81%a2%e5%a4%8d&quot;&gt;故障恢复&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.csdn.net/luodongri/article/details/52596780&quot;&gt;白话tensorflow分布式部署和开发&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/zwqjoy/article/details/81013527&quot;&gt;[深度学习] 分布式模式介绍（一）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/zwqjoy/article/details/89552866&quot;&gt;[深度学习] 分布式Tensorflow 2.0 介绍（二）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/zwqjoy/article/details/89415933&quot;&gt;[深度学习] 分布式Pytorch 1.0介绍（三）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/zwqjoy/article/details/89552432&quot;&gt;[深度学习] 分布式Horovod介绍（四）&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;单机多gpu&quot;&gt;单机多gpu&lt;/h2&gt;

&lt;p&gt;官方demo参考：&lt;a href=&quot;https://github.com/tensorflow/models/blob/master/tutorials/image/cifar10/cifar10_multi_gpu_train.py&quot;&gt;models/tutorials/image/cifar10/&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;假设你的机器上有3个GPU;&lt;/li&gt;
  &lt;li&gt;在单机单GPU的训练中，数据是一个batch一个batch的训练。在单机多GPU中，&lt;strong&gt;数据一次处理3个batch(假设是3个GPU训练），每个GPU处理一个batch的数据计算。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;变量，或者说参数，保存在CPU上&lt;/li&gt;
  &lt;li&gt;刚开始的时候数据由CPU分发给3个GPU，在GPU上完成了计算，得到每个batch要更新的梯度。&lt;/li&gt;
  &lt;li&gt;然后在CPU上收集完了3个GPU上的要更新的梯度，计算一下&lt;strong&gt;平均梯度&lt;/strong&gt;，然后更新参数。&lt;/li&gt;
  &lt;li&gt;然后继续循环这个过程。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;处理的速度取决于最慢的那个GPU的速度。如果3个GPU的处理速度差不多的话，处理速度就相当于单机单GPU的速度的3倍减去数据在CPU和GPU之间传输的开销，实际的效率提升看CPU和GPU之间数据的速度和处理数据的大小。&lt;/p&gt;

&lt;h2 id=&quot;分布式多机多gpu训练&quot;&gt;分布式多机多GPU训练&lt;/h2&gt;

&lt;p&gt;随着设计的模型越来越复杂，模型参数越来越多，越来越大，如果每次计算一轮，都要计算梯度、更新参数，当参数的量级上升到百亿量级甚至更大之后，参数的更新的性能都是问题。如果是单机16个GPU，一个step最多也是处理16个batch，这对于上TB级别的数据来说，不知道要训练到什么时候。于是就有了分布式的深度学习训练方法，或者说框架。&lt;/p&gt;

&lt;h3 id=&quot;参数服务器&quot;&gt;参数服务器&lt;/h3&gt;

&lt;p&gt;前面说道，当你的模型越来越大，模型的参数越来越多，多到模型参数的更新，一台机器的性能都不够的时候，很自然的我们就会想到把参数分开放到不同的机器去存储和更新。&lt;/p&gt;

&lt;p&gt;因为碰到上面提到的那些问题，所有参数服务器就被单独拧出来，于是就有了参数服务器的概念。参数服务器可以是多台机器组成的集群，这个就有点类似分布式的存储架构了，涉及到数据的同步，一致性等等，一般是key-value的形式，可以理解为一个分布式的key-value内存数据库，然后再加上一些参数更新的操作。反正就是当性能不够的时候，几百亿的参数分散到不同的机器上去保存和更新，解决参数存储和更新的性能问题。&lt;/p&gt;

&lt;h3 id=&quot;tensorflow的分布式&quot;&gt;tensorflow的分布式&lt;/h3&gt;

&lt;h4 id=&quot;in-graph-模式&quot;&gt;in-graph 模式&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-in-graph.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;一个client(显示调用tf::Session的进程),将里面的参数和ops指定给对应的job去完成.数据分发只由一个client完成.&lt;/p&gt;

&lt;p&gt;in-graph模式和单机多GPU模型有点类似。in-graph模式，把计算已经从单机多GPU，已经扩展到了多机多GPU了，不过数据分发还是在一个节点。这样的好处是配置简单，其他多机多GPU的计算节点，只要起个join操作，暴露一个网络接口，等在那里接受任务就好了。这些计算节点暴露出来的网络接口，使用起来就跟本机的一个GPU的使用一样，只要在操作的时候指定tf.device(“/job:worker/task:n”)，就可以向指定GPU一样，把操作指定到一个计算节点上计算，使用起来和多GPU的类似。&lt;strong&gt;但是这样的坏处是训练数据的分发依然在一个节点上，要把训练数据分发到不同的机器上，严重影响并发训练速度。&lt;/strong&gt;在大数据训练的情况下，不推荐使用这种模式。&lt;/p&gt;

&lt;h4 id=&quot;between-graph模式&quot;&gt;between-graph模式&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-between-graph.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;有很多独立的client,各个client构建了相同的graph(包含参数,通过使用tf.train.replica_device_setter,将这些参数映射到ps_server上.)&lt;/p&gt;

&lt;p&gt;between-graph模式下，训练的参数保存在参数服务器，&lt;strong&gt;数据不用分发，数据分片的保存在各个计算节点，各个计算节点自己算自己的，&lt;/strong&gt;算完了之后，把要更新的参数告诉参数服务器，参数服务器更新参数。这种模式的优点是不用训练数据的分发了，尤其是在数据量在TB级的时候，节省了大量的时间，所以大数据深度学习还是推荐使用between-graph模式。&lt;/p&gt;

&lt;h3 id=&quot;同步更新和异步更新&quot;&gt;同步更新和异步更新&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;in-graph模式和between-graph模式都支持同步和异步更新&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;在同步更新的时候，每次梯度更新，要等所有分发出去的数据计算完成后，返回回来结果之后，把梯度累加算了均值之后，再更新参数。这样的好处是loss的下降比较稳定，但是这个的坏处也很明显，处理的速度取决于最慢的那个分片计算的时间。&lt;/p&gt;

&lt;p&gt;在异步更新的时候，所有的计算节点，各自算自己的，更新参数也是自己更新自己计算的结果，这样的优点就是计算速度快，计算资源能得到充分利用，但是缺点是loss的下降不稳定，抖动大。&lt;/p&gt;

&lt;p&gt;在数据量小的情况下，各个节点的计算能力比较均衡的情况下，推荐使用同步模式；数据量很大，各个机器的计算性能掺差不齐的情况下，推荐使用异步的方式。&lt;/p&gt;

&lt;p&gt;官方分布式文档：&lt;a href=&quot;https://www.tensorflow.org/deploy/distributed&quot;&gt;https://www.tensorflow.org/deploy/distributed&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;replica_device_setter&quot;&gt;replica_device_setter&lt;/h2&gt;

&lt;h3 id=&quot;round-robin模式&quot;&gt;round-robin模式：&lt;/h3&gt;

&lt;p&gt;什么参数都不传，默认是round-robin&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-replica_device_setter.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;但如果只有两个ps任务，可能会把weight都给一个，bias都给另一个，这样就很不利。&lt;/p&gt;

&lt;h3 id=&quot;greedystrategy&quot;&gt;greedystrategy&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-replica_device_setter-strategy.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;依据变量占用的内存，会相对负载均衡。&lt;/p&gt;

&lt;h3 id=&quot;partition&quot;&gt;partition&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-replica_device_setter-partition.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;如果embedding需要lookup的表非常大，可以直接指定partition，这样在查表的时候，也只会去相应的partition查。&lt;/p&gt;

&lt;h2 id=&quot;sessionis-and-servers&quot;&gt;Sessionis and servers&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-sessions-and-servers.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;ClusterSpec是一个字典，每个节点都存同一份（如果是K8s之类的，会有集群管理者）&lt;/p&gt;

&lt;p&gt;ps代码：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-sessions-and-servers-ps.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;fault-tolerance&quot;&gt;fault tolerance&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-failure-tolerance-saver.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;设置saver的sharded=True，因为例如有3个ps，那么每个的saver就会保存它自己的全部变量。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-failure-tolerance-saver-chief.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;save的条件是，只有是chief(完成初始化参数、写检查点、写结果给tensorboard等)的时候才写。另外，写文件的地址可以是hdfs、google cloud等。&lt;/p&gt;

&lt;h3 id=&quot;故障恢复&quot;&gt;故障恢复&lt;/h3&gt;

&lt;p&gt;如果一个非chief的worker挂了，问题不大，重新启动一个就行了&lt;/p&gt;

&lt;p&gt;如果一个ps挂了,chief会暂停所有worker的作业，并在上一个检查点恢复所有ps任务&lt;/p&gt;

&lt;p&gt;如果chief挂了，类似ps挂，全部暂停，等故障修复后，把chief恢复到上一个检查点的状态。当然，也可以用zookeeper，用选举的机制，选出一个新的chief，而不用全部暂停。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-failure-tolerance-monitoredTrainingSession.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;MonitoredTrainingSession每10min都随着编写检查点的hook出现一次，每100个steps会写一次summary&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-arch.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;高level的api:experiment:&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/distributed-tf-experiment.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;详见：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/deploy/distributed&quot;&gt;https://www.tensorflow.org/deploy/distributed&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/extend/architecture&quot;&gt;https://www.tensorflow.org/extend/architecture&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/extend/estimators&quot;&gt;https://www.tensorflow.org/extend/estimators&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;简单的demo:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/thewintersun/distributeTensorflowExample&quot;&gt;https://github.com/thewintersun/distributeTensorflowExample&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>各框架的embedding实现</title>
   <link href="http://daiwk.github.io/posts/platform-embedding-implementation.html"/>
   <updated>2017-08-30T00:00:00+00:00</updated>
   <id>/posts/platform-embedding-implementation</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E6%9C%AC%E6%80%9D%E6%83%B3&quot;&gt;基本思想&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#keras&quot;&gt;keras&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#paddle&quot;&gt;paddle&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tensorflow&quot;&gt;tensorflow&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#talk-to-book&quot;&gt;talk to book&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%BA%94%E7%94%A8&quot;&gt;应用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#xx%E7%8E%87%E9%A2%84%E4%BC%B0&quot;&gt;xx率预估&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E6%A8%A1%E5%9E%8B%E7%BB%93%E6%9E%84&quot;&gt;模型结构&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E7%BC%96%E7%A0%81%E6%96%B9%E5%BC%8Fembedding&quot;&gt;编码方式（embedding）&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#%E9%9A%8F%E6%9C%BA%E7%BC%96%E7%A0%81&quot;&gt;随机编码&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E6%8C%82%E9%9D%A0%E7%BC%96%E7%A0%81&quot;&gt;挂靠编码&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%88%86%E8%AF%8D%E7%BC%96%E7%A0%81&quot;&gt;分词编码&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#online-update&quot;&gt;Online Update&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;基本思想&quot;&gt;基本思想&lt;/h2&gt;

&lt;p&gt;假设输入一个词，想把它embedding成一个向量。词典大小假设是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;，embedding向量的维度假设是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;。那么，相当于输入一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;维的&lt;strong&gt;one-hot&lt;/strong&gt;向量，输出一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;维的向量，中间就是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\times K\)&lt;/code&gt;的矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W\)&lt;/code&gt;。假设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K=3,V=2\)&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
(x_0,x_1)\times \begin{bmatrix}
w_{00} &amp;amp; w_{01} &amp;amp;w_{02} \\ 
w_{10} &amp;amp; w_{11} &amp;amp; w_{12}
\end{bmatrix}=\begin{bmatrix}
x_0w_{00}+x_1w_{10}\\ 
x_0w_{01}+x_1w_{11}\\ 
x_0w_{02}+x_1w_{12}
\end{bmatrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;举例，如果x=(0,1)那相当于是&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
(0,1)\times \begin{bmatrix}
w_{00} &amp;amp; w_{01} &amp;amp;w_{02} \\ 
w_{10} &amp;amp; w_{11} &amp;amp; w_{12}
\end{bmatrix}=\begin{bmatrix}
0w_{00}+1w_{10}\\ 
0w_{01}+1w_{11}\\ 
0w_{02}+1w_{12}
\end{bmatrix}=\begin{bmatrix}
w_{10}\\ 
w_{11}\\ 
w_{12}
\end{bmatrix}    
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;也就是说，&lt;strong&gt;选取了权重矩阵的第1行的值&lt;/strong&gt;出来，&lt;strong&gt;再转置一下，变成列向量&lt;/strong&gt;（即常说的lookup，或者paddle里的table_projection）。同理，如果x=(1,0)，那么相当于选取权重矩阵的第0行出来。&lt;/p&gt;

&lt;p&gt;而特殊地，如果x不是one-hot，是一个可以有多个1的vector（例如，某个特征，可能同时取多个值，比如x有6维，每一维表示1种颜色，而这个特征是图中包含了哪几种颜色，可能有的图片里有3种颜色），那么，得到的每一维embedding就相当于把对应的几行选择出来，然后每行里每列对应的值相加。更直观地，假设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V=3,K=4\)&lt;/code&gt;，假设x=(0,1,1)：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
(0,1,1)\times \begin{bmatrix}
w_{00} &amp;amp; w_{01} &amp;amp;w_{02} &amp;amp;w_{03}\\ 
w_{10} &amp;amp; w_{11} &amp;amp; w_{12} &amp;amp;w_{13}\\
w_{20} &amp;amp; w_{21} &amp;amp; w_{22} &amp;amp;w_{23}\\
\end{bmatrix}=\begin{bmatrix}
0w_{00}+1w_{10}+1w_{20}\\ 
0w_{01}+1w_{11}+1w_{21}\\ 
0w_{02}+1w_{12}+1w_{22}\\
0w_{03}+1w_{13}+1w_{23}\\
\end{bmatrix}=\begin{bmatrix}
w_{10}+w_{20}\\ 
w_{11}+w_{21}\\ 
w_{12}+w_{22}\\
w_{13}+w_{23}
\end{bmatrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;相当于把权重矩阵的第1行和第2行拿出来，然后对应元素相加，再转置一下。&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;keras&quot;&gt;keras&lt;/h2&gt;

&lt;p&gt;中文文档：&lt;a href=&quot;https://keras.io/zh/layers/embeddings/&quot;&gt;https://keras.io/zh/layers/embeddings/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras.io/layers/embeddings/&quot;&gt;https://keras.io/layers/embeddings/&lt;/a&gt; keras的embedding是参考&lt;a href=&quot;https://arxiv.org/pdf/1512.05287.pdf&quot;&gt;A Theoretically Grounded Application of Dropout in Recurrent Neural Networks&lt;/a&gt;这个实现的，类似对输入数据进行dropout。&lt;/p&gt;

&lt;h2 id=&quot;paddle&quot;&gt;paddle&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/issues/2910&quot;&gt;https://github.com/PaddlePaddle/Paddle/issues/2910&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;tensorflow&quot;&gt;tensorflow&lt;/h2&gt;

&lt;p&gt;代码（不止这些）：
&lt;a href=&quot;https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/lookup_ops.py&quot;&gt;https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/lookup_ops.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/embedding_ops.py&quot;&gt;https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/embedding_ops.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;embedding_lookup和embedding_lookup_sparse:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/huhu0769/article/details/71169346&quot;&gt;https://blog.csdn.net/huhu0769/article/details/71169346&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;tensorflow的embedding操作：
&lt;a href=&quot;https://memeda.github.io/%E6%8A%80%E6%9C%AF/2017/04/13/tfembedding.html&quot;&gt;https://memeda.github.io/%E6%8A%80%E6%9C%AF/2017/04/13/tfembedding.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如何使用预训练的embedding来初始化tf的embedding_lookup: 
&lt;a href=&quot;https://stackoverflow.com/questions/35687678/using-a-pre-trained-word-embedding-word2vec-or-glove-in-tensorflow&quot;&gt;https://stackoverflow.com/questions/35687678/using-a-pre-trained-word-embedding-word2vec-or-glove-in-tensorflow&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;talk-to-book&quot;&gt;talk to book&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-talk-to-book.html&quot;&gt;https://daiwk.github.io/posts/dl-talk-to-book.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;应用&quot;&gt;应用&lt;/h2&gt;

&lt;h3 id=&quot;xx率预估&quot;&gt;xx率预估&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://www.cnblogs.com/hujiapeng/p/6236857.html&quot;&gt;DNN在搜索场景中的应用&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;模型结构&quot;&gt;模型结构&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;wide model
    &lt;ul&gt;
      &lt;li&gt;id feature: item_id, seller_id，学习已经出现过的商品，卖家在训练数据上的表现。&lt;/li&gt;
      &lt;li&gt;id cross feature: user_id x item_id , user_id x seller_id&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;连续值统计特征是非常有用的特征，Google的模型是把embedding向量和统计特征放到同一个DNN网络中学习，但实验发现这样会削弱统计特征的作用。我们为统计特征专门又组建了一个包含2个隐层的网路，并且为了增强非线性效果，激活函数从RELU改为TanH/Sigmiod。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;deep model
    &lt;ul&gt;
      &lt;li&gt;首先需要把离散特征（item_id，item_tag, user_id，user_tag，query_tag）embedding成连续特征。&lt;/li&gt;
      &lt;li&gt;将embedding后的向量作为DNN的输入。考虑到最终线上预测性能的问题，目前我们的DNN网络还比较简单，只有1到2个隐层。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;整体模型使用三层全连接层用于sparse+dense特征表征学习，再用两层全连接层用于点击/购买与否分类的统一深度学习模型解决方案：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一层为编码层，包含商品编码，店家编码，类目编码，品牌编码，搜索词编码和用户编码。&lt;/li&gt;
  &lt;li&gt;从第二层到第四层组成了“域间独立”的“行为编码网络”，其中第二层为针对稀疏编码特别优化过的全连接层( Sparse Inner Product Layer )，通过该层将压缩后的编码信息投影到16维的低维向量空间中，第三层和第四层均为普通全连接层，其输出维度分别为16和32。“行为编码网络”也可以被看做是针对域信息的二次编码，但是与第一层不同，这部分的最终输出是基于行为数据所训练出来的结果，具有行为上相似的商品或者用户的最终编码更相近的特性。&lt;/li&gt;
  &lt;li&gt;第五层为concat层，其作用是将不同域的信息拼接到一起。&lt;/li&gt;
  &lt;li&gt;第六层到第八层网络被称为“预测网络”，该部分由三层全连接组成，隐层输出分别为64,64和1。该部分的作用在于综合考虑不同域之间的信息后给出一个最终的排序分数。&lt;/li&gt;
  &lt;li&gt;最后，Softmax作为损失函数被用在训练过程中; 非线性响应函数被用在每一个全连接之后。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在普适的CTR场景中，用户、商品、查询等若干个域的特征维度合计高达几十亿，假设在输入层后直接连接100个输出神经元的全连接层，那么这个模型的参数规模将达到千亿规模。直接接入全连接层将导致以下几个问题：1. 各个域都存在冷门的特征，这些冷门的特征将会被热门的特征淹没，基本不起作用，跟全连接层的连接边权值会趋向于0，冷门的商品只会更冷门。2. 模型的大小将会非常庞大，超过百G，在训练以及预测中都会出现很多工程上的问题。为了解决上述两个问题，本文引入了紫色编码层，具体分为以下两种编码方式：1. 随机编码 2. 挂靠编码，下面将对以上两种编码方式进行详细的描述。&lt;/p&gt;

&lt;h4 id=&quot;编码方式embedding&quot;&gt;编码方式（embedding）&lt;/h4&gt;
&lt;h5 id=&quot;随机编码&quot;&gt;随机编码&lt;/h5&gt;

&lt;p&gt;假设某一域的输入ID类特征的one-hot形式最大维度为N，&lt;/p&gt;

&lt;h5 id=&quot;挂靠编码&quot;&gt;挂靠编码&lt;/h5&gt;

&lt;h5 id=&quot;分词编码&quot;&gt;分词编码&lt;/h5&gt;

&lt;h4 id=&quot;online-update&quot;&gt;Online Update&lt;/h4&gt;

&lt;p&gt;双11当天数据分布会发生巨大变化，为了能更好的fit实时数据，我们将WDL的一部分参数做了在线实时训练。embeding层由于参数过多，并没有在线训练，其他模型参数都会在线学习更新。&lt;/p&gt;

&lt;p&gt;deep端网络参数和wide端参数更新的策略有所不同，wide端是大规模稀疏特征，为了使训练结果有稀疏性，最好用FTRL来做更新。deep端都是稠密连续特征，使用的普通的SGD来做更新，学习率最好设置小一点。&lt;/p&gt;

&lt;p&gt;和离线Batch training不同，Online learning会遇到一些特有的问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;实时streaming样本分布不均匀&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;现象：线上环境比较复杂，不同来源的日志qps和延迟都不同，造成不同时间段样本分布不一样，甚至在短时间段内样本分布异常。比如整体一天下来正负例1:9，如果某类日志延迟了，短时间可能全是负例，或者全是正例。 解决：Pairwise sampling。Pv日志到了后不立即产出负样本，而是等点击到了后找到关联的pv，然后把正负样本一起产出，这样的话就能保证正负样本总是1:9&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;异步SGD更新造成模型不稳定&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;现象：权重学飘掉(非常大或者非常小)，权重变化太大。解决：mini batch，一批样本梯度累加到一起，更新一次。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>keras使用多核cpu</title>
   <link href="http://daiwk.github.io/posts/platform-keras-multi-cpu.html"/>
   <updated>2017-08-29T00:00:00+00:00</updated>
   <id>/posts/platform-keras-multi-cpu</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#使用theano后端&quot;&gt;使用theano后端&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#开启theano后端&quot;&gt;开启theano后端&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#打开openmp的flag&quot;&gt;打开openmp的flag&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#确保gcc版本&quot;&gt;确保gcc版本&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#设置环境变量并运行&quot;&gt;设置环境变量并运行&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#使用tensorflow后端&quot;&gt;使用tensorflow后端&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#用htop查看进程情况&quot;&gt;用htop查看进程情况&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;使用theano后端&quot;&gt;使用theano后端&lt;/h2&gt;

&lt;h3 id=&quot;开启theano后端&quot;&gt;开启theano后端&lt;/h3&gt;

&lt;p&gt;vim &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.keras/keras.json&lt;/code&gt;，修改为&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
    &quot;epsilon&quot;: 1e-07,
    &quot;floatx&quot;: &quot;float32&quot;,
    &quot;image_data_format&quot;: &quot;channels_last&quot;,
    &quot;backend&quot;: &quot;theano&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;打开openmp的flag&quot;&gt;打开openmp的flag&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;theano&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;theano&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;确保gcc版本&quot;&gt;确保gcc版本&lt;/h3&gt;

&lt;p&gt;至少要是482的&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export PATH=/opt/compiler/gcc-4.8.2/bin/:$PATH
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;设置环境变量并运行&quot;&gt;设置环境变量并运行&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;OMP_NUM_THREADS=20 python xxx.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;使用tensorflow后端&quot;&gt;使用tensorflow后端&lt;/h2&gt;
&lt;p&gt;参考：&lt;a href=&quot;http://www.deepnlp.org/blog/tensorflow-parallelism/&quot;&gt;Tensorflow并行：多核(multicore)，多线程(multi-thread)&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;用htop查看进程情况&quot;&gt;用htop查看进程情况&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jumbo install htop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;htop样例如下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/htop.png&quot; style=&quot;max-height: 200px&quot; /&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>perl</title>
   <link href="http://daiwk.github.io/posts/knowledge-perl.html"/>
   <updated>2017-08-28T00:00:00+00:00</updated>
   <id>/posts/knowledge-perl</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#单双引号&quot;&gt;单/双引号&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#here文档&quot;&gt;Here文档&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#数据类型&quot;&gt;数据类型&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#haha&quot;&gt;haha&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;单双引号&quot;&gt;单/双引号&lt;/h2&gt;
&lt;p&gt;双引号可以正常解析一些转义字符与变量，而单引号无法解析会原样输出。&lt;/p&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;a = &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# a = 10&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;a = $a\n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;';&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# a = $a\n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;here文档&quot;&gt;Here文档&lt;/h2&gt;

&lt;p&gt;Here文档又称作heredoc、hereis、here-字串或here-脚本，是一种在命令行shell（如sh、csh、ksh、bash、PowerShell和zsh）和程序语言（像Perl、PHP、Python和Ruby）里定义一个字串的方法。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;1.必须后接分号，否则编译通不过。&lt;/li&gt;
  &lt;li&gt;2.END可以用任意其它字符代替，只需保证结束标识与开始标识一致。&lt;/li&gt;
  &lt;li&gt;3.结束标识必须顶格独自占一行(即必须从行首开始，前后不能衔接任何空白和字符)。&lt;/li&gt;
  &lt;li&gt;4.开始标识可以不带引号号或带单双引号，不带引号与带双引号效果一致，解释内嵌的变量和转义符号，带单引号则不解释内嵌的变量和转义符号。&lt;/li&gt;
  &lt;li&gt;5.当内容需要内嵌引号（单引号或双引号）时，不需要加转义符，本身对单双引号转义，此处相当与q和qq的用法。&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/perl&lt;/span&gt;
 
&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;lt;&amp;lt;&quot;EOF&quot;;
这是一个 Here 文档实例，使用双引号。
可以在这输如字符串和变量。
例如：a = $a
EOF
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$var&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;;&lt;/span&gt;
 
&lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;lt;&amp;lt;'EOF';
这是一个 Here 文档实例，使用单引号。
例如：a = $a
EOF
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$var&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 输出&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 这是一个 Here 文档实例，使用双引号。&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 可以在这输如字符串和变量。&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 例如：a = 10&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 这是一个 Here 文档实例，使用单引号。&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 例如：a = $a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;数据类型&quot;&gt;数据类型&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;标量
    &lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$myfirst&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;#数字123　&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$mysecond&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;#字符串123　&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;数组
```perl
@arr=(1,2,3)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;@names = (‘google’, ‘runoob’, ‘taobao’);&lt;/p&gt;

&lt;p&gt;@copy = @names;   # 复制数组
$size = @names;   # 数组赋值给标量，返回数组元素个数&lt;/p&gt;

&lt;p&gt;print “名字为 : @copy\n”;
print “名字数为 : $size\n”;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
@names 是一个数组，它应用在了两个不同的上下文中。第一个将其复制给另外一个数组，所以它输出了数组的所有元素。第二个我们将数组赋值给一个标量，它返回了数组的元素个数。

+ 哈希
```perl
%h=('a'=&amp;gt;1,'b'=&amp;gt;2); 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;各种转义字符的意思详见&lt;a href=&quot;http://www.runoob.com/perl/perl-data-types.html&quot;&gt;http://www.runoob.com/perl/perl-data-types.html&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-perl highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/perl&lt;/span&gt;
 
&lt;span class=&quot;nv&quot;&gt;%data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;('&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;',&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;runoob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;',&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;taobao&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;',&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
 
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;data{'google'} = &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;{'google'}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;data{'runoob'} = &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;{'runoob'}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;data{'taobao'} = &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;{'taobao'}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&quot;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;所谓上下文：指的是表达式所在的位置。
上下文是由等号左边的变量类型决定的，等号左边是标量，则是标量上下文，等号左边是列表，则是列表上下文。&lt;/p&gt;

&lt;h2 id=&quot;haha&quot;&gt;haha&lt;/h2&gt;
</content>
 </entry>
 
 <entry>
   <title>推荐系统</title>
   <link href="http://daiwk.github.io/posts/ml-recommender-systems.html"/>
   <updated>2017-08-24T00:00:00+00:00</updated>
   <id>/posts/ml-recommender-systems</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#als-wr&quot;&gt;ALS-WR&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#svd&quot;&gt;SVD&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#svd&quot;&gt;SVD++&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rbm-cf&quot;&gt;RBM-CF&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#occf&quot;&gt;OCCF&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#eals&quot;&gt;eALS&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#related-work&quot;&gt;related work&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#预备知识&quot;&gt;预备知识&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#als&quot;&gt;ALS&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#使用uniform-weighting来加速&quot;&gt;使用Uniform Weighting来加速&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#generic-element-wise-als-learner&quot;&gt;generic element-wise ALS learner&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.csdn.net/dark_scope/article/details/17228643&quot;&gt;从item-base到svd再到rbm，多种Collaborative Filtering(协同过滤算法)从原理到实现&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;als-wr&quot;&gt;ALS-WR&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://endymecy.gitbooks.io/spark-ml-source-analysis/content/%E6%8E%A8%E8%8D%90/papers/Large-scale%20Parallel%20Collaborative%20Filtering%20the%20Netflix%20Prize.pdf&quot;&gt;Large-scale Parallel Collaborative Filtering for the Netflix Prize&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;目标是最小化：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(U, M)=\sum_{(i, j) \in I}\left(r_{i j}-\mathbf{u}_{i}^{T} \mathbf{m}_{j}\right)^{2}+\lambda\left(\sum_{i} n_{u_{i}}\left\|\mathbf{u}_{i}\right\|^{2}+\sum_{j} n_{m_{j}}\left\|\mathbf{m}_{j}\right\|^{2}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;U矩阵的shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n_f\times n_u\)&lt;/code&gt;，M矩阵的shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n_f\times n_m\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;核心解法是：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;M矩阵的第一行（也就是所有item的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n_f\)&lt;/code&gt;个特征的第1个特征）初始化为该Item的平均分，其他位置设为一个比较小的随机值&lt;/li&gt;
  &lt;li&gt;固定M，通过最小化目标函数求解U&lt;/li&gt;
  &lt;li&gt;固定U，通过最小化目标函数求解M&lt;/li&gt;
  &lt;li&gt;重复以上两步，直到满足终止条件&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;首先，当M固定时，来更新U。令u的导数为0，经过如下推导，可以得到&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\begin{aligned} &amp;amp; \frac{1}{2} \frac{\partial f}{\partial u_{k i}}=0, \quad \forall i, k \\ \Rightarrow &amp;amp; \sum_{j \in I_{i}}\left(\mathbf{u}_{i}^{T} \mathbf{m}_{j}-r_{i j}\right) m_{k j}+\lambda n_{u_{i}} u_{k i}=0, \quad \forall i, k \\ \Rightarrow &amp;amp; \sum_{j \in I_{i}} m_{k j} \mathbf{m}_{j}^{T} \mathbf{u}_{i}+\lambda n_{u_{i}} u_{k i}=\sum_{j \in I_{i}} m_{k j} r_{i j}, \quad \forall i, k \\ \Rightarrow &amp;amp;\left(M_{I_{i}} M_{I_{i}}^{T}+\lambda n_{u_{i}} E\right) \mathbf{u}_{i}=M_{I_{i}} R^{T}\left(i, I_{i}\right), \quad \forall i \\ \Rightarrow &amp;amp; \mathbf{u}_{i}=A_{i}^{-1} V_{i}, &amp;amp; \forall i \end{aligned}\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A_{i}=M_{I_{i}} M_{I_{i}}^{T}+\lambda n_{u_{i}} E\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V_{i}=M_{I_{i}} R^{T}\left(i, I_{i}\right)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E\)&lt;/code&gt;是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n_{f} \times n_{f}\)&lt;/code&gt;的identity矩阵(单位矩阵，对角线为1，其他位置是0)。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M_{I_{i}}\)&lt;/code&gt;是从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;中把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j \in I_{i}\)&lt;/code&gt;这些列选出来组成的矩阵&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\left(i, I_{i}\right)\)&lt;/code&gt;是取出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;矩阵的第i行，然后再取出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j \in I_{i}\)&lt;/code&gt;这些列得到的行向量&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;类似地，固定U来更新M：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\mathbf{m}_{j}=A_{j}^{-1} V_{j}, \quad \forall j
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;同理，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A_{j}=U_{I_{j}} U_{I_{j}}^{T}+\lambda n_{m_{j}} E\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V_{j}=U_{I_{j}} R\left(I_{j}, j\right)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(U_{I_{j}}\)&lt;/code&gt;是从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(U\)&lt;/code&gt;中把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i \in I_{j}\)&lt;/code&gt;这些列取出来组成的矩阵&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\left(I_{j}, j\right)\)&lt;/code&gt;是取出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;的第j列，然后再取出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i \in I_{j}\)&lt;/code&gt;这些行组成的列向量&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;本文是用matlab来解的，了解一下求逆的时候用到的左除和右除。。参考&lt;a href=&quot;https://blog.csdn.net/qq_40655270/article/details/78250699&quot;&gt;https://blog.csdn.net/qq_40655270/article/details/78250699&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;左除：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Ax=b\)&lt;/code&gt;得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x=A^{-1}b\)&lt;/code&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A^{-1}\)&lt;/code&gt;可以看成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1/A\)&lt;/code&gt;，也就是说A在左边，而A是分母，所以，在matlab中写成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A\b&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;右除：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(xA=b\)&lt;/code&gt;得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x=bA^{-1}\)&lt;/code&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A^{-1}\)&lt;/code&gt;可以看成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1/A\)&lt;/code&gt;，所以在matlab中写成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b/A&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;所以论文里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X = matrix \ vector&lt;/code&gt;就是matrix的逆乘以vector&lt;/p&gt;

&lt;h2 id=&quot;svd&quot;&gt;SVD&lt;/h2&gt;

&lt;p&gt;hinton 07年提出的&lt;a href=&quot;https://www.cs.toronto.edu/~rsalakhu/papers/rbmcf.pdf&quot;&gt;Restricted boltzmann machines for collaborative filtering&lt;/a&gt;里面提到了用sgd来训练svd：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} f &amp;amp;=\sum_{i=1}^{N} \sum_{j=1}^{M} I_{i j}\left(\mathbf{u}_{\mathbf{i}} \mathbf{v}_{\mathbf{j}}^{\prime}-Y_{i j}\right)^{2} \\ &amp;amp;+\lambda \sum_{i j} I_{i j}\left(\left\|\mathbf{u}_{\mathbf{i}}\right\|_{F r o}^{2}+\left\|\mathbf{v}_{\mathbf{j}}\right\|_{F r o}^{2}\right) \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而在Koren的&lt;a href=&quot;https://datajobs.com/data-science-repo/Collaborative-Filtering-[Koren-and-Bell].pdf&quot;&gt;Advances in collaborative filtering&lt;/a&gt;有详细的介绍，其实就是加上一个user bias，再加一个item bias：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{r}_{u i}=\mu+b_{i}+b_{u}+q_{i}^{T} p_{u}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;优化目标就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\min _{b_{*}, q_{*}, p_{*}} \sum_{(u, i) \in \mathscr{K}}\left(r_{u i}-\mu-b_{i}-b_{u}-q_{i}^{T} p_{u}\right)^{2}+\lambda_{4}\left(b_{i}^{2}+b_{u}^{2}+\left\|q_{i}\right\|^{2}+\left\|p_{u}\right\|^{2}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;定义残差&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e_{u i} \stackrel{\mathrm{def}}{=} r_{u i}-\hat{r}_{u i}\)&lt;/code&gt;，使用梯度下降更新：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{array}{l}{b_{u} \leftarrow b_{u}+\gamma \cdot\left(e_{u i}-\lambda_{4} \cdot b_{u}\right)} \\ {b_{i} \leftarrow b_{i}+\gamma \cdot\left(e_{u i}-\lambda_{4} \cdot b_{i}\right)} \\ {q_{i} \leftarrow q_{i}+\gamma \cdot\left(e_{u i} \cdot p_{u}-\lambda_{4} \cdot q_{i}\right)} \\ {p_{u} \leftarrow p_{u}+\gamma \cdot\left(e_{u i} \cdot q_{i}-\lambda_{4} \cdot p_{u}\right)}\end{array}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;svd-1&quot;&gt;SVD++&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://datajobs.com/data-science-repo/Collaborative-Filtering-[Koren-and-Bell].pdf&quot;&gt;Advances in collaborative filtering&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;附：简单说一下svd++，就是user向量再加上这个用户的邻域信息(图中的y是用户的N(u)个历史item的隐式反馈)：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{r}_{u i}=\mu+b_{i}+b_{u}+q_{i}^{T}\left(p_{u}+|\mathrm{R}(u)|^{-\frac{1}{2}} \sum_{j \in \mathrm{R}(u)} y_{j}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;梯度下降：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{array}{l}{b_{u} \leftarrow b_{u}+\gamma \cdot\left(e_{u i}-\lambda_{5} \cdot b_{u}\right)} \\ {b_{i} \leftarrow b_{i}+\gamma \cdot\left(e_{u i}-\lambda_{5} \cdot b_{i}\right)} \\ {q_{i} \leftarrow q_{i}+\gamma \cdot\left(e_{u i} \cdot\left(p_{u}+|\mathrm{R}(u)|^{-\frac{1}{2}} \sum_{j \in \mathrm{R}(u)} y_{j}\right)-\lambda_{6} \cdot q_{i}\right)} \\ {p_{u} \leftarrow p_{u}+\gamma \cdot\left(e_{u i} \cdot q_{i}-\lambda_{6} \cdot p_{u}\right)} \\ {\forall j \in \mathrm{R}(u) :} \\ {y_{j} \leftarrow y_{j}+\gamma \cdot\left(e_{u i} \cdot|\mathrm{R}(u)|^{-\frac{1}{2}} \cdot q_{i}-\lambda_{6} \cdot y_{j}\right)}\end{array}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;rbm-cf&quot;&gt;RBM-CF&lt;/h2&gt;

&lt;p&gt;hinton 07年提出的&lt;a href=&quot;https://www.cs.toronto.edu/~rsalakhu/papers/rbmcf.pdf&quot;&gt;Restricted boltzmann machines for collaborative filtering&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;occf&quot;&gt;OCCF&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://www.rongpan.net/publications/pan-oneclasscf.pdf&quot;&gt;One-Class Collaborative Filtering&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对missing data有两种权重策略：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AMAU：All missing as unknown，观测到的数据的权重置成1，missing data的权重全部设成0。&lt;/li&gt;
  &lt;li&gt;AMAN：All missing as negative。观测到的数据的权重置成1，missing data的权重全部设成1。（论文里没直接提到，不过好像是这么个意思。。）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;wALS的损失函数就是要最小化：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} \mathcal{L}(\boldsymbol{U}, \boldsymbol{V})=&amp;amp; \sum_{i j} W_{i j}\left(R_{i j}-\boldsymbol{U}_{i .} \boldsymbol{V}_{j .}^{T}\right)^{2} \\ &amp;amp;+\lambda\left(\|\boldsymbol{U}\|_{F}^{2}+\|\boldsymbol{V}\|_{F}^{2}\right) \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;固定V，对U求导，令导数为0，去更新U的时候，推了半天，得到公式(6)&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{array}{r}{\boldsymbol{U}_{i .}=\boldsymbol{R}_{i .} \widetilde{\boldsymbol{W}_{i .}} \boldsymbol{V}\left(\boldsymbol{V}^{T} \widetilde{\boldsymbol{W}_{i .}} \boldsymbol{V}+\lambda\left(\sum_{j} W_{i j}\right) \boldsymbol{I}\right)^{-1}} \\ {\forall 1 \leq i \leq m}\end{array}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\widetilde{\boldsymbol{W}_{i .}} \in \mathfrak{R}^{n \times n}\)&lt;/code&gt;是一个对角矩阵，对角线元素为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{W}_{i .}\)&lt;/code&gt;，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(I\)&lt;/code&gt;是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\times d\)&lt;/code&gt;的单位矩阵（主对角线全1，其他全0）&lt;/p&gt;

&lt;p&gt;而固定U，对V求导，令导数为0，更新V，得到公式(7)：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{array}{r}{\boldsymbol{V}_{j .}=\boldsymbol{R}_{. j}^T \widetilde{\boldsymbol{W}_{. j}} \boldsymbol{U}\left(\boldsymbol{U}^{T} \widetilde{\boldsymbol{W}_{. j}} \boldsymbol{U}+\lambda\left(\sum_{j} W_{i j}\right) \boldsymbol{I}\right)^{-1}} \\ {\forall 1 \leq j \leq n}\end{array}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;类似地，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\widetilde{\boldsymbol{W}_{. j}} \in \mathfrak{R}^{m \times m}\)&lt;/code&gt;是一个对角矩阵，对角线元素为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{W}_{. j}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;然后整个wALS的过程就是不断地交替更新U和V直到满足收敛条件。&lt;/p&gt;

&lt;p&gt;注意，这里的权重不是可训练的参数，是一开始以某种方式进行分配的，论文给出如下三种权重分配方式：&lt;/p&gt;

&lt;p&gt;正样本，全部都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{i j}=1\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;对于missing data：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Uniform方式：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{i j}=\delta\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\delta \in[0,1]\)&lt;/code&gt;，每个missing data采用同一个权重&lt;/li&gt;
  &lt;li&gt;User-Oriented方式：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{i j} \propto \sum_{j} R_{i j}\)&lt;/code&gt;。相当于对于一个用户来讲，如果他的正例比较多，说明他已经表达了很充分『什么样的东西他喜欢』了，那么他的missing item是负样本的概率更大&lt;/li&gt;
  &lt;li&gt;Item-Oriented方式：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{i j} \propto m-\sum_{i} R_{i j}\)&lt;/code&gt;，如果一个item已经被很多用户喜欢了，那么它成为负样本的概率就更小了&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;eals&quot;&gt;eALS&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1708.05024.pdf&quot;&gt;Fast Matrix Factorization for Online Recommendation with Implicit Feedback&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;简介&quot;&gt;简介&lt;/h3&gt;

&lt;p&gt;以往的MF模型对于missing data，都是直接使用uniform weight(均匀分布)。然而在真实场景下，这个均匀分布的假设往往是不成立的。而且很多offline表现好的，到了动态变化的online场景上，往往表现不好。&lt;/p&gt;

&lt;p&gt;文章使用&lt;strong&gt;item的popularity&lt;/strong&gt;来给missing data权重，并提出了element-wise Alternating Least Squares(&lt;strong&gt;eALS&lt;/strong&gt;)，来对missing data的权重是变量的问题进行学习。对于新的feedback，设计了增量更新的策略。对于两个offline和online的公开数据集，eALS都能取得比sota的隐式MF方法更好的效果。&lt;/p&gt;

&lt;p&gt;之前的MF大多关注显式反馈，也就是用户的打分行为直接表达了用户对item的喜好程度。这种建模方式是基于这样一种假设：&lt;strong&gt;大量的无标注的ratings(例如，missing data)与用户的preference无关&lt;/strong&gt;。这就大大减轻了建模的工作量，大量类似的复杂模型被提出了，例如SVD++、time-SVD等。&lt;/p&gt;

&lt;p&gt;但在实际应用中，用户往往有大量的隐式反馈，例如浏览历史、购买历史等，而负反馈却是稀缺的。如果只对正反馈建模，那得到的是用户profile的一种biased的表示。针对这种负反馈缺失的场景（也叫one-class problem，参考&lt;a href=&quot;http://www.rongpan.net/publications/pan-oneclasscf.pdf&quot;&gt;One-class collaborative filtering&lt;/a&gt;），比较流行的解法是把missing data当做负反馈。但如果观测数据和missing data都要考虑的话，算法的学习效率就会大大降低。而且现实场景中，新的user/item/interaction都是流式地不停地进来的，所以模型的快速更新就更重要了。&lt;/p&gt;

&lt;p&gt;针对隐式反馈和online learning这两大问题，KDD2015的&lt;a href=&quot;https://arxiv.org/abs/1507.06452&quot;&gt;Dynamic matrix factorization with priors on unknown values&lt;/a&gt;已经提出了一种方法&lt;strong&gt;Dynamic MF&lt;/strong&gt;，但本文作者认为，这种方法对missing data的建模是『unrealistic』且『suboptimal』 的。也就是这种方法对missing data给予了uniform的权重，认为所有的missing data有同等概率被视为负反馈。另外这篇文章使用的是梯度下降，需要expensive line search来找到每一步的最优学习率。&lt;/p&gt;

&lt;p&gt;本文的方法比Huyifan和Koren在&lt;a href=&quot;http://yifanhu.net/PUB/cf.pdf&quot;&gt;Collaborative filtering for implicit feedback datasets&lt;/a&gt;中用的ALS要快K倍，K是latent factor的num，这个速度和上面提到的&lt;strong&gt;Dynamic MF&lt;/strong&gt;一样快。本文还提出了针对新数据的增量更新策略，能够快速更新模型参数。另外本文提出的方法不需要学习率，所以不需要像sgd那样去调学习率。&lt;/p&gt;

&lt;h3 id=&quot;related-work&quot;&gt;related work&lt;/h3&gt;

&lt;p&gt;针对负反馈缺失的问题，有以下两种策略：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;sample based learning：从missing data中采样出一部分，当成负反馈。性能好，但风险是效果不一定好。&lt;/li&gt;
  &lt;li&gt;whole-data based learning：所有missing data都当做是负反馈。覆盖会更多，但性能是瓶颈。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;只有&lt;a href=&quot;http://agents.sci.brooklyn.cuny.edu/internal/proceedings/kdd/kdd2009/docs/p667.pdf&quot;&gt;Mind the gaps: Weighting the unknown in large-scale one-class collaborative filtering&lt;/a&gt;和&lt;a href=&quot;http://www.rongpan.net/publications/pan-oneclasscf.pdf&quot;&gt;One-class collaborative filtering&lt;/a&gt;考虑了非uniform的weight方法。但时间复杂度较高，没法在大规模的数据集上使用。&lt;/p&gt;

&lt;p&gt;而优化方法方面，Koren的&lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.379.1951&amp;amp;rep=rep1&amp;amp;type=pdf&quot;&gt;Collaborative filtering with temporal dynamics&lt;/a&gt;和&lt;a href=&quot;https://arxiv.org/pdf/1205.2618.pdf&quot;&gt;Bpr: Bayesian personalized ranking from implicit feedback&lt;/a&gt;使用的是SGD，而&lt;a href=&quot;https://arxiv.org/abs/1507.06452&quot;&gt;Dynamic matrix factorization with priors on unknown values&lt;/a&gt;和&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=2911502&quot;&gt;Discrete collaborative filtering&lt;/a&gt;使用的是Coordinate Descent (CD)，而&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=2010002&amp;amp;dl=ACM&amp;amp;coll=DL&quot;&gt;Fast context-aware recommendations with factorization machines&lt;/a&gt;使用的是Markov Chain Monto Carlo(MCMC)。&lt;/p&gt;

&lt;p&gt;SGD是最流行的，但对于&lt;a href=&quot;http://yifanhu.net/PUB/cf.pdf&quot;&gt;Collaborative filtering for implicit feedback datasets&lt;/a&gt;提到的whole-data based MF来说，是不适合的，因为需要考虑整个user-item的交互矩阵，训练样本特别多。所以对于这种问题，ALS是CD的一个instantiation(实例化)，在&lt;a href=&quot;http://yifanhu.net/PUB/cf.pdf&quot;&gt;Collaborative filtering for implicit feedback datasets&lt;/a&gt;、&lt;a href=&quot;http://agents.sci.brooklyn.cuny.edu/internal/proceedings/kdd/kdd2009/docs/p667.pdf&quot;&gt;Mind the gaps: Weighting the unknown in large-scale one-class collaborative filtering&lt;/a&gt;、&lt;a href=&quot;http://www.rongpan.net/publications/pan-oneclasscf.pdf&quot;&gt;One-class collaborative filtering&lt;/a&gt;、&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=1835895&quot;&gt;Training and testing of recommender systems on data missing not at random&lt;/a&gt;中都提到了。而这种方法时间复杂度较高，在&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=1864726&quot;&gt;Fast als-based matrix factorization for explicit and implicit feedback datasets&lt;/a&gt;和&lt;a href=&quot;http://www.cs.toronto.edu/~mvolkovs/sigir2015_svd.pdf&quot;&gt;Effective latent models for binary feedback in recommender systems&lt;/a&gt;中都提到了。而&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=1864726&quot;&gt;Fast als-based matrix factorization for explicit and implicit feedback datasets&lt;/a&gt;提到了als的一种近似解法；而&lt;a href=&quot;https://arxiv.org/abs/1507.06452&quot;&gt;Dynamic matrix factorization with priors on unknown values&lt;/a&gt;使用的是Randomized block Coordinate Descent (RCD)；而&lt;a href=&quot;http://www.cs.toronto.edu/~mvolkovs/sigir2015_svd.pdf&quot;&gt;Effective latent models for binary feedback in recommender systems&lt;/a&gt;则通过neighbor-based similarly来丰富隐式反馈矩阵，然后使用unweighted SVD。&lt;/p&gt;

&lt;p&gt;而推荐系统的增量更新这块，有这么些研究：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;neighbor-based：&lt;a href=&quot;http://net.pku.edu.cn/~cuibin/Papers/2015SIGMOD-tencentRec.pdf&quot;&gt;TencentRec: Real-time stream recommendation in practice&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;graph-based：&lt;a href=&quot;https://www.comp.nus.edu.sg/~kanmy/papers/cikm15-trirank-cr.pdf&quot;&gt;Trirank: Review-aware explainable recommendation by modeling aspects&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;probabilistic：&lt;a href=&quot;https://www2007.org/papers/paper570.pdf&quot;&gt;Google News Personalization: Scalable online collaborative filtering&lt;/a&gt;。用的plsi&lt;/li&gt;
  &lt;li&gt;MF：
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1507.06452&quot;&gt;Dynamic matrix factorization with priors on unknown values&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=2365968&quot;&gt;Real-time top-n recommendation in social streams&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://ieeexplore.ieee.org/document/6252670&quot;&gt;Online learning for collaborative filtering&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.165.8010&amp;amp;rep=rep1&amp;amp;type=pdf&quot;&gt;Online-updating regularized kernel matrix factorization models for large scale recommender systems&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;对MF来讲，在线更新有如下研究：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;SGD：
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=2365968&quot;&gt;Real-time top-n recommendation in social streams&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.165.8010&amp;amp;rep=rep1&amp;amp;type=pdf&quot;&gt;Online-updating regularized kernel matrix factorization models for large scale recommender systems&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;RCD：
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1507.06452&quot;&gt;Dynamic matrix factorization with priors on unknown values&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;dual-averaging：
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://ieeexplore.ieee.org/document/6252670&quot;&gt;Online learning for collaborative filtering&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;本文是第一个用ALS的。&lt;/p&gt;

&lt;h3 id=&quot;预备知识&quot;&gt;预备知识&lt;/h3&gt;

&lt;h4 id=&quot;als&quot;&gt;ALS&lt;/h4&gt;

&lt;p&gt;假设&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{r}_{u i}=&amp;lt;\mathbf{p}_{u}, \mathbf{q}_{i}&amp;gt;=\mathbf{p}_{u}^{T} \mathbf{q}_{i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;考虑bias的话，就是&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{r}_{u i}=b_{u}+b_{i}+&amp;lt;\mathbf{p}_{u}^{B}, \mathbf{q}_{i}^{B}&amp;gt;
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可以转化为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{p}_{u} \leftarrow\left[\mathbf{p}_{u}^{B}, b_{u}, 1\right]\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{q}_{i} \leftarrow\left[\mathbf{q}_{i}^{B}, 1, b_{i}\right]\)&lt;/code&gt;，这样就又是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{r}_{u i}=&amp;lt;\mathbf{p}_{u}, \mathbf{q}_{i}&amp;gt;=\mathbf{p}_{u}^{T} \mathbf{q}_{i}\)&lt;/code&gt;啦~~&lt;/p&gt;

&lt;p&gt;目标函数是&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
J=\sum_{u=1}^{M} \sum_{i=1}^{N} w_{u i}\left(r_{u i}-\hat{r}_{u i}\right)^{2}+\lambda\left(\sum_{u=1}^{M}\left\|\mathbf{p}_{u}\right\|^{2}+\sum_{i=1}^{N}\left\|\mathbf{q}_{i}\right\|^{2}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;也就是要最小化&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
J_{u}=\left\|\mathbf{W}^{u}\left(\mathbf{r}_{u}-\mathbf{Q} \mathbf{p}_{u}\right)\right\|^{2}+\lambda\left\|\mathbf{p}_{u}\right\|^{2}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;用ALS的话，得到的就是&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} \frac{\partial J_{u}}{\partial \mathbf{p}_{u}} &amp;amp;=2 \mathbf{Q}^{T} \mathbf{W}^{u} \mathbf{Q} \mathbf{p}_{u}-2 \mathbf{Q}^{T} \mathbf{W}^{u} \mathbf{r}_{u}+2 \lambda \mathbf{p}_{u}=0 \\ \Rightarrow \mathbf{p}_{u} &amp;amp;=\left(\mathbf{Q}^{T} \mathbf{W}^{u} \mathbf{Q}+\lambda \mathbf{I}\right)^{-1} \mathbf{Q}^{T} \mathbf{W}^{u} \mathbf{r}_{u} \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;同样地，可以求出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{q}_{i}\)&lt;/code&gt;的解&lt;/p&gt;

&lt;p&gt;复杂度的话，对一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K \times K\)&lt;/code&gt;的矩阵求逆是一个比较耗时的操作，复杂度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O\left(K^{3}\right)\)&lt;/code&gt;。所以更新一次user向量的复杂度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O\left(K^{3}+N K^{2}\right)\)&lt;/code&gt;。然后整个ALS的过程的复杂度就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O\left((M+N) K^{3}+M N K^{2}\right)\)&lt;/code&gt;。&lt;/p&gt;

&lt;h4 id=&quot;使用uniform-weighting来加速&quot;&gt;使用Uniform Weighting来加速&lt;/h4&gt;

&lt;p&gt;对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{R}\)&lt;/code&gt;中的所有0元素，给同样的权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{0}\)&lt;/code&gt;。就有：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\mathbf{Q}^{T} \mathbf{W}^{u} \mathbf{Q}=w_{0} \mathbf{Q}^{T} \mathbf{Q}+\mathbf{Q}^{T}\left(\mathbf{W}^{u}-\mathbf{W}^{0}\right) \mathbf{Q}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{W}^{0}\)&lt;/code&gt;是一个对角矩阵，对角线元素都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{0}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{Q}^{T} \mathbf{Q}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u\)&lt;/code&gt;无关，所以可以预先算好。&lt;/p&gt;

&lt;p&gt;而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{W}^{u}-\mathbf{W}^{0}\)&lt;/code&gt;只有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left|\mathcal{R}_{u}\right|\)&lt;/code&gt;个非0值，所以上面这个式子的计算复杂度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O\left(\left|\mathcal{R}_{u}\right| K^{2}\right)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以总的时间复杂度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O\left((M+N) K^{3}+|\mathcal{R}| K^{2}\right)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而SGD的时间复杂度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(|\mathcal{R}| K)\)&lt;/code&gt;，远比ALS要小！！&lt;/p&gt;

&lt;h4 id=&quot;generic-element-wise-als-learner&quot;&gt;generic element-wise ALS learner&lt;/h4&gt;

&lt;p&gt;在&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=2010002&amp;amp;dl=ACM&amp;amp;coll=DL&quot;&gt;Fast context-aware recommendations with factorization machines&lt;/a&gt;里提到了&lt;/p&gt;

&lt;p&gt;对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{u f}\)&lt;/code&gt;求导&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial J}{\partial p_{u f}}=-2 \sum_{i=1}^{N}\left(r_{u i}-\hat{r}_{u i}^{f}\right) w_{u i} q_{i f}+2 p_{u f} \sum_{i=1}^{N} w_{u i} q_{i f}^{2}+2 \lambda p_{u f}
\]&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>python generator</title>
   <link href="http://daiwk.github.io/posts/knowledge-python-yield-generator.html"/>
   <updated>2017-08-24T00:00:00+00:00</updated>
   <id>/posts/knowledge-python-yield-generator</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;
&lt;p&gt;&lt;a href=&quot;http://www.cnblogs.com/cotyb/p/5260032.html&quot;&gt;http://www.cnblogs.com/cotyb/p/5260032.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;groupby经典用法(前提是输入的数据相同key的结果已经相邻)：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;itertools&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_group_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;segs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;segs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; 

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;iter&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;itertools&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;groupby&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stdin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_group_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>mr shuffle</title>
   <link href="http://daiwk.github.io/posts/knowledge-mr-shuffle.html"/>
   <updated>2017-08-24T00:00:00+00:00</updated>
   <id>/posts/knowledge-mr-shuffle</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;http://langyu.iteye.com/blog/992916&quot;&gt;http://langyu.iteye.com/blog/992916&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.csdn.net/gyflyx/article/details/16831015&quot;&gt;http://blog.csdn.net/gyflyx/article/details/16831015&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>hekaiming tutorials</title>
   <link href="http://daiwk.github.io/posts/cv-cvpr2017-hekaiming.html"/>
   <updated>2017-08-23T00:00:00+00:00</updated>
   <id>/posts/cv-cvpr2017-hekaiming</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#cvpr2017&quot;&gt;cvpr2017&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#Convolutional-Neural-Networks-Recap&quot;&gt;Convolutional Neural Networks: Recap&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ResNet&quot;&gt;ResNet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ResNeXt&quot;&gt;ResNeXt&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#eccv2018&quot;&gt;eccv2018&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;cvpr2017&quot;&gt;cvpr2017&lt;/h2&gt;

&lt;p&gt;cvpr2017 hekaiming的Learning Deep Features for Visual Recognition&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://deeplearning.csail.mit.edu/cvpr2017_tutorial_kaiminghe.pdf&quot;&gt;Learning Deep Features for Visual Recognition&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;convolutional-neural-networks-recap&quot;&gt;Convolutional Neural Networks: Recap&lt;/h3&gt;

&lt;h3 id=&quot;resnet&quot;&gt;ResNet&lt;/h3&gt;

&lt;h3 id=&quot;resnext&quot;&gt;ResNeXt&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2652047873&amp;amp;idx=3&amp;amp;sn=4dc44a7b4f050a6e55734faeb4297929&amp;amp;chksm=f1207ef0c657f7e61b4b14bfd2d3f915bf5fe79ffbd3dfd61a6fa23f18bd2179025e8f635fb4&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0625VZIm3H5Kd9PJYDeAExh0&amp;amp;pass_ticket=b29oFqpJS3l2z9rkgAH2HsO1MhYXnw6w%2FmAI30o3T46KiQgaX30qaNd4uUXfW4zq#rd&quot;&gt;何恺明团队新作ResNext：Instagram图片预训练，挑战ImageNet新精度&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ResNeXt-101：&lt;a href=&quot;https://arxiv.org/pdf/1805.00932.pdf&quot;&gt;Exploring the Limits of Weakly Supervised Pretraining&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如果没有有监督式预训练，很多方法现在还被认为是一种蛮干 ImageNet数据集实际上是预训练数据集。我们现在实际上对数据集的预训练了解相对较少。其原因很多：比如现存的预训练数据集数量很少，构建新数据集是劳动密集型的工作，需要大量的计算资源来进行实验。然而，鉴于预训练过程在机器学习相关领域的核心作用，扩大我们在这一领域的科学知识是非常重要的。&lt;/p&gt;

&lt;p&gt;本文试图通过研究一个未开发的数据体系来解决这个复杂的问题：使用外部社交媒体上数十亿的带有标签的图像作为数据源。该数据源具有大而且不断增长的优点，而且是“免费”注释的，因为数据不需要手动标记。显而易见，对这些数据的训练将产生良好的迁移学习结果。&lt;/p&gt;

&lt;p&gt;本文的主要成果是，在不使用手动数据集管理或复杂的数据清理的情况下，利用数千个不同主题标签作为标记的数十亿幅Instagram图像进行训练的模型，表现出了优异的传输学习性能。在目标检测和图像分类任务上实现了对当前SOTA性能的提升。在ImageNet-1k图像分类数据集上获得single-crop 最高准确率达到了85.4%，AP达到了45.2%。当在ImageNet-1k上训练（或预训练）相同模型时，分数分别为79.8％和43.7％。然而，我们的主要目标是提供关于此前未开发的制度的新实验数据。为此，我们进行了大量实验，揭示了一些有趣的趋势。&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://pytorch.org/hub/facebookresearch_WSL-Images_resnext/&quot;&gt;https://pytorch.org/hub/facebookresearch_WSL-Images_resnext/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;eccv2018&quot;&gt;eccv2018&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://kaiminghe.com/eccv18tutorial/eccv2018_tutorial_kaiminghe.pdf&quot;&gt;http://kaiminghe.com/eccv18tutorial/eccv2018_tutorial_kaiminghe.pdf&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>梯度下降优化算法</title>
   <link href="http://daiwk.github.io/posts/dl-optimization-methods.html"/>
   <updated>2017-08-15T00:00:00+00:00</updated>
   <id>/posts/dl-optimization-methods</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%a2%af%e5%ba%a6%e4%b8%8b%e9%99%8d&quot;&gt;梯度下降&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#momentum&quot;&gt;momentum&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#nag&quot;&gt;NAG&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#adagrad&quot;&gt;Adagrad&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#adadelta&quot;&gt;Adadelta&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rmsprop&quot;&gt;RMSprop&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#adam&quot;&gt;Adam&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#adam%e7%b1%bb%e6%96%b9%e6%b3%95%e5%b0%8f%e7%bb%93&quot;&gt;Adam类方法小结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#amsgrad&quot;&gt;AMSGrad&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#lookahead&quot;&gt;Lookahead&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#dladmm&quot;&gt;dlADMM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#radam&quot;&gt;RAdam&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#warm-up&quot;&gt;warm-up&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;梯度下降&quot;&gt;梯度下降&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;http://ruder.io/optimizing-gradient-descent/&quot;&gt;http://ruder.io/optimizing-gradient-descent/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;部分算法有对应的中文版：&lt;a href=&quot;https://blog.csdn.net/u010089444/article/details/76725843&quot;&gt;https://blog.csdn.net/u010089444/article/details/76725843&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;优缺点参考：&lt;a href=&quot;https://blog.csdn.net/u012759136/article/details/52302426&quot;&gt;https://blog.csdn.net/u012759136/article/details/52302426&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;！！！假设模型的参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta \in \mathbb{R}^{d}\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;普通sgd有如下缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;选择合适的learning rate比较困难&lt;/li&gt;
  &lt;li&gt;对所有的参数更新使用同样的learning rate。对于稀疏数据或者特征，有时我们可能想&lt;strong&gt;更新快&lt;/strong&gt;一些对于&lt;strong&gt;不经常出现的特征&lt;/strong&gt;，对于&lt;strong&gt;常出现&lt;/strong&gt;的特征&lt;strong&gt;更新慢&lt;/strong&gt;一些，这时候SGD就不太能满足要求了&lt;/li&gt;
  &lt;li&gt;SGD容易收敛到&lt;strong&gt;局部最优&lt;/strong&gt;，在某些情况下可能被困在&lt;strong&gt;鞍点&lt;/strong&gt;【但是在合适的初始化和学习率设置下，鞍点的影响其实没这么大】&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;momentum&quot;&gt;momentum&lt;/h2&gt;

&lt;p&gt;如果在峡谷地区(某些方向较另一些方向上陡峭得多，常见于局部极值点)，SGD会在这些地方附近振荡，从而导致收敛速度慢。这种情况下，动量(Momentum)便可以解决。动量在参数更新项中加上一次更新量(即动量项)。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} v_{t} &amp;amp;=\gamma v_{t-1}+\eta \nabla_{\theta} J(\theta) \\ \theta &amp;amp;=\theta-v_{t} \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;一开始初始化为0.5，后面变成0.9。一般直接设成0.9好了。。&lt;a href=&quot;http://ufldl.stanford.edu/tutorial/supervised/OptimizationStochasticGradientDescent/&quot;&gt;http://ufldl.stanford.edu/tutorial/supervised/OptimizationStochasticGradientDescent/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;简单地说，就是记录每次的权重更新量，这次的更新量就是&lt;strong&gt;上次更新量乘上一个系数&lt;/strong&gt;，再加上学习率乘以梯度。&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;下降&lt;strong&gt;初期&lt;/strong&gt;，使用上一次参数更新，下降方向一致，乘上较大的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;能够进行很好的&lt;strong&gt;加速&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;下降&lt;strong&gt;中后期&lt;/strong&gt;时，在局部最小值来回震荡的时候，梯度接近0， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;使得&lt;strong&gt;更新幅度变大&lt;/strong&gt;，可能可以跳出陷阱&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;梯度改变方向&lt;/strong&gt;的时候，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;可以&lt;strong&gt;减少更新&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;nag&quot;&gt;NAG&lt;/h2&gt;

&lt;p&gt;Nesterov accelerated gradient (NAG)&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} v_{t} &amp;amp;=\gamma v_{t-1}+\eta \nabla_{\theta} J\left(\theta-\gamma v_{t-1}\right) \\ \theta &amp;amp;=\theta-v_{t} \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;momentum首先计算一个梯度(短的蓝色向量)，然后在加速更新梯度的方向进行一个大的跳跃(长的蓝色向量)，nesterov项首先在之前加速的梯度方向进行一个大的跳跃(棕色向量)，计算梯度然后进行校正(绿色梯向量)&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/nag.png&quot; style=&quot;max-height: 150px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;adagrad&quot;&gt;Adagrad&lt;/h2&gt;

&lt;p&gt;首先&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
g_{t, i}=\nabla_{\theta} J\left(\theta_{t, i}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\theta_{t+1, i}=\theta_{t, i}-\eta \cdot g_{t, i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;再然后&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\theta_{t+1, i}=\theta_{t, i}-\frac{\eta}{\sqrt{G_{t, i i}+\epsilon}} \cdot g_{t, i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{t} \in \mathbb{R}^{d \times d}\)&lt;/code&gt;是一个对角矩阵，每个对角线位置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i,i\)&lt;/code&gt;为对应参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_i\)&lt;/code&gt;从第1轮到第t轮梯度的平方和。&lt;/p&gt;

&lt;p&gt;综合起来看就是&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\theta_{t+1}=\theta_{t}-\frac{\eta}{\sqrt{G_{t}+\epsilon}} \odot g_{t}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;也就是学习率除以前t轮的梯度的平方和加上一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon\)&lt;/code&gt;再开根号。这个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon\)&lt;/code&gt;一般设置成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1 e-8\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;前期梯度小时，分母大，能放大梯度&lt;/li&gt;
  &lt;li&gt;后期梯度大时，分母小，能约束梯度&lt;/li&gt;
  &lt;li&gt;适合处理&lt;strong&gt;稀疏梯度&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;仍然依赖&lt;strong&gt;人为设置的全局学习率&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;学习率设置得太大的话，会使得对梯度的调节太大&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;中后期&lt;/strong&gt;，分母会越来越大，&lt;strong&gt;梯度趋近0&lt;/strong&gt;，训练会&lt;strong&gt;提前结束&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;adadelta&quot;&gt;Adadelta&lt;/h2&gt;

&lt;p&gt;有点长。。&lt;/p&gt;

&lt;p&gt;首先&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
E\left[g^{2}\right]_{t}=\gamma E\left[g^{2}\right]_{t-1}+(1-\gamma) g_{t}^{2}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{array}{l}{\Delta \theta_{t}=-\eta \cdot g_{t, i}} \\ {\theta_{t+1}=\theta_{t}+\Delta \theta_{t}}\end{array}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;再然后，adagrad是这样的：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\Delta \theta_{t}=-\frac{\eta}{\sqrt{G_{t}+\epsilon}} \odot g_{t}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而我们把那个平方和替换成平均：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\Delta \theta_{t}=-\frac{\eta}{\sqrt{E\left[g^{2}\right]_{t}+\epsilon}} g_{t}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其实就是RMS(root mean square)，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R M S[g]_{t}=\sqrt{E\left[g^{2}\right]_{t}+\epsilon}\)&lt;/code&gt;，代入上式可以得到：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\Delta \theta_{t}=-\frac{\eta}{R M S[g]_{t}} g_{t}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后呢，定义一个参数平方的更新量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Delta \theta _t^2\)&lt;/code&gt;的指数平滑：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
E\left[\Delta \theta^{2}\right]_{t}=\gamma E\left[\Delta \theta^{2}\right]_{t-1}+(1-\gamma) \Delta \theta_{t}^{2}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后参数更新量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Delta \theta _t\)&lt;/code&gt;的RMSE就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
R M S[\Delta \theta]_{t}=\sqrt{E\left[\Delta \theta^{2}\right]_{t}+\epsilon}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R M S[\Delta \theta]_{t}\)&lt;/code&gt;是未知的，可以用直到上一个时间步的参数更新的RMS来近似。所以可以拿&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R M S[\Delta \theta]_{t-1}\)&lt;/code&gt;来替换学习率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;，从而得到：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} \Delta \theta_{t} &amp;amp;=-\frac{R M S[\Delta \theta]_{t-1}}{R M S[g]_{t}} g_{t} \\ \theta_{t+1} &amp;amp;=\theta_{t}+\Delta \theta_{t} \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;已经&lt;strong&gt;不用依赖&lt;/strong&gt;于&lt;strong&gt;全局学习率&lt;/strong&gt;了&lt;/li&gt;
  &lt;li&gt;训练&lt;strong&gt;初中期&lt;/strong&gt;，&lt;strong&gt;加速&lt;/strong&gt;效果不错，很快&lt;/li&gt;
  &lt;li&gt;训练&lt;strong&gt;后期&lt;/strong&gt;，反复在&lt;strong&gt;局部最小值附近抖动&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;rmsprop&quot;&gt;RMSprop&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} E\left[g^{2}\right]_{t} &amp;amp;=0.9 E\left[g^{2}\right]_{t-1}+0.1 g_{t}^{2} \\ \theta_{t+1} &amp;amp;=\theta_{t}-\frac{\eta}{\sqrt{E\left[g^{2}\right]_{t}+\epsilon}} g_{t} \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;就是在adagrad的基础上，分母那个平方和改成指数平滑，前一时间步0.9，当前时间步0.1，而且前一时间步依赖的是均值而不是平方和，与adadelta类似。hinton认为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;设成0.9，默认学习率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;设0.01比较好。&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;依赖于&lt;strong&gt;全局学习率&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;RMSprop算是Adagrad的一种发展，和Adadelta的变体，效果趋于二者之间&lt;/li&gt;
  &lt;li&gt;适合处理&lt;strong&gt;非平稳目标&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;对于&lt;strong&gt;RNN效果很好&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;adam&quot;&gt;Adam&lt;/h2&gt;

&lt;p&gt;rmsprop可以看成是对二阶矩做了指数平滑，adam就是对一阶矩也做一个指数平滑。然后二阶和一阶平滑完后要scale一下。最后二阶的搞完做分母，一阶的搞完做分子。&lt;/p&gt;

&lt;p&gt;两个的指数平滑如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} m_{t} &amp;amp;=\beta_{1} m_{t-1}+\left(1-\beta_{1}\right) g_{t} \\ v_{t} &amp;amp;=\beta_{2} v_{t-1}+\left(1-\beta_{2}\right) g_{t}^{2} \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} \hat{m}_{t} &amp;amp;=\frac{m_{t}}{1-\beta_{1}^{t}} \\ \hat{v}_{t} &amp;amp;=\frac{v_{t}}{1-\beta_{2}^{t}} \end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;再然后：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\theta_{t+1}=\theta_{t}-\frac{\eta}{\sqrt{\hat{v}_{t}}+\epsilon} \hat{m}_{t}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;默认值的话，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta_1=0.9\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta_2=0.999\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\epsilon=10^{-8}\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;特点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;结合了&lt;strong&gt;Adagrad&lt;/strong&gt;善于处理&lt;strong&gt;稀疏梯度&lt;/strong&gt;和&lt;strong&gt;RMSprop&lt;/strong&gt;善于处理&lt;strong&gt;非平稳目标&lt;/strong&gt;的优点&lt;/li&gt;
  &lt;li&gt;对&lt;strong&gt;内存&lt;/strong&gt;需求较&lt;strong&gt;小&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;为不同的参数计算不同的自适应学习率&lt;/li&gt;
  &lt;li&gt;也适用于大多&lt;strong&gt;非凸优化&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;适用于&lt;strong&gt;大数据集&lt;/strong&gt;和&lt;strong&gt;高维空间&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;adam类方法小结&quot;&gt;Adam类方法小结&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/wishchin/article/details/80567558&quot;&gt;https://blog.csdn.net/wishchin/article/details/80567558&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;结论：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Adam算法可以看做是修正后的Momentum+RMSProp算法&lt;/li&gt;
  &lt;li&gt;动量直接并入梯度一阶矩估计中（指数加权）&lt;/li&gt;
  &lt;li&gt;Adam通常被认为对超参数的选择相当鲁棒&lt;/li&gt;
  &lt;li&gt;学习率建议为0.001&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其实就是Momentum+RMSProp的结合，然后再修正其偏差。&lt;/p&gt;

&lt;h2 id=&quot;amsgrad&quot;&gt;AMSGrad&lt;/h2&gt;

&lt;p&gt;Adams其实有不少问题，参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/32262540&quot;&gt;https://zhuanlan.zhihu.com/p/32262540&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/abs/1904.09237&quot;&gt;On the Convergence of Adam and Beyond&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/wishchin/article/details/80567558&quot;&gt;https://blog.csdn.net/wishchin/article/details/80567558&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\Gamma_{t+1}=\left(\frac{\sqrt{V_{t+1}}}{\alpha_{t+1}}-\frac{\sqrt{V_{t}}}{\alpha_{t}}\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;RMSProp和Adam算法下的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Gamma_{t}\)&lt;/code&gt;可能是负的，所以文章探讨了一种替代方法，通过把超参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta_1\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\beta_2\)&lt;/code&gt;设置为随着&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;变化而变化，从而保证&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Gamma_{t}\)&lt;/code&gt;始终是个非负数。&lt;/p&gt;

&lt;h2 id=&quot;lookahead&quot;&gt;Lookahead&lt;/h2&gt;

&lt;p&gt;论文：&lt;a href=&quot;https://arxiv.org/abs/1907.08610v1&quot;&gt;Lookahead Optimizer: k steps forward, 1 step back&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766617&amp;amp;idx=2&amp;amp;sn=bc57e82d6236a31b6da4863e9de3283f&amp;amp;chksm=871ab967b06d30719277f8fd675ae55a71064e61816a38ee469446153f133b9487bc3788e947&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;Adam作者大革新， 联合Hinton等人推出全新优化方法Lookahead&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;dladmm&quot;&gt;dlADMM&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650768966&amp;amp;idx=5&amp;amp;sn=1a9dc281cbb62d3121e8601bbf5ef350&amp;amp;chksm=871a4238b06dcb2e6ee78374dd1c4c1a50c7340d4c955d1a4b8ba9ba3d87055f81e9ef762db6&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=mmBhl6hER5JU9q0KMKTTFnbwPDksdn18kk%2FlW9Ih3p2TCzi4%2BlfisKHhCysHq%2Bou#rd&quot;&gt;KDD 2019 | 不用反向传播就能训练DL模型，ADMM效果可超梯度下降&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1905.13611.pdf&quot;&gt;ADMM for Efficient Deep Learning with Global Convergence&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/xianggebenben/dlADMM&quot;&gt;https://github.com/xianggebenben/dlADMM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文提出了一种基于交替方向乘子法的深度学习优化算法 dlADMM。该方法可以避免随机梯度下降算法的梯度消失和病态条件等问题，弥补了此前工作的不足。此外，该研究提出了先后向再前向的迭代次序加快了算法的收敛速度，并且对于大部分子问题采用二次近似的方式进行求解，避免了矩阵求逆的耗时操作。在基准数据集的实验结果表明，dlADMM 击败了大部分现有的优化算法，进一步证明了它的有效性和高效。&lt;/p&gt;

&lt;h2 id=&quot;radam&quot;&gt;RAdam&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/LgxIQb1RelqKHqMtmGu7-Q&quot;&gt;Adam可以换了？UIUC中国博士生提出RAdam，收敛快精度高，大小模型通吃&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github: &lt;a href=&quot;https://github.com/LiyuanLucasLiu/RAdam&quot;&gt;https://github.com/LiyuanLucasLiu/RAdam&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1908.03265v1.pdf&quot;&gt;On the variance of the adaptive learning rate and beyond&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;它既能实现Adam快速收敛的优点，又具备SGD方法的优势，令模型收敛至质量更高的结果。&lt;/p&gt;

&lt;p&gt;包括Adam，RMSProp等在内的自适应学习率优化器都存在收敛到质量较差的局部最优解的可能。因此，几乎每个人都使用某种形式的“预热”方式来避免这种风险。&lt;/p&gt;

&lt;p&gt;根本问题是自适应学习率优化器具有太大的变化，特别是在训练的早期阶段，并且可能由于训练数据量有限出现过度跳跃，因此可能收敛至局部最优解。&lt;/p&gt;

&lt;p&gt;当优化器仅使用有限的训练数据时，采用“预热”（这一阶段的学习率要慢得多）是自适应优化器要求抵消过度方差的要求。&lt;/p&gt;

&lt;p&gt;vanilla Adam和其他自适应学习速率优化器可能会基于训练早期数据太少而做出错误决策。因此，如果没有某种形式的预热，很可能在训练一开始便会收敛局部最优解，这使得训练曲线由于糟糕的开局而变得更长、更难。&lt;/p&gt;

&lt;p&gt;作者在不用预热的情况下运行了Adam，但是在前2000次迭代（adam-2k）中避免使用动量，结果实现了与“Adam+预热”差不多的结果，从而验证了“预热”在训练的初始阶段中起到“降低方差”的作用，并可以避免Adam在没有足够数据的情况下在开始训练时即陷入局部最优解。&lt;/p&gt;

&lt;p&gt;可以将“预热”作为降低方差的方法，但所需的预热程度未知，而且具体情况会根据数据集不同而变化，本文确定了一个数学算法，作为“动态方差减少器”。作者建立了一个“整流项”，可以缓慢而稳定地允许将自适应动量作为基础方差的函数进行充分表达。&lt;/p&gt;

&lt;p&gt;作者指出，在某些情况下，由于衰减率和基本方差的存在，RAdam可以在动量等效的情况下退化为SGD。&lt;/p&gt;

&lt;p&gt;实验表明，RAdam优于传统的手动预热调整，其中需要预热或猜测需要预热的步骤数。RAdam自动提供方差缩减，在各种预热长度和各种学习率下都优于手动预热。&lt;/p&gt;

&lt;h2 id=&quot;warm-up&quot;&gt;warm-up&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/u011995719/article/details/77884728&quot;&gt;Resnet-18-训练实验-warm up操作&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1512.03385.pdf&quot;&gt;Deep Residual Learning for Image Recognition&lt;/a&gt;中对cifar10的实验用了一个trick是 warm up（热身），就是&lt;strong&gt;先&lt;/strong&gt;采用&lt;strong&gt;小的学习率&lt;/strong&gt;（0.01）进行训练，训练了&lt;strong&gt;400iterations之后&lt;/strong&gt;将&lt;strong&gt;学习率调整至0.1&lt;/strong&gt;开始正式训练。&lt;/p&gt;

&lt;p&gt;参考知乎的这个问题：&lt;a href=&quot;https://www.zhihu.com/question/338066667&quot;&gt;https://www.zhihu.com/question/338066667&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;有助于减缓模型在初始阶段对mini-batch的提前过拟合现象，保持分布的平稳&lt;/li&gt;
  &lt;li&gt;有助于保持模型深层的稳定性&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;可以参考这几篇：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1706.02677.pdf&quot;&gt;Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1708.03888.pdf&quot;&gt;Large batch training of convolutional networks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1810.13243.pdf&quot;&gt;A closer look at deep learning heuristics: learning rate restarts, warmup and distillation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>TSN</title>
   <link href="http://daiwk.github.io/posts/video-tsn.html"/>
   <updated>2017-08-14T00:00:00+00:00</updated>
   <id>/posts/video-tsn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1608.00859&quot;&gt;Temporal Segment Networks: Towards Good Practices for Deep Action Recognition&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/yjxiong/temporal-segment-networks&quot;&gt;https://github.com/yjxiong/temporal-segment-networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pytorch代码：&lt;a href=&quot;https://github.com/yjxiong/tsn-pytorch&quot;&gt;https://github.com/yjxiong/tsn-pytorch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u014380165/article/details/79029309&quot;&gt;https://blog.csdn.net/u014380165/article/details/79029309&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pytorch实现讲解：&lt;a href=&quot;https://blog.csdn.net/u014380165/article/details/79058147&quot;&gt;https://blog.csdn.net/u014380165/article/details/79058147&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>海量数据相似数据查找方法</title>
   <link href="http://daiwk.github.io/posts/ml-topknn.html"/>
   <updated>2017-08-04T00:00:00+00:00</updated>
   <id>/posts/ml-topknn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e9%ab%98%e7%bb%b4%e7%a8%80%e7%96%8f%e5%90%91%e9%87%8f%e7%9a%84%e7%9b%b8%e4%bc%bc%e6%9f%a5%e6%89%beminhash-lsh-simhash&quot;&gt;高维稀疏向量的相似查找——minhash, lsh, simhash&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#minhash&quot;&gt;minhash&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lshlocality-sensitive-hashing&quot;&gt;LSH（Locality-Sensitive Hashing）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#simhash&quot;&gt;simhash&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#simhash%e7%ae%97%e6%b3%95&quot;&gt;simhash算法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%87%8f%e5%b0%91%e9%81%8d%e5%8e%86%e6%ac%a1%e6%95%b0&quot;&gt;减少遍历次数&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#%e4%b8%80%e6%ac%a1%e6%8b%86%e5%88%86%e5%a4%9a%e4%bb%bdtable%e5%b9%b6exact-match&quot;&gt;一次拆分多份table并exact match&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#k3%e6%97%b6%e7%9a%84%e4%b8%80%e8%88%ac%e5%8c%96%e6%96%b9%e6%b3%95&quot;&gt;k!=3时的一般化方法&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%a8%a0%e5%af%86%e5%90%91%e9%87%8f%e7%9a%84%e7%9b%b8%e4%bc%bc%e6%9f%a5%e6%89%beannoy&quot;&gt;稠密向量的相似查找——Annoy&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%bb%ba%e6%a0%91%e8%bf%87%e7%a8%8b&quot;&gt;建树过程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%9f%a5%e8%af%a2%e8%bf%87%e7%a8%8b&quot;&gt;查询过程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%9f%a5%e8%af%a2%e4%b8%ad%e7%9a%84%e9%97%ae%e9%a2%98%e4%b8%8e%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95&quot;&gt;查询中的问题与解决方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%bf%94%e5%9b%9e%e6%9c%80%e7%bb%88%e8%bf%91%e9%82%bb%e8%8a%82%e7%82%b9&quot;&gt;返回最终近邻节点&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94&quot;&gt;性能对比&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#hnsw&quot;&gt;HNSW&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#faiss&quot;&gt;faiss&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#pq%e4%b9%98%e7%a7%af%e9%87%8f%e5%8c%96&quot;&gt;PQ乘积量化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ivfpq%e5%80%92%e6%8e%92%e4%b9%98%e7%a7%af%e9%87%8f%e5%8c%96&quot;&gt;IVFPQ(倒排乘积量化)&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#lopq&quot;&gt;lopq&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.csdn.net/hero_fantao/article/details/70245284&quot;&gt;海量数据相似查找系列1 – Minhashing &amp;amp; LSH &amp;amp; Simhash 技术汇总&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.csdn.net/hero_fantao/article/details/70245387&quot;&gt;海量数据相似查找系列2 – Annoy算法&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://yongyuan.name/blog/ann-search.html&quot;&gt;https://yongyuan.name/blog/ann-search.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cs.ubc.ca/research/flann/uploads/FLANN/flann_pami2014.pdf&quot;&gt;Scalable Nearest Neighbor Algorithms for High Dimensional Data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要分为&lt;strong&gt;高维稀疏向量&lt;/strong&gt;和&lt;strong&gt;稠密向量&lt;/strong&gt;两大方向。&lt;/p&gt;

&lt;h2 id=&quot;高维稀疏向量的相似查找minhash-lsh-simhash&quot;&gt;高维稀疏向量的相似查找——minhash, lsh, simhash&lt;/h2&gt;

&lt;p&gt;针对高维稀疏数据情况，如何通过哈希技术进行快速进行相似查找。&lt;/p&gt;

&lt;p&gt;例如，推荐系统中item-user矩阵。如果你有item数量是百万级别，user是千万级别，这个矩阵是十分稀疏的。你如何计算每一个item的Top N相似item呢？&lt;/p&gt;

&lt;p&gt;同样海量文本场景，文本集合可以看成doc-word 稀疏矩阵，如何求解每个文档的Top N相似文档？&lt;/p&gt;

&lt;p&gt;如果采用两两比较的话，至少有两个问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;O(n^2) 遍历&lt;/strong&gt;时间复杂度；&lt;/li&gt;
  &lt;li&gt;两个高维向量之间&lt;strong&gt;计算相似度&lt;/strong&gt;，比如jaccard相似度，时间很耗时。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;常见的解决方法如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;正排是doc-words，倒排是word-docs&lt;/li&gt;
  &lt;li&gt;对正排里doc1的所有words,拉出对应的倒排docs，归并&lt;/li&gt;
  &lt;li&gt;对正排里doc2的所有words，拉出对应的倒排docs，归并&lt;/li&gt;
  &lt;li&gt;对上述两个向量求相似度&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;问题：虽然时间复杂度可以减小，但高频词可能导致倒排的拉链长度太长，导致效率下降。&lt;/p&gt;

&lt;h3 id=&quot;minhash&quot;&gt;minhash&lt;/h3&gt;

&lt;p&gt;例如，小写字母代表词，大写字母代表文档：&lt;/p&gt;

&lt;p&gt;S1={a, d}, S2={c}, S3={b, d, e}, S4={a, c, d}&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/minhash_1.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;然后，把原来的词典{a, b, c, d, e} 顺序&lt;strong&gt;随机重排&lt;/strong&gt;，例如得到{b, e, a, d, c}，&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/minhash_2.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;定义一个函数h：计算集合S最小的minhash值，就是在这种顺序下最先出现1的元素。那么，&lt;/p&gt;

&lt;p&gt;h(S1) = a, h(S2)=c, h(S3)=b, h(S4)=a&lt;/p&gt;

&lt;p&gt;类似地，如果进行&lt;strong&gt;n次重排&lt;/strong&gt;的话，就会有&lt;strong&gt;n个minhash函数&lt;/strong&gt;，&lt;strong&gt;{h1(S), h2(S)…, hn(S)}&lt;/strong&gt;,  那原来每个高维集合，就会被降到n维空间，比如&lt;strong&gt;S1-&amp;gt;{h1(S1), h2(S1)…, hn(S1)}&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;但是实际中因为重排比较耗时，会用若干&lt;strong&gt;随机哈希函数&lt;/strong&gt;替代。比如设定一个哈希函数: h(x) = (i+1) % 5.&lt;/p&gt;

&lt;p&gt;以{a, b, c, d, e}顺序，“i”表示各个索引，比如a的“i”值为1， b的“i”值为2等。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对集合S1，计算&lt;strong&gt;各个元素哈希值&lt;/strong&gt;为{2，0}，其中h(a-&amp;gt;i) = 2, h(d-&amp;gt;i) = 0,&lt;/li&gt;
  &lt;li&gt;再计算&lt;strong&gt;最小哈希值对应的元素&lt;/strong&gt; minhash (h(S1))= 0(对应的是d这个元素)。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;同样可以定义&lt;strong&gt;n个哈希函数&lt;/strong&gt;【不需要重排，每个hash计算对应的值就行】，进行上述操作，那每个集合S就被降维到n维空间的签名。&lt;/p&gt;

&lt;p&gt;这样，每个集合就可以用一个n维向量，每维是那个hash下的minhash来表示了&lt;/p&gt;

&lt;p&gt;可以参考google那篇&lt;a href=&quot;https://www2007.org/papers/paper570.pdf&quot;&gt;Google News Personalization: Scalable Online Collaborative Filtering&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;lshlocality-sensitive-hashing&quot;&gt;LSH（Locality-Sensitive Hashing）&lt;/h3&gt;

&lt;p&gt;学习&lt;a href=&quot;https://github.com/FALCONN-LIB/FALCONN/wiki/LSH-Primer&quot;&gt;https://github.com/FALCONN-LIB/FALCONN/wiki/LSH-Primer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;minhash解决了文章一开始提到的第二个问题，&lt;strong&gt;高维向量间计算复杂度问题&lt;/strong&gt;(通过minhash 机制把高维降低到n维低纬空间)&lt;/p&gt;

&lt;p&gt;但是还没解决第一个问题：两两比较，时间复杂度O(n^2)。&lt;/p&gt;

&lt;p&gt;LSH 就是这样的机制，通过哈希机制，让&lt;strong&gt;相似向量尽可能出现一个桶中，而不相似的向量出现在不同的桶中&lt;/strong&gt;。相似度计算只在么个桶中进行，每个桶彼此之间不做相似度计算。&lt;/p&gt;

&lt;p&gt;在minhashing 签名的基础上做LSH。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一个高维向量通过minhashing处理后变成&lt;strong&gt;n维低维向量的签名&lt;/strong&gt;，现在把这n维签名分成b组，每组r个元素。&lt;/li&gt;
  &lt;li&gt;每组通过一个哈希函数，把这组的&lt;strong&gt;r个元素&lt;/strong&gt;组成&lt;strong&gt;r维向量&lt;/strong&gt;哈希到一个桶中。&lt;/li&gt;
  &lt;li&gt;每组可以使用同一个哈希函数，但是&lt;strong&gt;每组桶没交集，即使哈希值一样&lt;/strong&gt;。桶名可以类似：组名+哈希值。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;在一个桶中的向量才进行相似度计算&lt;/strong&gt;，&lt;strong&gt;相似度计算&lt;/strong&gt;的向量是&lt;strong&gt;minhash的n维向量&lt;/strong&gt;（不是r维向量）。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如：
minhash签名维度是12，分成4组，每组3个元素。&lt;/p&gt;

&lt;p&gt;拿band1来说，第二列和第四列向量是一样的(第二列是（0, 2, 1）, 第四列是（0, 2, 1）)，一定会哈希到相同的桶中（band1名下的桶），而第一列和第二列有可能不会在一个桶中（band1名下的桶）。&lt;/p&gt;

&lt;p&gt;这里就是重点设置分多少个组，每组多少个元素问题。这个可以根据实际情况和一些经验case来定。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/lsh_1.jpg&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;这些hash function需要满足以下两个条件：&lt;/p&gt;

&lt;p&gt;1）如果d(x,y) ≤ d1， 则h(x) = h(y)的概率 ≥ p1&lt;strong&gt;【距离近的，在同一个桶概率大】&lt;/strong&gt;；
2）如果d(x,y) ≥ d2， 则h(x) = h(y)的概率 ≤ p2&lt;strong&gt;【距离远的，在同一个桶概率小】&lt;/strong&gt;；&lt;/p&gt;

&lt;p&gt;其中d(x,y)表示x和y之间的距离，d1 &amp;lt; d2， h(x)和h(y)分别表示对x和y进行hash变换。&lt;/p&gt;

&lt;p&gt;满足以上两个条件的hash functions称为(d1,d2,p1,p2)-sensitive。而通过一个或多个(d1,d2,p1,p2)-sensitive的hash function对原始数据集合进行hashing生成一个或多个hash table的过程称为Locality-sensitive Hashing。&lt;/p&gt;

&lt;h3 id=&quot;simhash&quot;&gt;simhash&lt;/h3&gt;

&lt;h4 id=&quot;simhash算法&quot;&gt;simhash算法&lt;/h4&gt;

&lt;p&gt;simhash在工业界引起很大注意力是因为google 07那篇文章，把Simhash技术引入到海量文本去重领域。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.78.7794&amp;amp;rep=rep1&amp;amp;type=pdf&quot;&gt;Detecting Near-Duplicates for Web Crawling&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;google 通过Simhash把&lt;strong&gt;一篇文本&lt;/strong&gt;映射成&lt;strong&gt;64bits的二进制串&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;文档每个&lt;strong&gt;词&lt;/strong&gt;有个&lt;strong&gt;权重&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;文档每个&lt;strong&gt;词&lt;/strong&gt;哈希成一个&lt;strong&gt;二进制串&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;文档最终的签名是各个&lt;strong&gt;词和签名的加权和(如果该位是1则+weight，如果是0，则-weight)&lt;/strong&gt;，再&lt;strong&gt;求签名[&amp;gt;0则变成1，反之变成0]&lt;/strong&gt;得到一个64位二进制数。&lt;/li&gt;
  &lt;li&gt;如果两篇文档相同，则他们simhash签名汉明距离小于等于3。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;【注：汉明距离就是a和b两个二进制串算xor，然后看有多少个1】&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;c++中快速计算1的个数：
&lt;a href=&quot;https://stackoverflow.com/questions/14682641/count-number-of-1s-in-binary-format-of-decimal-number/14682688#14682688&quot;&gt;https://stackoverflow.com/questions/14682641/count-number-of-1s-in-binary-format-of-decimal-number/14682688#14682688&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;bitset&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;climits&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;popcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bitset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CHAR_BIT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;popcount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;因为simhash本质上是局部敏感hash，所以可以使用海明距离来衡量simhash值的相似度。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/simhash_1.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;减少遍历次数&quot;&gt;减少遍历次数&lt;/h4&gt;

&lt;h5 id=&quot;一次拆分多份table并exact-match&quot;&gt;一次拆分多份table并exact match&lt;/h5&gt;

&lt;p&gt;试想所有文档都用64bits代表，如果想要找到汉明距离小于等于3得文档，也有需要两两比较的问题，那这样又回到时间复杂度是O(n^2)这个问题。&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;http://grunt1223.iteye.com/blog/964564&quot;&gt;我的数学之美系列二 —— simhash与重复信息识别&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;!!!!!!思想!!!!!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;假设我们要寻找海明距离3以内的数值，根据抽屉原理，只要我们将整个64位的二进制串划分为4块，无论如何，匹配的两个simhash code之间至少有一块区域是完全相同的。&lt;/strong&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/simhash_chouti.jpg&quot; style=&quot;max-height: 100px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;由于我们无法事先得知完全相同的是哪一块区域，因此我们必须采用存储&lt;strong&gt;多份table&lt;/strong&gt;的方式。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;将64位的二进制串等分成&lt;strong&gt;四块&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;调整上述64位二进制，&lt;strong&gt;将任意一块作为前16位&lt;/strong&gt;，总共有四种组合，&lt;strong&gt;生成四份table&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;采用&lt;strong&gt;精确匹配&lt;/strong&gt;的方式查找&lt;strong&gt;前16位&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;如果样本库中存有&lt;strong&gt;2^34（差不多10亿）&lt;/strong&gt;的哈希指纹，则&lt;strong&gt;每个table&lt;/strong&gt;返回&lt;strong&gt;2^(34-16)=262144个候选&lt;/strong&gt;结果&lt;strong&gt;【这里的16是前16位的16！！下面16张表的28同理！！！】&lt;/strong&gt;，大大减少了汉明距离的计算成本&lt;/li&gt;
&lt;/ol&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/simhash_2.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;我们可以将这种方法拓展成多种配置，不过，请记住，table的数量与每个table返回的结果呈此消彼长的关系，也就是说，&lt;strong&gt;时间效率与空间效率不可兼得&lt;/strong&gt;，参看下图：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/simhash_4.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h5 id=&quot;k3时的一般化方法&quot;&gt;k!=3时的一般化方法&lt;/h5&gt;

&lt;p&gt;参考&lt;a href=&quot;http://leoncom.org/?p=650607&quot;&gt;simhash与Google的网页去重&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;问题：一个80亿的&lt;strong&gt;64-bit指纹组成的集合Q&lt;/strong&gt;，对于一个&lt;strong&gt;给定64-bit的指纹F&lt;/strong&gt;，如何在a few millionseconds中找到Q中和F至多只有&lt;strong&gt;k(k=3)位&lt;/strong&gt;差别的指纹。&lt;/p&gt;

&lt;p&gt;思想：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;对于一个具有2^d个记录的集合，只需要考虑d-bit hash。&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;选取一个d’使得&lt;/td&gt;
          &lt;td&gt;d’-d&lt;/td&gt;
          &lt;td&gt;十分小，因此如果两fingerprint在d’-bits上都相同，那么在d-bits也很可能相同。&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;然后在这些d-bit match的结果中寻找整个f-bit的Hamming Distance小于k的fingerprint。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;简单的说，就是利用fingerprint&lt;strong&gt;少量特征位数比较&lt;/strong&gt;从而&lt;strong&gt;首先缩小范围&lt;/strong&gt;，然后再去确定是否差异小于k个bit。&lt;/p&gt;

&lt;p&gt;算法：&lt;/p&gt;

&lt;p&gt;建表：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先对于集合Q&lt;strong&gt;构建多个表T1，T2…Tt&lt;/strong&gt;，每一个表都是采用对应的置换函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi _i\)&lt;/code&gt;将64-bit的fingerprint中的某&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_i\)&lt;/code&gt;位序列置换换到整个序列的&lt;strong&gt;最前面&lt;/strong&gt;。即每个表存储都是整个Q的fingerprint的复制置换。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;查询：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;step1: 对于给定的F，在&lt;strong&gt;每个Ti中进行匹配&lt;/strong&gt;，寻找&lt;strong&gt;所有前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_i\)&lt;/code&gt;位&lt;/strong&gt;与&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi _i(F)\)&lt;/code&gt;的前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_i)&lt;/code&gt;位相同&lt;/strong&gt;的fingerprint。&lt;/li&gt;
  &lt;li&gt;step2: 对于所有在step 1中匹配到的置换后的fingerprint，计算其是否与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\pi _i(F)\)&lt;/code&gt;至多有k-bit不同。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;算法的重点在于对于集合Q的&lt;strong&gt;分表&lt;/strong&gt;以及&lt;strong&gt;每个表所对应的置换函数&lt;/strong&gt;，假设对于64-bit的fingerprint，k=3，存储16个table，划分参考下图:&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/simhash_3.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;将64-bit按照16位划分为4个区间，每个区间剩余的48-bit再按照&lt;strong&gt;每个12-bit&lt;/strong&gt;划分为4个区间，因此总共16个table并行查找，即使三个不同的k-bit落在A、B、C、D中三个不同的区块，此划分方法也不会导致遗漏。&lt;/p&gt;

&lt;p&gt;而真正需要比较的是前16+12=28位，所以，如果有2^34个指纹，那么候选将变为2^(34-28)个结果。&lt;/p&gt;

&lt;p&gt;最优table数的选取：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/simhash_optimal_num_of_tables.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;稠密向量的相似查找annoy&quot;&gt;稠密向量的相似查找——Annoy&lt;/h2&gt;

&lt;p&gt;github: &lt;a href=&quot;https://github.com/spotify/annoy&quot;&gt;https://github.com/spotify/annoy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Annoy的目标是建立一个数据结构，使得&lt;strong&gt;查询一个点的最近邻点的时间复杂度是次线性。&lt;/strong&gt;Annoy 通过建立一个二叉树来使得每个点查找时间复杂度是O(log n)。&lt;/p&gt;

&lt;h3 id=&quot;建树过程&quot;&gt;建树过程&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;随机选择两个点，以这两个节点为初始中心节点，执行聚类数为2的kmeans过程，最终产生收敛后两个聚类中心点。&lt;/li&gt;
  &lt;li&gt;这两个聚类中心点之间连一条线段（灰色短线），建立一条垂直于这条灰线，并且通过灰线中心点的线（黑色粗线）。这条黑色粗线把数据空间分成两部分。在多维空间的话，这条黑色粗线可以看成等距垂直超平面。&lt;/li&gt;
  &lt;li&gt;在划分的子空间内进行不停的递归迭代继续划分，知道每个子空间最多只剩下K个数据节点。&lt;/li&gt;
  &lt;li&gt;通过多次递归迭代划分的话，最终原始数据会形成类似下面这样一个二叉树结构。二叉树底层是叶子节点记录原始数据节点，其他中间节点记录的是分割超平面的信息。Annoy建立这样的二叉树结构是希望满足这样的一个假设:  相似的数据节点应该在二叉树上位置更接近，一个分割超平面不应该把相似的数据节点分割二叉树的不同分支上。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;查询过程&quot;&gt;查询过程&lt;/h3&gt;

&lt;p&gt;查找的过程就是不断看他在分割超平面的哪一边。从二叉树索引结构来看，就是从根节点不停的往叶子节点遍历的过程。通过对二叉树每个中间节点（分割超平面相关信息）和查询数据节点进行相关计算来确定二叉树遍历过程是往这个中间节点左孩子节点走还是右孩子节点走。通过以上方式完成查询过程。&lt;/p&gt;

&lt;h3 id=&quot;查询中的问题与解决方法&quot;&gt;查询中的问题与解决方法&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;问题1：查询过程最终落到叶子节点的数据节点数小于 我们需要的Top N相似邻居节点数目怎么办？&lt;/li&gt;
  &lt;li&gt;问题2：两个相近的数据节点划分到二叉树不同分支上怎么办？&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;解决方法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果分割超平面的两边都很相似，那可以两边都遍历。&lt;/li&gt;
  &lt;li&gt;建立多棵二叉树树，构成一个森林，每个树建立机制都如上面所述那样。&lt;/li&gt;
  &lt;li&gt;采用优先队列机制：采用一个优先队列来遍历二叉树，从根节点往下的路径，根据查询节点与当前分割超平面距离（margin）进行排序。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;返回最终近邻节点&quot;&gt;返回最终近邻节点&lt;/h3&gt;

&lt;p&gt;每棵树都返回一堆近邻点后，如何得到最终的Top N相似集合呢？
首先所有树返回近邻点都插入到优先队列中，求并集去重, 然后计算和查询点距离，最终根据距离值从近距离到远距离排序，返回Top N近邻节点集合。&lt;/p&gt;

&lt;h3 id=&quot;性能对比&quot;&gt;性能对比&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/ann-benchmark.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;hnsw&quot;&gt;HNSW&lt;/h2&gt;

&lt;h2 id=&quot;faiss&quot;&gt;faiss&lt;/h2&gt;

&lt;p&gt;对于&lt;strong&gt;小数据集和中型规模&lt;/strong&gt;的数据集(几个million-几十个million)， FALCONN(LSH开源实现)和NMSLIB是一个非常不错的选择，&lt;/p&gt;

&lt;p&gt;如果对于&lt;strong&gt;大型规模数据集&lt;/strong&gt;(几百个million以上，亿级别)，基于&lt;strong&gt;矢量量化的Faiss&lt;/strong&gt;是一个明智的选择。&lt;/p&gt;

&lt;p&gt;矢量量化方法，即vector quantization，其具体定义为：将一个向量空间中的点&lt;strong&gt;用其中的一个有限子集&lt;/strong&gt;来&lt;strong&gt;进行编码&lt;/strong&gt;的过程。在矢量量化编码中，关键是&lt;strong&gt;码本的建立&lt;/strong&gt;和&lt;strong&gt;码字搜索&lt;/strong&gt;算法。&lt;/p&gt;

&lt;p&gt;比如常见的聚类算法，就是一种矢量量化方法。而在ANN近似最近邻搜索中，向量量化方法又以&lt;strong&gt;乘积量化&lt;/strong&gt;(PQ, Product Quantization)最为典型。&lt;/p&gt;

&lt;h3 id=&quot;pq乘积量化&quot;&gt;PQ乘积量化&lt;/h3&gt;

&lt;h3 id=&quot;ivfpq倒排乘积量化&quot;&gt;IVFPQ(倒排乘积量化)&lt;/h3&gt;

&lt;h2 id=&quot;lopq&quot;&gt;lopq&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>ftrl</title>
   <link href="http://daiwk.github.io/posts/ml-ftrl.html"/>
   <updated>2017-08-04T00:00:00+00:00</updated>
   <id>/posts/ml-ftrl</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%83%8c%e6%99%af&quot;&gt;背景&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e9%97%ae%e9%a2%98%e6%8f%8f%e8%bf%b0&quot;&gt;问题描述&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#google%e7%9a%84%e5%87%a0%e7%af%87%e9%87%8d%e8%a6%81paper&quot;&gt;google的几篇重要paper&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#adaptive-bound-optimization-for-online-convex-optimization&quot;&gt;Adaptive Bound Optimization for Online Convex Optimization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#a-unified-view-of-regularized-dual-averaging-and-mirror-descent-with-implicit-updates&quot;&gt;A Unified View of Regularized Dual Averaging and Mirror Descent with Implicit Updates&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#follow-the-regularized-leader-and-mirror-descent-equivalence-theorems-and-l1-regularization&quot;&gt;Follow-the-Regularized-Leader and Mirror Descent: Equivalence Theorems and L1 Regularization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ad-click-prediction-a-view-from-the-trenches&quot;&gt;Ad Click Prediction: a View from the Trenches&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.cnblogs.com/EE-NovRain/p/3810737.html&quot;&gt;http://www.cnblogs.com/EE-NovRain/p/3810737.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;背景&quot;&gt;背景&lt;/h2&gt;

&lt;p&gt;传统的批量（batch）算法无法有效地处理超大规模的数据集和在线数据流，FTRL（Follow-the-regularized-Leader）在处理诸如逻辑回归之类的带非光滑正则化项（例如1范数，做模型复杂度控制和稀疏化）的凸优化问题上性能非常出色。&lt;/p&gt;

&lt;h3 id=&quot;问题描述&quot;&gt;问题描述&lt;/h3&gt;

&lt;p&gt;对于loss函数+正则化的结构风险最小化的优化问题（逻辑回归也是这种形式）有两种等价的描述形式，以1范数为例&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;无约束优化形式的soft regularization formulation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{w}=\underset{w}{\textrm{argmin}}\sum_{i=1}^{n}L(w,z_i)+g||w||_1
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;带约束项的凸优化问题convex constraint formulation：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{w}=\underset{w}{\textrm{argmin}}\sum_{i=1}^{n}L(w,z_i), s.t.||w||_1\leq s
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;google的几篇重要paper&quot;&gt;google的几篇重要paper&lt;/h2&gt;

&lt;p&gt;这个讲得不错：&lt;a href=&quot;https://zhuanlan.zhihu.com/p/36410780&quot;&gt;https://zhuanlan.zhihu.com/p/36410780&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;adaptive-bound-optimization-for-online-convex-optimization&quot;&gt;Adaptive Bound Optimization for Online Convex Optimization&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://static.googleusercontent.com/media/research.google.com/zh-CN//pubs/archive/36483.pdf&quot;&gt;Adaptive Bound Optimization for Online Convex Optimization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2010年的理论性的paper，但未显式地支持正则化项迭代。&lt;/p&gt;

&lt;h3 id=&quot;a-unified-view-of-regularized-dual-averaging-and-mirror-descent-with-implicit-updates&quot;&gt;A Unified View of Regularized Dual Averaging and Mirror Descent with Implicit Updates&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://pdfs.semanticscholar.org/50eb/06a0e58962715393d7adc26318b54521db9b.pdf&quot;&gt;A Unified View of Regularized Dual Averaging and Mirror Descent with Implicit Updates&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;11年的paper，证明了regret bound以及引入通用的正则化项&lt;/p&gt;

&lt;h3 id=&quot;follow-the-regularized-leader-and-mirror-descent-equivalence-theorems-and-l1-regularization&quot;&gt;Follow-the-Regularized-Leader and Mirror Descent: Equivalence Theorems and L1 Regularization&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://static.googleusercontent.com/media/research.google.com/zh-CN//pubs/archive/37013.pdf&quot;&gt;Follow-the-Regularized-Leader and Mirror Descent: Equivalence Theorems and L1 Regularization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;11年，FOBOS、RDA、FTRL等各种方法对比的paper&lt;/p&gt;

&lt;p&gt;高度概括呢（参考开篇说到的那篇博客），就是最小化的loss有三项：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ftrl-and-other.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;第一项：梯度或累积梯度；&lt;/li&gt;
  &lt;li&gt;第二项：L1正则化项的处理(注：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Psi(x)\)&lt;/code&gt;是一个任意的非平滑的凸函数，例如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\Psi(x)=\lambda \|x\|_{1}\)&lt;/code&gt;)；&lt;/li&gt;
  &lt;li&gt;第三项：这个累积加和限定了新的迭代结果x&lt;strong&gt;不要离已迭代过的解太远&lt;/strong&gt;（也即FTRL-Proximal中&lt;strong&gt;proximal&lt;/strong&gt;的含义），或者&lt;strong&gt;离0太远（central）&lt;/strong&gt;，这一项其实也是&lt;strong&gt;low regret&lt;/strong&gt;的需求&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;ad-click-prediction-a-view-from-the-trenches&quot;&gt;Ad Click Prediction: a View from the Trenches&lt;/h3&gt;

&lt;p&gt;把ftrl用在ctr预估上的工程性的论文：&lt;a href=&quot;http://www.eecs.tufts.edu/~dsculley/papers/ad-click-prediction.pdf&quot;&gt;Ad Click Prediction: a View from the Trenches&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如最开始提到的那篇博客的作者所说，这里的per-coordinate，其意思是FTRL是&lt;strong&gt;对w每一维分开训练更新的&lt;/strong&gt;，&lt;strong&gt;每一维&lt;/strong&gt;使用的是&lt;strong&gt;不同的学习速率&lt;/strong&gt;，也是上面代码中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\lambda_2\)&lt;/code&gt;之前的那一项。与w所有特征维度使用统一的学习速率相比，这种方法考虑了&lt;strong&gt;训练样本本身在不同特征上分布的不均匀性&lt;/strong&gt;，如果包含w某一个维度特征的训练样本很少，每一个样本都很珍贵，那么该特征维度对应的训练速率可以独自保持比较大的值，每来一个包含该特征的样本，就可以在该样本的梯度上前进一大步，而&lt;strong&gt;不需要与其他特征维度的前进步调强行保持一致&lt;/strong&gt;。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>异常检测常用算法</title>
   <link href="http://daiwk.github.io/posts/ml-anormaly-detection.html"/>
   <updated>2017-08-04T00:00:00+00:00</updated>
   <id>/posts/ml-anormaly-detection</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-%e6%a6%82%e8%bf%b0&quot;&gt;1. 概述&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-%e5%bc%82%e5%b8%b8%e7%9a%84%e5%8e%9f%e5%9b%a0&quot;&gt;1.1 异常的原因&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-%e6%96%b9%e6%b3%95&quot;&gt;1.2 方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-%e6%96%b9%e6%b3%95&quot;&gt;1.3 方法&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-%e6%97%a0%e7%9b%91%e7%9d%a3%e7%ae%97%e6%b3%95&quot;&gt;2. 无监督算法&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-%e4%bd%9c%e7%94%a8%e4%ba%8enumeric-data%e7%9a%84%e6%96%b9%e6%b3%95&quot;&gt;2.1 作用于numeric data的方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-%e4%bd%9c%e7%94%a8%e4%ba%8ecategorical-data%e7%9a%84%e6%96%b9%e6%b3%95&quot;&gt;2.2 作用于categorical data的方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#23-exact-methods-vs-probalistic-methods&quot;&gt;2.3 Exact methods v.s. Probalistic methods&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#iforest&quot;&gt;iForest&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%be%ae%e8%bd%af%e7%9a%84%e5%bc%82%e5%b8%b8%e6%a3%80%e6%b5%8b%e6%9c%8d%e5%8a%a1&quot;&gt;微软的异常检测服务&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.163.com/zhoulili1987619@126/blog/static/353082012015211114642546&quot;&gt;数据挖掘（七）—异常检测&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-概述&quot;&gt;1. 概述&lt;/h2&gt;

&lt;p&gt;异常对象呗称为离群点(oulier)。异常检测也称偏差检测(deviation detection),因为异常对象的属性值明显偏离期望的或常见的属性值。异常检测也称例外挖掘(exception mining),因为异常在某种意义上是例外的。&lt;/p&gt;

&lt;h3 id=&quot;11-异常的原因&quot;&gt;1.1 异常的原因&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;数据来源于不同的类：某个数据对象可能不同于其他数据对象(即异常)，因为它属于一个不同的类型或类。
Hawkins 的离群点定义：离群点是一个观测值，它与其他观测值的差别如此之大，以至于怀疑它是由不同的机制产生的。&lt;/li&gt;
  &lt;li&gt;自然变异：许多数据集可以用一个统计分布建模，如正态(高斯)分布建模，其中数据对象的概率随对象到分布中心距离的增加而急剧减少。换言之，大部分数据对象靠近中心(平均对象)，数据对象显著地不同于这个平均对象的似然性很小。&lt;/li&gt;
  &lt;li&gt;数据测量和收集误差：数据收集和测量过程中的误差是另一个异常源。剔除这类异常是数据预处理(尤其是数据清理)的关注点。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;12-方法&quot;&gt;1.2 方法&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;基于模型的技术：许多异常检测技术首先建立一个数据模型。异常是那些同模型不能完美拟合的对象。&lt;/li&gt;
  &lt;li&gt;基于邻近度的技术：通常可以在对象之间定义邻近性度量，并且许多异常检测方法都基于邻近度。异常对象是那些远离大部分其他对象的对象，这一邻域的许多技术都基于距离，称作基于距离的离群点检测技术。&lt;/li&gt;
  &lt;li&gt;基于密度的技术：对象的密度估计可以相对直接地计算，特别是当对象之间存在邻近度度量时。低密度区域中的对象相对远离近邻，可能被看做异常。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;13-方法&quot;&gt;1.3 方法&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;有监督的异常检测：有异常类和正常类的训练集(可能存在多个正常类或异常类)&lt;/li&gt;
  &lt;li&gt;无监督的异常检测：在许多实际情况下，没有label，目标是将一个得分(或标号)赋予每个实例，反映该实例是异常的程度。注意：许多互相相似的异常的类可能导致他们都被标为正常，或具有较低的离群点得分。&lt;/li&gt;
  &lt;li&gt;半监督的异常检测：在半监督的情况下，目标是使用有标记的正常对象的信息，对于给定的对象集合，发现异常标号或得分。注意：在这种情况下，&lt;strong&gt;被评分对象集中许多相关的离群点的出现并不影响离群点的评估。&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-无监督算法&quot;&gt;2. 无监督算法&lt;/h2&gt;

&lt;h3 id=&quot;21-作用于numeric-data的方法&quot;&gt;2.1 作用于numeric data的方法&lt;/h3&gt;

&lt;p&gt;这类方法是基于异常数据属于低密度的区域的假设。这些方法都依赖数值数据的一个重要特性：序关系。所以类别数据如果可以将类别属性转换成数据属性，也可以用这类方法。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;基于距离的k-NN/LDOF&lt;/li&gt;
  &lt;li&gt;基于密度的LOF&lt;/li&gt;
  &lt;li&gt;基于聚类的CBOF&lt;/li&gt;
  &lt;li&gt;基于Isolation的iForest&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;假设有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;个样本，大部分方法时间复杂度都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(d^2)\)&lt;/code&gt;,如kNN/LOF；如果用R*-Tree之类的索引方法，可以达到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(dlogd)\)&lt;/code&gt;，但对于高维数据和类别数据，索引方法是会失效的。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;iforest基于子空间和子样本。&lt;/p&gt;

&lt;h3 id=&quot;22-作用于categorical-data的方法&quot;&gt;2.2 作用于categorical data的方法&lt;/h3&gt;

&lt;p&gt;学术界对这类数据的研究很少，大都基于异常数据属于低频区域的假设。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;基于频率pattern的FPOF&lt;/li&gt;
  &lt;li&gt;基于infrequent（罕见） pattern的LOADED&lt;/li&gt;
  &lt;li&gt;基于pattern的压缩算法：KRIMP和COMPREX。KRIMP基于frequent itemset；COMPREX利用Min Description Length原则，自动地从attribute groups(子空间)生成高信息增益的pattern，并且避免了costly frequent itemset search。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;上述三类模型都是基于&lt;strong&gt;全量&lt;/strong&gt;训练数据，训练一个model。&lt;/p&gt;

&lt;h3 id=&quot;23-exact-methods-vs-probalistic-methods&quot;&gt;2.3 Exact methods v.s. Probalistic methods&lt;/h3&gt;

&lt;p&gt;Exact methods对于相同的criteria，会给出相同的结果,概率方法则不然。kNN/LOF/FPOF/COMPREX都是exact methods。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;许多exact方法由于需要全量的数据集，所以会受困于masking(由于其他异常点的存在，有些异常点被忽略了)或者swamping(由于一些异常点的存在，有些点被误判为异常点了)。但概率方法基于子集，对这两类问题会更鲁棒。&lt;/li&gt;
  &lt;li&gt;exact方法使用了所有属性，相比只使用部分属性的概率方法而言，对不相关和噪音特征会更加敏感&lt;/li&gt;
  &lt;li&gt;因为不需要进行距离计算或者/且不需要搜索子空间，概率方法的时间复杂度更低，对大数据集尤为如此。&lt;/li&gt;
  &lt;li&gt;在准确率方面，概率方法至少会和持平，且大部分时间会表现得更好。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;iforest&quot;&gt;iForest&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25040651&quot;&gt;https://zhuanlan.zhihu.com/p/25040651&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;微软的异常检测服务&quot;&gt;微软的异常检测服务&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1906.03821&quot;&gt;Time-Series Anomaly Detection Service at Microsoft&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;时序数据的异常检测在实际生产环境中有着重要且广泛的应用，包括业务指标、DevOps 数据以及 IOT Sensor 数据的监控等。及时地发现异常可以有效降低企业成本，增加服务的稳定性。构建一个准确并实用的异常检测算法主要有以下几点挑战：1）缺乏标注：在系统中有百万级别的时间序列，我们很难对每种类型的数据进行足够的标注；2）泛化：时间序列的模式多种多样，产品要求算法对各式各样的时间序列具备很好的泛化能力；3）效率：线上服务对算法的执行效率有较高要求，一些复杂的模型虽然精度不错，但效率达不到产品的要求。&lt;/p&gt;

&lt;p&gt;在微软内部，AI Platform of Cloud+AI 产品组构建了一个异常检测平台，应用于包括 Azure、Office 以及必应在内的多个产品组，同时在微软认知服务上提供 API 供外部客户使用。我们针对该产品面临的挑战，发明了一种准确、高效的异常检测算法，带来了显著的效果提升。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>传统ctr预估</title>
   <link href="http://daiwk.github.io/posts/dl-traditional-ctr-models.html"/>
   <updated>2017-08-04T00:00:00+00:00</updated>
   <id>/posts/dl-traditional-ctr-models</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-基础知识&quot;&gt;0. 基础知识&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#特征离散化&quot;&gt;特征离散化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#特征抽取&quot;&gt;特征抽取&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#特征文件&quot;&gt;特征文件&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#ins&quot;&gt;ins&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#训练embedding训练lr&quot;&gt;训练（embedding训练/LR）&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#id化&quot;&gt;ID化&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#模型训练&quot;&gt;模型训练&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#模型格式转换&quot;&gt;模型格式转换&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#指标&quot;&gt;指标&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#auc定义&quot;&gt;AUC定义&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#aucqaucwqauc计算&quot;&gt;AUC/QAUC/WQAUC计算&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#预估ctr计算&quot;&gt;预估ctr计算&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#auc计算&quot;&gt;AUC计算&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#auc汇总&quot;&gt;AUC汇总&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#模型校验&quot;&gt;模型校验&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-传统方法&quot;&gt;1. 传统方法：&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#lr&quot;&gt;lr&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#owlqn&quot;&gt;OWLQN&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#牛顿法&quot;&gt;牛顿法&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#阻尼牛顿法&quot;&gt;阻尼牛顿法&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#拟牛顿法-quasi-newton-method&quot;&gt;拟牛顿法 (Quasi-Newton Method)&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#lbfgs限域牛顿法&quot;&gt;LBFGS—限域牛顿法&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#owlqn方法&quot;&gt;OWLQN方法&lt;/a&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#虚梯度&quot;&gt;虚梯度&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#line-search时限定象限&quot;&gt;Line Search时限定象限&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#owlqn步骤&quot;&gt;OWLQN步骤&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#ftrl&quot;&gt;ftrl&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lrgbdt&quot;&gt;lr+gbdt&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#lr&quot;&gt;LR&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#gbdt&quot;&gt;GBDT&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#回归树&quot;&gt;回归树&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#提升树算法boosting-decision-tree&quot;&gt;提升树算法（Boosting Decision Tree）&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#gbdt-1&quot;&gt;GBDT&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#参数设置&quot;&gt;参数设置&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#facebook-lrgbdt&quot;&gt;facebook LR+GBDT&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#引入id类特征&quot;&gt;引入id类特征&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-基于深度学习的ctr预估模型&quot;&gt;2. 基于深度学习的ctr预估模型&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;0-基础知识&quot;&gt;0. 基础知识&lt;/h2&gt;

&lt;h3 id=&quot;特征离散化&quot;&gt;特征离散化&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/u013385925/article/details/80143784&quot;&gt;https://blog.csdn.net/u013385925/article/details/80143784&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;特征抽取&quot;&gt;特征抽取&lt;/h3&gt;

&lt;h4 id=&quot;特征文件&quot;&gt;特征文件&lt;/h4&gt;

&lt;p&gt;格式&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; Fea_str &lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt; fea_sign64 &lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt; show_num &lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt; clk_num &lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt; ctr &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中Fea_str：s{特征名字}：{特征内容}：slot&lt;/p&gt;

&lt;p&gt;Feature作用如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;生成自解释特征词典，可以给下游，作为广告是否置信的依据。&lt;/li&gt;
  &lt;li&gt;训练之前有个id化，会根据feature生成一份&lt;strong&gt;全局词典&lt;/strong&gt;，并将instance中的&lt;strong&gt;每一个sign映射到这个词典上&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;保存的show和click值，在后面进行特征检查时有一定帮助，对比单个ins中该特征的show和click值以及该特征的整体的show click值，分析是否存在问题。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;ins&quot;&gt;ins&lt;/h4&gt;

&lt;p&gt;格式&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; Show_num clk_num fea1_sign64:1 fea2_sign64:1 ….fean_sign64:1&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;sign是通过签名函数使“特征-字面-slot”映射到sign，作为特征的唯一标示。&lt;/p&gt;

&lt;p&gt;slot的作用：
a. 训练时，不同的slot在训练的时候会有不同的惩罚因子。
b. 特征抽取时，不同slot会有不同的过滤条件。
c. 压缩数据时，&lt;strong&gt;相同的slot的会放到一起，以slot排序&lt;/strong&gt;。
d. 计算sign时，&lt;strong&gt;字面到sign的映射，会考虑PV使其分布均匀&lt;/strong&gt;。不让PV覆盖比较大的特征的ins覆盖过于集中，减小单个节点压力。&lt;/p&gt;

&lt;h3 id=&quot;训练embedding训练lr&quot;&gt;训练（embedding训练/LR）&lt;/h3&gt;

&lt;p&gt;整个流程主要包含下载数据、特征ID化、模型训练、模型格式转换，以及模型评估。&lt;/p&gt;

&lt;p&gt;训练的主要过程是，将ins中用到的sign进行ID化，将ins分到不同的MPI机器上进行计算。使用模型进行训练，将训练得到的模型进行评估，通过计算其AUC值实现，&lt;strong&gt;如果符合一定指标就认为模型可信&lt;/strong&gt;，将模型转换为要用的模式，也就是ID到sign的映射。&lt;/p&gt;

&lt;h4 id=&quot;id化&quot;&gt;ID化&lt;/h4&gt;

&lt;p&gt;注：如果是LR，那这个weight就是一个float；如果是DNN的embedding，假设映射成一个8维向量，那这个weight就是这8维向量。&lt;/p&gt;

&lt;p&gt;ID映射的输出有三个文件：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;全局的64位的sign到本地ID的映射&lt;/strong&gt;：对sign进行排序，平均分配到不同的MPI节点上，在各个节点上变为本地行号，因此将sign映射为[MPI节点-行号]这样一个本地ID，其中行号是32位int。这个全局号会存入fea_set文件，因此fea文件格式变为：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{sign 桶号:行号 show click ctr 特征字面}&lt;/code&gt;，这个文件会传给format_model模块，进行模型format使用。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;每台机器上的本地ID和对应的权重及pv&lt;/strong&gt;：平均分配ins文件到不同的MPI节点上，也会对这些ins里的sign继续排序，变为本地ID进行模型训练。最终会整合成模型文件输出，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{桶号-行号 weight PV}&lt;/code&gt;，在format阶段和fea文件进行format。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;每台机器上的标识这台机器存储了哪些sign的bitmap&lt;/strong&gt;：每个MPI节点用一个[fea个数]大小的位数组（&lt;strong&gt;空间是全局sign的大小&lt;/strong&gt;），存放该机器上存在sign的信息，将本地的sign对应到全局sign位数组上，有该sign就置为1。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;对于过来的一条ins，会将其分配到多台机器上，分别单独计算各自特征的权重值。&lt;/p&gt;

&lt;h4 id=&quot;模型训练&quot;&gt;模型训练&lt;/h4&gt;

&lt;p&gt;一台机器只计算本地的所有的ins下的特征权重，和bitmap对应后，将各个权重平均。再分到各个机器上，继续进行迭代计算新的特征权重。—-感觉描述得不清楚。。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/qq_32742009/article/details/81839071&quot;&gt;https://blog.csdn.net/qq_32742009/article/details/81839071&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对于sigmoid而言，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y=\frac{1}{1+e^{-wx}}\)&lt;/code&gt;，它的导数是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\partial y}{\partial w}=y(1-y)\cdot (-x)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;对于LR，假设有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;条样本，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n=1,2,...N\)&lt;/code&gt;，那么对于第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;条样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_n=sigmoid(wx_n)=\frac{1}{1+e^{-wx_n}}\)&lt;/code&gt;，假设label是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_n\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_n \in\{0,1\}\)&lt;/code&gt;。交叉熵损失函数是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Loss=\sum_{n=1}^{N}(t_nlogy_n+(1-t_n)log(1-y_n))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;推导梯度如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
\frac{\partial Loss}{\partial w} &amp;amp;= \sum_{n=1}^{N}(t_n\frac{1}{y_n}\partial y_n-(1-t_n)\frac{1}{1-y_n}\partial y_n) \\ 
 &amp;amp;=  \sum_{n=1}^{N}(\frac{t_n}{y_n}-\frac{1-t_n}{1-y_n})\partial y_n \\
 &amp;amp;=  \sum_{n=1}^{N}(\frac{t_n-y_nt_n-y_n+y_nt_n}{y_n(1-y_n)})\partial y_n \\
 &amp;amp;=  \sum_{n=1}^{N}(\frac{t_n-y_n}{y_n(1-y_n)})\partial y_n \\
 &amp;amp;=  \sum_{n=1}^{N}(\frac{t_n-y_n}{y_n(1-y_n)})y_n(1-y_n)\cdot (-x_n) \\
 &amp;amp;=  \sum_{n=1}^{N}(y_n-t_n)x_n\\
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，计算第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;条样本的梯度时，需要先算出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_n\)&lt;/code&gt;。对于特征离散化之后的情况，这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_n\)&lt;/code&gt;的空间是sign的空间，所以，如果一条样本有第i个特征，那么它的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x^{i}\)&lt;/code&gt;是1，否则是0。&lt;/p&gt;

&lt;p&gt;这里用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_n^{i}(t)\)&lt;/code&gt;表示第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;条样本的第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;个sign在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;时刻的权重。假设总共有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;条样本，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;个sign。&lt;/p&gt;

&lt;p&gt;假设在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;时刻，对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;条ins而言，总共有10个特征，25个feasign。那这些feasign可能分布在4台机器上，比如第一台机器负责更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{3}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{17}\)&lt;/code&gt;的权重。总体来说，应该是分成以下几步吧吧吧：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;master节点存有当前时刻各sign的权重值&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w(t)\)&lt;/code&gt;（应该是上一时刻更新完后的值，shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1*M\)&lt;/code&gt;）&lt;/li&gt;
  &lt;li&gt;把对应权重发给对应的节点，例如，把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w^{3}(t)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w^{17}(t)\)&lt;/code&gt;发给第一个节点&lt;/li&gt;
  &lt;li&gt;节点1里有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;个ins的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{3}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{17}\)&lt;/code&gt;，那其实就有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;行的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{n=1..N}^{3}(t)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;行的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{n=1,..N}^{17}(t)\)&lt;/code&gt;，把这两个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;行的权重矩阵传给master&lt;/li&gt;
  &lt;li&gt;比如第7条ins有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{3}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{22}\)&lt;/code&gt;，分别来自节点1和节点2。那么把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{7}^{3}(t)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{7}^{22}(t)\)&lt;/code&gt;求和，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{7}(t)\)&lt;/code&gt;，这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N*1\)&lt;/code&gt;的向量。master对这&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;条样本算完这个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;后，传给各节点&lt;/li&gt;
  &lt;li&gt;对于节点1，拿到了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;，对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{3}\)&lt;/code&gt;，计算这&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;条样本每一条在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{3}\)&lt;/code&gt;上的梯度，得到一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N*1\)&lt;/code&gt;的梯度矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{k=3}\)&lt;/code&gt;。同理，对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sign^{17}\)&lt;/code&gt;，得到梯度矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_{k=17}\)&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;master收集各节点传回来的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G_k\)&lt;/code&gt;，得到一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N*M\)&lt;/code&gt;的矩阵。一列是一个sign，对每一列的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;个元素求和，得到这&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;条样本在这个sign上的总梯度。然后对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;列的权重&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w(t)\)&lt;/code&gt;的每一列（sign）用对应的梯度进行更新。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;模型格式转换&quot;&gt;模型格式转换&lt;/h4&gt;

&lt;p&gt;模型训练结束后，完成&lt;strong&gt;特征值和sign的对应&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;训练时每个节点用的是『桶号:行号』，这里通过那个全局bitmap，把这个id映射回原来的sign。所以输出的格式是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Fean_sign64 \t weight \t pv}&lt;/code&gt;。其中，Fean_sign64为feature的签名，weight即feature训练得到的权重，pv为feature展示的次数，从fea_set文件获取。&lt;/p&gt;

&lt;h3 id=&quot;指标&quot;&gt;指标&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;AUC：衡量分类器排序质量的统计指标。全称Area Under roc Curve，为ROC曲线下的面积。在评估ctr model的AUC时，ROC曲线及AUC指标（ROC曲线下方的面积）的数值主要体现出的是&lt;strong&gt;预估CTR与实际CTR排序关系的一致性&lt;/strong&gt;。实际的计算与其它应用有一定差别，此处计算AUC值的方式如show和click有关。&lt;/li&gt;
  &lt;li&gt;QAUC：每个query下的AUC值的简单平均。&lt;/li&gt;
  &lt;li&gt;WQAUC：WQAUC为query下AUC的展现加权平均&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;auc定义&quot;&gt;AUC定义&lt;/h4&gt;

&lt;p&gt;参考《百面》P30，假设正样本有P个，负样本有N个。横轴单位刻度设为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{1}{N}\)&lt;/code&gt;，纵轴单位刻度设为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{1}{P}\)&lt;/code&gt;，再根据模型输出的预测概率对样本进行&lt;strong&gt;排序（从高到低）&lt;/strong&gt;。依次遍历样本，每遇到一个&lt;strong&gt;正样本&lt;/strong&gt;，就沿&lt;strong&gt;纵轴&lt;/strong&gt;方向画一个刻度的线，遇到一个&lt;strong&gt;负样本&lt;/strong&gt;，就沿&lt;strong&gt;横轴&lt;/strong&gt;方向画一个刻度的线，直到遍历完所有样本&lt;/p&gt;

&lt;p&gt;在ctr预估场景中，ROC曲线的横轴是sum(noclk)=sum(show)-sum(clk)，纵轴是总点击数sum(click)。我们先对预估的ctr降序排列，将点依次绘制在坐标轴上，就形成了ROC曲线。曲线下方的面积/曲线终点与原点形成的矩形面积 就是AUC值。&lt;strong&gt;每绘制一个点，与前一个点连接形成的直线斜率&lt;/strong&gt;就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1-\frac{1}{ctr}\)&lt;/code&gt;，也就是实际ctr越高，斜率越大。如果&lt;strong&gt;保证ROC曲线的斜率递减&lt;/strong&gt;，就可以&lt;strong&gt;保证曲线面积最大&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;经老司机指点，好像斜率应该是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{1}{\frac{1}{ctr}-1}\)&lt;/code&gt;，推导如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/auc-calc.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;换句话说，就是在&lt;strong&gt;预估ctr递减的条件下，如果实际ctr也是递减的，AUC值最高，说明模型效果最好&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;整体的AUC就是曲线下的面积除以曲线的起点、终点锚定矩型的面积。&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;aucqaucwqauc计算&quot;&gt;AUC/QAUC/WQAUC计算&lt;/h4&gt;

&lt;h5 id=&quot;预估ctr计算&quot;&gt;预估ctr计算&lt;/h5&gt;

&lt;p&gt;针对LR场景，比如总共有fea_size个特征，如果一条ins有其中的3个特征，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_1,w_{23},w_{652}\)&lt;/code&gt;，那就是在这fea_size的bitmap里，有3个位置是1，其他位置都是0，因为做了这个从原始特征值到sign的映射后，就不再看原始值了，所以，我们的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_1x_1+w_{23}x_{23}+w_{652}x_{652}\)&lt;/code&gt;其实就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_1+w_{23}+w_{652}\)&lt;/code&gt;。所以，对于一条ins，它的预估ctr就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{1}{1+e^{-\sum {weight}}}\)&lt;/code&gt;。为了计算方便，将CTR放大&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(10^{16}\)&lt;/code&gt;倍，变成一个uint64的整数。&lt;/p&gt;

&lt;p&gt;embed的时候 不就是在那个fea_size*emb_size(比如10亿特征，8维向量，就是10亿*8)的大矩阵里，比如你这条样本命中了3个feasign，就是取这3个对应的8维向量出来嘛。lr就是emb_size=1，所以是10亿*1。&lt;/p&gt;

&lt;h5 id=&quot;auc计算&quot;&gt;AUC计算&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;计算AUC时，要求预估的ctr值满足桶内有序，桶间有序，做到全局有序。&lt;/li&gt;
  &lt;li&gt;计算QAUC时，只要求相同query下的ctr值有序。&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;auc汇总&quot;&gt;AUC汇总&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;AUC汇总&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以两个分桶为例，每个分桶计算的AUC为图中的阴影部分。全局AUC部分需要补充P3部分的面积，&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/auc-summary.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;看更多一点的桶：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/auc-summary-more.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;这部分面积等于前i-1个桶的sum(click)乘以每i个桶的noclick。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;QAUC/WAUC汇总&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用每个桶输出的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(qauc_i\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(query\_num_i\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(wqauc_i\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sum\_show_i\)&lt;/code&gt;，使用如下公式计算整体的Qauc和Wqauc。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(QAUC= \sum (qauc_i * query\_num_i ) / \sum(query\_num_i)\)&lt;/code&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(WQAUC= \sum (wqauc_i * sum\_show_i ) / \sum( sum\_show_i)\)&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;模型校验&quot;&gt;模型校验&lt;/h4&gt;

&lt;p&gt;如果AUC值低于一定阈值会发出警报。&lt;/p&gt;

&lt;h2 id=&quot;1-传统方法&quot;&gt;1. 传统方法：&lt;/h2&gt;

&lt;h3 id=&quot;lr&quot;&gt;lr&lt;/h3&gt;

&lt;h4 id=&quot;owlqn&quot;&gt;OWLQN&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.arvinzyy.cn/2019/05/19/Scalable-Training-of-L1-Regularized-Log-Linear-Models/&quot;&gt;http://www.arvinzyy.cn/2019/05/19/Scalable-Training-of-L1-Regularized-Log-Linear-Models/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/hero_fantao/article/details/39321887&quot;&gt;https://blog.csdn.net/hero_fantao/article/details/39321887&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;牛顿法&quot;&gt;牛顿法&lt;/h5&gt;

&lt;h5 id=&quot;阻尼牛顿法&quot;&gt;阻尼牛顿法&lt;/h5&gt;

&lt;h5 id=&quot;拟牛顿法-quasi-newton-method&quot;&gt;拟牛顿法 (Quasi-Newton Method)&lt;/h5&gt;

&lt;h5 id=&quot;lbfgs限域牛顿法&quot;&gt;LBFGS—限域牛顿法&lt;/h5&gt;

&lt;h5 id=&quot;owlqn方法&quot;&gt;OWLQN方法&lt;/h5&gt;

&lt;h6 id=&quot;虚梯度&quot;&gt;虚梯度&lt;/h6&gt;

&lt;h6 id=&quot;line-search时限定象限&quot;&gt;Line Search时限定象限&lt;/h6&gt;

&lt;h6 id=&quot;owlqn步骤&quot;&gt;OWLQN步骤&lt;/h6&gt;

&lt;h4 id=&quot;ftrl&quot;&gt;ftrl&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/ml-ftrl.html&quot;&gt;https://daiwk.github.io/posts/ml-ftrl.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;lrgbdt&quot;&gt;lr+gbdt&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://www.cbdio.com/BigData/2015-08/27/content_3750170.htm&quot;&gt;CTR预估中GBDT与LR融合方案&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;lr-1&quot;&gt;LR&lt;/h4&gt;

&lt;p&gt;LR映射后的函数值就是CTR的预估值。这种线性模型很容易并行化，处理上亿条训练样本不是问题，但线性模型学习能力有限，需要大量特征工程预先分析出有效的特征、特征组合，从而去间接增强LR 的非线性学习能力。&lt;/p&gt;

&lt;p&gt;LR模型中的特征组合很关键，但又无法直接通过特征笛卡尔积 解决，只能依靠人工经验，耗时耗力同时并不一定会带来效果提升。&lt;/p&gt;

&lt;h4 id=&quot;gbdt&quot;&gt;GBDT&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/ScorpioLu/p/8296994.html&quot;&gt;https://www.cnblogs.com/ScorpioLu/p/8296994.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.jianshu.com/p/005a4e6ac775&quot;&gt;http://www.jianshu.com/p/005a4e6ac775&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GBDT又叫MART（Multiple Additive Regression Tree)，GBDT中的树是&lt;strong&gt;回归树&lt;/strong&gt;（不是分类树），GBDT用来做回归预测，调整后也可以用于分类。每次迭代都在&lt;strong&gt;减少残差的梯度方向新建立一颗决策树&lt;/strong&gt;，迭代多少次就会生成多少颗决策树。GBDT的思想使其具有天然优势，可以发现多种有区分性的特征以及特征组合，决策树的路径可以直接作为LR输入特征使用，省去了人工寻找特征、特征组合的步骤。这种通过GBDT生成LR特征的方式（GBDT+LR），业界已有实践（Facebook，Kaggle-2014），且效果不错。&lt;/p&gt;

&lt;h5 id=&quot;回归树&quot;&gt;回归树&lt;/h5&gt;

&lt;p&gt;回归树总体流程类似于分类树，区别在于，回归树的每一个节点都会得一个预测值，以年龄为例，该预测值等于属于这个节点的所有人年龄的平均值。分枝时穷举每一个feature的每个阈值找最好的分割点，但衡量最好的标准不再是最大熵，而是最小化平方误差。也就是被预测出错的人数越多，错的越离谱，平方误差就越大，通过最小化平方误差能够找到最可靠的分枝依据。分枝直到每个叶子节点上人的年龄都唯一或者达到预设的终止条件(如叶子个数上限)，若最终叶子节点上人的年龄不唯一，则以该节点上所有人的平均年龄做为该叶子节点的预测年龄。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.csdn.net/puqutogether/article/details/44593647&quot;&gt;http://blog.csdn.net/puqutogether/article/details/44593647&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/regression-tree.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;算法如下：&lt;/p&gt;
&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/regression-tree-algorithm.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.csdn.net/suranxu007/article/details/49910323&quot;&gt;http://blog.csdn.net/suranxu007/article/details/49910323&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;提升树算法boosting-decision-tree&quot;&gt;提升树算法（Boosting Decision Tree）&lt;/h5&gt;

&lt;p&gt;提升树是迭代多棵回归树来共同决策。当采用平方误差损失函数时，每一棵回归树学习的是之前所有树的结论和残差，拟合得到一个当前的残差回归树，残差的意义如公式：残差 = 真实值 - 预测值 。提升树即是整个迭代过程生成的回归树的累加。&lt;/p&gt;

&lt;p&gt;训练集是4个人，A，B，C，D年龄分别是14，16，24，26。样本中有购物金额、上网时长、经常到百度知道提问等特征。
参考&lt;a href=&quot;http://blog.csdn.net/suranxu007/article/details/49910323&quot;&gt;http://blog.csdn.net/suranxu007/article/details/49910323&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/boosting-tree.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;预测值等于所有树值得累加，如A的预测值 = 树1左节点 值 15 + 树2左节点 -1 = 14。
算法如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/boosting-decision-tree-algorithm.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h5 id=&quot;gbdt-1&quot;&gt;GBDT&lt;/h5&gt;

&lt;p&gt;提升树利用加法模型和前向分步算法实现学习的优化过程。当损失函数是平方损失和指数损失函数时，每一步的优化很简单，如平方损失函数学习残差回归树。常见损失函数及其梯度如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/loss-func-gradients.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;但对于一般的损失函数，往往每一步优化没那么容易，如上图中的绝对值损失函数和Huber损失函数。针对这一问题，Freidman提出了梯度提升算法：利用最速下降的近似方法，即利用损失函数的负梯度在当前模型的值，作为回归问题中提升树算法的残差的近似值，拟合一个回归树。（注：鄙人私以为，与其说负梯度作为残差的近似值，不如说残差是负梯度的一种特例）算法如下&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/gbdt-algorithm.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;算法步骤解释：&lt;/p&gt;

&lt;p&gt;1、初始化，估计使损失函数极小化的常数值，它是只有一个根节点的树，即ganma是一个常数值。
2、
（a）计算损失函数的负梯度在当前模型的值，将它作为残差的估计
（b）估计回归树叶节点区域，以拟合残差的近似值
（c）利用线性搜索估计叶节点区域的值，使损失函数极小化
（d）更新回归树
3、得到输出的最终模型 f(x)&lt;/p&gt;

&lt;h5 id=&quot;参数设置&quot;&gt;参数设置&lt;/h5&gt;

&lt;p&gt;推荐GBDT树的深度：6；（横向比较：DecisionTree/RandomForest需要把树的深度调到15或更高）&lt;/p&gt;

&lt;p&gt;一句话的解释，来自周志华老师的机器学习教科书（ 机器学习-周志华）：Boosting主要关注降低偏差，因此Boosting能基于泛化性能相当弱的学习器构建出很强的集成；Bagging主要关注降低方差，因此它在不剪枝的决策树、神经网络等学习器上效用更为明显。&lt;/p&gt;

&lt;p&gt;Bagging算法是这样做的：每个分类器都随机从原样本中做有放回的采样，然后分别在这些采样后的样本上训练分类器，然后再把这些分类器组合起来。简单的多数投票一般就可以。其代表算法是随机森林。Boosting的意思是这样，他通过迭代地训练一系列的分类器，每个分类器采用的样本分布都和上一轮的学习结果有关。其代表算法是AdaBoost, GBDT。&lt;/p&gt;

&lt;p&gt;其实就机器学习算法来说，其泛化误差可以分解为两部分，偏差（bias)和方差(variance)。这个可由下图的式子导出（这里用到了概率论公式D(X)=E(X^2)-[E(X)]^2）。偏差指的是算法的期望预测与真实预测之间的偏差程度，反应了模型本身的拟合能力；方差度量了同等大小的训练集的变动导致学习性能的变化，刻画了数据扰动所导致的影响。这个有点儿绕，不过你一定知道过拟合。
  如下图所示，当模型越复杂时，拟合的程度就越高，模型的训练偏差就越小。但此时如果换一组数据可能模型的变化就会很大，即模型的方差很大。所以模型过于复杂的时候会导致过拟合。
  当模型越简单时，即使我们再换一组数据，最后得出的学习器和之前的学习器的差别就不那么大，模型的方差很小。还是因为模型简单，所以偏差会很大。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/model-complexity-and-bias-variance.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;也就是说，当我们训练一个模型时，偏差和方差都得照顾到，漏掉一个都不行。
  对于Bagging算法来说，由于我们会并行地训练很多不同的分类器的目的就是降低这个方差(variance) ,因为采用了相互独立的基分类器多了以后，h的值自然就会靠近.所以对于每个基分类器来说，目标就是如何降低这个偏差（bias),所以我们会采用深度很深甚至不剪枝的决策树。
  对于Boosting来说，每一步我们都会在上一轮的基础上更加拟合原数据，所以可以保证偏差（bias）,所以对于每个基分类器来说，问题就在于如何选择variance更小的分类器，即更简单的分类器，所以我们选择了深度很浅的决策树。&lt;/p&gt;

&lt;h4 id=&quot;facebook-lrgbdt&quot;&gt;facebook LR+GBDT&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u010352603/article/details/80681100&quot;&gt;https://blog.csdn.net/u010352603/article/details/80681100&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这个讲得很细，还有好多工业上的注意点。。&lt;a href=&quot;https://zhuanlan.zhihu.com/p/57987311&quot;&gt;https://zhuanlan.zhihu.com/p/57987311&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;需要强调是，因为我们需要利用CTR计算&lt;strong&gt;精准&lt;/strong&gt;的出价、ROI等重要的后续预估值，因此CTR模型的预估值需要是一个&lt;strong&gt;具有物理意义的精准的CTR&lt;/strong&gt;，而&lt;strong&gt;不仅仅是输出广告排序的高低关系&lt;/strong&gt;。所以文中不仅把CTR calibration作为重要的评价指标，更是在最后介绍了&lt;strong&gt;模型校正&lt;/strong&gt;的相关方法。&lt;/p&gt;

&lt;p&gt;原始paper：&lt;a href=&quot;https://quinonero.net/Publications/predicting-clicks-facebook.pdf&quot;&gt;Practical Lessons from Predicting Clicks on Ads at Facebook&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GBDT与LR的融合方式，Facebook的paper有个例子如下图2所示，图中Tree1、Tree2为通过GBDT模型学出来的两颗树，x为一条输入样本，遍历两棵树后，x样本分别落到两颗树的叶子节点上，每个&lt;strong&gt;叶子节点&lt;/strong&gt;对应LR一维特征，那么通过遍历树，就得到了该样本对应的所有LR特征。由于树的每条路径，是通过最小化均方差等方法最终分割出来的有区分性路径，根据该路径得到的特征、特征组合都相对有区分性，效果理论上不会亚于人工经验的处理方式。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/facebook-lr+gbdt.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;另外，RF也是多棵树，但从效果上有实践证明不如GBDT。且GBDT前面的树，特征分裂主要体现对多数样本有区分度的特征；后面的树，主要体现的是经过前N颗树，残差仍然较大的少数样本。&lt;strong&gt;优先选用在整体上有区分度的特征，再选用针对少数样本有区分度的特征&lt;/strong&gt;，思路更加合理，这应该也是用GBDT的原因。&lt;/p&gt;

&lt;h4 id=&quot;引入id类特征&quot;&gt;引入id类特征&lt;/h4&gt;

&lt;p&gt;综合考虑方案如下，使用GBDT建两类树，非ID建一类树，ID建一类树。&lt;/p&gt;

&lt;p&gt;1）非ID类树：不以细粒度的ID建树，此类树作为base，即便曝光少的广告、广告主，仍可以通过此类树得到有区分性的特征、特征组合。&lt;/p&gt;

&lt;p&gt;2）ID类树：以细粒度 的ID建一类树，用于发现曝光充分的ID对应有区分性的特征、特征组合。如何根据GBDT建的两类树，对原始特征进行映射？以如下图3为例，当一条样本x进来之后，遍历两类树到叶子节点，得到的特征作为LR的输入。当AD曝光不充分不足以训练树时，其它树恰好作为补充。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/lr+gbdt-with-id-feature.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;通过GBDT 映射得到的特征空间维度如何？GBDT树有多少个叶子节点，通过GBDT得到的特征空间就有多大。一个叶子节点对应一种有区分性的特征、特征组合，对应LR的一维特征。估算一下，通过GBDT转换得到的特征空间较低，Base树、ID树各N颗，特征空间维度最高为N+N&lt;em&gt;广告数+N&lt;/em&gt;广告主数+ N*广告类目数。其中广告数、广告主数、广告类目数都是有限的，同时参考Kaggle竞赛中树的数目N最多为30，则估算通过GBDT 映射得到的特征空间维度并不高，且并不是每个ID训练样本都足以训练多颗树，实际上通过GBDT 映射得到的特征空间维度更低。&lt;/p&gt;

&lt;p&gt;如何使用GBDT 映射得到的特征？通过GBDT生成的特征，可直接作为LR的特征使用，省去人工处理分析特征的环节，LR的输入特征完全依赖于通过GBDT得到的特征。此思路已尝试，通过实验发现GBDT+LR在曝光充分的广告上确实有效果，但整体效果需要权衡优化各类树的使用。同时，也可考虑将GBDT生成特征与LR原有特征结合起来使用，待尝试。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://cloud.tencent.com/developer/article/1005416&quot;&gt;https://cloud.tencent.com/developer/article/1005416&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Facebook 在 2014 年发表文章介绍了通过 GBDT 解决 LR 的特征组合问题，其主要实现原理是：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;训练时，GBDT 建树的过程相当于自动进行的特征组合和离散化，然后从根结点到叶子节点的这条路径就可以看成是不同特征进行的特征组合，用叶子节点可以唯一的表示这条路径，并作为一个离散特征传入 LR 进行二次训练。&lt;/li&gt;
  &lt;li&gt;预测时，会先走 GBDT 的每棵树，得到某个叶子节点对应的一个离散特征(即一组特征组合)，然后把该特征以 one-hot 形式传入 LR 进行线性加权预测。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Facebook 的方案在实际使用中，发现并不可行，因为广告系统往往存在上亿维的 id 类特征(用户 guid10 亿维，广告 aid 上百万维)，而 GBDT 由于树的深度和棵树的限制，无法存储这么多 id 类特征，导致信息的损失。有如下改进方案供读者参考：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;方案一：GBDT 训练除 id 类特征以外的所有特征，其他 id 类特征在 LR 阶段再加入。这样的好处很明显，既利用了 GBDT 对连续特征的自动离散化和特征组合，同时 LR 又有效利用了 id 类离散特征，防止信息损失。&lt;/li&gt;
  &lt;li&gt;方案二：GBDT 分别训练 id 类树和非 id 类树，并把组合特征传入 LR 进行二次训练。对于 id 类树可以有效保留头部资源的信息不受损失；对于非 id 类树，长尾资源可以利用其泛化信息（反馈 CTR 等）。但这样做有一个缺点是，介于头部资源和长尾资源中间的一部分资源，其有效信息即包含在泛化信息(反馈 CTR) 中，又包含在 id 类特征中，而 GBDT 的非 id 类树只存的下头部的资源信息，所以还是会有部分信息损失。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;优缺点如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;优点：GBDT 可以自动进行特征组合和离散化，LR 可以有效利用海量 id 类离散特征，保持信息的完整性。&lt;/li&gt;
  &lt;li&gt;缺点：LR 预测的时候需要等待 GBDT 的输出，一方面 GBDT在线预测慢于单 LR，另一方面 GBDT 目前不支持在线算法，只能以离线方式进行更新。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-基于深度学习的ctr预估模型&quot;&gt;2. 基于深度学习的ctr预估模型&lt;/h2&gt;

&lt;p&gt;基于深度学习的可以参考&lt;a href=&quot;https://daiwk.github.io/posts/dl-dl-ctr-models.html&quot;&gt;基于深度学习的ctr预估模型集合（持续更新）&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Deep Learning for NLP Best Practices </title>
   <link href="http://daiwk.github.io/posts/nlp-dl-for-nlp-best-practices.html"/>
   <updated>2017-07-25T00:00:00+00:00</updated>
   <id>/posts/nlp-dl-for-nlp-best-practices</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#introductioin&quot;&gt;introductioin&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://ruder.io/deep-learning-nlp-best-practices/&quot;&gt;http://ruder.io/deep-learning-nlp-best-practices/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;introductioin&quot;&gt;introductioin&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/nlp-practice-lstm-attention-works-always.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;近两年来，一直有一个&lt;a href=&quot;https://twitter.com/IAugenstein/status/710837374473920512&quot;&gt;running joke&lt;/a&gt;，lstm with attention可以在任意任务上取得state-of-the-art的表现……但最近nlp领域有更多更有趣的研究来慢慢地远离这个标准的baseline。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>paddle相关tips</title>
   <link href="http://daiwk.github.io/posts/platform-paddlepaddle-tips.html"/>
   <updated>2017-07-19T00:00:00+00:00</updated>
   <id>/posts/platform-paddlepaddle-tips</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-tips&quot;&gt;1. tips&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-%E5%AD%A6%E4%B9%A0%E7%8E%87&quot;&gt;1.1 学习率&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-%E6%97%A5%E5%BF%97%E8%A7%A3%E9%87%8A&quot;&gt;1.2 日志解释&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-%E9%9B%86%E7%BE%A4%E8%AE%BE%E7%BD%AE&quot;&gt;1.3 集群设置&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#14-%E6%AD%A3%E5%88%99&quot;&gt;1.4 正则&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#15-%E5%88%9D%E5%A7%8B%E5%8C%96&quot;&gt;1.5 初始化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#16-%E8%BE%93%E5%87%BA%E4%B8%AD%E9%97%B4%E5%B1%82&quot;&gt;1.6 输出中间层&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;1-tips&quot;&gt;1. tips&lt;/h2&gt;

&lt;h3 id=&quot;11-学习率&quot;&gt;1.1 学习率&lt;/h3&gt;

&lt;p&gt;相关issue &lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/issues/1167&quot;&gt;https://github.com/PaddlePaddle/Paddle/issues/1167&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;paddle_pr/proto/TrainerConfig.proto中有关于momentum各个参数的解释&lt;/p&gt;

&lt;p&gt;学习率太大可能会出现floating point exception，可以调小一个数量级，或者使用&lt;a href=&quot;https://github.com/PaddlePaddle/models/blob/develop/nmt_without_attention/train.py#L35&quot;&gt;梯度clipping&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;12-日志解释&quot;&gt;1.2 日志解释&lt;/h3&gt;

&lt;p&gt;Q: 日志中打出来的是 训练误差还是测试误差？&lt;/p&gt;

&lt;p&gt;A: 训练的时候会打印训练误差，测试的时候会打印测试误差&lt;/p&gt;

&lt;h3 id=&quot;13-集群设置&quot;&gt;1.3 集群设置&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;应该要保证文件数目大于节点数&lt;/li&gt;
  &lt;li&gt;单个节点的trainer数要小于batch size&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;14-正则&quot;&gt;1.4 正则&lt;/h3&gt;

&lt;p&gt;虽然损失函数里有正则，但在神经网络真正实现时，正则和损失函数是分开的两个部分。并不需要显示的将正则定义在损失函数里面。&lt;/p&gt;

&lt;p&gt;全局化的正则（会加给每一个参数），设置方法如下：
https://github.com/PaddlePaddle/models/blob/develop/mt_with_external_memory/train.py#L73&lt;/p&gt;

&lt;p&gt;如果想针对性的调整某一个参数的正则，每个可学习的参数都可以通过 param_attr 关键字来设置一些如初始化 mead、std、正则系数等关键参数，调用方式可以参考这个例子：https://github.com/PaddlePaddle/models/blob/develop/text_classification/network_conf.py#L46&lt;/p&gt;

&lt;p&gt;如果在optimizer中设置了全局正则系数，下面调用可以取消对fc层参数的正则，当然，也可以设置一个非0值，调整这个参数的正则系数。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; fc = paddle.layer.fc(
                 input=input_layer,
                 size=128,
                 param_attr=paddle.attr.Param(decay_rate=0))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;15-初始化&quot;&gt;1.5 初始化&lt;/h3&gt;

&lt;p&gt;正交初始化：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ndim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linalg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;svd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;求正交要 svd ，可以用numpy算好之后，用u做初始化&lt;/p&gt;

&lt;h3 id=&quot;16-输出中间层&quot;&gt;1.6 输出中间层&lt;/h3&gt;

&lt;p&gt;参考issue：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/issues/4153&quot;&gt;https://github.com/PaddlePaddle/Paddle/issues/4153&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>机器学习相关tips</title>
   <link href="http://daiwk.github.io/posts/ml-tips.html"/>
   <updated>2017-07-05T00:00:00+00:00</updated>
   <id>/posts/ml-tips</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;
&lt;p&gt;autoauto- &lt;a href=&quot;#李宏毅的机器学习笔记&quot;&gt;李宏毅的机器学习笔记&lt;/a&gt;auto- &lt;a href=&quot;#评价指标&quot;&gt;评价指标&lt;/a&gt;auto    - &lt;a href=&quot;#关于auc和prf1&quot;&gt;关于auc和p/r/f1…&lt;/a&gt;auto- &lt;a href=&quot;#auc&quot;&gt;auc&lt;/a&gt;auto- &lt;a href=&quot;#roc-curve-vs-precision-recall-curve&quot;&gt;roc curve v.s. precision-recall curve&lt;/a&gt;auto    - &lt;a href=&quot;#roc与pr曲线&quot;&gt;ROC与PR曲线&lt;/a&gt;auto- &lt;a href=&quot;#过拟合问题&quot;&gt;过拟合问题&lt;/a&gt;auto    - &lt;a href=&quot;#获取更多数据&quot;&gt;获取更多数据&lt;/a&gt;auto        - &lt;a href=&quot;#从数据源头获取更多数据&quot;&gt;从数据源头获取更多数据&lt;/a&gt;auto        - &lt;a href=&quot;#根据当前数据集估计数据分布参数使用该分布产生更多数据&quot;&gt;根据当前数据集估计数据分布参数，使用该分布产生更多数据&lt;/a&gt;auto        - &lt;a href=&quot;#数据增强data-augmentation&quot;&gt;数据增强（Data Augmentation）&lt;/a&gt;auto    - &lt;a href=&quot;#使用合适的模型&quot;&gt;使用合适的模型&lt;/a&gt;auto        - &lt;a href=&quot;#网络结构&quot;&gt;网络结构&lt;/a&gt;auto        - &lt;a href=&quot;#训练时间early-stopping&quot;&gt;训练时间（early stopping）&lt;/a&gt;auto        - &lt;a href=&quot;#限制权值正则化&quot;&gt;限制权值/正则化&lt;/a&gt;auto        - &lt;a href=&quot;#增加噪声&quot;&gt;增加噪声&lt;/a&gt;auto            - &lt;a href=&quot;#在输入中加噪声&quot;&gt;在输入中加噪声&lt;/a&gt;auto            - &lt;a href=&quot;#在权值上加噪声&quot;&gt;在权值上加噪声&lt;/a&gt;auto            - &lt;a href=&quot;#对网络的响应加噪声&quot;&gt;对网络的响应加噪声&lt;/a&gt;auto    - &lt;a href=&quot;#使用多种模型&quot;&gt;使用多种模型&lt;/a&gt;auto        - &lt;a href=&quot;#bagging&quot;&gt;bagging&lt;/a&gt;auto        - &lt;a href=&quot;#boosting&quot;&gt;boosting&lt;/a&gt;auto        - &lt;a href=&quot;#dropout&quot;&gt;dropout&lt;/a&gt;auto    - &lt;a href=&quot;#贝叶斯方法&quot;&gt;贝叶斯方法&lt;/a&gt;auto- &lt;a href=&quot;#模型参数与训练样本规模的关系&quot;&gt;模型参数与训练样本规模的关系&lt;/a&gt;auto    - &lt;a href=&quot;#how-much-training-data-do-you-need&quot;&gt;How much training data do you need&lt;/a&gt;auto    - &lt;a href=&quot;#lstm参数规模计算&quot;&gt;lstm参数规模计算&lt;/a&gt;auto    - &lt;a href=&quot;#embedding-layer参数规模计算&quot;&gt;embedding layer参数规模计算&lt;/a&gt;auto- &lt;a href=&quot;#其他&quot;&gt;其他&lt;/a&gt;auto    - &lt;a href=&quot;#不恰当地分割数据&quot;&gt;不恰当地分割数据&lt;/a&gt;auto    - &lt;a href=&quot;#隐变量&quot;&gt;隐变量&lt;/a&gt;auto    - &lt;a href=&quot;#定错训练目标&quot;&gt;定错训练目标&lt;/a&gt;auto- &lt;a href=&quot;#其他2&quot;&gt;其他2&lt;/a&gt;auto    - &lt;a href=&quot;#cv的tip&quot;&gt;cv的tip&lt;/a&gt;auto- &lt;a href=&quot;#学习率与batchsize&quot;&gt;学习率与batchsize&lt;/a&gt;autoauto&lt;!-- /TOC --&gt;&lt;/p&gt;

&lt;h2 id=&quot;李宏毅的机器学习笔记&quot;&gt;李宏毅的机器学习笔记&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650767463&amp;amp;idx=1&amp;amp;sn=f3dba2069d04303f88e503db771f6957&amp;amp;chksm=871a4419b06dcd0fa7d48a74e78a999518f33eff9ac8b8e1d5704b45311230d4ffa22fe385e7&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;李宏毅机器学习完整笔记发布，AI界「最热视频博主」中文课程笔记全开源&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/datawhalechina/leeml-notes&quot;&gt;https://github.com/datawhalechina/leeml-notes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;视频：&lt;a href=&quot;https://www.bilibili.com/video/av59538266&quot;&gt;https://www.bilibili.com/video/av59538266&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;评价指标&quot;&gt;评价指标&lt;/h2&gt;

&lt;h3 id=&quot;关于auc和prf1&quot;&gt;关于auc和p/r/f1…&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://stackoverflow.com/questions/34698161/how-to-interpret-almost-perfect-accuracy-and-auc-roc-but-zero-f1-score-precisio&quot;&gt;https://stackoverflow.com/questions/34698161/how-to-interpret-almost-perfect-accuracy-and-auc-roc-but-zero-f1-score-precisio&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One must understand crucial difference between AUC ROC and “point-wise” metrics like accuracy/precision etc. ROC is a function of a threshold. Given a model (classifier) that outputs the probability of belonging to each class we usually classify element to the class with the highest support. However, sometimes we can get better scores by changing this rule and requiring one support to be 2 times bigger than the other to actually classify as a given class. This is often true for imbalanced datasets. This way you are actually modifing the learned prior of classes to better fit your data. ROC looks at &lt;strong&gt;“what would happen if I change this threshold to all possible values”&lt;/strong&gt; and then AUC ROC computes the integral of such a curve.&lt;/p&gt;

&lt;p&gt;Consequently:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;high AUC ROC vs low f1 or other “point” metric,&lt;/strong&gt; means that your classifier currently does a bad job, however you can find the threshold for which its score is actually pretty decent&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;low AUC ROC and low f1 or other “point” metric,&lt;/strong&gt; means that your classifier currently does a bad job, and even fitting a threshold will not change it&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;high AUC ROC and high f1 or other “point” metric,&lt;/strong&gt; means that your classifier currently does a decent job, and for many other values of threshold it would do the same&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;low AUC ROC vs high f1 or other “point” metric,&lt;/strong&gt; means that your classifier currently does a decent job, however for many other values of threshold - it is pretty bad&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;auc&quot;&gt;auc&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u013385925/article/details/80385873&quot;&gt;https://blog.csdn.net/u013385925/article/details/80385873&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;首先，P/R/accuracy/F-measure如下：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\\precision=\frac{TP}{所有预测为正例的样本}=\frac{TP}{TP+FP}
\\recall=\frac{TP}{所有真正的正例}=\frac{TP}{TP+FN}
\\accuracy=\frac{所有预测对的}{全量}=\frac{TP+TN}{P+N}
\\F-measure=\frac{2}{\frac{1}{presion}+\frac{1}{recall}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;ROC曲线（Receiver Operating Characteristic），y轴是TPR(就是recall)，x轴是FPR(1-specificity)。(所有我预测为P的样本里，拿预测对的去算tpr(纵轴)，预测错的去算fpr(横轴))
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\\TPR=\frac{TP}{所有真正的正例}=\frac{TP}{TP+FN}
\\FPR=\frac{FP}{所有真正的负例}=\frac{FP}{FP+TN}\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;第一个点，(0,1)，即FPR=0, TPR=1，这意味着FN（false negative）=0，并且FP（false positive）=0。这是一个完美的分类器，它将所有的样本都正确分类。&lt;/li&gt;
  &lt;li&gt;第二个点，(1,0)，即FPR=1，TPR=0，类似地分析可以发现这是一个最糟糕的分类器，因为它成功避开了所有的正确答案。&lt;/li&gt;
  &lt;li&gt;第三个点，(0,0)，即FPR=TPR=0，即FP（false positive）=TP（true positive）=0，可以发现该分类器预测所有的样本都为负样本（negative）。&lt;/li&gt;
  &lt;li&gt;第四个点（1,1），分类器实际上预测所有的样本都为正样本。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;再理一遍。。。参考&lt;a href=&quot;https://blog.csdn.net/abcd1f2/article/details/51887164&quot;&gt;https://blog.csdn.net/abcd1f2/article/details/51887164&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;特异性（specificity）：即TNR。对所有负样本来说，正确识别为负的有多少。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
specificity = TNR = TN /（FP + TN）=真正的负样本/所有负样本
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;1-特异性：即FPR。所有负样本来说，错误地识别为正样本的有多少。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
FPR=FP/(FP + TN)=1-specificity
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;灵敏度（sensitivity），即TPR。就是召回率，所有正样本中，正确识别为正的有多少。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
sensitivity = TPR = TP/(TP + FN)=真正的正样本/所有正样本
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;FPR是误诊率，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;1-灵敏度：即FNR。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
FNR=FN/(TP + FN) = 1-sensitivity=1-TPR
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;总结：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;特异性是TNR，表示所有负样本里预测为负的有多少，即没病的人里，多少是诊断对了这个人确实没病，即1-误诊率&lt;/li&gt;
  &lt;li&gt;1-特异性是FPR，是ROC曲线的横轴，即误诊率&lt;/li&gt;
  &lt;li&gt;灵敏度是TPR，是ROC曲线的纵轴，表示所有有病的人里，多少是诊断对了这个人确实有病，即1-漏诊率&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;从Mann-Whitney U statistic的角度来讲，AUC就是从所有1样本中随机选取一个样本，从所有0样本中随机选取一个样本，然后根据你的分类器对两个随机样本进行预测，把1样本预测为1的概率为p1，把0样本预测为1的概率为p0，p1&amp;gt;p0的概率就等于AUC。所以AUC反应的是分类器对样本的排序能力。&lt;/p&gt;

&lt;p&gt;也就是说，一个关于AUC的很有趣的性质是，它和Wilcoxon-Mann-Witney Test是等价的。因此有如下的auc计算方法：&lt;/p&gt;

&lt;p&gt;在有限样本中我们常用的得到概率的办法就是通过频率来估计之。这种估计随着样本规模的扩大而逐渐逼近真实值。这 和上面的方法中，样本数越多，计算的AUC越准确类似，也和计算积分的时候，小区间划分的越细，计算的越准确是同样的道理。&lt;/p&gt;

&lt;p&gt;M为正类样本的数目，N为负类样本的数目，n为样本数（即n=M+N）。&lt;/p&gt;

&lt;p&gt;可以首先对score从大到小排序，然后令最大score对应的sample 的rank为n，第二大score对应sample的rank为n-1，以此类推。然后把所有的正类样本的rank相加，再减去M-1种两个正样本组合的情况。得到的就是所有的样本中有多少对正类样本的score大于负类样本的score。然后再除以M×N。即&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
AUC=\frac {\sum _{i\in positiveClass} rank_i-\frac{M(1+M)}{2}}{M\times N}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;公式解释（抄的）：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;为了求的组合中正样本的score值大于负样本，如果所有的正样本score值都是大于负样本的，那么第一位与任意的进行组合score值都要大，我们取它的rank值为n，但是n-1中有M-1是正样例和正样例的组合这种是不在统计范围内的（为计算方便我们取n组，相应的不符合的有M个），所以要减掉，那么同理排在第二位的n-1，会有M-1个是不满足的，依次类推，故得到后面的公式M*(M+1)/2，我们可以验证在正样本score都大于负样本的假设下，AUC的值为1&lt;/li&gt;
  &lt;li&gt;根据上面的解释，不难得出，rank的值代表的是能够产生score前大后小的这样的组合数，但是这里包含了（正，正）的情况，所以要减去这样的组（即排在它后面正例的个数），即可得到上面的公式&lt;/li&gt;
  &lt;li&gt;另外，特别需要注意的是，再存在score相等的情况时，对相等score的样本，需要 赋予相同的rank(无论这个相等的score是出现在同类样本还是不同类的样本之间，都需要这样处理)。具体操作就是再把所有这些score相等的样本 的rank取平均。然后再使用上述公式。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;自己理解：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum _{i\in positiveClass} rank_i\)&lt;/code&gt;的含义是所有正样本的rank之和，也就是假设正样本比后面以及自己都大，所以我们要先减掉自己，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum _{i\in positiveClass} rank_i - M\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;然后，对于第一个正样本来讲，后面的M-1个正样本不能和他组pair，同理对于第二个来讲，后面的M-2个不能组pair，所以有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((M-1)+(M-2)+...+1=M(M-1)/2\)&lt;/code&gt;个要减掉。&lt;/p&gt;

&lt;p&gt;因此，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\sum _{i\in positiveClass} rank_i - M - M(M-1)/2=\sum _{i\in positiveClass} rank_i -(2M+M^2-M)/2=\sum _{i\in positiveClass} rank_i -(M+M^2)/2=\sum _{i\in positiveClass} rank_i -M(M+1)/2
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;awk代码见assets下的&lt;a href=&quot;../assets/calc_auc.sh&quot;&gt;calc_auc.sh&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;roc-curve-vs-precision-recall-curve&quot;&gt;roc curve v.s. precision-recall curve&lt;/h2&gt;

&lt;p&gt;在PR曲线中，以Recall为x轴，Precision为y轴。Recall与TPR的意思相同，而Precision指正确分类的正样本数占总正样本的比例。&lt;/p&gt;

&lt;p&gt;既然已经这么多评价标准，为什么还要使用ROC和AUC呢？因为&lt;strong&gt;ROC曲线&lt;/strong&gt;有个很好的特性：&lt;strong&gt;当测试集中的正负样本的分布变化的时候，ROC曲线能够保持不变。&lt;/strong&gt;在实际的数据集中经常会出现类不平衡（class imbalance）现象，即负样本比正样本多很多（或者相反），而且测试数据中的正负样本的分布也可能随着时间变化。下图是ROC曲线和Precision-Recall曲线&lt;a href=&quot;http://mark.goadrich.com/articles/davisgoadrichcamera2.pdf&quot;&gt;(The relationship between Precision-Recall and ROC curves)&lt;/a&gt;的对比：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/roc_vs_pr_curve.jpg&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;在上图中，(a)和(c)为ROC曲线，(b)和(d)为Precision-Recall曲线。(a)和(b)展示的是分类其在原始测试集（正负样本分布平衡）的结果，(c)和(d)是将测试集中负样本的数量增加到原来的10倍后，分类器的结果。可以明显的看出，ROC曲线基本保持原貌，而Precision-Recall曲线则变化较大。&lt;/p&gt;

&lt;h3 id=&quot;roc与pr曲线&quot;&gt;ROC与PR曲线&lt;/h3&gt;

&lt;p&gt;参考：
&lt;a href=&quot;https://stats.stackexchange.com/questions/7207/roc-vs-precision-and-recall-curves&quot;&gt;https://stats.stackexchange.com/questions/7207/roc-vs-precision-and-recall-curves&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对于needle-in-haystack（大海捞针） type problems or problems where the “positive” class is more interesting than the negative class这类问题，更适合使用PR曲线。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\ Precision=P(Y=1|\hat {Y}=1) 
\\ Recall=Sensitivity=P(\hat{Y}=1|Y=1)
\\ Specificity=P(\hat{Y}=1|Y=0)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;组成ROC曲线的Sensitivity和Specificity，基于的都是the true class label(真实的label，而非预测的label)的条件概率(given的都是Y=1或者Y=0)。所以，不论&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(Y=1)\)&lt;/code&gt;是多少，他们都是一样的。&lt;/p&gt;

&lt;p&gt;而precision和sensitivity组成了pr曲线。Precision基于的条件是你预测的，所以，当不同的数据集有不同的类别分布时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(Y=1)\)&lt;/code&gt;就会有差别，所以就有不同的baseline的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(Y=1)\)&lt;/code&gt;,那么，你预测的类别就会不一样。&lt;/p&gt;

&lt;p&gt;如果你只关心某一种已知background probabily的population，而且正例比负例更加interesting（例如，文档检索领域precision很流行），由于Precision正好回答了”What is the probability that this is a real hit given my classifier says it is?”，所以，PR曲线更适合。&lt;/p&gt;

&lt;p&gt;因此，&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;PR曲线适用于：『”How meaningful is a positive result from my classifier given the baseline probabilities of my problem?”』这类问题。&lt;/li&gt;
  &lt;li&gt;ROC曲线适用于：『”How well can this classifier be expected to perform in general, at a variety of different baseline probabilities?”』这类问题。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在ROC空间，ROC曲线越&lt;strong&gt;凸向左上方&lt;/strong&gt;向效果越好。与ROC曲线左上凸不同的是，PR曲线是&lt;strong&gt;右上凸&lt;/strong&gt;效果越好。&lt;/p&gt;

&lt;p&gt;ROC和PR曲线都被用于评估机器学习算法对一个给定数据集的分类性能，每个数据集都包含固定数目的正样本和负样本。而ROC曲线和PR曲线之间有着很深的关系。&lt;/p&gt;

&lt;p&gt;定理1：对于一个给定的包含正负样本的数据集，ROC空间和PR空间存在一一对应的关系，也就是说，如果recall不等于0，二者包含完全一致的混淆矩阵。我们可以将ROC曲线转化为PR曲线，反之亦然。&lt;/p&gt;

&lt;p&gt;定理2：对于一个给定数目的正负样本数据集，一条曲线在ROC空间中比另一条曲线有优势，当且仅当第一条曲线在PR空间中也比第二条曲线有优势。（这里的“一条曲线比其他曲线有优势”是指其他曲线的所有部分与这条曲线重合或在这条曲线之下。）&lt;/p&gt;

&lt;h2 id=&quot;过拟合问题&quot;&gt;过拟合问题&lt;/h2&gt;

&lt;p&gt;如何防止过拟合：
&lt;a href=&quot;https://www.zhihu.com/question/59201590/answer/167392763&quot;&gt;https://www.zhihu.com/question/59201590/answer/167392763&lt;/a&gt;
本质是：数据太少+模型太复杂
具体表现就是最终模型在训练集上效果好；在测试集上效果差。模型泛化能力弱。&lt;/p&gt;

&lt;h3 id=&quot;获取更多数据&quot;&gt;获取更多数据&lt;/h3&gt;

&lt;h4 id=&quot;从数据源头获取更多数据&quot;&gt;从数据源头获取更多数据&lt;/h4&gt;

&lt;p&gt;在很多情况下，大幅增加数据本身就不容易；另外，我们不清楚获取多少数据才算够。&lt;/p&gt;

&lt;h4 id=&quot;根据当前数据集估计数据分布参数使用该分布产生更多数据&quot;&gt;根据当前数据集估计数据分布参数，使用该分布产生更多数据&lt;/h4&gt;

&lt;p&gt;一般不用，因为估计分布参数的过程也会代入抽样误差&lt;/p&gt;

&lt;h4 id=&quot;数据增强data-augmentation&quot;&gt;数据增强（Data Augmentation）&lt;/h4&gt;

&lt;p&gt;例如在图像分类问题中，物体在图像中的位置、姿态、尺度，整体图片明暗度等都不会影响分类结果，所以，可以通过图像平移、翻转、缩放、切割等手段将数据库成倍扩充。&lt;/p&gt;

&lt;h3 id=&quot;使用合适的模型&quot;&gt;使用合适的模型&lt;/h3&gt;

&lt;h4 id=&quot;网络结构&quot;&gt;网络结构&lt;/h4&gt;

&lt;p&gt;减少网络的层数、神经元个数等来限制网络的拟合能力。&lt;/p&gt;

&lt;h4 id=&quot;训练时间early-stopping&quot;&gt;训练时间（early stopping）&lt;/h4&gt;

&lt;p&gt;当网络权值较小时，神经元的激活函数工作在线性区，此时神经元的拟合能力较弱（类似线性神经元）。权值太大or太小时，例如sigmoid，就会出现梯度消失。&lt;/p&gt;

&lt;p&gt;因为我们在初始化网络的时候一般都是初始为较小的权值。训练时间越长，部分网络权值可能越大。如果我们在合适时间停止训练，就可以将网络的能力限制在一定范围内。&lt;/p&gt;

&lt;h4 id=&quot;限制权值正则化&quot;&gt;限制权值/正则化&lt;/h4&gt;

&lt;p&gt;原理同上，但是这类方法直接将权值的大小加入到 Cost 里，在训练的时候限制权值变大。例如L2：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[C=C_0+\frac{\lambda}{2n}\sum _{i}w^2_i\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;训练过程需要降低整体的 Cost，这时候，一方面能降低实际输出与样本之间的误差&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C_0\)&lt;/code&gt;，另一方面也能降低权值大小。&lt;/p&gt;

&lt;h4 id=&quot;增加噪声&quot;&gt;增加噪声&lt;/h4&gt;

&lt;h5 id=&quot;在输入中加噪声&quot;&gt;在输入中加噪声&lt;/h5&gt;

&lt;p&gt;噪声会随着网络传播，按照权值的平方放大，并传播到输出层，对误差 Cost 产生影响。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/overfitting-add-noise-input.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h5 id=&quot;在权值上加噪声&quot;&gt;在权值上加噪声&lt;/h5&gt;

&lt;p&gt;在初始化网络的时候，用0均值的高斯分布作为初始化。Alex Graves 的手写识别 RNN 就是用了这个方法
&lt;a href=&quot;http://people.idsia.ch/~juergen/tpami_2008.pdf&quot;&gt;A novel connectionist system for unconstrained handwriting recognition.&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;对网络的响应加噪声&quot;&gt;对网络的响应加噪声&lt;/h5&gt;

&lt;p&gt;如在前向传播过程中，让默写神经元的输出变为 binary 或 random。这种做法会打乱网络的训练过程，让训练更慢，但据 Hinton 说，在测试集上效果会有显著提升。&lt;/p&gt;

&lt;h3 id=&quot;使用多种模型&quot;&gt;使用多种模型&lt;/h3&gt;

&lt;p&gt;训练多个模型，以每个模型的平均输出作为结果。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/overfitting-multi-models.png&quot; style=&quot;max-height: 350px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;bagging&quot;&gt;bagging&lt;/h4&gt;

&lt;p&gt;简单理解，就是分段函数的概念：用不同的模型拟合不同部分的训练集。以随机森林（Rand Forests）为例，就是训练了一堆互不关联的决策树。但由于训练神经网络本身就需要耗费较多自由，所以一般不单独使用神经网络做Bagging。&lt;/p&gt;

&lt;h4 id=&quot;boosting&quot;&gt;boosting&lt;/h4&gt;

&lt;p&gt;既然训练复杂神经网络比较慢，那我们就可以只使用简单的神经网络（层数、神经元数限制等）。通过训练一系列简单的神经网络，加权平均其输出。&lt;/p&gt;

&lt;h4 id=&quot;dropout&quot;&gt;dropout&lt;/h4&gt;

&lt;p&gt;在训练时，每次随机（如50%概率）忽略隐层的某些节点；这样，我们相当于随机从2^H个模型中采样选择模型。同时，由于每个网络只见过一个训练数据（每次都是随机的新网络），所以其实这是类似 bagging 的做法。&lt;/p&gt;

&lt;p&gt;此外，而不同模型之间权值共享（共同使用这 H 个神经元的连接权值），相当于一种权值正则方法，实际效果比 L2 regularization 更好。&lt;/p&gt;

&lt;h3 id=&quot;贝叶斯方法&quot;&gt;贝叶斯方法&lt;/h3&gt;

&lt;h2 id=&quot;模型参数与训练样本规模的关系&quot;&gt;模型参数与训练样本规模的关系&lt;/h2&gt;

&lt;h3 id=&quot;how-much-training-data-do-you-need&quot;&gt;How much training data do you need&lt;/h3&gt;

&lt;p&gt;10倍规则法——即是要训练出一个性能良好的模型，所需训练样本数量应是模型参数数量的10倍。&lt;/p&gt;

&lt;p&gt;因而，借由10倍规则法，将估量训练样本数量的问题转换为只要知道模型中参数数量就可以训练出一个性能良好的模型问题。基于这一点这引发了一些争论：&lt;/p&gt;

&lt;p&gt;（1）对于线性模型 ，例如逻辑回归模型。基于每个特征，模型都分配了相应的参数，从而使得参数的数量和输入的特征数量相等，然而这里可能存在一些问题：你的特征可能是稀疏的，所以，计数的特征数量并不是直接的。&lt;/p&gt;

&lt;p&gt;译者注：我觉得这句话的意思是，稀疏特征，例如稀疏特征的编码是01001001对于模型的训练能够起到作用的特征是少数的，而不起作用的特征占大多数。依照上述线性规则，若模型对于每个特征分配相应的参数，也就是说对于无用的特征也分配了相应的参数，再根据10倍规则法，获取是模型参数数量10倍的训练样本集，此时的训练样本数量对于最佳的训练模型来说可能是超量的，所以，此时用10倍规则法得到的训练样本集未必能够真实地得出好的训练模型。&lt;/p&gt;

&lt;p&gt;（2）由于规范化和特征选择技术，训练模型中真实输入的特征的数量少于原始特征数量。&lt;/p&gt;

&lt;p&gt;译者注：我觉得这两点即是在解释上述利用10倍规则法来得到性能良好模型的理论是有一定的局限性，这个理论是相对于输入特征是完备且每个特征对于模型的训练都有一定的贡献程度的。但是对于（1）、（2）这两点所说的稀疏特征和特征降维技术的使用导致特征数量减少的情况，利用10倍规则法能够得到性能良好的模型的理论还有待进一步讨论。&lt;/p&gt;

&lt;p&gt;解决上述（1）、（2）问题的一个办法即是：在提取特征时，你不仅要用到有类别标签的数据还要用到不带类别标签的数据来估计特征的数量。例如给定一个文本语料库，在标记数据进行训练之前，你可以通过统计每个单词出现的次数，来生成一个关于单词频率直方图，以此来理解你的特征空间。根据单词频率直方图，你可以去掉长尾词，来获得真实的、主要的特征数量，之后你可以运用10倍规则法来估测在得到性能良好的模型时，你所需要的训练样本数量。&lt;/p&gt;

&lt;p&gt;与像逻辑回归这样的线性模型相比，神经网络模型提出了一组不同的问题。为了得到神经网络中参数的数量你需要：&lt;/p&gt;

&lt;p&gt;（1）如果你的输入特征是稀疏的，计算嵌入层中（我觉得就是隐含层）参数数量。&lt;/p&gt;

&lt;p&gt;（2）计算神经网络模型中的边数。&lt;/p&gt;

&lt;p&gt;根本问题是在神经网络中参数之间的关系不再是线性的。所以基于逻辑回归模型的学习经验总结不再适用于神经网络模型。在像诸如神经网络这样的模型中，你可以将基于10倍规则法获取的训练样本数量作为在模型训练中输入的训练样本量的一个下界。&lt;/p&gt;

&lt;p&gt;译者注：是在神经网络这样非线性模型中，要想获得一个性能良好的训练模型，所需训练数据最少为模型参数的10倍，实际上所需的训练数据应该比这个还多。&lt;/p&gt;

&lt;p&gt;尽管会存在以上的争论，但是我的10倍规则法在大多数问题中都起到了作用。然而，带着对10倍规则法的疑问，你可以在开源工具箱Tensorflow中插入你自己的模型以及对训练样本的数量和模型的训练效果之间的关系作出一个假设，并通过模拟实验来研究模型的训练效果，如果你在运行过程中有了其它见解，欢迎随时分享。&lt;/p&gt;

&lt;p&gt;翻译版：
&lt;a href=&quot;http://www.cnblogs.com/yymn/articles/5024354.html&quot;&gt;http://www.cnblogs.com/yymn/articles/5024354.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原版：
&lt;a href=&quot;https://medium.com/@malay.haldar/how-much-training-data-do-you-need-da8ec091e956&quot;&gt;https://medium.com/@malay.haldar/how-much-training-data-do-you-need-da8ec091e956&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;lstm参数规模计算&quot;&gt;lstm参数规模计算&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/38080035/how-to-calculate-the-number-of-parameters-of-an-lstm-network&quot;&gt;https://datascience.stackexchange.com/questions/10615/number-of-parameters-in-an-lstm-model&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://datascience.stackexchange.com/questions/10615/number-of-parameters-in-an-lstm-model&quot;&gt;https://datascience.stackexchange.com/questions/10615/number-of-parameters-in-an-lstm-model&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;embedding-layer参数规模计算&quot;&gt;embedding layer参数规模计算&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/38189713/what-is-an-embedding-in-keras&quot;&gt;https://stackoverflow.com/questions/38189713/what-is-an-embedding-in-keras&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;其他&quot;&gt;其他&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766995&amp;amp;idx=4&amp;amp;sn=2417570839b7ccb2e630cef36a363e0a&amp;amp;chksm=871abaedb06d33fb74b4aacfbc1825d7525184a94514ee8a8f3c65aff79a152347fedd68b7e7&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1564548270785&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=zAXdHORK5tTx549e9RwAgNcm7bjJrH4ENwbbTYVrAZDqpsE%2Fu1hY63b%2FoRfnZQdM#rd&quot;&gt;谷歌高级研究员Nature发文：避开机器学习三大「坑」&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;不恰当地分割数据&quot;&gt;不恰当地分割数据&lt;/h3&gt;

&lt;h3 id=&quot;隐变量&quot;&gt;隐变量&lt;/h3&gt;

&lt;h3 id=&quot;定错训练目标&quot;&gt;定错训练目标&lt;/h3&gt;

&lt;h2 id=&quot;其他2&quot;&gt;其他2&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/SejwcPlEDjd3DsWtis1fxg&quot;&gt;优化 | 怎么判断一个优化问题是凸优化还是非凸优化？&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;cv的tip&quot;&gt;cv的tip&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/pD5DGRXRKxHFzUXEwII1Sg&quot;&gt;为什么我的 CV 模型不好用？没想到原因竟如此简单……&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;学习率与batchsize&quot;&gt;学习率与batchsize&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/64134994/answer/675171937&quot;&gt;https://www.zhihu.com/question/64134994/answer/675171937&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>图文相关性模型简介</title>
   <link href="http://daiwk.github.io/posts/cv-img-txt-sim.html"/>
   <updated>2017-07-05T00:00:00+00:00</updated>
   <id>/posts/cv-img-txt-sim</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-正负例直接算rank_cost&quot;&gt;1. 正负例直接算rank_cost&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-basic模型&quot;&gt;1.1 basic模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-升级文本表示为bi-lstm&quot;&gt;1.2 升级文本表示为bi-lstm&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-训练数据更新不使用标题&quot;&gt;1.3 训练数据更新（不使用标题）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#14-使用nlpc的wordembedding&quot;&gt;1.4 使用nlpc的wordembedding&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-回归现有模型&quot;&gt;2. 回归现有模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-拟合图搜相关性模型&quot;&gt;2.1 拟合图搜相关性模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-拟合idl模型&quot;&gt;2.2 拟合idl模型&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;1-正负例直接算rank_cost&quot;&gt;1. 正负例直接算rank_cost&lt;/h2&gt;

&lt;p&gt;使用rank_cost(&lt;a href=&quot;http://www.paddlepaddle.org/doc/api/v2/config/layer.html&quot;&gt;paddle_v2的layers&lt;/a&gt;):&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align}\begin{aligned}C_{i,j} &amp;amp; = -\tilde{P_{ij}} * o_{i,j} + log(1 + e^{o_{i,j}})\\o_{i,j} &amp;amp; =  o_i - o_j\\\tilde{P_{i,j}} &amp;amp; = \{0, 0.5, 1\} \ or \ \{0, 1\}\end{aligned}\end{align}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C_{i,j}\)&lt;/code&gt;是cross-entropy cost。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{P_{i,j}}\)&lt;/code&gt;是label。1是正序（左&amp;gt;右），0是逆序。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(o_i\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(o_j\)&lt;/code&gt;是左、右的输出，是1维的。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;weight的引入:参考&lt;a href=&quot;http://machinelearning.org/archive/icml2009/papers/163.pdf&quot;&gt;Ranking with Ordered Weighted Pairwise Classification&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;11-basic模型&quot;&gt;1.1 basic模型&lt;/h3&gt;

&lt;p&gt;文本采用word2vec获取标题向量，cos_sim计算 图文的相关性，然后用pairwise训练&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/img_txt_sim_basic.svg&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;12-升级文本表示为bi-lstm&quot;&gt;1.2 升级文本表示为bi-lstm&lt;/h3&gt;

&lt;p&gt;目前简单抽取1k的图文配对（1k正+1k随机产出的负例），&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;如果正例的相关性&amp;gt;负例的相关性，则暂认为：判定有效&lt;/li&gt;
  &lt;li&gt;模型误判：如果正例判定不相关（&amp;lt;0）或负例判定相关(&amp;gt;0)&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/img_txt_sim_lstm.svg&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;13-训练数据更新不使用标题&quot;&gt;1.3 训练数据更新（不使用标题）&lt;/h3&gt;

&lt;p&gt;之前都用title，现在改成单元内的bidword,pic的pair对。并且，引入rank_cost的weight参数。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/img_txt_sim_bidword.svg&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;14-使用nlpc的wordembedding&quot;&gt;1.4 使用nlpc的wordembedding&lt;/h3&gt;

&lt;p&gt;把lstm改成nlpc的wordembedding。如果文本数据量少（短文本）的话，用这个其实就好了。不用接lstm。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/img_txt_sim_bidword_nlpc_wordembedding.svg&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;2-回归现有模型&quot;&gt;2. 回归现有模型&lt;/h2&gt;

&lt;p&gt;把rank_cost换成regression_cost&lt;/p&gt;

&lt;h3 id=&quot;21-拟合图搜相关性模型&quot;&gt;2.1 拟合图搜相关性模型&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/img_txt_sim_bidword_regression_tusou.svg&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;22-拟合idl模型&quot;&gt;2.2 拟合idl模型&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/img_txt_sim_bidword_regression_idl.svg&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>tensor-to-tensor[理论篇]</title>
   <link href="http://daiwk.github.io/posts/platform-tensor-to-tensor.html"/>
   <updated>2017-06-21T00:00:00+00:00</updated>
   <id>/posts/platform-tensor-to-tensor</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-%E7%AE%80%E4%BB%8B&quot;&gt;1. 简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-%E6%A8%A1%E5%9D%97%E5%8C%96%E5%A4%9A%E4%BB%BB%E5%8A%A1%E8%AE%AD%E7%BB%83&quot;&gt;2. 模块化多任务训练&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-%E5%86%85%E7%BD%AE%E7%9A%84%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5&quot;&gt;3. 内置的最佳实践&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-attention-is-all-you-need&quot;&gt;4. Attention Is All You Need&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-%E8%83%8C%E6%99%AF&quot;&gt;4.1 背景&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-%E6%9E%B6%E6%9E%84&quot;&gt;4.2 架构&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#421-encoder-and-decoder-stacks&quot;&gt;4.2.1 encoder and decoder stacks&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#422-attention&quot;&gt;4.2.2 attention&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#423-position-wise-feed-forward-networks&quot;&gt;4.2.3 position-wise feed-forward networks&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#424-embeddings-and-softmax&quot;&gt;4.2.4 embeddings and softmax&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#425-positional-encoding&quot;&gt;4.2.5 positional encoding&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#426-why-self-attention&quot;&gt;4.2.6 why self-attention&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#427-%E7%94%9F%E6%88%90%E8%BF%87%E7%A8%8B&quot;&gt;4.2.7 生成过程&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#43-%E8%AE%AD%E7%BB%83&quot;&gt;4.3 训练&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#431-%E8%AE%AD%E7%BB%83%E6%95%B0%E6%8D%AE%E5%92%8Cbatching&quot;&gt;4.3.1 训练数据和batching&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#432-hardware--schedule&quot;&gt;4.3.2 hardware &amp;amp; schedule&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#433-optimizer&quot;&gt;4.3.3 optimizer&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#434-regularizatioin&quot;&gt;4.3.4 regularizatioin&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#44-%E7%BB%93%E6%9E%9C&quot;&gt;4.4 结果&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#441-%E6%9C%BA%E5%99%A8%E7%BF%BB%E8%AF%91&quot;&gt;4.4.1 机器翻译&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#442-model-variations&quot;&gt;4.4.2 model variations&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#443-english-constituency-parsing&quot;&gt;4.4.3 English Constituency Parsing&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-one-model-to-learn-them-all&quot;&gt;5. One Model To Learn Them All&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-%E5%AE%9E%E8%B7%B5&quot;&gt;6. 实践&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90&quot;&gt;7. 代码解析&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#71-%E5%8E%9F%E7%89%88tensor2tensor&quot;&gt;7.1 原版（tensor2tensor）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#72-%E5%8D%95%E7%BA%AFtransformer&quot;&gt;7.2 单纯transformer&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#8-%E6%94%B9%E8%BF%9B&quot;&gt;8. 改进&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#9-%E8%87%AA%E5%B7%B1%E7%9A%84%E5%B0%8F%E7%BB%93&quot;&gt;9. 自己的小结&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650728195&amp;amp;idx=1&amp;amp;sn=fee60fcf9d65a7fc1a4724c36518abc5&amp;amp;chksm=871b237db06caa6b4ec51f4bdb26d4f300d1fdb7883d067d16bb028fad5eeda85f9da205cafe&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0620OFv5szDePHE3dhbAsDgH&amp;amp;pass_ticket=02kbaa2bXJP8WM9p0uBjXe%2F%2FwL4NTu7po2dscLQk1ttBb0YAEBNnLOiV07OR7Anm#rd&quot;&gt;一个模型库学习所有：谷歌开源模块化深度学习系统Tensor2Tensor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github: &lt;a href=&quot;https://github.com/tensorflow/tensor2tensor&quot;&gt;https://github.com/tensorflow/tensor2tensor&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-简介&quot;&gt;1. 简介&lt;/h2&gt;

&lt;p&gt;T2T 能够帮助人们为各种机器学习程序创建最先进的模型，可应用于多个领域，如翻译、语法分析、图像信息描述等，大大提高了研究和开发的速度。T2T 中也包含一个数据集和模型库，其中包括谷歌近期发布的几篇论文中提出的最新模型（Attention Is All You Need、Depthwise Separable Convolutions for Neural Machine Translation 和 One Model to Learn Them All）。&lt;/p&gt;

&lt;p&gt;T2T 库中的两个模型：SliceNet 和 Transformer 的表现超过了此前的业界最佳水平 GNMT+MoE。其中，Transformer 的分数超过 GNMT 模型 3.8 分，而 GNMT 已经超过基准水平（基于短语的翻译系统 MOSES）4 分了。值得注意的是，&lt;strong&gt;使用 T2T，你可以用一块 GPU，一天的时间实现此前业界最佳水准的表现：使用小型 Transformer 模型，在单 GPU 训练一天时间后可以达到 24.9 的 BLEU 分数。&lt;/strong&gt;现在，所有人都可以自己构建最好的翻译模型了。&lt;/p&gt;

&lt;h2 id=&quot;2-模块化多任务训练&quot;&gt;2. 模块化多任务训练&lt;/h2&gt;

&lt;p&gt;T2T 库构建于 TensorFlow 之上，定义了深度学习系统所需的各个组件：数据集、模型架构、优化器、学习速率衰减方案、超参数等等。最重要的是，它实现了所有这些组件之间的标准对接形式，并使用了目前最好的机器学习方法。这样，你可以选择任何一组数据集、模型、优化器，然后设定超参数，开始训练，并查看它的性能。通过架构的模块化，&lt;strong&gt;在输入和输出数据之间的每一块都是张量-张量的函数&lt;/strong&gt;。这意味着如果你对模型架构有了新想法，你无须改动整个模型，你可以保留嵌入部分、损失函数和其他所有部分，仅仅用自己的函数替换模型体的一部分，该函数将张量作为输入，并返回一个张量。&lt;/p&gt;

&lt;p&gt;这意味着 T2T 很灵活，训练无需局限在一个特定的模型或数据集上。它如此容易以至于像著名的 LSTM 序列到序列模型这样的架构可通过几行代码被定义。你也可以在不同领域的多任务上训练一个单一模型，你甚至还可以同时在所有的数据集上训练一个单一模型。很高兴我们的 MultiModel（就像这样被训练并包含在 T2T 中）在很多任务上获得了好结果，甚至是在 ImageNet、MS COCO、WSJ、 WM 和 Penn Treebank 解析语料库上进行联合训练也不例外。这是首次单个模型被证明可以同时执行多个任务。&lt;/p&gt;

&lt;h2 id=&quot;3-内置的最佳实践&quot;&gt;3. 内置的最佳实践&lt;/h2&gt;

&lt;p&gt;我们的首次发行也提供了脚本，可生成大量数据集（广泛用于研究社区），一些模型，大量超参数配置，trade 的其他重要技巧的一个执行良好的实现。尽管全部列举它们很难，如果你决定用 T2T 运行你的模型，将会免费获得序列的正确填充（padding），相应的交叉熵损失，调试良好的 Adam 优化器参数，自适应批处理，同步的分布式训练，调试良好的图像数据增强，标签平滑和大量的超参数配置（包括上述在翻译方面取得当前最佳结果的超参数配置）。&lt;/p&gt;

&lt;p&gt;例如，考虑一下把英语语句解析成其语法选区树（grammatical constituency tree）。这个问题被研究了数十年，人们费了很大劲给出了可靠的方法。它可被称为序列到序列问题，可通过神经网络来解决，但它过去常常需要很多调试。借助 T2T，我们只需几天就可以添加解析数据集生成器，并调节我们的注意力转换器模型以训练这一问题。&lt;/p&gt;

&lt;h2 id=&quot;4-attention-is-all-you-need&quot;&gt;4. Attention Is All You Need&lt;/h2&gt;

&lt;p&gt;链接：&lt;a href=&quot;https://arxiv.org/abs/1706.03762&quot;&gt;https://arxiv.org/abs/1706.03762&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要的序列转换模型（dominant sequence transduction model）都是基于复杂的 RNN 或 CNN的encoder-decoder模型。表现最佳的模型也需通过注意力机制（attention mechanism）连接编码器和解码器。&lt;strong&gt;我们提出了一种新型的简单网络架构——Transformer，它完全基于注意力机制，彻底放弃了循环和卷积。&lt;/strong&gt;两项机器翻译任务的实验表明，这些模型的&lt;strong&gt;翻译质量更优，同时更并行，所需训练时间也大大减少。&lt;/strong&gt;我们的模型在 WMT 2014 英语转德语的翻译任务中取得了 BLEU 得分 28.4 的成绩，领先当前现有的最佳结果（包括集成模型）超过 2 个 BLEU 分值。在 WMT 2014 英语转法语翻译任务上，在 8 块 GPU 上训练了 3.5 天之后，我们的模型获得了新的单模型顶级 BLEU 得分 41.0，只是目前文献中最佳模型训练成本的一小部分。我们表明 Transformer 在其他任务上也泛化很好，把它成功应用到了有大量训练数据和有限训练数据的英语组别分析上。&lt;/p&gt;

&lt;h3 id=&quot;41-背景&quot;&gt;4.1 背景&lt;/h3&gt;

&lt;p&gt;rnn需要当前时间步的状态，以及之前时间步的状态，所以这种序列特性天生地（inherently）难以并行，当序列较长时，由于内存有限，这缺陷尤为明显。最近的解决方法主要有两种：factorization tricks&lt;a href=&quot;https://arxiv.org/abs/1703.10722&quot;&gt;《Factorization tricks for LSTM networks》&lt;/a&gt;和conditional computation&lt;a href=&quot;https://arxiv.org/abs/1701.06538&quot;&gt;《Outrageously large neural networks: The sparsely-gated mixture-of-experts layer》&lt;/a&gt;[Conditional computation, where parts of the network are active on a per-example basis, has been proposed in theory as a way of dramatically increasing model capacity without a proportional increase in computation.]。&lt;strong&gt;但序列计算的局限性一直还在。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;注意力机制往往只是与rnn结合使用，可以不需要关心输入输出序列的distance就能对输入输出序列的依赖关系进行建模(allows modeling of dependencies without regard to their distance in the input or output sequences.)。&lt;/p&gt;

&lt;p&gt;本文的Transformer避开了recurrence，只用attention，便可以刻画出输入和输出的依赖关系。由于并行性得到了提升，训练时间可以缩短到8个P100 GPU只要12小时，并且翻译质量有所提升。&lt;/p&gt;

&lt;p&gt;Extended Neural GPU(&lt;a href=&quot;https://arxiv.org/abs/1610.08613&quot;&gt;《Can active memory replace attention?》&lt;/a&gt;)、ByteNet(&lt;a href=&quot;https://arxiv.org/abs/1610.10099&quot;&gt;《Neural machine translation in linear time》&lt;/a&gt;)、ConvS2S（&lt;a href=&quot;https://arxiv.org/abs/1705.03122&quot;&gt;《Convolutional Sequence to Sequence Learning》&lt;/a&gt;）都&lt;strong&gt;使用CNN并行地计算所有输入和输出位置的隐层表示，从而减少序列计算。&lt;/strong&gt;其中，对任意两个输入和输出位置进行计算与他们间的距离有关，ConvS2S是线性时间复杂度，ByteNet是log时间复杂度。而Transformer则是常数时间复杂度(前提是the cost of reduced effective resolution due
to averaging attention-weighted positions, an effect we counteract with Multi-Head Attention)。&lt;/p&gt;

&lt;p&gt;另外，self-attention(即intra-attention)是一种会对一个单独的序列的不同位置进行关联的注意力机制。&lt;/p&gt;

&lt;h3 id=&quot;42-架构&quot;&gt;4.2 架构&lt;/h3&gt;

&lt;p&gt;沿用了encoder-decoder架构，框架用于解决由一个任意长度的源序列到另一个任意长度的目标序列的变换问题。即编码阶段将整个源序列编码成一个向量，解码阶段通过最大化预测序列概率，从中解码出整个目标序列。Transformer同样使用了encoder-decoder，在编码和解码阶段均使用stacked self-attention、point-wise和全连接。&lt;/p&gt;

&lt;h4 id=&quot;421-encoder-and-decoder-stacks&quot;&gt;4.2.1 encoder and decoder stacks&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/transformer-model architecture.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;encoder:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;encoder堆叠了N=6层，每层有两个子层：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;multi-head self-attention&lt;/li&gt;
  &lt;li&gt;position-wise的全连接层&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这两个子层内均是residual connection，再加上layer normalization，即，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(LayerNorm(x+Sublayer(x))\)&lt;/code&gt;（即图中的Add&amp;amp;Norm），Sublayer是子层自己实现的函数。&lt;strong&gt;为了方便这些residual connection，架构中的所有子层（包括embedding）,输出的维度均是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}=512\)&lt;/code&gt;。&lt;/strong&gt;这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;指的是embedding的dim。所以输入比如有n个词，那就是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\times d_{model}\)&lt;/code&gt;的矩阵。&lt;/p&gt;

&lt;p&gt;注：LayerNorm是Batch Normalization的一个变体，BN针对一个minibatch的输入样本，计算均值和方差，基于计算的均值和方差来对某一层神经网络的输入X中每一个case进行归一化操作。但BN有两个明显不足：1、高度依赖于mini-batch的大小，实际使用中会对mini-Batch大小进行约束，不适合类似在线学习（mini-batch为1）情况；2、不适用于RNN网络中normalize操作：BN实际使用时需要计算并且保存某一层神经网络mini-batch的均值和方差等统计信息，对于对一个固定深度的前向神经网络（DNN，CNN）使用BN，很方便；但对于RNN来说，sequence的长度是不一致的，换句话说RNN的深度不是固定的，不同的time-step需要保存不同的statics特征，可能存在一个特殊sequence比其的sequence长很多，这样training时，计算很麻烦。但LN可以有效解决上面这两个问题。LN中同层神经元输入拥有相同的均值和方差，不同的输入样本有不同的均值和方差；而BN中则针对不同神经元输入计算均值和方差，同一个minibatch中的输入拥有相同的均值和方差。因此，LN不依赖于mini-batch的大小和输入sequence的深度，因此可以用于bath-size为1和RNN中对边长的输入sequence的normalize操作。参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIxNDgzNDg3NQ==&amp;amp;mid=2247483765&amp;amp;idx=1&amp;amp;sn=be24746f5e99058a4b9e8e209848a717&amp;amp;chksm=97a0caa1a0d743b7440c224e17fdc8579a88793940470e6d028b34040f7385f2b99d4da17478&amp;amp;scene=21#wechat_redirect&quot;&gt;深度学习加速器Layer Normalization-LN&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;decoder:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;decoder同样堆叠了N=6层。&lt;strong&gt;在encoder的两个子层的基础上，decoder加了一个子层，即，对encoder的输出增加了一个masked multi-head attention层【通过添加mask，这个子层不要encoder的输出作为输入，只要output的embedding作为输入】。&lt;/strong&gt;连接一样是Add &amp;amp; Norm。另外，&lt;strong&gt;还对self-attention子层进行了修改，prevent positions from attending to subsequent positions.&lt;/strong&gt; &lt;strong&gt;由于输入的output embedding有一个position的offset，所以结合masking，可以保证对位置i的预测，只依赖于位置小于i的位置的输出。&lt;/strong&gt;====&amp;gt;让decoder只看到左边的词，不作弊！&lt;/p&gt;

&lt;h4 id=&quot;422-attention&quot;&gt;4.2.2 attention&lt;/h4&gt;

&lt;p&gt;attention函数：将一个query和一系列的(key, value)对映射起来。其中，&lt;strong&gt;query、key、value均是向量，维度分别为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_v\)&lt;/code&gt;。&lt;/strong&gt;最终的输出是各value的加权组合（v的权重w其实就是下面公式里V的系数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(softmax(\frac{QK^T}{\sqrt{d_k}})\)&lt;/code&gt;）：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q-&amp;gt;[(k_1,v_1), (k_2,v_2), ...]-&amp;gt;attention=w_1*v_1+w_2*v_2+...
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/transformer architecture-attention-noted.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;其中权重是通过如下方法获取的：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;scaled dot-product attention&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;query和所有key算点积，然后除以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sqrt{d_k}\)&lt;/code&gt;，然后算softmax，得到的就是value的weight。实际应用中，一系列的query，记为矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;，同样地，有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Attention(Q,K,V)=softmax(\frac{QK^T}{\sqrt{d_k}})V
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/scaled dot-product attention and multi-head attention.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;有两种常用的attention：addictive attention和dot-product attention。dot-product attention和scaled dot-product attention的唯一区别就是，没有除以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sqrt{d_k}\)&lt;/code&gt;。addictive attention使用一个只有一个隐层的前馈神经网络计算weight。计算复杂度上二者相似，但由于有高度优化的矩阵乘法实现，所以dot-product的速度和空间利用会更好。&lt;/p&gt;

&lt;p&gt;当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;不大时，additive attention比没用scale的dot-product attention表现更好。但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;很大时，点积会变得特别大，所以softmax会出现有些区域的梯度变得特别小（&lt;strong&gt;例如，假设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;是独立的随机变量，均值都是0，方差都是1，那么他们的点积&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(qk=\sum^{d_k}_{i=1}q_ik_i\)&lt;/code&gt;的均值是0，方差是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k*1^2=d_k\)&lt;/code&gt;。【详见下面的两个随机变量的和与积的方差推导】&lt;/strong&gt;），因此，需要通过除以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sqrt{d_k}\)&lt;/code&gt;来scale。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\设 X 与 Y 是两个随机变量，则方差
\\V(X+Y)= V(X)+V(Y)+2Cov(X,Y)
\\V(X -Y)= V(X)+V(Y)-2Cov(X,Y)
\\特别的，当X，Y是两个不相关的随机变量则
\\V(X+Y)=V(X)+V(Y),V(X-Y)=V(X)+V(Y)
\]&lt;/code&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/xy's variance.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;multi-head attention&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;实际应用中，并不是直接计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维【前面提到了，架构中的所有子层（包括embedding）,输出的维度均是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}=512\)&lt;/code&gt;】的query，key，value。而是把这&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维拆成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;份，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k=d_v=d_{model}/h\)&lt;/code&gt;,(因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;是一样的，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;也是一样的【本文设为8】，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k=d_v\)&lt;/code&gt;)。而这个变换通过对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q,K,W\)&lt;/code&gt;各自进行一个线性变换，变成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_k,d_k,d_v\)&lt;/code&gt;维即可，最终通过concat(把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;个结果首尾相连),然后再做一个线性变换，变成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\MultiHead(Q,K,V)=Concat(head_1,...,head_h)W^O
\\where\ head_i=Attention(QW_i^Q,KW_i^K,VW_i^V)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_i^Q\in \mathbb{R}^{d_{model}\times d_k}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_i^K\in \mathbb{R}^{d_{model}\times d_k}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_i^V\in \mathbb{R}^{d_{model}\times d_v}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W^O\in \mathbb{R}^{hd_v\times d_{model}}\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外，attention在本模型中的应用&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;encoder-decoder结构中，对当前层的decoder而言，query来自前一层的decoder，key和value来自encoder的输出。&lt;/li&gt;
  &lt;li&gt;encoder有self-attention层。在self-attention中，所有层的key，value和query都来自于前一层encoder的输出。因此，当前层的encoder的每个位置可以attend to前一层的所有位置。&lt;/li&gt;
  &lt;li&gt;类似的，当前层的encoder的每个位置（例如位置i）可以attend to前一层的所有位置（包括位置i）。但为了保持auto-regressive特性，需要阻止leftword infomation（左边，即encoder的输出） 流入decoder，所以在scaled dot-product attention里使用mask把所有输入给softmax的都mask掉（&lt;strong&gt;图figure2中的mask(opt)&lt;/strong&gt;）。相当于不要Q(上一层decoder的输出)，也不要K，V（encoder的输出，所以图里，encoder的输出在mask这层被干掉了，所以没画这条线）&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;423-position-wise-feed-forward-networks&quot;&gt;4.2.3 position-wise feed-forward networks&lt;/h4&gt;

&lt;p&gt;其实每个encoder和decoder层，除了attention子层之外，还有一个全连接的前馈网络。这个前馈网络会作用于每一个position。这个前馈网络包括了两种线性变换：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
FFN(x)=ReLU(xW_1+b_1)W_2+b_2=max(0, xW_1+b_1)W_2+b_2
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;不同位置的线性变换是一样的，不同层的变换是不同的。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;另一种理解方法其实就是，2个卷积层（kernel size=1）。本文中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}=512\)&lt;/code&gt;，inner-layer的维度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{ff}=2048\)&lt;/code&gt;。其实，这种position-wise的前馈网络实际上可以看作是另一种attention（附录里有说明。。。。）&lt;/p&gt;

&lt;h4 id=&quot;424-embeddings-and-softmax&quot;&gt;4.2.4 embeddings and softmax&lt;/h4&gt;

&lt;p&gt;与其他序列转换模型类似，本文也使用learned embeddings对input tokens和output tokens转换为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维的向量（架构图中的 input embedding和output embedding）。&lt;/p&gt;

&lt;p&gt;同时也使用learned linear变换和softmax将decoder的输出转换为预测的下一个token的概率。&lt;/p&gt;

&lt;p&gt;本文中，与&lt;a href=&quot;https://arxiv.org/pdf/1608.05859.pdf&quot;&gt;Using the output embedding to improve language models&lt;/a&gt;类似【看一看！！！。。。】，两个embedding层与pre-softmax transformation均共享同一个参数矩阵。&lt;/p&gt;

&lt;h4 id=&quot;425-positional-encoding&quot;&gt;4.2.5 positional encoding&lt;/h4&gt;

&lt;p&gt;鉴于本模型没有cnn也没有rnn，为了引入序列的位置信息，需要加入”position encoding”。在encoder和decoder的底部，也就是对应的embedding之后，加入了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维的position encoding，与embedding的维数一致，便于对二者求和。&lt;/p&gt;

&lt;p&gt;本文使用的position encoding如下:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\PE_{(pos,2i)}=sin(pos/10000^{2i/d_{model}})
\\PE_{(pos,2i+1)}=cos(pos/10000^{2i/d_{model}})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;pos是位置，i是维度。因为对于任意固定的offset k，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(PE_{pos+k}\)&lt;/code&gt;能被表示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(PE_{pos}\)&lt;/code&gt;的线性函数。即把id为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(pos\)&lt;/code&gt;的位置，映射成一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维的向量，这个向量的第i个元素的数值是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(PE_{(pos,i)}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://kexue.fm/archives/4765#Position%20Embedding&quot;&gt;https://kexue.fm/archives/4765#Position%20Embedding&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;将每个位置编号，然后&lt;strong&gt;每个编号对应一个向量&lt;/strong&gt;，通过结合位置向量和词向量，就给每个词都引入了一定的位置信息，这样Attention就可以分辨出不同位置的词了。&lt;/p&gt;

&lt;p&gt;与以往的position embedding的区别如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;以前在RNN、CNN模型中其实都出现过Position Embedding，但在那些模型中，Position Embedding是&lt;strong&gt;锦上添花&lt;/strong&gt;的辅助手段，也就是“有它会更好、没它也就差一点点”的情况，因为&lt;strong&gt;RNN、CNN本身就能捕捉到位置信息&lt;/strong&gt;。但是在这个纯Attention模型中，&lt;strong&gt;Position Embedding是位置信息的唯一来源&lt;/strong&gt;，因此它是模型的核心成分之一，并非仅仅是简单的辅助手段。&lt;/li&gt;
  &lt;li&gt;在以往的Position Embedding中，基本都是根据任务训练出来的向量。而Google&lt;strong&gt;直接给出了一个构造Position Embedding的公式&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;Position Embedding本身是一个&lt;strong&gt;绝对位置&lt;/strong&gt;的信息，但在语言中，&lt;strong&gt;相对位置&lt;/strong&gt;也很重要，Google选择前述的位置向量公式的一个重要原因是：有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sin(\alpha+\beta)=\sin\alpha\cos\beta+\cos\alpha\sin\beta\)&lt;/code&gt;，也有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\cos(\alpha+\beta)=\cos\alpha\cos\beta-\sin\alpha\sin\beta\)&lt;/code&gt;，所以，位置p+k的向量可以表示成位置p的向量的线性变换，这提供了表达相对位置信息的可能性。例如，假设p是奇数(它的pe是cosp)，k是偶数(它的pe是sink)，那p+k是奇数，所以他的pe是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(cos(p+k)=cosp\cdot cosk-sinp\cdot sink=C_1\cdot cosp-C_2\cdot sink\)&lt;/code&gt;，是p和k的pe的各自的线性变换。类似的p是奇数，k是奇数，p+k是偶数,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sin(p+k)=sinp\cdot cosk+cosp\cdot sink=C_1\cdot cosk+C_2\cdot cosp\)&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;结合位置向量和词向量有几个可选方案，可以把它们拼接起来作为一个新向量，也可以&lt;strong&gt;把位置向量定义为跟词向量一样大小，然后两者加起来。&lt;/strong&gt;FaceBook的论文和Google论文中用的都是后者。&lt;/p&gt;

&lt;h4 id=&quot;426-why-self-attention&quot;&gt;4.2.6 why self-attention&lt;/h4&gt;

&lt;p&gt;对比self-attention与CNN、RNN：&lt;/p&gt;

&lt;p&gt;对于同一个任务(正常的编码器/解码器中的一层)：将一个序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((x_1,...,x_n)\)&lt;/code&gt;映射成另一个序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((z_1,...,z_n)\)&lt;/code&gt;，其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i,z_i\in \mathbb{R}^d\)&lt;/code&gt;，也就是说，表示维度（&lt;strong&gt;就是词典的size，例如，字粒度、切词粒度之类的，就是对应的词典大小&lt;/strong&gt;）是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;。从以下3个方面考虑：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;每一层的总的&lt;strong&gt;计算复杂度&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;能被&lt;strong&gt;并行化&lt;/strong&gt;的计算量（可以用”&lt;strong&gt;所需要的最小的序列操作&lt;/strong&gt;“来衡量）&lt;/li&gt;
  &lt;li&gt;网络中&lt;strong&gt;长距离依赖&lt;/strong&gt;的路径长度&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;影响对长距离依赖的学习能力的一个重要影响因素就是，&lt;strong&gt;信号在网络中需要遍历的前向和后向的路径的长度&lt;/strong&gt;(the length of the paths forward and backward signals have to traverse in the network.)。输入和输出序列各position的任意组合间的路径越短，就越容易学习长距离依赖。因此，这里考虑&lt;strong&gt;输入和输出的任意两个position间的最长路径长度&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先，对self-attention与recurrrent进行对比：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;self-attention需要的序列操作是常数级别的，而recurrent需要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(n)\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;当序列长度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;比表示维度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;小的时候，self-attention比recurrent要快（这种情形正好是机器翻译中使用的最常见的句子表示，例如word-piece表示【&lt;a href=&quot;https://arxiv.org/pdf/1609.08144.pdf&quot;&gt;Google’s neural machine translation system: Bridging the gap between human and machine translation&lt;/a&gt;】或者byte-pair表示【&lt;a href=&quot;http://www.aclweb.org/anthology/P16-1162&quot;&gt;Neural machine translation of rare words with subword units&lt;/a&gt;，在&lt;a href=&quot;https://daiwk.github.io/posts/nlp-nmt.html#71-backbone%E6%A8%A1%E5%9E%8B&quot;&gt;https://daiwk.github.io/posts/nlp-nmt.html#71-backbone%E6%A8%A1%E5%9E%8B&lt;/a&gt; 也有提及】）。&lt;/p&gt;

&lt;p&gt;为了改进超长序列的计算性能，self-attention可以限制为只考虑以输入序列为中心的输出序列的大小为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;的邻居的positions。这样，可以把最大路径长度只会提升到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(n/r)\)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;然后，看convolutional&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;单一的卷积层（kernel size为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k&amp;lt;n\)&lt;/code&gt;）并没法连接所有输入和输出position的pair对，所以有两种方法可以解决：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;就contiguous kernels而言，可以将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(n/k)\)&lt;/code&gt;的卷积层stack起来(没太懂。。。)&lt;/li&gt;
  &lt;li&gt;就dilated convolutions(空洞卷积，参考&lt;a href=&quot;https://www.zhihu.com/question/54149221&quot;&gt;https://www.zhihu.com/question/54149221&lt;/a&gt;，原文是&lt;a href=&quot;https://arxiv.org/pdf/1610.10099.pdf&quot;&gt;Neural machine translation in linear time&lt;/a&gt;，参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/23795111&quot;&gt;https://zhuanlan.zhihu.com/p/23795111&lt;/a&gt;)而言，增加网络中任意两个position的最长路径的长度到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(log_k(n))\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;总的来说，因为有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;这个因素，convolutional比recurrent要更昂贵。Separable convolutions【&lt;a href=&quot;https://arxiv.org/pdf/1610.02357.pdf&quot;&gt; Xception: Deep learning with depthwise separable convolutions&lt;/a&gt;，以及&lt;a href=&quot;https://blog.csdn.net/u014380165/article/details/75142710&quot;&gt;https://blog.csdn.net/u014380165/article/details/75142710&lt;/a&gt;】能将复杂度降低到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(O(k\times n\times d+n\times d^2)\)&lt;/code&gt;，但即使&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k=n\)&lt;/code&gt;，这样的网络结构的复杂度也相当于一个self-attention层加上一个point-wise feed-forward层。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;self-attention还有一个好处，产出的模型更加可解释（interpretable）。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;不仅是不同head的attention学到的task不同，而且从下图的visualization来看，甚至可以理解为，有的head学习某种句法/语法关系，有的学习另一种句法/语法关系。。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/transformer-attention-visualization.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;如下表，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;是序列长度，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;是表示维度，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;是cnn的kernel size，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;是restricted self-attention的neighborhood size。&lt;/p&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Layer Type&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Complexity per Layer&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Sequential Operations&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;Maximum Path Length&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;Self-Attention&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(n^2\times d)\)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(1)\)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(1)\)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;Recurrent&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(n\times d^2)\)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(n)\)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(n)\)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;Convolutional&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(k\times n\times d^2)\)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(1)\)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(log_k(n))\)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;Self-Attention(restricted)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(r\times n\times d)\)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(1)\)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;\(O(n/r)\)&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;h4 id=&quot;427-生成过程&quot;&gt;4.2.7 生成过程&lt;/h4&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/27464080&quot;&gt;从《Convolutional Sequence to Sequence Learning》到《Attention Is All You Need》&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tensor2tensor-generation.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;43-训练&quot;&gt;4.3 训练&lt;/h3&gt;

&lt;h4 id=&quot;431-训练数据和batching&quot;&gt;4.3.1 训练数据和batching&lt;/h4&gt;

&lt;h4 id=&quot;432-hardware--schedule&quot;&gt;4.3.2 hardware &amp;amp; schedule&lt;/h4&gt;

&lt;h4 id=&quot;433-optimizer&quot;&gt;4.3.3 optimizer&lt;/h4&gt;

&lt;h4 id=&quot;434-regularizatioin&quot;&gt;4.3.4 regularizatioin&lt;/h4&gt;

&lt;h3 id=&quot;44-结果&quot;&gt;4.4 结果&lt;/h3&gt;

&lt;h4 id=&quot;441-机器翻译&quot;&gt;4.4.1 机器翻译&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tensor2tensor-bleu.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;442-model-variations&quot;&gt;4.4.2 model variations&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tensor2tensor-variants.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;443-english-constituency-parsing&quot;&gt;4.4.3 English Constituency Parsing&lt;/h4&gt;

&lt;p&gt;Constituency: 选区；选区的选民；支持者；主顾&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tensor2tensor-english-cons-parsing.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h2 id=&quot;5-one-model-to-learn-them-all&quot;&gt;5. One Model To Learn Them All&lt;/h2&gt;

&lt;p&gt;单一模型同时在 ImageNet、多个翻译任务、image caption（COCO 数据集）、一个语音识别语料库和一个英文解析任务中获得训练。该模型架构整合了多个领域的组件。它包含卷基层、注意力机制和 sparsely-gated 层，其中的每个组件对于特定任务都是非常重要的，我们观察到添加这些组件并不会影响模型性能——在大多数情况下，它反而会改善任务中的表现。我们还展示了&lt;strong&gt;多个任务联合训练会让仅含少量数据的任务收益颇丰，而大型任务的表现仅有少量性能衰减。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;链接：&lt;a href=&quot;https://arxiv.org/abs/1706.05137&quot;&gt;https://arxiv.org/abs/1706.05137&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;6-实践&quot;&gt;6. 实践&lt;/h2&gt;

&lt;p&gt;安装cuda 8.0，（现在的版本不强制啦）&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;安装cuda 8.0&lt;/li&gt;
  &lt;li&gt;安装cuda 8.0所需的对应cudnn版本（t2t要求cudnn5.0 for cuda 8.0）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;安装tensor2tensor&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Assumes tensorflow or tensorflow-gpu installed
pip install tensor2tensor

# Installs with tensorflow-gpu requirement
pip install tensor2tensor[tensorflow_gpu]

# Installs with tensorflow (cpu) requirement
pip install tensor2tensor[tensorflow]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;就有了一个bin，名为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t2t-trainer&lt;/code&gt;(其实是个python)。。然后执行&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;t2t-trainer &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--generate_data&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;~/t2t_data &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--output_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;~/t2t_train/mnist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--problem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;image_mnist &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;shake_shake &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--hparams_set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;shake_shake_quick &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--train_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--eval_steps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;100
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;7-代码解析&quot;&gt;7. 代码解析&lt;/h2&gt;

&lt;h3 id=&quot;71-原版tensor2tensor&quot;&gt;7.1 原版（tensor2tensor）&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor&quot;&gt;https://github.com/tensorflow/tensor2tensor&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;72-单纯transformer&quot;&gt;7.2 单纯transformer&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/Kyubyong/transformer&quot;&gt;https://github.com/Kyubyong/transformer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码解析：
&lt;a href=&quot;https://blog.csdn.net/mijiaoxiaosan/article/details/74909076&quot;&gt;https://blog.csdn.net/mijiaoxiaosan/article/details/74909076&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;8-改进&quot;&gt;8. 改进&lt;/h2&gt;

&lt;p&gt;阿里的文章&lt;a href=&quot;https://mp.weixin.qq.com/s/lgGDTCF3qg84njv2IeHC9A&quot;&gt;专访 | 大规模集成Transformer模型，阿里达摩院如何打造WMT 2018机器翻译获胜系统&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;阿里机器翻译团队在 WMT 2018 竞赛上主要采用的还是 Transformer 模型，但是会根据最近的一些最新研究对标准 Transformer 模型进行一些修正。这些修正有两方面：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;将 Transformer 中的 Multi-Head Attention 替换为&lt;strong&gt;多个自注意力分支&lt;/strong&gt;，而模型会在训练阶段中将学习结合这些分支注意力模块。&lt;/li&gt;
  &lt;li&gt;采用了一种&lt;strong&gt;编码相对位置的表征&lt;/strong&gt;以扩展自注意力机制，并令模型能更好地理解&lt;strong&gt;序列元素间的相对距离&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;9-自己的小结&quot;&gt;9. 自己的小结&lt;/h2&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/transformer-my-understanding.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>tensor-to-tensor[实践篇]</title>
   <link href="http://daiwk.github.io/posts/platform-tensor-to-tensor-coding.html"/>
   <updated>2017-06-21T00:00:00+00:00</updated>
   <id>/posts/platform-tensor-to-tensor-coding</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#2-paddle%e7%9a%84%e5%ae%9e%e7%8e%b0&quot;&gt;-2. paddle的实现&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-pytorch%e7%9a%84%e5%ae%9e%e7%8e%b0&quot;&gt;-1. pytorch的实现&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#0-suggested-datasets-and-models&quot;&gt;0. Suggested Datasets and Models&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#image-classification&quot;&gt;Image Classification&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#language-modeling&quot;&gt;Language Modeling&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sentiment-analysis&quot;&gt;Sentiment Analysis&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#speech-recognition&quot;&gt;Speech Recognition&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#summarization&quot;&gt;Summarization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#translation&quot;&gt;Translation&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-overview&quot;&gt;1. overview&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#datasets&quot;&gt;Datasets&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#problems-and-modalities&quot;&gt;Problems and Modalities&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#models&quot;&gt;Models&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1706.05137&quot;&gt;One model to learn them all&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;知乎专栏的讨论：&lt;a href=&quot;https://zhuanlan.zhihu.com/p/28680474&quot;&gt;https://zhuanlan.zhihu.com/p/28680474&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor&quot;&gt;https://github.com/tensorflow/tensor2tensor&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;-2-paddle的实现&quot;&gt;-2. paddle的实现&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/trainer_config_helpers/networks.py#L1498&quot;&gt;https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/trainer_config_helpers/networks.py#L1498&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;-1-pytorch的实现&quot;&gt;-1. pytorch的实现&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://nlp.seas.harvard.edu/2018/04/03/attention.html&quot;&gt;http://nlp.seas.harvard.edu/2018/04/03/attention.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码解析见：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/nlp-bert.html#pytorch%E7%89%88%E6%9C%AC&quot;&gt;https://daiwk.github.io/posts/nlp-bert.html#pytorch%E7%89%88%E6%9C%AC&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;0-suggested-datasets-and-models&quot;&gt;0. Suggested Datasets and Models&lt;/h2&gt;

&lt;p&gt;建议的超参是在Cloud TPUs或者8-GPU machines上训练好的&lt;/p&gt;

&lt;h3 id=&quot;image-classification&quot;&gt;Image Classification&lt;/h3&gt;

&lt;p&gt;数据集方面，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ImageNet (a large data-set):可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=image_imagenet&lt;/code&gt;，或者是缩小版的数据集&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image_imagenet224, image_imagenet64, image_imagenet32&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;CIFAR-10: 可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=image_cifar10&lt;/code&gt;，或者是关闭了data augmentation的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=image_cifar10_plain&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;CIFAR-100: 可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=image_cifar100&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;MNIST: 可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=image_mnist&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;模型方面，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ImageNet: 建议使用ResNet或者Xception
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--model=resnet --hparams_set=resnet_50&lt;/code&gt;，resnet的top-1 accuracy能达到76%以上。&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--model=xception --hparams_set=xception_base&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;CIFAR and MNIST：建议使用shake-shake模型，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--model=shake_shake --hparams_set=shakeshake_big&lt;/code&gt;。当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--train_steps=700000&lt;/code&gt;的时候，在CIFAR-10上，可以达到97% accuracy。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;language-modeling&quot;&gt;Language Modeling&lt;/h3&gt;

&lt;p&gt;数据集方面，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;PTB (a small data-set):
    &lt;ul&gt;
      &lt;li&gt;word-level的模型：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=languagemodel_ptb10k&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;character-level的模型：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=languagemodel_ptb_characters&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;LM1B (a billion-word corpus):
    &lt;ul&gt;
      &lt;li&gt;subword-level的模型：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=languagemodel_lm1b32k&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;character-level的模型：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=languagemodel_lm1b_characters&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;模型方面，建议直接上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--model=transformer&lt;/code&gt;，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;PTB：超参设置为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--hparams_set=transformer_small&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;LM1B：超参设置为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--hparams_set=transformer_base&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;sentiment-analysis&quot;&gt;Sentiment Analysis&lt;/h3&gt;

&lt;p&gt;IMDB数据集：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=sentiment_imdb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;建议使用模型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--model=transformer_encoder&lt;/code&gt;，由于这个数据集很小，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--hparams_set=transformer_tiny&lt;/code&gt;，以及比较少的训练步数就行了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--train_steps=2000&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;speech-recognition&quot;&gt;Speech Recognition&lt;/h3&gt;

&lt;p&gt;数据集：Librispeech (English speech to text)&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;完整的数据集：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=librispeech&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;清洗过的较小数据集：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=librispeech_clean&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;summarization&quot;&gt;Summarization&lt;/h3&gt;

&lt;p&gt;将CNN/DailyMail的文章摘要成一些句子的数据集：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=summarize_cnn_dailymail32k&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;模型使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--model=transformer&lt;/code&gt;，超参使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--hparams_set=transformer_prepend&lt;/code&gt;，这样可以得到不错的ROUGE scores。&lt;/p&gt;

&lt;h3 id=&quot;translation&quot;&gt;Translation&lt;/h3&gt;

&lt;p&gt;数据集：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;English-German: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=translate_ende_wmt32k&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;English-French: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=translate_enfr_wmt32k&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;English-Czech: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=translate_encs_wmt32k&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;English-Chinese: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=translate_enzh_wmt32k&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;English-Vietnamese: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=translate_envi_iwslt32k&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果要将源语言和目标语言调换，那么可以直接加一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_rev&lt;/code&gt;，也就是German-English即为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--problem=translate_ende_wmt32k_rev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;翻译问题，建议使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--model=transformer&lt;/code&gt;，在8 GPUs上训练300K steps之后，在English-German上可以达到28的BLEU。如果在单GPU上，建议使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--hparams_set=transformer_base_single_gpu&lt;/code&gt;，在大数据集上（例如English-French），想要达到很好的效果，使用大模型&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--hparams_set=transformer_big&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;针对机器翻译问题的一个基本流程：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# See what problems, models, and hyperparameter sets are available.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# You can easily swap between them (and add new ones).&lt;/span&gt;
t2t-trainer &lt;span class=&quot;nt&quot;&gt;--registry_help&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 1. 设置模型参数&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;PROBLEM&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;translate_ende_wmt32k
&lt;span class=&quot;nv&quot;&gt;MODEL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;transformer
&lt;span class=&quot;nv&quot;&gt;HPARAMS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;transformer_base_single_gpu

&lt;span class=&quot;nv&quot;&gt;DATA_DIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;/t2t_data
&lt;span class=&quot;nv&quot;&gt;TMP_DIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/t2t_datagen
&lt;span class=&quot;nv&quot;&gt;TRAIN_DIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;/t2t_train/&lt;span class=&quot;nv&quot;&gt;$PROBLEM&lt;/span&gt;/&lt;span class=&quot;nv&quot;&gt;$MODEL&lt;/span&gt;-&lt;span class=&quot;nv&quot;&gt;$HPARAMS&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$TMP_DIR&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$TRAIN_DIR&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 2. 生成数据&lt;/span&gt;
t2t-datagen &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--tmp_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$TMP_DIR&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--problem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$PROBLEM&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 3. 训练&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# *  If you run out of memory, add --hparams='batch_size=1024'.&lt;/span&gt;
t2t-trainer &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--problem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$PROBLEM&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$MODEL&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--hparams_set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HPARAMS&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--output_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$TRAIN_DIR&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 4. Decode&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;DECODE_FILE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt;/decode_this.txt
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello world&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$DECODE_FILE&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Goodbye world&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$DECODE_FILE&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Hallo Welt\nAuf Wiedersehen Welt'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ref-translation.de

&lt;span class=&quot;nv&quot;&gt;BEAM_SIZE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4
&lt;span class=&quot;nv&quot;&gt;ALPHA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.6

t2t-decoder &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--data_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DATA_DIR&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--problem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$PROBLEM&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$MODEL&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--hparams_set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HPARAMS&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--output_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$TRAIN_DIR&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--decode_hparams&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;beam_size=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$BEAM_SIZE&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;,alpha=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ALPHA&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--decode_from_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$DECODE_FILE&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--decode_to_file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;translation.en

&lt;span class=&quot;c&quot;&gt;# 5. 查看翻译结果&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;translation.en

&lt;span class=&quot;c&quot;&gt;# 6. Evaluate the BLEU score&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Note: Report this BLEU score in papers, not the internal approx_bleu metric.&lt;/span&gt;
t2t-bleu &lt;span class=&quot;nt&quot;&gt;--translation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;translation.en &lt;span class=&quot;nt&quot;&gt;--reference&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;ref-translation.de
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;1-overview&quot;&gt;1. overview&lt;/h2&gt;

&lt;h3 id=&quot;datasets&quot;&gt;Datasets&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;数据集都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow.Example&lt;/code&gt;的protobuf标准化处理过的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TFRecord&lt;/code&gt;文件。&lt;/li&gt;
  &lt;li&gt;所有数据集通&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/bin/t2t-datagen&quot;&gt;t2t-datagen&lt;/a&gt;进行注册与生成。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;problems-and-modalities&quot;&gt;Problems and Modalities&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Problems，指的是训练时针对task和dataset的hyperparameters，主要设置的是输入和输出的modalities（例如，symbol, image, audio, label）以及vocabularies。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所有的problem都在&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/data_generators/problem_hparams.py&quot;&gt;problem_hparams.py&lt;/a&gt;中定义了，或者通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@registry.register_problem&lt;/code&gt;进行注册。&lt;/p&gt;

&lt;p&gt;直接运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t2t-datagen&lt;/code&gt;可以查看目前支持的problems列表。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Modalities(形态)，将input和output的数据类型abstract away，从而使得model可以直接处理modality-independent tensors。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所有的modalities定义在&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/modality.py&quot;&gt;modality.py&lt;/a&gt;中。&lt;/p&gt;

&lt;h3 id=&quot;models&quot;&gt;Models&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T2TModel&lt;/code&gt;定义了核心的tensor-to-tensor变换，与input/output的modality或者task无关。模型的输入是dense tensors，输出是dense tensors，可以在final step中被一个modality进行变换(例如通过一直final linear transform，产出logits供softmax over classes使用)。&lt;/p&gt;

&lt;p&gt;在models目录下的&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/models/__init__.py&quot;&gt;&lt;strong&gt;init&lt;/strong&gt;.py&lt;/a&gt;中import了所有model。&lt;/p&gt;

&lt;p&gt;这些模型的基类是&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/t2t_model.py&quot;&gt;T2TModel&lt;/a&gt;，都是通过&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/registry.py&quot;&gt;@registry.register_model&lt;/a&gt;来进行注册的。&lt;/p&gt;

&lt;h3 id=&quot;hyperparameter-sets&quot;&gt;Hyperparameter Sets&lt;/h3&gt;

&lt;p&gt;超参数集合通过&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/registry.py&quot;&gt;@registry.register_hparams&lt;/a&gt;进行注册，并通过&lt;a href=&quot;https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/training/python/training/hparam.py&quot;&gt;tf.contrib.training.HParams&lt;/a&gt;编码成对象。&lt;/p&gt;

&lt;p&gt;HParams对model和problem都是可用的。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/layers/common_hparams.py&quot;&gt;common_hparams.py&lt;/a&gt;中定义了基本的超参集，而且超参集的函数可以组成其他超参集的函数。&lt;/p&gt;

&lt;h3 id=&quot;trainer&quot;&gt;Trainer&lt;/h3&gt;

&lt;p&gt;支持分布式训练，参考&lt;a href=&quot;https://tensorflow.github.io/tensor2tensor/distributed_training.html&quot;&gt;https://tensorflow.github.io/tensor2tensor/distributed_training.html&lt;/a&gt;，包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;多GPU&lt;/li&gt;
  &lt;li&gt;synchronous (1 master, many workers)&lt;/li&gt;
  &lt;li&gt;asynchronous (independent workers synchronizing through a parameter server)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-新增components&quot;&gt;2. 新增components&lt;/h2&gt;

&lt;h2 id=&quot;3-新增数据集&quot;&gt;3. 新增数据集&lt;/h2&gt;

&lt;h2 id=&quot;4-可视化&quot;&gt;4. 可视化&lt;/h2&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor/insights&quot;&gt;https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor/insights&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;首先，安装nodejs的npm：&lt;a href=&quot;https://nodejs.org/en/&quot;&gt;https://nodejs.org/en/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;然后用npm安装Bower：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; bower
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后用bower对本项目的insights部分进行安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;pushd &lt;/span&gt;tensor2tensor/insights/polymer
bower &lt;span class=&quot;nb&quot;&gt;install
popd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;还需要&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;oauth2client
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后写一个json，表示模型的各种配置：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;configuration&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;source_language&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;en&quot;&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;target_language&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;de&quot;&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;label&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;transformers_wmt32k&quot;&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;transformer&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&quot;model&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;transformer&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;model_dir&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;/tmp/t2t/train&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;data_dir&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;/tmp/t2t/data&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;hparams&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;hparams_set&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;transformer_base_single_gpu&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;problem&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;translate_ende_wmt32k&quot;&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
    &lt;span class=&quot;o&quot;&gt;}]&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;language&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;code&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;en&quot;&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;name&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;English&quot;&lt;/span&gt;,
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;code&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;de&quot;&lt;/span&gt;,
      &lt;span class=&quot;s2&quot;&gt;&quot;name&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;German&quot;&lt;/span&gt;,
    &lt;span class=&quot;o&quot;&gt;}]&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后启动：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;t2t-insights-server &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;--configuration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;configuration.json &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
          &lt;span class=&quot;nt&quot;&gt;--static_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;/tensor2tensor/insights/polymer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;x-备注&quot;&gt;x. 备注&lt;/h2&gt;

&lt;p&gt;当显存不是很够用的时候，可以参考&lt;a href=&quot;https://stackoverflow.com/questions/39465503/cuda-error-out-of-memory-in-tensorflow&quot;&gt;https://stackoverflow.com/questions/39465503/cuda-error-out-of-memory-in-tensorflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;可以试着把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib/python3.6/site-packages/tensor2tensor/utils/trainer_lib.py&lt;/code&gt;里的&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;gpu_options&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;per_process_gpu_memory_fraction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gpu_mem_fraction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;改成&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;gpu_options&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUOptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;per_process_gpu_memory_fraction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gpu_mem_fraction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allow_growth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;不过好像还是没啥用。。&lt;/p&gt;

&lt;h2 id=&quot;x2-其他开源库&quot;&gt;x.2 其他开源库&lt;/h2&gt;

&lt;p&gt;transformer-model&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;transformer-model
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;github: &lt;a href=&quot;https://github.com/zbloss/TransformerModel&quot;&gt;https://github.com/zbloss/TransformerModel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;要点：&lt;/p&gt;

&lt;p&gt;编码器：由 6 个完全相同的层堆叠而成，每个层有 2 个子层。在每个子层后面会跟一个残差连接和层正则化（layer normalization）。第一部分由一个多头（multi-head）自注意力机制，第二部分则是一个位置敏感的全连接前馈网络。&lt;/p&gt;

&lt;p&gt;解码器：解码器也由 6 个完全相同的层堆叠而成，不同的是这里每层有 3 个子层，第 3 个子层负责处理编码器输出的多头注意力机制。解码器的子层后面也跟了残差连接和层正则化。解码器的自注意力子层也做了相应修改。&lt;/p&gt;

&lt;p&gt;在编码器-解码器层当中，query 来自上一个解码层，编码器输出值（value）及 memory key。这样，解码器中所有位置都能照顾到输入序列中的所有位置。&lt;/p&gt;

&lt;p&gt;编码器含有自注意力层。在自注意力层中，所有的 key、value 和 query 都来自同一个地方，那就是编码器的上一层输出。编码器中的每一个位置都能照顾到编码器上一层中所有的位置。&lt;/p&gt;

&lt;p&gt;同样，解码器中的自注意力层让解码器中所有位置都能被注意到，包括那个位置本身。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>fasttext</title>
   <link href="http://daiwk.github.io/posts/nlp-fasttext.html"/>
   <updated>2017-06-21T00:00:00+00:00</updated>
   <id>/posts/nlp-fasttext</id>
   <content type="html">&lt;p&gt;目录
&lt;!-- TOC --&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-%e5%8e%9f%e7%90%86&quot;&gt;0. 原理&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#01-softmax%e5%9b%9e%e5%bd%92&quot;&gt;0.1 softmax回归&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#02-%e5%88%86%e5%b1%82softmax&quot;&gt;0.2 分层softmax&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#04-n-gram&quot;&gt;0.4 n-gram&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#05-cbow&quot;&gt;0.5 CBOW&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#051-%e5%89%8d%e5%90%91%e4%bc%a0%e6%92%ad&quot;&gt;0.5.1 前向传播&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#052-%e5%8f%8d%e5%90%91%e4%bc%a0%e6%92%ad&quot;&gt;0.5.2 反向传播&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#06-skip-gram&quot;&gt;0.6 skip-gram&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#07-fasttext&quot;&gt;0.7 fasttext&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-bin%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95&quot;&gt;1. bin使用方法&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%ae%ad%e7%bb%83&quot;&gt;训练&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e9%a2%84%e6%b5%8b&quot;&gt;预测&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%be%93%e5%87%ba%e5%90%91%e9%87%8f&quot;&gt;输出向量&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-python%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95&quot;&gt;2. python使用方法&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%ae%89%e8%a3%85&quot;&gt;安装&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%87%aa%e5%8a%a8%e8%b0%83%e5%8f%82&quot;&gt;自动调参&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考word2vec基本原理：&lt;a href=&quot;https://daiwk.github.io/posts/nlp-word2vec.html&quot;&gt;https://daiwk.github.io/posts/nlp-word2vec.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;paper:&lt;/p&gt;

&lt;p&gt;【提出fasttext的paper】&lt;a href=&quot;https://arxiv.org/pdf/1607.01759v3.pdf&quot;&gt;Bag of Tricks for Efficient Text Classification&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1607.04606.pdf&quot;&gt;Enriching Word Vectors with Subword Information&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;website: &lt;a href=&quot;https://fasttext.cc/&quot;&gt;https://fasttext.cc/&lt;/a&gt;【有pretrained-vectors可以下载】&lt;/p&gt;

&lt;h2 id=&quot;0-原理&quot;&gt;0. 原理&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650736673&amp;amp;idx=4&amp;amp;sn=d5cb11250b28912accbc08ddb5d9c97b&amp;amp;chksm=871acc5fb06d45492ee54f3ff42e767bdc668d12c615b8ddc7e0aaeae7748aafe1aa53686176&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0126lb9yqb0yUzJ30cmJc7ML&amp;amp;pass_ticket=5bhFv%2FwprJeuXSNRdbTSRrHitcKLawmckNnlQIBt%2FjavQ3ytUGB53qdfRz7NsZP4#rd&quot;&gt;fastText原理及实践&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在文本分类任务中，fastText（浅层网络）往往能取得和深度网络相媲美的精度，却在训练时间上比深度网络快许多数量级。在标准的多核CPU上， 能够训练&lt;strong&gt;10亿词级别语料库&lt;/strong&gt;的词向量在&lt;strong&gt;10分钟之内&lt;/strong&gt;，能够分类有着&lt;strong&gt;30万多类别&lt;/strong&gt;的&lt;strong&gt;50多万句子&lt;/strong&gt;在&lt;strong&gt;1分钟&lt;/strong&gt;之内。&lt;/p&gt;

&lt;h3 id=&quot;01-softmax回归&quot;&gt;0.1 softmax回归&lt;/h3&gt;

&lt;p&gt;softmax回归被称作多项逻辑回归（multinomial logistic regression），是逻辑回归在处理多类别任务上的推广。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fasttext-softmax.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fasttext-softmax-lr.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;02-分层softmax&quot;&gt;0.2 分层softmax&lt;/h3&gt;

&lt;p&gt;softmax中，我们需要对所有的K个概率做归一化，这在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(|y|\)&lt;/code&gt;很大时非常耗时。分层softmax的基本思想是&lt;strong&gt;使用树的层级结构&lt;/strong&gt;替代扁平化的标准Softmax，在计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(y=j)\)&lt;/code&gt;时，只需计算&lt;strong&gt;一条路径上的所有节点的概率值&lt;/strong&gt;，无需在意其它的节点。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fasttext-hierachical-softmax.png&quot; style=&quot;max-height: 100px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;树的结构是根据&lt;strong&gt;类标的频数&lt;/strong&gt;构造的&lt;strong&gt;霍夫曼树&lt;/strong&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;K&lt;/strong&gt;个不同的类标组成所有的&lt;strong&gt;叶子&lt;/strong&gt;节点。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;K-1个内部节点&lt;/strong&gt;作为内部参数。&lt;/li&gt;
  &lt;li&gt;从根节点到某个叶子节点经过的节点和边形成一条路径，路径长度被表示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L(y_j)\)&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(y_j)=\prod _{l=1}^{L(y_j)-1}\sigma (\left \lfloor n(y_j,l+1)=LC(n(y_j,l)) \right \rfloor \cdot \theta _{n(y_j,l)} ^TX) 
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l\)&lt;/code&gt;表示第几层（从1开始）；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma (\cdot )\)&lt;/code&gt;表示sigmoid函数；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(LC(n)\)&lt;/code&gt;表示n节点的左孩子；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left \lfloor \right \rfloor \)&lt;/code&gt;是一个特殊的函数，定义如下：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\left \lfloor x \right \rfloor =\left\{\begin{matrix}
1, if x =true
\\ -1,otherwise
\end{matrix}\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta _{n(y_j,l)}\)&lt;/code&gt;是中间节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n(y_j,l)\)&lt;/code&gt;的参数&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;高亮的节点和边是&lt;strong&gt;从根节点到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_2\)&lt;/code&gt;的路径&lt;/strong&gt;，路径长度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L(y_2)=3\)&lt;/code&gt;，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\P(y_2)=P(n(y_2,1),left)P(n(y_2,2),left)P(n(y_2,3),right)
\\=\sigma (\theta _{n(y_2,1)}^TX) \cdot \sigma (\theta _{n(y_2,2)}^TX) \cdot \sigma (-\theta _{n(y_2,3)}^TX) 
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;于是，从根节点走到叶子节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_2\)&lt;/code&gt;，其实是做了3次二分类的lr。通过分层的Softmax，计算复杂度一下从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(|K|\)&lt;/code&gt;降低到&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(log_2|K|\)&lt;/code&gt;&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;似乎暂时没有tf的实现，有keras的实现：&lt;a href=&quot;https://github.com/jmhessel/keras/blob/master/keras/layers/advanced_activations.py#L268-L395&quot;&gt;https://github.com/jmhessel/keras/blob/master/keras/layers/advanced_activations.py#L268-L395&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;04-n-gram&quot;&gt;0.4 n-gram&lt;/h3&gt;

&lt;p&gt;基本思想是将文本内容&lt;strong&gt;按照字节顺序&lt;/strong&gt;进行&lt;strong&gt;大小为N的滑动窗口&lt;/strong&gt;操作，最终形成长度为N的字节片段序列。&lt;strong&gt;n-gram产生的特征&lt;/strong&gt;只是作为&lt;strong&gt;文本特征的候选集&lt;/strong&gt;，你后面可能会采用信息熵、卡方统计、IDF等文本特征选择方式筛选出比较重要特征。&lt;/p&gt;

&lt;h3 id=&quot;05-cbow&quot;&gt;0.5 CBOW&lt;/h3&gt;

&lt;p&gt;CBOW模型的基本思路是：用上下文预测目标词汇。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fasttext-cbow.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;输入层由目标词汇y的上下文单词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\({x_1,...,x_c}\)&lt;/code&gt;组成，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;是一个one-hot后的V维向量；&lt;/li&gt;
  &lt;li&gt;隐层是N维向量h；&lt;/li&gt;
  &lt;li&gt;输出层是one-hot编码后的目标词y&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;权重：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每个输入向量通过VxN维的权重矩阵W（C个向量，共享同一个W）连接到隐层&lt;/li&gt;
  &lt;li&gt;隐层通过V*N维的权重矩阵W’连接到输出层&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;因为词库V往往非常大，使用标准的softmax计算相当耗时，于是CBOW的输出层采用的是分层Softmax。&lt;/p&gt;

&lt;h4 id=&quot;051-前向传播&quot;&gt;0.5.1 前向传播&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;隐层的输出是C个上下文单词向量先求和，然后乘以W,再取平均&lt;/strong&gt;，得到的一个N维向量:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
h=\frac{1}{C}W \sum ^C_{i=1}x_i 
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后计算输出层的每个节点：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_j=v'_{w_j}^T\cdot h\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(v'_{w_j}\)&lt;/code&gt;是矩阵W’的第j列&lt;strong&gt;(W’是N行V列，即，第j个词对应的N维向量)&lt;/strong&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_j\)&lt;/code&gt;就是一个N维向量和N维向量的转置乘出来的一个值，&lt;/p&gt;

&lt;p&gt;最后，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u_j\)&lt;/code&gt;作为softmax的输入，得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_j\)&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
y_j=p(w_{y_j}|w_1,...,w_C)=\frac {exp(u_j)}{\sum ^V_{j'=1}exp(u_{j'})}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;052-反向传播&quot;&gt;0.5.2 反向传播&lt;/h4&gt;

&lt;p&gt;定义损失函数，objective是最大化给定输入上下文，target单词的条件概率如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fasttext-back-prop.png&quot; style=&quot;max-height: 200px&quot; /&gt;&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;更新W’：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fasttext-back-prop-update-w1.png&quot; style=&quot;max-height: 200px&quot; /&gt;&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;更新W：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fasttext-back-prop-update-w.png&quot; style=&quot;max-height: 200px&quot; /&gt;&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;06-skip-gram&quot;&gt;0.6 skip-gram&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA5NzY0MDg1NA==&amp;amp;mid=2706953858&amp;amp;idx=1&amp;amp;sn=cb83cb61330cdb77275e846bec6e7433&amp;amp;scene=21#wechat_redirect&quot;&gt;技术干货 | 漫谈Word2vec之skip-gram模型&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;07-fasttext&quot;&gt;0.7 fasttext&lt;/h3&gt;

&lt;p&gt;word2vec把语料库中的&lt;strong&gt;每个单词当成原子&lt;/strong&gt;的，它会为每个单词生成一个向量。这&lt;strong&gt;忽略了单词内部的形态特征&lt;/strong&gt;，比如：“apple” 和“apples”，”xx公司”和”xx”，两个单词都有较多公共字符，即它们的内部形态类似，但是在传统的word2vec中，这种单词内部形态信息因为它们被转换成不同的id丢失了。&lt;/p&gt;

&lt;p&gt;为了克服这个问题，fastText使用了&lt;strong&gt;字符级别&lt;/strong&gt;的&lt;strong&gt;n-grams&lt;/strong&gt;来表示一个单词。例如，apple的3-gram如下：&lt;/p&gt;

&lt;p&gt;“&amp;lt;ap”,  “app”,  “ppl”,  “ple”, “le&amp;gt;”&lt;/p&gt;

&lt;p&gt;&amp;lt;表示前缀，&amp;gt;表示后缀。进一步，我们可以用这5个trigram的向量叠加来表示“apple”的词向量。&lt;/p&gt;

&lt;p&gt;好处：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;对于&lt;strong&gt;低频词生成的词向量&lt;/strong&gt;效果会更好。因为它们的n-gram可以和其它词共享。&lt;/li&gt;
  &lt;li&gt;对于&lt;strong&gt;训练词库之外的单词(未登录词)&lt;/strong&gt;，仍然可以构建它们的词向量。我们可以叠加它们的字符级n-gram向量。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;注：当然，最新的方法其实是&lt;a href=&quot;https://daiwk.github.io/posts/nlp-nmt.html#71-backbone%E6%A8%A1%E5%9E%8B&quot;&gt;BPE&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;fastext的架构图如下：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fasttext-arch.png&quot; style=&quot;max-height: 200px&quot; /&gt;&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;相同点：&lt;/p&gt;

&lt;p&gt;和CBOW一样，fastText模型也只有三层：输入层、隐含层、输出层（Hierarchical Softmax），输入都是多个经向量表示的单词，输出都是一个特定的target，隐含层都是对&lt;strong&gt;多个词向量的叠加平均&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;不同点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;CBOW的输入是目标单词的上下文，fastText的输入是&lt;strong&gt;多个单词&lt;/strong&gt;及&lt;strong&gt;其n-gram特征&lt;/strong&gt;，这些特征用来表示单个文档；&lt;/li&gt;
  &lt;li&gt;CBOW的输入单词被onehot编码过，fastText的输入特征是被embedding过；&lt;/li&gt;
  &lt;li&gt;word2vec是一个无监督算法，而fasttext是一个有监督算法。CBOW的输出是目标词汇，fastText的输出是文档对应的类标。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;对于N篇文档，最小化针对所有文档的 negative loglikelihood(其实就是多分类的交叉熵)：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
-\frac {1}{N}\sum ^N_{n=1}y_nlog(f(BAx_n))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_n\)&lt;/code&gt;是第n篇文档的normalized bag of features。A是基于word的look-up table，即词的embedding向量，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Ax_n\)&lt;/code&gt;是将word的embedding向量找到后相加或者取平均，得到hidden向量。B是函数f的参数，f是softmax。&lt;/p&gt;

&lt;h2 id=&quot;1-bin使用方法&quot;&gt;1. bin使用方法&lt;/h2&gt;

&lt;p&gt;编译好的bin地址：&lt;a href=&quot;https://daiwk.github.io/assets/fasttext&quot;&gt;https://daiwk.github.io/assets/fasttext&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;训练&quot;&gt;训练&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;训练数据demo：&lt;a href=&quot;https://daiwk.github.io/assets/train_demo_fasttext.txt&quot;&gt;https://daiwk.github.io/assets/train_demo_fasttext.txt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;格式(空格分割)：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;__label__xx w1 w2 w3 ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;训练命令：&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./fasttext supervised -input train_demo_fasttext.txt -output haha.model
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;高级参数：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-minn 1 -maxn 6: 不用切词，1-6直接n-gram
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;预测&quot;&gt;预测&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;测试数据demo：&lt;a href=&quot;https://daiwk.github.io/assets/test_demo_fasttext.txt&quot;&gt;https://daiwk.github.io/assets/test_demo_fasttext.txt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;格式(空格分割）：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;__label__00 key w1 w2 w3 ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中，&lt;strong&gt;key可以中间有\1等任何分隔符，但key里面不能有空格&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;预测命令&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat test_demo_fasttext.txt | ./fasttext predict-prob haha.model.bin - 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;预测输出&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;key __label__xx probability
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;输出向量&quot;&gt;输出向量&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat test_demo_fasttext.txt | ./fasttext print-vectors haha.model.bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;2-python使用方法&quot;&gt;2. python使用方法&lt;/h2&gt;

&lt;h3 id=&quot;安装&quot;&gt;安装&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;xxxx/pip install cython
xxxx/pip install fasttext
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;自动调参&quot;&gt;自动调参&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/LLrq1F2uEC2xEWZrd9uijA&quot;&gt;一行代码自动调参，支持模型压缩指定大小，Facebook升级FastText&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要有以下几个：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;-autotune-validation&lt;/span&gt; validation file to be used &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;evaluation 
&lt;span class=&quot;nt&quot;&gt;-autotune-metric&lt;/span&gt; metric objective &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;f1, f1:labelname&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;f1] 
&lt;span class=&quot;nt&quot;&gt;-autotune-predictions&lt;/span&gt; number of predictions used &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;evaluation &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;1] 
&lt;span class=&quot;nt&quot;&gt;-autotune-duration&lt;/span&gt; maximum duration &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;seconds &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;300] 
&lt;span class=&quot;nt&quot;&gt;-autotune-modelsize&lt;/span&gt; constraint model file size &lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;empty &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do &lt;/span&gt;not quantize&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Convolutional Sequence to Sequence Learning</title>
   <link href="http://daiwk.github.io/posts/nlp-convseq2seq.html"/>
   <updated>2017-06-21T00:00:00+00:00</updated>
   <id>/posts/nlp-convseq2seq</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;lua版本：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/facebookresearch/fairseq&quot;&gt;https://github.com/facebookresearch/fairseq&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pytorch版本：&lt;/p&gt;

&lt;p&gt;这个repository其实包括了很多：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Convolutional Neural Networks (CNN)
    &lt;ul&gt;
      &lt;li&gt;Dauphin et al. (2017): Language Modeling with Gated Convolutional Networks&lt;/li&gt;
      &lt;li&gt;Gehring et al. (2017): Convolutional Sequence to Sequence Learning&lt;/li&gt;
      &lt;li&gt;Edunov et al. (2018): Classical Structured Prediction Losses for Sequence to Sequence Learning&lt;/li&gt;
      &lt;li&gt;New Fan et al. (2018): Hierarchical Neural Story Generation&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Long Short-Term Memory (LSTM) networks
    &lt;ul&gt;
      &lt;li&gt;Luong et al. (2015): Effective Approaches to Attention-based Neural Machine Translation&lt;/li&gt;
      &lt;li&gt;Wiseman and Rush (2016): Sequence-to-Sequence Learning as Beam-Search Optimization&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Transformer (self-attention) networks
    &lt;ul&gt;
      &lt;li&gt;Vaswani et al. (2017): Attention Is All You Need&lt;/li&gt;
      &lt;li&gt;New Ott et al. (2018): Scaling Neural Machine Translation&lt;/li&gt;
      &lt;li&gt;New Edunov et al. (2018): Understanding Back-Translation at Scale&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/pytorch/fairseq&quot;&gt;https://github.com/pytorch/fairseq&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Convolutional Sequence to Sequence Learning&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1705.03122&quot;&gt;https://arxiv.org/abs/1705.03122&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.zhihu.com/question/59645329&quot;&gt;https://www.zhihu.com/question/59645329&lt;/a&gt;小结了一下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Trick1: 加上一个position embedding给CNN更多的“位置感”&lt;/li&gt;
  &lt;li&gt;Trick2: 给CNN的输出加gate&lt;/li&gt;
  &lt;li&gt;Trick3: 给CNN都加上residual connection&lt;/li&gt;
  &lt;li&gt;Trick4: 一层attention不够那就上叠加的attention然后就好用了&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.dataguru.cn/article-12165-1.html&quot;&gt;http://www.dataguru.cn/article-12165-1.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;传统的seq2seq模型是基于RNN来实现的，特别是LSTM，这就带来了计算量复杂的问题。Facebook作出大胆改变，&lt;strong&gt;将编码器、解码器、注意力机制甚至是记忆单元全部替换成卷积神经网络&lt;/strong&gt;。虽然单层CNN只能看到固定范围的上下文，但是&lt;strong&gt;将多个CNN叠加起来就可以很容易将有效的上下文范围放大。&lt;/strong&gt;Facebook将此模型成功地应用到了英语-法语机器翻译、英语-德语机器翻译，不仅刷新了二者前期的记录，而且还将训练速度提高了一个数量级，无论是GPU还是CPU上。&lt;/p&gt;

&lt;p&gt;用于语言建模的&lt;strong&gt;Gated CNN&lt;/strong&gt;模型如下图所示，可以看到，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;最上层的word embedding操作与传统的语言建模没有区别，&lt;/li&gt;
  &lt;li&gt;紧接着就是对这些embedding向量划分时间窗并做卷积操作，注意这里使用了两个卷积神经网络，
    &lt;ul&gt;
      &lt;li&gt;这两个神经网络中的其中一个通过激活函数一个与另外一个进行相乘，得到最终的输出。&lt;/li&gt;
      &lt;li&gt;其中一个卷积神经网络的功能就是充当了闸门的作用，即控制着多少有用的信息作为最终的输出。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/gated-cnn.jpg&quot; style=&quot;max-height: 500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;其中的CNN可以参考一下&lt;a href=&quot;http://paddlepaddle.org/documentation/docs/zh/1.0/beginners_guide/basics/recommender_system/index.html&quot;&gt;http://paddlepaddle.org/documentation/docs/zh/1.0/beginners_guide/basics/recommender_system/index.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;n个词，emb是k，输入nxk，filter的大小是hxk，一个filter做卷积就得到一个n-h+1维的向量，即第二张图的一列，这个例子是有4个卷积核，那第二张图里就有4列，然后这个”时间维的”max-pooling就是对这四列，每列的所有元素求max(相当于对于每列有一个pooling，它的大小和这一列一样大)，最终得到4个元素。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/conv-seq2seq.jpg&quot; style=&quot;max-height: 500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;如上图，对于编码器，原始的单词首先需要经过embedding层得到其相应的embedding向量，然后将embedding向量作为Gated CNN的输入，这里需要注意的是，为了保证经过卷积操作之后与之前的输入长度一致，卷积需要做pad操作。模型中有两个地方都使用到了GLU(Gated Linear Unit)，我在图中已经用红色字眼标出，编码器的embedding和解码器的embedding分别通过各自的GLU单元得到各自的分布式状态，将两个状态矩阵进行点乘即可得到注意力权重，图中已用红色字体Attention标出，具体的注意力权重的计算如下公式所示：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
a^l_{ij}=\frac{exp(d^l_i\cdot z^u_j)}{\sum ^m_{t=1}exp(d^l_i\cdot z^u_t)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;注意到图中编码器的embedding和编码器的状态进行相加，并且乘上注意力权重，得到的结果文中称之为条件输入c，这里我们可以对比传统的注意力机制，传统的注意力机制是直接将注意力权重和编码器的状态进行相乘，而这里引入了embedding量，文中解释是因为embedding可以在做预测的时候可以结合具体元素的信息，增加了位置感，条件输入c的计算如下图公式所示：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
c^l_{i}=\sum ^m_{j=1}a^l_{ij}(z^u_j+e_j)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;将条件输入c加上解码器的状态，即可得到输出序列的概率。作者最终在机器翻译上相比其他RNN的模型速度提高了近10倍。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>安装wget 1.18+</title>
   <link href="http://daiwk.github.io/posts/other-install-wget-1.18.html"/>
   <updated>2017-04-26T00:00:00+00:00</updated>
   <id>/posts/other-install-wget-1.18</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-安装&quot;&gt;0. 安装&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-安装gmp&quot;&gt;1. 安装gmp&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-安装nettle&quot;&gt;2. 安装nettle&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#３　安装gnutl&quot;&gt;３.　安装gnutl&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-安装wget&quot;&gt;4. 安装wget&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;0-安装&quot;&gt;0. 安装&lt;/h2&gt;

&lt;h2 id=&quot;1-安装gmp&quot;&gt;1. 安装gmp&lt;/h2&gt;

&lt;h2 id=&quot;2-安装nettle&quot;&gt;2. 安装nettle&lt;/h2&gt;

&lt;p&gt;LD_LIBRARY_PATH=/usr/local/lib64:/usr/local/lib/:/usr/lib/:/usr/lib64/:/opt/lib/:/opt/lib64/ NETTLE_CFLAGS=”-I/usr/local/include/” NETTLE_LIBS=”-L/usr/local/lib64/ -lnettle” HOGWEED_CFLAGS=”-I/usr/local/include” HOGWEED_LIBS=”-L/usr/local/lib64/ -lhogweed”&lt;/p&gt;

&lt;p&gt;3.1版本的可以，3.3的不行。。产生的makefile没有hogweed..&lt;/p&gt;

&lt;p&gt;##　３.　安装gnutl&lt;/p&gt;

&lt;p&gt;./configure –with-included-libtasn1=/usr/local/include/ –without-p11-kit&lt;/p&gt;

&lt;h2 id=&quot;4-安装wget&quot;&gt;4. 安装wget&lt;/h2&gt;

&lt;p&gt;其实，docker里的1.15就够用了。。
新建一个~/.netrc，内容如下&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;machine machine urs.earthdata.nasa.gov login xxx password xxx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后touch一个空文件urs_cookies&lt;/p&gt;

&lt;p&gt;然后执行命令：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget --content-disposition --load-cookies ../.urs_cookies --save-cookies ../.urs_cookies --auth-no-challenge=on --keep-session-cookies -i ../input.urls.txt 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>tf的seq2seq</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-seq2seq.html"/>
   <updated>2017-04-16T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-seq2seq</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-简介&quot;&gt;1. 简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-安装&quot;&gt;2. 安装&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-基本概念&quot;&gt;3. 基本概念&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-示例nmt&quot;&gt;4. 示例：nmt&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-数据格式&quot;&gt;4.1 数据格式&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-数据下载&quot;&gt;4.2 数据下载&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#43-小数据集generate-toy-data&quot;&gt;4.3 小数据集：generate toy data&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#44-定义模型&quot;&gt;4.4 定义模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#45-训练&quot;&gt;4.5 训练&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#46-预测&quot;&gt;4.6 预测&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#47-使用beamsearch进行decode&quot;&gt;4.7 使用beamsearch进行decode&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#48-基于checkpoint进行评估&quot;&gt;4.8 基于checkpoint进行评估&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#49-计算bleu&quot;&gt;4.9 计算BLEU&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;使用tf，但独立于tensorflow/models之外的一个seq2seq的框架：&lt;/p&gt;

&lt;p&gt;主页：&lt;a href=&quot;https://google.github.io/seq2seq/&quot;&gt;https://google.github.io/seq2seq/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-简介&quot;&gt;1. 简介&lt;/h2&gt;

&lt;p&gt;对应的论文：
&lt;a href=&quot;https://arxiv.org/pdf/1703.03906.pdf&quot;&gt;Massive Exploration of Neural Machine Translation Architectures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;整体架构图：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/tf-seq2seq architecture.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;2-安装&quot;&gt;2. 安装&lt;/h2&gt;

&lt;p&gt;首先安装python-tk（python的gui）&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt-get install python-tk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后设置matplotlib的&lt;a href=&quot;http://matplotlib.org/faq/usage_faq.html#what-is-a-backend&quot;&gt;backend&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo &quot;backend : Agg&quot; &amp;gt;&amp;gt; $HOME/.config/matplotlib/matplotlibrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后安装seq2seq&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd seq2seq
pip install -e .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;测试一下&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python -m unittest seq2seq.test.pipeline_test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-基本概念&quot;&gt;3. 基本概念&lt;/h2&gt;

&lt;h2 id=&quot;4-示例nmt&quot;&gt;4. 示例：nmt&lt;/h2&gt;

&lt;p&gt;经典：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1409.0473&quot;&gt;Neural Machine Translation by Jointly Learning to Align and Translate&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1703.01619.pdf&quot;&gt;Neural Machine Translation and Sequence-to-sequence Models: A Tutorial (Neubig et al.)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.tensorflow.org/tutorials/seq2seq&quot;&gt;Tensorflow Sequence-To-Sequence Tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;41-数据格式&quot;&gt;4.1 数据格式&lt;/h3&gt;

&lt;p&gt;正常的分词是空格切分或者常用的分词工具（如Moses的&lt;a href=&quot;https://github.com/moses-smt/mosesdecoder/blob/master/scripts/tokenizer/tokenizer.perl&quot;&gt;tokenizer.perl&lt;/a&gt;、框架有&lt;a href=&quot;https://spacy.io/docs/usage/processing-text&quot;&gt;spacy&lt;/a&gt;/&lt;a href=&quot;http://www.nltk.org/api/nltk.tokenize.html&quot;&gt;nltk&lt;/a&gt;/&lt;a href=&quot;https://nlp.stanford.edu/software/tokenizer.shtml&quot;&gt;stanford的分词&lt;/a&gt;）。&lt;/p&gt;

&lt;p&gt;但用于nmt时存在如下问题：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;nmt输出的是在词上的概率分布，所以如果可能的词很多，那么会非常慢。如果vocabulary中有拼错的词和派生词，那vocabulary就可能趋向无穷大……而我们实际上可能会&lt;strong&gt;人工限制vocabulary size在10,000-100,000的范围内。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;loved和loving来自同一词根，但却被当成完全不同的两个词来看待。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;对于这种&lt;strong&gt;open vocabulary&lt;/strong&gt;的问题一，一种解决方法就是从给定的文本中学习&lt;strong&gt;subword units&lt;/strong&gt;。例如，loved可以被分成lov和ed，loving可以被分成lov和ing。这样，一方面可以产生新的词（unknown words），另一方面，可以缩减vocabulary size。本示例用到的就是&lt;strong&gt;&lt;a href=&quot;https://arxiv.org/pdf/1508.07909.pdf&quot;&gt;Byte Pair Encoding (BPE)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Clone from Github
git clone https://github.com/rsennrich/subword-nmt
cd subword-nmt

# Learn a vocabulary using 10,000 merge operations
./learn_bpe.py -s 10000 &amp;lt; train.tok &amp;gt; codes.bpe

# Apply the vocabulary to the training file
./apply_bpe.py -c codes.bpe &amp;lt; train.tok &amp;gt; train.tok.bpe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果如下，会用对不常见的词（如例子中的Nikitin）使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@@&lt;/code&gt;进行切分。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Madam President , I should like to draw your attention to a case in which this Parliament has consistently shown an interest. It is the case of Alexander Ni@@ ki@@ tin .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;42-数据下载&quot;&gt;4.2 数据下载&lt;/h3&gt;

&lt;p&gt;使用预处理过的English-German WMT’16 Translation Task的数据集进行训练。 参考wmt16_en_de.sh，需要自定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OUTPUT_DIR&lt;/code&gt;变量。这个脚本下载数据、使用Moses Tokenizer进行分词、清理了training data、学习了约32,000的vocabulary大小的subword units.也可以直接去google drive下载预处理过的&lt;a href=&quot;https://drive.google.com/open?id=0B_bZck-ksdkpM25jRUN2X2UxMm8&quot;&gt;WMT’16 EN-DE Data (502MB)&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;格式如下：&lt;/p&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;文件名&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;描述&lt;/th&gt;

&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;train.tok.clean.bpe.32000.en&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;The English training data, one sentence per line, processed using BPE.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;train.tok.clean.bpe.32000.de&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;The German training data, one sentence per line, processed using BPE.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;vocab.bpe.32000&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;The full vocabulary used in the training data, one token per line.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;newstestXXXX.*&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;Development and test data sets, in the same format as the training data. We provide both pre-processed and original data files used for evaluation. &lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;p&gt;然后需要设置环境变量：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Set this to where you extracted the downloaded file
export DATA_PATH=

export VOCAB_SOURCE=${DATA_PATH}/vocab.bpe.32000
export VOCAB_TARGET=${DATA_PATH}/vocab.bpe.32000
export TRAIN_SOURCES=${DATA_PATH}/train.tok.clean.bpe.32000.en
export TRAIN_TARGETS=${DATA_PATH}/train.tok.clean.bpe.32000.de
export DEV_SOURCES=${DATA_PATH}/newstest2013.tok.bpe.32000.en
export DEV_TARGETS=${DATA_PATH}/newstest2013.tok.bpe.32000.de

export DEV_TARGETS_REF=${DATA_PATH}/newstest2013.tok.de
export TRAIN_STEPS=1000000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;43-小数据集generate-toy-data&quot;&gt;4.3 小数据集：generate toy data&lt;/h3&gt;

&lt;p&gt;直接跑toy.sh就行了,然后设置一下环境变量&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export DATA_PATH=
export DATA_TYPE=copy # or reverse

export VOCAB_SOURCE=${DATA_PATH}/nmt_data/toy_${DATA_TYPE}/train/vocab.sources.txt
export VOCAB_TARGET=${DATA_PATH}/nmt_data/toy_${DATA_TYPE}/train/vocab.targets.txt
export TRAIN_SOURCES=${DATA_PATH}/nmt_data/toy_${DATA_TYPE}/train/sources.txt
export TRAIN_TARGETS=${DATA_PATH}/nmt_data/toy_${DATA_TYPE}/train/targets.txt
export DEV_SOURCES=${DATA_PATH}/nmt_data/toy_${DATA_TYPE}/dev/sources.txt
export DEV_TARGETS=${DATA_PATH}/nmt_data/toy_${DATA_TYPE}/dev/targets.txt

export DEV_TARGETS_REF=${DATA_PATH}/nmt_data/toy_${DATA_TYPE}/dev/targets.txt
export TRAIN_STEPS=1000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;44-定义模型&quot;&gt;4.4 定义模型&lt;/h3&gt;

&lt;p&gt;标准的模型是&lt;a href=&quot;https://arxiv.org/abs/1409.0473&quot;&gt;seq2seq with attention&lt;/a&gt;,它有大量的超参数。在example_configs下有small/medium/large等conf，例如，medium的conf:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;model: AttentionSeq2Seq
model_params:
  attention.class: seq2seq.decoders.attention.AttentionLayerBahdanau
  attention.params:
    num_units: 256
  bridge.class: seq2seq.models.bridges.ZeroBridge
  embedding.dim: 256
  encoder.class: seq2seq.encoders.BidirectionalRNNEncoder
  encoder.params:
    rnn_cell:
      cell_class: GRUCell
      cell_params:
        num_units: 256
      dropout_input_keep_prob: 0.8
      dropout_output_keep_prob: 1.0
      num_layers: 1
  decoder.class: seq2seq.decoders.AttentionDecoder
  decoder.params:
    rnn_cell:
      cell_class: GRUCell
      cell_params:
        num_units: 256
      dropout_input_keep_prob: 0.8
      dropout_output_keep_prob: 1.0
      num_layers: 2
  optimizer.name: Adam
  optimizer.params:
    epsilon: 0.0000008
  optimizer.learning_rate: 0.0001
  source.max_seq_len: 50
  source.reverse: false
  target.max_seq_len: 50
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;45-训练&quot;&gt;4.5 训练&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;单GPU（例如TitanX），即使是small模型，训练WMT’16 English-German数据要收敛得好几天。在8GPU的集群上，用tf的分布式训练，large模型需要2-3天。&lt;/strong&gt;而对于toy数据，在cpu上，1000 step大概要10min。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export MODEL_DIR=${TMPDIR:-/tmp}/nmt_tutorial
mkdir -p $MODEL_DIR

python -m bin.train \
  --config_paths=&quot;
      ./example_configs/nmt_small.yml,
      ./example_configs/train_seq2seq.yml,
      ./example_configs/text_metrics_bpe.yml&quot; \
  --model_params &quot;
      vocab_source: $VOCAB_SOURCE
      vocab_target: $VOCAB_TARGET&quot; \
  --input_pipeline_train &quot;
    class: ParallelTextInputPipeline
    params:
      source_files:
        - $TRAIN_SOURCES
      target_files:
        - $TRAIN_TARGETS&quot; \
  --input_pipeline_dev &quot;
    class: ParallelTextInputPipeline
    params:
       source_files:
        - $DEV_SOURCES
       target_files:
        - $DEV_TARGETS&quot; \
  --batch_size 32 \
  --train_steps $TRAIN_STEPS \
  --output_dir $MODEL_DIR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中，参数如下：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;config_paths：可以输入多个conf文件，会&lt;strong&gt;按顺序merge在一起&lt;/strong&gt;。nmt_small.yml描述模型类型及超参数，train_seq2seq.yml包括了common options，例如，追踪什么metric、多久sample一次response。&lt;/li&gt;
  &lt;li&gt;model_params：可以重写模型参数，yaml/json格式。大部分参数其实在nmt_small.yml里都定义了，但&lt;/li&gt;
  &lt;li&gt;input_pipeline_train：如何读训练集。例子中是parallel text format。&lt;/li&gt;
  &lt;li&gt;input_pipeline_dev：如何读验证集。例子中是parallel text format。&lt;/li&gt;
  &lt;li&gt;output_dir：模型的checkpoint和summary的存放位置。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用tensorboard监控输出目录（有log_perplexity和bleu等指标）：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;tensorboard --logdir $MODEL_DIR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;46-预测&quot;&gt;4.6 预测&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export PRED_DIR=${MODEL_DIR}/pred
mkdir -p ${PRED_DIR}

python -m bin.infer \
  --tasks &quot;
    - class: DecodeText&quot; \
  --model_dir $MODEL_DIR \
  --input_pipeline &quot;
    class: ParallelTextInputPipeline
    params:
      source_files:
        - $DEV_SOURCES&quot; \
  &amp;gt;  ${PRED_DIR}/predictions.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中，&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;tasks：&lt;/li&gt;
  &lt;li&gt;model_dir：&lt;/li&gt;
  &lt;li&gt;model_dir：&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;47-使用beamsearch进行decode&quot;&gt;4.7 使用beamsearch进行decode&lt;/h3&gt;

&lt;p&gt;beamsearch并不是使用贪心的方法去寻找最可能的词，而是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;keeps several hypotheses, or &quot;beams&quot;, in memory and chooses the best one based on a scoring function.&lt;/code&gt;。可以通过指定model_params来使用beamsearch。另外，使用beamsearch会使预测时间变得&lt;strong&gt;significantly longer&lt;/strong&gt;。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python -m bin.infer \
  --tasks &quot;
    - class: DecodeText
    - class: DumpBeams
      params:
        file: ${PRED_DIR}/beams.npz&quot; \
  --model_dir $MODEL_DIR \
  --model_params &quot;
    inference.beam_search.beam_width: 5&quot; \
  --input_pipeline &quot;
    class: ParallelTextInputPipeline
    params:
      source_files:
        - $DEV_SOURCES&quot; \
  &amp;gt; ${PRED_DIR}/predictions.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;另外，上面的例子显示了，tasks参数可以传多个task进去；另外，还把结果存放在了${PRED_DIR}/beams.npz中。用法：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import numpy as np
r = np.load(&quot;/tmp/nmt_tutorial/pred/beams.npz&quot;)
print r.files
##得到：['predicted_ids', 'beam_parent_ids', 'log_probs', 'scores']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;48-基于checkpoint进行评估&quot;&gt;4.8 基于checkpoint进行评估&lt;/h3&gt;

&lt;p&gt;infer脚本默认只评估最新的checkpoint，如果想指定checkpoint，可以传&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;checkpoint_path&lt;/code&gt;参数。&lt;/p&gt;

&lt;h3 id=&quot;49-计算bleu&quot;&gt;4.9 计算BLEU&lt;/h3&gt;

&lt;p&gt;以下命令可以计算bleu：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./bin/tools/multi-bleu.perl ${DEV_TARGETS_REF} &amp;lt; ${PRED_DIR}/predictions.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;注意：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;如果你使用toy造数据，可以直接&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git clone https://github.com/daiwk/seq2seq.git&lt;/code&gt;，然后运行dwk_train.sh即可。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>seq2seq</title>
   <link href="http://daiwk.github.io/posts/dl-seq2seq.html"/>
   <updated>2017-04-16T00:00:00+00:00</updated>
   <id>/posts/dl-seq2seq</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-基础知识&quot;&gt;1. 基础知识&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-one-hot词向量&quot;&gt;1.1 one-hot词向量&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-句子的表示rnn&quot;&gt;1.2 句子的表示——rnn&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考： gitchat:深度学习第三课：文本生成&lt;/p&gt;

&lt;h2 id=&quot;1-基础知识&quot;&gt;1. 基础知识&lt;/h2&gt;

&lt;h3 id=&quot;11-one-hot词向量&quot;&gt;1.1 one-hot词向量&lt;/h3&gt;

&lt;p&gt;需要正确区分one-hot表示与词向量&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;one-hot表示：n维向量，每个维度只能有&lt;strong&gt;{0,1}的取值&lt;/strong&gt;，只能编码2^n种信息&lt;/li&gt;
  &lt;li&gt;词向量表示：是一种&lt;strong&gt;distributed representation&lt;/strong&gt;，即&lt;strong&gt;词嵌入（word embedding）&lt;/strong&gt;。用&lt;strong&gt;更低维度&lt;/strong&gt;的向量表示，每个维度的取值是&lt;strong&gt;实数&lt;/strong&gt;。可以对抗&lt;strong&gt;维度灾难&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;12-句子的表示rnn&quot;&gt;1.2 句子的表示——rnn&lt;/h3&gt;

&lt;p&gt;可以证明，&lt;strong&gt;rnn是图灵完备的&lt;/strong&gt;。rnn的公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\h_t=f_h(x_t,h_{t-1})=\phi_h(W^Th_{t-1}+U^Tx_t)
\\y_t=f_O(h_t)=\phi_O(V_Th_t)
\]&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>虚拟环境中类似人类语言习得的深度组合性框架（A Deep Compositional Framework for Human-like Language Acquisition in Virtual Environment）</title>
   <link href="http://daiwk.github.io/posts/rl-A-Deep-Compositional-Framework-for-Human-like-Language-Acquisition-in-Virtual-Environment.html"/>
   <updated>2017-04-06T00:00:00+00:00</updated>
   <id>/posts/rl-A-Deep-Compositional-Framework-for-Human-like-Language-Acquisition-in-Virtual-Environment</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;中文翻译：&lt;a href=&quot;https://zhuanlan.zhihu.com/p/26120297&quot;&gt;知乎专栏：百度通用 AI 大突破，智能体通过交互式学习实现举一反三&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;../assets/A Deep Compositional Framework for Human-like Language Acquisition in Virtual Environment.pdf&quot;&gt;paper地址&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>tf step by step: chap 2. 对比</title>
   <link href="http://daiwk.github.io/posts/platform-tf-step-by-step-2-contrasts.html"/>
   <updated>2017-04-04T00:00:00+00:00</updated>
   <id>/posts/platform-tf-step-by-step-2-contrasts</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-tensorflow&quot;&gt;1. tensorflow&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#x-现有几个平台的对比&quot;&gt;x. 现有几个平台的对比&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#使用构成&quot;&gt;使用构成&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#分布式实现&quot;&gt;分布式实现&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;– 整理自《tensorflow实战》 chap 2&lt;/p&gt;

&lt;h2 id=&quot;1-tensorflow&quot;&gt;1. tensorflow&lt;/h2&gt;

&lt;p&gt;和Theano一样支持自动求导，用户不用通过bp求梯度。和Caffe一样，核心代码是用C++写的，简化上线部署的复杂度（手机这种内存&amp;amp;cpu都紧张的嵌入式设备，也可以直接用C++接口运行复杂模型）。tf还通过swig，提供了官方的py/go/java接口。&lt;/p&gt;

&lt;p&gt;使用python时，有一个影响效率的问题：每个mini-batch要从python中feed到网络中，&lt;/p&gt;

&lt;h2 id=&quot;x-现有几个平台的对比&quot;&gt;x. 现有几个平台的对比&lt;/h2&gt;

&lt;p&gt;感谢xxx同学提供~~&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://chenrudan.github.io/blog/2017/02/25/comparetfmxpd.html&quot;&gt;https://chenrudan.github.io/blog/2017/02/25/comparetfmxpd.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;使用构成&quot;&gt;使用构成&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;tf&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TensorFlow想做的是人工智能的算法引擎，它不仅提供了深度学习的基本元件例如卷积、pooling、lstm等，提供很多基本计算操作，在这一年还围绕着算法开发推出了TensorFlow Serving用于将算法动态部署到线上[6]、想取代scikit-learn的tf.contrib.learn[7]、将不同尺寸的输入处理成相同规模用于批处理的TensorFlow Fold[8]、在移动平台上跑算法[9]、支持Java/Go语言的接口、分布式实例等等。这些都可以看出TensorFlow在不断扩张版图，它不只是一个框架提供一些API供用户调用，也同时在围绕着算法推出各种配套服务。也许由于TensorFlow的扩张，做优化的人不够多，导致现在运行效率就算是分布式版本都比其他框架都要慢[4]，而且版本间有时候函数接口还老不兼容。最新的1.0的版本说Inception v3神经网络的训练速度提升了58倍[10]，个人觉得tf底层实现不会大改，如果要提速可能需要针对各个网络单独设计优化版本。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/compare-tf.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;mxnet&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MXNet也是将算法表达成了有向计算图，将数据和计算表达成有向图中的节点，与TensorFlow不同的是，MXNet将计算图中每一个节点，包括数据节点variable、基本计算floor、神经网络操作pooling都封装在symbol里面，而TensorFlow将数据节点、基本计算、神经网络操作封装成了不同的类，所以它们之间流通需要通过tensor，而MXNet计算图的节点输出类型统一是symbol，通过outputs访问symbol中的NDarray数据。当构建好计算图的节点、连接方式，就通过executor来启动计算，包括计算图的前向计算输出和反向计算导数。MXNet为训练深度学习实现了Model/Module两个类，Model在executor上又封装了一层，实现了feedforward功能，将forward和backward整合在了一起，用户直接调用feedforward.fit即可完成训练、更新参数。而Module的接口好像也差不多，官网说Model只是为了提供一个接口方便训练，Module是为了更高一层的封装。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;paddle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;paddle的架构挺像caffe的，基于神经网络中的功能层来开发的，一个层包括了许多复杂的操作，例如图1中右边展开的所有操作合起来可以作为这里的一个卷积层。它将数据读取DataProvider、功能层Layers、优化方式Optimizer、训练Evaluators这几个分别实现成类，组合层构成整个网络，但是只能一层一层的累加还不够实用，为了提高灵活性，额外设置了mixed_layer用来组合不同的输入。但是这种比较粗粒度的划分就算能组合不同输入也不会像上面的灵活，比如add和conv这种操作在上面两种框架中是属于同一层面的，而在paddle中则会是conv里面包含add。看得出paddle在尽可能简化构造神经网络的过程，它甚至帮用户封装好了networks类，里面是一些可能需要的组合，例如卷积+batchNorm+pooling。它希望提供更简便的使用方式，用户不需要更改什么主体文件，直接换数据用命令行跑。&lt;/p&gt;

&lt;h3 id=&quot;分布式实现&quot;&gt;分布式实现&lt;/h3&gt;

&lt;p&gt;首先说说深度学习算法实现分布式需要干什么，分布式就是将一个参数巨多、数据居多的神经网络分成一些小任务放在多个机器多个显卡上面执行，针对这两个特性有两种解决方案，参数多就把网络切分放在不同设备上，数据量大就多台机器同时执行相同代码处理不同数据，前者称为模型并行后者称为数据并行[19]。神经网络相对其他分布式问题的特殊之处在于，不同机器上的网络参数在训练时都会独立的求导更新，然而这些参数在新的迭代开始之前要保证相对一致(由于可以异步更新，不同机器之间可以不完全一样，但肯定不能差别过大)，因此就出现了Parameter Server[20]，它保存了神经网络的权重等参数，决定了何时接收对这些数据的修改，决定了何时将修改后的数据发放到不同机器的计算节点上。假设需要训练图3中的神经网络，其中节点b和e是网络参数，machine 0和machine 1构成了模型并行，machine01和machine23构成了数据并行，中间的是参数服务器，用于收发参数。目前三个框架都说支持模型并行和数据并行，从用户实现上来看还是各有不同。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/compare-distributed-1.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;tf不同的功能以job划分，例如运行整个computation graph是一个功能，作为parameter server存储更新交换参数是一个功能。job由一系列task组成，每个task是一个进程，它完成指定的工作，计算、传输、读写等等。在tf的分布式实现中，用户要实现的分布式程序主要是两个部分，分别是graph的配置和集群的配置，client需要定义computation graph的结构，节点放在哪个job/task上，并且创建session，通过session请求graph计算到什么地方，一个client对应一个computation graph，如果采用了数据并行，那么就会产生多个graph。集群配置确定了有多少台机器，哪台机器执行哪个task。具体的代码实现参考[21]。&lt;/p&gt;

&lt;p&gt;tf没有专门实现paramter server，而是实现了server，server对象负责交换数据，但不是只交换网络的参数，只要涉及到不同设备间要交换的数据都是由server管理，例如下图中machine0的device 0和device 1之间交换网络的输入输出，因此，在实现神经网络时一般需要将网络参数放在称为ps的job中，从而在网络运行时自动的更新参数。一个task会实例化一个server对象，不同机器之间交换数据的协议有多种，例如gRPC、RDMA等。然后手动在不同的机器上分别执行程序，如下图所示。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/compare-distributed-2.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;MXNet通过kvstore实现parameter server从而实现多机运行程序，设备之间传输是通过确定数据所在的context后相互交换NDArray。从15年推出的时候就已经支持分布式了，它将网络分布式训练的过程已经封装好，用户只需要确定网络的配置，哪些操作放在哪个GPU之上，开放给用户的接口是Module类的fit函数，这个函数内部会自动创建kvstore对象，在训练的时候梯度和权重会自己push/pull。启动分布式程序也不需要自己手动在多台机器上执行命令，MXNet封装好了launch.py，传入机器个数、主机ip等就能在一台机器上启动多台机器运行程序。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/compare-distributed-3.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;PaddlePaddle的分布式结构主要有两个部分，trainer和parameter server。前者是配置网络，功能层、优化算法的python接口封装在了trainer_config_helpers类中，用户可以使用这些接口配置自己的网络，后者没有将接口放出来给用户使用，而是在c++程序中实现的，再通过python来调用c++程序。pd给的例子中通过fabric库来启动分布式程序，可以做到在一台机器上启动多台机器。在parameter server的使用上，pd针对一个网络可以拥有多个参数服务器ps，每个ps负责一部分的网络参数，与所有的trainers进行交换数据，如下图所示。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/compare-distributed-4.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

</content>
 </entry>
 
 <entry>
   <title>tf step by step: chap 1. 基础知识</title>
   <link href="http://daiwk.github.io/posts/platform-tf-step-by-step-1-basics.html"/>
   <updated>2017-04-04T00:00:00+00:00</updated>
   <id>/posts/platform-tf-step-by-step-1-basics</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-概要&quot;&gt;1. 概要&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-编程模型简介&quot;&gt;2. 编程模型简介&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-核心概念&quot;&gt;2.1 核心概念&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-实现原理&quot;&gt;2.2 实现原理&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#221-节点分配策略&quot;&gt;2.2.1 节点分配策略&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#222-通信机制&quot;&gt;2.2.2 通信机制&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#223-分布式通讯机制&quot;&gt;2.2.3 分布式通讯机制&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#23-拓展功能&quot;&gt;2.3 拓展功能&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#231-自动求导&quot;&gt;2.3.1 自动求导&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#232-单独执行子图&quot;&gt;2.3.2 单独执行子图&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#233-计算图的控制流&quot;&gt;2.3.3 计算图的控制流&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#234-自定义数据输入方式&quot;&gt;2.3.4 自定义数据输入方式&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#235-队列&quot;&gt;2.3.5 队列&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#236-容器&quot;&gt;2.3.6 容器&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#24-性能优化&quot;&gt;2.4 性能优化&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#241-数据并行&quot;&gt;2.4.1 数据并行&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#242-模型并行&quot;&gt;2.4.2 模型并行&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#243-流水线并行&quot;&gt;2.4.3 流水线并行&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;– 整理自《tensorflow实战》 chap 1&lt;/p&gt;

&lt;h2 id=&quot;1-概要&quot;&gt;1. 概要&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;2015年11月，tensorflow在github上开源。&lt;/li&gt;
  &lt;li&gt;2016年4月，发布了分布式版本。&lt;/li&gt;
  &lt;li&gt;2017年1月，发布1.0版本的预览。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;注：而google还开源了android、Chromium、Go、Javascript V8、protobuf、Bazel(编译工具)、Tesseract(OCR工具)……&lt;/p&gt;

&lt;p&gt;2011年，Google启动了Google Brain，同时搞了第一代分布式机器学习框架DistBelief，约50个项目在用。之后搞了tensorlfow，将Inception Net从DistBelief迁移到tf后，有6倍的训练速度提升。截至2016年，Google内近2k个项目用了tf。&lt;/p&gt;

&lt;p&gt;tf的一些特点：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;使用tf，不需要给大规模的模型训练和小规模的应用分别开发部署不同的系统，&lt;strong&gt;避免了同时维护两套程序的成本。&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;tf的计算可以表示为&lt;strong&gt;有状态的数据流式图&lt;/strong&gt;，对于大规模的神经网络训练，可以简单地实现并行计算，同时使用&lt;strong&gt;不同的硬件资源&lt;/strong&gt;进行训练，&lt;strong&gt;同步或异步&lt;/strong&gt;地更新&lt;strong&gt;全局共享&lt;/strong&gt;的模型&lt;strong&gt;参数和状态&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;串行改并行&lt;/strong&gt;的改造成本非常低。&lt;/li&gt;
  &lt;li&gt;前端支持py、cxx、go、java等语言，后端使用cxx、cuda。&lt;/li&gt;
  &lt;li&gt;除了ml/dl，tf抽象的数据流式图也可以应用在&lt;strong&gt;通用数值计算和符号计算&lt;/strong&gt;上（如分形图计算或者偏微分方程数值求解）&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-编程模型简介&quot;&gt;2. 编程模型简介&lt;/h2&gt;

&lt;h3 id=&quot;21-核心概念&quot;&gt;2.1 核心概念&lt;/h3&gt;

&lt;p&gt;tf中的计算可以表示为有向图（directed graph）或计算图（computation graph）。&lt;/p&gt;

&lt;p&gt;每个运算操作是一个节点，节点间的连接是边。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;每个节点可以有任意多的输入和输出&lt;/strong&gt;，节点可以算作运算操作的实例化（instance）。在边中流动的数据称为张量（tensor）。&lt;strong&gt;tensor的数据类型，可以一开始定义好，也可以通过计算图的结构推断得到。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;没有数据流动&lt;/strong&gt;的特殊的边：依赖控制（control dependencies），作用是起始节点执行完再执行目标节点，例如控制内存使用的最高峰值。&lt;/p&gt;

&lt;p&gt;示例：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/computation graph.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;运算核（kernel）是一个运算操作中某个具体硬件（cpu/gpu…）中的实现。tf中，可以通过&lt;strong&gt;注册机制&lt;/strong&gt;，加入新的运算或运算核。tf的内建运算操作如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;标量运算：Add/Sub/Mul/Div/Exp/Log/Greater/Less/Equal&lt;/li&gt;
  &lt;li&gt;向量运算：Concat/Slice/Split/Constant/Rank/Shape/Shuffle&lt;/li&gt;
  &lt;li&gt;矩阵运算：MatMul/MatrixInverse/MatrixDeterminant&lt;/li&gt;
  &lt;li&gt;带状态的运算：Variable/Assign/AssignAdd&lt;/li&gt;
  &lt;li&gt;神经网络组件：SoftMax/Sigmoid/ReLU/Convolution2D/MaxPooling&lt;/li&gt;
  &lt;li&gt;存储、恢复：Save/Restore&lt;/li&gt;
  &lt;li&gt;队列及同步运算：Enqueue/Dequeue/MutexAcquire/MutexRelease&lt;/li&gt;
  &lt;li&gt;控制流：Merge/Switch/Enter/Leave/NextIteration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;session是用户使用tf时的交互式接口，可以通过Session的&lt;strong&gt;extend方法添加新的节点和边&lt;/strong&gt;，然后run。&lt;/p&gt;

&lt;p&gt;大多数运算中，计算图会被反复执行多次，数据（即tensor）并不会被持续保留。&lt;strong&gt;但Variable是特殊的运算操作，可以将一些需要保留的tensor存储在内存或显存中，同时可以被更新，如模型参数。&lt;/strong&gt;Assign/AssignAdd/AssignMul都是Variable的特殊操作。&lt;/p&gt;

&lt;h3 id=&quot;22-实现原理&quot;&gt;2.2 实现原理&lt;/h3&gt;

&lt;p&gt;client通过session的接口和master以及多个worker相连。而每个worker可以和多个硬件设备相连。master可以指导所有worker按流程执行计算图。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;单机：client、master、worker在同一台机器的同一个进程中&lt;/li&gt;
  &lt;li&gt;分布式：client、master、worker在不同机器的不同进程中，由集群调度系统统一管理各任务。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;一个worker可以管理多个设备&lt;/strong&gt;，&lt;strong&gt;每个设备&lt;/strong&gt;的name包含&lt;strong&gt;硬件类别、编号、任务号&lt;/strong&gt;（单机没有）：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;单机：/job:localhost/device:cpu:0&lt;/li&gt;
  &lt;li&gt;分布式：/job:worker/task:17/device:gpu:3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;tf为cpu和gpu提供了管理设备的对象接口，每一个对象负责&lt;strong&gt;分配、释放设备的内存&lt;/strong&gt;，以及&lt;strong&gt;执行节点的运算核。&lt;/strong&gt;每个设备有单独的allocator负责存储各种数据类型的tensor，同时tensor的引用次数也会被记录，&lt;strong&gt;引用次数为0时，内存会被释放。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;如果只有一个设备，计算图会按照依赖关系被顺序执行。当一个节点所有上游依赖都被执行完时（依赖数位0），这个节点就会被加入ready queue以等待执行。同时，下游所有节点的依赖数减1（标准的计算拓扑序的方式）。而对于多设备，有以下2个难点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;问题1：每一个节点该让&lt;strong&gt;什么硬件设备&lt;/strong&gt;执行&lt;/li&gt;
  &lt;li&gt;问题2：如何管理节点间的&lt;strong&gt;数据通信&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;221-节点分配策略&quot;&gt;2.2.1 节点分配策略&lt;/h4&gt;

&lt;p&gt;针对问题1，tf有一个为节点分配设备的策略。&lt;/p&gt;

&lt;p&gt;首先需要计算一个&lt;strong&gt;代价模型，估算每个节点的输入、输出tensor的大小，以及所需计算时间。&lt;/strong&gt;此代价模型一部分由人工经验指定的启发式规则得到，另一部分由对一小部分数据进行实际运算测量得到。&lt;/p&gt;

&lt;p&gt;接下来，分配策略会模拟执行整个计算图，从起点开始，按拓扑序执行。在模拟执行某节点时，会将能执行此节点的所有设备都测试一遍，考虑代价模型对这个节点的计算时间的估算，加上数据传输到这个节点的通信时间，最后选择综合时间最短的设备作为执行设备（贪婪策略，不能保证全局最优）。&lt;/p&gt;

&lt;p&gt;考虑的因素除了时间之外，还有内存的最高使用峰值。也允许用户对节点分配设置限制条件，例如，“只给此节点分配gpu设备”，“只给此节点分配/job:worker/task:17上的设备”，“此节点分配的设备必须和variable3一致”。tf会先计算每个节点可用的设备，然后用&lt;strong&gt;&lt;a href=&quot;http://blog.csdn.net/dm_vincent/article/details/7655764&quot;&gt;并查集（union-find）&lt;/a&gt;&lt;/strong&gt;找到必须使用同一设备的节点。&lt;/p&gt;

&lt;h4 id=&quot;222-通信机制&quot;&gt;2.2.2 通信机制&lt;/h4&gt;

&lt;p&gt;针对问题2， 就是所谓的通信机制了。&lt;/p&gt;

&lt;p&gt;当节点分配设备的方案确定了，整个计算图就会被划分为多个子图，使用同一设备并相邻的节点会被划分到同一子图。然后图中的边，会被替换成一个发送节点（send node）和一个接收节点（receive node），以及一条从发送节点到接收节点的边。两个子图间可能有多个接收节点，如果这些接收节点接收的是同一个tensor，那么所有这些接收节点会被合成同一个节点。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/tf communication mechanism.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;这样的通讯机制可以转化为发送节点和接收节点的实现问题，用户无须设计节点间的通信流程，可以用同一套代码自动扩展到不同硬件环境并处理复杂的通信流程。下图就是cpu和gpu间的通讯。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/cpu gpu communication.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;代码层面，从单机单设备到单机多设备的修改，只要一行，就可以实现单gpu到多gpu的修改。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/code 1 gpu to multi gpu.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;223-分布式通讯机制&quot;&gt;2.2.3 分布式通讯机制&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;发送节点和接收节点与单机的实现不同：变为不同机器间使用TCP或RDMA（Remote Direct Memory Access,不需要cpu的参与直接把数据复制到远程机器的内存指定地址的操作。）。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;容错方面，故障会在两种情况下被检测，一种是信息从发送节点传输到接收节点失败时；另一种是周期性的worker心跳检测失败时。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;故障恢复：当故障被检测到时，整个计算图会被终止并重启。Variable node可以被持久化，tf支持检查点（checkpoint）的保存和恢复，每个Variable node都会链接到一个Save node，每隔几轮迭代就会保存一次数据到持久化的存储系统（例如分布式文件系统）。同样地，每个Variable node都会链接一个Restore node，每次重启时，都会被调用并恢复数据。所以，发生故障并重启后，模型参数将得到保留，训练可以从上一个checkpoint恢复而不需要从头开始。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另，GPU集群和单GPU的加速比变化如图。少于16卡时，基本没性能损耗。50卡时，加速比40。100卡时，加速比达到56。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/gpu cluster accelerate.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h3 id=&quot;23-拓展功能&quot;&gt;2.3 拓展功能&lt;/h3&gt;

&lt;h4 id=&quot;231-自动求导&quot;&gt;2.3.1 自动求导&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;优势：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;计算cost function的梯度是最基本需求，所以tf原生地支持&lt;strong&gt;自动求导&lt;/strong&gt;。例如，tensor &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;在计算图中有一组依赖的tensor&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\({X_k}\)&lt;/code&gt;，那么，tf中可以自动求出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(dC/dX_k\)&lt;/code&gt;。此过程是通过在计算图中拓展节点的方式实现的，不过求梯度的节点对用户透明。&lt;/p&gt;

&lt;p&gt;如图所示，计算tensor &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;关于tensor &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(I\)&lt;/code&gt;的梯度时，会从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;回溯到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(I\)&lt;/code&gt;，对回溯路径上的每个节点添加一个对应的求解梯度的节点，并依据链式法则计算总的梯度（BP）。这些新增的节点会计算梯度函数（gradient function），例如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\([db,dW,dx]=tf.gradients(C, [b, W, x])\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/gradient calculation.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;问题：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;自动求导带来的问题：tf对计算的优化（例如为节点分配设备的策略）会变得很麻烦，尤其是内存使用问题。正向执行计算图时，因为执行顺序是确定的，所以使用经验规则是较容易取得好效果的，tensor在产生后很快会被后续节点使用，不会持续占用内存。但反向传播时，往往要用到计算图开头的tensor,这些tensor可能会占用大量GPU显存，因此限制了模型的规模。&lt;/p&gt;

&lt;h4 id=&quot;232-单独执行子图&quot;&gt;2.3.2 单独执行子图&lt;/h4&gt;

&lt;p&gt;用户可以选择计算图的任意子图，沿某些边输入数据，从另一些边获取结果。tf用&lt;strong&gt;节点名+port&lt;/strong&gt;的形式指定数据，例如，a:0表示a节点的第一个输出。用户&lt;strong&gt;可以&lt;/strong&gt;选择一组输入数据的映射，如name:port-&amp;gt;tensor；用户&lt;strong&gt;必须&lt;/strong&gt;指定一组输出数据，例如name[:port]。整个计算图会依据输入输出自动调整，输入节点会连接一个feed node，输出节点会连接一个fetch node，然后tf会依据输出数据自动推导哪些节点需要被执行。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/subgraph execution.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;233-计算图的控制流&quot;&gt;2.3.3 计算图的控制流&lt;/h4&gt;

&lt;p&gt;因为ml需要反复迭代，所以if-condition/while-loop之类控制流是很重要的。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Switch和Merge的operator：根据某个布尔值跳过某段子图，然后把某段子图的结果合并，实现if-else。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Enter、Leave和NextIteration：实现循环和迭代。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Loop中的每一次循环会有唯一的tag，其执行结果会输出成frame，可以方便地查询结果日志。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;控制流支持分布式。循环内的节点会被划分到不同的小的子图上，每个子图会连接控制节点，实现自己的循环，同时将循环终止等信号发送给其他子图。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;控制流支持对计算图中隐含的梯度计算节点的支持&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;使用高级语言（如python）的if-else, while, for时，会被自动编译成operator。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;234-自定义数据输入方式&quot;&gt;2.3.4 自定义数据输入方式&lt;/h4&gt;

&lt;p&gt;数据输入可以通过feed node, 也有特殊的input node，可以让用户直接输入文件系统的路径。&lt;/p&gt;

&lt;p&gt;若从feed node输入数据，则数据必须从client读取，通过网络传到分布式系统的其他节点，有较大网络开销。直接用文件路径，可以让worker节点读取本地文件，提高效率。&lt;/p&gt;

&lt;h4 id=&quot;235-队列&quot;&gt;2.3.5 队列&lt;/h4&gt;

&lt;p&gt;队列可以让计算图的&lt;strong&gt;不同节点异步地执行&lt;/strong&gt;。使用队列的目的：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;当一个batch的数据运算时，提前从磁盘读取下一个batch的数据，减少磁盘I/O阻塞时间。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;另外，还可以异步地计算许多梯度，再组合成一个更复杂的梯度。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;除了传统的FIFO（先进先出）队列，tf还实现了洗牌队列(shuffling queue)，满足ml算法对随机性的要求，对于损失函数优化和模型收敛有帮助。&lt;/p&gt;

&lt;h4 id=&quot;236-容器&quot;&gt;2.3.6 容器&lt;/h4&gt;

&lt;p&gt;容器（container）是一种特殊的管理&lt;strong&gt;长期变量&lt;/strong&gt;的机制。例如，Variable对象就存储在容器中。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;每个进程会有一个默认的容器一直存在，直到进程结束。&lt;/strong&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;使用容器可以允许不同计算图的不同Session间共享一些状态值。&lt;/strong&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;24-性能优化&quot;&gt;2.4 性能优化&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;避免冗余计算：tf中有许多高度抽象的运算操作，可能是由&lt;strong&gt;多层&lt;/strong&gt;的复杂计算组合而成的。当有多个高阶运算操作同时存在时，它们的前几层可能是完全一致的重复计算（输入&amp;amp;运算完全一样），tf会自动识别这些重复计算，同时&lt;strong&gt;改写计算图&lt;/strong&gt;，只执行一次重复计算。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;巧妙安排运算顺序：可以极大改善数据传输和内存占用问题。例如，适当调整顺序以错开某些数据同时存在于内存的时间，对显存较小的GPU尤其重要。tf也会精细地安排接收节点的执行时间，若节点过早地接收数据，数据会堆积在设备内存中，因此，tf有策略&lt;strong&gt;让接收节点刚好需要数据来计算时才开始接收数据。&lt;/strong&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;支持异步计算：线程无须一直等待某个节点计算完成。有些节点，如receive/enqueue/dequeue就是异步实现，不必等待I/O而阻塞一个线程继续执行其他任务。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;支持几种高度优化的第三方计算库：
    &lt;ul&gt;
      &lt;li&gt;线性代数：eign&lt;/li&gt;
      &lt;li&gt;矩阵乘法：BLAS/cuBLAS&lt;/li&gt;
      &lt;li&gt;深度学习：cuda-convnet/cuDNN&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;支持数据压缩：许多算法在数字精度较低时仍可以正常工作。例如，将32bit浮点数有损地压缩成16bit的浮点数，可以降低在不同设备之前传输数据时的网络开销。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;三种不同的加速神经网络训练的并行计算模式：&lt;/p&gt;

&lt;h4 id=&quot;241-数据并行&quot;&gt;2.4.1 数据并行&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;将一个mini-batch的数据放在不同设备上计算&lt;/strong&gt;，实现梯度计算的并行化。例如，将&lt;strong&gt;1000条样本的mini-batch再拆成10个100样本的数据&lt;/strong&gt;并行计算，完成后将10份梯度数据合并得到最终梯度并更新到参数服务器（parameter server）。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/data parallel.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;这样的操作&lt;strong&gt;会产生许多完全一样的子图的副本&lt;/strong&gt;，在client上可以用一个线程同步控制这些副本的循环。当然，这个操作也可以改成异步的，多个线程控制梯度计算，每个线程计算完成后异步地更新模型参数。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;同步的方式，相当于用了一个较大的mini-batch（&lt;strong&gt;其中的batch normalization还是独立的&lt;/strong&gt;）。优点是没有梯度干扰，缺点是容错性差。一台机器出现问题后，可能需要重跑。&lt;/li&gt;
  &lt;li&gt;异步的方式，优点是有一定的容错性，但缺点是因为梯度干扰，每一组梯度的利用效率都下降了&lt;/li&gt;
  &lt;li&gt;混合式，例如两组异步的，其中每组有50份同步的训练。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;一般而言，同步训练的模型精度(纵轴)较好：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/data parallel performance.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;10块gpu和50块gpu训练Inception的对比，可以发现，达到相同精度时，50块gpu需要的时间是10块的1/4左右。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/data parallel performance for Inception.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;相比模型并行，数据并行的计算性能损耗非常小，尤其对于sparse的model。因为不同mini-batch间干扰的概率非常小，所以经常可以同时进行很多份（replicas）数据并行，甚至高达上千份。&lt;/p&gt;

&lt;h4 id=&quot;242-模型并行&quot;&gt;2.4.2 模型并行&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;将计算图的不同部分放在不同设备上运行，目标在于减少每一轮训练迭代的时间。模型并行本身要求模型有大量可以并行且互相不依赖或依赖程序不高的子图。&lt;/strong&gt;在不同硬件环境上损耗不同：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;单核CPU上使用SIMD没有额外开销。&lt;/li&gt;
  &lt;li&gt;多核CPU上使用多线程也基本没有额外开销。&lt;/li&gt;
  &lt;li&gt;多GPU上限制主要在PCIe的带宽。&lt;/li&gt;
  &lt;li&gt;多机间的限制主要在网络开销。&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/model parallel.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;h4 id=&quot;243-流水线并行&quot;&gt;2.4.3 流水线并行&lt;/h4&gt;

&lt;p&gt;和异步的数据并行很像，只不过是&lt;strong&gt;在同一个硬件设备上实现并行&lt;/strong&gt;，大致思路是将计算做成流水线，在一个设备上连续并行，提高设备利用率。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tf step by step/chap1/pipeline parallel.png&quot; style=&quot;max-height: 350px;max-width:500px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;未来，有如下规划：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;把任意子图独立出来，封装成一个函数，让不同的前端语言（python/c++）调用。从而可以将设计好的子图发布在社区中，便于分享。&lt;/li&gt;
  &lt;li&gt;推出优化计算图执行的(just-in-time编译器)[http://blog.sina.com.cn/s/blog_742eb90201015a2g.html]（目前在1.0.0-rc0中已有试验性的XLA组件，可以提供JIT及AOT编译优化），期望可以自动推断出tensor的类型、大小，并自动生成一条高度优化过的流水线。
＋ 持续优化为运算节点分配硬件设备的策略，以及节点执行排序的策略。&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>加速机器学习算法</title>
   <link href="http://daiwk.github.io/posts/platform-speed-up-ml-algorithms.html"/>
   <updated>2017-03-29T00:00:00+00:00</updated>
   <id>/posts/platform-speed-up-ml-algorithms</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650724906&amp;amp;idx=2&amp;amp;sn=7a91a2e48828c6ae75d9beddd02ffbe5&amp;amp;chksm=871b1e54b06c9742aa897e1f46abd07ddc84037f4d628fdceff53cd35c0d1bddc2a017e44b21&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0328ox36ifqqlF9JL8wTkp5b&amp;amp;pass_ticket=Htlvu54Fy3u%2F7%2BrIyF5OA2H235rnZvGZ9TN12axFlz1gzulu4QPYwr2E5IM3920U#rd&quot;&gt;演讲 | 亚马逊机器学习总监Alex Smola：为什么你的机器学习代码运行速度慢&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>搜索相关性、知识图谱和DuerOS</title>
   <link href="http://daiwk.github.io/posts/nlp-dnn_ps_and_dueros.html"/>
   <updated>2017-03-29T00:00:00+00:00</updated>
   <id>/posts/nlp-dnn_ps_and_dueros</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#搜索相关性&quot;&gt;搜索相关性&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#知识图谱&quot;&gt;知识图谱&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#dueros&quot;&gt;DuerOS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2651995452&amp;amp;idx=2&amp;amp;sn=c580e8adb8bd30d13cf918babdd994af&amp;amp;chksm=f12149cdc656c0db351dbceab737adb5e5eec027a9d722031d196810bb1b8dec10ea3642ebee&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0329BlcIHBOnMVgpnJ6H5aXI&amp;amp;pass_ticket=Htlvu54Fy3u%2F7%2BrIyF5OA2H235rnZvGZ9TN12axFlz1gzulu4QPYwr2E5IM3920U#rd&quot;&gt;《新智元：【独家】百度朱凯华：智能搜索和对话式OS最新技术全面解读（65PPT）》&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;搜索相关性&quot;&gt;搜索相关性&lt;/h2&gt;

&lt;h2 id=&quot;知识图谱&quot;&gt;知识图谱&lt;/h2&gt;

&lt;h2 id=&quot;dueros&quot;&gt;DuerOS&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>lstm</title>
   <link href="http://daiwk.github.io/posts/dl-lstm.html"/>
   <updated>2017-03-29T00:00:00+00:00</updated>
   <id>/posts/dl-lstm</id>
   <content type="html">&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#lstm-的优势&quot;&gt;LSTM 的优势&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考： &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650724769&amp;amp;idx=1&amp;amp;sn=796676a27891f0ed5c5eac30ac1ac3a3&amp;amp;chksm=871b1ddfb06c94c9d853dfcdcedceeb7ca337972032da7f2db7bee12056e663dfda84b2e7270&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0326w3RMbGfNfIGCmwpXxabV&amp;amp;pass_ticket=Htlvu54Fy3u%2F7%2BrIyF5OA2H235rnZvGZ9TN12axFlz1gzulu4QPYwr2E5IM3920U#rd&quot;&gt;lstm之父Jürgen Schmidhuber&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;lstm-的优势&quot;&gt;LSTM 的优势&lt;/h2&gt;

&lt;p&gt;LSTM 是 Sepp Hochreiter 和 Jürgen Schmidhuber 在上世纪九十年代中期提出的，它可以解决梯度消失的问题，这一问题是影响 RNN 表现的重大障碍。&lt;/p&gt;

&lt;p&gt;LSTM 还可保留误差，用于沿时间和层进行反向传递。LSTM 将误差保持在更为恒定的水平，让 RNN 能够进行许多个时间步的学习（超过 1000 个时间步），从而打开了建立远距离因果联系的通道。&lt;/p&gt;

&lt;p&gt;LSTM 将信息存放在递归网络正常信息流之外的门控单元中。这些单元可以存储、写入或读取信息，就像计算机内存中的数据一样。单元通过门的开关判定存储哪些信息，以及何时允许读取、写入或清除信息。但与计算机中的数字式存储器不同的是，这些门是模拟的，包含输出范围全部在 0～１之间的 sigmoid 函数的逐元素相乘操作。相比数字式存储，模拟值的优点是可微分，因此适合反向传播。&lt;/p&gt;

&lt;p&gt;这些门依据接收到的信号而开关，而且与神经网络的节点类似，它们会用自有的权重集对信息进行筛选，根据其强度和导入内容决定是否允许信息通过。这些权重就像调制输入和隐藏状态的权重一样，会通过递归网络的学习过程进行调整。也就是说，记忆单元会通过猜测、误差反向传播、用梯度下降调整权重的迭代过程学习何时允许数据进入、离开或被删除。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/lstm-detail.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;首先，最底部的三个箭头表示信息从多个点流入记忆单元。当前输入与过去的单元状态不只被送入记忆单元本身，同时也进入单元的三个门，而这些门将决定如何处理输入。&lt;/p&gt;

&lt;p&gt;图中的黑点即是门，分别决定何时允许新输入进入，何时清除当前的单元状态，以及/或何时让单元状态对当前时间步的网络输出产生影响。S_c 是记忆单元的当前状态，而 g_y_in 是当前的输入。记住，每个门都可开可关，而且门在每个时间步都会重新组合开关状态。记忆单元在每个时间步都可以决定是否遗忘其状态，是否允许写入，是否允许读取，相应的信息流如图所示。&lt;/p&gt;

&lt;p&gt;图中较大的黑体字母即是每项操作的结果。&lt;/p&gt;

&lt;p&gt;下面是另一张图，将简单 RNN（左）与 LSTM 单元（右）进行对比。蓝线可忽略；图例有助理解。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/lstm-rnn.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;应当注意的是，LSTM 的记忆单元在输入转换中给予加法和乘法不同的角色。两张图中央的加号其实就是 LSTM 的秘密。虽然看起来异常简单，这一基本的改变能帮助 LSTM 在必须进行深度反向传播时维持恒定的误差。LSTM 确定后续单元状态的方式并非将当前状态与新输入相乘，而是将两者相加，这正是 LSTM 的特别之处。（当然，遗忘门依旧使用乘法。）&lt;/p&gt;

&lt;p&gt;不同的权重集对输入信息进行筛选，决定是否输入、输出或遗忘。遗忘门的形式是一个线性恒等函数，因为如果门打开，则记忆单元的当前状态就只会与 1 相乘，正向传播一个时间步。&lt;/p&gt;

&lt;p&gt;此外，讲到简单的窍门，将每个 LSTM 单元遗忘门的偏差设定为 1，经证明可以提升网络表现。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;关于梯度消失的解决，可以参考&lt;a href=&quot;https://www.zhihu.com/question/34878706&quot;&gt;知乎：LSTM如何来避免梯度弥散和梯度爆炸？&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;An Empirical Exploration of Recurrent Network Architectures&lt;/li&gt;
  &lt;li&gt;Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外，梯度爆炸问题，lstm无法解决，需要通过梯度cliping来解决(On the difficulty of training Recurrent Neural Networks)&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>tf的im2txt</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-im2txt.html"/>
   <updated>2017-03-22T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-im2txt</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-必需的包&quot;&gt;0. 必需的包&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-数据集准备&quot;&gt;1. 数据集准备&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-下载inception-v3-checkpoint&quot;&gt;2. 下载Inception v3 Checkpoint&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-训练&quot;&gt;3. 训练&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://github.com/tensorflow/models/tree/master/im2txt&quot;&gt;https://github.com/tensorflow/models/tree/master/im2txt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文：
“Show and Tell: Lessons learned from the 2015 MSCOCO Image Captioning Challenge.”, Oriol Vinyals, Alexander Toshev, Samy Bengio, Dumitru Erhan., IEEE transactions on pattern analysis and machine intelligence (2016).
&lt;a href=&quot;http://arxiv.org/abs/1609.06647&quot;&gt;http://arxiv.org/abs/1609.06647&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;0-必需的包&quot;&gt;0. 必需的包&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;bazel(&lt;a href=&quot;https://bazel.build/versions/master/docs/install.html&quot;&gt;官网&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;tf&lt;/li&gt;
  &lt;li&gt;numpy&lt;/li&gt;
  &lt;li&gt;nltk(下载数据可以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python -m nltk.downloader -d /home/data/docker_share/nltk_data all&lt;/code&gt;指定存放目录[约12G])&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-数据集准备&quot;&gt;1. 数据集准备&lt;/h2&gt;

&lt;p&gt;输入数据格式是“native TFRecord format”：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;The TFRecord format consists of a set of sharded files containing serialized tf.SequenceExample protocol buffers. Each tf.SequenceExample proto contains an image (JPEG format), a caption and metadata such as the image id.

Each caption is a list of words. During preprocessing, a dictionary is created that assigns each word in the vocabulary to an integer-valued id. Each caption is encoded as a list of integer word ids in the tf.SequenceExample protos.

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用mscoco数据集。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## Make sure there is at least 150G space available!!!!!!!!!!!!!!!

# Location to save the MSCOCO data. 

function prepare()
{
# Build the preprocessing script.
bazel build im2txt/download_and_preprocess_mscoco

# Run the preprocessing script.
bazel-bin/im2txt/download_and_preprocess_mscoco &quot;${MSCOCO_DIR}&quot;

return $?

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当最后一句话是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Finished processing all 20267 image-caption pairs in data set 'test'.&lt;/code&gt;时，就成功了。&lt;/p&gt;

&lt;p&gt;最终数据：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;256个训练文件：train-?????-of-00256&lt;/li&gt;
  &lt;li&gt;4个验证文件：val-?????-of-00004&lt;/li&gt;
  &lt;li&gt;8个测试文件：test-?????-of-00008&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-下载inception-v3-checkpoint&quot;&gt;2. 下载Inception v3 Checkpoint&lt;/h2&gt;

&lt;p&gt;使用inception v3来初始化img部分的权重。tf专门搞了个&lt;strong&gt;slim&lt;/strong&gt;来存这些预训练好的模型（&lt;a href=&quot;https://github.com/tensorflow/models/tree/master/slim#tensorflow-slim-image-classification-library&quot;&gt;https://github.com/tensorflow/models/tree/master/slim#tensorflow-slim-image-classification-library&lt;/a&gt;）。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function get_inception()
{
# Location to save the Inception v3 checkpoint.
export INCEPTION_DIR=&quot;${HOME}/im2txt/data&quot;
mkdir -p ${INCEPTION_DIR}

wget &quot;http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz&quot;
tar -xvf &quot;inception_v3_2016_08_28.tar.gz&quot; -C ${INCEPTION_DIR}
rm &quot;inception_v3_2016_08_28.tar.gz&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意：这里的inception v3只用于&lt;strong&gt;第一步&lt;/strong&gt;的模型初始化，后面整个模型的训练过程中，会有新的checkpoint，这个inception v3就没啥用了。&lt;/p&gt;

&lt;h2 id=&quot;3-训练&quot;&gt;3. 训练&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function train() 
{

# Inception v3 checkpoint file.
INCEPTION_CHECKPOINT=&quot;${HOME}/im2txt/data/inception_v3.ckpt&quot;

# Directory to save the model.
MODEL_DIR=&quot;${HOME}/im2txt/model&quot;

# Build the model.
bazel build -c opt im2txt/...

# Run the training script.
bazel-bin/im2txt/train \
  --input_file_pattern=&quot;${MSCOCO_DIR}/train-?????-of-00256&quot; \
  --inception_checkpoint_file=&quot;${INCEPTION_CHECKPOINT}&quot; \
  --train_dir=&quot;${MODEL_DIR}/train&quot; \
  --train_inception=false \
  --number_of_steps=1000000

return $?
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>image-qa</title>
   <link href="http://daiwk.github.io/posts/image-visual-qa.html"/>
   <updated>2017-03-13T00:00:00+00:00</updated>
   <id>/posts/image-visual-qa</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;2017年vqa challenge冠军：
&lt;a href=&quot;https://mp.weixin.qq.com/s/BQy3qpmQqeQquhf92hwcvQ&quot;&gt;2017 VQA Challenge 第一名技术报告&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考文献：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Yang et al. (2015), Stacked Attention Networks for Image Question Answering&lt;/li&gt;
  &lt;li&gt;Ren et al. (2015), Exploring Models and Data for Image Question Answering&lt;/li&gt;
  &lt;li&gt;Goyal et al. (2016), Making the V in VQA Matter: Elevating the Role of Image Understanding in Visual Question Answering.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;博客：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://avisingh599.github.io/deeplearning/visual-qa/&quot;&gt;https://avisingh599.github.io/deeplearning/visual-qa/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://github.com/avisingh599/visual-qa&quot;&gt;http://github.com/avisingh599/visual-qa&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>雅虎开源tensorflow on spark</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-on-spark.html"/>
   <updated>2017-02-17T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-on-spark</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650723201&amp;amp;idx=2&amp;amp;sn=81dd9fbd5f00b1d17437c17a2e14f8c9&amp;amp;chksm=871b17ffb06c9ee92edc24c76eb32c05173ce2831c4109fb779293fdec9de146bf5e34fadd6d&amp;amp;scene=0&amp;amp;pass_ticket=cRg%2Fd4VBk6Un%2BRmxRlIcTdkS45x%2BlrSXeEh%2BFaF%2FTqiBl%2Ba0V%2BvF404WLkESRtTu#rd&quot;&gt;机器知心文章：&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;TensorFlowOnSpark 开源地址： &lt;a href=&quot;github.com/yahoo/TensorFlowOnSpark&quot;&gt;github.com/yahoo/TensorFlowOnSpark&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;RDMA 增强版开源地址： &lt;a href=&quot;github.com/yahoo/tensorflow/tree/yahoo&quot;&gt;github.com/yahoo/tensorflow/tree/yahoo&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;提供多示例程序（包括 MNIST，Cifar10，Inception，and VGG）以说明 TensorFlow 程序到TensorFlowOnspar转换过程，并且利用 RDMA。地址：&lt;a href=&quot;https://github.com/yahoo/TensorFlowOnSpark/tree/master/examples&quot;&gt;https://github.com/yahoo/TensorFlowOnSpark/tree/master/examples&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;提供一张亚马逊机器图像用于在 AWS EC2 上应用 TensorFlowOnSpark。接着，与 CaffeOnSpark 一样，我们会推进 TensorFlowOnSpark。地址：&lt;a href=&quot;https://github.com/yahoo/TensorFlowOnSpark/wiki/GetStarted_EC2&quot;&gt;https://github.com/yahoo/TensorFlowOnSpark/wiki/GetStarted_EC2 &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;近几年，深度学习发展的非常迅速。在雅虎，我们发现，为了从海量数据中获得洞察力，需要部署分布式深度学习。现有的深度学习框架常常要求为深度学习单独设定集群，迫使我们要为一个机器学习流程（见下图 1）创建多个程序。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/tf-on-spark-figure1.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;设定独立的集群则需要我们转移大数据集，带来了不必要的系统复杂性和端到端的学习延迟。&lt;/p&gt;

&lt;p&gt;去年我们通过开发和公开 CaffeOnSpark 解决了 scaleout 的问题，我们开源的框架支持在相同的 Spark 和 Hadoop 集群上进行分布式深度学习和大数据处理。我们在雅虎内部使用 CaffeOnSpark 改善了我们的 NSFW 图像检测，自动从实况录像中识别电竞比赛片段等等。在社区大量有价值的反馈和贡献下，CaffeOnSpark 已经得到了更新，现在可以支持 LSTM，有了一个新的数据层，可以训练与测试交错，有了一个 Python API，和 Docker container 的部署。这些都提升了我们的用户体验。但是那些使用 TensorFlow 框架的人怎么办？于是我们效仿了之前的做法，开发了 TensorFlowOnSpark。&lt;/p&gt;

&lt;p&gt;TensorFlow 公开后，谷歌于 2016 年 4 月就开放了一个带有分布式学习功能的增强版 TensorFlow。2016 年 10 月，TensorFlow 开始支持 HDFS。然而在谷歌云之外，用户仍然需要一个 TensorFlow 应用的专用集群。TensorFlow 程序无法在现有的大数据集群上部署，这样一来，那些想大规模使用这个技术的人就需要花更多的成本和时间。&lt;/p&gt;

&lt;p&gt;为了打破这个限制，几个社区项目将 TensorFlow 连接到 Spark 集群上。SparkNet 让 Spark 执行器获得了可以运行 TensorFlow 网络的能力。DataBricks 提出 tensorframe，用来使用 TensorFlow 程序操纵 Apache Spark 的数据帧。虽然这些方法都朝着正确的方向迈出了一步，但是我们检查他们的代码后发现，我们无法让多个 TensorFlow 过程直接相互沟通，我们也无法实现异步分布式学习，并且我们需要在迁移现有的 tensorflow 程序上花大功夫。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>安装h5py</title>
   <link href="http://daiwk.github.io/posts/knowledge-h5py-installation.html"/>
   <updated>2017-02-17T00:00:00+00:00</updated>
   <id>/posts/knowledge-h5py-installation</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;http://www.cnblogs.com/Ponys/p/3671458.html&quot;&gt;http://www.cnblogs.com/Ponys/p/3671458.html&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;确定系统有python，numpy，libhdf5-serial-dev，和HDF5.前三者一般都有。这里要安装HDF5&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;去HDF5官方网站下载编译好的bin（是的，尽管教程让编译，这里给用户的就是编译好的bin，搞得我这小白编译了半天）;&lt;/p&gt;

    &lt;p&gt;　　&lt;a href=&quot;http://www.hdfgroup.org/HDF5/&quot;&gt;http://www.hdfgroup.org/HDF5/&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;解压，重命名文件夹为hdf5，移动到 /usr/local/hdf5 下&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;添加环境变量:&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;　　export HDF5_DIR=/usr/local/hdf5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;　　到这里HDF5就安装好了，只有安装好的HDF5才能顺利安装h5py&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;pip install h5py&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果在度厂内部机器，如果你已经装了paddle，那么，其实只要&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jumbo install hdf5
pip install h5py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;就行了。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>openai-universe</title>
   <link href="http://daiwk.github.io/posts/platform-openai-universe.html"/>
   <updated>2017-02-16T00:00:00+00:00</updated>
   <id>/posts/platform-openai-universe</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;官网：&lt;a href=&quot;https://universe.openai.com/&quot;&gt;https://universe.openai.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;博客：&lt;a href=&quot;https://openai.com/blog/universe/&quot;&gt;https://openai.com/blog/universe/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2016 年的最后一个月，OpenAI 在 NIPS 2016 来临之际发布 Universe，一个在世界范围内的游戏、网站及其他应用上衡量和训练 AI 通用智能的平台。&lt;/p&gt;

&lt;p&gt;Universe 可以让一个 AI agent 像人一样来使用电脑：观看屏幕像素并操作一个虚拟键盘和鼠标。我们必须在期望其解决的广泛任务上训练出 AI 系统，而 Universe 就可以让单个 agent 在任何人类可以通过计算机完成的任务上进行训练。&lt;/p&gt;

&lt;p&gt;4 月份，我们启动了 Gym，这是用来开发和比较强化学习算法的工具。而借助 Universe，任何程序都可以被转换成一个 Gym 的环境。Universe 通过自动启动程序在一个 VNC 远程桌上进行工作——所以它不需要对程序内部、源码或者 bot API 的特别访问。&lt;/p&gt;

&lt;p&gt;今天的发布内容包括一千个环境如 Flash Game，Browser task，以及slither.io GTA V 的游戏。其中数百个已经可以直接测试强化学习算法，而几乎所有的都可以使用 universe python 库自由运行&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>LS-GAN</title>
   <link href="http://daiwk.github.io/posts/image-lsgan.html"/>
   <updated>2017-02-16T00:00:00+00:00</updated>
   <id>/posts/image-lsgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25204020&quot;&gt;知乎专栏：条条大路通罗马LS-GAN：把GAN建立在Lipschitz密度上&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/guojunq/lsgan&quot;&gt;https://github.com/guojunq/lsgan&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这个对f-函数的Lipschitz连续假设，就是沟通LS-GAN和WGAN的关键，因为LS-GAN就是为了限制GAN的无限建模能力而提出的。无限建模能力正是一切麻烦的来源。LS-GAN就是希望去掉这个麻烦&lt;/p&gt;

&lt;p&gt;LS-GAN可以看成是使用成对的（Pairwise）“真实/生成样本对”上的统计量来学习f-函数。这点迫使真实样本和生成样本必须相互配合，从而更高效的学习LS-GAN。
如果生成的样本和真实样本已经很接近，我们就不必要求他们的L-函数非得有个固定间隔，因为，这个时候生成的样本已经非常好了，接近或者达到了真实样本水平。
这样呢，LS-GAN就可以集中力量提高那些距离真实样本还很远，真实度不那么高的样本上了。这样就可以更合理使用LS-GAN的建模能力。在后面我们一旦限定了建模能力后，也不用担心模型的生成能力有损失了。这个我们称为“按需分配”。&lt;/p&gt;

&lt;p&gt;我们证明了，WGAN在对f-函数做出Lipschitz连续的约束后，其实也是将生成样本的密度假设为了Lipschiz 密度。这点上，和LS-GAN是一致的！两者都是建立在Lipschitz密度基础上的生成对抗网络。&lt;/p&gt;

&lt;p&gt;好了，这样我们就给出了WGAN分析梯度消失时候，缺失的哪个定量分析了。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tensorflow-folding/eager execution</title>
   <link href="http://daiwk.github.io/posts/platform-tensorflow-folding.html"/>
   <updated>2017-02-14T00:00:00+00:00</updated>
   <id>/posts/platform-tensorflow-folding</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#tensorflow-fold&quot;&gt;tensorflow fold&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#eager-execution&quot;&gt;eager execution&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;tensorflow-fold&quot;&gt;tensorflow fold&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25216368?utm_medium=social&quot;&gt;知乎专栏：以静制动的TensorFlow Fold&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;eager-execution&quot;&gt;eager execution&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/uwr44uouqcnsuqb60zk2/article/details/78431019&quot;&gt;终于！TensorFlow引入了动态图机制Eager Execution&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Eager Execution 的优点如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;快速调试即刻的运行错误并通过 Python 工具进行整合&lt;/li&gt;
  &lt;li&gt;借助易于使用的 Python 控制流支持动态模型&lt;/li&gt;
  &lt;li&gt;为自定义和高阶梯度提供强大支持&lt;/li&gt;
  &lt;li&gt;适用于几乎所有可用的 TensorFlow 运算&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;当你启动 Eager Execution 时，运算会即刻执行，无需 Session.run() 就可以把它们的值返回到 Python。比如，要想使两个矩阵相乘：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow.contrib.eager&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tfe&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tfe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enable_eager_execution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后就可以直接&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>pytorch</title>
   <link href="http://daiwk.github.io/posts/platform-pytorch.html"/>
   <updated>2017-02-14T00:00:00+00:00</updated>
   <id>/posts/platform-pytorch</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#pytorch%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90&quot;&gt;pytorch源码解析&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pytorch-lightning&quot;&gt;pytorch-lightning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pytorch-12&quot;&gt;pytorch 1.2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pytorch-13&quot;&gt;pytorch 1.3&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#pytorch-torchscript&quot;&gt;PyTorch TorchScript&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tensorflow-eager&quot;&gt;TensorFlow Eager&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;中文文档：&lt;a href=&quot;https://www.pytorchtutorial.com/docs/&quot;&gt;https://www.pytorchtutorial.com/docs/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pytorch的多gpu使用tips：&lt;a href=&quot;https://medium.com/huggingface/training-larger-batches-practical-tips-on-1-gpu-multi-gpu-distributed-setups-ec88c3e51255&quot;&gt;https://medium.com/huggingface/training-larger-batches-practical-tips-on-1-gpu-multi-gpu-distributed-setups-ec88c3e51255&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pytorch源码解析&quot;&gt;pytorch源码解析&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650763179&amp;amp;idx=1&amp;amp;sn=c41e016ef58f4b4079bb70fbe05081f4&amp;amp;chksm=871aabd5b06d22c38550e6bdf2c645be073537d65e4a686c0345ca70f71ab68d8ff86f5ae35d&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=TloMdmvUbLd5jnKvVTzrccQhGuskwL6KQ0HhJLF56Nwtcb16%2BVvMA09bw32tFrjs#rd&quot;&gt;万字综述，核心开发者全面解读PyTorch内部机制&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.52coding.com.cn/2019/05/05/PyTorch0/&quot;&gt;PyTorch源码浅析：简介&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pytorch-lightning&quot;&gt;pytorch-lightning&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/williamFalcon/pytorch-lightning&quot;&gt;https://github.com/williamFalcon/pytorch-lightning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650767325&amp;amp;idx=1&amp;amp;sn=dc36e55d6201529e4fd3e984b8c618b1&amp;amp;chksm=871abba3b06d32b5ed875cb90060a3c5436f38464afc6ba9645efac32aaa8fd03ef99b5f1e0c&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;基于PyTorch的「Keras」：除了核心逻辑通通都封装&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pytorch-12&quot;&gt;pytorch 1.2&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650767663&amp;amp;idx=3&amp;amp;sn=f4cf39ee5f0e0ac8d6e9c7e488237885&amp;amp;chksm=871a4551b06dcc47f751b413f331314ddcc9c142aaa79316bf87d523cec36b9d92f8a97e8a68&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=mmBhl6hER5JU9q0KMKTTFnbwPDksdn18kk%2FlW9Ih3p2TCzi4%2BlfisKHhCysHq%2Bou#rd&quot;&gt;正式支持Transformer与TensorBoard，PyTorch 1.2新鲜出炉&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PyTorch 1.2 版本加入了标准的 nn.Transformer 模块。nn.Transformer 模块完全依赖注意机制描述输入和输出之间的全局依赖关系。nn.Transformer 模块的组件是单独设计的，以便于被分开使用。例如，没有更大的 nn.Transformer 的情况下，nn.TransformerEncoder 可被自身使用。&lt;/p&gt;

&lt;p&gt;简单的输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;from torch.untils.tensorboard import SummaryWriter&lt;/code&gt;就能启动 TensorBoard。一旦我们安装了 TensorBoard，PyTorch 的这项新 API 就会将模型和指标记录到 TensorBoard UI 的目录中，并进行可视化。它对所有基于 PyTorch、Caffe 2 的模型和张量都支持数量、图像、直方图、图和嵌入可视化。&lt;/p&gt;

&lt;h2 id=&quot;pytorch-13&quot;&gt;pytorch 1.3&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/n5iwQs_k8BLRnUrA8nlQhg&quot;&gt;2019年度机器学习框架王者之战！PyTorch 1.3重磅发布，TensorFlow有未来吗？&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/LvPm4HuD5c09T4tQjumg_Q&quot;&gt;图灵奖得主力推：PyTorch 1.3 今天发布&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/VlghKVslhM8gOLogAy5Igw&quot;&gt;PyTorch称霸学界，TensorFlow固守业界，ML框架之争将走向何方？&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;pytorch-torchscript&quot;&gt;PyTorch TorchScript&lt;/h3&gt;

&lt;p&gt;PyTorch JIT是PyTorch的一个中间表征（IR），被称为TorchScript。TorchScript是PyTorch的「图」表征。你可以使用tracing或script模式把一个常规的PyTorch模型转换为TorchScript。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;tracing接受一个&lt;strong&gt;函数&lt;/strong&gt;和一个&lt;strong&gt;输入&lt;/strong&gt;，记录下用该输入&lt;strong&gt;执行的操作&lt;/strong&gt;，然后构建IR。虽然简单，但tracing也有其缺点。例如，它无法捕获还未执行的控制流。例如，如果执行了条件语句的true block，它就无法捕获false block。&lt;/li&gt;
  &lt;li&gt;Script模式接收一个&lt;strong&gt;函数/类&lt;/strong&gt;，&lt;strong&gt;重新解释Python代码&lt;/strong&gt;，然后直接输出TorchScript IR。这使得它可以支持任意代码，但它需要&lt;strong&gt;重新解释 Python&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;一旦你的 PyTorch 模型在这个 IR 中，我们就得到了图模式的所有好处。我们可以在&lt;strong&gt;没有Python依赖&lt;/strong&gt;的情况下&lt;strong&gt;用C++部署PyTorch模型&lt;/strong&gt;，还可以优化该模型。&lt;/p&gt;

&lt;h3 id=&quot;tensorflow-eager&quot;&gt;TensorFlow Eager&lt;/h3&gt;

&lt;p&gt;在API层面上，TensorFlow的eager模式与PyTorch的eager模式基本相同，最初是由Chainer发明的。加入eager模式之后，TensorFlow就拥有了PyTorch eager模式的大部分优势（易用、可调试等）。&lt;/p&gt;

&lt;p&gt;但这也给TensorFlow带来了相同的劣势。TensorFlow的eager模型&lt;strong&gt;不能导出到非Python环境中&lt;/strong&gt;，&lt;strong&gt;无法优化&lt;/strong&gt;，也&lt;strong&gt;无法在移动端运行&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;这将TensorFlow置于与PyTorch相同的境地，它们的解决方式也基本相同——要么&lt;strong&gt;trace&lt;/strong&gt;你的代码&lt;strong&gt;（tf.function）&lt;/strong&gt;，要么&lt;strong&gt;重新解释Python代码（Autograph）&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;因此，TensorFlow的eager模式也不是万能的。尽管你可以&lt;strong&gt;用tf.function注释&lt;/strong&gt;将eager代码&lt;strong&gt;转换为静态图&lt;/strong&gt;，但这并不是一个无缝过程（PyTorch的TorchScript也有类似问题）。tracing在根本上被限制了，&lt;strong&gt;重新解释Python代码&lt;/strong&gt;本质上需要很大程度上&lt;strong&gt;重写Python编译器&lt;/strong&gt;。当然，通过限制深度学习中用到的Python子集可以极大地简化这一范围。&lt;/p&gt;

&lt;p&gt;在默认情况下启用eager模式时，TensorFlow强迫用户做出选择，要么为了易用性使用eager执行，这种做法&lt;strong&gt;需要为了部署而重写&lt;/strong&gt;；&lt;strong&gt;要么彻底不用eager执行&lt;/strong&gt;。PyTorch也面临相同的问题，但PyTorch可选择性加入的TorchScript似乎更加令人愉悦。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>paddlepaddle on kubernetes</title>
   <link href="http://daiwk.github.io/posts/platform-paddlepaddle-on-kubernetes.html"/>
   <updated>2017-02-14T00:00:00+00:00</updated>
   <id>/posts/platform-paddlepaddle-on-kubernetes</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#paddlek8s&quot;&gt;paddle+k8s&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;paddlepaddle和kubernetes结合：
&lt;a href=&quot;http://blog.kubernetes.io/2017/02/run-deep-learning-with-paddlepaddle-on-kubernetes.html&quot;&gt;http://blog.kubernetes.io/2017/02/run-deep-learning-with-paddlepaddle-on-kubernetes.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;paddlek8s&quot;&gt;paddle+k8s&lt;/h2&gt;

&lt;p&gt;1期：
功能点：
内嵌式服务发现：能获取所有endpoints
参数服务器、master、trainer可以自动部署&lt;/p&gt;

&lt;p&gt;2期：
基于paddle的notebook的学习体验的过程，然后可以使用最新的client，启动大规模的容器。
数据放在bos上&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/blob/develop/doc/howto/usage/k8s/k8s_distributed_cn.md&quot;&gt;https://github.com/PaddlePaddle/Paddle/blob/develop/doc/howto/usage/k8s/k8s_distributed_cn.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;caffe-&amp;gt;paddle:
&lt;a href=&quot;https://github.com/PaddlePaddle/models/tree/develop/image_classification/caffe2paddle&quot;&gt;https://github.com/PaddlePaddle/models/tree/develop/image_classification/caffe2paddle&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>cnn模型们</title>
   <link href="http://daiwk.github.io/posts/image-classical-cnns.html"/>
   <updated>2017-02-14T00:00:00+00:00</updated>
   <id>/posts/image-classical-cnns</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#cnn-basic&quot;&gt;cnn basic&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%8d%b7%e7%a7%af&quot;&gt;卷积&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%b1%a0%e5%8c%96&quot;&gt;池化&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%8f%91%e5%b1%95%e5%8e%86%e7%a8%8b&quot;&gt;发展历程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#sota&quot;&gt;SOTA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%b7%a5%e5%85%b7%e5%ba%93&quot;&gt;工具库&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#simplecv&quot;&gt;simplecv&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#kornia&quot;&gt;kornia&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#acnet&quot;&gt;ACNet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#torchcv&quot;&gt;torchcv&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;cnn-basic&quot;&gt;cnn basic&lt;/h2&gt;

&lt;h3 id=&quot;卷积&quot;&gt;卷积&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://deeplearning.stanford.edu/wiki/index.php/%E5%8D%B7%E7%A7%AF%E7%89%B9%E5%BE%81%E6%8F%90%E5%8F%96&quot;&gt;ufldl-卷积特征提取&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;自然图像有其固有特性，也就是说，图像的一部分的统计特性与其他部分是一样的。这也意味着我们在这一部分学习的特征也能用在另一部分上，所以对于这个图像上的所有位置，我们都能使用同样的学习特征。&lt;/p&gt;

&lt;p&gt;更恰当的解释是，当从一个大尺寸图像中随机选取一小块，比如说 8x8 作为样本，并且从这个小块样本中学习到了一些特征，这时我们可以把从这个 8x8 样本中学习到的特征作为探测器，应用到这个图像的任意地方中去。特别是，我们可以用从 8x8 样本中所学习到的特征跟原本的大尺寸图像作卷积，从而对这个大尺寸图像上的任一位置获得一个不同特征的激活值。&lt;/p&gt;

&lt;p&gt;下面给出一个具体的例子：假设你已经从一个 96x96 的图像中学习到了它的一个 8x8 的样本所具有的特征，假设这是由有 100 个隐含单元的自编码完成的。为了得到卷积特征，需要对 96x96 的图像的每个 8x8 的小块图像区域都进行卷积运算。也就是说，抽取 8x8 的小块区域，并且从起始坐标开始依次标记为（1，1），（1，2），…，一直到（89，89），然后对抽取的区域逐个运行训练过的稀疏自编码来得到特征的激活值。在这个例子里，显然可以得到 100 个集合，每个集合含有 89x89 个卷积特征。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/convolution.gif&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;假设给定了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(r \times c\)&lt;/code&gt;的大尺寸图像，将其定义为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{large}\)&lt;/code&gt;。首先通过从大尺寸图像中抽取的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a \times b\)&lt;/code&gt;的小尺寸图像样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{small}\)&lt;/code&gt;训练稀疏自编码，计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f = \sigma (W^{(1)}x_{small} + b^{(1)})\)&lt;/code&gt;（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sigma \)&lt;/code&gt;是一个 sigmoid 型函数）得到了k个特征， 其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W^{(1)}\)&lt;/code&gt; 和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(b^{(1)}\)&lt;/code&gt;是可视层单元和隐含单元之间的权重和偏差值。对于每一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a \times b\)&lt;/code&gt;大小的小图像&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_s\)&lt;/code&gt;，计算出对应的值&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_s = \sigma (W^{(1)}x_s + b^{(1)})\)&lt;/code&gt;，对这些&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_{convolved}\)&lt;/code&gt;值做卷积，就可以得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k \times (r - a + 1) \times (c - b + 1)\)&lt;/code&gt;个卷积后的特征的矩阵。&lt;/p&gt;

&lt;p&gt;【即一个3x3的卷积核,和图中3x3的小块对应元素相乘，然后再相加，得到一个值[其实就是element-wise乘积，再对所有元素求和。。]。得到的新矩阵就是(5-3+1)x(5-3+1)=3x3。&lt;/p&gt;

&lt;p&gt;而如果有m个3x3的卷积核，那就得到了m个3x3的结果，当然，卷积核的大小可以不一样。&lt;/p&gt;

&lt;p&gt;如果有4个通道，2个卷积核，那么最终得到两个图(2个通道)，每个图是4个通道对应的卷积结果对应元素相加，再经过激活得到的】&lt;/p&gt;

&lt;p&gt;权值共享：&lt;/p&gt;

&lt;p&gt;有m个axb的卷积核，那么就只有mxaxb个参数，因为生成最终m个feature map的过程中，生成每个feature map时都是共享同一个axb的卷积核（axb个参数）。&lt;/p&gt;

&lt;h3 id=&quot;池化&quot;&gt;池化&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://deeplearning.stanford.edu/wiki/index.php/%E6%B1%A0%E5%8C%96&quot;&gt;ufldl-池化&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在通过卷积获得了特征 (features) 之后，下一步我们希望利用这些特征去做分类。理论上讲，人们可以用所有提取得到的特征去训练分类器，例如 softmax 分类器，但这样做面临计算量的挑战。例如：对于一个 96X96 像素的图像，假设我们已经学习得到了400个定义在8X8输入上的特征，每一个特征和图像卷积都会得到一个 (96 − 8 + 1) * (96 − 8 + 1) = 7921 维的卷积特征，由于有 400 个特征，所以每个样例 (example) 都会得到一个 892 * 400 = 3,168,400 维的卷积特征向量。学习一个拥有超过 3 百万特征输入的分类器十分不便，并且容易出现过拟合 (over-fitting)。&lt;/p&gt;

&lt;p&gt;为了解决这个问题，首先回忆一下，我们之所以决定使用卷积后的特征是因为图像具有一种“静态性”的属性，这也就意味着在一个图像区域有用的特征极有可能在另一个区域同样适用。因此，为了描述大的图像，一个很自然的想法就是对不同位置的特征进行聚合统计，例如，人们可以计算图像一个区域上的某个特定特征的平均值 (或最大值)。这些概要统计特征不仅具有低得多的维度 (相比使用所有提取得到的特征)，同时还会改善结果(不容易过拟合)。这种聚合的操作就叫做池化 (pooling)，有时也称为平均池化或者最大池化 (取决于计算池化的方法)。&lt;/p&gt;

&lt;p&gt;下图显示池化如何应用于一个图像的四块不重合区域。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/pooling.gif&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;如果人们选择图像中的连续范围作为池化区域，并且只是池化相同(重复)的隐藏单元产生的特征，那么，这些池化单元就具有平移不变性 (translation invariant)。这就意味着即使图像经历了一个小的平移之后，依然会产生相同的 (池化的) 特征。在很多任务中 (例如物体检测、声音识别)，我们都更希望得到具有平移不变性的特征，因为即使图像经过了平移，样例(图像)的标记仍然保持不变。例如，如果你处理一个MNIST数据集的数字，把它向左侧或右侧平移，那么不论最终的位置在哪里，你都会期望你的分类器仍然能够精确地将其分类为相同的数字。&lt;/p&gt;

&lt;p&gt;形式上，在获取到我们前面讨论过的卷积特征后，我们要确定池化区域的大小(假定为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m \times n\)&lt;/code&gt;)，来池化我们的卷积特征。那么，我们把卷积特征划分到数个大小为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m \times n\)&lt;/code&gt;的不相交区域上，然后用这些区域的平均(或最大)特征来获取池化后的卷积特征。这些池化后的特征便可以用来做分类。&lt;/p&gt;

&lt;h2 id=&quot;发展历程&quot;&gt;发展历程&lt;/h2&gt;

&lt;p&gt;包括lenet, alexnet, vgg, googlenet, resnet等。&lt;/p&gt;

&lt;p&gt;inception发展过程：
&lt;a href=&quot;http://blog.csdn.net/u010402786/article/details/52433324&quot;&gt;http://blog.csdn.net/u010402786/article/details/52433324&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;科普贴：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/27642620?utm_source=itdadao&amp;amp;utm_medium=referral&quot;&gt;https://zhuanlan.zhihu.com/p/27642620?utm_source=itdadao&amp;amp;utm_medium=referral&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;sota&quot;&gt;SOTA&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.paperswithcode.com/sota/image-classification-on-imagenet&quot;&gt;https://www.paperswithcode.com/sota/image-classification-on-imagenet&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;工具库&quot;&gt;工具库&lt;/h2&gt;

&lt;h3 id=&quot;simplecv&quot;&gt;simplecv&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://simplecv.org/&quot;&gt;http://simplecv.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SimpleCV，一个开源框架，允许你构建计算机视觉应用程序。它类似于OpenCV，使你可以访问高级计算机视觉库。这意味着你不必担心错综复杂的概念。&lt;/p&gt;

&lt;p&gt;有了它，你可以制作计算机视觉项目，而无需在基础知识上投入太多时间。毕竟，出于某种原因，它被命名为SimpleCV。&lt;/p&gt;

&lt;h3 id=&quot;kornia&quot;&gt;kornia&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/SdNDlTOCIwakV6rHHpI_Eg&quot;&gt;可微分的「OpenCV」：这是基于PyTorch的可微计算机视觉库&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/arraiyopensource/kornia&quot;&gt;https://github.com/arraiyopensource/kornia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;文档：&lt;a href=&quot;https://kornia.readthedocs.io/en/latest/&quot;&gt;https://kornia.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;acnet&quot;&gt;ACNet&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1904.03579&quot;&gt;Adaptively Connected Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/wanggrun/Adaptively-Connected-Neural-Networks&quot;&gt;https://github.com/wanggrun/Adaptively-Connected-Neural-Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/84205427&quot;&gt;https://zhuanlan.zhihu.com/p/84205427&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;torchcv&quot;&gt;torchcv&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/nzbo52Gi43MmO92_rBerng&quot;&gt;基于PyTorch的CV模型框架，北大学生出品TorchCV&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/donnyyou/torchcv&quot;&gt;https://github.com/donnyyou/torchcv&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>vae</title>
   <link href="http://daiwk.github.io/posts/image-vae.html"/>
   <updated>2017-01-21T00:00:00+00:00</updated>
   <id>/posts/image-vae</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/22464760&quot;&gt;知乎专栏：VAE(1)——从KL说起&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/22464764&quot;&gt;知乎专栏：VAE(2)——基本思想&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/22464768&quot;&gt;知乎专栏：VAE(3)——公式与实现&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/22684931&quot;&gt;知乎专栏：VAE(4)——实现&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>wgan</title>
   <link href="http://daiwk.github.io/posts/image-wgan.html"/>
   <updated>2017-01-20T00:00:00+00:00</updated>
   <id>/posts/image-wgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650722818&amp;amp;idx=3&amp;amp;sn=03cb67c8a8ee7f83a7448b518f4336ab&amp;amp;chksm=871b167cb06c9f6a018a99b79d8b2764b207be2b4d03f132151d99124edf2aff4c116a9dc98d&amp;amp;scene=0&amp;amp;pass_ticket=vjEpmxe2DG4P%2By4GjgdfVEMIt0g0SpbViafCaNrBt8viOsGkibUK9SIS47UfCM27#rd&quot;&gt;微信公众号”机器之心”文章&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25071913&quot;&gt;知乎专栏:令人拍案叫绝的Wasserstein GAN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考顾险峰老师的文章：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3NTM4MzY1Mg==&amp;amp;mid=2650813024&amp;amp;idx=1&amp;amp;sn=31e326bd79ed24f5f47b35091385b9ab&amp;amp;chksm=8485c46bb3f24d7d36d1a93b48d9f4d0335262b1152de0bd0f2f1d09527e4acb2ae3d4730913&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=02121jrcKo3ax5gCkgeZ7aS6&amp;amp;pass_ticket=6F3WrFmalMgZ5Oj086HJDIpCgEgR3p0cvrPVa2BxK2A4pl0bhEU19uXGpI43TaYF#rd&quot;&gt;看穿机器学习（W-GAN模型）的黑箱&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;相关数学知识：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3NTM4MzY1Mg==&amp;amp;mid=401810859&amp;amp;idx=1&amp;amp;sn=085601ed23400b162363c724651b98cb&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0212spaxFITf4n7M3jA1OV8L&amp;amp;pass_ticket=6F3WrFmalMgZ5Oj086HJDIpCgEgR3p0cvrPVa2BxK2A4pl0bhEU19uXGpI43TaYF#rd&quot;&gt;最优传输理论（一）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3NTM4MzY1Mg==&amp;amp;mid=401899637&amp;amp;idx=1&amp;amp;sn=ae6ddb620b9b2d0bc140dc10aaef0e39&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0212pgfZXnZLASC3s00v6ALz&amp;amp;pass_ticket=6F3WrFmalMgZ5Oj086HJDIpCgEgR3p0cvrPVa2BxK2A4pl0bhEU19uXGpI43TaYF#rd&quot;&gt;最优传输理论（二）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3NTM4MzY1Mg==&amp;amp;mid=402015289&amp;amp;idx=1&amp;amp;sn=7c547abab1c6c33460795ebb1019d29a&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0212GqLHCpF8c1uVyCjUdEht&amp;amp;pass_ticket=6F3WrFmalMgZ5Oj086HJDIpCgEgR3p0cvrPVa2BxK2A4pl0bhEU19uXGpI43TaYF#rd&quot;&gt;最优传输理论（三）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3NTM4MzY1Mg==&amp;amp;mid=402434159&amp;amp;idx=1&amp;amp;sn=cc0ece42454fcb464be8ecd03d97b56a&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0212Cb7GHtEVM0VfqRq70P3B&amp;amp;pass_ticket=6F3WrFmalMgZ5Oj086HJDIpCgEgR3p0cvrPVa2BxK2A4pl0bhEU19uXGpI43TaYF#rd&quot;&gt;最优传输理论（四）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3NTM4MzY1Mg==&amp;amp;mid=2650812934&amp;amp;idx=1&amp;amp;sn=1f1475529ee55c794c1dfab749700e3a&amp;amp;chksm=8485c40db3f24d1b89a4f2c985c54a50c4e7ce4b68b9aa60e39378e21e49881d7990caede42c&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=02128ANnHWrmdy8dJSOYPzwJ&amp;amp;pass_ticket=6F3WrFmalMgZ5Oj086HJDIpCgEgR3p0cvrPVa2BxK2A4pl0bhEU19uXGpI43TaYF#rd&quot;&gt;最优传输理论应用：色彩变换算法&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要效果如下：&lt;/p&gt;

&lt;p&gt;+　彻底解决GAN&lt;strong&gt;训练不稳定&lt;/strong&gt;的问题，不再需要小心平衡生成器和判别器的训练程度
+　基本解决了&lt;strong&gt;collapse mode&lt;/strong&gt;的问题，确保了生成样本的多样性 
+　训练过程中终于有一个像交叉熵、准确率这样的&lt;strong&gt;数值来指示训练的进程&lt;/strong&gt;，这个&lt;strong&gt;数值越小代表GAN训练得越好&lt;/strong&gt;，代表生成器产生的图像质量越高(如下图所示)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/wgan-progress.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;+　以上一切好处不需要精心设计的网络架构，&lt;strong&gt;最简单的多层全连接网络就可以做到&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;主要改进有以下四点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;判别器最后一层去掉sigmoid&lt;/strong&gt;(原始GAN的判别器做的是true/false二分类任务，所以最后一层是sigmoid，但是现在WGAN中的判别器做的是近似拟合&lt;strong&gt;Wasserstein距离，属于回归任务&lt;/strong&gt;，所以要把最后一层的sigmoid拿掉。)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;生成器和判别器的loss不取log&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;每次更新判别器的参数之后把它们的绝对值截断到不超过一个固定常数c&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;不要用基于动量的优化算法&lt;/strong&gt;（包括momentum和Adam），&lt;strong&gt;推荐RMSProp&lt;/strong&gt;，SGD也行&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>ppgn</title>
   <link href="http://daiwk.github.io/posts/image-ppgn.html"/>
   <updated>2017-01-20T00:00:00+00:00</updated>
   <id>/posts/image-ppgn</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA5MDMwMTIyNQ==&amp;amp;mid=2649289111&amp;amp;idx=1&amp;amp;sn=42e565c3b77259742ae5504eed446486&amp;amp;chksm=8811e7d1bf666ec7f22987f2ee9160d67fab557114801f9d67c04041b6d39d94235ad16939ab&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1209d3upz2esvwGmY4gfAcNl&amp;amp;pass_ticket=6F3WrFmalMgZ5Oj086HJDIpCgEgR3p0cvrPVa2BxK2A4pl0bhEU19uXGpI43TaYF#rd&quot;&gt;ppgn&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>m-rnn</title>
   <link href="http://daiwk.github.io/posts/image-mrnn.html"/>
   <updated>2017-01-20T00:00:00+00:00</updated>
   <id>/posts/image-mrnn</id>
   <content type="html">&lt;p&gt;目录：&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;项目主页：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.stat.ucla.edu/~junhua.mao/m-RNN.html&quot;&gt;http://www.stat.ucla.edu/~junhua.mao/m-RNN.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1412.6632&quot;&gt;Deep Captioning with Multimodal Recurrent Neural Networks (m-RNN)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;更新的进展：
tf上的img2txt：
&lt;a href=&quot;https://arxiv.org/pdf/1411.4555.pdf&quot;&gt;Show and Tell: A Neural Image Caption Generator&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;../assets/img-qa/Show and Tell Lessons learned from the 2015 MSCOCO Image Captioning Challenge.pdf&quot;&gt;Show and Tell: Lessons learned from the 2015　MSCOCO Image Captioning Challenge&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>lapgan</title>
   <link href="http://daiwk.github.io/posts/image-lapgan.html"/>
   <updated>2017-01-20T00:00:00+00:00</updated>
   <id>/posts/image-lapgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIzOTY2NTQ5Mg==&amp;amp;mid=2247483957&amp;amp;idx=1&amp;amp;sn=cb776f4d50f07efe1c44057bfea0fef2&amp;amp;chksm=e927ea0ede50631826d8d66f3bca656aeee314a8b374edc252c8513c70cdbf3a108700757f44&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0203HxhVLtnRdsDX6B1XWwKF&amp;amp;pass_ticket=vjEpmxe2DG4P%2By4GjgdfVEMIt0g0SpbViafCaNrBt8viOsGkibUK9SIS47UfCM27#rd&quot;&gt;微信公众号”学术兴趣小组”文章&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考：&lt;a href=&quot;http://blog.csdn.net/solomon1558/article/details/52562851&quot;&gt;csdn Solomon1588的文章&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>infogan & improvedgan</title>
   <link href="http://daiwk.github.io/posts/image-infogan-improvedgan.html"/>
   <updated>2017-01-20T00:00:00+00:00</updated>
   <id>/posts/image-infogan-improvedgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIzOTY2NTQ5Mg==&amp;amp;mid=2247483933&amp;amp;idx=1&amp;amp;sn=83eb5de5b79c8b5161280b6698c8736e&amp;amp;chksm=e927ea26de506330c5e7aec431479d8fad27a385e470a041807c96efc800529e9ccc74a3e7ec&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0204aXv03UtASgP9ESxScBZR&amp;amp;pass_ticket=vjEpmxe2DG4P%2By4GjgdfVEMIt0g0SpbViafCaNrBt8viOsGkibUK9SIS47UfCM27#rd&quot;&gt;微信公众号”学术兴趣小组”文章&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>GAN(Generative Adversarial Nets)</title>
   <link href="http://daiwk.github.io/posts/image-gan.html"/>
   <updated>2017-01-20T00:00:00+00:00</updated>
   <id>/posts/image-gan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#gans的优缺点&quot;&gt;GANs的优缺点&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#some-notes&quot;&gt;some notes&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#代码&quot;&gt;代码&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIzOTY2NTQ5Mg==&amp;amp;mid=2247483885&amp;amp;idx=1&amp;amp;sn=c4c0c88b8ca8f283fe498c7093a01b5c&amp;amp;chksm=e927e9d6de5060c0bf41bae65996339520510b1c11fc5dcf9645f8750e0d9f934637ad9b43e9&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=01304oIWF6vST7ZocMixrrd8&amp;amp;pass_ticket=vjEpmxe2DG4P%2By4GjgdfVEMIt0g0SpbViafCaNrBt8viOsGkibUK9SIS47UfCM27#rd&quot;&gt;微信公众号”学术兴趣小组”文章&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIzOTY2NTQ5Mg==&amp;amp;mid=2247484065&amp;amp;idx=1&amp;amp;sn=b337dbc92dd5d52b3244513b2a2e46cb&amp;amp;chksm=e927ea9ade50638c0505b5691e3f42e5bdefd5fbda4a89d18bf6405ee7acfabf8200b9ab7f87&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0216UDu7Ro1ioqBc8BRf1p3E&amp;amp;pass_ticket=u1vycqe63pZgCToOiBMo91eBz45UOdEC7BjPCBAb3qEseQK%2Bzpx6F2WLEhjShxVF#rd&quot;&gt;微信公众号”学术兴趣小组”文章：gan应用情况调研&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;gans的优缺点&quot;&gt;GANs的优缺点&lt;/h2&gt;

&lt;p&gt;优点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;相比于FVBNs（全可见信念网络，Fully visible belief networks）模型，GANs能并行生成样本，不需要逐维产生。&lt;/li&gt;
  &lt;li&gt;相比于玻尔兹曼机（Boltzmann machines）、非线性ICA（non-linear ICA）等生成模型，GANs对Generator的设计的限制很少。&lt;/li&gt;
  &lt;li&gt;理论上保证了某些GANs能够收敛到纳什均衡。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;缺点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;训练GANs实际上是在找纳什均衡解，这比优化一个目标函数要困难。&lt;/li&gt;
  &lt;li&gt;GANs生成的图像比较sharp，也就是说，它倾向于生成相似的图像。作者在2016 NIPS tutorial中指出，这个缺陷与采用何种KL散度作为loss无关，而可能是与训练过程有关。详细的讨论可以参看参考文献2的3.2.5和5.1.1章节。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;some-notes&quot;&gt;some notes&lt;/h2&gt;

&lt;p&gt;Generative Adversarial Nets, Goodfellow et al, NIPS 2014&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://ishmaelbelghazi.github.io/ALI&quot;&gt;https://ishmaelbelghazi.github.io/ALI&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DCGAN:
Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks, Radford et al, ICLR 2015&lt;/p&gt;

&lt;p&gt;反卷积（实际是卷积把步长调成&amp;gt;=2）&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;擅长生成特定Domain 里的小图片&lt;/li&gt;
  &lt;li&gt;向量空间运算（类word embedding）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;模型崩溃——minibatch GAN （Salimans et al, NIPS 2016）&lt;/p&gt;

&lt;p&gt;Generative Adversarial Text to Image Synthesis, Reed et al, ICML 2016
原来GAN输入只是噪音，现在多一些其他维度的描述（例如，文本）&lt;/p&gt;

&lt;p&gt;Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network, Ledig et al, arxiv 2016&lt;/p&gt;

&lt;p&gt;针对小数据集，传统方法+nn比直接上nn其实差不多（deepface）&lt;/p&gt;

&lt;h2 id=&quot;代码&quot;&gt;代码&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Theano+pylearn2版本（Goodfellow提供）： &lt;a href=&quot;https://github.com/goodfeli/adversarial&quot;&gt;https://github.com/goodfeli/adversarial&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Keras版本： &lt;a href=&quot;https://github.com/jhayes14/GAN&quot;&gt;https://github.com/jhayes14/GAN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Tensorflow版本（含GANs+VAEs+DRAW）： &lt;a href=&quot;https://github.com/ikostrikov/TensorFlow-VAE-GAN-DRAW&quot;&gt;https://github.com/ikostrikov/TensorFlow-VAE-GAN-DRAW&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>dcgan</title>
   <link href="http://daiwk.github.io/posts/image-dcgan.html"/>
   <updated>2017-01-20T00:00:00+00:00</updated>
   <id>/posts/image-dcgan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIzOTY2NTQ5Mg==&amp;amp;mid=2247483910&amp;amp;idx=1&amp;amp;sn=a5b88757993bfbe5564986cd6eb17b46&amp;amp;chksm=e927ea3dde50632ba9e3abbdfe53b0bc5425dc9e17324aac5c0e2dec1579555193533d737e2c&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0205HKoKDBBVc1YoHgM1UrGO&amp;amp;pass_ticket=vjEpmxe2DG4P%2By4GjgdfVEMIt0g0SpbViafCaNrBt8viOsGkibUK9SIS47UfCM27#rd&quot;&gt;微信公众号”学术兴趣小组”文章&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>image-qa</title>
   <link href="http://daiwk.github.io/posts/image-image-qa.html"/>
   <updated>2017-01-15T00:00:00+00:00</updated>
   <id>/posts/image-image-qa</id>
   <content type="html">&lt;p&gt;项目主页：
&lt;a href=&quot;http://idl.baidu.com/FM-IQA.html&quot;&gt;http://idl.baidu.com/FM-IQA.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;目录：&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E8%83%8C%E6%99%AF%E4%BB%8B%E7%BB%8D&quot;&gt;背景介绍&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%A8%A1%E5%9E%8B%E6%A6%82%E8%A7%88&quot;&gt;模型概览&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%95%B0%E6%8D%AE%E5%87%86%E5%A4%87&quot;&gt;数据准备&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%95%B0%E6%8D%AE%E4%BB%8B%E7%BB%8D%E4%B8%8E%E4%B8%8B%E8%BD%BD&quot;&gt;数据介绍与下载&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E4%B8%8B%E8%BD%BDmscoco%E6%95%B0%E6%8D%AE&quot;&gt;下载MSCOCO数据&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E4%B8%8B%E8%BD%BD%E5%9B%BE%E7%89%87%EF%BC%8Dqa%E7%9A%84%E6%A0%87%E6%B3%A8%E6%95%B0%E6%8D%AE%E9%9B%86&quot;&gt;下载图片－q/a的标注数据集&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E8%8B%B1%E6%96%87qa&quot;&gt;英文qa:&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E4%B8%AD%E6%96%87qa&quot;&gt;中文qa:&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%A8%A1%E5%9E%8B%E7%BB%93%E6%9E%84&quot;&gt;模型结构&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#31-lstmq&quot;&gt;3.1 LSTM(Q)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#31-cnn&quot;&gt;3.1 CNN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#31-lstma&quot;&gt;3.1 LSTM(A)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#34-fusing-layer&quot;&gt;3.4 fusing layer&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#35-intermediate-and-softmax&quot;&gt;3.5 intermediate and softmax&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;背景介绍&quot;&gt;背景介绍&lt;/h2&gt;

&lt;p&gt;本文模型取名为mQA(Multilingual Image Question Answering)，可以自动回答关于图片的问题。answer可以是&lt;strong&gt;一句话，一个短语或者是一个单词&lt;/strong&gt;。主要包含了四部分：lstm-q(提取question representation), cnn(提取visual representation), lstm-a(提取answer的linguistic context), fusing component(结合上述三个模块的输出，生成answer)。数据集大小：15w图片以及他们对应　的31w的中文q-a对，以及他们对应的英文翻译。&lt;/p&gt;

&lt;p&gt;本教程主要介绍图像分类的深度学习模型，以及如何使用PaddlePaddle训练m-rnn模型。&lt;/p&gt;

&lt;h2 id=&quot;模型概览&quot;&gt;模型概览&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;../assets/img-qa/model-intro.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;数据准备&quot;&gt;数据准备&lt;/h2&gt;

&lt;h3 id=&quot;数据介绍与下载&quot;&gt;数据介绍与下载&lt;/h3&gt;

&lt;p&gt;通用图像分类公开的标准数据集常用的有&lt;a href=&quot;https://www.cs.toronto.edu/~kriz/cifar.html&quot;&gt;CIFAR&lt;/a&gt;、&lt;a href=&quot;http://image-net.org/&quot;&gt;ImageNet&lt;/a&gt;、&lt;a href=&quot;http://mscoco.org/&quot;&gt;COCO&lt;/a&gt;等，常用的细粒度图像分类数据集包括&lt;a href=&quot;http://www.vision.caltech.edu/visipedia/CUB-200-2011.html&quot;&gt;CUB-200-2011&lt;/a&gt;、&lt;a href=&quot;http://vision.stanford.edu/aditya86/ImageNetDogs/&quot;&gt;Stanford Dog&lt;/a&gt;、&lt;a href=&quot;http://www.robots.ox.ac.uk/~vgg/data/flowers/&quot;&gt;Oxford-flowers&lt;/a&gt;等。其中ImageNet数据集规模相对较大，大量研究成果基于ImageNet。ImageNet数据从2010年来稍有变化，常用的是ImageNet-2012数据集，该数据集包含1000个类别：训练集包含1,281,167张图片，每个类别数据732至1300张不等，验证集包含50,000张图片，平均每个类别50张图片。&lt;/p&gt;

&lt;p&gt;由于ImageNet数据集较大，下载和训练较慢，为了方便大家学习，我们使用&lt;a href=&quot;https://www.cs.toronto.edu/~kriz/cifar.html&quot;&gt;CIFAR10&lt;/a&gt;数据集。CIFAR10数据集包含60,000张32x32的彩色图片，10个类别，每个类包含6,000张。其中50,000张图片作为训练集，10000张作为测试集。图11从每个类别中随机抽取了10张图片，展示了所有的类别。&lt;/p&gt;

&lt;h4 id=&quot;下载mscoco数据&quot;&gt;下载MSCOCO数据&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;download_mscoco&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir &lt;/span&gt;data
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;data

&lt;span class=&quot;c&quot;&gt;# http://mscoco.org/dataset/#download&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Training images [80K/13GB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Val. images [40K/6.2GB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Testing images [40K/6.2GB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2015 Testing images [80K/12.4G]&lt;/span&gt;
wget http://msvocds.blob.core.windows.net/coco2014/train2014.zip
wget http://msvocds.blob.core.windows.net/coco2014/val2014.zip
wget http://msvocds.blob.core.windows.net/coco2014/test2014.zip
wget http://msvocds.blob.core.windows.net/coco2015/test2015.zip


&lt;span class=&quot;c&quot;&gt;#annotations&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Train/Val object instances [158MB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Train/Val person keypoints [70MB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Train/Val image captions [18.8MB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Testing Image info [0.74MB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2015 Testing Image info [1.83MB]&lt;/span&gt;
wget http://msvocds.blob.core.windows.net/annotations-1-0-3/instances_train-val2014.zip
wget http://msvocds.blob.core.windows.net/annotations-1-0-3/person_keypoints_trainval2014.zip
wget http://msvocds.blob.core.windows.net/annotations-1-0-3/captions_train-val2014.zip
wget http://msvocds.blob.core.windows.net/annotations-1-0-4/image_info_test2014.zip
wget http://msvocds.blob.core.windows.net/annotations-1-0-4/image_info_test2015.zip

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;unzip_data&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;data
&lt;span class=&quot;c&quot;&gt;#2014 Training images [80K/13GB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Val. images [40K/6.2GB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Testing images [40K/6.2GB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2015 Testing images [80K/12.4G]&lt;/span&gt;
unzip train2014.zip
unzip val2014.zip
unzip test2014.zip
unzip test2015.zip


&lt;span class=&quot;c&quot;&gt;#annotations&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Train/Val object instances [158MB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Train/Val person keypoints [70MB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Train/Val image captions [18.8MB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2014 Testing Image info [0.74MB]&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2015 Testing Image info [1.83MB]&lt;/span&gt;
unzip instances_train-val2014.zip
unzip person_keypoints_trainval2014.zip
unzip captions_train-val2014.zip
unzip image_info_test2014.zip
unzip image_info_test2015.zip



&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

download_mscoco
unzip_data
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;下载图片qa的标注数据集&quot;&gt;下载图片－q/a的标注数据集&lt;/h3&gt;

&lt;h5 id=&quot;英文qa&quot;&gt;英文qa:&lt;/h5&gt;

&lt;p&gt;下载地址为&lt;a href=&quot;http://pan.baidu.com/s/1qXh68w8&quot;&gt;http://pan.baidu.com/s/1qXh68w8&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;数据schema:&lt;/p&gt;

&lt;h4 id=&quot;中文qa&quot;&gt;中文qa:&lt;/h4&gt;

&lt;p&gt;下载地址为&lt;a href=&quot;http://pan.baidu.com/s/1qXh68w8&quot;&gt;http://pan.baidu.com/s/1qXh68w8&lt;/a&gt;
数据schema:&lt;/p&gt;

&lt;p&gt;下面命令用于下载数据和基于训练集计算图像均值，在网络输入前，基于该均值对输入数据做预处理。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./data/get_data.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;模型结构&quot;&gt;模型结构&lt;/h3&gt;

&lt;p&gt;本教程中m-QA模型包含了LSTM(Q)、CNN、LSTM(A)、fusing layer以及intermediate and softmax层。&lt;/p&gt;

&lt;h4 id=&quot;31-lstmq&quot;&gt;3.1 LSTM(Q)&lt;/h4&gt;

&lt;h4 id=&quot;31-cnn&quot;&gt;3.1 CNN&lt;/h4&gt;

&lt;h4 id=&quot;31-lstma&quot;&gt;3.1 LSTM(A)&lt;/h4&gt;

&lt;h4 id=&quot;34-fusing-layer&quot;&gt;3.4 fusing layer&lt;/h4&gt;

\[\mathbf{f}(t)=g(\mathbf{V}_{\mathbf{r}_{Q}}\mathbf{r}_Q+\mathbf{V}_{\mathbf{I}}\mathbf{I}+\mathbf{V}_{\mathbf{r}_{A}}\mathbf{r}_A(t)+\mathbf{V}_{\mathbf{w}}\mathbf{w}(t))\]

&lt;h4 id=&quot;35-intermediate-and-softmax&quot;&gt;3.5 intermediate and softmax&lt;/h4&gt;

</content>
 </entry>
 
 <entry>
   <title>安装PIL</title>
   <link href="http://daiwk.github.io/posts/image-install-pil.html"/>
   <updated>2017-01-04T00:00:00+00:00</updated>
   <id>/posts/image-install-pil</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-一键安装paddle&quot;&gt;0. 一键安装paddle&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-编译jpeg&quot;&gt;1. 编译jpeg&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-安装pil&quot;&gt;2. 安装PIL&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h3 id=&quot;0-一键安装paddle&quot;&gt;0. 一键安装paddle&lt;/h3&gt;

&lt;p&gt;如果是度厂内部的机器，记得先装jumbo，然后jumbo装cmake\swig&lt;/p&gt;

&lt;h3 id=&quot;1-编译jpeg&quot;&gt;1. 编译jpeg&lt;/h3&gt;

&lt;p&gt;注意，source ~/.bashrc,保证我们的python是上面安装完的paddle的python&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; ~/mylib
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/mylib

&lt;span class=&quot;c&quot;&gt;# install jpeglib&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/opt/compiler/gcc-4.8.2/bin/:&lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;

/bin/rm &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; jpeg&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;

wget http://www.ijg.org/files/jpegsrc.v8c.tar.gz
&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-xvzf&lt;/span&gt; jpegsrc.v8c.tar.gz
&lt;span class=&quot;nv&quot;&gt;LOCAL_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;jpeg-8c/
./configure &lt;span class=&quot;nt&quot;&gt;--prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$LOCAL_PATH&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-shared&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--enable-static&lt;/span&gt;
make
make &lt;span class=&quot;nb&quot;&gt;install
cd&lt;/span&gt; -
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后会提示：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Libraries have been installed in:
   /home/work/mylib/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;于是，我们用root权限：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo &quot;/home/data/mylib/lib/&quot; &amp;gt;&amp;gt; /etc/ld.so.conf
ldconfig
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者，没root的话&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#export LD_LIBRARY_PATH=/home/work/mylib/lib:$LD_LIBRARY_PATH

## jpeglib zlib
export LD_LIBRARY_PATH=/home/work/.jumbo/lib/:/home/work/mylib/lib:$LD_LIBRARY_PATH
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2-安装pil&quot;&gt;2. 安装PIL&lt;/h3&gt;

&lt;p&gt;首先，下载安装包并解压&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/bin/rm &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; Imaging&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;

wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz
&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-xvzf&lt;/span&gt; Imaging-1.1.7.tar.gz
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;Imaging-1.1.7/

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后，修改setup.py：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;LOCAL_PATH = &quot;/home/work/mylib/&quot;
JUMBO_PATH = &quot;/home/work/.jumbo/&quot;
JPEG_ROOT = (LOCAL_PATH + &quot;/lib&quot;, LOCAL_PATH + &quot;/include&quot;)
ZLIB_ROOT = (JUMBO_PATH + &quot;/lib&quot;, JUMBO_PATH + &quot;/include&quot;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后，先看看有没有问题&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python setup.py build_ext &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# force&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ./build/lib.linux-x86_64-2.7/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.so ./PIL

python selftest.py

&lt;span class=&quot;nt&quot;&gt;--------------------------------------------------------------------&lt;/span&gt;
PIL 1.1.7 TEST SUMMARY 
&lt;span class=&quot;nt&quot;&gt;--------------------------------------------------------------------&lt;/span&gt;
Python modules loaded from ./PIL
Binary modules loaded from ./PIL
&lt;span class=&quot;nt&quot;&gt;--------------------------------------------------------------------&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; PIL CORE support ok
&lt;span class=&quot;k&quot;&gt;***&lt;/span&gt; TKINTER support not installed
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; JPEG support ok
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; ZLIB &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;PNG/ZIP&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; support ok
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; FREETYPE2 support ok
&lt;span class=&quot;k&quot;&gt;***&lt;/span&gt; LITTLECMS support not installed
&lt;span class=&quot;nt&quot;&gt;--------------------------------------------------------------------&lt;/span&gt;
Running selftest:
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; 57 tests passed.

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;没有问题的时候，再&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python setup.py &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;就可以了！！&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>lightgbm</title>
   <link href="http://daiwk.github.io/posts/platform-lightgbm.html"/>
   <updated>2017-01-01T00:00:00+00:00</updated>
   <id>/posts/platform-lightgbm</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#安装&quot;&gt;安装&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#安装cxx包&quot;&gt;安装cxx包&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#安装py&quot;&gt;安装py&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#验证&quot;&gt;验证&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/Microsoft/LightGBM&quot;&gt;https://github.com/Microsoft/LightGBM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.jianshu.com/p/48e82dbb142b&quot;&gt;http://www.jianshu.com/p/48e82dbb142b&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

&lt;h3 id=&quot;安装cxx包&quot;&gt;安装cxx包&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt-get update
apt-get install git
apt-get install cmake
apt-get install openmpi-bin openmpi-doc libopenmpi-dev
git clone --recursive https://github.com/Microsoft/LightGBM ; cd LightGBM
mkdir build ; cd build
cmake -DUSE_MPI=ON ..
make -j4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;安装py&quot;&gt;安装py&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip install lightgbm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;验证&quot;&gt;验证&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import lightgbm as lgb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>在docker中不要使用sshd</title>
   <link href="http://daiwk.github.io/posts/other-use-docker-without-sshd.html"/>
   <updated>2016-12-15T00:00:00+00:00</updated>
   <id>/posts/other-use-docker-without-sshd</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#备份我的数据&quot;&gt;备份我的数据?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#检查日志&quot;&gt;检查日志?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#重启service&quot;&gt;重启service?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#修改我的配置文件&quot;&gt;修改我的配置文件&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#调试我的应用&quot;&gt;调试我的应用？&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#如何远程访问呢&quot;&gt;如何远程访问呢？&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.oschina.net/translate/why-you-dont-need-to-run-sshd-in-docker?cmp&quot;&gt;博客&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;备份我的数据&quot;&gt;备份我的数据?&lt;/h1&gt;

&lt;p&gt;你的数据应该存在于 volume中. 然后你可以使用–volumes-from选项来运行另一个容器，与第一个容器共享这个volume。这样做的好处：如果你需要安装新的工具（如s75pxd）来将你备份的数据长期保存，或将数据转移到其他永久存储时，你可以在这个特定的备份容器中进行，而不是在主服务容器中。这很简洁。&lt;/p&gt;

&lt;h1 id=&quot;检查日志&quot;&gt;检查日志?&lt;/h1&gt;

&lt;p&gt;再次使用 volume! 如果你将所有日志写入一个特定的目录下，且这个目录是一个volume的话，那你可以启动另一个log inspection” 容器（使用–volumes-from，还记得么?)且在这里面做你需要做的事。如果你还需要特殊的工具（或只需要一个有意思的ack-grep），你可以在这个容器中安装它们，这样可以保持主容器的原始环境。&lt;/p&gt;

&lt;h1 id=&quot;重启service&quot;&gt;重启service?&lt;/h1&gt;

&lt;p&gt;基本上所有service都可以通过信号来重启。当你使用/etc/init.d/foo restart或service foo restart时，实际上它们都会给进程发送一个特定的信号。你可以使用docker kill -s &lt;signal&gt;来发送这个信号。一些service可能不会监听这些信号，但可以在一个特定的socket上接受命令。如果是一个TCP socket，只需要通过网络连接上就可以了。如果是一个UNIX套接字，你可以再次使用volume。将容器和service的控制套接字设置到一个特定的目录中，且这个目录是一个volume。然后启动一个新的容器来访问这个volume；这样就可以使用UNIX套接字了。&lt;/signal&gt;&lt;/p&gt;

&lt;p&gt;“但这也太复杂了吧！”－其实不然。假设你名为foo的servcie 在/var/run/foo.sock创建了一个套接字，且需要你运行fooctl restart来完成重启。只需要使用-v /var/run(或在Docker文件中添加VOLUME /var/run)来启动这个service就可以了。当你想重启的时候，使用–volumes-from选项并重载命令来启动相同的镜像。像这样：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Starting the service
CID=$(docker run -d -v /var/run fooservice)
# Restarting the service with a sidekick container
docker run --volumes-from $CID fooservice fooctl restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;修改我的配置文件&quot;&gt;修改我的配置文件&lt;/h1&gt;

&lt;p&gt;如果你正在执行一个持久的配置变更，你最好把他的改变放在image中，因为如果你又启动一个container，那么服务还是使用的老的配置，你的配置变更将丢失。所以，没有您的SSH访问！“但是我需要在服务存活期间，改变我的配置；例如增加一个新的虚拟站点！”这种情况下，你需要使用……等待……volume！配置应该在volume中，并且该volume应该和一个特殊目的“配置编辑器”容器共享。你可以在这个容器中使用任何你喜欢的东西：SSH + 你最喜欢的编辑器，或一个接受API调用的web服务，或一个从外部源抓取信息的定时任务；诸如此类。另外，分离关注：一个容器运行服务，另外一个处理配置更新。“但是我做临时更改，因为我正在测试不同的值！”在这种情况下，查看下一章节！&lt;/p&gt;

&lt;h1 id=&quot;调试我的应用&quot;&gt;调试我的应用？&lt;/h1&gt;

&lt;p&gt;这可能是唯一需要进入container的场景了。因为你要运行gdb, strace, tweak配置，等。这种情况下，你需要 nsenter。&lt;/p&gt;

&lt;p&gt;nsenter是一个小的工具，用来进入命名空间中。技术上，它可以进入现有的命名空间，或者产生一个进程进入新的一组命名空间。“命名空间是什么?”他们是容器的重要组成部分。简单点说：通过使用 nsenter ，你可以进入一个已经存在的container中，尽管这个container没有运行ssh 或者任意特殊用途的守护进程。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#安装nsenter&lt;/span&gt;
docker run &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; /usr/local/bin:/target jpetazzo/nsenter

&lt;span class=&quot;c&quot;&gt;#登陆到容器中&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;imageid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8aa68fd0bc55 &lt;span class=&quot;c&quot;&gt;# e.g.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;PID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;docker inspect &lt;span class=&quot;nt&quot;&gt;--format&lt;/span&gt;  &lt;span class=&quot;nv&quot;&gt;$imageid&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
nsenter &lt;span class=&quot;nt&quot;&gt;--target&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$PID&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--mount&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--uts&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--ipc&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--net&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--pid&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;如何远程访问呢&quot;&gt;如何远程访问呢？&lt;/h1&gt;

&lt;p&gt;如果你需要从一个远程主机进入一个容器，有（至少）两个方法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;SSH 进入 Docker 主机，并使用 nsenter;&lt;/li&gt;
  &lt;li&gt;SSH 进入 Docker 主机，通过一个特殊的密钥参数授权nsenter命令  (也就是，nsenter)。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;第一种方法相对简单；但是需要root权限访问Docker主机（从安全角度来说不是很好）。第二种方法在 SSH 的 authorized_keys 文件中使用 command= 模式。你可能熟悉 “古典的” authorized_keys文件，它看起来像这样：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ssh-rsa bbb…QOID== aaa@ccc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;（当然，实际上一个真正的密钥是很长的，一般都会占据好几行。）你也可以强制使用一个专有的命令。如果你想要在你的系统上查看一个远程的主机上可以有效使用的内存，可以使用SSH密钥，但是你不会希望交出所有的shell权限，你可以在authorized_keys文件中输入下面的内容：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;command=&quot;free&quot; ssh-rsa bbb…QOID== aaa@ccc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;现在，当使用专有的密钥进行连接时，替换取得的shell，它可以执行free命令。除此之外，就不能做其他的。（通常，你可能还想要添加no-port-forwarding；如果希望了解更多信息可以查看authorized_keys(5)的手册（manpage））。这种机制的关键是使得责任分离。Alice把服务放在容器内部；她不用处理远程的访问，登陆等事务。Betty会添加SSH层，在特殊情况（调试奇怪的问题）下使用。Charlotte会考虑登陆。等等。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>deep learning book-第5章 Machine Learning Basics</title>
   <link href="http://daiwk.github.io/posts/dl-dlbook-chap5.html"/>
   <updated>2016-12-10T00:00:00+00:00</updated>
   <id>/posts/dl-dlbook-chap5</id>
   <content type="html">&lt;p&gt;几个git链接：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/HFTrader/DeepLearningBook&quot;&gt;https://github.com/HFTrader/DeepLearningBook&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-CN&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-CN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目录：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;5.1 Learning Algorithms
    &lt;ul&gt;
      &lt;li&gt;The Task, T&lt;/li&gt;
      &lt;li&gt;The Performance Measure, P&lt;/li&gt;
      &lt;li&gt;The Experience, E&lt;/li&gt;
      &lt;li&gt;Example: Linear Regression&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;5.2 Capacity, Overfitting and Underfitting
    &lt;ul&gt;
      &lt;li&gt;The No Free Lunch Theorem&lt;/li&gt;
      &lt;li&gt;Regularization&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;5.3 Hyperparameters and Validation Sets
    &lt;ul&gt;
      &lt;li&gt;Cross-Validation&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;5.4 Estimators, Bias and Variance
    &lt;ul&gt;
      &lt;li&gt;Point Estimation&lt;/li&gt;
      &lt;li&gt;Bias&lt;/li&gt;
      &lt;li&gt;Variance and Standard Error&lt;/li&gt;
      &lt;li&gt;Trading off Bias and Variance to Minimize Mean Squared Error&lt;/li&gt;
      &lt;li&gt;Consistency&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;5.5 Maximum Likelihood Estimation
    &lt;ul&gt;
      &lt;li&gt;Conditional Log-Likelihood and Mean Squared Error&lt;/li&gt;
      &lt;li&gt;Properties of Maximum Likelihood&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;5.6 Bayesian Statistics
    &lt;ul&gt;
      &lt;li&gt;Maximum A Posteriori (MAP) Estimation&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;5.7 Supervised Learning Algorithms
    &lt;ul&gt;
      &lt;li&gt;Probabilistic Supervised Learning&lt;/li&gt;
      &lt;li&gt;Support Vector Machines&lt;/li&gt;
      &lt;li&gt;Other Simple Supervised Learning Algorithms&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;5.8 Unsupervised Learning Algorithms
    &lt;ul&gt;
      &lt;li&gt;Principal Components Analysis&lt;/li&gt;
      &lt;li&gt;k-means Clustering&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;5.9 Stochastic Gradient Descent&lt;/li&gt;
  &lt;li&gt;5.10 Building a Machine Learning Algorithm&lt;/li&gt;
  &lt;li&gt;5.11 Challenges Motivating Deep Learning
    &lt;ul&gt;
      &lt;li&gt;The Curse of Dimensionality&lt;/li&gt;
      &lt;li&gt;Local Constancy and Smoothness Regularization&lt;/li&gt;
      &lt;li&gt;Manifold Learning&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>dual learning for mt</title>
   <link href="http://daiwk.github.io/posts/rl-dual-learning-mt.html"/>
   <updated>2016-12-08T00:00:00+00:00</updated>
   <id>/posts/rl-dual-learning-mt</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;
&lt;p&gt;&lt;a href=&quot;../assets/rl/dual-learning-nmt/dual-learning-for-machine-translation.pdf&quot;&gt;dual-learning-for-machine-translation.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;深度学习之所以能够取得巨大的成功，一个非常重要的因素就是大数据，特别是大规模的带标签的数据。例如在图像识别中，深度神经网络使用上百万的带标签的图像进行训练，在机器翻译中我们会用上千万的双语句对进行训练，在围棋中我们会用上千万的专业棋手的落子进行训练……这种做法有两个局限性。首先，人工标注获取标签的代价很高。例如我们考虑机器翻译这个任务：现在市场人工翻译一个单词的价格差不多是5到10美分，如果一个句子的平均长度为三十个单词，那么1000万个句子人工翻译的代价差不多是7.5美分×30×1000万，约等于2200万美元。现在一个商业公司的翻译引擎通常支持上百种语言的相互翻译，为了训练这样规模的翻译模型，人工标注的代价就会达到上千亿美元。其次，在很多任务中，我们没办法收集到大规模的标注数据，例如在医疗中或在小语种的相互翻译。为了使深度学习能够取得更广泛的成功，我们需要降低其对大规模标注数据的依赖性。为了解决这个问题，我们提出了一种新的学习范式，我们把它称作对偶学习。&lt;/p&gt;

&lt;p&gt;考虑一个对偶翻译游戏，里面有两个玩家小明和爱丽丝，如下图所示。小明只能讲中文，爱丽丝只会讲英文，他们两个人一起希望能够提高英文到中文的翻译模型f和中文到英文的翻译模型g。给定一个英文的句子x，爱丽丝首先通过f把这个句子翻译成中文句子y1，然后把这个中文的句子发给小明。因为没有标注，所以小明不知道正确的翻译是什么，但是小明可以知道，这个中文的句子是不是语法正确、符不符合中文的语言模型，这些信息都能帮助小明大概判断翻译模型f是不是做的好。然后小明再把这个中文的句子y1通过翻译模型g翻译成一个新的英文句子x1，并发给爱丽丝。通过比较x和x1是不是相似，爱丽丝就能够知道翻译模型f和g是不是做得好，尽管x只是一个没有标注的句子。因此，通过这样一个对偶游戏的过程，我们能够从没有标注的数据上获得反馈，从而知道如何提高机器学习模型。&lt;/p&gt;

&lt;p&gt;实际上这个对偶游戏和强化学习的过程比较类似。在强化学习中，我们希望提高我们的策略以最大化长远的回报，但是没有标注的样本告诉我们在某个状态x哪个动作y是正确的。我们只有通过使用这个策略在不同的状态下执行不同的动作，观测该动作带来的回报，从而改善我们的策略。在以上这个翻译对偶游戏中，两个翻译模型就是我们的策略，因为没有标注的双语句对，所以我们不能直接改善它们。这个对偶游戏把一个没有标注的句子x，先翻译成另外一种语言的句子y1，再翻译回来为x1，这里x就是强化学习中的状态，y1和x1就是我们的策略所执行的动作，x和x1的相似度就是我们获得的回报。&lt;/p&gt;

&lt;p&gt;我们可以用已有的强化学习的算法来训练我们这两个翻译模型，比如策略梯度方法。策略梯度方法的基本思想非常简单：如果我们在执行某个动作之后，观测到了一个很大的回报，我们就通过调整策略（在当前策略函数的参数上加上它的梯度）来增加这个状态下执行这个动作的概率；相反，如果我们在执行某个动作之后，观测到了一个很小的回报，甚至是负的回报，那么我们就需要调整策略（在当前策略函数的参数上减去它的梯度），以降低在这个状态下执行这个动作的概率。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>deep learning book-第4章 Numerical Computation</title>
   <link href="http://daiwk.github.io/posts/dl-dlbook-chap4.html"/>
   <updated>2016-12-05T00:00:00+00:00</updated>
   <id>/posts/dl-dlbook-chap4</id>
   <content type="html">&lt;p&gt;几个git链接：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/HFTrader/DeepLearningBook&quot;&gt;https://github.com/HFTrader/DeepLearningBook&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-CN&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-CN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目录：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;4.1 Overflow and Underflow&lt;/li&gt;
  &lt;li&gt;4.2 Poor Conditioning&lt;/li&gt;
  &lt;li&gt;4.3 Gradient-Based Optimization
    &lt;ul&gt;
      &lt;li&gt;Beyond the Gradient: Jacobian and Hessian Matrices&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;4.4 Constrained Optimization&lt;/li&gt;
  &lt;li&gt;4.5 Example: Linear Least Squares&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;问题：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;1.为什么会出现underflow或overflow?&lt;/li&gt;
  &lt;li&gt;2.condition number的含义&amp;amp;condition的作用&lt;/li&gt;
  &lt;li&gt;3.海森矩阵为什么可以帮助梯度下降法跨越鞍点&lt;/li&gt;
  &lt;li&gt;4.公式4.21怎么对X求导的，矩阵求导法则？&lt;/li&gt;
  &lt;li&gt;5.一阶优化算法与二阶优化算法的优劣对比&lt;/li&gt;
  &lt;li&gt;6.KKT条件什么时候是最优化问题可以求取最优值的充要条件？&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>deep learning book-第3章 Probability and Information Theory</title>
   <link href="http://daiwk.github.io/posts/dl-dlbook-chap3.html"/>
   <updated>2016-12-03T00:00:00+00:00</updated>
   <id>/posts/dl-dlbook-chap3</id>
   <content type="html">&lt;p&gt;几个git链接：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/HFTrader/DeepLearningBook&quot;&gt;https://github.com/HFTrader/DeepLearningBook&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-CN&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-CN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;几个问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;1.什么是熵，熵代表什么意义&lt;/li&gt;
  &lt;li&gt;2.熵与KL散度有什么关系&lt;/li&gt;
  &lt;li&gt;3.KL散度代表什么意义，为什么是不对称的&lt;/li&gt;
  &lt;li&gt;4.有向概率图模型和无向图模型的区别&lt;/li&gt;
  &lt;li&gt;5.概率密度函数的数值意义是什么？&lt;/li&gt;
  &lt;li&gt;6.We can thus think of the normal distribution as being the one that inserts the least amount of prior knowledge into a model. 原文中这句话怎么理解&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目录：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;3.1 Why Probability?&lt;/li&gt;
  &lt;li&gt;3.2 Random Variables&lt;/li&gt;
  &lt;li&gt;3.3 Probability Distributions
    &lt;ul&gt;
      &lt;li&gt;Discrete Variables and Probability Mass Functions&lt;/li&gt;
      &lt;li&gt;Continuous Variables and Probability Density Functions&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;3.4 Marginal Probability&lt;/li&gt;
  &lt;li&gt;3.5 Conditional Probability&lt;/li&gt;
  &lt;li&gt;3.6 The Chain Rule of Conditional Probabilities&lt;/li&gt;
  &lt;li&gt;3.7 Independence and Conditional Independence&lt;/li&gt;
  &lt;li&gt;3.8 Expectation, Variance and Covariance&lt;/li&gt;
  &lt;li&gt;3.9 Common Probability Distributions
    &lt;ul&gt;
      &lt;li&gt;Bernoulli Distribution&lt;/li&gt;
      &lt;li&gt;Multinoulli Distribution&lt;/li&gt;
      &lt;li&gt;Gaussian Distribution&lt;/li&gt;
      &lt;li&gt;Exponential and Laplace Distributions&lt;/li&gt;
      &lt;li&gt;The Dirac Distribution and Empirical Distribution&lt;/li&gt;
      &lt;li&gt;Mixtures of Distributions&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;3.10 Useful Properties of Common Functions&lt;/li&gt;
  &lt;li&gt;3.11 Bayes’ Rule&lt;/li&gt;
  &lt;li&gt;3.12 Technical Details of Continuous Variables&lt;/li&gt;
  &lt;li&gt;3.13 Information Theory&lt;/li&gt;
  &lt;li&gt;3.14 Structured Probabilistic Models&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>deep learning book-第2章 Linear Algebra</title>
   <link href="http://daiwk.github.io/posts/dl-dlbook-chap2.html"/>
   <updated>2016-12-02T00:00:00+00:00</updated>
   <id>/posts/dl-dlbook-chap2</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#21-scalars-vectors-matrices-and-tensors&quot;&gt;2.1 Scalars, Vectors, Matrices and Tensors&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#22-multiplying-matrices-and-vectors&quot;&gt;2.2 Multiplying Matrices and Vectors&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#23-identity-and-inverse-matrices&quot;&gt;2.3 Identity and Inverse Matrices&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#24-linear-dependence-and-span&quot;&gt;2.4 Linear Dependence and Span&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#25-norms&quot;&gt;2.5 Norms&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#26-special-kinds-of-matrices-and-vectors&quot;&gt;2.6 Special Kinds of Matrices and Vectors&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#27-eigendecomposition&quot;&gt;2.7 Eigendecomposition&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#28-singular-value-decomposition&quot;&gt;2.8 Singular Value Decomposition&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#29-the-moore-penrose-pseudoinverse&quot;&gt;2.9 The Moore-Penrose Pseudoinverse&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#210-the-trace-operator&quot;&gt;2.10 The Trace Operator&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#211-the-determinant&quot;&gt;2.11 The Determinant&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#212-example-principal-components-analysis&quot;&gt;2.12 Example: Principal Components Analysis&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;几个git链接：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/HFTrader/DeepLearningBook&quot;&gt;https://github.com/HFTrader/DeepLearningBook&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-CN&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-CN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目录：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;2.1 Scalars, Vectors, Matrices and Tensors&lt;/li&gt;
  &lt;li&gt;2.2 Multiplying Matrices and Vectors&lt;/li&gt;
  &lt;li&gt;2.3 Identity and Inverse Matrices&lt;/li&gt;
  &lt;li&gt;2.4 Linear Dependence and Span&lt;/li&gt;
  &lt;li&gt;2.5 Norms&lt;/li&gt;
  &lt;li&gt;2.6 Special Kinds of Matrices and Vectors&lt;/li&gt;
  &lt;li&gt;2.7 Eigendecomposition&lt;/li&gt;
  &lt;li&gt;2.8 Singular Value Decomposition&lt;/li&gt;
  &lt;li&gt;2.9 The Moore-Penrose Pseudoinverse&lt;/li&gt;
  &lt;li&gt;2.10 The Trace Operator&lt;/li&gt;
  &lt;li&gt;2.11 The Determinant&lt;/li&gt;
  &lt;li&gt;2.12 Example: Principal Components Analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;21-scalars-vectors-matrices-and-tensors&quot;&gt;2.1 Scalars, Vectors, Matrices and Tensors&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Scalars：标量，一个单独的数字。用斜体小写字母表示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a,n,x\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;vectors：向量，一个1-D的数组(&lt;strong&gt;默认都是列向量！！&lt;/strong&gt;)。用小写斜体加粗表示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{x}\in \mathbb{R}^n\)&lt;/code&gt;：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\boldsymbol{x} = \begin{bmatrix}x_1
\\ x_2
\\ ...
\\ x_n
\end{bmatrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;matrices：矩阵，一个2-D的数组。用大写斜体加粗表示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{A}\in \mathbb{R}^{m\times n}\)&lt;/code&gt;：
    &lt;ul&gt;
      &lt;li&gt;矩阵转置，简单理解就是将矩阵沿着主对角线做一次镜像：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((\boldsymbol{A}^T)_{i,j}=\boldsymbol{A}_{j,i}\)&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;../assets/deeplearningbook/chap2/matrix_transpose.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
(\boldsymbol{A}\boldsymbol{B})^T=\boldsymbol{B}^T\boldsymbol{A}^T
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;tensors：张量：0维==&amp;gt;标量；1维==&amp;gt;向量；2维==&amp;gt;矩阵；可以更多维(用加粗大写非斜体表示)&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{A}\in \mathbb{R}^{i\times j\times k}\)&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;22-multiplying-matrices-and-vectors&quot;&gt;2.2 Multiplying Matrices and Vectors&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;矩阵点积（dot product）&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\\boldsymbol{C}=\boldsymbol{A}\boldsymbol{B}
\\C_{i,j}=\sum _kA_{i,k}B_{k,j}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;另外，矩阵的&lt;strong&gt;Hadamard product（element-wise product）&lt;/strong&gt;是对应元素相乘：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\\boldsymbol{C}=\boldsymbol{A}\odot \boldsymbol{B}
\\C_{i,j}=A_{i,j}B_{k,j}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;矩阵的点积具有&lt;strong&gt;分配律和结合律&lt;/strong&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\\boldsymbol{A}(\boldsymbol{B}+\boldsymbol{C})=\boldsymbol{A}\boldsymbol{B}+\boldsymbol{A}\boldsymbol{C}
\\(\boldsymbol{A}\boldsymbol{B})\boldsymbol{C}=\boldsymbol{A}(\boldsymbol{B}\boldsymbol{C})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;注意，对于&lt;strong&gt;向量&lt;/strong&gt;而言（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{x}\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\times 1\)&lt;/code&gt;的列向量，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{x}^T\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1\times n\)&lt;/code&gt;的行向量）,所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{x}_T\boldsymbol{y}\)&lt;/code&gt;是一个标量，他等于他的转置：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\boldsymbol{x}^T\boldsymbol{y}=(\boldsymbol{x}^T\boldsymbol{y})^T=\boldsymbol{y}^T\boldsymbol{x}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;接下来看一下线性方程组（linear equations），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{A}\in \mathbb{R}^{m\times n}\)&lt;/code&gt;是一个已知的矩阵，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{b}\in \mathbb{R}^{m}\)&lt;/code&gt;是一个已知的向量，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{x}\in \mathbb{R}^{n}\)&lt;/code&gt;是一个未知的向量：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\boldsymbol{x}^T\boldsymbol{y}=(\boldsymbol{x}^T\boldsymbol{y})^T=\boldsymbol{y}^T\boldsymbol{x}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;等价于&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\\boldsymbol{A}_{1,:}\boldsymbol{x}=\boldsymbol{A}_{1,1}x_1+\boldsymbol{A}_{1,2}x_2+...+\boldsymbol{A}_{1,n}x_n=b1
\\\boldsymbol{A}_{2,:}\boldsymbol{x}=\boldsymbol{A}_{2,1}x_1+\boldsymbol{A}_{2,2}x_2+...+\boldsymbol{A}_{2,n}x_n=b2
\\...
\\\boldsymbol{A}_{m,:}\boldsymbol{x}=\boldsymbol{A}_{m,1}x_1+\boldsymbol{A}_{m,2}x_2+...+\boldsymbol{A}_{m,n}x_n=bm
\]&lt;/code&gt;&lt;/p&gt;

&lt;h1 id=&quot;23-identity-and-inverse-matrices&quot;&gt;2.3 Identity and Inverse Matrices&lt;/h1&gt;

&lt;p&gt;单位矩阵：主对角线全1，其他元素全0的矩阵。单位矩阵和任何向量相乘，结果都是该向量本身。即，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{I}_n\in \mathbb{R}_{n\times n}\)&lt;/code&gt;，有：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\forall \boldsymbol{x}\in \mathbb{R}_{n},\ \boldsymbol{I}_n\boldsymbol{x}=\boldsymbol{x}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;矩阵的逆：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{A}^{-1}\)&lt;/code&gt;满足：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{A}^{-1}\boldsymbol{A}=\boldsymbol{I}_n\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以可以对方程组进行求解：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{x}=\boldsymbol{A}^-1\boldsymbol{b}\)&lt;/code&gt;&lt;/p&gt;

&lt;h1 id=&quot;24-linear-dependence-and-span&quot;&gt;2.4 Linear Dependence and Span&lt;/h1&gt;

&lt;p&gt;线性方程组的解只有以下三种情况：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;无解&lt;/li&gt;
  &lt;li&gt;有无穷多的解：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{x}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{y}\)&lt;/code&gt;都是解，那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{z}=\alpha \boldsymbol{x}+(1-\alpha )\boldsymbol{y}\)&lt;/code&gt;也是解&lt;/li&gt;
  &lt;li&gt;只有一个解&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;为了分析方程组有多少个解，我们可以从这个角度来理解线性方程组：我们从m维零向量出发，经过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{A}\)&lt;/code&gt;的n个方向的变换，最终到达m维目标向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{b}\)&lt;/code&gt;所在的位置。那么，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;就表示在这n个方向上，我分别走了多少步。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\boldsymbol{A}\boldsymbol{x}=\sum _i x_i\boldsymbol{A}_{:,i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;上面的式子表示，n个m维向量相加，得到一个m维向量这种操作称为线性组合（linear combination）。n个向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\({\boldsymbol{v}^{(1)},...,\boldsymbol{v}^{(n)}}\)&lt;/code&gt;的线性组合，指的就是每一个向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\boldsymbol{v}^{(i)}\)&lt;/code&gt;都乘以一个系数，并进行累加得到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\sum _ic_i\boldsymbol{v}^{(i)}\)&lt;/code&gt;。&lt;/p&gt;

&lt;h1 id=&quot;25-norms&quot;&gt;2.5 Norms&lt;/h1&gt;

&lt;h1 id=&quot;26-special-kinds-of-matrices-and-vectors&quot;&gt;2.6 Special Kinds of Matrices and Vectors&lt;/h1&gt;

&lt;h1 id=&quot;27-eigendecomposition&quot;&gt;2.7 Eigendecomposition&lt;/h1&gt;

&lt;h1 id=&quot;28-singular-value-decomposition&quot;&gt;2.8 Singular Value Decomposition&lt;/h1&gt;

&lt;h1 id=&quot;29-the-moore-penrose-pseudoinverse&quot;&gt;2.9 The Moore-Penrose Pseudoinverse&lt;/h1&gt;

&lt;h1 id=&quot;210-the-trace-operator&quot;&gt;2.10 The Trace Operator&lt;/h1&gt;

&lt;h1 id=&quot;211-the-determinant&quot;&gt;2.11 The Determinant&lt;/h1&gt;

&lt;h1 id=&quot;212-example-principal-components-analysis&quot;&gt;2.12 Example: Principal Components Analysis&lt;/h1&gt;

</content>
 </entry>
 
 <entry>
   <title>强化学习概述</title>
   <link href="http://daiwk.github.io/posts/rl-introduction.html"/>
   <updated>2016-12-01T00:00:00+00:00</updated>
   <id>/posts/rl-introduction</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%BC%BA%E5%8C%96%E5%AD%A6%E4%B9%A0%E6%A6%82%E8%BF%B0&quot;&gt;强化学习概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E4%BB%B7%E5%80%BC%E5%87%BD%E6%95%B0%EF%BC%88value-function%EF%BC%89&quot;&gt;价值函数（VALUE FUNCTION）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E8%B4%B4%E7%8E%B0%E5%9B%A0%E5%AD%90%EF%BC%88discounting-factor%EF%BC%89&quot;&gt;贴现因子（DISCOUNTING FACTOR）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E7%AD%96%E7%95%A5%E5%87%BD%E6%95%B0%EF%BC%88policy-function%EF%BC%89&quot;&gt;策略函数（POLICY FUNCTION）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#q-%E5%87%BD%E6%95%B0%EF%BC%88q-function%EF%BC%89&quot;&gt;Q-函数（Q-FUNCTION）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#q-%E5%AD%A6%E4%B9%A0%EF%BC%88q-learning%EF%BC%89&quot;&gt;Q-学习（Q-Learning）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%B7%B1%E5%BA%A6-q-%E5%AD%A6%E4%B9%A0%EF%BC%88deep-q-learning%EF%BC%89&quot;&gt;深度 Q-学习（DEEP Q-LEARNING）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#alphago&quot;&gt;AlphaGo&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%85%B6%E4%BB%96&quot;&gt;其他&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#q-learning%E5%92%8Cpolicy-gradient%E7%9A%84%E5%8C%BA%E5%88%AB&quot;&gt;q learning和policy gradient的区别&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%9B%9E%E7%AD%941by-%E4%BF%9E%E6%89%AC%EF%BC%9A&quot;&gt;回答1(by 俞扬)：&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%9B%9E%E7%AD%942%EF%BC%9A&quot;&gt;回答2：&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#actor-critic&quot;&gt;Actor-Critic&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;更多资料参见&lt;a href=&quot;http://www.wildml.com/2016/10/learning-reinforcement-learning/&quot;&gt;wildml的rl专题&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;from &lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650719294&amp;amp;idx=1&amp;amp;sn=f1a01cd6710e6ea9629619cd3324d102&amp;amp;chksm=871b0040b06c895642ff961a6fe81f05c5e9776aff5da4845f2d3d874f88213863afd2059833&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1002mDtDsEDixxCswQJOs2rH&amp;amp;pass_ticket=DoiMlYDlmCK%2FTS99n6JzBzzsHdN7QoyC81j%2BvUNHFkqqmuADrJsZlH0yXSTgpVEB#rd&quot;&gt;机器之心翻译&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;选自 devblogs.nvidia.com;机器之心编译;参与：武竞、Rick R、吴攀&lt;/p&gt;

&lt;p&gt;本文是 NVIDIA 博客上 Tim Dettmers 所写的《Deep Learning in a Nutshell》系列文章的第四篇。据介绍，该系列文章的目的是「提供对每一种概念的理解而不是其数学和理论上的细节」。除了必要理解的数学术语之外，这些文章还提供了直观的类比和图像来帮助理解深度学习。在此之前，作者已经探讨了深度学习的核心概念、深度学习的训练及其历史、序列学习，本文（第四篇）的主题是强化学习（Reinforcement Learning）。&lt;/p&gt;

&lt;h2 id=&quot;强化学习概述&quot;&gt;强化学习概述&lt;/h2&gt;

&lt;p&gt;学习骑自行车需要试错（trial and error），这很像强化学习。&lt;/p&gt;

&lt;p&gt;还记得你是如何学习骑自行车的吗？一般都有一个大人在你旁边或你后面帮助你，并鼓励你骑出你自行车生涯的第一步，在你绊倒或摔倒的时候还会帮助你再次尝试。解释儿童学习骑自行车的方式是很困难的，而即使很好的解释对从没骑过自行车的人而言也不会有多少意义：你必须自己去感受。&lt;/p&gt;

&lt;p&gt;所以如果你不能解释如何骑上自行车的，又怎么能学会骑自行车呢？事实上，你是在不断地通过尝试进行学习，你很可能会跌倒，或至少可能会突然停下然后不得不控制好自己。你会不断跌倒或跌跌撞撞——直到你突然掌握了一些诀窍，能在你再次跌倒前前进几米了。
在这个学习过程中，反馈信号告诉我们：我们所做的要么会带来痛苦（嗷，痛死啦！我下次不能再这样做了！），要么会带来奖励（喔！我骑上去了！感觉好棒！我只需要一直这样做就行了！）&lt;/p&gt;

&lt;p&gt;在强化学习的问题上，我们认为存在一个代理（agent）在尝试通过决策来最大化其所能收到的奖励。因此一个可以收到最大可能的奖励的代理可以被看作是在给定的状态下执行了最好的动作。这里所谓的代理（agent）是指一种抽象的实体，它可以是任何执行动作的对象或主体：自动汽车、机器人、人类、客户支持聊天机器人、围棋棋手。代理的状态（state）是指代理的位置以及代理在其抽象环境中的存在状态；比如说，在虚拟现实世界中的一个特定位置、一栋建筑、一个国际象棋棋盘或在赛道上的位置和速度。&lt;/p&gt;

&lt;p&gt;为了简化强化学习的问题和解决方案，通常会对环境进行简化，使代理只知道对决策重要的细节，而其它的都可以被抛弃。就像学习骑自行车的例子一样，强化学习也只有两个可以学习的反馈源：惩罚（penalty，跌倒的痛苦）和奖励（reward，成功骑行几米的快乐）。如果我们将惩罚看作是负奖励（negative reward），那么整个学习问题都将是关于探索环境和经过一个又一个的状态来尝试最大化我们的代理所得到的奖励，直到实现目标状态（从 A 到 B 自动驾驶；赢得一场国际象棋比赛；通过聊天解决一个客户问题）：简而言之，这就是强化学习。&lt;/p&gt;

&lt;h2 id=&quot;价值函数value-function&quot;&gt;价值函数（VALUE FUNCTION）&lt;/h2&gt;

&lt;p&gt;强化学习是通过奖励或惩罚来学习怎样选择能产生最大积累奖励的行动的算法。为了找到这些行动，在我们目前的环境（environment）中首先找到最有价值的状态（states）是很有用的。举一个例子，在赛车跑道上最有价值的是终点线，这也是奖励最多的状态，因此在跑道之上的状态也比在跑道之外的状态更有价值。&lt;/p&gt;

&lt;p&gt;一旦我们确定了哪些状态是有价值的，我们就可以给这些状态赋回馈值。例如，当赛车偏离跑道时，这些状态就被赋予惩罚；而当赛车跑完一圈时，就会收到一个奖励；当赛车跑出当前最短的时间时，也会收到一个奖励；等等。&lt;/p&gt;

&lt;p&gt;我们可以将这些状态和回馈当成一个离散函数。比如把跑道看作 1×1 的方格。对应到这些方格中的奖励就可以用数字表示。例如目标状态的奖励可能为 10，而偏离跑道的方格的惩罚为 -2，其它方格的奖励为 0。假设这个函数是 3 维的，那么最佳的行动是让函数的值越高越好（即最大奖励）的行动。&lt;/p&gt;

&lt;p&gt;这些状态中，有些中间状态并不一定有奖励（奖励为 0），但是这些状态是通向奖励状态的必经之路。例如，你必须在跑完一圈之前转弯，或者必须跑完一圈才有机会跑出最短时间。&lt;/p&gt;

&lt;p&gt;经过训练的或设定好的价值函数会给中间状态赋予部分奖励，例如，认为赛车完成第二个转弯比第一个更有价值，因为第二个转弯代表了更接近目标的状态。价值函数通过提供中间奖励，能帮助代理（agent）更容易地判断下一步要移动到哪个状态。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/rl/intro/value_function.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;图 1：通过全状态的价值迭代（value iteration）构建价值函数（value function）。每一个方格代表一个状态：S 是起始状态，G 是目标状态，T 是陷阱，而黑色的方格不能进入。价值迭代中，我们初始化陷阱状态和目标状态的回馈，然后这些回馈值就会随着时间传递到所有状态直至达到平衡。取决于陷阱状态的惩罚值和目标状态的奖励值，不同的回馈值可能产生不同的解决方案；最后的两张图是两种不同的解。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/rl/intro/value_function_2.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;图 2：转弯处不同的赛车线。不同赛车线的距离不同，可能的速度不同，磨损轮胎的力度也不同。优化跑圈时间的价值函数会找到使转弯处的总时间耗费最少的状态转换（state transitions）。&lt;/p&gt;

&lt;p&gt;这个框架说明了，每一个状态的值都会被周围状态的值所影响。图 1 每一个方格代表一个状态：S 和 G 分别是起始状态和目标状态，T 方块是陷阱，而黑色的方格不能进入。目的是从状态 S 移动到状态 G。把这些格子当作赛车跑道，那么随着时间推移我们创建了一个「斜坡」，连接起始状态和目标状态，所以只要沿着最陡峭的方向前进就可以达到目标。这就是价值函数的主旨思想：提供每个状态的价值估计，从而我们可以一步一步根据奖励做出决策。&lt;/p&gt;

&lt;p&gt;价值函数也可以察觉细微的问题。例如，相比偏离跑道外的状态，赛车在跑道两侧的惩罚并没有那么大；但是这并不能有效地将赛车纠正回跑道中心，也就无法达到理想的跑圈时间（赛车无法在高速下转弯）。因此，安全和速度二者无法兼顾，价值函数在赛道上找到一条最适合的既快又安全的路径。通过更改回馈值，可以调整最后的解决方案。例如，如果跑圈时间越短奖励越多，那么价值函数会给危险但可能使时间更短的状态赋予更高的价值。&lt;/p&gt;

&lt;h2 id=&quot;贴现因子discounting-factor&quot;&gt;贴现因子（DISCOUNTING FACTOR）&lt;/h2&gt;

&lt;p&gt;尽管价值函数决定回馈值的大小，agent 采取的行动还取决于一个叫贴现因子（discounting factor）的参数，它限定了 agent 会在多大程度上受到较远状态的影响。当状态改变，局部回馈值会乘以贴现因子计入价值函数。当贴现因子为 0.5 时，仅经过 3 次状态改变，回馈值就会变成初始值的八分之一，所以 agent 更倾向于搜索临近状态的回馈值。因此贴现因子是决定价值函数推荐稳健还是激进行动的权重要素。&lt;/p&gt;

&lt;p&gt;例如，跑得快的赛车可能得到高奖励，所以一个接近于零的贴现因子就会导向跑得越快越好的激进策略。但是一个谨慎的 agent 知道，当前方有急转弯时，跑得越快越好并不是一个好策略；而在恰当时机将速度慢下来，可能会在之后得到一个更短的跑圈时间，因此得到更高的奖励。要有这样的远见，agent 需要将跑得快的奖励延后，所以一个接近于 1 的贴现因子可能会更好。&lt;/p&gt;

&lt;p&gt;价值函数的贴现因子保证了，远处的回馈值能根据距离或步数部分的减少。这个因子通常被设定为一个值，这个值很重视长期的回馈，但又不是那么重视（例如每经过一次状态转换衰减 5%）。&lt;/p&gt;

&lt;p&gt;该算法首先把所有状态的回馈值初始化为 0 或者特定的值，然后搜索所有状态的可能的下一个状态，并估计下一个状态 agent 可能得到的回馈，通过这种方式学习每一个状态特有的局部回馈值。如果 agent 在下一个状态得到回馈，那么这个回馈就会累计到当前的状态中。重复这个过程，直到每个状态的局部回馈值不再改变，意味着每次变换状态采取的可能转向以及每个状态的回馈值都被考虑在内。图 1 为价值迭代的过程。&lt;/p&gt;

&lt;p&gt;这种方法也许起初看起来并不高效，但是动态规划（dynamic programming）的技巧可以让它更加高效。在子问题已经解决的前提下，动态规划能解决更高层次的问题：B 到 C 的回馈值可以被用于计算 A-&amp;gt;B-&amp;gt;C 和 D→B-&amp;gt;C 的链式状态的回馈值。&lt;/p&gt;

&lt;p&gt;总之，价值函数和价值迭代提供了一个局部回馈值的地图，agent 根据它就可以找到有最大奖励的状态。&lt;/p&gt;

&lt;h2 id=&quot;策略函数policy-function&quot;&gt;策略函数（POLICY FUNCTION）&lt;/h2&gt;

&lt;p&gt;策略函数（policy function）是根据价值函数选择产生最大（长期）奖励的行动的一组策略。在所有可能的下一步行动中，通常没有明确的优胜者。例如，agent 面临选择下一步进入 4 个状态 A, B, C, D 中的一个，它们的奖励分别为 A=10, B=10, C=5, D=5。A 与 B 都是好的即刻选择，但是随着时间的推移，A 状态之后的路径得到的奖励可能比 B 状态好得多，或者进入 C 状态的行动甚至是最好的选择。所以在训练的过程中，探索所有的选择是值得的，但同时，如果只看到即刻奖励，就可能会导致非最优的选择。那么，我们如何在探索尽量高的奖励和探索尽量少的路径之间找到一个平衡点呢？&lt;/p&gt;

&lt;p&gt;一个聪明的方法是按照奖励值的比例随机选取状态。在这个例子中，选择 A 的概率为 33% ( 10/(10+10+5+5) )，同样选择 B, C, D 的概率分别为 33%, 16%, 16%。策略函数的随机选择的原理对学习一个好的策略至关重要。因为，也许存在一个看上去是反直觉的，但是有效甚至关键的成功策略。&lt;/p&gt;

&lt;p&gt;例如，如果你以跑得快来训练赛车，它会尝试以最快的速度在转弯抄近道。但是当你把其它选手也纳入模型时，这个策略并不是最优的。agent 会将其他选手在转弯处减速考虑在内，以免发生超车甚至撞车。另一个场景是，在转弯处高速行驶可能会更快的磨损轮胎，导致赛程中的停顿，浪费宝贵的时间。&lt;/p&gt;

&lt;p&gt;值得注意的是，策略函数和价值函数相互依赖。给定一个价值函数，不同的策略会导致不同的选择；同样给定一个策略函数，agent 会将不同行动赋予不同的价值。给一个棋类游戏赋予「快去赢」的策略，价值函数就会对赢棋可能性高的行动赋予高价值（牺牲棋子以获得赢棋胜算会被赋予高价值）。但是如果赋予「以大比分领先获胜」的策略，那么策略函数会学习在特定的游戏中选择能最大化得分的行动（从不牺牲棋子）。&lt;/p&gt;

&lt;p&gt;这些只是许多例子中的两个。如果想得到特定的结果，我们可以同时使用策略和价值函数来指导 agent 学习策略，最终达到特定的结果。这使得强化学习既灵活又强大。&lt;/p&gt;

&lt;p&gt;我们训练策略函数通过（1）随机初始化——例如，让每一个状态被选择的概率与它的回馈值成正比——然后用这些回馈值初始化价值函数；即把所有没有直接回馈值状态的回馈值设成 0（如跑道终点的奖励为 10，偏离跑道外的惩罚为 -2，所有跑道上的状态的回馈值为 0）。（2）训练价值函数知道收敛（见图 1）.（3）对于特定状态（状态 A），增大能让奖励增加最多的行动（从 A 移动到 B）的概率值（相比从 A 到 C，这种移动也许会导致很低甚至负的回馈值，像牺牲棋子，但是它仍可以最终赢得游戏）。（4）最后，重复步骤（1）直到策略不再改变。&lt;/p&gt;

&lt;h2 id=&quot;q-函数q-function&quot;&gt;Q-函数（Q-FUNCTION）&lt;/h2&gt;

&lt;p&gt;我们已经看到策略和价值函数是高度相依的：我们的策略大多取决于我们看重什么，而我们看重什么决定了我们的行动。因此我们或许可以把策略和价值函数结合起来，这个结合就叫 Q-函数（Q-function）。&lt;/p&gt;

&lt;p&gt;Q-函数考虑了当前的状态（如价值函数）和下一步行动（如策略函数），然后针对状态-行动组合，返回局部回馈值。在更复杂的情况下，Q-函数可能会结合更多状态来预测下一步状态。例如，如果行动的方向是很重要的，我们需要至少 2 个状态去预测下一步状态，因为只用一个状态（如一幅静止图像）去推断精确的方向几乎是不可能的。我们也可以将输入状态传给 Q-函数去得到每个可能状态的局部回馈值。然后我们可以按照局部回馈值的比例，随机选取下一步的行动（这种方法叫探索（exploration））；或者直接选取最高价值的行动（这种方法叫利用（exploitation））。&lt;/p&gt;

&lt;p&gt;然而，Q-函数的要旨实际上并不在此。设想一辆自动驾驶汽车：有太多的「状态」以至于无法建立一个覆盖所有状态的价值函数；地球上的所有道路的每一个可能的位置和可能的速度实在太多，要计算它们的局部回馈值几乎是不可能的。相反，Q-函数（1）只在一步的范围内查找所有可能的下一步状态然后（2）基于当前状态和下一步状态，查看最佳的可能行动。所以对于每一个下一步状态，Q-函数会向前探索一步（并不会探索所有可能的步数直到终止，如价值函数）。这些「向前探索一步」被表示为状态-行动组合。例如，状态 A 有 4 个可能行动，所以我们有行动组合 A-&amp;gt;A, A-&amp;gt;B, A-&amp;gt;C, A→D。对每一个状态有 4 个行动的一个 10×10 网格的状态，我们可以将整个 Q-函数表示为 4 个 10×10 的矩阵，或一个 10×10×4 的张量（tensor）。图 3 展示了一个网格问题（在 2D 平面上每步可以移动到相邻状态，目标为右下角的 G）的解。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/rl/intro/q_function.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;图 3：一个含有障碍状态（黑色方格）网格问题的 Q-函数，目标为右下角的 G。图示的 4 个矩阵展示了每个状态 4 种行动的回馈值（或者 Q-函数中的 Q-值），绿色颜色越深代表 Q-值越高。agent 会以更高的概率选择颜色更深的状态，或在贪婪（greedy）的情况下直接选择当前状态周围颜色最深的做行动。这会使 agent 以最快的速度达到目标。这个行为是与初始位置独立的。&lt;/p&gt;

&lt;p&gt;在有些情况下我们需要为绝对无限状态建模。例如，自动驾驶汽车的「状态」经常会被表示为一个连续函数，如神经网络，神经网络会将所有状态的变量（如速度和位置）纳入，然后对每一个行动输出 Q-值。&lt;/p&gt;

&lt;p&gt;为什么只获取一些状态的信息是有益的呢？很多状态是非常相关的，所以在两个不同但是相似的状态采取相同的行动可能都会取得成功。例如，跑道上每一个转弯是不同的，但是每一个左转弯赛车学到的内容——什么时候开始转弯，怎样调整速度等等——对下一个左转弯时有用的。所以，随着时间的推移，一个赛车 agent 会学到越来越好的左转弯技术，将来即使遇到没见过的赛道也会轻松应对。&lt;/p&gt;

&lt;h2 id=&quot;q-学习q-learning&quot;&gt;Q-学习（Q-Learning）&lt;/h2&gt;

&lt;p&gt;为了训练 Q-函数我们将所有状态-行动组合的 Q-值初始化为零，并将一些状态给定的回馈值作为这些状态的初始化值。起初 agent 并不知道如何获得奖励（agent 只能看到下一个状态的 Q-值，初始时都为 0），agent 可能会探索很多状态直到发现一个奖励。因此我们会对训练 Q-函数定义一个训练长度（例如 100 步），或者定义训练直到达到某些状态（跑道上完成一圈）。这保证了我们不会卡在学习一个无用状态行动的过程中，这些无用状态可能不管做了多少次迭代却仍无法得到有用的回馈值。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/rl/intro/q_learning.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;图 4：网格中的 Q-学习，S 是起始状态，G 是目标状态，T 方格是陷阱，黑色的方格是障碍状态。在 Q-学习过程中，agent 一步一步探索环境，起初并不知道目标状态 G。一旦目标状态到起始状态附近的路径建立，算法会迅速收敛到一个解，然后再进一步找到问题的最佳策略。&lt;/p&gt;

&lt;p&gt;学习 Q-函数是从结果（奖励）到开始（起始状态）进行的。图 4 描绘了与图 1 比较的 Q-学习的网格变化。假设目标是以最少的步数达到目标状态 G。最开始 agent 随机移动直到（碰巧）进入陷阱或目标状态。因为陷阱离起始状态很近，agent 很可能最初会先碰到陷阱，但是一旦 agent 在磕磕绊绊中遇到目标状态，这种情况就会改变。从那以后的迭代，到达目标状态之前的那些状态（向前探索一步）就会被赋予局部奖励，同时因为奖励更接近起始位置，agent 也更可能遇到这样的奖励状态。通过这种方式，我们建立起一连串从目标到起始状态的局部回馈值，而且 agent 越是常遇到有局部回馈值的状态，局部回馈值就收敛得越快（见图 4）。&lt;/p&gt;

&lt;h2 id=&quot;深度-q-学习deep-q-learning&quot;&gt;深度 Q-学习（DEEP Q-LEARNING）&lt;/h2&gt;

&lt;p&gt;一辆自动驾驶汽车也许需要考虑许多状态：每一个不同速度和位置的组合就是一个不同的状态。但是大多数状态是相似的。将相似的状态放在一起并认为它们有相似的 Q-值是否可行？这就是深度学习需要派上用场的地方。&lt;/p&gt;

&lt;p&gt;我们可以将当前驾驶员看到的视野——一张图像——输入到卷积神经网络（CNN），训练它预测下一个可能行动的回馈值。因为相似状态的图像也是相似的（许多左转弯看起来相似），它们也会导致相似的行动。例如。神经网络会生成许多左转弯，并且甚至在没有遇到过的左转弯做出适当的行动。正如一个通过很多物品的图像训练的卷积神经网络能够准确识别这些物品一样，一个通过很多相似左转弯训练的网络也能够对不同左转弯做出速度和位置的微调。&lt;/p&gt;

&lt;p&gt;然而要成功地使用深度 Q 学习，我们不能简单地应用规则来训练之前所描述的 Q 函数。如果我们盲目地应用 Q 学习规则，那么网络将在进行左转弯时学习做好左转弯，但同时将开始忘记如何做好右转弯。这是因为所有神经网络的动作都使用相同的权重；调整左转弯的权重会使它们在其他情况中表现得更糟糕。解决方案是将所有输入图像和输出动作存储为「经验（experiences）」：即将状态、动作和奖励三者一起存储。&lt;/p&gt;

&lt;p&gt;运行了一段时间训练算法后，我们从迄今为止的所有经验中随机选择一个，并为神经网络权重创造一个均值更新，它能为每一个发生在那些经验期间的动作最大化 Q 值（奖励）。这样我们可以在同一时间教我们的神经网络左转和右转。由于在跑道上的较早期驾驶经验并不重要——因为它们源于我们代理的经验不足、甚至是初学者的一个时期——我们只跟踪固定数量的过去经验并忽略休息期。这个过程被称为经验回放（experience replay）。&lt;/p&gt;

&lt;p&gt;经验回放是一种由生物学所启发的方法。人脑中的海马体是每个大脑半球的强化学习中心。海马体储存我们白天制造的所有经验，但它的经验记忆能力有限，一旦达到记忆限度，学习会变得更加困难（考前填鸭式）。在夜间，海马体的这种记忆缓冲区被遍布皮质的神经活动清空到皮质中。皮质是大脑的「硬盘驱动器」，几乎所有记忆都储存在那里。手部动作的记忆存储在「手区」，听觉记忆存储在「听觉区」，等等。这种从海马体向外扩散的特征性神经活动称为睡眠纺锤（sleep spindle）。虽然目前没有强有力的证据来支持它，许多睡眠研究人员认为，我们通过做梦来帮助海马体将白天收集到的经验与我们在皮质中的记忆整合到一起，从而形成连贯的图片。&lt;/p&gt;

&lt;p&gt;所以你看，存储记忆与将记忆以某种协调的方式写回，这不仅对于深度强化学习、而且对于人类学习来说都是一个重要的过程。这种生物相似性给我们增加了一点信心，说明我们的大脑理论可能是正确的，也说明我们设计的算法走在了正确的道路上。&lt;/p&gt;

&lt;h2 id=&quot;alphago&quot;&gt;AlphaGo&lt;/h2&gt;

&lt;p&gt;由谷歌 DeepMind 所开发的 AlphaGo 在 2016 年制造了大新闻，它成为第一个在游戏中击败人类职业玩家的计算机程序。接着它以 4:1 击败了一位世界顶尖棋手李世石（Lee Sedol）。AlphaGo 结合了之前本文所提到的许多元素；即（1）价值（value）和（2）策略（policy）神经网络，其中（1）代表了围棋游戏中当前配置的值函数，从而预测每步棋之间的相对值；而（2）策略函数则表明应该选择走哪部棋以赢得游戏。这些网络是卷积网络，它把棋盘当做一张 19×19 的输入「图像」（每个位置一个像素）。&lt;/p&gt;

&lt;p&gt;由于我们已经有了许多围棋游戏的记录，通过使用来自专业人员的围棋游戏数据，这些记录对于训练策略网络很有用。该策略网络基于这些数据训练，以在给定游戏配置的游戏中预测出围棋冠军的下一步棋。&lt;/p&gt;

&lt;p&gt;一旦完成该监督训练阶段，强化学习就登场了。这里 AlphaGo 与自己对抗并试图改善其策略来挑选行动（策略网络），以评估谁将获胜（价值网络）。即使是仅仅训练策略网络，这种方法也比之前最著名的围棋算法 Pachi 更好，后者利用树搜索（tree-search）算法和启发式算法（heuristics）。然而仅仅在价值网络的帮助下，深度学习方法的性能仍然可以得到显著提高。&lt;/p&gt;

&lt;p&gt;当价值网络在整个游戏中被训练时往往很难进行推广，由于配置的高度相关性，网络会去学习识别一局比赛（比如说，如果这场比赛在 1978 年北京的 A vs. B，那么我会根据历史知道是 A 获胜了）而不是去识别好的走子。为了解决这个问题，DeepMind 生成了很多使 AlphaGo 与自身对抗的数据，然后选取每场比赛中的几个位置来训练价值网络。这与经验回放类似，其中我们从未独立地查看过冗长的动作序列，而是以非常不同的状态和行动的组合方式。&lt;/p&gt;

&lt;p&gt;价值网络表现出与带有 rollout 策略的蒙特卡洛（Monte Carlo）搜索树相似的表现，但是 AlphaGo 在深度学习方法的顶端使用了蒙特卡洛搜索树以取得更好的性能。带有推出策略的蒙特卡洛搜索树是什么？想象这样一个游戏配置树，其中一次行动就是一个边缘，而其节点是不同的游戏配置。例如你处于一个特定的游戏配置中，拥有 200 个可能的行动——也就是有 200 个节点连接到你的当前节点上——然后你选择某一次行动（一个边缘导致一个节点），从而产生一棵有着 199 个不同节点的新树，这些节点可在当前的行动之后被连接上。然而这棵树从未被完全扩大，因为它以指数级生长并需要太长时间来完全地被评估，所以在蒙特卡洛树搜索中，我们只采取一条沿着树进入到一定深度的路线，以使评估更有效。&lt;/p&gt;

&lt;p&gt;在 rollout 策略中，我们查看当前的状态，运用策略网络来选择树中下一节点的行动，并为每个玩家重复随后的所有行动，直到游戏结束得出胜负。这提供了另一种快速而贪婪的方法来评估一次行动的价值。然后这种洞察力也可以用来提高价值网络的准确性，有了更准确的价值函数，我们也能够进一步改善我们的策略函数：对一个行动是好是坏了解得更准确，使我们能够基于良好的、之前被认为是坏的行动来开发策略。或者换句话说，AlphaGo 能够思考一些对人类来说不可能办到、但却能赢得比赛的策略。&lt;/p&gt;

&lt;p&gt;AlphaGo 也使用蒙特卡洛搜索树用于训练。该树包含在每次迭代中更新的 Q 值边界、访问数 (N) 和一个先验概率 (P)。最初，Q 值、访问数和先验概率为零。一次迭代中，每次行动根据三个参数 (Q,N,P) 进行选择。例如要决定走 E5 是否为一步好棋，该行动后的新棋盘状态是由下述两个因素结合进行评估：（1）策略网络，为该行动设置初始先验概率；（2a）价值网络，为该行动分配一个值；（2b）蒙特卡洛 rollout，为该行动分配另一个值。步骤（2a）和（2b）通过一个参数和 Q 值进行加权，访问数 (N) 由该路径上的平均估值进行更新（如果该行动平均而言是相对好的则增加 Q 值，反之则减少 Q 值）。同一次行动的值随着每一次迭代的进行，由访问数（的增加）而略有减少，以至于将有更高的概率勘探到新的行动。这保证了勘探和开采之间的平衡。然而我们也得到越来越多对于已采取行动的准确估计。随着时间的推移，树为那些非常有力的行动分配更高的 Q 值。通过这个训练过程，AlphaGo 用每次训练迭代去学习采取更好的行动，从而学习哪次动作将赢得比赛。至此直到我们创造一个优于人类专家的围棋机器人，剩下的仅仅是计算能力和时间的问题了。&lt;/p&gt;

&lt;h2 id=&quot;其他&quot;&gt;其他&lt;/h2&gt;

&lt;h3 id=&quot;q-learning和policy-gradient的区别&quot;&gt;q learning和policy gradient的区别&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.zhihu.com/question/49787932&quot;&gt;https://www.zhihu.com/question/49787932&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;问题：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;这两种方法的本质上是否是一样的（解空间是否相等）？比如说如果可以收敛到最优解，那么对于同一个问题它们一定会收敛到一样的情况？&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;在Karpathy的blog中提到说更多的人更倾向于Policy Gradient，那么它们两种方法之间一些更细节的区别是什么呢？&lt;/li&gt;
  &lt;li&gt;有人愿意再对比一下action-critic就更好了(:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;问题3的回答见下一节。。&lt;/p&gt;

&lt;h4 id=&quot;回答1by-俞扬&quot;&gt;回答1(by 俞扬)：&lt;/h4&gt;

&lt;p&gt;Q-learning 是一种基于&lt;strong&gt;值函数估计&lt;/strong&gt;的强化学习方法，Policy Gradient是一种&lt;strong&gt;策略搜索&lt;/strong&gt;强化学习方法。两者是求解强化学习问题的不同方法，如果熟悉监督学习，前者可类比Naive Bayes——通过&lt;strong&gt;估计后验概率来得到预测&lt;/strong&gt;，后者可类比SVM——&lt;strong&gt;不估计后验概率而直接优化学习目标&lt;/strong&gt;。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;这两种方法的本质上是否是一样的（解空间是否相等）？比如说如果可以收敛到最优解，那么对于同一个问题它们一定会收敛到一样的情况？&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;两者是不同的求解方法，而&lt;strong&gt;解空间（策略空间）不是由求解方法确定的，而是由策略模型确定的&lt;/strong&gt;。两者可以使用相同的模型，例如相同大小的神经网络，这时它们的解空间是一样的。&lt;/p&gt;

&lt;p&gt;Q-learning在离散状态空间中&lt;strong&gt;理论上可以收敛到最优&lt;/strong&gt;策略，但&lt;strong&gt;收敛速度可能极慢&lt;/strong&gt;。在使用&lt;strong&gt;函数逼近后（例如使用神经网络策略模型）&lt;/strong&gt;则不一定。&lt;/p&gt;

&lt;p&gt;Policy Gradient由于&lt;strong&gt;使用梯度方法&lt;/strong&gt;求解&lt;strong&gt;非凸目标&lt;/strong&gt;，&lt;strong&gt;只能收敛到不动点&lt;/strong&gt;，不能证明收敛到最优策略。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;在Karpathy的blog中提到说更多的人更倾向于Policy Gradient，那么它们两种方法之间一些更细节的区别是什么呢？&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;基于&lt;strong&gt;值函数&lt;/strong&gt;的方法（Q-learning, SARSA等等经典强化学习研究的大部分算法）存在&lt;strong&gt;策略退化&lt;/strong&gt;问题，即值函数估计已经很准确了，但通过值函数得到的策略仍然不是最优。这一现象类似于监督学习中通过后验概率来分类，&lt;strong&gt;后验概率估计的精度很高，但得到的分类仍然可能是错的&lt;/strong&gt;，例如真实正类后验概率为0.501，如果估计为0.9，虽然差别有0.3，&lt;strong&gt;如果估计为0.499，虽然差别只有0.002，但分类确是错的&lt;/strong&gt;。尤其是当强化学习使用值函数近似时，策略退化现象非常常见。可见&lt;strong&gt;俞扬的&lt;a href=&quot;http://lamda.nju.edu.cn/yuy/Default.aspx?Page=adl-rl&amp;amp;AspxAutoDetectCookieSupport=1&quot;&gt;《Tutorial on Reinforcement Learning  slides》&lt;/a&gt;&lt;/strong&gt;中的例子。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Policy Gradient不会出现策略退化现象&lt;/strong&gt;，其&lt;strong&gt;目标表达更直接&lt;/strong&gt;，&lt;strong&gt;求解方法更现代&lt;/strong&gt;，还能够&lt;strong&gt;直接求解stochastic policy&lt;/strong&gt;等等优点更加实用。&lt;/p&gt;

&lt;h4 id=&quot;回答2&quot;&gt;回答2：&lt;/h4&gt;

&lt;p&gt;按我的理解，两者从本质上就是不一样的。和MDP里面的value iteration与policy iteration不同(&lt;a href=&quot;https://www.zhihu.com/question/41477987/answer/91389684&quot;&gt;MDP马尔可夫决策过程中的值迭代和策略迭代感觉并没有本质区别？ - nia nia 的回答&lt;/a&gt;)，Q Learning和Policy Gradient是在&lt;strong&gt;解决不同的问题&lt;/strong&gt;，而不是同一问题的两种不同的数值方法。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Q learning:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;首先，在modeling上，Q Learning&lt;strong&gt;假设policy是deterministic的&lt;/strong&gt;，而且它的&lt;strong&gt;求解空间是函数空间&lt;/strong&gt;，一个 &lt;strong&gt;(state, action) -&amp;gt; R 的函数&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;其次，在一些很弱的假设下(一般都成立)，Q Learning算法本身定义的&lt;strong&gt;迭代算子&lt;/strong&gt;是一个&lt;strong&gt;contraction operator(压缩算子，什么鬼。。。)&lt;/strong&gt;，所以在 t -&amp;gt; infinity &lt;strong&gt;保证收敛到全局最优&lt;/strong&gt;解 (&lt;strong&gt;最优解不一定对应唯一的policy&lt;/strong&gt;)。&lt;/p&gt;

&lt;p&gt;当然，和Machine Learning中要学习一个分布一样，&lt;strong&gt;实际操作中往往是parametrize Q函数&lt;/strong&gt;，假设Q函数是由有限维参数决定，然后求解最优的函数。这个时候收敛性就不好说了，比如&lt;strong&gt;像DQN用神经网络来描述，用随机梯度下降来求解&lt;/strong&gt;，当t -&amp;gt; infinity，replay buffer size -&amp;gt; infinity的情况下能保证收敛到最优的吗？我觉得答案是否定的。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;policy gradient:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在modeling上，Policy Gradient&lt;strong&gt;假设policy是stochastic&lt;/strong&gt;的，而且是&lt;strong&gt;服从一个parametrized的policy分布&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;得到这个&lt;strong&gt;最优的参数&lt;/strong&gt;就得到了&lt;strong&gt;在这个分布假设下最优的Policy&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;求解算法就是通过&lt;strong&gt;梯度下降，每一步迭代的梯度也是stochastic的，没有全局收敛性的保障&lt;/strong&gt;。如果learning rate逐渐下降能收敛到一个局部最优。&lt;/p&gt;

&lt;h3 id=&quot;actor-critic&quot;&gt;Actor-Critic&lt;/h3&gt;

&lt;p&gt;Actor-Critic(A3C，玩家-评委)就是在&lt;strong&gt;求解策略&lt;/strong&gt;的同时&lt;strong&gt;用值函数进行辅助&lt;/strong&gt;，用&lt;strong&gt;估计的值函数&lt;/strong&gt;替代&lt;strong&gt;采样的reward&lt;/strong&gt;，提高样本利用率。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actor-critic可被视作Policy Gradient&lt;/strong&gt;，唯一的区别是进一步&lt;strong&gt;通过Q function来降低Policy Gradient每一步stochastic梯度的variance&lt;/strong&gt;。&lt;strong&gt;Q function&lt;/strong&gt;也是用同样的sample，&lt;strong&gt;通过Q Learning来学习&lt;/strong&gt;的，可以说是充分利用了agent每一步探索得到的宝贵信息吧。&lt;/p&gt;

&lt;p&gt;可以参考&lt;a href=&quot;https://www.zhihu.com/question/56692640&quot;&gt;https://www.zhihu.com/question/56692640&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Deep Deterministic Policy Gradient(&lt;a href=&quot;https://arxiv.org/pdf/1509.02971v2.pdf&quot;&gt;Continuous control with deep reinforcement learning&lt;/a&gt;)是现有的actor-critic算法中比较好的，原始代码在&lt;a href=&quot;https://github.com/pemami4911/deep-rl&quot;&gt;https://github.com/pemami4911/deep-rl&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;修改后的代码&lt;a href=&quot;https://github.com/wangshusen/deep-rl&quot;&gt;https://github.com/wangshusen/deep-rl&lt;/a&gt;以及对应的解读：&lt;/p&gt;

&lt;p&gt;视频解读：&lt;a href=&quot;https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/6-2-A-DDPG/&quot;&gt;https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/6-2-A-DDPG/&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>bigflow的坑们</title>
   <link href="http://daiwk.github.io/posts/other-bigflow.html"/>
   <updated>2016-12-01T00:00:00+00:00</updated>
   <id>/posts/other-bigflow</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E8%87%AA%E5%8A%A8%E7%BC%A9%E5%87%8F&quot;&gt;自动缩减&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E4%B8%80%E4%BA%9Breduce%E7%9A%84task%E9%9D%9E%E5%B8%B8%E6%85%A2&quot;&gt;一些reduce的task非常慢&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%95%B0%E6%8D%AE%E5%A4%AA%E9%95%BF&quot;&gt;数据太长&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#lazyvar&quot;&gt;lazyvar&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h3 id=&quot;自动缩减&quot;&gt;自动缩减&lt;/h3&gt;

&lt;p&gt;自动缩减：”abaci.dag.datasize.per.reduce”: “20000000”, # 20m&lt;/p&gt;

&lt;p&gt;default_concurrency：在create_pileline的时候设置&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;自动缩减只能调小不能调大。&lt;/strong&gt;所以如果default concurrency比较小，就不会调了。这个default concurrcency最好是设置成比预估稍微大一点的并发，如果设置的太大，会影响dce shuffle的性能。&lt;/p&gt;

&lt;p&gt;也就是说&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;split_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasize_per_reduce&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;split_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default_concurrency&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;default_concurrency&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;split_size&lt;/span&gt;
 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;一些reduce的task非常慢&quot;&gt;一些reduce的task非常慢&lt;/h3&gt;

&lt;p&gt;如果后面还有执行慢的问题的话。可以设置下cpu_profile，这样那里计算耗时可以通过pprof显示出来&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pipeline = base.Pipeline.create(*****, cpu_profile=True)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;数据太长&quot;&gt;数据太长&lt;/h3&gt;

&lt;p&gt;看dce-writer-xxxxxxxxxxxxxxx文件，出现这句就挂了。。。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;FATAL [writer.cc:188] 0113 17:27:47.340183 1342 | CHECK failed: key.length() + value.length() &amp;lt; max_length: Too Big Data.Crashing...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;lazyvar&quot;&gt;lazyvar&lt;/h3&gt;

&lt;p&gt;目前外部词典的加载方式主要有3种：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;方法1. 如果词典文件极小（不超过10M）直接使用Python的闭包功能&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;方法2. 如果词典文件较大（10M &amp;lt; 词典大小 &amp;lt; 1G），可以使用bigflow提供的lazy_var模块在远端加载词典。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# coding: utf-8
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;bigflow&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lazy_var&lt;/span&gt;

&lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
使用建议：
    以下demo中的my_lazy_var是一个全局变量，实际上my_lazy_var可以在全局和局部使用。

    我们建议最好在局部使用。

    如果多个函数依赖my_lazy_var，可以适当修改业务代码，封装在类中，
    my_lazy_var作为类的一个成员，在类的所有成员函数中以self.my_var_lazy使用，
    效果几乎等价于全局.
&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
mydict.txt

key1    value1
key2    value2
key3    value3
&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;load_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;my_lazy_var&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lazy_var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;declare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./mydict.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 获取lazy_var内容
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;my_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_lazy_var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;my_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_lazy_var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 可以在本地直接获取lazy_var内容
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# [&quot;value1&quot;, &quot;value2&quot;, &quot;value3&quot;]
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;pipeline&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;local&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./mydict.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./mydict.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parallelize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;key1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;key2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;key3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;key4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 可以在transforms中获取lazy_var内容
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# [&quot;value1&quot;, &quot;value2&quot;, &quot;value3&quot;, None]
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;方法3. 使用side_input（在1.0.3之前的版本中此功能在下游并发较大时，会出现执行效率比较差的情况）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外，如果自定义的类型(python的class)会出现bad marshal等错误时，可以用lazyvar来搞&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;my_lazy_var&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;brand_tagger&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BrandTagger&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init_tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BrandTagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fname_model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./third/model/model&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fname_bigram&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./third/model/bigram.dat&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_fnames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fname_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fname_bigram&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;do_tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_loaded&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;csid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;userid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;real_rawpicurl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;encoded_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;raw_width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;raw_height&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;descs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;picurl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;srcs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ext_json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;to_cmp_descs_str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;search_word&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;search_word_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;search_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;formatted_res_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;search_word_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sub_item&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sub_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sub_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;brands&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;do_tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;brand_tag_res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brand&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brands&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;brand_tag_res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brand_tag_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;{brand}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;formatted_res_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brand_tag_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;formatted_res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formatted_res_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;res_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formatted_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res_list&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;_pipeline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workspace_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./opt-feeds-image/image-tagging/postprocess/my_lazy_var.py&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;my_lazy_var.py&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_lazy_var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;declare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;format_brand_res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter_res&lt;/span&gt;\
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;do_tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt;\
        &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>deep learning book-概览</title>
   <link href="http://daiwk.github.io/posts/dl-dlbook-intro.html"/>
   <updated>2016-12-01T00:00:00+00:00</updated>
   <id>/posts/dl-dlbook-intro</id>
   <content type="html">&lt;p&gt;几个git链接：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/HFTrader/DeepLearningBook&quot;&gt;https://github.com/HFTrader/DeepLearningBook&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-ReadingNotes&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ExtremeMart/DeepLearningBook-CN&quot;&gt;https://github.com/ExtremeMart/DeepLearningBook-CN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;内容简介：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Table of Contents&lt;/li&gt;
  &lt;li&gt;Acknowledgements&lt;/li&gt;
  &lt;li&gt;Notation&lt;/li&gt;
  &lt;li&gt;1 Introduction&lt;/li&gt;
  &lt;li&gt;Part I: Applied Math and Machine Learning Basics
    &lt;ul&gt;
      &lt;li&gt;2 Linear Algebra&lt;/li&gt;
      &lt;li&gt;3 Probability and Information Theory&lt;/li&gt;
      &lt;li&gt;4 Numerical Computation&lt;/li&gt;
      &lt;li&gt;5 Machine Learning Basics&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Part II: Modern Practical Deep Networks
    &lt;ul&gt;
      &lt;li&gt;6 Deep Feedforward Networks&lt;/li&gt;
      &lt;li&gt;7 Regularization for Deep Learning&lt;/li&gt;
      &lt;li&gt;8 Optimization for Training Deep Models&lt;/li&gt;
      &lt;li&gt;9 Convolutional Networks&lt;/li&gt;
      &lt;li&gt;10 Sequence Modeling: Recurrent and Recursive Nets&lt;/li&gt;
      &lt;li&gt;11 Practical Methodology&lt;/li&gt;
      &lt;li&gt;12 Applications&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Part III: Deep Learning Research
    &lt;ul&gt;
      &lt;li&gt;13 Linear Factor Models&lt;/li&gt;
      &lt;li&gt;14 Autoencoders&lt;/li&gt;
      &lt;li&gt;15 Representation Learning&lt;/li&gt;
      &lt;li&gt;16 Structured Probabilistic Models for Deep Learning&lt;/li&gt;
      &lt;li&gt;17 Monte Carlo Methods&lt;/li&gt;
      &lt;li&gt;18 Confronting the Partition Function&lt;/li&gt;
      &lt;li&gt;19 Approximate Inference&lt;/li&gt;
      &lt;li&gt;20 Deep Generative Models&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Bibliography&lt;/li&gt;
  &lt;li&gt;Index&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>神奇的gcc48</title>
   <link href="http://daiwk.github.io/posts/knowledge-gcc48.html"/>
   <updated>2016-11-29T00:00:00+00:00</updated>
   <id>/posts/knowledge-gcc48</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1%e7%a5%9e%e5%a5%87%e7%9a%84gcc48&quot;&gt;1.神奇的gcc48&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9&quot;&gt;2.注意事项&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1optcompilergcc-482lib%e6%88%96%e8%80%85lib64%e6%b7%bb%e5%8a%a0%e5%88%b0ldlibrarypath%e4%b8%ad%e5%90%97&quot;&gt;1）/opt/compiler/gcc-4.8.2/lib或者lib64添加到LD_LIBRARY_PATH中吗&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2%e4%bd%bf%e7%94%a8gcc4%e7%bc%96%e8%af%91%e5%be%97%e5%88%b0%e7%9a%84so%e5%8f%af%e4%bb%a5%e8%a2%abgcc3%e7%bc%96%e8%af%91%e5%be%97%e5%88%b0%e7%9a%84binary%e5%8a%a0%e8%bd%bd%e5%90%97&quot;&gt;2）使用gcc4编译得到的so，可以被gcc3编译得到的binary加载吗&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;1神奇的gcc48&quot;&gt;1.神奇的gcc48&lt;/h2&gt;

&lt;p&gt;使用gcc48的最直接方法：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/opt/compiler/gcc-4.8.2/bin:&lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;2注意事项&quot;&gt;2.注意事项&lt;/h2&gt;

&lt;h3 id=&quot;1optcompilergcc-482lib或者lib64添加到ld_library_path中吗&quot;&gt;1）/opt/compiler/gcc-4.8.2/lib或者lib64添加到LD_LIBRARY_PATH中吗&lt;/h3&gt;

&lt;p&gt;不可以，因为/opt/compiler/gcc-4.8.2/下的库与系统自身的程序和库在OS ABI上不兼容，如果添加之后，系统自身的程序(不要说python了，ls都用不了。。。)将无法运行，会出现如下错误：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;LD_LIBRARY_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/opt/compiler/gcc-4.8.2/lib64:&lt;span class=&quot;nv&quot;&gt;$LD_LIBRARY_PATH&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;ls
ls&lt;/span&gt;: error &lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;loading shared libraries: /opt/compiler/gcc-4.8.2/lib64/librt.so.1: ELF file OS ABI invalid
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;取巧方法：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/opt/compiler/gcc-4.8.2/lib/ld-linux-x86-64.so.2 &lt;span class=&quot;nt&quot;&gt;--library-path&lt;/span&gt; /opt/compiler/gcc-4.8.2/lib:&lt;span class=&quot;nv&quot;&gt;$LD_LIBRARY_PATH&lt;/span&gt; /bin/ls
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当然，我们用pip装tf或者torch的时候，可能有些用的是编译好的so，里面一些符号找不到。。这个时候也可以用这种风骚的方法来使用python。。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/opt/compiler/gcc-4.8.2/lib/ld-linux-x86-64.so.2 &lt;span class=&quot;nt&quot;&gt;--library-path&lt;/span&gt; /opt/compiler/gcc-4.8.2/lib:&lt;span class=&quot;nv&quot;&gt;$LD_LIBRARY_PATH&lt;/span&gt; ./python-3.6.9/bin/python3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;2使用gcc4编译得到的so可以被gcc3编译得到的binary加载吗&quot;&gt;2）使用gcc4编译得到的so，可以被gcc3编译得到的binary加载吗&lt;/h3&gt;

&lt;p&gt;不可以，因为gcc3编译得到的binary（譬如系统自带的python）中，其运行时加载的是/lib64/tls/libc.so.6，而gcc4编译得到的so（譬如gcc4编译得到的python扩展sofa.so）可能依赖高版本libc.so的符号。
系统自带的python运行时加载的是系统自身的/lib64/tls/libc.so.6，其libc版本为2.3.4(发现jumbo的python也是一个shi样子……)：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ldd &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;which /usr/bin/python&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
        libpython2.3.so.1.0 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /usr/lib64/libpython2.3.so.1.0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0bb00000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libpthread.so.0 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/tls/libpthread.so.0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0b900000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libdl.so.2 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/libdl.so.2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0b300000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libutil.so.1 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/libutil.so.1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0db00000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libm.so.6 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/tls/libm.so.6 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0b500000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libc.so.6 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/tls/libc.so.6 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0b000000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        /lib64/ld-linux-x86-64.so.2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0ae00000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

ldd &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;which ~/.jumbo/bin/python&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
        libpython2.7.so.1.0 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /home/users/daiwenkai/.jumbo/lib/libpython2.7.so.1.0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x00007ff0d7bea000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libpthread.so.0 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/tls/libpthread.so.0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0b900000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libdl.so.2 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/libdl.so.2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0b300000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libutil.so.1 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/libutil.so.1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0db00000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libm.so.6 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/tls/libm.so.6 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0b500000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        libc.so.6 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; /lib64/tls/libc.so.6 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0b000000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        /lib64/ld-linux-x86-64.so.2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x0000003f0ae00000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而gcc4编译得到的python扩展_CRFPP.so依赖高版本libc.so的符号：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nm ~/CRF_LIB/_CRFPP.so | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;GLIBC
                 U _ZdaPv@@GLIBCXX_3.4
                 U _Znam@@GLIBCXX_3.4
                 w __cxa_finalize@@GLIBC_2.2.5
                 U fputc@@GLIBC_2.2.5
                 U fputs@@GLIBC_2.2.5
                 U free@@GLIBC_2.2.5
                 U fwrite@@GLIBC_2.2.5
                 U malloc@@GLIBC_2.2.5
                 U memcpy@@GLIBC_2.14
                 U &lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt;@@GLIBC_2.2.5
                 U strcmp@@GLIBC_2.2.5
                 U strlen@@GLIBC_2.2.5
                 U strncmp@@GLIBC_2.2.5
                 U strncpy@@GLIBC_2.2.5
                 U strstr@@GLIBC_2.2.5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://unix.stackexchange.com/questions/132158/how-do-i-maintain-a-separate-newer-glibc-gcc-stack-as-non-root-on-linu&quot;&gt;http://unix.stackexchange.com/questions/132158/how-do-i-maintain-a-separate-newer-glibc-gcc-stack-as-non-root-on-linu
&lt;/a&gt;
&lt;a href=&quot;http://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host&quot;&gt;http://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host&lt;/a&gt;
rtldi: &lt;a href=&quot;http://bitwagon.com/rtldi/rtldi.html&quot;&gt;http://bitwagon.com/rtldi/rtldi.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;所以呢，就悲剧了，有一个很trick的办法，可以让gcc48编译出来的so给gcc34编译出来的py用：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/opt/compiler/gcc-4.8.2/lib/ld-linux-x86-64.so.2 &lt;span class=&quot;nt&quot;&gt;--library-path&lt;/span&gt; /opt/compiler/gcc-4.8.2/lib &lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;which python&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;import CRFPP&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当然了，这有点太恶心了吧。。。我们发现，其实直接用gcc34也可以编译出CRFPP的。。那么我们就直接用就好啦：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
./configure
make
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ./python/
python setup.py build
python setup.py &lt;span class=&quot;nb&quot;&gt;install

export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;LD_LIBRARY_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;~/crfpp/CRF++-0.58.gcc3.4.5/.libs/:&lt;span class=&quot;nv&quot;&gt;$LD_LIBRARY_PATH&lt;/span&gt; 
python &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;import CRFPP&quot;&lt;/span&gt;

nm ./build/lib.linux-x86_64-2.7/_CRFPP.so | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;GLIBC
                 w __cxa_finalize@@GLIBC_2.2.5
                 U fputs@@GLIBC_2.2.5
                 U free@@GLIBC_2.2.5
                 U malloc@@GLIBC_2.2.5
                 U memcpy@@GLIBC_2.2.5
                 U memset@@GLIBC_2.2.5
                 U &lt;span class=&quot;nb&quot;&gt;printf&lt;/span&gt;@@GLIBC_2.2.5
                 U strcmp@@GLIBC_2.2.5
                 U strcpy@@GLIBC_2.2.5
                 U strlen@@GLIBC_2.2.5
                 U strncmp@@GLIBC_2.2.5
                 U strncpy@@GLIBC_2.2.5
                 U strstr@@GLIBC_2.2.5
                 U _ZdaPv@@GLIBCXX_3.4
                 U _Znam@@GLIBCXX_3.4

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;完美……&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>机器学习common</title>
   <link href="http://daiwk.github.io/posts/ml-basics.html"/>
   <updated>2016-11-22T00:00:00+00:00</updated>
   <id>/posts/ml-basics</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#决策树&quot;&gt;决策树&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#随机森林&quot;&gt;随机森林&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#逻辑回归&quot;&gt;逻辑回归&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#svm&quot;&gt;svm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#朴素贝叶斯&quot;&gt;朴素贝叶斯&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#k近邻&quot;&gt;k近邻&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#k均值&quot;&gt;k均值&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#adaboost&quot;&gt;adaboost&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#神经网络&quot;&gt;神经网络&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#马尔可夫&quot;&gt;马尔可夫&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考这个地址：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MjM5MDI1ODUyMA==&amp;amp;mid=2672937675&amp;amp;idx=1&amp;amp;sn=6f19200d6c7d5be9759e40e1dd9e0229&amp;amp;chksm=bce2eefc8b9567eae27154e615872c5aba0ab6025331cb0ad948fa8c7ab5561d7724d581d8d8&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1122Hn68oGNIjs2pcV96YOHP&amp;amp;pass_ticket=96hgthjn7Zme8VJsxfnxkww%2BOlnhhE%2BSxuWv5FtwCOheowPVC7ofpSdg6HzTwd6Z#rd&quot;&gt;《轻松看懂机器学习十大常用算法》&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;决策树&quot;&gt;决策树&lt;/h1&gt;

&lt;p&gt;根据一些 feature 进行分类，每个节点提一个问题，通过判断，将数据分为两类，再继续提问。这些问题是根据已有数据学习出来的，再投入新数据的时候，就可以根据这棵树上的问题，将数据划分到合适的叶子上。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/decision tree.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;随机森林&quot;&gt;随机森林&lt;/h1&gt;

&lt;p&gt;在源数据中随机选取数据，组成几个子集&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/subsets of rf.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;S 矩阵是源数据，有 1-&amp;gt;N 条数据，A B C 是feature，最后一列C是类别&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/input of rf.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;由 S 随机生成 M 个子矩阵&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/dtrees of rf.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;这 M 个子集得到 M 个决策树。&lt;/strong&gt;将新数据投入到这 M 个树中，得到 M 个分类结果，计数看预测成哪一类的数目最多，就将此类别作为最后的预测结果&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/class prediction of rf.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;逻辑回归&quot;&gt;逻辑回归&lt;/h1&gt;

&lt;p&gt;当预测目标是概率这样的，值域需要满足&lt;strong&gt;大于等于0，小于等于1&lt;/strong&gt;的，这个时候单纯的线性模型是做不到的，因为在定义域不在某个范围之内时，值域也超出了规定区间。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/linear model of lr.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;所以此时需要这样的形状的模型会比较好&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/logistic plot of lr.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;那么怎么得到这样的模型呢？这个模型需要满足两个条件 大于等于0，小于等于1&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;大于等于0 的模型可以选择 绝对值，平方值，这里用 &lt;strong&gt;指数函数，一定大于0&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;小于等于1 用除法，&lt;strong&gt;分子是自己，分母是自身加上1&lt;/strong&gt;，那一定是小于1的了&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;注意：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{exp(x)}{exp(x)+1}=\frac{exp(-x)exp(x)}{exp(-x)exp(x)+exp(-x)}=\frac{1}{1+exp(-x)}=sigmoid(x)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/2steps to 0 to 1 of lr.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;再做一下变形，就得到了 logistic regression 模型(&lt;strong&gt;仍然有linear的意味~&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/linear thinking in lr of lr.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;通过源数据计算可以得到相应的系数了&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/estimated lr of lr.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;最后得到 logistic 的图形&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/logistic model of lr.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;svm&quot;&gt;svm&lt;/h1&gt;

&lt;p&gt;要将两类分开，想要得到一个超平面，最优的&lt;strong&gt;超平面是到两类的 margin 达到最大&lt;/strong&gt;(即，这个分界面很有区分性，它附近很少有容易被判错的点)，margin就是超平面与离它最近一点的距离，如下图，Z2&amp;gt;Z1，所以绿色的超平面(hyperplane)比较好&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/target of svm.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;将这个超平面表示成一个线性方程，在线上方的一类，都大于等于1，另一类小于等于－1&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/hyperplane to linear function of svm.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;点到面的距离根据图中的公式计算&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/distance of dot to plane of svm.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;所以得到 total margin 的表达式如下，&lt;strong&gt;目标是最大化这个 margin，就需要最小化分母（就是x的系数w）&lt;/strong&gt;，于是变成了一个优化问题&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/total margin of svm.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;举个栗子，三个点，找到最优的超平面，定义了 weight vector＝（2，3）－（1，1）&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/example1.1 of svm.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;得到 weight vector 为（a，2a），将两个点代入方程，代入（2，3）令其值＝1【因为class1的标签是1】，代入（1，1）令其值＝-1【因为class2的标签是-1】，求解出 a 和 截矩 w0 的值，进而得到超平面的表达式。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/example1.2 of svm.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a 求出来后，代入（a，2a），得到的&lt;strong&gt;w就是 support vector&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;a 和 w0 代入，得到的&lt;strong&gt;超平面的方程就是 support vector machine&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;朴素贝叶斯&quot;&gt;朴素贝叶斯&lt;/h1&gt;

&lt;p&gt;给一段文字，返回情感分类，这段文字的态度是positive，还是negative&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/sentiment example of naive bayes.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;为了解决这个问题，可以只看其中的一些单词(例如，高频词)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/sentiment example1.2 of naive bayes.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这段文字，将仅由一些单词和它们的计数代表&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/sentiment example1.3 of naive bayes.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;原始问题是：给你一句话，它属于哪一类。通过 bayes rules 变成一个比较简单容易求得的问题&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/sentiment example1.4 problem trans of naive bayes.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;问题变成，&lt;strong&gt;这一类中这句话出现的概率是多少&lt;/strong&gt;&lt;strong&gt;（given 标签，算出现这个单词、句子的概率）&lt;/strong&gt;，当然，别忘了公式里的另外两个概率&lt;/p&gt;

&lt;p&gt;栗子：单词 love 在 positive 的情况下出现的概率是 0.1，在 negative 的情况下出现的概率是 0.001&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/sentiment example1.5 calc probabilities of naive bayes.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;k近邻&quot;&gt;k近邻&lt;/h1&gt;

&lt;p&gt;k nearest neighbours。给一个新的数据时，离它最近的 k 个点中，哪个类别多，这个数据就属于哪一类&lt;/p&gt;

&lt;p&gt;栗子：要区分 猫 和 狗，通过 claws 和 sound 两个feature来判断的话，圆形和三角形是已知分类的了，那么这个 star 代表的是哪一类呢&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/knn1.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;k＝3时，这三条线链接的点就是最近的三个点，那么圆形多一些，所以这个star就是属于猫&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/knn2.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;k均值&quot;&gt;k均值&lt;/h1&gt;
&lt;p&gt;kmeans。想要将一组数据，分为三类，粉色数值大，黄色数值小&lt;/p&gt;

&lt;p&gt;最开始先初始化，这里面选了最简单的 3，2，1 作为各类的初始值&lt;/p&gt;

&lt;p&gt;剩下的数据里，每个都与三个初始值计算距离，然后归类到离它最近的初始值所在类别&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/kmeans1.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;分好类后，计算每一类的平均值，作为新一轮的中心点&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/kmeans2.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;几轮之后，分组不再变化了，就可以停止了&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/kmeans3.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/kmeans4.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;adaboost&quot;&gt;adaboost&lt;/h1&gt;

&lt;p&gt;adaboost 是 boosting 的方法之一。boosting就是把&lt;strong&gt;若干个分类效果并不好的分类器&lt;/strong&gt;综合起来考虑，会得到一个效果比较好的分类器。下图，左右两个决策树，单个看是效果不怎么好的，但是把同样的数据投入进去，把两个结果加起来考虑，就会增加可信度&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/adaboost1.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;adaboost 的栗子，手写识别中，在画板上可以抓取到很多 features，例如 始点的方向，始点和终点的距离等等&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/calculate features of adaboost.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;training 的时候，会得到每个 feature 的 weight，例如 2 和 3 的开头部分很像，这个 feature 对分类起到的作用很小，它的权重也就会较小&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/training of adaboost.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;而这个 alpha 角 就具有很强的识别性，这个 feature 的权重就会较大，最后的预测结果是综合考虑这些 feature 的结果&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/training2 of adaboost.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;神经网络&quot;&gt;神经网络&lt;/h1&gt;

&lt;p&gt;Neural Networks 适合一个input可能落入至少两个类别里&lt;/p&gt;

&lt;p&gt;NN 由若干层神经元，和它们之间的联系组成&lt;/p&gt;

&lt;p&gt;第一层是 input 层，最后一层是 output 层&lt;/p&gt;

&lt;p&gt;在 hidden 层 和 output 层都有自己的 classifier&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/1 of nn.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;input 输入到网络中，被激活，计算的分数被传递到下一层，激活后面的神经层，最后output 层的节点上的分数代表属于各类的分数，下图例子得到分类结果为 class 1&lt;/p&gt;

&lt;p&gt;同样的 input 被传输到不同的节点上，之所以会得到不同的结果是因为各自节点有不同的weights 和 bias&lt;/p&gt;

&lt;p&gt;这也就是 forward propagation&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/2 of nn.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;马尔可夫&quot;&gt;马尔可夫&lt;/h1&gt;

&lt;p&gt;Markov Chains 由** state &lt;strong&gt;和 **transitions&lt;/strong&gt; 组成&lt;/p&gt;

&lt;p&gt;栗子，根据这一句话 ‘the quick brown fox jumps over the lazy dog’，要得到 markov chain&lt;/p&gt;

&lt;p&gt;步骤，先给每一个单词设定成一个状态，然后计算状态间转换的概率&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/1 of markov.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这是一句话计算出来的概率，当你用大量文本去做统计的时候，会得到更大的状态转移矩阵，例如 the 后面可以连接的单词，及相应的概率&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/basics/2 of markov.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>crf++用法</title>
   <link href="http://daiwk.github.io/posts/nlp-crfpp.html"/>
   <updated>2016-11-16T00:00:00+00:00</updated>
   <id>/posts/nlp-crfpp</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#安装&quot;&gt;安装&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#训练&quot;&gt;训练&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#训练数据格式&quot;&gt;训练数据格式：&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#特征模板&quot;&gt;特征模板&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#测试&quot;&gt;测试&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#测试数据格式&quot;&gt;测试数据格式&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#测试结果&quot;&gt;测试结果&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#处理中文&quot;&gt;处理中文&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#python接口&quot;&gt;python接口&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;github：&lt;a href=&quot;https://github.com/taku910/crfpp&quot;&gt;https://github.com/taku910/crfpp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;下载链接：&lt;a href=&quot;https://drive.google.com/drive/folders/0B4y35FiV1wh7fngteFhHQUN2Y1B5eUJBNHZUemJYQV9VWlBUb3JlX0xBdWVZTWtSbVBneU0&quot;&gt;google drive&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主页：&lt;a href=&quot;https://taku910.github.io/crfpp/&quot;&gt;https://taku910.github.io/crfpp/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.52nlp.cn/%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%85%A5%E9%97%A8%E4%B9%8B%E5%AD%97%E6%A0%87%E6%B3%A8%E6%B3%954&quot;&gt;博客&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;安装&quot;&gt;安装&lt;/h1&gt;

&lt;p&gt;开发机：/home/users/daiwenkai/crfpp/brand
从下载链接&lt;a href=&quot;https://drive.google.com/drive/folders/0B4y35FiV1wh7fngteFhHQUN2Y1B5eUJBNHZUemJYQV9VWlBUb3JlX0xBdWVZTWtSbVBneU0&quot;&gt;google drive&lt;/a&gt;下载CRF++-0.58.tar.gz，解压&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./configure 
make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;训练&quot;&gt;训练&lt;/h1&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./crf_learn 
CRF++: Yet Another CRF Tool Kit
Copyright &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 2005-2013 Taku Kudo, All rights reserved.

Usage: /home/users/daiwenkai/crfpp/CRF++-0.58/.libs/lt-crf_learn &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;options] files
 &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;INT              use features that occuer no less than INT&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;default 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--maxiter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;INT           &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;INT &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;max iterations &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;LBFGS routine&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;default 10k&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--cost&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;FLOAT            &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;FLOAT &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;cost parameter&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;default 1.0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--eta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;FLOAT             &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;FLOAT &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;termination criterion&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;default 0.0001&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--convert&lt;/span&gt;               convert text model to binary model
 &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--textmodel&lt;/span&gt;             build also text model file &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;debugging
 &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--algorithm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=(&lt;/span&gt;CRF|MIRA&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;select &lt;/span&gt;training algorithm
 &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;INT            number of threads &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;default auto-detect&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--shrinking-size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;INT    &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;INT &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;number of iterations variable needs to  be optimal before considered &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;shrinking. &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;default 20&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;               show the version and &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--help&lt;/span&gt;                  show this &lt;span class=&quot;nb&quot;&gt;help &lt;/span&gt;and &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;训练数据格式&quot;&gt;训练数据格式：&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;毎 k   B
日 k   I
新 k   I
聞 k   I
社 k   I
特 k   B
別 k   I
顧 k   B
問 k   I                                                 
４ n   B
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里第一列是待分词的日文字，第二列暂且认为其是词性标记，第三列是字标注中的2-tag(B, I)标记，这个很重要，对于我们需要准备的训练集，主要是把这一列的标记做好，不过需要注意的是，&lt;strong&gt;其断句是靠空行来完成的。&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;特征模板&quot;&gt;特征模板&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Unigram&lt;/span&gt;
U00:%x[-2,0]
U01:%x[-1,0]
U02:%x[0,0]
U03:%x[1,0]
U04:%x[2,0]
U05:%x[-2,0]/%x[-1,0]/%x[0,0]
U06:%x[-1,0]/%x[0,0]/%x[1,0]
U07:%x[0,0]/%x[1,0]/%x[2,0]
U08:%x[-1,0]/%x[0,0]                                          
U09:%x[0,0]/%x[1,0]

&lt;span class=&quot;c&quot;&gt;# Bigram&lt;/span&gt;
B
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;关于CRF++中特征模板的说明和举例，请大家参考官方文档上的“Preparing feature templates”这一节，而以下部分的说明拿上述日文分词数据举例。在特征模板文件中，每一行(如U00:%x[-2,0]）代表一个特征，而宏“%x[行位置,列位置]”则代表了相对于当前指向的token的行偏移和列的绝对位置，以上述训练集为例，如果当前扫描到“新	k	I”这一行，&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;毎 k   B
日 k   I
新 k   I   &amp;lt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 扫描到这一行，代表当前位置
聞 k   I
社 k   I
特 k   B
別 k   I
顧 k   B
問 k   I
４ n   B
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;那么依据特征模板文件抽取的特征如下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Unigram&lt;/span&gt;
U00:%x[-2,0] &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 毎
U01:%x[-1,0] &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 日
U02:%x[0,0]  &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 新
U03:%x[1,0]  &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 聞
U04:%x[2,0]  &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 社
U05:%x[-2,0]/%x[-1,0]/%x[0,0] &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 每/日/新
U06:%x[-1,0]/%x[0,0]/%x[1,0]  &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 日/新/聞
U07:%x[0,0]/%x[1,0]/%x[2,0]   &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 新/聞/社
U08:%x[-1,0]/%x[0,0]          &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 日/新
U09:%x[0,0]/%x[1,0]           &lt;span class=&quot;o&quot;&gt;==&amp;gt;&lt;/span&gt; 新/聞

&lt;span class=&quot;c&quot;&gt;# Bigram&lt;/span&gt;
B
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;CRF++里将特征分成两种类型，一种是Unigram的，“U”起头，另外一种是Bigram的，“B”起头。对于Unigram的特征，假如一个特征模板是”U01:%x[-1,0]”, CRF++会自动的生成一组特征函数(func1 … funcN) 集合:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;func1 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;output &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; B and &lt;span class=&quot;nv&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;U01:日&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return &lt;/span&gt;1 &lt;span class=&quot;k&quot;&gt;else return &lt;/span&gt;0
func2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;output &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; I and &lt;span class=&quot;nv&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;U01:日&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return &lt;/span&gt;1 &lt;span class=&quot;k&quot;&gt;else return &lt;/span&gt;0
....
funcXX &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;output &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; B and &lt;span class=&quot;nv&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;U01:問&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return &lt;/span&gt;1  &lt;span class=&quot;k&quot;&gt;else return &lt;/span&gt;0
funcXY &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;output &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; I and &lt;span class=&quot;nv&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;U01:問&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return &lt;/span&gt;1  &lt;span class=&quot;k&quot;&gt;else return &lt;/span&gt;0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;生成的特征函数的数目 = (L * N)，其中L&lt;strong&gt;是输出的类型的个数，这里L=2，是B/I这两个tag&lt;/strong&gt;，N是通过模板扩展出来的所有单个字符串(特征）的个数(即所有的x[-1,0])，这里指的是在扫描所有训练集的过程中找到的日文字（特征）。&lt;/p&gt;

&lt;p&gt;而Bigram特征主要是当前的token和前面一个位置token的自动组合生成的bigram特征集合。最后需要注意的是U01和U02这些标志位，与特征token组合到一起主要是区分&lt;strong&gt;“U01:問”和“U02:問”这类特征，虽然抽取的日文”字”特征是一样的，但是在CRF++中这是有区别的特征。&lt;/strong&gt;&lt;/p&gt;

&lt;h1 id=&quot;测试&quot;&gt;测试&lt;/h1&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ./crf_test &lt;span class=&quot;nt&quot;&gt;--help&lt;/span&gt;
CRF++: Yet Another CRF Tool Kit
Copyright &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 2005-2013 Taku Kudo, All rights reserved.

Usage: /home/users/daiwenkai/crfpp/CRF++-0.58/.libs/lt-crf_test &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;options] files
 &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;FILE         &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;FILE &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;model file
 &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--nbest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;INT          output n-best results
 &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--verbose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;INT        &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;INT &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;verbose level
 &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--cost-factor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;FLOAT  &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;cost &lt;span class=&quot;nb&quot;&gt;factor&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;FILE        use FILE as output file
 &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;            show the version and &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
 &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--help&lt;/span&gt;               show this &lt;span class=&quot;nb&quot;&gt;help &lt;/span&gt;and &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;测试数据格式&quot;&gt;测试数据格式&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;よ h   I
っ h   I
て h   I
私 k   B
た h   B
ち h   I
の h   B                                                     
世 k   B
代 k   I
が h   B
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;同样也有3列，第一列是日文字，第二列第三列与上面是相似的，不过在测试集里&lt;strong&gt;第三列主要是占位作用&lt;/strong&gt;。事实上，CRF++对于训练集和测试集文件格式的要求是比较灵活的，首先需要多列，但不能不一致，既在一个文件里有的行是两列，有的行是三列；其次第一列代表的是需要标注的“字或词”，&lt;strong&gt;最后一列是输出位”标记tag”&lt;/strong&gt;，如果有额外的特征，例如词性什么的，可以加到中间列里，所以训练集或者测试集的文件最少要有两列。&lt;/p&gt;

&lt;h2 id=&quot;测试结果&quot;&gt;测试结果&lt;/h2&gt;

&lt;p&gt;最后多了一列，就是预测的tag&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;よ h   I   B
っ h   I   I
て h   I   B
私 k   B   B
た h   B   B
ち h   I   I
の h   B   B                                      
世 k   B   B
代 k   I   I
が h   B   B
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;处理中文&quot;&gt;处理中文&lt;/h2&gt;

&lt;p&gt;我们来抄一下傲哥的代码brand_filter/brand_tagger.py&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_parse_string_to_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;format a string to character list:
    a Chinese character or a ascii string will be treat as an item(char)
    in the list.
    Args:
        word, a gbk encoded string;
    Returns:
        a list of Chinese characters and ascii strings
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;char_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# the value of the first byte of a Chinese character is &amp;gt;= 128
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;char_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;char_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;char_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;char_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;char_list&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;python接口&quot;&gt;python接口&lt;/h2&gt;

&lt;p&gt;首先，需要对setup.py进行修改：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env python
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;distutils.core&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Extension&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;string&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;mecab-python&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;py_modules&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;CRFPP&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;ext_modules&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Extension&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;_CRFPP&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;CRFPP_wrap.cxx&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,],&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;include_dirs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/home/users/daiwenkai/crfpp/CRF++-0.58/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 新增这行 
&lt;/span&gt;                               &lt;span class=&quot;n&quot;&gt;library_dirs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/home/users/daiwenkai/crfpp/CRF++-0.58/.libs/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 新增这行
&lt;/span&gt;                               &lt;span class=&quot;n&quot;&gt;libraries&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;crfpp&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pthread&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;接下来就是安装过程了（可以参考&lt;a href=&quot;https://daiwk.github.io/posts/knowledge-gcc48.html&quot;&gt;神奇的gcc48&lt;/a&gt;）&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;CRF++-0.58/python
python setup.py build
python setup.py &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#当然，这里有坑，如果使用gcc48编译的话，呵呵了，如果gcc34编译的，完美兼容&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;LD_LIBRARY_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;~/crfpp/CRF++-0.58.gcc3.4.5/.libs/:&lt;span class=&quot;nv&quot;&gt;$LD_LIBRARY_PATH&lt;/span&gt; 
python &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;import CRFPP&quot;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们看一下test.py&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;CRFPP&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# -v 3: access deep information like alpha,beta,prob
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# -nN: enable nbest output. N should be &amp;gt;= 2
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CRFPP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;-m ../model -v 3 -n2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# clear internal context
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# add context
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Confidence NN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;in IN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;the DT&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;pound NN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;is VBZ&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;widely RB&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;expected VBN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;to TO&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;take VB&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;another DT&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;sharp JJ&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;dive NN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;if IN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;trade NN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;figures NNS&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;for IN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;September NNP&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;column size: &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;token size: &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tag size: &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ysize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tagset information:&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ysize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ysize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ysize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tag &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# parse and change internal stated as 'parsed'
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;conditional prob=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; log(Z)=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;xsize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Details&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ysize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;yname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/prob=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/alpha=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/beta=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;beta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;nbest outputs:&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;nbest n=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;conditional prob=&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tagger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# you can access any information using tagger.y()...
&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Done&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;RuntimeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;RuntimeError: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Deep Recurrent Models with Fast-Forward Connections for Neural Machine Translation(rnn_nmt_baidu)</title>
   <link href="http://daiwk.github.io/posts/nlp-rnn-nmt-baidu.html"/>
   <updated>2016-11-04T00:00:00+00:00</updated>
   <id>/posts/nlp-rnn-nmt-baidu</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-摘要&quot;&gt;&lt;strong&gt;0. 摘要&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-介绍&quot;&gt;&lt;strong&gt;1. 介绍&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-nmt&quot;&gt;&lt;strong&gt;2. NMT&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-deep-topology&quot;&gt;&lt;strong&gt;3. Deep Topology&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-network&quot;&gt;&lt;strong&gt;3.1 Network&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-train-technique&quot;&gt;&lt;strong&gt;3.2 Train technique&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#33-generation&quot;&gt;&lt;strong&gt;3.3 Generation&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-experiments&quot;&gt;&lt;strong&gt;4. Experiments&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-data-sets&quot;&gt;&lt;strong&gt;4.1 Data sets&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-model-settings&quot;&gt;&lt;strong&gt;4.2 Model settings&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#43-optimization&quot;&gt;&lt;strong&gt;4.3 Optimization&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#44-results&quot;&gt;&lt;strong&gt;4.4 Results&lt;/strong&gt;&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#441-single-models&quot;&gt;&lt;strong&gt;4.4.1 Single models&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#442-post-processing&quot;&gt;&lt;strong&gt;4.4.2 Post processing&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#45-analysis&quot;&gt;&lt;strong&gt;4.5 Analysis&lt;/strong&gt;&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#451-length&quot;&gt;&lt;strong&gt;4.5.1 Length&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#452-unknown-words&quot;&gt;&lt;strong&gt;4.5.2 Unknown words&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#453-over-fitting&quot;&gt;&lt;strong&gt;4.5.3 Over-fitting&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-conclusion&quot;&gt;&lt;strong&gt;5. Conclusion&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;这篇论文发表在acl,2016上
&lt;a href=&quot;../assets/Deep Recurrent Models with Fast-Forward Connections for Neural Machine Translation.pdf&quot;&gt;论文地址&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;0-摘要&quot;&gt;&lt;strong&gt;0. 摘要&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;基于&lt;strong&gt;deep lstm networks + interleaved(插入/交错) deep bi-lstm&lt;/strong&gt;，使用了新的linear connections&lt;strong&gt;(fast-forward connections)&lt;/strong&gt;.fast-forward connections在&lt;strong&gt;propagating gradient&lt;/strong&gt;以及建立&lt;strong&gt;深度达到16的深度拓扑&lt;/strong&gt;中起到了重要作用。&lt;/p&gt;

&lt;p&gt;在wmt’14的English-&amp;gt;French的翻译中，&lt;strong&gt;单一attention&lt;/strong&gt;的模型BLEU达到37.7（超越了传统nmt的单浅层模型6.2的BLEU）；去掉attention，BLEU=36.3。在&lt;strong&gt;对unknown words进行了特殊的处理&lt;/strong&gt;，同时进行&lt;strong&gt;模型ensemble&lt;/strong&gt;之后，可以达到BLEU=40.4。&lt;/p&gt;

&lt;h1 id=&quot;1-介绍&quot;&gt;&lt;strong&gt;1. 介绍&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;传统mt模型（statistical mt，SMT）包括了multiple separately tuned components，而NMT将源序列encode到一个&lt;strong&gt;continuous representation space&lt;/strong&gt;，然后使用end-to-end的方式生成新的序列。&lt;/p&gt;

&lt;p&gt;NMT一般有两种拓扑：encoder-decoder network(&lt;a href=&quot;../assets/sequence to sequence learning with neural networks.pdf&quot;&gt;Sutskever et al., 2014&lt;/a&gt;)以及attention网络（&lt;a href=&quot;../assets/neural machine translation by jointly learning to align and translate.pdf&quot;&gt;Bahdanau et al., 2015&lt;/a&gt;）。&lt;/p&gt;

&lt;p&gt;encoder-decoder网络将源序列表示成一个&lt;strong&gt;fixed dimensional vector&lt;/strong&gt;，并&lt;strong&gt;word by word&lt;/strong&gt;地生成目标序列。&lt;/p&gt;

&lt;p&gt;attention网络使用&lt;strong&gt;all time steps的输入&lt;/strong&gt;建立一个&lt;strong&gt;targetwords和inputwords之间&lt;/strong&gt;的detailed relationship。&lt;/p&gt;

&lt;p&gt;但single的neural network和最好的conventional(传统) SMT还是不能比的，6层BLEU才只有31.5，但传统方法有37.0。&lt;/p&gt;

&lt;p&gt;近两年，在computer vision领域，imagenet比赛前几名的，基本都是几十甚至上百层的网络，但NMT领域，成功的模型里，最深的也就6层。原因在于，&lt;strong&gt;与卷积层相比，lstm里面有更多的非线性激活函数，而这些激活函数significantly decrease the magnititude（重要性）of the gradient in the deep topology, especially when the gradient progates in recurrent form.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;本文中使用了一种new type of &lt;strong&gt;linear connections (fast forward connections)&lt;/strong&gt; for 多层的recurrent network。而且，我们还在&lt;strong&gt;encoder中&lt;/strong&gt;使用了一个&lt;strong&gt;interleaved bi-directional architecture to stack lstm layers&lt;/strong&gt;。这种拓扑可以在encoder-decoder网络中用，也可以在attention网络中使用。&lt;/p&gt;

&lt;h1 id=&quot;2-nmt&quot;&gt;&lt;strong&gt;2. NMT&lt;/strong&gt;&lt;/h1&gt;

&lt;h1 id=&quot;3-deep-topology&quot;&gt;&lt;strong&gt;3. Deep Topology&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;31-network&quot;&gt;&lt;strong&gt;3.1 Network&lt;/strong&gt;&lt;/h2&gt;

&lt;h2 id=&quot;32-train-technique&quot;&gt;&lt;strong&gt;3.2 Train technique&lt;/strong&gt;&lt;/h2&gt;

&lt;h2 id=&quot;33-generation&quot;&gt;&lt;strong&gt;3.3 Generation&lt;/strong&gt;&lt;/h2&gt;

&lt;h1 id=&quot;4-experiments&quot;&gt;&lt;strong&gt;4. Experiments&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;41-data-sets&quot;&gt;&lt;strong&gt;4.1 Data sets&lt;/strong&gt;&lt;/h2&gt;

&lt;h2 id=&quot;42-model-settings&quot;&gt;&lt;strong&gt;4.2 Model settings&lt;/strong&gt;&lt;/h2&gt;

&lt;h2 id=&quot;43-optimization&quot;&gt;&lt;strong&gt;4.3 Optimization&lt;/strong&gt;&lt;/h2&gt;

&lt;h2 id=&quot;44-results&quot;&gt;&lt;strong&gt;4.4 Results&lt;/strong&gt;&lt;/h2&gt;

&lt;h3 id=&quot;441-single-models&quot;&gt;&lt;strong&gt;4.4.1 Single models&lt;/strong&gt;&lt;/h3&gt;

&lt;h3 id=&quot;442-post-processing&quot;&gt;&lt;strong&gt;4.4.2 Post processing&lt;/strong&gt;&lt;/h3&gt;

&lt;h2 id=&quot;45-analysis&quot;&gt;&lt;strong&gt;4.5 Analysis&lt;/strong&gt;&lt;/h2&gt;

&lt;h3 id=&quot;451-length&quot;&gt;&lt;strong&gt;4.5.1 Length&lt;/strong&gt;&lt;/h3&gt;

&lt;h3 id=&quot;452-unknown-words&quot;&gt;&lt;strong&gt;4.5.2 Unknown words&lt;/strong&gt;&lt;/h3&gt;

&lt;h3 id=&quot;453-over-fitting&quot;&gt;&lt;strong&gt;4.5.3 Over-fitting&lt;/strong&gt;&lt;/h3&gt;

&lt;h1 id=&quot;5-conclusion&quot;&gt;&lt;strong&gt;5. Conclusion&lt;/strong&gt;&lt;/h1&gt;
</content>
 </entry>
 
 <entry>
   <title>paddlepaddle上的lstm crf做序列标注</title>
   <link href="http://daiwk.github.io/posts/platform-paddlepaddle-lstm-crf.html"/>
   <updated>2016-10-20T00:00:00+00:00</updated>
   <id>/posts/platform-paddlepaddle-lstm-crf</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-2015bidirectional-lstm-crf-models-for-sequence-tagging&quot;&gt;&lt;strong&gt;1. 【2015】Bidirectional LSTM-CRF Models for Sequence Tagging&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-2016conditional-random-fields-as-recurrent-neural-networks&quot;&gt;&lt;strong&gt;2. 【2016】Conditional Random Fields as Recurrent Neural Networks&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-2016acldeep-recurrent-models-with-fast-forward-connections-for-neural-machine-translation&quot;&gt;&lt;strong&gt;3. 【2016,ACL】Deep Recurrent Models with Fast-Forward Connections for Neural Machine Translation&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-%E5%9C%A8paddlepaddle%E4%B8%8A%E5%AE%9E%E7%8E%B0&quot;&gt;&lt;strong&gt;4. 在paddlepaddle上实现&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-%E6%95%B0%E6%8D%AE%E9%9B%86%E8%AF%B4%E6%98%8E&quot;&gt;4.1 数据集说明&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-dataprovider&quot;&gt;4.2 dataprovider&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#421-initializer%E7%9A%84%E6%B5%81%E7%A8%8B&quot;&gt;4.2.1 initializer的流程：&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#421-process%E7%9A%84%E6%B5%81%E7%A8%8B&quot;&gt;4.2.1 process的流程：&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#43-linearcrf&quot;&gt;4.3 linear_crf&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#44-rnncrf&quot;&gt;4.4 rnn_crf&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h1 id=&quot;1-2015bidirectional-lstm-crf-models-for-sequence-tagging&quot;&gt;&lt;strong&gt;1. 【2015】Bidirectional LSTM-CRF Models for Sequence Tagging&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;&lt;a href=&quot;../assets/Bidirectional LSTM-CRF Models for Sequence Tagging.pdf&quot;&gt;论文地址&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;2-2016conditional-random-fields-as-recurrent-neural-networks&quot;&gt;&lt;strong&gt;2. 【2016】Conditional Random Fields as Recurrent Neural Networks&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;&lt;a href=&quot;../assets/Conditional Random Fields as Recurrent Neural Networks.pdf&quot;&gt;论文地址&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;3-2016acldeep-recurrent-models-with-fast-forward-connections-for-neural-machine-translation&quot;&gt;&lt;strong&gt;3. 【2016,ACL】Deep Recurrent Models with Fast-Forward Connections for Neural Machine Translation&lt;/strong&gt;&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;../assets/Deep Recurrent Models with Fast-Forward Connections for Neural Machine Translation.pdf&quot;&gt;论文地址&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;4-在paddlepaddle上实现&quot;&gt;&lt;strong&gt;4. 在paddlepaddle上实现&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;fluid:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/models/tree/develop/fluid/sequence_tagging_for_ner&quot;&gt;https://github.com/PaddlePaddle/models/tree/develop/fluid/sequence_tagging_for_ner&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;v2:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/models/tree/develop/legacy/sequence_tagging_for_ner&quot;&gt;https://github.com/PaddlePaddle/models/tree/develop/legacy/sequence_tagging_for_ner&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;fluid文档：&lt;a href=&quot;http://paddlepaddle.org/documentation/docs/zh/0.14.0/new_docs/beginners_guide/basics/label_semantic_roles/README.cn.html&quot;&gt;http://paddlepaddle.org/documentation/docs/zh/0.14.0/new_docs/beginners_guide/basics/label_semantic_roles/README.cn.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;掉渣天的徐老师已经在开源版本的paddlepaddle实现了&lt;a href=&quot;https://github.com/baidu/Paddle/tree/develop/demo/sequence_tagging&quot;&gt;rnn+crf&lt;/a&gt;，我们直接拿来学习学习就好啦！！！【此demo只在github版本有，且paddle的其他源都已经不维护了，诸君请自重~】&lt;/p&gt;

&lt;h2 id=&quot;41-数据集说明&quot;&gt;4.1 数据集说明&lt;/h2&gt;

&lt;p&gt;先看一下数据集的说明：
&lt;a href=&quot;http://www.clips.uantwerpen.be/conll2000/chunking/&quot;&gt;http://www.clips.uantwerpen.be/conll2000/chunking/&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#[NP He ] [VP reckons ] [NP the current account deficit ] [VP will narrow ] [PP to ] [NP only # 1.8 billion ] [PP in ] [NP September ] .&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#He        PRP  B-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#reckons   VBZ  B-VP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#the       DT   B-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#current   JJ   I-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#account   NN   I-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#deficit   NN   I-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#will      MD   B-VP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#narrow    VB   I-VP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#to        TO   B-PP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#only      RB   B-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;##         #    I-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#1.8       CD   I-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#billion   CD   I-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#in        IN   B-PP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#September NNP  B-NP&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#.         .    O&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#The O chunk tag is used for tokens which are not part of any chunk&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;首先，需要将文件组织成三列，第一列是单词（中文的单字），第二列是tag_pos（part-of-speech tag as derived by the Brill tagger），第三列是标签tag（B-x表示开始，I-x表示中间）。比如，我们写好了test.txt，那么我们运行&lt;strong&gt;gzip test.txt&lt;/strong&gt;，这样，就生成了test.txt.gz（解压的时候gzip -dc test.txt.gz &amp;gt; test.txt，就会生成test.txt[&lt;strong&gt;如果不用-c参数，会默认删掉test.txt.gz&lt;/strong&gt;]）。&lt;/p&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;文件&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;行数&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;序列个数&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;train.txt&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;220663&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;8936&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;test.txt&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;49389&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;2012&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;42-dataprovider&quot;&gt;4.2 dataprovider&lt;/h2&gt;

&lt;p&gt;然后我们来仔细看一下dataprovider：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;c1&quot;&gt;# Feature combination patterns.(crf要用到的特征？)
# [[-1,0], [0,0]]  means previous token at column 0 and current token at 
# column 0 are combined as one feature.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;patterns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; 

    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;dict_label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-ADJP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-ADJP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-ADVP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-ADVP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-CONJP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-CONJP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-INTJ'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-INTJ'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-LST'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-LST'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-NP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-NP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-PP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-PP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-PRT'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-PRT'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-SBAR'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-SBAR'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-UCP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-UCP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'B-VP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'I-VP'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;'O'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;make_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#比如，特征是[[-1,0]]，但现在是第0个字，传进来pos就是0-1，但不能0-1啊，就标为#B1（向前超出了1个）
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'#B%s'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#同样道理，向后超出了多少个
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'#E%s'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patterns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#例如，feature是[[0,1], [1,1], [2,1]],那么，fname可能是NNP/NNP/POS，如果是[[2,1]],那么，fname就是POS
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;#create_dictionaries函数中：
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#cutoff: a list of numbers. If count of a feature is smaller than this,              
# it will be ignored.          
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cutoff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cutoff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;patterns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;cutoff&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;                                                      

&lt;span class=&quot;c1&quot;&gt;#if oov_policy[i] is OOV_POLICY_USE, id 0 is reserved for OOV features of            
#i-th column.                                                                        
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;OOV_POLICY_IGNORE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;OOV_POLICY_USE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;OOV_POLICY_ERROR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OOV_POLICY_IGNORE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OOV_POLICY_ERROR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OOV_POLICY_ERROR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OOV_POLICY_IGNORE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;patterns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

                                                                             
&lt;span class=&quot;c1&quot;&gt;#return a list of dict for each column
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_dictionaries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cutoff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add_to_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Wrong number of features &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cutoff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gzip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'rb'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#空串，用于区分不同句子
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;make_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;add_to_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dct&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OOV_POLICY_USE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;todo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iteritems&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cutoff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;todo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;dct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OOV_POLICY_USE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# placeholder so that len(dct) will be the number of features
&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# including OOV
&lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;dct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'#OOV#'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'column %d dict size=%d, ignored %d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;todo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;todo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;421-initializer的流程&quot;&gt;4.2.1 initializer的流程：&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;初始化cutoff=[3, 1, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]（长度3+len(patterns)，即3+feature）&lt;/li&gt;
  &lt;li&gt;初始化oov_policy=[0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]，其中，&lt;strong&gt;OOV_POLICY_IGNORE = 0；OOV_POLICY_USE = 1；OOV_POLICY_ERROR = 2&lt;/strong&gt;，也就是说除了第二个和第三个（i=1,2,…）设成oov_err，其他全部设成oov_ignore！&lt;/li&gt;
  &lt;li&gt;dicts = create_dictionaries(file, cutoff, oov_policy)：
    &lt;ul&gt;
      &lt;li&gt;初始化dicts数组，数组长度为num_features(即len(cutoff)，即3+feature)，每个元素是一个空字典&lt;/li&gt;
      &lt;li&gt;初始化sequence = []&lt;/li&gt;
      &lt;li&gt;遍历文件，
        &lt;ul&gt;
          &lt;li&gt;features = line.split(‘ ‘)，将features放进sequence中。（也就是说sequence最开始会有3个元素：词，tag_pos，tag，比如’jetliners’, ‘NNS’, ‘I-NP’）&lt;/li&gt;
          &lt;li&gt;每读到空行，表示一个序列结束了，
            &lt;ul&gt;
              &lt;li&gt;进行make_features(sequence)：将这一个序列（比如训练数据有100个序列，这是第77个，而这个序列有19个字符）sequence[i]【0&amp;lt;=i&amp;lt;=18】变成[‘jetliners’, ‘NNS’, ‘I-NP’, “‘s”, ‘747’, ‘jetliners’, ‘.’, ‘#E1’, ‘747/jetliners’, ‘jetliners/.’]之类的，&lt;strong&gt;长度为feature+3&lt;/strong&gt;个数的数组（其实就是最开始放进去的那3个元素，再加上每个feature拿到对应的值）&lt;/li&gt;
              &lt;li&gt;进行add_to_dict(sequence, dicts)：遍历sequence中的每个元素features（features长度为feature+3），对每个元素，遍历len(features)=feature+3，如果features[i] in dicts[i]，那么dicts[i][features[i]] += 1，反之，dicts[i][features[i]] = 1。实际上，就是&lt;strong&gt;dicts长度始终是feature+3&lt;/strong&gt;，每个元素是一个字典，key是sequence（长度为序列个数）里面每个元素features的每个元素(特征)，value是这些元素出现的次数。&lt;/li&gt;
              &lt;li&gt;重置sequence = []，并continue&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;遍历dicts(i=0-&amp;gt;features+3),
        &lt;ul&gt;
          &lt;li&gt;dct = dicts[i]&lt;/li&gt;
          &lt;li&gt;n = 1 if oov_policy[i] == OOV_POLICY_USE else 0&lt;/li&gt;
          &lt;li&gt;todo = []&lt;/li&gt;
          &lt;li&gt;遍历dct的元素k, v
            &lt;ul&gt;
              &lt;li&gt;如果v &amp;lt; cutoff[i]，那么todo.append(k)&lt;strong&gt;【表示，这个feature的这个元素出现的次数小于cutoff里面定义的次数，例如[-2,0]这个feature里面，有一个词’hello’,他出现的次数只有5，那么我就把这个feature里的’hello’(也就是k)放到todo里面，最后干掉！】&lt;/strong&gt;&lt;/li&gt;
              &lt;li&gt;否则：dct[k] = n；n += 1【对dicts[i]也就是dct的value进行重新赋值！按顺序来，从n开始。例如，这次可能cutoff的阈值是3，然后我们保留下来大于3的7个feature值，可能他们各自的次数是成百上千，但这里，&lt;strong&gt;我们重新编个号，如果oov_policy[i]=OOV_POLICY_USE，那么就是1-7，否则就是0-6&lt;/strong&gt;】&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;如果oov_policy[i] == OOV_POLICY_USE，那么dct[‘#OOV#’] = 0 【placeholder so that len(dct) will be the number of features】&lt;/li&gt;
          &lt;li&gt;遍历todo的每个元素k，del dct[k]&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;dicts[2] = dict_label 【dict_label = {‘B-ADJP’: 0, ‘I-ADJP’: 1,…,’O’: 22 },因为可能训练集里并没有全部的B-xx,I-xx和O，所以，&lt;strong&gt;这里用这一步把训练集的第2个元素变成全集！！&lt;/strong&gt; ，这样一来，&lt;strong&gt;dicts就是，词、brill tagger、B-/I-xx/O、23个feature出现次数大于他们各自阈值的value以及他们对应的重新赋予的id（oov_policy如果这个feature是OOV_POLICY_USE就从1开始，否则从0开始），另外，B-/I-xx/O那列是全集！&lt;/strong&gt;】&lt;/li&gt;
  &lt;li&gt;settings.dicts = dicts&lt;/li&gt;
  &lt;li&gt;settings.oov_policy = oov_policy&lt;/li&gt;
  &lt;li&gt;input_types = []&lt;/li&gt;
  &lt;li&gt;num_features = len(dicts)&lt;/li&gt;
  &lt;li&gt;for i=0-&amp;gt;num_original_columns:【num_original_columns=3】
    &lt;ul&gt;
      &lt;li&gt;input_types.append(integer_sequence(len(dicts[i])))&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;如果patterns不空，那么：
    &lt;ul&gt;
      &lt;li&gt;dim = 0&lt;/li&gt;
      &lt;li&gt;for i=num_original_columns-&amp;gt;num_features:
        &lt;ul&gt;
          &lt;li&gt;dim += len(dicts[i])&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;input_types.append(sparse_binary_vector_sequence(dim))【sparse binary vector, most of the value is &lt;strong&gt;0,&lt;/strong&gt; and the non zero elements are fixed to &lt;strong&gt;1.&lt;/strong&gt;】&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;settings.input_types = input_types【&lt;strong&gt;只有4个元素，前三个每个均是长度为len(dicts[i])的integer sequence，最后一个元素是sparse_binary_vector，维度是3-&amp;gt;3+feature的所有len(dicts[i])的和&lt;/strong&gt;】&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;421-process的流程&quot;&gt;4.2.1 process的流程：&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;num_features = len(dicts)&lt;/li&gt;
  &lt;li&gt;num_sequences = 0&lt;/li&gt;
  &lt;li&gt;sequence = []&lt;/li&gt;
  &lt;li&gt;遍历输入文件：
    &lt;ul&gt;
      &lt;li&gt;features = line.split(‘ ‘)，将features放进sequence中。（也就是说sequence最开始会有3个元素：词，tag_pos，tag，比如’jetliners’, ‘NNS’, ‘I-NP’）&lt;/li&gt;
      &lt;li&gt;每读到空行，表示一个序列结束了，
        &lt;ul&gt;
          &lt;li&gt;进行make_features(sequence)【和initialize一样】&lt;/li&gt;
          &lt;li&gt;yield gen_sample(sequence)【代码如下所示，大致是，对每一个sequence的元素，产出一个feature+3维的数组sample，前三个元素都是list，从dicts中获取相应value对应的id，append进去，如果拿不到==&amp;gt;若对应的oov_policy是OOV_POLICY_IGNORE(feature0),那么append 0xffffffff，否则如果是OOV_POLICY_ERROR（feature1\2），那么打日志，即仍是空数组，否则填充0；然后，sample append一个[]。然后vec=[],对于后面feature个元素，遍历完一个元素，dim += len(dicts[i])，对于这个元素，如果找得到id，那么vec.append dim + id，否则如果OOV_POLICY_IGNORE，那么pass，否则如果是OOV_POLICY_ERROR，那么打日志，即vec仍为[]，否则vec.ids.append(dim + 0)。遍历完了之后，把这个vec append到sample[-1]里面去。然后返回sample。也就是说，&lt;strong&gt;sample的格式是[[feature0的id们], [feature1的id们], [feature2的id们], [[feature3的0+id们], [feature4的len(dicts[3])+id们], feature5的len(dicts[3])+len(dicts[4])+id们…]]&lt;/strong&gt;】&lt;/li&gt;
          &lt;li&gt;sequence = []&lt;/li&gt;
          &lt;li&gt;num_sequences += 1&lt;/li&gt;
          &lt;li&gt;continue&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;打印一下num_sequences，关闭文件，结束&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gen_sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_original_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#【num_original_columns=3】
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patterns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([])&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
                &lt;span class=&quot;s&quot;&gt;&quot;Wrong number of features: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_original_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 前三个
&lt;/span&gt;                &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OOV_POLICY_IGNORE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xffffffff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OOV_POLICY_ERROR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fatal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Unknown token: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;   
                    &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patterns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_original_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#后面那些crf feature
&lt;/span&gt;                    &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OOV_POLICY_IGNORE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;    
                    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;oov_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OOV_POLICY_ERROR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fatal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Unknown token: %s&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;   
                        &lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                            
                    &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dicts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;43-linear_crf&quot;&gt;4.3 linear_crf&lt;/h2&gt;

&lt;p&gt;实现的其实是&lt;a href=&quot;http://leon.bottou.org/projects/sgd#stochastic_gradient_crfs&quot;&gt;sgd_crf&lt;/a&gt;，我们可以看到，模型结构图如下所示【不要急 慢慢看】：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/linear_crf.jpeg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;首先，从train.txt中，我们可以发现：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;slot 0 size=6778 # dicts[0],输入单词字典的大小&lt;/li&gt;
  &lt;li&gt;slot 1 size=44 # dicts[1],brill tag的大小&lt;/li&gt;
  &lt;li&gt;slot 2 size=23 # dicts[2],B-/I-xx/O的大小&lt;/li&gt;
  &lt;li&gt;feature size=76328 # 3-&amp;gt;3+feature的所有len(dicts[i])的和&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外，num_label_types=int(math.ceil(float(slot2_size)) / 8)) * 8 = 24&lt;/p&gt;

&lt;p&gt;4个&lt;strong&gt;data_layer&lt;/strong&gt;：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;features：size=76328&lt;/li&gt;
  &lt;li&gt;word： size=6778&lt;/li&gt;
  &lt;li&gt;pos: size=44&lt;/li&gt;
  &lt;li&gt;chunk: size=num_label_types=24&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;接下来是一个fc_layer(input=features,size=num_label_types=24,name=crf_input)&lt;/p&gt;

&lt;p&gt;然后这个crf_input后面接了一个crf_layer(input=crf_input,label=chunk)&lt;/p&gt;

&lt;h2 id=&quot;44-rnn_crf&quot;&gt;4.4 rnn_crf&lt;/h2&gt;

&lt;p&gt;实现的其实是大标题3的双向lstm和大标题2的crf，我们可以看到，模型结构图如下所示【不要急 慢慢看】：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/rnn_crf.jpeg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>paddlepaddle layers</title>
   <link href="http://daiwk.github.io/posts/platform-paddlepaddle-layers.html"/>
   <updated>2016-10-20T00:00:00+00:00</updated>
   <id>/posts/platform-paddlepaddle-layers</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;p&gt;暂无（格式有问题）&lt;/p&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;Layers:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.paddlepaddle.org/doc/ui/api/trainer_config_helpers/layers.html&quot;&gt;http://www.paddlepaddle.org/doc/ui/api/trainer_config_helpers/layers.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ParameterAttribute:
&lt;a href=&quot;http://www.paddlepaddle.org/doc/ui/api/trainer_config_helpers/attrs.html&quot;&gt;http://www.paddlepaddle.org/doc/ui/api/trainer_config_helpers/attrs.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;parameter--extra-layer-attribute&quot;&gt;Parameter &amp;amp; Extra Layer Attribute&lt;/h2&gt;

&lt;h4 id=&quot;parameteratribute&quot;&gt;ParameterAtribute&lt;/h4&gt;

&lt;p&gt;在fine-tuning training的过程中，可以设置这个object来控制training的详情，诸如l1/l2 rate/learning rate/如何初始化参数等&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) – default parameter name.&lt;/li&gt;
      &lt;li&gt;is_static (bool) – True if this parameter will be fixed while training.&lt;/li&gt;
      &lt;li&gt;initial_std (float or None) – Gauss Random initialization standard deviation. None if not using Gauss Random initialize parameter.&lt;/li&gt;
      &lt;li&gt;initial_mean (float or None) – Gauss Random initialization mean. None if not using Gauss Random initialize parameter.&lt;/li&gt;
      &lt;li&gt;initial_max (float or None) – Uniform initialization max value.&lt;/li&gt;
      &lt;li&gt;initial_min (float or None) – Uniform initialization min value.&lt;/li&gt;
      &lt;li&gt;l1_rate (float or None) – the l1 regularization factor&lt;/li&gt;
      &lt;li&gt;l2_rate (float or None) – the l2 regularization factor&lt;/li&gt;
      &lt;li&gt;learning_rate (float or None) – The parameter learning rate. None means 1. The learning rate when optimize is LEARNING_RATE = GLOBAL_LEARNING_RATE * PARAMETER_LEARNING_RATE * SCHEDULER_FACTOR.&lt;/li&gt;
      &lt;li&gt;momentum (float or None) – The parameter momentum. None means use global value.&lt;/li&gt;
      &lt;li&gt;sparse_update (bool) – Enable sparse update for this parameter. It will enable both local and remote sparse update&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;set_default_parameter_name&quot;&gt;set_default_parameter_name&lt;/h4&gt;

&lt;p&gt;设置parameter的默认名字，如果不设，那就用默认的parameter name&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) – default parameter name.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;extralayeratribute&quot;&gt;ExtraLayerAtribute&lt;/h4&gt;

&lt;p&gt;一些高阶的layer attribute设置，可以设置所有，但有些layer并不支持所有attribute，&lt;strong&gt;一旦设置了不支持的，会报错且core……&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;error_clipping_threshold (float) – Error clipping threshold.&lt;/li&gt;
      &lt;li&gt;drop_rate (float) – Dropout rate. Dropout will create a mask on layer output. The dropout rate is the zero rate of this mask. The details of what dropout is please refer to here.&lt;/li&gt;
      &lt;li&gt;device (int) – device ID of layer. device=-1, use CPU. device&amp;gt;0, use GPU. The details allocation in parallel_nn please refer to here.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;paramattr&quot;&gt;ParamAttr&lt;/h4&gt;

&lt;p&gt;是ParameterAtribute的alias&lt;/p&gt;

&lt;h4 id=&quot;extraattr&quot;&gt;ExtraAttr&lt;/h4&gt;

&lt;p&gt;是ExtraLayerAttribute的alias&lt;/p&gt;

&lt;h2 id=&quot;base&quot;&gt;Base&lt;/h2&gt;

&lt;h3 id=&quot;layertype&quot;&gt;LayerType&lt;/h3&gt;

&lt;p&gt;layer type enumerations.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;type_name (basestring) – layer type name. Because layer type enumerations are strings.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;True if is a layer_type&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;bool&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;layeroutput&quot;&gt;LayerOutput&lt;/h3&gt;

&lt;p&gt;layer函数的输出，主要用于：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;检查layer的connection是否make sense： 例如，FC(Softmax) =&amp;gt; Cost(MSE Error) is not good&lt;/li&gt;
  &lt;li&gt;tracking layer connection&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;作为layer方法的输入&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) – Layer output name.&lt;/li&gt;
      &lt;li&gt;layer_type (basestring) – Current Layer Type. One of LayerType enumeration.&lt;/li&gt;
      &lt;li&gt;activation (BaseActivation.) – Layer Activation.&lt;/li&gt;
      &lt;li&gt;parents (list/tuple/collection.Sequence) – Layer’s parents.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;data-layer&quot;&gt;Data layer&lt;/h2&gt;

&lt;h1 id=&quot;data_layer&quot;&gt;data_layer&lt;/h1&gt;

&lt;p&gt;数据层的定义。用法：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) – Name of this data layer.&lt;/li&gt;
      &lt;li&gt;size (int) – Size of this data layer.&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute.) – Extra Layer Attribute.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;fully-connected-layers&quot;&gt;Fully Connected Layers&lt;/h1&gt;

&lt;h2 id=&quot;fc_layer&quot;&gt;fc_layer&lt;/h2&gt;

&lt;p&gt;全连接层，用法：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LinearActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;bias_attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;等价于：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mixed_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;full_matrix_projection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) : The Layer Name.&lt;/li&gt;
      &lt;li&gt;input (LayerOutput/list/tuple) : The input layer. Could be a list/tuple of input layer.&lt;/li&gt;
      &lt;li&gt;size (int) : The layer dimension.&lt;/li&gt;
      &lt;li&gt;act (BaseActivation) – Activation Type. Default is &lt;strong&gt;tanh&lt;/strong&gt;.&lt;/li&gt;
      &lt;li&gt;param_attr (ParameterAttribute) – The Parameter Attribute/list.&lt;/li&gt;
      &lt;li&gt;bias_attr (ParameterAttribute/None/Any) – The Bias Attribute. If no bias, then pass False or something not type of ParameterAttribute. None will get a default Bias.&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute/None) – Extra Layer config.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return Type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;selective_fc_layer&quot;&gt;selective_fc_layer&lt;/h2&gt;

&lt;p&gt;与全连接层的区别：输出可能是sparse的。有一个select参数，指定several selected columns for output。如果select参数没有被指定，那么他和fc_layer是一样的。用法如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sel_fc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;selective_fc_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TanhActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) : The Layer Name.&lt;/li&gt;
      &lt;li&gt;input (LayerOutput/list/tuple) : The input layer. Could be a list/tuple of input layer.&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;select (LayerOutput)&lt;/strong&gt; : The select layer. The output of select layer should be &lt;strong&gt;a sparse binary matrix&lt;/strong&gt;, and treat as the mask of selective fc.&lt;/li&gt;
      &lt;li&gt;size (int) : The layer dimension.&lt;/li&gt;
      &lt;li&gt;act (BaseActivation) – Activation Type. Default is &lt;strong&gt;tanh&lt;/strong&gt;.&lt;/li&gt;
      &lt;li&gt;param_attr (ParameterAttribute) – The Parameter Attribute/list.&lt;/li&gt;
      &lt;li&gt;bias_attr (ParameterAttribute/None/Any) – The Bias Attribute. If no bias, then pass False or something not type of ParameterAttribute. None will get a default Bias.&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute/None) – Extra Layer config.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return Type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;conv-layers&quot;&gt;Conv Layers&lt;/h1&gt;

&lt;h2 id=&quot;conv_operator&quot;&gt;conv_operator&lt;/h2&gt;

&lt;p&gt;与img_conv_layer不同，conv_op是一个Operator，能在mixed_layer里面使用。conv_op需要两个input来perform convolution。第一个input是image，第二个input是filter kernel。&lt;strong&gt;只支持GPU mode&lt;/strong&gt;。用法：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                   &lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                   &lt;span class=&quot;n&quot;&gt;filter_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                   &lt;span class=&quot;n&quot;&gt;num_filters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                   &lt;span class=&quot;n&quot;&gt;num_channels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;img (LayerOutput) – input image&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;filter (LayerOutput) – input filter&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;filter_size (int) – The x dimension of a filter kernel.&lt;/li&gt;
      &lt;li&gt;filter_size_y (int) – The y dimension of a filter kernel. Since PaddlePaddle now supports rectangular filters, the filter’s shape can be (filter_size, filter_size_y).&lt;/li&gt;
      &lt;li&gt;num_filters (int) – channel of output data.&lt;/li&gt;
      &lt;li&gt;num_channel (int) – channel of input data.&lt;/li&gt;
      &lt;li&gt;stride (int) – The x dimension of the stride.&lt;/li&gt;
      &lt;li&gt;stride_y (int) – The y dimension of the stride.&lt;/li&gt;
      &lt;li&gt;padding (int) – The x dimension of padding.&lt;/li&gt;
      &lt;li&gt;padding_y (int) – The y dimension of padding.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;a ConvOperator Object.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return tpye:
    &lt;ul&gt;
      &lt;li&gt;ConvOperator&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conv_shift_layer&quot;&gt;conv_shift_layer&lt;/h2&gt;

&lt;p&gt;计算两个input的cyclic(环形的，循环的) convolution：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
c[i]=\sum _{j-(N-1)/2}^{(N-1)/2}a_{i+1}*b_j
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;上式中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;个元素，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(b\)&lt;/code&gt;有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;个元素(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;为奇数)，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;个元素。当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(b\)&lt;/code&gt;的下标为负数时，表示从右往左的下标。&lt;/p&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;conv_shift&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_shift_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) – layer name&lt;/li&gt;
      &lt;li&gt;a (LayerOutput) – Input layer a.&lt;/li&gt;
      &lt;li&gt;b (LayerOutput) – input layer b&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;img_conv_layer&quot;&gt;img_conv_layer&lt;/h2&gt;

&lt;p&gt;image的卷积层。目前Paddle只支持width=height的正方形图片作为输入。卷积层的具体定义见&lt;a href=&quot;http://ufldl.stanford.edu/tutorial/supervised/FeatureExtractionUsingConvolution/&quot;&gt;UFLDL&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;其中的num_channel是输入的image的channel数，当输入是图片时可以是1或者3（mono【单通道】 or RGB），当输入是layer时，可以是上一个layer的num_filters * num_group&lt;/p&gt;

&lt;p&gt;Paddle中有一些group的filter，每个group可以处理inputs的一些channel。例如，一个input num_channel=256, group=4, num_filter=32，那么Paddle会生成&lt;strong&gt;32*4=128个filter&lt;/strong&gt;对inputs进行处理。channels会被分成4部分，first 256/4个channels被first 32个filters处理，以此类推。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) – Layer name.&lt;/li&gt;
      &lt;li&gt;input (LayerOutput) – Layer Input.&lt;/li&gt;
      &lt;li&gt;filter_size (int/tuple/list) – The x dimension of a filter kernel. Or input a tuple for two image dimension.&lt;/li&gt;
      &lt;li&gt;filter_size_y (int/None) – The y dimension of a filter kernel. Since PaddlePaddle currently supports rectangular filters, the filter’s shape will be (filter_size, filter_size_y).&lt;/li&gt;
      &lt;li&gt;num_filters – Each filter group’s number of filter&lt;/li&gt;
      &lt;li&gt;act (BaseActivation) – Activation type. Default is tanh&lt;/li&gt;
      &lt;li&gt;groups (int) – Group size of filters.&lt;/li&gt;
      &lt;li&gt;stride (int/tuple/list) – The x dimension of the stride. Or input a tuple for two image dimension.&lt;/li&gt;
      &lt;li&gt;stride_y (int) – The y dimension of the stride.&lt;/li&gt;
      &lt;li&gt;padding (int/tuple/list) – The x dimension of the padding. Or input a tuple for two image dimension&lt;/li&gt;
      &lt;li&gt;padding_y (int) – The y dimension of the padding.&lt;/li&gt;
      &lt;li&gt;bias_attr (ParameterAttribute/False) – Convolution bias attribute. None means default bias. False means no bias.&lt;/li&gt;
      &lt;li&gt;num_channels (int) – number of input channels. If None will be set automatically from previous output.&lt;/li&gt;
      &lt;li&gt;param_attr (ParameterAttribute) – Convolution param attribute. None means default attribute&lt;/li&gt;
      &lt;li&gt;shared_biases (bool) – Is biases will be shared between filters or not.&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute) – Layer Extra Attribute.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;context_projection&quot;&gt;context_projection&lt;/h2&gt;

&lt;p&gt;将一个序列根据设置的context_len，转化为以context_start=-(context_len - 1) / 2为开头的序列。如果context position超出了序列的长度，如果padding_attr=False，那么将会填充0，否则，padding是可以通过训练得到的（learnable），并将此变量赋值为ParameterAttribute类型。&lt;/p&gt;

&lt;p&gt;例如，原始序列是[a b c d e f g]，context_len=3，padding_attr=False，那么，context_start = -1，所以，产生的序列就是[0ab abc bcd cde def efg fg0]&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;input (LayerOutput) – Input Sequence.&lt;/li&gt;
      &lt;li&gt;context_len (int) – context length.&lt;/li&gt;
      &lt;li&gt;context_start (int) – context start position. Default is -(context_len - 1)/2&lt;/li&gt;
      &lt;li&gt;padding_attr (bool/ParameterAttribute) – Padding Parameter Attribute. If false, it means padding always be zero. Otherwise Padding is learnable, and parameter attribute is set by this parameter.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;Projection&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;Projection&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;image-pooling-layer&quot;&gt;Image Pooling Layer&lt;/h1&gt;

&lt;h2 id=&quot;img_pool_layer&quot;&gt;img_pool_layer&lt;/h2&gt;

&lt;p&gt;图像处理中的pooling层，详见&lt;a href=&quot;http://ufldl.stanford.edu/tutorial/supervised/Pooling/&quot;&gt;UFLDL的pooling介绍&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params：
    &lt;ul&gt;
      &lt;li&gt;padding (int) – pooling padding&lt;/li&gt;
      &lt;li&gt;name (basestring.) – name of pooling layer&lt;/li&gt;
      &lt;li&gt;input (LayerOutput) – layer’s input&lt;/li&gt;
      &lt;li&gt;pool_size (int) – pooling size&lt;/li&gt;
      &lt;li&gt;num_channels (int) – number of input channel.&lt;/li&gt;
      &lt;li&gt;pool_type (BasePoolingType) – pooling type. MaxPooling or AveragePooling. &lt;strong&gt;Default is MaxPooling.&lt;/strong&gt;&lt;/li&gt;
      &lt;li&gt;stride (int) – stride of pooling.&lt;/li&gt;
      &lt;li&gt;start (int) – start position of pooling operation.&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute) – Extra Layer attribute.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns：
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;norm-layer&quot;&gt;Norm Layer&lt;/h1&gt;

&lt;h2 id=&quot;img_cmrnorm_layer&quot;&gt;img_cmrnorm_layer&lt;/h2&gt;

&lt;p&gt;Response normalization across feature maps，详见&lt;a href=&quot;../assets/ImageNet Classification with Deep Convolutional Neural Networks.pdf&quot;&gt;Alex的paper(alexnet?)&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (None/basestring) – layer name.&lt;/li&gt;
      &lt;li&gt;input (LayerOutput) – layer’s input.&lt;/li&gt;
      &lt;li&gt;size (int) – Normalize in number of sizesize feature maps.&lt;/li&gt;
      &lt;li&gt;scale (float) – The hyper-parameter.&lt;/li&gt;
      &lt;li&gt;power (float) – The hyper-parameter.&lt;/li&gt;
      &lt;li&gt;num_channels – input layer’s filers number or channels. If num_channels is None, it will be set automatically.&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute) – Extra Layer Attribute.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;batch_norm_layer&quot;&gt;batch_norm_layer&lt;/h2&gt;

&lt;p&gt;batch normalization，定义如下(详见&lt;a href=&quot;../assets/Batch Normalization Accelerating Deep Network Training by Reducing Internal Covariate Shift.pdf&quot;&gt;paper&lt;/a&gt;)：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\mu _\beta\leftarrow \frac{1}{m}\sum _{i=1}^mx_i \ \ //\ mini-batch\ mean \\
\sigma _\beta^2\leftarrow \frac{1}{m}\sum _{i=1}^m(x_i-\mu _\beta)^2 \ \ //\ mini-batch\ variance \\
\hat{x_i}\leftarrow \frac{(x_i-\mu _\beta )}{\sqrt{\sigma _\beta ^2+\epsilon }} \ \ //\ normalize \\
y_i \leftarrow \gamma \hat{x_i}+\beta \ \ //\ scale\ and\ shift \\
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;上式中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;是一个mini-batch的input features&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;name (basestring) – layer name.&lt;/li&gt;
      &lt;li&gt;input (LayerOutput) – batch normalization input. Better be linear activation. Because there is an activation inside batch_normalization.&lt;/li&gt;
      &lt;li&gt;
        &lt;table&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;batch_norm_type (None&lt;/td&gt;
              &lt;td&gt;string, None or “batch_norm” or “cudnn_batch_norm”) – We have batch_norm and cudnn_batch_norm. batch_norm supports both CPU and GPU. cudnn_batch_norm requires cuDNN version greater or equal to v4 (&amp;gt;=v4). But cudnn_batch_norm is faster and needs less memory than batch_norm. By default (None), we will automaticly select cudnn_batch_norm for GPU and batch_norm for CPU. Otherwise, select batch norm type based on the specified type. If you use cudnn_batch_norm, we suggested you use latest version, such as v5.1.&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/li&gt;
      &lt;li&gt;act (BaseActivation) – Activation Type. Better be relu. Because batch normalization will normalize input near zero.&lt;/li&gt;
      &lt;li&gt;num_channels (int) – num of image channels or previous layer’s number of filters. None will automatically get from layer’s input.&lt;/li&gt;
      &lt;li&gt;bias_attr (ParameterAttribute) – ββ, better be zero when initialize. So the initial_std=0, initial_mean=1 is best practice.&lt;/li&gt;
      &lt;li&gt;param_attr (ParameterAttribute) – γγ, better be one when initialize. So the initial_std=0, initial_mean=1 is best practice.&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute) – Extra Layer Attribute.&lt;/li&gt;
      &lt;li&gt;use_global_stats (bool/None.) – whether use moving mean/variance statistics during testing peroid. If None or True, it will use moving mean/variance statistics during testing. If False, it will use the mean and variance of current batch of test data for testing.&lt;/li&gt;
      &lt;li&gt;moving_average_fraction (float.) – Factor used in the moving average computation, referred to as facotr,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(runningMean=newMean*(1-factor)+runningMean*factor\)&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;sum_to_one_norm_layer&quot;&gt;sum_to_one_norm_layer&lt;/h2&gt;

&lt;p&gt;NEURAL TURING MACHINE中用到的sum-to-one normalization:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
out[i]=\frac{in[i]}{\sum _{k-1}^{N}in[k]}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(in\)&lt;/code&gt;是一个输入vector（batch_size * data_dim）,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(out\)&lt;/code&gt;是一个输出vector（batch_size * data_dim）。用法：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sum_to_one_norm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum_to_one_norm_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;input (LayerOutput) – Input layer.&lt;/li&gt;
      &lt;li&gt;name (basestring) – Layer name.&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute.) – extra layer attributes.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;recurrent-layers&quot;&gt;Recurrent Layers&lt;/h1&gt;

&lt;h2 id=&quot;recurrent_layer&quot;&gt;recurrent_layer&lt;/h2&gt;

&lt;p&gt;最简单的recurrent unit layer，只是全连接层through both time and neural network。&lt;/p&gt;

&lt;p&gt;对每个[start, end]的序列，计算：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
out_i=act(in_i)\ for\ i=start \\
out_i=act(in_i+out_{i-1}*W)\ for\ i&amp;lt;start&amp;lt;=end \\
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果reverse=True，那么：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
out_i=act(in_i)\ for\ i=end \\
out_i=act(in_i+out_{i+1}*W)\ for\ i&amp;lt;=start&amp;lt;end \\
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Params:
    &lt;ul&gt;
      &lt;li&gt;input (LayerOutput) – Input Layer&lt;/li&gt;
      &lt;li&gt;act (BaseActivation) – activation.&lt;/li&gt;
      &lt;li&gt;bias_attr (ParameterAttribute) – bias attribute.&lt;/li&gt;
      &lt;li&gt;param_attr (ParameterAttribute) – parameter attribute.&lt;/li&gt;
      &lt;li&gt;name (basestring) – name of the layer&lt;/li&gt;
      &lt;li&gt;layer_attr (ExtraLayerAttribute) – Layer Attribute.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;lstmemory&quot;&gt;lstmemory&lt;/h2&gt;

&lt;p&gt;公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
i_t=\sigma (W_ \\
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;lstm_step_layer&quot;&gt;lstm_step_layer&lt;/h2&gt;

&lt;h2 id=&quot;grumemory&quot;&gt;grumemory&lt;/h2&gt;

&lt;h2 id=&quot;gru_step_layer&quot;&gt;gru_step_layer&lt;/h2&gt;

&lt;h1 id=&quot;recurrent-layer-group&quot;&gt;Recurrent Layer Group&lt;/h1&gt;

&lt;h2 id=&quot;recurrent_group&quot;&gt;recurrent_group&lt;/h2&gt;

&lt;h2 id=&quot;beam_search&quot;&gt;beam_search&lt;/h2&gt;

&lt;h2 id=&quot;get_output_layer&quot;&gt;get_output_layer&lt;/h2&gt;

&lt;h1 id=&quot;mixed-layer&quot;&gt;Mixed Layer&lt;/h1&gt;

&lt;h2 id=&quot;mixed_layer&quot;&gt;mixed_layer&lt;/h2&gt;

&lt;h2 id=&quot;embedding_layer&quot;&gt;embedding_layer&lt;/h2&gt;

&lt;h2 id=&quot;dotmul_projection&quot;&gt;dotmul_projection&lt;/h2&gt;

&lt;h2 id=&quot;dotmul_operator&quot;&gt;dotmul_operator&lt;/h2&gt;

&lt;h2 id=&quot;full_matrix_projection&quot;&gt;full_matrix_projection&lt;/h2&gt;

&lt;h2 id=&quot;identity_projection&quot;&gt;identity_projection&lt;/h2&gt;

&lt;h2 id=&quot;table_projection&quot;&gt;table_projection&lt;/h2&gt;

&lt;h2 id=&quot;trans_full_matrix_projection&quot;&gt;trans_full_matrix_projection&lt;/h2&gt;

&lt;h1 id=&quot;aggregate-layers&quot;&gt;Aggregate Layers&lt;/h1&gt;

&lt;h2 id=&quot;pooling_layer&quot;&gt;pooling_layer&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;序列输入的pooling层，和图像输入不一样！！！&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;seq_pool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pooling_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                         &lt;span class=&quot;n&quot;&gt;pooling_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AvgPooling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
                         &lt;span class=&quot;n&quot;&gt;agg_level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AggregateLevel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EACH_SEQUENCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;Params：
    &lt;ul&gt;
      &lt;li&gt;agg_level (AggregateLevel) – AggregateLevel.EACH_TIMESTEP or AggregateLevel.EACH_SEQUENCE&lt;/li&gt;
      &lt;li&gt;name (basestring) – layer name.&lt;/li&gt;
      &lt;li&gt;input (LayerOutput) – input layer name.&lt;/li&gt;
      &lt;li&gt;
        &lt;table&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;pooling_type (BasePoolingType&lt;/td&gt;
              &lt;td&gt;None) – Type of pooling, MaxPooling(default), AvgPooling, SumPooling, SquareRootNPooling.&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;table&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;bias_attr (ParameterAttribute&lt;/td&gt;
              &lt;td&gt;None&lt;/td&gt;
              &lt;td&gt;False) – Bias parameter attribute. False if no bias.&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;table&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;layer_attr (ExtraLayerAttribute&lt;/td&gt;
              &lt;td&gt;None) – The Extra Attributes for layer, such as dropout.&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Returns：
    &lt;ul&gt;
      &lt;li&gt;LayerOutput object&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Return type:
    &lt;ul&gt;
      &lt;li&gt;LayerOutput&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;last_seq&quot;&gt;last_seq&lt;/h2&gt;

&lt;h2 id=&quot;first_seq&quot;&gt;first_seq&lt;/h2&gt;

&lt;h2 id=&quot;concat_layer&quot;&gt;concat_layer&lt;/h2&gt;

&lt;h1 id=&quot;reshaping-layers&quot;&gt;Reshaping Layers&lt;/h1&gt;

&lt;h2 id=&quot;block_expand_layer&quot;&gt;block_expand_layer&lt;/h2&gt;

&lt;h2 id=&quot;expand_layer&quot;&gt;expand_layer&lt;/h2&gt;

&lt;h1 id=&quot;math-layers&quot;&gt;Math Layers&lt;/h1&gt;

&lt;h2 id=&quot;addto_layer&quot;&gt;addto_layer&lt;/h2&gt;

&lt;h2 id=&quot;linear_comb_layer&quot;&gt;linear_comb_layer&lt;/h2&gt;

&lt;h2 id=&quot;interpolation_layer&quot;&gt;interpolation_layer&lt;/h2&gt;

&lt;h2 id=&quot;power_layer&quot;&gt;power_layer&lt;/h2&gt;

&lt;h2 id=&quot;scaling_layer&quot;&gt;scaling_layer&lt;/h2&gt;

&lt;h2 id=&quot;slope_intercept_layer&quot;&gt;slope_intercept_layer&lt;/h2&gt;

&lt;h2 id=&quot;tensor_layer&quot;&gt;tensor_layer&lt;/h2&gt;

&lt;h2 id=&quot;cos_sim&quot;&gt;cos_sim&lt;/h2&gt;

&lt;h2 id=&quot;trans_layer&quot;&gt;trans_layer&lt;/h2&gt;

&lt;h1 id=&quot;sampling-layers&quot;&gt;Sampling Layers&lt;/h1&gt;

&lt;h2 id=&quot;maxid_layer&quot;&gt;maxid_layer&lt;/h2&gt;

&lt;h2 id=&quot;sampling_id_layer&quot;&gt;sampling_id_layer&lt;/h2&gt;

&lt;h1 id=&quot;cost-layers&quot;&gt;Cost Layers&lt;/h1&gt;

&lt;h2 id=&quot;cross_entropy&quot;&gt;cross_entropy&lt;/h2&gt;

&lt;h2 id=&quot;cross_entropy_with_selfnorm&quot;&gt;cross_entropy_with_selfnorm&lt;/h2&gt;

&lt;h2 id=&quot;multi_binary_label_cross_entropy&quot;&gt;multi_binary_label_cross_entropy&lt;/h2&gt;

&lt;h2 id=&quot;huber_cost&quot;&gt;huber_cost&lt;/h2&gt;

&lt;h2 id=&quot;lambda_cost&quot;&gt;lambda_cost&lt;/h2&gt;

&lt;h2 id=&quot;rank_cost&quot;&gt;rank_cost&lt;/h2&gt;

&lt;h2 id=&quot;crf_layer&quot;&gt;crf_layer&lt;/h2&gt;

&lt;h2 id=&quot;crf_decoding_layer&quot;&gt;crf_decoding_layer&lt;/h2&gt;

&lt;h2 id=&quot;ctc_layer&quot;&gt;ctc_layer&lt;/h2&gt;

&lt;h2 id=&quot;hsigmoid&quot;&gt;hsigmoid&lt;/h2&gt;

&lt;h1 id=&quot;check-layer&quot;&gt;Check Layer&lt;/h1&gt;

&lt;h2 id=&quot;eos_layer&quot;&gt;eos_layer&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>概率图模型（HMM/MEMM/CRF）</title>
   <link href="http://daiwk.github.io/posts/nlp-probalistic-graphical-models.html"/>
   <updated>2016-10-20T00:00:00+00:00</updated>
   <id>/posts/nlp-probalistic-graphical-models</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-概率图模型&quot;&gt;&lt;strong&gt;1. 概率图模型&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-贝叶斯网络&quot;&gt;&lt;strong&gt;2. 贝叶斯网络&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-马尔可夫模型&quot;&gt;&lt;strong&gt;3. 马尔可夫模型&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-隐马尔可夫模型&quot;&gt;&lt;strong&gt;4. 隐马尔可夫模型&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-层次化的隐马尔可夫模型&quot;&gt;&lt;strong&gt;5. 层次化的隐马尔可夫模型&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-马尔可夫网络&quot;&gt;&lt;strong&gt;6. 马尔可夫网络&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-最大熵模型&quot;&gt;&lt;strong&gt;7. 最大熵模型&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#8-最大熵隐马尔可夫模型&quot;&gt;&lt;strong&gt;8. 最大熵隐马尔可夫模型&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#9-crf&quot;&gt;&lt;strong&gt;9. CRF&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#友情链接&quot;&gt;&lt;strong&gt;友情链接&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;《统计自然语言处理（第2版）》p104-128,《机器学习》p319-340&lt;/p&gt;

&lt;h1 id=&quot;1-概率图模型&quot;&gt;&lt;strong&gt;1. 概率图模型&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;概率图模型(probalistic graphical models)在&lt;strong&gt;概率模型&lt;/strong&gt;的基础上，使用了&lt;strong&gt;基于图的方法&lt;/strong&gt;来表示概率分布（或者概率密度、密度函数），是一种通用化的不确定性知识表示和处理方法。概率图模型的表达中，&lt;strong&gt;结点表示变量&lt;/strong&gt;，&lt;strong&gt;结点之间的边表示相应变量之间的对应关系&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;例如，假设S为一个汉语句子，X是句子S切分出来的词序列，那么，分词的过程可以看作是推断使&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(X|S)\)&lt;/code&gt;最大的词序列X的分布。而词性标注中，可以看作在给定序列X的情况下，寻找一组最可能的词性标签分布T，使得&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(T|X)\)&lt;/code&gt;最大。&lt;/p&gt;

&lt;p&gt;根据图模型的边是否有向，可以分为有向概率图模型和无向概率图模型：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/probalistic graphical models.JPG&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;而概率图模型在nlp中的演变过程如下所示：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/probalistic graphical models in nlp.JPG&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;2-贝叶斯网络&quot;&gt;&lt;strong&gt;2. 贝叶斯网络&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;贝叶斯网络又称&lt;strong&gt;信度网络或信念网络&lt;/strong&gt;（belief networks），基础理论是贝叶斯公式，目的是通过&lt;strong&gt;概率推理&lt;/strong&gt; 处理&lt;strong&gt;不确定性和不完整性问题&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;一个贝叶斯网络就是一个&lt;strong&gt;有向无环图&lt;/strong&gt;（directed acyclic graph, DAG），&lt;strong&gt;结点表示随机变量&lt;/strong&gt; （可观测量/隐含变量/未知参量或假设等），&lt;strong&gt;有向边表示条件依存关系&lt;/strong&gt;（箭头指向的结点依存于发出箭头的结点【父节点】）。两个结点没有连线表示两个随机变量在某些特定情况下&lt;strong&gt;条件独立&lt;/strong&gt;，如果有连线，则任何条件下都不可能条件独立。&lt;/p&gt;

&lt;p&gt;每一个结点都和一个概率函数相关，概率函数的输入是该结点的父结点所表示的随机变量的一组特定值，输出为当前结点表示的随机变量的概率值。概率值的大小表示结点之间已存关系的强度。&lt;/p&gt;

&lt;p&gt;对于一篇写了某个岛屿的文章而言，是否是一篇新闻记为事件N，是否记录了历史记为事件H，是否记录了旅游风光记为事件S。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;img src=&quot;../assets/bayes belief network.png&quot; alt=&quot;&quot; /&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;则三个事件的联合概率函数为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(H,S,N)=P(H|S,N)P(S|N)P(N)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;下面我们来算一个概率：一篇关于某岛屿的文章是记录了历史，那么，它是一篇新闻的概率有多大？&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(N=T|H=T)=\frac{P(H=T,N=T))}{P(H=T))}\\
=\frac {\sum _{S\in \{T,F\}}P(H=T,S,N=T)} {\sum _{N,S\in \{T,F\}}P(H=T,S,N)}\\
=\frac{(0.4\times 0.1\times 0.2=0.008)_{TTT}+(0.3\times 0.9\times 0.2=0.054)_{TFT}}{0.008_{TTT}+0.054_{TFT}+0.256_{TTF}+0.24_{TFF}}\\
=11.11\%
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;构造贝叶斯网络主要有&lt;strong&gt;表示、推断和学习&lt;/strong&gt;三大问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;表示：&lt;/strong&gt;在某一随机变量的集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x=\{X_1,L,X_n\}\)&lt;/code&gt;上给出其&lt;strong&gt;联合概率分布&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;。贝叶斯网络表示中的主要问题是：即使在随机变量&lt;strong&gt;仅有2种取值&lt;/strong&gt;的简单情况下，&lt;strong&gt;一个联合概率分布也需要对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_1,L,x_n\)&lt;/code&gt;的所有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2^n\)&lt;/code&gt;种不同取值下的概率进行说明&lt;/strong&gt;，计算代价、需要学习的参数数量都非常大，几步不可完成。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;推断：&lt;/strong&gt;由于贝叶斯网络是变量及其关系的完整模型，所以可以回答关于变量的询问（如当观察到某些变量（证据变量）时，推断另一些变量子集的变化）。在已知某些证据的情况下&lt;strong&gt;计算变量的后验分布&lt;/strong&gt;的过程称为&lt;strong&gt;概率推理&lt;/strong&gt;。常用的&lt;strong&gt;精确推理方法&lt;/strong&gt;包括&lt;strong&gt;变量消除法&lt;/strong&gt;（variable elimination）和&lt;strong&gt;团树法&lt;/strong&gt; （clique【完全联通子图】 tree）。变量消除法的基本任务是计算条件概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(X_Q|X_E=x)\)&lt;/code&gt;，其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X_Q\)&lt;/code&gt;是询问变量的集合，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X_E\)&lt;/code&gt;是已知证据的变量集合。基本思想是通过分步计算不同变量的边缘分布，按顺序逐个消除未观察到的非询问变量。团树法使用更全局化的数据结构调度各种操作，以获得更加有益的计算代价。常用的&lt;strong&gt;近似推理算法&lt;/strong&gt;有重要性抽样法（importance sampling）、随机马尔可夫链蒙特卡罗模拟法（Markov chain Monte Carlo,MCMC）、循环信念传播法（loopy belief propagation）和泛化信念传播法（generalized belief propagation）等。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;学习：&lt;/strong&gt;包括&lt;strong&gt;参数学习&lt;/strong&gt;和&lt;strong&gt;结构学习&lt;/strong&gt;两部分。参数学习目的是决定变量之间相互关联的量化关系，即依存强度估计。对每个结点而言，需要计算&lt;strong&gt;给定父结点条件下该结点的条件概率&lt;/strong&gt;。通常的参数学习方法包括&lt;strong&gt;最大似然估计法、最大后验概率法、期望最大化法（EM）和贝叶斯估计方法&lt;/strong&gt;。贝叶斯图模型中用的比较多的是贝叶斯估计方法。结构学习指的是，寻找变量之间的图关系。简单情况下可以由专家来构造一个图，但多数实用系统中，需要从大量数据里学习网络结构和局部分布的参数。&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;3-马尔可夫模型&quot;&gt;&lt;strong&gt;3. 马尔可夫模型&lt;/strong&gt;&lt;/h1&gt;

&lt;h1 id=&quot;4-隐马尔可夫模型&quot;&gt;&lt;strong&gt;4. 隐马尔可夫模型&lt;/strong&gt;&lt;/h1&gt;

&lt;h1 id=&quot;5-层次化的隐马尔可夫模型&quot;&gt;&lt;strong&gt;5. 层次化的隐马尔可夫模型&lt;/strong&gt;&lt;/h1&gt;

&lt;h1 id=&quot;6-马尔可夫网络&quot;&gt;&lt;strong&gt;6. 马尔可夫网络&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;类似贝叶斯网络，但主要有以下两个区别：一是可以表示贝叶斯网络无法表示的一些依赖关系（如循环依赖）；另一方面，不能表示贝叶斯网络能表达的某些关系（如推导关系）。&lt;/p&gt;

&lt;p&gt;马尔可夫网络：一组有马尔可夫性质的随机变量的联合概率分布模型，由无向图&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;和定义在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;上的势函数组成。一个无向图&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G=(V,E)\)&lt;/code&gt;，每个顶点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i \in V\)&lt;/code&gt;表示在集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;上的一个随机变量，每条边&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\{x_i,x_j\}\in E(i\neq j)\)&lt;/code&gt;表示直接相连的两个随机变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_j\)&lt;/code&gt;之间的一种依赖关系。&lt;/p&gt;

&lt;p&gt;如果图的一个子图（点和边都是原图的子集）的&lt;strong&gt;任意两个结点之间都有边相连&lt;/strong&gt;，那么这个子图就是一个完全子图（complete subgraph），又称&lt;strong&gt;团（clique）。&lt;/strong&gt;一个团的完全子团称为子团。如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;是无向图&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;的一个子团，且不能再加进任何一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;的结点使其成为一个更大的团，则称&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;为&lt;strong&gt;最大团&lt;/strong&gt;。无向图中，不使用条件概率密度进行参数化，而使用称为团势能（clique potential）作为参数化因子。&lt;strong&gt;每个团都对应着一个势函数（非负实函数），表示团的一个状态。&lt;/strong&gt;一般，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{x}_C\)&lt;/code&gt;表示团&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;中的所有结点，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)\)&lt;/code&gt;表示团势能。由于要求势能函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)\)&lt;/code&gt;&lt;strong&gt;非负&lt;/strong&gt;，所以，一般将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)\)&lt;/code&gt;定义为：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)=exp\{-E(\mathbf{x}_C)\}\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E(\mathbf{x}_C)\)&lt;/code&gt;称为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mathbf{x}_C\)&lt;/code&gt;的能量函数。&lt;/p&gt;

&lt;p&gt;如果分布&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\phi }(x_1,x_2,...,x_n)\)&lt;/code&gt;的图模型可以表示为一个马尔可夫网络&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;，当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;上完全子图的集合【《统计学习方法》p193以及《机器学习》p323说，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;应该是&lt;strong&gt;最大团&lt;/strong&gt;】时，我们说&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;上的分布&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\phi }(x_1,x_2,...,x_n)\)&lt;/code&gt;可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;的团势能函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)\)&lt;/code&gt;进行因子化：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\phi =\{\phi_1(\textbf{x}_{C_{1}}),...,\phi_K(\textbf{x}_{C_{K}})\}\)&lt;/code&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p_{\phi }(x_1,x_2,...,x_n)\)&lt;/code&gt;可以看作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;上的一个吉布斯分布（Gibbs distribution），其概率分布密度为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(x_1,x_2,...,x_n)=\frac{1}{Z}\prod _{i=1}^K\phi _i(\textbf{x}_{C_i})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Z\)&lt;/code&gt;是一个归一化常量，称为划分函数（partition function）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Z=\sum _{x_1,...,x_n}\prod _{i=1}^K\phi _i(\textbf{x}_{C_i})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\textbf{x}_{C_i}\subseteq \{x_1,x_2,...,x_n\}(1\leqslant i\leqslant K)\)&lt;/code&gt;，并且满足&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\bigcup_{i=1}^{K}\textbf{x}_{C_i}=\{x_1,x_2,...,x_n\}\)&lt;/code&gt;（意思是，不需要所有的团，只需要能覆盖所有结点的子团就行了，所以呢，也经常直接用最大团来代替&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C_i\)&lt;/code&gt;）。【另外，个人对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Z\)&lt;/code&gt;的理解，可以理解为遍历每个结点，把包含他的子团乘起来，最后累加，不知对不对……】显然，在无向图模型中每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C_i\)&lt;/code&gt;对应于一个团，而相应的吉布斯分布就是整个图模型的概率分布。因子化的乘积运算可以变成加法运算：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(x_1,x_2,...,x_n)=\frac{1}{Z}exp\{-\sum _{i=1}^KE_{C_i}(x_{C_i})\}=\frac{1}{Z}exp\{-E(\textbf{x})\}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E(\textbf{x})=\sum_{i=1}^{K}E_{C_i}(x_{C_i})\)&lt;/code&gt;。&lt;/p&gt;

&lt;h1 id=&quot;7-最大熵模型&quot;&gt;&lt;strong&gt;7. 最大熵模型&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;最大熵模型的基本原理是：在只掌握关于未知分布的部分信息的情况下，符合已知知识的概率分布可能有多个，但使熵值最大的概率分布最真实地反映了事件的分布情况，因为&lt;strong&gt;熵定义了随机变量的不确定性，当熵最大时，随机变量最不确定，最难准确地预测其行为，&lt;/strong&gt;也就是说，在已知部分信息的前提下，关于未知分布最合理的推断应该是符合已知信息的最不确定或最大随机的推断。&lt;/p&gt;

&lt;p&gt;对于自然语言处理中某个歧义消解问题，用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;表示待消歧问题所有可能候选结果的集合，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(B\)&lt;/code&gt;表示当前歧义点所在上下文信息构成的集合，则称&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((a,b)\)&lt;/code&gt;为模型的一个特征。一般定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\{0,1\}\)&lt;/code&gt;域上的一个二值函数来表示特征：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(a,b) =
\begin{cases}
1, 如果(a,b)\in(A,B)，且满足某种条件 \\
0, else \\
\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;在不引起混淆的情况下，有时也直接把特征函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f(a,b)\)&lt;/code&gt;称作特征。将“判定歧义问题为某种可能的结果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\in A\)&lt;/code&gt;”看作一个事件，该歧义点所在的上下文出现的某些信息看作这个事件发生的条件&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(b\in B\)&lt;/code&gt;，那么，建立最大熵模型的目的就是计算判定结果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;的条件概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(a|b)\)&lt;/code&gt;，即利用条件最大熵模型选择条件概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(a|b)\)&lt;/code&gt;最大的候选结果作为最终的判定结果：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{p}(a|b)=\underset{p\in P}{argmax}H(p)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;是指所建模型中所有与已知样本的概率分布相吻合的概率分布的集合。&lt;/p&gt;

&lt;h1 id=&quot;8-最大熵隐马尔可夫模型&quot;&gt;&lt;strong&gt;8. 最大熵隐马尔可夫模型&lt;/strong&gt;&lt;/h1&gt;

&lt;h1 id=&quot;9-crf&quot;&gt;&lt;strong&gt;9. CRF&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;CRF(Conditional Random Field)条件随机场是用于标注和划分&lt;strong&gt;序列结构数据&lt;/strong&gt;的&lt;strong&gt;概率化&lt;/strong&gt;结构模型。&lt;/p&gt;

&lt;p&gt;对于给定的输出标识序列Y和观测序列X，CRF通过定义&lt;strong&gt;条件概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(Y|X)\)&lt;/code&gt;&lt;/strong&gt;而非联合概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(X,Y)\)&lt;/code&gt;来描述模型。也可以将CRF看作一个&lt;strong&gt;无向图模型&lt;/strong&gt;或者，马尔可夫随机场（Markov Random Field）。&lt;/p&gt;

&lt;p&gt;定义：设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G=(V,E)\)&lt;/code&gt;为一个无向图，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;为结点集合，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E\)&lt;/code&gt;为无向边的集合。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y=\{Y_v|v\in V\}\)&lt;/code&gt;，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;中的 每一个结点对应于一个随机变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y_v\)&lt;/code&gt;，其取值范围为可能的标记集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\{y\}\)&lt;/code&gt;。如果以观察序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;为条件，每一个随机变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y_v\)&lt;/code&gt;都满足以下马尔可夫特性 \eqref{eq:9.1}：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(Y_v|X,Y_w,w\neq v)=P(Y_v|X,Y_w,w\sim v)
\tag{Eq-9.1}
\label{eq:9.1}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(，w\sim v\)&lt;/code&gt;表示两个结点在图&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;中是邻近结点。那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((X,Y)\)&lt;/code&gt;为一个随机场。&lt;/p&gt;

&lt;p&gt;理论上，只要在标记序列中描述了一定的条件独立性，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;的图结构可以是任意的。对序列进行建模可以形成最简单、最普通的链式结构（chain-structured）图，结点对应标记序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y\)&lt;/code&gt;中的元素：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/crf-demo.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;显然，观察序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;的元素之间并不存在图结构，因为这里只是将观察序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;作为条件，并不对其做任何独立性假设。&lt;/p&gt;

&lt;p&gt;在给定观察序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;时，某个特定标记序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Y\)&lt;/code&gt;的概率可以定义为 \eqref{eq:9.2}：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
exp(\sum _j \lambda _jt_j(y_{i-1},y_i,X,i)+\sum _k \mu _ks_k(y_i,X,i))
\tag{Eq-9.2}
\label{eq:9.2}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_j(y_{i-1},y_i,X,i)\)&lt;/code&gt;是&lt;strong&gt;转移函数&lt;/strong&gt;，表示对于观察序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;及其标注序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;及&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i-1\)&lt;/code&gt;位置上标记的转移概率。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_k(y_i,X,i)\)&lt;/code&gt;是&lt;strong&gt;状态函数&lt;/strong&gt;，表示对于观察序列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;其&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;位置的标记概率。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\lambda _j\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\mu _k\)&lt;/code&gt;分别是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_j\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s_k\)&lt;/code&gt;的权重，需要从训练样本中预估出来（模型参数）。&lt;/p&gt;

&lt;p&gt;参照最大熵模型的做法，在定义特征函数时可以定义一组关于观察序列的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\{0,1\}\)&lt;/code&gt;二值特征&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(b(X,i)\)&lt;/code&gt;来标识训练样本中某些分布特性，例如&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
b(X,i) = 
\begin{cases}
1, X的i位置为某个特定的词 \\
0, else \\
\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;转移函数可以定义为如下形式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
t_j(y_{i-1},y_i,X,i) = 
\begin{cases}
b(X,i), y_{i-1}和y_i满足某种搭配条件 \\
0, else \\
\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;为了便于描述，可以将状态函数写成如下形式：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
s(y_i,X,i)=s(y_{i-1},y_i,X,i)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这样，特征函数可以统一表示为 \eqref{eq:9.3}：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
F_j(Y,X)=\sum _{i=1}^{n}f_j(y_{i-1},y_i,X,i)
\tag{Eq-9.3}
\label{eq:9.3}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，每个局部特征函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f_j(y_{i-1},y_i,X,i)\)&lt;/code&gt;表示状态特征&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s(y_{i-1},y_i,X,i)\)&lt;/code&gt;或转移函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t_j(y_{i-1},y_i,X,i)\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;由此，条件随机场定义的条件概率如下\eqref{eq:9.4}：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
p(Y|X,\lambda )=\frac{1}{Z(X)}exp(\lambda _j\cdot F_j(Y,X))
\tag{Eq-9.4}
\label{eq:9.4}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，分母&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Z(X)\)&lt;/code&gt;为归一化因子\eqref{eq:9.5}：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
Z(X)=\sum _Yexp(\lambda _j \cdot F_j(Y,X))
\tag{Eq-9.5}
\label{eq:9.5}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;条件随机场模型也需要解决3个问题：&lt;strong&gt;特征的选取、参数训练和解码&lt;/strong&gt;。训练过程可以在训练数据集上基于&lt;strong&gt;对数似然函数的最大化&lt;/strong&gt;进行。&lt;/p&gt;

&lt;p&gt;相对于HMM,CRF的主要优点在于他的&lt;strong&gt;条件随机性&lt;/strong&gt;，只需要考虑当前已经出现的观测状态，&lt;strong&gt;没有独立性的严格要求&lt;/strong&gt; ，对于整个序列内部的信息和外部观测信息均可有效利用，&lt;strong&gt;避免了&lt;/strong&gt;MEMM（最大熵隐马尔可夫模型）和其他针对线性序列模型的条件马尔可夫模型会出现的&lt;strong&gt;标识偏置问题&lt;/strong&gt;。(the transitions leaving a given
state compete only against each other, rather than against
all other transitions in the model.In the extreme
case, a state with a single outgoing transition effectively
ignores the observation. In those cases, unlike in HMMs,
Viterbi decoding cannot downgrade a branch based on observations
after the branch point, and models with statetransition
structures that have sparsely connected chains of
states are not properly handled.)&lt;/p&gt;

&lt;p&gt;CRF具有MEMM的一切优点，亮着的关键区别在于，MEMM使用每一个状态的指数模型来计算给定前一个状态下当前状态的条件概率，而CRF用&lt;strong&gt;单个指数模型来计算给定观察序列和整个标记序列的联合概率&lt;/strong&gt;。因此，不同状态的不同特征权重可以相互交换代替。(The critical difference between CRFs and
MEMMs is that a MEMM uses per-state exponential models
for the conditional probabilities of next states given the
current state, while a CRF has a single exponential model
for the joint probability of the entire sequence of labels
given the observation sequence.)&lt;/p&gt;

&lt;p&gt;(We can also think of a CRF as a finite state model with unnormalized
transition probabilities. However, unlike some
other weighted finite-state approaches (LeCun et al., 1998),
CRFs assign a well-defined probability distribution over
possible labelings, trained by maximum likelihood or MAP
estimation. Furthermore, the loss function is convex,2 guaranteeing
convergence to the global optimum.)&lt;/p&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;算法&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;优缺点&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;隐马尔可夫模型(HMM)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;最大的缺点就是由于其输出独立性假设，导致其不能考虑上下文的特征，限制了特征的选择&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;最大熵隐马尔可夫模型(MEMM)&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;解决了隐马的问题，可以任意选择特征，但由于其在每一节点都要进行归一化，所以只能找到局部的最优值，同时也带来了标记偏见的问题，即凡是训练语料中未出现的情况全都忽略掉&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;条件随机场（CRF）&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;很好的解决了这一问题，他并不在每一个节点进行归一化，而是所有特征进行全局归一化，因此可以求得全局的最优值。&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;p&gt;crf经典资料：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;开山之作：&lt;a href=&quot;../assets/Conditional random fields Probabilistic models for segmenting and labeling sequence data.pdf&quot;&gt;Conditional random fields: Probabilistic models for segmenting and labeling sequence data(Lafferty,2001)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;工具：&lt;strong&gt;CRF++(c++)/CRFSuite(c)/MALLET(java)/nltk(python)&lt;/strong&gt;&lt;/p&gt;

&lt;h1 id=&quot;友情链接&quot;&gt;&lt;strong&gt;友情链接&lt;/strong&gt;&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://ai.stanford.edu/~koller/Papers/Koller+al:SRL07.pdf&quot;&gt;Graphical Models in a Nutshell&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;图模型教科书：《Probabilistic Graphical Models: Principles and Techniques》&lt;/li&gt;
  &lt;li&gt;Prasoon Goyal的博客
    &lt;ul&gt;
      &lt;li&gt;part1:
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;https://blog.statsbot.co/probabilistic-graphical-models-tutorial-and-solutions-e4f1d72af189&quot;&gt;https://blog.statsbot.co/probabilistic-graphical-models-tutorial-and-solutions-e4f1d72af189&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650725041&amp;amp;idx=1&amp;amp;sn=0c57ba70e2613e6af80c4ab61c996d44&amp;amp;chksm=871b1ecfb06c97d9547e50705d3e74a2b8c41254f0efc2dd88d2e89eec3bfac5da089f28c398&amp;amp;scene=21#wechat_redirect&quot;&gt;想了解概率图模型？你要先理解图论的基本定义与形式&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;part2:
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;https://blog.statsbot.co/probabilistic-graphical-models-tutorial-d855ba0107d1&quot;&gt;https://blog.statsbot.co/probabilistic-graphical-models-tutorial-d855ba0107d1&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/S-6Mb6zNzVPpxR8DbWdT-A&quot;&gt;读懂概率图模型：你需要从基本概念和参数估计开始&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>paddlepaddle中的seq2seq的demo</title>
   <link href="http://daiwk.github.io/posts/platform-paddlepaddle-seq2seq.html"/>
   <updated>2016-10-19T00:00:00+00:00</updated>
   <id>/posts/platform-paddlepaddle-seq2seq</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-seq2seq%E7%AE%80%E4%BB%8B&quot;&gt;&lt;strong&gt;1. seq2seq简介&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-seq2seq%E6%95%B0%E6%8D%AE%E9%9B%86%E7%AE%80%E4%BB%8B&quot;&gt;&lt;strong&gt;2. seq2seq数据集简介&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-seq2seq%E6%95%B0%E6%8D%AE%E9%A2%84%E5%A4%84%E7%90%86&quot;&gt;&lt;strong&gt;2. seq2seq数据预处理&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-seq2seq%E8%AE%AD%E7%BB%83&quot;&gt;&lt;strong&gt;3. seq2seq训练&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-seq2seq%E7%94%9F%E6%88%90%E6%96%87%E6%9C%AC&quot;&gt;&lt;strong&gt;3. seq2seq生成文本&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h1 id=&quot;1-seq2seq简介&quot;&gt;&lt;strong&gt;1. seq2seq简介&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;seq2seq的demo：&lt;a href=&quot;http://www.paddlepaddle.org/doc/demo/text_generation/text_generation.html&quot;&gt;http://www.paddlepaddle.org/doc/demo/text_generation/text_generation.html&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;2-seq2seq数据集简介&quot;&gt;&lt;strong&gt;2. seq2seq数据集简介&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;首先，拿到的数据是这样的（法语-&amp;gt;英语,wmt14数据集）：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/wmt14 directory.JPG&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;gen/test/train三个目录，每个下面有xx.src和xx.trg两个文件，一行是一句话，src和trg的相同行表示那句话对应的翻译是什么，所以，src和trg一样多行。&lt;/p&gt;

&lt;h1 id=&quot;2-seq2seq数据预处理&quot;&gt;&lt;strong&gt;2. seq2seq数据预处理&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;然后，需要进行预处理(-i INPUT: the path of input original dataset;-d DICTSIZE: the specified word count of dictionary, if not set, dictionary will contain all the words in input dataset;-m –mergeDict: merge source and target dictionary, thus, two dictionaries have the same context)&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python preprocess.py &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; data/wmt14 &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; 30000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;得到结果如下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/wmt14 preprocessed directory.JPG&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;其中，train/test/gen目录下是将原始数据的src和trg对应的行用\t连接起来生成的。&lt;/li&gt;
  &lt;li&gt;train/test/gen.list是上述三个目录对应文件的指针。&lt;/li&gt;
  &lt;li&gt;src/trg.dict是上述dict_size大小的字典，包括dict_size-3个高频词和3个特殊词：&amp;lt;s&amp;gt;（sequence的开头）&amp;lt;e&amp;gt;（sequence的结尾）&amp;lt;unk&amp;gt;（不在词典中的词）&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;3-seq2seq训练&quot;&gt;&lt;strong&gt;3. seq2seq训练&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;接下来开始进行训练：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;c1&quot;&gt;#其实seq_to_seq_data里面，读trg.dict和src.dict的时候，使用了
#trg_dict = dict()
#for line_count, line in enumerate(open(trg_lang_dict, &quot;r&quot;)):
#    trg_dict[line.strip()] = line_count
#对dict文件的每个单词，按照行号进行了编号！！
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#train.conf
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;..&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;seqToseq_net&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# whether this config is used for generating
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Data Definiation
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_dir&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./data/pre-wmt14&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;train_conf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_to_seq_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                             &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Algorithm Configuration
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;learning_method&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AdamOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5e-4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Network Architecture
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gru_encoder_decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_conf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;#train.cluster.conf
#edit-mode: -*- python -*-
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;..&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### for cluster training
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cluster_config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fs_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hdfs://xxxxxxxxxxxx&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fs_ugi&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;xxxx,xxxx&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;work_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/xxxxxxxxxxxx&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;seqToseq_net&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# whether this config is used for generating
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# whether this config is used for cluster training
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_cluster&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_config_arg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'is_cluster'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Data Definiation
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_dir&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./data/pre-wmt14&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_cluster&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;train_conf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_to_seq_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                             &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Algorithm Configuration
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;learning_method&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AdamOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5e-4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Network Architecture
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gru_encoder_decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_conf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;train.conf对应的网络结构图(慎入！。。)：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;../assets/train_seq2seq.jpeg&quot;&gt;train_seq2seq.jpeg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;注：获取mpi job的status的脚本：&lt;a href=&quot;../source_codes/get_mpi_job_status.py&quot;&gt;get_mpi_job_status.py&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;c&quot;&gt;#num_passes: set number of passes. One pass in paddle means training all samples in dataset one time&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#show_parameter_stats_period: here show parameter statistic every 100 batches&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#trainer_count: set number of CPU threads or GPU devices&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#log_period: here print log every 10 batches&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#dot_period: here print ‘.’ every 5 batches&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;## local&lt;/span&gt;
paddle train &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'translation/train.conf'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--save_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'translation/model'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--use_gpu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--num_passes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;16 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--show_parameter_stats_period&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;100 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--trainer_count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--log_period&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--dot_period&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
2&amp;gt;&amp;amp;1 | &lt;span class=&quot;nb&quot;&gt;tee&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'translation/train.log'&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;## cluster&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;thirdparty&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ../dataprovider.py &lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt;/.
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ../seqToseq_net.py &lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt;/.
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ../data/pre-wmt14/src.dict &lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt;/.
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ../data/pre-wmt14/trg.dict &lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt;/.

paddle cluster_train &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;train.cluster.conf &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--config_args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;is_cluster&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--use_gpu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;cpu &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--trainer_count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--num_passes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;16 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--log_period&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--thirdparty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./thirdparty &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--num_nodes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--job_priority&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;normal &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--job_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxxxx_paddle_platform_translation_demo &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--time_limit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;00:30:00 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--submitter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxxxxxxx &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;--where&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxxxxxxxxx


&lt;span class=&quot;nv&quot;&gt;jobid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;jobid train.log.&lt;span class=&quot;nv&quot;&gt;$timestamp&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'jobid='&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $2}'&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'.'&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'{print $1}'&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$jobid&lt;/span&gt;
~/.jumbo/bin/python &lt;span class=&quot;nv&quot;&gt;$workspace_path&lt;/span&gt;/get_mpi_job_status.py &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$jobid&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; ecom_off
&lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$?&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ne&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;mpi job failed...&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$jobid&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;日志形如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#I0719 19:16:45.952062 15563 TrainerInternal.cpp:160]  Batch=10 samples=500 AvgCost=198.475 CurrentCost=198.475 Eval: classification_error_evaluator=0.737155  CurrentEval: classification_error_evaluator=0.737155&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#I0719 19:17:56.707319 15563 TrainerInternal.cpp:160]  Batch=20 samples=1000 AvgCost=157.479 CurrentCost=116.483 Eval: classification_error_evaluator=0.698392  CurrentEval: classification_error_evaluator=0.659065&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#.....&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#AvgCost: Average Cost from 0th batch to current batch&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#CurrentCost: Cost in current batch&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#classification_error_evaluator(Eval): False prediction rate for each word from 0th evaluation to current evaluation&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#classification_error_evaluator(CurrentEval): False prediction rate for each word in current evaluation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;3-seq2seq生成文本&quot;&gt;&lt;strong&gt;3. seq2seq生成文本&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;最后，需要生成文本：&lt;/p&gt;

&lt;p&gt;首先，把模型文件拷到data/wmt14_model目录下，然后gen.conf如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;..&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;seqToseq_net&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# whether this config is used for generating
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Data Definiation
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen_conf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seq_to_seq_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./data/pre-wmt14&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;gen_result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;./translation/gen_result&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Algorithm Configuration
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;learning_method&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AdamOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;### Network Architecture
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gru_encoder_decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen_conf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_generating&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;gen.conf对应的网络结构图(慎入！。。)：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;../assets/gen_seq2seq.jpeg&quot;&gt;gen_seq2seq.jpeg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;生成的命令如下&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# local&lt;/span&gt;
paddle train &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--job&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'translation/gen.conf'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--save_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'data/wmt14_model'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--use_gpu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--num_passes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;13 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--test_pass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;12 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--trainer_count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    2&amp;gt;&amp;amp;1 | &lt;span class=&quot;nb&quot;&gt;tee&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'translation/gen.log'&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#job: set job mode to test&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#save_dir: the path of saved models&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#num_passes and test_pass: loading model parameters from test_pass to (num_passes - 1), here only loads data/wmt14_model/pass-00012&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;gen的结果是这个文件translation/gen_result，内容如下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0
0       &lt;span class=&quot;nt&quot;&gt;-11&lt;/span&gt;.1314         The &amp;lt;unk&amp;gt; &amp;lt;unk&amp;gt; about the width of the seats &lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;large controls are at stake &amp;lt;e&amp;gt;
1       &lt;span class=&quot;nt&quot;&gt;-11&lt;/span&gt;.1519         The &amp;lt;unk&amp;gt; &amp;lt;unk&amp;gt; on the width of the seats &lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;large controls are at stake &amp;lt;e&amp;gt;
2       &lt;span class=&quot;nt&quot;&gt;-11&lt;/span&gt;.5988         The &amp;lt;unk&amp;gt; &amp;lt;unk&amp;gt; about the width of the seats &lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;large controls are at stake &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &amp;lt;e&amp;gt;

1
0       &lt;span class=&quot;nt&quot;&gt;-24&lt;/span&gt;.4149         The dispute is between the major aircraft manufacturers about the width of the tourist seats on the &amp;lt;unk&amp;gt; flights , paving the way &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;a &amp;lt;unk&amp;gt; confrontation during the month of the Dubai &amp;lt;unk&amp;gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &amp;lt;e&amp;gt;
1       &lt;span class=&quot;nt&quot;&gt;-26&lt;/span&gt;.9524         The dispute is between the major aircraft manufacturers about the width of the tourist seats on the &amp;lt;unk&amp;gt; flights , paving the way &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;a &amp;lt;unk&amp;gt; confrontation during the month of Dubai &amp;amp;apos&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; s &amp;lt;unk&amp;gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &amp;lt;e&amp;gt;
2       &lt;span class=&quot;nt&quot;&gt;-27&lt;/span&gt;.9574         The dispute is between the major aircraft manufacturers about the width of the tourist seats on the &amp;lt;unk&amp;gt; flights , paving the way &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;a &amp;lt;unk&amp;gt; confrontation during the month of Dubai &amp;amp;apos&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; s Dubai &amp;lt;unk&amp;gt; &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &amp;lt;e&amp;gt;
...

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;解释：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#This is the beam search result, where beam size is 3&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#‘0’ in 1st-line and ‘1’ in 6th-line mean the sequence-id in gen data&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#Other six lines list the beam search results&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#  The 2nd-column is the score of beam search (from large to small)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#  The 3rd-colunm is the generating English sequence&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#There is 2 special tokens:&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#  &amp;lt;e&amp;gt;: the end of a sequence&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#  &amp;lt;unk&amp;gt;: a word not included in dictionary&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>老Paddle使用整理</title>
   <link href="http://daiwk.github.io/posts/platform-old-paddle-tutorial.html"/>
   <updated>2016-10-14T00:00:00+00:00</updated>
   <id>/posts/platform-old-paddle-tutorial</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E8%80%81paddle%E4%BD%BF%E7%94%A8%E6%95%B4%E7%90%86&quot;&gt;老Paddle使用整理&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-%E9%A2%84%E5%A4%84%E7%90%86&quot;&gt;1. 预处理&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#11-%E9%A2%84%E5%A4%84%E7%90%86%E5%9F%BA%E6%9C%AC%E7%9F%A5%E8%AF%86&quot;&gt;1.1 预处理基本知识&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#111-%E9%A2%84%E5%A4%84%E7%90%86%E7%9A%84%E6%96%87%E6%9C%AC%E7%BB%93%E6%9E%84&quot;&gt;1.1.1 预处理的文本结构：&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#112-%E9%A2%84%E5%A4%84%E7%90%86%E7%9A%847%E4%B8%AAparam&quot;&gt;1.1.2 预处理的7个param&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#113-proto%E6%A0%BC%E5%BC%8F&quot;&gt;1.1.3 proto格式&lt;/a&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#1131-local%E9%A2%84%E5%A4%84%E7%90%86&quot;&gt;1.1.3.1 local预处理&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#1132-cluster%E9%A2%84%E5%A4%84%E7%90%86&quot;&gt;1.1.3.2 cluster预处理&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#114-%E4%BD%BF%E7%94%A8pydataprovider%E6%A0%BC%E5%BC%8F&quot;&gt;1.1.4 使用pyDataProvider格式&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-%E8%AE%AD%E7%BB%83&quot;&gt;2. 训练&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h1 id=&quot;老paddle使用整理&quot;&gt;老Paddle使用整理&lt;/h1&gt;

&lt;p&gt;以&lt;strong&gt;bi-lstm+crf&lt;/strong&gt;进行品牌词识别为例，对老paddle的使用进行总结。&lt;/p&gt;

&lt;h2 id=&quot;1-预处理&quot;&gt;1. 预处理&lt;/h2&gt;

&lt;h3 id=&quot;11-预处理基本知识&quot;&gt;1.1 预处理基本知识&lt;/h3&gt;

&lt;h3 id=&quot;111-预处理的文本结构&quot;&gt;1.1.1 预处理的文本结构：&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;label;slotid0 fea1[:weight] fea2[:weight];slotid1 fea1[:weight] ...;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;fea:可以是离散值也可以是连续值&lt;/li&gt;
  &lt;li&gt;slot数至少为1，也就是，至少有两个分号&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;说明&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;样本例子&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;连续特征，单slot，特征维数是5&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;0;0 3.15 0 0 0 6.28;&lt;br /&gt;
1;0 3.14 6.28 9.42; 非法，对于连续特征，每个slot必须包含的特征数应该跟维数一致。&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;连续特征，2个slot，特征维数都是5&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;0;0 0 1 2 3 4.5;1 3.14 0 0 0 0;&lt;br /&gt;
1;1 3.14 6.28 0 0 0; 合法，slot0为空。&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;离散特征，单slot，特征维数是1024&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;0;0 1 2 3 5;&lt;br /&gt;
1;0 1 32 64 512 1023;&lt;br /&gt;
0;0 1023 1024; 非法，特征维数是1024，从0开始，所以最大的特征index只能是1023，特征越界。&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;离散特征，3个slot，第1个slot有1024维特征，第2、3个slot有512维特征&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;0;0 1 4 1023;1 4 7 3;2 2 6 511;&lt;br /&gt;
1;2 1 5 88 511;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;离散带权特征，单slot，特征维数是1024&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;0;0 1:3.14 2:6.28 0:0 4:12.56 1023:3.1415926;&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;h4 id=&quot;112-预处理的7个param&quot;&gt;1.1.2 预处理的7个param&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;param1：不需要修改，默认dnn_inst_&lt;/li&gt;
  &lt;li&gt;param2：多少个slot，每个slot分别是什么类型&lt;/li&gt;
&lt;/ul&gt;
&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;param2取值&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;含义&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;0&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;连续值特征，fea为浮点数的情况&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;1&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;离散值特征，不带权，fea为整数的情况&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;2&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;离散值特征，带权，fea为整数，后面跟着冒号和浮点数weight的情况&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;3&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;id特征，仅包含一个整数，可作为样本的label或者id （从Paddle 1.0.0.7版本开始支持）&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;
&lt;p&gt;使用场景：&lt;/p&gt;
&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;使用场景&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;用法&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;样本只有1个slot，该slot由离散型特征组成&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;param2=1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;样本只有1个slot，该slot由连续型特征组合&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;param2=0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;样本有3个slot，其中slot0包含连续特征，其余slot包含离散特征&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;param2=“0 1 1” 【记得这里有双引号】&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;样本有4个slot，其中slot0和slot2的特征是连续的，其他都是离散型特征&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;param2=“0 1 0 1” 记得这里有双引号&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;样本有若干个slot，所有slot的特征都是离散型特征&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;param2=1&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;param3：维数1 维数2 … label数
例如：&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;param3&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;说明&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;&quot;100 100 336 10&quot;&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;多slot样本，三个slot的维数分别为100,100和336，label为10，即十分类问题&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;&quot;536 2&quot;&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;单slot样本，第一个slot为536维特征，label为2，即二分类问题&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;param4：是否对转换后二进制格式的数据压缩，默认不压缩(False)&lt;/li&gt;
  &lt;li&gt;param5：label/哪个slot需要使用词典替换特征。例如：param5=”–dict=0:dict11,2:dict2”  表示，label用dict1替换，slot0，slot1【写1,2，实际上是slot0,1】用dict2替换&lt;/li&gt;
  &lt;li&gt;param6：如果文本格式中的slot_id是不是从0开始的整数，而是字符串等，需要设置该参数，设置样例：param6=”–slotid=slotid0 slotid1 slotid2”&lt;/li&gt;
  &lt;li&gt;param7：如果忽略不在词典中的word，需要设置该参数为True，设置样例：param7=”–ignore=True”&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;113-proto格式&quot;&gt;1.1.3 proto格式&lt;/h4&gt;
&lt;p&gt;假定一条样本为中文句子&lt;strong&gt;“百度 公司 创立于 2000年”&lt;/strong&gt;，样本的类别为“0”。假定“百度”在词表中的id为23，“公司”为35，“创立于”为10，“2000年”为87，词表大小为23984，共有3个类别。
首先我们将其转换成文本格式（label; slot_id word_id1 word_id2 word_id3 word_id4……）【注意，slotid没有实际意义，只是一个编号，表示有多少维特征，从0开始递增】:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;0;0 23 35 10 87;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;而这里我们要需要进行序列标注，所以在wordid这一维特征之外，还要用到别的两个特征，总共如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;wordid：假设训练集使用jieba分词完有4870个词。&lt;/li&gt;
  &lt;li&gt;词性：pos，假设使用jieba进行词性标注，那么有110个类别。&lt;a href=&quot;http://fhqllt.iteye.com/blog/947917&quot;&gt;ICTCLAS 汉语词性标注集&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;占位符：全部写0【老paddle需要这个字段，新paddle不用】&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;比如，一个单词有8个汉字，那么，我们转化为：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;0;0 1383 2523 4396 1253 3967 4333 490 613;1 48 94 94 32 86 17 70 25;2 0 0 0 0 0 0 0 0;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;接下来，使用txt2proto工具进行转换：&lt;/p&gt;

&lt;h5 id=&quot;1131-local预处理&quot;&gt;1.1.3.1 local预处理&lt;/h5&gt;

&lt;p&gt;使用txt2proto这个bin，三个参数，第一个是输出文件，第二个是param2，第三个是param3&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;INPUT_FILE | txt2proto OUTPUT_FILE &lt;span class=&quot;s2&quot;&gt;&quot;1&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;23984 3&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;1132-cluster预处理&quot;&gt;1.1.3.2 cluster预处理&lt;/h5&gt;

&lt;p&gt;需要编写两个conf：&lt;/p&gt;

&lt;p&gt;以train为例：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Local dir.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;LOCAL_HADOOP_HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxxxx

&lt;span class=&quot;c&quot;&gt;# HDFS.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;conf_hdfs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;hdfs://xxxxxx
&lt;span class=&quot;nv&quot;&gt;conf_ugi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxxx,xxxx
&lt;span class=&quot;nv&quot;&gt;conf_tracker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxxx

&lt;span class=&quot;c&quot;&gt;# Data dir.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;input_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;xxx/brand_recognize/input/data_test&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;output_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;xxx/brand_recognize/preprocess/data_test_pb&quot;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Dictionary dir&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;dict_dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#If force_reuse_output_path is True ,paddle will remove outputdir without check outputdir exist&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;force_reuse_output_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Job parameters.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;JOBNAME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;xxxx_gen_proto_test_brand_recognize&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;MAPCAP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5000
&lt;span class=&quot;nv&quot;&gt;REDCAP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5000
&lt;span class=&quot;nv&quot;&gt;MAPNUM&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000
&lt;span class=&quot;nv&quot;&gt;REDNUM&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;114-使用pydataprovider格式&quot;&gt;1.1.4 使用pyDataProvider格式&lt;/h4&gt;

&lt;h2 id=&quot;2-训练&quot;&gt;2. 训练&lt;/h2&gt;
&lt;p&gt;需要准备common.conf和trainer_config.conf&lt;/p&gt;

&lt;p&gt;集群版的common.conf:
local版的common.conf：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://daiwk.github.io/assets/bilstm_crf.conf&quot;&gt;trainer.conf&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/lstm-crf.jpeg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;math&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;################################### Data Configuration ###################################
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4870&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pos_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;110&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;label_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TrainData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProtoData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;files&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;train.list&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto_sequence&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;TestData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProtoData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;files&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;test.list&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;proto_sequence&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;################################### Algorithm Configuration ###################################
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Settings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;algorithm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sgd'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;learning_rate_decay_a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;learning_rate_decay_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;learning_method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'adagrad'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;c1&quot;&gt;#ada_epsilon=1.0,
&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;#num_batches_per_send_parameter=2,
&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;#num_batches_per_get_parameter=1,
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;################################### Network Configuration ###################################
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;word&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pos&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;place_holder&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;hidden_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sentence_vec_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pos_embedding_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;num_rnn_layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr_keep&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;word&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pos&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;place_holder&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;word&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;word&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;MixedLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_word_embedding&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TableProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_keep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;embedding&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#sparse_remote_update=True
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_rnn_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;input_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_reverse_rnn%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_word_embedding&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MixedLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_rnn%d_input&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FullMatrixProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s_rnn%d_input_to_hidden&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_rnn%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;lstmemory&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;active_state_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;active_gate_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sigmoid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'%s_rnn%d_bias'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_keep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_rnn%d_input&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s_rnn%d_weight&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MixedLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_reverse_rnn%d_input&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hidden_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullMatrixProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_rnn%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%s_reverse_rnn%d_input1_to_hidden&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;FullMatrixProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'%s_reverse_rnn%d_input2_to_hidden'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_reverse_rnn%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;lstmemory&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;active_state_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;active_gate_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sigmoid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;reversed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'%s_reverse_rnn%d_bias'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_reverse_rnn%d_input&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hidden_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s_reverse_rnn%d_weight&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;MixedLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_embeddding_and_hidden&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence_vec_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;relu&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullMatrixProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_rnn%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.000001&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s_rnn%d_hidden_to_pooling&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_rnn_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullMatrixProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_reverse_rnn%d&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.000001&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s_reverse_rnn%d_hidden_to_pooling&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_rnn_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullMatrixProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_word_embedding&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.000001&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;%s_embedding_to_pooling&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_hid_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;MixedLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pos_embedding&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos_embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TableProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;pos&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;pos_embedding&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#sparse_remote_update=True
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;FCLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;combine_vec&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'combine_vec_bias'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_embeddding_and_hidden&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence_vec_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'combine_%s_weight'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;pos_embedding&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'combine_pos_weight'&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;FCLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;similarity&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'similarity_bias'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullMatrixProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;combine_vec&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;learning_rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'similarity_weight'&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;MixedLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#active_type = &quot;softmax&quot;,
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#active_type = &quot;sigmoid&quot;,
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;#bias = Bias(initial_std = 0, parameter_name = 'output_bias', learning_rate = lr_output),
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FullMatrixProjection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;similarity&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#learning_rate = lr_output, 
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;initial_std&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#parameter_name = 'output_weight'
&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;CRFLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;crf_cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;crfw&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;crf_layer&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'crf_decoding'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#bias = Bias(initial_std = 0, parameter_name = 'crf_bias', learning_rate = lr_output),
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#Input(&quot;label&quot;)
&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Evaluator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;error&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;sum&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;crf_layer&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#Evaluator(
#    name = &quot;chunk_f1&quot;,
#    type = &quot;chunk&quot;,
#    inputs = [&quot;crf_layer&quot;, &quot;label&quot;],
#    #chunk_scheme = &quot;plain&quot;,
#    chunk_scheme = &quot;IOB&quot;,
#    #num_chunk_types = 3,
#    num_chunk_types = 2,
#)
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;crf_cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;注意：&lt;/strong&gt;运行submit的时候，会自己产出train.list和test.list，并且会生成tester_config.conf&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;使用的是ecom的线下集群，&lt;/p&gt;

&lt;p&gt;查看所有任务：&lt;a href=&quot;http://xxxx/job/&quot;&gt;http://xxxx/job/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;查看当前任务（jobid=287811.xxx）运行：
&lt;a href=&quot;http://xxxx/job/i-287811/&quot;&gt;http://xxxx/job/i-287811/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;干掉这个job：
qdel 287811.xxxx&lt;/p&gt;

&lt;p&gt;预测：
predict这个bin的用法：
I1020 12:35:10.277036 25364 Main.cpp:44] Model path or feature type or both missing. Please read the usage below:
I1020 12:35:10.277549 25364 Main.cpp:46] ./predict model_path feature_type(s) [logLevel] [isSequence]
I1020 12:35:10.277555 25364 Main.cpp:47] @model_path: path where the model stored. the directory indicated by model_path should contain 1 binary network configuration file and 1 sub directory naming ‘model’ that contains the model itself.
I1020 12:35:10.277562 25364 Main.cpp:52] @feature_type: [0|1|2] integer(s) to indicate the type of features in each instance.
I1020 12:35:10.277567 25364 Main.cpp:54] 0 -&amp;gt; continues values. floating points.
I1020 12:35:10.277572 25364 Main.cpp:55] 1 -&amp;gt; discrete values without weights. integers.
I1020 12:35:10.277577 25364 Main.cpp:56] 2 -&amp;gt; discrete values with weights. integers:float.
I1020 12:35:10.277581 25364 Main.cpp:57] @logLevel: from 0 to 4(default), 0 for print debug info (which will lead to core dump on failure), 4 for suppress debug info.
I1020 12:35:10.277586 25364 Main.cpp:60] isSequence is used for SparseNonValuePredictor or MultipleTypesPredictor.
I1020 12:35:10.277591 25364 Main.cpp:61] If isSequence=1, sparse_non_value slots (and only those slots) will be treated as SEQUENCE.
I1020 12:35:10.277596 25364 Main.cpp:62] example: 
I1020 12:35:10.277601 25364 Main.cpp:63] ./predict ./myModel 0
I1020 12:35:10.277604 25364 Main.cpp:64] ./predict ./myModel “0 0 1 2”  &amp;lt;- instance with 4 slots, each slot contain different type of features.&lt;/p&gt;

&lt;p&gt;[INFO][PredictorInternal.cpp][readBinaryConf][40] content length of binary conf is [3923].
[INFO][PredictorInternal.cpp][readBinaryConf][72] read binary conf file done.
[INFO][PredictorInternal.cpp][init][420] output[0]: dim=[4], name=[crf_layer].
[INFO][PredictorInternal.cpp][init][429] input[0]: dim=[4870], name=[word].
[INFO][PredictorInternal.cpp][init][429] input[1]: dim=[110], name=[pos].
[INFO][PredictorInternal.cpp][init][429] input[2]: dim=[1], name=[place_holder].&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>paddlepaddle快速入门教程</title>
   <link href="http://daiwk.github.io/posts/platform-paddlepaddle-tutorial.html"/>
   <updated>2016-10-05T00:00:00+00:00</updated>
   <id>/posts/platform-paddlepaddle-tutorial</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#输出日志log&quot;&gt;输出日志(Log)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;输出日志log&quot;&gt;输出日志(Log)&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;TrainerInternal.cpp:160]  Batch=20 samples=2560 AvgCost=0.628761 CurrentCost=0.628761 Eval: classification_error_evaluator=0.304297  CurrentEval: classification_error_evaluator=0.304297
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;模型训练会看到这样的日志，详细的参数解释如下面表格：&lt;/p&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;名称&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;解释&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;Batch=20&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt; 表示过了20个batch &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;samples=2560&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt; 表示过了2560个样本 &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;AvgCost&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt; 每个pass的第0个batch到当前batch所有样本的平均cost &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;CurrentCost&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt; 当前log_period个batch所有样本的平均cost &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;Eval: classification_error_evaluator&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt; 每个pass的第0个batch到当前batch所有样本的平均分类错误率 &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;CurrentEval: classification_error_evaluator&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt; 当前log_period个batch所有样本的平均分类错误率 &lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Useful Links</title>
   <link href="http://daiwk.github.io/posts/links-useful-links.html"/>
   <updated>2016-10-02T00:00:00+00:00</updated>
   <id>/posts/links-useful-links</id>
   <content type="html">&lt;p&gt;目录：&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#basic-knowledges&quot;&gt;&lt;strong&gt;Basic Knowledges&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#machine-learning&quot;&gt;Machine Learning&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%a0%91%e6%a8%a1%e5%9e%8b&quot;&gt;&lt;strong&gt;树模型&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%9f%ba%e7%a1%80%e7%ae%97%e6%b3%95&quot;&gt;&lt;strong&gt;基础算法&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e7%bb%9f%e8%ae%a1%e5%ad%a6%e4%b9%a0%e6%96%b9%e6%b3%95&quot;&gt;&lt;strong&gt;统计学习方法&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#deep-learning&quot;&gt;Deep Learning&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#cnn&quot;&gt;CNN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#rnn&quot;&gt;RNN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#gan&quot;&gt;GAN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#reinforcement-learning&quot;&gt;Reinforcement Learning&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#pnnprogressive-neural-network%e8%bf%9e%e7%bb%ad%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c&quot;&gt;PNN(Progressive Neural Network)连续神经网络&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%9b%be%e5%8d%b7%e7%a7%af%e7%bd%91%e7%bb%9c&quot;&gt;图卷积网络&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#copynet&quot;&gt;copynet&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#useful-tools&quot;&gt;&lt;strong&gt;Useful Tools&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#datasets&quot;&gt;Datasets&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pretrained-models&quot;&gt;pretrained models&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#deep-learning-tools&quot;&gt;Deep Learning Tools&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#mxnet&quot;&gt;mxnet&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#theano&quot;&gt;theano&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#torch&quot;&gt;torch&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tensorflow&quot;&gt;tensorflow&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#docker&quot;&gt;docker&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#docker-images&quot;&gt;docker-images&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#useful-courses--speeches&quot;&gt;&lt;strong&gt;Useful Courses &amp;amp;&amp;amp; Speeches&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#courses&quot;&gt;Courses&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#speeches&quot;&gt;Speeches&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e7%bb%8f%e5%85%b8%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90&quot;&gt;经典学习资源&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#applications&quot;&gt;&lt;strong&gt;Applications&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#nlp&quot;&gt;NLP&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e5%88%86%e8%af%8d&quot;&gt;分词&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#text-abstraction&quot;&gt;Text Abstraction&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#image-processing&quot;&gt;Image Processing&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#image2txt&quot;&gt;image2txt&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#collections&quot;&gt;Collections&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#csdn%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e4%bb%a3%e7%a0%81%e4%b8%93%e6%a0%8f&quot;&gt;csdn深度学习代码专栏&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#chiristopher-olah%e7%9a%84%e5%8d%9a%e5%ae%a2&quot;&gt;chiristopher olah的博客&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%bf%80%e6%b4%bb%e5%87%bd%e6%95%b0%e7%b3%bb%e5%88%97&quot;&gt;激活函数系列&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%a2%af%e5%ba%a6%e4%b8%8b%e9%99%8d%e7%ae%97%e6%b3%95%e7%b3%bb%e5%88%97&quot;&gt;梯度下降算法系列&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://www.knime.com/&quot;&gt;https://www.knime.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Knime是一款出色的工具，可让你无需编写任何代码即可完成端到端的数据科学工作流程。&lt;/p&gt;

&lt;p&gt;它甚至配备了一个拖放式界面，UI清晰，操作简单直观，可以说是懒人福音了。&lt;/p&gt;

&lt;p&gt;操作起来非常简单，首先使用该工具进行数据收集和转换；完成后，你可以创建一个模型并将其可视化。在生产方面，你可以部署和管理数据科学项目。&lt;/p&gt;

&lt;p&gt;最后，你可以通过使用Knime生成洞察来利用你的实现。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://uber.github.io/ludwig/getting_started/&quot;&gt;https://uber.github.io/ludwig/getting_started/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Uber Ludwig是另一款适合初学者的优秀工具。有了它，你可以快速测试和训练深度学习模型。用户可以选择启用懒人模式（拖拽界面），或者直接操作代码。&lt;/p&gt;

&lt;p&gt;使用起来比Knime稍微复杂一点点。需要先加载CSV文件来训练数据。通过使用预先训练的模型，你可以预测输出目标。最后，你可以使用可用的可视化选项可视化你的数据。&lt;/p&gt;

&lt;p&gt;如果你是编程的初学者，你还可以在Python中使用他们扩展的API和训练模型。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/mlflow/mlflow&quot;&gt;https://github.com/mlflow/mlflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MLFlow让你可以解决端到端的机器学习生命周期问题。它有三个主要组件。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;MLflow跟踪 - 通过记录和比较结果和参数来处理实验&lt;/li&gt;
  &lt;li&gt;MLflow项目 - 允许你将项目打包成其他成员的可重用表单&lt;/li&gt;
  &lt;li&gt;MLflow模型 - 帮助你在不同平台中部署和管理ML库&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MLFlow的另一个惊人功能是它与库无关。这意味着你可以将其与其他机器学习库一起使用而不会出现任何兼容性问题。为了实现library-agonistic行为，它使用REST API和CLI。&lt;/p&gt;

&lt;p&gt;动手学深度学习：&lt;a href=&quot;https://github.com/d2l-ai/d2l-zh&quot;&gt;https://github.com/d2l-ai/d2l-zh&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zh.d2l.ai/&quot;&gt;https://zh.d2l.ai/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;机器学习笔记：&lt;a href=&quot;https://createmomo.github.io/2018/01/23/Super-Machine-Learning-Revision-Notes/#tableofcontents&quot;&gt;https://createmomo.github.io/2018/01/23/Super-Machine-Learning-Revision-Notes/#tableofcontents&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;jaxnet：&lt;a href=&quot;https://mp.weixin.qq.com/s/mS40TE7_KmO6iAiDkrrTFg&quot;&gt;JAXnet：一行代码定义计算图，兼容三大主流框架，可GPU加速&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://paperswithcode.com/sota&quot;&gt;https://paperswithcode.com/sota&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.jianshu.com/u/511ba5d71aef&quot;&gt;https://www.jianshu.com/u/511ba5d71aef&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/pinard/&quot;&gt;https://www.cnblogs.com/pinard/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;牛逼的网站：&lt;/p&gt;

&lt;p&gt;工业级ml应用的templates：&lt;a href=&quot;http://predictionio.apache.org/&quot;&gt;http://predictionio.apache.org/&lt;/a&gt;，对应的代码&lt;a href=&quot;https://github.com/apache/predictionio&quot;&gt;https://github.com/apache/predictionio&lt;/a&gt;，例如推荐系统示例：&lt;a href=&quot;http://predictionio.apache.org/templates/recommendation/quickstart/&quot;&gt;http://predictionio.apache.org/templates/recommendation/quickstart/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;自动上色，不过因为资金不足。。主网站关了。。&lt;a href=&quot;https://github.com/lllyasviel/style2paints&quot;&gt;https://github.com/lllyasviel/style2paints&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/jwasham/coding-interview-university/blob/master/translations/README-cn.md&quot;&gt;https://github.com/jwasham/coding-interview-university/blob/master/translations/README-cn.md&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;basic-knowledges&quot;&gt;&lt;strong&gt;Basic Knowledges&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;machine-learning&quot;&gt;Machine Learning&lt;/h2&gt;

&lt;h3 id=&quot;树模型&quot;&gt;&lt;strong&gt;树模型&lt;/strong&gt;&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Python &amp;amp; R的树模型&lt;/strong&gt;：&lt;a href=&quot;https://www.analyticsvidhya.com/blog/2016/04/complete-tutorial-tree-based-modeling-scratch-in-python&quot;&gt;A Complete Tutorial on Tree Based Modeling from Scratch (in R &amp;amp; Python)&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;基础算法&quot;&gt;&lt;strong&gt;基础算法&lt;/strong&gt;&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;最小二乘的几何意义及投影矩阵&lt;/strong&gt;：&lt;a href=&quot;http://mp.weixin.qq.com/s?__biz=MzA5ODUxOTA5Mg==&amp;amp;mid=2652550323&amp;amp;idx=1&amp;amp;sn=654ccf3d7cb12c68e5e7a2aa85907688&amp;amp;chksm=8b7e45e8bc09ccfe87b2d16a77205ae21a7ffcf2231d95a143cd06980e15997836425a4df284&amp;amp;scene=25#wechat_redirect&quot;&gt;博客地址&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;统计学习方法&quot;&gt;&lt;strong&gt;统计学习方法&lt;/strong&gt;&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;统计学习方法笔记&lt;/strong&gt;：&lt;a href=&quot;http://m.blog.csdn.net/article/details?id=8351337&quot;&gt;csdn博客&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;deep-learning&quot;&gt;Deep Learning&lt;/h2&gt;

&lt;h3 id=&quot;cnn&quot;&gt;CNN&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;人脸合成&lt;/strong&gt;：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2651987619&amp;amp;idx=2&amp;amp;sn=dfafcfe8956f4ca686532271cc1b0326&amp;amp;chksm=f1216a52c656e3446fe34187945a48e61ca49bdca187f5c56aab3d59829540e60645949856a4&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1002uGDVOwvDY4eeWR5mzos9&amp;amp;pass_ticket=lD2bnuoAxxbEWgy8KxGVnWVzLL%2FeiSX9MsE68ZdaZQzVoXKXHlCJQ3sVCfTnR7MQ#rd&quot;&gt;《使用CNN进行人脸合成》&lt;/a&gt;，代码地址：&lt;a href=&quot;https://github.com/zo7/facegen&quot;&gt;https://github.com/zo7/facegen&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Residual Net&lt;/strong&gt;：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzAwMjM3MTc5OA==&amp;amp;mid=402120336&amp;amp;idx=1&amp;amp;sn=4bdc7abbfe47bc342c86129e1a18ff34&amp;amp;scene=18&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1002wbRSWdlbb0r77vY5iWAo&amp;amp;pass_ticket=DoiMlYDlmCK%2FTS99n6JzBzzsHdN7QoyC81j%2BvUNHFkqqmuADrJsZlH0yXSTgpVEB#rd&quot;&gt;《ICCV 2015 揭开152层神经网络的面纱》&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;rnn&quot;&gt;RNN&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;WILDML-RNN&lt;/strong&gt;：&lt;a href=&quot;http://www.wildml.com/2015/09/recurrent-neural-networks-tutorial-part-1-introduction-to-rnns/&quot;&gt;part-1-4&lt;/a&gt;；代码地址：&lt;a href=&quot;https://github.com/dennybritz/rnn-tutorial-gru-lstm&quot;&gt;https://github.com/dennybritz/rnn-tutorial-gru-lstm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;LSTM及其11种变种&lt;/strong&gt;：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650719562&amp;amp;idx=1&amp;amp;sn=ad6693cdeaa18034ed1c53271f642ef7&amp;amp;chksm=871b0134b06c8822bf89781a81081c161eb82b06d0c20b655bd7b991202d363b6c233ef137ff&amp;amp;scene=0&amp;amp;pass_ticket=lD2bnuoAxxbEWgy8KxGVnWVzLL%2FeiSX9MsE68ZdaZQzVoXKXHlCJQ3sVCfTnR7MQ#rd&quot;&gt;《图解LSTM神经网络架构及其11种变体（附论文）》&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;augmented-rnns&lt;/strong&gt;：google大脑的研究员在博客中讲述了Neural Turing Machine、Attentional Interfaces、Adaptive Computation Time和Neural Programmers四大部分。&lt;a href=&quot;http://distill.pub/2016/augmented-rnns/&quot;&gt;英文原文&lt;/a&gt;；&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2651986905&amp;amp;idx=4&amp;amp;sn=dcfdeb7c92826c0603569d5a86025536&amp;amp;chksm=f1216f28c656e63e309d7c92fd06a1c67ac96ebea2a8c6f90169cd944876fb367a8bf819b4f4&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1002ho2GSC2PTnFhFUio3EYj&amp;amp;pass_ticket=DoiMlYDlmCK%2FTS99n6JzBzzsHdN7QoyC81j%2BvUNHFkqqmuADrJsZlH0yXSTgpVEB#rd&quot;&gt;新智元翻译版&lt;/a&gt;；gitbub博客代码：&lt;a href=&quot;https://github.com/distillpub/post--augmented-rnns&quot;&gt;https://github.com/distillpub/post–augmented-rnns&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;漫谈4种RNN-decoder&lt;/strong&gt;：&lt;a href=&quot;http://jacoxu.com/?p=1852&quot;&gt;博客地址&lt;/a&gt;；github地址：&lt;a href=&quot;https://github.com/jacoxu/encoder_decoder&quot;&gt;https://github.com/jacoxu/encoder_decoder&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;小合辑（从lstm到gru到双向多层到nmt到attention）:&lt;a href=&quot;https://daiwk.github.io/posts/nlp-nmt.html&quot;&gt;https://daiwk.github.io/posts/nlp-nmt.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;attention model集合：&lt;a href=&quot;https://daiwk.github.io/posts/dl-attention-models.html&quot;&gt;https://daiwk.github.io/posts/dl-attention-models.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;只要attention的tensor2tensor：&lt;a href=&quot;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&quot;&gt;https://daiwk.github.io/posts/platform-tensor-to-tensor.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;nested lstm: &lt;a href=&quot;https://daiwk.github.io/posts/dl-nested-lstm.html&quot;&gt;https://daiwk.github.io/posts/dl-nested-lstm.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;indRNN: &lt;a href=&quot;https://daiwk.github.io/posts/dl-indrnn.html&quot;&gt;https://daiwk.github.io/posts/dl-indrnn.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;colah的博客：翻译版之一：&lt;a href=&quot;https://www.leiphone.com/news/201701/UIlrDBnwiqoQUbqB.html&quot;&gt;https://www.leiphone.com/news/201701/UIlrDBnwiqoQUbqB.html&lt;/a&gt;，原文：&lt;a href=&quot;http://colah.github.io/posts/2015-08-Understanding-LSTMs/&quot;&gt;http://colah.github.io/posts/2015-08-Understanding-LSTMs/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gan&quot;&gt;GAN&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;GAN简介&lt;/strong&gt;：&lt;a href=&quot;https://adeshpande3.github.io/adeshpande3.github.io/Deep-Learning-Research-Review-Week-1-Generative-Adversarial-Nets&quot;&gt;《Deep Learning Research Review Week 1: Generative Adversarial Nets》&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;生成式对抗网络GAN研究进展系列笔记&lt;/strong&gt;：&lt;a href=&quot;http://blog.csdn.net/solomon1558/article/details/52537114&quot;&gt;http://blog.csdn.net/solomon1558/article/details/52537114&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;cleverhans&lt;/strong&gt;：Ian Goodfellow等人在openai中开源了cleverhans，基于tf+keras+GPU：&lt;a href=&quot;https://github.com/openai/cleverhans&quot;&gt;https://github.com/openai/cleverhans&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GAN-zoo&lt;/strong&gt;：&lt;a href=&quot;https://github.com/hindupuravinash/the-gan-zoo&quot;&gt;https://github.com/hindupuravinash/the-gan-zoo&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;reinforcement-learning&quot;&gt;Reinforcement Learning&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;WILDML-Deep Reinforcement Learning&lt;/strong&gt;：&lt;a href=&quot;http://www.wildml.com/2016/10/learning-reinforcement-learning/&quot;&gt;http://www.wildml.com/2016/10/learning-reinforcement-learning/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;强化学习概览&lt;/strong&gt;：NVIDIA 博客上 Tim Dettmers 所写的《Deep Learning in a Nutshell》系列文章的第四篇：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650719294&amp;amp;idx=1&amp;amp;sn=f1a01cd6710e6ea9629619cd3324d102&amp;amp;chksm=871b0040b06c895642ff961a6fe81f05c5e9776aff5da4845f2d3d874f88213863afd2059833&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1002mDtDsEDixxCswQJOs2rH&amp;amp;pass_ticket=DoiMlYDlmCK%2FTS99n6JzBzzsHdN7QoyC81j%2BvUNHFkqqmuADrJsZlH0yXSTgpVEB#rd&quot;&gt;强化学习概览&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;pnnprogressive-neural-network连续神经网络&quot;&gt;PNN(Progressive Neural Network)连续神经网络&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;PNN简介&lt;/strong&gt;：“我们想要从一个任务开始，在上面获得专家级别的表现，随后，我们迁移到另一个连续性的任务上，使用相同的神经网络来获得专家级别的表现，在这个过程中，神经网络不会忘掉此前学会的技巧，并可以在不同的任务间实现这些技巧的相互迁移。如果任务类似的话，我希望任务1中的技巧可以有效地迁移到任务4中。我想要实现的是，只要根据任务1进行训练，就能知道其中的技巧能否写入我的神经网络代码中，并可以迁移到下一个任务。”把单独的神经网络称为一个栏（Column），这些栏在神经网络的每一层旁边形成互连，并且， 我也会固定权重（模型的参数），这样我训练第二个栏的时候，我就知道如何使用栏1的特征，但是我不需要重新编写它们。&lt;a href=&quot;https://arxiv.org/pdf/1606.04671.pdf&quot;&gt;论文下载&lt;/a&gt;
&lt;a href=&quot;http://www.cnblogs.com/wangxiaocvpr/p/6002214.html&quot;&gt;论文笔记&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;图卷积网络&quot;&gt;图卷积网络&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Graph Convolutional Networks&lt;/strong&gt;：&lt;a href=&quot;http://tkipf.github.io/graph-convolutional-networks/&quot;&gt;http://tkipf.github.io/graph-convolutional-networks/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;copynet&quot;&gt;copynet&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;copynet&lt;/strong&gt;:&lt;a href=&quot;../assets/Incorporating Copying Mechanism in Sequence-to-Sequence Learning.pdf&quot;&gt;Incorporating Copying Mechanism in Sequence-to-Sequence Learning.pdf&lt;/a&gt; &lt;a href=&quot;https://github.com/MultiPath/CopyNet&quot;&gt;github(基于theano，作者开源的)&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;useful-tools&quot;&gt;&lt;strong&gt;Useful Tools&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;datasets&quot;&gt;Datasets&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Youtube-8m&lt;/strong&gt;：该数据集包含了 800 万个 YouTube 视频 URL（代表着 500,000 小时的视频）以及它们的视频层面的标签（video-level labels），这些标签来自一个多样化的包含 4800 个知识图谱实体（Knowledge Graph entity）的集合。相比于之前已有的视频数据集，这个数据集的规模和多样性都实现了显著的增长。比如说，我们所知的之前最大的视频数据集 Sports-1M 包含了大约 100 万段 YouTube 视频和 500 个体育领域的分类——YouTube-8M 在视频数量和分类数量上都差不多比它高一个数量级。论文：&lt;strong&gt;《YouTube-8M: A Large-Scale Video Classification Benchmark》&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Open Images(图片数据集，包含9百万标注图片)&lt;/strong&gt;:一个包含了900万图像URL的数据集，值得一提的是，这些图像全部都是标签数据，标签种类超过6000种。我们尽量让数据集变得实用：数据集中所使用的标签类型比拥有1000个分类的ImageNet数据集更加贴近实际生活。 &lt;a href=&quot;https://github.com/openimages/dataset&quot;&gt;https://github.com/openimages/dataset&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;pretrained-models&quot;&gt;pretrained models&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;大规模语言建模模型库(基于One Billion Word Benchmark)&lt;/strong&gt;：这个数据库含有大约 10 亿个单词，词汇有 80 万单词，大部分都是新闻数据。由于训练中句子是被打乱了的，模型可以不理会文本，集中句子层面的语言建模。在此基础上，作者在论文描述了一个模型，混合了字符CNN（character CNN）、大规模深度 LSTM，以及一个专门的 Softmanx 架构，最终得到的结果可以说是迄今最好的。&lt;a href=&quot;https://github.com/tensorflow/models/tree/master/lm_1b&quot;&gt;github&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;deep-learning-tools&quot;&gt;Deep Learning Tools&lt;/h2&gt;

&lt;h3 id=&quot;mxnet&quot;&gt;mxnet&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;NNVM和tinyflow&lt;/strong&gt;：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650719529&amp;amp;idx=3&amp;amp;sn=6992a6067c79349583762cb28eecda89&amp;amp;chksm=871b0157b06c8841587bdfb992c19290c8d66386a6f8accdf70998ce3f86b36330219c09672d&amp;amp;scene=0&amp;amp;pass_ticket=lD2bnuoAxxbEWgy8KxGVnWVzLL%2FeiSX9MsE68ZdaZQzVoXKXHlCJQ3sVCfTnR7MQ#rd&quot;&gt;《NNVM打造模块化深度学习系统》&lt;/a&gt;前端把计算表达成一个中间形式，通常我们称之为计算图，NNVM 则统一的对图做必要的操作和优化，然后再生成后端硬件代码。NNVM 是一个神经网络的比较高级的中间表示模块，它包含了图的表示以及执行无关的各种优化（例如内存分配，数据类型和形状的推导）。核心的是这两个github地址：&lt;a href=&quot;https://github.com/dmlc/nnvm&quot;&gt;https://github.com/dmlc/nnvm&lt;/a&gt;和&lt;a href=&quot;https://github.com/tqchen/tinyflow&quot;&gt;https://github.com/tqchen/tinyflow&lt;/a&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;tf-slim&lt;/strong&gt;：　今年早些时候，我们发布了图像分类模型 Inception V3 在 TensorFlow 上的运行案例。代码能够让用户使用同步梯度下降用 ImageNet 分类数据库训练模型。Inception V3 模型的基础是一个叫做 TF-Slim 的 TensorFlow 库，用户可以使用这个软件包定义、训练、评估 TensorFlow 模型。TF-Slim 库提供的常用抽象能使用户快速准确地定义模型，同时确保模型架构透明，超参数明确。有更多新的层（&lt;strong&gt;比如 Atrous 卷积层和 Deconvolution&lt;/strong&gt;）、更多新的代价函数和评估指标（&lt;strong&gt;如 mAP，IoU&lt;/strong&gt;），同时有很多pre-trained的模型（比如 &lt;strong&gt;Inception、VGG、AlexNet、ResNet&lt;/strong&gt;）。	&lt;a href=&quot;https://github.com/tensorflow/models/tree/master/slim&quot;&gt;https://github.com/tensorflow/models/tree/master/slim&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;theano&quot;&gt;theano&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;bay area dl school’s tutorial&lt;/strong&gt;：&lt;a href=&quot;https://github.com/daiwk/bayareadlschool-learning-theano&quot;&gt;https://github.com/daiwk/bayareadlschool-learning-theano&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;torch&quot;&gt;torch&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;bay area dl school’s tutorial&lt;/strong&gt;：&lt;a href=&quot;https://github.com/daiwk/bayareadlschool-learning-torch&quot;&gt;https://github.com/daiwk/bayareadlschool-learning-torch&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;tensorflow&quot;&gt;tensorflow&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;bay area dl school’s tutorial&lt;/strong&gt;：&lt;a href=&quot;https://github.com/daiwk/bayareadlschool-learning-tensorflow&quot;&gt;https://github.com/daiwk/bayareadlschool-learning-tensorflow&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;a tour of tensorflow&lt;/strong&gt;：&lt;a href=&quot;https://arxiv.org/pdf/1610.01178v1.pdf&quot;&gt;a tour of tensorflow&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;docker&quot;&gt;docker&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;nvidia-docker&lt;/strong&gt;：&lt;a href=&quot;https://github.com/NVIDIA/nvidia-docker&quot;&gt;https://github.com/NVIDIA/nvidia-docker&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;docker-images&quot;&gt;docker-images&lt;/h3&gt;

&lt;p&gt;链接:&lt;strong&gt;&lt;a href=&quot;http://pan.baidu.com/s/1kUU9znh&quot;&gt;http://pan.baidu.com/s/1kUU9znh&lt;/a&gt; 密码:yyfp&lt;/strong&gt;&lt;/p&gt;

&lt;h1 id=&quot;useful-courses--speeches&quot;&gt;&lt;strong&gt;Useful Courses &amp;amp;&amp;amp; Speeches&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;courses&quot;&gt;Courses&lt;/h2&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;cs224d(nlp)&lt;/strong&gt;：课程链接：&lt;a href=&quot;http://cs224d.stanford.edu/syllabus.html&quot;&gt;http://cs224d.stanford.edu/syllabus.html&lt;/a&gt;；百度云课程pdf下载：&lt;a href=&quot;http://pan.baidu.com/s/1dFaA7PR&quot;&gt;http://pan.baidu.com/s/1dFaA7PR&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;cs231n(cnn)&lt;/strong&gt;：课程链接：&lt;a href=&quot;http://cs231n.stanford.edu/&quot;&gt;http://cs231n.stanford.edu/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Bay Area Deep Learning School 2016&lt;/strong&gt;：
课程安排（附课件下载链接）：
&lt;a href=&quot;http://www.bayareadlschool.org/schedule&quot;&gt;http://www.bayareadlschool.org/schedule&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;演讲视频：&lt;/p&gt;

&lt;p&gt;day1: &lt;a href=&quot;https://www.youtube.com/watch?v=eyovmAtoUx0&quot;&gt;youtube&lt;/a&gt;; &lt;a href=&quot;http://v.youku.com/v_show/id_XMTczNzYxNjg5Ng==.html&quot;&gt;优酷&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;day2：&lt;a href=&quot;https://www.youtube.com/watch?v=eyovmAtoUx0&quot;&gt;youtube&lt;/a&gt;；&lt;a href=&quot;http://v.youku.com/v_show/id_XMTczODc2ODE3Mg==.html&quot;&gt;优酷&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;daiwk整理: &lt;a href=&quot;http://pan.baidu.com/s/1mim7A2G&quot;&gt;deep reinforcement learning&lt;/a&gt;；&lt;a href=&quot;http://pan.baidu.com/s/1o8tVcue&quot;&gt;deep unsupervised learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;小结：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650719442&amp;amp;idx=1&amp;amp;sn=ff9f8412f08dbb8e52cb1fdb748e5a4e&amp;amp;chksm=871b00acb06c89ba46582fe5c481a5bc93a69cca5e1eb9f0d86d03db99cb9db3b1c8fdaabde4&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=092660FGFS96T6aIXG9i1pI0&amp;amp;pass_ticket=DoiMlYDlmCK%2FTS99n6JzBzzsHdN7QoyC81j%2BvUNHFkqqmuADrJsZlH0yXSTgpVEB#rd&quot;&gt;Yoshua Bengio压轴解读深度学习的基础和挑战&lt;/a&gt;；&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2651987425&amp;amp;idx=1&amp;amp;sn=e5d4bb50352bf536d786bacb4cb16258&amp;amp;chksm=f1216910c656e006cdc2de9aa4cdf19b82f4dd79bb1ede4710f8876056381655e339a57b0bea&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1002SLviFNa9PO6AXaJghl00&amp;amp;pass_ticket=DoiMlYDlmCK%2FTS99n6JzBzzsHdN7QoyC81j%2BvUNHFkqqmuADrJsZlH0yXSTgpVEB#rd&quot;&gt;Andrej Karpathy 最新演讲计算机视觉深度学习技术与趋势&lt;/a&gt;；&lt;/p&gt;

&lt;h2 id=&quot;speeches&quot;&gt;Speeches&lt;/h2&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;google brain最近的7大研究项目&lt;/strong&gt;：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI3MTA0MTk1MA==&amp;amp;mid=2651987619&amp;amp;idx=1&amp;amp;sn=3f24b3384a9b10fce2001f4074b789ee&amp;amp;chksm=f1216a52c656e344b1f449f384fe0f41461c9e72992946a1b79c2d1d996eb4bef73e27e04f6c&amp;amp;scene=0&amp;amp;pass_ticket=lD2bnuoAxxbEWgy8KxGVnWVzLL%2FeiSX9MsE68ZdaZQzVoXKXHlCJQ3sVCfTnR7MQ#rd&quot;&gt;《谷歌大脑最近7大研究项目》&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;经典学习资源&quot;&gt;经典学习资源&lt;/h2&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Michael Nielsen的neuralnetworksanddeeplearning&lt;/strong&gt;：&lt;a href=&quot;http://neuralnetworksanddeeplearning.com&quot;&gt;http://neuralnetworksanddeeplearning.com&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;applications&quot;&gt;&lt;strong&gt;Applications&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;nlp&quot;&gt;NLP&lt;/h2&gt;

&lt;h3 id=&quot;分词&quot;&gt;分词&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;THULAC.so&lt;/strong&gt;：THULAC（THU Lexical Analyzer for Chinese）由清华大学自然语言处理与社会人文计算实验室研制推出的一套中文词法分析工具包，具有中文分词和词性标注功能。&lt;a href=&quot;https://github.com/thunlp/THULAC.so&quot;&gt;https://github.com/thunlp/THULAC.so&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;text-abstraction&quot;&gt;Text Abstraction&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Abstractive Text Summarization using Seq-to-Seq RNNs and Beyond&lt;/strong&gt;：来自IBM Watson，本文是一篇非常优秀的paper，在seq2seq+attention的基础上融合了很多的features、trick进来，提出了多组对比的模型，并且在多种不同类型的数据集上做了评测，都证明了本文模型更加出色。&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247483777&amp;amp;idx=1&amp;amp;sn=d766a3dec1761bab4186cf89ce8a8723&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0919dvXyKB1zjz6S543FH1Ib&amp;amp;pass_ticket=DoiMlYDlmCK%2FTS99n6JzBzzsHdN7QoyC81j%2BvUNHFkqqmuADrJsZlH0yXSTgpVEB#rd&quot;&gt;张俊的分析&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;image-processing&quot;&gt;Image Processing&lt;/h2&gt;

&lt;h3 id=&quot;image2txt&quot;&gt;image2txt&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;img2txt&lt;/strong&gt;：
&lt;a href=&quot;https://research.googleblog.com/2016/09/show-and-tell-image-captioning-open.html&quot;&gt;google博文链接&lt;/a&gt;；&lt;a href=&quot;http://arxiv.org/abs/1609.06647&quot;&gt;论文链接&lt;/a&gt;；&lt;a href=&quot;https://github.com/tensorflow/models/tree/master/im2txt&quot;&gt;代码链接（tensorflow/img2txt）&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;karpathy&lt;/strong&gt;:&lt;a href=&quot;https://github.com/karpathy/neuraltalk&quot;&gt;https://github.com/karpathy/neuraltalk&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/karpathy/neuraltalk2&quot;&gt;https://github.com/karpathy/neuraltalk2&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;pretrained_resnet&lt;/strong&gt;：paddle的model_zoo中:&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/tree/develop/v1_api_demo/model_zoo/resnet&quot;&gt;https://github.com/PaddlePaddle/Paddle/tree/develop/v1_api_demo/model_zoo/resnet&lt;/a&gt;，用法:&lt;a href=&quot;http://doc.paddlepaddle.org/doc_cn/tutorials/imagenet_model/resnet_model_cn.html?highlight=zoo&quot;&gt;http://doc.paddlepaddle.org/doc_cn/tutorials/imagenet_model/resnet_model_cn.html?highlight=zoo&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;collections&quot;&gt;Collections&lt;/h2&gt;

&lt;h3 id=&quot;csdn深度学习代码专栏&quot;&gt;csdn深度学习代码专栏&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://code.csdn.net/blog/41&quot;&gt;https://code.csdn.net/blog/41&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;chiristopher-olah的博客&quot;&gt;chiristopher olah的博客&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;http://colah.github.io/&quot;&gt;http://colah.github.io/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;激活函数系列&quot;&gt;激活函数系列&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzI1NTE4NTUwOQ==&amp;amp;mid=2650325236&amp;amp;idx=1&amp;amp;sn=7bd8510d59ddc14e5d4036f2acaeaf8d&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=1214qIBJrRhevScKXQQuqas4&amp;amp;pass_ticket=w2yCF%2F3Z2KTqyWW%2FUwkvnidRV3HF9ym5iEfJ%2BZ1dMObpcYUW3hQymA4BpY9W3gn4#rd&quot;&gt;激活函数&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;梯度下降算法系列&quot;&gt;梯度下降算法系列&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA5ODUxOTA5Mg==&amp;amp;mid=2652550294&amp;amp;idx=1&amp;amp;sn=820ddc89e1d1af35f14ccf645b963a76&amp;amp;chksm=8b7e45cdbc09ccdb985b3bbc22fbc0dcd013d53e9e9a6073d09d1b676b338af7bd8b7dd2a92d&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0930LxixeTcq5wCcRStBTylE&amp;amp;pass_ticket=w2yCF%2F3Z2KTqyWW%2FUwkvnidRV3HF9ym5iEfJ%2BZ1dMObpcYUW3hQymA4BpY9W3gn4#rd&quot;&gt;梯度下降优化算法&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>word2vec</title>
   <link href="http://daiwk.github.io/posts/nlp-word2vec.html"/>
   <updated>2016-10-01T00:00:00+00:00</updated>
   <id>/posts/nlp-word2vec</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-%E7%BB%9F%E8%AE%A1%E8%AF%AD%E8%A8%80%E6%A8%A1%E5%9E%8B&quot;&gt;1. 统计语言模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#n-gram%E6%A8%A1%E5%9E%8B&quot;&gt;N-gram模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E8%AF%AD%E8%A8%80%E6%A8%A1%E5%9E%8Bnnlm&quot;&gt;神经网络语言模型（NNLM）&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-cbowcontinuous-bag-of-words&quot;&gt;2. CBOW(Continuous Bag-of-Words)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-continuous-skip-gram&quot;&gt;3. Continuous skip-gram&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-hs--nce&quot;&gt;4. HS &amp;amp; NCE&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-hierachical-softmax&quot;&gt;4.1 hierachical softmax&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#hierarchical-softmax%E6%A2%AF%E5%BA%A6%E8%AE%A1%E7%AE%97&quot;&gt;Hierarchical Softmax梯度计算&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8Ehierarchical-softmax%E7%9A%84cbow&quot;&gt;基于Hierarchical Softmax的CBOW&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8Ehierarchical-softmax%E7%9A%84skip-gram&quot;&gt;基于Hierarchical Softmax的Skip-Gram&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#hierarchical-softmax%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90&quot;&gt;hierarchical softmax源码解析&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-%E8%B4%9F%E9%87%87%E6%A0%B7%E5%92%8Cnce&quot;&gt;4.2 负采样和nce&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#negative-sampling%E6%A2%AF%E5%BA%A6%E8%AE%A1%E7%AE%97&quot;&gt;Negative Sampling梯度计算&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#negative-sampling%E8%B4%9F%E9%87%87%E6%A0%B7%E6%96%B9%E6%B3%95&quot;&gt;Negative Sampling负采样方法&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8Enegative-sampling%E7%9A%84cbow%E6%A8%A1%E5%9E%8B&quot;&gt;基于Negative Sampling的CBOW模型&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E4%BA%8Enegative-sampling%E7%9A%84skip-gram%E6%A8%A1%E5%9E%8B&quot;&gt;基于Negative Sampling的Skip-Gram模型&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#negative-sampling%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90&quot;&gt;negative sampling源码解析&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-%E9%9D%A2%E8%AF%95%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98&quot;&gt;5. 面试常见问题&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#x-tensorflow%E7%9A%84%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0&quot;&gt;x. tensorflow的简单实现&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E7%AE%80%E4%BB%8B&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E4%BB%A3%E7%A0%81%E8%A7%A3%E8%AF%BB&quot;&gt;代码解读&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E8%AF%BB%E6%95%B0%E6%8D%AE&quot;&gt;读数据&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%BB%BA%E7%AB%8B%E6%95%B0%E6%8D%AE%E9%9B%86&quot;&gt;建立数据集&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E7%94%9F%E6%88%90skipgram%E7%9A%84%E4%B8%80%E4%B8%AAbatch&quot;&gt;生成skipgram的一个batch&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9E%8B&quot;&gt;定义模型&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#placeholder%E7%AD%89&quot;&gt;placeholder等&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E7%BD%91%E7%BB%9C%E5%8F%82%E6%95%B0&quot;&gt;网络参数&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#loss&quot;&gt;loss&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E8%AE%A1%E7%AE%97cos&quot;&gt;计算cos&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#%E5%85%B6%E4%BB%96&quot;&gt;其他&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E8%AE%AD%E7%BB%83&quot;&gt;训练&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E5%8F%AF%E8%A7%86%E5%8C%96&quot;&gt;可视化&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#y1-tensorflow%E7%9A%84%E9%AB%98%E7%BA%A7%E5%AE%9E%E7%8E%B01&quot;&gt;y1. tensorflow的高级实现1&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#y2-tensorflow%E7%9A%84%E9%AB%98%E7%BA%A7%E5%AE%9E%E7%8E%B02&quot;&gt;y2. tensorflow的高级实现2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E8%87%AA%E5%B7%B1%E7%9A%84%E6%80%BB%E7%BB%93&quot;&gt;自己的总结&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考cdsn博客：&lt;a href=&quot;http://blog.csdn.net/zhaoxinfan/article/details/27352659&quot;&gt;http://blog.csdn.net/zhaoxinfan/article/details/27352659&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考paddlepaddle book: &lt;a href=&quot;https://github.com/PaddlePaddle/book/blob/develop/04.word2vec/README.cn.md&quot;&gt;https://github.com/PaddlePaddle/book/blob/develop/04.word2vec/README.cn.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考fasttext及更多cbow/skip-gram：&lt;a href=&quot;https://daiwk.github.io/posts/nlp-fasttext.html&quot;&gt;https://daiwk.github.io/posts/nlp-fasttext.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Word2vec的原理主要涉及到&lt;strong&gt;统计语言模型&lt;/strong&gt;（包括N-gram模型和神经网络语言模型(nnlm)），&lt;strong&gt;continuous bag-of-words&lt;/strong&gt;模型以及&lt;strong&gt;continuous skip-gram&lt;/strong&gt;模型。&lt;/p&gt;

&lt;p&gt;语言模型旨在为语句的联合概率函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(w_1,...,w_T)\)&lt;/code&gt;建模。语言模型的目标是，希望模型对有意义的句子赋予大概率，对没意义的句子赋予小概率。 常用条件概率表示语言模型：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(w_1, ..., w_T) = \prod_{t=1}^TP(w_t | w_1, ... , w_{t-1})
\]&lt;/code&gt;&lt;/p&gt;

&lt;h1 id=&quot;1-统计语言模型&quot;&gt;1. 统计语言模型&lt;/h1&gt;
&lt;h2 id=&quot;n-gram模型&quot;&gt;N-gram模型&lt;/h2&gt;

&lt;p&gt;n-gram是一种重要的文本表示方法，表示一个文本中连续的n个项。基于具体的应用场景，每一项可以是一个字母、单词或者音节。一般用每个n-gram的历史n-1个词语组成的内容来预测第n个词。&lt;/p&gt;

&lt;h2 id=&quot;神经网络语言模型nnlm&quot;&gt;神经网络语言模型（NNLM）&lt;/h2&gt;

&lt;p&gt;Yoshua Bengio等科学家就于2003年在著名论文 Neural Probabilistic Language Models中介绍如何学习一个神经元网络表示的词向量模型。神经概率语言模型（Neural Network Language Model，NNLM）通过&lt;strong&gt;一个线性映射和一个非线性隐层连接，&lt;/strong&gt;同时学习了语言模型和词向量，即通过学习大量语料得到词语的向量表达，通过这些向量得到整个句子的概率。用这种方法学习语言模型可以&lt;strong&gt;克服维度灾难（curse of dimensionality）,即训练和测试数据不同导致的模型不准。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;实际上越远的词语其实对该词的影响越小，那么如果考虑一个n-gram, 每个词都只受其前面n-1个词的影响，则有：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(w_1, ..., w_T) = \prod_{t=n}^TP(w_t|w_{t-1}, w_{t-2}, ..., w_{t-n+1})
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;给定一些真实语料，这些语料中都是有意义的句子，N-gram模型的优化目标则是最大化目标函数:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{1}{T}\sum_t f(w_t, w_{t-1}, ..., w_{t-n+1};\theta) + R(\theta)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f(w_t, w_{t-1}, ..., w_{t-n+1})\)&lt;/code&gt;表示根据历史n-1个词得到当前词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;的条件概率，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R(\theta)\)&lt;/code&gt;表示参数正则项。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/word2vec-nnlm.png&quot; style=&quot;max-width: 400px&quot; /&gt;
&lt;img src=&quot;../assets/word2vec-nnlm-noted.jpg&quot; style=&quot;max-width: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;对于每个样本，模型输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{t-n+1},...w_{t-1}\)&lt;/code&gt;，输出句子第t个词为字典中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(|V|\)&lt;/code&gt;个词的概率。每个输入词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_{t-n+1},...w_{t-1}\)&lt;/code&gt;通过矩阵&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;映射到词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C(w_{t-n+1}),...C(w_{t-1})\)&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;然后所有词语的词向量连接成一个大向量，并经过一个非线性映射得到历史词语的隐层表示：&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
g=Utanh(\theta^Tx + b_1) + Wx + b_2
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，x为所有词语的词向量连接成的大向量，表示文本历史特征；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g\)&lt;/code&gt;表示未经归一化的所有输出单词概率，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g_i\)&lt;/code&gt;表示未经归一化的字典中第i个单词的输出概率。其他参数为词向量层到隐层连接的参数。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;根据softmax的定义，通过归一化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g_i\)&lt;/code&gt;, 生成目标词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;的概率为：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(w_t | w_1, ..., w_{t-n+1}) = \frac{e^{g_{w_t}}}{\sum_i^{|V|} e^{g_i}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;整个网络的损失值(cost)为多类分类交叉熵，用公式表示为：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
J(\theta) = -\sum_{i=1}^N\sum_{c=1}^{|V|}y_k^{i}log(softmax(g_k^i))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_k^i\)&lt;/code&gt;表示第i个样本第k类的真实标签(0或1)，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(softmax(g_k^i)\)&lt;/code&gt;表示第i个样本第k类softmax输出的概率。&lt;/p&gt;

&lt;h1 id=&quot;2-cbowcontinuous-bag-of-words&quot;&gt;2. CBOW(Continuous Bag-of-Words)&lt;/h1&gt;

&lt;p&gt;CBOW模型通过一个词的上下文（各N个词）预测当前词。当N=2时，模型如下图所示：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/word2vec-cbow.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;具体来说，不考虑上下文的词语输入顺序，CBOW是用上下文词语的词向量的均值来预测当前词。即：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
context = \frac{x_{t-1} + x_{t-2} + x_{t+1} + x_{t+2}}{4}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_t\)&lt;/code&gt;为第t个词的词向量，分类分数（score）向量 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z=U*context\)&lt;/code&gt;，最终的分类y采用softmax，损失函数采用多类分类交叉熵。&lt;/p&gt;

&lt;p&gt;CBOW经常结合hierarchical softmax一起实现，详见&lt;a href=&quot;https://daiwk.github.io/posts/nlp-fasttext.html#02-%E5%88%86%E5%B1%82softmax&quot;&gt;https://daiwk.github.io/posts/nlp-fasttext.html#02-%E5%88%86%E5%B1%82softmax&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;3-continuous-skip-gram&quot;&gt;3. Continuous skip-gram&lt;/h1&gt;

&lt;p&gt;CBOW的好处是对上下文词语的分布在词向量上进行了平滑，去掉了噪声，因此在小数据集上很有效。而Skip-gram的方法中，&lt;strong&gt;用一个词预测其上下文，得到了当前词上下文的很多样本，因此可用于更大的数据集。&lt;/strong&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/word2vec-skipgram.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;如上图所示，Skip-gram模型的具体做法是，将一个词的词向量映射到2n个词的词向量（2n表示当前输入词的前后各n个词），然后分别通过softmax得到这2n个词的分类损失值之和。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u014595019/article/details/54093161&quot;&gt;https://blog.csdn.net/u014595019/article/details/54093161&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;『我当时使用的是Hierarchical Softmax+CBOW的模型。给我的感觉是比较累，既要费力去写huffman树，还要自己写计算梯度的代码，完了按层softmax速度还慢。这次我决定用tensorflow来写，除了极大的精简了代码以外，可以使用gpu对运算进行加速。此外，这次使用了&lt;strong&gt;负采样(negative sampling)+skip-gram&lt;/strong&gt;模型，从而&lt;strong&gt;避免了使用Huffman树导致训练速度变慢&lt;/strong&gt;的情况，&lt;strong&gt;适合大规模的文本&lt;/strong&gt;。』&lt;/p&gt;

&lt;p&gt;而且，在tf中的实现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/tensorflow/examples/tutorials/word2vec/word2vec_basic.py&lt;/code&gt;，也是基于skip-gram+nce_loss的。&lt;/p&gt;

&lt;h1 id=&quot;4-hs--nce&quot;&gt;4. HS &amp;amp; NCE&lt;/h1&gt;

&lt;p&gt;word2vec的主要改进有两点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对于从输入层到隐藏层的映射，没有采取神经网络的线性变换加激活函数，而是直接&lt;strong&gt;简单地对所有输入词向量求和并取平均&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;从隐藏层到输出的&lt;strong&gt;softmax&lt;/strong&gt;层这里的计算量进行改进。而这个改进主要有hierarchical softmax和nce两种方式&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;41-hierachical-softmax&quot;&gt;4.1 hierachical softmax&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.cnblogs.com/pinard/p/7243513.html&quot;&gt;http://www.cnblogs.com/pinard/p/7243513.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;所谓哈夫曼树，就是一个二叉树，每个叶子节点和中间节点都有权重（不是边有权重）。比如有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;个词，那就是有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;个叶子节点，每个节点的权重一般是词频。把这&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;个节点从大到小排序，第一次迭代是找到&lt;strong&gt;最小的两个叶子节点&lt;/strong&gt;，生成他们的一个新的根节点（权重为两个叶子权重之和），完成一次迭代。第二次迭代就是这个根和其他叶子里，&lt;strong&gt;找出最小的两个&lt;/strong&gt;，再生成一个根，如此继续。迭代的过程中要&lt;strong&gt;保证权重大的在左边，权重小的在右边。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;在w2v中，我们规定向&lt;strong&gt;左&lt;/strong&gt;走，就是&lt;strong&gt;负类&lt;/strong&gt;（哈夫曼&lt;strong&gt;编码1&lt;/strong&gt;）&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(-)\)&lt;/code&gt;，向&lt;strong&gt;右&lt;/strong&gt;走就是&lt;strong&gt;正类&lt;/strong&gt;（哈夫曼&lt;strong&gt;编码0&lt;/strong&gt;）&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(+)\)&lt;/code&gt;。判别正负类根据sigmoid：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
P(+) &amp;amp;= \sigma(x_w^T\theta) = \frac{1}{1+e^{-x_w^T\theta}} \\
P(-) &amp;amp;=  1-P(+) \\
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;是当前内部节点的词向量，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;是当前节点的模型参数。&lt;/p&gt;

&lt;p&gt;在某一个内部节点，要判断是沿左子树还是右子树走的标准就是&lt;strong&gt;看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(−)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(+)\)&lt;/code&gt;谁的概率值大&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;我们的目标就是找到合适的所有节点的&lt;strong&gt;词向量&lt;/strong&gt;和所有&lt;strong&gt;内部节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;&lt;/strong&gt;, 使训练样本达到最大似然。&lt;/p&gt;

&lt;p&gt;哈夫曼树主要有两个好处：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;计算量由&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;变为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(log_2V\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;高频的词靠近树根&lt;/strong&gt;，这样高频词需要&lt;strong&gt;更少的时间会被找到&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;hierarchical-softmax梯度计算&quot;&gt;Hierarchical Softmax梯度计算&lt;/h3&gt;

&lt;p&gt;假设我们要的一个叶子是通过『左、左、右』三步得到的，那么我们期望最大化如下的似然函数：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\prod_{i=1}^3P(n(w_i),i) = (1- \frac{1}{1+e^{-x_w^T\theta_1}})(1- \frac{1}{1+e^{-x_w^T\theta_2}})\frac{1}{1+e^{-x_w^T\theta_3}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;假设&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;：输入的词&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;：输入的词从输入层词向量&lt;strong&gt;求和平均后&lt;/strong&gt;的霍夫曼树根节点词向量&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(l_w\)&lt;/code&gt;：从根节点到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;所在的叶子节点的&lt;strong&gt;路径上包含的节点总数&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p^w_i\)&lt;/code&gt;：从根节点开始，经过的第i个节点，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i = 2,3,...,l_w\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(d^w_i\in {0,1}\)&lt;/code&gt;：从根节点开始，经过的第i个节点的哈夫曼编码，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i = 2,3,...,l_w\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta ^w_i\)&lt;/code&gt;：从根节点开始，经过的第i个&lt;strong&gt;中间节点&lt;/strong&gt;的模型参数，因为是中间节点，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i = 1,2,...,l_w-1\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在走到目标词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;的路上经过的某个节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j\)&lt;/code&gt;的概率：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(d_j^w|x_w, \theta_{j-1}^w)= \begin{cases}  \sigma(x_w^T\theta_{j-1}^w)&amp;amp; {d_j^w=0}\\ 1-  \sigma(x_w^T\theta_{j-1}^w) &amp;amp; {d_j^w = 1} \end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以对于某个目标输出的叶子节点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;，最大似然是整条路径的概率连乘：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\prod_{j=2}^{l_w}P(d_j^w|x_w, \theta_{j-1}^w) = \prod_{j=2}^{l_w} [\sigma(x_w^T\theta_{j-1}^w)] ^{1-d_j^w}[1-\sigma(x_w^T\theta_{j-1}^w)]^{d_j^w}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以其对数似然就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L= log \prod_{j=2}^{l_w}P(d_j^w|x_w, \theta_{j-1}^w) = \sum\limits_{j=2}^{l_w} ((1-d_j^w) log [\sigma(x_w^T\theta_{j-1}^w)]  + d_j^w log[1-\sigma(x_w^T\theta_{j-1}^w)])
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;求参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{j-1}^w\)&lt;/code&gt;的梯度：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
\frac{\partial L}{\partial \theta_{j-1}^w} &amp;amp; = (1-d_j^w)\frac{\sigma(x_w^T\theta_{j-1}^w)(1-\sigma(x_w^T\theta_{j-1}^w))}{\sigma(x_w^T\theta_{j-1}^w)}x_w - d_j^w \frac{\sigma(x_w^T\theta_{j-1}^w)(1-\sigma(x_w^T\theta_{j-1}^w))}{1- \sigma(x_w^T\theta_{j-1}^w)}x_w  \\
&amp;amp; =  (1-d_j^w)(1-\sigma(x_w^T\theta_{j-1}^w))x_w - d_j^w\sigma(x_w^T\theta_{j-1}^w)x_w \\
&amp;amp; = (1-d_j^w-\sigma(x_w^T\theta_{j-1}^w))x_w
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;同样地，对参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;的梯度是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial L}{\partial x_w} = \sum\limits_{j=2}^{l_w}(1-d_j^w-\sigma(x_w^T\theta_{j-1}^w))\theta_{j-1}^w
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;可以发现，都有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((1-d_j^w-\sigma(x_w^T\theta_{j-1}^w))\)&lt;/code&gt;这一项，只是对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{j-1}^w\)&lt;/code&gt;，要乘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;，而对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;，要乘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{j-1}^w\)&lt;/code&gt;，并求和。&lt;/p&gt;

&lt;h3 id=&quot;基于hierarchical-softmax的cbow&quot;&gt;基于Hierarchical Softmax的CBOW&lt;/h3&gt;

&lt;p&gt;对于从输入层到隐藏层（投影层）：对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;周围的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;个词向量求和取平均即可：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x_w = \frac{1}{2c}\sum\limits_{i=1}^{2c}x_i
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后通过梯度上升法来更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{j-1}^w\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;，因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;个向量求和得到的，所以梯度更新后，会用&lt;strong&gt;同一个梯度项&lt;/strong&gt;(即下面提到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e\)&lt;/code&gt;)直接更新原始的&lt;strong&gt;各个&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i(i=1,2,...,2c)\)&lt;/code&gt;，即：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
\theta_{j-1}^w &amp;amp;= \theta_{j-1}^w + \eta  (1-d_j^w-\sigma(x_w^T\theta_{j-1}^w))x_w \\
x_w &amp;amp;= x_w +\eta \sum\limits_{j=2}^{l_w}(1-d_j^w-\sigma(x_w^T\theta_{j-1}^w))\theta_{j-1}^w \;(i =1,2..,2c) \\
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;总结：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输入：词向量的维度大小为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;，CBOW的上下文大小为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;，步长&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;输出：哈夫曼树内部节点模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，所有词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;基于语料训练样本建立哈夫曼树。&lt;/li&gt;
    &lt;li&gt;随机初始化所有的模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，所有的词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;。&lt;/li&gt;
    &lt;li&gt;对于训练集中的每一个样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((context(w), w)\)&lt;/code&gt;：
      &lt;ol&gt;
        &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e=0\)&lt;/code&gt;计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w= \frac{1}{2c}\sum\limits_{i=1}^{2c}x_i\)&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j=2,3,...,l_w\)&lt;/code&gt;，计算
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
  \begin{align*}
  f &amp;amp;= \sigma(x_w^T\theta_{j-1}^w) \\
  g &amp;amp;= (1-d_j^w-f)\eta \\
  e &amp;amp;= e + g\theta_{j-1}^w \\
  \theta_{j-1}^w&amp;amp;= \theta_{j-1}^w + gx_w \\
  \end{align*}
\]&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(context(w)\)&lt;/code&gt;中的每一个词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;（共有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;个）进行更新：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x_i = x_i + e
\]&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;如果梯度收敛，则结束梯度迭代，否则回到步骤3继续迭代。&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;基于hierarchical-softmax的skip-gram&quot;&gt;基于Hierarchical Softmax的Skip-Gram&lt;/h3&gt;

&lt;p&gt;输入的只有一个词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;，输出的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;个词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(context(w)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;对于从输入层到隐藏层（投影层），这一步比CBOW简单，由于只有一个词，所以，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;就是词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;对应的词向量。&lt;/p&gt;

&lt;p&gt;然后通过梯度上升法来更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{j-1}^w\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;，这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;周围有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;个词向量，有两种处理方式：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;期望&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(x_i|x_w), i=1,2...2c\)&lt;/code&gt;最大。也就是从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;出发，走到第&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;个词，需要走&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;条路径&lt;/li&gt;
  &lt;li&gt;期望&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(x_w|x_i), i=1,2...2c\)&lt;/code&gt;最大。也就是对于每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;，从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;出发，走到&lt;strong&gt;同一个终点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;&lt;/strong&gt;，同样是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;条路径。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;word2vec中，使用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(x_w|x_i)\)&lt;/code&gt;。这样一来，在一个迭代窗口内，不是只更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;一个词，而是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i,i=1,2,...,2c\)&lt;/code&gt;共&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;个词。这样整体的迭代会更加的均衡。因此，Skip-Gram模型并没有和CBOW模型一样对输入进行迭代更新，而是对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;个输出进行迭代更新。&lt;/p&gt;

&lt;p&gt;总结：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输入：基于Skip-Gram的语料训练样本，词向量的维度大小&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;，Skip-Gram的上下文大小&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;,步长&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;输出：哈夫曼树的内部节点模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，所有的词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;基于语料训练样本建立哈夫曼树。&lt;/li&gt;
    &lt;li&gt;随机初始化所有的模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，所有的词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;。&lt;/li&gt;
    &lt;li&gt;对于训练集中的每一个样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((w, context(w))\)&lt;/code&gt;：
      &lt;ol&gt;
        &lt;li&gt;for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i=1,2,...,2c\)&lt;/code&gt;
          &lt;ol&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e=0\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j=2,3,...,l_w\)&lt;/code&gt;，计算
 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
   \begin{align*}
   f &amp;amp;= \sigma(x_i^T\theta_{j-1}^w) \\
   g &amp;amp;= (1-d_j^w-f)\eta \\
   e &amp;amp;= e + g\theta_{j-1} \\
   \theta_{j-1}^w&amp;amp;= \theta_{j-1}^w + gx_i \\
   \end{align*}
 \]&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;进行完&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j\)&lt;/code&gt;的for循环后，更新一次&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;
 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
 x_i = x_i + e
 \]&lt;/code&gt;&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
        &lt;li&gt;进行完&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;的for循环后，如果梯度收敛，则结束梯度迭代，否则回到步骤3.1继续迭代。&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;hierarchical-softmax源码解析&quot;&gt;hierarchical softmax源码解析&lt;/h3&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/tmikolov/word2vec/blob/master/word2vec.c&quot;&gt;https://github.com/tmikolov/word2vec/blob/master/word2vec.c&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;neu1e&lt;/code&gt;：相当于上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syn0&lt;/code&gt;：相当于上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syn1&lt;/code&gt;：相当于上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_{j-1}^i\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layer1_size&lt;/code&gt;：相当于上面的词向量的维度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;window&lt;/code&gt;：相当于上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vocab[word].code[d]&lt;/code&gt;：当前单词word的，第d个编码，编码不含Root结点&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vocab[word].point[d]&lt;/code&gt;：当前单词word，第d个编码下，前置的结点&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;cbow + hs：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;codelen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// Propagate hidden -&amp;gt; output&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EXP_TABLE_SIZE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))];&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// 'g' is the gradient multiplied by the learning rate&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// Propagate errors output -&amp;gt; hidden&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// Learn weights hidden -&amp;gt; output&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;skipgram + hs:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;codelen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// Propagate hidden -&amp;gt; output&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EXP_TABLE_SIZE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))];&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// 'g' is the gradient multiplied by the learning rate&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// Propagate errors output -&amp;gt; hidden&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// Learn weights hidden -&amp;gt; output&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;42-负采样和nce&quot;&gt;4.2 负采样和nce&lt;/h2&gt;

&lt;p&gt;下文对nce有大概的讲解。先来重点看下负采样。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/pinard/p/7249903.html&quot;&gt;https://www.cnblogs.com/pinard/p/7249903.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;hs的一个缺点就是，如果我们的训练样本里的&lt;strong&gt;中心词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;是一个很生僻的词&lt;/strong&gt;，那么就得在哈夫曼树中辛苦的向下走很久。&lt;/p&gt;

&lt;p&gt;对于中心词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;和他的上下文&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(context(w)\)&lt;/code&gt;，这是一个正例。可以采集neg个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i, i=1,2,..neg\)&lt;/code&gt;，这样分别和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(context(w)\)&lt;/code&gt;可以构成neg个负例。然后利用这些正负例，进行二元logistic regression，得到负采样的每个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i\)&lt;/code&gt;对应的模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta_i\)&lt;/code&gt;，和每个词的词向量。&lt;/p&gt;

&lt;h3 id=&quot;negative-sampling梯度计算&quot;&gt;Negative Sampling梯度计算&lt;/h3&gt;

&lt;p&gt;定义正例为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_0\)&lt;/code&gt;，neg个负例为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i, i=1,2,..neg\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;正例的概率和label是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(context(w_0), w_i) = \sigma(x_{w_0}^T\theta^{w_i}) ,y_i=1, i=0
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;负例的概率和label是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(context(w_0), w_i) =1-  \sigma(x_{w_0}^T\theta^{w_i}), y_i = 0, i=1,2,..neg
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;期望能最大化：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\prod_{i=0}^{neg}P(context(w_0), w_i) = \sigma(x_{w_0}^T\theta^{w_0})\prod_{i=1}^{neg}(1-  \sigma(x_{w_0}^T\theta^{w_i}))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以似然函数是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\prod_{i=0}^{neg} \sigma(x_{w_0}^T\theta^{w_i})^{y_i}(1-  \sigma(x_{w_0}^T\theta^{w_i}))^{1-y_i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;对应的对数似然函数就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L = \sum\limits_{i=0}^{neg}y_i log(\sigma(x_{w_0}^T\theta^{w_i})) + (1-y_i) log(1-  \sigma(x_{w_0}^T\theta^{w_i}))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;与hs类似，需要对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{w_i}\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^{w_i},  i=0,1,..neg\)&lt;/code&gt;这两大参数进行更新：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^{w_i}\)&lt;/code&gt;的梯度如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
\frac{\partial L}{\partial \theta^{w_i} } &amp;amp;= y_i(1-  \sigma(x_{w_0}^T\theta^{w_i}))x_{w_0}-(1-y_i)\sigma(x_{w_0}^T\theta^{w_i})x_{w_0} \\
&amp;amp; = (y_i -\sigma(x_{w_0}^T\theta^{w_i})) x_{w_0}
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;同样地，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{w_0}\)&lt;/code&gt;的梯度如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial L}{\partial x_{w_0} } = \sum\limits_{i=0}^{neg}(y_i -\sigma(x_{w_0}^T\theta^{w_i}))\theta^{w_i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;negative-sampling负采样方法&quot;&gt;Negative Sampling负采样方法&lt;/h3&gt;

&lt;p&gt;如果词汇表的大小为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;，那么我们将一段长度为1的线段分成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;份，每份对应词汇表中的一个词。高频词对应的线段长，低频词对应的线段短(和&lt;a href=&quot;https://daiwk.github.io/posts/knowledge-algorithms.html#%E8%BD%AE%E7%9B%98%E8%B5%8C&quot;&gt;轮盘赌&lt;/a&gt;有异曲同工之妙)。每个词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;的线段长度由下式决定：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
len(w) = \frac{count(w)}{\sum\limits_{u \in vocab} count(u)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;word2vec中，分子和分母都取了3/4次幂：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
len(w) = \frac{count(w)^{3/4}}{\sum\limits_{u \in vocab} count(u)^{3/4}}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;采样前，我们将这段长度为1的线段划分&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;&lt;strong&gt;等份&lt;/strong&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M &amp;gt;&amp;gt; V\)&lt;/code&gt;。这样可以保证每个词对应的线段都会划分成对应的小块。M份中的每一份都会落在某一个词对应的线段上。&lt;/p&gt;

&lt;p&gt;采样的时候，我们只需要从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;个位置中&lt;strong&gt;采样出neg个位置&lt;/strong&gt;就行，此时采样到的每一个位置对应到的线段所属的词就是我们的负例词。&lt;/p&gt;

&lt;p&gt;在word2vec中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;取值默认为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(10^8\)&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;基于negative-sampling的cbow模型&quot;&gt;基于Negative Sampling的CBOW模型&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;输入：基于CBOW的语料训练样本，词向量的维度大小&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;，CBOW的上下文大小&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;，步长&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;, 负采样的个数neg&lt;/li&gt;
  &lt;li&gt;输出：词汇表每个词对应的模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，所有的词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;随机初始化所有的模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，所有的词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;。&lt;/li&gt;
    &lt;li&gt;对于训练集中的每一个样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((context(w_0), w_0)\)&lt;/code&gt;，负采样出neg个负例中心词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i, i=1,2,...neg\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;进行梯度上升迭代过程，对于训练集中的每一个样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((context(w_0), w_0,w_1,...w_{neg})\)&lt;/code&gt;做如下处理：
      &lt;ol&gt;
        &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e=0\)&lt;/code&gt;计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{w_0}= \frac{1}{2c}\sum\limits_{i=1}^{2c}x_i\)&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i=0,1,...,neg\)&lt;/code&gt;，计算
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
  \begin{align*}
  f &amp;amp;= \sigma(x_{w_0}^T\theta^{w_i}) \\
  g &amp;amp;= (y_i-f)\eta \\
  e &amp;amp;= e + g\theta^{w_i} \\
  \theta^{w_i}&amp;amp;= \theta^{w_i} + gx_{w_0} \\
  \end{align*}
\]&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(context(w)\)&lt;/code&gt;中的每一个词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_k\)&lt;/code&gt;（共有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;个）进行更新：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x_k = x_k + e
\]&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;如果梯度收敛，则结束梯度迭代，否则回到步骤3继续迭代。&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;基于negative-sampling的skip-gram模型&quot;&gt;基于Negative Sampling的Skip-Gram模型&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;输入：基于Skip-Gram的语料训练样本，词向量的维度大小&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;，Skip-Gram的上下文大小&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2c\)&lt;/code&gt;，步长&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;，负采样的个数neg&lt;/li&gt;
  &lt;li&gt;输出：词汇表每个词对应的模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，所有的词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;随机初始化所有的模型参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，所有的词向量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;。&lt;/li&gt;
    &lt;li&gt;对于训练集中的每一个样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((context(w_0), w_0)\)&lt;/code&gt;，负采样出neg个负例中心词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_i, i=1,2,...neg\)&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;进行梯度上升迭代过程，对于训练集中的每一个样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((context(w_0), w_0,w_1,...w_{neg})\)&lt;/code&gt;做如下处理：
      &lt;ol&gt;
        &lt;li&gt;for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i=1,2,...,2c\)&lt;/code&gt;
          &lt;ol&gt;
            &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e=0\)&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j=0,1,...,neg\)&lt;/code&gt;，计算
 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
   \begin{align*}
   f &amp;amp;= \sigma(x_{w_{0i}}^T\theta^{w_j}) \\
   g &amp;amp;= (y_j-f)\eta \\
   e &amp;amp;= e + g\theta^{w_j} \\
   \theta^{w_j} &amp;amp;= \theta^{w_j} + gx_{w_{0i}} \\
   \end{align*}
 \]&lt;/code&gt;&lt;/li&gt;
            &lt;li&gt;进行完&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(j\)&lt;/code&gt;的for循环后，更新一次&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{w_{0i}}\)&lt;/code&gt;
 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
 x_{w_{0i}} = x_{w_{0i}} + e
 \]&lt;/code&gt;&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
        &lt;li&gt;进行完&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;的for循环后，如果梯度收敛，则结束梯度迭代，否则回到步骤3.1继续迭代。&lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;negative-sampling源码解析&quot;&gt;negative sampling源码解析&lt;/h3&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/tmikolov/word2vec/blob/master/word2vec.c&quot;&gt;https://github.com/tmikolov/word2vec/blob/master/word2vec.c&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;neu1e&lt;/code&gt;：相当于上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(e\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syn0&lt;/code&gt;：相当于上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_w\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syn1neg&lt;/code&gt;：相当于上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^{w_i}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layer1_size&lt;/code&gt;：相当于上面的词向量的维度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;window&lt;/code&gt;：相当于上面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;negative&lt;/code&gt;：相当于上面的neg&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;table_size&lt;/code&gt;：相当于上面讲负采样时提到的划分数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vocab[word].code[d]&lt;/code&gt;：当前单词word的，第d个编码，编码不含Root结点&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vocab[word].point[d]&lt;/code&gt;：当前单词word，第d个编码下，前置的结点&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;cbow + negative sampling:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;negative&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;negative&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;next_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25214903917&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1neg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EXP_TABLE_SIZE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1neg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1neg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;skipgram + negative sampling:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;negative&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;negative&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;next_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25214903917&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next_random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1neg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expTable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EXP_TABLE_SIZE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_EXP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neu1e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1neg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer1_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn1neg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;syn0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;5-面试常见问题&quot;&gt;5. 面试常见问题&lt;/h1&gt;

&lt;p&gt;参考 &lt;a href=&quot;https://blog.csdn.net/zhangxb35/article/details/74716245&quot;&gt;https://blog.csdn.net/zhangxb35/article/details/74716245&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;x-tensorflow的简单实现&quot;&gt;x. tensorflow的简单实现&lt;/h1&gt;

&lt;p&gt;讲解：&lt;a href=&quot;https://www.tensorflow.org/tutorials/word2vec&quot;&gt;https://www.tensorflow.org/tutorials/word2vec&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;a href=&quot;https://github.com/tensorflow/tensorflow/blob/r1.8/tensorflow/examples/tutorials/word2vec/word2vec_basic.py&quot;&gt;https://github.com/tensorflow/tensorflow/blob/r1.8/tensorflow/examples/tutorials/word2vec/word2vec_basic.py&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;使用maximum likelihood principle，最大化给定previous words &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;，下一个词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;的概率（使用softmax定义）：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
P(w_t | h) &amp;amp;= \text{softmax} (\text{score} (w_t, h)) \\
           &amp;amp;= \frac{\exp \{ \text{score} (w_t, h) \} }
             {\sum_\text{Word w' in Vocab} \exp \{ \text{score} (w', h) \} }
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;需要最大化的对应的log-likelihood就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
 J_\text{ML} &amp;amp;= \log P(w_t | h) \\
  &amp;amp;= \text{score} (w_t, h) -
     \log \left( \sum_\text{Word w' in Vocab} \exp \{ \text{score} (w', h) \} \right).
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果直接硬算，对于每个时间步，都需要遍历词典大小的空间，显然效率是不行的。在word2vec中，将这样一个多分类问题，变成了一个区分目标词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;和k个noise words &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde w\)&lt;/code&gt;的二分类问题。下图是cbow的示例，skipgram正好是倒过来。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/nce-nplm.png&quot; style=&quot;max-width: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;数学上，目标就是要最大化：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
J_\text{NEG} = \log Q_\theta(D=1 |w_t, h) +
  k \mathop{\mathbb{E}}_{\tilde w \sim P_\text{noise}}
     \left[ \log Q_\theta(D = 0 |\tilde w, h) \right]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q_\theta(D=1 | w, h)\)&lt;/code&gt;是使用学到的embedding vector &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，在给定数据集&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D\)&lt;/code&gt;中的上下文&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;，看到词&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w\)&lt;/code&gt;的概率。在实践中，我们通过从噪声分布中抽取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个对比词来逼近期望值（即计算&lt;a href=&quot;https://en.wikipedia.org/wiki/Monte_Carlo_integration&quot;&gt;蒙特卡洛平均值&lt;/a&gt;）。&lt;/p&gt;

&lt;p&gt;直观地理解，这个目标就是希望预测为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;的概率尽可能大，同时预测为非&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde w\)&lt;/code&gt;的概率尽可能大，也就是，&lt;strong&gt;希望预测为真实词的概率尽量大，预测为noise word的概率尽量小&lt;/strong&gt;。在极限情况下，这可以近似为softmax，但这计算量比softmax小很多。这就是所谓的&lt;a href=&quot;https://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf&quot;&gt;negative sampling&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;tensorflow有一个很类似的损失函数&lt;a href=&quot;https://papers.nips.cc/paper/5165-learning-word-embeddings-efficiently-with-noise-contrastive-estimation.pdf&quot;&gt;noise-contrastive estimation(NCE)&lt;/a&gt;，即『噪声对比估算』，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.nn.nce_loss()&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;上面讲的是cbow，而skipgram其实也类似，假设拿一个词去预测周围的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2C\)&lt;/code&gt;个词：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
J &amp;amp;= -\log p(w_{t-C},...,w_{t+C}|w_t) \\
  &amp;amp;= -\log \prod ^{2C}_{c=1}\frac{\exp(u_{c,j^{*}_c})}{\sum ^V_{j=1} \exp(u_j)} \\
  &amp;amp;= -\sum ^{2C}_{c=1}u_{j^{*}_c} + 2C \log\sum ^{V}_{j=1}\exp(u_j)
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\log\sum ^{V}_{j=1}\exp(u_j)\)&lt;/code&gt;部分，也可以用负采样和nce去搞。&lt;/p&gt;

&lt;p&gt;针对句子&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;the quick brown fox jumped over the lazy dog
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果使用window_size=1（一个词&lt;strong&gt;左右各&lt;/strong&gt;采一个词），那么对于CBOW，就有：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;([the, brown], quick), ([quick, fox], brown), ([brown, jumped], fox), ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而对于skipgram，则有：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(quick, the), (quick, brown), (brown, quick), (brown, fox), ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在训练步&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;中，例如要用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quick&lt;/code&gt;来预测&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;the&lt;/code&gt;，我们从某个噪声分布（通常为一元分布&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(w)\)&lt;/code&gt;）中抽取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;num_noise&lt;/code&gt;个噪声对比样本。假设num_noise=1，并且将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sheep&lt;/code&gt;选为噪声样本。那么在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;时刻的目标是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
J^{(t)}_\text{NEG} = \log Q_\theta(D=1 | \text{the, quick}) +
  \log(Q_\theta(D=0 | \text{sheep, quick}))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;画图时，可以使用&lt;a href=&quot;https://lvdmaaten.github.io/tsne/&quot;&gt;t-SNE&lt;/a&gt;的降维方法，将高维向量映射到2维空间。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/linear-relationships.png&quot; style=&quot;max-width: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;代码解读&quot;&gt;代码解读&lt;/h2&gt;

&lt;h3 id=&quot;读数据&quot;&gt;读数据&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Read the data into a list of strings.
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Extract the first file enclosed in a zip file as a list of words.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zipfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ZipFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;namelist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;调用：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;vocabulary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;建立数据集&quot;&gt;建立数据集&lt;/h3&gt;

&lt;p&gt;build一个dataset：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Process raw inputs into a dataset.&quot;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'UNK'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;most_common&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;## 取出词频top n_words-1的词，词频高的index小
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;unk_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# dictionary['UNK']
&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;unk_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;unk_count&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;reversed_dictionary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reversed_dictionary&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;调用：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reverse_dictionary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;build_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;vocabulary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vocabulary_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;生成skipgram的一个batch&quot;&gt;生成skipgram的一个batch&lt;/h3&gt;

&lt;p&gt;生成一个batch的方法如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skip_window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skip_window&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ndarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skip_window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# [ skip_window target skip_window ]
&lt;/span&gt;  &lt;span class=&quot;nb&quot;&gt;buffer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deque&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maxlen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# pylint: disable=redefined-builtin
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;context_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skip_window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;words_to_use&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context_word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words_to_use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;skip_window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Backtrack a little bit to avoid skipping words in the end of a batch
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;调用：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generate_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;skip_window&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;定义模型&quot;&gt;定义模型&lt;/h3&gt;

&lt;h4 id=&quot;placeholder等&quot;&gt;placeholder等&lt;/h4&gt;

&lt;p&gt;先定义一下validation set&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;valid_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Random set of words to evaluate similarity on.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Only pick dev samples in the head of the distribution.
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_examples&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valid_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后定义一下input和label的placeholder：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Input data.
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'inputs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;valid_dataset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;int32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;网络参数&quot;&gt;网络参数&lt;/h4&gt;

&lt;p&gt;定义：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;vocabulary_size x embedding_size的embedding&lt;/li&gt;
  &lt;li&gt;vocabulary_size x embedding_size的nce_weights&lt;/li&gt;
  &lt;li&gt;vocabulary_size的nce_biases&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;# Ops and variables pinned to the CPU because of missing GPU implementation
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'/cpu:0'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Look up embeddings for inputs.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'embeddings'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random_uniform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocabulary_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;embed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Construct the variables for the NCE loss
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'weights'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;nce_weights&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;truncated_normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocabulary_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'biases'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;nce_biases&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocabulary_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;loss&quot;&gt;loss&lt;/h4&gt;

&lt;p&gt;在NCE的实现中，使用的是log_uniform_candidate_sampler：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;会在[0, range_max)中采样出一个整数k(&lt;strong&gt;k相当于词的id&lt;/strong&gt;)&lt;/li&gt;
  &lt;li&gt;P(k) = (log(k + 2) - log(k + 1)) / log(range_max + 1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned}
P(k)&amp;amp;=\frac{1}{log(range\_max+1)}log(\frac{k+2}{k+1}) \\ 
 &amp;amp;= \frac{1}{log(range\_max+1)}log(1+\frac{1}{k+1}) \\
\end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k越大，被采样到的概率越小。&lt;/strong&gt;而我们的词典中，可以发现&lt;strong&gt;词频高&lt;/strong&gt;的&lt;strong&gt;index小&lt;/strong&gt;，所以高词频的词会被&lt;strong&gt;优先&lt;/strong&gt;采样为负样本。&lt;/p&gt;

&lt;p&gt;下面定义loss&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'loss'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nce_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nce_weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;biases&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nce_biases&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;num_sampled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_sampled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;num_classes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocabulary_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Construct the SGD optimizer using a learning rate of 1.0.
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'optimizer'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GradientDescentOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;计算cos&quot;&gt;计算cos&lt;/h4&gt;

&lt;p&gt;只是为了看看迭代过程中，validtion set里每个词最近的topk词，看看效果。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;# Compute the cosine similarity between minibatch examples and all embeddings.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keep_dims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;normalized_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;## 使用valid_dataset做输入，normalized_embeddings做参数，进行lookup得到的emb
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;valid_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normalized_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                            &lt;span class=&quot;n&quot;&gt;valid_dataset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# (valid_size, emb_size) x (emb_size, vocab_size) = (valid_size, vocab_size)
&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 因为valid_emb和norm_emb都是emb/norm(emb)，所以两者的乘积就是cos
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;similarity&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;valid_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;normalized_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transpose_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;其他&quot;&gt;其他&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;# Merge all summaries.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;merged&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;merge_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Add variable initializer.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;init&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Create a saver.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;训练&quot;&gt;训练&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Open a writer to write summaries.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FileWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;graph&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# We must initialize all variables before we use them.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Initialized'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;average_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_steps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;batch_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generate_batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_skips&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                                &lt;span class=&quot;n&quot;&gt;skip_window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Define metadata variable.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;run_metadata&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RunMetadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# We perform one update step by evaluating the optimizer op (including it
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# in the list of returned values for session.run()
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Also, evaluate the merged op to get all summaries from the returned &quot;summary&quot; variable.
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Feed metadata variable to session for visualizing the graph in TensorBoard.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss_val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;run_metadata&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run_metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;average_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss_val&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Add returned summaries to writer in each step.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Add metadata to visualize the graph for the last run.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_steps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_run_metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run_metadata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'step%d'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;average_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# The average loss is an estimate of the loss over the last 2000 batches.
&lt;/span&gt;      &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Average loss at step '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;': '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;average_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;average_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# Note that this is expensive (~20% slowdown if computed every 500 steps)
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;sim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;similarity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;valid_word&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reverse_dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;top_k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# number of nearest neighbors
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;nearest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:]).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argsort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;top_k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;log_str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Nearest to %s:'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valid_word&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;top_k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;close_word&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reverse_dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nearest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;log_str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'%s %s,'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;close_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;final_embeddings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;normalized_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Write corresponding labels for the embeddings.
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'/metadata.tsv'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'w'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocabulary_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reverse_dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Save the model for checkpoints.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;saver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'model.ckpt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Create a configuration for visualizing embeddings with the labels in TensorBoard.
&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;projector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProjectorConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;embedding_conf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;embedding_conf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;embedding_conf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;metadata_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FLAGS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'metadata.tsv'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;projector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visualize_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;可视化&quot;&gt;可视化&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Function to draw visualization of distance between embeddings.
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;plot_with_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;low_dim_embs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;low_dim_embs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'More labels than embeddings'&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figsize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# in inches
&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;low_dim_embs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;annotate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;xytext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;textcoords&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'offset points'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'right'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;va&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'bottom'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;savefig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# pylint: disable=g-import-not-at-top
&lt;/span&gt;  &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn.manifold&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TSNE&lt;/span&gt;
  &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;tsne&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TSNE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;perplexity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_components&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'pca'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_iter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'exact'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plot_only&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;low_dim_embs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tsne&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit_transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;final_embeddings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot_only&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:])&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reverse_dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;xrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot_only&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;plot_with_labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;low_dim_embs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gettempdir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'tsne.png'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ImportError&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Please install sklearn, matplotlib, and scipy to show embeddings.'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;y1-tensorflow的高级实现1&quot;&gt;y1. tensorflow的高级实现1&lt;/h1&gt;

&lt;p&gt;如果您发现模型在&lt;strong&gt;输入数据方面&lt;/strong&gt;存在严重&lt;strong&gt;瓶颈&lt;/strong&gt;，您可能需要针对您的问题实现&lt;strong&gt;自定义数据读取器&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec.py&quot;&gt;https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec.py&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;y2-tensorflow的高级实现2&quot;&gt;y2. tensorflow的高级实现2&lt;/h1&gt;

&lt;p&gt;如果您的模型不再受 I/O 限制，但您仍希望提高性能，则可以通过&lt;strong&gt;编写自己的 TensorFlow 操作&lt;/strong&gt;（如&lt;a href=&quot;https://www.tensorflow.org/guide/extend/op&quot;&gt;添加新操作&lt;/a&gt;中所述）进一步采取措施：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec_optimized.py&quot;&gt;https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec_optimized.py&lt;/a&gt;&lt;/p&gt;

&lt;h1 id=&quot;自己的总结&quot;&gt;自己的总结&lt;/h1&gt;

&lt;p&gt;某个时候整理了个ppt：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/w2v-fasttext.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>命名实体识别</title>
   <link href="http://daiwk.github.io/posts/nlp-named-entity-recognition.html"/>
   <updated>2016-10-01T00:00:00+00:00</updated>
   <id>/posts/nlp-named-entity-recognition</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-方法概述&quot;&gt;&lt;strong&gt;1. 方法概述&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-基本概念&quot;&gt;&lt;strong&gt;1.1 基本概念&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-基于统计模型的命名实体识别方法归纳&quot;&gt;&lt;strong&gt;1.2 基于统计模型的命名实体识别方法归纳&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-基于crf的命名实体识别方法&quot;&gt;2. 基于CRF的命名实体识别方法&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;详见《统计自然语言处理(第二版)》p150-162&lt;/p&gt;

&lt;h1 id=&quot;1-方法概述&quot;&gt;&lt;strong&gt;1. 方法概述&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;11-基本概念&quot;&gt;&lt;strong&gt;1.1 基本概念&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;实体概念在文本中的引用（entity mention，即“指称项”）有三种形式：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;命名性指称&lt;/strong&gt;：例如，乔丹、麦迪。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;名词性指称&lt;/strong&gt;：例如，中国国家男足主教练、xx学生会主席。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;代词性指称&lt;/strong&gt;：例如，他、她。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MUC-6最早提出命名实体（named entity），当时关注的焦点是information extraction，即从报章等非结构化文本中抽取关于公司活动和国防相关活动的结构化信息，而&lt;strong&gt;人名、地名、组织机构名、时间和数字表达（时间、日期、货币量和百分数等）&lt;/strong&gt;是结构化信息的关键内容。&lt;/p&gt;

&lt;p&gt;相关的评测会议还有CoNLL(Conference on Computational Natural Language Learning)、ACE和IEER(Information Extraction-Entity Recognition Evaluation)。&lt;/p&gt;

&lt;p&gt;MUC-6关注人名、地名（细化为城市、州和国家）、组织机构名。CoNLL扩大了专有名词的范围，包含产品名的识别。其他研究工作中也涉及了电影名、书名、项目名、研究领域名、电子邮件地址、电话号码等，还有生物信息学领域的专有名词等。本文主要关注&lt;strong&gt;人名、地名和组织机构名&lt;/strong&gt;三类专有名词的识别方法。&lt;/p&gt;

&lt;h2 id=&quot;12-基于统计模型的命名实体识别方法归纳&quot;&gt;&lt;strong&gt;1.2 基于统计模型的命名实体识别方法归纳&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;大致分类如下：&lt;/p&gt;

&lt;html&gt;
&lt;center&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;all&quot; frame=&quot;border&quot;&gt;

&lt;thead&gt;
&lt;tr&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;类型&lt;/th&gt;
&lt;th scope=&quot;col&quot; class=&quot;left&quot;&gt;采用的模型或方法&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;有监督&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;隐马尔科夫模型或语言模型&lt;br /&gt;最大熵模型&lt;br /&gt;支持向量机&lt;br /&gt;条件随机场&lt;br /&gt;决策树&lt;br /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;半监督/弱监督&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;利用标注的小数据集（种子数据）自举（bootstrap）学习&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;无监督&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;利用词汇资源（如WordNet）进行上下文聚类&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;left&quot;&gt;混合方法&lt;/td&gt;
&lt;td class=&quot;left&quot;&gt;集中模型相结合或利用统计方法和人工总结的知识库&lt;/td&gt;
&lt;/tr&gt;

&lt;/tbody&gt;
&lt;/table&gt;&lt;/center&gt;
&lt;/html&gt;

&lt;h1 id=&quot;2-基于crf的命名实体识别方法&quot;&gt;2. 基于CRF的命名实体识别方法&lt;/h1&gt;

&lt;p&gt;基本思路（以汉语为例）：&lt;/p&gt;

&lt;p&gt;将给定的文本首先进行&lt;strong&gt;分词&lt;/strong&gt;处理，然后对&lt;strong&gt;人名、简单地名和简单的组织机构名&lt;/strong&gt; 进行识别，最后识别&lt;strong&gt;复合地名和复合组织机构名&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;简单地名如北京、大不列颠；复合地名：北京市海淀区中关村xx路、中华人民共和国；简单组织机构名：北京大学、卫生部、联合国；复合组织机构名：欧洲中央银行、中华人民共和国卫生部、联合国世界粮食计划署。&lt;/p&gt;

&lt;p&gt;由于基于CRF的命名实体识别属于有监督的学习方法，因此需要使用已标注的大规模语料，例如北京大学计算语言学研究所标注的现代汉语多级加工语料【&lt;a href=&quot;http://klcl.pku.edu.cn:8088/qt/info!input.action?id=4#1&quot;&gt;现代汉语多级加工语料库&lt;/a&gt;】。还有一个在线的资源&lt;a href=&quot;http://ccl.pku.edu.cn/corpus.asp&quot;&gt;北大的CCL&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;训练阶段：&lt;/p&gt;

&lt;p&gt;首先，将分词语料的标记转化成&lt;strong&gt;用于命名实体序列标注的标记&lt;/strong&gt;，例如pnb表示人名的起始用字，pni表示名字的内部用字。同样的，还有locb/loci/orgb/orgi。另外，out表示不属于某一实体。&lt;/p&gt;

&lt;p&gt;然后，确定&lt;strong&gt;特征模板&lt;/strong&gt;。特征模板一般采用&lt;strong&gt;当前位置的前后n(n&amp;gt;=1)个位置上的字&lt;/strong&gt;（或词、字母、数字、标点等，统称“字串”）&lt;strong&gt;及其标记&lt;/strong&gt; 表示，即[w(-n)/tag(-n), …, w(-1)/tag(-1), w(0)/tag(0), w(1)/tag(1), …, w(n)/tag(n)]。综合考虑性能和涵盖的信息量，&lt;strong&gt;通常n取2~3&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;由于不同命名实体往往出现在不同的上下文语境中，因此，对不同的命名实体识别一般采用不同的特征模板。&lt;/p&gt;

&lt;p&gt;例如，识别汉语中的人名时，考虑到&lt;strong&gt;不同国家的人名构成特点&lt;/strong&gt;有明显的不同，可以划分为中国人名、日本人名、俄罗斯人名、欧美人名等。&lt;/p&gt;

&lt;p&gt;同时考虑到出现在&lt;strong&gt;人名左右两边的字串对于确定人名的边界&lt;/strong&gt;有一定的帮助，如某些称谓、某些动词和标点等，可以拿某些&lt;strong&gt;总结出来的“指界词”&lt;/strong&gt;（左指界词或者右指界词）作为特征。&lt;/p&gt;

&lt;p&gt;例如，人名识别常用的特征模板：&lt;/p&gt;

&lt;p&gt;x(w0sn):当前词是否为中国人名姓氏用字，是：True,否：false&lt;/p&gt;

&lt;p&gt;x(w0gn):当前词是否为中国人名名字用字，是：True,否：false&lt;/p&gt;

&lt;p&gt;x(w0jn):当前词是否为日本人名用字，是：True,否：false&lt;/p&gt;

&lt;p&gt;x(w0an):当前词是否为欧美人名用字，是：True,否：false&lt;/p&gt;

&lt;p&gt;x(w0rn):当前词是否为俄罗斯人名用字，是：True,否：false&lt;/p&gt;

&lt;p&gt;x(w+-ib):当前词左右两边第i个词（i=1,2）是否为指界词，是：True,否：false&lt;/p&gt;

&lt;p&gt;也可以组合特征：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(x(w0sn),y(w0sn)) = 
\begin{cases}
1, x(w0sn)=true\ and\ y(w0sn) = pnb \\
0, else \\
\end{cases}
\]&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>自然语言处理基本常识</title>
   <link href="http://daiwk.github.io/posts/nlp-first-glance.html"/>
   <updated>2016-10-01T00:00:00+00:00</updated>
   <id>/posts/nlp-first-glance</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#1-初识nlp&quot;&gt;&lt;strong&gt;1. 初识NLP&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-关于理解的标准turing-test&quot;&gt;&lt;strong&gt;1.1 关于理解的标准(Turing test)&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-自然语言处理研究的内容和面临的困难&quot;&gt;&lt;strong&gt;1.2 自然语言处理研究的内容和面临的困难&lt;/strong&gt;&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#121-研究方向&quot;&gt;&lt;strong&gt;1.2.1 研究方向&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#122-层次&quot;&gt;&lt;strong&gt;1.2.2 层次&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#123-困难&quot;&gt;&lt;strong&gt;1.2.3 困难&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-统计自然语言处理第2版内容安排&quot;&gt;&lt;strong&gt;1.3 《统计自然语言处理（第2版）》内容安排&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h1 id=&quot;1-初识nlp&quot;&gt;&lt;strong&gt;1. 初识NLP&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;11-关于理解的标准turing-test&quot;&gt;&lt;strong&gt;1.1 关于理解的标准(Turing test)&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;英国数学家&lt;strong&gt;图灵&lt;/strong&gt;在1950年，提出了判断计算机是否可以被认为“能思考”的标准（图灵测试，Turing test）：如果一个计算机系统的&lt;strong&gt;&lt;em&gt;表现(act)、反应(react)和相互作用(interact)&lt;/em&gt;&lt;/strong&gt;都和有意识的个体一样，那么，这个计算机系统就应该被认为是有意识的。&lt;/p&gt;

&lt;h2 id=&quot;12-自然语言处理研究的内容和面临的困难&quot;&gt;&lt;strong&gt;1.2 自然语言处理研究的内容和面临的困难&lt;/strong&gt;&lt;/h2&gt;

&lt;h3 id=&quot;121-研究方向&quot;&gt;&lt;strong&gt;1.2.1 研究方向&lt;/strong&gt;&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;机器翻译&lt;/strong&gt;(machine translation, MT): 一种语言到另一种语言的自动翻译&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;自动文摘&lt;/strong&gt;(automatic summarizing/automatic abstracting):自动归纳、提炼原文档的摘要和缩写。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;信息检索&lt;/strong&gt;(information retrieval, IR)：海量文档中检索到符合用户需求的相关文档，面对两种或两种以上语言的IR叫做跨语言信息检索(cross-language/trans-lingual information retrieval)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;文档分类&lt;/strong&gt;(document/text categorization/classification)：文档/文本自动分类。近年来，情感分类（sentiment classification）或称文本倾向性识别（text orientation identification）比较火，已经成为支撑舆情分析（public opinion analysis）的基本技术。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;问答系统&lt;/strong&gt;(question-answering system)：人机对话系统。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;信息过滤&lt;/strong&gt;(information filtering)：自动识别和过滤。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;信息抽取&lt;/strong&gt;(information extraction)：从文本中抽取特定的事件(event)或事实信息，又称事件抽取(event extraction)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;文本挖掘&lt;/strong&gt;(text mining/data mining)：挖掘高质量信息。涉及文本分类、文本聚类(text clustering)、概念或实体抽取(concept/entity extraction)、粒度分类、情感分析、自动文摘和实体关系建模(entity relation modeling)等多种技术。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;舆情分析&lt;/strong&gt;(public opinion analysis)：一定的社会空间内，围绕中介性社会事件的发生、发展及变化，民众对社会管理者产生和持有的社会政治态度。主要信息来源有新闻评论、论坛(bulletin board system, BBS)、聚合新闻(简易供稿，really simple syndication, RSS)、twitter等。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;隐喻计算&lt;/strong&gt;(metaphorical computation)：隐喻指的是，用乙事物或某些特征来描述甲事物的语言现象。隐喻计算就是研究自然语言语句或篇章中隐喻修辞的理解方法。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;文字编辑和自动校对&lt;/strong&gt;(automatic proffreading)：对文字拼写、用词、语法、文档格式等进行自动检查、校对和编排。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;作文自动评分&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;光读字符识别&lt;/strong&gt;(optical character recognition, OCR)：印刷/手写字体识别。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;语音识别&lt;/strong&gt;(speech recognition)：语音转成文字。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;文语转换&lt;/strong&gt;(text-to-speech conversion)：书面文本自动转换成对应的语音表征，即&lt;strong&gt;&lt;em&gt;语音合成(speech synthesis)&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;说话人识别/认证/验证&lt;/strong&gt;(speaker recognition/identification/verification)：对说话人的言语样本做声学分析，从而推断/确定/验证说话人的身份。&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;122-层次&quot;&gt;&lt;strong&gt;1.2.2 层次&lt;/strong&gt;&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;形态学(morphology)&lt;/strong&gt;：即词法，研究此内部结构，包括屈折变化和构词法两部分。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;语法学(syntax)&lt;/strong&gt;：研究句子结构成分之间的相互关系和组成句子序列的规则。关注：&lt;strong&gt;为什么一句话可以这么说，也可以那么说？&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;语义学(semantics)&lt;/strong&gt;：研究语言的意义，研究语言的各级单位（词素、词、词组、句子、句子群、整段整篇的话语和文章乃至整个著作）的意义，以及语义与语音、语法、修辞、文字、语境、哲学思想、社会环境、个人修养的关系等。关注：&lt;strong&gt;这个语言单位到底说了什么？&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;语用学(pragmatics)&lt;/strong&gt;：可以集中在句子层次上进行语用研究，也可以是超出句子，对语言的实际使用情况进行调查研究，甚至与会话分析、语篇分析相结合，研究在不同上下文中的语句应用，以及上下文对语句理解所产生的影响。关注：&lt;strong&gt;为什么在特定的上下文中，要说这样的话？&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;123-困难&quot;&gt;&lt;strong&gt;1.2.3 困难&lt;/strong&gt;&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;歧义消解(disambiguation)&lt;/strong&gt;：例如，如何划分分词边界。
歧义结构分析结果的数量是随介词短语的数目的增加而呈现指数上升的。歧义结构的组合数称为开塔兰数(Catalan numbers, 记作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C_{n}\)&lt;/code&gt;)，即，如果句子中存在这样&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;个介词短语，那么：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[C_n = C^{n}_{2n} \dfrac{1}{n+1}\]&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;未知语言现象&lt;/strong&gt;：对于一个特定系统来说，总是有可能遇到未知词汇、未知结构等意外情况，而同时，每一种语言又随着社会的发展而动态变化着，&lt;strong&gt;新的词汇（新的人名、地名、组织机构名和专用词汇）、新的词义、新的词汇用法（新词类）&lt;/strong&gt;，甚至&lt;strong&gt;新的句子结构&lt;/strong&gt;都在不断地出现，尤其在口语对话或者计算机网络对话中，稀奇古怪的词汇和话语结构更是司空见惯。因此，鲁棒性(robustness)很重要。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;13-统计自然语言处理第2版内容安排&quot;&gt;&lt;strong&gt;1.3 《统计自然语言处理（第2版）》内容安排&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;http://i.imgur.com/0V4CvSu.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>深入理解svm</title>
   <link href="http://daiwk.github.io/posts/ml-svm-3-levels.html"/>
   <updated>2016-10-01T00:00:00+00:00</updated>
   <id>/posts/ml-svm-3-levels</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#0-推导&quot;&gt;0. 推导&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#1-最大间隔分类器&quot;&gt;1. 最大间隔分类器&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-logistic-regression&quot;&gt;1.1 logistic regression&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#12-1-1分类&quot;&gt;1.2 +1/-1分类&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-函数间隔与几何间隔&quot;&gt;1.3 函数间隔与几何间隔&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#131-函数间隔functional-margin\\hat\gamma\&quot;&gt;1.3.1 函数间隔(functional margin)&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#132-几何间隔geometric-margin\\tilde\gamma\&quot;&gt;1.3.2 几何间隔(geometric margin)&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{\gamma}\)&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#133-最大间隔分类器maximum-margin-classifier&quot;&gt;1.3.3 最大间隔分类器（maximum margin classifier）&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-support-vector&quot;&gt;2. support vector&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3-kernel&quot;&gt;3. kernel&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4-outliers&quot;&gt;4. Outliers&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#5-numerical-optimization&quot;&gt;5. Numerical Optimization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#6-duality&quot;&gt;6. Duality&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#7-kernel-ii&quot;&gt;7. Kernel II&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;支持向量机通俗导论（理解SVM的三层境界）&lt;/strong&gt;：&lt;a href=&quot;http://blog.csdn.net/v_july_v/article/details/7624837&quot;&gt;http://blog.csdn.net/v_july_v/article/details/7624837&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;支持向量机: Maximum Margin Classifier&lt;/strong&gt;
&lt;a href=&quot;http://blog.pluskid.org/?p=632&quot;&gt;http://blog.pluskid.org/?p=632&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;支持向量机: Support Vector&lt;/strong&gt;
&lt;a href=&quot;http://blog.pluskid.org/?p=682&quot;&gt;http://blog.pluskid.org/?p=682&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;支持向量机: Kernel&lt;/strong&gt;
&lt;a href=&quot;http://blog.pluskid.org/?p=685&quot;&gt;http://blog.pluskid.org/?p=685&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;支持向量机：Outliers&lt;/strong&gt;
&lt;a href=&quot;http://blog.pluskid.org/?p=692&quot;&gt;http://blog.pluskid.org/?p=692&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;支持向量机：Numerical Optimization&lt;/strong&gt;
&lt;a href=&quot;http://blog.pluskid.org/?p=696&quot;&gt;http://blog.pluskid.org/?p=696&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;支持向量机：Duality&lt;/strong&gt;
&lt;a href=&quot;http://blog.pluskid.org/?p=702&quot;&gt;http://blog.pluskid.org/?p=702&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;支持向量机：Kernel II&lt;/strong&gt;
&lt;a href=&quot;http://blog.pluskid.org/?p=723&quot;&gt;http://blog.pluskid.org/?p=723&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;0-推导&quot;&gt;0. 推导&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u014433413/article/details/78427574&quot;&gt;https://blog.csdn.net/u014433413/article/details/78427574&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/lijil168/article/details/69395023&quot;&gt;https://blog.csdn.net/lijil168/article/details/69395023&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L=1/2w^2+\sum _{i=1}^m\alpha _i[1-y_i(w^Tx+b)]\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1-y_i(w^Tx+b)\)&lt;/code&gt;这项&amp;lt;=0，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(alpha\)&lt;/code&gt;&amp;gt;=0，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L=1/2*w^2-CC\)&lt;/code&gt;，CC&amp;gt;=0，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L&amp;lt;=1/2*w^2\)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(max_{\alpha} L=1/2*w^2\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;原问题是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(min_{w,b}1/2*w^2\)&lt;/code&gt;，所以就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(min_{w,b}max_{\alpha}L\)&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-最大间隔分类器&quot;&gt;1. 最大间隔分类器&lt;/h2&gt;

&lt;h3 id=&quot;11-logistic-regression&quot;&gt;1.1 logistic regression&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;0/1分类：&lt;/strong&gt; 给sigmoid之前的函数值&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^Tx\)&lt;/code&gt;是负无穷到正无穷，sigmoid之后的值相当于取y=1时的概率值，大于0.5就视为1的类。如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^Tx&amp;gt;0\)&lt;/code&gt;则&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sigmoid(\theta^Tx)&amp;gt;0.5\)&lt;/code&gt;，换言之，要学到参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，使正例的的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^Tx\)&lt;/code&gt;远大于0，负例的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\theta^Tx\)&lt;/code&gt;远小于0.&lt;/p&gt;

&lt;h3 id=&quot;12-1-1分类&quot;&gt;1.2 +1/-1分类&lt;/h3&gt;

&lt;p&gt;svm中，使用+1/-1分类(方便推导公式)，而非logistic regression的0/1分类。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
g(f(x))=g(\omega^Tx+b) =
\begin{cases}
+1, \omega^Tx+b &amp;gt;= 0 \\
-1, \omega^Tx+b &amp;lt; 0 \\
\end{cases}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;最简单的情形，假设数据是线性可分的。&lt;/p&gt;

&lt;p&gt;所以，要寻找超平面的两条边界间的最大间隔，从而求w和b。&lt;/p&gt;

&lt;h3 id=&quot;13-函数间隔与几何间隔&quot;&gt;1.3 函数间隔与几何间隔&lt;/h3&gt;

&lt;h4 id=&quot;131-函数间隔functional-marginhatgamma&quot;&gt;1.3.1 函数间隔(functional margin)&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;函数间隔：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{\gamma}=y(\omega^Tx+b)=yf(x)\)&lt;/code&gt;（保证margin的非负）&lt;/p&gt;

&lt;p&gt;超平面&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((\omega,b)\)&lt;/code&gt;关于数据集T上的函数间隔：(w,b)关于T中所以样本点的函数间隔的最小值：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{\gamma}=min\hat{\gamma}_i,i=0,1,...
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;当w，b同时扩大时，超平面没变（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\omega^Tx+b=0,2\omega^Tx+2b=0\)&lt;/code&gt;），&lt;strong&gt;但函数间隔却变大了&lt;/strong&gt;，所以，需要规范化。&lt;/p&gt;

&lt;h4 id=&quot;132-几何间隔geometric-margintildegamma&quot;&gt;1.3.2 几何间隔(geometric margin)&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{\gamma}\)&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;../assets/geometric margin.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;投影到超平面的点为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;,因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\omega\)&lt;/code&gt;是垂直于超平面的法向量，设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;到超平面的距离是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;,那么&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x=x_0+\gamma \frac{\omega}{||\omega||}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;是超平面上的点，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f(x_0)=0\)&lt;/code&gt;，所以
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\\x=x_0+\gamma\frac{\omega}{||\omega||}
\\\omega^Tx=\omega^Tx_0+\gamma\frac{\omega^T\omega}{||\omega||}
\\\omega^Tx+b=\omega^Tx_0+b+\gamma\frac{\omega^T\omega}{||\omega||}
\\f(x)=0+\gamma\frac{\omega^T\omega}{||\omega||}
\\f(x)=\gamma||\omega||
\\\gamma=\frac{f(x)}{||\omega||}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;也是有符号的，而间隔只要他的绝对值，所以，几何间隔定义为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\tilde{\gamma}=y\gamma=\frac{yf(x)}{||\omega||}=\frac{\hat{\gamma}}{||\omega||}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;即，&lt;strong&gt;几何间隔＝函数间隔/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(||\omega||\)&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;133-最大间隔分类器maximum-margin-classifier&quot;&gt;1.3.3 最大间隔分类器（maximum margin classifier）&lt;/h4&gt;

&lt;p&gt;对一个数据点进行分类，当这个点和超平面之间的间隔越大的时候，分类正确的把握越大(离超平面越远，那他肯定不是分界面附近的点，肯定属于某一个类别)。对于一个包含n个点的数据集，我们可以很自然地定义它的间隔为所有这n个点的间隔中最小的那个。于是，为了使得分类的把握尽量大，我们希望所选择的超平面能够最大化这个间隔值（&lt;strong&gt;最大化这n个点里离超平面最近的点和超平面间的间隔&lt;/strong&gt;）。&lt;/p&gt;

&lt;p&gt;所以，最大间隔分类器的目标就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\max\tilde{\gamma}, s.t. y_i(\omega^Tx_i+b)=\hat{\gamma}_i&amp;gt;=\hat{\gamma}, i=1,2,...,n
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{\gamma}=\tilde{\gamma}||\omega||\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;由于，即使超平面固定，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;也会随着&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(||\omega||\)&lt;/code&gt;的变化而变化，而我们的目标是固定超平面，所以可以固定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;也可以固定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(||\omega||\)&lt;/code&gt;。为了方便推导，这里选择固定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;。令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{\gamma}=1\)&lt;/code&gt;，则&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{\gamma}=\frac{1}{||\omega||}\)&lt;/code&gt;，所以，优化目标转化为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\max\frac{1}{||\omega||}, s.t. y_i(\omega^Tx_i+b)=\hat{\gamma}_i&amp;gt;=1, i=1,2,...,n
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/maximum margin classifier.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;图中，红色和蓝色两个超平面到中间的超平面的距离都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\tilde{\gamma}\)&lt;/code&gt;，当这个分类间隔最大的时候，这两个超平面上的点都叫做support vector,满足&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y(\omega^Tx+b)=1\)&lt;/code&gt;，而非support vector的点，都有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y(\omega^Tx+b)&amp;gt;1\)&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;2-support-vector&quot;&gt;2. support vector&lt;/h3&gt;

&lt;p&gt;support vector的一个最直接的好处就是计算和存储上的优越性，几百万个点，实际上可能只有一两百个surpport vector，存储这一两百个点拿来做inference之类的就够了。&lt;/p&gt;

&lt;h3 id=&quot;3-kernel&quot;&gt;3. kernel&lt;/h3&gt;

&lt;h3 id=&quot;4-outliers&quot;&gt;4. Outliers&lt;/h3&gt;

&lt;h3 id=&quot;5-numerical-optimization&quot;&gt;5. Numerical Optimization&lt;/h3&gt;

&lt;h3 id=&quot;6-duality&quot;&gt;6. Duality&lt;/h3&gt;

&lt;h3 id=&quot;7-kernel-ii&quot;&gt;7. Kernel II&lt;/h3&gt;

</content>
 </entry>
 
 <entry>
   <title>regularization相关</title>
   <link href="http://daiwk.github.io/posts/ml-regularization.html"/>
   <updated>2016-10-01T00:00:00+00:00</updated>
   <id>/posts/ml-regularization</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#l0范数&quot;&gt;l0范数&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#l1范数&quot;&gt;l1范数&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#稀疏的两个好处&quot;&gt;稀疏的两个好处&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#公式&quot;&gt;公式&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#l2范数&quot;&gt;l2范数&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#概述-1&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#l2的好处&quot;&gt;l2的好处&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#常用图解释&quot;&gt;常用图解释&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#不可导点的处理&quot;&gt;不可导点的处理&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.csdn.net/zouxy09/article/details/24971995&quot;&gt;http://blog.csdn.net/zouxy09/article/details/24971995&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.mamicode.com/info-detail-517504.html&quot;&gt;http://www.mamicode.com/info-detail-517504.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;l0范数&quot;&gt;l0范数&lt;/h2&gt;

&lt;p&gt;L0范数是指向量中非0的元素的个数，如果我们用L0范数来规则化一个参数矩阵W的话，就是希望W的大部分元素都是0，也就是，让参数W是稀疏的。&lt;/p&gt;

&lt;p&gt;为什么不用L0，而要用L1呢？个人理解一是因为L0范数很难优化求解（NP难问题），二是L1范数是L0范数的最优凸近似，而且它比L0范数要容易优化求解。&lt;/p&gt;

&lt;h2 id=&quot;l1范数&quot;&gt;l1范数&lt;/h2&gt;

&lt;h3 id=&quot;概述&quot;&gt;概述&lt;/h3&gt;

&lt;p&gt;l1(也叫Lasso regularization)可以使模型变得更sparse，因为它是L0范数的最优凸近似，任何的规则化算子，如果他在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_i=0\)&lt;/code&gt;的地方不可微，并且可以分解为一个“求和”的形式，那么这个规则化算子就可以实现稀疏。这说是这么说，W的L1范数是绝对值，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(|w|\)&lt;/code&gt;在w=0处是不可微&lt;/p&gt;

&lt;h3 id=&quot;稀疏的两个好处&quot;&gt;稀疏的两个好处&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;特征选择(Feature Selection)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;一般来说，xi的大部分元素（也就是特征）都是和最终的输出yi没有关系或者不提供任何信息的，在最小化目标函数的时候考虑xi这些额外的特征，虽然可以获得更小的训练误差，但在预测新的样本时，这些没用的信息反而会被考虑，从而干扰了对正确yi的预测。稀疏规则化算子的引入就是为了完成特征自动选择的光荣使命，它会学习地去掉这些没有信息的特征，也就是把这些特征对应的权重置为0。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;可解释性(Interpretability)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;例如患某种病的概率是y，然后我们收集到的数据x是1000维的，也就是我们需要寻找这1000种因素到底是怎么影响患上这种病的概率的。假设我们这个是个回归模型：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y=w1*x1+w2*x2+…+w1000*x1000+b\)&lt;/code&gt;（当然了，为了让y限定在[0,1]的范围，一般还得加个Logistic函数）。通过学习，如果最后学习到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(w^*\)&lt;/code&gt;就只有很少的非零元素，例如只有5个非零的wi，那么我们就有理由相信，这些对应的特征在患病分析上面提供的信息是巨大的，决策性的。也就是说，患不患这种病只和这5个因素有关，那医生就好分析多了。但如果1000个wi都非0，医生面对这1000种因素，累觉不爱。&lt;/p&gt;

&lt;h3 id=&quot;公式&quot;&gt;公式&lt;/h3&gt;

&lt;p&gt;假设有n个样本，在原始的cost function加上一项，即所有权重的绝对值的和乘以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\lambda}{n}\)&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
C=C_0+\frac{\lambda }{n}\sum _w|w|
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;梯度如下，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sgn(w)\)&lt;/code&gt;表示w的符号，即，w&amp;gt;0时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sgn(w)\)=1&lt;/code&gt;，w&amp;lt;0时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(sgn(w)\)=-1&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\frac{\partial C}{\partial w}=\frac{\partial C_0}{\partial w}+\frac{\lambda}{n}sgn(w)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;权重更新如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
w=w-\frac{\eta \lambda}{n}sgn(w)-\eta \frac{\partial C_0}{\partial w}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;比原始的更新规则多出了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\eta \lambda}{n}sgn(w)\)&lt;/code&gt;这一项。当&lt;strong&gt;w为正&lt;/strong&gt;时，更新后的&lt;strong&gt;w变小&lt;/strong&gt;。当&lt;strong&gt;w为负&lt;/strong&gt;时，&lt;strong&gt;更新后的w变大&lt;/strong&gt;。因此它的效果就是&lt;strong&gt;让w往0靠&lt;/strong&gt;，使网络中的&lt;strong&gt;权重尽可能为0&lt;/strong&gt;，也就相当于&lt;strong&gt;减小了网络复杂度&lt;/strong&gt;，防止过拟合。&lt;/p&gt;

&lt;p&gt;当w等于0时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(|w|\)&lt;/code&gt;是不可导的，所以我们只能按照原始的未经正则化的方法去更新w，这就相当于去掉&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\frac{\eta \lambda}{n}sgn(w)\)&lt;/code&gt;这一项，所以我们可以规定sgn(0)=0，这样就把w=0的情况也统一进来了。&lt;/p&gt;

&lt;h2 id=&quot;l2范数&quot;&gt;l2范数&lt;/h2&gt;

&lt;h3 id=&quot;概述-1&quot;&gt;概述&lt;/h3&gt;

&lt;p&gt;用上l2的回归也叫“岭回归”（Ridge Regression），能够防止过拟合。让L2范数的规则项&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(||W||_2\)&lt;/code&gt;最小，可以使得W的每个元素都很小，都接近于0，但与L1范数不同，它不会让它等于0，而是接近于0。越小的参数说明模型越简单，越简单的模型则越不容易产生过拟合现象。我也不懂，我的理解是：限制了参数很小，实际上就限制了多项式某些分量的影响很小（看上面线性回归的模型的那个拟合的图），这样就相当于减少参数个数。&lt;/p&gt;

&lt;h3 id=&quot;l2的好处&quot;&gt;l2的好处&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;学习理论的角度：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;从学习理论的角度来说，L2范数可以防止过拟合，提升模型的泛化能力。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;优化计算的角度：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;从优化或者数值计算的角度来说，L2范数有助于处理 condition number不好的情况下矩阵求逆很困难的问题。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/daiwk/dl.ai/master/c2/imgs/l2-improve-condition-number.png&quot; alt=&quot;l2-improve-condition-number.png&quot; /&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/l1l2-pictures.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;优化有两大难题，一是：局部最小值，二是：ill-condition病态问题。&lt;/p&gt;

&lt;p&gt;ill-condition对应的是well-condition。那他们分别代表什么？假设我们有个方程组AX=b，我们需要求解X。如果A或者b稍微的改变，会使得X的解发生很大的改变，那么这个方程组系统就是ill-condition的，反之就是well-condition的。condition number就是拿来衡量ill-condition系统的可信度的。condition number衡量的是输入发生微小变化的时候，输出会发生多大的变化。也就是系统对微小变化的敏感度。如果一个矩阵的condition number在1附近，那么它就是well-conditioned的，如果远大于1，那么它就是ill-conditioned的。&lt;/p&gt;

&lt;p&gt;如果方阵A是非奇异的(行列式不为0，又叫满秩矩阵)，那么A的conditionnumber定义为：&lt;/p&gt;

&lt;p&gt;l2范数如何解决ill-conditioned问题：&lt;/p&gt;

&lt;h2 id=&quot;常用图解释&quot;&gt;常用图解释&lt;/h2&gt;

&lt;p&gt;对于l1，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a|w1| +b|w2| + c=0&lt;/code&gt;，假设a=b=1，c=-1
w1&amp;gt;0 w2&amp;gt;0，就有w1+w2-1=0==&amp;gt;w2=-w1+1，就是图中第一象限的那条直线，
同理w1&amp;gt;0,w2&amp;lt;0，w1-w2-1=0==&amp;gt;w2=w1-1，就是图中第四象限的直线，
同理w1&amp;lt;0,w2&amp;gt;0，-w1+w2-1=0==&amp;gt;w2=w1+1，第二象限
所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a|w1| +b|w2| + c=0&lt;/code&gt;是那个菱形的等高线
假设原来的损失函数是f(w1,w2)，就是图中的椭圆等高线&lt;/p&gt;

&lt;p&gt;f(w1,w2)和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a|w1|+b|w2|+c&lt;/code&gt;最先相交的点 就是w1=0,w2&amp;gt;0的时候，就是图中画的那个y轴上的点
而l2的最先的交点 不用w1取0，就可以相交了&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/l1l2-pictures.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;不可导点的处理&quot;&gt;不可导点的处理&lt;/h2&gt;

&lt;p&gt;使用Proximal Gradient Descent，参考&lt;a href=&quot;https://www.cnblogs.com/breezedeus/p/3426757.html&quot;&gt;https://www.cnblogs.com/breezedeus/p/3426757.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;西瓜书p252-254&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;整个loss变为soft thresholding：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\text{prox}_{\mu g} (z) = \text{sign}(z) \max\{|z| - \mu, \ 0\}
\]&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>pearson相关系数</title>
   <link href="http://daiwk.github.io/posts/ml-pearson-correlation-coefficient.html"/>
   <updated>2016-10-01T00:00:00+00:00</updated>
   <id>/posts/ml-pearson-correlation-coefficient</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#计算公式&quot;&gt;计算公式&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#代码&quot;&gt;代码&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考：
&lt;a href=&quot;https://www.zhihu.com/question/19734616&quot;&gt;https://www.zhihu.com/question/19734616&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;计算公式&quot;&gt;计算公式&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://blog.csdn.net/zhangjunjie789/article/details/51737366&quot;&gt;http://blog.csdn.net/zhangjunjie789/article/details/51737366&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;代码&quot;&gt;代码&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;math&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#a,b两个列表的数据一一对应相乘之后求和
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sum_ab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sum_ab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum_ab&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;cal_pearson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#求x_list、y_list元素之和
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sum_x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sum_y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#求x_list、y_list元素乘积之和
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sum_xy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#求x_list、y_list的平方和
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sum_x2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sum_y2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;molecular&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_xy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;#计算Pearson相关系数，molecular为分子，denominator为分母
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;denominator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_x2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_y2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;molecular&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;denominator&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;皮尔逊相关系数的适用范围：&lt;/p&gt;

&lt;p&gt;当两个变量的标准差都不为零时，相关系数才有定义，皮尔逊相关系数适用于：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;两个变量之间是线性关系，都是连续数据。&lt;/li&gt;
  &lt;li&gt;两个变量的总体是正态分布，或接近正态的单峰分布。&lt;/li&gt;
  &lt;li&gt;两个变量的观测值是成对的，每对观测值之间相互独立。&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>basic usage</title>
   <link href="http://daiwk.github.io/posts/first-test.html"/>
   <updated>2016-09-30T00:00:00+00:00</updated>
   <id>/posts/first-test</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#mathjax基本公式语法&quot;&gt;mathjax基本公式语法&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;mathjax基本公式语法&quot;&gt;mathjax基本公式语法&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://blog.csdn.net/ethmery/article/details/50670297&quot;&gt;http://blog.csdn.net/ethmery/article/details/50670297&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P(E)   = {n \choose k} p^k (1-p)^{ n-k}
\tag{Eq-x}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\Gamma(n) = (n-1)!\quad\forall n\in\mathbb N
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
x = \dfrac{-b \pm \sqrt{b^2 - 4ac}}{2a}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In equation \eqref{eq:sample}, we find the value of an
interesting integral:&lt;/p&gt;

&lt;p&gt;\begin{equation}
\int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15}
\label{eq:sample}
\end{equation}&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Item&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Value&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Qty&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Computer&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1600 USD&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Phone&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;12 USD&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;12&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;Pipe&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1 USD&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;234&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;pre&gt;&lt;code&gt;
@requires_authorization
def somefunc(param1='', param2=0):
    '''A docstring'''
    if param1 &amp;gt; param2: # interesting
        print 'Greater'
    return (param2 - param1 + 1) or None
class SomeClass:
    pass
&amp;gt;&amp;gt;&amp;gt; message = '''interpreter
... prompt'''
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code class=&quot;language-sequence&quot;&gt;Alice-&amp;gt;Bob: Hello Bob, how are you?
Note right of Bob: Bob thinks
Bob--&amp;gt;Alice: I am good thanks!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- [ ] haha&lt;/code&gt;
 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- [x] lalala&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-flow&quot;&gt;st=&amp;gt;start: Start
io=&amp;gt;inputoutput: verification
op=&amp;gt;operation: Your Operation
cond=&amp;gt;condition: Yes or No?
sub=&amp;gt;subroutine: Your Subroutine
e=&amp;gt;end

st-&amp;gt;io-&amp;gt;op-&amp;gt;cond
cond(yes)-&amp;gt;e
cond(no)-&amp;gt;sub-&amp;gt;io
&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 
 <entry>
   <title>视频相关paper</title>
   <link href="http://daiwk.github.io/posts/video-video-papers.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/video-video-papers</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%a7%86%e9%a2%91%e5%88%86%e7%b1%bb&quot;&gt;视频分类&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%a7%86%e9%a2%91%e8%a1%a8%e7%a4%ba%e5%ad%a6%e4%b9%a0&quot;&gt;视频表示学习&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#dense-predictive-coding&quot;&gt;Dense Predictive Coding&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tcc&quot;&gt;TCC&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%ba%94%e7%94%a8&quot;&gt;应用&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;视频分类&quot;&gt;视频分类&lt;/h2&gt;

&lt;p&gt;AAAI 2018&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://iiis.tsinghua.edu.cn/~weblt/papers/multimodal-keyless-attention.pdf&quot;&gt;Multimodal Keyless Attention Fusion for Video Classification&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Multimodal Representation意思是多模式表示，在行为识别任务上，文章采用了视觉特征（Visual Features，包含RGB特征 和 flow features）；声学特征（Acoustic Feature）；前面两个特征都是针对时序，但是时序太长并不适合直接喂到LSTM，所以作者采用了分割的方法（Segment-Level Features），将得到的等长的Segment喂到LSTM。&lt;/p&gt;

&lt;p&gt;优点：多模态信息融合方法优化: 两路model分别lstm+attention,再concat ,能得到多模态 互补识别效果,train model 效果优于early fusion/later fusion&lt;/p&gt;

&lt;p&gt;CVPR 2018&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://cn.arxiv.org/abs/1711.09550&quot;&gt;Attention Clusters: Purely Attention Based Local Feature Integration for Video Classification&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;多组attention参数有效引入diversity,能学习到不同的注意力模式&lt;/p&gt;

&lt;p&gt;不同component用各自attention(通过线性变换映射到另一个空间，相当于加约束),再concat&lt;/p&gt;

&lt;p&gt;temporal action proposal: actionness score（精彩不精彩，二分类），然后取出分布连续且值比较高的片段，然后找边界（boundary aware network），然后加一些多尺度(action pyramid network)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1705.07750v1&quot;&gt;Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;利用膨胀3D卷积网络(I3D)将视频的帧间差值做处理,再采用CNN进行分类。&lt;/p&gt;

&lt;p&gt;AdaIN&lt;/p&gt;

&lt;p&gt;style transfer: AdaIN(自适应示例正则化)，两个目标：纹理一致性+语义一致性（类似GAN）&lt;/p&gt;

&lt;h2 id=&quot;视频表示学习&quot;&gt;视频表示学习&lt;/h2&gt;

&lt;h3 id=&quot;dense-predictive-coding&quot;&gt;Dense Predictive Coding&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.04656&quot;&gt;Video Representation Learning by Dense Predictive Coding&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;近期来自 VGG 的高质量工作，因为没有在主会议发表所以没有引起大范围关注，但保持了一贯低调又实用的风格。本文提出了一种新型的自监督学习（self-supervised learning）方法 Dense Predictive Coding，学习视频的时空表征，在动作识别任务（UCF101 和 HMDB51 数据集）上获得了 state-of-the-art 的正确率，并且用无需标注的自监督学习方法在视频动作识别上达到了 ImageNet 预训练的正确率。&lt;/p&gt;

&lt;p&gt;自监督学习是利用无标注的数据设计代理任务（proxy task），使网络从中学到有意义的数据表征。本文设计的代理任务是预测未来几秒的视频的特征，并且用对比损失（contrastive loss）使得预测的特征和实际的特征相似度高，却不必要完全相等。因为在像素级别（pixel-level）预测未来的帧容易受到大量随机干扰如光照强度、相机移动的影响，而在特征级别（feature-level）做回归（regression）则忽视了未来高层特征的不可预测性（如视频的未来发展存在多种可能）。&lt;/p&gt;

&lt;p&gt;文中的设计促使网络学习高层语义特征，避免了网络拘泥于学习低层特征。作者在不带标注的 Kinetics400 上训练了自监督任务（Dense Predictive Coding），然后在 UCF101 和 HMDB51 上测试了网络所学权重在动作识别上的正确率。&lt;/p&gt;

&lt;p&gt;Dense Predictive Coding 在 UCF101 数据集上获得了 75.7% 的 top1 正确率，超过了使用带标注的 ImageNet 预训练权重所获得的 73.0% 正确率。该研究结果证明了大规模自监督学习在视频分类上的有效性。&lt;/p&gt;

&lt;h3 id=&quot;tcc&quot;&gt;TCC&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/xocs_UI5HzgFZtTPWABRKw&quot;&gt;使用时间循环一致性学习 (TCC) 理解视频内容&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1608.00859&quot;&gt;Temporal Segment Networks: Towards Good Practices for Deep Action Recognition&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://sites.google.com/view/temporal-cycle-consistency/home&quot;&gt;https://sites.google.com/view/temporal-cycle-consistency/home&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/google-research/google-research/tree/master/tcc&quot;&gt;https://github.com/google-research/google-research/tree/master/tcc&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;应用&quot;&gt;应用&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/5snO8CYpc01CCky9WT6SrQ&quot;&gt;用AI实现动画角色的姿势迁移，Adobe等提出新型「木偶动画」&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1910.02060v1.pdf&quot;&gt;Neural Puppet: Generative Layered Cartoon Characters&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>pytorch常用函数</title>
   <link href="http://daiwk.github.io/posts/pytorch-usage.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/pytorch-usage</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%87%aa%e5%8a%a8%e6%b1%82%e5%af%bc&quot;&gt;自动求导&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#torch%e5%b8%b8%e7%94%a8%e6%93%8d%e4%bd%9c&quot;&gt;torch常用操作&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#torchnn&quot;&gt;torch.nn&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#torchnnadaptivelogsoftmaxwithloss&quot;&gt;torch.nn.AdaptiveLogSoftmaxWithLoss&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#torchnnfunctional&quot;&gt;torch.nn.functional&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#torchnnconv2d&quot;&gt;torch.nn.Conv2d&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#torchnnembedding&quot;&gt;torch.nn.Embedding&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#torch&quot;&gt;torch&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#torchaddmm&quot;&gt;torch.addmm&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#torchmm&quot;&gt;torch.mm&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#torchbmm&quot;&gt;torch.bmm&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#torchunsqueeze&quot;&gt;torch.unsqueeze&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#torchtensor&quot;&gt;torch.Tensor&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#view&quot;&gt;view&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;中文教程：&lt;a href=&quot;http://pytorch123.com&quot;&gt;http://pytorch123.com&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;自动求导&quot;&gt;自动求导&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/qjk19940101/article/details/79557204&quot;&gt;https://blog.csdn.net/qjk19940101/article/details/79557204&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#encoding=utf8
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;torch&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;torch.autograd&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; 
&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;## 输出tensor([19.], grad_fn=&amp;lt;AddBackward&amp;gt;)
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;## z=(x+2)^2+3==&amp;gt;dz/dx=2(x+2)
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;## 输出tensor([8.])
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/luckyscarlett/p/10552747.html&quot;&gt;https://www.cnblogs.com/luckyscarlett/p/10552747.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;torch常用操作&quot;&gt;torch常用操作&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/xholes/article/details/81667211&quot;&gt;https://blog.csdn.net/xholes/article/details/81667211&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;torchnn&quot;&gt;torch.nn&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/&quot;&gt;https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;torchnnadaptivelogsoftmaxwithloss&quot;&gt;torch.nn.AdaptiveLogSoftmaxWithLoss&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1609.04309.pdf&quot;&gt;Efficient softmax approximation for GPUs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://pytorch.org/docs/stable/nn.html?highlight=adaptivelogsoftmaxwithloss#torch.nn.AdaptiveLogSoftmaxWithLoss&quot;&gt;https://pytorch.org/docs/stable/nn.html?highlight=adaptivelogsoftmaxwithloss#torch.nn.AdaptiveLogSoftmaxWithLoss&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;torchnnfunctional&quot;&gt;torch.nn.functional&lt;/h3&gt;

&lt;h3 id=&quot;torchnnconv2d&quot;&gt;torch.nn.Conv2d&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;in_channels(int) – 输入信号的通道&lt;/li&gt;
  &lt;li&gt;out_channels(int) – 卷积产生的通道&lt;/li&gt;
  &lt;li&gt;kerner_size(int or tuple) - 卷积核的尺寸&lt;/li&gt;
  &lt;li&gt;stride(int or tuple, optional) - 卷积步长&lt;/li&gt;
  &lt;li&gt;padding(int or tuple, optional) - 输入的每一条边补充0的层数&lt;/li&gt;
  &lt;li&gt;dilation(int or tuple, optional) – 卷积核元素之间的间距（空洞卷积时使用）&lt;/li&gt;
  &lt;li&gt;groups(int, optional) – 从输入通道到输出通道的阻塞连接数&lt;/li&gt;
  &lt;li&gt;bias(bool, optional) - 如果bias=True，添加偏置&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;正常的卷积如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输入图片的shape是(height, width, in_channels)，&lt;/li&gt;
  &lt;li&gt;filter的shape是(height_f, width_f, in_channels)，&lt;strong&gt;filter和输入的第三维必须相等&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;对于一个filter而言，输出的图片是(height_o, width_o)，&lt;strong&gt;注意，并没有第三维！！&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;所谓的outchannels就是filter的个数，所以输出是(height_o, width_o, out_channels)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;卷积操作就是，将这个三维的filter（例如3x3x3）与输入图像的对应位置相乘，再将这27个数相加，得到的结果就是output的一个元素。&lt;/p&gt;

&lt;p&gt;对于复杂的卷积，假设输入的尺度是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((N, C_{in},H,W)\)&lt;/code&gt;，输出尺度&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((N,C_out,H_out,W_out)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
out(N_i, C_{out_j})=bias(C_{out_j})+\sum^{C_{in}-1}_{k=0}weight(C_{out_j},k)\bigotimes input(N_i,k)
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\bigotimes\)&lt;/code&gt;: 表示二维的相关系数计算 stride: 控制相关系数的计算步长&lt;/li&gt;
  &lt;li&gt;dilation(空洞卷积): 用于控制内核点之间的距离，详细描述在&lt;a href=&quot;https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md&quot;&gt;https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;groups: 控制输入和输出之间的连接：
    &lt;ul&gt;
      &lt;li&gt;group=1，输出是所有的输入的卷积；&lt;/li&gt;
      &lt;li&gt;group=2，此时相当于有并排的两个卷积层，每个卷积层计算输入通道的一半，并且产生的输出是输出通道的一半，随后将这两个输出连接起来。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参数kernel_size，stride,padding，dilation也可以是一个int的数据，此时卷积height和width值相同;也可以是一个tuple数组，tuple的第一维度表示height的数值，tuple的第二维度表示width的数值&lt;/p&gt;

&lt;p&gt;shape：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;input: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((N,C_{in},H_{in},W_{in})\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;output: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((N,C_{out},H_{out},W_{out})\)&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H_{out}=floor((H_{in}+2padding[0]-dilation[0](kernerl\_size[0]-1)-1)/stride[0]+1)\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{out}=floor((W_{in}+2padding[1]-dilation[1](kernerl\_size[1]-1)-1)/stride[1]+1)\)&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;torchnnmaxpool2d&quot;&gt;torch.nn.MaxPool2d&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;pooling的基本思想，与卷积的运算基本一样，区别在于，filter和原图像做的不是卷积操作，而是对原图像的filter大小的区域做max/min之类的操作，而且filter并没有第三维，输入和输出的第三维是一样的。&lt;/p&gt;

&lt;p&gt;针对pooling，并没有需要学习的参数。&lt;/p&gt;

&lt;p&gt;如果输入的大小是(N,C,H,W)，那么输出的大小是(N,C,H_out,W_out)和池化窗口大小(kH,kW)的关系是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
out(N_i, C_j,k)=\max^{kH-1}_{m=0}\max^{kW-1}_{m=0}input(N_{i},C_j,stride[0]h+m,stride[1]w+n)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果padding不是0，会在输入的每一边添加相应数目0&lt;/p&gt;

&lt;p&gt;dilation用于控制内核点之间的距离，详细描述在&lt;a href=&quot;https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md&quot;&gt;https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参数kernel_size，stride, padding，dilation数据类型： 可以是一个int类型的数据，此时卷积height和width值相同; 也可以是一个tuple数组（包含来两个int类型的数据），第一个int数据表示height的数值，tuple的第二个int类型的数据表示width的数值&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;kernel_size(int or tuple) - max pooling的窗口大小&lt;/li&gt;
  &lt;li&gt;stride(int or tuple, optional) - max pooling的窗口移动的步长。&lt;strong&gt;!!!!默认值是kernel_size!!!!!!&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;padding(int or tuple, optional) - 输入的每一条边补充0的层数&lt;/li&gt;
  &lt;li&gt;dilation(int or tuple, optional) – 一个控制窗口中元素步幅的参数&lt;/li&gt;
  &lt;li&gt;return_indices - 如果等于True，会返回输出最大值的序号，对于上采样操作会有帮助&lt;/li&gt;
  &lt;li&gt;ceil_mode - 如果等于True，计算输出信号大小的时候，会使用向上取整，代替默认的向下取整的操作&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;shape:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输入: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((N,C,H_{in},W_{in})\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;输出: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((N,C,H_{out},W_{out})\)&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(H_{out}=floor((H_{in} + 2padding[0] - dilation[0](kernel\_size[0] - 1) - 1)/stride[0] + 1\)&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(W_{out}=floor((W_{in} + 2padding[1] - dilation[1](kernel\_size[1] - 1) - 1)/stride[1] + 1\)&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;torchnnembedding&quot;&gt;torch.nn.Embedding&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/#sparse-layers&quot;&gt;https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/#sparse-layers&lt;/a&gt;。一个保存了固定字典和大小的简单查找表。这个模块常用来保存词嵌入和用下标检索它们。模块的输入是一个下标的列表，输出是对应的词嵌入。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2, scale_grad_by_freq=False, sparse=False)&lt;/code&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;num_embeddings：嵌入字典的大小。&lt;/li&gt;
  &lt;li&gt;embedding_dim：每个嵌入向量的大小。&lt;/li&gt;
  &lt;li&gt;padding_idx：如果提供的话，输出遇到此下标时用零填充。&lt;/li&gt;
  &lt;li&gt;max_norm：如果提供的话，会重新归一化词嵌入，使它们的范数小于提供的值。&lt;/li&gt;
  &lt;li&gt;norm_type：对于max_norm选项计算p范数时的p。&lt;/li&gt;
  &lt;li&gt;scale_grad_by_freq：如果提供的话，会根据字典中单词频率缩放梯度。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;torch&quot;&gt;torch&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch/&quot;&gt;https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;torchaddmm&quot;&gt;torch.addmm&lt;/h3&gt;

&lt;p&gt;对矩阵mat1和mat2进行矩阵乘操作(用@表示)。矩阵mat加到最终结果。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;out=(beta∗M)+(alpha∗mat1@mat2)&lt;/code&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;torch.addmm(beta=1, mat, alpha=1, mat1, mat2, out=None)&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;torchmm&quot;&gt;torch.mm&lt;/h3&gt;

&lt;p&gt;对矩阵mat1和mat2进行相乘。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;torch.mm(mat1, mat2, out=None)&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;torchbmm&quot;&gt;torch.bmm&lt;/h3&gt;

&lt;p&gt;对存储在两个批batch1和batch2内的矩阵进行批矩阵乘操作。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;torch.bmm(batch1, batch2, out=None)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bmm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;torchunsqueeze&quot;&gt;torch.unsqueeze&lt;/h3&gt;

&lt;p&gt;返回一个新的张量，对输入的指定位置插入维度1。注意： 返回张量与输入张量共享内存，所以改变其中一个的内容会改变另一个。&lt;/p&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;5.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;7.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;8.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;9.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unsqueeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;3.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;7.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;8.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;9.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;torchtensor&quot;&gt;torch.Tensor&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/&quot;&gt;https://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;view&quot;&gt;view&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view(*args)&lt;/code&gt;: 返回一个有相同数据但大小不同的tensor。 返回的tensor必须有与原tensor相同的数据和相同数目的元素，但可以有不同的大小。(类似reshape)&lt;/p&gt;

&lt;h3 id=&quot;masked_fill_&quot;&gt;masked_fill_&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;masked_fill_(mask, value)&lt;/code&gt;: 在mask值为1的位置处用value填充。mask的元素个数需和本tensor相同，但尺寸可以不同。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>paddle源码</title>
   <link href="http://daiwk.github.io/posts/platform-paddle-code.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/platform-paddle-code</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#paddle源码&quot;&gt;paddle源码&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;paddle源码&quot;&gt;paddle源码&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/wiki/Paddle%E7%9B%AE%E5%89%8D%E7%9A%84%E5%AE%9E%E7%8E%B0%E7%9A%84%E5%90%84%E4%B8%AA%E6%A8%A1%E5%9D%97%E7%BB%86%E8%8A%82&quot;&gt;Paddle目前的实现的各个模块细节&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/tree/develop/doc&quot;&gt;paddle文档&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/Paddle/tree/develop/doc/design&quot;&gt;paddle设计文档&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>高并发架构设计</title>
   <link href="http://daiwk.github.io/posts/platform-high-concurrency.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/platform-high-concurrency</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/WSocbTKWYBqOSfimdfm_JA&quot;&gt;每秒100W请求，12306秒杀业务，架构如何优化？&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/ohwHg3ZQ42F_sAav1rXlyA&quot;&gt;同样是高并发，QQ/微博/12306的架构难度一样吗？&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>深度学习架构、硬件、部署等</title>
   <link href="http://daiwk.github.io/posts/platform-dl-architectures.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/platform-dl-architectures</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/37QdgXCJKUc6jEoqTDh-Rg&quot;&gt;「破局」AI规模化落地，英特尔至强的七重助力&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/VL620AYMGz1tnOipT7Ty8A&quot;&gt;多硬件支持、轻量化部署等，百度Paddle Lite特性全解读&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>cache</title>
   <link href="http://daiwk.github.io/posts/platform-cache.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/platform-cache</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#transient&quot;&gt;transient&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;transient&quot;&gt;transient&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/chenpi/p/6185773.html&quot;&gt;https://www.cnblogs.com/chenpi/p/6185773.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;先解释下Java中的对象序列化
在讨论transient之前，有必要先搞清楚Java中序列化的含义；&lt;/p&gt;

&lt;p&gt;Java中对象的序列化指的是&lt;strong&gt;将对象转换成以字节序列的形式&lt;/strong&gt;来表示，这些字节序列包含了对象的数据和信息，一个序列化后的对象可以被写到数据库或文件中，也可用于网络传输，一般当我们使用缓存cache（内存空间不够有可能会本地存储到硬盘）或远程调用rpc（网络传输）的时候，经常需要让我们的实体类实现Serializable接口，目的就是为了让其可序列化。&lt;/p&gt;

&lt;p&gt;当然，序列化后的&lt;strong&gt;最终目的是为了反序列化&lt;/strong&gt;，恢复成原先的Java对象，要不然序列化后干嘛呢，所以序列化后的字节序列都是可以恢复成Java对象的，这个过程就是反序列化。&lt;/p&gt;

&lt;p&gt;Java中transient关键字的作用，简单地说，就是&lt;strong&gt;让某些被修饰的成员属性变量不被序列化&lt;/strong&gt;，这一看好像很好理解，就是不被序列化，那么什么情况下，一个对象的某些字段不需要被序列化呢？如果有如下情况，可以考虑使用关键字transient修饰：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;类中的字段值可以根据其它字段推导出来&lt;/strong&gt;，如一个长方形类有三个属性：长度、宽度、面积（示例而已，一般不会这样设计），那么在序列化的时候，面积这个属性就没必要被序列化了；&lt;/li&gt;
  &lt;li&gt;其它，看具体业务需求吧，哪些字段不想被序列化；&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;PS，HashMap源码中，有个字段是用transient修饰的，，确实没必要对这个modCount字段进行序列化，因为没有意义，modCount主要用于判断HashMap是否被修改（像put、remove操作的时候，modCount都会自增），对于这种变量，一开始可以为任何值，0当然也是可以（new出来、反序列化出来、或者克隆clone出来的时候都是为0的），没必要持久化其值。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>时间序列预测算法</title>
   <link href="http://daiwk.github.io/posts/other-time-series.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/other-time-series</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#holt-winters&quot;&gt;holt winters&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#一次指数平滑&quot;&gt;一次指数平滑&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#二次指数平滑&quot;&gt;二次指数平滑&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#三次指数平滑&quot;&gt;三次指数平滑&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tensorflow&quot;&gt;tensorflow&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#keras&quot;&gt;keras&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#时间序列建模相关&quot;&gt;时间序列建模相关&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.zhihu.com/question/21229371&quot;&gt;https://www.zhihu.com/question/21229371&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.analyticsvidhya.com/blog/2018/02/time-series-forecasting-methods/&quot;&gt;https://www.analyticsvidhya.com/blog/2018/02/time-series-forecasting-methods/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;比较流行的还有holt winters、arima等，在statsmodels这个lib里就有：&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.jianshu.com/p/2c607fe926f0&quot;&gt;https://www.jianshu.com/p/2c607fe926f0&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;holt-winters&quot;&gt;holt winters&lt;/h2&gt;

&lt;h3 id=&quot;一次指数平滑&quot;&gt;一次指数平滑&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;statsmodels.tsa.holtwinters&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SimpleExpSmoothing&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ets1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SimpleExpSmoothing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ets1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pred1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'origin'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'fitted'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fittedvalues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'pred'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pred1&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;二次指数平滑&quot;&gt;二次指数平滑&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;statsmodels.tsa.holtwinters&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Holt&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ets2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Holt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;r2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ets2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pred2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'origin'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'fitted'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fittedvalues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'pred'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pred2&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;三次指数平滑&quot;&gt;三次指数平滑&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;statsmodels.tsa.holtwinters&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ExponentialSmoothing&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;x3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Series&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ets3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ExponentialSmoothing&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'add'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seasonal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'add'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seasonal_periods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ets3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pred3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'origin'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'fitted'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fittedvalues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'pred'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pred3&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;tensorflow&quot;&gt;tensorflow&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/contrib/timeseries&quot;&gt;https://www.tensorflow.org/api_docs/python/tf/contrib/timeseries&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;keras&quot;&gt;keras&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://machinelearningmastery.com/time-series-forecasting-long-short-term-memory-network-python/&quot;&gt;https://machinelearningmastery.com/time-series-forecasting-long-short-term-memory-network-python/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;时间序列建模相关&quot;&gt;时间序列建模相关&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://bbs.cvmart.net/articles/526/zi-yuan-shi-xu-xing-wei-jian-ce-xiang-guan-zi-yuan-lie-biao?from=timeline&amp;amp;isappinstalled=0&quot;&gt;【资源】时序行为检测相关资源列表&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>thoughts</title>
   <link href="http://daiwk.github.io/posts/other-thoughts.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/other-thoughts</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#nature%e6%96%87%e7%ab%a0&quot;&gt;nature文章&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU5OTI0NTc3Mg==&amp;amp;mid=2247487438&amp;amp;idx=1&amp;amp;sn=86671a6ce9a6658e8885e13bcc44e37c&amp;amp;chksm=feb6996dc9c1107ba519e52e6911722ca2c2fb0c5c7644cb5cb16ab72106047e729b88363e98&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1565158831453&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;从《华为的冬天》到AI的冬天 | 甲子光年&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/4wR-wWGIgaQTj4LCBg-EbA&quot;&gt;中国搜索引擎往事&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/t0B08Fu2cGygEh9Hn8SwXg&quot;&gt;抖音总裁张楠：抖音的美好与价值&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/hDF-ObBFpR50KogIgPCtRg&quot;&gt;万万没想到，未得图灵奖的LSTM之父Jürgen，30年前做了这么多DL前沿研究&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/4AL8v3shAjfwoLIGl33lVg&quot;&gt;2万字超长干货论文，MIT人工智能实验室：如何做研究？&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/ExIUslFyu9xDn1UH77sTrQ&quot;&gt;Github中文项目排行榜，你永远想不到开发者都用它干了什么&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/kon9chunkit/GitHub-Chinese-Top-Charts&quot;&gt;https://github.com/kon9chunkit/GitHub-Chinese-Top-Charts&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/3ioUTLGpyylDny2u_o6g3w&quot;&gt;面对谷歌，Facebook 是如何赢得「社交保卫战」的？ | 36氪领读&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/cE1P2tWXAtoZ_6zKsBE0BQ&quot;&gt;汤晓鸥纵论商汤AR：AI驱动发力已超3年，落地多个场景&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://36kr.com/p/5237300&quot;&gt;字节跳动的敌人只有时间&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;nature文章&quot;&gt;nature文章&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.nature.com/articles/d41586-019-03013-5&quot;&gt;https://www.nature.com/articles/d41586-019-03013-5&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/7emoVh1yjiEqFtCezBgSmg&quot;&gt;Nature发文：深度学习系统为什么这么好骗？&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>scrapy安装</title>
   <link href="http://daiwk.github.io/posts/other-scrapy.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/other-scrapy</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#安装ssl&quot;&gt;安装ssl&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#安装scrapy&quot;&gt;安装scrapy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#demo&quot;&gt;demo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;安装ssl&quot;&gt;安装ssl&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;apt-get install libssl-dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;安装scrapy&quot;&gt;安装scrapy&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip install scrapy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;demo&quot;&gt;demo&lt;/h2&gt;

&lt;p&gt;有一个lpmining的demo可以参考：
&lt;a href=&quot;https://github.com/daiwk/lp_mining&quot;&gt;https://github.com/daiwk/lp_mining&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>others</title>
   <link href="http://daiwk.github.io/posts/other-others.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/other-others</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%ae%97%e6%b3%95&quot;&gt;算法&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#c&quot;&gt;c++&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0&quot;&gt;机器学习&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0&quot;&gt;深度学习&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/imhuay/Algorithm_Interview_Notes-Chinese&quot;&gt;https://github.com/imhuay/Algorithm_Interview_Notes-Chinese&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;算法&quot;&gt;算法&lt;/h2&gt;

&lt;h2 id=&quot;c&quot;&gt;c++&lt;/h2&gt;

&lt;h2 id=&quot;机器学习&quot;&gt;机器学习&lt;/h2&gt;

&lt;p&gt;网盘链接&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/q361239731/article/details/81012904&quot;&gt;https://blog.csdn.net/q361239731/article/details/81012904&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;周志华机器学习+统计学习基础PDF
链接: https://pan.baidu.com/s/1xIl6Zz1sd6F92EByNUCoCg 密码: 5c47
林轩田机器学习基石&amp;amp;机器学习技法（带字幕）
链接: https://pan.baidu.com/s/1PhKT9TWM3kBSNQcV3H2JXA 提取码: 3j2j
吴恩达-机器学习-斯坦福大学课堂（带字幕，黄海广翻译）
链接: https://pan.baidu.com/s/1RJXPPWJKSdxe0xfz0WDtdg 密码: zi6t
网易云链接：https://study.163.com/course/courseMain.htm?courseId=1004570029
吴恩达-深度学习（带中文字幕）
网易云链接：https://mooc.study.163.com/smartSpec/detail/1001319001.htm
徐亦达-机器学习（开源）
链接: https://pan.baidu.com/s/1M1reV0pIcbVwTcXKq7qZbw 密码: mw3i
自然语言处理NLP&amp;amp;机器学习导论（中科院宗成庆&amp;amp;哈工大关毅&amp;amp;上交大张志华）
链接: https://pan.baidu.com/s/1xF7iK0Zk_9tLeiUr3g7TpA 密码: v8yk&lt;/p&gt;

&lt;p&gt;kkt:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/liaohuiqiang/p/7805954.html&quot;&gt;https://www.cnblogs.com/liaohuiqiang/p/7805954.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;拉格朗日乘子法：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/lijil168/article/details/69395023&quot;&gt;https://blog.csdn.net/lijil168/article/details/69395023&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;gbdt:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.jianshu.com/p/0e5ccc88d2cb&quot;&gt;https://www.jianshu.com/p/0e5ccc88d2cb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;xgboost:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.jianshu.com/p/7467e616f227&quot;&gt;https://www.jianshu.com/p/7467e616f227&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;hmm crf:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.scofield7419.xyz/&quot;&gt;http://www.scofield7419.xyz/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;fm:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/itplus/article/details/40534885&quot;&gt;https://blog.csdn.net/itplus/article/details/40534885&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;w2v:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/itplus/article/details/37969519&quot;&gt;https://blog.csdn.net/itplus/article/details/37969519&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;L1/L2/最小二乘：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/20447622&quot;&gt;https://www.zhihu.com/question/20447622&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;proximal&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://yq.aliyun.com/ziliao/408453&quot;&gt;https://yq.aliyun.com/ziliao/408453&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;深度学习&quot;&gt;深度学习&lt;/h2&gt;

&lt;p&gt;lstm vanishing&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://weberna.github.io/blog/2017/11/15/LSTM-Vanishing-Gradients.html&quot;&gt;https://weberna.github.io/blog/2017/11/15/LSTM-Vanishing-Gradients.html&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>k8s常用命令</title>
   <link href="http://daiwk.github.io/posts/other-k8s-cmds.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/other-k8s-cmds</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#cluster-info&quot;&gt;cluster-info&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#get&quot;&gt;get&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#get-deployments&quot;&gt;get deployments&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#get-nodes&quot;&gt;get nodes&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#get-replicasets&quot;&gt;get replicasets&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#get-pods&quot;&gt;get pods&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#get-pod-detail&quot;&gt;get pod detail&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#get-rc&quot;&gt;get rc&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#get-services&quot;&gt;get services&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#get-service-detail&quot;&gt;get service detail&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#delete&quot;&gt;delete&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#delete-pod&quot;&gt;delete pod&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#delete-service&quot;&gt;delete service&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#delete-deployment&quot;&gt;delete deployment&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#describe&quot;&gt;describe&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#logs&quot;&gt;logs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#exec&quot;&gt;exec&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#demo&quot;&gt;demo&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AAdeployment&quot;&gt;新建一个deployment&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E8%87%AA%E5%8A%A8%E6%89%A9%E5%AE%B9&quot;&gt;自动扩容&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%9A%B4%E9%9C%B2%E7%AB%AF%E5%8F%A3&quot;&gt;暴露端口&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%B8%A6label%E5%9C%B0%E6%9F%A5%E8%AF%A2&quot;&gt;带label地查询&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pod%E7%9A%84%E8%87%AA%E5%8A%A8%E6%81%A2%E5%A4%8D&quot;&gt;pod的自动恢复&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pod%E7%9A%84%E8%87%AA%E5%8A%A8%E5%88%86%E9%85%8D&quot;&gt;pod的自动分配&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E6%9E%84%E5%BB%BAnamespace&quot;&gt;构建namespace&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E4%BB%8Eyaml%E6%96%87%E4%BB%B6%E5%87%BA%E5%8F%91&quot;&gt;从yaml文件出发&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E8%87%AA%E5%8A%A8%E6%89%A9%E5%AE%B9-1&quot;&gt;自动扩容&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E7%89%88%E6%9C%AC%E6%9B%B4%E6%96%B0&quot;&gt;版本更新&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%B0%8F%E6%B5%81%E9%87%8F&quot;&gt;小流量&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#abtest&quot;&gt;abtest&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#configmap&quot;&gt;configmap&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#secret&quot;&gt;secret&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;cluster-info&quot;&gt;cluster-info&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl cluster-info
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Kubernetes master is running at https://xxx.xx.xx.xxx:6443
Heapster is running at https://xxx.xx.xx.xxx:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://xxx.xx.xx.xxx:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
monitoring-influxdb is running at https://xxx.xx.xx.xxx:6443/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy

To further debug and diagnose cluster problems, use &lt;span class=&quot;s1&quot;&gt;'kubectl cluster-info dump'&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;get&quot;&gt;get&lt;/h2&gt;

&lt;h3 id=&quot;get-deployments&quot;&gt;get deployments&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get deployments
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1         1         1            1           2m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;get-nodes&quot;&gt;get nodes&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get nodes &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; wide
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;NAME            STATUS    ROLES     AGE       VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION              CONTAINER-RUNTIME
192.168.0.142   Ready     &amp;lt;none&amp;gt;    15h       v1.11.5   192.168.0.142   &amp;lt;none&amp;gt;        CentOS Linux 7 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Core&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;   3.10.0-957.1.3.el7.x86_64   docker://18.9.2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;get-replicasets&quot;&gt;get replicasets&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get replicasets &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; wide 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get replicasets &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; wide 
NAME                             DESIRED   CURRENT   READY     AGE       CONTAINERS            IMAGES                                   SELECTOR
kubernetes-bootcamp-8465764b6b   1         1         1         4m        kubernetes-bootcamp   hub.baidubce.com/bootcamp/mynode:1.0.0   pod-template-hash&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4021320626,run&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;kubernetes-bootcamp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;get-pods&quot;&gt;get pods&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pods &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; wide
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pods &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; wide
NAME                                   READY     STATUS    RESTARTS   AGE       IP            NODE            NOMINATED NODE
kubernetes-bootcamp-8465764b6b-8555s   1/1       Running   0          3m        172.19.0.29   192.168.0.142   &amp;lt;none&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;命名：deployment名-replicaset名-pod名&lt;/p&gt;

&lt;h4 id=&quot;get-pod-detail&quot;&gt;get pod detail&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pod xxxxx &lt;span class=&quot;nt&quot;&gt;-template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;p&gt;？？？&lt;/p&gt;

&lt;h3 id=&quot;get-rc&quot;&gt;get rc&lt;/h3&gt;

&lt;p&gt;获取replication controllers信息&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get rc &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; wide
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;p&gt;？？？&lt;/p&gt;

&lt;h3 id=&quot;get-services&quot;&gt;get services&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get services
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;S&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;   AGE
kubernetes   ClusterIP   172.16.0.1   &amp;lt;none&amp;gt;        443/TCP   15h
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;get-service-detail&quot;&gt;get service detail&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get service  kubernetes &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;apiVersion&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;v1&quot;&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;kind&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;Service&quot;&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;metadata&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&quot;creationTimestamp&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;2019-02-25T00:52:55Z&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;labels&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s2&quot;&gt;&quot;component&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;apiserver&quot;&lt;/span&gt;,
            &lt;span class=&quot;s2&quot;&gt;&quot;provider&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;kubernetes&quot;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;name&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;kubernetes&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;namespace&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;default&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;resourceVersion&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;44&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;selfLink&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;/api/v1/namespaces/default/services/kubernetes&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;uid&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;xxxxx-xxx-xxxxxx&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;spec&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&quot;clusterIP&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;172.16.0.1&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;ports&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;s2&quot;&gt;&quot;name&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;https&quot;&lt;/span&gt;,
                &lt;span class=&quot;s2&quot;&gt;&quot;port&quot;&lt;/span&gt;: 443,
                &lt;span class=&quot;s2&quot;&gt;&quot;protocol&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;TCP&quot;&lt;/span&gt;,
                &lt;span class=&quot;s2&quot;&gt;&quot;targetPort&quot;&lt;/span&gt;: 6443
            &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;sessionAffinity&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;None&quot;&lt;/span&gt;,
        &lt;span class=&quot;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;ClusterIP&quot;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
    &lt;span class=&quot;s2&quot;&gt;&quot;status&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&quot;loadBalancer&quot;&lt;/span&gt;: &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;delete&quot;&gt;delete&lt;/h2&gt;

&lt;h3 id=&quot;delete-pod&quot;&gt;delete pod&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl delete pod &amp;lt;podname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;delete-service&quot;&gt;delete service&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl delete service &amp;lt;instancename&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;delete-deployment&quot;&gt;delete deployment&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl delete deployment &amp;lt;deploymentname&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;describe&quot;&gt;describe&lt;/h2&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl describe service kubernetes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Name:              kubernetes
Namespace:         default
Labels:            &lt;span class=&quot;nv&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;apiserver
                   &lt;span class=&quot;nv&quot;&gt;provider&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;kubernetes
Annotations:       &amp;lt;none&amp;gt;
Selector:          &amp;lt;none&amp;gt;
Type:              ClusterIP
IP:                172.16.0.1
Port:              https  443/TCP
TargetPort:        6443/TCP
Endpoints:         xxx.xx.xx.127:6443,xxx.xx.xx.128:6443,xxx.xx.xx.129:6443
Session Affinity:  None
Events:            &amp;lt;none&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;logs&quot;&gt;logs&lt;/h2&gt;

&lt;p&gt;显示一个pod中的container的log&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl logs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c&quot;&gt;# Return snapshot logs from pod nginx with only one container&lt;/span&gt;
  kubectl logs nginx
  
  &lt;span class=&quot;c&quot;&gt;# Return snapshot logs from pod nginx with multi containers&lt;/span&gt;
  kubectl logs nginx &lt;span class=&quot;nt&quot;&gt;--all-containers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
  
  &lt;span class=&quot;c&quot;&gt;# Return snapshot logs from all containers in pods defined by label app=nginx&lt;/span&gt;
  kubectl logs &lt;span class=&quot;nt&quot;&gt;-lapp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nginx &lt;span class=&quot;nt&quot;&gt;--all-containers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
  
  &lt;span class=&quot;c&quot;&gt;# Return snapshot of previous terminated ruby container logs from pod web-1&lt;/span&gt;
  kubectl logs &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; ruby web-1
  
  &lt;span class=&quot;c&quot;&gt;# Begin streaming the logs of the ruby container in pod web-1&lt;/span&gt;
  kubectl logs &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; ruby web-1
  
  &lt;span class=&quot;c&quot;&gt;# Display only the most recent 20 lines of output in pod nginx&lt;/span&gt;
  kubectl logs &lt;span class=&quot;nt&quot;&gt;--tail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;20 nginx
  
  &lt;span class=&quot;c&quot;&gt;# Show all logs from pod nginx written in the last hour&lt;/span&gt;
  kubectl logs &lt;span class=&quot;nt&quot;&gt;--since&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1h nginx
  
  &lt;span class=&quot;c&quot;&gt;# Return snapshot logs from first container of a job named hello&lt;/span&gt;
  kubectl logs job/hello
  
  &lt;span class=&quot;c&quot;&gt;# Return snapshot logs from container nginx-1 of a deployment named nginx&lt;/span&gt;
  kubectl logs deployment/nginx &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; nginx-1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;exec&quot;&gt;exec&lt;/h2&gt;

&lt;p&gt;在一个pod的一个container中执行一句命令&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c&quot;&gt;# Get output from running 'date' from pod 123456-7890, using the first container by default&lt;/span&gt;
  kubectl &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;123456-7890 &lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt;
  
  &lt;span class=&quot;c&quot;&gt;# Get output from running 'date' in ruby-container from pod 123456-7890&lt;/span&gt;
  kubectl &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;123456-7890 &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; ruby-container &lt;span class=&quot;nb&quot;&gt;date&lt;/span&gt;
  
  &lt;span class=&quot;c&quot;&gt;# Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-7890&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# and sends stdout/stderr from 'bash' back to the client&lt;/span&gt;
  kubectl &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;123456-7890 &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; ruby-container &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; bash &lt;span class=&quot;nt&quot;&gt;-il&lt;/span&gt;
  
  &lt;span class=&quot;c&quot;&gt;# List contents of /usr from the first container of pod 123456-7890 and sort by modification time.&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# If the command you want to execute in the pod has any flags in common (e.g. -i),&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# you must use two dashes (--) to separate your command's flags/arguments.&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# Also note, do not surround your command and its flags/arguments with quotes&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# unless that is how you would execute it normally (i.e., do ls -t /usr, not &quot;ls -t /usr&quot;).&lt;/span&gt;
  kubectl &lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;123456-7890 &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; /usr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;demo&quot;&gt;demo&lt;/h2&gt;

&lt;h3 id=&quot;新建一个deployment&quot;&gt;新建一个deployment&lt;/h3&gt;

&lt;p&gt;跑一个deployment：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl run kubernetes-demo-daiwk &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxx/mynode_daiwenkai:1.0.0 &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看一下deployment：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get deployments
NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-demo-daiwk   1         1         1            0           8s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看下pod，可见这个deployment里有1个pod，如果我们设置replicas为3，那应该有3个pods&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pods
NAME                                     READY     STATUS    RESTARTS   AGE
kubernetes-demo-daiwk-6549bc9c4b-d6m6r   1/1       Running   0          12m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动本地proxy：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl proxy &amp;amp;
Starting to serve on 127.0.0.1:8001
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后记录一下podname&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;POD_NAME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;kubectl get pods &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; go-template &lt;span class=&quot;nt&quot;&gt;--template&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$POD_NAME&lt;/span&gt;
kubernetes-demo-daiwk-6549bc9c4b-d6m6r
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl http://localhost:8001/api/v1/namespaces/default/pods/&lt;span class=&quot;nv&quot;&gt;$POD_NAME&lt;/span&gt;/proxy/
Hello Kubernetes &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; | Running on: kubernetes-demo-daiwk-6549bc9c4b-d6m6r | &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果出现：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Error: &lt;span class=&quot;s1&quot;&gt;'dial tcp 172.19.0.10:8080: connect: connection refused'&lt;/span&gt;
Trying to reach: &lt;span class=&quot;s1&quot;&gt;'http://172.19.0.10:8080/'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;说明第一步的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl run&lt;/code&gt;并没有成功。&lt;/p&gt;

&lt;p&gt;然后看下pod的日志：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl logs &lt;span class=&quot;nv&quot;&gt;$POD_NAME&lt;/span&gt;
Kubernetes Bootcamp App Started At: 2019-02-25T17:59:48.607Z | Running On:  kubernetes-demo-daiwk-6549bc9c4b-d6m6r 

Running On: kubernetes-demo-daiwk-6549bc9c4b-d6m6r | Total Requests: 1 | App Uptime: 10.433 seconds | Log Time: 2019-02-25T17:59:59.040Z
Running On: kubernetes-demo-daiwk-6549bc9c4b-d6m6r | Total Requests: 2 | App Uptime: 160.284 seconds | Log Time: 2019-02-25T18:02:28.891Z
Running On: kubernetes-demo-daiwk-6549bc9c4b-d6m6r | Total Requests: 3 | App Uptime: 379.319 seconds | Log Time: 2019-02-25T18:06:07.926Z
Running On: kubernetes-demo-daiwk-6549bc9c4b-d6m6r | Total Requests: 4 | App Uptime: 491.223 seconds | Log Time: 2019-02-25T18:07:59.830Z
Running On: kubernetes-demo-daiwk-6549bc9c4b-d6m6r | Total Requests: 5 | App Uptime: 670.407 seconds | Log Time: 2019-02-25T18:10:59.014Z
Running On: kubernetes-demo-daiwk-6549bc9c4b-d6m6r | Total Requests: 6 | App Uptime: 852.984 seconds | Log Time: 2019-02-25T18:14:01.591Z
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后执行env：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$POD_NAME&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;env
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
&lt;span class=&quot;nv&quot;&gt;HOSTNAME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;kubernetes-demo-daiwk-6549bc9c4b-d6m6r
&lt;span class=&quot;nv&quot;&gt;KUBERNETES_PORT_443_TCP_PROTO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;tcp
&lt;span class=&quot;nv&quot;&gt;KUBERNETES_PORT_443_TCP_PORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;443
&lt;span class=&quot;nv&quot;&gt;KUBERNETES_PORT_443_TCP_ADDR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;172.16.0.1
&lt;span class=&quot;nv&quot;&gt;KUBERNETES_SERVICE_HOST&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;172.16.0.1
&lt;span class=&quot;nv&quot;&gt;KUBERNETES_SERVICE_PORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;443
&lt;span class=&quot;nv&quot;&gt;KUBERNETES_SERVICE_PORT_HTTPS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;443
&lt;span class=&quot;nv&quot;&gt;KUBERNETES_PORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;tcp://172.16.0.1:443
&lt;span class=&quot;nv&quot;&gt;KUBERNETES_PORT_443_TCP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;tcp://172.16.0.1:443
&lt;span class=&quot;nv&quot;&gt;NODE_VERSION&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;11.10.0
&lt;span class=&quot;nv&quot;&gt;YARN_VERSION&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1.13.0
&lt;span class=&quot;nv&quot;&gt;HOME&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/root
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后就可以对这个pod像docker的container一样来搞啦&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-ti&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$POD_NAME&lt;/span&gt; bash
root@kubernetes-demo-daiwk-6549bc9c4b-d6m6r:/# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;自动扩容&quot;&gt;自动扩容&lt;/h3&gt;

&lt;p&gt;然后可以对我们的deploymen进行扩容：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl scale &lt;span class=&quot;nt&quot;&gt;--replicas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3 deployment/kubernetes-demo-daiwk
deployment.extensions/kubernetes-demo-daiwk scaled
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;暴露端口&quot;&gt;暴露端口&lt;/h3&gt;

&lt;p&gt;然后可以把deploy通过端口再通过NodePort暴露出来，这样外网就可以访问了，否则只有登上k8s集群内部的机器才能访问：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl expose deployment/kubernetes-demo-daiwk &lt;span class=&quot;nt&quot;&gt;--type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;NodePort&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8080
service/kubernetes-demo-daiwk exposed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个时候就可以发现：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get services
NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;S&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          AGE
kubernetes              ClusterIP   172.16.0.1      &amp;lt;none&amp;gt;        443/TCP          17h
kubernetes-demo-daiwk   NodePort    172.16.182.56   &amp;lt;none&amp;gt;        8080:32561/TCP   15m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;类似地，我们还可以把clusterip，loadbalancer暴露出来：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl expose deployment/kubernetes-demo-daiwk &lt;span class=&quot;nt&quot;&gt;--type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;LoadBalancer&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8080
kubectl expose deployment/kubernetes-demo-daiwk &lt;span class=&quot;nt&quot;&gt;--type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ClusterIP&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt; 8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;变成loadbalancer的时候，external-ip就会有一个值：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get service &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwk
NAME                    TYPE           CLUSTER-IP       EXTERNAL-IP     PORT&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;S&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          AGE
daiwk-1                 NodePort       172.16.177.108   &amp;lt;none&amp;gt;          8080:32689/TCP   34m
daiwk-2                 LoadBalancer   172.16.32.199    106.12.43.102   8080:31405/TCP   13m
kubernetes-demo-daiwk   NodePort       172.16.193.53    &amp;lt;none&amp;gt;          8080:31763/TCP   17h
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就可以&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; curl 106.12.43.102:8080
Hello Kubernetes! | Running on: daiwk-2-56d68cc894-mbmhj | &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后describe一下这个service：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl describe services/kubernetes-demo-daiwk
Name:                     kubernetes-demo-daiwk
Namespace:                default
Labels:                   &lt;span class=&quot;nv&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;kubernetes-demo-daiwk
Annotations:              &amp;lt;none&amp;gt;
Selector:                 &lt;span class=&quot;nv&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;kubernetes-demo-daiwk
Type:                     NodePort
IP:                       172.16.182.56
Port:                     &amp;lt;&lt;span class=&quot;nb&quot;&gt;unset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;  8080/TCP
TargetPort:               8080/TCP
NodePort:                 &amp;lt;&lt;span class=&quot;nb&quot;&gt;unset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;  32561/TCP
Endpoints:                172.19.0.19:8080,172.19.0.20:8080,172.19.0.21:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   &amp;lt;none&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后把nodeport存一下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NODE_PORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;kubectl get services/kubernetes-demo-daiwk &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; go-template&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$NODE_PORT&lt;/span&gt;
32561
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;假设我们的vm ip是106.12.xx.xx(怎么来的。。。)&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl 106.12.xx.xx:&lt;span class=&quot;nv&quot;&gt;$NODE_PORT&lt;/span&gt;
Hello Kubernetes! | Running on: kubernetes-demo-daiwk-6549bc9c4b-7tzbs | &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后get pods发现确实scale成了3个：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pods
NAME                                     READY     STATUS    RESTARTS   AGE
kubernetes-demo-daiwk-6549bc9c4b-7tzbs   1/1       Running   0          2m
kubernetes-demo-daiwk-6549bc9c4b-d6m6r   1/1       Running   0          17m
kubernetes-demo-daiwk-6549bc9c4b-hsmkc   1/1       Running   0          2m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;带label地查询&quot;&gt;带label地查询&lt;/h3&gt;

&lt;p&gt;然后我们带条件地去get（加上-l参数）：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pods &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;kubernetes-demo-daiwk
NAME                                     READY     STATUS    RESTARTS   AGE
kubernetes-demo-daiwk-6549bc9c4b-7tzbs   1/1       Running   0          3m
kubernetes-demo-daiwk-6549bc9c4b-d6m6r   1/1       Running   0          19m
kubernetes-demo-daiwk-6549bc9c4b-hsmkc   1/1       Running   0          3m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get services &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;kubernetes-demo-daiwk
NAME                    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;S&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          AGE
kubernetes-demo-daiwk   NodePort   172.16.182.56   &amp;lt;none&amp;gt;        8080:32561/TCP   19m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们加个label：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl label pod kubernetes-demo-daiwk-6549bc9c4b-7tzbs &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x1
kubectl label pod kubernetes-demo-daiwk-6549bc9c4b-d6m6r &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x1 &lt;span class=&quot;nt&quot;&gt;--overwrite&lt;/span&gt;
kubectl label pod kubernetes-demo-daiwk-6549bc9c4b-hsmkc &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x2 &lt;span class=&quot;nt&quot;&gt;--overwrite&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再get一下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pod &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x2
NAME                                     READY     STATUS    RESTARTS   AGE
kubernetes-demo-daiwk-6549bc9c4b-hsmkc   1/1       Running   0          7m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;还有&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pod &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x1
NAME                                     READY     STATUS    RESTARTS   AGE
kubernetes-demo-daiwk-6549bc9c4b-7tzbs   1/1       Running   0          7m
kubernetes-demo-daiwk-6549bc9c4b-d6m6r   1/1       Running   0          23m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;我们来describe一下，可以发现加上了我们指定的label~&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl describe pod kubernetes-demo-daiwk-6549bc9c4b-7tzbs
Name:           kubernetes-demo-daiwk-6549bc9c4b-7tzbs
Namespace:      default
Node:           192.168.0.142/192.168.0.142
Start Time:     Tue, 26 Feb 2019 02:15:32 +0800
Labels:         &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x1
                pod-template-hash&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2105675706
                &lt;span class=&quot;nv&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;kubernetes-demo-daiwk
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;pod的自动恢复&quot;&gt;pod的自动恢复&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl delete pod kubernetes-demo-daiwk-6549bc9c4b-hsmkc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们可以发现自己又拉了一个起来&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pod
NAME                                     READY     STATUS    RESTARTS   AGE
kubernetes-demo-daiwk-6549bc9c4b-7tzbs   1/1       Running   0          10m
kubernetes-demo-daiwk-6549bc9c4b-d6m6r   1/1       Running   0          26m
kubernetes-demo-daiwk-6549bc9c4b-jg7p5   1/1       Running   0          35s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;pod的自动分配&quot;&gt;pod的自动分配&lt;/h3&gt;

&lt;p&gt;2个Node上跑3个pod，1个node跑2个，1个node跑1个，杀掉跑2个pod的node，看pod的变化&lt;/p&gt;

&lt;p&gt;看到还是有3个pod在跑，只不过是新增了两个pod，且跑在剩余的node上&lt;/p&gt;

&lt;p&gt;扩容原则：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;有pod处于pending状态&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;缩容原则：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;node少于50%利用率&lt;/li&gt;
  &lt;li&gt;上面的pod可以被调到其他节点上&lt;/li&gt;
  &lt;li&gt;没有系统pod&lt;/li&gt;
  &lt;li&gt;没有用本地磁盘的pod&lt;/li&gt;
  &lt;li&gt;node没有打上不驱逐的label&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;构建namespace&quot;&gt;构建namespace&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create namespace daiwk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;之后所有操作都带上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-namespace=daiwk&lt;/code&gt;，例如&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl run kubernetes-demo-daiwk &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxx/mynode_daiwenkai:1.0.0 &lt;span class=&quot;nt&quot;&gt;--port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;8080 &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;从yaml文件出发&quot;&gt;从yaml文件出发&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create –f xxx.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;加上参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dry-run&lt;/code&gt;用于测试&lt;/p&gt;

&lt;p&gt;yaml文件示例：&lt;/p&gt;

&lt;p&gt;Namesapce:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Namespace&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwk&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Deployment:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;extensions/v1beta1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;kubernetes-demo-daiwk&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwk&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;stable&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1.0.0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;xxx/mynode_daiwenkai:1.0.0&quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;http&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Service:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode-svc&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwk&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;NodePort&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后呢：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get service &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwk
NAME                    TYPE       CLUSTER-IP       EXTERNAL-IP   PORT&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;S&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          AGEx
mynode-svc              NodePort   172.16.122.209   &amp;lt;none&amp;gt;        8080:31671/TCP   1m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;假设我们的vm ip是106.12.xx.xx，完美~&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl 106.12.xx.xx:31671
Hello Kubernetes &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; | Running on: kubernetes-demo-daiwk-78659d8587-bts5s | &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;kubectl exec可以直接进到容器里，然后可以直接curl servicename:port&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; daiwk-1-748867bfb-h65tr /bin/bash &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwk    
root@daiwk-1-748867bfb-h65tr:/# curl daiwk-1:8080                
Hello Kubernetes bootcamp! | Running on: daiwk-1-748867bfb-h65tr | &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
root@daiwk-1-748867bfb-h65tr:/# curl daiwk-1-748867bfb-h65tr:8080
Hello Kubernetes bootcamp! | Running on: daiwk-1-748867bfb-h65tr | &lt;span class=&quot;nv&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;常见的配置文件处理方式有几种：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;使用env的方式，将配置以环境变量方式注入到容器内&lt;/li&gt;
  &lt;li&gt;使用kubernetes的configmap，将配置文件挂载到容器内/注入到环境变量中&lt;/li&gt;
  &lt;li&gt;同代码一同build到容器内&lt;/li&gt;
  &lt;li&gt;将配置保存到宿主机，通过目录挂载到容器内&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;对于1，需要update/create deployment
对于2，在configmap更新后重启pod即可
对于3的方式比较简单，重新上传容器，rolling update即可
对于4的方式，如果程序支持热加载，不需要做任何操作，但是缺点是损失无状态，且配置修改需要登录到机器上操作&lt;/p&gt;

&lt;p&gt;这几种方式各有优劣，基本上要根据实际情况做分析和选型&lt;/p&gt;

&lt;p&gt;如果使用kubernetes的configmap或secret，kubernetes会自动实施更新，所以如果程序支持热加载配置，也同样可以不需要重启pod&lt;/p&gt;

&lt;p&gt;如果delete了一个deployment，但service还在，那么：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl describe service mypython-svc &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2
Name:                     mypython-svc
Namespace:                daiwenkai-2
Labels:                   &lt;span class=&quot;nv&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;mypython
Annotations:              &amp;lt;none&amp;gt;
Selector:                 &lt;span class=&quot;nv&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;mypython
Type:                     NodePort
IP:                       172.16.72.253
Port:                     &amp;lt;&lt;span class=&quot;nb&quot;&gt;unset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;  8080/TCP
TargetPort:               8080/TCP
NodePort:                 &amp;lt;&lt;span class=&quot;nb&quot;&gt;unset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;  31753/TCP
Endpoints:                &amp;lt;none&amp;gt;
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   &amp;lt;none&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以发现endpoints没东西了，说明这个service死掉了。。那么我们可以再搞个deployment，只要能select到，就又活了&lt;/p&gt;

&lt;h2 id=&quot;自动扩容-1&quot;&gt;自动扩容&lt;/h2&gt;

&lt;p&gt;通过如下Dockerfile进行build并Push一个镜像：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;FROM node:slim
EXPOSE 8080
COPY server.js &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
CMD node server.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中的server.js如下：&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;http&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;http&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;requests&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;podname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;HOSTNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;startTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;handleRequest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// run loop for increasing usage of cpu&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setHeader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Content-Type&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;text/plain&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;writeHead&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Hello Kubernetes bootcamp! | Running on: &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; | v=3&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Running On:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;| Total Requests:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;requests&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;| App Uptime:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;startTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;seconds&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;| Log Time:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;www&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;http&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;createServer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;handleRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;www&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;listen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8080&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;startTime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;host&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;HOSTNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Kubernetes Bootcamp App Started At:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;startTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;| Running On: &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;通过如下yaml建立一个deployment：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;extensions/v1beta1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode-deployment&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;stable&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1.0.0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;resources&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;requests&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;cpu&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;300m&quot;&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1Gi&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;limits&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;cpu&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;500m&quot;&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2Gi&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;imagePullPolicy&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Always&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;xxxx/mynode_node_daiwenkai:autoscale&quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;http&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后自动扩容&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl autoscale deployment mynode-deployment &lt;span class=&quot;nt&quot;&gt;--min&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1 &lt;span class=&quot;nt&quot;&gt;--max&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4 &lt;span class=&quot;nt&quot;&gt;--cpu-percent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2 &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;成功的话，会提示：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;horizontalpodautoscaler.autoscaling/mynode-deployment autoscaled&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;然后通过以下yaml建立一个service：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode-svc&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;NodePort&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mynode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后获取端口：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NODE_PORT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;kubectl get services/mynode-svc &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; go-template&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们用ab来压测（如果是Centos，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yum -y install httpd-tools&lt;/code&gt;来安装）：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们可以监控实例：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;watch &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; 1 &lt;span class=&quot;s1&quot;&gt;'kubectl top pod --namespace=daiwenkai-2'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;一开始可能是：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Every 1.0s: kubectl top pod &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2                                                                                      Wed Feb 27 20:12:34 2019

NAME                                 CPU&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;cores&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;   MEMORY&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;bytes&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
mynode-deployment-64c64d98b9-6rx85   164m         8Mi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当压力大的时候(cpu超过200m的时候)，就会自动扩容：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Every 1.0s: kubectl top pod &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2                                                                                      Wed Feb 27 20:25:15 2019

NAME                                 CPU&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;cores&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;   MEMORY&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;bytes&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
mynode-deployment-64c64d98b9-2gphw   0m           8Mi
mynode-deployment-64c64d98b9-rc6ng   500m         13Mi
mynode-deployment-64c64d98b9-rtnrq   0m           10Mi
mynode-deployment-64c64d98b9-zmlgd   22m          8Mi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;删除自动扩容策略：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl delete horizontalpodautoscaler.autoscaling/mynode-deployment &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;版本更新&quot;&gt;版本更新&lt;/h2&gt;

&lt;p&gt;set image可以改image&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;image deployments/mypython-deployment &lt;span class=&quot;nv&quot;&gt;mypython&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxxx/bootcamp_7/mynode_python_daiwenkai:2.0.0 &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后就可以看到：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pods &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2                                                                               NAME                                   READY     STATUS        RESTARTS   AGE
mypython-deployment-5989ddfd4f-652kd   1/1       Running       0          8s
mypython-deployment-5989ddfd4f-fx8cj   1/1       Running       0          9s
mypython-deployment-5989ddfd4f-lfzwt   1/1       Running       0          11s
mypython-deployment-5989ddfd4f-ngpm2   1/1       Running       0          11s
mypython-deployment-66bc86658-qvmm5    1/1       Terminating   0          15m
mypython-deployment-66bc86658-tcjdf    1/1       Terminating   0          11m
mypython-deployment-66bc86658-z4t4m    1/1       Terminating   0          15m
mypython-deployment-66bc86658-z9c25    1/1       Terminating   0          15m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再过一会儿，就是：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get pods &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2
NAME                                   READY     STATUS    RESTARTS   AGE
mypython-deployment-5989ddfd4f-652kd   1/1       Running   0          41s
mypython-deployment-5989ddfd4f-fx8cj   1/1       Running   0          42s
mypython-deployment-5989ddfd4f-lfzwt   1/1       Running   0          44s
mypython-deployment-5989ddfd4f-ngpm2   1/1       Running   0          44s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以查看进度(显示的是running的个数，如果还有在terminating的，只要都running了，就是succ)：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl rollout status deployments/mypython-deployment &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2
deployment &lt;span class=&quot;s2&quot;&gt;&quot;mypython-deployment&quot;&lt;/span&gt; successfully rolled out
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们可以回滚：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl rollout undo deployment/mypython-deployment &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;daiwenkai-2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;小流量&quot;&gt;小流量&lt;/h2&gt;

&lt;p&gt;先创建一个deployment，里面有3个pod，version是1&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;extensions/v1beta1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython-deployment&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;stable&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1.0.0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;xxx/bootcamp_7/mynode_python_daiwenkai:1.0.0&quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;http&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再创建一个deployment，里面有1个pod，version是2&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;extensions/v1beta1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython-canary&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;canary&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2.0.0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;hello&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;xxx/bootcamp_7/mynode_python_daiwenkai:2.0.0&quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;http&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后创建一个service，把这四个pod连接起来（通过app=mypython连接到一起~）：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython-svc&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;NodePort&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样，我们往这个Service发请求，就会发现75%的流量在v1上，25%的流量在v2上&lt;/p&gt;

&lt;h2 id=&quot;abtest&quot;&gt;abtest&lt;/h2&gt;

&lt;p&gt;先建一个deployment：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;extensions/v1beta1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython-deployment&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;stable&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1.0.0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;xxx/bootcamp_7/mynode_python_daiwenkai:1.0.0&quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;http&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再建一个deployment，唯一的区别就是version和name不一样，其他都一样，包括replicas：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;extensions/v1beta1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deployment&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython-green&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;replicas&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;stable&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2.0.0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;xxx/bootcamp_7/mynode_python_daiwenkai:2.0.0&quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;http&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;containerPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;同样地，用一个service把这6个node都连起来(selector是app=mypython)：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython-svc&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;targetPort&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8080&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;NodePort&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply -f&lt;/code&gt;命令来通过yaml文件来修改对象：&lt;/p&gt;

&lt;p&gt;比如我们用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mynode-1.0.0.yaml&lt;/code&gt;这个文件来选定v1版本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl apply -f mynode-1.0.0.yaml&lt;/code&gt;，这样发请求，这6个节点就都是v1了：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython-svc&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mypython&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;1.0.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;类似地，我们用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mynode-2.0.0.yaml&lt;/code&gt;这个文件来选定v2版本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl apply -f mynode-2.0.0.yaml&lt;/code&gt;，这样发请求，这6个节点就都是v2了：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Service&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;mypython-svc&quot;&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;daiwenkai-2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;selector&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;mypython&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;2.0.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;configmap&quot;&gt;configmap&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create configmap &amp;lt;map-name&amp;gt; &amp;lt;data-source&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;data-source可以是目录：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir &lt;/span&gt;conf
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;foo=bar&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; conf/a.properties
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;foo=baz&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; conf/b.properties

kubectl create configmap dir-conf &lt;span class=&quot;nt&quot;&gt;--from-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;data-source也可以是文件：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;foo=bar&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; a.properties

kubectl create configmap file-conf &lt;span class=&quot;nt&quot;&gt;--from-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;a.properties
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;data-source也可以直接在命令里指定&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create configmap literial-conf &lt;span class=&quot;nt&quot;&gt;--from-literial&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;foo_bar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;baz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在pod内使用configmap可以按文件引用：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt; 
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;volumeMounts&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;config-volume&lt;/span&gt; 
    &lt;span class=&quot;na&quot;&gt;mountPath&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/etc/foo”&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;readOnly:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;true&lt;/span&gt; 
  &lt;span class=&quot;s&quot;&gt;volumes:&lt;/span&gt; 
  &lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;name:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;config-volume&lt;/span&gt; 
    &lt;span class=&quot;s&quot;&gt;configMap:&lt;/span&gt; 
      &lt;span class=&quot;s&quot;&gt;name:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;file-conf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以按环境变量引用：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt; 
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;FOO_BAR&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;configMapKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;literial-conf&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;foo_bar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;secret&quot;&gt;secret&lt;/h2&gt;

&lt;p&gt;可以直接通过文件来创建：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; –n “root” &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; username
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; –n ‘root123!’ &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; password
kubectl create secret generic user-password &lt;span class=&quot;nt&quot;&gt;--from-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;username &lt;span class=&quot;nt&quot;&gt;--from-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;password
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以通过yaml，首先：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;root&quot;&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;base64
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cm9vdA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'root123!'&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;base64
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cm9vdDEyMyE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后打开一个secrets.yaml：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Secret&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;user-password&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Opaque&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cm9vdA==&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;cm9vdDEyMyE=&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl create &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; secrets.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;创建完，我们就可以发现：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl get secret &lt;span class=&quot;nt&quot;&gt;--namespace&lt;/span&gt; daiwenkai
NAME                  TYPE                                  DATA      AGE
default-token-xbt78   kubernetes.io/service-account-token   3         31m
user-password         Opaque                                2         13s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在pod内使用secret可以按文件引用：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt; 
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;volumeMounts&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;secret-volume&lt;/span&gt; 
    &lt;span class=&quot;na&quot;&gt;mountPath&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/etc/foo”&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;readOnly:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;true&lt;/span&gt; 
  &lt;span class=&quot;s&quot;&gt;volumes:&lt;/span&gt; 
  &lt;span class=&quot;s&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;name:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;secret-volume&lt;/span&gt; 
    &lt;span class=&quot;s&quot;&gt;secret:&lt;/span&gt; 
      &lt;span class=&quot;s&quot;&gt;secretName:&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user-password&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以按环境变量引用：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;apiVersion&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;v1&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Pod&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;metadata&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt; 
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mysql&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;MYSQL_ROOT_PASSWORD&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;valueFrom&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;secretKeyRef&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;user-password&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;password&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>docker常用命令</title>
   <link href="http://daiwk.github.io/posts/other-docker-cmds.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/other-docker-cmds</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#docker%E8%BF%9B%E5%85%A5%E5%AE%B9%E5%99%A8%E6%96%B9%E6%B3%95%E6%B1%87%E6%80%BB&quot;&gt;docker进入容器方法汇总&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#docker-exec&quot;&gt;docker exec&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#install-docker-enter&quot;&gt;install docker-enter&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-run&quot;&gt;docker run&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-images&quot;&gt;docker images&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-ps&quot;&gt;docker ps&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-pull&quot;&gt;docker pull&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-commit&quot;&gt;docker commit&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-push&quot;&gt;docker push&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-save&quot;&gt;docker save&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-load&quot;&gt;docker load&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-export&quot;&gt;docker export&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#docker-import&quot;&gt;docker import&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%B3%A8%E6%84%8F&quot;&gt;注意&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E4%B8%80%E4%BA%9B%E7%96%91%E9%9A%BE%E6%9D%82%E7%97%87&quot;&gt;一些疑难杂症&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;docker进入容器方法汇总&quot;&gt;docker进入容器方法汇总&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/sqzhao/article/details/71307518&quot;&gt;https://blog.csdn.net/sqzhao/article/details/71307518&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;docker-exec&quot;&gt;docker exec&lt;/h3&gt;

&lt;p&gt;不用交互式shell，直接执行命令。（&lt;strong&gt;注意：命令不能加”“。。。另外，好像也不能有&amp;amp;&amp;amp;之类的[可以后续再探索探索。。。]&lt;/strong&gt;）&lt;/p&gt;

&lt;p&gt;其中：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;-d :分离模式: 在后台运行&lt;/li&gt;
  &lt;li&gt;-i :即使没有附加也保持STDIN 打开&lt;/li&gt;
  &lt;li&gt;-t :分配一个伪终端&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /home/disk0/daiwk_img_data/demo_scrapy/lp_mining/lp_spider/containerid | xargs &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; docker &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-dt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt; /bin/bash /home/data/demo_scrap
y/lp_mining/lp_spider/run_all_images.sh 

&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /home/disk0/daiwk_img_data/demo_scrapy/lp_mining/lp_spider/containerid | xargs &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; docker &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;tail&lt;/span&gt; /home/data/demo_scrapy/lp_min
ing/lp_spider/crawl.images8.log

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当然，也可以交互式啦~~~！！&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; docker ps
CONTAINER ID        IMAGE                                               COMMAND             CREATED             STATUS              PORTS                    NAMES
e56da0385825        tensorflow/tensorflow:latest   &lt;span class=&quot;s2&quot;&gt;&quot;/bin/bash&quot;&lt;/span&gt;         3 months ago        Up 3 months         6006/tcp, 8888/tcp       boring_gates
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后直接如下就行了，即使退出了，再重进也是一样的啦&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;work@myhost ~] docker &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; e56da0385825 /bin/bash
root@e56da0385825:/notebooks# ll
total 416
drwxr-xr-x  2 root root   4096 Oct 11 06:40 ./
drwxr-xr-x 22 root root   4096 Jul  3 12:43 ../
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root  25023 Apr 28 00:37 1_hello_tensorflow.ipynb
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root 164559 Apr 28 00:37 2_getting_started.ipynb
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root 209951 Apr 28 00:37 3_mnist_from_scratch.ipynb
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root    119 Apr 28 00:37 BUILD
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root    586 Apr 28 00:37 LICENSE
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt;  1 root root      0 Oct 11 06:40 x
root@e56da0385825:/notebooks# &lt;span class=&quot;nb&quot;&gt;exit
exit&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;work@myhost ~] docker &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; e56da0385825 /bin/bash
root@e56da0385825:/notebooks# ll
total 416
drwxr-xr-x  2 root root   4096 Oct 11 06:40 ./
drwxr-xr-x 22 root root   4096 Jul  3 12:43 ../
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root  25023 Apr 28 00:37 1_hello_tensorflow.ipynb
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root 164559 Apr 28 00:37 2_getting_started.ipynb
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root 209951 Apr 28 00:37 3_mnist_from_scratch.ipynb
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root    119 Apr 28 00:37 BUILD
&lt;span class=&quot;nt&quot;&gt;-rw-rw-r--&lt;/span&gt;  1 root root    586 Apr 28 00:37 LICENSE
&lt;span class=&quot;nt&quot;&gt;-rw-r--r--&lt;/span&gt;  1 root root      0 Oct 11 06:40 x
root@e56da0385825:/notebooks# 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;install-docker-enter&quot;&gt;install docker-enter&lt;/h3&gt;

&lt;p&gt;注：mac似乎不能用此方法，因为那个bin是在linux下编的&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;http://blog.csdn.net/fenglailea/article/details/44900401&quot;&gt;使用nsenter进入Docker容器&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker pull jpetazzo/nsenter
docker run --rm -v /usr/bin:/target jpetazzo/nsenter
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter

wget -P ~ https://raw.githubusercontent.com/yeasy/docker_practice/docker-legacy/_local/.bashrc_docker --no-check-certificate;

echo &quot;[ -f ~/.bashrc_docker ] &amp;amp;&amp;amp; . ~/.bashrc_docker&quot; &amp;gt;&amp;gt; ~/.bashrc; source ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用时，直接找到对应的containerid，然后：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker-enter ffd32a5b82f7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-run&quot;&gt;docker run&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run -idt -v /home/work/daiwenkai:/home/data 390b21e493af /bin/bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-images&quot;&gt;docker images&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu                            ru                   latest              f5bb94a8fac4        11 days ago         117.3 MB
xxx.baidu.com/daiwenkai/deep-learning-factory   1.0.2               390b21e493af        7 weeks ago         2.174 GB
tensorflow/tensorflow                                latest              05a46e5af4d3        8 weeks ago         1.028 GB
xxx.baidu.com/public/centos6u3                  1.0.1               9a6d077da3a3        23 months ago       1.363 GB
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-ps&quot;&gt;docker ps&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker ps
CONTAINER ID        IMAGE                 COMMAND             CREATED              STATUS              PORTS                NAMES
ffd32a5b82f7        ubuntu:latest         &quot;/bin/bash&quot;         About a minute ago   Up About a minute                        loving_torvalds     
166ab859e45c        390b21e493af:latest   &quot;/bin/bash&quot;         6 weeks ago          Up 6 weeks          6006/tcp, 8888/tcp   lonely_bartik 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;kill并rm所有container&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker ps -aq | xargs docker rm -f 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-pull&quot;&gt;docker pull&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker pull ubuntu
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-commit&quot;&gt;docker commit&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker commit -m &quot;1.0.1 version&quot; ffd32a5b82f7 xxx.baidu.com/daiwenkai/scrapy-framework:1.0.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-push&quot;&gt;docker push&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker push xxx.baidu.com/daiwenkai/scrapy-framework:1.0.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-save&quot;&gt;docker save&lt;/h2&gt;

&lt;p&gt;导出image&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker save -o scrapy-framework.docker.tar 8ce4bbc775d2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-load&quot;&gt;docker load&lt;/h2&gt;

&lt;p&gt;导入image&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker load -i scrapy-framework.docker.tar 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-export&quot;&gt;docker export&lt;/h2&gt;

&lt;p&gt;导出container&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker export ffd32a5b82f7 &amp;gt; scrapy-framework.1.0.1.tar    
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;docker-import&quot;&gt;docker import&lt;/h2&gt;

&lt;p&gt;导入container对应的image，启动时，和之前的container一样&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat scrapy-framework.tar | docker import - daiwk/scrapy-framework  
cat deep-learning-factory.1.0.3.tar | docker import - test/deep-learning-factory  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;注意&quot;&gt;注意&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;在win7安装时，要用docker toolbox(&lt;a href=&quot;https://download.docker.com/win/stable/DockerToolbox.exe&quot;&gt;https://download.docker.com/win/stable/DockerToolbox.exe&lt;/a&gt;)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;win10以上时，用docker-ce(&lt;a href=&quot;https://store.docker.com/editions/community/docker-ce-desktop-windows?tab=description&quot;&gt;https://store.docker.com/editions/community/docker-ce-desktop-windows?tab=description&lt;/a&gt;)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;一些疑难杂症&quot;&gt;一些疑难杂症&lt;/h2&gt;

&lt;p&gt;发现有的container没法stop/kill/rm的时候，例如&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker &lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; 316f2eca7a3b
Error response from daemon: Could not &lt;span class=&quot;nb&quot;&gt;kill &lt;/span&gt;running container, cannot remove - &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2] Container does not exist: container destroyed
Error: failed to remove containers: &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;316f2eca7a3b]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;尝试找到docker的daemon进程，并kill，然后重启：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ps aux | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;docker
root      4157  0.0  0.0 105352   832 pts/3    S+   13:26   0:00 &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;docker
root     32816  0.0  0.0 1562760 42500 ?       Sl   Jun02   1:36 /usr/bin/docker &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; docker0 &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; tcp://0.0.0.0:2375 &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; unix:///var/run/docker.sock &lt;span class=&quot;nt&quot;&gt;--insecure-registry&lt;/span&gt; registry.xxxxx.com &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; /home/work/docker
&lt;span class=&quot;nb&quot;&gt;kill&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-9&lt;/span&gt; 32816
 &lt;span class=&quot;nb&quot;&gt;nohup&lt;/span&gt; /usr/bin/docker &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; docker0 &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; tcp://0.0.0.0:2375 &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; unix:///var/run/docker.sock &lt;span class=&quot;nt&quot;&gt;--insecure-registry&lt;/span&gt; registry.xxxxx.com &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; /home/work/docker &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个时候就发现container的状态已经是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exited (137) 7 seconds ago&lt;/code&gt;了，再去rm，就可以啦&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>社区发现算法</title>
   <link href="http://daiwk.github.io/posts/other-community-discovery.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/other-community-discovery</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u012369559/article/details/78713500&quot;&gt;https://blog.csdn.net/u012369559/article/details/78713500&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/benedekrozemberczki/awesome-community-detection&quot;&gt;https://github.com/benedekrozemberczki/awesome-community-detection&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;nature上的文章：&lt;a href=&quot;https://www.nature.com/articles/srep30750.pdf&quot;&gt;https://www.nature.com/articles/srep30750.pdf&lt;/a&gt;
wikipedia &lt;a href=&quot;https://en.wikipedia.org/wiki/Community_structure&quot;&gt;https://en.wikipedia.org/wiki/Community_structure&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Newman:&lt;/p&gt;

&lt;p&gt;GN: Finding and evaluating community structure in networks&lt;/p&gt;

&lt;p&gt;FN: Fast algorithm for detecting community structure in networks&lt;/p&gt;

&lt;p&gt;CNM: Finding community structure in very large networks&lt;/p&gt;

&lt;p&gt;others:&lt;/p&gt;

&lt;p&gt;Louvain: Fast unfolding of communities in large networks&lt;/p&gt;

&lt;p&gt;标签传播：Finding overlapping communities in networks by label propagation&lt;/p&gt;

&lt;p&gt;游走：Computing communities in large networks using random walks&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>文本生成模型实践</title>
   <link href="http://daiwk.github.io/posts/nlp-text-generation-practice.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/nlp-text-generation-practice</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#keraslstm&quot;&gt;keras+lstm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#transsent&quot;&gt;transsent&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;keraslstm&quot;&gt;keras+lstm&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/eoPFC6fms1qhGTOqCk-3Qg&quot;&gt;用自己的风格教AI说话，语言生成模型可以这样学&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/maelfabien/Machine_Learning_Tutorials&quot;&gt;https://github.com/maelfabien/Machine_Learning_Tutorials&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.kaggle.com/shivamb/beginners-guide-to-text-generation-using-lstms&quot;&gt;https://www.kaggle.com/shivamb/beginners-guide-to-text-generation-using-lstms&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;transsent&quot;&gt;transsent&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.05364&quot;&gt;TransSent: Towards Generation of Structured Sentences with Discourse Marker&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/1024er/TransSent_dataset&quot;&gt;https://github.com/1024er/TransSent_dataset&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文来自中科院，论文将知识图谱的 Trans 思想引入到句子表示空间，提出了一个新的任务 Sentence Transfer，为了解决这个任务构建了三个数据集。人会在写作的时候使用连词（discourse marker，比如 and, but, if…）来衔接子句（discourses），连词用来显式地表达前后子句之间的语义关系。&lt;/p&gt;

&lt;p&gt;因此作者认为在 embedding space 中，子句之间可以利用连词（表示的关系）进行迁移/翻译。在学习出句子之间的 Trans 模型后，可以用来分阶段生成结构化长句：首先生成首子句，然后选择一种关系，再 Trans 生成尾子句。这样的生成方式可以和现有的问答等任务结合。作者还给出了一个数据集上的实验结果。总体来说本文给出了一个新的生成的思路，值得发掘。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>情感分析</title>
   <link href="http://daiwk.github.io/posts/nlp-sentiment-analysis.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/nlp-sentiment-analysis</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/wK4MNY2Nd_mzB7dftdPugw&quot;&gt;华为云NLP算法专家：全面解读文本情感分析任务&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>map/ndcg</title>
   <link href="http://daiwk.github.io/posts/nlp-ndcg.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/nlp-ndcg</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#map&quot;&gt;MAP&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ndcg&quot;&gt;NDCG&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#g&quot;&gt;G&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cg&quot;&gt;CG&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dcg&quot;&gt;DCG&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#idcg&quot;&gt;IDCG&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ndcg-1&quot;&gt;NDCG&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#limitations&quot;&gt;limitations&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;map&quot;&gt;MAP&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.sciencenet.cn/blog-791354-763077.html&quot;&gt;http://blog.sciencenet.cn/blog-791354-763077.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;ndcg&quot;&gt;NDCG&lt;/h2&gt;

&lt;p&gt;NDCG表示归一化折损累积增益(Normalized discounted cumulative gain)，&lt;/p&gt;

&lt;p&gt;NDCG如何理解？对于搜索引擎，本质是用户搜一个query，引擎返回一个结果列表，那么如何衡量这个结果列表的好坏？我能够想到的是：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;我们希望把最相关的结果放到排名最靠前的位置，因为大部分用户都是从上往下阅读的，那么最相关的在前面可以最大程度减少用户的阅读时间；&lt;/li&gt;
  &lt;li&gt;我们希望整个列表的结果尽可能的和query相关；&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;第一个条件的满足是首要的，而第二个条件的加入是保证整体结果质量，而这两个条件都体现在了NDCG里面，首先，计算NDCG，需要计算Gain，这个gain即是每条结果的质量的定义，NDCG把所有结果相加最终相加保证，整体质量越高的列表NDCG值越大。同时，Discounted的设计使得越靠前的结果权重越大，这保证了第一条，更相关的排在靠前的结果会有更大的NDCG值。从这两点看，以NDCG为优化目标，保证了搜索引擎在返回结果总体质量好的情况下，把更高质量结果排在更前面。&lt;/p&gt;

&lt;h3 id=&quot;g&quot;&gt;G&lt;/h3&gt;

&lt;p&gt;首先，理解g(gain)，表示自己定义的一种获得，用来衡量网页的相关性程度，例如&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ndcg-gain.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;cg&quot;&gt;CG&lt;/h3&gt;

&lt;p&gt;CG（cumulative gain），累积获得，指的是网页gain的累加，例如&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ndcg-cg.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\mathrm{CG}_p=\sum^p_{i=1}{rel_i}\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(rel_i\)&lt;/code&gt;是第i个网页的gain&lt;/p&gt;

&lt;h3 id=&quot;dcg&quot;&gt;DCG&lt;/h3&gt;

&lt;p&gt;分母与位次正相关，也就是说，越靠后的doc，我们对他的打压越严重。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\mathrm{DCG}_p=\sum^p_{i=1}{\frac{rel_i}{log_2(i+1)}}=rel_1+\sum^p_{i=2}{\frac{rel_i}{log_2(i+1)}}\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(rel_i\)&lt;/code&gt;是第i个网页的gain&lt;/p&gt;

&lt;p&gt;大多数搜索公司和kaggle用的DCG如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\mathrm{DCG}_p=\sum^p_{i=1}{\frac{2^{rel_i}-1}{log_2(i+1)}}\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;idcg&quot;&gt;IDCG&lt;/h3&gt;

&lt;p&gt;Ideal DCG，理想状况下的DCG。也就是说，相关性完全由高到低排序时算出的DCG：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\mathrm{IDCG}_p=\sum^{|REL|}_{i=1}{\frac{2^{rel_i}-1}{log_2(i+1)}}\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;ndcg-1&quot;&gt;NDCG&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[\mathrm{nDCG}_p=\frac{\mathrm{DCG}_p}{\mathrm{IDCG}_p}\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;最完美的排序算法就是DCG=IDCG，即NDCG=1。&lt;/p&gt;

&lt;h3 id=&quot;limitations&quot;&gt;limitations&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;并没有对bad的文档进行惩罚。&lt;/li&gt;
  &lt;li&gt;xxx&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>learning to rank(ltr)</title>
   <link href="http://daiwk.github.io/posts/nlp-ltr.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/nlp-ltr</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#learning-to-rank%e7%ae%80%e4%bb%8b&quot;&gt;learning to rank简介&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#point-wise&quot;&gt;point-wise&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pair-wise&quot;&gt;pair-wise&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#gbrank&quot;&gt;gbrank&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#ranksvm%e5%92%8cirsvm&quot;&gt;ranksvm和IRsvm&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#ranknet&quot;&gt;ranknet&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#list-wise&quot;&gt;list-wise&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ltr%e7%9a%84%e5%b8%b8%e8%a7%81%e7%bd%91%e7%bb%9c%e7%bb%93%e6%9e%84&quot;&gt;ltr的常见网络结构&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#pairwise&quot;&gt;pairwise&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#listwise%e7%9a%84lambdarank&quot;&gt;listwise的lambdarank&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%ba%94%e7%94%a8&quot;&gt;应用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e7%be%8e%e5%9b%a2%e7%82%b9%e8%af%84%e7%9a%84lambdarank&quot;&gt;美团点评的lambdarank&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e7%94%a8%e6%88%b7id%e7%9a%84embedding&quot;&gt;用户ID的Embedding&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#lambdadnn%e7%9a%84%e4%bd%bf%e7%94%a8&quot;&gt;lambdadnn的使用&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;learning-to-rank简介&quot;&gt;learning to rank简介&lt;/h2&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/PaddlePaddle/models/tree/develop/legacy/ltr&quot;&gt;https://github.com/PaddlePaddle/models/tree/develop/legacy/ltr&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考刘铁岩老师的书&lt;a href=&quot;https://www.cda.cn/uploadfile/image/20151220/20151220115436_46293.pdf&quot;&gt;Learning to Rank for Information Retrieval&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;还有ppt：&lt;a href=&quot;http://wwwconference.org/www2009/pdf/T7A-LEARNING%20TO%20RANK%20TUTORIAL.pdf&quot;&gt;http://wwwconference.org/www2009/pdf/T7A-LEARNING%20TO%20RANK%20TUTORIAL.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;李航老师的书&lt;a href=&quot;http://www.iro.umontreal.ca/~nie/IFT6255/Books/Learning-to-rank.pdf&quot;&gt;Learning to rank for information retrieval and natural language processing&lt;/a&gt;，&lt;/p&gt;

&lt;p&gt;对比各种wise的优缺点：&lt;a href=&quot;https://blog.csdn.net/lipengcn/article/details/80373744&quot;&gt;https://blog.csdn.net/lipengcn/article/details/80373744&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/338044033/answer/815202813?hb_wx_block=0&amp;amp;utm_source=wechat_session&amp;amp;utm_medium=social&amp;amp;utm_oi=632586637935251456&quot;&gt;pairwise 的排序算法用于推荐系统的排序任务中为什么效果差于pointwise的ctr？&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;point-wise&quot;&gt;point-wise&lt;/h3&gt;

&lt;h3 id=&quot;pair-wise&quot;&gt;pair-wise&lt;/h3&gt;

&lt;h4 id=&quot;gbrank&quot;&gt;gbrank&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/bentuwuying/p/6684585.html&quot;&gt;https://www.cnblogs.com/bentuwuying/p/6684585.html&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;ranksvm和irsvm&quot;&gt;ranksvm和IRsvm&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/bentuwuying/p/6683832.html&quot;&gt;https://www.cnblogs.com/bentuwuying/p/6683832.html&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;ranknet&quot;&gt;ranknet&lt;/h4&gt;

&lt;p&gt;ranknet用的是crossentropy的loss：&lt;a href=&quot;https://blog.csdn.net/puqutogether/article/details/42124491&quot;&gt;https://blog.csdn.net/puqutogether/article/details/42124491&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ranknet的文章：&lt;a href=&quot;https://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf&quot;&gt;https://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;list-wise&quot;&gt;list-wise&lt;/h3&gt;

&lt;p&gt;lambdarank&lt;/p&gt;

&lt;p&gt;NDCG是一个处处非平滑的函数，直接以它为目标函数进行优化是不可行的。&lt;/p&gt;

&lt;p&gt;LambdaRank提供了一种思路：绕过目标函数本身，&lt;strong&gt;直接构造一个特殊的梯度（称之为Lambda梯度）&lt;/strong&gt;，按照梯度的方向修正模型参数，最终能达到拟合NDCG的方法。通过该梯度构造出的深度学习网络称之为LambdaDNN。&lt;/p&gt;

&lt;p&gt;LambdaRank模型是通过Pairwise来构造的，通常将&lt;strong&gt;同Query&lt;/strong&gt;下&lt;strong&gt;有点&lt;/strong&gt;击样本和&lt;strong&gt;无点&lt;/strong&gt;击样本构造成一个样本Pair&lt;/p&gt;

&lt;p&gt;了解一下LambdaMART：&lt;a href=&quot;https://blog.csdn.net/huagong_adu/article/details/40710305&quot;&gt;https://blog.csdn.net/huagong_adu/article/details/40710305&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;ltr的常见网络结构&quot;&gt;ltr的常见网络结构&lt;/h2&gt;

&lt;h3 id=&quot;pairwise&quot;&gt;pairwise&lt;/h3&gt;

&lt;p&gt;以paddle为例：&lt;/p&gt;

&lt;p&gt;我们希望训练一个dnn来描述“语义相似度”。期望的输入是两个词，输出一个float值，表示这两个字有多么相似。这个相似度最好和人们的直观感受相对应，而对于具体数值其实并不是很关心。比如，我们训练出来的模型计算出“北京vs上海=1.2”，“北京vs鼠标=0.12”，看起来就挺合理的。&lt;/p&gt;

&lt;p&gt;在这种情况下，需要训练的并不是“某一条instance对应一个label”，而是“北京和上海的语义相似度，大于北京和鼠标的语义相似度”。这时候就需要引入pairwise的训练。在这个例子中，每一条样本有4个slot和一个label：slot0是北京，slot1是上海，slot2是北京，slot3是鼠标，label是1。&lt;/p&gt;

&lt;p&gt;假设模型已经训练好了，我们需要的是“两个输入一个输出”，但是训练的时候却是“四个输入一个输出”。这需要我们把配置写成“共享参数”。即，在“4个输入”的情况下，这4个输入的前2个所使用的网络，和后2个使用的网络，是一样的。获得两个“子网络”的输出之后，再在最后接一个pn-pair层，把两个子网络的输出和label接在一起。&lt;/p&gt;

&lt;p&gt;结构如下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/ltr-paddle-demo.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;c1&quot;&gt;## data
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;input0_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;input1_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;input0_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;input1_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;## 左侧的网络和右侧的网络，注意连接方式和参数名字都是一样的，只是layer和input的不同。
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input0_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer1_1.w0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;layer1_0_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer1_1.wbias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input1_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer1_1.w0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;layer1_1_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer1_1.wbias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input0_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer1_1.w0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;layer1_0_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer1_1.wbias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input1_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer1_1.w0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;layer1_1_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer1_1.wbias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;## 加多两层
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer1_0_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer2_2.w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer1_1_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer2_3.w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;layer2_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer2.bias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer1_0_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer2_2.w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer1_1_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer2_3.w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;layer2_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layer2.bias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer2_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layerO.w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;output_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layerO.bias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;layer2_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layerO.w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;output_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;_layerO.bias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;## 输出cost
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;output_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;output_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rank-cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;input0_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;input1_left&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;input0_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;input1_right&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;n&quot;&gt;Outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;listwise的lambdarank&quot;&gt;listwise的lambdarank&lt;/h3&gt;

&lt;p&gt;业界公认的LTR方法性能排序为Listwise~=Pairwise » Pointwise。基于listwise的lambdarank在Yahoo! LTR challenge中夺得了冠军。&lt;strong&gt;lambdaRank训练速度相对pairwise方法提升20倍&lt;/strong&gt;，同时保持效果不变。&lt;/p&gt;

&lt;p&gt;Listwise方法的几个优势：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;原始数据无需组pair，从而避免了因组pair导致的数据量、数据大小的成倍增长。这也一定程度上加快了训练过程。&lt;/li&gt;
  &lt;li&gt;优化目标为NDCG，通过指定NDCG截断个数，可以忽略大量尾部带噪声的样本的排序，从而集中优化前几位的排序。&lt;/li&gt;
  &lt;li&gt;直接利用原始数据的打分信息进行排序学习，避免了通过分数大小组pair带来的信息损失。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在paddle中使用lambdaRank需要注意数据输入格式。为了保证同一个query下的title组成的样本（即一个list）不被随机分拆、打乱，用户需要把同一个query的所有样本组成一个sequence输入网络（可以使用ProtoDataProvider或者PyDataProvider实现）。另外，用户还需要输入每一条样本的打分，以计算NDCG并更新梯度。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;DataLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;query&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DataLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DataLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;score&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DataLayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;emb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;active_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;relu&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;query&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;emb.w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameter_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;emb.bias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;emb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 
&lt;span class=&quot;n&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;lambda_cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NDCG_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max_sort_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;score&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
 
&lt;span class=&quot;n&quot;&gt;Inputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;query&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;score&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Outputs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;实际上这里并不需要用到label，只是为了兼容DataProvider而作占位。&lt;/li&gt;
  &lt;li&gt;lambda_cost的输出是当前batch的平均NDCG。&lt;/li&gt;
  &lt;li&gt;lambda_cost参数说明：
    &lt;ul&gt;
      &lt;li&gt;NDCG_num指定NDCG截断个数，这里即计算NDCG@8。&lt;/li&gt;
      &lt;li&gt;max_sort_size指定部分排序的个数。由lambdaRank算法原理，该数必须大于等于NDCG_num。其数值越大，则用于计算梯度的pair越多，从而信息越多，效果越好。默认为-1，此时同一list下的所有样本均会组成pair用于更新梯度（即信息利用最全面）。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;由于需要计算NDCG，因此用户需要保证所有list的样本数量均大于等于NDCG_num，并且保证不含有打分全为0的list。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;应用&quot;&gt;应用&lt;/h2&gt;

&lt;h3 id=&quot;美团点评的lambdarank&quot;&gt;美团点评的lambdarank&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MjM5NjQ5MTI5OA==&amp;amp;mid=2651750220&amp;amp;idx=1&amp;amp;sn=42df36757a7007808c56b53ee6832713&amp;amp;chksm=bd12a6018a652f17de2f66e28ba203bde1e8ae22155687fd3abe73b0336900a855c057e6ad38&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0117dRsxGP0zSDCmQ4pTmBDF&amp;amp;pass_ticket=yoIK672aXk4WPiJRK3zkCxK5C5wwnua1%2B%2F115s%2FKJyXjdHQlvctIkGZpDsP%2FPVPZ#rd&quot;&gt;大众点评搜索基于知识图谱的深度学习排序实践&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;用户id的embedding&quot;&gt;用户ID的Embedding&lt;/h4&gt;

&lt;p&gt;常用方法是直接将用户ID经过Embedding后作为特征接入到模型中，但是最后上线的效果却不尽如人意。通过分析用户的行为数据，我们发现相当一部分用户ID的行为数据较为稀疏，导致用户ID的Embedding没有充分收敛，未能充分刻画用户的偏好信息。&lt;/p&gt;

&lt;p&gt;Airbnb发表在KDD 2018上的文章&lt;a href=&quot;https://astro.temple.edu/~tua95067/kdd2018.pdf&quot;&gt;Real-time Personalization using Embeddings for Search Ranking at Airbnb&lt;/a&gt;为这种问题提供了一种解决思路——利用&lt;strong&gt;用户基础画像和行为数据&lt;/strong&gt;对&lt;strong&gt;用户ID进行聚类&lt;/strong&gt;。Airbnb的主要场景是为旅游用户提供民宿短租服务，一般&lt;strong&gt;用户一年旅游的次数在1-2次之间&lt;/strong&gt;，因此Airbnb的用户行为数据相比点评搜索会更为稀疏一些。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/user-embed-cluster.webp&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;如上图所示，将用户画像特征和行为特征进行离散分桶，拼接特征名和所属桶号，得到的聚类ID为：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;US_lt1_pn3_pg3_r3_5s4_c2_b1_bd2_bt2_nu3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;采取了类似Airbnb的方案，稀疏性的问题得到了很好的解决，并且这样做还获得了一些额外的收益。大众点评作为一个本地化的生活信息服务平台，大部分用户的行为都集中自己的常驻地，导致用户到达一个&lt;strong&gt;新地方&lt;/strong&gt;时，排序个性化明显不足。通过这种聚类的方式，将&lt;strong&gt;异地&lt;/strong&gt;有&lt;strong&gt;相同行为的用户聚集&lt;/strong&gt;在一起，也能解决一部分跨站的个性化问题。&lt;/p&gt;

&lt;h4 id=&quot;lambdadnn的使用&quot;&gt;lambdadnn的使用&lt;/h4&gt;

&lt;p&gt;Lambda梯度需要对同Query下的样本进行计算，但是正常情况下所有的样本是随机Shuffle到各个Worker的。因此我们需要对样本进行预处理：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;通过QueryId进行Shuffle，将同一个Query的样本聚合在一起，同一个Query的样本打包进一个TFRecord。&lt;/li&gt;
  &lt;li&gt;由于每次请求Query召回的Doc数不一样，对于可变Size的Query样本在拉取数据进行训练时需要注意，TF会自动补齐Mini-Batch内每个样本大小一致，导致输入数据中存在大量无意义的默认值样本。这里我们提供两点处理方式：
    &lt;ul&gt;
      &lt;li&gt;MR过程中对Key进行处理，使得多个Query的样本聚合在一起，然后在训练的时候进行动态切分。&lt;/li&gt;
      &lt;li&gt;读取到补齐的样本，根据设定的补齐标记获取索引位，去除补齐数据。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;还进行了如下优化：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;将ID类特征的映射等操作一并在预处理中完成，减少多轮Training过程中的重复计算。&lt;/li&gt;
  &lt;li&gt;将样本转TfRecord，利用RecordDataSet方式读取数据并计算处理，Worker的计算性能大概提升了10倍。&lt;/li&gt;
  &lt;li&gt;Concat多个Categorical特征，组合成Multi-Hot的Tensor进行一次Embedding_Lookup操作，减少Map操作的同时有助于参数做分片存储计算。&lt;/li&gt;
  &lt;li&gt;稀疏Tensor在计算梯度以及正则化处理时保留索引值，仅对有数值的部分进行更新操作。&lt;/li&gt;
  &lt;li&gt;多个PS服务器间进行分片存储大规模Tensor变量，减少Worker同步更新的通讯压力，减少更新阻塞，达到更平滑的梯度更新效果。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;整体下来，对于30亿左右的样本量、上亿级别的特征维度，一轮迭代大概在半小时内完成。适当的增加并行计算的资源，可以达到分钟级的训练任务。&lt;/p&gt;

&lt;p&gt;NDCG的计算公式中，折损的权重是随着位置呈指数变化的。然而实际曝光点击率随位置变化的曲线与NDCG的理论折损值存在着较大的差异。&lt;/p&gt;

&lt;p&gt;对于移动端的场景来说，用户在下拉滑动列表进行浏览时，视觉的焦点会随着滑屏、翻页而发生变动。例如用户翻到第二页时，往往会重新聚焦，因此，会发现第二页头部的曝光点击率实际上是高于第一页尾部位置的。我们尝试了两种方案去微调NDCG中的指数位置折损：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;根据实际曝光点击率拟合折损曲线：根据实际统计到的曝光点击率数据，拟合公式替代NDCG中的指数折损公式，绘制的曲线如图12所示。&lt;/li&gt;
  &lt;li&gt;计算Position Bias作为位置折损：Position Bias在业界有较多的讨论，其中[7][8]将用户点击商户的过程分为观察和点击两个步骤：a.用户需要首先看到该商户，而看到商户的概率取决于所在的位置；b.看到商户后点击商户的概率只与商户的相关性有关。步骤a计算的概率即为Position Bias，这块内容可以讨论的东西很多，这里不再详述。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;经过上述对NDCG计算改造训练出的LambdaDNN模型，相较Base树模型和Pointwise DNN模型，在业务指标上有了非常显著的提升。&lt;/p&gt;

&lt;p&gt;Lambda梯度除了与DNN网络相结合外，事实上可以与绝大部分常见的网络结构相结合。为了进一步学习到更多交叉特征，我们在LambdaDNN的基础上分别尝试了LambdaDeepFM和LambdaDCN网络；其中DCN网络是一种加入Cross的并行网络结构，交叉的网络每一层的输出特征与第一层的原始输入特征进行显性的两两交叉，相当于每一层学习特征交叉的映射去拟合层之间的残差。&lt;/p&gt;

&lt;p&gt;离线的对比实验表明，Lambda梯度与DCN网络结合之后充分发挥了DCN网络的特点，简洁的多项式交叉设计有效地提升模型的训练效果。&lt;/p&gt;

&lt;p&gt;近期Google开源的TF Ranking(参考&lt;a href=&quot;https://daiwk.github.io/posts/platform-tf-ranking.html&quot;&gt;https://daiwk.github.io/posts/platform-tf-ranking.html&lt;/a&gt;)提出的Groupwise模型也对我们有一些启发。目前绝大部分的Listwise方法只是体现在模型训练阶段，在打分预测阶段依然是Pointwise的，即只会考虑当前商户相关的特征，而不会考虑列表上下文的结果，未来我们也会在这个方向上进行一些探索。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>不止glue</title>
   <link href="http://daiwk.github.io/posts/nlp-glue-above.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/nlp-glue-above</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/NrJRAXzgM0m7BIJaX_F5oA&quot;&gt;超难NLP新基准SuperGLUE正式发布：横扫SOTA模型BERT勉强过关&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SuperGLUE使用BERT-LARGE-CASED variant.11作为模型性能基准。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://gluebenchmark.com&quot;&gt;https://gluebenchmark.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SuperGLUE还包含基于已有数据的抽取、单个数值的表现指标，以及一套分析工具包jiant。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://jiant.info/&quot;&gt;https://jiant.info/&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>树模型</title>
   <link href="http://daiwk.github.io/posts/ml-tree-models.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/ml-tree-models</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%9F%BA%E7%A1%80%E6%A0%91%E6%A8%A1%E5%9E%8B&quot;&gt;基础树模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#id3&quot;&gt;ID3&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#c45&quot;&gt;C4.5&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cart&quot;&gt;CART&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ensemble&quot;&gt;ensemble&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#ensemble%E7%9A%84%E8%83%8C%E6%99%AF&quot;&gt;ensemble的背景&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#1%E6%A8%A1%E5%9E%8B%E9%80%89%E6%8B%A9model-selection&quot;&gt;1.模型选择（Model Selection）&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#2%E6%95%B0%E6%8D%AE%E9%9B%86%E8%BF%87%E5%B0%8F%E6%88%96%E8%BF%87%E5%A4%A7too-much-or-too-little-data&quot;&gt;2.数据集过小或过大（Too much or too little data）&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#3%E5%88%86%E6%B2%BBdivide-and-conquer&quot;&gt;3.分治（Divide and Conquer）&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#4%E6%95%B0%E6%8D%AE%E8%9E%8D%E5%90%88data-fusion&quot;&gt;4.数据融合（Data Fusion）&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#boosting&quot;&gt;boosting&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bagging&quot;&gt;bagging&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gbdt&quot;&gt;gbdt&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#xgboost&quot;&gt;xgboost&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;基础树模型&quot;&gt;基础树模型&lt;/h2&gt;

&lt;h3 id=&quot;id3&quot;&gt;ID3&lt;/h3&gt;

&lt;h3 id=&quot;c45&quot;&gt;C4.5&lt;/h3&gt;

&lt;h3 id=&quot;cart&quot;&gt;CART&lt;/h3&gt;

&lt;p&gt;CART，又名分类回归树，是在ID3的基础上进行优化的决策树，学习CART记住以下几个关键点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;CART既能是分类树，又能是分类树；&lt;/li&gt;
  &lt;li&gt;节点分裂的依据：
    &lt;ul&gt;
      &lt;li&gt;分类树时，采用GINI值；&lt;/li&gt;
      &lt;li&gt;回归树时，采用样本的最小方差；&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;CART是一棵二叉树&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;详见&lt;a href=&quot;https://www.cnblogs.com/canyangfeixue/p/7802835.html&quot;&gt;https://www.cnblogs.com/canyangfeixue/p/7802835.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;ensemble&quot;&gt;ensemble&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u010659278/article/details/44527437&quot;&gt;https://blog.csdn.net/u010659278/article/details/44527437&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;集成学习是指将&lt;strong&gt;若干弱分类器组合之后产生一个强分类器&lt;/strong&gt;。弱分类器（weak learner）指那些分类准确率只稍好于随机猜测的分类器（error rate &amp;lt; 50%）。&lt;/p&gt;

&lt;p&gt;集成算法成功的关键在于能&lt;strong&gt;保证弱分类器的多样性&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;ensemble的背景&quot;&gt;ensemble的背景&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.scholarpedia.org/article/Ensemble_learning&quot;&gt;http://www.scholarpedia.org/article/Ensemble_learning&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;1模型选择model-selection&quot;&gt;1.模型选择（Model Selection）&lt;/h4&gt;

&lt;h4 id=&quot;2数据集过小或过大too-much-or-too-little-data&quot;&gt;2.数据集过小或过大（Too much or too little data）&lt;/h4&gt;

&lt;h4 id=&quot;3分治divide-and-conquer&quot;&gt;3.分治（Divide and Conquer）&lt;/h4&gt;

&lt;h4 id=&quot;4数据融合data-fusion&quot;&gt;4.数据融合（Data Fusion）&lt;/h4&gt;

&lt;h3 id=&quot;boosting&quot;&gt;boosting&lt;/h3&gt;

&lt;h3 id=&quot;bagging&quot;&gt;bagging&lt;/h3&gt;

&lt;h2 id=&quot;gbdt&quot;&gt;gbdt&lt;/h2&gt;

&lt;p&gt;gbdt 是通过采用&lt;strong&gt;加法模型&lt;/strong&gt;（即&lt;strong&gt;对基函数进行线性组合&lt;/strong&gt;），以及不断减小训练过程产生的残差来达到将数据分类或者回归的算法。&lt;/p&gt;

&lt;p&gt;参考《统计学习方法》P147-&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/pinard/p/6140514.html&quot;&gt;https://www.cnblogs.com/pinard/p/6140514.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.zybuluo.com/yxd/note/611571&quot;&gt;https://www.zybuluo.com/yxd/note/611571&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/29765582&quot;&gt;https://zhuanlan.zhihu.com/p/29765582&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;再看一个很6的pdf：&lt;a href=&quot;https://daiwk.github.io/assets/gbdt.pdf&quot;&gt;https://daiwk.github.io/assets/gbdt.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;xgboost&quot;&gt;xgboost&lt;/h2&gt;

&lt;p&gt;xgboost的树的叶子和predict结果有什么关系&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;假设只有一棵树，
    &lt;ul&gt;
      &lt;li&gt;如果是回归，那就是叶子的得分+0.5的bias。&lt;a href=&quot;https://github.com/dmlc/xgboost/issues/1077&quot;&gt;https://github.com/dmlc/xgboost/issues/1077&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;如果是分类，那是叶子的得分sigmoid&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;假设有多棵树，那分类就是每棵树的叶子的得分加起来再过个sigmoid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://stats.stackexchange.com/questions/395697/what-is-an-intuitive-interpretation-of-the-leaf-values-in-xgboost-base-learners&quot;&gt;https://stats.stackexchange.com/questions/395697/what-is-an-intuitive-interpretation-of-the-leaf-values-in-xgboost-base-learners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;xgboost的predict函数线程不安全：&lt;a href=&quot;https://github.com/dmlc/xgboost/issues/311&quot;&gt;https://github.com/dmlc/xgboost/issues/311&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;解决：可以将handle放到一个pool里，每次要predict的时候，从pool里去拿。&lt;/p&gt;

&lt;p&gt;xgboost调参：&lt;a href=&quot;https://segmentfault.com/a/1190000014040317&quot;&gt;https://segmentfault.com/a/1190000014040317&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;注意，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grid_scores_&lt;/code&gt;这个参数改名了，改成了````cv_results_```。&lt;/p&gt;

&lt;p&gt;xgboost的坑：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://xgboost.readthedocs.io/en/latest/faq.html#why-do-i-see-different-results-with-sparse-and-dense-data&quot;&gt;https://xgboost.readthedocs.io/en/latest/faq.html#why-do-i-see-different-results-with-sparse-and-dense-data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;所以，如果我们用libsvm的格式来给python喂数据，如果用的是tree，那么需要加上missing参数：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;dtrain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xgb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DMatrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./output/%sins.dat.train.cls&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;dtest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xgb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DMatrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./output/%sins.dat.test.cls&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;missing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'booster'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'gbtree'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'objective'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'binary:logistic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'eval_metric'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'auc'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'max_depth'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'min_child_weight'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'eta'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'nthread'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'silent'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而在线预测的时候，默认值给0.0：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sample_rows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feature_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sample_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;DMatrixHandle&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 这里的0.0就是Missing的默认值&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;XGDMatrixCreateFromMat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sample_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;XGDMatrixFree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LOG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WARNING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fail in XGDMatrixCreateFromMat&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bst_ulong&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pred_result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BoosterHandlePool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handle_pool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xgb_dict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handle_pool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handle_pool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;BoosterHandle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret_p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;XGBoosterPredict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pred_result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;LOG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WARNING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fail in XGBoosterPredict&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;XGDMatrixFree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>传统机器学习算法</title>
   <link href="http://daiwk.github.io/posts/ml-traditional-ml.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/ml-traditional-ml</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#em%e7%ae%97%e6%b3%95&quot;&gt;EM算法&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;em算法&quot;&gt;EM算法&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/fuqiuai/article/details/79484421&quot;&gt;https://blog.csdn.net/fuqiuai/article/details/79484421&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;分布式的EM，以PLSI为例，看google www07发的那篇讲google news的推荐算法的：&lt;a href=&quot;https://www2007.org/papers/paper570.pdf&quot;&gt;Google News Personalization: Scalable Online Collaborative Filtering&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(T\)&lt;/code&gt;个训练样本来讲，新闻是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;，用户是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(u\)&lt;/code&gt;，引入一个中间隐变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(z\)&lt;/code&gt;，就有两个条件概率(CPD, conditional probability distribution)&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(z|u)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(s|z)\)&lt;/code&gt;，(也就是&lt;strong&gt;从u到z再到s&lt;/strong&gt;)然后就是要最大化条件似然的乘积，也就是要最小化负的经验log风险：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(\theta) = - \frac{1}{T}\sum ^{T}_{t=1}\log(p(s_t|u_t;\theta))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而这可以通过EM来解。&lt;/p&gt;

&lt;p&gt;E-step计算如下的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;值，也就是后验的隐变量的概率：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q^*(z;u,s;\hat{\theta}):=p(z|u,s;\hat{\theta})=\frac{\hat{p}(s|z)\hat{p}(z|u)}{\sum_{z\in Z}\hat{p}(s|z)\hat{p}(z|u)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;M-step使用上面的Q计算如下分布：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
p(s|z) &amp;amp;= \frac{\sum_uq^*(z;u,s;\hat{\theta})}{\sum_s\sum_uq^*(z;u,s;\hat{\theta})}\\ 
p(z|u) &amp;amp;= \frac{\sum_sq^*(z;u,s;\hat{\theta})}{\sum_z\sum_sq^*(z;u,s;\hat{\theta})}
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{p}\)&lt;/code&gt;指的是上一轮EM迭代产出的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;假设M=N=1kw，L=1000,也就是1kw个user，1kw个item，然后有1000个隐变量，那么需要存&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((M+N)\times L\times 4=80GB\)&lt;/code&gt;的内存来存储两个CPD(假设double占4字节)。单机是不行的，搞成分布式的。。&lt;/p&gt;

&lt;p&gt;加两个中间项：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{matrix}
N(z,s)=\sum_uq^*(z;u,s;\hat{\theta})\\
N(z)=\sum_s\sum_u^*(z;u,s;\hat{\theta})
\end{matrix}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;那么，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
q^*(z;u,s;\hat{\theta}):=p(z|u,s;\hat{\theta})=\frac{\frac{N(z,s)}{N(z)}\hat{p}(z|u)}{\sum_{z\in Z}\frac{N(z,s)}{N(z)}\hat{p}(z|u)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{p}(z|u) = \frac{\sum_sq^*(z;u,s;\hat{\theta})}{\sum_z\sum_sq^*(z;u,s;\hat{\theta})}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这样，就可以把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(s|z)\)&lt;/code&gt;干掉了&lt;/p&gt;

&lt;p&gt;对于每个给定的(u,s)的pair对，计算 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q^*(z;u,s;\hat{\theta}):=p(z|u,s;\hat{\theta})\)&lt;/code&gt;时，需要之前多轮迭代的信息：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{p}(z|u)\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N(z,s)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N(z)\)&lt;/code&gt;。当统计信息足够多时，对于一个(u,s)对，计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(q^*\)&lt;/code&gt;是可以独立且并行的。&lt;/p&gt;

&lt;p&gt;我们看一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\times K\)&lt;/code&gt;的shard。假设所有用户被hash到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;个group里，所有news被hash到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;个group里。也就是说&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((u_1,s_1)\)&lt;/code&gt;这个pair对，按下图就是被分到了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C_{R3}\)&lt;/code&gt;这个shard里去了。然后在一个shard中，只要存储这个shard里的所有(u,s)的pair对的点展信息就行了，来计算里面的CPD。所以一个shard需要存的CPD就只有1/R的用户的CPD和1/K的news的CPD。&lt;/p&gt;

&lt;p&gt;map输出3个kv：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((u,q^*)\)&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((i,q^*)\)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\((z,q^*)\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;对于reduce来说，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;负责s的reduce对所有的z计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N(z,s)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;负责u的reduce计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(p(z|u)\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;负责z的reduce计算&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(N(z)\)&lt;/code&gt;。每一个(u,s)的pair就会产出一条记录给负责z的reduce。。所以可以在shuffle阶段做一些预处理，防止reduce单节点太慢&lt;/li&gt;
&lt;/ul&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/google-news-rec-em-plsi-mr.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>online learning</title>
   <link href="http://daiwk.github.io/posts/ml-online-learning.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/ml-online-learning</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#xxx&quot;&gt;xxx&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#adaptive-regularization&quot;&gt;adaptive regularization&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;找到两个综述：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1802.02871.pdf&quot;&gt;Online Learning: A Comprehensive Survey&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.jmlr.org/papers/volume18/14-428/14-428.pdf&quot;&gt;A Survey of Algorithms and Analysis for Adaptive Online Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://plushunter.github.io/2017/07/26/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E7%AE%97%E6%B3%95%E7%B3%BB%E5%88%97%EF%BC%8831%EF%BC%89%EF%BC%9A%E5%9C%A8%E7%BA%BF%E6%9C%80%E4%BC%98%E5%8C%96%E6%B1%82%E8%A7%A3%EF%BC%88online%20Optimization%EF%BC%89/&quot;&gt;机器学习算法系列（31）：在线最优化求解（online Optimization）&lt;/a&gt;，这个真的写得非常好。。&lt;/p&gt;

&lt;p&gt;美团的&lt;a href=&quot;https://tech.meituan.com/2016/04/21/online-learning.html&quot;&gt;Online Learning算法理论与实践&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;FTRL可以参考&lt;a href=&quot;https://daiwk.github.io/posts/ml-ftrl.html&quot;&gt;https://daiwk.github.io/posts/ml-ftrl.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;xxx&quot;&gt;xxx&lt;/h2&gt;

&lt;p&gt;aaa&lt;/p&gt;

&lt;h2 id=&quot;adaptive-regularization&quot;&gt;adaptive regularization&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=2124313&quot;&gt;Learning recommender systems with adaptive regularization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/google19890102/article/details/73301949&quot;&gt;https://blog.csdn.net/google19890102/article/details/73301949&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这几篇paper的pdf可以从这里下载：&lt;a href=&quot;https://github.com/buptjz/Factorization-Machine&quot;&gt;https://github.com/buptjz/Factorization-Machine&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;可以发现，FM里的正则有一坨&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\lambda\)&lt;/code&gt;，&lt;/p&gt;

&lt;p&gt;然后有一篇Incremental Factorization Machines for Persistently Cold-starting Online Item Recommendation&lt;/p&gt;

&lt;p&gt;简单地理解，就是adaptive regularization是先更新参数，然后更新lambda；而这篇是借鉴了另一篇的，先看在测试集的效果，再更新参数的思想，所以一次处理一条，这条先拿来更新lambda，然后用更新完的lambda来更新参数。。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>损失函数</title>
   <link href="http://daiwk.github.io/posts/ml-loss-functions.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/ml-loss-functions</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%8D%9F%E5%A4%B1%E5%87%BD%E6%95%B0&quot;&gt;损失函数&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#hinge-loss&quot;&gt;Hinge loss&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#softmax-loss&quot;&gt;Softmax Loss&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#squared-loss&quot;&gt;Squared Loss&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#exponentially-loss&quot;&gt;Exponentially Loss&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%85%B6%E4%BB%96loss&quot;&gt;其他loss&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u010976453/article/details/78488279&quot;&gt;https://blog.csdn.net/u010976453/article/details/78488279&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;损失函数&quot;&gt;损失函数&lt;/h2&gt;

&lt;p&gt;是一个&lt;strong&gt;非负实值&lt;/strong&gt;函数，通常用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L(Y,f(x))\)&lt;/code&gt; 来表示。&lt;strong&gt;损失函数越小，模型的鲁棒性就越好。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;常用损失函数有：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;铰链损失（Hinge Loss）：主要用于支持向量机（SVM） 中；&lt;/li&gt;
  &lt;li&gt;交叉熵损失 （Cross Entropy Loss，Softmax Loss ）：用于Logistic 回归与Softmax 分类中；&lt;/li&gt;
  &lt;li&gt;平方损失（Square Loss）：主要是最小二乘法（OLS）中；&lt;/li&gt;
  &lt;li&gt;指数损失（Exponential Loss） ：主要用于Adaboost 集成学习算法中；&lt;/li&gt;
  &lt;li&gt;其他损失（如0-1损失，绝对值损失）&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;hinge-loss&quot;&gt;Hinge loss&lt;/h3&gt;

&lt;p&gt;Hinge loss的通用的函数表达式为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[L(m_i) = max(0,1-m_i(w))\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;表示如果被正确分类，损失是0，否则损失就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(1-m_i(w)\)&lt;/code&gt;，其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(m_i(w)\)&lt;/code&gt;就是实际值和预测值之差。下图的红色虚线就是hinge loss&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/hinge-loss.jpg&quot; style=&quot;max-height: 200px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;Hinge 可以用来解&lt;strong&gt;间距最大化&lt;/strong&gt;的问题，最有代表性的就是SVM问题：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
    \underset{w,\zeta}{argmin} \frac{1}{2}||w||^2+ C\sum_i \zeta_i \\
st.\quad \forall y_iw^Tx_i \geq 1- \zeta_i \\
\zeta_i \geq 0
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;将约束项进行变形，则为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
    \zeta_i \geq 1-y_iw^Tx_i
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;进一步地，可以把损失函数写为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{equation}\begin{split}J(w)&amp;amp;=\frac{1}{2}||w||^2 + C\sum_i max(0,1-y_iw^Tx_i) \\
&amp;amp;= \frac{1}{2}||w||^2 + C\sum_i max(0,1-m_i(w)) \\
&amp;amp;= \frac{1}{2}||w||^2 + C\sum_i L_{Hinge}(m_i)
\end{split}\end{equation}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因此， SVM 的损失函数可以看作是&lt;strong&gt;L2-norm&lt;/strong&gt;和&lt;strong&gt;Hinge loss&lt;/strong&gt;之和。&lt;/p&gt;

&lt;h3 id=&quot;softmax-loss&quot;&gt;Softmax Loss&lt;/h3&gt;

&lt;p&gt;平方损失函数可以通过线性回归在假设样本是高斯分布的条件下推导得到，而逻辑回归得到的并不是平方损失。在逻辑回归的推导中，它&lt;strong&gt;假设样本服从伯努利分布（0-1分布）&lt;/strong&gt;，然后求得满足该分布的似然函数，接着取对数求极值等等。而逻辑回归并没有求似然函数的极值，而是把极大化当做是一种思想，进而推导出它的经验风险函数为：最小化负的似然函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\max F(y, f(x)) \rightarrow \min -F(y, f(x)))\)&lt;/code&gt;，从损失函数的视角来看，它就成了Softmax 损失函数了。&lt;/p&gt;

&lt;p&gt;log损失函数的标准形式：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(Y,P(Y|X)) = -\log P(Y|X)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;取对数是为了方便计算极大似然估计，因为在MLE中，直接求导比较困难，所以通常都是先取对数再求导找极值点。损失函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L(Y, P(Y|X))\)&lt;/code&gt;表达的是样本X 在分类Y的情况下，使概率&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(Y|X)\)&lt;/code&gt;达到最大值（换言之，就是利用已知的样本分布，找到最有可能（即最大概率）导致这种分布的参数值；或者说什么样的参数才能使我们观测到目前这组数据的概率最大）。因为log函数是单调递增的，所以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(logP(Y|X)\)&lt;/code&gt;，因此在前面加上负号之后，最大化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(Y|X)\)&lt;/code&gt;就等价于最小化&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L\)&lt;/code&gt;了。&lt;/p&gt;

&lt;h3 id=&quot;squared-loss&quot;&gt;Squared Loss&lt;/h3&gt;

&lt;p&gt;最小二乘法是线性回归的一种，OLS将问题转化成了一个凸优化问题。在线性回归中，它假设样本和噪声都服从高斯分布（中心极限定理），最后通过极大似然估计（MLE）可以推导出最小二乘式子。最小二乘的基本原则是：最优拟合直线应该是使各点到回归直线的距离和最小的直线，即平方和最小。&lt;/p&gt;

&lt;p&gt;平方损失（Square loss）的标准形式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(Y, f(X)) = (Y - f(X))^2
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;当样本个数为n时，此时的损失函数为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(Y, f(X)) =\sum_{i=1} ^n (Y-f(X))^2
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;实际应用中，我们使用均方差（MSE）作为一项衡量指标，公式如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
MSE = \frac{1}{n} \sum_{i=1} ^{n} (\tilde{Y_i} - Y_i )^2
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;exponentially-loss&quot;&gt;Exponentially Loss&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(Y,f(X)) = \exp [-Yf(X)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;主要应用于 Boosting 算法中，在Adaboost 算法中，经过 m 次迭代后，可以得到&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f_m(x)=f_{m-1}(x) + \alpha_m G_m(x)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Adaboost 每次迭代时的目的都是找到最小化下列式子的参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\alpha\)&lt;/code&gt;和G：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\arg \min_{\alpha,G} = \sum_{i=1}^N \exp[-y_i(f_{m-1}(x_i) + \alpha G(x_i))]
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;所以，Adabooost 的目标式子就是指数损失，在给定n个样本的情况下，Adaboost 的损失函数为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(Y,f(X)) = \frac{1}{2} \sum_{i=1}^n \exp[-y_if(x_I)]
\]&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;其他loss&quot;&gt;其他loss&lt;/h3&gt;

&lt;p&gt;0-1 损失函数：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(Y,f(X)) =\left\{
\begin{aligned}
0 &amp;amp; \quad if \quad  Y \neq f(X)\\
1 &amp;amp; \quad if \quad Y = f(X)\\
\end{aligned}
\right.
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;绝对值损失函数：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
L(Y,f(X)) =  |Y-f(X)|
\]&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>few-shot learning</title>
   <link href="http://daiwk.github.io/posts/ml-few-shot-learning.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/ml-few-shot-learning</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%ae%9a%e4%b9%89&quot;&gt;定义&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%88%86%e7%b1%bb&quot;&gt;分类&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#model-based&quot;&gt;model based&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#one-shot-learning-with-memory-augmented-neural-networks&quot;&gt;One-shot learning with memory-augmented neural networks&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#meta-networks&quot;&gt;Meta networks&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#metric-based&quot;&gt;metric based&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#siamese-network&quot;&gt;Siamese Network&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#match-network&quot;&gt;Match Network&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#prototype-network&quot;&gt;Prototype Network&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#relation-network&quot;&gt;Relation Network&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#optimization-based&quot;&gt;optimization based&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#optimization-as-a-model-for-few-shot-learning&quot;&gt;Optimization as a model for few-shot learning&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#model-agnostic-meta-learning-for-fast-adaptation-of-deep-networks&quot;&gt;Model-agnostic meta-learning for fast adaptation of deep networks&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#zero-shot%e7%9b%b8%e5%85%b3&quot;&gt;zero-shot相关&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%a7%a3%e5%86%b3%e7%89%b9%e5%be%81%e6%b7%b7%e6%b7%86afc-gan&quot;&gt;解决特征混淆:AFC-GAN&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;cvpr2019相关的参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650763945&amp;amp;idx=3&amp;amp;sn=bac11c77b276c472b78d02d42f749e95&amp;amp;chksm=871ab6d7b06d3fc117cc068d20de12fbc7abfaa9e07c70f3a81205bc514999941d9c6f967ab6&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=%2BD9Ask8qPVeDCkEHTF8NEBVBQX9YmDDkPy9VdMIfOYJ2VtpyHOOhIYdS3wUnvPjn#rd&quot;&gt;CVPR 2019提前看：少样本学习专题&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650759590&amp;amp;idx=3&amp;amp;sn=d7573d59fdffae5fc7bb83ea77a26fa6&amp;amp;chksm=871aa5d8b06d2cce197f93547df18d412c173dcb2149f31797c1c9fbea2ec7948f6b1100ab3e&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0429y1qxBdLrdZ8C5KgRqP0e&amp;amp;pass_ticket=ZltpB2rBQ1hXYEBIClPX5yEh187BJtWG0mhs8mqho%2F%2FHR%2BUZZqkJ8efTvcnsT5KF#rd&quot;&gt;小样本学习（Few-shot Learning）综述&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/qq_16234613/article/details/79902085&quot;&gt;当小样本遇上机器学习 fewshot learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://arxiv.org/pdf/1904.05046.pdf&quot;&gt;Few-shot Learning: A Survey&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;关于meta learning，可以参考&lt;a href=&quot;https://daiwk.github.io/posts/dl-meta-learning.html&quot;&gt;https://daiwk.github.io/posts/dl-meta-learning.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247496949&amp;amp;idx=1&amp;amp;sn=f0c2ec025be78ebc9f87fe30b78967c3&amp;amp;chksm=96ea2b75a19da26365cae1a30f7e8d7363fae366ced91277ee0380758b98096a7d839e80df7e&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=OEoJxI2kFvfmi6pDQlY3W%2FGC2MeNgyiIRuMCWgKgSHf5DYmZLcpg4jkhV1VOz5EE#rd&quot;&gt;基于小样本学习的意图识别冷启动&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;定义&quot;&gt;定义&lt;/h2&gt;

&lt;p&gt;Few-shot Learning 是 Meta Learning 在&lt;strong&gt;监督学习&lt;/strong&gt;领域的应用&lt;/p&gt;

&lt;p&gt;Meta Learning，又称为learning to learn，在meta training阶段将数据集分解为不同的&lt;strong&gt;meta task&lt;/strong&gt;，去学习类别变化的情况下模型的泛化能力，在&lt;strong&gt;meta testing&lt;/strong&gt;阶段，面对全新的类别，&lt;strong&gt;不需要变动已有的模型&lt;/strong&gt;，就可以完成分类。&lt;/p&gt;

&lt;p&gt;few-shot的训练集中包含了很多的类别，每个类别中有多个样本。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;在训练阶段，会在训练集中随机&lt;strong&gt;抽取C个类别&lt;/strong&gt;，每个类别&lt;strong&gt;K个样本&lt;/strong&gt;（总共CK个数据），构建一个 meta-task，作为模型的&lt;strong&gt;支撑集（support set）&lt;/strong&gt;输入；&lt;/li&gt;
  &lt;li&gt;再从这&lt;strong&gt;C个类&lt;/strong&gt;中&lt;strong&gt;剩余的数据&lt;/strong&gt;中抽取一批（batch）样本作为模型的&lt;strong&gt;预测对象（batch set）&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;即要求模型从C*K个数据中学会如何区分这C个类别，这样的任务被称为&lt;strong&gt;C-way K-shot&lt;/strong&gt;(C类，每个类K个样本)问题。&lt;/p&gt;

&lt;p&gt;训练过程中，每次训练 &lt;strong&gt;(episode)&lt;/strong&gt;都会&lt;strong&gt;采样&lt;/strong&gt;得到&lt;strong&gt;不同meta-task&lt;/strong&gt;。这种机制使得模型学会&lt;strong&gt;不同meta-task&lt;/strong&gt;中的&lt;strong&gt;共性部分&lt;/strong&gt;，比如如何提取重要特征及比较样本相似等，&lt;strong&gt;忘掉&lt;/strong&gt; meta-task中&lt;strong&gt;task相关&lt;/strong&gt;部分，因此，在面对新的&lt;strong&gt;未见过的 meta-task&lt;/strong&gt;时，也能较好地进行分类。&lt;/p&gt;

&lt;h2 id=&quot;分类&quot;&gt;分类&lt;/h2&gt;

&lt;p&gt;主要分为Mode Based，Metric Based 和 Optimization Based三大类。&lt;/p&gt;

&lt;h3 id=&quot;model-based&quot;&gt;model based&lt;/h3&gt;

&lt;p&gt;通过模型结构的设计快速在&lt;strong&gt;少量样本上更新参数&lt;/strong&gt;，直接建立输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;和预测值&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;的映射函数。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P_{\theta}(y | x, S)=f_{\theta}(x, S)
\]&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;one-shot-learning-with-memory-augmented-neural-networks&quot;&gt;One-shot learning with memory-augmented neural networks&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1605.06065&quot;&gt;One-shot learning with memory-augmented neural networks&lt;/a&gt;使用&lt;strong&gt;记忆增强&lt;/strong&gt;的方法。基于记忆的神经网络方法早在2001年被证明可以用于meta-learning。通过&lt;strong&gt;权重更新&lt;/strong&gt;来&lt;strong&gt;调节bias&lt;/strong&gt;，并且通过学习&lt;strong&gt;将表达&lt;/strong&gt;快速&lt;strong&gt;缓存&lt;/strong&gt;到&lt;strong&gt;记忆&lt;/strong&gt;中来调节输出。&lt;/p&gt;

&lt;p&gt;利用循环神经网络的&lt;strong&gt;内部记忆&lt;/strong&gt;单元&lt;strong&gt;无法扩展&lt;/strong&gt;到需要对&lt;strong&gt;大量新信息&lt;/strong&gt;进行编码的&lt;strong&gt;新任务&lt;/strong&gt;上。因此，需要让存储在记忆中的表达既要&lt;strong&gt;稳定&lt;/strong&gt;又要是&lt;strong&gt;元素粒度访问&lt;/strong&gt;的，前者是说当&lt;strong&gt;需要时就能&lt;/strong&gt;可靠地访问，后者是说可&lt;strong&gt;选择性地&lt;/strong&gt;访问相关的信息；另外，参数数量&lt;strong&gt;不能被内存的大小束缚&lt;/strong&gt;。&lt;strong&gt;神经图灵机&lt;/strong&gt;（NTMs）和&lt;strong&gt;记忆网络&lt;/strong&gt;就符合这种必要条件。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/dl-ntm-memory-networks.html&quot;&gt;https://daiwk.github.io/posts/dl-ntm-memory-networks.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;文章基于&lt;strong&gt;神经网络图灵机（NTMs）&lt;/strong&gt;的思想，因为 NTMs 能通过外部存储（external memory）进行短时记忆，并能通过&lt;strong&gt;缓慢权值更新&lt;/strong&gt;来进行&lt;strong&gt;长时记忆&lt;/strong&gt;，NTMs可以学习将表达存入记忆的策略，并如何用这些表达来进行预测。由此，文章方法可以&lt;strong&gt;快速准确&lt;/strong&gt;地&lt;strong&gt;预测&lt;/strong&gt;那些&lt;strong&gt;只出现过一次的数据&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;基于LSTM等RNN的模型，将数据&lt;strong&gt;看成序列&lt;/strong&gt;来训练，在测试时输入新的类的样本进行分类。&lt;/p&gt;

&lt;p&gt;如下图，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;时刻，模型输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\left(\mathbf{x}_{t}, y_{t-1}\right)\)&lt;/code&gt;，即&lt;strong&gt;当前时刻&lt;/strong&gt;的&lt;strong&gt;样本&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_t\)&lt;/code&gt;，&lt;strong&gt;上一时刻&lt;/strong&gt;的&lt;strong&gt;真实label&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_{t-1}\)&lt;/code&gt;，然后基于这两个来&lt;strong&gt;预测当前时刻的类别&lt;/strong&gt;。在新episode开始时，要对样本和label进行shuffle。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/one-shot-memory-augment-a.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;如下图，在external memory里面里存储绑定好（bind &amp;amp; encode，得到bound information）的上一次的输入的表示和对应的label（representation-class label）。具体地，当前时间步的输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_t\)&lt;/code&gt;的label&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_t\)&lt;/code&gt;是在下一时间步给出的（上面那段话说的）。如果后面的时间步(图中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t+n\)&lt;/code&gt;）中的样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{t+n}\)&lt;/code&gt;，有这个class的样本出现的时候，应该能够直接从external memory中retrieve到bound information，并直接进行预测。从这个prediction step进行反向传播时对&lt;strong&gt;之前时间步的权重更新&lt;/strong&gt;进行shape，从而提升这种bind strategy。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/one-shot-memory-augment-b.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h4 id=&quot;meta-networks&quot;&gt;Meta networks&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.00837&quot;&gt;Meta Networks&lt;/a&gt;的快速泛化能力源自其&lt;strong&gt;“快速权重”&lt;/strong&gt;的机制，在&lt;strong&gt;训练&lt;/strong&gt;过程中&lt;strong&gt;产生的梯度&lt;/strong&gt;被用来作为&lt;strong&gt;快速权重的生成&lt;/strong&gt;。模型包含一个&lt;strong&gt;meta learner&lt;/strong&gt;和一个&lt;strong&gt;base learner&lt;/strong&gt;：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;meta learner：用于&lt;strong&gt;学习meta task之间的泛化信息&lt;/strong&gt;，并使用&lt;strong&gt;memory机制&lt;/strong&gt;保存这种信息&lt;/li&gt;
  &lt;li&gt;base learner：用于&lt;strong&gt;快速适应新的task&lt;/strong&gt;，并&lt;strong&gt;和meta learner交互&lt;/strong&gt;产生预测输出&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;metric-based&quot;&gt;metric based&lt;/h3&gt;

&lt;p&gt;通过度量&lt;strong&gt;batch集中&lt;/strong&gt;的样本和&lt;strong&gt;support集&lt;/strong&gt;中样本的&lt;strong&gt;距离&lt;/strong&gt;，借助最近邻的思想完成分类&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P_{\theta}(y | x, S)=\sum_{\left(x_{i}, y_{i}\right) \in S} k_{\theta}\left(x, x_{i}, S\right) y_{i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果在 Few-shot Learning 的任务中去训练普通的基于cross-entropy的&lt;strong&gt;神经网络分类器&lt;/strong&gt;，那么几乎&lt;strong&gt;肯定是会过拟合&lt;/strong&gt;，因为神经网络分类器中有数以万计的参数需要优化。&lt;/p&gt;

&lt;p&gt;所以，很多&lt;strong&gt;非参数化的方法&lt;/strong&gt;（最近邻、K-近邻、Kmeans）是不需要优化参数的，因此可以在meta-learning的框架下构造一种可以端到端训练的 few-shot 分类器。该方法是&lt;strong&gt;对样本间距离分布进行建模&lt;/strong&gt;，使得&lt;strong&gt;同类样本靠近，异类样本远离&lt;/strong&gt;。&lt;/p&gt;

&lt;h4 id=&quot;siamese-network&quot;&gt;Siamese Network&lt;/h4&gt;

&lt;p&gt;siamese的经典论文：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.researchgate.net/publication/4156225_Learning_a_similarity_metric_discriminatively_with_application_to_face_verification?enrichId=rgreq-69df456718ba3ae3e30d3cdc40686c21-XXX&amp;amp;enrichSource=Y292ZXJQYWdlOzQxNTYyMjU7QVM6MTAxMzE1NTUyMjE5MTQyQDE0MDExNjY5MTg0MTE%3D&amp;amp;el=1_x_3&amp;amp;_esc=publicationCoverPdf&quot;&gt;Learning a similarity metric discriminatively, with application to face verification&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.researchgate.net/publication/221620245_Signature_Verification_Using_a_Siamese_Time_Delay_Neural_Network?enrichId=rgreq-4152865765293445c4c6b41da78b8790-XXX&amp;amp;enrichSource=Y292ZXJQYWdlOzIyMTYyMDI0NTtBUzoxODEyMDE3ODAzNTUwNzJAMTQyMDIxMzI3OTMxNA%3D%3D&amp;amp;el=1_x_3&amp;amp;_esc=publicationCoverPdf&quot;&gt;Signature Verification Using a Siamese Time Delay Neural Network&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cs.cmu.edu/~rsalakhu/papers/oneshot1.pdf&quot;&gt;Siamese neural networks for one-shot image recognition&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;通过&lt;strong&gt;有监督&lt;/strong&gt;的方式训练&lt;strong&gt;孪生网络&lt;/strong&gt;来学习，然后重用网络所提取的特征进行one/few-shot学习。&lt;/p&gt;

&lt;p&gt;孪生网络是一个双路的神经网络，训练时，通过组合的方式构造不同的成对样本，输入网络进行训练，在最上层通过&lt;strong&gt;样本对的距离&lt;/strong&gt;判断他们&lt;strong&gt;是否属于同一个类&lt;/strong&gt;，并产生对应的概率分布。&lt;/p&gt;

&lt;p&gt;在&lt;strong&gt;预测阶段&lt;/strong&gt;，孪生网络处理&lt;strong&gt;测试&lt;/strong&gt;样本和&lt;strong&gt;支撑&lt;/strong&gt;集之间&lt;strong&gt;每一个样本对&lt;/strong&gt;，最终预测结果为&lt;strong&gt;支撑集上概率最高的类别&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;可以参考下&lt;a href=&quot;https://yq.aliyun.com/articles/209297&quot;&gt;https://yq.aliyun.com/articles/209297&lt;/a&gt;，翻译自&lt;a href=&quot;https://sorenbouma.github.io/blog/oneshot/&quot;&gt;https://sorenbouma.github.io/blog/oneshot/&lt;/a&gt;，代码：&lt;a href=&quot;https://github.com/sorenbouma/keras-oneshot&quot;&gt;https://github.com/sorenbouma/keras-oneshot&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;match-network&quot;&gt;Match Network&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1606.04080&quot;&gt;Matching networks for one shot learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;为&lt;strong&gt;支撑&lt;/strong&gt;集和&lt;strong&gt;Batch&lt;/strong&gt;集构建&lt;strong&gt;不同的编码器&lt;/strong&gt;，最终分类器的输出是&lt;strong&gt;支撑集&lt;/strong&gt;样本和&lt;strong&gt;query&lt;/strong&gt;之间预测值的&lt;strong&gt;加权求和&lt;/strong&gt;。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/few-shot-match-network.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;建模过程的创新，文章提出了基于&lt;strong&gt;memory&lt;/strong&gt;和&lt;strong&gt;attention&lt;/strong&gt;的matching nets，使得可以快速学习。&lt;/p&gt;

&lt;p&gt;训练过程的创新，文章基于传统机器学习的一个原则，即训练和测试是要在同样条件下进行的，提出在&lt;strong&gt;训练时&lt;/strong&gt;不断地&lt;strong&gt;让网络只看每一类&lt;/strong&gt;的&lt;strong&gt;少量样本&lt;/strong&gt;，这将&lt;strong&gt;和测试的过程保持一致&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;构建一个从有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;个image-label的pair对的样本的支撑集&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S=\left\{\left(x_{i}, y_{i}\right)\right\}_{i=1}^{k}\)&lt;/code&gt;到分类器&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c_{S}(\hat{x})\)&lt;/code&gt;的一个map，使得given一个测试样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{x}\)&lt;/code&gt;能得到他的输出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{y}\)&lt;/code&gt;的概率分布。&lt;/p&gt;

&lt;p&gt;定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P(\hat{y} | \hat{x}, S):S \rightarrow c_{S}(\hat{x})\)&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;是通过nn的参数构建的。所以，给定一个没见过的样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{x}\)&lt;/code&gt;和支撑集&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;，最终的预测结果就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\arg \max _{y} P(y | \hat{x}, S)\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;模型的最终输出形式就是：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\hat{y}=\sum_{i=1}^{k} a\left(\hat{x}, x_{i}\right) y_{i}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_{i}, y_{i}\)&lt;/code&gt;来自于支撑集&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S=\left\{\left(x_{i}, y_{i}\right)\right\}_{i=1}^{k}\)&lt;/code&gt;。而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;是attention机制，是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(X \times X\)&lt;/code&gt;上的一种kernel，所以类似于一个kernel density estimator。&lt;/p&gt;

&lt;p&gt;attention kernel一个常见的形式如下，对cos距离&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;求softmax：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
a\left(\hat{x}, x_{i}\right)=e^{c\left(f(\hat{x}), g\left(x_{i}\right)\right)} / \sum_{j=1}^{k} e^{c\left(f(\hat{x}), g\left(x_{j}\right)\right)}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f\)&lt;/code&gt;是对测试集样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\hat{x}\)&lt;/code&gt;的emb，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g\)&lt;/code&gt;是对支撑集样本&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;的emb。&lt;/p&gt;

&lt;p&gt;进一步地，期望支撑集学到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g\)&lt;/code&gt;可以modify测试集的emb函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f\)&lt;/code&gt;。可以通过如下两种方式来：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;把整个支撑集的数据都告诉&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g\)&lt;/code&gt;，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g\)&lt;/code&gt;不只是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g(x_i)\)&lt;/code&gt;，应该是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g(x_i,S)\)&lt;/code&gt;。可以把整个支撑集看成一个sequence，然后用一个双向lstm来根据整个支撑集这个context来进行emb。&lt;/li&gt;
  &lt;li&gt;Fully Conditional Embeddings (FCE)：使用一个对整个set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;有read-attention的LSTM。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
f(\hat{x}, S)=\operatorname{attLSTM}\left(f^{\prime}(\hat{x}), g(S), K\right)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f^{\prime}(\hat{x})\)&lt;/code&gt;是LSTM的输入features(在每个时间步都是&lt;strong&gt;常量&lt;/strong&gt;)；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;是LSTM的unrolling steps的一个固定数目；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g(S)\)&lt;/code&gt;是对集合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;中每个元素&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;的emb函数。&lt;/p&gt;

&lt;p&gt;参考附录，attLSTM的一个时间步的运算如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} 
\hat{h}_{k}, c_{k} &amp;amp;=\operatorname{LSTM}\left(f^{\prime}(\hat{x}),\left[h_{k-1}, r_{k-1}\right], c_{k-1}\right) \\ 
h_{k} &amp;amp;=\hat{h}_{k}+f^{\prime}(\hat{x}) \\ 
r_{k-1} &amp;amp;=\sum_{i=1}^{|S|} a\left(h_{k-1}, g\left(x_{i}\right)\right) g\left(x_{i}\right) \\ 
a\left(h_{k-1}, g\left(x_{i}\right)\right) &amp;amp;=\operatorname{softmax}\left(h_{k-1}^{T} g\left(x_{i}\right)\right) 
\end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;假设&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g^{\prime}\left(x_{i}\right)\)&lt;/code&gt;是一个神经网络（类似上面提到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(f^{\prime}\)&lt;/code&gt;），定义&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g\left(x_{i}, S\right)=\vec{h}_{i}+\overleftarrow{h}_{i}+g^{\prime}\left(x_{i}\right)\)&lt;/code&gt;，从而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(g(S)\)&lt;/code&gt;如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned} \vec{h}_{i}, \vec{c}_{i} &amp;amp;=\operatorname{LSTM}\left(g^{\prime}\left(x_{i}\right), \vec{h}_{i-1}, \vec{c}_{i-1}\right) \\ 
\overleftarrow{h}_{i}, \overleftarrow{c}_{i} &amp;amp;=\operatorname{LSTM}\left(g^{\prime}\left(x_{i}\right), \overleftarrow{h}_{i+1}, \overleftarrow{c}_{i+1}\right) 
\end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;prototype-network&quot;&gt;Prototype Network&lt;/h4&gt;

&lt;p&gt;每个类别都存在一个原型表达，该类的原型是&lt;strong&gt;support set&lt;/strong&gt;在&lt;strong&gt;embedding空间中的均值&lt;/strong&gt;。然后，分类问题变成在&lt;strong&gt;embedding空间中的最近邻&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;文章采用在Bregman散度下的指数族分布的混合密度估计，文章在训练时采用&lt;strong&gt;比测试&lt;/strong&gt;时&lt;strong&gt;更多的类别数&lt;/strong&gt;，即&lt;strong&gt;训练&lt;/strong&gt;时每个episodes采用20个类（&lt;strong&gt;20 way&lt;/strong&gt;），而&lt;strong&gt;测试&lt;/strong&gt;对在5个类（&lt;strong&gt;5 way&lt;/strong&gt;）中进行，其效果相对训练时也采用5 way的提升了2.5个百分点。&lt;/p&gt;

&lt;h4 id=&quot;relation-network&quot;&gt;Relation Network&lt;/h4&gt;

&lt;p&gt;前面介绍的几个网络结构在最终的&lt;strong&gt;距离度量&lt;/strong&gt;上都使用了&lt;strong&gt;固定的度量&lt;/strong&gt;方式，如&lt;strong&gt;cosine，欧式距离等&lt;/strong&gt;，这种模型结构下所有的&lt;strong&gt;学习过程&lt;/strong&gt;都发生在&lt;strong&gt;样本的embedding&lt;/strong&gt;阶段。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1711.06025&quot;&gt;Learning to compare: Relation network for few-shot learning&lt;/a&gt;认为，度量方式也是网络中非常重要的一环，需要对其进行建模，所以该网络不满足单一且固定的距离度量方式，而是&lt;strong&gt;训练一个网络来学习（例如 CNN）距离的度量方式&lt;/strong&gt;，在 loss 方面也有所改变，考虑到relation network&lt;strong&gt;更多的关注relation score&lt;/strong&gt;，更像一种回归，而非 0/1 分类，所以使用了&lt;strong&gt;MSE&lt;/strong&gt;取代了cross-entropy。&lt;/p&gt;

&lt;h3 id=&quot;optimization-based&quot;&gt;optimization based&lt;/h3&gt;

&lt;p&gt;认为&lt;strong&gt;普通的梯度下降&lt;/strong&gt;方法&lt;strong&gt;难以&lt;/strong&gt;在few-shot场景下&lt;strong&gt;拟合&lt;/strong&gt;，因此通过调整优化方法来完成小样本分类的任务。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P_{\theta}(y | x, S)=f_{\theta(S)}(x)
\]&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;optimization-as-a-model-for-few-shot-learning&quot;&gt;Optimization as a model for few-shot learning&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://openreview.net/pdf?id=rJY0-Kcll&quot;&gt;Optimization as a model for few-shot learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;文章发现：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;首先，这些梯度优化算法包括momentum, adagrad, adadelta, ADAM等，&lt;strong&gt;无法在几步内完成优化&lt;/strong&gt;，特别是在非凸的问题上，&lt;strong&gt;多种超参的选取无法保证收敛的速度&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;其次，&lt;strong&gt;不同任务&lt;/strong&gt;&lt;strong&gt;分别随机初始化&lt;/strong&gt;会影响任务收敛到好的解上。虽然 finetune 这种迁移学习能缓解这个问题，但当新数据相对原始数据偏差比较大时，迁移学习的性能会大大下降。我们需要一个&lt;strong&gt;系统的学习通用初始化&lt;/strong&gt;，使得训练从一个好的点开始，它和迁移学习不同的是，它能保证该初始化能&lt;strong&gt;让finetune从一个好的点开始。&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;文章&lt;strong&gt;学习&lt;/strong&gt;的是一个&lt;strong&gt;模型参数的更新函数&lt;/strong&gt;，即更新规则。它不是在多轮的episodes学习一个单模型，而是在&lt;strong&gt;每个episode学习特定的模型&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;学习基于梯度下降的参数更新算法，采用LSTM表达meta learner，用其&lt;strong&gt;状态&lt;/strong&gt;表达&lt;strong&gt;目标分类器的参数的更新&lt;/strong&gt;，最终学会如何在新的分类任务上，对分类器网络（learner）进行&lt;strong&gt;初始化&lt;/strong&gt;和&lt;strong&gt;参数更新&lt;/strong&gt;。这个优化算法同时考虑&lt;strong&gt;一个任务&lt;/strong&gt;的&lt;strong&gt;短时知识&lt;/strong&gt;和跨&lt;strong&gt;多个任务&lt;/strong&gt;的&lt;strong&gt;长时知识&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;设定目标为通过少量的迭代步骤捕获优化算法的泛化能力，由此meta learner可以训练让learner在每个任务上收敛到一个好的解。另外，通过捕获所有任务之前&lt;strong&gt;共享的基础知识&lt;/strong&gt;，进而更好地&lt;strong&gt;初始化&lt;/strong&gt;learner。&lt;/p&gt;

&lt;p&gt;以训练 miniImage 数据集为例，&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;训练&lt;/strong&gt;过程中，从训练集（64 个类，每类 600 个样本）中随机采样 5 个类，每个类 5 个样本，构成&lt;strong&gt;支撑集&lt;/strong&gt;，去&lt;strong&gt;学习learner&lt;/strong&gt;；然后从训练集的样本（采出的 5 个类，每类剩下的样本）中采样构成&lt;strong&gt;Batch集&lt;/strong&gt;，集合中每类有 15 个样本，用来&lt;strong&gt;获得learner的loss&lt;/strong&gt;，去&lt;strong&gt;学习meta leaner&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;测试&lt;/strong&gt;的流程一样，从测试集（16 个类，每类 600 个样本）中随机采样 5 个类，每个类 5 个样本，构成&lt;strong&gt;支撑集&lt;/strong&gt;Support Set，去&lt;strong&gt;学习learner&lt;/strong&gt;；然后从测试集剩余的样本（采出的 5 个类，每类剩下的样本）中采样构成&lt;strong&gt;Batch集&lt;/strong&gt;，集合中每类有 15 个样本，用来&lt;strong&gt;获得learner的参数&lt;/strong&gt;，进而&lt;strong&gt;得到预测的类别概率&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;这两个过程分别如下图中虚线左侧和右侧。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/fewshot-optimization-as-a-model.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;meta learner的目标是在各种&lt;strong&gt;不同的学习任务&lt;/strong&gt;上&lt;strong&gt;学出一个模型&lt;/strong&gt;，使得可以仅用少量的样本就能解决一些新的学习任务。这种任务的挑战是模型需要结合之前的经验和当前新任务的少量样本信息，并避免在新数据上过拟合。&lt;/p&gt;

&lt;h4 id=&quot;model-agnostic-meta-learning-for-fast-adaptation-of-deep-networks&quot;&gt;Model-agnostic meta-learning for fast adaptation of deep networks&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.03400&quot;&gt;Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;使得可以在小量样本上，用少量的迭代步骤就可以获得较好的泛化性能，而且模型是容易 fine-tine 的。而且这个方法无需关心模型的形式，也不需要为 meta learning 增加新的参数，直接用梯度下降来训练 learner。&lt;/p&gt;

&lt;h2 id=&quot;zero-shot相关&quot;&gt;zero-shot相关&lt;/h2&gt;

&lt;h3 id=&quot;解决特征混淆afc-gan&quot;&gt;解决特征混淆:AFC-GAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/aaz-s87vorroyepNCd9-AA&quot;&gt;节后收心困难？这15篇论文，让你迅速找回学习状态&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1909.07615&quot;&gt;Alleviating Feature Confusion for Generative Zero-shot Learning&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是电子科技大学和昆士兰大学发表于 ACM MM 2019 的工作。论文首次提出了生成式零样本学习中的特征混淆（feature confusion）问题，并提出了一种简单可行的解决特征混淆的方法。此外，作者还首次提出了一个特征混淆的度量指标。零样本学习结果在公开数据集上达到目前 SOTA 水平。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/lijin118/AFC-GAN&quot;&gt;https://github.com/lijin118/AFC-GAN&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>主动学习</title>
   <link href="http://daiwk.github.io/posts/ml-active-learning.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/ml-active-learning</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/qaLQK3uzaeyp68AbL0aOOQ&quot;&gt;怎么在视频标注上省钱？这里有一个面向视频推荐的多视图主动学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.ijcai.org/proceedings/2019/0284.pdf&quot;&gt;Multi-View Active Learning for Video Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/qTZzQZEqHIJt_LAhYMd5lw&quot;&gt;2019年主动学习有哪些进展？答案在这三篇论文里&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>【置顶】导引——推荐系统论文集合</title>
   <link href="http://daiwk.github.io/posts/links-navigation-recommender-system.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/links-navigation-recommender-system</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%90%84%e7%a7%8d%e7%bb%bc%e8%bf%b0&quot;&gt;各种综述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e4%b8%80%e4%ba%9b%e7%bb%8f%e5%85%b8%e8%ae%ba%e6%96%87&quot;&gt;一些经典论文&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#dssm&quot;&gt;dssm&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#youtube&quot;&gt;youtube&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%ba%8f%e5%88%97%e5%bb%ba%e6%a8%a1&quot;&gt;序列建模&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%a0%87%e7%ad%be%e4%bd%93%e7%b3%bb&quot;&gt;标签体系&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%9c%80%e6%96%b0paper1&quot;&gt;最新paper1&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#a-review-on-deep-learning-for-recommender-systems-challenges-and-remedies&quot;&gt;A review on deep learning for recommender systems: challenges and remedies&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#next-item-recommendation-with-self-attention&quot;&gt;Next Item Recommendation with Self-Attention&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#metric-factorization-recommendation-beyond-matrix-factorization&quot;&gt;Metric Factorization: Recommendation beyond Matrix Factorization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#collaborative-memory-network-for-recommendation-systems&quot;&gt;Collaborative Memory Network for Recommendation Systems&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#evaluation-of-session-based-recommendation-algorithms&quot;&gt;Evaluation of Session-based Recommendation Algorithms&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ripplenet-propagating-user-preferences-on-the-knowledge-graph-for-recommender-systems&quot;&gt;RippleNet: Propagating User Preferences on the Knowledge Graph for Recommender Systems&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#content-based-citation-recommendation&quot;&gt;Content-Based Citation Recommendation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#explainable-recommendation-a-survey-and-new-perspectives&quot;&gt;Explainable Recommendation: A Survey and New Perspectives&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#stamp-short-term-attentionmemory-priority-model-for-session-based-recommendation&quot;&gt;STAMP: Short-Term Attention/Memory Priority Model for Session-based Recommendation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#real-time-personalization-using-embeddings-for-search-ranking-at-airbnb&quot;&gt;Real-time Personalization using Embeddings for Search Ranking at Airbnb&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#billion-scale-commodity-embedding-for-e-commerce-recommendation-in-alibaba&quot;&gt;Billion-scale Commodity Embedding for E-commerce Recommendation in Alibaba&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sequential-recommendation-with-user-memory-networks&quot;&gt;Sequential Recommendation with User Memory Networks&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#aesthetic-based-clothing-recommendation&quot;&gt;Aesthetic-based Clothing Recommendation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#multi-pointer-co-attention-networks-for-recommendation&quot;&gt;Multi-Pointer Co-Attention Networks for Recommendation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#atrank-an-attention-based-user-behavior-modeling-framework-for-recommendation&quot;&gt;ATRank: An Attention-Based User Behavior Modeling Framework for Recommendation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#deep-matrix-factorization-models-for-recommender-systems&quot;&gt;Deep Matrix Factorization Models for Recommender Systems&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%9c%80%e6%96%b0paper2&quot;&gt;最新paper2&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#deeprec-an-open-source-toolkit-for-deep-learning-based-recommendation&quot;&gt;DeepRec: An Open-source Toolkit for Deep Learning based Recommendation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#a-survey-on-session-based-recommender-systems&quot;&gt;A Survey on Session-based Recommender Systems&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#reinforcement-learning-to-optimize-long-term-user-engagement-in-recommender-systems&quot;&gt;Reinforcement Learning to Optimize Long-term User Engagement in Recommender Systems&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#representing-and-recommending-shopping-baskets-with-complementarity-compatibility-and-loyalty&quot;&gt;Representing and Recommending Shopping Baskets with Complementarity, Compatibility, and Loyalty&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#attentive-group-recommendation&quot;&gt;Attentive Group Recommendation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#variational-autoencoders-for-collaborative-filtering&quot;&gt;Variational Autoencoders for Collaborative Filtering&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#exploiting-emotion-on-reviews-for-recommender-systems&quot;&gt;Exploiting Emotion on Reviews for Recommender Systems&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#an-attentive-interaction-network-for-context-aware-recommendations&quot;&gt;An Attentive Interaction Network for Context-aware Recommendations&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#regularizing-matrix-factorization-with-user-and-item-embeddings-for-recommendation&quot;&gt;Regularizing Matrix Factorization with User and Item Embeddings for Recommendation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#explainable-recommendation-via-multi-task-learning-in-opinionated-text-data&quot;&gt;Explainable Recommendation via Multi-Task Learning in Opinionated Text Data&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#top-k-off-policy-correction-for-a-reinforce-recommender-system&quot;&gt;Top-K Off-Policy Correction for a REINFORCE Recommender System&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#neural-news-recommendation-with-personalized-attention&quot;&gt;Neural News Recommendation with Personalized Attention&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#lambdaopt-learn-to-regularize-recommender-models-in-finer-levels&quot;&gt;LambdaOpt: Learn to Regularize Recommender Models in Finer Levels&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%b6%e4%bb%96&quot;&gt;其他&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#fast-matrix-factorization-for-online-recommendation-with-implicit-feedback&quot;&gt;Fast Matrix Factorization for Online Recommendation with Implicit Feedback&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2019%e9%a1%b6%e4%bc%9a%e7%9b%b8%e5%85%b3%e6%96%87%e7%ab%a0&quot;&gt;2019顶会相关文章&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#kdd2019%e7%9b%b8%e5%85%b3&quot;&gt;kdd2019相关&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#recsys2019%e7%9b%b8%e5%85%b3&quot;&gt;recsys2019相关&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#best-paper&quot;&gt;best paper&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%8f%af%e8%a7%a3%e9%87%8a%e6%8e%a8%e8%8d%90%e7%b3%bb%e7%bb%9f&quot;&gt;可解释推荐系统&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e4%b8%80%e4%ba%9b%e6%80%9d%e8%80%83&quot;&gt;一些思考&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#social-recomendation&quot;&gt;social recomendation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rsgan&quot;&gt;RSGAN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pe-ltr&quot;&gt;pe-ltr&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;各种综述&quot;&gt;各种综述&lt;/h2&gt;

&lt;p&gt;传统mf算法：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/ml-recommender-systems.html&quot;&gt;https://daiwk.github.io/posts/ml-recommender-systems.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其他几个综述：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/Batmaz2018_Article_AReviewOnDeepLearningForRecomm.pdf&quot;&gt;Batmaz2018_Article_AReviewOnDeepLearningForRecomm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/DL_on_RS.pdf&quot;&gt;Deep Learning based Recommender System: A Survey and New Perspectives&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.comp.nus.edu.sg/~xiangnan/sigir18-deep.pdf&quot;&gt;Deep Learning for Matching in Search and Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;好像上面的链接403了，这个应该Ok：&lt;a href=&quot;http://www.hangli-hl.com/uploads/3/4/4/6/34465961/wsdm_2019_tutorial.pdf&quot;&gt;http://www.hangli-hl.com/uploads/3/4/4/6/34465961/wsdm_2019_tutorial.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;个人小结(更新中)：&lt;/p&gt;

&lt;p&gt;传统部分：&lt;a href=&quot;https://daiwk.github.io/posts/dl-match-for-search-recommendation-traditional.html&quot;&gt;https://daiwk.github.io/posts/dl-match-for-search-recommendation-traditional.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;深度学习部分：&lt;a href=&quot;https://daiwk.github.io/posts/dl-match-for-search-recommendation.html&quot;&gt;https://daiwk.github.io/posts/dl-match-for-search-recommendation.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;一些经典论文&quot;&gt;一些经典论文&lt;/h2&gt;

&lt;h3 id=&quot;dssm&quot;&gt;dssm&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/nlp-dssm.html&quot;&gt;https://daiwk.github.io/posts/nlp-dssm.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;youtube&quot;&gt;youtube&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-youtube-video-recommendation.html&quot;&gt;https://daiwk.github.io/posts/dl-youtube-video-recommendation.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;序列建模&quot;&gt;序列建模&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1511.06939&quot;&gt;Session-based Recommendations with Recurrent Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;paddle实现：&lt;a href=&quot;https://github.com/PaddlePaddle/models/tree/develop/fluid/PaddleRec/gru4rec&quot;&gt;https://github.com/PaddlePaddle/models/tree/develop/fluid/PaddleRec/gru4rec&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;标签体系&quot;&gt;标签体系&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/nlp-tagspaces.html&quot;&gt;https://daiwk.github.io/posts/nlp-tagspaces.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;最新paper1&quot;&gt;最新paper1&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247491818&amp;amp;idx=1&amp;amp;sn=311962e2e41119a565c252a19037dd76&amp;amp;chksm=96ea3f6aa19db67c3fbfa77fbec65797d0ccc8f2930290d57c2016a3e55a8bb18b77fd10180b&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0928qyE33GaHUFg2ddzaDfmw&amp;amp;pass_ticket=QWrkCW0n7ulxJGBU0GG41I42RJFST5los9jWx8%2B%2BvEjJLwDxvQFM9Vs9mfvzCdFq#rd&quot;&gt;想了解推荐系统最新研究进展？请收好这16篇论文&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;a-review-on-deep-learning-for-recommender-systems-challenges-and-remedies&quot;&gt;A review on deep learning for recommender systems: challenges and remedies&lt;/h3&gt;

&lt;p&gt;本文是最新发表的一篇利用深度学习做推荐系统的综述，不仅从深度学习模型方面对文献进行了分类，而且从推荐系统研究的问题方面对文献做了分类。&lt;/p&gt;

&lt;h3 id=&quot;next-item-recommendation-with-self-attention&quot;&gt;Next Item Recommendation with Self-Attention&lt;/h3&gt;

&lt;p&gt;本文提出了一种基于 self-attention 的基于序列的推荐算法，该算法是用 self-attention 从用户的交互记录中自己的去学习用的近期的兴趣，同时该模型也保留了用户的长久的兴趣。整个网络是在 metric learning 的框架下，是第一次将 self-attention 和 metric learning的结合的尝试。&lt;/p&gt;

&lt;p&gt;实验结果表明，通过 self-attention，模型可以很好的学习用户的短期兴趣爱好， 并且能有效的提升模型效果。通过和近期的文章得对比发现，该方法可以在很大程度上改善序列化推荐的效果。&lt;/p&gt;

&lt;h3 id=&quot;metric-factorization-recommendation-beyond-matrix-factorization&quot;&gt;Metric Factorization: Recommendation beyond Matrix Factorization&lt;/h3&gt;

&lt;p&gt;本文提出了一种新型的推荐系统算法——Metric Factorization（距离分解）， 该方法旨在改进传统的基于矩阵分解的推荐系统算法。矩阵分解一个很大的问题就是不符合 inequality property， 这很大程度上阻碍了其表现。&lt;/p&gt;

&lt;p&gt;本文提出新型的解决方案，通过把用户和商品看作是一个低纬空间里面的点，然后用他们之间的距离来表示他们的距离。通过类似于矩阵分解的 squared loss 就能很好的从已有的历史数据中学出用户和商品在这个低维空间的位置。&lt;/p&gt;

&lt;p&gt;Metric Factorization 可以用在评分预测和排序两个经典的推荐场景，并且都取得了 state-of-the-art 的结果，超过基于深度学习以及已有的 Metric learning 的推荐算法。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/cheungdaven/metricfactorization&quot;&gt;https://github.com/cheungdaven/metricfactorization&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;collaborative-memory-network-for-recommendation-systems&quot;&gt;Collaborative Memory Network for Recommendation Systems&lt;/h3&gt;

&lt;p&gt;本文是圣塔克拉拉大学和 Google 联合发表于 SIGIR 2018 的工作。在传统的协同过滤模型中，隐藏因子模型能够捕捉交互的全局特征，基于近邻的相似度模型能够捕捉交互的局部特征。本文将两类协同过滤模型进行统一，根据注意力机制和记忆模块刻画复杂的用户-物品交互关系。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tebesu/CollaborativeMemoryNetwork&quot;&gt;https://github.com/tebesu/CollaborativeMemoryNetwork&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;evaluation-of-session-based-recommendation-algorithms&quot;&gt;Evaluation of Session-based Recommendation Algorithms&lt;/h3&gt;

&lt;p&gt;本文系统地介绍了 Session-based Recommendation，主要针对 baseline methods, nearest-neighbor techniques, recurrent neural networks, 和 (hybrid) factorization-based methods 等 4 大类算法进行介绍。&lt;/p&gt;

&lt;p&gt;此外，论文使用 RSC15、TMALL、ZALANDO、RETAILROCKET、8TRACKS 、AOTM、30MUSIC、NOWPLAYING、CLEF 等 7 个数据集进行分析，在 Mean Reciprocal Rank (MRR)、Coverage、Popularity bias、Cold start、Scalability、Precision、Recall 等指标上进行比较。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.dropbox.com/sh/7qdquluflk032ot/AACoz2Go49q1mTpXYGe0gaANa?dl=0&quot;&gt;https://www.dropbox.com/sh/7qdquluflk032ot/AACoz2Go49q1mTpXYGe0gaANa?dl=0&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;ripplenet-propagating-user-preferences-on-the-knowledge-graph-for-recommender-systems&quot;&gt;RippleNet: Propagating User Preferences on the Knowledge Graph for Recommender Systems&lt;/h3&gt;

&lt;p&gt;本文是上海交大、微软亚洲研究院和香港理工大学联合发表于 CIKM 2018 的工作。为了解决协同过滤的稀疏性和冷启动问题，研究人员通常利用社交网络或项目属性等辅助信息来提高推荐效果。本文将知识图谱应用到推荐系统中，是一个很新颖的方法，给推荐系统提供了一个全新的思路。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/hwwang55/RippleNet&quot;&gt;https://github.com/hwwang55/RippleNet&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;content-based-citation-recommendation&quot;&gt;Content-Based Citation Recommendation&lt;/h3&gt;

&lt;p&gt;本文提出了一种基于文章表示学习的方法，在为学术论文进行引文推荐任务上取得了较大成效。将给定的查询文档嵌入到向量空间中，然后将其邻近选项作为候选，并使用经过训练的判别模型重新排列候选项，以区分观察到的和未观察到的引用。此外，本文还发布了一个包含 700 万篇研究文章的公开数据集。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/allenai/citeomatic&quot;&gt;https://github.com/allenai/citeomatic&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;explainable-recommendation-a-survey-and-new-perspectives&quot;&gt;Explainable Recommendation: A Survey and New Perspectives&lt;/h3&gt;

&lt;p&gt;本文是对“可解释性推荐系统”相关以及最新研究的调研总结，内容包括问题定义、问题历史、解决方案、相关应用和未来方向。论文内容较为全面，对于刚接触这一方向或者已经从事搭配领域的业者学者有很好的借鉴意义，文章最后对于一些可以发展的方向的论述也很有启发意义。&lt;/p&gt;

&lt;h3 id=&quot;stamp-short-term-attentionmemory-priority-model-for-session-based-recommendation&quot;&gt;STAMP: Short-Term Attention/Memory Priority Model for Session-based Recommendation&lt;/h3&gt;

&lt;p&gt;本文是电子科大发表于 KDD 2018 的工作。论文提出了一种短期注意力/记忆优先的网络模型，在建模长时间序列的用户点击行为时，着重加强用户近期行为的影响。该方法既考虑了从长期历史行为挖掘用户的一般兴趣，又考虑了用户上一次点击挖掘用户的即时兴趣。实验表明，本文工作在 CIKM16 和 RSC15 两个经典数据集上均达到了最优结果。&lt;/p&gt;

&lt;h3 id=&quot;real-time-personalization-using-embeddings-for-search-ranking-at-airbnb&quot;&gt;Real-time Personalization using Embeddings for Search Ranking at Airbnb&lt;/h3&gt;

&lt;p&gt;本文是 Airbnb 团队发表于 KDD 18 的工作，摘得 Applied Data Science Track Best Paper 奖项。论文介绍了 Airbnb 利用 word embedding 的思路训练 Listing（也就是待选择的民宿房间）和用户的 embedding 向量，并在此基础上实现相似房源推荐和实时个性化搜索。&lt;/p&gt;

&lt;h3 id=&quot;billion-scale-commodity-embedding-for-e-commerce-recommendation-in-alibaba&quot;&gt;Billion-scale Commodity Embedding for E-commerce Recommendation in Alibaba&lt;/h3&gt;

&lt;p&gt;本文是阿里巴巴和香港科技大学发表于 SIGKDD 2018 的工作，论文结合节点 side information，解决了图表示学习中稀疏性和冷启动问题，在电商 i2i 推荐上取得很好的效果。&lt;/p&gt;

&lt;h3 id=&quot;sequential-recommendation-with-user-memory-networks&quot;&gt;Sequential Recommendation with User Memory Networks&lt;/h3&gt;

&lt;p&gt;本文是清华大学发表于 WSDM 2018 的工作。现有的深度学习推荐模型通常把用户的历史记录编码成一个 latent vector，但是可能会丢失 per-item 的共现信息。本文提出一种记忆增强的神经网络，把用户历史存到 memory 里，并设计了 read/write 机制更新 memory 内容，使序列网络能更动态地记录用户历史信息。&lt;/p&gt;

&lt;h3 id=&quot;aesthetic-based-clothing-recommendation&quot;&gt;Aesthetic-based Clothing Recommendation&lt;/h3&gt;

&lt;p&gt;本文是清华大学发表于 WWW 18 的工作，论文利用图片增强效果，传统的方法只考虑 CNN 抽取的图像特征；而本文考虑了图片中的美学特征对于推荐的影响；作者利用 BDN 从图片中学习美学特征，然后将其融合到 DCF 中，增强用户-产品，产品-时间矩阵，从而提高了推荐效果；在亚马逊和 AVA 数据集上都取得了良好的效果。&lt;/p&gt;

&lt;h3 id=&quot;multi-pointer-co-attention-networks-for-recommendation&quot;&gt;Multi-Pointer Co-Attention Networks for Recommendation&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/vanzytay/KDD2018_MPCN&quot;&gt;https://github.com/vanzytay/KDD2018_MPCN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是南洋理工大学发表于 KDD 2018 的工作。在预测用户对商品的评分时，如何学习用户和商品的表示至关重要。本文基于协同注意力机制，在 review-level 和 word-level 对用户评论和与商品相关的评论进行选择，选择最重要的一条或若干条评论来对用户和商品进行表示。&lt;/p&gt;

&lt;h3 id=&quot;atrank-an-attention-based-user-behavior-modeling-framework-for-recommendation&quot;&gt;ATRank: An Attention-Based User Behavior Modeling Framework for Recommendation&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/jinze1994/ATRank&quot;&gt;https://github.com/jinze1994/ATRank&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文来自阿里巴巴，论文尝试设计和实现了一种能够融合用户多种时序行为数据的方法，较为创新的想法在于提出了一种同时考虑异构行为和时序的解决方案，并给出较为简洁的实现方式。使用类似 Google 的 self-attention 机制去除 CNN、LSTM 的限制，让网络训练和预测速度变快的同时，效果还可以略有提升。 此框架便于扩展。可以允许更多不同类型的行为数据接入，同时提供多任务学习的机会，来弥补行为稀疏性。&lt;/p&gt;

&lt;h3 id=&quot;deep-matrix-factorization-models-for-recommender-systems&quot;&gt;Deep Matrix Factorization Models for Recommender Systems&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/RuidongZ/Deep_Matrix_Factorization_Models&quot;&gt;https://github.com/RuidongZ/Deep_Matrix_Factorization_Models&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文在利用深度学习做推荐时，考虑了推荐的显式反馈和隐式反馈，将其融合构建成一个矩阵，从而将用户和产品的不同向量输入到两个并行的深层网络中去。最后，设计了一种新型的损失函数以同时考虑评分和交互两种不同类型的反馈数据。&lt;/p&gt;

&lt;h2 id=&quot;最新paper2&quot;&gt;最新paper2&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247497070&amp;amp;idx=1&amp;amp;sn=f5d8489a9ae0edaf64281bb4e9e0f911&amp;amp;chksm=96ea2aeea19da3f81b5be9933497bdd921abd4dec0457fc49d0055a66d6d8f21c4e16ec1db0b&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=gsCKgAT5ncC4Oyqcqu32vlRROsqT7GGmlwcs6Mru0krjDac9tUZg2yxUB7dE5%2BpP#rd&quot;&gt;推荐系统阅读清单：最近我们在读哪些论文？&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;deeprec-an-open-source-toolkit-for-deep-learning-based-recommendation&quot;&gt;DeepRec: An Open-source Toolkit for Deep Learning based Recommendation&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.researchgate.net/publication/332555888_DeepRec_An_Open-source_Toolkit_for_Deep_Learning_based_Recommendation&quot;&gt;DeepRec: An Open-source Toolkit for Deep Learning based Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文提出了一个全新开源工具库，包含诸多基于深度学习的推荐算法，代码可以直接运行，可以用来做为 baselines 和开发自己的算法，是一个非常不错的工具。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/cheungdaven/DeepRec&quot;&gt;https://github.com/cheungdaven/DeepRec&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;a-survey-on-session-based-recommender-systems&quot;&gt;A Survey on Session-based Recommender Systems&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1902.04864&quot;&gt;A Survey on Session-based Recommender Systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本文是第一篇全面深入总结 session-based recommendations 的综述文章，值得推荐。文章系统总结了目前一种新型推荐范式：session-based recommendations 的特点、挑战和目前取得的进展，对整个推荐系统研究领域和相关的工业界人员提供了一个全面了解推荐系统领域最新研究进展的机会。&lt;/p&gt;

&lt;p&gt;该文从问题本质和相关的数据特征入手，为 session-based recommendations 建立了一个层次化模型来深入理解里面存在的各种数据复杂性和潜在挑战，然后采用了两个不同维度对现有研究成果进行了系统分类和总结，最后提出了展望。&lt;/p&gt;

&lt;h3 id=&quot;reinforcement-learning-to-optimize-long-term-user-engagement-in-recommender-systems&quot;&gt;Reinforcement Learning to Optimize Long-term User Engagement in Recommender Systems&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1902.05570&quot;&gt;Reinforcement Learning to Optimize Long-term User Engagement in Recommender Systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文针对利用强化学习解决推荐系统时存在用户行为难以建模的问题，提出了一种新的强化学习框架 FeedRec，包括两个网络：Q 网络利用层次化 LSTM 对复杂用户行为建模，S 网络用来模拟环境，辅助和稳定 Q 网络的训练。方法在合成数据和真实数据上进行了验证，取得了 SOTA 的结果。&lt;/p&gt;

&lt;h3 id=&quot;representing-and-recommending-shopping-baskets-with-complementarity-compatibility-and-loyalty&quot;&gt;Representing and Recommending Shopping Baskets with Complementarity, Compatibility, and Loyalty&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.microsoft.com/en-us/research/uploads/prod/2019/01/cikm18_mwan.pdf&quot;&gt;Representing and Recommending Shopping Baskets with Complementarity, Compatibility, and Loyalty&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;与在线购物不同的是，在超市购物场景下，商品间的互补性和用户对商品的忠诚度起着决定性作用。本文基于这两个维度提出了一种新的表示学习方法——triple2vec。此外，作者在上述方法得到的表示基础上，提出了一种考虑忠诚度的推荐算法，用忠诚系数来权衡表示模型和统计模型计算出的购买偏好。&lt;/p&gt;

&lt;p&gt;源码：&lt;a href=&quot;https://github.com/MengtingWan/grocery&quot;&gt;https://github.com/MengtingWan/grocery&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;attentive-group-recommendation&quot;&gt;Attentive Group Recommendation&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.comp.nus.edu.sg/~xiangnan/papers/sigir18-groupRS.pdf&quot;&gt;Attentive Group Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文应用神经协同网络和注意力机制为群组用户进行 Top-N 商品推荐，主要解决了群组用户兴趣的动态组合、群组与个人用户的协同商品推荐，以及新用户的冷启动问题。&lt;/p&gt;

&lt;p&gt;源码：&lt;a href=&quot;https://github.com/LianHaiMiao/Attentive-Group-Recommendation&quot;&gt;https://github.com/LianHaiMiao/Attentive-Group-Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;variational-autoencoders-for-collaborative-filtering&quot;&gt;Variational Autoencoders for Collaborative Filtering&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1802.05814&quot;&gt;Variational Autoencoders for Collaborative Filtering&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文将变分自编码器（VAE）扩展到协同过滤以进行隐式反馈，通过非线性概率模型克服线性因子模型的局限。其次，作者引入了具有多项式似然（multinomial likelihood）的生成模型，并使用贝叶斯推断进行参数估计。作者基于 VAE 提出了一个生成模型 VAE_CF，并针对 VAE 的正则参数和概率模型选取做了适当调整，使其在当前推荐任务中取得最佳结果。&lt;/p&gt;

&lt;p&gt;源码：&lt;a href=&quot;https://github.com/dawenl/vae_cf&quot;&gt;https://github.com/dawenl/vae_cf&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;exploiting-emotion-on-reviews-for-recommender-systems&quot;&gt;Exploiting Emotion on Reviews for Recommender Systems&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.aaai.org/ocs/index.php/AAAI/AAAI18/paper/viewFile/16608/16603&quot;&gt;Exploiting Emotion on Reviews for Recommender Systems&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;现有的推荐系统大多依靠用户的历史评分或者评论文本进行推荐，往往由于数据资源不足而面临数据稀疏和难以进行冷启动的问题。本文基于原则性和数学的方式，对用户评论中的积极/消极情感加以充分利用，提出了一个全新推荐框架 MIRROR，并且在 Ciao 和 Epinions 这两个真实数据集上证明了该框架的有效性。&lt;/p&gt;

&lt;h3 id=&quot;an-attentive-interaction-network-for-context-aware-recommendations&quot;&gt;An Attentive Interaction Network for Context-aware Recommendations&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=3271813&quot;&gt;An Attentive Interaction Network for Context-aware Recommendations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文关注的问题是基于上下文感知的推荐系统。作者提出了一种新型注意力交互网络，用来捕捉内容、user和item之间的交互影响。此外，作者还提出了一种效应级注意力机制来聚合多种交互影响。通过在三个公开数据集 Food、Yelp 和 Frappe 上的大量实验表明，本文模型效果优于当前最先进的上下文感知推荐算法。&lt;/p&gt;

&lt;h3 id=&quot;regularizing-matrix-factorization-with-user-and-item-embeddings-for-recommendation&quot;&gt;Regularizing Matrix Factorization with User and Item Embeddings for Recommendation&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1809.00979&quot;&gt;Regularizing Matrix Factorization with User and Item Embeddings for Recommendation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文提出了一个基于 RME（Regularized Multi-Embedding）的推荐模型，不再基于用户的共同喜好进行物品推荐，而是创新地提出刻画物品对共同被讨厌的特征，进而避免向用户推荐其讨厌的物品。&lt;/p&gt;

&lt;p&gt;源码：&lt;a href=&quot;https://github.com/thanhdtran/RME&quot;&gt;https://github.com/thanhdtran/RME&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;explainable-recommendation-via-multi-task-learning-in-opinionated-text-data&quot;&gt;Explainable Recommendation via Multi-Task Learning in Opinionated Text Data&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1806.03568&quot;&gt;Explainable Recommendation via Multi-Task Learning in Opinionated Text Data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;论文提出了一个用于可解释推荐任务的多任务学习方法，通过联合张量分解将用户、产品、特征和观点短语映射到同一表示空间。&lt;/p&gt;

&lt;p&gt;源码：&lt;a href=&quot;https://github.com/MyTHWN/MTER&quot;&gt;https://github.com/MyTHWN/MTER&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;top-k-off-policy-correction-for-a-reinforce-recommender-system&quot;&gt;Top-K Off-Policy Correction for a REINFORCE Recommender System&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1812.02353.pdf&quot;&gt;Top-K Off-Policy Correction for a REINFORCE Recommender System&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/dl-topk-off-policy-correction.html&quot;&gt;https://daiwk.github.io/posts/dl-topk-off-policy-correction.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;neural-news-recommendation-with-personalized-attention&quot;&gt;Neural News Recommendation with Personalized Attention&lt;/h3&gt;

&lt;p&gt;KDD2019 微软的&lt;a href=&quot;https://arxiv.org/pdf/1907.05559.pdf&quot;&gt;Neural News Recommendation with Personalized Attention&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在浏览新闻时，不同的用户通常具有不同的兴趣，并且对于同一篇新闻，不同的用户往往会关注其不同方面。然而已有的新闻推荐方法往往无法建模不同用户对同一新闻的兴趣差异。&lt;/p&gt;

&lt;p&gt;该论文提出了一种 neural news recommendation with personalized attention（NPA） 模型，可在新闻推荐任务中应用个性化注意力机制来建模用户对于不同上下文和新闻的不同兴趣。该模型的核心是新闻表示、用户表示和点击预估。&lt;/p&gt;

&lt;p&gt;在新闻表示模块中，我们基于新闻标题学习新闻表示。新闻标题中的每个词语先被映射为词向量，之后我们使用 CNN 来学习词语的上下文表示，最后通过词语级的注意力网络选取重要的词语，构建新闻标题的表示。&lt;/p&gt;

&lt;p&gt;在用户表示模块中，我们基于用户点击的新闻来学习用户的表示。由于不同新闻表示用户具有不同的信息量，我们应用新闻级的注意力网络选取重要的新闻文章，构建用户的表示。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/npa-arch.png&quot; style=&quot;max-width: 350px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;另外，相同的新闻和词语可能对表示不同的用户具有不同的信息量。因此，我们提出了一种个性化的注意力网络，其利用用户 ID 的嵌入向量来生成用于词和新闻级注意的查询向量其结构如下图：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/npa-attention.png&quot; style=&quot;max-width: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;最终候选新闻的点击预估分数通过用户和新闻表示向量之间的内积计算，作为对用户进行个性化新闻推荐时的排序依据。&lt;/p&gt;

&lt;h2 id=&quot;lambdaopt-learn-to-regularize-recommender-models-in-finer-levels&quot;&gt;LambdaOpt: Learn to Regularize Recommender Models in Finer Levels&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.11596&quot;&gt;LambdaOpt: Learn to Regularize Recommender Models in Finer Levels&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;推荐系统经常涉及大量的类别型变量，例如用户、商品 ID 等，这样的类别型变量通常是高维、长尾的——头部用户的行为记录可能多达百万条，而尾部用户则仅有几十条记录。这样的数据特性使得我们在训练推荐模型时容易遭遇数据稀疏的问题，因而适度的模型正则化对于推荐系统来说十分重要。正则化超参的调校十分具有挑战性。常用的网格搜索需要消耗大量计算资源，且通常无法顾及更细粒度的正则化。针对推荐模型和推荐数据集的特性，我们提出了一种新的、更加细粒度的自动正则化超参选择方案&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\lambda Opt\)&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;其他&quot;&gt;其他&lt;/h2&gt;

&lt;h3 id=&quot;fast-matrix-factorization-for-online-recommendation-with-implicit-feedback&quot;&gt;Fast Matrix Factorization for Online Recommendation with Implicit Feedback&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/ml-recommender-systems.html#eals&quot;&gt;https://daiwk.github.io/posts/ml-recommender-systems.html#eals&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;2019顶会相关文章&quot;&gt;2019顶会相关文章&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/69559974&quot;&gt;最新！五大顶会2019必读的深度推荐系统与CTR预估相关的论文&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;偷个图：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/recommender_system_dl.jpg&quot; style=&quot;max-width: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;kdd2019相关&quot;&gt;kdd2019相关&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650767325&amp;amp;idx=3&amp;amp;sn=af31e659d4570093ff394e6087bbccfc&amp;amp;chksm=871abba3b06d32b5b086fcc42360f86850af8af20f8455e4767a9cab54d42a26a19357c5cbf9&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=Kz97uXi0CH4ceADUC3ocCNkjZjy%2B0DTtVYOM7n%2FmWttTt5YKTC2DQT9lqCel7dDR#rd&quot;&gt;KDD 提前看 | KDD 里的技术实践和突破&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;recsys2019相关&quot;&gt;recsys2019相关&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/92udRhAQHqDCCFpK6YUFOw&quot;&gt;RecSys提前看 | 深度学习在推荐系统中的最新应用&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1907.06558&quot;&gt;Addressing Delayed Feedback for Continuous Training with Neural Networks in CTR prediction&lt;/a&gt;，提出一种基于损失函数的神经网络模型，用于解决连续学习过程中的延迟反馈问题。&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1908.02569&quot;&gt;Tripartite Heterogeneous Graph Propagation for Large-scale Social Recommendation&lt;/a&gt;，提出一种应用于社会化推荐系统的异构图传播结构。&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1908.05544&quot;&gt;On Gossip-based Information Dissemination in Pervasive Recommender Systems&lt;/a&gt;，提出一种离线手机端与邻近区域内其他设备之间推荐评分数据交换问题的普适推荐系统策略。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;best-paper&quot;&gt;best paper&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/GNYDX0SfitFtiIU-jq51Pg&quot;&gt;推荐系统顶会 RecSys2019 最佳论文奖出炉！可复现性成为焦点—18篇顶级会议只有7篇可以合理复现&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;paperweekly的&lt;a href=&quot;https://mp.weixin.qq.com/s/EGByoYg9BPEHrVT7C1M77A&quot;&gt;RecSys 2019最佳论文：基于深度学习的推荐系统是否真的优于传统经典方法？&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1907.06902&quot;&gt;Are We Really Making Much Progress? A Worrying Analysis of Recent Neural Recommendation Approaches&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/MaurizioFD/RecSys2019_DeepLearning_Evaluation&quot;&gt;https://github.com/MaurizioFD/RecSys2019_DeepLearning_Evaluation&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;可解释推荐系统&quot;&gt;可解释推荐系统&lt;/h2&gt;

&lt;p&gt;第四范式写的&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/55349847&quot;&gt;可解释推荐系统：身怀绝技，一招击中用户心理&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/55433428&quot;&gt;可解释推荐系统：知其然，知其所以然&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;一些思考&quot;&gt;一些思考&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzU1NDA4NjU2MA==&amp;amp;mid=2247497477&amp;amp;idx=2&amp;amp;sn=cf23fbba12dcacaf0f7cdcdc504bfe46&amp;amp;chksm=fbea4ecacc9dc7dcc77e122ef2bc89c72883c7cd83360003050f876d8d2cc4292767e637b391&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;sharer_sharetime=1564505444451&amp;amp;sharer_shareid=8e95986c8c4779e3cdf4e60b3c7aa752&amp;amp;pass_ticket=zAXdHORK5tTx549e9RwAgNcm7bjJrH4ENwbbTYVrAZDqpsE%2Fu1hY63b%2FoRfnZQdM#rd&quot;&gt;基于深度学习的推荐系统效果遭质疑，它真的有带来实质性进展吗？&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对应的论文是RecSys2019上的：&lt;a href=&quot;https://arxiv.org/abs/1907.06902&quot;&gt;Are We Really Making Much Progress? A Worrying Analysis of Recent Neural Recommendation Approaches&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/7gDMUk2zFGuyAlfEMvwECQ&quot;&gt;推荐系统走向下一阶段最重要的三个问题&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;和真实应用场景贴近的统一 benchmark。我们需要一个工业级、可以迭代真实应用场景技术的数据集。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;推荐系统的可解释性。在我看来可解释性重要的不是指导研究人员之后怎么迭代方法，而是增加透明性，让使用推荐系统的用户和为推荐系统提供内容的生产者能看得懂推荐系统，能参与进来。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;算法对系统数据的 Confounding 问题。可以简单理解为反馈循环（Feedback Loop）问题，简单来说就是算法会决定推荐系统展示给用户内容，从而间接的影响了用户的行为。而用户的行为反馈数据又会决定算法的学习。形成一个循环。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;social-recomendation&quot;&gt;social recomendation&lt;/h2&gt;

&lt;h2 id=&quot;rsgan&quot;&gt;RSGAN&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://daiwk.github.io/posts/dl-rsgan.html&quot;&gt;https://daiwk.github.io/posts/dl-rsgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pe-ltr&quot;&gt;pe-ltr&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/JXW--wzpaFwRHSSvZEA0mg&quot;&gt;阿里提出多目标优化全新算法框架，同时提升电商GMV和CTR&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=3346998&quot;&gt;A pareto-efficient algorithm for multiple objective optimization in e-commerce recommendation&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>【置顶】导引——nlp论文集合</title>
   <link href="http://daiwk.github.io/posts/links-navigation-nlp.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/links-navigation-nlp</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%9c%ba%e5%99%a8%e7%bf%bb%e8%af%91&quot;&gt;机器翻译&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#10%e7%af%87%e5%bf%85%e8%af%bb&quot;&gt;10篇必读&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%b6%e4%bb%96&quot;&gt;其他&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#nnlm%e7%bb%bc%e8%bf%b0&quot;&gt;nnlm综述&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e9%98%85%e8%af%bb%e7%90%86%e8%a7%a3%e7%bb%bc%e8%bf%b0&quot;&gt;阅读理解综述&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#acl2019%e9%a2%84%e8%ae%ad%e7%bb%83%e8%af%ad%e8%a8%80%e6%a8%a1%e5%9e%8b&quot;&gt;ACL2019预训练语言模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nlp%e7%bb%bc%e8%bf%b0&quot;&gt;nlp综述&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nlp%e8%b7%af%e7%ba%bf%e5%9b%be&quot;&gt;nlp路线图&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#probability--statistics&quot;&gt;Probability &amp;amp; Statistics&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#machine-learning&quot;&gt;Machine Learning&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#text-mining&quot;&gt;Text Mining&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#natural-language-processing&quot;&gt;Natural Language Processing&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#teacher-forcing&quot;&gt;teacher forcing&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;机器翻译&quot;&gt;机器翻译&lt;/h2&gt;

&lt;p&gt;参考清华机器翻译组整理的&lt;a href=&quot;https://github.com/THUNLP-MT/MT-Reading-List&quot;&gt;https://github.com/THUNLP-MT/MT-Reading-List&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;10篇必读&quot;&gt;10篇必读&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://aclweb.org/anthology/J93-2003&quot;&gt;The Mathematics of Statistical Machine Translation: Parameter Estimation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://aclweb.org/anthology/P02-1040&quot;&gt;BLEU: a Method for Automatic Evaluation of Machine Translation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://aclweb.org/anthology/N03-1017&quot;&gt;Statistical Phrase-Based Translation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://aclweb.org/anthology/P03-1021&quot;&gt;Minimum Error Rate Training in Statistical Machine Translation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://aclweb.org/anthology/J07-2003&quot;&gt;Hierarchical Phrase-Based Translation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://papers.nips.cc/paper/5346-sequence-to-sequence-learning-with-neural-networks.pdf&quot;&gt;Sequence to Sequence Learning with Neural Networks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1409.0473.pdf&quot;&gt;Neural Machine Translation by Jointly Learning to Align and Translate&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1412.6980&quot;&gt;Adam: A Method for Stochastic Optimization&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1508.07909.pdf&quot;&gt;Neural Machine Translation of Rare Words with Subword Units&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://papers.nips.cc/paper/7181-attention-is-all-you-need.pdf&quot;&gt;Attention is All You Need&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;其他&quot;&gt;其他&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1906.09777.pdf&quot;&gt;A Tensorized Transformer for Language Modeling&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;多头注意力机制限制了模型的发展，使得模型需要较大的算力支持。为了解决这一问题，基于&lt;strong&gt;张量分解&lt;/strong&gt;和&lt;strong&gt;参数共享&lt;/strong&gt;的思想，本文提出了&lt;strong&gt;多头线性注意力（Multi-linear attention）&lt;/strong&gt;和&lt;strong&gt;Block-Term Tensor Decomposition（BTD）&lt;/strong&gt;。研究人员在语言建模任务及神经翻译任务上进行了测试，与许多语言建模方法相比，多头线性注意力机制不仅可以&lt;strong&gt;大大压缩模型参数数量&lt;/strong&gt;，而且&lt;strong&gt;提升了模型的性能&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;分解那块参考的&lt;a href=&quot;http://www.kolda.net/publication/TensorReview.pdf&quot;&gt;Tensor decompositions and applications&lt;/a&gt;和&lt;a href=&quot;https://www.researchgate.net/publication/220656664_Decompositions_of_a_Higher-Order_Tensor_in_Block_Terms-Part_II_Definitions_and_Uniqueness?_iepl%5BgeneralViewId%5D=GidOAKvMwvySBJxXGLf7dnk20JGueu7IopW8&amp;amp;_iepl%5Bcontexts%5D%5B0%5D=searchReact&amp;amp;_iepl%5BviewId%5D=7E2q1w0hmoIJSdCgfRKWh6JguNUiAGrpN0Lk&amp;amp;_iepl%5BsearchType%5D=publication&amp;amp;_iepl%5Bdata%5D%5BcountLessEqual20%5D=1&amp;amp;_iepl%5Bdata%5D%5BinteractedWithPosition1%5D=1&amp;amp;_iepl%5Bdata%5D%5BwithoutEnrichment%5D=1&amp;amp;_iepl%5Bposition%5D=1&amp;amp;_iepl%5BrgKey%5D=PB%3A220656664&amp;amp;_iepl%5BtargetEntityId%5D=PB%3A220656664&amp;amp;_iepl%5BinteractionType%5D=publicationTitle&quot;&gt;Decompositions of a higher-order tensor in block terms—part ii: Definitions and uniqueness&lt;/a&gt;，后者好像没有pdf可以下载。。&lt;/p&gt;

&lt;h3 id=&quot;nnlm综述&quot;&gt;nnlm综述&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766368&amp;amp;idx=4&amp;amp;sn=d7876bb4adb0ded6ad3736b878f8e541&amp;amp;chksm=871ab85eb06d31481e25aea171f87d355ac25465dc173abe49a1e075463cb19b777d1776b4dd&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;从经典结构到改进方法，神经网络语言模型综述&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;阅读理解综述&quot;&gt;阅读理解综述&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&amp;amp;mid=2247498503&amp;amp;idx=1&amp;amp;sn=ca27b9f04effcdfd8add3cd22aede262&amp;amp;chksm=96ea2487a19dad91aa546eb1d49d851d3e43b56f360ad75bd7bb2d18a2977c16b1908ce1b43f&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;神经机器阅读理解最新综述：方法和趋势&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;acl2019预训练语言模型&quot;&gt;ACL2019预训练语言模型&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766791&amp;amp;idx=4&amp;amp;sn=c2e2088877e0ec88fe67e68ed7db5f65&amp;amp;chksm=871ab9b9b06d30afaeb21aefff562d6d3eab86f82b0b0a86315e6c75f54171e2d6ddaf2d0199&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;ACL 2019提前看：预训练语言模型的最新探索&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;nlp综述&quot;&gt;nlp综述&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1807.10854v2&quot;&gt;A Survey of the Usages of Deep Learning in Natural Language Processing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;过去数年，深度学习模型的爆炸式使用推动了自然语言处理领域的发展。在本文中，研究者简要介绍了自然语言处理领域的基本情况，并概述了深度学习架构和方法。然后，他们对近来的研究进行了筛选，对大量相关的文献展开总结。除了诸多计算机语言学的应用之外，研究者还分析研究了语言处理方面的几个核心问题。最后他们讨论了当前 SOTA 技术和方法并对该领域未来的研究提出了建议。&lt;/p&gt;

&lt;h3 id=&quot;nlp路线图&quot;&gt;nlp路线图&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/graykode/nlp-roadmap&quot;&gt;https://github.com/graykode/nlp-roadmap&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;probability--statistics&quot;&gt;Probability &amp;amp; Statistics&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/prob-stats.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h4 id=&quot;machine-learning&quot;&gt;Machine Learning&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ml.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h4 id=&quot;text-mining&quot;&gt;Text Mining&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/textmining.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h4 id=&quot;natural-language-processing&quot;&gt;Natural Language Processing&lt;/h4&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/nlp.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h3 id=&quot;teacher-forcing&quot;&gt;teacher forcing&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/93030328&quot;&gt;https://zhuanlan.zhihu.com/p/93030328&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Teacher Forcing 是一种用于序列生成任务的训练技巧，与Autoregressive模式相对应，这里阐述下两者的区别：&lt;/p&gt;

&lt;p&gt;Autoregressive 模式下，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;时刻decoder模块的输入是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t-1\)&lt;/code&gt;时刻的&lt;strong&gt;输出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_{t-1}\)&lt;/code&gt;&lt;/strong&gt;。这时候我们称 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y_{t-1}\)&lt;/code&gt; 为当前预测步的context;
Teacher-Forcing 模式下，在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(t\)&lt;/code&gt;时刻decoder模块的输入是Ground-truth语句中位置的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y^{*}_{t-1}\)&lt;/code&gt;单词。这时候我们称 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(y^{*}_{t-1}\)&lt;/code&gt;为当前预测步的context；&lt;/p&gt;

&lt;p&gt;Teacher-Forcing 技术之所以作为一种有用的训练技巧，主要是因为：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Teacher-Forcing能够在&lt;strong&gt;训练&lt;/strong&gt;的时候&lt;strong&gt;矫正模型的预测&lt;/strong&gt;，避免在序列生成的过程中误差进一步放大。&lt;/li&gt;
  &lt;li&gt;Teacher-Forcing能够极大的&lt;strong&gt;加快模型的收敛速度&lt;/strong&gt;，令模型训练过程更加快&amp;amp;平稳。&lt;/li&gt;
  &lt;li&gt;Teacher-Forcing技术是保证Transformer模型能够在训练过程中&lt;strong&gt;完全并行计算所有token&lt;/strong&gt;的关键技术。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Teacher Forcing的问题(后面两个是acl2019最佳论文提出的)：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;最常见的问题就是Exposure Bias了。由于训练和预测的时候decode行为的不一致， 导致预测单词（predict words）在训练和预测的时候是从不同的分布中推断出来的。而这种不一致导致训练模型和预测模型直接的Gap，就叫做Exposure Bias。&lt;/li&gt;
  &lt;li&gt;Teacher-Forcing技术在解码的时候生成的字符都受到了 Ground-Truth 的约束，希望模型生成的结果都必须和参考句一一对应。这种约束在训练过程中&lt;strong&gt;减少模型发散&lt;/strong&gt;，加快收敛速度。但是一方面也&lt;strong&gt;扼杀了翻译多样性&lt;/strong&gt;的可能。&lt;/li&gt;
  &lt;li&gt;Teacher-Forcing技术在这种约束下，还会导致一种叫做Overcorrect(矫枉过正)的问题，导致生成的语句不通顺。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/RNaFuacpbQ32OtIUthcPRQ&quot;&gt;ACL2019最佳论文冯洋：Teacher Forcing亟待解决 ，通用预训练模型并非万能&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1906.02448&quot;&gt;Bridging the Gap between Training and Inference for Neural Machine Translation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;提出了使用 Oracle 词语，用于替代 Ground Truth 中的词语，作为训练阶段约束模型的数据。&lt;/p&gt;

&lt;p&gt;选择 Oracle Word 的方法有两种，一种是选择 word-level oracle，另一种则是 sentence-level oracle。&lt;/p&gt;

&lt;p&gt;word-level oracle 的选择方法，在时间步为 j 时，获取前一个时间步模型预测出的每个词语的预测分数。为了提高模型的鲁棒性，论文在预测分数基础上加上了 Gumbel noise，最终取分数最高的词语作为此时的 Oracle Word。&lt;/p&gt;

&lt;p&gt;sentence-level oracle 的选择方法则是在训练时，在解码句子的阶段，使用集束搜索的方法，选择集束宽为 k 的句子（即 top k 个备选句子），然后计算每个句子的 BLEU 分数，最终选择分数最高的句子。&lt;/p&gt;

&lt;p&gt;当然，这会带来一个问题，即每个时间步都需要获得该时间步长度上的备选句子，因此集束搜索获得的句子长度必须和时间步保持一致。如果集束搜索生成的实际句子超出或短于这一长度该怎么办？这里研究人员使用了「Force Decoding」的方法进行干预。而最终选择的 Oracle Word 也会和 Ground Truth 中的词语混合，然后使用衰减式采样（Decay Sampling）的方法从中挑选出作为约束模型训练的词。&lt;/p&gt;

&lt;p&gt;机器之心：我们知道，这篇论文的基本思想是：不仅使用 Ground Truth 进行约束，在训练过程中，也利用训练模型预测出的上一个词语作为其中的备选词语，这样的灵感是从哪里得到的呢？&lt;/p&gt;

&lt;p&gt;冯洋：我们很早就发现了这样一个问题——训练和测试的时候模型的输入是不一样的。我们希望模型在训练过程中也能够用到预测出的词语。看到最近一些周边的工作，我们慢慢想到，将 Ground Truth 和模型自己预测出的词一起以 Sampling 的方式输入进模型。&lt;/p&gt;

&lt;p&gt;机器之心：刚才您提到有一些周边的工作，能否请您谈谈有哪些相关的论文？&lt;/p&gt;

&lt;p&gt;冯洋：这些周边的论文在 Related Work 中有写到，这些工作的基本思想都是一样的，都是希望将预测出的词语作为模型输入。比如说，根据 DAD（Data as Demonstrator）的方法。这一方法将预测出的词语和后一个词语组成的词语对（word-pair）以 bigram 的方式输入作为训练实例加入。另一种是 Scheduled Sampling 的方式，也是用 Sampling 的方式，把预测出的词语作为输入加入到模型训练中。&lt;/p&gt;

&lt;p&gt;机器之心：论文使用了两种方法实现将预测词语作为训练的输入，一种是在 Word-level 选择 Oracle Word，另一种是在 Sentence-level 选择 Oracle Sentence，能否请您详细介绍下 Sentence-level 的方法？&lt;/p&gt;

&lt;p&gt;冯洋：Sentence-level 的方法可以简单理解为进行了一次解码。我们从句子中取出前 k 个候选译文。这里的 k 我们选择了 3，即 Top3 的句子。然后在这些句子中再计算他们的 BLEU 分数，并选择分数最高的句子，作为 Oracle Sentence。&lt;/p&gt;

&lt;p&gt;机器之心：我们知道，论文中，在选择 Oracle Sentence 的过程中会进行「Force Decoding」。需要强制保证生成的句子和原有的句子保持一致的长度。您认为这样的方法会带来什么样的问题？&lt;/p&gt;

&lt;p&gt;冯洋：这是强制模型生成和 Ground Truth 长度一样的句子。这样模型可能会生成一些原本并不是模型想生成的结果，这可能会带来一些问题。但是对于 Teacher Forcing 来说这是必须的，因为 Teacher Forcing 本身要求每一个词都要对应。所以说，虽然看起来我们干预了句子的生成，但是在 Teacher Forcing 的场景下，这种干预不一定是坏的。&lt;/p&gt;

&lt;p&gt;机器之心：为什么说这样的干预不一定是坏的？&lt;/p&gt;

&lt;p&gt;冯洋：我们需要留意的是，Force Decoding 的方法是在训练阶段进行的，如果训练中这样做了，模型就会逐渐地适应这个过程。另一方面，Force Decoding 可以平衡一些极端的生成结果。比如说，当句子长度为 10，但模型只生成了仅有 2 个词的句子，或者是模型生成了有 20 个词的句子，所以说 Force Decoding 也可以平衡这样的极端情况。在 Teacher Forcing 的场景下，这是一种折中的方法，不能完全说这样的方法是不好的。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>【置顶】导引——图相关</title>
   <link href="http://daiwk.github.io/posts/links-navigation-gnn.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/links-navigation-gnn</id>
   <content type="html">&lt;p&gt;目录：&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%a1%86%e6%9e%b6&quot;&gt;框架&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gnn&quot;&gt;gnn&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gnn%e6%8e%a8%e8%8d%90&quot;&gt;gnn+推荐&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gnnnlp&quot;&gt;gnn+nlp&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#nips2019&quot;&gt;nips2019&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#mixhop&quot;&gt;mixhop&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;框架&quot;&gt;框架&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/platform-gnn-frameworks.html&quot;&gt;https://daiwk.github.io/posts/platform-gnn-frameworks.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;gnn&quot;&gt;gnn&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-gnn.html&quot;&gt;https://daiwk.github.io/posts/dl-gnn.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-graph-representations.html&quot;&gt;https://daiwk.github.io/posts/dl-graph-representations.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-graphembedding.html&quot;&gt;https://daiwk.github.io/posts/dl-graphembedding.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;gnn推荐&quot;&gt;gnn+推荐&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-graph-recommendations.html&quot;&gt;https://daiwk.github.io/posts/dl-graph-recommendations.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;gnnnlp&quot;&gt;gnn+nlp&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/nlp-nlp-gnn.html&quot;&gt;https://daiwk.github.io/posts/nlp-nlp-gnn.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;nips2019&quot;&gt;nips2019&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/IIr4zWqtL2_1hvaINXypDQ&quot;&gt;八篇 NeurIPS 2019 最新图神经网络（GNN）相关论文&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;mixhop&quot;&gt;mixhop&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-mixhop.html&quot;&gt;https://daiwk.github.io/posts/dl-mixhop.html&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>【置顶】导引——gan论文集合</title>
   <link href="http://daiwk.github.io/posts/links-navigation-gan.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/links-navigation-gan</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%a1%86%e6%9e%b6&quot;&gt;框架&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tf-gan&quot;&gt;tf-gan&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%9f%ba%e6%9c%ac%e5%8e%9f%e7%90%86&quot;&gt;基本原理&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%ba%a6%e9%87%8f%e6%96%b9%e6%b3%95&quot;&gt;度量方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#basic-gan&quot;&gt;basic gan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gan-landscape&quot;&gt;gan landscape&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gan-with-the-wind&quot;&gt;gan with the wind&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gan%e6%8e%a8%e5%af%bc&quot;&gt;gan推导&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%af%b9%e6%8a%97%e6%a0%b7%e6%9c%ac%e5%90%ab%e4%b9%89&quot;&gt;对抗样本含义&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gan%e5%89%aa%e6%9e%9d&quot;&gt;gan剪枝&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cv&quot;&gt;cv&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#ls-gan&quot;&gt;ls-gan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#wgan&quot;&gt;wgan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#lapgan&quot;&gt;lapgan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#infogan--improvedgan&quot;&gt;infogan &amp;amp; improvedgan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#dcgan&quot;&gt;dcgan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#bigganbiggan-deepbigbigan&quot;&gt;BigGAN+BigGAN-deep+BigBiGAN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#treegan&quot;&gt;TreeGAN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#stackgan&quot;&gt;stackGAN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#cyclegan&quot;&gt;cycleGAN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#deblurgan&quot;&gt;deblurGAN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#vq-vae&quot;&gt;VQ-VAE&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#nlp&quot;&gt;nlp&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#seqgan&quot;&gt;seqgan&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%90%9c%e7%b4%a2%e4%b8%8e%e6%8e%a8%e8%8d%90&quot;&gt;搜索与推荐&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#irgan&quot;&gt;irgan&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gan-cdqn&quot;&gt;GAN-CDQN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#rsgan&quot;&gt;rsgan&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%a4%9a%e6%a8%a1%e6%80%81&quot;&gt;多模态&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#txt2img&quot;&gt;txt2img&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#automl&quot;&gt;automl&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#autogan&quot;&gt;autoGAN&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#audio&quot;&gt;audio&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#gan-tts&quot;&gt;gan-tts&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;框架&quot;&gt;框架&lt;/h2&gt;

&lt;h3 id=&quot;tf-gan&quot;&gt;tf-gan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/platform-tf-gan.html&quot;&gt;https://daiwk.github.io/posts/platform-tf-gan.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;基本原理&quot;&gt;基本原理&lt;/h2&gt;

&lt;h3 id=&quot;度量方法&quot;&gt;度量方法&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/jGSqB6UpCeQo8FC219TWbQ&quot;&gt;学习GAN模型量化评价，先从掌握FID开始吧&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;basic-gan&quot;&gt;basic gan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/image-gan.html&quot;&gt;https://daiwk.github.io/posts/image-gan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;gan-landscape&quot;&gt;gan landscape&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-gan-overview.html&quot;&gt;https://daiwk.github.io/posts/cv-gan-overview.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;gan-with-the-wind&quot;&gt;gan with the wind&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-gan-with-the-wind.html&quot;&gt;https://daiwk.github.io/posts/cv-gan-with-the-wind.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;gan推导&quot;&gt;gan推导&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-gan-theories.html&quot;&gt;https://daiwk.github.io/posts/cv-gan-theories.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;对抗样本含义&quot;&gt;对抗样本含义&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/thEWzBBSb6q97xz1gFmb4A&quot;&gt;你真的懂对抗样本吗？一文重新思考对抗样本背后的含义&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;gan剪枝&quot;&gt;gan剪枝&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-gan-pruning.html&quot;&gt;https://daiwk.github.io/posts/cv-gan-pruning.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;cv&quot;&gt;cv&lt;/h2&gt;

&lt;h3 id=&quot;ls-gan&quot;&gt;ls-gan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/image-lsgan.html&quot;&gt;https://daiwk.github.io/posts/image-lsgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;wgan&quot;&gt;wgan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/image-wgan.html&quot;&gt;https://daiwk.github.io/posts/image-wgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;lapgan&quot;&gt;lapgan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/image-lapgan.html&quot;&gt;https://daiwk.github.io/posts/image-lapgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;infogan--improvedgan&quot;&gt;infogan &amp;amp; improvedgan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/image-infogan-improvedgan.html&quot;&gt;https://daiwk.github.io/posts/image-infogan-improvedgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;dcgan&quot;&gt;dcgan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/image-dcgan.html&quot;&gt;https://daiwk.github.io/posts/image-dcgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;bigganbiggan-deepbigbigan&quot;&gt;BigGAN+BigGAN-deep+BigBiGAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-biggan.html&quot;&gt;https://daiwk.github.io/posts/cv-biggan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;treegan&quot;&gt;TreeGAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/nlp-treegan.html&quot;&gt;https://daiwk.github.io/posts/nlp-treegan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;stackgan&quot;&gt;stackGAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-stackgan.html&quot;&gt;https://daiwk.github.io/posts/cv-stackgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;cyclegan&quot;&gt;cycleGAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-cyclegan.html&quot;&gt;https://daiwk.github.io/posts/cv-cyclegan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;deblurgan&quot;&gt;deblurGAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-deblurgan.html&quot;&gt;https://daiwk.github.io/posts/cv-deblurgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;vq-vae&quot;&gt;VQ-VAE&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-vq-vae.html&quot;&gt;https://daiwk.github.io/posts/cv-vq-vae.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;nlp&quot;&gt;nlp&lt;/h2&gt;

&lt;h3 id=&quot;seqgan&quot;&gt;seqgan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/rl-seqgan.html&quot;&gt;https://daiwk.github.io/posts/rl-seqgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;搜索与推荐&quot;&gt;搜索与推荐&lt;/h2&gt;

&lt;h3 id=&quot;irgan&quot;&gt;irgan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/rl-irgan.html&quot;&gt;https://daiwk.github.io/posts/rl-irgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;gan-cdqn&quot;&gt;GAN-CDQN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-gan-rl-recommend.html&quot;&gt;https://daiwk.github.io/posts/dl-gan-rl-recommend.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;rsgan&quot;&gt;rsgan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/dl-rsgan.html&quot;&gt;https://daiwk.github.io/posts/dl-rsgan.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;多模态&quot;&gt;多模态&lt;/h2&gt;

&lt;h3 id=&quot;txt2img&quot;&gt;txt2img&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-gan-txt2img.html&quot;&gt;https://daiwk.github.io/posts/cv-gan-txt2img.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;automl&quot;&gt;automl&lt;/h2&gt;

&lt;h3 id=&quot;autogan&quot;&gt;autoGAN&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/cv-autogan.html&quot;&gt;https://daiwk.github.io/posts/cv-autogan.html&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;audio&quot;&gt;audio&lt;/h2&gt;

&lt;h3 id=&quot;gan-tts&quot;&gt;gan-tts&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/posts/audio-gan-tts.html&quot;&gt;https://daiwk.github.io/posts/audio-gan-tts.html&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>url</title>
   <link href="http://daiwk.github.io/posts/knowledge-url.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-url</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#url格式&quot;&gt;url格式&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;url格式&quot;&gt;url格式&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// URI scheme : http://en.wikipedia.org/wiki/URI_scheme
//
//  foo://username:password@example.com:8042/over/there/index.dtb?type=animal&amp;amp;name=narwhal#nose
//  \_/   \_______________/ \_________/ \__/            \___/ \_/ \______________________/ \__/
//   |           |               |       |                |    |            |                |
//   |       userinfo           host    port              |    |          query          fragment
//   |    \________________________________/\_____________|____|/ \__/        \__/
// schema                 |                          |    |    |    |          |
//                    authority                      |    |    |    |          |
//                                                 path   |    |    interpretable as keys
//                                                        |    |
//        \_______________________________________________|____|/       \____/     \_____/
//                             |                          |    |          |           |
//                     hierarchical part                  |    |    interpretable as values
//                                                        |    |
//                                   interpretable as filename |
//                                                             |
//                                                             |
//                                               interpretable as extension
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>tf常用函数</title>
   <link href="http://daiwk.github.io/posts/knowledge-tf-usage.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-tf-usage</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%89%b9%e5%be%81%e7%9b%b8%e5%85%b3&quot;&gt;特征相关&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#estimator%e7%9b%b8%e5%85%b3&quot;&gt;estimator相关&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%9f%ba%e6%9c%ac%e5%87%bd%e6%95%b0&quot;&gt;基本函数&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tfsegmentsum&quot;&gt;tf.segment_sum&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tftruncated-normal&quot;&gt;tf.truncated-normal&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tfreduce&quot;&gt;tf.reduce-*&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tfnn&quot;&gt;tf.nn&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#cost&quot;&gt;cost&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#tfnnsoftmax-cross-entropy-with-logits&quot;&gt;tf.nn.softmax-cross-entropy-with-logits&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tfnnweighted-cross-entropy-with-logits&quot;&gt;tf.nn.weighted-cross-entropy-with-logits&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tfnnnce-loss&quot;&gt;tf.nn.nce-loss&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#activations&quot;&gt;activations&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#tfnnrelu&quot;&gt;tf.nn.relu&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ops&quot;&gt;ops&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#tfnnembedding-lookup&quot;&gt;tf.nn.embedding-lookup&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tfnnconv2d&quot;&gt;tf.nn.conv2d&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#tfnnmax-pool&quot;&gt;tf.nn.max-pool&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tfcontriblayers&quot;&gt;tf.contrib.layers&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tfcontriblayersflatten&quot;&gt;tf.contrib.layers.flatten&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tfcontriblayersfully-connected&quot;&gt;tf.contrib.layers.fully-connected&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98&quot;&gt;常见问题&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e5%8f%82%e6%95%b0%e9%87%8f&quot;&gt;如何统计参数量&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#tf%e4%b8%8b%e8%bd%bd%e6%95%b0%e6%8d%ae%e9%9b%86%e5%87%ba%e7%8e%b0ssl%e9%97%ae%e9%a2%98%e6%97%b6&quot;&gt;tf下载数据集出现ssl问题时&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#steps-vs-epoch&quot;&gt;steps v.s. epoch&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#nan&quot;&gt;nan&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%83%85%e5%86%b51loss%e5%87%ba%e7%8e%b0nan&quot;&gt;情况1：loss出现nan&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%e6%83%85%e5%86%b52%e6%9b%b4%e6%96%b0%e7%bd%91%e7%bb%9c%e6%97%b6%e5%87%ba%e7%8e%b0nan%e5%80%bc&quot;&gt;情况2：更新网络时出现Nan值&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;特征相关&quot;&gt;特征相关&lt;/h2&gt;

&lt;p&gt;官方文档参考&lt;a href=&quot;https://www.tensorflow.org/guide/feature_columns?hl=zh_cn&quot;&gt;https://www.tensorflow.org/guide/feature_columns?hl=zh_cn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;实践参考&lt;a href=&quot;https://daiwk.github.io/posts/ml-feature-engineering.html#6-tf%E7%9A%84%E7%89%B9%E5%BE%81%E5%B7%A5%E7%A8%8B&quot;&gt;https://daiwk.github.io/posts/ml-feature-engineering.html#6-tf%E7%9A%84%E7%89%B9%E5%BE%81%E5%B7%A5%E7%A8%8B&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;estimator相关&quot;&gt;estimator相关&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/guide/custom_estimators?hl=zh_cn&quot;&gt;https://www.tensorflow.org/guide/custom_estimators?hl=zh_cn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;分类的demo：&lt;a href=&quot;https://github.com/daiwk/grace_t/blob/master/python/grace_t/basic_demos/custom_estimator_classifier.py&quot;&gt;https://github.com/daiwk/grace_t/blob/master/python/grace_t/basic_demos/custom_estimator_classifier.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;回归的demo：&lt;a href=&quot;https://github.com/daiwk/grace_t/blob/master/python/grace_t/basic_demos/custom_estimator_regressor.py&quot;&gt;https://github.com/daiwk/grace_t/blob/master/python/grace_t/basic_demos/custom_estimator_regressor.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如果想要estimator把一个东西输出到tensorboard中，只要自己定义一个scalar就行了(参考&lt;a href=&quot;https://stackoverflow.com/questions/50264302/trying-to-log-accuracy-of-cnn-tensorflow-on-tensorboard&quot;&gt;https://stackoverflow.com/questions/50264302/trying-to-log-accuracy-of-cnn-tensorflow-on-tensorboard&lt;/a&gt;)：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;losses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean_squared_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expand_dims&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;predictions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'total-loss'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'avg-loss'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;基本函数&quot;&gt;基本函数&lt;/h2&gt;

&lt;h3 id=&quot;tfsegment_sum&quot;&gt;tf.segment_sum&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.w3cschool.cn/tensorflow_python/tensorflow_python-ua7w2jip.html&quot;&gt;https://www.w3cschool.cn/tensorflow_python/tensorflow_python-ua7w2jip.html&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/tf_segment_add.png&quot; style=&quot;max-height: 100px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;segment_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# [[13 32 32  5]
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# [ 5  6  7  8]]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;tftruncated-normal&quot;&gt;tf.truncated-normal&lt;/h3&gt;

&lt;p&gt;tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)&lt;/p&gt;

&lt;p&gt;从截断的正态分布中输出随机值。 
生成的值服从具有指定平均值和标准偏差的正态分布，如果生成的值大于平均值2个标准偏差的值则丢弃重新选择。&lt;/p&gt;

&lt;p&gt;在tf.truncated_normal中如果x的取值在区间（μ-2σ，μ+2σ）之外则重新进行选择。这样&lt;strong&gt;保证了生成的值都在均值附近。&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;tfreduce-&quot;&gt;tf.reduce-*&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;tf.reduce_mean:&lt;/strong&gt; computes the mean of elements across dimensions of a tensor. Use this to sum the losses over all the examples to get the overall cost. You can check the full documentation &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/reduce_mean&quot;&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;实现代码在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/python/ops/math_ops.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;tensorflow中有一类在tensor的某一维度上求值的函数。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;求最大值tf.reduce_max()&lt;/li&gt;
  &lt;li&gt;求平均值tf.reduce_mean()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参数：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;input_tensor:待求值的tensor。&lt;/li&gt;
  &lt;li&gt;keepdims:是否保持其他维不变。（之前叫keep_dims）&lt;/li&gt;
  &lt;li&gt;axis:要对哪一维进行操作(之前叫reduction_indices)，只对这维求max/min，其他维删除。如果设置了keepdims=True，那么其他维的大小保持不变，要在[-rank(input_tensor), rank(input_tensor))范围内。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;先看一个简单的例子：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# x.shape=(2, 3)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keepdims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;c1&quot;&gt;# 2行3列，axis=1就在列维度操作，n列变成1列，即每一行求max，合到一列里
# 相当于只有第1维有值其他几维没东西了，第1维存的是其他几维的max
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keepdims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;c1&quot;&gt;# 2行3列，axis=0就在行维度操作，n行变成1行，即每一列求max，合到一行里
# 相当于只有第0维有值其他几维没东西了，第0维存的是其他几维的max
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再看个复杂一点的&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;55&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;77&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# x.shape=(2, 2, 3)
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# [
# [
# [
# [1,2,3], [4,5,6]
# ]
# ], 
# [
# [
# [22,33,44], [55,66,77]
# ]
# ]
# ]
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keepdims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keepdims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keepdims&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;22.&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;33.&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;44.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;55.&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;66.&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;77.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;[[[&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;5.&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;

 &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;55.&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;66.&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;77.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;[[[&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;3.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;

 &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;44.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;77.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;tfnn&quot;&gt;tf.nn&lt;/h2&gt;

&lt;h3 id=&quot;cost&quot;&gt;cost&lt;/h3&gt;

&lt;h4 id=&quot;tfnnsoftmax-cross-entropy-with-logits&quot;&gt;tf.nn.softmax-cross-entropy-with-logits&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;tf.nn.softmax_cross_entropy_with_logits(logits = Z3, labels = Y):&lt;/strong&gt; computes the softmax entropy loss. This function both computes the softmax activation function as well as the resulting loss. You can check the full documentation  &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/softmax_cross_entropy_with_logits&quot;&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;tfnnweighted-cross-entropy-with-logits&quot;&gt;tf.nn.weighted-cross-entropy-with-logits&lt;/h4&gt;

&lt;p&gt;正常的cross-entropy loss如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;targets * -log(sigmoid(logits)) +
    (1 - targets) * -log(1 - sigmoid(logits))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其实就是，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(L(\hat y, y)=-(ylog\hat y+(1-y)log(1-\hat y))\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而所谓的weighted，就是乘了一个pos_weight：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;targets * -log(sigmoid(logits)) * pos_weight +
    (1 - targets) * -log(1 - sigmoid(logits))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;tfnnnce-loss&quot;&gt;tf.nn.nce-loss&lt;/h4&gt;

&lt;p&gt;原理和使用参考&lt;a href=&quot;https://daiwk.github.io/posts/nlp-word2vec.html#4-nce&quot;&gt;https://daiwk.github.io/posts/nlp-word2vec.html#4-nce&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.tensorflow.org/versions/r1.9/api_docs/python/tf/nn/nce_loss&quot;&gt;https://www.tensorflow.org/versions/r1.9/api_docs/python/tf/nn/nce_loss&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;抄过来：&lt;/p&gt;

&lt;p&gt;在NCE的实现中，使用的是log_uniform_candidate_sampler：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;会在[0, range_max)中采样出一个整数k(&lt;strong&gt;k相当于词的id&lt;/strong&gt;)&lt;/li&gt;
  &lt;li&gt;P(k) = (log(k + 2) - log(k + 1)) / log(range_max + 1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{aligned}
P(k)&amp;amp;=\frac{1}{log(range\_max+1)}log(\frac{k+2}{k+1}) \\ 
 &amp;amp;= \frac{1}{log(range\_max+1)}log(1+\frac{1}{k+1}) \\
\end{aligned}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;k越大，被采样到的概率越小。&lt;/strong&gt;而我们的词典中，可以发现&lt;strong&gt;词频高&lt;/strong&gt;的&lt;strong&gt;index小&lt;/strong&gt;，所以高词频的词会被&lt;strong&gt;优先&lt;/strong&gt;采样为负样本。&lt;/p&gt;

&lt;p&gt;nce的实现可以参考：&lt;a href=&quot;https://www.jianshu.com/p/fab82fa53e16&quot;&gt;https://www.jianshu.com/p/fab82fa53e16&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;activations&quot;&gt;activations&lt;/h3&gt;

&lt;h4 id=&quot;tfnnrelu&quot;&gt;tf.nn.relu&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;tf.nn.relu(Z1):&lt;/strong&gt; computes the elementwise ReLU of Z1 (which can be any shape). You can read the full documentation &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/relu&quot;&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;ops&quot;&gt;ops&lt;/h3&gt;

&lt;h4 id=&quot;tfnnembedding-lookup&quot;&gt;tf.nn.embedding-lookup&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embedding_lookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;partition_strategy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'mod'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;validate_indices&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;max_norm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.gather&lt;/code&gt;的generalization。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;params&lt;/code&gt;:&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ids&lt;/code&gt;: 一个int32或者int64的tensor，包括了需要lookup的ids&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;partition_strategy&lt;/code&gt;: 如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;len(params) &amp;gt; 1&lt;/code&gt;，这个参数有用。有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;div&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mod&lt;/code&gt;两种取值，默认是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mod&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt;: 这个op的名字&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;validate_indices&lt;/code&gt;: 已经没用了&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max_norm&lt;/code&gt;：如果非None，如果emb的l2 norm比这个值大，就clip掉&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;详解：&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;params&lt;/code&gt;是一个list的tensors。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;如果这个list的size是1，也就是说只有一个vocab_size x emb_size的tensor，那就是普通的emb。&lt;/li&gt;
  &lt;li&gt;如果这个list的size大于1，即&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;len(params) &amp;gt; 1&lt;/code&gt;，&lt;strong&gt;这个list里每个tensor第二维要一样（emb的size），第一维可以不一样（每个partition里有多少个词）&lt;/strong&gt;。其实就是一个大型embedding tensor的partitioning，可以是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PartitionedVariable&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果，那么会根据partition_strategy来进行partition：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;mod的切分方式：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;partition号是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p = id % len(params)&lt;/code&gt;。所以如果有5个id，要分成3个partition，那么结果就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[0, 3], [1, 4], [2]]&lt;/code&gt;。所以这个时候，输入的params的shape就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[(2, emb_size), (2, emb_size), (1, emb_size)]&lt;/code&gt;。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;div的切分方式：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;partition是连续的。&lt;/p&gt;

&lt;p&gt;所以如果有5个id，要分成3个partition，那么结果就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[0, 1], [2, 3], [4]]&lt;/code&gt;。所以这个时候，输入的params的shape就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[(2, emb_size), (2, emb_size), (1, emb_size)]&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;返回的shape：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shape(ids) + shape(params)[1:]&lt;/code&gt;，即ids的size x emb_size&lt;/p&gt;

&lt;h4 id=&quot;tfnnconv2d&quot;&gt;tf.nn.conv2d&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;tf.nn.conv2d(X,W1, strides = [1,s,s,1], padding = ‘SAME’):&lt;/strong&gt; given an input $X$ and a group of filters &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;W1&lt;/code&gt;, this function convolves &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;W1&lt;/code&gt;’s filters on X. The third input ([1,f,f,1]) represents the strides for each dimension of the input (m, n_H_prev, n_W_prev, n_C_prev). You can read the full documentation &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/conv2d&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;实现代码在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow/python/ops/gen_nn_ops.py&lt;/code&gt;中。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;strides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;use_cudnn_on_gpu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data_format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'NHWC'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dilations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输入的shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch, in_height, in_width, in_channels]&lt;/code&gt;，即『NHWC』，一个kernel或filter的shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[filter_height, filter_width, in_channels, out_channels]&lt;/code&gt;。这个函数实现如下功能：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对filter进行flatten，变成一个shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[filter_height * filter_width * in_channels, output_channels]&lt;/code&gt;的2D矩阵&lt;/li&gt;
  &lt;li&gt;将input tensor的image patches进行extract，并组成一个shape是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[batch, out_height, out_width, filter_height * filter_width * in_channels]&lt;/code&gt;的virtual tensor。&lt;/li&gt;
  &lt;li&gt;对于每一个patch，right-multiplies the filter matrix and the image patch vector.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sum_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;di&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;di&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strides&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
                    &lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;di&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;必须满足&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strides[0] = strides[3] = 1&lt;/code&gt;，对于最common的case，也就是horizontal and vertices strides是一样的， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strides = [1, stride, stride, 1]&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;tfnnmax-pool&quot;&gt;tf.nn.max-pool&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;tf.nn.max_pool(A, ksize = [1,f,f,1], strides = [1,s,s,1], padding = ‘SAME’):&lt;/strong&gt; given an input A, this function uses a window of size (f, f) and strides of size (s, s) to carry out max pooling over each window. You can read the full documentation &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/nn/max_pool&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;tfcontriblayers&quot;&gt;tf.contrib.layers&lt;/h2&gt;

&lt;h3 id=&quot;tfcontriblayersflatten&quot;&gt;tf.contrib.layers.flatten&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;tf.contrib.layers.flatten(P)&lt;/strong&gt;: given an input P, this function flattens each example into a 1D vector it while maintaining the batch-size. It returns a flattened tensor with shape [batch_size, k]. You can read the full documentation &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/contrib/layers/flatten&quot;&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;tfcontriblayersfully-connected&quot;&gt;tf.contrib.layers.fully-connected&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;tf.contrib.layers.fully_connected(F, num_outputs):&lt;/strong&gt; given a the flattened input F, it returns the output computed using a fully connected layer. You can read the full documentation &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/contrib/layers/fully_connected&quot;&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;常见问题&quot;&gt;常见问题&lt;/h2&gt;

&lt;h3 id=&quot;如何统计参数量&quot;&gt;如何统计参数量&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/feynman233/article/details/79187304&quot;&gt;https://blog.csdn.net/feynman233/article/details/79187304&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;tf下载数据集出现ssl问题时&quot;&gt;tf下载数据集出现ssl问题时&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/wangxiaotian2007/article/details/79284124&quot;&gt;https://blog.csdn.net/wangxiaotian2007/article/details/79284124&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;例如，修改&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python-2.7.14/lib/python2.7/site-packages/tensorflow/python/keras/utils/data_utils.py&lt;/code&gt;，增加：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ssl&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_create_default_https_context&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ssl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_create_unverified_context&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;steps-vs-epoch&quot;&gt;steps v.s. epoch&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://stackoverflow.com/questions/42816124/steps-vs-num-epochs-in-tensorflow-getting-started-tutorial&quot;&gt;https://stackoverflow.com/questions/42816124/steps-vs-num-epochs-in-tensorflow-getting-started-tutorial&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;整理了一下，大概是这样吧：&lt;/p&gt;

&lt;p&gt;epoch：1个epoch等于使用一次训练集中的全部样本训练；也就是训练整个数据集的重复数。
step: number of times the training loop in your learning algorithm will run to update the parameters in the model. 相当于如果每batchsize个样本update一次模型，那么应该就是&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
num_examples \times epochs / batchsize = steps
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果设置的steps比上面的大，可能会继续训？相当于加多了epoch？如果比上面的数小的话，相当于early stopping吧？&lt;/p&gt;

&lt;p&gt;Why do we need this? There are many variations on gradient descent (batch, stochastic, mini-batch) as well as other algorithms for optimizing the learning parameters (e.g., L-BFGS). Some of them need to see the data in batches, while others see one datum at a time. Also, some of them include random factors/steps, hence you might need multiple passes on the data to get good convergence.&lt;/p&gt;

&lt;h3 id=&quot;nan&quot;&gt;nan&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/qq_22291287/article/details/82712050&quot;&gt;https://blog.csdn.net/qq_22291287/article/details/82712050&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;情况1loss出现nan&quot;&gt;情况1：loss出现nan&lt;/h4&gt;

&lt;p&gt;大致的解决办法就是，在出现Nan值的loss中一般是使用的TensorFlow的log函数，然后计算得到的Nan，一般是输入的值中出现了负数值或者0值，在TensorFlow的官网上的教程中，使用其调试器调试Nan值的出现，也是查到了计算log的传参为0；而解决的办法也很简单，假设传参给log的参数为y，那么在调用log前，进行一次数值剪切，修改调用如下：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clip_by_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样，y的最小值为0的情况就被替换成了一个极小值，1e-8，这样就不会出现Nan值了&lt;/p&gt;

&lt;p&gt;tf.clip_by_value这个函数，是将第一个参数，限制在第二、三个参数指定的范围之内，使用这个函数的原意是要避免0值，并没有限制最大值，可以稍加修改，就确保了对于y值的剪切，不会影响到其数值的上限：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clip_by_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;但是在实际的神经网络中使用的时候，我发现这样修改后，虽然loss的数值一直在变化，可是优化后的结果几乎是保持不变的，这就存在问题了。&lt;/p&gt;

&lt;p&gt;经过检查，其实并不能这么简单的为了持续训练，而修改计算损失函数时的输入值。这样修改后，loss的数值很可能（存在0的话确定就是）假的数值，会对优化器优化的过程造成一定的影响，导致优化器并不能正常的工作。&lt;/p&gt;

&lt;p&gt;要解决这个假的loss的方法很简单，就是人为的改造神经网络，来控制输出的结果，不会存在0。这就需要设计好最后一层输出层的激活函数，每个激活函数都是存在值域的。&lt;/p&gt;

&lt;p&gt;比如要给一个在(0,1)之间的输出（不包含0），那么显然sigmoid是最好的选择。不过需要注意的是，在TensorFlow中，tf.nn.sigmoid函数，在输出的参数非常大，或者非常小的情况下，会给出边界值1或者0的输出，这就意味着，改造神经网络的过程，并不只是最后一层输出层的激活函数，你必须确保自己大致知道每一层的输出的一个范围，这样才能彻底的解决Nan值的出现。&lt;/p&gt;

&lt;p&gt;举例说明就是TensorFlow的官网给的教程，其输出层使用的是softmax激活函数，其数值在[0,1]，这在设计的时候，基本就确定了会出现Nan值的情况，只是发生的时间罢了。&lt;/p&gt;

&lt;h4 id=&quot;情况2更新网络时出现nan值&quot;&gt;情况2：更新网络时出现Nan值&lt;/h4&gt;

&lt;p&gt;更新网络中出现Nan值很难发现，但是一般调试程序的时候，会用summary去观测权重等网络中的值的更新，因而，此时出现Nan值的话，会报错类似如下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;InvalidArgumentError &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;see above &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;traceback&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: Nan &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;summary histogram &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;: weight_1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样的情况，一般是由于优化器的学习率设置不当导致的，而且一般是学习率设置过高导致的，因而此时可以尝试使用更小的学习率进行训练来解决这样的问题。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;数据本身，是否存在Nan,可以用numpy.any(numpy.isnan(x))检查一下input和target&lt;/li&gt;
  &lt;li&gt;在训练的时候，整个网络随机初始化，很容易出现Nan，这时候需要把学习率调小，可以尝试0.1，0.01，0.001，直到不出现Nan为止，如果一直都有，那可能是网络实现问题。学习率和网络的层数一般成反比，层数越多，学习率通常要减小。有时候可以先用较小的学习率训练5000或以上次迭代，得到参数输出，手动kill掉训练，用前面的参数fine tune，这时候可以加大学习率，能更快收敛哦&lt;/li&gt;
  &lt;li&gt;如果是图片，那么得转化为float 也就是/255.&lt;/li&gt;
  &lt;li&gt;relu和softmax两层不要连着用，最好将relu改成tanh,什么原因呢&lt;/li&gt;
  &lt;li&gt;参数初始化&lt;/li&gt;
  &lt;li&gt;batch size 选择过小&lt;/li&gt;
  &lt;li&gt;最后还没有排除问题的话，TensorFlow有专门的内置调试器(tf_debug)来帮助调试此类问题&lt;a href=&quot;https://www.tensorflow.org/guide/debugger&quot;&gt;https://www.tensorflow.org/guide/debugger&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow.python&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf_debug&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 建立原来的Session
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 用tfdbg的Wrapper包裹原来的Session对象：
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf_debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LocalCLIDebugWrapperSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_tensor_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;has_inf_or_nan&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf_debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_inf_or_nan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 以上为所有需要的代码变动，其余的代码可以保留不变，因为包裹有的sess和原来的界面一致。
# 但是每次执行`sess.run`的时候，自动进入调试器命令行环境。
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train_op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;！！！&lt;strong&gt;目前看，只能在mac或者windows这种有图形界面的地方玩的哈！！&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;在tfdbg命令行环境里面，输入如下命令，可以让程序执行到inf或nan第一次出现。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;tfdbg&amp;gt; run &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; has_inf_or_nan
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;一旦inf/nan出现，界面现实所有包含此类病态数值的张量，按照时间排序。所以第一个就最有可能是最先出现inf/nan的节点。可以用node_info, list_inputs等命令进一步查看节点的类型和输入，来发现问题的缘由。&lt;/p&gt;

&lt;p&gt;来看个简单demo（假设是demo.py）：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;__future__&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print_function&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow.python&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf_debug&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_deubg_nan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;a_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;init&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;## add this
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf_debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LocalCLIDebugWrapperSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_tensor_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;has_inf_or_nan&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf_debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;has_inf_or_nan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;divide&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;test_deubg_nan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python demo.py&lt;/code&gt;，出现如下界面时，输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run -f has_inf_or_nan&lt;/code&gt;：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tfdebug-1.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;然后在这个页面里，就可以拿鼠标乱点了。。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tfdebug-2.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;比如点&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true:div:0&lt;/code&gt;，就会出现详情，也可以点左上角的返回啥的，继续看其他tensor啦。。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/tfdebug-3.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>tf安装</title>
   <link href="http://daiwk.github.io/posts/knowledge-tf-install.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-tf-install</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E8%A3%85%E5%A4%87%E5%B7%A5%E4%BD%9C&quot;&gt;装备工作&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#bazel&quot;&gt;bazel&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#jdk18&quot;&gt;jdk1.8&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%BA%90%E7%A0%81%E5%AE%89%E8%A3%85&quot;&gt;源码安装&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#clone&quot;&gt;clone&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#configure&quot;&gt;configure&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E7%94%9F%E6%88%90pippackage&quot;&gt;生成pip_package&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E4%BB%85cpu&quot;&gt;仅cpu&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#gpu&quot;&gt;gpu&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E7%94%9F%E6%88%90whl&quot;&gt;生成whl&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%E5%AE%89%E8%A3%85c%E5%BA%93&quot;&gt;安装c++库&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%E6%89%8B%E5%8A%A8%E4%B8%8B%E8%BD%BD%E4%BE%9D%E8%B5%96%E5%BA%93&quot;&gt;手动下载依赖库&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E9%87%8D%E6%96%B0configure&quot;&gt;重新configure&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E7%BC%96%E8%AF%91cc%E7%9A%84so&quot;&gt;编译cc的so&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E6%8B%B7%E8%B4%9D%E6%89%80%E9%9C%80%E5%A4%B4%E6%96%87%E4%BB%B6&quot;&gt;拷贝所需头文件&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#%E6%8B%B7%E8%B4%9D%E6%89%80%E9%9C%80lib%E6%96%87%E4%BB%B6&quot;&gt;拷贝所需lib文件&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E6%BA%90%E7%A0%81%E5%AE%89%E8%A3%85tf-serving&quot;&gt;源码安装tf-serving&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;装备工作&quot;&gt;装备工作&lt;/h2&gt;

&lt;h3 id=&quot;bazel&quot;&gt;bazel&lt;/h3&gt;

&lt;h3 id=&quot;jdk18&quot;&gt;jdk1.8&lt;/h3&gt;

&lt;h2 id=&quot;源码安装&quot;&gt;源码安装&lt;/h2&gt;

&lt;h3 id=&quot;clone&quot;&gt;clone&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/tensorflow/tensorflow 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;configure&quot;&gt;configure&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./configure
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意，这里可以配置默认python路径&lt;/p&gt;

&lt;h3 id=&quot;生成pip_package&quot;&gt;生成pip_package&lt;/h3&gt;

&lt;h4 id=&quot;仅cpu&quot;&gt;仅cpu&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build &lt;span class=&quot;nt&quot;&gt;--config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;opt //tensorflow/tools/pip_package:build_pip_package
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;gpu&quot;&gt;gpu&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build &lt;span class=&quot;nt&quot;&gt;--config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;opt &lt;span class=&quot;nt&quot;&gt;--config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;cuda //tensorflow/tools/pip_package:build_pip_package
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;生成whl&quot;&gt;生成whl&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样，就在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp/tensorflow_pkg&lt;/code&gt;生成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorflow-xxxx-py2-none-any.whl&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;安装c库&quot;&gt;安装c++库&lt;/h3&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/hemajun815/tutorial/blob/master/tensorflow/compilling-tensorflow-source-code-into-C++-library-file.md&quot;&gt;https://github.com/hemajun815/tutorial/blob/master/tensorflow/compilling-tensorflow-source-code-into-C++-library-file.md&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;手动下载依赖库&quot;&gt;手动下载依赖库&lt;/h4&gt;

&lt;p&gt;进入目录：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;tensorflow/contrib/makefile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;执行文件：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sh &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; ./build_all_linux.sh
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; -
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意：&lt;/p&gt;

&lt;p&gt;必要时修改&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;download_dependencies.sh&lt;/code&gt;文件：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;curl需要支持https(可以升级)，比如，我们发现jumbo的curl是符合预期的，那可以在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;build_all_linux.sh&lt;/code&gt;一开头就加上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export PATH=~/.jumbo/bin/:$PATH&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;wget加上&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--no-check-certificate&lt;/code&gt;参数，还有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--secure-protocol=TLSv1.2&lt;/code&gt;参数，当然如果版本不够高就升级wget。如果不好升级可以做如下改动：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;把&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget &lt;span class=&quot;nt&quot;&gt;-P&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;tempdir&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;改成&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-Ls&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;tempdir&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;/xxx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;重新configure&quot;&gt;重新configure&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;tensorflow
./configure
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;编译cc的so&quot;&gt;编译cc的so&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;tensorflow
bazel build :libtensorflow_cc.so
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;产出在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bazel-bin/tensorflow/libtensorflow_cc.so&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;拷贝所需头文件&quot;&gt;拷贝所需头文件&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;tensorflow_include&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./tensorflow_include
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; tensorflow/contrib/makefile/downloads/eigen/Eigen &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; tensorflow/contrib/makefile/downloads/eigen/unsupported &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; tensorflow/contrib/makefile/gen/protobuf/include/google &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/
&lt;span class=&quot;nb&quot;&gt;cp &lt;/span&gt;tensorflow/contrib/makefile/downloads/nsync/public/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; bazel-genfiles/tensorflow &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; tensorflow/cc &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/tensorflow
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; tensorflow/core &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/tensorflow
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/third_party
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; third_party/eigen3 &lt;span class=&quot;nv&quot;&gt;$tensorflow_include&lt;/span&gt;/third_party/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;拷贝所需lib文件&quot;&gt;拷贝所需lib文件&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;tensorflow_lib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./tensorflow_lib
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tensorflow_lib&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cp &lt;/span&gt;bazel-bin/tensorflow/libtensorflow_&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.so &lt;span class=&quot;nv&quot;&gt;$tensorflow_lib&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;源码安装tf-serving&quot;&gt;源码安装tf-serving&lt;/h2&gt;

&lt;p&gt;github地址：&lt;a href=&quot;https://github.com/tensorflow/serving&quot;&gt;https://github.com/tensorflow/serving&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如果要clone某个版本，可以直接&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; r1.12 https://github.com/tensorflow/serving.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果在docker内安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./tools/run_in_docker.sh  bazel build tensorflow_serving/model_servers:tensorflow_model_server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果在非docker内安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bazel build tensorflow_serving/model_servers:tensorflow_model_server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们就发现产出了这么一个bin文件：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server&lt;/code&gt;。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>单元测试工具</title>
   <link href="http://daiwk.github.io/posts/knowledge-testing-tools.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-testing-tools</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#gtest&quot;&gt;gtest&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#显示所有测试用例&quot;&gt;显示所有测试用例&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#只执行部分测试用例&quot;&gt;只执行部分测试用例&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#bullseyecoverage&quot;&gt;BullseyeCoverage&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#压力工具&quot;&gt;压力工具&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#httpload&quot;&gt;httpload&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#siege&quot;&gt;siege&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ab&quot;&gt;ab&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#引流工具&quot;&gt;引流工具&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tcpcopy&quot;&gt;tcpcopy&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;gtest&quot;&gt;gtest&lt;/h2&gt;

&lt;p&gt;简介：&lt;a href=&quot;https://www.cnblogs.com/jycboy/p/6057677.html&quot;&gt;Google C++单元测试框架—Gtest框架简介（译文）&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;显示所有测试用例&quot;&gt;显示所有测试用例&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./xxx --gtest_list_tests
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;只执行部分测试用例&quot;&gt;只执行部分测试用例&lt;/h3&gt;

&lt;p&gt;例如上面的输出是&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;TestNodeA.
  test_a1
  test_a2
TestNodeB.
  test_b1
  test_b2
  test_b3
  test_b4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;那么，可以&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./xxx &lt;span class=&quot;nt&quot;&gt;--gtest_filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;TestNodeA.&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
./xxx &lt;span class=&quot;nt&quot;&gt;--gtest_filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;TestNodeA.test&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
./xxx &lt;span class=&quot;nt&quot;&gt;--gtest_filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;TestNodeA.test_a1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;bullseyecoverage&quot;&gt;BullseyeCoverage&lt;/h2&gt;

&lt;p&gt;官网的下载页面：&lt;a href=&quot;https://www.bullseye.com/cgi-bin/download.sh&quot;&gt;https://www.bullseye.com/cgi-bin/download.sh&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;简介：&lt;a href=&quot;http://blog.csdn.net/billbliss/article/details/43971629&quot;&gt;代码覆盖率工具BullseyeCoverage研究&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;压力工具&quot;&gt;压力工具&lt;/h2&gt;

&lt;h3 id=&quot;httpload&quot;&gt;httpload&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# -r 频率,每秒的请求数&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -s 时间,一共压测多长时间&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# url_list是url列表  每一条一行&lt;/span&gt;

./http_load &lt;span class=&quot;nt&quot;&gt;-r&lt;/span&gt; 3 &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; 3 url_list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;siege&quot;&gt;siege&lt;/h3&gt;

&lt;h3 id=&quot;ab&quot;&gt;ab&lt;/h3&gt;

&lt;h2 id=&quot;引流工具&quot;&gt;引流工具&lt;/h2&gt;

&lt;h3 id=&quot;tcpcopy&quot;&gt;tcpcopy&lt;/h3&gt;

</content>
 </entry>
 
 <entry>
   <title>栈、堆、core、gdb等</title>
   <link href="http://daiwk.github.io/posts/knowledge-stack-heap-core.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-stack-heap-core</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#gdb&quot;&gt;gdb&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#打印内存&quot;&gt;打印内存&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#pretty-stl&quot;&gt;pretty stl&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gogogo&quot;&gt;gogogo&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#基础命令&quot;&gt;基础命令&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#堆与栈&quot;&gt;堆与栈&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#core&quot;&gt;core&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#堆栈导致的core&quot;&gt;堆、栈导致的core&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#栈空间不足&quot;&gt;栈空间不足&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#其他容易core的点&quot;&gt;其他容易core的点&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#其他的一些操作&quot;&gt;其他的一些操作&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#把so里的地址转换成行号&quot;&gt;把so里的地址转换成行号&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#用gdb查py的core&quot;&gt;用gdb查py的core&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;gdb&quot;&gt;gdb&lt;/h2&gt;

&lt;h2 id=&quot;打印内存&quot;&gt;打印内存&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/allenlinrui/article/details/5964046&quot;&gt;https://blog.csdn.net/allenlinrui/article/details/5964046&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;u 表示从当前地址往后请求的字节数，如果不指定的话，GDB&lt;strong&gt;默认是4个bytes&lt;/strong&gt;。u参数可以用下面的字符来代替，b表示单字节，h表示双字节，w表示四字 节，g表示八字节。当我们指定了字节长度后，GDB会从指内存定的内存地址开始，读写指定字节，并把其当作一个值取出来。&lt;/p&gt;

&lt;p&gt;例如，假设有一个unordered_map a，&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;unordered_map&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unordered_map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;c1&quot;&gt;//a[222] = 333;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;888&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4444&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;777888&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;44449999&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;那么&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;  p a._M_h           
&lt;span class=&quot;nv&quot;&gt;$19&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_base&amp;lt;int, std::pair&amp;lt;int const, int&amp;gt;, std::__detail::_Select1st, std::equal_to&amp;lt;int&amp;gt;, std::hash&amp;lt;int&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hash_code_base&amp;lt;int, std::pair&amp;lt;int const, int&amp;gt;, std::__detail::_Select1st, std::hash&amp;lt;int&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::__detail::_Select1st, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Select1st&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;1, std::hash&amp;lt;int&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::hash&amp;lt;int&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__hash_base&amp;lt;unsigned long, int&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;2, std::__detail::_Mod_range_hashing, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Mod_range_hashing&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::equal_to&amp;lt;int&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::equal_to&amp;lt;int&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::binary_function&amp;lt;int, int, bool&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Map_base&amp;lt;int, std::pair&amp;lt;int const, int&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;int const, int&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;int&amp;gt;, std::hash&amp;lt;int&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Insert&amp;lt;int, std::pair&amp;lt;int const, int&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;int const, int&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;int&amp;gt;, std::hash&amp;lt;int&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Insert_base&amp;lt;int, std::pair&amp;lt;int const, int&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;int const, int&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;int&amp;gt;, std::hash&amp;lt;int&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Rehash_base&amp;lt;int, std::pair&amp;lt;int const, int&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;int const, int&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;int&amp;gt;, std::hash&amp;lt;int&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Equality&amp;lt;int, std::pair&amp;lt;int const, int&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;int const, int&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;int&amp;gt;, std::hash&amp;lt;int&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_buckets &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x60d140, _M_bucket_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11, 
  _M_bbegin &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;int const, int&amp;gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;int const, int&amp;gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_node &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x60d1c0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, _M_element_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, _M_rehash_policy &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    static _S_growth_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, _M_max_load_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, _M_next_resize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以看到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_M_buckets&lt;/code&gt;的地址是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x60d140&lt;/code&gt;，所以我们沿着这个地址一直打看看&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; x/240w 0x60d140
0x60d140:       0       0       4294958720      32767
0x60d150:       0       0       0       0
0x60d160:       0       0       0       0
0x60d170:       0       0       0       0
0x60d180:       6345152 0       0       0
0x60d190:       0       0       33      0
0x60d1a0:       0       0       888     4444
0x60d1b0:       0       0       33      0
0x60d1c0:       6345120 0       777888  44449999
0x60d1d0:       0       0       134705  0
0x60d1e0:       0       0       0       0
0x60d1f0:       0       0       0       0
0x60d200:       0       0       0       0
0x60d210:       0       0       0       0
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;pretty-stl&quot;&gt;pretty stl&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://sourceware.org/gdb/wiki/STLSupport&quot;&gt;https://sourceware.org/gdb/wiki/STLSupport&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;首先的首先！！！确定gdb的版本在7.x以上！！！&lt;/p&gt;

&lt;p&gt;首先，例如进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home/maude/gdb_printers/&lt;/code&gt;这个文件夹，&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后把下面这段贴到```~/.gdbinit``里：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python
import sys
sys.path.insert&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0, &lt;span class=&quot;s1&quot;&gt;'/home/maude/gdb_printers/python'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果是某个奇葩版本的gcc，例如是4.8.4的&lt;a href=&quot;https://www2.cs.duke.edu/csed/cplus/gcc-4.8.4/libstdc++-api-html/a00955_source.html&quot;&gt;https://www2.cs.duke.edu/csed/cplus/gcc-4.8.4/libstdc++-api-html/a00955_source.html&lt;/a&gt;，可以修改&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;libstdcxx/v6/printers.py&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StdBaiduHashtableIterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_bbegin'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_node'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_nxt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__node_type'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__iter__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__next__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;StopIteration&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;elt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dereference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_nxt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#valptr = elt['_M_storage'].address
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;valptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;elt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;valptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;elt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;template_argument&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;#return valptr.dereference()
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;valptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dereference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;调用的时候&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        &lt;span class=&quot;c1&quot;&gt;##data = self.flatten (imap (self.format_one, StdHashtableIterator (self.hashtable())))
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flatten&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format_one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StdBaiduHashtableIterator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hashtable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;详见&lt;a href=&quot;https://daiwk.github.io/assets/printers_gcc484.py&quot;&gt;printers_gcc484.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;当然，更直接的，我们可以用github上的分支！！&lt;a href=&quot;https://github.com/gcc-mirror/gcc/tree/gcc-4_8-branch&quot;&gt;https://github.com/gcc-mirror/gcc/tree/gcc-4_8-branch&lt;/a&gt;。。。。比较大。。要下好久，建议直接把zip下下来试试。&lt;/p&gt;

&lt;p&gt;对应的脚本在&lt;a href=&quot;https://github.com/gcc-mirror/gcc/blob/gcc-4_8-branch/libstdc%2B%2B-v3/python/libstdcxx/v6/printers.py&quot;&gt;https://github.com/gcc-mirror/gcc/blob/gcc-4_8-branch/libstdc%2B%2B-v3/python/libstdcxx/v6/printers.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;我们发现。。直接有_M_v的。。有点神奇&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tr1HashtableIterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_bbegin'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_node'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_nxt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__node_type'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__iter__&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__next__&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;StopIteration&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dereference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_v'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dereference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_M_nxt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;来深入了解一下这个神奇的『dereference』：&lt;a href=&quot;https://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean&quot;&gt;https://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;算了。。还是学习一下python的gdb库：&lt;a href=&quot;https://segmentfault.com/a/1190000005718889&quot;&gt;https://segmentfault.com/a/1190000005718889&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;打印一个string*，假设它的地址是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x21d4b4910&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;char&lt;span class=&quot;k&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;0x21d4b4910
&lt;span class=&quot;nv&quot;&gt;$3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x20f0e6578 &lt;span class=&quot;s2&quot;&gt;&quot;社232&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;同样地，我也进行了一顿乱改。。参考&lt;a href=&quot;https://daiwk.github.io/assets/printers_gcc48_dwk.py&quot;&gt;printers_gcc48_dwk.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对于stl的容器，直接&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p readlist_news_map 
&lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; std::unordered_map with 10 elements &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{[&lt;/span&gt;9602626015596390711] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x204a8bc08, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;9523043020393950406] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1a7b47188, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;9331244886375957645] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x204a8cd88, 
  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;9300120563861322953] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x11c951508, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;9249702819011901361] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x204a8ca08, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;9107056655757970751] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1a9e1fb88, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;8955599203978976702] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x19ab1f488, 
  &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;8742931355624092612] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x204a8ae08, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;8551664616403609062] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0xe0285f88, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;10450143932246181310] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0xe840ae08&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果不想要pretty的格式，直接&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p /r readlist_news_map   
&lt;span class=&quot;nv&quot;&gt;$4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::unordered_map&amp;lt;unsigned long, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::equal_to&amp;lt;unsigned long&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &amp;amp;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; @0x29688320: &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__allow_copy_cons&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, 
  _M_h &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_base&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hash_code_base&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::__detail::_Select1st, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Select1st&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;1, std::hash&amp;lt;unsigned long&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::hash&amp;lt;unsigned long&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__hash_base&amp;lt;unsigned long, unsigned long&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;2, std::__detail::_Mod_range_hashing, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Mod_range_hashing&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::equal_to&amp;lt;unsigned long&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::equal_to&amp;lt;unsigned long&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::binary_function&amp;lt;unsigned long, unsigned long, bool&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Map_base&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Insert&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Insert_base&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Rehash_base&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Equality&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_buckets &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1ce392a80, _M_bucket_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 47, 
    _M_bbegin &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_node &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        _M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0xe2cabcc0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, _M_element_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 10, _M_rehash_policy &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static _S_growth_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, _M_max_load_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, _M_next_resize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 47&lt;span class=&quot;o&quot;&gt;}}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;继续探究一下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;nv&quot;&gt;$7&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::__detail::_Hash_node_base &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0xe2cabcc0
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;nv&quot;&gt;$8&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x210605c80&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt
&lt;span class=&quot;nv&quot;&gt;$9&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0xe2caa700&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0xe2caaf60&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$11&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1f7059200&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$12&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x107a5abc0&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$13&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0xe2cab7a0&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$14&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1b382e0a0&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$15&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1c1679ce0&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$16&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1e3d4bd80&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$17&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果不要中间的星号，那么&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;nv&quot;&gt;$33&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::__detail::_Hash_node_base &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x1c1679ce0
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt
&lt;span class=&quot;nv&quot;&gt;$34&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::__detail::_Hash_node_base &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x1e3d4bd80
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;readlist_news_map._M_h._M_bbegin._M_node._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt._M_nxt&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_nxt._M_nxt
&lt;span class=&quot;nv&quot;&gt;$35&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::__detail::_Hash_node_base &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其实_Hash_node_base就只是一个里面只有一个指针的结构体。。没啥用的样子，我们看看另一个变量&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p readlist_news_map._M_h._M_buckets
&lt;span class=&quot;nv&quot;&gt;$46&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::_Hashtable&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;::__bucket_type &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x1ce392a80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后我们参考&lt;a href=&quot;https://www2.cs.duke.edu/csed/cplus/gcc-4.8.4/libstdc++-api-html/a00406.html&quot;&gt;https://www2.cs.duke.edu/csed/cplus/gcc-4.8.4/libstdc++-api-html/a00406.html&lt;/a&gt;，会发现_M_buckets『好像』是一个数组！&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Each _Hashtable data structure has:

_Bucket[] _M_buckets
_Hash_node_base _M_bbegin
size_type _M_bucket_count
size_type _M_element_count

with _Bucket being _Hash_node&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; and _Hash_node containing:

_Hash_node&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; _M_next
Tp _M_value
size_t _M_hash_code &lt;span class=&quot;k&quot;&gt;if &lt;/span&gt;cache_hash_code is &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;别被骗了。。这是个指针，不是数组&lt;a href=&quot;https://www2.cs.duke.edu/csed/cplus/gcc-4.8.4/libstdc++-api-html/a00955_source.html&quot;&gt;https://www2.cs.duke.edu/csed/cplus/gcc-4.8.4/libstdc++-api-html/a00955_source.html&lt;/a&gt;：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;__bucket_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;_M_buckets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p readlist_news_map._M_h._M_buckets[0]
&lt;span class=&quot;nv&quot;&gt;$47&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::_Hashtable&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;::__bucket_type&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x0
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p readlist_news_map._M_h._M_buckets[1]
&lt;span class=&quot;nv&quot;&gt;$48&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::_Hashtable&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;::__bucket_type&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x210605c80
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p readlist_news_map._M_h._M_buckets[2]
&lt;span class=&quot;nv&quot;&gt;$49&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::_Hashtable&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;::__bucket_type&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x0
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p readlist_news_map._M_h._M_buckets[3]
&lt;span class=&quot;nv&quot;&gt;$50&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::_Hashtable&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;::__bucket_type&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x0
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p readlist_news_map._M_h._M_buckets[4]
&lt;span class=&quot;nv&quot;&gt;$51&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::_Hashtable&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;::__bucket_type&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x0
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p readlist_news_map._M_h._M_buckets[5]
&lt;span class=&quot;nv&quot;&gt;$52&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::_Hashtable&amp;lt;unsigned long, std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::allocator&amp;lt;std::pair&amp;lt;unsigned long const, rec::common::RecNewsInfo&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;unsigned long&amp;gt;, std::hash&amp;lt;unsigned long&amp;gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;::__bucket_type&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x1b382e0a0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再然后看看_Hash_node的定义：&lt;a href=&quot;https://www2.cs.duke.edu/csed/cplus/gcc-4.8.4/libstdc++-api-html/a00957_source.html&quot;&gt;https://www2.cs.duke.edu/csed/cplus/gcc-4.8.4/libstdc++-api-html/a00957_source.html&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_Hash_node_base&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;_Hash_node_base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_M_nxt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;_Hash_node_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_M_nxt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;_Hash_node_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_Hash_node_base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_M_nxt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_Hash_node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_Hash_node_base&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;_Value&lt;/span&gt;       &lt;span class=&quot;n&quot;&gt;_M_v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;_Hash_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_M_v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)...)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

      &lt;span class=&quot;n&quot;&gt;_Hash_node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;_M_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_Hash_node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_M_nxt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__node_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_Hash_node_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__bucket_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__node_base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;……绝望了。。一切都是_Hash_node_base。。。只有个next指针，，玩毛啊&lt;/p&gt;

&lt;h3 id=&quot;gogogo&quot;&gt;gogogo&lt;/h3&gt;

&lt;p&gt;首先，我们需要把讨厌的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Type &amp;lt;return&amp;gt; to continue, or q &amp;lt;return&amp;gt; to quit&quot;&lt;/code&gt;给去掉：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;pagination off
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;我们可以发现如果字符串较长，会有”…“，因此可以这么搞：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; show print elements
Limit on string chars or array elements to print is 200.
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;print elements 0
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; show print elements
Limit on string chars or array elements to print is unlimited.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;另外，可以进行如下设置，这样就可以有缩进地打印了&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;print pretty on 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后一步步打印vector中的元素：&lt;/p&gt;

&lt;p&gt;把reslut的第5个元素打出来：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;nv&quot;&gt;$27&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__shared_ptr&amp;lt;rec::common::RidTmpInfo, &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;__gnu_cxx::_Lock_policy&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;2&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_ptr &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e653b860, _M_refcount &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_pi &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4bd3ec80&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;把它的_M_ptr打出来：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr
&lt;span class=&quot;nv&quot;&gt;$28&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;rid &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6189532051180867495, source_type &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, cupai_score &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0.528647482, real_score &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, res_score &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0.581260145, &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 43, mark &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, category &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, 
  slotid &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, predict_result &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static npos &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &amp;lt;optimized out&amp;gt;, 
    _M_dataplus &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, 
      _M_p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7ff3ac4e93f8 &amp;lt;std::string::_Rep::_S_empty_rep_storage+24&amp;gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, predictor_extmsg &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static npos &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &amp;lt;optimized out&amp;gt;, 
    _M_dataplus &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, 
      _M_p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x64f5b5d8 &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\f\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;63&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;63:&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, news_info &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0, video_info &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090508, click &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, show &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, ext_msg_v2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    static npos &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &amp;lt;optimized out&amp;gt;, _M_dataplus &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, 
      _M_p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1a1ecd648 &lt;span class=&quot;s2&quot;&gt;&quot;EhQZAAAAIK7q4D8gODEAAAAgrurgPxoIEgI0MxoCNDM=&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;把里面的video_info打出来：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr.video_info
&lt;span class=&quot;nv&quot;&gt;$31&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;rid &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 13611788894022198871, tag_w &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__allow_copy_cons&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_h &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hash_code_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::__detail::_Select1st, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::__detail::_Select1st, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Select1st&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;1, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__hash_base&amp;lt;unsigned long, std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;2, std::__detail::_Mod_range_hashing, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Mod_range_hashing&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::binary_function&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, bool&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Map_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Insert&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Insert_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Rehash_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Equality&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_buckets &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c0345a0, _M_bucket_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11, _M_bbegin &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_node &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, _M_element_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _M_rehash_policy &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static _S_growth_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, _M_max_load_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, _M_next_resize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11&lt;span class=&quot;o&quot;&gt;}}}&lt;/span&gt;, manual_tags &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__allow_copy_cons&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_h &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hash_code_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::__detail::_Select1st, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::__detail::_Select1st, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Select1st&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;1, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__hash_base&amp;lt;unsigned long, std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;2, std::__detail::_Mod_range_hashing, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Mod_range_hashing&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::binary_function&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, bool&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Map_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Insert&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Insert_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Rehash_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Equality&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_buckets &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c034600, _M_bucket_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11, _M_bbegin &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_node &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, _M_element_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _M_rehash_policy &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static _S_growth_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, _M_max_load_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, _M_next_resize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11&lt;span class=&quot;o&quot;&gt;}}}&lt;/span&gt;, videoinfo_pb &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::Message&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::MessageLite&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_vptr.MessageLite &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7ff3a084d5d0 &amp;lt;vtable &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;rec::doc::VideoInfo+16&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, static kRidFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, static kTitleSignFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, static kContentSignFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 3, static kQualityFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, static kGenreFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 5, static kTagsFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6, static kPublicTimeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 7, static kTabFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 8, static kManualTagsFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 9, static kNewCateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 10, static kNewSubCateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11, static kCheckAttributeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 12, static kVideoTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 13, static kLongVideoTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 14, static kIsCooperateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 15, static kIsNatureFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 16, static kCheckpropertyEntityFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 17, static kCheckpropertyPeopleFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 18, static kCheckpropertySceneFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 19, static kDomainFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 20, static kLiveTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 21, static kUploaderFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 22, static kIdlCate1FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 23, static kIdlCate2FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 24, static kMthidFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 25, static kCategoryFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 26, static kBigImgFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 27, static kDelTagFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 28, static kIsMicrovideoFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 29, static kAuthorAuthorityScoreV1FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 30, _unknown_fields_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;fields_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, rid_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 13611788894022198871, title_sign_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 14762509907472026416, content_sign_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 14762509907472026416, quality_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, genre_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 12, tags_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c0905b8, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, public_time_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1519785790, tab_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e65331f8, manual_tags_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090600, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x4e6418990, 0x4e64d0540, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, new_cate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533208, new_sub_cate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533210, check_attribute_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090648, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x4e6533218, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, video_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533220, long_video_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533228, is_cooperate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, checkproperty_entity_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090698, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, checkproperty_people_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c0906d0, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, checkproperty_scene_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090708, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, is_nature_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, idl_cate1_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 780, domain_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533230, live_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533238, uploader_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533240, mthid_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533248, category_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, idl_cate2_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1786, del_tag_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, big_img_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1de8908 &amp;lt;google::protobuf::internal::kEmptyString&amp;gt;, is_microvideo_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, author_authority_score_v1_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _cached_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _has_bits_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;167311071&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, static default_instance_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x27a6240&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;发现里面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;videoinfo_pb&lt;/code&gt;是我们想要的，于是&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr.video_info&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;.videoinfo_pb
&lt;span class=&quot;nv&quot;&gt;$34&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::Message&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::MessageLite&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_vptr.MessageLite &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7ff3a084d5d0 &amp;lt;vtable &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;rec::doc::VideoInfo+16&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, static kRidFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, static kTitleSignFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, static kContentSignFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 3, static kQualityFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, static kGenreFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 5, static kTagsFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6, static kPublicTimeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 7, static kTabFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 8, static kManualTagsFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 9, static kNewCateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 10, static kNewSubCateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11, static kCheckAttributeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 12, static kVideoTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 13, static kLongVideoTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 14, static kIsCooperateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 15, static kIsNatureFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 16, static kCheckpropertyEntityFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 17, static kCheckpropertyPeopleFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 18, static kCheckpropertySceneFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 19, static kDomainFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 20, static kLiveTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 21, static kUploaderFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 22, static kIdlCate1FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 23, static kIdlCate2FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 24, static kMthidFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 25, static kCategoryFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 26, static kBigImgFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 27, static kDelTagFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 28, static kIsMicrovideoFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 29, static kAuthorAuthorityScoreV1FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 30, _unknown_fields_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;fields_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, rid_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 13611788894022198871, title_sign_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 14762509907472026416, content_sign_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 14762509907472026416, quality_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, genre_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 12, tags_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c0905b8, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, public_time_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1519785790, tab_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e65331f8, manual_tags_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090600, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x4e6418990, 0x4e64d0540, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, new_cate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533208, new_sub_cate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533210, check_attribute_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090648, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x4e6533218, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, video_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533220, long_video_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533228, is_cooperate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, checkproperty_entity_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090698, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, checkproperty_people_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c0906d0, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, checkproperty_scene_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4c090708, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, is_nature_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, idl_cate1_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 780, domain_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533230, live_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533238, uploader_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533240, mthid_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e6533248, category_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, idl_cate2_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1786, del_tag_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, big_img_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1de8908 &amp;lt;google::protobuf::internal::kEmptyString&amp;gt;, is_microvideo_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, author_authority_score_v1_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _cached_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _has_bits_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;167311071&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, static default_instance_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x27a6240&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再然后，我们想看看new_sub_cate_这个变量：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr.video_info&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;.videoinfo_pb.new_sub_cate_
&lt;span class=&quot;nv&quot;&gt;$35&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;std::string &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 0x4e6533210
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再把它的值打出来：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr.video_info&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;.videoinfo_pb.new_sub_cate_&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$36&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static npos &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &amp;lt;optimized out&amp;gt;, _M_dataplus &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e64189d8 &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\3&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;51&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\3&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\3&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\3&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;07&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+4&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr.video_info&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;.videoinfo_pb.new_sub_cate_&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_dataplus._M_p
&lt;span class=&quot;nv&quot;&gt;$39&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x4e64189d8 &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\3&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;51&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\3&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\3&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\3&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;47&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\2&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;07&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;!!!大boss来了！！&lt;/strong&gt;，我们来学习一下下什么叫高科技：(参考&lt;a href=&quot;https://www.zhihu.com/question/26902926&quot;&gt;https://www.zhihu.com/question/26902926&lt;/a&gt;)&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#encoding=utf8
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;urllib&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ChangeCoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;''' 处理中文文件名的编码 '''&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#s='&quot;\\346\\226\\260\\345\\273\\272\\346\\226\\207\\344\\273\\266\\345\\244\\271/\\345\\226\\260\\345\\273\\272\\346\\226\\207\\344\\273\\266\\345\\244\\271/\\346\\226\\260\\345\\273\\272\\346\\226\\207\\346\\234\\254\\346\\226\\207\\346\\241\\243.txt&quot;'
#pattern=re.compile(r'^&quot;.*?((\\\d\d\d){3,})(/(?P&amp;lt;s&amp;gt;(\\\d\d\d){3,}))*.+&quot;$')
#match=pattern.match(a)
&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'(?P&amp;lt;s&amp;gt;(\\\d\d\d){3,})'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;finditer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;old&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;old&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'%x'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isdigit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'%'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'%'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;CN_name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urllib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unquote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'utf-8'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;old&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CN_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&quot;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\347\251\272\345\247\220\350\201\224\347\233\237&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ChangeCoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以发现结果：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python /tmp/x.py    
风尚大片
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看看另一个core，其实类似：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr.video_info&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;.videoinfo_pb.rid_
&lt;span class=&quot;nv&quot;&gt;$5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6842212632
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr
&lt;span class=&quot;nv&quot;&gt;$8&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;rid &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4279846197472829173, source_type &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, cupai_score &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0.520694852, real_score &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, res_score &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 43, mark &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, category &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, slotid &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, 
  predict_result &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static npos &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &amp;lt;optimized out&amp;gt;, 
    _M_dataplus &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, 
      _M_p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7fbd7b5823f8 &amp;lt;std::string::_Rep::_S_empty_rep_storage+24&amp;gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, predictor_extmsg &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static npos &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &amp;lt;optimized out&amp;gt;, 
    _M_dataplus &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, 
      _M_p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197e24c18 &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\f\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;63&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;02&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;63:&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, news_info &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0, video_info &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x2cfb188, click &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, show &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, ext_msg_v2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    static npos &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &amp;lt;optimized out&amp;gt;, _M_dataplus &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;char&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, 
      _M_p &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x198255b78 &lt;span class=&quot;s2&quot;&gt;&quot;EhQZAAAAQIip4D8gODEAAABAiKngPxoIEgI0MxoCNDM=&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;发现rid外层是对的，但在内层的videoinfo_pb里却是错的，说明在给videoinfo_pb赋值的地方有问题，进一步地，我们可以看看中层的video_info字段，发现这里的rid也是错的！！发现给这个video_info赋值的代码包在一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#pragma omp parallel for&lt;/code&gt;里，尝试把这个注释掉…&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; p &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;result._M_impl._M_start+3&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;._M_ptr.video_info&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;rid &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6842219464, tag_w &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__allow_copy_cons&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_h &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hash_code_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::__detail::_Select1st, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::__detail::_Select1st, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Select1st&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;1, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__hash_base&amp;lt;unsigned long, std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;2, std::__detail::_Mod_range_hashing, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Mod_range_hashing&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::binary_function&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, bool&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Map_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Insert&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Insert_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Rehash_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Equality&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_buckets &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197d3fbd0, _M_bucket_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6842219480, _M_bbegin &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_node &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197e6fe80&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, _M_element_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 47244640257, _M_rehash_policy &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static _S_growth_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, _M_max_load_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1.40129846e-45, _M_next_resize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6844600064&lt;span class=&quot;o&quot;&gt;}}}&lt;/span&gt;, manual_tags &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__allow_copy_cons&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_h &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hash_code_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::__detail::_Select1st, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::__detail::_Select1st, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Select1st&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;1, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__hash_base&amp;lt;unsigned long, std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;2, std::__detail::_Mod_range_hashing, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Mod_range_hashing&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Hashtable_ebo_helper&amp;lt;0, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::binary_function&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, bool&amp;gt;&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Map_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Insert&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::__detail::_Insert_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Rehash_base&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;std::__detail::_Equality&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, std::allocator&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Select1st, std::equal_to&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::hash&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits&amp;lt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_buckets &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0, _M_bucket_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6844599824, _M_bbegin &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;std::allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;__gnu_cxx::new_allocator&amp;lt;std::__detail::_Hash_node&amp;lt;std::pair&amp;lt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; const, float&amp;gt;, &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, _M_node &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_M_nxt &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, _M_element_count &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _M_rehash_policy &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static _S_growth_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, _M_max_load_factor &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1.65121836e-37, _M_next_resize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0&lt;span class=&quot;o&quot;&gt;}}}&lt;/span&gt;, videoinfo_pb &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::Message&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::MessageLite&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;_vptr.MessageLite &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x10ae850 &amp;lt;vtable &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;google::protobuf::MessageLite+16&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, static kRidFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, static kTitleSignFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 2, static kContentSignFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 3, static kQualityFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, static kGenreFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 5, static kTagsFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6, static kPublicTimeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 7, static kTabFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 8, static kManualTagsFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 9, static kNewCateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 10, static kNewSubCateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 11, static kCheckAttributeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 12, static kVideoTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 13, static kLongVideoTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 14, static kIsCooperateFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 15, static kIsNatureFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 16, static kCheckpropertyEntityFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 17, static kCheckpropertyPeopleFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 18, static kCheckpropertySceneFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 19, static kDomainFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 20, static kLiveTypeFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 21, static kUploaderFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 22, static kIdlCate1FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 23, static kIdlCate2FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 24, static kMthidFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 25, static kCategoryFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 26, static kBigImgFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 27, static kDelTagFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 28, static kIsMicrovideoFieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 29, static kAuthorAuthorityScoreV1FieldNumber &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 30, _unknown_fields_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;fields_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197d3e120&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, rid_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6842212632, title_sign_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6842212640, content_sign_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6842212648, quality_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;.49276465e-24, genre_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, tags_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0xb00000002, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1745334528&lt;/span&gt;, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x197f84e10, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, public_time_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 39895184, tab_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x426f4300, manual_tags_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x2cfb280, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1747721928&lt;/span&gt;, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1747721936&lt;/span&gt;, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x197d3e138, 0x197d3e140, 0x197e6fe80, 0xb00000003&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, new_cate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1, new_sub_cate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197f84f00, check_attribute_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1745334768&lt;/span&gt;, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x260c090, 0x0, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, video_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197d3e150, long_video_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197d3e148, is_cooperate_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 6842212688, checkproperty_entity_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197d3e158, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1746469248&lt;/span&gt;, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x1, 0x197f84f00, 0x0, 0x197f84e10&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, checkproperty_people_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 39895184, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0x0, 0x0, 0x197d3e168, 0x197d3e160&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, checkproperty_scene_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&amp;lt;google::protobuf::internal::RepeatedPtrFieldBase&amp;gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;static kInitialSize &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4, elements_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x197d3e168, current_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1747721872&lt;/span&gt;, allocated_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, total_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1746469248&lt;/span&gt;, initial_space_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;0xb00000005, 0x3, 0x197f84f00, 0x0&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;, &amp;lt;No data fields&amp;gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, is_nature_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-1745334648&lt;/span&gt;, idl_cate1_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1, domain_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0, live_type_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x0, uploader_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x260c090, mthid_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x426f4300, category_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, idl_cate2_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 4279, del_tag_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, big_img_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x1de8908 &amp;lt;google::protobuf::internal::kEmptyString&amp;gt;, is_microvideo_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, author_authority_score_v1_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _cached_size_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0, _has_bits_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;167311071&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;, static default_instance_ &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x27a6240&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然而问题并不在这里。。。后来仔细review代码发现：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func_aaa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TmpResultBuffer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;TmpResultBuffer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mid_tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mid_tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func_xxx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rid_sim_vec_trunc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mid_tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;uint64_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adress&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mid_tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_doc_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RecVideoInfo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;videoinfo_ptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RecVideoInfo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;RidTmpInfoPtr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rid_tmp_info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mid_tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_res_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rid_tmp_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;video_info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;videoinfo_ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;content_v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rid_tmp_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;//...&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_result_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;video_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tmp_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;video_info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp_res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_result_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也就是说，临时变量&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mid_tmp_res&lt;/code&gt;里的一个地址&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adress&lt;/code&gt;被赋值给了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rid_tmp_info&lt;/code&gt;这个变量的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;video_info&lt;/code&gt;这个成员！！但&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mid_tmp_res&lt;/code&gt;这个变量在函数执行完后就被回收了，所以后面还想要用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tmp_res&lt;/code&gt;里的成员变量的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;video_info&lt;/code&gt;的时候，会发现这个地址取出来的值是不对的！！。。。解决方法呢，就是把这个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mid_tmp_res&lt;/code&gt;的生命周期变得和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tmp_res&lt;/code&gt;一样长了，看框架的设计机制咯。。&lt;/p&gt;

&lt;p&gt;最最简单的方法，不要&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mid_tmp_res&lt;/code&gt;这个变量了，反正和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tmp_res&lt;/code&gt;是同一个类型的，直接把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tmp_res&lt;/code&gt;传给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func_xxx&lt;/code&gt;就行了咯。。。&lt;/p&gt;

&lt;h2 id=&quot;基础命令&quot;&gt;基础命令&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;ulimit&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt;
core file size          &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;blocks, &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; unlimited
data seg size           &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;kbytes, &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; unlimited
file size               &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;blocks, &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; unlimited
pending signals                 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1031511
max locked memory       &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;kbytes, &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 64
max memory size         &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;kbytes, &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; unlimited
open files                      &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 10240
pipe size            &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;512 bytes, &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 8
POSIX message queues     &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;bytes, &lt;span class=&quot;nt&quot;&gt;-q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 819200
stack size              &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;kbytes, &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 10240
cpu &lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt;               &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;seconds, &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; unlimited
max user processes              &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 1031511
virtual memory          &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;kbytes, &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; unlimited
file locks                      &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; unlimited
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可见，stack size即栈空间的大小是10240KB，也就是10MB。可用ulimit -s可以只看栈空间大小。&lt;/p&gt;

&lt;h2 id=&quot;堆与栈&quot;&gt;堆与栈&lt;/h2&gt;

&lt;h2 id=&quot;core&quot;&gt;core&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/caspiansea/article/details/24450377&quot;&gt;https://blog.csdn.net/caspiansea/article/details/24450377&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;已经core了的文件好像查不了mappings，用gdb启动的可以看：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;info proc  mappings
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;前面会有：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;gdb&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; info proc  mappings   
process 544  
Mapped address spaces:  
  
    Start Addr   End Addr       Size     Offset objfile  
        0x8000     0x9000     0x1000        0x0 /mnt/test_class  
       0x10000    0x11000     0x1000        0x0 /mnt/test_class  
       0x11000    0x32000    0x21000        0x0 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;heap]  
    0xb6d39000 0xb6e64000   0x12b000        0x0 /lib/libc-2.19.so  
    0xb6e64000 0xb6e6c000     0x8000   0x12b000 /lib/libc-2.19.so  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;说明0x11000-0x32000这总共0x21000的大小是堆空间&lt;/p&gt;

&lt;p&gt;最后面会有：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;      0x7ffff7ffb000     0x7ffff7ffc000     0x1000        0x0 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;vdso]
      0x7ffff7ffc000     0x7ffff7ffd000     0x1000    0x20000 /home/opt/gcc-4.8.2.bpkg-r4/gcc-4.8.2.bpkg-r4/lib64/ld-2.18.so
      0x7ffff7ffd000     0x7ffff7ffe000     0x1000    0x21000 /home/opt/gcc-4.8.2.bpkg-r4/gcc-4.8.2.bpkg-r4/lib64/ld-2.18.so
      0x7ffff7ffe000     0x7ffff7fff000     0x1000        0x0 
      0x7ffffff73000     0x7ffffffff000    0x8c000        0x0 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;stack]
  0xffffffffff600000 0xffffffffff601000     0x1000        0x0 &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;vsyscall]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;说明0x7ffffff73000-0x7ffffffff000这总共0x8c000=789999=789k=0.8MB的大小是栈空间？？好像不太对呢。。。&lt;/p&gt;

&lt;p&gt;查看当前frame：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;info frame
Stack level 1, frame at 0x7f7ed3284310:
 rip &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f7ed6da1f50 &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;nerl::NerlPlus::tagging &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;baidu/xxxx/src/dddd.cpp:599&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; saved rip &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x7f7ed6d9964e
 called by frame at 0x7f7ed3284360, &lt;span class=&quot;nb&quot;&gt;caller &lt;/span&gt;of frame at 0x7f7ed32841e0
 &lt;span class=&quot;nb&quot;&gt;source &lt;/span&gt;language c++.
 Arglist at 0x7f7ed32841d8, args: &lt;span class=&quot;nv&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x2fd6950, &lt;span class=&quot;nv&quot;&gt;iTokens&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x7f7caae7f010, &lt;span class=&quot;nv&quot;&gt;iTokensCount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1, &lt;span class=&quot;nv&quot;&gt;iNerlBuff&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x7f7c9f706710, &lt;span class=&quot;nv&quot;&gt;tmpTags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;..., &lt;span class=&quot;nv&quot;&gt;oTags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;..., 
    &lt;span class=&quot;nv&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;nerl::DEFAULT_FLAGS
 Locals at 0x7f7ed32841d8, Previous frame&lt;span class=&quot;s1&quot;&gt;'s sp is 0x7f7ed3284310
 Saved registers:
  rbx at 0x7f7ed32842d8, rbp at 0x7f7ed32842e0, r12 at 0x7f7ed32842e8, r13 at 0x7f7ed32842f0, r14 at 0x7f7ed32842f8, r15 at 0x7f7ed3284300,
  rip at 0x7f7ed3284308
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;堆栈导致的core&quot;&gt;堆、栈导致的core&lt;/h2&gt;

&lt;h3 id=&quot;栈空间不足&quot;&gt;栈空间不足&lt;/h3&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://blog.csdn.net/u011866460/article/details/42525171&quot;&gt;https://blog.csdn.net/u011866460/article/details/42525171&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;例如，程序中有两个大小为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2048*2048\)&lt;/code&gt;的char数组，算下来，一个char是一个字节，两个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2048*2048\)&lt;/code&gt;的数组便是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(2*2048*2048=8388608=8*1024*1024=8MB\)&lt;/code&gt;的空间。所以，如果这个时候还有别的栈上的变量，而栈空间如果 只有8MB，那么，就会core!!!&lt;/p&gt;

&lt;p&gt;linux限制了栈空间大小，自己定义的变量都是在栈空间上分配的，子函数在调用时才会装入栈中，当定义的变量过大则会超出栈空间，从而段错误。所以，尽可能使用堆空间，比如用new malloc vector等&lt;/p&gt;

&lt;h2 id=&quot;其他容易core的点&quot;&gt;其他容易core的点&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;类定义时会给这个指针默认分配一个地址，不是0=NULL=nullptr，如果没有new一个这个类型的，就直接用，会core&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外，了解一下nullptr和NULL等的区别：&lt;a href=&quot;https://www.cnblogs.com/DswCnblog/p/5629073.html&quot;&gt;https://www.cnblogs.com/DswCnblog/p/5629073.html&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;is 0&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//&amp;lt;&amp;lt; nullptr &amp;lt;&amp;lt; xx.a &amp;lt;&amp;lt; endl;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;is NULL&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//&amp;lt;&amp;lt; nullptr &amp;lt;&amp;lt; xx.a &amp;lt;&amp;lt; endl;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;nullptr==NULL&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//&amp;lt;&amp;lt; nullptr &amp;lt;&amp;lt; xx.a &amp;lt;&amp;lt; endl;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;nullptr==0&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//&amp;lt;&amp;lt; nullptr &amp;lt;&amp;lt; xx.a &amp;lt;&amp;lt; endl;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;before not nullptr&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//&amp;lt;&amp;lt; nullptr &amp;lt;&amp;lt; xx.a &amp;lt;&amp;lt; endl;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 这里ok&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;444&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 这里会core..&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;after not nullptr&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//&amp;lt;&amp;lt; nullptr &amp;lt;&amp;lt; xx.a &amp;lt;&amp;lt; endl;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;nullptr&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//&amp;lt;&amp;lt; nullptr &amp;lt;&amp;lt; xx.a &amp;lt;&amp;lt; endl;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;其他的一些操作&quot;&gt;其他的一些操作&lt;/h2&gt;

&lt;h3 id=&quot;把so里的地址转换成行号&quot;&gt;把so里的地址转换成行号&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://stackoverflow.com/questions/8545931/using-gdb-to-convert-addresses-to-lines&quot;&gt;https://stackoverflow.com/questions/8545931/using-gdb-to-convert-addresses-to-lines&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://stackoverflow.com/questions/7648642/how-to-use-the-addr2line-command-in-linux/7648883#7648883&quot;&gt;https://stackoverflow.com/questions/7648642/how-to-use-the-addr2line-command-in-linux/7648883#7648883&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://stackoverflow.com/questions/49150331/given-program-counter-find-the-source-line-in-a-shared-library&quot;&gt;https://stackoverflow.com/questions/49150331/given-program-counter-find-the-source-line-in-a-shared-library&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;用gdb查py的core&quot;&gt;用gdb查py的core&lt;/h3&gt;

&lt;p&gt;下载&lt;a href=&quot;https://svn.python.org/projects/python/trunk/Tools/gdb/libpython.py&quot;&gt;https://svn.python.org/projects/python/trunk/Tools/gdb/libpython.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/dkblog/p/3806277.html&quot;&gt;https://www.cnblogs.com/dkblog/p/3806277.html&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>sphinx</title>
   <link href="http://daiwk.github.io/posts/knowledge-sphinx.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-sphinx</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;以&lt;a href=&quot;https://github.com/daiwk/demo-code&quot;&gt;https://github.com/daiwk/demo-code&lt;/a&gt;为例，讲解一下sphinx的使用咯~&lt;/p&gt;

&lt;p&gt;目录结构如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.
├── docs
│   ├── Makefile
│   ├── build
│   ├── gen_doc.sh
│   ├── make.bat
│   └── source
│       ├── _static
│       ├── _templates
│       ├── conf.py
│       ├── demo.models.rst
│       ├── demo.rst
│       ├── demo.tools.rst
│       ├── index.rst
│       ├── introduction.rst
│       └── modules.rst
├── python
│   └── demo
│       ├── __init__.py
│       ├── models
│       │   ├── __init__.py
│       │   └── demo1.py
│       └── tools
│           ├── __init__.py
│           └── demo2.py
└── requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requirements.txt&lt;/code&gt;写了需要的依赖包，例如tensroflow。然后在demo1.py和demo2.py中，我们使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'''xxx'''&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;&quot;xxx&quot;&quot;&quot;&lt;/code&gt;这样的注释，让sphinx可以自动捕捉到。&lt;/p&gt;

&lt;p&gt;而docs目录下，是通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sphinx-quickstart&lt;/code&gt;生成的，我们直接copy就行了。&lt;/p&gt;

&lt;p&gt;然后在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docs/source/conf.py&lt;/code&gt;中加上：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;abspath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'./../../python/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后使用gen_doc.sh:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;docs
sh &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt; gen_doc.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以看到这个shell里有下面一句命令：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sphinx-apidoc &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; ./source ../python &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docs/source/index.srt&lt;/code&gt;中记得加上下面这段，表示要显示modules.rst这个文件&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.. toctree::
   :maxdepth: 1
   :caption: API docs

   modules
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;首先，用github登录&lt;a href=&quot;https://readthedocs.org/&quot;&gt;https://readthedocs.org/&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;然后import一个project，&lt;/p&gt;

&lt;p&gt;然后『管理』-&amp;gt;『高级设置』&lt;/p&gt;

&lt;p&gt;在『所需求的文件:』中把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requirements.txt&lt;/code&gt;填进去&lt;/p&gt;

&lt;p&gt;在『Python 配置文件:』中把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docs/source/conf.py&lt;/code&gt;填进去&lt;/p&gt;

&lt;p&gt;然后只要一提交，就会自动触发构建啦&lt;/p&gt;

&lt;p&gt;然后去对应的网址看看，例如我的这个叫&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://demo-code.readthedocs.io/zh_CN/latest/&lt;/code&gt;，当然，你应该取另一个名字，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;https://yyy-demo-code.readthedocs.io/zh_CN/latest/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果只想暴露某些函数/类（注意：只能链接到类Demo1，而没法到Demo1的get函数），当然，也可以直接在py文件里声明一个函数（不属于某个类），这样就可以暴露这个函数啦：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.. autofunction:: demo.models.demo1.Demo1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;另外，类的private函数（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__xxx&lt;/code&gt;开头的函数）以及类函数第一个变量self是不会生成文档的&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>rnn实现</title>
   <link href="http://daiwk.github.io/posts/knowledge-rnn-implementation.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-rnn-implementation</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#pytorch%E7%9A%84rnn&quot;&gt;pytorch的rnn&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tf%E7%9A%84rnn&quot;&gt;tf的rnn&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#paddle%E7%9A%84rnn&quot;&gt;paddle的rnn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;pytorch的rnn&quot;&gt;pytorch的rnn&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.ddlee.cn/posts/7b4533bb/&quot;&gt;https://blog.ddlee.cn/posts/7b4533bb/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python2.7/site-packages/torch/nn/_functions/rnn.py&lt;/code&gt;中。&lt;/p&gt;

&lt;h2 id=&quot;tf的rnn&quot;&gt;tf的rnn&lt;/h2&gt;

&lt;h2 id=&quot;paddle的rnn&quot;&gt;paddle的rnn&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>pytorch安装</title>
   <link href="http://daiwk.github.io/posts/knowledge-pytorch-install.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-pytorch-install</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C&quot;&gt;准备工作&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E4%BD%BF%E7%94%A8conda&quot;&gt;使用conda&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E9%9D%9Econda&quot;&gt;非conda&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%8E%82%E5%86%85%E5%AE%89%E8%A3%85&quot;&gt;厂内安装&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/pytorch/pytorch#from-source&quot;&gt;https://github.com/pytorch/pytorch#from-source&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;准备工作&quot;&gt;准备工作&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone &lt;span class=&quot;nt&quot;&gt;--recursive&lt;/span&gt; https://github.com/pytorch/pytorch
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;pytorch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;首先需要3.5以上的cmake：&lt;a href=&quot;https://cmake.org/download/&quot;&gt;https://cmake.org/download/&lt;/a&gt;，例如&lt;a href=&quot;https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz&quot;&gt;https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./bootstrap &lt;span class=&quot;nt&quot;&gt;--prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/home/work/xxx
make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用conda&quot;&gt;使用conda&lt;/h2&gt;

&lt;p&gt;首先需要&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;conda &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;numpy pyyaml mkl mkl-include setuptools cmake cffi typing
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在linux上的话：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Add LAPACK support for the GPU if needed&lt;/span&gt;
conda &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; pytorch magma-cuda90 &lt;span class=&quot;c&quot;&gt;# or [magma-cuda80 | magma-cuda92 | magma-cuda100 ] depending on your cuda version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后安装&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;CMAKE_PREFIX_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;CONDA_PREFIX&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:-&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dirname&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;which conda&lt;span class=&quot;si&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/../&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
python setup.py &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;非conda&quot;&gt;非conda&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;../python-2.7.14-tf-2.0/bin/python ../python-2.7.14-tf-2.0/bin/pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;pyyaml mkl mkl-include setuptools cmake cffi typing
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;厂内安装&quot;&gt;厂内安装&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./xxx/bin/python ./xxx/bin/pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;torch
/opt/compiler/gcc-4.8.2/lib/ld-linux-x86-64.so.2 &lt;span class=&quot;nt&quot;&gt;--library-path&lt;/span&gt; /opt/compiler/gcc-4.8.2/lib ./xxx/bin/python &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;import torch&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>python小技巧</title>
   <link href="http://daiwk.github.io/posts/knowledge-python.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-python</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%bc%96%e8%af%91%e5%ae%89%e8%a3%85python&quot;&gt;编译安装python&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#jupyter&quot;&gt;jupyter&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#mkdocs&quot;&gt;mkdocs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#collections&quot;&gt;collections&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%bf%9b%e7%a8%8b%e6%b1%a0multiprocessingpool&quot;&gt;进程池multiprocessing.Pool&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e7%ba%bf%e7%a8%8b%e6%b1%a0threadpool&quot;&gt;线程池threadpool&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#mac%e7%89%88python3%e7%9a%84tf&quot;&gt;mac版python3的tf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#copy-deepcopy&quot;&gt;copy deepcopy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gc&quot;&gt;gc&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%bc%95%e7%94%a8%e8%ae%a1%e6%95%b0%e4%b8%bb%e8%a6%81&quot;&gt;引用计数（主要）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e6%a0%87%e8%ae%b0%e6%b8%85%e9%99%a4&quot;&gt;标记清除&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%88%86%e4%bb%a3%e5%9b%9e%e6%94%b6&quot;&gt;分代回收&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#collections-1&quot;&gt;collections&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#ordereddict&quot;&gt;OrderedDict&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#conda&quot;&gt;conda&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#flush&quot;&gt;flush&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e6%8a%80%e5%b7%a7&quot;&gt;技巧&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#kwargs%e4%b8%8eargs&quot;&gt;kwargs与args&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#%e5%ae%9a%e4%b9%89%e6%97%b6%e4%bd%bf%e7%94%a8&quot;&gt;定义时使用&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#%e8%b0%83%e7%94%a8%e6%97%b6%e4%bd%bf%e7%94%a8&quot;&gt;调用时使用&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#heartrate&quot;&gt;Heartrate&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%b6%e4%bb%96%e6%9c%89%e7%94%a8%e7%9a%84%e5%ba%93&quot;&gt;其他有用的库&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#selenium&quot;&gt;Selenium&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;编译安装python&quot;&gt;编译安装python&lt;/h2&gt;

&lt;p&gt;python总体上有两个版本，cp27m是ucs2，cp27&lt;strong&gt;mu&lt;/strong&gt;是&lt;strong&gt;ucs4&lt;/strong&gt;，UCS2认为每个字符占用2个字节，UCS4认为每个字节占用4个字符，都是UNICODE的编码形式。&lt;/p&gt;

&lt;p&gt;一般基于python的深度学习框架大多默认提供ucs4版本（当然也有ucs2版本），为了以后使用方便，下面我们会用gcc482编译生成python-ucs4版本&lt;/p&gt;

&lt;p&gt;1、官网下载python release， https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz&lt;/p&gt;

&lt;p&gt;2、解压缩到 Python-2.7.14，cd Python-2.7.14&lt;/p&gt;

&lt;p&gt;3、编译：&lt;/p&gt;

&lt;p&gt;首先，手动安装tk&lt;/p&gt;

&lt;p&gt;如果是64位系统，可能需要修改&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;LIB_RUNTIME_DIR &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;libdir&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;:/usr/X11R6/lib64
X11_LIB_SWITCHES &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;XFT_LIBS&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt;/usr/X11R6/lib64 &lt;span class=&quot;nt&quot;&gt;-lX11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在python解压出来的路径里&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./configure &lt;span class=&quot;nt&quot;&gt;--enable-unicode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;ucs4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果你要自己装sqlite3而不是用jumbo的，那也可以参考&lt;a href=&quot;https://blog.csdn.net/sparkexpert/article/details/79118448&quot;&gt;https://blog.csdn.net/sparkexpert/article/details/79118448&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;如果要sqlite3，可能要加上&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nt&quot;&gt;-enable-loadable-sqlite-extensions&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;打开Makefile，&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#修改36行为&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;CC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/opt/compiler/gcc-4.8.2/bin/gcc &lt;span class=&quot;nt&quot;&gt;-pthread&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#修改37行为&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;CXX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/opt/compiler/gcc-4.8.2/bin/g++ &lt;span class=&quot;nt&quot;&gt;-pthread&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#修改101行(prefix=)为想要编译生成python的位置，例如 &lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;     /home/work/xxx/exp_space/python-2.7.14

&lt;span class=&quot;c&quot;&gt;## 如果要安装支持tk的（matplot.pyplot需要！！）&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;## 那么就修改以下两个地方&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;TCLTK_INCLUDES&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt;/home/work/.jumbo/include/ &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt;/usr/X11R6/include/
&lt;span class=&quot;nv&quot;&gt;TCLTK_LIBS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt;/home/work/.jumbo/lib/ &lt;span class=&quot;nt&quot;&gt;-ltk8&lt;/span&gt;.6 &lt;span class=&quot;nt&quot;&gt;-ltcl8&lt;/span&gt;.6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后还需要！！！！&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;LD_LIBRARY_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/home/work/.jumbo/lib/:&lt;span class=&quot;nv&quot;&gt;$LD_LIBRARY_PATH&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果需要支持&lt;strong&gt;sqlite3&lt;/strong&gt;，那得先装一下，然后修改当前目录的setup.py：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;        sqlite_inc_paths &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/usr/include'&lt;/span&gt;,
                             &lt;span class=&quot;s1&quot;&gt;'/usr/include/sqlite'&lt;/span&gt;,
                             &lt;span class=&quot;s1&quot;&gt;'/usr/include/sqlite3'&lt;/span&gt;,
                             &lt;span class=&quot;s1&quot;&gt;'/usr/local/include'&lt;/span&gt;,
                             &lt;span class=&quot;s1&quot;&gt;'/usr/local/include/sqlite'&lt;/span&gt;,
                             &lt;span class=&quot;s1&quot;&gt;'/usr/local/include/sqlite3'&lt;/span&gt;,

                           &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后记得把tk的so拷过来，例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ~/.jumbo/lib/libtcl8.6.so /home/disk2/daiwenkai/tools/python-2.7.14-tk/lib/python2.7/lib-dynload/
&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ~/.jumbo/lib/libtk8.6.so /home/disk2/daiwenkai/tools/python-2.7.14-tk/lib/python2.7/lib-dynload/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;编完之后，发现可能编出来的_sqlite3.so是不能用的，，这个时候就需要这样了：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; /home/work/.jumbo/lib/python2.7/lib-dynload/_sqlite3.so ./python-2.7.14-latest/lib/python2.7/lib-dynload/_sqlite3.so
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果是py3，就&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cp&lt;/span&gt; ~/.jumbo/lib/libsqlite3.&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; ./python-3.6.9/lib/python3.6/lib-dynload/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;4、安装pip&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl https://bootstrap.pypa.io/get-pip.py &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; get-pip.py
python get-pip.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;setuppy安装时指定ld_library_path&quot;&gt;setup.py安装时指定LD_LIBRARY_PATH&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/opt/compiler/gcc-gcc-4.8.2/bin/:&lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;LDFLAGS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt;/home/work/daiwenkai/paddle_space/app_cold/python/lib/ ../python/bin/python ./setup.py &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;jupyter&quot;&gt;jupyter&lt;/h2&gt;

&lt;p&gt;小技巧：如何把整个notebook里的各级目录的东西一起下载？(我们发现在界面里一次只能下载一个文件。。。)&lt;/p&gt;

&lt;p&gt;打开一个python窗口，然后输入(google搜出来的啦。。)&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-czhvf&lt;/span&gt; notebook.tar.gz &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中的-h参数，是把软链对应的真实文件搞过来哈~。。&lt;/p&gt;

&lt;p&gt;10个小技巧：&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650766829&amp;amp;idx=1&amp;amp;sn=18d429c632972d9fa52a16516b2cc1df&amp;amp;chksm=871ab993b06d3085b8f49d214deb257fa629994c7cb139d266df3def31831a2c56d8c0103b6b&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=I7vMVoY36Vu5%2FFz%2FMUDKXgy%2FHocjPiCFYYtVANqq1m0CCQBpIAQhSU5BGMcu7Il0#rd&quot;&gt;十大至简规则，用Jupyter Notebook写代码应该这样来&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;mkdocs&quot;&gt;mkdocs&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;mkdocs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考：&lt;a href=&quot;https://www.mkdocs.org/&quot;&gt;https://www.mkdocs.org/&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;collections&quot;&gt;collections&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/17xwTlwJi1ckht3wGk5ttA&quot;&gt;四种高性能数据类型，Python collections助你优化代码、简洁任务&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;进程池multiprocessingpool&quot;&gt;进程池multiprocessing.Pool&lt;/h2&gt;

&lt;p&gt;官方文档：&lt;a href=&quot;https://docs.python.org/2.7/library/multiprocessing.html&quot;&gt;https://docs.python.org/2.7/library/multiprocessing.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/gengyi/p/8620853.html&quot;&gt;https://www.cnblogs.com/gengyi/p/8620853.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;demo代码&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;multiprocessing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mp&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Pool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 创建5条进程
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'hello-%d'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;apply_async&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 向进程池添加任务
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 关闭进程池，不再接受请求
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 等待所有的子进程结束
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;线程池threadpool&quot;&gt;线程池threadpool&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;http://www.cnblogs.com/Eva-J/p/5106564.html&quot;&gt;http://www.cnblogs.com/Eva-J/p/5106564.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在初始化中，它会根据我们的需求，启动相应数量的线程，这些线程是初始化好的，一直到程序结束，不会停止。&lt;/p&gt;

&lt;p&gt;它们从任务队列中获取任务，在没有任务的时候就阻塞，&lt;/p&gt;

&lt;p&gt;他们当我们有任务的时候，对任务进行初始化，放入任务队列，&lt;/p&gt;

&lt;p&gt;拿到任务的线程结束了自己的阻塞人生，欢欢喜喜的拿回去执行，&lt;/p&gt;

&lt;p&gt;并在执行完毕之后，将结果放入结果队列，继续到任务队列中取任务，如果没有任务就进入阻塞状态。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/py-threadpool.png&quot; style=&quot;max-height: 300px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;demo代码&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;threadpool&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;threadpool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ThreadPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;poll_timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;dic&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;threadpool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;makeRequests&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;putRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;req&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果遇到『most likely raised during interpreter』问题，可以参考&lt;a href=&quot;https://blog.csdn.net/daijiguo/article/details/79488548&quot;&gt;https://blog.csdn.net/daijiguo/article/details/79488548&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;写成：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;pool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;threadpool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ThreadPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;poll_timeout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;mac版python3的tf&quot;&gt;mac版python3的tf&lt;/h2&gt;

&lt;p&gt;在&lt;a href=&quot;https://pypi.org/project/tensorflow/1.11.0/#files&quot;&gt;https://pypi.org/project/tensorflow/1.11.0/#files&lt;/a&gt;找到tensorflow-1.8.0-cp36-cp36m-macosx_10_11_x86_64.whl这个版本的，下下来(注，如果macosx的版本是10.11以下，好像最高只能装1.8的tf，macosx版本高的话，再试试咯~)&lt;/p&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; pip3 &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; ./tensorflow-1.8.0-cp36-cp36m-macosx_10_11_x86_64.whl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;copy-deepcopy&quot;&gt;copy deepcopy&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://iaman.actor/blog/2016/04/17/copy-in-python&quot;&gt;https://iaman.actor/blog/2016/04/17/copy-in-python&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python中的对象之间赋值(=运算)时是按引用传递的，如果需要拷贝对象，需要使用标准库中的copy模块。&lt;/p&gt;

&lt;p&gt;copy.copy 浅拷贝 vs copy.deepcopy 深拷贝：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对于简单的 object，用 shallow copy 和 deep copy 没区别&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;copy&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;c1&quot;&gt;#cop1 是 origin 的shallow copy
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deepcopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;c1&quot;&gt;#cop2 是 origin 的 deep copy
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop1&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop2&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#cop1 和 cop2 都不会随着 origin 改变自己的值
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop2&lt;/span&gt;
&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop2&lt;/span&gt;
&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;复杂的object， 如list中套着list的情况，&lt;strong&gt;shallow copy中的子list，并未从原object真的「独立」出来。&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;copy&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#origin 里边有三个元素：1， 2，[3, 4]
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deepcopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop2&lt;/span&gt;
&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop2&lt;/span&gt;
&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; 
&lt;span class=&quot;c1&quot;&gt;#cop1 和 cop2 看上去相同，但已不再是同一个object
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hey!&quot;&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;origin&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'hey!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop1&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'hey!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cop2&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#把origin内的子list [3, 4] 改掉了一个元素，观察 cop1 和 cop2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;浅copy容易遇到的『坑』：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 把 [1, 2, 3] 看成一个物品。a = [1, 2, 3] 就相当于给这个物品上贴上 a 这个标签。而 b = a 就是给这个物品又贴上了一个 b 的标签。
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 赋新的值给 a
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# a 的值改变后，b 并没有随着 a 变。
# a = [4, 5, 6] 就相当于把 a 标签从 [1 ,2, 3] 上撕下来，贴到了 [4, 5, 6] 上。
# 在这个过程中，[1, 2, 3] 这个物品并没有消失。 b 自始至终都好好的贴在 [1, 2, 3] 上，既然这个 reference 也没有改变过。 b 的值自然不变。
&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 改变原来 list 中的元素
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# a 的值改变后，b 随着 a 变了
# a[0], a[1], a[2] = 4, 5, 6 则是直接改变了 [1, 2, 3] 这个物品本身。把它内部的每一部分都重新改装了一下。内部改装完毕后，[1, 2, 3] 本身变成了 [4, 5, 6]。
# 而在此过程当中，a 和 b 都没有动，他们还贴在那个物品上。因此自然 a b 的值都变成了 [4, 5, 6]。
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;gc&quot;&gt;gc&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://juejin.im/post/5b34b117f265da59a50b2fbe&quot;&gt;https://juejin.im/post/5b34b117f265da59a50b2fbe&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;引用计数主要&quot;&gt;引用计数（主要）&lt;/h3&gt;

&lt;p&gt;在Python中，每一个对象的核心就是一个结构体PyObject，它的内部有一个引用计数器（ob_refcnt）&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct_object&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ob_refcnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;struct_typeobject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ob_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PyObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;引用计数的意思就是，一个对象在它刚被New出来的时候因为被New方法引用了所以他的引用计数就是1，如果它被引用（也就是在之前的基础上 例如：b=a，被丢入函数列表等等被引用就会在引用计数上加1），如果引用它的对象被删除的时候（在之前的基础上DEL b）那么它的引用计数就会减少一一直到当它的引用计数变为0的时候，垃圾回收机制就会回收。&lt;/p&gt;

&lt;p&gt;优点就是简单快速，缺点是维护成本稍微有点高（多了个refcnt要维护），无法解决循环引用的情况：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEL&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;DEL&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;标记清除&quot;&gt;标记清除&lt;/h3&gt;

&lt;p&gt;标记清除就是用来解决循环引用的问题的只有容器对象才会出现引用循环，比如列表、字典、类、元组。
首先，为了追踪容器对象，需要每个容器对象维护两个额外的指针，
用来将容器对象组成一个链表，指针分别指向前后两个容器对象，方便插入和删除操作。试想一下，现在有两种情况：&lt;/p&gt;

&lt;p&gt;case1:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;case2:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;有两块，一个是root链表(root object)，另外一个是unreachable链表。&lt;/p&gt;

&lt;p&gt;对于case1，原来再未执行DEL语句的时候，a,b的引用计数都为2（init+append=2），但是在DEL执行完以后，a,b引用次数互相减1。a,b陷入循环引用的圈子中，然后标记-清除算法开始出来做事，找到其中一端a,开始拆这个a,b的引用环（我们从A出发，因为它有一个对B的引用，则将B的引用计数减1；然后顺着引用达到B，因为B有一个对A的引用，同样将A的引用减1，这样，就完成了循环引用对象间环摘除。），去掉以后发现，a,b循环引用变为了0，所以a,b就被处理到unreachable链表中直接被做掉。&lt;/p&gt;

&lt;p&gt;对于case2，简单一看那b取环后引用计数还为1，但是a取环，就为0了。这个时候a已经进入unreachable链表中，已经被判为死刑了，但是这个时候，root链表中有b。如果a被做掉，那世界上还有什么正义… ，在root链表中的b会被进行引用检测引用了a，如果a被做掉了，那么b就…凉凉，一审完事，二审a无罪，所以被拉到了root链表中。&lt;/p&gt;

&lt;p&gt;搞两个链表的原因：&lt;/p&gt;

&lt;p&gt;之所以要剖成两个链表，是基于这样的一种考虑：现在的unreachable可能存在被root链表中的对象，直接或间接引用的对象，这些对象是不能被回收的，一旦在标记的过程中，发现这样的对象，就将其从unreachable链表中移到root链表中；当完成标记后，unreachable链表中剩下的所有对象就是名副其实的垃圾对象了，接下来的垃圾回收只需限制在unreachable链表中即可。&lt;/p&gt;

&lt;h3 id=&quot;分代回收&quot;&gt;分代回收&lt;/h3&gt;

&lt;p&gt;了解分类回收，首先要了解一下，GC的阈值，所谓阈值就是一个临界点的值。随着你的程序运行，Python解释器保持对新创建的对象，以及因为引用计数为零而被释放掉的对象的追踪。从理论上说，创建==释放数量应该是这样子。但是如果存在循环引用的话，肯定是创建&amp;gt;释放数量，当创建数与释放数量的差值达到规定的阈值的时候，分代回收机制就登场啦。&lt;/p&gt;

&lt;p&gt;垃圾回收=垃圾检测+释放。&lt;/p&gt;

&lt;p&gt;分代回收思想将对象分为三代（generation 0,1,2），0代表幼年对象，1代表青年对象，2代表老年对象。根据弱代假说（越年轻的对象越容易死掉，老的对象通常会存活更久。）
新生的对象被放入0代，如果该对象在第0代的一次gc垃圾回收中活了下来，那么它就被放到第1代里面（它就升级了）。如果第1代里面的对象在第1代的一次gc垃圾回收中活了下来，它就被放到第2代里面。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gc.set_threshold(threshold0[,threshold1[,threshold2]])&lt;/code&gt;设置gc每一代垃圾回收所触发的阈值。从上一次第0代gc后，如果分配对象的个数减去释放对象的个数大于threshold0，那么就会对第0代中的对象进行gc垃圾回收检查。 从上一次第1代gc后，如过第0代被gc垃圾回收的次数大于threshold1，那么就会对第1代中的对象进行gc垃圾回收检查。同样，从上一次第2代gc后，如过第1代被gc垃圾回收的次数大于threshold2，那么就会对第2代中的对象进行gc垃圾回收检查。&lt;/p&gt;

&lt;h2 id=&quot;collections-1&quot;&gt;collections&lt;/h2&gt;

&lt;h3 id=&quot;ordereddict&quot;&gt;OrderedDict&lt;/h3&gt;

&lt;p&gt;使用OrderedDict会根据&lt;strong&gt;放入元素的先后顺序&lt;/strong&gt;进行排序。所以输出的值是排好序的。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'A'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'c'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'C'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'B'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'1'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'A'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'c'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'C'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'B'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'1'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;xxx&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#输出
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;xxx&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;OrderedDict对象的字典对象，如果其顺序不同那么Python也会把他们当做是两个不同的对象&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'A'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;d2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collections&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'2'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;d2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'A'&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d1&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;## False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;conda&quot;&gt;conda&lt;/h2&gt;

&lt;p&gt;当&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME&lt;/code&gt;容易满时。。可以用这个方法：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.jianshu.com/p/deaaf3f2af91&quot;&gt;https://www.jianshu.com/p/deaaf3f2af91&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;当然，可以在安装的时候指定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--prefix=/home/work/anaconda3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果发现装东西的时候（例如：&lt;a href=&quot;https://github.com/nyu-mll/GLUE-baselines&quot;&gt;https://github.com/nyu-mll/GLUE-baselines&lt;/a&gt;）里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;conda env create -f environment.yml&lt;/code&gt;，pip装psycopg2时gcc失败了，&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    Running setup.py install for psycopg2: finished with status 'error'
    ...

  gcc -pthread -B /home/work/anaconda3/envs/glue/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DPSYCOPG_VERSION=2.8.3 (dt dec pq3 ext) -DPG_VERSION_NUM=80411 -I/home/work/anaconda3/envs/glue/include/python3.6m -I. -I/usr/include -I/usr/include/pgsql/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.6/psycopg/psycopgmodule.o -Wdeclaration-after-statement
  In file included from psycopg/psycopgmodule.c:27:
  ./psycopg/psycopg.h:30:2: error: #error &quot;Psycopg requires PostgreSQL client library (libpq) &amp;gt;= 9.1&quot;
  psycopg/psycopgmodule.c: In function ‘libpq_version’:
  psycopg/psycopgmodule.c:414: warning: implicit declaration of function ‘PQlibVersion’
  
  It appears you are missing some prerequisite to build the package from source.
  
  You may install a binary package by installing 'psycopg2-binary' from PyPI.
  If you want to install psycopg2 from source, please install the packages
  required for the build and try again.
  
  For further information please check the 'doc/src/install.rst' file (also at
  &amp;lt;http://initd.org/psycopg/docs/install.html&amp;gt;).
  
  error: command 'gcc' failed with exit status 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以在外面先：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;conda &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;psycopg2 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;flush&quot;&gt;flush&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file&quot;&gt;https://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;bufsize&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'file.txt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'w'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffering&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bufsize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;技巧&quot;&gt;技巧&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/Yeqd0q30-taiQcdSmrM-pg&quot;&gt;30段极简Python代码：这些小技巧你都Get了么&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;kwargs与args&quot;&gt;kwargs与args&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://kodango.com/variable-arguments-in-python&quot;&gt;理解 Python 中的 *args 和 **kwargs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*args是可变的positional arguments列表，**kwargs是可变的keyword arguments列表。并且，*args必须位于**kwargs之前，因为positional arguments必须位于keyword arguments之前。&lt;/p&gt;

&lt;h3 id=&quot;定义时使用&quot;&gt;定义时使用&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Required argument: '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Optional argument: '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;test_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# result:
# Required argument: 1
# Optional argument:  2
# Optional argument:  3
# Optional argument:  4
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Required argument: '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Optional argument (*args): '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Optional argument %s (*kwargs): %s'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;test_kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# results:
# Required argument:  1
# Optional argument (*args):  2
# Optional argument (*args):  3
# Optional argument (*args):  4
# Optional argument k2 (*kwargs): 6
# Optional argument k1 (*kwargs): 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;调用时使用&quot;&gt;调用时使用&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;third&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fourth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fifth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'First argument: '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Second argument: '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Third argument: '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;third&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Fourth argument: '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fourth&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Fifth argument: '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fifth&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Use *args
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;test_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# results:
# First argument:  1
# Second argument:  2
# Third argument:  3
# Fourth argument:  4
# Fifth argument:  5
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Use **kwargs
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'first'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'second'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'third'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'fourth'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'fifth'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;test_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# results:
# First argument:  1
# Second argument:  2
# Third argument:  3
# Fourth argument:  4
# Fifth argument:  5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;heartrate&quot;&gt;Heartrate&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/GWvMURaNhRbS3jUI5uBjrA&quot;&gt;Heartrate：如追综心跳般实时动态可视化监测Python程序运行&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/alexmojaki/heartrate&quot;&gt;https://github.com/alexmojaki/heartrate&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;其他有用的库&quot;&gt;其他有用的库&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/76112940&quot;&gt;吐血整理！绝不能错过的24个顶级Python库&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;selenium&quot;&gt;Selenium&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.analyticsvidhya.com/blog/2019/05/scraping-classifying-youtube-video-data-python-selenium/&quot;&gt;数据科学项目：使用Python和Selenium抓取YouTube数据对视频进行分类&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>numpy</title>
   <link href="http://daiwk.github.io/posts/knowledge-numpy-usage.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-numpy-usage</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0&quot;&gt;常用函数&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#npshape&quot;&gt;np.shape&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#npmultiply&quot;&gt;np.multiply&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#npdot&quot;&gt;np.dot&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#npmatmul&quot;&gt;np.matmul&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#npsqueeze&quot;&gt;np.squeeze&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;常用函数&quot;&gt;常用函数&lt;/h2&gt;

&lt;h3 id=&quot;npshape&quot;&gt;np.shape&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;W&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# (2,2,3,8)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;npmultiply&quot;&gt;np.multiply&lt;/h3&gt;

&lt;p&gt;element-wise product&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html&quot;&gt;https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;3.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;5.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;7.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;8.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;10.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;mf&quot;&gt;7.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;mf&quot;&gt;16.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;npdot&quot;&gt;np.dot&lt;/h3&gt;

&lt;p&gt;点乘&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html&quot;&gt;https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;具体地：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).&lt;/li&gt;
  &lt;li&gt;If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.&lt;/li&gt;
  &lt;li&gt;If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.&lt;/li&gt;
  &lt;li&gt;If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.&lt;/li&gt;
  &lt;li&gt;If a is an N-D array and b is an M-D array (where M&amp;gt;=2), it is a sum product over the last axis of a and the second-to-last axis of b:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,:]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,:,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;对于两个2D-array而言，就是矩阵乘法（最好使用matmul）：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;而&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[::&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;499128&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,:]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,:,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;499128&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;npmatmul&quot;&gt;np.matmul&lt;/h3&gt;

&lt;p&gt;矩阵乘法&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html#numpy.matmul&quot;&gt;https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html#numpy.matmul&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If both arguments are 2-D they are multiplied like conventional matrices.&lt;/li&gt;
  &lt;li&gt;If either argument is N-D, N &amp;gt; 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.&lt;/li&gt;
  &lt;li&gt;If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.&lt;/li&gt;
  &lt;li&gt;If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Multiplication by a scalar is not allowed, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; instead&lt;/p&gt;

&lt;p&gt;matmul和dot的区别：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Multiplication by scalars is not allowed.&lt;/li&gt;
  &lt;li&gt;Stacks of matrices are broadcast together as if the matrices were elements.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matmul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;npsqueeze&quot;&gt;np.squeeze&lt;/h3&gt;

&lt;p&gt;从数组的形状中&lt;strong&gt;删除单维度条目&lt;/strong&gt;，即&lt;strong&gt;把shape中为1的维度去掉&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/zenghaitao0128/article/details/78512715&quot;&gt;https://blog.csdn.net/zenghaitao0128/article/details/78512715&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squeeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输入：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a表示输入的数组；&lt;/li&gt;
  &lt;li&gt;axis用于指定需要删除的维度，但是指定的维度必须为单维度，否则将会报错；&lt;/li&gt;
  &lt;li&gt;axis的取值可为None 或 int 或 tuple of ints, 可选。&lt;strong&gt;若axis为空，则删除所有单维度的条目&lt;/strong&gt;；&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;返回值：数组，不会修改原数组。&lt;/p&gt;

&lt;p&gt;场景：在机器学习和深度学习中，通常算法的结果是可以表示向量的数组（即包含两对或以上的方括号形式&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[]]&lt;/code&gt;），如果直接利用这个数组进行画图可能显示界面为空（见后面的示例）。我们可以利用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;squeeze&lt;/code&gt;函数将表示向量的数组转换为秩为1的数组，这样利用matplotlib库函数画图时，就可以正常的显示结果了。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;squares&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;squares&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# (1, 5)
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squeeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squares&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# (5,)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>mpi</title>
   <link href="http://daiwk.github.io/posts/knowledge-mpi.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-mpi</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#hello-world&quot;&gt;hello world&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#mpi%E6%95%99%E7%A8%8B&quot;&gt;mpi教程&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#reduce%E4%B8%8Eallreduce&quot;&gt;reduce与allreduce&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;hello-world&quot;&gt;hello world&lt;/h2&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&quot;mpi.h&quot;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numproces&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namelen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;processor_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_MAX_PROCESSOR_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;MPI_Init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MPI_Comm_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MPI_Comm_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numproces&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MPI_Get_processor_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processor_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;namelen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;hello world! process %d of %d on %s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numproces&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;processor_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MPI_Finalize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mpic++ hello.cc &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; hello.out
mpirun &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; 4 hello.out
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出(其中的xxx.xxx就是机器名)：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello world! process 0 of 4 on xxx.xxx
hello world! process 1 of 4 on xxx.xxx
hello world! process 2 of 4 on xxx.xxx
hello world! process 3 of 4 on xxx.xxx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;mpi教程&quot;&gt;mpi教程&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://github.com/wesleykendall/mpitutorial/tree/gh-pages&quot;&gt;https://github.com/wesleykendall/mpitutorial/tree/gh-pages&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;reduce与allreduce&quot;&gt;reduce与allreduce&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;http://mpitutorial.com/tutorials/mpi-reduce-and-allreduce/&quot;&gt;http://mpitutorial.com/tutorials/mpi-reduce-and-allreduce/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;中文版：&lt;a href=&quot;https://blog.csdn.net/yiran103/article/details/79851180&quot;&gt;https://blog.csdn.net/yiran103/article/details/79851180&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;先看看makefile&lt;/p&gt;

&lt;div class=&quot;language-makefile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;EXECS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;reduce_avg reduce_stddev
&lt;span class=&quot;nv&quot;&gt;MPICC&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;?=&lt;/span&gt;mpicc

&lt;span class=&quot;nl&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;${EXECS}&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;reduce_avg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reduce_avg.c&lt;/span&gt;
	&lt;span class=&quot;nv&quot;&gt;${MPICC}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; reduce_avg reduce_avg.c

&lt;span class=&quot;nl&quot;&gt;reduce_stddev&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;reduce_stddev.c&lt;/span&gt;
	&lt;span class=&quot;nv&quot;&gt;${MPICC}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; reduce_stddev reduce_stddev.c &lt;span class=&quot;nt&quot;&gt;-lm&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;clean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;${EXECS}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后reduce的例子是求平均数：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;mpi.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;assert.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;time.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Creates an array of random numbers. Each number has a value from 0 - 1&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RAND_MAX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Usage: avg num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;atoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;MPI_Init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Comm_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;world_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Comm_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Create a random array of elements on all processes.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;srand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// Seed the random number generator to get different results each time for each processor&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Sum the numbers locally&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;local_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;local_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Print the random numbers on each process&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Local sum for process %d - %f, avg = %f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;local_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;local_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Reduce all of the local sums into the global sum&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;global_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MPI_FLOAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MPI_SUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Print the result&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Total sum = %f, avg = %f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;global_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
           &lt;span class=&quot;n&quot;&gt;global_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Clean up&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;MPI_Barrier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Finalize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;很简单，把所有节点的结果加起来，存到0号节点去&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/mpi_reduce.png&quot; style=&quot;max-height: 150px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;然后求标准差是个allreduce的例子：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;mpi.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;math.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;assert.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Creates an array of random numbers. Each number has a value from 0 - 1&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RAND_MAX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Usage: avg num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;atoi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;MPI_Init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Comm_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;world_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Comm_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Create a random array of elements on all processes.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;srand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Seed the random number generator of processes uniquely&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Sum the numbers locally&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;local_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;local_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Reduce all of the local sums into the global sum in order to&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// calculate the mean&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;global_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Allreduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MPI_FLOAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MPI_SUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;global_sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;world_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Compute the local sum of the squared differences from the mean&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;local_sq_diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;local_sq_diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Reduce the global sum of the squared differences to the root process&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// and print off the answer&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;global_sq_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local_sq_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_sq_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MPI_FLOAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MPI_SUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// The standard deviation is the square root of the mean of the squared&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// differences.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;world_rank&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stddev&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_sq_diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;
                        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_elements_per_proc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;world_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Mean - %f, Standard deviation = %f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stddev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// Clean up&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_nums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;MPI_Barrier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MPI_Finalize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;和reduce的区别在于，把所有节点的结果加起来之后，又重新分发到每一个节点啦（相当于Bcast）：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/mpi_allreduce.png&quot; style=&quot;max-height: 150px;max-width:200px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;
</content>
 </entry>
 
 <entry>
   <title>mmr</title>
   <link href="http://daiwk.github.io/posts/knowledge-mmr.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-mmr</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/jiangnanboy/NewsSummary&quot;&gt;https://github.com/jiangnanboy/NewsSummary&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/Silience_Probe/article/details/80700005&quot;&gt;https://blog.csdn.net/Silience_Probe/article/details/80700005&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/zjrn1027/article/details/81136761&quot;&gt;https://blog.csdn.net/zjrn1027/article/details/81136761&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MMR是Maximal Marginal Relevance的缩写，中文为&lt;strong&gt;最大边界相关算法&lt;/strong&gt;或&lt;strong&gt;最大边缘相关算法&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;用来计算Query语句与被搜索文档之间的&lt;strong&gt;相似度&lt;/strong&gt;，从而对文档进行rank排序的算法。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
MMR(Q,C,R)= \underset{D_i \in R \setminus S}{argmax}[\lambda sim_1(Q,d_i)-(1-\lambda)\max _{D_j\in S}(sim_2(d_i, d_j))]
\]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(D_i\)&lt;/code&gt;：集合C中的一篇文档&lt;/li&gt;
  &lt;li&gt;Q: query&lt;/li&gt;
  &lt;li&gt;R: C中的相关文档集合&lt;/li&gt;
  &lt;li&gt;S: 当前结果集合&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\lambda\)&lt;/code&gt;：可调超参，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\lambda\)&lt;/code&gt;越大，准确率越高；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\lambda\)&lt;/code&gt;越小，准确率越低&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;公式分为两部分，&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;左边是候选句子和query的相似程度，&lt;/li&gt;
  &lt;li&gt;右边部分是候选句子与所有已选择句子集合相似度最大值，取了负号，意味着&lt;strong&gt;最终候选的句子间相似度越低越好&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>机器学习中的矩阵、向量求导</title>
   <link href="http://daiwk.github.io/posts/knowledge-matrix.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-matrix</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#数学基础&quot;&gt;数学基础&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#矩阵向量求导&quot;&gt;矩阵、向量求导&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#常用梯度&quot;&gt;常用梯度&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#交叉熵&quot;&gt;交叉熵&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;数学基础&quot;&gt;数学基础&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/v1OMpDUaGqVA5gyoculeuQ&quot;&gt;【资源】机器学习数学全书，1900页PDF下载&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cis.upenn.edu/~jean/math-deep.pdf&quot;&gt;https://www.cis.upenn.edu/~jean/math-deep.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;矩阵向量求导&quot;&gt;矩阵、向量求导&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650737481&amp;amp;idx=1&amp;amp;sn=10e82e52991eb87170e22109857c3dec&amp;amp;chksm=871acf37b06d4621aabc409f95c72f935670c8595f274d62bf5283fdf275f052f29b73c27ab0&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0207pDzvA7Kk5C3JAPj19P1a&amp;amp;pass_ticket=SeHiwrhprfhEeBDsE1XoKKgiqXKD0hs5Oyunmf09XE%2BrWYKA98pvhGxAVGX75FF1#rd&quot;&gt;机器之心最干的文章：机器学习中的矩阵、向量求导&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://daiwk.github.io/assets/matrix+vector+derivatives+for+machine+learning.pdf&quot;&gt;pdf地址&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;常用梯度&quot;&gt;常用梯度&lt;/h2&gt;

&lt;h3 id=&quot;交叉熵&quot;&gt;交叉熵&lt;/h3&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;n&quot;&gt;mf1_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mf2_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lr1_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#, 0.2, 0.3]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;lr2_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#, 0.2, 0.3]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;bias_v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#, 0.1, 0.2]
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mf1_v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mf2_v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lr1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lr1_v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lr2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lr2_v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias_v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requires_grad&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;torch.sigmoid(mf1.dot(mf2)+ lr1 + lr2) GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Check torch.sigmoid(mf1.dot(mf2) + lr1 + lr2) GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后看看正例和负例的loss的梯度：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;c1&quot;&gt;## z = wx+b
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;## cross-entropy loss = -(y*log(sigmoid(z)) + (1-y)*log(1-sigmoid(z)))
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;## cross-entropy loss's w gradient: x(sigmoid(z)-y)
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;## cross-entropy loss's b gradient: (sigmoid(z)-y)
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 

    &lt;span class=&quot;n&quot;&gt;positive_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;positive_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;positive loss mf1 GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Check positive loss mf1 GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# y=1
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;positive loss mf2 GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Check positive loss mf2 GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# y=1
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;ss2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;negative_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ss2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;negative_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;negative loss mf1 GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Check negative loss mf1 GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# y=0
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;negative loss mf2 GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Check negative loss mf2 GRAD:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# y=0
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mf2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grad&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;举例&quot;&gt;举例&lt;/h3&gt;

&lt;p&gt;A是m*n，X是n*k，AX是m*k&lt;/p&gt;

&lt;p&gt;Y=reduce_sum(AX)对X的导数就包括&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;reduce_sum对AX的导数：一个全1的m*k的矩阵&lt;/li&gt;
  &lt;li&gt;AX对X的导数：A^T，n*m&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/pinard/p/10825264.html&quot;&gt;https://www.cnblogs.com/pinard/p/10825264.html&lt;/a&gt;&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;

&lt;img src=&quot;../assets/matrix-qiudao.png&quot; style=&quot;max-height: 400px&quot; /&gt;
&lt;br /&gt;

&lt;/html&gt;

&lt;p&gt;所以这个就是 A^T * 全1矩阵，也就是n*m和m*k相乘，得到的n*k，和原来的X一样的维度&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>matplotlib</title>
   <link href="http://daiwk.github.io/posts/knowledge-matplotlib.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-matplotlib</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650755443&amp;amp;idx=3&amp;amp;sn=0229c2d8eeb8e07486d6393cd9b983ae&amp;amp;chksm=871a950db06d1c1b9d7a08b38f2f16f974e5a79f994fb44175dab7a3ef5d7ece39e7763c8f49&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0113CI7lPTEcV99uFQdNOXdp&amp;amp;pass_ticket=GbqnkzYDgSDQxJoviNYzckA8ZJ6bULsWpoyug4CHgCsT0B80C5nEC38bRj4CywCT#rd&quot;&gt;50种常用的matplotlib可视化，再也不用担心模型背着我乱跑了&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原始文章&lt;a href=&quot;https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python&quot;&gt;https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;通用配置：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# !pip install brewer2mpl
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mpl&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;seaborn&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sns&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;warnings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warnings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filterwarnings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'once'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;large&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;med&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;small&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'axes.titlesize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;large&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;'legend.fontsize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;med&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;'figure.figsize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;'axes.labelsize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;med&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;'axes.titlesize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;med&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;'xtick.labelsize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;med&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;'ytick.labelsize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;med&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;'figure.titlesize'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;large&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rcParams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'seaborn-whitegrid'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;white&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matplotlib&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inline&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Version
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mpl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__version__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;#&amp;gt; 3.0.0
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__version__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;#&amp;gt; 0.9.0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>linux</title>
   <link href="http://daiwk.github.io/posts/knowledge-linux.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-linux</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#查看机器支持的指定集&quot;&gt;查看机器支持的指定集&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#每一条命令都显示执行的时间戳&quot;&gt;每一条命令都显示执行的时间戳&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;查看机器支持的指定集&quot;&gt;查看机器支持的指定集&lt;/h2&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;work@xxx&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /proc/cpuinfo | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;flags | &lt;span class=&quot;nb&quot;&gt;uniq
&lt;/span&gt;flags           : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm dca sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch arat epb xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms invpcid rtm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;每一条命令都显示执行的时间戳&quot;&gt;每一条命令都显示执行的时间戳&lt;/h2&gt;

&lt;p&gt;首先要安装ts命令，参考&lt;a href=&quot;https://rentes.github.io/unix/utilities/2015/07/27/moreutils-package/&quot;&gt;https://rentes.github.io/unix/utilities/2015/07/27/moreutils-package/&lt;/a&gt;。如果在centos，就：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yum &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;moreutils
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后假设sh文件是：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-x&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;c&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;d&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;于是我们执行：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sh xx.sh 2&amp;gt;&amp;amp;1 | ts &lt;span class=&quot;s1&quot;&gt;'[%Y-%m-%d %H:%M:%S]'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;就可以看到：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2019-03-27 11:10:34] + &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;a
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2019-03-27 11:10:34] a
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2019-03-27 11:10:34] + &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;b
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2019-03-27 11:10:34] b
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2019-03-27 11:10:34] + &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;c
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2019-03-27 11:10:34] c
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2019-03-27 11:10:34] + &lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;d
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2019-03-27 11:10:34] d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>hadoop streaming</title>
   <link href="http://daiwk.github.io/posts/knowledge-hadoop-streaming.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-hadoop-streaming</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#排序相关&quot;&gt;排序相关&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#如何确定map任务数&quot;&gt;如何确定map任务数&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#如何确定reduce任务数&quot;&gt;如何确定reduce任务数&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考。。内部文档&lt;/p&gt;

&lt;h2 id=&quot;排序相关&quot;&gt;排序相关&lt;/h2&gt;

&lt;p&gt;通过partition来简单地对key进行二次排序&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# -D map.output.key.field.separator=.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -D stream.num.map.output.key.fields=2: 输出有n部分，前两列是key&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -D stream.map.output.field.separator=.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -D num.key.fields.for.partition=1: key里面第1列拿来分桶。第一列相同的分在一个桶里，然后桶内再按第2列排序 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;还可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-D mapred.text.key.partitioner.options=spec&lt;/code&gt;来指定更详细的分桶方式&lt;/p&gt;

&lt;p&gt;而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;KeyFieldBasedComparator&lt;/code&gt;可以指定key的排序方式&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hadoop streaming &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-partitioner&lt;/span&gt; org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-jobconf&lt;/span&gt; stream.num.map.output.key.fields&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-jobconf&lt;/span&gt; stream.map.output.field.separator&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-jobconf&lt;/span&gt; map.output.key.field.separator&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-jobconf&lt;/span&gt; mapred.text.key.partitioner.options&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-k1&lt;/span&gt;,1 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-jobconf&lt;/span&gt; mapred.text.key.comparator.options&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-k3&lt;/span&gt;,3n &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-jobconf&lt;/span&gt; mapred.output.key.comparator.class&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;org.apache.hadoop.mapred.lib.KeyFieldBasedComparator

&lt;span class=&quot;c&quot;&gt;## 前3列是key，key里第一列拿来做分桶，然后只拿第三列来排序&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;如何确定map任务数&quot;&gt;如何确定map任务数&lt;/h2&gt;

&lt;p&gt;确定map任务数时依次优先参考如下几个原则：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每个map任务使用的内存不超过800M，尽量在500M以下&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;比如处理256MB数据需要的时间为10分钟，内存为800MB，此时如果处理128MB时，内存可以减小为400MB，则选择每一个map的处理数据量为128MB&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每个map任务运行时间控制在大约20分钟，最好1-3分钟&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;比如处理256MB数据需要的时间为30分钟，内存为200MB，则应该考虑减小map的计算时间，比如将每一个map的处理数据量设置为128MB，将时间减小为15分钟。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每个map任务处理的最大数据量为一个HDFS块大小（目前为256MB），一个map任务处理的输入不能跨文件&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;比如指定map任务数为N，输入数据总量为S。如果S / N &amp;gt; 256MB，平台会自动增加map任务数使每个map任务处理数据量不超过256MB；如果S / N &amp;lt; 256MB，平台认为每个map任务最多处理S/N大小的数据，但是一个map任务的输入不能跨文件，所以可能有的文件切分到最后一部分时较小于S/N，那么下一个map任务的输入小于平均，最终的map任务数大于N。最终实际运行的map任务数可以在JobTracker监控页面查看。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;map任务总数不超过平台可用的任务槽位&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果在一个map处理256MB时就能将平台可用的任务槽位占满，此时不应该再增加map任务数。&lt;/p&gt;

&lt;h2 id=&quot;如何确定reduce任务数&quot;&gt;如何确定reduce任务数&lt;/h2&gt;

&lt;p&gt;确定reduce任务数时依次优先参考如下几个方面：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每个reduce任务使用的内存不超过800M，尽量在500M以下&lt;/li&gt;
  &lt;li&gt;每个reduce任务运行时间控制在大约20分钟，最好1-3分钟&lt;/li&gt;
  &lt;li&gt;整个reduce阶段的输入数据总量&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;reduce的输入是map的输出，整个reduce阶段的输入数据总量就是所有map任务的输出数据总量，如果map输出的中间结果较大，推荐使用lzo进行压缩&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每个reduce任务处理的数据量控制在500MB以内&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;由于一个reducer处理的数据会按照key进行排序，每个reduce任务处理数据量过大会导致shuffle和排序时间很长&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;map任务数与reduce任务数的乘积&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在reduce的shuffle阶段每一个reducer需要到多个mapper去用tcp连接来下载自己要处理的数据。如果map任务数和reduce任务数乘积较大，可能造成需要建立过多的tcp连接，如果每一次连接传输数据又很少，就会导致reduce的 shuffle时间很长。建议每个reduce的一次连接下载的数据量 ＝ map输出数据总量 /（map数*reduce数）不要小于100KB。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输出数据要求&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果reduce计算后的结果数据用于下一次MapReduce计算，或者结果文件不宜太小，那么在满足或者大致满足以上原则的前提下，可以调小reduce任务数，以便每一个reduce任务数的输出不会太小。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>cpu、内存问题排查（gperftools、valgrind、perf等）</title>
   <link href="http://daiwk.github.io/posts/knowledge-gperftools.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-gperftools</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f&quot;&gt;内存泄漏&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%b8%b8%e7%94%a8%e5%b7%a5%e5%85%b7&quot;&gt;常用工具&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#gperftools%e7%9a%84%e5%8a%9f%e8%83%bd%e6%94%af%e6%8c%81&quot;&gt;gperftools的功能支持&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gperf%e5%ae%89%e8%a3%85&quot;&gt;gperf安装&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gperf%e7%a4%ba%e4%be%8bc&quot;&gt;gperf示例（c++）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#gperf%e7%a4%ba%e4%be%8bpython&quot;&gt;gperf示例（python）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#valgrind%e7%a4%ba%e4%be%8b&quot;&gt;valgrind示例&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#perf%e7%81%ab%e7%84%b0%e5%9b%be&quot;&gt;perf+火焰图&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#%e7%81%ab%e7%84%b0%e5%9b%be%e5%b7%a5%e5%85%b7&quot;&gt;火焰图工具&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#perf&quot;&gt;perf&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#sanitizers&quot;&gt;sanitizers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzIxNTgyMDMwMw==&amp;amp;mid=2247484403&amp;amp;idx=1&amp;amp;sn=5b260e7d681a4550811ee5611a1dd4ce&amp;amp;chksm=97933293a0e4bb85104066213606f5c89002fe78fa0518d95c71918a5a6ae656c62beac0f1ae&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=0608zWg71wg9411XBaYQ7o1V&amp;amp;pass_ticket=xLsJxSJh9Kgj4HKrq0S6VH1cKTCnSBShWGuwGJy9Gfpbp1CgoA6crqJiPhq9JjnM#rd&quot;&gt;高阶干货|如何用gperftools分析深度学习框架的内存泄漏问题&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;内存泄漏&quot;&gt;内存泄漏&lt;/h2&gt;

&lt;p&gt;内存泄漏一般是由于程序&lt;strong&gt;在堆(heap)上&lt;/strong&gt;分配了内存而没有释放，随着程序的运行占用的内存越来越大，一方面会影响程序的稳定性，可能让运行速度越来越慢，或者造成oom，甚至会影响程序所运行的机器的稳定性，造成宕机。&lt;/p&gt;

&lt;h2 id=&quot;常用工具&quot;&gt;常用工具&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;valgrind直接分析非常困难，需要自己编译debug版本的、带valgrind支持的专用Python版本，而且输出的信息中大部分是Python自己的符号和调用信息，很难看出有用的信息，另外&lt;strong&gt;使用valgrind会让程序运行速度变得非常慢&lt;/strong&gt;，所以不建议使用。&lt;/li&gt;
  &lt;li&gt;gperftools使用简单，无需重新编译代码即可运行，对运行速度的影响也比较小。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;gperftools的功能支持&quot;&gt;gperftools的功能支持&lt;/h3&gt;

&lt;p&gt;gperftool主要支持以下四个功能：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;thread-caching malloc&lt;/li&gt;
  &lt;li&gt;heap-checking using tcmalloc&lt;/li&gt;
  &lt;li&gt;heap-profiling using tcmalloc&lt;/li&gt;
  &lt;li&gt;CPU profiler&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;首先的首先，需要定义如下环境变量：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PPROF_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/root/gopath/bin/pprof
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PPROF_BINARY_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/root/gopath/bin/pprof
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;LD_PRELOAD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/lib/libtcmalloc.so.4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后运行程序前还要加上如下参数：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;HEAPPROFILE：设置生成的堆分析文件的目录和文件前缀&lt;/li&gt;
  &lt;li&gt;HEAP_PROFILE_ALLOCATION_INTERVAL：设置&lt;strong&gt;每分配多少存储dump一次&lt;/strong&gt;，默认1GB&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;gperf安装&quot;&gt;gperf安装&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; /home/work/xxx
git clone https://github.com/gperftools/gperftools.git
./autogen.sh
./configure &lt;span class=&quot;nt&quot;&gt;--prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/home/work/xxx
make
make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/u011217649/article/details/77683126&quot;&gt;https://blog.csdn.net/u011217649/article/details/77683126&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;安装tcmalloc相关的lib&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz&quot;&gt;http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;gperf示例c&quot;&gt;gperf示例（c++）&lt;/h3&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_destory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mega&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mega&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mega&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;malloc_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_destory_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mega&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;create_destory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_destory_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g++ ./test_heap.cpp &lt;span class=&quot;nt&quot;&gt;-ltcmalloc&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-lprofiler&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; heap_profiler
&lt;span class=&quot;nv&quot;&gt;HEAPPROFILE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/tmp/profile ./heap_profiler
pprof ./heap_profiler &lt;span class=&quot;nt&quot;&gt;--pdf&lt;/span&gt; /tmp/profile.0009.heap  &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; x.pdf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当然，也可以通过text来看：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pprof ./heap_profiler &lt;span class=&quot;nt&quot;&gt;--text&lt;/span&gt; /tmp/profile.0009.heap
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;显示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Using &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;file ./heap_profiler.
Using &lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;file /tmp/profile.0009.heap.
Total: 4096.0 MB
  4096.0 100.0% 100.0%   4096.0 100.0% create
     0.0   0.0% 100.0%      0.0   0.0% std::basic_string::_Rep::_S_create
     0.0   0.0% 100.0%      0.0   0.0% 00007fcca097dde1
     0.0   0.0% 100.0%      0.0   0.0% 00007fcca097ddc0
     0.0   0.0% 100.0%      0.0   0.0% 00007fcca098c405
     0.0   0.0% 100.0%      0.0   0.0% 0x0072656c69666f71
     0.0   0.0% 100.0%   4096.0 100.0% __libc_start_main
     0.0   0.0% 100.0%      0.0   0.0% _init
     0.0   0.0% 100.0%      0.0   0.0% _init &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;inline&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
     0.0   0.0% 100.0%   4096.0 100.0% _start
     0.0   0.0% 100.0%   4096.0 100.0% main
     0.0   0.0% 100.0%      0.0   0.0% std::basic_string::basic_string
     0.0   0.0% 100.0%      0.0   0.0% std::basic_string::copy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面的方式是完整模式，我们还可以通过diff模式，对两个时刻的heap做diff，把一些内存分配没有发生变化的模块去掉，而把增量部分显示出来。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pprof &lt;span class=&quot;nt&quot;&gt;--pdf&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--base&lt;/span&gt; test.log.0010.heap ./heap_profiler test.log.1045.heap
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;gperf示例python&quot;&gt;gperf示例（python）&lt;/h3&gt;

&lt;p&gt;和c++几乎一样，区别如下(把可执行文件换成python)：&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-shel&quot;&gt;env HEAPPROFILE=/tmp/profile HEAP_PROFILE_ALLOCATION_INTERVAL=209715200 python train.py
pprof --pdf python test.log.0012.heap
pprof --pdf --base test.log.0010.heap python test.log.1045.heap
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;valgrind示例&quot;&gt;valgrind示例&lt;/h3&gt;

&lt;p&gt;不能开tcmalloc。。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//const int array_count = 40000;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;array_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array_count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g++ a.cpp
valgrind &lt;span class=&quot;nt&quot;&gt;--leak-check&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;full &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--tool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;memcheck &lt;span class=&quot;nt&quot;&gt;--show-reachable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;yes&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--track-origins&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;yes&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--log-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;./x.log ./a.out
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后看看x.log文件，可见泄漏了两次，int那次4byte，double那次8byte：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; Memcheck, a memory error detector
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; Copyright &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 2002-2010, and GNU GPL&lt;span class=&quot;s1&quot;&gt;'d, by Julian Seward et al.
==2220== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==2220== Command: ./a.out
==2220== Parent PID: 11791
==2220== 
--2220-- 
--2220-- Valgrind options:
--2220--    --leak-check=full
--2220--    -v
--2220--    --tool=memcheck
--2220--    --show-reachable=yes
--2220--    --track-origins=yes
--2220--    --log-file=x.log
--2220-- Contents of /proc/version:
--2220--   Linux version 3.10.0_1-0-0-8 (whistler@www.local) (gcc version 4.9.2 20150212 (Red Hat 4.9.2-6) (GCC) ) #1 SMP Thu Mar 24 15:26:50 CST 2016
--2220-- Arch and hwcaps: AMD64, amd64-sse3-cx16
--2220-- Page sizes: currently 4096, max supported 4096
--2220-- Valgrind library directory: /usr/lib64/valgrind
--2220-- Reading syms from /home/learn_tools/a.out (0x400000)
--2220-- Reading syms from /usr/lib64/valgrind/memcheck-amd64-linux (0x38000000)
--2220--    object doesn'&lt;/span&gt;t have a dynamic symbol table
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; Reading syms from /lib64/ld-2.12.so &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x318a600000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; Reading suppressions file: /usr/lib64/valgrind/default.supp
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; REDIR: 0x318a6174a0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;strlen&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; redirected to 0x38042ae7 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;vgPlain_amd64_linux_REDIR_FOR_strlen&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; Reading syms from /usr/lib64/valgrind/vgpreload_core-amd64-linux.so &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x4801000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; Reading syms from /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x4a02000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; WARNING: new redirection conflicts with existing &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; ignoring it
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt;     new: 0x318a6174a0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;strlen              &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; R-&amp;gt; 0x04a07830 strlen
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; REDIR: 0x318a617310 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; redirected to 0x4a07470 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; REDIR: 0x318a617390 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;strcmp&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; redirected to 0x4a07df0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;strcmp&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; Reading syms from /usr/lib64/libstdc++.so.6.0.13 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x318ea00000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt;    object doesn&lt;span class=&quot;s1&quot;&gt;'t have a symbol table
--2220-- Reading syms from /lib64/libm-2.12.so (0x318b600000)
--2220-- Reading syms from /lib64/libgcc_s-4.4.6-20120305.so.1 (0x318da00000)
--2220--    object doesn'&lt;/span&gt;t have a symbol table
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; Reading syms from /lib64/libc-2.12.so &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x318ae00000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; REDIR: 0x318ae84100 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;strcasecmp&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; redirected to 0x4801560 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;_vgnU_ifunc_wrapper&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; REDIR: 0x318ae863c0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;strncasecmp&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; redirected to 0x4801560 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;_vgnU_ifunc_wrapper&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; REDIR: 0x318ae82070 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;__GI_strrchr&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; redirected to 0x4a072f0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;__GI_strrchr&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; REDIR: 0x318eabd1b0 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;operator new[]&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;unsigned long&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; redirected to 0x4a06680 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;operator new[]&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;unsigned long&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; REDIR: 0x318ae7a950 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;free&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; redirected to 0x4a05890 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;free&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; HEAP SUMMARY:
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;use at &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;: 12 bytes &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;2 blocks
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;   total heap usage: 2 allocs, 0 frees, 12 bytes allocated
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; Searching &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;pointers to 2 not-freed blocks
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; Checked 179,384 bytes
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 4 bytes &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;1 blocks are definitely lost &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;loss record 1 of 2
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;    at 0x4A0674C: operator new[]&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;unsigned long&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;vg_replace_malloc.c:305&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;    by 0x4005DF: main &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; /home/learn_tools/a.out&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 8 bytes &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;1 blocks are definitely lost &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;loss record 2 of 2
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;    at 0x4A0674C: operator new[]&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;unsigned long&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;vg_replace_malloc.c:305&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;    by 0x4005ED: main &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; /home/learn_tools/a.out&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; LEAK SUMMARY:
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;    definitely lost: 12 bytes &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;2 blocks
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;    indirectly lost: 0 bytes &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;0 blocks
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;      possibly lost: 0 bytes &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;0 blocks
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;    still reachable: 0 bytes &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;0 blocks
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;         suppressed: 0 bytes &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;0 blocks
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; ERROR SUMMARY: 2 errors from 2 contexts &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;suppressed: 6 from 6&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; 
&lt;span class=&quot;nt&quot;&gt;--2220--&lt;/span&gt; used_suppression:      6 dl-hack3-cond-1
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;2220&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; ERROR SUMMARY: 2 errors from 2 contexts &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;suppressed: 6 from 6&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;perf火焰图&quot;&gt;perf+火焰图&lt;/h3&gt;

&lt;h4 id=&quot;火焰图工具&quot;&gt;火焰图工具&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/brendangregg/FlameGraph&quot;&gt;https://github.com/brendangregg/FlameGraph&lt;/a&gt;下载下来&lt;/p&gt;

&lt;p&gt;然后解压并改个名：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;unzip master.zip
&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; ./FlameGraph-master/ ./FlameGraph
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;perf&quot;&gt;perf&lt;/h4&gt;

&lt;p&gt;如果内核版本2.6.32_1-23-0-0以上，系统有自带的(centos6u3以上的也自带)。否则需要安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.12.9.tar.xz
xz &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; linux-4.12.9.tar.xz
&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;xf linux-4.12.9.tar
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;linux-4.12.9/tools/perf
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/opt/compiler/gcc-4.8.2/bin:/opt/stap/bin:&lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;
make &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;make &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;perf record &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt; 500 &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$task_id&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; perf.data &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sleep&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$time&lt;/span&gt; &amp;amp;
&lt;span class=&quot;nb&quot;&gt;wait&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;## 参数&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;##record - Run a command and record its profile into perf.data&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;##-F，--freq= ，Profile at this frequency.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;##-p， --pid=，Record events on existing process ID&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;##-o，--output=，Output file name.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;##-g，--call-graph，Do call-graph (stack chain/backtrace) recording.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;##sleep，采集时长，单位s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;perf script &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; perf.data &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; out.perf
./FlameGraph/stackcollapse-perf.pl out.perf &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; out.folded
./FlameGraph/flamegraph.pl out.folded &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; cpu.svg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;例如，我们对某进程搞一下，可以看到以下火焰图，然后我们发现xgboost的预测&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xgboost::predictor::CPUPredictor::PredLoopSpecalize&lt;/code&gt;就占了总cpu利用的9.36%！。。：&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/flame-perf.png&quot; style=&quot;max-height: 250px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;还可以用这个&lt;a href=&quot;https://daiwk.github.io/assets/perf.tar.gz&quot;&gt;https://daiwk.github.io/assets/perf.tar.gz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;然后，&lt;strong&gt;用root权限&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;实时热点展示&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./perf top &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pid&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;统计信息：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./perf &lt;span class=&quot;nb&quot;&gt;stat&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pid&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;sanitizers&quot;&gt;sanitizers&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/google/sanitizers/wiki&quot;&gt;https://github.com/google/sanitizers/wiki&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/C_lazy/article/details/80009627&quot;&gt;使用AddressSanitizer进行内存访问越界检查&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/google/sanitizers/wiki/AddressSanitizer&quot;&gt;https://github.com/google/sanitizers/wiki/AddressSanitizer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;看看test_asan.cpp：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后，参考&lt;a href=&quot;https://tsdgeos.blogspot.com/2014/03/asan-and-gcc-how-to-get-line-numbers-in.html&quot;&gt;https://tsdgeos.blogspot.com/2014/03/asan-and-gcc-how-to-get-line-numbers-in.html&lt;/a&gt;，gcc要下面这样才能看到行号。。而clang可以直接看到行号。。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ASAN_OPTIONS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;symbolize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
/opt/compiler/gcc-4.8.2/bin/g++ &lt;span class=&quot;nt&quot;&gt;-fsanitize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;address &lt;span class=&quot;nt&quot;&gt;-O1&lt;/span&gt;  &lt;span class=&quot;nt&quot;&gt;-fno-omit-frame-pointer&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt;  ./test_asan.cpp 
./a.out
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后运行：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;=================================================================
==202427== ERROR: AddressSanitizer: heap-use-after-free on address 0x600e0000dfb5 at pc 0x4008a3 bp 0x7fffb353a230 sp 0x7fffb353a228
READ of size 1 at 0x600e0000dfb5 thread T0
    #0 0x4008a2 in main /home/work/daiwenkai/workspaces/Useful_tools/./test_asan.cpp:5
    #1 0x7efc7d1b6bd4 in __libc_start_main (/opt/compiler/gcc-4.8.2/lib/libc.so.6+0x21bd4)
    #2 0x400748 in _start (/home/disk2/daiwenkai/workspaces/Useful_tools/a.out+0x400748)
0x600e0000dfb5 is located 5 bytes inside of 80-byte region [0x600e0000dfb0,0x600e0000e000)
freed by thread T0 here:
    #0 0x7efc7dd734ba in __interceptor_free /opt/compiler/gcc-4.8.3-build-tmp/gcc-4.8.3/build_gcc/x86_64-baidu-linux-gnu/libsanitizer/asan/../../../../libsanitizer/asan/asan_malloc_linux.cc:61
    #1 0x40087d in main /home/work/daiwenkai/workspaces/Useful_tools/./test_asan.cpp:4
    #2 0x7efc7d1b6bd4 in __libc_start_main (/opt/compiler/gcc-4.8.2/lib/libc.so.6+0x21bd4)
previously allocated by thread T0 here:
    #0 0x7efc7dd7359a in malloc /opt/compiler/gcc-4.8.3-build-tmp/gcc-4.8.3/build_gcc/x86_64-baidu-linux-gnu/libsanitizer/asan/../../../../libsanitizer/asan/asan_malloc_linux.cc:71
    #1 0x400872 in main /home/work/daiwenkai/workspaces/Useful_tools/./test_asan.cpp:3
    #2 0x7efc7d1b6bd4 in __libc_start_main (/opt/compiler/gcc-4.8.2/lib/libc.so.6+0x21bd4)
SUMMARY: AddressSanitizer: heap-use-after-free /home/work/daiwenkai/workspaces/Useful_tools/./test_asan.cpp:5 main
Shadow bytes around the buggy address:
  0x0c023fff9ba0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c023fff9bb0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c023fff9bc0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c023fff9bd0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c023fff9be0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=&amp;gt;0x0c023fff9bf0: fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd fd fd
  0x0c023fff9c00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c023fff9c10: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c023fff9c20: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c023fff9c30: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c023fff9c40: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:     fa
  Heap righ redzone:     fb
  Freed Heap region:     fd
  Stack left redzone:    f1
  Stack mid redzone:     f2
  Stack right redzone:   f3
  Stack partial redzone: f4
  Stack after return:    f5
  Stack use after scope: f8
  Global redzone:        f9
  Global init order:     f6
  Poisoned by user:      f7
  ASan internal:         fe
==202427== ABORTING
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可见，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test_asan.cpp:5&lt;/code&gt;使用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test_asan.cpp:4&lt;/code&gt;就free掉的指针，而这个指针是在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test_asan.cpp:3&lt;/code&gt;进行的内存分配。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>git常用操作</title>
   <link href="http://daiwk.github.io/posts/knowledge-git.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-git</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#git常用操作&quot;&gt;git常用操作&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#如何把fork的源代码的更新merge到自己的来&quot;&gt;如何把fork的源代码的更新merge到自己的来&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#如何回滚到某一次commit&quot;&gt;如何回滚到某一次commit&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#git-reset&quot;&gt;git reset&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#git-revert&quot;&gt;git revert&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#如何把一个分支的代码合并到另一个分支&quot;&gt;如何把一个分支的代码合并到另一个分支&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#如何往github的某一个分支push代码&quot;&gt;如何往github的某一个分支push代码&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#submodule&quot;&gt;submodule&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;git常用操作&quot;&gt;git常用操作&lt;/h2&gt;

&lt;h3 id=&quot;如何把fork的源代码的更新merge到自己的来&quot;&gt;如何把fork的源代码的更新merge到自己的来&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git remote &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; 
git remote add upstream git@github.com:xxx/xxx.git
&lt;span class=&quot;c&quot;&gt;## 例如： git remote add upstream https://github.com/codertimo/BERT-pytorch.git&lt;/span&gt;
git fetch upstream
git merge upstream/master
git push 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;如何回滚到某一次commit&quot;&gt;如何回滚到某一次commit&lt;/h3&gt;

&lt;h4 id=&quot;git-reset&quot;&gt;git reset&lt;/h4&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git reset &lt;span class=&quot;nt&quot;&gt;--hard&lt;/span&gt; HEAD^         &lt;span class=&quot;c&quot;&gt;#回退到上个版本&lt;/span&gt;
git reset &lt;span class=&quot;nt&quot;&gt;--hard&lt;/span&gt; HEAD~3        &lt;span class=&quot;c&quot;&gt;#回退到前3次提交之前，以此类推，回退到n次提交之前&lt;/span&gt;
git reset &lt;span class=&quot;nt&quot;&gt;--hard&lt;/span&gt; commit_id     &lt;span class=&quot;c&quot;&gt;#退到/进到 指定commit的sha码&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;git revert 和 git reset的区别：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;git revert是用一次新的commit来回滚之前的commit，git reset是直接删除指定的commit。&lt;/li&gt;
  &lt;li&gt;在回滚这一操作上看，效果差不多。但是在日后继续merge以前的老版本时有区别。因为git revert是用一次逆向的commit“中和”之前的提交，因此日后合并老的branch时，导致这部分改变不会再次出现，但是git reset是之间把某些commit在某个branch上删除，因而和老的branch再次merge时，这些被回滚的commit应该还会被引入。&lt;/li&gt;
  &lt;li&gt;git reset 是把HEAD向后移动了一下，而git revert是HEAD继续前进，只是新的commit的内容和要revert的内容正好相反，能够抵消要被revert的内容。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;强行推送到远端&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git push origin HEAD --force
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;git-revert&quot;&gt;git revert&lt;/h4&gt;

&lt;p&gt;revert操作：
&lt;a href=&quot;https://blog.csdn.net/secretx/article/details/51461972&quot;&gt;https://blog.csdn.net/secretx/article/details/51461972&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;官方文档：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://git-scm.com/docs/git-revert&quot;&gt;https://git-scm.com/docs/git-revert&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;首先，git log看一下&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;commit 23bfb9d15d634111111111111 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;HEAD -&amp;gt; master, origin/master, origin/HEAD&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Merge: 1f35488 941d4a7
Author: aaa
Date:   Mon Oct 14 19:46:37 2019 +0800

    Merge branch &lt;span class=&quot;s1&quot;&gt;'branch_xxx'&lt;/span&gt; into master

commit 941d4a7939b933333333333333 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;branch_xxx&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Author: x11111
Date:   Mon Oct 14 15:28:42 2019 +0800

    change xxx
    
    Change-Id: I934e97d3e08a88888888

commit 669835a2859a24444444444444 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;origin/branch_xxx&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Author: x222
Date:   Sat Oct 12 20:14:17 2019 +0800

    change iiiii
    
    Change-Id: I89f1a409776c036222222222

commit 783a64f999999999999999
Merge: 3848c42 1f35488
Author: x3444
Date:   Sat Oct 12 14:57:47 2019 +0800

    Merge remote-tracking branch &lt;span class=&quot;s1&quot;&gt;'origin/master'&lt;/span&gt; into branch_xxx
    
    Change-Id: Ib6af2ff31633333333

commit 1f35488521d47777777777 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;origin/branch_iii&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Merge: cf0491a 743b9b1
Author: iimmm
Date:   Sat Oct 12 14:27:12 2019 +0800

    Merge branch &lt;span class=&quot;s1&quot;&gt;'branch_iii'&lt;/span&gt; into master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也就是说，整个历史是这样的：&lt;/p&gt;

&lt;p&gt;首先branch_iii通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1f354885&lt;/code&gt;这次commit合到了master，然后在branch_xxx里有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;783a64f&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;669835a&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;941d4a7&lt;/code&gt;这么三次commit，最后就把branch_xxx通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;23bfb9d1&lt;/code&gt;这次commit合到了master。&lt;/p&gt;

&lt;p&gt;现在想把最后一次commit给revert掉，也就是还原到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1f354885&lt;/code&gt;这次commit的状态。&lt;/p&gt;

&lt;p&gt;我们发现，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;23bfb9d1&lt;/code&gt;这个commit，写着：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Merge: 1f35488 941d4a7&lt;/code&gt;，这其实包括了两次的commit信息&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;我们看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;941d4a7&lt;/code&gt;开头的这次commit，可见这是合入master前在分支branch_xxx的最后一次commit：&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;commit 941d4a7939b933333333333333 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;branch_xxx&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Author: x11111
Date:   Mon Oct 14 15:28:42 2019 +0800

    change xxx
    
    Change-Id: I934e97d3e08a88888888
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;我们再看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1f35488&lt;/code&gt;开头的这次commit，可见这是master的上一次合入（从branch_iii）合过来的：&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;commit 1f35488521d47777777777 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;origin/branch_iii&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Merge: cf0491a 743b9b1
Author: iimmm
Date:   Sat Oct 12 14:27:12 2019 +0800

    Merge branch &lt;span class=&quot;s1&quot;&gt;'branch_iii'&lt;/span&gt; into master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;我现在是想把最新的这次合入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;23bfb9d15d634111111111111&lt;/code&gt;给去掉，也就是我要还原到上次合到master的状态。再看回&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Merge: 1f35488 941d4a7&lt;/code&gt;，-m 1表示的是回到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1f35488&lt;/code&gt;这个commit，-m 2表示的是回到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;941d4a7&lt;/code&gt;这个commit。我们想要的是把这个分支新加的东西都干掉，所以是回到上次master的commit，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1f35488&lt;/code&gt;，所以我们选择-m 1。&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git revert &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; 1 23bfb9d15d634111111111111
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;如何把一个分支的代码合并到另一个分支&quot;&gt;如何把一个分支的代码合并到另一个分支&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone xxx
git checkout branch_a
git merge origin/branch_b
&lt;span class=&quot;c&quot;&gt;# 然后本地处理conflict&lt;/span&gt;
git add &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
git commit &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;合并分支&quot;&lt;/span&gt;
git push origin HEAD:refs/for/branch_a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;如何往github的某一个分支push代码&quot;&gt;如何往github的某一个分支push代码&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;case1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果已经在github在界面上建好了分支&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test-dwk&lt;/code&gt;，那么&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git pull
git branch &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;创建本地分支test-dwk(名字必须一致！！)，并且和远程origin/test-dwk分支关联&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git checkout test-dwk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;提交&lt;/p&gt;
&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git push &lt;span class=&quot;nt&quot;&gt;--set-upstream&lt;/span&gt; origin test-dwk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;case2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;如果没有在github在界面上建好了分支&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test-dwk&lt;/code&gt;，那么&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git branch &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; test-dwk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样就创建了本地分支test-dwk&lt;/p&gt;

&lt;p&gt;提交&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git push &lt;span class=&quot;nt&quot;&gt;--set-upstream&lt;/span&gt; origin test-dwk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样，远程也就多了origin/test-dwk这样一个分支&lt;/p&gt;

&lt;h2 id=&quot;submodule&quot;&gt;submodule&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.jianshu.com/p/9000cd49822c&quot;&gt;https://www.jianshu.com/p/9000cd49822c&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/LEON1741/article/details/90259836&quot;&gt;https://blog.csdn.net/LEON1741/article/details/90259836&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ftp</title>
   <link href="http://daiwk.github.io/posts/knowledge-ftp.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-ftp</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;设置FTP虚拟账号密码&lt;/p&gt;

&lt;p&gt;修改proftpd配置文件（早期系统镜像的默认FTP配置路径为/etc/proftpd.conf）&lt;/p&gt;

&lt;p&gt;注释&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Anonymous ~ftp&amp;gt; … &amp;lt;/Anonymous&amp;gt;&lt;/code&gt;之间相关配置&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Anonymous ~ftp&amp;gt; … &amp;lt;/Anonymous&amp;gt;&lt;/code&gt;之外新增如下配置&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;AuthOrder mod_auth_file.c

AuthUserFile /etc/proftpd.passwd

RequireValidShell off
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;获取ftpasswd用于设置虚拟账号&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget http://www.castaglia.org/proftpd/contrib/ftpasswd
&lt;span class=&quot;nb&quot;&gt;mv &lt;/span&gt;ftpasswd /usr/sbin
&lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x /usr/sbin/ftpasswd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;设置账号密码&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ftpasswd &lt;span class=&quot;nt&quot;&gt;--file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/etc/proftpd.passwd &lt;span class=&quot;nt&quot;&gt;--home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxx &lt;span class=&quot;nt&quot;&gt;--shell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/bin/false &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxx &lt;span class=&quot;nt&quot;&gt;--uid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;99 &lt;span class=&quot;nt&quot;&gt;--gid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;99 &lt;span class=&quot;nt&quot;&gt;--passwd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;配置说明&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;--home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxx 指定 ftp 用户登录后的根目录（eg. &lt;span class=&quot;nt&quot;&gt;--home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/home）
&lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xxx 指定 ftp 用户名
&lt;span class=&quot;nt&quot;&gt;--uid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;99 &lt;span class=&quot;nt&quot;&gt;--gid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;99 指定账号关联对应系统用户和组（eg. 99:nobody/501:work 可通过命令id work查看用户对应uid/gid）
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ./ftpasswd &lt;span class=&quot;nt&quot;&gt;--file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/etc/proftpd.passwd &lt;span class=&quot;nt&quot;&gt;--home&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;// &lt;span class=&quot;nt&quot;&gt;--shell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/bin/sh &lt;span class=&quot;nt&quot;&gt;--name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;dwk &lt;span class=&quot;nt&quot;&gt;--uid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;505 &lt;span class=&quot;nt&quot;&gt;--gid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;506 &lt;span class=&quot;nt&quot;&gt;--passwd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;强密码生成命令（仅供参考）：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;strings /dev/urandom |tr &lt;span class=&quot;nt&quot;&gt;-dc&lt;/span&gt; A-Za-z0-9 | &lt;span class=&quot;nb&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;wget获取文件路径以–home指定路径为基础进行拼接（eg. –home=/home; wget ftp://…/home/work =&amp;gt; wget ftp://…/work ）
删除ftpasswd
cd /usr/sbin; rm -f ftpasswd
重启FTP服务(如启动失败，proftpd -t -d5 检查配置文件出错点)&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;service proftpd restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用时：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget ftp://xxxxxxx/x.txt &lt;span class=&quot;nt&quot;&gt;--ftp-user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;dwk &lt;span class=&quot;nt&quot;&gt;--ftp-password&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;dwk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>eigen</title>
   <link href="http://daiwk.github.io/posts/knowledge-eigen.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-eigen</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%a5%e9%97%a8&quot;&gt;入门&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c&quot;&gt;基本操作&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#eigenmkl%e5%8a%a0%e9%80%9f&quot;&gt;eigen+mkl加速&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%b8%b8%e7%94%a8%e5%87%bd%e6%95%b0&quot;&gt;常用函数&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#block&quot;&gt;block&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;入门&quot;&gt;入门&lt;/h2&gt;

&lt;p&gt;安装只需要把&lt;a href=&quot;http://eigen.tuxfamily.org/index.php?title=Main_Page#Download&quot;&gt;http://eigen.tuxfamily.org/index.php?title=Main_Page#Download&lt;/a&gt;的包下载下来，然后解压，把&lt;strong&gt;Eigen&lt;/strong&gt;目录拿出来就行了，不用编译成so什么的！！&lt;/p&gt;

&lt;p&gt;比如是这个目录&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home/work/my_lib/Eigen&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;有github的mirror：&lt;a href=&quot;https://github.com/eigenteam/eigen-git-mirror&quot;&gt;https://github.com/eigenteam/eigen-git-mirror&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;Eigen/Dense&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MatrixXd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MatrixXd&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;编译：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g++ &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt; /home/work/my_lib/ my_program.cpp &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; my_program
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;运行结果：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  3  &lt;span class=&quot;nt&quot;&gt;-1&lt;/span&gt;
2.5 1.5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;基本操作&quot;&gt;基本操作&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://blog.csdn.net/y363703390/article/details/78407346&quot;&gt;https://blog.csdn.net/y363703390/article/details/78407346&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;注意，eigen的vector默认是个列向量：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Eigen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vector3d&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;td&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;here is v1:&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 输出：&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// here is v1:1&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 2&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;向量点积是dot，而cross product只对size为3的vector生效&lt;/p&gt;

&lt;p&gt;VectorXf和MatrixXf可以搞出指定size的向量和矩阵：&lt;/p&gt;

&lt;h2 id=&quot;eigenmkl加速&quot;&gt;eigen+mkl加速&lt;/h2&gt;

&lt;p&gt;首先装一下mkl&lt;/p&gt;

&lt;p&gt;然后在编译的时候加上(我也不知道后面这坨哪个比较有用。。)：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DEIGEN_USE_MKL_ALL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msse&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msse2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msse3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lmkl_core&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lmkl_gf_lp64&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lmkl_intel_lp64&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lmkl_sequential&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lmkl_def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lmkl_mc3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;链接时加上&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ldl&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考的是&lt;a href=&quot;http://eigen.tuxfamily.org/dox-3.2/TopicUsingIntelMKL.html&quot;&gt;http://eigen.tuxfamily.org/dox-3.2/TopicUsingIntelMKL.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-DEIGEN_USE_MKL_ALL&lt;/code&gt;相当于在cpp文件一开头定义如下宏，然后再include相关eigen库：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#define EIGEN_USE_MKL_ALL
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;常用函数&quot;&gt;常用函数&lt;/h2&gt;

&lt;h3 id=&quot;block&quot;&gt;block&lt;/h3&gt;

&lt;p&gt;截取从startRow开始blockRows行，以及从startCol开始blockCols列的小矩阵出来。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Block&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startCol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockRows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockCols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Block&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startCol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockRows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockCols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;/** This is the const version of block(Index,Index,Index,Index). */&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Block&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startCol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockRows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Index&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockCols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Block&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startCol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockRows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockCols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>disk</title>
   <link href="http://daiwk.github.io/posts/knowledge-disk.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-disk</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;先看一下现状&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;root@daiwk-dev ~]# &lt;span class=&quot;nb&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda1              40G  9.3G   29G  25% /
tmpfs                 3.9G   11M  3.9G   1% /dev/shm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看看当前有多少磁盘待挂载&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;root@daiwk-dev ~]#  fdisk &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt;

Disk /dev/vda: 42.9 GB, 42949672960 bytes
16 heads, 63 sectors/track, 83220 cylinders
Units &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; cylinders of 1008 &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; 512 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 516096 bytes
Sector size &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;logical/physical&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 512 bytes / 512 bytes
I/O size &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;minimum/optimal&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 512 bytes / 512 bytes
Disk identifier: 0xea92f8b1

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1               1       83220    41942848+  83  Linux

Disk /dev/vdb: 536.9 GB, 536870912000 bytes
16 heads, 63 sectors/track, 1040253 cylinders
Units &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; cylinders of 1008 &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; 512 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 516096 bytes
Sector size &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;logical/physical&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 512 bytes / 512 bytes
I/O size &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;minimum/optimal&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/vdc: 536.9 GB, 536870912000 bytes
16 heads, 63 sectors/track, 1040253 cylinders
Units &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; cylinders of 1008 &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; 512 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 516096 bytes
Sector size &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;logical/physical&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 512 bytes / 512 bytes
I/O size &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;minimum/optimal&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/vdd: 536.9 GB, 536870912000 bytes
16 heads, 63 sectors/track, 1040253 cylinders
Units &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; cylinders of 1008 &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; 512 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 516096 bytes
Sector size &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;logical/physical&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 512 bytes / 512 bytes
I/O size &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;minimum/optimal&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: 512 bytes / 512 bytes
Disk identifier: 0x00000000

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;格式化掉，要等一段时间&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;root@daiwk-dev ~]# mkfs.ext4 /dev/vdb
mke2fs 1.41.12 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;17-May-2010&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Filesystem &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
OS &lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;: Linux
Block &lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4096 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Fragment &lt;span class=&quot;nv&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4096 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;Stride&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 blocks, Stripe &lt;span class=&quot;nv&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 blocks
65536000 inodes, 131072000 blocks
6553600 blocks &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;5.00%&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; reserved &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;the super user
First data &lt;span class=&quot;nv&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0
Maximum filesystem &lt;span class=&quot;nv&quot;&gt;blocks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;4294967296
4000 block &lt;span class=&quot;nb&quot;&gt;groups
&lt;/span&gt;32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
	102400000

Writing inode tables: &lt;span class=&quot;k&quot;&gt;done
&lt;/span&gt;Creating journal &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;32768 blocks&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: &lt;span class=&quot;k&quot;&gt;done
&lt;/span&gt;Writing superblocks and filesystem accounting information:
&lt;span class=&quot;k&quot;&gt;done

&lt;/span&gt;This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; or &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; to override.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;挂载到/home/work下：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;root@daiwk-dev ~]# mount &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; ext4 /dev/vdb /home/
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;root@daiwk-dev ~]# &lt;span class=&quot;nb&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda1              40G   14G   25G  35% /
tmpfs                 3.9G   11M  3.9G   1% /dev/shm
/dev/vdb              485G   70M  460G   1% /home
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;编辑配置文件使云盘开机自动挂载  （注意，文件格式错误会导致机器重启失败。）&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim /etc/fstab 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;加入一行&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/dev/vdc        /home   auto    defaults,nofail,comment=cloudconfig     0       2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;同样地，可以挂一下vdc，但不要也挂到/home/work下，可以挂到/home/disk1下&lt;/p&gt;

&lt;p&gt;如果不慎将/dev/vdc 挂载到了 /home/，导致原来数据被隐藏，可以用root用户&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim /etc/mtab
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;将最底下的一行&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/dev/vdc /home ext4 rw 0 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;删除，然后&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; /home/disk1
mount &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; ext4 /dev/vdc /home/disk1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样原来的数据就可以找回来，并且将新盘挂载到了/home/disk1目录&lt;/p&gt;

&lt;p&gt;另外，可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;df -Th&lt;/code&gt;把Type打出来&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;root@daiwk-dev ~]# &lt;span class=&quot;nb&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-Th&lt;/span&gt;
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/vda1     ext4     40G   14G   25G  35% /
tmpfs        tmpfs    3.9G   11M  3.9G   1% /dev/shm
/dev/vdb      ext4    485G  1.7G  458G   1% /home
/dev/vdc      ext4    485G   70M  460G   1% /home/disk1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>cuda</title>
   <link href="http://daiwk.github.io/posts/knowledge-cuda.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-cuda</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;首先，下载 cuda 的 driver，例如440.33.01版本的 driver：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;https://www.nvidia.com/download/driverResults.aspx/154570
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后用root安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sh NVIDIA-Linux-x86_64-440.33.01.run &lt;span class=&quot;nt&quot;&gt;--no-kernel-module&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后应该就可以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nvidia-smi&lt;/code&gt;了，只是出来的可能是：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;NVIDIA-SMI 440.33.01    Driver Version: 440.33.01    CUDA Version: N/A 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后，安装 cuda，例如440.33.01版本对应的 cuda10.2：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;https://developer.nvidia.com/cuda-10.2-download-archive?target_os&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;Linux&amp;amp;target_arch&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x86_64&amp;amp;target_distro&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;CentOS&amp;amp;target_version&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;6&amp;amp;target_type&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;runfilelocal
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也就是：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_rhel6.run &lt;span class=&quot;nt&quot;&gt;--no-check-certificate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后用root(加tmpdir 是因为默认看的是/tmp，很容易满的)：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sh cuda_10.2.89_440.33.01_rhel6.run &lt;span class=&quot;nt&quot;&gt;-tmpdir&lt;/span&gt; /home/work/tmp &lt;span class=&quot;nt&quot;&gt;--silent&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--toolkit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再然后：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;work@xxx.xxx ~]&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt; nvcc &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;
nvcc: NVIDIA &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;R&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; Cuda compiler driver
Copyright &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_19:24:38_PDT_2019
Cuda compilation tools, release 10.2, V10.2.89
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>ctags</title>
   <link href="http://daiwk.github.io/posts/knowledge-ctags.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-ctags</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#ctags&quot;&gt;ctags&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;ctags&quot;&gt;ctags&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/zl-graduate/p/5777711.html&quot;&gt;https://www.cnblogs.com/zl-graduate/p/5777711.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;首先，执行&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ctags &lt;span class=&quot;nt&quot;&gt;-R&lt;/span&gt; ./&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;【!!!注意：不是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ctags ./* -R&lt;/code&gt;!!!】&lt;/p&gt;

&lt;p&gt;在vim打开源码时，指定tags文件，才可正常使用，通常手动指定，在vim命令行输入：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set &lt;span class=&quot;nv&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/xxx/path/tags&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;当前路径下的tags文件&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者，设置 ~/.vimrc，加入一行，则不用手动设置tags路径：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/xxx/path/tags
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;若要加入系统函数或全局变量的tag标签，则需执行：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ctags &lt;span class=&quot;nt&quot;&gt;-I&lt;/span&gt; __THROW &lt;span class=&quot;nt&quot;&gt;--file-scope&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;yes&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--langmap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;c:+.h &lt;span class=&quot;nt&quot;&gt;--languages&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;c,c++ &lt;span class=&quot;nt&quot;&gt;--links&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;yes&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--c-kinds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;+p &lt;span class=&quot;nt&quot;&gt;--fields&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;+s &lt;span class=&quot;nt&quot;&gt;-R&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; /home/work/.vim/systags /usr/include /usr/local/include /opt/compiler/gcc-4.8.2/include/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;并且在~/.vimrc中添加（亦可用上面描述的手动加入的方式）：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;tags+&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;~/.vim/systags
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样，便可以享受系统库函数名补全、原型预览等功能了。&lt;/p&gt;

&lt;p&gt;常用命令如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Ctrl + ]：跳转到变量或函数的定义处，或者用命令&lt;/li&gt;
  &lt;li&gt;Ctrl + o/t：返回到跳转前的位置&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>合辑</title>
   <link href="http://daiwk.github.io/posts/knowledge-collections.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-collections</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;p&gt;本文pdf：&lt;a href=&quot;https://daiwk.github.io/assets/collections.pdf&quot;&gt;https://daiwk.github.io/assets/collections.pdf&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>c++有用的特性</title>
   <link href="http://daiwk.github.io/posts/knowledge-c++-useful-features.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-c++-useful-features</id>
   <content type="html">&lt;p&gt;目录
&lt;!-- TOC --&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#字符串相关&quot;&gt;字符串相关&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#缓冲区溢出问题&quot;&gt;缓冲区溢出问题&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#strncpystrncat&quot;&gt;strncpy/strncat&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#snprintf&quot;&gt;snprintf&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#各种容器&quot;&gt;各种容器&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#map与unordered-map对比&quot;&gt;map与unordered map对比&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#各种智能指针&quot;&gt;各种智能指针&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#unique_ptr&quot;&gt;unique_ptr&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#shared_ptr&quot;&gt;shared_ptr&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#weak_ptr&quot;&gt;weak_ptr&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#各种多线程&quot;&gt;各种多线程&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#thread基本用法&quot;&gt;thread基本用法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#thread_local&quot;&gt;thread_local&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#atomic&quot;&gt;atomic&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#unique_lock与lock_guard&quot;&gt;unique_lock与lock_guard&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#其他tips&quot;&gt;其他tips&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#值引用指针&quot;&gt;值/引用/指针&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#避免大struct的拷贝&quot;&gt;避免大struct的拷贝&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#builtin相关&quot;&gt;builtin相关&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;字符串相关&quot;&gt;字符串相关&lt;/h2&gt;

&lt;h3 id=&quot;缓冲区溢出问题&quot;&gt;缓冲区溢出问题&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/clover-toeic/p/3737011.html&quot;&gt;https://www.cnblogs.com/clover-toeic/p/3737011.html&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;strncpystrncat&quot;&gt;strncpy/strncat&lt;/h4&gt;

&lt;p&gt;该对函数是strcpy/strcat调用的“安全”版本，但仍存在一些问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;strncpy和strncat要求程序员给出剩余的空间，而不是给出缓冲区的总大小。缓冲区大小一经分配就不再变化，但缓冲区中剩余的空间量会在每次添加或删除数据时发生变化。这意味着程序员需始终跟踪或重新计算剩余的空间，而这种跟踪或重新计算很容易出错。&lt;/li&gt;
  &lt;li&gt;在发生溢出(和数据丢失)时，strncpy和strncat返回结果字符串的起始地址(而不是其长度)。虽然这有利于链式表达，但却无法报告缓冲区溢出。&lt;/li&gt;
  &lt;li&gt;若源字符串长度至少和目标缓冲区相同，则strncpy不会使用NUL来结束字符串；这可能会在以后导致严重破坏。因此，在执行strncpy后通常需要手工终止目标字符串。&lt;/li&gt;
  &lt;li&gt;strncpy还可复制源字符串的一部分到目标缓冲区，要复制的字符数目通常基于源字符串的相关信息来计算。这种操作也会产生未终止字符串。&lt;/li&gt;
  &lt;li&gt;strncpy会在源字符串结束时使用NUL来填充整个目标缓冲区，这在源字符串较短时存在性能问题。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;snprintf&quot;&gt;snprintf&lt;/h4&gt;

&lt;p&gt;sprintf使用控制字符串来指定输出格式，该字符串通常包括”%s”(字符串输出)。若指定字符串输出的精确指定符，则可通过指定输出的最大长度来防止缓冲区溢出(如%.10s将复制不超过10个字符)。也可以使用”&lt;em&gt;“作为精确指定符(如”%.&lt;/em&gt;s”)，这样就可传入一个最大长度值。精确字段仅指定一个参数的最大长度，但缓冲区需要针对组合起来的数据的最大尺寸调整大小。&lt;/p&gt;

&lt;p&gt;“字段宽度”(如”%10s”，无点号)仅指定最小长度——而非最大长度，从而留下缓冲区溢出隐患。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/52php/p/5724390.html&quot;&gt;https://www.cnblogs.com/52php/p/5724390.html&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;snprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;restrict&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;restrict&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;函数说明：&lt;strong&gt;最多&lt;/strong&gt;从源串中拷贝&lt;strong&gt;n－1&lt;/strong&gt;个字符到目标串中，然后&lt;strong&gt;再在后面加一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'\0'&lt;/code&gt;&lt;/strong&gt;。所以如果&lt;strong&gt;目标串的大小为 n 的话，将不会溢出&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;函数返回值：若成功则返回欲写入的字符串长度(而不是实际写入的字符串度)，若出错则返回负值。&lt;/p&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;snprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0123456789012345678&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;str=%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;各种容器&quot;&gt;各种容器&lt;/h2&gt;

&lt;h3 id=&quot;map与unordered-map对比&quot;&gt;map与unordered map对比&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/ljp1919/article/details/50463761&quot;&gt;https://blog.csdn.net/ljp1919/article/details/50463761&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;效率上：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;boost::unordered_map （34s）插入比map(49s)快。&lt;/li&gt;
  &lt;li&gt;boost::unordered_map （15s）查找操作比map(23s)快。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;内存空间占用上：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;boost::unordered_map 内存占用26%。7.6GB*0.26=1.976GB。&lt;/li&gt;
  &lt;li&gt;map内存占用29%。7.6GB*0.29=2.2GB。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;所以，&lt;strong&gt;在效率和内存占用上面，boost::unordered_map 都更具有优势&lt;/strong&gt;。&lt;/p&gt;

&lt;h2 id=&quot;各种智能指针&quot;&gt;各种智能指针&lt;/h2&gt;

&lt;h3 id=&quot;unique_ptr&quot;&gt;unique_ptr&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/weixin_40081916/article/details/79377564&quot;&gt;https://blog.csdn.net/weixin_40081916/article/details/79377564&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;auto_ptr&lt;/strong&gt;是用于C++11之前的智能指针。由于 auto_ptr 基于排他所有权模式：两个指针不能指向同一个资源，&lt;strong&gt;复制或赋值都会改变资源的所有权。&lt;/strong&gt;auto_ptr 主要有两大问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;复制和赋值会改变资源的所有权&lt;/strong&gt;，不符合人的直觉。&lt;/li&gt;
  &lt;li&gt;在 STL 容器中无法使用auto_ptr ，&lt;strong&gt;因为容器内的元素&lt;/strong&gt;必需支持&lt;strong&gt;可复制（copy constructable）&lt;/strong&gt;和&lt;strong&gt;可赋值（assignable）&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;unique_ptr特性&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;拥有&lt;/strong&gt;它所指向的&lt;strong&gt;对象&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;无法进行复制构造&lt;/strong&gt;，也&lt;strong&gt;无法进行复制赋值&lt;/strong&gt;操作&lt;/li&gt;
  &lt;li&gt;保存指向某个对象的指针，当&lt;strong&gt;它本身离开作用域时&lt;/strong&gt;会&lt;strong&gt;自动释放它指向的对象&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;unique_ptr可以：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;为&lt;strong&gt;动态申请的内存&lt;/strong&gt;提供&lt;strong&gt;异常安全&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;将动态申请&lt;strong&gt;内存的所有权传递给某个函数&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;从某个函数返回动态申请内存的所有权&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;在&lt;strong&gt;容器中保存指针&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;unique_ptr十分依赖于&lt;strong&gt;右值引用&lt;/strong&gt;和&lt;strong&gt;移动语义&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;在C++11中已经&lt;strong&gt;放弃auto_ptr&lt;/strong&gt;转而&lt;strong&gt;推荐使用unique_ptr和shared_ptr&lt;/strong&gt;。unique跟auto_ptr类似同样只能有一个智能指针对象指向某块内存，但它还有些其他特性。unique_ptr对auto_ptr的改进如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;auto_ptr支持拷贝构造与赋值操作，但unique_ptr不直接支持&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;auto_ptr通过拷贝构造或者operator=赋值后，对象所有权转移到新的auto_ptr中去了，&lt;strong&gt;原来的auto_ptr对象就不再有效&lt;/strong&gt;，这点不符合人的直觉。unique_ptr则直接禁止了拷贝构造与赋值操作。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;unique_ptr可以用在函数返回值中&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;unique_ptr像上面这样一般意义上的复制构造和赋值或出错，但在函数中作为返回值却可以用&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;unique_ptr可做为容器元素&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;我们知道auto_ptr不可做为容器元素，会导致编译错误。虽然unique_ptr同样&lt;strong&gt;不能直接做为容器元素&lt;/strong&gt;，但可以&lt;strong&gt;通过move语义实现&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;unique_ptr持有对对象的独有权，&lt;strong&gt;同一时刻只能有一个&lt;/strong&gt;unique_ptr指向给定对象（通过&lt;strong&gt;禁止拷贝&lt;/strong&gt;语义、&lt;strong&gt;只有移动&lt;/strong&gt;语义来实现）。&lt;/p&gt;

&lt;p&gt;unique_ptr指针本身的生命周期：从unique_ptr指针创建时开始，直到离开作用域。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;离开作用域时&lt;/strong&gt;，&lt;strong&gt;若其指向对象&lt;/strong&gt;，则&lt;strong&gt;将其所指对象销毁&lt;/strong&gt;(默认使用delete操作符，用户可指定其他操作)。&lt;/p&gt;

&lt;p&gt;类似shared_ptr，用unique_ptr管理非new对象、没有析构函数的类时，需要向unique_ptr传递一个删除器。不同的是，unique_ptr管理删除器的方式，我们必须在尖括号中unique_ptr指向类型后面提供删除器的类型，在创建或reset一个这种unique_ptr对象时，必须提供一个相同类型的可调用对象（删除器），这个删除器接受一个T*参数。&lt;/p&gt;

&lt;p&gt;unique_ptr不允许两个独占指针指向同一个对象，在没有裸指针的情况下，我们只能用release获取内存的地址，同时放弃对对象的所有权，这样就有效避免了多个独占指针同时指向一个对象。 而使用裸指针就很容易打破这一点。&lt;/p&gt;

&lt;p&gt;在调用u.release()时是不会释放u所指的内存的，这时返回值就是对这块内存的唯一索引，如果没有使用这个返回值释放内存或是保存起来，这块内存就泄漏了。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/qq_33266987/article/details/78784286&quot;&gt;https://blog.csdn.net/qq_33266987/article/details/78784286&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;shared_ptr&quot;&gt;shared_ptr&lt;/h3&gt;

&lt;p&gt;shared_ptr允许&lt;strong&gt;多个&lt;/strong&gt;该智能指针共享地“拥有”同一堆分配对象的内存，这&lt;strong&gt;通过引用计数（reference counting）实现&lt;/strong&gt;，会记录有多少个shared_ptr共同指向一个对象，&lt;strong&gt;一旦最后一个这样的指针被销毁&lt;/strong&gt;，也就是一旦某个对象的&lt;strong&gt;引用计数变为0&lt;/strong&gt;，&lt;strong&gt;这个对象会被自动删除。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;shared_ptr的额外开销：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;shared_ptr对象除了包括一个所拥有对象的指针外, 还必须包括一个引用计数代理对象的指针.&lt;/li&gt;
  &lt;li&gt;时间上的开销主要在初始化和拷贝操作上, *和-&amp;gt;操作符重载的开销跟auto_ptr是一样.&lt;/li&gt;
  &lt;li&gt;开销并不是我们不使用shared_ptr的理由, 永远不要进行不成熟的优化, 直到性能分析器告诉你这一点.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;使用：&lt;/p&gt;

&lt;p&gt;可以使用模板函数 make_shared 创建对象, make_shared 需指定类型(‘&amp;lt;&amp;gt;’中)及参数(‘()’内), 传递的参数必须与指定的类型的构造函数匹配:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sp1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sp2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello c++&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;也可以定义 auto 类型的变量来保存 make_shared 的结果。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sp3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;sp3=%d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sp3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sp4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;C++11&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;sp4=%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sp4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;成员函数：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;use_count 返回引用计数的个数&lt;/li&gt;
  &lt;li&gt;unique 返回是否是独占所有权( use_count 为 1)&lt;/li&gt;
  &lt;li&gt;swap 交换两个 shared_ptr 对象(即交换所拥有的对象)&lt;/li&gt;
  &lt;li&gt;reset 放弃内部对象的所有权或拥有对象的变更, 会引起原有对象的引用计数的减少&lt;/li&gt;
  &lt;li&gt;get 返回内部对象(指针), 由于已经重载了()方法, 因此和直接使用对象是一样的.如 shared_ptr&lt;int&gt; sp(new int(1)); sp 与 sp.get()是等价的&lt;/int&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;shared_ptr是线程安全的，但get函数不是：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.zhihu.com/question/56836057&quot;&gt;https://www.zhihu.com/question/56836057&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/lsgxeva/p/7788061.html&quot;&gt;https://www.cnblogs.com/lsgxeva/p/7788061.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/diysoul/p/5930361.html&quot;&gt;https://www.cnblogs.com/diysoul/p/5930361.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;当做函数返回值的时候，返回值就好，不要返回引用 ！！！！&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/24440613/c-how-to-return-a-shared-ptr-from-function&quot;&gt;https://stackoverflow.com/questions/24440613/c-how-to-return-a-shared-ptr-from-function&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://stackoverflow.com/questions/10643563/how-to-return-smart-pointers-shared-ptr-by-reference-or-by-value&quot;&gt;https://stackoverflow.com/questions/10643563/how-to-return-smart-pointers-shared-ptr-by-reference-or-by-value&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;weak_ptr&quot;&gt;weak_ptr&lt;/h3&gt;

&lt;p&gt;weak_ptr是为配合shared_ptr而引入的一种智能指针来协助shared_ptr工作，它可以从一个shared_ptr或另一个weak_ptr对象构造，&lt;strong&gt;它的构造和析构不会引起引用计数的增加或减少&lt;/strong&gt;。没有重载 * 和 -&amp;gt; 但可以&lt;strong&gt;使用lock获得一个可用的shared_ptr对象&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;weak_ptr的使用更为复杂一点，它可以指向shared_ptr指针指向的对象内存，&lt;strong&gt;却并不拥有该内存&lt;/strong&gt;，而使用weak_ptr成员lock，则可返回其指向内存的一个share_ptr对象，且&lt;strong&gt;在所指对象内存已经无效时，返回指针空值nullptr&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;小结一下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;weak_ptr并不拥有资源的所有权，所以不能直接使用资源。&lt;/li&gt;
  &lt;li&gt;可以从一个weak_ptr构造一个shared_ptr以取得共享资源的所有权。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;各种多线程&quot;&gt;各种多线程&lt;/h2&gt;

&lt;h3 id=&quot;thread基本用法&quot;&gt;thread基本用法&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/wangguchangqing/p/6134635.html&quot;&gt;https://www.cnblogs.com/wangguchangqing/p/6134635.html&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;thread_local&quot;&gt;thread_local&lt;/h3&gt;

&lt;p&gt;参考&lt;a href=&quot;https://www.cnblogs.com/pop-lar/p/5123014.html&quot;&gt;https://www.cnblogs.com/pop-lar/p/5123014.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;thread_local变量是C++ 11新引入的一种存储类型。它会影响变量的存储周期(Storage duration)，C++中有4种存储周期：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;automatic&lt;/li&gt;
  &lt;li&gt;static&lt;/li&gt;
  &lt;li&gt;dynamic&lt;/li&gt;
  &lt;li&gt;thread&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;有且只有thread_local关键字修饰的变量具有线程周期(thread duration)，这些变量(或者说对象）在线程开始的时候被生成(allocated)，在线程结束的时候被销毁(deallocated)。并且每 一个线程都拥有一个独立的变量实例(Each thread has its own instance of the object)。thread_local 可以和static 与 extern关键字联合使用，这将影响变量的链接属性(to adjust linkage)。&lt;/p&gt;

&lt;p&gt;那么，哪些变量可以被声明为thread_local？以下3类都是ok的&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;命名空间下的全局变量&lt;/li&gt;
  &lt;li&gt;类的static成员变量&lt;/li&gt;
  &lt;li&gt;本地变量&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;既然每个线程都拥有一份独立的thread_local变量，那么就有2个问题需要考虑：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;各线程的thread_local变量是如何初始化的&lt;/li&gt;
  &lt;li&gt;各线程的thread_local变量在初始化之后拥有怎样的生命周期，特别是被声明为thread_local的本地变量(local variables)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;atomic&quot;&gt;atomic&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;http://zh.cppreference.com/w/cpp/atomic/atomic&quot;&gt;http://zh.cppreference.com/w/cpp/atomic/atomic&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;unique_lock与lock_guard&quot;&gt;unique_lock与lock_guard&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.2cto.com/kf/201706/649733.html&quot;&gt;https://www.2cto.com/kf/201706/649733.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;C++多线程编程中通常会对共享的数据进行写保护，以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为。通常的做法是在修改共享数据成员的时候进行加锁–mutex。在使用锁的时候通常是在&lt;strong&gt;对共享数据进行修改之前进行lock操作&lt;/strong&gt;，在&lt;strong&gt;写完之后再进行unlock操作&lt;/strong&gt;，经常会出现由于疏忽，导致由于lock之后，&lt;strong&gt;在离开共享成员操作区域时忘记unlock，导致死锁。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;针对以上的问题，C++11中引入了std::unique_lock与std::lock_guard两种数据结构。通过对lock和unlock进行一次薄的封装，实现&lt;strong&gt;自动unlock&lt;/strong&gt;的功能。&lt;/p&gt;

&lt;h2 id=&quot;其他tips&quot;&gt;其他tips&lt;/h2&gt;

&lt;h3 id=&quot;值引用指针&quot;&gt;值/引用/指针&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://segmentfault.com/a/1190000006812825&quot;&gt;https://segmentfault.com/a/1190000006812825&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;避免大struct的拷贝&quot;&gt;避免大struct的拷贝&lt;/h3&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unordered_map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seconds_pass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;test1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;test2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;288&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// sort的时候会涉及大量拷贝&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seconds_pass&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seconds_pass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以改成&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unordered_map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seconds_pass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shared_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;APtr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;APtr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;APtr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_shared&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seconds_pass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;test1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_pair&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;test2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;288&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;APtr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;APtr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seconds_pass&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seconds_pass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;builtin相关&quot;&gt;builtin相关&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://stackoverflow.com/questions/66230707/c-builtin-functions-source-codes&quot;&gt;https://stackoverflow.com/questions/66230707/c-builtin-functions-source-codes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/gcc-mirror/gcc/blob/master/gcc/builtins.def#L317&quot;&gt;https://github.com/gcc-mirror/gcc/blob/master/gcc/builtins.def#L317&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__builtin_strlen&lt;/code&gt;函数就是&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;DEF_LIB_BUILTIN&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BUILT_IN_STRLEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;strlen&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BT_FN_SIZE_CONST_STRING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ATTR_PURE_NOTHROW_NONNULL_LEAF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在厂内的话，就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/compiler/gcc-8.2/lib64/gcc/x86_64-pc-linux-gnu/8.2.0/plugin/include/builtins.def&lt;/code&gt;中&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;DEF_LIB_BUILTIN_CHKP&lt;/span&gt;   &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BUILT_IN_STRLEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;strlen&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BT_FN_SIZE_CONST_STRING&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ATTR_PURE_NOTHROW_NONNULL_LEAF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>基础知识</title>
   <link href="http://daiwk.github.io/posts/knowledge-basic-knowledges.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-basic-knowledges</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#jsonprotobuf%e7%9a%84%e5%9d%91&quot;&gt;json+protobuf的坑&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;jsonprotobuf的坑&quot;&gt;json+protobuf的坑&lt;/h2&gt;

&lt;p&gt;protobuf中的bool类型，如果通过python的json来给：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;aa&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;那pb读出来会是false。。变成0&lt;/p&gt;

&lt;p&gt;所以应该&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;aa&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>常用算法</title>
   <link href="http://daiwk.github.io/posts/knowledge-algorithms.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/knowledge-algorithms</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#蓄水池抽样&quot;&gt;蓄水池抽样&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#轮盘赌&quot;&gt;轮盘赌&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h3 id=&quot;蓄水池抽样&quot;&gt;蓄水池抽样&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/huagong_adu/article/details/7619665&quot;&gt;https://blog.csdn.net/huagong_adu/article/details/7619665&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;从一个包含n个对象的列表S中随机选取k个对象，n为一个非常大或者不知道的值。通常情况下，n是一个非常大的值，大到无法一次性把所有列表S中的对象都放到内存中。&lt;/p&gt;

&lt;p&gt;如果k=1，我们总是选择第一个对象，以1/2的概率选择第二个，以1/3的概率选择第三个，以此类推，以1/m的概率选择第m个对象。当该过程结束时，每一个对象具有相同的选中概率，即1/n，&lt;/p&gt;

&lt;p&gt;证明：第m个对象最终被选中的概率P=选择m的概率*其后面所有对象不被选择的概率&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
P=1/m*m/(m+1)*(m+1)/(m+2)*...*(n-1)/n=1/n
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;对应蓄水池抽样问题，可以类似的思路解决。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;先把读到的前k个对象放入“水库”&lt;/li&gt;
  &lt;li&gt;对于第k+1个对象开始，以k/(k+1)的概率选择该对象，以k/(k+2)的概率选择第k+2个对象，以此类推，以k/m的概率选择第m个对象（m&amp;gt;k）。&lt;/li&gt;
  &lt;li&gt;如果m被选中，则随机替换水库中的一个对象。最终每个对象被选中的概率均为k/n，证明如下。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;证明：第m个对象被选中的概率=选择m的概率*（其后元素不被选择的概率+其后元素被选择的概率*不替换第m个对象的概率），即&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\[
\begin{align*}
P &amp;amp;=\frac{k}{m}*[(\frac{m+1-k}{m+1}+\frac{k}{m+1}*\frac{k-1}{k})*(\frac{m+2-k}{m+2}+\frac{k}{m+2}*\frac{k-1}{k})*...*(\frac{n-k}{n}+\frac{k}{n}*\frac{k-1}{k})] \\ 
 &amp;amp;=k/m*m/n\\
&amp;amp;=k/n
\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;cstdlib&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;ctime&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt; 
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 
&lt;span class=&quot;c1&quot;&gt;// generate a random number between i and k,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// both i and k are inclusive.&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// swap&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;c1&quot;&gt;// take m samples to result from input of n items.&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reservoir_sampling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;srand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
 
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// 对于第i个元素来说，它的概率应该是k/i, 这里的k就是m，也就是从0-i中随机一个数，如果它小于m，那就选中啦&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
 
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reservoir_sampling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;轮盘赌&quot;&gt;轮盘赌&lt;/h3&gt;

&lt;p&gt;蚁群算法作为一种启发式算法，在进行路径选择的过程中，当选择下一目标时，通过轮盘赌概率选择的方式完成，这也&lt;strong&gt;保证了每次循环都能随机的命中概率较大的目标。&lt;/strong&gt;其算法思路如下：&lt;/p&gt;

&lt;p&gt;设P(i)，其中i=1..n，为n个个体被选择的概率，在轮盘上表示为所占扇区的面积百分比，这里显然sum(P)=1。select用来保存n次选择的结果。&lt;/p&gt;

&lt;p&gt;比如轮盘上有3个区域，概率分别是0.4,0.27,0.33，那么可以在[0,1]这条线段上划分出三个区间，两个分隔点是0.4,0.67，三个区间是[0,0.4],[0.4,0.67],[0.67,1]，然后随机一个[0-1]的数，看落在哪个区间，就相当于选中哪个数啦~&lt;/p&gt;

&lt;p&gt;实际应用中，例如对一个每个元素有自己score的N个元素的list，想生成一个重新排序的list。可以进行N次轮盘赌：&lt;/p&gt;

&lt;p&gt;对剩下的元素走softmax归一化一下，算出每个的区间值，然后随机一个数，看在哪个区间里，就意味着选中了这个区间。&lt;/p&gt;

&lt;p&gt;然后把这个区间的那个数扔出去，再对剩下的元素重复上面的过程（softmax，分区间，随机数，选区间，扔掉），直到选出了N个元素的list。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/zheng_zhiwei/article/details/23209729&quot;&gt;https://blog.csdn.net/zheng_zhiwei/article/details/23209729&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/u010807846/article/details/51088750&quot;&gt;https://blog.csdn.net/u010807846/article/details/51088750&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt;&lt;span class=&quot;cpf&quot;&gt;&amp;lt;math.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt;&lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt;&lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;generate_random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//randomly generate number between [0,1]&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RAND_MAX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//printf(&quot;%f\n&quot;, rand_num);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;generate_probability_band&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob_arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//产生概率带&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//传入参数是在main函数中指定的概率分布&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//传出参数就是概率带（积累概率）&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//计算累计概率，保存在sum_array数组中，用malloc写了一下。&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;一共有%d个概率数据&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_array_tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//多一个是为了保存概率带最左边那个0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sum_array_tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prob_arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;sum=%f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//先执行++，目的是把概率带最左边那个0保存在sum_array[0]上&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//printf(&quot;%x--&amp;gt;%f\n&quot;, sum_array,*sum_array);&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//sum_array = sum_array_tmp;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum_array_tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//free(sum_array);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;//判断随机数位于概率带的哪个位置&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;judge_random_location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//count数组用来统计 落在某个概率上的数量&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;//初始化为0&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;sum_array[%d] = %f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//产生1000个随机数&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generate_random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;c1&quot;&gt;//对应的概率带 计数器加1&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//probability array&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prob_arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//按照概率1:2:3:4&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob_arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob_arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generate_probability_band&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob_arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;judge_random_location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\b&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;一共有4个概率数据
&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.100000
&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.300000
&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0.600000
&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1.000000
sum_array[0] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0.000000
sum_array[1] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0.100000
sum_array[2] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0.300000
sum_array[3] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0.600000
sum_array[4] &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 1.000000
96:194:321:389:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>深度学习加速tricks</title>
   <link href="http://daiwk.github.io/posts/dl-speedup-tricks.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/dl-speedup-tricks</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s/Mv7QTTGo3WIsQFmXN4fD5g&quot;&gt;26秒单GPU训练CIFAR10，Jeff Dean也点赞的深度学习优化技巧&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;colab地址：&lt;a href=&quot;https://colab.research.google.com/github/davidcpage/cifar10-fast/blob/master/bag_of_tricks.ipynb&quot;&gt;https://colab.research.google.com/github/davidcpage/cifar10-fast/blob/master/bag_of_tricks.ipynb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;blog地址：&lt;a href=&quot;https://myrtle.ai/how-to-train-your-resnet-8-bag-of-tricks/&quot;&gt;https://myrtle.ai/how-to-train-your-resnet-8-bag-of-tricks/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;主要通过以下几步将时间减小到26s：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;GPU 上进行数据预处理 (70s)&lt;/li&gt;
  &lt;li&gt;更换最大池化顺序 (64s)&lt;/li&gt;
  &lt;li&gt;标签平滑化 (59s)&lt;/li&gt;
  &lt;li&gt;使用 CELU 激活函数 (52s)&lt;/li&gt;
  &lt;li&gt;幽灵批归一化 (46s)&lt;/li&gt;
  &lt;li&gt;固定批归一化的缩放 (43s)&lt;/li&gt;
  &lt;li&gt;输入 patch 白化 (36s)&lt;/li&gt;
  &lt;li&gt;指数移动平均时间 (34s)&lt;/li&gt;
  &lt;li&gt;测试状态增强 (26s)&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>ntm和memory networks</title>
   <link href="http://daiwk.github.io/posts/dl-ntm-memory-networks.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/dl-ntm-memory-networks</id>
   <content type="html">&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#neural-turing-machine&quot;&gt;Neural Turing Machine&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#主要架构&quot;&gt;主要架构&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#memory-networks&quot;&gt;Memory Networks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;区分neural turing machine与memory networks：&lt;a href=&quot;https://blog.csdn.net/seahailang/article/details/78146350&quot;&gt;https://blog.csdn.net/u012436149/article/details/52959593&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;NTM与MN同样都使用了一个&lt;strong&gt;外部记忆单元&lt;/strong&gt;。在NTM中，其最精彩的部分是&lt;strong&gt;基于内容和地址&lt;/strong&gt;的&lt;strong&gt;soft addressing&lt;/strong&gt;，soft addressing&lt;strong&gt;选择性&lt;/strong&gt;的&lt;strong&gt;读取和写入&lt;/strong&gt;记忆单元的&lt;strong&gt;某些位置&lt;/strong&gt;，同时soft addressing 的引入也使得&lt;strong&gt;原来不可微分的寻址过程&lt;/strong&gt;变成了&lt;strong&gt;可微分的过程&lt;/strong&gt;，使得整个模型可以使用基于梯度的方法来进行训练&lt;/li&gt;
  &lt;li&gt;MN相对来说实现比较简单，它比较&lt;strong&gt;类似于attention model&lt;/strong&gt;。&lt;strong&gt;读和写&lt;/strong&gt;的过程都比较&lt;strong&gt;直接&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ntm以及对应的tf源码解析：&lt;a href=&quot;https://blog.csdn.net/u012436149/article/details/52959593&quot;&gt;https://blog.csdn.net/u012436149/article/details/52959593&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;mn以及对应的代码：&lt;a href=&quot;https://blog.csdn.net/u011274209/article/details/53384232&quot;&gt;https://blog.csdn.net/u011274209/article/details/53384232&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;facebook的官方memNN的代码(有lua的torch7，也有tf的实现)：&lt;a href=&quot;https://github.com/facebook/MemNN&quot;&gt;https://github.com/facebook/MemNN&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;neural-turing-machine&quot;&gt;Neural Turing Machine&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1410.5401&quot;&gt;Neural Turing Machines&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NTM的主要思想是，当一个样本输入后，由一个&lt;strong&gt;控制器&lt;/strong&gt;决定从&lt;strong&gt;memory中寻址&lt;/strong&gt;，找到需要读取的内容后将内容返回给控制器，控制器再&lt;strong&gt;结合样本&lt;/strong&gt;和&lt;strong&gt;读取的内容&lt;/strong&gt;去&lt;strong&gt;更新memory&lt;/strong&gt;，并且向外产生一个输出。&lt;/p&gt;

&lt;h3 id=&quot;主要架构&quot;&gt;主要架构&lt;/h3&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/ntm.png&quot; style=&quot;max-height: 200px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;h2 id=&quot;memory-networks&quot;&gt;Memory Networks&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1410.3916&quot;&gt;Memory Networks&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>federated learning/联邦学习</title>
   <link href="http://daiwk.github.io/posts/dl-federated-learning.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/dl-federated-learning</id>
   <content type="html">&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#google2017%e5%b9%b4%e7%9a%84blog&quot;&gt;google2017年的blog&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#paper1-communication-efficient-learning-of-deep-networks-from-decentralized-data&quot;&gt;paper1: Communication-Efficient Learning of Deep Networks from Decentralized Data&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#paper2-federated-learning-strategies-for-improving-communication-efficiency&quot;&gt;paper2: Federated Learning: Strategies for Improving Communication Efficiency&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#paper3-federated-optimization-distributed-machine-learning-for-on-device-intelligence&quot;&gt;paper3: Federated Optimization: Distributed Machine Learning for On-Device Intelligence&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#paper4-practical-secure-aggregation-for-privacy-preserving-machine-learning&quot;&gt;paper4: Practical Secure Aggregation for Privacy Preserving Machine Learning&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#yangqiang%e7%9a%84paper&quot;&gt;yangqiang的paper&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fate&quot;&gt;FATE&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e8%bf%91%e6%9c%9f%e8%bf%9b%e5%b1%95&quot;&gt;近期进展&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#%e5%85%b1%e4%ba%ab%e5%ad%a6%e4%b9%a0&quot;&gt;共享学习&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;google2017年的blog&quot;&gt;google2017年的blog&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://ai.googleblog.com/2017/04/federated-learning-collaborative.html&quot;&gt;https://ai.googleblog.com/2017/04/federated-learning-collaborative.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;联邦学习能不用把数据从mobile传到cloud就可以训模型，也就是说mobile不仅可以做prediction，还可以搞训练。&lt;/p&gt;

&lt;p&gt;大致流程如下，首先一个手机把当前模型下载下来，然后使用本机的数据进行更新，并把这些变化summarize成a small focused update。只有这个update会通过加密上传到cloud，然后这个用户的update会和其他用户的update一起进行平均，拿来更新shared model。所有的训练数据都存在设备上，不会把个人的update存到cloud去。&lt;/p&gt;

&lt;html&gt;
&lt;br /&gt;
&lt;img src=&quot;../assets/FederatedLearning_FinalFiles_Flow Chart1.png&quot; style=&quot;max-width: 300px&quot; /&gt;
&lt;br /&gt;
&lt;/html&gt;

&lt;p&gt;这种方式也有一个immediate benefit，那就是，在给shared model提供update之外，本地的improved model也可以立即在本地生效。&lt;/p&gt;

&lt;p&gt;传统的机器学习系统，在cloud中通过partition分布在异构server上的大数据集上，使用sgd等优化算法。这种高度iterative的算法，要求与训练数据有低延迟、高吞吐的连接。而在federal learning的场景下，明显是更高的延迟、更低的吞吐，而且拿来训练的时候是intermittently（断断续续的）。&lt;/p&gt;

&lt;p&gt;因此有了Federated Averaging algorithm，也就是&lt;a href=&quot;https://arxiv.org/abs/1602.05629&quot;&gt;Communication-Efficient Learning of Deep Networks from Decentralized Data&lt;/a&gt;，能够使用比naively federated version的sgd少10-100倍的连接来训练深度网络。核心思想是使用手机上更强大的处理器，计算比普通的gradient steps更higher quality的update。这样，因为用了high quality的update，所以通过更少的迭代就可以得到一个好的模型了，所以需要的连接也更少了。&lt;/p&gt;

&lt;p&gt;因为上传速度往往比下载速度慢得多(可以参考&lt;a href=&quot;https://www.speedtest.net/reports/united-states/&quot;&gt;https://www.speedtest.net/reports/united-states/&lt;/a&gt;)，可以采用random rotation和quantization来得到压缩后的updates，能够降低100倍的上传耗时，参考&lt;a href=&quot;https://arxiv.org/abs/1610.05492&quot;&gt;Federated Learning: Strategies for Improving Communication Efficiency&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;而对于高维稀疏的凸模型，例如ctr预估问题，可以参考&lt;a href=&quot;https://arxiv.org/abs/1610.02527&quot;&gt;Federated Optimization: Distributed Machine Learning for On-Device Intelligence&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;部署需要一个复杂的技术栈，on device training需要一个miniature（小型的）版本的tf，调度需要仔细的设计，保证当设备处于idle、充电中、有wifi等场景下，保证对用户的体验没有影响。&lt;/p&gt;

&lt;p&gt;通信与聚合要求是一种secure, efficient, scalable, and fault-tolerant的方式，所以只有包括了这些的infrastructure可以被考虑使用。&lt;/p&gt;

&lt;p&gt;参考&lt;a href=&quot;https://eprint.iacr.org/2017/281&quot;&gt;https://eprint.iacr.org/2017/281&lt;/a&gt;，提出了&lt;a href=&quot;https://eprint.iacr.org/2017/281.pdf&quot;&gt;Practical Secure Aggregation for Privacy Preserving Machine Learning&lt;/a&gt;，也就是一种Secure Aggregation protocol，一个coordinating server只有当100s或者1000s的用户参与了，才会对average update进行解密，因此在average之前，没有哪个个人手机的update能被观察(inspected)到。所以coordinating server只需要average update，使得Secure Aggregation能被使用。不过这个协议还没有实现出来，有希望在near future得以部署。&lt;/p&gt;

&lt;p&gt;目前的研究还只是皮毛，联邦学习解决不了不是所有机器学习问题，例如，&lt;a href=&quot;https://ai.googleblog.com/2016/08/improving-inception-and-image.html&quot;&gt;https://ai.googleblog.com/2016/08/improving-inception-and-image.html&lt;/a&gt;提到的在carefully标注的数据集上识别不同狗的种类的问题，或者是很多训练数据早已存储在cloud的问题(例如训练垃圾邮件识别问题)。所以，cloud-based的ml会持续发展，而federated learning也会持续寻求解决更多问题。&lt;/p&gt;

&lt;h3 id=&quot;paper1-communication-efficient-learning-of-deep-networks-from-decentralized-data&quot;&gt;paper1: Communication-Efficient Learning of Deep Networks from Decentralized Data&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1602.05629&quot;&gt;Communication-Efficient Learning of Deep Networks from Decentralized Data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://column.hongliangjie.com/%E8%AF%BB%E8%AE%BA%E6%96%87/2017/06/18/aistats2017-dist-sgd/&quot;&gt;http://column.hongliangjie.com/%E8%AF%BB%E8%AE%BA%E6%96%87/2017/06/18/aistats2017-dist-sgd/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;FederatedAveraging算法总共有三个基本的参数，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;（0到1）控制有多少比例的的客户端参与优化，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E\)&lt;/code&gt;控制每一轮多少轮SGD需要在客户端运行，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(B\)&lt;/code&gt;是每一轮的Mini-Batch的数目大小。另外，假设总共有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;个客户端。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;每一轮都&lt;strong&gt;随机选择&lt;/strong&gt;出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(\max (CK,1)\)&lt;/code&gt;个的客户端&lt;/li&gt;
  &lt;li&gt;对于&lt;strong&gt;每个客户端&lt;/strong&gt;进行&lt;strong&gt;Mini-Batch&lt;/strong&gt;的大小为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(B\)&lt;/code&gt;，&lt;strong&gt;轮数&lt;/strong&gt;为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\(E\)&lt;/code&gt;的SGD更新&lt;/li&gt;
  &lt;li&gt;对于参数直接进行&lt;strong&gt;加权平均&lt;/strong&gt;（这里的&lt;strong&gt;权重&lt;/strong&gt;是每个&lt;strong&gt;客户端的数据相对大小&lt;/strong&gt;）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;文章对这里的最后一步进行了说明。之前有其他研究表明，如何直接对参数空间进行加权平均，特别是Non-Convex的问题，会得到任意坏的结果。这篇文章里，作者们对于这样的问题的处理是，让&lt;strong&gt;每一轮&lt;/strong&gt;的&lt;strong&gt;各个客户端&lt;/strong&gt;的&lt;strong&gt;起始参数值相同&lt;/strong&gt;（也就是&lt;strong&gt;前一轮的全局参数值&lt;/strong&gt;）。这一步使得算法效果大幅度提高。&lt;/p&gt;

&lt;h3 id=&quot;paper2-federated-learning-strategies-for-improving-communication-efficiency&quot;&gt;paper2: Federated Learning: Strategies for Improving Communication Efficiency&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1610.05492&quot;&gt;Federated Learning: Strategies for Improving Communication Efficiency&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;使用两种update来减少通信的耗时：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;structured updates: 在一个使用&lt;strong&gt;更少的变量&lt;/strong&gt;（例如low-rank或者随机mask）来参数化表示的&lt;strong&gt;受限空间内&lt;/strong&gt;，&lt;strong&gt;直接学习&lt;/strong&gt;一个&lt;strong&gt;update&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;sketched updates: 学习&lt;strong&gt;整个模型的update&lt;/strong&gt;，并在发送给server之前，使用&lt;strong&gt;quantization, random rotations, and subsampling&lt;/strong&gt;来进行&lt;strong&gt;压缩&lt;/strong&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;paper3-federated-optimization-distributed-machine-learning-for-on-device-intelligence&quot;&gt;paper3: Federated Optimization: Distributed Machine Learning for On-Device Intelligence&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1610.02527&quot;&gt;Federated Optimization: Distributed Machine Learning for On-Device Intelligence&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;提到了SVRG(Stochastic Variance Reduced Gradient)、 DANE(Distributed Approximate Newton algorithm)，然后提出了Federated SVRG(FSVRG)。&lt;/p&gt;

&lt;h3 id=&quot;paper4-practical-secure-aggregation-for-privacy-preserving-machine-learning&quot;&gt;paper4: Practical Secure Aggregation for Privacy Preserving Machine Learning&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://eprint.iacr.org/2017/281.pdf&quot;&gt;Practical Secure Aggregation for Privacy Preserving Machine Learning&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;yangqiang的paper&quot;&gt;yangqiang的paper&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1902.04885.pdf&quot;&gt;Federated Machine Learning: Concept and Applications&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;fate&quot;&gt;FATE&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650765998&amp;amp;idx=4&amp;amp;sn=a6fdc4c39e29e0260dc06779bceed6ad&amp;amp;chksm=871abed0b06d37c617a063ba4c98867658f981b4fae6d34e0f2785edebeae8a09b6a2cef22e7&amp;amp;mpshare=1&amp;amp;scene=1&amp;amp;srcid=&amp;amp;pass_ticket=zzUnWIgdqTLvX39vSLCKaOJN8KVDYuvxPgj7h5mQNNMiTnEMdrWSwBJSd3ch3aLL#rd&quot;&gt;怎样扩充大数据？你需要了解的第一个联邦学习开源框架FATE&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github：&lt;a href=&quot;https://github.com/WeBankFinTech/FATE&quot;&gt;https://github.com/WeBankFinTech/FATE&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;近期进展&quot;&gt;近期进展&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/s4E9jM_HmOf9G4m0TIy61Q&quot;&gt;打破数据孤岛：联邦学习近期重要研究进展&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;经典的联邦学习问题基于存储在数千万至数百万远程客户端设备上的数据学习全局模型。在训练过程中，客户端设备需要周期性地与中央服务器进行通信。目前，联邦学习面临的难点主要包括四个方面：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;高昂的通信代价。在联邦学习问题中，原始数据保存在远程客户端设备本地，必须与中央服务器不断交互才能完成全局模型的构建。通常整个联邦学习网络可能包含了大量的设备，网络通信速度可能比本地计算慢许多个数量级，这就造成高昂的通信代价成为了联邦学习的关键瓶颈。&lt;/li&gt;
  &lt;li&gt;系统异质性。由于客户端设备硬件条件（CPU、内存）、网络连接（3G、4G、5G、WiFi）和电源（电池电量）的变化，联邦学习网络中每个设备的存储、计算和通信能力都有可能不同。网络和设备本身的限制可能导致某一时间仅有一部分设备处于活动状态。此外，设备还会出现没电、网络无法接入等突发状况，导致瞬时无法连通。这种异质性的系统架构影响了联邦学习整体策略的制定。&lt;/li&gt;
  &lt;li&gt;统计异质性。设备通常以不同分布方式在网络上生成和收集数据，跨设备的数据数量、特征等可能有很大的变化，因此联邦学习网络中的数据为非独立同分布（Non-indepent and identically distributed, Non-IID）的。目前，主流机器学习算法主要是基于 IID 数据的假设前提推导建立的。因此，异质性的 Non-IID 数据特征给建模、分析和评估都带来了很大挑战。&lt;/li&gt;
  &lt;li&gt;隐私问题。联邦学习共享客户端设备中的模型参数更新（例如梯度信息）而不是原始数据，因此在数据隐私保护方面优于其他的分布式学习方法。然而，在训练过程中传递模型的更新信息仍然存在向第三方或中央服务器暴露敏感信息的风险。隐私保护成为联邦学习需要重点考虑的问题。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4篇paper：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1804.08333&quot;&gt;Client selection for federated learning with heterogeneous resources in mobile edge&lt;/a&gt;, 提出了一个用于机器学习的移动边缘计算框架，它利用分布式客户端数据和计算资源来训练高性能机器学习模型，同时保留客户端隐私；&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1902.00146v1&quot;&gt;Agnostic Federated Learning&lt;/a&gt;，解决之前联邦学习机制中会对某些客户端任务发生倾斜的问题；&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.12022v1&quot;&gt;Bayesian Nonparametric Federated Learning of Neural Networks&lt;/a&gt;. ICML 2019. 提出单样本/少样本探索式的学习方法来解决通信问题；&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/pdf/1812.00984.pdf&quot;&gt;Protection Against Reconstruction and Its Applications in Private Federated Learning&lt;/a&gt;，提出了一种差异性隐私保护方法。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;共享学习&quot;&gt;共享学习&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/VGK_V-ZW1IPmxWYu7RYYug&quot;&gt;共享学习：蚂蚁金服提出全新数据孤岛解决方案&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>dl tips</title>
   <link href="http://daiwk.github.io/posts/dl-dl-tips.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/dl-dl-tips</id>
   <content type="html">&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#dl基础概念&quot;&gt;dl基础概念&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#个问题&quot;&gt;个问题&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#i-数据集问题&quot;&gt;I. 数据集问题&lt;/a&gt;
      - &lt;a href=&quot;#检查你的输入数据&quot;&gt;检查你的输入数据&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#尝试随机输入&quot;&gt;尝试随机输入&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#检查数据加载器&quot;&gt;检查数据加载器&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#确保输入与输出相关联&quot;&gt;确保输入与输出相关联&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#输入与输出之间的关系是否太随机&quot;&gt;输入与输出之间的关系是否太随机？&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#数据集中是否有太多的噪音&quot;&gt;数据集中是否有太多的噪音？&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#shuffle-数据集&quot;&gt;Shuffle 数据集&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#减少类别失衡&quot;&gt;减少类别失衡&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#你有足够的训练实例吗&quot;&gt;你有足够的训练实例吗？&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#确保你采用的批量数据不是单一标签&quot;&gt;确保你采用的批量数据不是单一标签&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#缩减批量大小&quot;&gt;缩减批量大小&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ii-数据归一化增强&quot;&gt;II. 数据归一化/增强&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#归一化特征&quot;&gt;归一化特征&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#你是否应用了过量的数据增强&quot;&gt;你是否应用了过量的数据增强？&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#检查你的预训练模型的预处理过程&quot;&gt;检查你的预训练模型的预处理过程&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#检查训练验证测试集的预处理&quot;&gt;检查训练、验证、测试集的预处理&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#iii-实现的问题&quot;&gt;III. 实现的问题&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#试着解决某一问题的更简易的版本&quot;&gt;试着解决某一问题的更简易的版本。&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#碰巧寻找正确的损失&quot;&gt;「碰巧」寻找正确的损失&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#检查你的损失函数&quot;&gt;检查你的损失函数&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#核实损失输入&quot;&gt;核实损失输入&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#调整损失权重&quot;&gt;调整损失权重&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#监控其它指标&quot;&gt;监控其它指标&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#测试任意的自定义层&quot;&gt;测试任意的自定义层&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#检查冷冻层或变量&quot;&gt;检查「冷冻」层或变量&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#扩大网络规模&quot;&gt;扩大网络规模&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#检查隐维度误差&quot;&gt;检查隐维度误差&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#探索梯度检查gradient-checking&quot;&gt;探索梯度检查（Gradient checking）&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#iv-训练问题&quot;&gt;IV. 训练问题&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#一个真正小的数据集&quot;&gt;一个真正小的数据集&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#检查权重初始化&quot;&gt;检查权重初始化&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#改变你的超参数&quot;&gt;改变你的超参数&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#减少正则化&quot;&gt;减少正则化&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#给它一些时间&quot;&gt;给它一些时间&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#从训练模式转换为测试模式&quot;&gt;从训练模式转换为测试模式&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#可视化训练&quot;&gt;可视化训练&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#尝试不同的优化器&quot;&gt;尝试不同的优化器&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#梯度爆炸梯度消失&quot;&gt;梯度爆炸、梯度消失&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#增加减少学习速率&quot;&gt;增加、减少学习速率&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#克服-nans&quot;&gt;克服 NaNs&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#karpathy的总结&quot;&gt;Karpathy的总结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#其他&quot;&gt;其他&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cnn-tips&quot;&gt;cnn tips&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;dl基础概念&quot;&gt;dl基础概念&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/nO49e-cX17y0Mry-HKY2wQ&quot;&gt;18个月自学AI，2年写就三万字长文，过来人教你如何掌握这几个AI基础概念&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;个问题&quot;&gt;个问题&lt;/h2&gt;

&lt;p&gt;参考&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650729285&amp;amp;idx=1&amp;amp;sn=8f78edc716bbd2198cd7b14f62a93298&amp;amp;chksm=871b2f3bb06ca62d60632da0faebbee63068405a5841934ebec300dc4bbace4b5e6f79daaeed&amp;amp;scene=0&amp;amp;key=e57780a9dd53e6fca81bb30f385de04141ec864e6c9f291dad63adcdf12ace00f946586e7ee7bfdf3fc77b95a40ff9c9294df46e69970f039bc6f24a1772e9ffec6e7b3531b394112d6a9ed1f186e2fc&amp;amp;ascene=0&amp;amp;uin=MjcxNzk4NjUyMw==&amp;amp;devicetype=iMac+MacBookPro11,1+OSX+OSX+10.12.5+build(16F73)&amp;amp;version=12020810&amp;amp;nettype=WIFI&amp;amp;fontScale=100&amp;amp;pass_ticket=wp6oDaO5BXGSrsUVsHD7622ZQWqqkDMj94Q8giNHxboROCC/RVbdZu5NL971vLLH&quot;&gt;训练的神经网络不工作？一文带你跨过这37个坑&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;神经网络已经持续训练了 12 个小时。它看起来很好：梯度在变化，损失也在下降。但是预测结果出来了：全部都是零值，全部都是背景，什么也检测不到。我质问我的计算机：「我做错了什么？」，它却无法回答。&lt;/p&gt;

&lt;p&gt;如果你的模型正在输出垃圾（比如预测所有输出的平均值，或者它的精确度真的很低），那么你从哪里开始检查呢？&lt;/p&gt;

&lt;p&gt;无法训练神经网络的原因有很多，因此通过总结诸多调试，作者发现有一些检查是经常做的。这张列表汇总了作者的经验以及最好的想法，希望也对读者有所帮助。&lt;/p&gt;

&lt;h3 id=&quot;i-数据集问题&quot;&gt;I. 数据集问题&lt;/h3&gt;

&lt;h5 id=&quot;检查你的输入数据&quot;&gt;检查你的输入数据&lt;/h5&gt;

&lt;p&gt;检查馈送到网络的输入数据是否正确。例如，我不止一次混淆了图像的宽度和高度。有时，我错误地令输入数据全部为零，或者一遍遍地使用同一批数据执行梯度下降。因此打印／显示若干批量的输入和目标输出，并确保它们正确。&lt;/p&gt;

&lt;h4 id=&quot;尝试随机输入&quot;&gt;尝试随机输入&lt;/h4&gt;

&lt;p&gt;尝试传递随机数而不是真实数据，看看错误的产生方式是否相同。如果是，说明在某些时候你的网络把数据转化为了垃圾。试着逐层调试，并查看出错的地方。&lt;/p&gt;

&lt;h4 id=&quot;检查数据加载器&quot;&gt;检查数据加载器&lt;/h4&gt;

&lt;p&gt;你的数据也许很好，但是读取输入数据到网络的代码可能有问题，所以我们应该在所有操作之前打印第一层的输入并进行检查。&lt;/p&gt;

&lt;h4 id=&quot;确保输入与输出相关联&quot;&gt;确保输入与输出相关联&lt;/h4&gt;

&lt;p&gt;检查少许输入样本是否有正确的标签，同样也确保 shuffling 输入样本同样对输出标签有效。&lt;/p&gt;

&lt;h4 id=&quot;输入与输出之间的关系是否太随机&quot;&gt;输入与输出之间的关系是否太随机？&lt;/h4&gt;

&lt;p&gt;相较于随机的部分（可以认为股票价格也是这种情况），输入与输出之间的非随机部分也许太小，即输入与输出的关联度太低。没有一个统一的方法来检测它，因为这要看数据的性质。&lt;/p&gt;

&lt;h4 id=&quot;数据集中是否有太多的噪音&quot;&gt;数据集中是否有太多的噪音？&lt;/h4&gt;

&lt;p&gt;我曾经遇到过这种情况，当我从一个食品网站抓取一个图像数据集时，错误标签太多以至于网络无法学习。手动检查一些输入样本并查看标签是否大致正确。&lt;/p&gt;

&lt;h4 id=&quot;shuffle-数据集&quot;&gt;Shuffle 数据集&lt;/h4&gt;

&lt;p&gt;如果你的数据集没有被 shuffle，并且有特定的序列（按标签排序），这可能给学习带来不利影响。你可以 shuffle 数据集来避免它，并确保输入和标签都被重新排列。&lt;/p&gt;

&lt;h4 id=&quot;减少类别失衡&quot;&gt;减少类别失衡&lt;/h4&gt;

&lt;p&gt;一张类别 B 图像和 1000 张类别 A 图像？如果是这种情况，那么你也许需要平衡你的损失函数或者尝试其他解决类别失衡的方法。&lt;/p&gt;

&lt;h4 id=&quot;你有足够的训练实例吗&quot;&gt;你有足够的训练实例吗？&lt;/h4&gt;

&lt;p&gt;如果你在从头开始训练一个网络（即不是调试），你很可能需要大量数据。对于图像分类，每个类别你需要 1000 张图像甚至更多。&lt;/p&gt;

&lt;h4 id=&quot;确保你采用的批量数据不是单一标签&quot;&gt;确保你采用的批量数据不是单一标签&lt;/h4&gt;

&lt;p&gt;这可能发生在排序数据集中（即前 10000 个样本属于同一个分类）。可通过 shuffle 数据集轻松修复。&lt;/p&gt;

&lt;h4 id=&quot;缩减批量大小&quot;&gt;缩减批量大小&lt;/h4&gt;

&lt;p&gt;巨大的批量大小会降低模型的泛化能力（参阅：&lt;a href=&quot;https://arxiv.org/abs/1609.04836&quot;&gt;https://arxiv.org/abs/1609.04836&lt;/a&gt;）&lt;/p&gt;

&lt;h3 id=&quot;ii-数据归一化增强&quot;&gt;II. 数据归一化/增强&lt;/h3&gt;

&lt;h4 id=&quot;归一化特征&quot;&gt;归一化特征&lt;/h4&gt;

&lt;p&gt;你的输入已经归一化到零均值和单位方差了吗？&lt;/p&gt;

&lt;h4 id=&quot;你是否应用了过量的数据增强&quot;&gt;你是否应用了过量的数据增强？&lt;/h4&gt;

&lt;p&gt;数据增强有正则化效果（regularizing effect）。过量的数据增强，加上其它形式的正则化（权重 L2，中途退出效应等）可能会导致网络欠拟合（underfit）。&lt;/p&gt;

&lt;h4 id=&quot;检查你的预训练模型的预处理过程&quot;&gt;检查你的预训练模型的预处理过程&lt;/h4&gt;

&lt;p&gt;如果你正在使用一个已经预训练过的模型，确保你现在正在使用的归一化和预处理与之前训练模型时的情况相同。例如，一个图像像素应该在 [0, 1]，[-1, 1] 或 [0, 255] 的范围内吗？&lt;/p&gt;

&lt;h4 id=&quot;检查训练验证测试集的预处理&quot;&gt;检查训练、验证、测试集的预处理&lt;/h4&gt;

&lt;p&gt;CS231n 指出了一个常见的陷阱：「任何预处理数据（例如数据均值）必须只在训练数据上进行计算，然后再应用到验证、测试数据中。例如计算均值，然后在整个数据集的每个图像中都减去它，再把数据分发进训练、验证、测试集中，这是一个典型的错误。」此外，要在每一个样本或批量（batch）中检查不同的预处理。&lt;/p&gt;

&lt;h3 id=&quot;iii-实现的问题&quot;&gt;III. 实现的问题&lt;/h3&gt;

&lt;h4 id=&quot;试着解决某一问题的更简易的版本&quot;&gt;试着解决某一问题的更简易的版本。&lt;/h4&gt;

&lt;p&gt;这将会有助于找到问题的根源究竟在哪里。例如，如果目标输出是一个物体类别和坐标，那就试着把预测结果仅限制在物体类别当中（尝试去掉坐标）。&lt;/p&gt;

&lt;h4 id=&quot;碰巧寻找正确的损失&quot;&gt;「碰巧」寻找正确的损失&lt;/h4&gt;

&lt;p&gt;还是来源于 CS231n 的技巧：用小参数进行初始化，不使用正则化。例如，如果我们有 10 个类别，「碰巧」就意味着我们将会在 10% 的时间里得到正确类别，Softmax 损失是正确类别的负 log 概率： -ln(0.1) = 2.302。然后，试着增加正则化的强度，这样应该会增加损失。&lt;/p&gt;

&lt;h4 id=&quot;检查你的损失函数&quot;&gt;检查你的损失函数&lt;/h4&gt;

&lt;p&gt;如果你执行的是你自己的损失函数，那么就要检查错误，并且添加单元测试。通常情况下，损失可能会有些不正确，并且损害网络的性能表现。&lt;/p&gt;

&lt;h4 id=&quot;核实损失输入&quot;&gt;核实损失输入&lt;/h4&gt;

&lt;p&gt;如果你正在使用的是框架提供的损失函数，那么要确保你传递给它的东西是它所期望的。例如，在 PyTorch 中，我会混淆 NLLLoss 和 CrossEntropyLoss，因为一个需要 softmax 输入，而另一个不需要。&lt;/p&gt;

&lt;h4 id=&quot;调整损失权重&quot;&gt;调整损失权重&lt;/h4&gt;

&lt;p&gt;如果你的损失由几个更小的损失函数组成，那么确保它们每一个的相应幅值都是正确的。这可能会涉及到测试损失权重的不同组合。&lt;/p&gt;

&lt;h4 id=&quot;监控其它指标&quot;&gt;监控其它指标&lt;/h4&gt;

&lt;p&gt;有时损失并不是衡量你的网络是否被正确训练的最佳预测器。如果可以的话，使用其它指标来帮助你，比如精度。&lt;/p&gt;

&lt;h4 id=&quot;测试任意的自定义层&quot;&gt;测试任意的自定义层&lt;/h4&gt;

&lt;p&gt;你自己在网络中实现过任意层吗？检查并且复核以确保它们的运行符合预期。&lt;/p&gt;

&lt;h4 id=&quot;检查冷冻层或变量&quot;&gt;检查「冷冻」层或变量&lt;/h4&gt;

&lt;p&gt;检查你是否无意中阻止了一些层或变量的梯度更新，这些层或变量本来应该是可学的。&lt;/p&gt;

&lt;h4 id=&quot;扩大网络规模&quot;&gt;扩大网络规模&lt;/h4&gt;

&lt;p&gt;可能你的网络的表现力不足以采集目标函数。试着加入更多的层，或在全连层中增加更多的隐藏单元。&lt;/p&gt;

&lt;h4 id=&quot;检查隐维度误差&quot;&gt;检查隐维度误差&lt;/h4&gt;

&lt;p&gt;如果你的输入看上去像（k,H,W）= (64, 64, 64)，那么很容易错过与错误维度相关的误差。给输入维度使用一些「奇怪」的数值（例如，每一个维度使用不同的质数），并且检查它们是如何通过网络传播的。&lt;/p&gt;

&lt;h4 id=&quot;探索梯度检查gradient-checking&quot;&gt;探索梯度检查（Gradient checking）&lt;/h4&gt;

&lt;p&gt;如果你手动实现梯度下降，梯度检查会确保你的反向传播（backpropagation）能像预期中一样工作。&lt;/p&gt;

&lt;h3 id=&quot;iv-训练问题&quot;&gt;IV. 训练问题&lt;/h3&gt;

&lt;h4 id=&quot;一个真正小的数据集&quot;&gt;一个真正小的数据集&lt;/h4&gt;

&lt;p&gt;过拟合数据的一个小子集，并确保其工作。例如，仅使用 1 或 2 个实例训练，并查看你的网络是否学习了区分它们。然后再训练每个分类的更多实例。&lt;/p&gt;

&lt;h4 id=&quot;检查权重初始化&quot;&gt;检查权重初始化&lt;/h4&gt;

&lt;p&gt;如果不确定，请使用 Xavier 或 He 初始化。同样，初始化也许会给你带来坏的局部最小值，因此尝试不同的初始化，看看是否有效。&lt;/p&gt;

&lt;h4 id=&quot;改变你的超参数&quot;&gt;改变你的超参数&lt;/h4&gt;

&lt;p&gt;或许你正在使用一个很糟糕的超参数集。如果可行，尝试一下网格搜索。&lt;/p&gt;

&lt;h4 id=&quot;减少正则化&quot;&gt;减少正则化&lt;/h4&gt;

&lt;p&gt;太多的正则化可致使网络严重地欠拟合。减少正则化，比如 dropout、批规范、权重／偏差 L2 正则化等。在优秀课程&lt;a href=&quot;http://course.fast.ai&quot;&gt;《编程人员的深度学习实战》&lt;/a&gt;中，Jeremy Howard 建议首先解决欠拟合。这意味着你充分地过拟合数据，并且只有在那时处理过拟合。&lt;/p&gt;

&lt;h4 id=&quot;给它一些时间&quot;&gt;给它一些时间&lt;/h4&gt;

&lt;p&gt;也许你的网络需要更多的时间来训练，在它能做出有意义的预测之前。如果你的损失在稳步下降，那就再多训练一会儿。&lt;/p&gt;

&lt;h4 id=&quot;从训练模式转换为测试模式&quot;&gt;从训练模式转换为测试模式&lt;/h4&gt;

&lt;p&gt;一些框架的层很像批规范、Dropout，而其他的层在训练和测试时表现并不同。转换到适当的模式有助于网络更好地预测。&lt;/p&gt;

&lt;h4 id=&quot;可视化训练&quot;&gt;可视化训练&lt;/h4&gt;

&lt;p&gt;监督每一层的激活值、权重和更新。确保它们的大小匹配。例如，参数更新的大小（权重和偏差）应该是 1-e3。
考虑可视化库，比如 Tensorboard 和 Crayon。紧要时你也可以打印权重／偏差／激活值。
寻找平均值远大于 0 的层激活。尝试批规范或者 ELUs。&lt;/p&gt;

&lt;p&gt;Deeplearning4j指出了权重和偏差柱状图中的期望值：对于权重，一些时间之后这些柱状图应该有一个近似高斯的（正常）分布。对于偏差，这些柱状图通常会从 0 开始，并经常以近似高斯（这种情况的一个例外是 LSTM）结束。留意那些向 +/- 无限发散的参数。留意那些变得很大的偏差。这有时可能发生在分类的输出层，如果类别的分布不均匀。&lt;/p&gt;

&lt;p&gt;检查层更新，它们应该有一个高斯分布。&lt;/p&gt;

&lt;h4 id=&quot;尝试不同的优化器&quot;&gt;尝试不同的优化器&lt;/h4&gt;

&lt;p&gt;优化器的选择不应当妨碍网络的训练，除非你选择了一个特别糟糕的参数。但是，为任务选择一个合适的优化器非常有助于在最短的时间内获得最多的训练。描述你正在使用的算法的论文应当指定优化器；如果没有，我倾向于选择 Adam 或者带有动量的朴素 SGD。&lt;/p&gt;

&lt;h4 id=&quot;梯度爆炸梯度消失&quot;&gt;梯度爆炸、梯度消失&lt;/h4&gt;

&lt;p&gt;检查隐蔽层的最新情况，过大的值可能代表梯度爆炸。这时，梯度截断（Gradient clipping）可能会有所帮助。
检查隐蔽层的激活值。Deeplearning4j 中有一个很好的指导方针：「一个好的激活值标准差大约在 0.5 到 2.0 之间。明显超过这一范围可能就代表着激活值消失或爆炸。」&lt;/p&gt;

&lt;h4 id=&quot;增加减少学习速率&quot;&gt;增加、减少学习速率&lt;/h4&gt;

&lt;p&gt;低学习速率将会导致你的模型收敛很慢；
高学习速率将会在开始阶段减少你的损失，但是可能会导致你很难找到一个好的解决方案。
试着把你当前的学习速率乘以 0.1 或 10。&lt;/p&gt;

&lt;h4 id=&quot;克服-nans&quot;&gt;克服 NaNs&lt;/h4&gt;

&lt;p&gt;据我所知，在训练 RNNs 时得到 NaN（Non-a-Number）是一个很大的问题。一些解决它的方法：&lt;/p&gt;

&lt;p&gt;减小学习速率，尤其是如果你在前 100 次迭代中就得到了 NaNs。
NaNs 的出现可能是由于用零作了除数，或用零或负数作了自然对数。
Russell Stewart 对如何处理 NaNs 很有心得&lt;a href=&quot;http://russellsstewart.com/notes/0.html&quot;&gt;http://russellsstewart.com/notes/0.html&lt;/a&gt;。
尝试逐层评估你的网络，这样就会看见 NaNs 到底出现在了哪里。&lt;/p&gt;

&lt;h2 id=&quot;karpathy的总结&quot;&gt;Karpathy的总结&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650761143&amp;amp;idx=1&amp;amp;sn=e39d7da6124ab2c2516688f67cdb793c&amp;amp;chksm=871aa3c9b06d2adf6d13a3601468e2791a0350863a20bc7bd17713bf2f70f087810711c47512&amp;amp;scene=0&amp;amp;xtrack=1&amp;amp;pass_ticket=9jPIbRAwWh%2F25BWVFlqVGXLCnz9uNgmnO77aFj4t5vlD2Xnw8kbQn%2Fl5JIH%2Bl%2Bg%2B#rd&quot;&gt;出神入化：特斯拉AI主管、李飞飞高徒Karpathy的33个神经网络「炼丹」技巧&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;原文：&lt;a href=&quot;https://karpathy.github.io/2019/04/25/recipe/&quot;&gt;https://karpathy.github.io/2019/04/25/recipe/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&amp;amp;mid=2650721602&amp;amp;idx=2&amp;amp;sn=f18e2d3a23dec485350611651e571031&amp;amp;chksm=871b093cb06c802aecc953e10c6bf5a14784ce3bad2c82170262ed6d6d7daedfcea9cceb804d&amp;amp;scene=21#wechat_redirect&quot;&gt;学界 | Andrej Karpathy：你为什么应该理解反向传播&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;其他&quot;&gt;其他&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/0PQ4z8qKToeiu7-M9nhoJA&quot;&gt;你有哪些deep learning（rnn、cnn）调参的经验？&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;cnn-tips&quot;&gt;cnn tips&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/RZjK8aXXvkkvXZq_sYO_RQ&quot;&gt;卷积神经网络性能优化&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>语义分割</title>
   <link href="http://daiwk.github.io/posts/cv-semantic-segmentation.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/cv-semantic-segmentation</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#emanet&quot;&gt;EMANet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;h2 id=&quot;emanet&quot;&gt;EMANet&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/DlGQshlOjSFvtsJw2XbPAA&quot;&gt;ICCV 2019 | 解读北大提出的期望最大化注意力网络EMANet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1907.13426&quot;&gt;Expectation-Maximization Attention Networks for Semantic Segmentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/XiaLiPKU/EMANet&quot;&gt;https://github.com/XiaLiPKU/EMANet&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ocr</title>
   <link href="http://daiwk.github.io/posts/cv-ocr.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/cv-ocr</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tesseract-ocr/tesseract&quot;&gt;https://github.com/tesseract-ocr/tesseract&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tesseract OCR是一款功能强大的光学字符识别软件，可让你识别语言。&lt;/p&gt;

&lt;p&gt;它支持100多种语言，也可以编程识别新语言。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>人脸识别</title>
   <link href="http://daiwk.github.io/posts/cv-face-recognition.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/cv-face-recognition</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;人脸识别最简洁的工具：&lt;a href=&quot;https://github.com/ageitgey/face_recognition&quot;&gt;https://github.com/ageitgey/face_recognition&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/seetafaceengine/SeetaFace2&quot;&gt;https://github.com/seetafaceengine/SeetaFace2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/Am2ZiNOA7Dcf-Ifi7YJnQw&quot;&gt;SeetaFace2.0：中科视拓开源跨平台C++商业人脸识别库&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mp.weixin.qq.com/s/DqgiJ88kRyvrucwbGK82Dg&quot;&gt;大小仅1MB，超轻量级通用人脸检测模型登上GitHub趋势榜&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB&quot;&gt;https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;过滤小于10x10的人脸的数据集的下载地址：&lt;a href=&quot;https://pan.baidu.com/share/init?surl=m600pp-AsNot6XgIiqDlOw&quot;&gt;https://pan.baidu.com/share/init?surl=m600pp-AsNot6XgIiqDlOw&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;提取码：x5gt&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>音频处理常用库</title>
   <link href="http://daiwk.github.io/posts/audio-libs.html"/>
   <updated>1970-01-01T00:00:00+00:00</updated>
   <id>/posts/audio-libs</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

&lt;!-- TOC --&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#librosa&quot;&gt;LibROSA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#madmom&quot;&gt;Madmom&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#pyaudioanalysis&quot;&gt;pyAudioAnalysis&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- /TOC --&gt;

&lt;p&gt;参考&lt;a href=&quot;https://zhuanlan.zhihu.com/p/76112940&quot;&gt;吐血整理！绝不能错过的24个顶级Python库&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;librosa&quot;&gt;LibROSA&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://librosa.github.io/librosa/&quot;&gt;https://librosa.github.io/librosa/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;LibROSA是一个用于音乐和音频分析的Python库。它提供了创建音乐信息检索系统所需的构建块。&lt;/p&gt;

&lt;p&gt;安装指南传送门：&lt;a href=&quot;https://librosa.github.io/librosa/install.html&quot;&gt;https://librosa.github.io/librosa/install.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这是一篇关于音频处理及其工作原理的深度文章：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.analyticsvidhya.com/blog/2017/08/audio-voice-processing-deep-learning/&quot;&gt;利用深度学习开始音频数据分析（含案例研究）&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;madmom&quot;&gt;Madmom&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/CPJKU/madmom&quot;&gt;https://github.com/CPJKU/madmom&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Madmom是一个用于音频数据分析的很棒的Python库。它是一个用Python编写的音频信号处理库，主要用于音乐信息检索（MIR）任务。&lt;/p&gt;

&lt;p&gt;以下是安装Madmom的必备条件：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;NumPy&lt;/li&gt;
  &lt;li&gt;SciPy&lt;/li&gt;
  &lt;li&gt;Cython&lt;/li&gt;
  &lt;li&gt;Mido&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以下软件包用于测试安装：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;PyTest&lt;/li&gt;
  &lt;li&gt;Fyaudio&lt;/li&gt;
  &lt;li&gt;PyFftw&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;安装Madmom的代码：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;madmom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;下文可用以了解Madmom如何用于音乐信息检索：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.analyticsvidhya.com/blog/2018/02/audio-beat-tracking-for-music-information-retrieval/&quot;&gt;学习音乐信息检索的音频节拍追踪（使用Python代码）&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;pyaudioanalysis&quot;&gt;pyAudioAnalysis&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/tyiannak/pyAudioAnalysis&quot;&gt;https://github.com/tyiannak/pyAudioAnalysis&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pyAudioAnalysis是一个用于音频特征提取、分类和分段的Python库，涵盖广泛的音频分析任务，例如：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对未知声音进行分类&lt;/li&gt;
  &lt;li&gt;检测音频故障并排除长时间录音中的静音时段&lt;/li&gt;
  &lt;li&gt;进行监督和非监督的分割&lt;/li&gt;
  &lt;li&gt;提取音频缩略图等等&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;可以使用以下代码进行安装：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;pyAudioAnalysis
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 
</feed>