<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title></title>
 <link href="http://hxhlwf.github.io/atom.xml" rel="self"/>
 <link href="http://hxhlwf.github.io"/>
 <updated>2018-03-29T07:33:52+00:00</updated>
 <author>
   <name>hxhlwf</name>
   <email>hxhlwf#foxmail.com</email>
 </author>

 
 <entry>
   <title>zookeeper</title>
   <link href="http://hxhlwf.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;a href=&quot;https://github.com/apache/zookeeper&quot;&gt;https://github.com/apache/zookeeper&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>thrift</title>
   <link href="http://hxhlwf.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://hxhlwf.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>nginx</title>
   <link href="http://hxhlwf.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://hxhlwf.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;!-- /TOC --&gt;
</content>
 </entry>
 
 <entry>
   <title>memcached</title>
   <link href="http://hxhlwf.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>kubernetes</title>
   <link href="http://hxhlwf.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;/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;/ul&gt;

&lt;!-- /TOC --&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;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;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;单集群可以一次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;docker .baidu.com
registry.baidu.com
dockerhub.baidubce.com&lt;/p&gt;

&lt;p&gt;微服务
自动伸缩
使用supervisor,如果主进程被Kill了，会把这个信号量传给每个子进程，避免孤儿进程
节点亲和&amp;amp;应用亲和：例如混布cpu+gpu（后来，1.6的k8s原生支持gpu了）&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>grpc</title>
   <link href="http://hxhlwf.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;!-- /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;
</content>
 </entry>
 
 <entry>
   <title>baidurpc</title>
   <link href="http://hxhlwf.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;!-- /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;
</content>
 </entry>
 
 <entry>
   <title>tensorflow基础用法</title>
   <link href="http://hxhlwf.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;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;o&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;c&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;o&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;o&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;c&quot;&gt;# Create another six-element vector. Each element in the vector will be&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# initialized to 1. The first argument is the shape of the tensor (more&lt;/span&gt;
  &lt;span class=&quot;c&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;o&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;o&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;c&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;o&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;c&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;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;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;o&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;c&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;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;o&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;c&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;o&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;c&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;o&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;c&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;o&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;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;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;o&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;o&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;c&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;o&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;o&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;c&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;o&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;o&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;c&quot;&gt;#[[0. 0. 0.]&lt;/span&gt;
    &lt;span class=&quot;c&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;highlighter-rouge&quot;&gt;[6]&lt;/code&gt; 的张量，则大小为 &lt;code class=&quot;highlighter-rouge&quot;&gt;[1]&lt;/code&gt; 或 &lt;code class=&quot;highlighter-rouge&quot;&gt;[]&lt;/code&gt; 的张量可以作为运算数。&lt;/li&gt;
  &lt;li&gt;如果指令需要大小为 &lt;code class=&quot;highlighter-rouge&quot;&gt;[4, 6]&lt;/code&gt; 的张量，则以下任何大小的张量都可以作为运算数。
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[1, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[]&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;如果指令需要大小为 &lt;code class=&quot;highlighter-rouge&quot;&gt;[3, 5, 6]&lt;/code&gt; 的张量，则以下任何大小的张量都可以作为运算数。
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[1, 5, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[3, 1, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[3, 5, 1]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[1, 1, 1]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[5, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[1, 6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[6]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;[1]&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;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;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;o&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;c&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;o&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;o&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;c&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;o&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;o&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;c&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;o&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;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;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;o&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;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;o&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;c&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;o&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;o&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;c&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;o&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;o&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;c&quot;&gt;# Multiply `x` by `y`. &lt;/span&gt;
  &lt;span class=&quot;c&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;o&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;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;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;o&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;c&quot;&gt;# [[35 58]&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;#  [35 33]&lt;/span&gt;
    &lt;span class=&quot;c&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;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;o&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;c&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;o&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;o&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;c&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;o&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;c&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;o&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;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;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;o&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;o&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;o&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;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;o&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;c&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;o&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;o&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;c&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;o&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;c&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;o&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;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;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;o&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;o&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;o&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;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;with&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;as_default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
  &lt;span class=&quot;c&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;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;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# Create a variable of shape [1], with a random initial value,&lt;/span&gt;
  &lt;span class=&quot;c&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;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;tf&lt;/span&gt;&lt;span class=&quot;o&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;o&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;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;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;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;o&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;c&quot;&gt;# Caught expected error:  Attempting to use uninitialized value Variable&lt;/span&gt;
	    &lt;span class=&quot;c&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;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;o&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;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;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;o&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;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;c&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;o&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;o&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;o&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;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;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;o&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;o&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;c&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;o&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;o&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;c&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;o&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;c&quot;&gt;# Execute the assignment op.&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;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;c&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;o&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;highlighter-rouge&quot;&gt;10x3&lt;/code&gt; 二维张量，其中：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;列 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;2&lt;/code&gt; 均存储一个骰子的一次投掷值。&lt;/li&gt;
  &lt;li&gt;列 &lt;code class=&quot;highlighter-rouge&quot;&gt;3&lt;/code&gt; 存储同一行中列 &lt;code class=&quot;highlighter-rouge&quot;&gt;1&lt;/code&gt; 和 &lt;code class=&quot;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;highlighter-rouge&quot;&gt;1&lt;/code&gt; 存储 &lt;code class=&quot;highlighter-rouge&quot;&gt;4&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;列 &lt;code class=&quot;highlighter-rouge&quot;&gt;2&lt;/code&gt; 存储 &lt;code class=&quot;highlighter-rouge&quot;&gt;3&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;列 &lt;code class=&quot;highlighter-rouge&quot;&gt;3&lt;/code&gt; 存储 &lt;code class=&quot;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;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;o&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;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;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;c&quot;&gt;# Task 2: Simulate 10 throws of two dice. Store the results&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# in a 10x3 matrix.&lt;/span&gt;

  &lt;span class=&quot;c&quot;&gt;# We're going to place dice throws inside two separate&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# 10x1 matrices. We could have placed dice throws inside&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# a single 10x2 matrix, but adding different columns of&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# the same matrix is tricky. We also could have placed&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# dice throws inside two 1-D tensors (vectors); doing so&lt;/span&gt;
  &lt;span class=&quot;c&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;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;tf&lt;/span&gt;&lt;span class=&quot;o&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;o&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;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;tf&lt;/span&gt;&lt;span class=&quot;o&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;o&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;c&quot;&gt;# We may add dice1 and dice2 since they share the same shape&lt;/span&gt;
  &lt;span class=&quot;c&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;o&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;c&quot;&gt;# We've got three separate 10x1 matrices. To produce a single&lt;/span&gt;
  &lt;span class=&quot;c&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;o&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;c&quot;&gt;# The variables haven't been initialized within the graph yet,&lt;/span&gt;
  &lt;span class=&quot;c&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;o&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;o&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;o&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;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;highlighter-rouge&quot;&gt;Series&lt;/code&gt;&lt;/strong&gt;，它是单&lt;strong&gt;一列&lt;/strong&gt;。&lt;code class=&quot;highlighter-rouge&quot;&gt;DataFrame&lt;/code&gt; 中包含一个或多个 &lt;code class=&quot;highlighter-rouge&quot;&gt;Series&lt;/code&gt;，每个 &lt;code class=&quot;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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;c&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;c&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;c&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;o&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;o&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;o&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;o&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;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;c&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;c&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;o&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;c&quot;&gt;#City name	Population	Area square miles	Population density	Is wide and has saint name&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#2	Sacramento	485199	97.92	4955.055147	False&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#0	San Francisco	852469	46.87	18187.945381	False&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#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;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&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;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;/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;o&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;c&quot;&gt;#	City name	Population	Area square miles	Population density	Is wide and has saint name&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#0	San Francisco	852469.0	46.87	18187.945381	False&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#4	NaN	NaN	NaN	NaN	NaN&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#5	NaN	NaN	NaN	NaN	NaN&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#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;c&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;c&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;o&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;c&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;o&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&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;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;c&quot;&gt;# Configure the linear regression model with our feature columns and optimizer.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 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;o&quot;&gt;.&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;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;c&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;o&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;o&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;c&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;o&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;c&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;o&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;o&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;c&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;o&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;c&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;o&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;o&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;o&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;c&quot;&gt;# Create an input function for predictions.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Note: Since we're making just one prediction for each example, we don't &lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 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;c&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;o&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;c&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;o&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;c&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;o&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;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;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): &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%0.3&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;f&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): &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%0.3&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;f&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;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_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;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_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: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%0.3&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;f&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: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%0.3&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;f&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.: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%0.3&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;f&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: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%0.3&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;f&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;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;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;c&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;feature_column&lt;/span&gt;&lt;span class=&quot;o&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;c&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;c&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;o&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contrib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&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;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;o&quot;&gt;.&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;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;c&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;o&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;o&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;o&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;o&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;o&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;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;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;o&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;o&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;o&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;c&quot;&gt;# Train the model, but do so inside a loop so that we can periodically assess&lt;/span&gt;
  &lt;span class=&quot;c&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;c&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;o&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;c&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;o&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;o&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;c&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;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;metrics&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;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;c&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 &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%02&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;d : &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%0.2&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;c&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;o&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;c&quot;&gt;# Finally, track the weights and biases over time.&lt;/span&gt;
    &lt;span class=&quot;c&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;o&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;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;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;o&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/&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;o&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;o&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;o&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;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;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;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;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;o&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;c&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;o&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;o&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;o&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;o&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;o&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;o&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;c&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;o&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;o&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;o&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;o&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;o&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): &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%0.2&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;f&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://hxhlwf.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://hxhlwf.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>基于TensorFlow的机器学习速成课程（中文版）</title>
   <link href="http://hxhlwf.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;
</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap3 基于模型的动态规划方法</title>
   <link href="http://hxhlwf.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;highlighter-rouge&quot;&gt;\(S,A,P,r,\gamma\)&lt;/code&gt;表示。&lt;code class=&quot;highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;为状态集，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;为动作集，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;为&lt;strong&gt;转移概率&lt;/strong&gt;【对应环境和智能体的&lt;strong&gt;模型&lt;/strong&gt;】，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(r\)&lt;/code&gt;为回报函数，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;为折扣因子，用于计算累积回报&lt;code class=&quot;highlighter-rouge&quot;&gt;\(R=\sum_{t=0}^T\gamma ^tr_t\)&lt;/code&gt;。若&lt;code class=&quot;highlighter-rouge&quot;&gt;\(T\)&lt;/code&gt;为有限值，强化学习过程称为有限范围强化学习。若&lt;code class=&quot;highlighter-rouge&quot;&gt;\(T=\infty\)&lt;/code&gt;，称为无限范围强化学习。下面以有限范围强化学习为例。&lt;/p&gt;

&lt;p&gt;强化学习的目标是找到最优策略&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;，使得累积回报的期望最大。策略是状态到动作的映射&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi:s\rightarrow a\)&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\tau \)&lt;/code&gt;表示从状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;到最终状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_T\)&lt;/code&gt;的一个序列&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\tau:s_t,s_{t+1},...,s_T\)&lt;/code&gt;，目标函数是累积回报函数的期望&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\underset{\pi}{max}\int R(\tau)p_{\pi}(\tau)d\tau\)&lt;/code&gt;，最终目标是找到最优策略&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi ^*:s\rightarrow u^*\)&lt;/code&gt;。这里的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u\)&lt;/code&gt;指的是决策序列&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\[
\\\upsilon ^*(s)=\underset{a}{max}R^a_{ss'}+\gamma \sum _{s'\in S}P^a_{ss'}\upsilon ^*(s')
\\q^*(s,a)=R^a_{ss'}+\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;highlighter-rouge&quot;&gt;\[
\upsilon_{\pi}(s)=\sum_{a\in A}\pi(a|s)(R^a_{ss'}+\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;highlighter-rouge&quot;&gt;\(\upsilon_{\pi}(s)\)&lt;/code&gt;&lt;/strong&gt;可以用&lt;strong&gt;后继状态的值函数&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(R^a_{ss'}\)&lt;/code&gt;都是已知的，&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\[
\upsilon_{k+1}(s)=\sum_{a\in A}\pi(a|s)(R^a_{ss'}+\gamma \sum_{s'\in S}P^a_{ss'}\upsilon _{k}(s'))
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;算法步骤如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;输入：需要评估的策略&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;状态转移概率&lt;code class=&quot;highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;回报函数&lt;code class=&quot;highlighter-rouge&quot;&gt;\(R^a_s\)&lt;/code&gt;，折扣因子&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;初始化值函数：&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\upsilon(s)=0\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;repeat k=0,1,…
    &lt;ul&gt;
      &lt;li&gt;for s in S:
        &lt;ul&gt;
          &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\upsilon_{k+1}(s)=\sum _{a\in A}\pi(a|s)(R^a_{ss'}+\gamma \sum_{s'\in S}P^a_{ss'}\upsilon_k(s'))\)&lt;/code&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;until &lt;code class=&quot;highlighter-rouge&quot;&gt;\(\upsilon_{k+1}=\upsilon_k\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;输出：&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\upsilon(s)\)&lt;/code&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;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;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://hxhlwf.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;
</content>
 </entry>
 
 <entry>
   <title>深入浅出强化学习-chap2 马尔科夫决策过程</title>
   <link href="http://hxhlwf.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;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;highlighter-rouge&quot;&gt;\(s_{t+1}\)&lt;/code&gt;&lt;strong&gt;仅与当前状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;有关&lt;/strong&gt;，与之前的状态无关。&lt;/p&gt;

&lt;p&gt;定义：状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;是马尔科夫的，当且仅当&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\((S,P)\)&lt;/code&gt;，且满足：&lt;code class=&quot;highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;是有限状态集合，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;是状态转移概率。状态转移概率矩阵如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;为有限的状态集&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;为有限的动作集&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;为状态转移概率&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(R\)&lt;/code&gt;为回报函数&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;表示，给定状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;时，动作集上的一个分布，即：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;在每个状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;指定一个动作概率。如果给出的策略&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;是确定性的，那么策略&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;在每个状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;指定一个确定的动作。注，&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;时，假设从状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;出发，可能有很多个状态序列，所以对应的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(G_t\)&lt;/code&gt;也有多个可能值。为了评价状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_t\)&lt;/code&gt;的价值，需要一个确定的量来描述。而&lt;strong&gt;累积回报&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;时&lt;/strong&gt;，累积回报服从一个分布，&lt;strong&gt;累积回报&lt;/strong&gt;在&lt;strong&gt;状态&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;相对应的&lt;/strong&gt;，因为策略&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi\)&lt;/code&gt;决定了累积回报&lt;code class=&quot;highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;的状态分布。&lt;/p&gt;

