Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Add word2vec #12

Merged
merged 8 commits into from Jan 1, 2017
Merged

Add word2vec #12

merged 8 commits into from Jan 1, 2017

Conversation

Zrachel
Copy link
Collaborator

@Zrachel Zrachel commented Dec 21, 2016

resolve #6

@@ -0,0 +1,90 @@
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们对Python源码文件的命名是有规范的吗?(这个问题好像应该问我自己。。。)

我建议采用Google Python filename convention,都是小写字母,单词之间用下划线连接,比如这里的 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/summary

这个规范和我们的C++ source file naming convention一致。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

# See the License for the specific language governing permissions and
# limitations under the License.

from paddle.trainer_config_helpers import *
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 style guide https://www.python.org/dev/peps/pep-0008/#imports

Wildcard imports ( from import * ) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样写固然很遵守规则,但每次还要check引了trainer_config_helper里的哪些层,会不会太麻烦了?
另外现在的paddlepaddle/paddle里也都是import *, 也要改吗?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我想现有的代码不用修改了。即便修改,也可以日后某个机会集中修改。

咱们自己的代码,怎么都行;至于这个给学生们看的教程,我想里面的源码最好是符合规范的,以免“误人子弟”了。哈哈。

@@ -1 +1,272 @@
TODO: Base on [this tutorial](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/tutorials/embedding_model/index_en.md).
TODO: Base on [this tutorial](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/tutorials/embedding_model/index_en.md).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO 这行是不是可以删掉了?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


# 背景介绍

本章我们主要讲如何训练词语的向量表征,即词语在向量空间模型(vector space models)中的特征表示。首先我们来熟悉词向量和语言模型。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在阅读了 https://en.wikipedia.org/wiki/Word_embedding 之后,我感觉貌似可以写一点儿历史八卦,来引发读者的一点兴趣。请看看这样如何?

本章我们词的向量表征,也称为word embedding。Word embedding是自然语言处理中常见的一个操作,是搜索引擎、广告系统、推荐系统等互联网服务背后常见的基础技术。

在这些互联网服务里,我们经常要比较两个词或者两段文本之间的相关性。为了做这样的比较,我们往往先要把词表示成计算机适合处理的方式。最自然的方式恐怕莫过于 vector space model。在这种方式里,每个词被表示成一个实数向量,其长度为字典大小,其中每个维度对应一个字典里的每个词。任何一个单词对应一个向量,其中绝大多数元素都是0,只有这个词对应的维度上的值是1。这样的向量有个术语名字——one-hot vector。

One-hot vector虽然自然,但是用处有限。比如,在互联网广告系统里,如果用户输入的query是“母亲节”,而有一个广告的关键词是“康乃馨”。按照常理,我们知道这两个词之间是有联系的——母亲节通常应该送给母亲一束康乃馨。但是这两个词对应的one-hot vectors之间的距离度量——无论是欧氏距离还是cosine similarity——都认为这两个词毫无相关性。

这样与我们认识相悖的结论的根本原因是,每个词本身的信息量都很小,所以仅仅给定两个词,不足以让我们准确判别它们是否相关。要想精确计算相关性,我们还需要更多的信息——从大量数据里通过机器学习方法归纳出来的知识!

在机器学习领域里,各种“知识库”被用各种模型(models)表示。其中有一类模型被称为word embedding model —— 它们可能是概率模型、也可能是co-occurrence matrix模型,也可能是神经元网络模型;它们的作用是可以把一个 one-hot vector 理解成一个实数向量(embedding vector),通常更短(维度更低),而且两个语义上(或者用法上)相似的词对应的 embedding vectors 通常“更像”。比如我们希望“母亲节”和“康乃馨”对应的embedding vectors的cosine similarity不再为零了。

Word embedding 的研究从2000年开始。Yoshua Bengio等科学家于2003年发表了著名的论文 Neural Probabilistic Language Models介绍如何学习一个神经元网络表示的embedding model。而近年来最有名的神经元网络 word embedding model 恐怕是 Tomas Mikolov 在Google 研发的 wordvec