&lt;p&gt;状态-行为值函数为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;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;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;highlighter-rouge&quot;&gt;\(P^a_{ss'}\)&lt;/code&gt;表示从状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s\)&lt;/code&gt;开始，采取行动&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;，移动到状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s'\)&lt;/code&gt;的概率)&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\[
q_{\pi}(s,a)=R^a_{ss'}+\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;highlighter-rouge&quot;&gt;\[
\upsilon_{\pi}(s)=\sum_{a\in A}\pi(a|s)(R^a_{ss'}+\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;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;highlighter-rouge&quot;&gt;\[
q_{\pi}(s,a)=R^a_{ss'}+\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;highlighter-rouge&quot;&gt;\(\pi(a|s)=0.5,\gamma =1\)&lt;/code&gt;。&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_4\)&lt;/code&gt;通过睡觉，可以到达&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_5\)&lt;/code&gt;，而通过发表，可以到达&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_3\)&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_2\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_4\)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\upsilon ^*(s)\)&lt;/code&gt;为在所有策略中值最大的值函数，即&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(q^*(s,a)\)&lt;/code&gt;为在所有策略中最大的状态-行为值函数，即&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\upsilon _{\pi}(s)\)&lt;/code&gt;的区别是，少乘了一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi(a|s)\)&lt;/code&gt;)：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\[
\upsilon ^*(s)=\underset{a}{max}R^a_{ss'}+\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;highlighter-rouge&quot;&gt;\[
q^*(s,a)=R^a_{ss'}+\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;highlighter-rouge&quot;&gt;\(q^*(s,a)\)&lt;/code&gt;来决定：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(S\)&lt;/code&gt;为&lt;strong&gt;状态集&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;为&lt;strong&gt;动作集&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;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;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;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;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;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;highlighter-rouge&quot;&gt;\(\tau \)&lt;/code&gt;为一个&lt;strong&gt;轨迹序列&lt;/strong&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\tau =(s_0,a_0,s_1,a_1,...)\)&lt;/code&gt;
&lt;code class=&quot;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;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;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;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;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;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;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;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;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;highlighter-rouge&quot;&gt;\(\varepsilon -greedy\)&lt;/code&gt;策略&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;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;highlighter-rouge&quot;&gt;\(\mu_{\theta}\)&lt;/code&gt;为&lt;strong&gt;确定性部分&lt;/strong&gt;，&lt;code class=&quot;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;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;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;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://hxhlwf.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://hxhlwf.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://hxhlwf.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;o&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;o&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;o&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;highlighter-rouge&quot;&gt;/gym/gym/envs/classic_control/cartpole.py&lt;/code&gt;
注册的代码在&lt;code class=&quot;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;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np_random&lt;/span&gt;&lt;span class=&quot;o&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;o&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;o&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;o&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;o&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;o&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;o&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;c&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;o&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;c&quot;&gt;## 填充一个矩形&lt;/span&gt;
            &lt;span class=&quot;c&quot;&gt;#添加台车转换矩阵属性&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;o&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;o&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;o&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;c&quot;&gt;#加入几何体台车&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;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;c&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;o&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;o&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;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;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;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;c&quot;&gt;#添加摆杆转换矩阵属性&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;o&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;o&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;o&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;o&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;o&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;c&quot;&gt;#加入几何体&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;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;c&quot;&gt;#创建摆杆和台车之间的连接&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;o&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;o&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;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;o&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;o&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;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;o&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;o&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;set_color&lt;/span&gt;&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;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;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;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;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;o&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;c&quot;&gt;#创建台车来回滑动的轨道，即一条直线&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;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;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;o&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;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;o&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;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;o&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;o&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;o&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;c&quot;&gt;# MIDDLE OF CART&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;#设置平移属性&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;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;o&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;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;o&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action_space&lt;/span&gt;&lt;span class=&quot;o&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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;r (&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;o&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;c&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;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;force_mag&lt;/span&gt; &lt;span class=&quot;c&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;o&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;c&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;o&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;c&quot;&gt;# sin&lt;/span&gt;
        &lt;span class=&quot;c&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;o&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;o&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;o&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;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;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;o&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;o&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;c&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;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_mass&lt;/span&gt; &lt;span class=&quot;c&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;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;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;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;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;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;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;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;thetaacc&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# 积分球下一步的状态&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&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;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;o&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;o&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;o&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;o&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;o&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;c&quot;&gt;# Pole just fell!&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&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;o&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;o&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;o&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;o&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;o&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://hxhlwf.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://hxhlwf.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;!-- /TOC --&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;

</content>
 </entry>
 
 <entry>
   <title>nested lstm</title>
   <link href="http://hxhlwf.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;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;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;highlighter-rouge&quot;&gt;\(c^{outer}_t=h^{inner}_t\)&lt;/code&gt;。&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>对抗样本防御</title>
   <link href="http://hxhlwf.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://hxhlwf.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;highlighter-rouge&quot;&gt;\(v = (0, 0.9)\)&lt;/code&gt;，向量的模是&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;和&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(W_{ij}\)&lt;/code&gt;应用到前一层的输出&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;上，例如，使用一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(m\times k\)&lt;/code&gt;的矩阵，将&lt;code class=&quot;highlighter-rouge&quot;&gt;\(k\times D\)&lt;/code&gt;的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;变成一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(m\times D\)&lt;/code&gt;的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\hat u_{j|i}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;然后计算&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c_{ij}\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\hat u_{j|i}\)&lt;/code&gt;的加权和，得到&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_j\)&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;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;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;highlighter-rouge&quot;&gt;\(W_{ij}\)&lt;/code&gt;在胶囊中仍然用&lt;strong&gt;反向传播&lt;/strong&gt;训练。不过，耦合系数&lt;code class=&quot;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;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;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;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;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;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>2017年度10大值得读的cv方面的paper</title>
   <link href="http://hxhlwf.github.io/posts/cv-2017-cv-papers.html"/>
   <updated>2018-01-31T00:00:00+00:00</updated>
   <id>/posts/cv-2017-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;#1-mask-r-cnn&quot;&gt;1. Mask R-CNN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-image-to-image-translation-with-conditional-adversarial-networks&quot;&gt;2. Image-to-Image Translation with Conditional Adversarial Networks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3--a-fast-rcnn-hard-positive-generation-via-adversary-for-object-detection&quot;&gt;3.  A-Fast-RCNN: Hard Positive Generation via Adversary for Object Detection&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-bayesian-gan&quot;&gt;4. Bayesian GAN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-interpretable-r-cnn&quot;&gt;5. Interpretable R-CNN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-learning-feature-pyramids-for-human-pose-estimation&quot;&gt;6. Learning Feature Pyramids for Human Pose Estimation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-unpaired-image-to-image-translation-using-cycle-consistent-adversarial-networks&quot;&gt;7. Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#8-high-resolution-image-synthesis-and-semantic-manipulation-with-conditional-gans&quot;&gt;8. High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#9-triple-generative-adversarial-nets&quot;&gt;9. Triple Generative Adversarial Nets&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#10-realtime-multi-person-2d-pose-estimation-using-part-affinity-fields&quot;&gt;10. Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields&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论文 | CV篇 · 评选结果公布&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;1-mask-r-cnn&quot;&gt;1. Mask R-CNN&lt;/h2&gt;

&lt;h2 id=&quot;2-image-to-image-translation-with-conditional-adversarial-networks&quot;&gt;2. Image-to-Image Translation with Conditional Adversarial Networks&lt;/h2&gt;

&lt;h2 id=&quot;3--a-fast-rcnn-hard-positive-generation-via-adversary-for-object-detection&quot;&gt;3.  A-Fast-RCNN: Hard Positive Generation via Adversary for Object Detection&lt;/h2&gt;

&lt;h2 id=&quot;4-bayesian-gan&quot;&gt;4. Bayesian GAN&lt;/h2&gt;

&lt;h2 id=&quot;5-interpretable-r-cnn&quot;&gt;5. Interpretable R-CNN&lt;/h2&gt;

&lt;h2 id=&quot;6-learning-feature-pyramids-for-human-pose-estimation&quot;&gt;6. Learning Feature Pyramids for Human Pose Estimation&lt;/h2&gt;

&lt;h2 id=&quot;7-unpaired-image-to-image-translation-using-cycle-consistent-adversarial-networks&quot;&gt;7. Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks&lt;/h2&gt;

&lt;h2 id=&quot;8-high-resolution-image-synthesis-and-semantic-manipulation-with-conditional-gans&quot;&gt;8. High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs&lt;/h2&gt;

&lt;h2 id=&quot;9-triple-generative-adversarial-nets&quot;&gt;9. Triple Generative Adversarial Nets&lt;/h2&gt;

&lt;h2 id=&quot;10-realtime-multi-person-2d-pose-estimation-using-part-affinity-fields&quot;&gt;10. Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields&lt;/h2&gt;
</content>
 </entry>
 
 <entry>
   <title>2017年度10大值得读的nlp方面的paper</title>
   <link href="http://hxhlwf.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://hxhlwf.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;#超分辨率&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冠军&quot;&gt;DSB2017冠军&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;#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://hxhlwf.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;/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;
</content>
 </entry>
 
 <entry>
   <title>dl中的normalization</title>
   <link href="http://hxhlwf.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;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;h2 id=&quot;layer-normalization&quot;&gt;layer normalization&lt;/h2&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;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://hxhlwf.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;#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;#ccpm&quot;&gt;CCPM&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;/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;#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;/ul&gt;

&lt;!-- /TOC --&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;h2 id=&quot;fm&quot;&gt;FM&lt;/h2&gt;

&lt;p&gt;二阶多项式模型：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(w_0\in \mathbb{R}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\mathbf{w}\in \mathbb{R}^n\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(\mathbf{v}_i\)&lt;/code&gt;就表示&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\mathbf{V}\)&lt;/code&gt;这个矩阵的第i行（有k列），而&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\hat{w}_{ij}\)&lt;/code&gt;，而&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\hat{w}\)&lt;/code&gt;这个矩阵是&lt;code class=&quot;highlighter-rouge&quot;&gt;\((n-1)\times(n-1)\)&lt;/code&gt;维的矩阵，刻画的是相邻两个x【&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_{i+1}\)&lt;/code&gt;】之间的系数。因此，可以理解为，将这个&lt;code class=&quot;highlighter-rouge&quot;&gt;\((n-1)\times(n-1)\)&lt;/code&gt;维的矩阵用一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(n\times k\)&lt;/code&gt;维的低秩矩阵来表示。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ffm&quot;&gt;FFM&lt;/h2&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;h3 id=&quot;fnn&quot;&gt;FNN&lt;/h3&gt;

&lt;h3 id=&quot;snn&quot;&gt;SNN&lt;/h3&gt;

&lt;h2 id=&quot;ccpm&quot;&gt;CCPM&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://dl.acm.org/citation.cfm?id=2806603&quot;&gt;A Convolutional Click Prediction Model&lt;/a&gt;&lt;/p&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;h3 id=&quot;ipnn&quot;&gt;IPNN&lt;/h3&gt;

&lt;h3 id=&quot;opnn&quot;&gt;OPNN&lt;/h3&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;用于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;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;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;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;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;
</content>
 </entry>
 
 <entry>
   <title>parameter server详解</title>
   <link href="http://hxhlwf.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;#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-对比parameter-server与通用分布式系统&quot;&gt;3. 对比parameter server与通用分布式系统&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-parameter-server的优势&quot;&gt;4. parameter server的优势&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#5-parameter-server系统架构&quot;&gt;5. parameter server系统架构&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;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#52-kv-range-push--pull&quot;&gt;5.2 (k,v), range push &amp;amp; pull&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#53-asynchronous-tasks-and-dependency&quot;&gt;5.3 Asynchronous Tasks and Dependency&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#6-implementation&quot;&gt;6. Implementation&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#61-vector-clock&quot;&gt;6.1 Vector Clock&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#62-messages&quot;&gt;6.2 Messages&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#63-consistent-hashing&quot;&gt;6.3 Consistent Hashing&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#64-replication-and-consistency&quot;&gt;6.4 Replication and Consistency&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#641-默认的复制方式-chain-replication-强一致性-可靠&quot;&gt;6.4.1 默认的复制方式: &lt;strong&gt;Chain replication (强一致性, 可靠)&lt;/strong&gt;：&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#642-replication-after-aggregation&quot;&gt;6.4.2 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;#65-server-management&quot;&gt;6.5 Server Management&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;1-背景&quot;&gt;1. 背景&lt;/h2&gt;

&lt;p&gt;现实中，训练数据的数量可能达到1TB到1PB之间，而训练过程中的参数可能会达到&lt;code class=&quot;highlighter-rouge&quot;&gt;\(10^9\)&lt;/code&gt;（十亿）到&lt;code class=&quot;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;2-发展历程&quot;&gt;2. 发展历程&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;3-对比parameter-server与通用分布式系统&quot;&gt;3. 对比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;4-parameter-server的优势&quot;&gt;4. 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;5-parameter-server系统架构&quot;&gt;5. parameter server系统架构&lt;/h2&gt;

&lt;h3 id=&quot;51-总体架构&quot;&gt;5.1 总体架构&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;h2 id=&quot;52-kv-range-push--pull&quot;&gt;5.2 (k,v), range push &amp;amp; pull&lt;/h2&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;h2 id=&quot;53-asynchronous-tasks-and-dependency&quot;&gt;5.3 Asynchronous Tasks and Dependency&lt;/h2&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;highlighter-rouge&quot;&gt;\(\tau \)&lt;/code&gt;作为最大的延时时间。也就是说，只有&lt;code class=&quot;highlighter-rouge&quot;&gt;\(&amp;gt;\tau \)&lt;/code&gt;之前的任务都被完成了，才能开始一个新的任务。极端的情况：&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\tau = 0\)&lt;/code&gt;情况就是 Sequential；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\tau = \infty \)&lt;/code&gt;情况就是 Sequential；&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;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;6-implementation&quot;&gt;6. Implementation&lt;/h2&gt;

&lt;h3 id=&quot;61-vector-clock&quot;&gt;6.1 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;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;highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;是算法中产生的所有的range，那么空间复杂度就变成了&lt;code class=&quot;highlighter-rouge&quot;&gt;\(O(k*m)\)&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;62-messages&quot;&gt;6.2 Messages&lt;/h3&gt;

&lt;p&gt;一条 message 包括：时间戳，len(range)对k-v：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;63-consistent-hashing&quot;&gt;6.3 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;highlighter-rouge&quot;&gt;\(S_1\)&lt;/code&gt;复制了&lt;code class=&quot;highlighter-rouge&quot;&gt;\(S_2\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(S_3\)&lt;/code&gt;内的key。&lt;/p&gt;

&lt;h3 id=&quot;64-replication-and-consistency&quot;&gt;6.4 Replication and Consistency&lt;/h3&gt;

&lt;p&gt;两种方式保证slave跟master之间的数据一致性：&lt;/p&gt;

&lt;h4 id=&quot;641-默认的复制方式-chain-replication-强一致性-可靠&quot;&gt;6.4.1 默认的复制方式: &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;642-replication-after-aggregation&quot;&gt;6.4.2 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;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;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;65-server-management&quot;&gt;6.5 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;

</content>
 </entry>
 
 <entry>
   <title>irgan</title>
   <link href="http://hxhlwf.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;#观点&quot;&gt;观点&lt;/a&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;/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;/p&gt;

&lt;p&gt;本篇 SIGIR2017 的满分论文则首次提出将两方面流派的数据模型通过一种对抗训练的方式统一在一起，使得两方面的模型能够相互提高，最终使得检索到的文档更加精准。文章的实验分别在网络搜索、推荐系统以及问答系统三个应用场景中实现并验证了结果的有效性。&lt;/p&gt;

&lt;h2 id=&quot;观点&quot;&gt;观点&lt;/h2&gt;

&lt;h3 id=&quot;观点1&quot;&gt;观点1&lt;/h3&gt;

&lt;p&gt;IR 系统，从数据库中检索相似的信息，应对的是离散的数据，而 GAN 一般在连续情况下容易 work。常用的 SGD 在这里并不 work，作者采用 RL 的 policy gradient 作为替代。 IRGAN 将 generative retrieval model 和 discriminative retrieval model 分别作为 GAN 的 generator 和 discriminator（文章里对生成模型和判别模型的提法跟我们通常所说的刚好相反）。所以，IRGAN 训练的结果是两个 IR 系统！一个是生成模型，一个是判别模型！ 作者开源了代码，做了一些实验实验：web search，item recommendation，question answering，实验结果表明，IRGAN 打败了多种 strong baselines，带来显著的性能提升。作者认为，这种性能提升得益于 GAN 的对抗训练机制。两类 IR 模型统一到 GAN 框架下，虽然它们的性能不同，但是跟没有采用对抗训练的模型相比，它们之中至少有一个能够得到显著的性能提升。&lt;/p&gt;

&lt;h3 id=&quot;观点2&quot;&gt;观点2&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;信息获取主要有两种模式：a. 预测给出的文档相关性；b. 给出文档对，判别他们之间的相关性；&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;经典相关性模型着重于，如何从查询生成(相关)文档；独立模型，每个token是独立从相关文档档中生成；统计语言模型一般是从文档中生成查询元素；在词嵌模型中，词从其上下文中生成；在推荐系统中，也有类似的方法，从 item 的上下文中生成 item；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;模型扩展到 pointwise, pairwise, listwise, 其中 pointwise 基于人的判断来衡量相关性，pairwise 主要是在所有文档对中找出最相关的文档对，listwise 着重于返回最合理的相关性排序；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;观察到的正例和未观察到的正例之间会存在内在联系，生成器需要基于判别器的信息来快速推动这些未观察到的正例；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;与 conditional GAN 有些相似；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;生成模型提供了一种新的负采样方式；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;使用 IR 的奖励机制，是在传统模型中不可获取的；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;应用于：网页搜索在线排序（sf: LambdaRank, LambdaMART, RankNet）、item 推荐系统（cf matrix factorisation）、问答系统（退化成 IR 的评估）&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;观点3&quot;&gt;观点3&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;不像传统的 GAN，用噪声信号作为输入做生成，然后判别，而是将 Query 做输入，直接利用 Generative 和 Discriminative IR Models 做 GAN；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;将 RL 的 Policy Gradient 引入针对离散的输入变量；以上两个步骤使得 GAN 更具推广意义。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

</content>
 </entry>
 
 <entry>
   <title>c++11新特性</title>
   <link href="http://hxhlwf.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;/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;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;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;

</content>
 </entry>
 
 <entry>
   <title>deep & cross network for ad click predictions</title>
   <link href="http://hxhlwf.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;#概述&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方法&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;#神经网络&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://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;h4 id=&quot;field-aware-factorization-machinesffms&quot;&gt;Field-aware Factorization Machines(FFMs)&lt;/h4&gt;

&lt;h3 id=&quot;神经网络&quot;&gt;神经网络&lt;/h3&gt;

</content>
 </entry>
 
 <entry>
   <title>激活函数</title>
   <link href="http://hxhlwf.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;#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;!-- /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;Visualising Activation Functions in Neural Networks&lt;/a&gt;【可以在里面选择不同的激活函数，看图】&lt;/p&gt;

&lt;h2 id=&quot;1-step&quot;&gt;1. Step&lt;/h2&gt;

&lt;p&gt;激活函数 Step 更倾向于理论而不是实际，它模仿了生物神经元要么全有要么全无的属性。它无法应用于神经网络，因为其导数是 0（除了零点导数无定义以外），这意味着基于梯度的优化方法并不可行。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;2-identity&quot;&gt;2. Identity&lt;/h2&gt;

&lt;p&gt;通过激活函数 Identity，节点的输入等于输出。它完美适合于潜在行为是线性（与线性回归相似）的任务。当存在非线性，单独使用该激活函数是不够的，但它依然可以在最终输出节点上作为激活函数用于回归任务。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\[
\begin{align*}f(x) = x\end{align*}  
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\[
\begin{align*}f'(x) = 1\end{align*}
\]&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&quot;3-relu&quot;&gt;3. ReLU&lt;/h2&gt;

&lt;p&gt;修正线性单元（Rectified linear unit，ReLU）是神经网络中最常用的激活函数。它保留了 step 函数的生物学启发（只有输入超出阈值时神经元才激活），不过当输入为正的时候，导数不为零，从而允许基于梯度的学习（尽管在 x=0 的时候，导数是未定义的）。使用这个函数能使计算变得很快，因为无论是函数还是其导数都不包含复杂的数学运算。然而，当输入为负值的时候，ReLU 的学习速度可能会变得很慢，甚至使神经元直接无效，因为此时输入小于零而梯度为零，从而其权重无法得到更新，在剩下的训练过程中会一直保持静默。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;4-sigmoid&quot;&gt;4. Sigmoid&lt;/h2&gt;

&lt;p&gt;Sigmoid 因其在 logistic 回归中的重要地位而被人熟知，值域在 0 到 1 之间。Logistic Sigmoid（或者按通常的叫法，Sigmoid）激活函数给神经网络引进了概率的概念。它的导数是非零的，并且很容易计算（是其初始输出的函数）。然而，在分类任务中，sigmoid 正逐渐被 Tanh 函数取代作为标准的激活函数，因为后者为奇函数（关于原点对称）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;5-tanh&quot;&gt;5. Tanh&lt;/h2&gt;

&lt;p&gt;在分类任务中，双曲正切函数（Tanh）逐渐取代 Sigmoid 函数作为标准的激活函数，其具有很多神经网络所钟爱的特征。它是完全可微分的，反对称，对称中心在原点。为了解决学习缓慢和/或梯度消失问题，可以使用这个函数的更加平缓的变体（log-log、softsign、symmetrical sigmoid 等等）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;6-leaky-relu&quot;&gt;6. Leaky Relu&lt;/h2&gt;

&lt;p&gt;经典（以及广泛使用的）ReLU 激活函数的变体，带泄露修正线性单元（Leaky ReLU）的输出对负值输入有很小的坡度。由于导数总是不为零，这能减少静默神经元的出现，允许基于梯度的学习（虽然会很慢）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;7-prelu&quot;&gt;7. PReLU&lt;/h2&gt;

&lt;p&gt;参数化修正线性单元（Parameteric Rectified Linear Unit，PReLU）属于 ReLU 修正类激活函数的一员。它和 RReLU 以及 Leaky ReLU 有一些共同点，即为负值输入添加了一个线性项。而最关键的区别是，这个线性项的斜率实际上是在模型训练中学习到的。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;8-rrelu&quot;&gt;8. RReLU&lt;/h2&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;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;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;h2 id=&quot;9-elu&quot;&gt;9. ELU&lt;/h2&gt;

&lt;p&gt;指数线性单元（Exponential Linear Unit，ELU）也属于 ReLU 修正类激活函数的一员。和 PReLU 以及 RReLU 类似，为负值输入添加了一个非零输出。和其它修正类激活函数不同的是，它包括一个负指数项，从而防止静默神经元出现，导数收敛为零，从而提高学习效率。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;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;highlighter-rouge&quot;&gt;\(\alpha = 1\)&lt;/code&gt;的版本&lt;code class=&quot;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;n&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;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&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;n&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;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&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;h2 id=&quot;10-selu&quot;&gt;10. SELU&lt;/h2&gt;

&lt;p&gt;参考
&lt;a href=&quot;https://zhuanlan.zhihu.com/p/27362891&quot;&gt;引爆机器学习圈：「自归一化神经网络」提出新型激活函数SELU&lt;/a&gt;
&lt;a href=&quot;http://skyhigh233.com/blog/2017/07/21/norm/&quot;&gt;加速网络收敛——BN、LN、WN与selu&lt;/a&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;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;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;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 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;n&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;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&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;n&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;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&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;h2 id=&quot;11-srelu&quot;&gt;11. SReLU&lt;/h2&gt;

&lt;p&gt;S 型整流线性激活单元（S-shaped Rectified Linear Activation Unit，SReLU）属于以 ReLU 为代表的整流激活函数族。它由三个分段线性函数组成。其中两种函数的斜度，以及函数相交的位置会在模型训练中被学习。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;12-hard-sigmoid&quot;&gt;12. Hard Sigmoid&lt;/h2&gt;

&lt;p&gt;Hard Sigmoid 是 Logistic Sigmoid 激活函数的分段线性近似。它更易计算，这使得学习计算的速度更快，尽管首次派生值为零可能导致静默神经元/过慢的学习速率（详见 ReLU）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;13-hard-tanh&quot;&gt;13. Hard Tanh&lt;/h2&gt;

&lt;p&gt;Hard Tanh 是 Tanh 激活函数的线性分段近似。相较而言，它更易计算，这使得学习计算的速度更快，尽管首次派生值为零可能导致静默神经元/过慢的学习速率（详见 ReLU）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;h2 id=&quot;14-lecun-tanh&quot;&gt;14. LeCun Tanh&lt;/h2&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;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;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;h2 id=&quot;15-arctan&quot;&gt;15. ArcTan&lt;/h2&gt;

&lt;h2 id=&quot;16-softsign&quot;&gt;16. SoftSign&lt;/h2&gt;

&lt;p&gt;Softsign 是 Tanh 激活函数的另一个替代选择。就像 Tanh 一样，Softsign 是反对称、去中心、可微分，并返回-1 和 1 之间的值。其更平坦的曲线与更慢的下降导数表明它可以更高效地学习。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;n&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;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&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;n&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;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&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;h2 id=&quot;17-softplus&quot;&gt;17. SoftPlus&lt;/h2&gt;

&lt;h2 id=&quot;18-signum&quot;&gt;18. Signum&lt;/h2&gt;

&lt;h2 id=&quot;19-bent-identity&quot;&gt;19. Bent Identity&lt;/h2&gt;

&lt;h2 id=&quot;20-symmetrical-sigmoid&quot;&gt;20. Symmetrical Sigmoid&lt;/h2&gt;

&lt;h2 id=&quot;21-log-log&quot;&gt;21. Log Log&lt;/h2&gt;

&lt;h2 id=&quot;22-gaussian&quot;&gt;22. Gaussian&lt;/h2&gt;

&lt;h2 id=&quot;23-absolute&quot;&gt;23. Absolute&lt;/h2&gt;

&lt;h2 id=&quot;24-sinusoid&quot;&gt;24. Sinusoid&lt;/h2&gt;

&lt;h2 id=&quot;25-cos&quot;&gt;25. Cos&lt;/h2&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;h2 id=&quot;26-sinc&quot;&gt;26. Sinc&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>MUSE</title>
   <link href="http://hxhlwf.github.io/posts/nlp-muse.html"/>
   <updated>2017-12-26T00:00:00+00:00</updated>
   <id>/posts/nlp-muse</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

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

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

&lt;p&gt;基于GAN的无监督机器翻译框架
&lt;a href=&quot;https://github.com/facebookresearch/MUSE&quot;&gt;https://github.com/facebookresearch/MUSE&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>特征工程</title>
   <link href="http://hxhlwf.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;#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;/ul&gt;

&lt;!-- /TOC --&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;h2 id=&quot;1-概述&quot;&gt;1. 概述&lt;/h2&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;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;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;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;
</content>
 </entry>
 
 <entry>
   <title>Progressive growing of GANs</title>
   <link href="http://hxhlwf.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>nmt</title>
   <link href="http://hxhlwf.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;#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为何可以缓解梯度消失&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-双向gru&quot;&gt;2. 双向GRU&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-encoder-decoder框架&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-注意力机制&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-模型结构&quot;&gt;6.1 模型结构&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#611-编码器&quot;&gt;6.1.1 编码器&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#612-解码器&quot;&gt;6.1.2 解码器&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#6121-得到对齐模型&quot;&gt;6.1.2.1 得到对齐模型&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#6122-构造解码器rnn的初始状态&quot;&gt;6.1.2.2 构造解码器RNN的初始状态&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#6123-定义解码阶段每一个时间步的rnn行为&quot;&gt;6.1.2.3 定义解码阶段每一个时间步的RNN行为&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#6124-定义解码器框架名字及gru\_decoder\_with\_attention的前两个输入&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-训练模式的decoder&quot;&gt;6.2 训练模式的decoder&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#63-生成模型的解码器&quot;&gt;6.3 生成模型的解码器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#64-attention的可视化&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-应用&quot;&gt;7. 应用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#71-backbone模型&quot;&gt;7.1 Backbone模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#72-其他一些尝试&quot;&gt;7.2 其他一些尝试&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#73-上述模型存在的问题&quot;&gt;7.3 上述模型存在的问题&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#74-采用增强学习进行优化&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进一步改进的方向&quot;&gt;7.6 rl进一步改进的方向&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#77-模型融合&quot;&gt;7.7 模型融合&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#8-其他&quot;&gt;8. 其他&lt;/a&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;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;highlighter-rouge&quot;&gt;\(\circ \)&lt;/code&gt;表示element-wise的乘积：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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+W_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;highlighter-rouge&quot;&gt;\(c_0=0\)&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h_0=0\)&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\circ \)&lt;/code&gt;表示element-wise的乘积，也就是图中的&lt;code class=&quot;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;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;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;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;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;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;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;highlighter-rouge&quot;&gt;\(W \in R^{h \times d}\)&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(U \in R^{h \times h}\)&lt;/code&gt;和&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\sigma _g\)&lt;/code&gt;: sigmoid&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\sigma _c\)&lt;/code&gt;: tanh&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\sigma _h\)&lt;/code&gt;: tanh，但在peephole LSTM中，这个是&lt;code class=&quot;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;code class=&quot;highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;了，大部分都换成了&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c_{t-1}\)&lt;/code&gt;。。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(\sigma _c(xxxh_{t-1})\)&lt;/code&gt;【其中&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\sigma _c\)&lt;/code&gt;是tanh】乘以w，然后再算sigmoid。而lstm对这项只是乘了一个(0,1)的系数(&lt;code class=&quot;highlighter-rouge&quot;&gt;\(i_t\)&lt;/code&gt;)，然后加上&lt;code class=&quot;highlighter-rouge&quot;&gt;\(f_t \dot c_{t-1}\)&lt;/code&gt;，再求tanh再乘一个(0,1)的系数（&lt;code class=&quot;highlighter-rouge&quot;&gt;\(o_t\)&lt;/code&gt;），也就是说，并没有乘w，只是和一堆（0,1）的系数进行了线性变换，然后求和。。。&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;highlighter-rouge&quot;&gt;\(z_t\)&lt;/code&gt;【更新门】和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(r_t\)&lt;/code&gt;【重置门】都作用(sigmoid)于&lt;code class=&quot;highlighter-rouge&quot;&gt;\([h_{t-1},x_t]\)&lt;/code&gt;【即上一时刻的隐层状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;和这一时刻的输入&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_t\)&lt;/code&gt;】。&lt;/p&gt;

&lt;p&gt;而重置门作用于&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h_{t-1}\)&lt;/code&gt;得到&lt;code class=&quot;highlighter-rouge&quot;&gt;\(r_t*h_{t-1}\)&lt;/code&gt;【注意，是element-wise乘积】再与&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_t\)&lt;/code&gt;一起经过tanh得到节点状态。&lt;/p&gt;

&lt;p&gt;最终的输出是更新门与节点状态相乘，再加上&lt;code class=&quot;highlighter-rouge&quot;&gt;\(1-z_t\)&lt;/code&gt;与上一时刻的隐层状态&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;(&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c=qh\)&lt;/code&gt;，如果不用注意力，可以直接取最后一个时间步的编码&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;。&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_0\)&lt;/code&gt;是开始标志&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;, &lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;用的是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;，而这里用的是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;，也就是，对每个真实目标语言序列中的词&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;【注意！！是目标语言的i，不是源语言的！！】，都有一个特定的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;与之对应。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;进行加权平均实现的。权重&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a_{ij}\)&lt;/code&gt;计算方法如下：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;）和&lt;strong&gt;源语言第j个词&lt;/strong&gt;（第j个词的上下文片段&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;，和i时刻RNN的隐层状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;，计算出下一个隐层状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;将&lt;code class=&quot;highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;通过softmax【只针对beamsearch得到的可能的解做处理】归一化，得到的目标语言序列的第i+1个单词的概率分布&lt;code class=&quot;highlighter-rouge&quot;&gt;\(p_{i+1}\)&lt;/code&gt;
&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(p_{i+1}\)&lt;/code&gt;采样出单词&lt;code class=&quot;highlighter-rouge&quot;&gt;u_{i+1}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;重复前三个步骤，直到&lt;strong&gt;获得句子的结束标记&lt;code class=&quot;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;c&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;c&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;c&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;c&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;c&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;c&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;c&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;c&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;o&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;【解码器中的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;】即&lt;code class=&quot;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;networks&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;networks&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;】过一个fc，得到其映射&lt;code class=&quot;highlighter-rouge&quot;&gt;encoded_proj&lt;/code&gt;【即，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;的映射，也就是对齐模型&lt;code class=&quot;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&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;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;highlighter-rouge&quot;&gt;src_backward&lt;/code&gt;）的最后一个状态(&lt;code class=&quot;highlighter-rouge&quot;&gt;backward_first&lt;/code&gt;)进行非线性映射fc，作为源语言上下文向量&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;的初始值（&lt;code class=&quot;highlighter-rouge&quot;&gt;decoder_boot&lt;/code&gt;），即&lt;code class=&quot;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&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;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;highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;、解码器隐层状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;、真实目标语言中的第i个词&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;】，来预测第i+1个词的概率&lt;code class=&quot;highlighter-rouge&quot;&gt;\(p_{i+1}\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;主要过程（输入&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;【即&lt;code class=&quot;highlighter-rouge&quot;&gt;enc_vec&lt;/code&gt;】、&lt;code class=&quot;highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;【即&lt;code class=&quot;highlighter-rouge&quot;&gt;enc_proj&lt;/code&gt;】和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;【即&lt;code class=&quot;highlighter-rouge&quot;&gt;current_word&lt;/code&gt;】：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;通过&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c_0\)&lt;/code&gt;的初始化，得到解码器隐层状态&lt;code class=&quot;highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;【即&lt;code class=&quot;highlighter-rouge&quot;&gt;decoder_mem&lt;/code&gt;】&lt;/li&gt;
  &lt;li&gt;然后通过&lt;code class=&quot;highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;生成attention&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a_{ij}\)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;再将&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a_{ij}\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;一起得到&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;【和上一步一起，封装在simple_attention函数中，得到&lt;code class=&quot;highlighter-rouge&quot;&gt;context&lt;/code&gt;】&lt;/li&gt;
  &lt;li&gt;接下来，通过&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c_i\)&lt;/code&gt;、&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(z_i\)&lt;/code&gt;得到&lt;code class=&quot;highlighter-rouge&quot;&gt;\(z_{i+1}\)&lt;/code&gt;【先&lt;code class=&quot;highlighter-rouge&quot;&gt;context&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;current_word&lt;/code&gt;一起经过一个fc，得到&lt;code class=&quot;highlighter-rouge&quot;&gt;decoder_inputs&lt;/code&gt;，然后和&lt;code class=&quot;highlighter-rouge&quot;&gt;decoder_mem&lt;/code&gt;一起给&lt;code class=&quot;highlighter-rouge&quot;&gt;gru_step&lt;/code&gt;做输入，得到&lt;code class=&quot;highlighter-rouge&quot;&gt;gru_step&lt;/code&gt;】&lt;/li&gt;
  &lt;li&gt;最后，对使用softmax归一化计算单词的概率，即通过&lt;code class=&quot;highlighter-rouge&quot;&gt;full_matrix_projection&lt;/code&gt;+softmax，得到&lt;code class=&quot;highlighter-rouge&quot;&gt;out&lt;/code&gt;【即公式：&lt;code class=&quot;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;networks&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&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;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;o&quot;&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&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;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;highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;需要分别通过&lt;code class=&quot;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;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;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;o&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;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;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;o&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;o&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;o&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;c&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;o&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;o&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;c&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;o&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;o&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;c&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;highlighter-rouge&quot;&gt;trg_embedding&lt;/code&gt;，作为&lt;code class=&quot;highlighter-rouge&quot;&gt;current_word&lt;/code&gt;，即&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_i\)&lt;/code&gt;，放到&lt;code class=&quot;highlighter-rouge&quot;&gt;group_inputs&lt;/code&gt;中&lt;/li&gt;
  &lt;li&gt;然后使用&lt;code class=&quot;highlighter-rouge&quot;&gt;recurrent_group&lt;/code&gt;，step指定为&lt;code class=&quot;highlighter-rouge&quot;&gt;gru_decoder_with_attention&lt;/code&gt;，input为&lt;code class=&quot;highlighter-rouge&quot;&gt;group_inputs&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;使用目标语言的下一个词序列&lt;code class=&quot;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&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;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;o&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;c&quot;&gt;# For decoder equipped with attention mechanism, in training,&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# target embeding (the groudtruth) is the data input,&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# while encoded source sequence is accessed to as an unbounded memory.&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# Here, the StaticInput defines a read-only memory&lt;/span&gt;
    &lt;span class=&quot;c&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_type&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;highlighter-rouge&quot;&gt;beam_search&lt;/code&gt;函数循环调用&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(h_j\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(e_{ij}\)&lt;/code&gt;的StaticInput，第三个输入有区别：训练是目标语言序列的embedding，而生成使用的是GeneratedInput&lt;/li&gt;
  &lt;li&gt;使用&lt;code class=&quot;highlighter-rouge&quot;&gt;beam_search&lt;/code&gt;，而训练使用的是&lt;code class=&quot;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;c&quot;&gt;# In generation, the decoder predicts a next target word based on&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# the encoded source sequence and the previous generated target word.&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# The encoded source sequence (encoder's output) must be specified by&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# StaticInput, which is a read-only memory.&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# Embedding of the previous generated word is automatically retrieved&lt;/span&gt;
    &lt;span class=&quot;c&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;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;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;highlighter-rouge&quot;&gt;\(\theta \)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\pi _\theta (\cdot )\)&lt;/code&gt;为现有的翻译模型&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\tau&lt;/code&gt;为根据现有模型所生成的一个翻译结果&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\{t_i\}^K_{i=1}\)&lt;/code&gt;，然后根据bleu score计算平均值：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;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;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;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;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;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;o&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;c&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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pooling&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pooling&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layer&lt;/span&gt;&lt;span class=&quot;o&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;o&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;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;o&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;

</content>
 </entry>
 
 <entry>
   <title>alphago-zero</title>
   <link href="http://hxhlwf.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://hxhlwf.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;#安装&quot;&gt;安装&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#有一个装了tf的py&quot;&gt;有一个装了tf的py&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#安装openmpi&quot;&gt;安装openmpi&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#pip安装&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;/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;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;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;highlighter-rouge&quot;&gt;/home/work/tools/openmpi/bin/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;如果是GPU的，确保&lt;code class=&quot;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;highlighter-rouge&quot;&gt;-lpython2.7&lt;/code&gt;，如果没有root权限，去报错的gcc命令里找&lt;code class=&quot;highlighter-rouge&quot;&gt;-L&lt;/code&gt;的路径，发现最简单粗暴的方法就是&lt;/p&gt;

&lt;div class=&quot;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;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;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://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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; | awk &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://hxhlwf.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://hxhlwf.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://hxhlwf.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;#单机多gpu&quot;&gt;单机多gpu&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#分布式多机多gpu训练&quot;&gt;分布式多机多GPU训练&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;#tensorflow的分布式&quot;&gt;tensorflow的分布式&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#in-graph-模式&quot;&gt;in-graph 模式&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#between-graph模式&quot;&gt;between-graph模式&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;#replica_device_setter&quot;&gt;replica_device_setter&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#round-robin模式&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;#故障恢复&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;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>Modeling Interestingness with Deep Neural Networks(DSSM)</title>
   <link href="http://hxhlwf.github.io/posts/nlp-dssm.html"/>
   <updated>2017-09-01T00:00:00+00:00</updated>
   <id>/posts/nlp-dssm</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

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

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#2013年cikm的dssm&quot;&gt;2013年CIKM的dssm&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2014年cikm的dssm-引入cnn&quot;&gt;2014年CIKM的dssm: 引入cnn&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2014年的dssm&quot;&gt;2014年的dssm&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;参考&lt;a href=&quot;https://www.microsoft.com/en-us/research/wp-content/uploads/2014/10/604_Paper.pdf&quot;&gt;Modeling Interestingness with Deep Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;对应的ppt：
&lt;a href=&quot;../assets/Modeling Interestingness with Deep Neural Networks_ppt.pdf&quot;&gt;ppt&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;2013年cikm的dssm&quot;&gt;2013年CIKM的dssm&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/cikm2013_DSSM_fullversion.pdf&quot;&gt;Learning Deep Structured Semantic Models for Web Search using Clickthrough Data&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;相当于一个q，和每个d分别算cos。&lt;/p&gt;

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

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

&lt;/html&gt;

&lt;h2 id=&quot;2014年cikm的dssm-引入cnn&quot;&gt;2014年CIKM的dssm: 引入cnn&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://www.iro.umontreal.ca/~lisa/pointeurs/ir0895-he-2.pdf&quot;&gt;A Latent Semantic Model with Convolutional-Pooling Structure for Information Retrieval&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;2014年的dssm&quot;&gt;2014年的dssm&lt;/h2&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;a href=&quot;https://www.microsoft.com/en-us/research/wp-content/uploads/2014/10/604_Paper.pdf&quot;&gt;Modeling Interestingness with Deep Neural Networks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;paddle上，基于dssm，做了支持pairwise rank和分类/回归的一个通用模型：
&lt;a href=&quot;https://github.com/PaddlePaddle/models/blob/develop/dssm/README.cn.md&quot;&gt;https://github.com/PaddlePaddle/models/blob/develop/dssm/README.cn.md&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>各框架的embedding实现</title>
   <link href="http://hxhlwf.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;#基本思想&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;#应用&quot;&gt;应用&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#xx率预估&quot;&gt;xx率预估&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;#编码方式embedding&quot;&gt;编码方式（embedding）&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;#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;highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;，embedding向量的维度假设是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;。那么，相当于输入一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;维的&lt;strong&gt;one-hot&lt;/strong&gt;向量，输出一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;维的向量，中间就是一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(V\times K\)&lt;/code&gt;的矩阵&lt;code class=&quot;highlighter-rouge&quot;&gt;\(W\)&lt;/code&gt;。假设&lt;code class=&quot;highlighter-rouge&quot;&gt;\(K=3,V=2\)&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;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;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;应用&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://hxhlwf.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;highlighter-rouge&quot;&gt;~/.keras/keras.json&lt;/code&gt;，修改为&lt;/p&gt;
&lt;div class=&quot;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;o&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;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;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;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;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://hxhlwf.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;s&quot;&gt;&quot;a = $a\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&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;s&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;s&quot;&gt;&quot;$var\n&quot;&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;'EOF';
这是一个 Here 文档实例，使用单引号。
例如：a = $a
EOF
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;$var\n&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;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;s&quot;&gt;&quot;123&quot;&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;/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;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;s&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;s&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;s&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;s&quot;&gt;&quot;\$data{'google'} = $data{'google'}\n&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;\$data{'runoob'} = $data{'runoob'}\n&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;\$data{'taobao'} = $data{'taobao'}\n&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;h2 id=&quot;haha&quot;&gt;haha&lt;/h2&gt;
</content>
 </entry>
 
 <entry>
   <title>推荐系统</title>
   <link href="http://hxhlwf.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;!-- /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;

</content>
 </entry>
 
 <entry>
   <title>python generator</title>
   <link href="http://hxhlwf.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;

</content>
 </entry>
 
 <entry>
   <title>mr shuffle</title>
   <link href="http://hxhlwf.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>cvpr2017 hekaiming的Learning Deep Features for Visual Recognition</title>
   <link href="http://hxhlwf.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;#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;!-- /TOC --&gt;

&lt;h2 id=&quot;convolutional-neural-networks-recap&quot;&gt;Convolutional Neural Networks: Recap&lt;/h2&gt;

&lt;h2 id=&quot;resnet&quot;&gt;ResNet&lt;/h2&gt;

&lt;h2 id=&quot;resnext&quot;&gt;ResNeXt&lt;/h2&gt;
</content>
 </entry>
 
 <entry>
   <title>梯度下降优化算法</title>
   <link href="http://hxhlwf.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;#梯度下降&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;/ul&gt;

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

&lt;h2 id=&quot;梯度下降&quot;&gt;梯度下降&lt;/h2&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;highlighter-rouge&quot;&gt;\[
\\v_t=\gamma v_{t-1}+ \eta \triangledown _\theta\triangledown J(\theta)
\\\theta=\theta-v_t    
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\eta\)&lt;/code&gt;一开始初始化为0.5，后面变成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;h2 id=&quot;nag&quot;&gt;NAG&lt;/h2&gt;

&lt;h2 id=&quot;adagrad&quot;&gt;Adagrad&lt;/h2&gt;

&lt;h2 id=&quot;adadelta&quot;&gt;Adadelta&lt;/h2&gt;

&lt;h2 id=&quot;rmsprop&quot;&gt;RMSprop&lt;/h2&gt;

&lt;h2 id=&quot;adam&quot;&gt;Adam&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>海量数据相似数据查找方法</title>
   <link href="http://hxhlwf.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;#高维稀疏向量的相似查找minhash-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算法&quot;&gt;simhash算法&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;#一次拆分多份table并exact-match&quot;&gt;一次拆分多份table并exact match&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#k3时的一般化方法&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;#稠密向量的相似查找annoy&quot;&gt;稠密向量的相似查找——Annoy&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;/ul&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;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))= d。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;同样可以定义&lt;strong&gt;n个哈希函数&lt;/strong&gt;【不需要重排，每个hash计算对应的值就行】，进行上述操作，那每个集合S就被降维到n维空间的签名。&lt;/p&gt;

&lt;h3 id=&quot;lshlocality-sensitive-hashing&quot;&gt;LSH（Locality-Sensitive Hashing）&lt;/h3&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;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 &amp;lt;bitset&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;climits&amp;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;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;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;highlighter-rouge&quot;&gt;\(\pi _i\)&lt;/code&gt;将64-bit的fingerprint中的某&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(p_i\)&lt;/code&gt;位&lt;/strong&gt;与&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\pi _i(F)\)&lt;/code&gt;的前&lt;code class=&quot;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;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;

</content>
 </entry>
 
 <entry>
   <title>ftrl</title>
   <link href="http://hxhlwf.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;#背景&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;#test&quot;&gt;test&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;p&gt;传统的批量（batch）算法无法有效地处理超大规模的数据集和在线数据流，FTRL（Follow-the-regularized-Leader）在处理诸如逻辑回归之类的带非光滑正则化项（例如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;对于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;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;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;h3 id=&quot;test&quot;&gt;test&lt;/h3&gt;

</content>
 </entry>
 
 <entry>
   <title>异常检测常用算法</title>
   <link href="http://hxhlwf.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-概述&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-作用于numeric-data的方法&quot;&gt;2.1 作用于numeric data的方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-作用于categorical-data的方法&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;/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;highlighter-rouge&quot;&gt;\(d\)&lt;/code&gt;个样本，大部分方法时间复杂度都是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(O(d^2)\)&lt;/code&gt;,如kNN/LOF；如果用R*-Tree之类的索引方法，可以达到&lt;code class=&quot;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;

</content>
 </entry>
 
 <entry>
   <title>youtube视频推荐系统</title>
   <link href="http://hxhlwf.github.io/posts/dl-youtube-video-recommendation.html"/>
   <updated>2017-08-04T00:00:00+00:00</updated>
   <id>/posts/dl-youtube-video-recommendation</id>
   <content type="html">&lt;p&gt;目录&lt;/p&gt;

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

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#候选生成网络candidate-generation-network&quot;&gt;候选生成网络（Candidate Generation 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/155797861_465975&quot;&gt;http://www.sohu.com/a/155797861_465975&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://static.googleusercontent.com/media/research.google.com/zh-CN//pubs/archive/45530.pdf&quot;&gt;Deep neural networks for youtube recommendations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;YouTube是世界上最大的视频上传、分享和发现网站，YouTube推荐系统为超过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;。&lt;/p&gt;

&lt;h2 id=&quot;候选生成网络candidate-generation-network&quot;&gt;候选生成网络（Candidate Generation Network）&lt;/h2&gt;

&lt;p&gt;候选生成网络将推荐问题建模为一个&lt;strong&gt;类别数极大的多分类问题&lt;/strong&gt;：对于一个Youtube用户，使用其观看历史（视频ID）、搜索词记录（search tokens）、人口学信息（如地理位置、用户登录设备）、二值特征（如性别，是否登录）和连续特征（如用户年龄）等，对视频库中所有视频进行多分类，得到每一类别的分类结果（即每一个视频的推荐概率），最终输出概率较高的几百个视频。===&amp;gt;即，【使用&lt;strong&gt;用户特征&lt;/strong&gt;，对所有视频进行分类，得到&lt;strong&gt;和这个用户最相关的几百个候选结果。&lt;/strong&gt;】&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>传统ctr预估</title>
   <link href="http://hxhlwf.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;#1-传统方法&quot;&gt;1. 传统方法：&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-lrgbdt&quot;&gt;1.1 LR+GBDT&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#111-lr&quot;&gt;1.1.1 LR&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#112-gbdt&quot;&gt;1.1.2 GBDT&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#1121-回归树&quot;&gt;1.1.2.1 回归树&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#1122-提升树算法boosting-decision-tree&quot;&gt;1.1.2.2 提升树算法（Boosting Decision Tree）&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#1123-gbdt&quot;&gt;1.1.2.3 GBDT&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#1124-参数设置&quot;&gt;1.1.2.4 参数设置&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#113-lrgbdt&quot;&gt;1.1.3 LR+GBDT&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#114-引入id类特征&quot;&gt;1.1.4 引入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;1-传统方法&quot;&gt;1. 传统方法：&lt;/h2&gt;

&lt;h3 id=&quot;11-lrgbdt&quot;&gt;1.1 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;111-lr&quot;&gt;1.1.1 LR&lt;/h4&gt;

&lt;p&gt;LR映射后的函数值就是CTR的预估值。这种线性模型很容易并行化，处理上亿条训练样本不是问题，但线性模型学习能力有限，需要大量特征工程预先分析出有效的特征、特征组合，从而去间接增强LR 的非线性学习能力。&lt;/p&gt;

&lt;p&gt;LR模型中的特征组合很关键，但又无法直接通过特征笛卡尔积 解决，只能依靠人工经验，耗时耗力同时并不一定会带来效果提升。&lt;/p&gt;

&lt;h4 id=&quot;112-gbdt&quot;&gt;1.1.2 GBDT&lt;/h4&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;1121-回归树&quot;&gt;1.1.2.1 回归树&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;1122-提升树算法boosting-decision-tree&quot;&gt;1.1.2.2 提升树算法（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;1123-gbdt&quot;&gt;1.1.2.3 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;1124-参数设置&quot;&gt;1.1.2.4 参数设置&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;113-lrgbdt&quot;&gt;1.1.3 LR+GBDT&lt;/h4&gt;

&lt;p&gt;GBDT与LR的融合方式，Facebook的paper有个例子如下图2所示，图中Tree1、Tree2为通过GBDT模型学出来的两颗树，x为一条输入样本，遍历两棵树后，x样本分别落到两颗树的叶子节点上，每个叶子节点对应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;114-引入id类特征&quot;&gt;1.1.4 引入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;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://hxhlwf.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://hxhlwf.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-学习率&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;/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;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linalg&lt;/span&gt;&lt;span class=&quot;o&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;

</content>
 </entry>
 
 <entry>
   <title>机器学习相关tips</title>
   <link href="http://hxhlwf.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;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-关于auc和prf1&quot;&gt;1.1 关于auc和p/r/f1…&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#12-auc&quot;&gt;1.2 auc&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#13-roc-curve-vs-precision-recall-curve&quot;&gt;1.3 roc curve v.s. precision-recall curve&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#131-roc与pr曲线&quot;&gt;1.3.1 ROC与PR曲线&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;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;#数据增强data-augmentation&quot;&gt;数据增强（Data Augmentation）&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-训练时间early-stopping&quot;&gt;2.2.2 训练时间（early stopping）&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;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#2241-在输入中加噪声&quot;&gt;2.2.4.1 在输入中加噪声&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#2242-在权值上加噪声&quot;&gt;2.2.4.2 在权值上加噪声&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#2243-对网络的响应加噪声&quot;&gt;2.2.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;#23-使用多种模型&quot;&gt;2.3 使用多种模型&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#231-bagging&quot;&gt;2.3.1 bagging&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#232-boosting&quot;&gt;2.3.2 boosting&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#233-dropout&quot;&gt;2.3.3 dropout&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;/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-how-much-training-data-do-you-need&quot;&gt;3.1 How much training data do you need&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-lstm参数规模计算&quot;&gt;3.2 lstm参数规模计算&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#33-embedding-layer参数规模计算&quot;&gt;3.3 embedding layer参数规模计算&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2 id=&quot;1-评价指标&quot;&gt;1. 评价指标&lt;/h2&gt;

&lt;h3 id=&quot;11-关于auc和prf1&quot;&gt;1.1 关于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;12-auc&quot;&gt;1.2 auc&lt;/h2&gt;

&lt;p&gt;首先，P/R/accuracy/F-measure如下：
&lt;code class=&quot;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;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;从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;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;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;13-roc-curve-vs-precision-recall-curve&quot;&gt;1.3 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呢？因为ROC曲线有个很好的特性：当测试集中的正负样本的分布变化的时候，ROC曲线能够保持不变。在实际的数据集中经常会出现类不平衡（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;131-roc与pr曲线&quot;&gt;1.3.1 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;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;highlighter-rouge&quot;&gt;\(P(Y=1)\)&lt;/code&gt;是多少，他们都是一样的。&lt;/p&gt;

&lt;p&gt;而precision和sensitivity组成了pr曲线。Precision基于的条件是你预测的，所以，当不同的数据集有不同的类别分布时，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(P(Y=1)\)&lt;/code&gt;就会有差别，所以就有不同的baseline的&lt;code class=&quot;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;2-过拟合问题&quot;&gt;2. 过拟合问题&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;21-获取更多数据&quot;&gt;2.1 获取更多数据&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;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;/p&gt;

&lt;h4 id=&quot;222-训练时间early-stopping&quot;&gt;2.2.2 训练时间（early stopping）&lt;/h4&gt;

&lt;p&gt;当网络权值较小时，神经元的激活函数工作在线性区，此时神经元的拟合能力较弱（类似线性神经元）。权值太大or太小时，例如sigmoid，就会出现梯度消失。&lt;/p&gt;

&lt;p&gt;因为我们在初始化网络的时候一般都是初始为较小的权值。训练时间越长，部分网络权值可能越大。如果我们在合适时间停止训练，就可以将网络的能力限制在一定范围内。&lt;/p&gt;

&lt;h4 id=&quot;223-限制权值正则化&quot;&gt;2.2.3 限制权值/正则化&lt;/h4&gt;

&lt;p&gt;原理同上，但是这类方法直接将权值的大小加入到 Cost 里，在训练的时候限制权值变大。例如L2：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(C_0\)&lt;/code&gt;，另一方面也能降低权值大小。&lt;/p&gt;

&lt;h4 id=&quot;224-增加噪声&quot;&gt;2.2.4 增加噪声&lt;/h4&gt;

&lt;h5 id=&quot;2241-在输入中加噪声&quot;&gt;2.2.4.1 在输入中加噪声&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;2242-在权值上加噪声&quot;&gt;2.2.4.2 在权值上加噪声&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;2243-对网络的响应加噪声&quot;&gt;2.2.4.3 对网络的响应加噪声&lt;/h5&gt;

&lt;p&gt;如在前向传播过程中，让默写神经元的输出变为 binary 或 random。这种做法会打乱网络的训练过程，让训练更慢，但据 Hinton 说，在测试集上效果会有显著提升。&lt;/p&gt;

&lt;h3 id=&quot;23-使用多种模型&quot;&gt;2.3 使用多种模型&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;231-bagging&quot;&gt;2.3.1 bagging&lt;/h4&gt;

&lt;p&gt;简单理解，就是分段函数的概念：用不同的模型拟合不同部分的训练集。以随机森林（Rand Forests）为例，就是训练了一堆互不关联的决策树。但由于训练神经网络本身就需要耗费较多自由，所以一般不单独使用神经网络做Bagging。&lt;/p&gt;

&lt;h4 id=&quot;232-boosting&quot;&gt;2.3.2 boosting&lt;/h4&gt;

&lt;p&gt;既然训练复杂神经网络比较慢，那我们就可以只使用简单的神经网络（层数、神经元数限制等）。通过训练一系列简单的神经网络，加权平均其输出。&lt;/p&gt;

&lt;h4 id=&quot;233-dropout&quot;&gt;2.3.3 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;24-贝叶斯方法&quot;&gt;2.4 贝叶斯方法&lt;/h3&gt;

&lt;h2 id=&quot;3-模型参数与训练样本规模的关系&quot;&gt;3. 模型参数与训练样本规模的关系&lt;/h2&gt;

&lt;h3 id=&quot;31-how-much-training-data-do-you-need&quot;&gt;3.1 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;32-lstm参数规模计算&quot;&gt;3.2 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;33-embedding-layer参数规模计算&quot;&gt;3.3 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;
</content>
 </entry>
 
 <entry>
   <title>图文相关性模型简介</title>
   <link href="http://hxhlwf.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;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;highlighter-rouge&quot;&gt;\(C_{i,j}\)&lt;/code&gt;是cross-entropy cost。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(o_i\)&lt;/code&gt;和&lt;code class=&quot;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://hxhlwf.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-简介&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-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-背景&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;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-生成过程&quot;&gt;4.2.6 生成过程&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#43-训练&quot;&gt;4.3 训练&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#431-训练数据和batching&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-结果&quot;&gt;4.4 结果&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#441-机器翻译&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-实践&quot;&gt;6. 实践&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#61-安装cuda-80&quot;&gt;6.1 安装cuda 8.0&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#62-安装tensor2tensor&quot;&gt;6.2 安装tensor2tensor&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#7-代码解析&quot;&gt;7. 代码解析&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#71-原版tensor2tensor&quot;&gt;7.1 原版（tensor2tensor）&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#72-单纯transformer&quot;&gt;7.2 单纯transformer&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=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;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;highlighter-rouge&quot;&gt;\(d_{model}=512\)&lt;/code&gt;。&lt;/strong&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;&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;highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_v\)&lt;/code&gt;。&lt;/strong&gt;最终的输出是各value的加权组合（v的权重w其实就是下面公式里V的系数&lt;code class=&quot;highlighter-rouge&quot;&gt;\(softmax(\frac{QK^T}{\sqrt{d_k}})\)&lt;/code&gt;）：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\sqrt{d_k}\)&lt;/code&gt;，然后算softmax，得到的就是value的weight。实际应用中，一系列的query，记为矩阵&lt;code class=&quot;highlighter-rouge&quot;&gt;\(Q\)&lt;/code&gt;，同样地，有&lt;code class=&quot;highlighter-rouge&quot;&gt;\(K\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;不大时，additive attention比没用scale的dot-product attention表现更好。但&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_k\)&lt;/code&gt;很大时，点积会变得特别大，所以softmax会出现有些区域的梯度变得特别小（&lt;strong&gt;例如，假设&lt;code class=&quot;highlighter-rouge&quot;&gt;\(q\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(k\)&lt;/code&gt;是独立的随机变量，均值都是0，方差都是1，那么他们的点积&lt;code class=&quot;highlighter-rouge&quot;&gt;\(qk=\sum^{d_k}_{i=1}q_ik_i\)&lt;/code&gt;的均值是0，方差是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_k*1^2=d_k\)&lt;/code&gt;。【详见下面的两个随机变量的和与积的方差推导】&lt;/strong&gt;），因此，需要通过除以&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\sqrt{d_k}\)&lt;/code&gt;来scale。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维【前面提到了，架构中的所有子层（包括embedding）,输出的维度均是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_{model}=512\)&lt;/code&gt;】的query，key，value。而是把这&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维拆成&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;份，所以&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_k=d_v=d_{model}/h\)&lt;/code&gt;,(因为&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;是一样的，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;也是一样的【本文设为8】，所以&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_k=d_v\)&lt;/code&gt;)。而这个变换通过对&lt;code class=&quot;highlighter-rouge&quot;&gt;\(Q,K,W\)&lt;/code&gt;各自进行一个线性变换，变成&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_k,d_k,d_v\)&lt;/code&gt;维即可，最终通过concat(把&lt;code class=&quot;highlighter-rouge&quot;&gt;\(h\)&lt;/code&gt;个结果首尾相连),然后再做一个线性变换，变成&lt;code class=&quot;highlighter-rouge&quot;&gt;\(d_{model}\)&lt;/code&gt;维。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\[
\\MultiHead(Q,K,V)=Concat(head_1,...,head_h)W^Q
\\where\ head_i=Attention(QW_i^Q,KW_i^K,VW_i^V)
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其中，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(W_i^Q\in \mathbb{R}^{d_{model}\times d_k}\)&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(W_i^K\in \mathbb{R}^{d_{model}\times d_k}\)&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(W_i^V\in \mathbb{R}^{d_{model}\times d_v}\)&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(W_i^O\in \mathbb{R}^{hd_v\times d_{model}}\)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&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;/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;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;highlighter-rouge&quot;&gt;\(d_{model}=512\)&lt;/code&gt;，inner-layer的维度是&lt;code class=&quot;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;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;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;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;highlighter-rouge&quot;&gt;\(PE_{pos+k}\)&lt;/code&gt;能被表示为&lt;code class=&quot;highlighter-rouge&quot;&gt;\(PE_{pos}\)&lt;/code&gt;的线性函数 。&lt;/p&gt;

&lt;h4 id=&quot;426-生成过程&quot;&gt;4.2.6 生成过程&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;h4 id=&quot;442-model-variations&quot;&gt;4.4.2 model variations&lt;/h4&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;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;h3 id=&quot;61-安装cuda-80&quot;&gt;6.1 安装cuda 8.0&lt;/h3&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;h3 id=&quot;62-安装tensor2tensor&quot;&gt;6.2 安装tensor2tensor&lt;/h3&gt;

&lt;div class=&quot;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;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;
</content>
 </entry>
 
 <entry>
   <title>fasttext</title>
   <link href="http://hxhlwf.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-原理&quot;&gt;0. 原理&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#01-softmax回归&quot;&gt;0.1 softmax回归&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#02-分层softmax&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-前向传播&quot;&gt;0.5.1 前向传播&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#052-反向传播&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使用方法&quot;&gt;1. bin使用方法&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;#2-python使用方法&quot;&gt;2. python使用方法&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;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;highlighter-rouge&quot;&gt;\(|y|\)&lt;/code&gt;很大时非常耗时。分层softmax的基本思想是使用树的层级结构替代扁平化的标准Softmax，在计算&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(L(y_j)\)&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(l\)&lt;/code&gt;表示第几层（从1开始）；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\sigma (\cdot )\)&lt;/code&gt;表示sigmoid函数；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(LC(n)\)&lt;/code&gt;表示n节点的左孩子；&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\left \lfloor \right \rfloor \)&lt;/code&gt;是一个特殊的函数，定义如下：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\theta _{n(y_j,l)}\)&lt;/code&gt;是中间节点&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(y_2\)&lt;/code&gt;的路径&lt;/strong&gt;，路径长度&lt;code class=&quot;highlighter-rouge&quot;&gt;\(L(y_2)=3\)&lt;/code&gt;，&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(y_2\)&lt;/code&gt;，其实是做了3次二分类的lr。通过分层的Softmax，计算复杂度一下从&lt;code class=&quot;highlighter-rouge&quot;&gt;\(|K|\)&lt;/code&gt;降低到&lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(log_2|K|\)&lt;/code&gt;&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;04-n-gram&quot;&gt;0.4 n-gram&lt;/h3&gt;

&lt;p&gt;基本思想是将文本内容按照字节顺序进行大小为N的滑动窗口操作，最终形成长度为N的字节片段序列。n-gram产生的特征只是作为文本特征的候选集，你后面可能会采用信息熵、卡方统计、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;highlighter-rouge&quot;&gt;\({x_1,...,x_c}\)&lt;/code&gt;组成，&lt;code class=&quot;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;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;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;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;highlighter-rouge&quot;&gt;\(u_j\)&lt;/code&gt;就是一个N维向量和N维向量的转置乘出来的一个值，&lt;/p&gt;

&lt;p&gt;最后，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(u_j\)&lt;/code&gt;作为softmax的输入，得到&lt;code class=&quot;highlighter-rouge&quot;&gt;\(y_j\)&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;
&amp;lt;img src='../assets/fasttext-back-prop.png
'style='max-height: 200px'/&amp;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;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;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;highlighter-rouge&quot;&gt;\(x_n\)&lt;/code&gt;是第n篇文档的normalized bag of features。A是基于word的look-up table，即词的embedding向量，&lt;code class=&quot;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;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;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;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;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;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;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;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;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;

</content>
 </entry>
 
 <entry>
   <title>安装wget 1.18+</title>
   <link href="http://hxhlwf.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;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;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://hxhlwf.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;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;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;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;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;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;highlighter-rouge&quot;&gt;@@&lt;/code&gt;进行切分。&lt;/p&gt;

&lt;div class=&quot;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;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;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;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;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;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;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;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;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;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;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;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;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;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://hxhlwf.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;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://hxhlwf.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://hxhlwf.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;/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;
</content>
 </entry>
 
 <entry>
   <title>tf step by step: chap 1. 基础知识</title>
   <link href="http://hxhlwf.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;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/tf communication mechanism.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这样的通讯机制可以转化为发送节点和接收节点的实现问题，用户无须设计节点间的通信流程，可以用同一套代码自动扩展到不同硬件环境并处理复杂的通信流程。下图就是cpu和gpu间的通讯。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/cpu gpu communication.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;代码层面，从单机单设备到单机多设备的修改，只要一行，就可以实现单gpu到多gpu的修改。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/code 1 gpu to multi gpu.png&quot; alt=&quot;&quot; /&gt;&lt;/p&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;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/gpu cluster accelerate.png&quot; alt=&quot;&quot; /&gt;&lt;/p&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;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;在计算图中有一组依赖的tensor&lt;code class=&quot;highlighter-rouge&quot;&gt;\({X_k}\)&lt;/code&gt;，那么，tf中可以自动求出&lt;code class=&quot;highlighter-rouge&quot;&gt;\(dC/dX_k\)&lt;/code&gt;。此过程是通过在计算图中拓展节点的方式实现的，不过求梯度的节点对用户透明。&lt;/p&gt;

&lt;p&gt;如图所示，计算tensor &lt;code class=&quot;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;关于tensor &lt;code class=&quot;highlighter-rouge&quot;&gt;\(I\)&lt;/code&gt;的梯度时，会从&lt;code class=&quot;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;回溯到&lt;code class=&quot;highlighter-rouge&quot;&gt;\(I\)&lt;/code&gt;，对回溯路径上的每个节点添加一个对应的求解梯度的节点，并依据链式法则计算总的梯度（BP）。这些新增的节点会计算梯度函数（gradient function），例如，&lt;code class=&quot;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;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/data parallel.png&quot; alt=&quot;&quot; /&gt;&lt;/p&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;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/data parallel performance.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;10块gpu和50块gpu训练Inception的对比，可以发现，达到相同精度时，50块gpu需要的时间是10块的1/4左右。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/data parallel performance for Inception.png&quot; alt=&quot;&quot; /&gt;&lt;/p&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;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/model parallel.png&quot; alt=&quot;&quot; /&gt;&lt;/p&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;p&gt;&lt;img src=&quot;../assets/tf step by step/chap1/pipeline parallel.png&quot; alt=&quot;&quot; /&gt;&lt;/p&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://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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;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;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;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;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;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;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://hxhlwf.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://hxhlwf.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://hxhlwf.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;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;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://hxhlwf.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://hxhlwf.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</title>
   <link href="http://hxhlwf.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;!-- /TOC --&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;