在本章里,我们展示神经元 word embedding model 的细节,以及如何用Paddle 训练一个 word embedding model,把语义相近的词表示成距离相近的向量。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我试着写了一下。我的意图是自己也学习一下相关背景,试着把历史因由写出来,调调读者的胃口。也顺势举例,引发问题的定义 —— 希望学习一个模型,能把语义相近的词表示成距离相近的向量。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同意背景介绍放历史因由。那么现在的“词向量、语言模型、词向量与语言模型的关系”显得有点多,可以把这部分放到模型概览里面。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在这些互联网服务里,我们经常要比较两个词或者两段文本之间的相关性。为了做这样的比较,我们往往先要把词表示成计算机适合处理的方式。最自然的方式恐怕莫过于 vector space model。在这种方式里,每个词被表示成一个实数向量,其长度为字典大小,其中每个维度对应一个字典里的每个词。任何一个单词对应一个向量,其中绝大多数元素都是0,只有这个词对应的维度上的值是1。这样的向量有个术语名字——one-hot vector。

这段的位置建议下移,不如下一段引人入胜,比较抽象。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 已添加内容,修改说法。


#训练模型

1. 首先将$w_t$之前的$n-1$个词 $w_{t-n+1},...w_{t-1}$通过|V|*D的矩阵映射到D维词向量(本例config中取D=32),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$|V|*D$ 或者 |V|*D,否则 * 被理解成Markdown的“着重号”,导致后面的文字是斜体显示。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


# 背景介绍

本章我们主要讲如何训练词语的向量表征,即词语在向量空间模型(vector space models)中的特征表示。首先我们来熟悉词向量和语言模型。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同意背景介绍放历史因由。那么现在的“词向量、语言模型、词向量与语言模型的关系”显得有点多,可以把这部分放到模型概览里面。