</content>
 </entry>
 
 <entry>
   <title>pytorch</title>
   <link href="http://hxhlwf.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;!-- /TOC --&gt;

&lt;p&gt;占个坑先。。&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>paddlepaddle on kubernetes</title>
   <link href="http://hxhlwf.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://hxhlwf.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;#卷积&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;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;highlighter-rouge&quot;&gt;\(r \times c\)&lt;/code&gt;的大尺寸图像，将其定义为&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_{large}\)&lt;/code&gt;。首先通过从大尺寸图像中抽取的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a \times b\)&lt;/code&gt;的小尺寸图像样本&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_{small}\)&lt;/code&gt;训练稀疏自编码，计算&lt;code class=&quot;highlighter-rouge&quot;&gt;\(f = \sigma (W^{(1)}x_{small} + b^{(1)})\)&lt;/code&gt;（&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\sigma \)&lt;/code&gt;是一个 sigmoid 型函数）得到了k个特征， 其中&lt;code class=&quot;highlighter-rouge&quot;&gt;\(W^{(1)}\)&lt;/code&gt; 和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(b^{(1)}\)&lt;/code&gt;是可视层单元和隐含单元之间的权重和偏差值。对于每一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a \times b\)&lt;/code&gt;大小的小图像&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_s\)&lt;/code&gt;，计算出对应的值&lt;code class=&quot;highlighter-rouge&quot;&gt;\(f_s = \sigma (W^{(1)}x_s + b^{(1)})\)&lt;/code&gt;，对这些&lt;code class=&quot;highlighter-rouge&quot;&gt;\(f_{convolved}\)&lt;/code&gt;值做卷积，就可以得到&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(m \times n\)&lt;/code&gt;)，来池化我们的卷积特征。那么，我们把卷积特征划分到数个大小为&lt;code class=&quot;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;
</content>
 </entry>
 
 <entry>
   <title>vae</title>
   <link href="http://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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;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;更新的进展：
tf上的img2txt：&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;

&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;#alexnet&quot;&gt;Alexnet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#vgg&quot;&gt;VGG&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#mrnn&quot;&gt;mrnn&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#resnet&quot;&gt;ResNet&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;#数据提供给paddlepaddle&quot;&gt;数据提供给PaddlePaddle&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;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#vgg-1&quot;&gt;VGG&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#resnet-1&quot;&gt;ResNet&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;#预测&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;#参考文献&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;本教程主要介绍图像分类的深度学习模型，以及如何使用PaddlePaddle训练m-rnn模型。&lt;/p&gt;

&lt;h2 id=&quot;效果展示&quot;&gt;效果展示&lt;/h2&gt;

&lt;p&gt;图像分类包括通用图像分类、细粒度图像分类等。图1展示了通用图像分类效果，即模型可以正确识别图像上的主要物体。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/dog_cat.png &quot; width=&quot;350&quot; /&gt;&lt;br /&gt;
图1. 通用图像分类展示
&lt;/p&gt;

&lt;p&gt;图2展示了细粒度图像分类-花卉识别的效果，要求模型可以正确识别花的类别。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/flowers.png&quot; width=&quot;400&quot; /&gt;&lt;br /&gt;
图2. 细粒度图像分类展示
&lt;/p&gt;

&lt;p&gt;一个好的模型既要对不同类别识别正确，同时也应该能够对不同视角、光照、背景、变形或部分遮挡的图像正确识别(这里我们统一称作图像扰动)。图3展示了一些图像的扰动，较好的模型会像聪明的人类一样能够正确识别。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/variations.png&quot; width=&quot;550&quot; /&gt;&lt;br /&gt;
图3. 扰动图片展示[22]
&lt;/p&gt;

&lt;h2 id=&quot;模型概览&quot;&gt;模型概览&lt;/h2&gt;

&lt;p&gt;图像识别领域大量的研究成果都是建立在&lt;a href=&quot;http://host.robots.ox.ac.uk/pascal/VOC/&quot;&gt;PASCAL VOC&lt;/a&gt;、&lt;a href=&quot;http://image-net.org/&quot;&gt;ImageNet&lt;/a&gt;等公开的数据集上，很多图像识别算法通常在这些数据集上进行测试和比较。PASCAL VOC是2005年发起的一个视觉挑战赛，ImageNet是2010年发起的大规模视觉识别竞赛(ILSVRC)的数据集，在本章中我们基于这些竞赛的一些论文介绍图像分类模型。&lt;/p&gt;

&lt;p&gt;在2012年之前的传统图像分类方法可以用背景描述中提到的三步完成，但通常完整建立图像识别模型一般包括底层特征学习、特征编码、空间约束、分类器设计、模型融合等几个阶段。
  1). &lt;strong&gt;底层特征提取&lt;/strong&gt;: 通常从图像中按照固定步长、尺度提取大量局部特征描述。常用的局部特征包括SIFT(Scale-Invariant Feature Transform, 尺度不变特征转换) [&lt;a href=&quot;#参考文献&quot;&gt;1&lt;/a&gt;]、HOG(Histogram of Oriented Gradient, 方向梯度直方图) [&lt;a href=&quot;#参考文献&quot;&gt;2&lt;/a&gt;]、LBP(Local Bianray Pattern, 局部二值模式) [&lt;a href=&quot;#参考文献&quot;&gt;3&lt;/a&gt;] 等，一般也采用多种特征描述子，防止丢失过多的有用信息。
  2). &lt;strong&gt;特征编码&lt;/strong&gt;: 底层特征中包含了大量冗余与噪声，为了提高特征表达的鲁棒性，需要使用一种特征变换算法对底层特征进行编码，称作特征编码。常用的特征编码包括向量量化编码 [&lt;a href=&quot;#参考文献&quot;&gt;4&lt;/a&gt;]、稀疏编码 [&lt;a href=&quot;#参考文献&quot;&gt;5&lt;/a&gt;]、局部线性约束编码 [&lt;a href=&quot;#参考文献&quot;&gt;6&lt;/a&gt;]、Fisher向量编码 [&lt;a href=&quot;#参考文献&quot;&gt;7&lt;/a&gt;] 等。
  3). &lt;strong&gt;空间特征约束&lt;/strong&gt;: 特征编码之后一般会经过空间特征约束，也称作&lt;strong&gt;特征汇聚&lt;/strong&gt;。特征汇聚是指在一个空间范围内，对每一维特征取最大值或者平均值，可以获得一定特征不变形的特征表达。金字塔特征匹配是一种常用的特征聚会方法，这种方法提出将图像均匀分块，在分块内做特征汇聚。
  4). &lt;strong&gt;通过分类器分类&lt;/strong&gt;: 经过前面步骤之后一张图像可以用一个固定维度的向量进行描述，接下来就是经过分类器对图像进行分类。通常使用的分类器包括SVM(Support Vector Machine, 支持向量机)、随机森林等。而使用核方法的SVM是最为广泛的分类器，在传统图像分类任务上性能很好。&lt;/p&gt;

&lt;p&gt;这种方法在PASCAL VOC竞赛中的图像分类算法中被广泛使用 [&lt;a href=&quot;#参考文献&quot;&gt;18&lt;/a&gt;]。&lt;a href=&quot;http://www.nec-labs.com/&quot;&gt;NEC实验室&lt;/a&gt;在ILSVRC2010中采用SIFT和LBP特征，两个非线性编码器以及SVM分类器获得图像分类的冠军 [&lt;a href=&quot;#参考文献&quot;&gt;8&lt;/a&gt;]。&lt;/p&gt;

&lt;p&gt;Alex Krizhevsky在2012年ILSVRC提出的CNN模型 [&lt;a href=&quot;#参考文献&quot;&gt;9&lt;/a&gt;] 取得了历史性的突破，效果大幅度超越传统方法，获得了ILSVRC2012冠军，该模型被称作AlexNet。这也是首次将深度学习用于大规模图像分类中。从AlexNet之后，涌现了一系列CNN模型，不断地在ImageNet上刷新成绩，如图4展示。随着模型变得越来越深以及精妙的结构设计，Top-5的错误率也越来越低，降到了3.5%附近。而在同样的ImageNet数据集上，人眼的辨识错误率大概在5.1%，也就是目前的深度学习模型的识别能力已经超过了人眼。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/ilsvrc.png&quot; width=&quot;500&quot; /&gt;&lt;br /&gt;
图4. ILSVRC图像分类Top-5错误率
&lt;/p&gt;

&lt;h3 id=&quot;alexnet&quot;&gt;Alexnet&lt;/h3&gt;

&lt;p&gt;传统CNN包含卷积层、全连接层等组件，并采用softmax多类别分类器和多类交叉熵损失函数，一个典型的卷积神经网络如图5所示，我们先介绍用来构造CNN的常见组件。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/lenet.png&quot; /&gt;&lt;br /&gt;
图5. CNN网络示例[20] 
&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;卷积层(convolution layer): 执行卷积操作提取底层到高层的特征，发掘出图片局部关联性质和空间不变性质。&lt;/li&gt;
  &lt;li&gt;池化层(pooling layer): 执行降采样操作。通过取卷积输出特征图中局部区块的最大值(max-pooling)或者均值(avg-pooling)。降采样也是图像处理中常见的一种操作，可以过滤掉一些不重要的高频信息。&lt;/li&gt;
  &lt;li&gt;全连接层(fully-connected layer，或者fc layer): 输入层到隐藏层的神经元是全部连接的。&lt;/li&gt;
  &lt;li&gt;非线性变化: 卷积层、全连接层后面一般都会接非线性变化层，例如Sigmoid、Tanh、ReLu等来增强网络的表达能力，在CNN里最常使用的为ReLu激活函数。&lt;/li&gt;
  &lt;li&gt;Dropout [&lt;a href=&quot;#参考文献&quot;&gt;10&lt;/a&gt;] : 在模型训练阶段随机让一些隐层节点权重不工作，提高网络的泛化能力，一定程度上防止过拟合。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外，在训练过程中由于每层参数不断更新，会导致下一次输入分布发生变化，这样导致训练过程需要精心设计超参数。如2015年Sergey Ioffe和Christian Szegedy提出了Batch Normalization (BN)算法 [&lt;a href=&quot;#参考文献&quot;&gt;14&lt;/a&gt;] 中，每个batch对网络中的每一层特征都做归一化，使得每层分布相对稳定。BN算法不仅起到一定的正则作用，而且弱化了一些超参数的设计。经过实验证明，BN算法加速了模型收敛过程，在后来较深的模型中被广泛使用。&lt;/p&gt;

&lt;p&gt;接下来我们主要介绍VGG，GoogleNet和ResNet网络结构。&lt;/p&gt;

&lt;h3 id=&quot;vgg&quot;&gt;VGG&lt;/h3&gt;

&lt;p&gt;牛津大学VGG(Visual Geometry Group)组在2014年ILSVRC提出的模型被称作VGG模型 [&lt;a href=&quot;#参考文献&quot;&gt;11&lt;/a&gt;] 。该模型相比以往模型进一步加宽和加深了网络结构，它的核心是五组卷积操作，每两组之间做Max-Pooling空间降维。同一组内采用多次连续的3X3卷积，卷积核的数目由较浅组的64增多到最深组的512，同一组内的卷积核数目是一样的。卷积之后接两层全连接层，之后是分类层。由于每组内卷积层的不同，有11、13、16、19层这几种模型，下图展示一个16层的网络结构。VGG模型结构相对简洁，提出之后也有很多文章基于此模型进行研究，如在ImageNet上首次公开超过人眼识别的模型[&lt;a href=&quot;#参考文献&quot;&gt;19&lt;/a&gt;]就是借鉴VGG模型的结构。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/vgg16.png&quot; width=&quot;750&quot; /&gt;&lt;br /&gt;
图6. 基于ImageNet的VGG16模型
&lt;/p&gt;

&lt;h3 id=&quot;mrnn&quot;&gt;mrnn&lt;/h3&gt;

&lt;p&gt;GoogleNet [&lt;a href=&quot;#参考文献&quot;&gt;12&lt;/a&gt;] 在2014年ILSVRC的获得了冠军，在介绍该模型之前我们先来了解NIN(Network in Network)模型 [&lt;a href=&quot;#参考文献&quot;&gt;13&lt;/a&gt;] 和Inception模块，因为GoogleNet模型由多组Inception模块组成，模型设计借鉴了NIN的一些思想。&lt;/p&gt;

&lt;p&gt;NIN模型主要有两个特点：1) 引入了多层感知卷积网络(Multi-Layer Perceptron Convolution, MLPconv)代替一层线性卷积网络。MLPconv是一个微小的多层卷积网络，即在线性卷积后面增加若干层1x1的卷积，这样可以提取出高度非线性特征。2) 传统的CNN最后几层一般都是全连接层，参数较多。而NIN模型设计最后一层卷积层包含类别维度大小的特征图，然后采用全局均值池化(Avg-Pooling)替代全连接层，得到类别维度大小的向量，再进行分类。这种替代全连接层的方式有利于减少参数。&lt;/p&gt;

&lt;p&gt;Inception模块如下图7所示，图(a)是最简单的设计，输出是3个卷积层和一个池化层的特征拼接。这种设计的缺点是池化层不会改变特征通道数，拼接后会导致特征的通道数较大，经过几层这样的模块堆积后，通道数会越来越大，导致参数和计算量也随之增大。为了改善这个缺点，图(b)引入3个1x1卷积层进行降维，所谓的降维就是减少通道数，同时如NIN模型中提到的1x1卷积也可以修正线性特征。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/inception.png&quot; width=&quot;800&quot; /&gt;&lt;br /&gt;
图7. Inception模块
&lt;/p&gt;

&lt;p&gt;GoogleNet由多组Inception模块堆积而成。另外，在网络最后也没有采用传统的多层全连接层，而是像NIN网络一样采用了均值池化层；但与NIN不同的是，池化层后面接了一层到类别数映射的全连接层。除了这两个特点之外，由于网络中间层特征也很有判别性，GoogleNet在中间层添加了两个辅助分类器，在后向传播中增强梯度并且增强正则化，而整个网络的损失函数是这个三个分类器的损失加权求和。&lt;/p&gt;

&lt;p&gt;GoogleNet整体网络结构如图8所示，总共22层网络：开始由3层普通的卷积组成；接下来由三组子网络组成，第一组子网络包含2个Inception模块，第二组包含5个Inception模块，第三组包含2个Inception模块；然后接均值池化层、全连接层。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/googlenet.jpeg&quot; /&gt;&lt;br /&gt;
图8. GoogleNet[12] 
&lt;/p&gt;

&lt;p&gt;上面介绍的是GoogleNet第一版模型(称作GoogleNet-v1)。GoogleNet-v2 [&lt;a href=&quot;#参考文献&quot;&gt;14&lt;/a&gt;] 引入BN层；GoogleNet-v3 [&lt;a href=&quot;#参考文献&quot;&gt;16&lt;/a&gt;] 对一些卷积层做了分解，进一步提高网络非线性能力和加深网络；GoogleNet-v4 [&lt;a href=&quot;#参考文献&quot;&gt;17&lt;/a&gt;] 引入下面要讲的ResNet设计思路。从v1到v4每一版的改进都会带来准确度的提升，介于篇幅，这里不再详细介绍v2到v4的结构。&lt;/p&gt;

&lt;h3 id=&quot;resnet&quot;&gt;ResNet&lt;/h3&gt;

&lt;p&gt;ResNet(Residual Network) [&lt;a href=&quot;#参考文献&quot;&gt;15&lt;/a&gt;] 是2015年ImageNet图像分类、图像物体定位和图像物体检测比赛的冠军。针对训练卷积神经网络时加深网络导致准确度下降的问题，ResNet提出了采用残差学习。在已有设计思路(BN, 小卷积核，全卷积网络)的基础上，引入了残差模块。每个残差模块包含两条路径，其中一条路径是输入特征的直连通路，另一条路径对该特征做两到三次卷积操作得到该特征的残差，最后再将两条路径上的特征相加。&lt;/p&gt;

&lt;p&gt;残差模块如图9所示，左边是基本模块连接方式，由两个输出通道数相同的3x3卷积组成。右边是瓶颈模块(Bottleneck)连接方式，之所以称为瓶颈，是因为上面的1x1卷积用来降维(图示例即256-&amp;gt;64)，下面的1x1卷积用来升维(图示例即64-&amp;gt;256)，这样中间3x3卷积的输入和输出通道数都较小(图示例即64-&amp;gt;64)。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/resnet_block.jpg&quot; width=&quot;400&quot; /&gt;&lt;br /&gt;
图9. 残差模块
&lt;/p&gt;

&lt;p&gt;图10展示了50、101、152层网络连接示意图，使用的是瓶颈模块。这三个模型的区别在于每组中残差模块的重复次数不同(见图右上角)。ResNet训练收敛较快，成功的训练了上百乃至近千层的卷积神经网络。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/resnet.png&quot; /&gt;&lt;br /&gt;
图10. 基于ImageNet的ResNet模型
&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数据集规模相对较大，如&lt;a href=&quot;#模型概览&quot;&gt;模型概览&lt;/a&gt;一章所讲，大量研究成果基于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;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/cifar.png&quot; width=&quot;350&quot; /&gt;&lt;br /&gt;
图11. CIFAR10数据集[21]
&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;数据提供给paddlepaddle&quot;&gt;数据提供给PaddlePaddle&lt;/h3&gt;

&lt;p&gt;我们使用Python接口传递数据给系统，下面 &lt;code class=&quot;highlighter-rouge&quot;&gt;dataprovider.py&lt;/code&gt; 针对CIFAR10数据给出了完整示例。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;initializer&lt;/code&gt; 函数进行dataprovider的初始化，这里加载图像的均值，定义了输入image和label两个字段的类型。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;process&lt;/code&gt; 函数将数据逐条传输给系统，在图像分类任务里，可以在该函数中完成数据扰动操作，再传输给PaddlePaddle。这里对训练集做随机左右翻转，并将原始图片减去均值后传输给系统。&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;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;cPickle&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;paddle.trainer.PyDataProvider2&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;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&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;mean_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_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;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_train&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_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;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;settings&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;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&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;mean_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'mean'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_types&lt;/span&gt; &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;'image'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dense_vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'label'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integer_value&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;nd&quot;&gt;@provider&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_hook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer&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;CacheType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CACHE_PASS_IN_MEM&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;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&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;file_list&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;file_list&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;fdata&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;fname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fdata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;fo&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;fname&lt;/span&gt;&lt;span class=&quot;o&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;'rb'&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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cPickle&lt;/span&gt;&lt;span class=&quot;o&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;fo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;fo&lt;/span&gt;&lt;span class=&quot;o&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;images&lt;/span&gt; &lt;span class=&quot;o&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;s&quot;&gt;'data'&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;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'labels'&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;im&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lab&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;images&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_train&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&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;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;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;im&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im&lt;/span&gt;&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;im&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;settings&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;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;s&quot;&gt;'image'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im&lt;/span&gt;&lt;span class=&quot;o&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;'label'&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;lab&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;数据定义&quot;&gt;数据定义&lt;/h3&gt;

&lt;p&gt;在模型配置中，定义通过 &lt;code class=&quot;highlighter-rouge&quot;&gt;define_py_data_sources2&lt;/code&gt; 函数从 dataprovider 中读入数据， 其中 args 指定均值文件的路径。如果该配置文件用于预测，则不需要数据定义部分。&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;paddle.trainer_config_helpers&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;n&quot;&gt;is_predict&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;&quot;is_predict&quot;&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;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_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;define_py_data_sources2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;train_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'data/train.list'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;test_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'data/test.list'&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;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'dataprovider'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&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;s&quot;&gt;'process'&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;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'mean_path'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'data/mean.meta'&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;highlighter-rouge&quot;&gt;settings&lt;/code&gt; 设置训练使用的优化算法，并指定batch size 、初始学习率、momentum以及L2正则。&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;settings&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;128&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.1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;128.0&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;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;learning_rate_decay_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50000&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;n&quot;&gt;learning_rate_schedule&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'discexp'&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;MomentumOptimizer&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;n&quot;&gt;regularization&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L2Regularization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0005&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;通过 &lt;code class=&quot;highlighter-rouge&quot;&gt;learning_rate_decay_a&lt;/code&gt; (简写$a$） 、&lt;code class=&quot;highlighter-rouge&quot;&gt;learning_rate_decay_b&lt;/code&gt; (简写$b$) 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;learning_rate_schedule&lt;/code&gt; 指定学习率调整策略，这里采用离散指数的方式调节学习率，计算公式如下， $n$ 代表已经处理过的累计总样本数，$lr_{0}$ 即为 &lt;code class=&quot;highlighter-rouge&quot;&gt;settings&lt;/code&gt; 里设置的 &lt;code class=&quot;highlighter-rouge&quot;&gt;learning_rate&lt;/code&gt;。&lt;/p&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;lr = lr_{0} * a^ {\lfloor \frac{n}{ b}\rfloor}&lt;/script&gt;

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

&lt;p&gt;本教程中我们提供了VGG和ResNet两个模型的配置。&lt;/p&gt;

&lt;h4 id=&quot;vgg-1&quot;&gt;VGG&lt;/h4&gt;

&lt;p&gt;首先介绍VGG模型结构，由于CIFAR10图片大小和数量相比ImageNet数据小很多，因此这里的模型针对CIFAR10数据做了一定的适配。卷积部分引入了BN和Dropout操作。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;定义数据输入及其维度&lt;/p&gt;

    &lt;p&gt;网络输入定义为 &lt;code class=&quot;highlighter-rouge&quot;&gt;data_layer&lt;/code&gt; (数据层)，在图像分类中即为图像像素信息。CIFRAR10是RGB 3通道32x32大小的彩色图，因此输入数据大小为3072(3x32x32)，类别大小为10，即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;datadim&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;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;classdim&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;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;'image'&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;datadim&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;/li&gt;
  &lt;li&gt;
    &lt;p&gt;定义VGG网络核心模块&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;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vgg_bn_drop&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;p&gt;VGG核心模块的输入是数据层，&lt;code class=&quot;highlighter-rouge&quot;&gt;vgg_bn_drop&lt;/code&gt; 定义了16层VGG结构，每层卷积后面引入BN层和Dropout层，详细的定义如下：&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;vgg_bn_drop&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;num_channels&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;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropouts&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;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;img_conv_group&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;ipt&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;n&quot;&gt;num_channels_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;pool_size&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;pool_stride&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;conv_num_filter&lt;/span&gt;&lt;span class=&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_filter&lt;/span&gt;&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;groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;conv_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;conv_act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReluActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;conv_with_batchnorm&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;conv_batchnorm_drop_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropouts&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;n&quot;&gt;MaxPooling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
	
     &lt;span class=&quot;n&quot;&gt;conv1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&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;64&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;mf&quot;&gt;0.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;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;conv2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv1&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;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;0.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;n&quot;&gt;conv3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv2&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;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;0.4&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;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;conv4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv3&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;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;0.4&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;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;conv5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv4&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;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;0.4&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;mi&quot;&gt;0&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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_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;conv5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_rate&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;n&quot;&gt;fc1&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;drop&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;512&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;bn&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;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&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;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReluActivation&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;ExtraAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;drop_rate&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;n&quot;&gt;fc2&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;bn&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;512&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc2&lt;/span&gt;
	
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;2.1. 首先定义了一组卷积网络，即conv_block。卷积核大小为3x3，池化窗口大小为2x2，窗口滑动大小为2，groups决定每组VGG模块是几次连续的卷积操作，dropouts指定Dropout操作的概率。所使用的&lt;code class=&quot;highlighter-rouge&quot;&gt;img_conv_group&lt;/code&gt;是在&lt;code class=&quot;highlighter-rouge&quot;&gt;paddle.trainer_config_helpers&lt;/code&gt;中预定义的模块，由若干组 &lt;code class=&quot;highlighter-rouge&quot;&gt;Conv-&amp;gt;BN-&amp;gt;ReLu-&amp;gt;Dropout&lt;/code&gt; 和 一组 &lt;code class=&quot;highlighter-rouge&quot;&gt;Pooling&lt;/code&gt; 组成，&lt;/p&gt;

    &lt;p&gt;2.2. 五组卷积操作，即 5个conv_block。 第一、二组采用两次连续的卷积操作。第三、四、五组采用三次连续的卷积操作。每组最后一个卷积后面Dropout概率为0，即不使用Dropout操作。&lt;/p&gt;

    &lt;p&gt;2.3. 最后接两层512维的全连接。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;定义分类器&lt;/p&gt;

    &lt;p&gt;通过上面VGG网络提取高层特征，然后经过全连接层映射到类别维度大小的向量，再通过Softmax归一化得到每个类别的概率，也可称作分类器。&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;out&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;net&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_num&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;SoftmaxActivation&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;/li&gt;
  &lt;li&gt;
    &lt;p&gt;定义损失函数和网络输出&lt;/p&gt;

    &lt;p&gt;在有监督训练中需要输入图像对应的类别信息，同样通过&lt;code class=&quot;highlighter-rouge&quot;&gt;data_layer&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;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_predict&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;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;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;n&quot;&gt;class_num&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;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;out&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;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;cost&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;outputs&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;resnet-1&quot;&gt;ResNet&lt;/h3&gt;

&lt;p&gt;ResNet模型的第1、3、4步和VGG模型相同，这里不再介绍。主要介绍第2步即CIFAR10数据集上ResNet核心模块。&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;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;resnet_cifar10&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;depth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;56&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;highlighter-rouge&quot;&gt;resnet_cifar10&lt;/code&gt;中的一些基本函数，再介绍网络连接过程。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;conv_bn_layer&lt;/code&gt; : 带BN的卷积层。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;shortcut&lt;/code&gt; : 残差模块的”直连”路径，”直连”实际分两种形式：残差模块输入和输出特征通道数不等时，采用1x1卷积的升维操作；残差模块输入和输出通道相等时，采用直连操作。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;basicblock&lt;/code&gt; : 一个基础残差模块，即图9左边所示，由两组3x3卷积组成的路径和一条”直连”路径组成。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;bottleneck&lt;/code&gt; : 一个瓶颈残差模块，即图9右边所示，由上下1x1卷积和中间3x3卷积组成的路径和一条”直连”路径组成。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;layer_warp&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;conv_bn_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;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;stride&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;active_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReluActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;ch_in&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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_conv_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;filter_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter_size&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;n&quot;&gt;ch_in&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;ch_out&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;n&quot;&gt;stride&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;n&quot;&gt;padding&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;span class=&quot;k&quot;&gt;return&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;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&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;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active_type&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;shortcut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&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;stride&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;n_in&lt;/span&gt; &lt;span class=&quot;o&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&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;mi&quot;&gt;1&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;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;LinearActivation&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;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ch_in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_filters&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;conv_bn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;stride&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&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;ch_out&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;1&lt;/span&gt;&lt;span class=&quot;p&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;short&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shortcut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addto_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;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short&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;ReluActivation&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;bottleneck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ch_in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_filter&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;conv_bn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;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;stride&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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&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;ch_out&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;1&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;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&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;ch_out&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;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;n&quot;&gt;LinearActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shortcut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addto_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;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short&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;ReluActivation&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;layer_warp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipt&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;count&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;p&quot;&gt;):&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;block_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&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;stride&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;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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block_func&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;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;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;tmp&lt;/span&gt;

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

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;resnet_cifar10&lt;/code&gt; 的连接结构主要有以下几个过程。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;底层输入连接一层 &lt;code class=&quot;highlighter-rouge&quot;&gt;conv_bn_layer&lt;/code&gt;，即带BN的卷积层。&lt;/li&gt;
  &lt;li&gt;然后连接3组残差模块即下面配置3组 &lt;code class=&quot;highlighter-rouge&quot;&gt;layer_warp&lt;/code&gt; ，每组采用图 10 左边残差模块组成。&lt;/li&gt;
  &lt;li&gt;最后对网络做均值池化并返回该层。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;注意：除过第一层卷积层和最后一层全连接层之外，要求三组 &lt;code class=&quot;highlighter-rouge&quot;&gt;layer_warp&lt;/code&gt; 总的含参层数能够被6整除，即 &lt;code class=&quot;highlighter-rouge&quot;&gt;resnet_cifar10&lt;/code&gt; 的 depth 要满足 $(depth - 2) % 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;resnet_cifar10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&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;mi&quot;&gt;56&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# depth should be one of 20, 32, 44, 56, 110, 1202&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&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;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;6&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;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&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;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;6&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;nStages&lt;/span&gt; &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;16&lt;/span&gt;&lt;span class=&quot;p&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;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;conv1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ch_in&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;ch_out&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;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;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;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;res1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_warp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv1&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;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;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;res2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_warp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res1&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;n&quot;&gt;n&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;res3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_warp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res2&lt;/span&gt;&lt;span class=&quot;p&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;n&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;pool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_pool_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;res3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                         &lt;span class=&quot;n&quot;&gt;pool_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;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;pool_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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pool&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;执行脚本 train.sh 进行模型训练， 其中指定配置文件、设备类型、线程个数、总共训练的轮数、模型存储路径等。&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;sh train.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;脚本 &lt;code class=&quot;highlighter-rouge&quot;&gt;train.sh&lt;/code&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;&lt;span class=&quot;c&quot;&gt;#cfg=models/resnet.py&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;models/vgg.py
&lt;span class=&quot;nv&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;output
&lt;span class=&quot;nv&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;train.log

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;nv&quot;&gt;$cfg&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;true&lt;/span&gt; &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;
    &lt;span class=&quot;nt&quot;&gt;--log_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;--num_passes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;300 &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;nv&quot;&gt;$output&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    2&amp;gt;&amp;amp;1 | tee &lt;span class=&quot;nv&quot;&gt;$log&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;highlighter-rouge&quot;&gt;--config=$cfg&lt;/code&gt; : 指定配置文件，默认是 &lt;code class=&quot;highlighter-rouge&quot;&gt;models/vgg.py&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--use_gpu=true&lt;/code&gt; : 指定使用GPU训练，若使用CPU，设置为false。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--trainer_count=1&lt;/code&gt; : 指定线程个数或GPU个数。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--log_period=100&lt;/code&gt; : 指定日志打印的batch间隔。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--save_dir=$output&lt;/code&gt; : 指定模型存储路径。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;一轮训练log示例如下所示，经过1个pass， 训练集上平均error为0.79958 ，测试集上平均error为0.7858 。&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;TrainerInternal.cpp:165]  Batch=300 samples=38400 AvgCost=2.07708 CurrentCost=1.96158 Eval: classification_error_evaluator=0.81151  CurrentEval: classification_error_evaluator=0.789297
TrainerInternal.cpp:181]  Pass=0 Batch=391 samples=50000 AvgCost=2.03348 Eval: classification_error_evaluator=0.79958
Tester.cpp:115]  Test samples=10000 cost=1.99246 Eval: classification_error_evaluator=0.7858
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;图12是训练的分类错误率曲线图，运行到第200个pass后基本收敛，最终得到测试集上分类错误率为8.54%。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/plot.png&quot; width=&quot;400&quot; /&gt;&lt;br /&gt;
图12. CIFAR10数据集上VGG模型的分类错误率
&lt;/p&gt;

&lt;h2 id=&quot;模型应用&quot;&gt;模型应用&lt;/h2&gt;

&lt;p&gt;在训练完成后，模型会保存在路径 &lt;code class=&quot;highlighter-rouge&quot;&gt;output/pass-%05d&lt;/code&gt; 下，例如第300个pass的模型会保存在路径 &lt;code class=&quot;highlighter-rouge&quot;&gt;output/pass-00299&lt;/code&gt;。 可以使用脚本 &lt;code class=&quot;highlighter-rouge&quot;&gt;classify.py&lt;/code&gt; 对图片进行预测或提取特征，注意该脚本默认使用模型配置为 &lt;code class=&quot;highlighter-rouge&quot;&gt;models/vgg.py&lt;/code&gt;，&lt;/p&gt;

&lt;h3 id=&quot;预测&quot;&gt;预测&lt;/h3&gt;

&lt;p&gt;可以按照下面方式预测图片的类别，默认使用GPU预测，如果使用CPU预测，在后面加参数 &lt;code class=&quot;highlighter-rouge&quot;&gt;-c&lt;/code&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;python classify.py &lt;span class=&quot;nt&quot;&gt;--job&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;predict &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;output/pass-00299 &lt;span class=&quot;nt&quot;&gt;--data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;image/dog.png &lt;span class=&quot;c&quot;&gt;# -c&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-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Label of image/dog.png is: 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;特征提取&quot;&gt;特征提取&lt;/h3&gt;

&lt;p&gt;可以按照下面方式对图片提取特征，和预测使用方式不同的是指定job类型为extract，并需要指定提取的层。&lt;code class=&quot;highlighter-rouge&quot;&gt;classify.py&lt;/code&gt; 默认以第一层卷积特征为例提取特征，并画出了类似图13的可视化图。VGG模型的第一层卷积有64个通道，图13展示了每个通道的灰度图。&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;python classify.py &lt;span class=&quot;nt&quot;&gt;--job&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;extract &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;output/pass-00299 &lt;span class=&quot;nt&quot;&gt;--data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;image/dog.png &lt;span class=&quot;c&quot;&gt;# -c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/fea_conv0.png&quot; width=&quot;500&quot; /&gt;&lt;br /&gt;
图13. 卷积特征可视化图 
&lt;/p&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

&lt;p&gt;传统图像分类方法由多个阶段构成，框架较为复杂，而端到端的CNN模型结构可一步到位，而且大幅度提升了分类准确率。本文我们首先介绍VGG、GoogleNet、ResNet三个经典的模型；然后基于CIFAR10数据集，介绍如何使用PaddlePaddle配置和训练CNN模型，尤其是VGG和ResNet模型；最后介绍如何使用PaddlePaddle的API接口对图片进行预测和特征提取。对于其他数据集比如ImageNet，配置和训练流程是同样的，大家可以自行进行实验。&lt;/p&gt;

&lt;h2 id=&quot;参考文献&quot;&gt;参考文献&lt;/h2&gt;

&lt;p&gt;[1] Mao J, Xu W, Yang Y, et al. &lt;a href=&quot;https://arxiv.org/abs/1412.6632&quot;&gt;Deep captioning with multimodal recurrent neural networks (m-rnn)&lt;/a&gt;[J]. arXiv preprint arXiv:1412.6632, 2014.&lt;/p&gt;

&lt;p&gt;[2] N. Dalal, B. Triggs, &lt;a href=&quot;http://vision.stanford.edu/teaching/cs231b_spring1213/papers/CVPR05_DalalTriggs.pdf&quot;&gt;Histograms of Oriented Gradients for Human Detection&lt;/a&gt;, Proc. IEEE Conf. Computer Vision and Pattern Recognition, 2005.&lt;/p&gt;

&lt;p&gt;[3] Ahonen, T., Hadid, A., and Pietikinen, M. (2006). &lt;a href=&quot;http://ieeexplore.ieee.org/document/1717463/&quot;&gt;Face description with local binary patterns: Application to face recognition&lt;/a&gt;. PAMI, 28.&lt;/p&gt;

&lt;p&gt;[4] J. Sivic, A. Zisserman, &lt;a href=&quot;http://www.robots.ox.ac.uk/~vgg/publications/papers/sivic03.pdf&quot;&gt;Video Google: A Text Retrieval Approach to Object Matching in Videos&lt;/a&gt;, Proc. Ninth Int’l Conf. Computer Vision, pp. 1470-1478, 2003.&lt;/p&gt;

&lt;p&gt;[5] B. Olshausen, D. Field, &lt;a href=&quot;http://redwood.psych.cornell.edu/papers/olshausen_field_1997.pdf&quot;&gt;Sparse Coding with an Overcomplete Basis Set: A Strategy Employed by V1?&lt;/a&gt;, Vision Research, vol. 37, pp. 3311-3325, 1997.&lt;/p&gt;

&lt;p&gt;[6] Wang, J., Yang, J., Yu, K., Lv, F., Huang, T., and Gong, Y. (2010). &lt;a href=&quot;http://ieeexplore.ieee.org/abstract/document/5540018/&quot;&gt;Locality-constrained Linear Coding for image classification&lt;/a&gt;. In CVPR.&lt;/p&gt;

&lt;p&gt;[7] Perronnin, F., Sánchez, J., &amp;amp; Mensink, T. (2010). &lt;a href=&quot;http://dl.acm.org/citation.cfm?id=1888101&quot;&gt;Improving the fisher kernel for large-scale image classification&lt;/a&gt;. In ECCV (4).&lt;/p&gt;

&lt;p&gt;[8] Lin, Y., Lv, F., Cao, L., Zhu, S., Yang, M., Cour, T., Yu, K., and Huang, T. (2011). &lt;a href=&quot;http://ieeexplore.ieee.org/document/5995477/&quot;&gt;Large-scale image clas- sification: Fast feature extraction and SVM training&lt;/a&gt;. In CVPR.&lt;/p&gt;

&lt;p&gt;[9] Krizhevsky, A., Sutskever, I., and Hinton, G. (2012). &lt;a href=&quot;http://www.cs.toronto.edu/~kriz/imagenet_classification_with_deep_convolutional.pdf&quot;&gt;ImageNet classification with deep convolutional neu- ral networks&lt;/a&gt;. In NIPS.&lt;/p&gt;

&lt;p&gt;[10] G.E. Hinton, N. Srivastava, A. Krizhevsky, I. Sutskever, and R.R. Salakhutdinov. &lt;a href=&quot;https://arxiv.org/abs/1207.0580&quot;&gt;Improving neural networks by preventing co-adaptation of feature detectors&lt;/a&gt;. arXiv preprint arXiv:1207.0580, 2012.&lt;/p&gt;

&lt;p&gt;[11] K. Chatfield, K. Simonyan, A. Vedaldi, A. Zisserman. &lt;a href=&quot;https://arxiv.org/abs/1405.3531&quot;&gt;Return of the Devil in the Details: Delving Deep into Convolutional Nets&lt;/a&gt;. BMVC, 2014。&lt;/p&gt;

&lt;p&gt;[12] Szegedy, C., Liu, W., Jia, Y., Sermanet, P., Reed, S., Anguelov, D., Erhan, D., Vanhoucke, V., Rabinovich, A., &lt;a href=&quot;https://arxiv.org/abs/1409.4842&quot;&gt;Going deeper with convolutions&lt;/a&gt;. In: CVPR. (2015)&lt;/p&gt;

&lt;p&gt;[13] Lin, M., Chen, Q., and Yan, S. &lt;a href=&quot;https://arxiv.org/abs/1312.4400&quot;&gt;Network in network&lt;/a&gt;. In Proc. ICLR, 2014.&lt;/p&gt;

&lt;p&gt;[14] S. Ioffe and C. Szegedy. &lt;a href=&quot;https://arxiv.org/abs/1502.03167&quot;&gt;Batch normalization: Accelerating deep network training by reducing internal covariate shift&lt;/a&gt;. In ICML, 2015.&lt;/p&gt;

&lt;p&gt;[15] K. He, X. Zhang, S. Ren, J. Sun. &lt;a href=&quot;https://arxiv.org/abs/1512.03385&quot;&gt;Deep Residual Learning for Image Recognition&lt;/a&gt;. CVPR 2016.&lt;/p&gt;

&lt;p&gt;[16] Szegedy, C., Vanhoucke, V., Ioffe, S., Shlens, J., Wojna, Z. &lt;a href=&quot;https://arxiv.org/abs/1512.00567&quot;&gt;Rethinking the incep-tion architecture for computer vision&lt;/a&gt;. In: CVPR. (2016).&lt;/p&gt;

&lt;p&gt;[17] Szegedy, C., Ioffe, S., Vanhoucke, V. &lt;a href=&quot;https://arxiv.org/abs/1602.07261&quot;&gt;Inception-v4, inception-resnet and the impact of residual connections on learning&lt;/a&gt;. arXiv:1602.07261 (2016).&lt;/p&gt;

&lt;p&gt;[18] Everingham, M., Eslami, S. M. A., Van Gool, L., Williams, C. K. I., Winn, J. and Zisserman, A. &lt;a href=&quot;http://link.springer.com/article/10.1007/s11263-014-0733-5&quot;&gt;The Pascal Visual Object Classes Challenge: A Retrospective&lt;/a&gt;. International Journal of Computer Vision, 111(1), 98-136, 2015.&lt;/p&gt;

&lt;p&gt;[19] He, K., Zhang, X., Ren, S., and Sun, J. &lt;a href=&quot;https://arxiv.org/abs/1502.01852&quot;&gt;Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification&lt;/a&gt;. ArXiv e-prints, February 2015.&lt;/p&gt;

&lt;p&gt;[20] http://deeplearning.net/tutorial/lenet.html&lt;/p&gt;

&lt;p&gt;[21] https://www.cs.toronto.edu/~kriz/cifar.html&lt;/p&gt;

&lt;p&gt;[22] http://cs231n.github.io/classification/&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
&lt;a rel=&quot;license&quot; href=&quot;http://creativecommons.org/licenses/by-nc-sa/4.0/&quot;&gt;&lt;img alt=&quot;知识共享许可协议&quot; style=&quot;border-width:0&quot; src=&quot;https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span xmlns:dct=&quot;http://purl.org/dc/terms/&quot; href=&quot;http://purl.org/dc/dcmitype/Text&quot; property=&quot;dct:title&quot; rel=&quot;dct:type&quot;&gt;本教程&lt;/span&gt; 由 &lt;a xmlns:cc=&quot;http://creativecommons.org/ns#&quot; href=&quot;http://book.paddlepaddle.org&quot; property=&quot;cc:attributionName&quot; rel=&quot;cc:attributionURL&quot;&gt;PaddlePaddle&lt;/a&gt; 创作，采用 &lt;a rel=&quot;license&quot; href=&quot;http://creativecommons.org/licenses/by-nc-sa/4.0/&quot;&gt;知识共享 署名-非商业性使用-相同方式共享 4.0 国际 许可协议&lt;/a&gt;进行许可。&lt;/p&gt;

&lt;p&gt;Image-sentence retrieval.&lt;/p&gt;

&lt;p&gt;Generating novel sentence descriptions for images.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>lapgan</title>
   <link href="http://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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;#背景介绍&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;#cnn&quot;&gt;CNN&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#vgg&quot;&gt;VGG&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#googlenet&quot;&gt;GoogleNet&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#resnet&quot;&gt;ResNet&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;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#下载mscoco数据&quot;&gt;下载MSCOCO数据&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#下载图片qa的标注数据集&quot;&gt;下载图片－q/a的标注数据集&lt;/a&gt;
      - &lt;a href=&quot;#英文qa&quot;&gt;英文qa:&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#中文qa&quot;&gt;中文qa:&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#数据提供给paddlepaddle&quot;&gt;数据提供给PaddlePaddle&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;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;li&gt;&lt;a href=&quot;#vgg-1&quot;&gt;VGG&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#resnet-1&quot;&gt;ResNet&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;#预测&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;#参考文献&quot;&gt;参考文献&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;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;图像分类包括通用图像分类、细粒度图像分类等。图1展示了通用图像分类效果，即模型可以正确识别图像上的主要物体。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/dog_cat.png &quot; width=&quot;350&quot; /&gt;&lt;br /&gt;
图1. 通用图像分类展示
&lt;/p&gt;

&lt;p&gt;图2展示了细粒度图像分类-花卉识别的效果，要求模型可以正确识别花的类别。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/flowers.png&quot; width=&quot;400&quot; /&gt;&lt;br /&gt;
图2. 细粒度图像分类展示
&lt;/p&gt;

&lt;p&gt;一个好的模型既要对不同类别识别正确，同时也应该能够对不同视角、光照、背景、变形或部分遮挡的图像正确识别(这里我们统一称作图像扰动)。图3展示了一些图像的扰动，较好的模型会像聪明的人类一样能够正确识别。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/variations.png&quot; width=&quot;550&quot; /&gt;&lt;br /&gt;
图3. 扰动图片展示[22]
&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;p&gt;图像识别领域大量的研究成果都是建立在&lt;a href=&quot;http://host.robots.ox.ac.uk/pascal/VOC/&quot;&gt;PASCAL VOC&lt;/a&gt;、&lt;a href=&quot;http://image-net.org/&quot;&gt;ImageNet&lt;/a&gt;等公开的数据集上，很多图像识别算法通常在这些数据集上进行测试和比较。PASCAL VOC是2005年发起的一个视觉挑战赛，ImageNet是2010年发起的大规模视觉识别竞赛(ILSVRC)的数据集，在本章中我们基于这些竞赛的一些论文介绍图像分类模型。&lt;/p&gt;

&lt;p&gt;在2012年之前的传统图像分类方法可以用背景描述中提到的三步完成，但通常完整建立图像识别模型一般包括底层特征学习、特征编码、空间约束、分类器设计、模型融合等几个阶段。
  1). &lt;strong&gt;底层特征提取&lt;/strong&gt;: 通常从图像中按照固定步长、尺度提取大量局部特征描述。常用的局部特征包括SIFT(Scale-Invariant Feature Transform, 尺度不变特征转换) [&lt;a href=&quot;#参考文献&quot;&gt;1&lt;/a&gt;]、HOG(Histogram of Oriented Gradient, 方向梯度直方图) [&lt;a href=&quot;#参考文献&quot;&gt;2&lt;/a&gt;]、LBP(Local Bianray Pattern, 局部二值模式) [&lt;a href=&quot;#参考文献&quot;&gt;3&lt;/a&gt;] 等，一般也采用多种特征描述子，防止丢失过多的有用信息。
  2). &lt;strong&gt;特征编码&lt;/strong&gt;: 底层特征中包含了大量冗余与噪声，为了提高特征表达的鲁棒性，需要使用一种特征变换算法对底层特征进行编码，称作特征编码。常用的特征编码包括向量量化编码 [&lt;a href=&quot;#参考文献&quot;&gt;4&lt;/a&gt;]、稀疏编码 [&lt;a href=&quot;#参考文献&quot;&gt;5&lt;/a&gt;]、局部线性约束编码 [&lt;a href=&quot;#参考文献&quot;&gt;6&lt;/a&gt;]、Fisher向量编码 [&lt;a href=&quot;#参考文献&quot;&gt;7&lt;/a&gt;] 等。
  3). &lt;strong&gt;空间特征约束&lt;/strong&gt;: 特征编码之后一般会经过空间特征约束，也称作&lt;strong&gt;特征汇聚&lt;/strong&gt;。特征汇聚是指在一个空间范围内，对每一维特征取最大值或者平均值，可以获得一定特征不变形的特征表达。金字塔特征匹配是一种常用的特征聚会方法，这种方法提出将图像均匀分块，在分块内做特征汇聚。
  4). &lt;strong&gt;通过分类器分类&lt;/strong&gt;: 经过前面步骤之后一张图像可以用一个固定维度的向量进行描述，接下来就是经过分类器对图像进行分类。通常使用的分类器包括SVM(Support Vector Machine, 支持向量机)、随机森林等。而使用核方法的SVM是最为广泛的分类器，在传统图像分类任务上性能很好。&lt;/p&gt;

&lt;p&gt;这种方法在PASCAL VOC竞赛中的图像分类算法中被广泛使用 [&lt;a href=&quot;#参考文献&quot;&gt;18&lt;/a&gt;]。&lt;a href=&quot;http://www.nec-labs.com/&quot;&gt;NEC实验室&lt;/a&gt;在ILSVRC2010中采用SIFT和LBP特征，两个非线性编码器以及SVM分类器获得图像分类的冠军 [&lt;a href=&quot;#参考文献&quot;&gt;8&lt;/a&gt;]。&lt;/p&gt;

&lt;p&gt;Alex Krizhevsky在2012年ILSVRC提出的CNN模型 [&lt;a href=&quot;#参考文献&quot;&gt;9&lt;/a&gt;] 取得了历史性的突破，效果大幅度超越传统方法，获得了ILSVRC2012冠军，该模型被称作AlexNet。这也是首次将深度学习用于大规模图像分类中。从AlexNet之后，涌现了一系列CNN模型，不断地在ImageNet上刷新成绩，如图4展示。随着模型变得越来越深以及精妙的结构设计，Top-5的错误率也越来越低，降到了3.5%附近。而在同样的ImageNet数据集上，人眼的辨识错误率大概在5.1%，也就是目前的深度学习模型的识别能力已经超过了人眼。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/ilsvrc.png&quot; width=&quot;500&quot; /&gt;&lt;br /&gt;
图4. ILSVRC图像分类Top-5错误率
&lt;/p&gt;

&lt;h3 id=&quot;cnn&quot;&gt;CNN&lt;/h3&gt;

&lt;p&gt;传统CNN包含卷积层、全连接层等组件，并采用softmax多类别分类器和多类交叉熵损失函数，一个典型的卷积神经网络如图5所示，我们先介绍用来构造CNN的常见组件。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/lenet.png&quot; /&gt;&lt;br /&gt;
图5. CNN网络示例[20] 
&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;卷积层(convolution layer): 执行卷积操作提取底层到高层的特征，发掘出图片局部关联性质和空间不变性质。&lt;/li&gt;
  &lt;li&gt;池化层(pooling layer): 执行降采样操作。通过取卷积输出特征图中局部区块的最大值(max-pooling)或者均值(avg-pooling)。降采样也是图像处理中常见的一种操作，可以过滤掉一些不重要的高频信息。&lt;/li&gt;
  &lt;li&gt;全连接层(fully-connected layer，或者fc layer): 输入层到隐藏层的神经元是全部连接的。&lt;/li&gt;
  &lt;li&gt;非线性变化: 卷积层、全连接层后面一般都会接非线性变化层，例如Sigmoid、Tanh、ReLu等来增强网络的表达能力，在CNN里最常使用的为ReLu激活函数。&lt;/li&gt;
  &lt;li&gt;Dropout [&lt;a href=&quot;#参考文献&quot;&gt;10&lt;/a&gt;] : 在模型训练阶段随机让一些隐层节点权重不工作，提高网络的泛化能力，一定程度上防止过拟合。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;另外，在训练过程中由于每层参数不断更新，会导致下一次输入分布发生变化，这样导致训练过程需要精心设计超参数。如2015年Sergey Ioffe和Christian Szegedy提出了Batch Normalization (BN)算法 [&lt;a href=&quot;#参考文献&quot;&gt;14&lt;/a&gt;] 中，每个batch对网络中的每一层特征都做归一化，使得每层分布相对稳定。BN算法不仅起到一定的正则作用，而且弱化了一些超参数的设计。经过实验证明，BN算法加速了模型收敛过程，在后来较深的模型中被广泛使用。&lt;/p&gt;

&lt;p&gt;接下来我们主要介绍VGG，GoogleNet和ResNet网络结构。&lt;/p&gt;

&lt;h3 id=&quot;vgg&quot;&gt;VGG&lt;/h3&gt;

&lt;p&gt;牛津大学VGG(Visual Geometry Group)组在2014年ILSVRC提出的模型被称作VGG模型 [&lt;a href=&quot;#参考文献&quot;&gt;11&lt;/a&gt;] 。该模型相比以往模型进一步加宽和加深了网络结构，它的核心是五组卷积操作，每两组之间做Max-Pooling空间降维。同一组内采用多次连续的3X3卷积，卷积核的数目由较浅组的64增多到最深组的512，同一组内的卷积核数目是一样的。卷积之后接两层全连接层，之后是分类层。由于每组内卷积层的不同，有11、13、16、19层这几种模型，下图展示一个16层的网络结构。VGG模型结构相对简洁，提出之后也有很多文章基于此模型进行研究，如在ImageNet上首次公开超过人眼识别的模型[&lt;a href=&quot;#参考文献&quot;&gt;19&lt;/a&gt;]就是借鉴VGG模型的结构。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/vgg16.png&quot; width=&quot;750&quot; /&gt;&lt;br /&gt;
图6. 基于ImageNet的VGG16模型
&lt;/p&gt;

&lt;h3 id=&quot;googlenet&quot;&gt;GoogleNet&lt;/h3&gt;

&lt;p&gt;GoogleNet [&lt;a href=&quot;#参考文献&quot;&gt;12&lt;/a&gt;] 在2014年ILSVRC的获得了冠军，在介绍该模型之前我们先来了解NIN(Network in Network)模型 [&lt;a href=&quot;#参考文献&quot;&gt;13&lt;/a&gt;] 和Inception模块，因为GoogleNet模型由多组Inception模块组成，模型设计借鉴了NIN的一些思想。&lt;/p&gt;

&lt;p&gt;NIN模型主要有两个特点：1) 引入了多层感知卷积网络(Multi-Layer Perceptron Convolution, MLPconv)代替一层线性卷积网络。MLPconv是一个微小的多层卷积网络，即在线性卷积后面增加若干层1x1的卷积，这样可以提取出高度非线性特征。2) 传统的CNN最后几层一般都是全连接层，参数较多。而NIN模型设计最后一层卷积层包含类别维度大小的特征图，然后采用全局均值池化(Avg-Pooling)替代全连接层，得到类别维度大小的向量，再进行分类。这种替代全连接层的方式有利于减少参数。&lt;/p&gt;

&lt;p&gt;Inception模块如下图7所示，图(a)是最简单的设计，输出是3个卷积层和一个池化层的特征拼接。这种设计的缺点是池化层不会改变特征通道数，拼接后会导致特征的通道数较大，经过几层这样的模块堆积后，通道数会越来越大，导致参数和计算量也随之增大。为了改善这个缺点，图(b)引入3个1x1卷积层进行降维，所谓的降维就是减少通道数，同时如NIN模型中提到的1x1卷积也可以修正线性特征。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/inception.png&quot; width=&quot;800&quot; /&gt;&lt;br /&gt;
图7. Inception模块
&lt;/p&gt;

&lt;p&gt;GoogleNet由多组Inception模块堆积而成。另外，在网络最后也没有采用传统的多层全连接层，而是像NIN网络一样采用了均值池化层；但与NIN不同的是，池化层后面接了一层到类别数映射的全连接层。除了这两个特点之外，由于网络中间层特征也很有判别性，GoogleNet在中间层添加了两个辅助分类器，在后向传播中增强梯度并且增强正则化，而整个网络的损失函数是这个三个分类器的损失加权求和。&lt;/p&gt;

&lt;p&gt;GoogleNet整体网络结构如图8所示，总共22层网络：开始由3层普通的卷积组成；接下来由三组子网络组成，第一组子网络包含2个Inception模块，第二组包含5个Inception模块，第三组包含2个Inception模块；然后接均值池化层、全连接层。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/googlenet.jpeg&quot; /&gt;&lt;br /&gt;
图8. GoogleNet[12] 
&lt;/p&gt;

&lt;p&gt;上面介绍的是GoogleNet第一版模型(称作GoogleNet-v1)。GoogleNet-v2 [&lt;a href=&quot;#参考文献&quot;&gt;14&lt;/a&gt;] 引入BN层；GoogleNet-v3 [&lt;a href=&quot;#参考文献&quot;&gt;16&lt;/a&gt;] 对一些卷积层做了分解，进一步提高网络非线性能力和加深网络；GoogleNet-v4 [&lt;a href=&quot;#参考文献&quot;&gt;17&lt;/a&gt;] 引入下面要讲的ResNet设计思路。从v1到v4每一版的改进都会带来准确度的提升，介于篇幅，这里不再详细介绍v2到v4的结构。&lt;/p&gt;

&lt;h3 id=&quot;resnet&quot;&gt;ResNet&lt;/h3&gt;

&lt;p&gt;ResNet(Residual Network) [&lt;a href=&quot;#参考文献&quot;&gt;15&lt;/a&gt;] 是2015年ImageNet图像分类、图像物体定位和图像物体检测比赛的冠军。针对训练卷积神经网络时加深网络导致准确度下降的问题，ResNet提出了采用残差学习。在已有设计思路(BN, 小卷积核，全卷积网络)的基础上，引入了残差模块。每个残差模块包含两条路径，其中一条路径是输入特征的直连通路，另一条路径对该特征做两到三次卷积操作得到该特征的残差，最后再将两条路径上的特征相加。&lt;/p&gt;

&lt;p&gt;残差模块如图9所示，左边是基本模块连接方式，由两个输出通道数相同的3x3卷积组成。右边是瓶颈模块(Bottleneck)连接方式，之所以称为瓶颈，是因为上面的1x1卷积用来降维(图示例即256-&amp;gt;64)，下面的1x1卷积用来升维(图示例即64-&amp;gt;256)，这样中间3x3卷积的输入和输出通道数都较小(图示例即64-&amp;gt;64)。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/resnet_block.jpg&quot; width=&quot;400&quot; /&gt;&lt;br /&gt;
图9. 残差模块
&lt;/p&gt;

&lt;p&gt;图10展示了50、101、152层网络连接示意图，使用的是瓶颈模块。这三个模型的区别在于每组中残差模块的重复次数不同(见图右上角)。ResNet训练收敛较快，成功的训练了上百乃至近千层的卷积神经网络。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/resnet.png&quot; /&gt;&lt;br /&gt;
图10. 基于ImageNet的ResNet模型
&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数据集规模相对较大，如&lt;a href=&quot;#模型概览&quot;&gt;模型概览&lt;/a&gt;一章所讲，大量研究成果基于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;highlighter-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;
mkdir 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;数据提供给paddlepaddle&quot;&gt;数据提供给PaddlePaddle&lt;/h3&gt;

&lt;p&gt;我们使用Python接口传递数据给系统，下面 &lt;code class=&quot;highlighter-rouge&quot;&gt;dataprovider.py&lt;/code&gt; 针对CIFAR10数据给出了完整示例。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;initializer&lt;/code&gt; 函数进行dataprovider的初始化，这里加载图像的均值，定义了输入image和label两个字段的类型。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;process&lt;/code&gt; 函数将数据逐条传输给系统，在图像分类任务里，可以在该函数中完成数据扰动操作，再传输给PaddlePaddle。这里对训练集做随机左右翻转，并将原始图片减去均值后传输给系统。&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;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;cPickle&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;paddle.trainer.PyDataProvider2&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;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&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;mean_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_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;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_train&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_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;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;settings&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;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&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;mean_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'mean'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_types&lt;/span&gt; &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;'image'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dense_vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'label'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integer_value&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;nd&quot;&gt;@provider&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init_hook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;initializer&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;CacheType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CACHE_PASS_IN_MEM&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;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&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;file_list&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;file_list&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;fdata&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;fname&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fdata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;fo&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;fname&lt;/span&gt;&lt;span class=&quot;o&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;'rb'&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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cPickle&lt;/span&gt;&lt;span class=&quot;o&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;fo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;fo&lt;/span&gt;&lt;span class=&quot;o&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;images&lt;/span&gt; &lt;span class=&quot;o&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;s&quot;&gt;'data'&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;batch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'labels'&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;im&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lab&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;images&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;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_train&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&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;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;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;im&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im&lt;/span&gt;&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;im&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;settings&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;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;s&quot;&gt;'image'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im&lt;/span&gt;&lt;span class=&quot;o&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;'label'&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;lab&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;数据定义&quot;&gt;数据定义&lt;/h3&gt;

&lt;p&gt;在模型配置中，定义通过 &lt;code class=&quot;highlighter-rouge&quot;&gt;define_py_data_sources2&lt;/code&gt; 函数从 dataprovider 中读入数据， 其中 args 指定均值文件的路径。如果该配置文件用于预测，则不需要数据定义部分。&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;paddle.trainer_config_helpers&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;n&quot;&gt;is_predict&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;&quot;is_predict&quot;&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;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_predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;define_py_data_sources2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;train_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'data/train.list'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;test_list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'data/test.list'&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;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'dataprovider'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&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;s&quot;&gt;'process'&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;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'mean_path'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'data/mean.meta'&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;highlighter-rouge&quot;&gt;settings&lt;/code&gt; 设置训练使用的优化算法，并指定batch size 、初始学习率、momentum以及L2正则。&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;settings&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;128&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.1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;128.0&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;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;learning_rate_decay_b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50000&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;n&quot;&gt;learning_rate_schedule&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'discexp'&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;MomentumOptimizer&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;n&quot;&gt;regularization&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;L2Regularization&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0005&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;通过 &lt;code class=&quot;highlighter-rouge&quot;&gt;learning_rate_decay_a&lt;/code&gt; (简写$a$） 、&lt;code class=&quot;highlighter-rouge&quot;&gt;learning_rate_decay_b&lt;/code&gt; (简写$b$) 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;learning_rate_schedule&lt;/code&gt; 指定学习率调整策略，这里采用离散指数的方式调节学习率，计算公式如下， $n$ 代表已经处理过的累计总样本数，$lr_{0}$ 即为 &lt;code class=&quot;highlighter-rouge&quot;&gt;settings&lt;/code&gt; 里设置的 &lt;code class=&quot;highlighter-rouge&quot;&gt;learning_rate&lt;/code&gt;。&lt;/p&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;lr = lr_{0} * a^ {\lfloor \frac{n}{ b}\rfloor}&lt;/script&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;

&lt;script type=&quot;math/tex; mode=display&quot;&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;/script&gt;

&lt;h4 id=&quot;35-intermediate-and-softmax&quot;&gt;3.5 intermediate and softmax&lt;/h4&gt;

&lt;h4 id=&quot;vgg-1&quot;&gt;VGG&lt;/h4&gt;

&lt;p&gt;首先介绍VGG模型结构，由于CIFAR10图片大小和数量相比ImageNet数据小很多，因此这里的模型针对CIFAR10数据做了一定的适配。卷积部分引入了BN和Dropout操作。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;定义数据输入及其维度&lt;/p&gt;

    &lt;p&gt;网络输入定义为 &lt;code class=&quot;highlighter-rouge&quot;&gt;data_layer&lt;/code&gt; (数据层)，在图像分类中即为图像像素信息。CIFRAR10是RGB 3通道32x32大小的彩色图，因此输入数据大小为3072(3x32x32)，类别大小为10，即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;datadim&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;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;classdim&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;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;'image'&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;datadim&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;/li&gt;
  &lt;li&gt;
    &lt;p&gt;定义VGG网络核心模块&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;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vgg_bn_drop&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;p&gt;VGG核心模块的输入是数据层，&lt;code class=&quot;highlighter-rouge&quot;&gt;vgg_bn_drop&lt;/code&gt; 定义了16层VGG结构，每层卷积后面引入BN层和Dropout层，详细的定义如下：&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;vgg_bn_drop&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;num_channels&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;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropouts&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;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;img_conv_group&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;ipt&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;n&quot;&gt;num_channels_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;pool_size&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;pool_stride&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;conv_num_filter&lt;/span&gt;&lt;span class=&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_filter&lt;/span&gt;&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;groups&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;conv_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;conv_act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReluActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;conv_with_batchnorm&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;conv_batchnorm_drop_rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dropouts&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;n&quot;&gt;MaxPooling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
	
     &lt;span class=&quot;n&quot;&gt;conv1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&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;64&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;mf&quot;&gt;0.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;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;conv2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv1&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;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;0.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;n&quot;&gt;conv3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv2&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;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;0.4&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;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;conv4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv3&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;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;0.4&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;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;conv5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv4&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;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;0.4&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;mi&quot;&gt;0&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;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_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;conv5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dropout_rate&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;n&quot;&gt;fc1&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;drop&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;512&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;bn&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;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&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;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReluActivation&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;ExtraAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;drop_rate&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;n&quot;&gt;fc2&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;bn&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;512&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fc2&lt;/span&gt;
	
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;2.1. 首先定义了一组卷积网络，即conv_block。卷积核大小为3x3，池化窗口大小为2x2，窗口滑动大小为2，groups决定每组VGG模块是几次连续的卷积操作，dropouts指定Dropout操作的概率。所使用的&lt;code class=&quot;highlighter-rouge&quot;&gt;img_conv_group&lt;/code&gt;是在&lt;code class=&quot;highlighter-rouge&quot;&gt;paddle.trainer_config_helpers&lt;/code&gt;中预定义的模块，由若干组 &lt;code class=&quot;highlighter-rouge&quot;&gt;Conv-&amp;gt;BN-&amp;gt;ReLu-&amp;gt;Dropout&lt;/code&gt; 和 一组 &lt;code class=&quot;highlighter-rouge&quot;&gt;Pooling&lt;/code&gt; 组成，&lt;/p&gt;

    &lt;p&gt;2.2. 五组卷积操作，即 5个conv_block。 第一、二组采用两次连续的卷积操作。第三、四、五组采用三次连续的卷积操作。每组最后一个卷积后面Dropout概率为0，即不使用Dropout操作。&lt;/p&gt;

    &lt;p&gt;2.3. 最后接两层512维的全连接。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;定义分类器&lt;/p&gt;

    &lt;p&gt;通过上面VGG网络提取高层特征，然后经过全连接层映射到类别维度大小的向量，再通过Softmax归一化得到每个类别的概率，也可称作分类器。&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;out&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;net&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_num&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;SoftmaxActivation&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;/li&gt;
  &lt;li&gt;
    &lt;p&gt;定义损失函数和网络输出&lt;/p&gt;

    &lt;p&gt;在有监督训练中需要输入图像对应的类别信息，同样通过&lt;code class=&quot;highlighter-rouge&quot;&gt;data_layer&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;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_predict&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;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;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;n&quot;&gt;class_num&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;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;out&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;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;cost&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;outputs&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;resnet-1&quot;&gt;ResNet&lt;/h3&gt;

&lt;p&gt;ResNet模型的第1、3、4步和VGG模型相同，这里不再介绍。主要介绍第2步即CIFAR10数据集上ResNet核心模块。&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;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;resnet_cifar10&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;depth&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;56&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;highlighter-rouge&quot;&gt;resnet_cifar10&lt;/code&gt;中的一些基本函数，再介绍网络连接过程。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;conv_bn_layer&lt;/code&gt; : 带BN的卷积层。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;shortcut&lt;/code&gt; : 残差模块的”直连”路径，”直连”实际分两种形式：残差模块输入和输出特征通道数不等时，采用1x1卷积的升维操作；残差模块输入和输出通道相等时，采用直连操作。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;basicblock&lt;/code&gt; : 一个基础残差模块，即图9左边所示，由两组3x3卷积组成的路径和一条”直连”路径组成。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;bottleneck&lt;/code&gt; : 一个瓶颈残差模块，即图9右边所示，由上下1x1卷积和中间3x3卷积组成的路径和一条”直连”路径组成。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;layer_warp&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;conv_bn_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;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;stride&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;active_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReluActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
                  &lt;span class=&quot;n&quot;&gt;ch_in&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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_conv_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;filter_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter_size&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;n&quot;&gt;ch_in&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;ch_out&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;n&quot;&gt;stride&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;n&quot;&gt;padding&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;span class=&quot;k&quot;&gt;return&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;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;o&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;act&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active_type&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;shortcut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&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;stride&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;n_in&lt;/span&gt; &lt;span class=&quot;o&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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&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;mi&quot;&gt;1&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;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;LinearActivation&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;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ch_in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_filters&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;conv_bn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;stride&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&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;ch_out&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;1&lt;/span&gt;&lt;span class=&quot;p&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;short&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shortcut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addto_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;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short&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;ReluActivation&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;bottleneck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ch_in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_filter&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;conv_bn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;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;stride&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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&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;ch_out&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;1&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;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&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;ch_out&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;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;n&quot;&gt;LinearActivation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shortcut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch_out&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;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addto_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;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short&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;ReluActivation&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;layer_warp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipt&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;count&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;p&quot;&gt;):&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;block_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&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;stride&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;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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block_func&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;features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;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;tmp&lt;/span&gt;

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

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;resnet_cifar10&lt;/code&gt; 的连接结构主要有以下几个过程。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;底层输入连接一层 &lt;code class=&quot;highlighter-rouge&quot;&gt;conv_bn_layer&lt;/code&gt;，即带BN的卷积层。&lt;/li&gt;
  &lt;li&gt;然后连接3组残差模块即下面配置3组 &lt;code class=&quot;highlighter-rouge&quot;&gt;layer_warp&lt;/code&gt; ，每组采用图 10 左边残差模块组成。&lt;/li&gt;
  &lt;li&gt;最后对网络做均值池化并返回该层。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;注意：除过第一层卷积层和最后一层全连接层之外，要求三组 &lt;code class=&quot;highlighter-rouge&quot;&gt;layer_warp&lt;/code&gt; 总的含参层数能够被6整除，即 &lt;code class=&quot;highlighter-rouge&quot;&gt;resnet_cifar10&lt;/code&gt; 的 depth 要满足 $(depth - 2) % 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;resnet_cifar10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&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;mi&quot;&gt;56&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# depth should be one of 20, 32, 44, 56, 110, 1202&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&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;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;6&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;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&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;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;6&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;nStages&lt;/span&gt; &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;16&lt;/span&gt;&lt;span class=&quot;p&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;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;conv1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_bn_layer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ch_in&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;ch_out&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;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;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;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;res1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_warp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv1&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;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;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;res2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_warp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res1&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;n&quot;&gt;n&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;res3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layer_warp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;basicblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res2&lt;/span&gt;&lt;span class=&quot;p&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;n&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;pool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_pool_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;res3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                         &lt;span class=&quot;n&quot;&gt;pool_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;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;pool_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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pool&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;执行脚本 train.sh 进行模型训练， 其中指定配置文件、设备类型、线程个数、总共训练的轮数、模型存储路径等。&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;sh train.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;脚本 &lt;code class=&quot;highlighter-rouge&quot;&gt;train.sh&lt;/code&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;&lt;span class=&quot;c&quot;&gt;#cfg=models/resnet.py&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;models/vgg.py
&lt;span class=&quot;nv&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;output
&lt;span class=&quot;nv&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;train.log

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;nv&quot;&gt;$cfg&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;true&lt;/span&gt; &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;
    &lt;span class=&quot;nt&quot;&gt;--log_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;--num_passes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;300 &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;nv&quot;&gt;$output&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    2&amp;gt;&amp;amp;1 | tee &lt;span class=&quot;nv&quot;&gt;$log&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;highlighter-rouge&quot;&gt;--config=$cfg&lt;/code&gt; : 指定配置文件，默认是 &lt;code class=&quot;highlighter-rouge&quot;&gt;models/vgg.py&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--use_gpu=true&lt;/code&gt; : 指定使用GPU训练，若使用CPU，设置为false。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--trainer_count=1&lt;/code&gt; : 指定线程个数或GPU个数。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--log_period=100&lt;/code&gt; : 指定日志打印的batch间隔。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;--save_dir=$output&lt;/code&gt; : 指定模型存储路径。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;一轮训练log示例如下所示，经过1个pass， 训练集上平均error为0.79958 ，测试集上平均error为0.7858 。&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;TrainerInternal.cpp:165]  Batch=300 samples=38400 AvgCost=2.07708 CurrentCost=1.96158 Eval: classification_error_evaluator=0.81151  CurrentEval: classification_error_evaluator=0.789297
TrainerInternal.cpp:181]  Pass=0 Batch=391 samples=50000 AvgCost=2.03348 Eval: classification_error_evaluator=0.79958
Tester.cpp:115]  Test samples=10000 cost=1.99246 Eval: classification_error_evaluator=0.7858
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;图12是训练的分类错误率曲线图，运行到第200个pass后基本收敛，最终得到测试集上分类错误率为8.54%。&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/plot.png&quot; width=&quot;400&quot; /&gt;&lt;br /&gt;
图12. CIFAR10数据集上VGG模型的分类错误率
&lt;/p&gt;

&lt;h2 id=&quot;模型应用&quot;&gt;模型应用&lt;/h2&gt;

&lt;p&gt;在训练完成后，模型会保存在路径 &lt;code class=&quot;highlighter-rouge&quot;&gt;output/pass-%05d&lt;/code&gt; 下，例如第300个pass的模型会保存在路径 &lt;code class=&quot;highlighter-rouge&quot;&gt;output/pass-00299&lt;/code&gt;。 可以使用脚本 &lt;code class=&quot;highlighter-rouge&quot;&gt;classify.py&lt;/code&gt; 对图片进行预测或提取特征，注意该脚本默认使用模型配置为 &lt;code class=&quot;highlighter-rouge&quot;&gt;models/vgg.py&lt;/code&gt;，&lt;/p&gt;

&lt;h3 id=&quot;预测&quot;&gt;预测&lt;/h3&gt;

&lt;p&gt;可以按照下面方式预测图片的类别，默认使用GPU预测，如果使用CPU预测，在后面加参数 &lt;code class=&quot;highlighter-rouge&quot;&gt;-c&lt;/code&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;python classify.py &lt;span class=&quot;nt&quot;&gt;--job&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;predict &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;output/pass-00299 &lt;span class=&quot;nt&quot;&gt;--data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;image/dog.png &lt;span class=&quot;c&quot;&gt;# -c&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-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Label of image/dog.png is: 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;特征提取&quot;&gt;特征提取&lt;/h3&gt;

&lt;p&gt;可以按照下面方式对图片提取特征，和预测使用方式不同的是指定job类型为extract，并需要指定提取的层。&lt;code class=&quot;highlighter-rouge&quot;&gt;classify.py&lt;/code&gt; 默认以第一层卷积特征为例提取特征，并画出了类似图13的可视化图。VGG模型的第一层卷积有64个通道，图13展示了每个通道的灰度图。&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;python classify.py &lt;span class=&quot;nt&quot;&gt;--job&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;extract &lt;span class=&quot;nt&quot;&gt;--model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;output/pass-00299 &lt;span class=&quot;nt&quot;&gt;--data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;image/dog.png &lt;span class=&quot;c&quot;&gt;# -c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;image/fea_conv0.png&quot; width=&quot;500&quot; /&gt;&lt;br /&gt;
图13. 卷积特征可视化图 
&lt;/p&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

&lt;p&gt;传统图像分类方法由多个阶段构成，框架较为复杂，而端到端的CNN模型结构可一步到位，而且大幅度提升了分类准确率。本文我们首先介绍VGG、GoogleNet、ResNet三个经典的模型；然后基于CIFAR10数据集，介绍如何使用PaddlePaddle配置和训练CNN模型，尤其是VGG和ResNet模型；最后介绍如何使用PaddlePaddle的API接口对图片进行预测和特征提取。对于其他数据集比如ImageNet，配置和训练流程是同样的，大家可以自行进行实验。&lt;/p&gt;

&lt;h2 id=&quot;参考文献&quot;&gt;参考文献&lt;/h2&gt;

&lt;p&gt;[1] Gao H, Mao J, Zhou J, et al. &lt;a href=&quot;http://papers.nips.cc/paper/5641-are-you-talking-to-a-machine-dataset-and-methods-for-multilingual-image-question&quot;&gt;Are you talking to a machine? dataset and methods for multilingual image question&lt;/a&gt;[C]//Advances in Neural Information Processing Systems. 2015: 2296-2304.&lt;/p&gt;

&lt;p&gt;[2] N. Dalal, B. Triggs, &lt;a href=&quot;http://vision.stanford.edu/teaching/cs231b_spring1213/papers/CVPR05_DalalTriggs.pdf&quot;&gt;Histograms of Oriented Gradients for Human Detection&lt;/a&gt;, Proc. IEEE Conf. Computer Vision and Pattern Recognition, 2005.&lt;/p&gt;

&lt;p&gt;[3] Ahonen, T., Hadid, A., and Pietikinen, M. (2006). &lt;a href=&quot;http://ieeexplore.ieee.org/document/1717463/&quot;&gt;Face description with local binary patterns: Application to face recognition&lt;/a&gt;. PAMI, 28.&lt;/p&gt;

&lt;p&gt;[4] J. Sivic, A. Zisserman, &lt;a href=&quot;http://www.robots.ox.ac.uk/~vgg/publications/papers/sivic03.pdf&quot;&gt;Video Google: A Text Retrieval Approach to Object Matching in Videos&lt;/a&gt;, Proc. Ninth Int’l Conf. Computer Vision, pp. 1470-1478, 2003.&lt;/p&gt;

&lt;p&gt;[5] B. Olshausen, D. Field, &lt;a href=&quot;http://redwood.psych.cornell.edu/papers/olshausen_field_1997.pdf&quot;&gt;Sparse Coding with an Overcomplete Basis Set: A Strategy Employed by V1?&lt;/a&gt;, Vision Research, vol. 37, pp. 3311-3325, 1997.&lt;/p&gt;

&lt;p&gt;[6] Wang, J., Yang, J., Yu, K., Lv, F., Huang, T., and Gong, Y. (2010). &lt;a href=&quot;http://ieeexplore.ieee.org/abstract/document/5540018/&quot;&gt;Locality-constrained Linear Coding for image classification&lt;/a&gt;. In CVPR.&lt;/p&gt;

&lt;p&gt;[7] Perronnin, F., Sánchez, J., &amp;amp; Mensink, T. (2010). &lt;a href=&quot;http://dl.acm.org/citation.cfm?id=1888101&quot;&gt;Improving the fisher kernel for large-scale image classification&lt;/a&gt;. In ECCV (4).&lt;/p&gt;

&lt;p&gt;[8] Lin, Y., Lv, F., Cao, L., Zhu, S., Yang, M., Cour, T., Yu, K., and Huang, T. (2011). &lt;a href=&quot;http://ieeexplore.ieee.org/document/5995477/&quot;&gt;Large-scale image clas- sification: Fast feature extraction and SVM training&lt;/a&gt;. In CVPR.&lt;/p&gt;

&lt;p&gt;[9] Krizhevsky, A., Sutskever, I., and Hinton, G. (2012). &lt;a href=&quot;http://www.cs.toronto.edu/~kriz/imagenet_classification_with_deep_convolutional.pdf&quot;&gt;ImageNet classification with deep convolutional neu- ral networks&lt;/a&gt;. In NIPS.&lt;/p&gt;

&lt;p&gt;[10] G.E. Hinton, N. Srivastava, A. Krizhevsky, I. Sutskever, and R.R. Salakhutdinov. &lt;a href=&quot;https://arxiv.org/abs/1207.0580&quot;&gt;Improving neural networks by preventing co-adaptation of feature detectors&lt;/a&gt;. arXiv preprint arXiv:1207.0580, 2012.&lt;/p&gt;

&lt;p&gt;[11] K. Chatfield, K. Simonyan, A. Vedaldi, A. Zisserman. &lt;a href=&quot;https://arxiv.org/abs/1405.3531&quot;&gt;Return of the Devil in the Details: Delving Deep into Convolutional Nets&lt;/a&gt;. BMVC, 2014。&lt;/p&gt;

&lt;p&gt;[12] Szegedy, C., Liu, W., Jia, Y., Sermanet, P., Reed, S., Anguelov, D., Erhan, D., Vanhoucke, V., Rabinovich, A., &lt;a href=&quot;https://arxiv.org/abs/1409.4842&quot;&gt;Going deeper with convolutions&lt;/a&gt;. In: CVPR. (2015)&lt;/p&gt;

&lt;p&gt;[13] Lin, M., Chen, Q., and Yan, S. &lt;a href=&quot;https://arxiv.org/abs/1312.4400&quot;&gt;Network in network&lt;/a&gt;. In Proc. ICLR, 2014.&lt;/p&gt;

&lt;p&gt;[14] S. Ioffe and C. Szegedy. &lt;a href=&quot;https://arxiv.org/abs/1502.03167&quot;&gt;Batch normalization: Accelerating deep network training by reducing internal covariate shift&lt;/a&gt;. In ICML, 2015.&lt;/p&gt;

&lt;p&gt;[15] K. He, X. Zhang, S. Ren, J. Sun. &lt;a href=&quot;https://arxiv.org/abs/1512.03385&quot;&gt;Deep Residual Learning for Image Recognition&lt;/a&gt;. CVPR 2016.&lt;/p&gt;

&lt;p&gt;[16] Szegedy, C., Vanhoucke, V., Ioffe, S., Shlens, J., Wojna, Z. &lt;a href=&quot;https://arxiv.org/abs/1512.00567&quot;&gt;Rethinking the incep-tion architecture for computer vision&lt;/a&gt;. In: CVPR. (2016).&lt;/p&gt;

&lt;p&gt;[17] Szegedy, C., Ioffe, S., Vanhoucke, V. &lt;a href=&quot;https://arxiv.org/abs/1602.07261&quot;&gt;Inception-v4, inception-resnet and the impact of residual connections on learning&lt;/a&gt;. arXiv:1602.07261 (2016).&lt;/p&gt;

&lt;p&gt;[18] Everingham, M., Eslami, S. M. A., Van Gool, L., Williams, C. K. I., Winn, J. and Zisserman, A. &lt;a href=&quot;http://link.springer.com/article/10.1007/s11263-014-0733-5&quot;&gt;The Pascal Visual Object Classes Challenge: A Retrospective&lt;/a&gt;. International Journal of Computer Vision, 111(1), 98-136, 2015.&lt;/p&gt;

&lt;p&gt;[19] He, K., Zhang, X., Ren, S., and Sun, J. &lt;a href=&quot;https://arxiv.org/abs/1502.01852&quot;&gt;Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification&lt;/a&gt;. ArXiv e-prints, February 2015.&lt;/p&gt;

&lt;p&gt;[20] http://deeplearning.net/tutorial/lenet.html&lt;/p&gt;

&lt;p&gt;[21] https://www.cs.toronto.edu/~kriz/cifar.html&lt;/p&gt;

&lt;p&gt;[22] http://cs231n.github.io/classification/&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;
&lt;a rel=&quot;license&quot; href=&quot;http://creativecommons.org/licenses/by-nc-sa/4.0/&quot;&gt;&lt;img alt=&quot;知识共享许可协议&quot; style=&quot;border-width:0&quot; src=&quot;https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span xmlns:dct=&quot;http://purl.org/dc/terms/&quot; href=&quot;http://purl.org/dc/dcmitype/Text&quot; property=&quot;dct:title&quot; rel=&quot;dct:type&quot;&gt;本教程&lt;/span&gt; 由 &lt;a xmlns:cc=&quot;http://creativecommons.org/ns#&quot; href=&quot;http://book.paddlepaddle.org&quot; property=&quot;cc:attributionName&quot; rel=&quot;cc:attributionURL&quot;&gt;PaddlePaddle&lt;/a&gt; 创作，采用 &lt;a rel=&quot;license&quot; href=&quot;http://creativecommons.org/licenses/by-nc-sa/4.0/&quot;&gt;知识共享 署名-非商业性使用-相同方式共享 4.0 国际 许可协议&lt;/a&gt;进行许可。&lt;/p&gt;

&lt;h2 id=&quot;3-模型介绍&quot;&gt;3. 模型介绍&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>安装PIL</title>
   <link href="http://hxhlwf.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;mkdir ~/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 install
&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;div class=&quot;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;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;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;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;

cp ./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 install
&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://hxhlwf.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;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;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;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://hxhlwf.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;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;k&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;k&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;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;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://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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://hxhlwf.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;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;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;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;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;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;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;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;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;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;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;highlighter-rouge&quot;&gt;\(\boldsymbol{x}\)&lt;/code&gt;是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(n\times 1\)&lt;/code&gt;的列向量，所以&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\boldsymbol{x}^T\)&lt;/code&gt;是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(1\times n\)&lt;/code&gt;的行向量）,所以&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\boldsymbol{x}_T\boldsymbol{y}\)&lt;/code&gt;是一个标量，他等于他的转置：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\boldsymbol{A}\in \mathbb{R}^{m\times n}\)&lt;/code&gt;是一个已知的矩阵，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\boldsymbol{b}\in \mathbb{R}^{m}\)&lt;/code&gt;是一个已知的向量，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\boldsymbol{x}\in \mathbb{R}^{n}\)&lt;/code&gt;是一个未知的向量：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;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;highlighter-rouge&quot;&gt;\(\boldsymbol{A}^{-1}\)&lt;/code&gt;满足：&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(\boldsymbol{x}\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\boldsymbol{y}\)&lt;/code&gt;都是解，那么&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\boldsymbol{A}\)&lt;/code&gt;的n个方向的变换，最终到达m维目标向量&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\boldsymbol{b}\)&lt;/code&gt;所在的位置。那么，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;就表示在这n个方向上，我分别走了多少步。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\({\boldsymbol{v}^{(1)},...,\boldsymbol{v}^{(n)}}\)&lt;/code&gt;的线性组合，指的就是每一个向量&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\boldsymbol{v}^{(i)}\)&lt;/code&gt;都乘以一个系数，并进行累加得到&lt;code class=&quot;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://hxhlwf.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;#强化学习概述&quot;&gt;强化学习概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#价值函数value-function&quot;&gt;价值函数（VALUE FUNCTION）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#贴现因子discounting-factor&quot;&gt;贴现因子（DISCOUNTING FACTOR）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#策略函数policy-function&quot;&gt;策略函数（POLICY FUNCTION）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#q-函数q-function&quot;&gt;Q-函数（Q-FUNCTION）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#q-学习q-learning&quot;&gt;Q-学习（Q-Learning）&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#深度-q-学习deep-q-learning&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;/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;
</content>
 </entry>
 
 <entry>
   <title>bigflow的坑们</title>
   <link href="http://hxhlwf.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;#自动缩减&quot;&gt;自动缩减&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#一些reduce的task非常慢&quot;&gt;一些reduce的task非常慢&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;#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;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;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;&lt;a href=&quot;http://bigflow.baidu.com/doc/faq.html#bigflow&quot;&gt;http://bigflow.baidu.com/doc/faq.html#bigflow&lt;/a&gt;&lt;/p&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;p&gt;lazy_var从1.0.4版本开始提供，目前只提供 一个临时下载地址：wget http://bigflow.baidu.com/download/module/lazy_var.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;c&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;o&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;o&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;c&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;o&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;o&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;o&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;c&quot;&gt;# 可以在本地直接获取lazy_var内容&lt;/span&gt;
    &lt;span class=&quot;c&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;o&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;o&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;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;o&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;o&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;c&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;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;n&quot;&gt;get_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c&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;o&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;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import my_lazy_var
from brand_tagger import BrandTagger

def init_tagger():
    &quot;&quot;&quot;
    &quot;&quot;&quot;

    tagger = BrandTagger()
    fname_model = &quot;./third/model/model&quot;
    fname_bigram = &quot;./third/model/bigram.dat&quot;
    tagger.set_fnames(fname_model, fname_bigram)
    return tagger


def do_tag(line, tagger):
    &quot;&quot;&quot;
    &quot;&quot;&quot;

    if not tagger.has_loaded():
        ret = tagger.load_model()

    csid = line[0]
    userid = line[1]
    real_rawpicurl = line[2]
    encoded_data = line[3]
    raw_width = line[4]
    raw_height = line[5]
    descs = line[6]
    picurl = line[7]
    srcs = line[8]
    ext_json = line[9]
    to_cmp_descs_str = line[10]
    search_word = line[11]
    search_word_list = search_word.split(&quot;;&quot;)

    formatted_res_list = []
    for item in search_word_list:
        sub_item = item.split(&quot;:&quot;)
        word = sub_item[0]
        prob = sub_item[1]
        brands = tagger.do_tag(word)
        brand_tag_res = word
        for brand in brands:
            brand_tag_res = brand_tag_res.replace(brand, &quot;{brand}&quot;)
        formatted_res_list.append(&quot;:&quot;.join([brand_tag_res, prob]))
    formatted_res = &quot;;&quot;.join(formatted_res_list)

    res_list = line[: -1] + [formatted_res]

    return res_list

_pipeline.add_file(workspace_path + &quot;./opt-feeds-image/image-tagging/postprocess/my_lazy_var.py&quot;, &quot;my_lazy_var.py&quot;)
tagger = my_lazy_var.declare(init_tagger)

format_brand_res = filter_res\
        .map(lambda x: do_tag(x, tagger.get()))\
        .map(lambda x: &quot;\t&quot;.join(x))


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>deep learning book-概览</title>
   <link href="http://hxhlwf.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://hxhlwf.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神奇的gcc48&quot;&gt;1.神奇的gcc48&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;#1optcompilergcc-482lib或者lib64添加到ld_library_path中吗&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使用gcc4编译得到的so可以被gcc3编译得到的binary加载吗&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;h1 id=&quot;1神奇的gcc48&quot;&gt;1.神奇的gcc48&lt;/h1&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;h1 id=&quot;2注意事项&quot;&gt;2.注意事项&lt;/h1&gt;

&lt;h2 id=&quot;1optcompilergcc-482lib或者lib64添加到ld_library_path中吗&quot;&gt;1）/opt/compiler/gcc-4.8.2/lib或者lib64添加到LD_LIBRARY_PATH中吗？&lt;/h2&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;h2 id=&quot;2使用gcc4编译得到的so可以被gcc3编译得到的binary加载吗&quot;&gt;2）使用gcc4编译得到的so，可以被gcc3编译得到的binary加载吗？&lt;/h2&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 install

&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;

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://hxhlwf.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;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://hxhlwf.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 install
&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 factor
 &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;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;c&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;o&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;o&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;o&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;o&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;c&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;c&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;c&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 install
&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;c&quot;&gt;# -v 3: access deep information like alpha,beta,prob&lt;/span&gt;
    &lt;span class=&quot;c&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;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;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;c&quot;&gt;# clear internal context&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;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# add context&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;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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;o&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;c&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;o&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;o&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;o&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;o&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;o&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;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;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;o&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;o&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;o&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;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;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;o&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;o&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;o&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;c&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://hxhlwf.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://hxhlwf.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-在paddlepaddle上实现&quot;&gt;&lt;strong&gt;4. 在paddlepaddle上实现&lt;/strong&gt;&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-dataprovider&quot;&gt;4.2 dataprovider&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#421-initializer的流程&quot;&gt;4.2.1 initializer的流程：&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#421-process的流程&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-linear_crf&quot;&gt;4.3 linear_crf&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#44-rnn_crf&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;掉渣天的徐老师已经在开源版本的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;c&quot;&gt;# Feature combination patterns.(crf要用到的特征？)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# [[-1,0], [0,0]]  means previous token at column 0 and current token at &lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 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;c&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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;c&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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;o&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;c&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;o&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;c&quot;&gt;#create_dictionaries函数中：&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;#cutoff: a list of numbers. If count of a feature is smaller than this,              &lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 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;c&quot;&gt;#if oov_policy[i] is OOV_POLICY_USE, id 0 is reserved for OOV features of            &lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#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;c&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;o&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;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;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;o&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;c&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;o&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;o&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;o&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;o&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;c&quot;&gt;# placeholder so that len(dct) will be the number of features&lt;/span&gt;
            &lt;span class=&quot;c&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;o&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 &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;d dict size=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;d, ignored &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&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;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;o&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;c&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;o&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;c&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;o&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;o&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;o&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;o&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: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;o&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;c&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;o&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;o&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;o&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: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ids&lt;/span&gt;&lt;span class=&quot;o&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;o&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://hxhlwf.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;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;highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;有&lt;code class=&quot;highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;个元素，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(b\)&lt;/code&gt;有&lt;code class=&quot;highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;个元素(&lt;code class=&quot;highlighter-rouge&quot;&gt;\(N\)&lt;/code&gt;为奇数)，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(c\)&lt;/code&gt;有&lt;code class=&quot;highlighter-rouge&quot;&gt;\(M\)&lt;/code&gt;个元素。当&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;或&lt;code class=&quot;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;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;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;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;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;highlighter-rouge&quot;&gt;\(in\)&lt;/code&gt;是一个输入vector（batch_size * data_dim）,&lt;code class=&quot;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;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;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;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;o&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://hxhlwf.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;highlighter-rouge&quot;&gt;\(P(X|S)\)&lt;/code&gt;最大的词序列X的分布。而词性标注中，可以看作在给定序列X的情况下，寻找一组最可能的词性标签分布T，使得&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(x=\{X_1,L,X_n\}\)&lt;/code&gt;上给出其&lt;strong&gt;联合概率分布&lt;/strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(P\)&lt;/code&gt;。贝叶斯网络表示中的主要问题是：即使在随机变量&lt;strong&gt;仅有2种取值&lt;/strong&gt;的简单情况下，&lt;strong&gt;一个联合概率分布也需要对&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_1,L,x_n\)&lt;/code&gt;的所有&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(p(X_Q|X_E=x)\)&lt;/code&gt;，其中，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(X_Q\)&lt;/code&gt;是询问变量的集合，&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;和定义在&lt;code class=&quot;highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;上的势函数组成。一个无向图&lt;code class=&quot;highlighter-rouge&quot;&gt;\(G=(V,E)\)&lt;/code&gt;，每个顶点&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_i \in V\)&lt;/code&gt;表示在集合&lt;code class=&quot;highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;上的一个随机变量，每条边&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\{x_i,x_j\}\in E(i\neq j)\)&lt;/code&gt;表示直接相连的两个随机变量&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_i\)&lt;/code&gt;和&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;是无向图&lt;code class=&quot;highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;的一个子团，且不能再加进任何一个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;的结点使其成为一个更大的团，则称&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\mathbf{x}_C\)&lt;/code&gt;表示团&lt;code class=&quot;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;中的所有结点，用&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)\)&lt;/code&gt;表示团势能。由于要求势能函数&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)\)&lt;/code&gt;&lt;strong&gt;非负&lt;/strong&gt;，所以，一般将&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)\)&lt;/code&gt;定义为：&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)=exp\{-E(\mathbf{x}_C)\}\)&lt;/code&gt;，其中&lt;code class=&quot;highlighter-rouge&quot;&gt;\(E(\mathbf{x}_C)\)&lt;/code&gt;称为&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\mathbf{x}_C\)&lt;/code&gt;的能量函数。&lt;/p&gt;

&lt;p&gt;如果分布&lt;code class=&quot;highlighter-rouge&quot;&gt;\(p_{\phi }(x_1,x_2,...,x_n)\)&lt;/code&gt;的图模型可以表示为一个马尔可夫网络&lt;code class=&quot;highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;，当&lt;code class=&quot;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;上完全子图的集合【《统计学习方法》p193以及《机器学习》p323说，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;应该是&lt;strong&gt;最大团&lt;/strong&gt;】时，我们说&lt;code class=&quot;highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;上的分布&lt;code class=&quot;highlighter-rouge&quot;&gt;\(p_{\phi }(x_1,x_2,...,x_n)\)&lt;/code&gt;可以用&lt;code class=&quot;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;的团势能函数&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\phi (\mathbf{x}_C)\)&lt;/code&gt;进行因子化：&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\phi =\{\phi_1(\textbf{x}_{C_{1}}),...,\phi_K(\textbf{x}_{C_{K}})\}\)&lt;/code&gt;。&lt;code class=&quot;highlighter-rouge&quot;&gt;\(p_{\phi }(x_1,x_2,...,x_n)\)&lt;/code&gt;可以看作&lt;code class=&quot;highlighter-rouge&quot;&gt;\(H\)&lt;/code&gt;上的一个吉布斯分布（Gibbs distribution），其概率分布密度为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(Z\)&lt;/code&gt;是一个归一化常量，称为划分函数（partition function）。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(C_i\)&lt;/code&gt;）。【另外，个人对&lt;code class=&quot;highlighter-rouge&quot;&gt;\(Z\)&lt;/code&gt;的理解，可以理解为遍历每个结点，把包含他的子团乘起来，最后累加，不知对不对……】显然，在无向图模型中每个&lt;code class=&quot;highlighter-rouge&quot;&gt;\(C_i\)&lt;/code&gt;对应于一个团，而相应的吉布斯分布就是整个图模型的概率分布。因子化的乘积运算可以变成加法运算：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(A\)&lt;/code&gt;表示待消歧问题所有可能候选结果的集合，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(B\)&lt;/code&gt;表示当前歧义点所在上下文信息构成的集合，则称&lt;code class=&quot;highlighter-rouge&quot;&gt;\((a,b)\)&lt;/code&gt;为模型的一个特征。一般定义&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\{0,1\}\)&lt;/code&gt;域上的一个二值函数来表示特征：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(f(a,b)\)&lt;/code&gt;称作特征。将“判定歧义问题为某种可能的结果&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a\in A\)&lt;/code&gt;”看作一个事件，该歧义点所在的上下文出现的某些信息看作这个事件发生的条件&lt;code class=&quot;highlighter-rouge&quot;&gt;\(b\in B\)&lt;/code&gt;，那么，建立最大熵模型的目的就是计算判定结果&lt;code class=&quot;highlighter-rouge&quot;&gt;\(a\)&lt;/code&gt;的条件概率&lt;code class=&quot;highlighter-rouge&quot;&gt;\(p(a|b)\)&lt;/code&gt;，即利用条件最大熵模型选择条件概率&lt;code class=&quot;highlighter-rouge&quot;&gt;\(p(a|b)\)&lt;/code&gt;最大的候选结果作为最终的判定结果：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(P(Y|X)\)&lt;/code&gt;&lt;/strong&gt;而非联合概率&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(G=(V,E)\)&lt;/code&gt;为一个无向图，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;为结点集合，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(E\)&lt;/code&gt;为无向边的集合。&lt;code class=&quot;highlighter-rouge&quot;&gt;\(Y=\{Y_v|v\in V\}\)&lt;/code&gt;，即&lt;code class=&quot;highlighter-rouge&quot;&gt;\(V\)&lt;/code&gt;中的 每一个结点对应于一个随机变量&lt;code class=&quot;highlighter-rouge&quot;&gt;\(Y_v\)&lt;/code&gt;，其取值范围为可能的标记集合&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\{y\}\)&lt;/code&gt;。如果以观察序列&lt;code class=&quot;highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;为条件，每一个随机变量&lt;code class=&quot;highlighter-rouge&quot;&gt;\(Y_v\)&lt;/code&gt;都满足以下马尔可夫特性 \eqref{eq:9.1}：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(，w\sim v\)&lt;/code&gt;表示两个结点在图&lt;code class=&quot;highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;中是邻近结点。那么&lt;code class=&quot;highlighter-rouge&quot;&gt;\((X,Y)\)&lt;/code&gt;为一个随机场。&lt;/p&gt;