以上结果可以通过运行`caldis.py`, 加载字典里的单词和对应训练特征结果得到,我们将在[应用模型](#应用模型)中详细描述用法。

#模型概览
在这里我们介绍4个训练词向量的模型。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里主要介绍了n-gram模型,因此可以首先介绍n-gram。把剩下的三个模型放到n-gram介绍后再简略介绍。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,74 @@
#!/bin/env python
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyright统一用PaddlePaddle

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,4 @@
wget http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在第一行放#!/bin/bash

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Zrachel Zrachel requested a review from llxxxll December 21, 2016 05:25

# 背景介绍

本章我们主要讲如何训练词语的向量表征,即词语在向量空间模型(vector space models)中的特征表示。首先我们来熟悉词向量和语言模型。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“向量空间模型(vector space models)”一般是指早期传统的基于one-hot或tf-idf的文档或者词的表示方法吧?@wangkuiyi 的表达是对的。可以更书面的写一下word embedding出现的历史原因,其对于vector space models的改进和优势。word embedding成为了dl in nlp的基石。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Zrachel Zrachel force-pushed the add_w2v branch 2 times, most recently from 0925e8b to cc70edd Compare December 21, 2016 06:28
Copy link
Member

@llxxxll llxxxll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第一张图 ,一些单词有重叠 ,可以精简或者重新对单词排版.

@Zrachel Zrachel force-pushed the add_w2v branch 17 times, most recently from 39843ac to df878e7 Compare December 23, 2016 08:19
PS:由于下面介绍的也是神经网络语言模型,我们在这里不用其NNLM的本名,考虑到其具体做法,本文中称该模型为N-gram neural model。


n-gram模型是统计语言模型中的一种重要方法,文中 \[[1](#参考文献)\] 提出,可以通过学习大量语料得到词语的向量表达,通过这些向量得到整个句子的概率。用这种方法学习语言模型可以克服维度诅咒(curse of dimensionality),即训练和测试数据不同导致的模型不准。在上文中我们已经讲到语言模型的目标是对$P(w_1, ..., w_T)$建模, 如果假设文本中每个词都是相互独立的,则句话的联合概率可以表示为其中所有词语条件概率的乘积,即
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

维度灾难(curse of dimensionality)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@Zrachel
Copy link
Collaborator Author

Zrachel commented Dec 30, 2016

昨天晚上最近的这次review,除了部分单独回复的comments外,其他all Done. @luotao1

@Zrachel Zrachel force-pushed the add_w2v branch 3 times, most recently from 70a3e3f to 17a1dc0 Compare December 30, 2016 03:17

在机器学习领域里,各种“知识”被各种模型表示,词向量模型(word embedding model)就是其中的一类。通过词向量模型可将一个 one-hot vector映射到一个维度更低的实数向量(embedding vector),如$embedding(母亲节) = [0.3, 4.2, -1.5, ...], embedding(康乃馨) = [0.2, 5.6, -2.3, ...]$。在这个映射到的实数向量表示中,希望两个语义(或用法)上相似的词对应的词向量“更像”,这样如“母亲节”和“康乃馨”的对应词向量的余弦相似度就不再为零了。

词向量模型可以是概率模型、co-occurrence matrix模型或神经元网络模型。在用神经网络求词向量之前,传统做法是统计一个word co-occurrence矩阵$X$。$X$是一个`|V|*|V|`大小的矩阵,$X_{ij}$表示在所有语料中,词汇表(vocabulary)中第i个词和第j个词同时出现的词数,`|V|`为词汇表的大小。对$X$做矩阵分解(如Singular Value Decomposition),即
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

co-occurrence matrix都改成:共生矩阵(co-occurrence matrix),

对$X$做矩阵分解(如奇异值分解,Singular Value Decomposition),得到的$U$即视为所有词的词向量:


$$X = USV^T$$

其中得到的$U$即视为所有词的word embedding. 这样做有很多问题:<br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但传统做法存在很多问题:

$$X = USV^T$$

其中得到的$U$即视为所有词的word embedding. 这样做有很多问题:<br/>
1) 很多词没有出现,导致矩阵极其稀疏,也需要对词频做额外tricks来达到好的SVD效果;<br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于很多词没有出现,导致矩阵极其稀疏,因此需要对词频做额外的工作,来达到好的矩阵分解效果。

其中得到的$U$即视为所有词的word embedding. 这样做有很多问题:<br/>
1) 很多词没有出现,导致矩阵极其稀疏,也需要对词频做额外tricks来达到好的SVD效果;<br/>
2) 矩阵非常大,维度太高(通常达到$10^6*10^6$的数量级);<br/>
3) 需要手动去掉停用词(如although, a,...),不然这些频繁出现的词会影响SVD效果
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

也会影响矩阵分解的效果。

3) 需要手动去掉停用词(如although, a,...),不然这些频繁出现的词会影响SVD效果


而基于神经网络的模型就可以很好的解决以上问题,而不需要计算存储一个在全语料上统计的大表。在本章里,我们将展示基于神经网络训练词向量的细节,以及如何用PaddlePaddle训练一个词向量模型。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基于神经网络的模型不需要计算存储一个在全语料上统计的大表,因此能很好地解决以上问题。