&lt;p&gt;理论上，只要在标记序列中描述了一定的条件独立性，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(G\)&lt;/code&gt;的图结构可以是任意的。对序列进行建模可以形成最简单、最普通的链式结构（chain-structured）图，结点对应标记序列&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;的元素之间并不存在图结构，因为这里只是将观察序列&lt;code class=&quot;highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;作为条件，并不对其做任何独立性假设。&lt;/p&gt;

&lt;p&gt;在给定观察序列&lt;code class=&quot;highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;时，某个特定标记序列&lt;code class=&quot;highlighter-rouge&quot;&gt;\(Y\)&lt;/code&gt;的概率可以定义为 \eqref{eq:9.2}：
&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;及其标注序列&lt;code class=&quot;highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;及&lt;code class=&quot;highlighter-rouge&quot;&gt;\(i-1\)&lt;/code&gt;位置上标记的转移概率。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_k(y_i,X,i)\)&lt;/code&gt;是&lt;strong&gt;状态函数&lt;/strong&gt;，表示对于观察序列&lt;code class=&quot;highlighter-rouge&quot;&gt;\(X\)&lt;/code&gt;其&lt;code class=&quot;highlighter-rouge&quot;&gt;\(i\)&lt;/code&gt;位置的标记概率。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\lambda _j\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\mu _k\)&lt;/code&gt;分别是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(t_j\)&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s_k\)&lt;/code&gt;的权重，需要从训练样本中预估出来（模型参数）。&lt;/p&gt;