```
-i INPUT: 输入的(二进制)词向量模型名称
-o OUTPUT: 输出的文本模型名称
-d DIM: (词向量)参数维度
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其中,INPUT是输入的(二进制)词向量模型名称,XXX

其中,第一行是PaddlePaddle 输出文件的格式说明,包含3个属性:
1. PaddlePaddle的版本号,本例中为0
2. 浮点数占用的字节数,本例中为4
3. 总计的参数个数, 本例中为62496(即1953*32)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 把381的“第一行为文件信息,之后每一行都按顺序表示字典里一个词的特征,用逗号分隔”和390这里放一起,重新组织下吧
  2. 390-393这里显示的全连起来了。把123,变成1)2)3),每行后面加分号或句号。

```bash
python format_convert.py --b2t -i model/pass-00029/_proj -o model/pass-00029/_proj.txt -d 32
```
转换后得到的文本文件中,第一行为文件信息,之后每一行都按顺序表示字典里一个词的特征,用逗号分隔, 如:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

转换后得到的文本文件如下:


### 修改词向量

我们可以对词向量进行修改,并转换成paddle参数二进制格式,方法:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同样,我们也可以对XXX,PaddlePaddle,方法如下:

-i INPUT: 输入的文本词向量模型名称
-o OUTPUT: 输出的二进制词向量模型名称

输入的文本格式如下:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

输入的文本格式如下(注意,不包含上面二进制转文本后第一行的格式说明):

413行可以删了

@Zrachel
Copy link
Collaborator Author

Zrachel commented Dec 30, 2016

All Done for the last review. @luotao1

@@ -0,0 +1,10 @@
#!/bin/bash

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set -e

@@ -0,0 +1,6 @@
#!/bin/bash

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set -e

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add .gitignore for getdata.sh generated files.

image


$$X = USV^T$$

其中得到的$U$即视为所有词的word embedding. 这样做有很多问题:<br/>
其中得到的$U$即视为所有词的词向量。但这样的传统做法有很多问题:<br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“其中得到的$U$即视为所有词的词向量”可以去掉,因为上面已经讲过了。

@@ -11,22 +11,22 @@ One-hot vector虽然自然,但是用处有限。比如,在互联网广告系

在机器学习领域里,各种“知识”被各种模型表示,词向量模型(word embedding model)就是其中的一类。通过词向量模型可将一个 one-hot vector映射到一个维度更低的实数向量(embedding vector),如$embedding(母亲节) = [0.3, 4.2, -1.5, ...], embedding(康乃馨) = [0.2, 5.6, -2.3, ...]$。在这个映射到的实数向量表示中,希望两个语义(或用法)上相似的词对应的词向量“更像”,这样如“母亲节”和“康乃馨”的对应词向量的余弦相似度就不再为零了。

词向量模型可以是概率模型、共生矩阵(co-occurrence matrix)模型或神经元网络模型。在用神经网络求词向量之前,传统做法是统计一个word co-occurrence矩阵$X$。$X$是一个`|V|*|V|`大小的矩阵,$X_{ij}$表示在所有语料中,词汇表(vocabulary)中第i个词和第j个词同时出现的词数,`|V|`为词汇表的大小。对$X$做矩阵分解(如Singular Value Decomposition),即
词向量模型可以是概率模型、共生矩阵(co-occurrence matrix)模型或神经元网络模型。在用神经网络求词向量之前,传统做法是统计一个词语的共生矩阵$X$。$X$是一个`|V|*|V|`大小的矩阵,$X_{ij}$表示在所有语料中,词汇表`V`(vocabulary)中第i个词和第j个词同时出现的词数,`|V|`为词汇表的大小。对$X$做矩阵分解(如奇异值分解,Singular Value Decomposition \[[5](#参考文献)\]),得到的$U$即视为所有词的词向量:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$|V| \times |V|$,$|V|$


$$X = USV^T$$

其中得到的$U$即视为所有词的word embedding. 这样做有很多问题:<br/>
其中得到的$U$即视为所有词的词向量。但这样的传统做法有很多问题:<br/>
1) 很多词没有出现,导致矩阵极其稀疏,也需要对词频做额外tricks来达到好的SVD效果;<br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于很多词没有出现,导致矩阵极其稀疏,因此需要对词频做额外工作来达到好的矩阵分解效果;

1) 很多词没有出现,导致矩阵极其稀疏,也需要对词频做额外tricks来达到好的SVD效果;<br/>
2) 矩阵非常大,维度太高(通常达到$10^6*10^6$的数量级);<br/>
3) 需要手动去掉停用词(如although, a,...),不然这些频繁出现的词会影响SVD效果
3) 需要手动去掉停用词(如although, a,...),也会影响矩阵分解的效果。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“不然这些频繁出现的词”可以保留吧,改为:不然这些频繁出现的词也会影响矩阵分解的效果。


### Continuous Bag-of-Words model(CBOW)

近年来最有名的神经元网络 word embedding model 恐怕是 Tomas Mikolov 在Google 研发的 wordvec\[[4](#参考文献)\]。其中介绍了两个模型,Continuous Bag-of-Words model和Skip-Gram model,这两个网络很浅很简单,但训练效果非常好。和N-gram neural model 类似,这两个模型同样利用了上下文信息。 CBOW模型通过一个词的上下文(各N个词)预测当前词,而Skip-gram模型用一个词预测其上下文。
近年来最有名的神经元网络 word embedding model 恐怕是 Tomas Mikolov 在Google 研发的 wordvec\[[3](#参考文献)\]。其中介绍了两个模型,Continuous Bag-of-Words model和Skip-Gram model,这两个网络很浅很简单,但训练效果非常好。和N-gram neural model 类似,这两个模型同样利用了上下文信息。 CBOW模型通过一个词的上下文(各N个词)预测当前词。当N=2时,模型如下图所示:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

103行只需要:“CBOW模型通过一个词的上下文(各N个词)预测当前词。当N=2时,模型如下图所示:”。前面几句话在“模型概览”的最开始已经讲过了。

其中,第一行是PaddlePaddle 输出文件的格式说明,包含3个属性:<br/>
1). PaddlePaddle的版本号,本例中为0;<br/>
2). 浮点数占用的字节数,本例中为4;<br/>
3). 总计的参数个数, 本例中为62496(即1953*32);<br/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1)后面的.去掉。同2)3)

@@ -300,7 +305,7 @@ settings(
learning_rate = 1))
```