&lt;p&gt;参照最大熵模型的做法，在定义特征函数时可以定义一组关于观察序列的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\{0,1\}\)&lt;/code&gt;二值特征&lt;code class=&quot;highlighter-rouge&quot;&gt;\(b(X,i)\)&lt;/code&gt;来标识训练样本中某些分布特性，例如&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;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;highlighter-rouge&quot;&gt;\(f_j(y_{i-1},y_i,X,i)\)&lt;/code&gt;表示状态特征&lt;code class=&quot;highlighter-rouge&quot;&gt;\(s(y_{i-1},y_i,X,i)\)&lt;/code&gt;或转移函数&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(Z(X)\)&lt;/code&gt;为归一化因子\eqref{eq:9.5}：
&lt;code class=&quot;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://hxhlwf.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简介&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数据集简介&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数据预处理&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训练&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生成文本&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;c&quot;&gt;#其实seq_to_seq_data里面，读trg.dict和src.dict的时候，使用了&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#trg_dict = dict()&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#for line_count, line in enumerate(open(trg_lang_dict, &quot;r&quot;)):&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    trg_dict[line.strip()] = line_count&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#对dict文件的每个单词，按照行号进行了编号！！&lt;/span&gt;

&lt;span class=&quot;c&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;o&quot;&gt;.&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;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;c&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;c&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;c&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;c&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;c&quot;&gt;#train.cluster.conf&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#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;o&quot;&gt;.&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;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;c&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;c&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;c&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;c&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;c&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;c&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 | tee &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;nv&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;
cp ../dataprovider.py &lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt;/.
cp ../seqToseq_net.py &lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt;/.
cp ../data/pre-wmt14/src.dict &lt;span class=&quot;nv&quot;&gt;$dir&lt;/span&gt;/.
cp ../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; | awk &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; | awk &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;o&quot;&gt;.&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;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;c&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;c&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;c&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;c&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 | tee &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;【然而。。用jumbo装的paddle有问题。。版本太老。。我们从源码再来装一个好了。。：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://deeplearning.baidu.com/doc_cn/build/internal/build_from_source_zh_cn.html#jumbo&quot;&gt;http://deeplearning.baidu.com/doc_cn/build/internal/build_from_source_zh_cn.html#jumbo&lt;/a&gt;&lt;/p&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://hxhlwf.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;#老paddle使用整理&quot;&gt;老Paddle使用整理&lt;/a&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;#111-预处理的文本结构&quot;&gt;1.1.1 预处理的文本结构：&lt;/a&gt;
            &lt;ul&gt;
              &lt;li&gt;&lt;a href=&quot;#112-预处理的7个param&quot;&gt;1.1.2 预处理的7个param&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href=&quot;#113-proto格式&quot;&gt;1.1.3 proto格式&lt;/a&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=&quot;#1131-local预处理&quot;&gt;1.1.3.1 local预处理&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=&quot;#1132-cluster预处理&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-使用pydataprovider格式&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-训练&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;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;c&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;c&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;c&quot;&gt;#ada_epsilon=1.0,&lt;/span&gt;
         &lt;span class=&quot;c&quot;&gt;#num_batches_per_send_parameter=2,&lt;/span&gt;
         &lt;span class=&quot;c&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;c&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;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;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;c&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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_reverse_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_reverse_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_reverse_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_reverse_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s_reverse_rnn&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;c&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;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;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_&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;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;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;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;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;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;c&quot;&gt;#active_type = &quot;softmax&quot;,&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;#active_type = &quot;sigmoid&quot;,&lt;/span&gt;
    &lt;span class=&quot;c&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;c&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;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;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;c&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;c&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;c&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;c&quot;&gt;#Evaluator(&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    name = &quot;chunk_f1&quot;,&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    type = &quot;chunk&quot;,&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    inputs = [&quot;crf_layer&quot;, &quot;label&quot;],&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    #chunk_scheme = &quot;plain&quot;,&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    chunk_scheme = &quot;IOB&quot;,&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    #num_chunk_types = 3,&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#    num_chunk_types = 2,&lt;/span&gt;
&lt;span class=&quot;c&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;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;老paddle支持的所有layer：
&lt;a href=&quot;http://wiki.baidu.com/pages/viewpage.action?pageId=42366874&quot;&gt;http://wiki.baidu.com/pages/viewpage.action?pageId=42366874&lt;/a&gt;&lt;/p&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://hxhlwf.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;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://hxhlwf.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;#1-basic-knowledges&quot;&gt;&lt;strong&gt;1. Basic Knowledges&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#11-machine-learning&quot;&gt;1.1 Machine Learning&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#111-树模型&quot;&gt;1.1.1 &lt;strong&gt;树模型&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#112-基础算法&quot;&gt;1.1.2 &lt;strong&gt;基础算法&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#113-统计学习方法&quot;&gt;1.1.3 &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;#12-deep-learning&quot;&gt;1.2 Deep Learning&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#121-cnn&quot;&gt;1.2.1 CNN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#122-rnn&quot;&gt;1.2.2 RNN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#123-gan&quot;&gt;1.2.3 GAN&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#124-reinforcement-learning&quot;&gt;1.2.4 Reinforcement Learning&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#125-pnnprogressive-neural-network连续神经网络&quot;&gt;1.2.5 PNN(Progressive Neural Network)连续神经网络&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#126-图卷积网络&quot;&gt;1.2.6 图卷积网络&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#127-copynet&quot;&gt;1.2.7 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;#2-useful-tools&quot;&gt;&lt;strong&gt;2. Useful Tools&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#21-datasets&quot;&gt;2.1 Datasets&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-pretrained-models&quot;&gt;2.2 pretrained models&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#23-deep-learning-tools&quot;&gt;2.3 Deep Learning Tools&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#231-mxnet&quot;&gt;2.3.1 mxnet&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#232-theano&quot;&gt;2.3.2 theano&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#233-torch&quot;&gt;2.3.3 torch&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#234-tensorflow&quot;&gt;2.3.4 tensorflow&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#235-docker&quot;&gt;2.3.5 docker&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#24-docker-images&quot;&gt;2.4 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;#3-useful-courses--speeches&quot;&gt;&lt;strong&gt;3. Useful Courses &amp;amp;&amp;amp; Speeches&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-courses&quot;&gt;3.1 Courses&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-speeches&quot;&gt;3.2 Speeches&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;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-applications&quot;&gt;4. &lt;strong&gt;Applications&lt;/strong&gt;&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-nlp&quot;&gt;4.1 NLP&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#411-分词&quot;&gt;4.1.1 分词&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#412-text-abstraction&quot;&gt;4.1.2 Text Abstraction&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-image-processing&quot;&gt;4.2 Image Processing&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#421-image2txt&quot;&gt;4.2.1 image2txt&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#43-collections&quot;&gt;4.3 Collections&lt;/a&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#431-csdn深度学习代码专栏&quot;&gt;4.3.1 csdn深度学习代码专栏&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#432-chiristopher-olah的博客&quot;&gt;4.3.2 chiristopher olah的博客&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#433-激活函数系列&quot;&gt;4.3.3 激活函数系列&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#434-梯度下降算法系列&quot;&gt;4.3.4 梯度下降算法系列&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;h1 id=&quot;1-basic-knowledges&quot;&gt;&lt;strong&gt;1. Basic Knowledges&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;11-machine-learning&quot;&gt;1.1 Machine Learning&lt;/h2&gt;

&lt;h3 id=&quot;111-树模型&quot;&gt;1.1.1 &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;112-基础算法&quot;&gt;1.1.2 &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;113-统计学习方法&quot;&gt;1.1.3 &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;12-deep-learning&quot;&gt;1.2 Deep Learning&lt;/h2&gt;

&lt;h3 id=&quot;121-cnn&quot;&gt;1.2.1 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;122-rnn&quot;&gt;1.2.2 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;/ol&gt;

&lt;h3 id=&quot;123-gan&quot;&gt;1.2.3 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;124-reinforcement-learning&quot;&gt;1.2.4 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;125-pnnprogressive-neural-network连续神经网络&quot;&gt;1.2.5 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;126-图卷积网络&quot;&gt;1.2.6 图卷积网络&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;127-copynet&quot;&gt;1.2.7 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;2-useful-tools&quot;&gt;&lt;strong&gt;2. Useful Tools&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;21-datasets&quot;&gt;2.1 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;&lt;em&gt;《YouTube-8M: A Large-Scale Video Classification Benchmark》&lt;/em&gt;&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;22-pretrained-models&quot;&gt;2.2 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;23-deep-learning-tools&quot;&gt;2.3 Deep Learning Tools&lt;/h2&gt;

&lt;h3 id=&quot;231-mxnet&quot;&gt;2.3.1 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;232-theano&quot;&gt;2.3.2 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;233-torch&quot;&gt;2.3.3 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;234-tensorflow&quot;&gt;2.3.4 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;235-docker&quot;&gt;2.3.5 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;24-docker-images&quot;&gt;2.4 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;3-useful-courses--speeches&quot;&gt;&lt;strong&gt;3. Useful Courses &amp;amp;&amp;amp; Speeches&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;31-courses&quot;&gt;3.1 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;32-speeches&quot;&gt;3.2 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;33-经典学习资源&quot;&gt;3.3 经典学习资源&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;4-applications&quot;&gt;4. &lt;strong&gt;Applications&lt;/strong&gt;&lt;/h1&gt;

&lt;h2 id=&quot;41-nlp&quot;&gt;4.1 NLP&lt;/h2&gt;

&lt;h3 id=&quot;411-分词&quot;&gt;4.1.1 分词&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;412-text-abstraction&quot;&gt;4.1.2 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;42-image-processing&quot;&gt;4.2 Image Processing&lt;/h2&gt;

&lt;h3 id=&quot;421-image2txt&quot;&gt;4.2.1 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;43-collections&quot;&gt;4.3 Collections&lt;/h2&gt;