5. 最后,将文本隐层特征层,再经过一个全连接,映射成一个`|V|`维向量,同时通过softmax归一化得到这`|V|`个词的生成概率。
5. 最后,将文本隐层特征,再经过一个全连接,映射成一个`|V|`维向量,同时通过softmax归一化得到这`|V|`个词的生成概率。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$|V|$

整个网络的cost为多类分类交叉熵,用公式表示为
其中$g_i$为预测当前词的层中第$i$个输出词节点的值。具体来说,$g_i = \theta_i^Tx + b$, $x$为隐层特征(一个线性映射`embedding`通过全连接`fully connect`投影到的非线性隐层), $\theta$和$b$为隐层特征层到词预测层的全连接参数。

整个网络的损失值(cost)为多类分类交叉熵,用公式表示为

$$J(\theta) = -\sum_{i=1}^N\sum_{c=1}^{|V|}y_k^{i}log(softmax(g_k^i))$$

其中$y_k^i$表示第i个样本第k类的真实label(0或1),$softmax(g_k^i)$表示第i个样本第k类softmax输出的概率。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 第$i$个样本第$k$类
  2. 真实标签(0或1)


$$P(w_1, ..., w_T) = \prod_{t=1}^TP(w_t)$$

然而我们知道语句中的每个词出现的概率都与其前面的词紧密相关, 即
然而我们知道语句中的每个词出现的概率都与其前面的词紧密相关, 即

$$P(w_1, ..., w_T) = \prod_{t=1}^TP(w_t | w_1, ... , w_{t-1})$$
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

64行和68行的公式移到语言模型中


$$\frac{1}{T}\sum_t f(w_t, w_{t-1}, ..., w_{t-n+1};\theta) + R(\theta)$$

其中$f(w_t, w_{t-1}, ..., w_{t-n+1})$, 表示根据历史n-1个词得到当前词$w_t$的条件概率,$R(\theta)$表示参数正则项。

其中$f(w_t, w_{t-1}, ..., w_{t-n+1})$表示根据历史n-1个词得到当前词$w_t$的条件概率,$R(\theta)$表示参数正则项。
函数$f$的网络示意图如下:
<p align="center">
<img src="image/ngram.png"><br/>
图3. n-gram神经网络模型
</p>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

换一副图,下面的公式和上面的图可以对应说明

@Zrachel Zrachel force-pushed the add_w2v branch 3 times, most recently from 119204d to a11e387 Compare December 30, 2016 08:15
@Zrachel
Copy link
Collaborator Author

Zrachel commented Dec 30, 2016

All done recent @reyoung and @luotao1 's review comment.

@@ -0,0 +1,3 @@
data/train.list
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文件名改成.gitignore


<p align="center">
<img src="image/ngram.png"><br/>
图3. 模型配置中的N-gram神经网络模型
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图6


$$\frac{1}{T}\sum_t f(w_t, w_{t-1}, ..., w_{t-n+1};\theta) + R(\theta)$$
在计算语言学中,n-gram是一种重要的文本表示方法,表示一个文本中连续的n个项。基于具体的应用场景,每一项可以是一个字母、单词或者音节。 n-gram模型也是统计语言模型中的一种重要方法,用n-gram训练语言模型时,一般用每个n-gram的历史n-1个词语组成的内容来预测第n个词。
Yoshua Bengio等科学家就于2003年在著名论文 Neural Probabilistic Language Models \[[1](#参考文献)\] 中介绍如何学习一个神经元网络表示的词向量模型。文中的神经概率语言模型(Neural Network Language Model,NNLM)通过一个线性映射和一个非线性隐层连接,同时学习了语言模型和词向量,即通过学习大量语料得到词语的向量表达,通过这些向量得到整个句子的概率。用这种方法学习语言模型可以克服维度灾难(curse of dimensionality),即训练和测试数据不同导致的模型不准。我们在上文中已经讲到用条件概率建模语言模型,即一句话中第$t$个词的概率和该句话的前$t-1$个词相关。可实际上越远的词语其实对该词的影响越小,那么如果考虑一个n-gram, 每个词都只受其前面`n-1`个词的影响,则有:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 70和71行之间空一行。
  2. 把99行放到“即训练和测试数据不同导致的模型不准“后面。
  3. 我们在上文中已经讲到用条件概率建模语言模型,XXX。从这里开始另起一行。

<p align="center">
<img src="image/nnlm.png"><br/>
图3. N-gram神经网络模型
</p>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

公式和图不对应,请统一。先放图,再将公式。


注意:由于“神经概率语言模型”说法较为泛泛,我们在这里不用其NNLM的本名,考虑到其具体做法,本文中称该模型为N-gram neural model。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

74行和72行中间不用空。


其中$g_i$为预测当前词的层中第$i$个输出词节点的值。具体来说,$g_i = \theta_i^Tx + b$, $x$为隐层特征(一个线性映射`embedding`通过全连接`fully connect`投影到的非线性隐层), $\theta$和$b$为隐层特征层到词预测层的全连接参数。
- 给定一些真实语料,这些语料中都是有意义的句子,语言模型的优化目标则是最大化目标函数:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

80行不用 - 。因为和下面的几点不是并列的。80行中的语言模型改成N-gram模型


其中$y_k^i$表示第$i$个样本第$k$类的真实标签(0或1),$softmax(g_k^i)$表示第i个样本第k类softmax输出的概率。

整个网络示意图如下所示:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图可以挪到85行前面。

104行改成:图2展示了N-gram神经网络模型,从下往上看,该模型分为以下几个部分:

@Zrachel
Copy link
Collaborator Author

Zrachel commented Dec 30, 2016

All done recent review suggestions. @luotao1

@luotao1
Copy link
Collaborator

luotao1 commented Dec 30, 2016

@wangkuiyi 这篇教程已经仔细改过一遍了,请问您还有其他意见么?另外,中文词向量预训练好的模型,会在下一个PR中补上。


<center>![图片](image/2d_similarity.png)</center>

另一方面,我们还可以通过计算词向量的cosine得到相似度, 如:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cosine ==> cosine similarity

@wangkuiyi wangkuiyi merged commit 4672708 into PaddlePaddle:develop Jan 1, 2017
@wangkuiyi
Copy link
Collaborator

赞!不好意思merge晚了~惭愧惭愧

@yupbank
Copy link

yupbank commented Jan 5, 2017

看上去很厉害啊。。准备学习学习

不知道有啥地方可以需要参与的

@luotao1
Copy link
Collaborator

luotao1 commented Jan 6, 2017

@yupbank 我们现在有两个JD如下:

欢迎加入我们!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

词向量
8 participants