&lt;h3 id=&quot;431-csdn深度学习代码专栏&quot;&gt;4.3.1 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;432-chiristopher-olah的博客&quot;&gt;4.3.2 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;433-激活函数系列&quot;&gt;4.3.3 激活函数系列&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;434-梯度下降算法系列&quot;&gt;4.3.4 梯度下降算法系列&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://hxhlwf.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-统计语言模型&quot;&gt;1. 统计语言模型&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#n-gram模型&quot;&gt;N-gram模型&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#神经网络语言模型nnlm&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;/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;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;highlighter-rouge&quot;&gt;\(P(w_1,...,w_T)\)&lt;/code&gt;建模。语言模型的目标是，希望模型对有意义的句子赋予大概率，对没意义的句子赋予小概率。 常用条件概率表示语言模型：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(f(w_t, w_{t-1}, ..., w_{t-n+1})\)&lt;/code&gt;表示根据历史n-1个词得到当前词&lt;code class=&quot;highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;的条件概率，&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(w_{t-n+1},...w_{t-1}\)&lt;/code&gt;，输出句子第t个词为字典中&lt;code class=&quot;highlighter-rouge&quot;&gt;\(|V|\)&lt;/code&gt;个词的概率。每个输入词&lt;code class=&quot;highlighter-rouge&quot;&gt;\(w_{t-n+1},...w_{t-1}\)&lt;/code&gt;通过矩阵&lt;code class=&quot;highlighter-rouge&quot;&gt;\(C\)&lt;/code&gt;映射到词向量&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(g\)&lt;/code&gt;表示未经归一化的所有输出单词概率，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(g_i\)&lt;/code&gt;表示未经归一化的字典中第i个单词的输出概率。其他参数为词向量层到隐层连接的参数。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;根据softmax的定义，通过归一化&lt;code class=&quot;highlighter-rouge&quot;&gt;\(g_i\)&lt;/code&gt;, 生成目标词&lt;code class=&quot;highlighter-rouge&quot;&gt;\(w_t\)&lt;/code&gt;的概率为：&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(y_k^i\)&lt;/code&gt;表示第i个样本第k类的真实标签(0或1)，&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(x_t\)&lt;/code&gt;为第t个词的词向量，分类分数（score）向量 &lt;code class=&quot;highlighter-rouge&quot;&gt;\(z=U*context\)&lt;/code&gt;，最终的分类y采用softmax，损失函数采用多类分类交叉熵。&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;/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;
</content>
 </entry>
 
 <entry>
   <title>命名实体识别</title>
   <link href="http://hxhlwf.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;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://hxhlwf.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;highlighter-rouge&quot;&gt;\(C_{n}\)&lt;/code&gt;)，即，如果句子中存在这样&lt;code class=&quot;highlighter-rouge&quot;&gt;\(n\)&lt;/code&gt;个介词短语，那么：
&lt;code class=&quot;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://hxhlwf.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;#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;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;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;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;highlighter-rouge&quot;&gt;\(\theta^Tx\)&lt;/code&gt;是负无穷到正无穷，sigmoid之后的值相当于取y=1时的概率值，大于0.5就视为1的类。如果&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\theta^Tx&amp;gt;0\)&lt;/code&gt;则&lt;code class=&quot;highlighter-rouge&quot;&gt;\(sigmoid(\theta^Tx)&amp;gt;0.5\)&lt;/code&gt;，换言之，要学到参数&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\theta\)&lt;/code&gt;，使正例的的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\theta^Tx\)&lt;/code&gt;远大于0，负例的&lt;code class=&quot;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;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;highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;函数间隔：&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\((\omega,b)\)&lt;/code&gt;关于数据集T上的函数间隔：(w,b)关于T中所以样本点的函数间隔的最小值：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;投影到超平面的点为&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;,因为&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\omega\)&lt;/code&gt;是垂直于超平面的法向量，设&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x\)&lt;/code&gt;到超平面的距离是&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;,那么&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;\[
x=x_0+\gamma \frac{\omega}{||\omega||}
\]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;因为&lt;code class=&quot;highlighter-rouge&quot;&gt;\(x_0\)&lt;/code&gt;是超平面上的点，所以&lt;code class=&quot;highlighter-rouge&quot;&gt;\(f(x_0)=0\)&lt;/code&gt;，所以
&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\gamma\)&lt;/code&gt;也是有符号的，而间隔只要他的绝对值，所以，几何间隔定义为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;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;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;highlighter-rouge&quot;&gt;\(\hat{\gamma}=\tilde{\gamma}||\omega||\)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;由于，即使超平面固定，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;也会随着&lt;code class=&quot;highlighter-rouge&quot;&gt;\(||\omega||\)&lt;/code&gt;的变化而变化，而我们的目标是固定超平面，所以可以固定&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;也可以固定&lt;code class=&quot;highlighter-rouge&quot;&gt;\(||\omega||\)&lt;/code&gt;。为了方便推导，这里选择固定&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\hat{\gamma}\)&lt;/code&gt;。令&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\hat{\gamma}=1\)&lt;/code&gt;，则&lt;code class=&quot;highlighter-rouge&quot;&gt;\(\tilde{\gamma}=\frac{1}{||\omega||}\)&lt;/code&gt;，所以，优化目标转化为：&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;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;highlighter-rouge&quot;&gt;\(\tilde{\gamma}\)&lt;/code&gt;，当这个分类间隔最大的时候，这两个超平面上的点都叫做support vector,满足&lt;code class=&quot;highlighter-rouge&quot;&gt;\(y(\omega^Tx+b)=1\)&lt;/code&gt;，而非support vector的点，都有&lt;code class=&quot;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://hxhlwf.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;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#l2范数&quot;&gt;l2范数&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;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;p&gt;l1(也叫Lasso regularization)可以使模型变得更sparse，因为它是L0范数的最优凸近似，任何的规则化算子，如果他在&lt;code class=&quot;highlighter-rouge&quot;&gt;\(W_i=0\)&lt;/code&gt;的地方不可微，并且可以分解为一个“求和”的形式，那么这个规则化算子就可以实现稀疏。这说是这么说，W的L1范数是绝对值，&lt;code class=&quot;highlighter-rouge&quot;&gt;\(|w|\)&lt;/code&gt;在w=0处是不可微&lt;/p&gt;

&lt;p&gt;稀疏的两个好处：&lt;/p&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;highlighter-rouge&quot;&gt;\(y=w1*x1+w2*x2+…+w1000*x1000+b\)&lt;/code&gt;（当然了，为了让y限定在[0,1]的范围，一般还得加个Logistic函数）。通过学习，如果最后学习到的&lt;code class=&quot;highlighter-rouge&quot;&gt;\(w^*\)&lt;/code&gt;就只有很少的非零元素，例如只有5个非零的wi，那么我们就有理由相信，这些对应的特征在患病分析上面提供的信息是巨大的，决策性的。也就是说，患不患这种病只和这5个因素有关，那医生就好分析多了。但如果1000个wi都非0，医生面对这1000种因素，累觉不爱。&lt;/p&gt;

&lt;h2 id=&quot;l2范数&quot;&gt;l2范数&lt;/h2&gt;

&lt;p&gt;用上l2的回归也叫“岭回归”（Ridge Regression），能够防止过拟合。让L2范数的规则项&lt;code class=&quot;highlighter-rouge&quot;&gt;\(||W||_2\)&lt;/code&gt;最小，可以使得W的每个元素都很小，都接近于0，但与L1范数不同，它不会让它等于0，而是接近于0。越小的参数说明模型越简单，越简单的模型则越不容易产生过拟合现象。我也不懂，我的理解是：限制了参数很小，实际上就限制了多项式某些分量的影响很小（看上面线性回归的模型的那个拟合的图），这样就相当于减少参数个数。&lt;/p&gt;

&lt;p&gt;l2的好处：&lt;/p&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;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;

</content>
 </entry>
 
 <entry>
   <title>pearson相关系数</title>
   <link href="http://hxhlwf.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;c&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;c&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;c&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;c&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;c&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://hxhlwf.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;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;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;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;highlighter-rouge&quot;&gt;- [ ] haha&lt;/code&gt;
 &lt;code class=&quot;highlighter-rouge&quot;&gt;- [x] lalala&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>paddle源码</title>
   <link href="http://hxhlwf.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>scrapy安装</title>
   <link href="http://hxhlwf.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;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;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>docker常用命令</title>
   <link href="http://hxhlwf.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;#install-docker-enter&quot;&gt;install docker-enter&lt;/a&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-enter&quot;&gt;docker-enter&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;#docker-exec&quot;&gt;docker exec&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;install-docker-enter&quot;&gt;install docker-enter&lt;/h2&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;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://github.com/yeasy/docker_practice/raw/master/_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;h2 id=&quot;docker-run&quot;&gt;docker run&lt;/h2&gt;

&lt;div class=&quot;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;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
registry.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
registry.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;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;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-enter&quot;&gt;docker-enter&lt;/h2&gt;

&lt;div class=&quot;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-pull&quot;&gt;docker pull&lt;/h2&gt;

&lt;div class=&quot;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;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 registry.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;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker push registry.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;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;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;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;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;docker-exec&quot;&gt;docker exec&lt;/h2&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;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat /home/disk0/daiwk_img_data/demo_scrapy/lp_mining/lp_spider/containerid | xargs -i docker exec -dt {} /bin/bash /home/data/demo_scrap
y/lp_mining/lp_spider/run_all_images.sh 

cat /home/disk0/daiwk_img_data/demo_scrapy/lp_mining/lp_spider/containerid | xargs -i docker exec {} tail /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;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;
</content>
 </entry>
 
 <entry>
   <title>map/ndcg</title>
   <link href="http://hxhlwf.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;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;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;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;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;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;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;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://hxhlwf.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简介&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;/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的常见网络结构&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的lambdarank&quot;&gt;listwise的lambdarank&lt;/a&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;a href=&quot;https://github.com/PaddlePaddle/models/tree/develop/ltr&quot;&gt;https://github.com/PaddlePaddle/models/tree/develop/ltr&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;p&gt;ranknet&lt;/p&gt;

&lt;h3 id=&quot;list-wise&quot;&gt;list-wise&lt;/h3&gt;

&lt;p&gt;lambdarank&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;c&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;c&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;c&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;c&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;
</content>
 </entry>
 
 <entry>
   <title>url</title>
   <link href="http://hxhlwf.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;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>单元测试工具</title>
   <link href="http://hxhlwf.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;/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;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;

</content>
 </entry>
 
 <entry>
   <title>机器学习中的矩阵、向量求导</title>
   <link href="http://hxhlwf.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;!-- /TOC --&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;
</content>
 </entry>
 
 <entry>
   <title>git常用操作</title>
   <link href="http://hxhlwf.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;/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;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
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;div class=&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;强行推送到远端&lt;/p&gt;

&lt;div class=&quot;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;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;
</content>
 </entry>
 
 <entry>
   <title>dl tips</title>
   <link href="http://hxhlwf.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;#i-数据集问题&quot;&gt;I. 数据集问题&lt;/a&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-确保输入与输出相关联&quot;&gt;4. 确保输入与输出相关联&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#5-输入与输出之间的关系是否太随机&quot;&gt;5. 输入与输出之间的关系是否太随机？&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#6-数据集中是否有太多的噪音&quot;&gt;6. 数据集中是否有太多的噪音？&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#7-shuffle-数据集&quot;&gt;7. Shuffle 数据集&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#8-减少类别失衡&quot;&gt;8. 减少类别失衡&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#9-你有足够的训练实例吗&quot;&gt;9. 你有足够的训练实例吗？&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#10-确保你采用的批量数据不是单一标签&quot;&gt;10. 确保你采用的批量数据不是单一标签&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#11-缩减批量大小&quot;&gt;11. 缩减批量大小&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;#12-归一化特征&quot;&gt;12. 归一化特征&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#13-你是否应用了过量的数据增强&quot;&gt;13. 你是否应用了过量的数据增强？&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#14-检查你的预训练模型的预处理过程&quot;&gt;14. 检查你的预训练模型的预处理过程&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#15-检查训练验证测试集的预处理&quot;&gt;15. 检查训练、验证、测试集的预处理&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;#16-试着解决某一问题的更简易的版本&quot;&gt;16. 试着解决某一问题的更简易的版本。&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#17碰巧寻找正确的损失&quot;&gt;17.「碰巧」寻找正确的损失&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#18-检查你的损失函数&quot;&gt;18. 检查你的损失函数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#19-核实损失输入&quot;&gt;19. 核实损失输入&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#20-调整损失权重&quot;&gt;20. 调整损失权重&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#21-监控其它指标&quot;&gt;21. 监控其它指标&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#22-测试任意的自定义层&quot;&gt;22. 测试任意的自定义层&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#23-检查冷冻层或变量&quot;&gt;23. 检查「冷冻」层或变量&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#24-扩大网络规模&quot;&gt;24. 扩大网络规模&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#25-检查隐维度误差&quot;&gt;25. 检查隐维度误差&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#26-探索梯度检查gradient-checking&quot;&gt;26. 探索梯度检查（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;#27-一个真正小的数据集&quot;&gt;27. 一个真正小的数据集&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#28-检查权重初始化&quot;&gt;28. 检查权重初始化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#29-改变你的超参数&quot;&gt;29. 改变你的超参数&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#30-减少正则化&quot;&gt;30. 减少正则化&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#31-给它一些时间&quot;&gt;31. 给它一些时间&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-从训练模式转换为测试模式&quot;&gt;32. 从训练模式转换为测试模式&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#33-可视化训练&quot;&gt;33. 可视化训练&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#34-尝试不同的优化器&quot;&gt;34. 尝试不同的优化器&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#35-梯度爆炸梯度消失&quot;&gt;35. 梯度爆炸、梯度消失&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#36-增加减少学习速率&quot;&gt;36. 增加、减少学习速率&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#37-克服-nans&quot;&gt;37. 克服 NaNs&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=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;h2 id=&quot;i-数据集问题&quot;&gt;I. 数据集问题&lt;/h2&gt;

&lt;h3 id=&quot;1-检查你的输入数据&quot;&gt;1. 检查你的输入数据&lt;/h3&gt;

&lt;p&gt;检查馈送到网络的输入数据是否正确。例如，我不止一次混淆了图像的宽度和高度。有时，我错误地令输入数据全部为零，或者一遍遍地使用同一批数据执行梯度下降。因此打印／显示若干批量的输入和目标输出，并确保它们正确。&lt;/p&gt;

&lt;h3 id=&quot;2-尝试随机输入&quot;&gt;2. 尝试随机输入&lt;/h3&gt;

&lt;p&gt;尝试传递随机数而不是真实数据，看看错误的产生方式是否相同。如果是，说明在某些时候你的网络把数据转化为了垃圾。试着逐层调试，并查看出错的地方。&lt;/p&gt;

&lt;h3 id=&quot;3-检查数据加载器&quot;&gt;3. 检查数据加载器&lt;/h3&gt;

&lt;p&gt;你的数据也许很好，但是读取输入数据到网络的代码可能有问题，所以我们应该在所有操作之前打印第一层的输入并进行检查。&lt;/p&gt;

&lt;h3 id=&quot;4-确保输入与输出相关联&quot;&gt;4. 确保输入与输出相关联&lt;/h3&gt;

&lt;p&gt;检查少许输入样本是否有正确的标签，同样也确保 shuffling 输入样本同样对输出标签有效。&lt;/p&gt;

&lt;h3 id=&quot;5-输入与输出之间的关系是否太随机&quot;&gt;5. 输入与输出之间的关系是否太随机？&lt;/h3&gt;

&lt;p&gt;相较于随机的部分（可以认为股票价格也是这种情况），输入与输出之间的非随机部分也许太小，即输入与输出的关联度太低。没有一个统一的方法来检测它，因为这要看数据的性质。&lt;/p&gt;

&lt;h3 id=&quot;6-数据集中是否有太多的噪音&quot;&gt;6. 数据集中是否有太多的噪音？&lt;/h3&gt;

&lt;p&gt;我曾经遇到过这种情况，当我从一个食品网站抓取一个图像数据集时，错误标签太多以至于网络无法学习。手动检查一些输入样本并查看标签是否大致正确。&lt;/p&gt;

&lt;h3 id=&quot;7-shuffle-数据集&quot;&gt;7. Shuffle 数据集&lt;/h3&gt;

&lt;p&gt;如果你的数据集没有被 shuffle，并且有特定的序列（按标签排序），这可能给学习带来不利影响。你可以 shuffle 数据集来避免它，并确保输入和标签都被重新排列。&lt;/p&gt;

&lt;h3 id=&quot;8-减少类别失衡&quot;&gt;8. 减少类别失衡&lt;/h3&gt;

&lt;p&gt;一张类别 B 图像和 1000 张类别 A 图像？如果是这种情况，那么你也许需要平衡你的损失函数或者尝试其他解决类别失衡的方法。&lt;/p&gt;

&lt;h3 id=&quot;9-你有足够的训练实例吗&quot;&gt;9. 你有足够的训练实例吗？&lt;/h3&gt;

&lt;p&gt;如果你在从头开始训练一个网络（即不是调试），你很可能需要大量数据。对于图像分类，每个类别你需要 1000 张图像甚至更多。&lt;/p&gt;

&lt;h3 id=&quot;10-确保你采用的批量数据不是单一标签&quot;&gt;10. 确保你采用的批量数据不是单一标签&lt;/h3&gt;

&lt;p&gt;这可能发生在排序数据集中（即前 10000 个样本属于同一个分类）。可通过 shuffle 数据集轻松修复。&lt;/p&gt;

&lt;h3 id=&quot;11-缩减批量大小&quot;&gt;11. 缩减批量大小&lt;/h3&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;h2 id=&quot;ii-数据归一化增强&quot;&gt;II. 数据归一化/增强&lt;/h2&gt;

&lt;h3 id=&quot;12-归一化特征&quot;&gt;12. 归一化特征&lt;/h3&gt;

&lt;p&gt;你的输入已经归一化到零均值和单位方差了吗？&lt;/p&gt;

&lt;h3 id=&quot;13-你是否应用了过量的数据增强&quot;&gt;13. 你是否应用了过量的数据增强？&lt;/h3&gt;

&lt;p&gt;数据增强有正则化效果（regularizing effect）。过量的数据增强，加上其它形式的正则化（权重 L2，中途退出效应等）可能会导致网络欠拟合（underfit）。&lt;/p&gt;

&lt;h3 id=&quot;14-检查你的预训练模型的预处理过程&quot;&gt;14. 检查你的预训练模型的预处理过程&lt;/h3&gt;

&lt;p&gt;如果你正在使用一个已经预训练过的模型，确保你现在正在使用的归一化和预处理与之前训练模型时的情况相同。例如，一个图像像素应该在 [0, 1]，[-1, 1] 或 [0, 255] 的范围内吗？&lt;/p&gt;

&lt;h3 id=&quot;15-检查训练验证测试集的预处理&quot;&gt;15. 检查训练、验证、测试集的预处理&lt;/h3&gt;

&lt;p&gt;CS231n 指出了一个常见的陷阱：「任何预处理数据（例如数据均值）必须只在训练数据上进行计算，然后再应用到验证、测试数据中。例如计算均值，然后在整个数据集的每个图像中都减去它，再把数据分发进训练、验证、测试集中，这是一个典型的错误。」此外，要在每一个样本或批量（batch）中检查不同的预处理。&lt;/p&gt;

&lt;h2 id=&quot;iii-实现的问题&quot;&gt;III. 实现的问题&lt;/h2&gt;

&lt;h3 id=&quot;16-试着解决某一问题的更简易的版本&quot;&gt;16. 试着解决某一问题的更简易的版本。&lt;/h3&gt;

&lt;p&gt;这将会有助于找到问题的根源究竟在哪里。例如，如果目标输出是一个物体类别和坐标，那就试着把预测结果仅限制在物体类别当中（尝试去掉坐标）。&lt;/p&gt;

&lt;h3 id=&quot;17碰巧寻找正确的损失&quot;&gt;17.「碰巧」寻找正确的损失&lt;/h3&gt;

&lt;p&gt;还是来源于 CS231n 的技巧：用小参数进行初始化，不使用正则化。例如，如果我们有 10 个类别，「碰巧」就意味着我们将会在 10% 的时间里得到正确类别，Softmax 损失是正确类别的负 log 概率： -ln(0.1) = 2.302。然后，试着增加正则化的强度，这样应该会增加损失。&lt;/p&gt;

&lt;h3 id=&quot;18-检查你的损失函数&quot;&gt;18. 检查你的损失函数&lt;/h3&gt;

&lt;p&gt;如果你执行的是你自己的损失函数，那么就要检查错误，并且添加单元测试。通常情况下，损失可能会有些不正确，并且损害网络的性能表现。&lt;/p&gt;

&lt;h3 id=&quot;19-核实损失输入&quot;&gt;19. 核实损失输入&lt;/h3&gt;

&lt;p&gt;如果你正在使用的是框架提供的损失函数，那么要确保你传递给它的东西是它所期望的。例如，在 PyTorch 中，我会混淆 NLLLoss 和 CrossEntropyLoss，因为一个需要 softmax 输入，而另一个不需要。&lt;/p&gt;

&lt;h3 id=&quot;20-调整损失权重&quot;&gt;20. 调整损失权重&lt;/h3&gt;

&lt;p&gt;如果你的损失由几个更小的损失函数组成，那么确保它们每一个的相应幅值都是正确的。这可能会涉及到测试损失权重的不同组合。&lt;/p&gt;

&lt;h3 id=&quot;21-监控其它指标&quot;&gt;21. 监控其它指标&lt;/h3&gt;

&lt;p&gt;有时损失并不是衡量你的网络是否被正确训练的最佳预测器。如果可以的话，使用其它指标来帮助你，比如精度。&lt;/p&gt;

&lt;h3 id=&quot;22-测试任意的自定义层&quot;&gt;22. 测试任意的自定义层&lt;/h3&gt;

&lt;p&gt;你自己在网络中实现过任意层吗？检查并且复核以确保它们的运行符合预期。&lt;/p&gt;

&lt;h3 id=&quot;23-检查冷冻层或变量&quot;&gt;23. 检查「冷冻」层或变量&lt;/h3&gt;

&lt;p&gt;检查你是否无意中阻止了一些层或变量的梯度更新，这些层或变量本来应该是可学的。&lt;/p&gt;

&lt;h3 id=&quot;24-扩大网络规模&quot;&gt;24. 扩大网络规模&lt;/h3&gt;

&lt;p&gt;可能你的网络的表现力不足以采集目标函数。试着加入更多的层，或在全连层中增加更多的隐藏单元。&lt;/p&gt;

&lt;h3 id=&quot;25-检查隐维度误差&quot;&gt;25. 检查隐维度误差&lt;/h3&gt;

&lt;p&gt;如果你的输入看上去像（k,H,W）= (64, 64, 64)，那么很容易错过与错误维度相关的误差。给输入维度使用一些「奇怪」的数值（例如，每一个维度使用不同的质数），并且检查它们是如何通过网络传播的。&lt;/p&gt;

&lt;h3 id=&quot;26-探索梯度检查gradient-checking&quot;&gt;26. 探索梯度检查（Gradient checking）&lt;/h3&gt;

&lt;p&gt;如果你手动实现梯度下降，梯度检查会确保你的反向传播（backpropagation）能像预期中一样工作。&lt;/p&gt;

&lt;h2 id=&quot;iv-训练问题&quot;&gt;IV. 训练问题&lt;/h2&gt;

&lt;h3 id=&quot;27-一个真正小的数据集&quot;&gt;27. 一个真正小的数据集&lt;/h3&gt;

&lt;p&gt;过拟合数据的一个小子集，并确保其工作。例如，仅使用 1 或 2 个实例训练，并查看你的网络是否学习了区分它们。然后再训练每个分类的更多实例。&lt;/p&gt;

&lt;h3 id=&quot;28-检查权重初始化&quot;&gt;28. 检查权重初始化&lt;/h3&gt;

&lt;p&gt;如果不确定，请使用 Xavier 或 He 初始化。同样，初始化也许会给你带来坏的局部最小值，因此尝试不同的初始化，看看是否有效。&lt;/p&gt;

&lt;h3 id=&quot;29-改变你的超参数&quot;&gt;29. 改变你的超参数&lt;/h3&gt;

&lt;p&gt;或许你正在使用一个很糟糕的超参数集。如果可行，尝试一下网格搜索。&lt;/p&gt;

&lt;h3 id=&quot;30-减少正则化&quot;&gt;30. 减少正则化&lt;/h3&gt;

&lt;p&gt;太多的正则化可致使网络严重地欠拟合。减少正则化，比如 dropout、批规范、权重／偏差 L2 正则化等。在优秀课程[《编程人员的深度学习实战》]（http://course.fast.ai）中，Jeremy Howard 建议首先解决欠拟合。这意味着你充分地过拟合数据，并且只有在那时处理过拟合。&lt;/p&gt;

&lt;h3 id=&quot;31-给它一些时间&quot;&gt;31. 给它一些时间&lt;/h3&gt;

&lt;p&gt;也许你的网络需要更多的时间来训练，在它能做出有意义的预测之前。如果你的损失在稳步下降，那就再多训练一会儿。&lt;/p&gt;

&lt;h3 id=&quot;32-从训练模式转换为测试模式&quot;&gt;32. 从训练模式转换为测试模式&lt;/h3&gt;

&lt;p&gt;一些框架的层很像批规范、Dropout，而其他的层在训练和测试时表现并不同。转换到适当的模式有助于网络更好地预测。&lt;/p&gt;

&lt;h3 id=&quot;33-可视化训练&quot;&gt;33. 可视化训练&lt;/h3&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;h3 id=&quot;34-尝试不同的优化器&quot;&gt;34. 尝试不同的优化器&lt;/h3&gt;

&lt;p&gt;优化器的选择不应当妨碍网络的训练，除非你选择了一个特别糟糕的参数。但是，为任务选择一个合适的优化器非常有助于在最短的时间内获得最多的训练。描述你正在使用的算法的论文应当指定优化器；如果没有，我倾向于选择 Adam 或者带有动量的朴素 SGD。&lt;/p&gt;

&lt;h3 id=&quot;35-梯度爆炸梯度消失&quot;&gt;35. 梯度爆炸、梯度消失&lt;/h3&gt;

&lt;p&gt;检查隐蔽层的最新情况，过大的值可能代表梯度爆炸。这时，梯度截断（Gradient clipping）可能会有所帮助。
检查隐蔽层的激活值。Deeplearning4j 中有一个很好的指导方针：「一个好的激活值标准差大约在 0.5 到 2.0 之间。明显超过这一范围可能就代表着激活值消失或爆炸。」&lt;/p&gt;

&lt;h3 id=&quot;36-增加减少学习速率&quot;&gt;36. 增加、减少学习速率&lt;/h3&gt;

&lt;p&gt;低学习速率将会导致你的模型收敛很慢；
高学习速率将会在开始阶段减少你的损失，但是可能会导致你很难找到一个好的解决方案。
试着把你当前的学习速率乘以 0.1 或 10。&lt;/p&gt;

&lt;h3 id=&quot;37-克服-nans&quot;&gt;37. 克服 NaNs&lt;/h3&gt;

&lt;p&gt;据我所知，在训练 RNNs 时得到 NaN（Non-a-Number）是一个很大的问题。一些解决它的方法：&lt;/p&gt;

&lt;p&gt;减小学习速率，尤其是如果你在前 100 次迭代中就得到了 NaNs。
NaNs 的出现可能是由于用零作了除数，或用零或负数作了自然对数。
Russell Stewart 对如何处理 NaNs 很有心得[http://russellsstewart.com/notes/0.html]（http://russellsstewart.com/notes/0.html）。
尝试逐层评估你的网络，这样就会看见 NaNs 到底出现在了哪里。&lt;/p&gt;
</content>
 </entry>
 
 
</feed>