NCE Layer引用自论文\[[1](#参考文献)\],NCE是指Noise-contrastive estimation,目的是用来提高训练速度并改善所得词向量的质量。与h-sigmoid[[2](#参考文献)\]相比,NCE不再使用复杂的Huffman树来构造目标函数,而是采用相对简单的随机负采样,这样能够大幅度提升计算性能。
假设已知具体的上下文 h,并且知道这个分布为 ${ P }^{ h }(w)$ ,我们将训练样例作为正样例,从一个噪音分布 ${ P }_{ n }(w)$ 抽样产生负样例。我们可以任意选择合适的噪音分布,默认为无偏的均匀分布。这里我们同时假设噪音样例 k 倍于数据样例,则训练数据被抽中的概率为[[2](#参考文献)]:
$$
{ P }^{ h }(D=1|w,\theta )=\frac { { P }_{ \theta }^{ h }(w) }{ { P }^{ h }_{ \theta }(w)+k{ P }_{ n }(w) } =\sigma (\Delta { s }_{ \theta }^{ }(w,h))
$$
其中 $\Delta { s }_{ \theta }^{ }(w,h)={ s }_{ \theta }^{ }(w,h)-\log { (k{ P }_{ n }^{ }(w)) }$ ,${ s }_{ \theta }(w,h)$ 表示选择在生成 $w$ 字并处于上下文 $h$ 时的特征向量,整体目标函数的目的就是增大正样本的概率同时降低负样本的概率。目标函数如下[[2](#参考文献)]:
$$
{ J }_{ }^{ h }(\theta )={ E }_{ { P }_{ d }^{ h } }^{ }\left[ \log { { P }_{ }^{ h }(D=1|w,\theta ) } \right] +k{ E }_{ { P }_{ n }^{ } }^{ }\left[ \log { { P }_{ }^{ h } } (D=0|w,\theta ) \right] \\\qquad ={ E }_{ { P }_{ d }^{ h } }^{ }\left[ \log { \sigma (\Delta { s }_{ \theta }^{ }(w,h)) } \right] +k{ E }_{ { P }_{ n }^{ } }^{ }\left[ \log { (1-\sigma (\Delta { s }_{ \theta }^{ }(w,h))) } \right]
$$
NCE 原理是通过构造一个逻辑回归(logistic regression),对正样例和负样例做二分类,对于每一个样本,将自身的预测词 label 作为正样例,同时采样出 k 个其他词 label 作为负样例,从而只需要计算样本在这 k+1 个 label 上的概率。相比原始的 softmax 分类需要计算每个类别的分数,然后归一化得到概率,softmax 这个计算过程是十分耗时的。
## 实验数据
本文采用 Penn Treebank (PTB)数据集([Tomas Mikolov预处理版本](http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz)),这是一个可以用来训练语言模型的数据集。PaddlePaddle 提供 [paddle.dataset.imikolov](https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/dataset/imikolov.py) 接口来方便调用数据,其中实现了数据自动下载,字典生成,滑动窗口等功能。数据接口给出的是前4个词让语言模型预测第5个词。
1. Mnih A, Kavukcuoglu K. [Learning word embeddings efficiently with noise-contrastive estimation](https://papers.nips.cc/paper/5165-learning-word-embeddings-efficiently-with-noise-contrastive-estimation.pdf)[C]//Advances in neural information processing systems. 2013: 2265-2273.
1. Mathematiques C D R. [Quick Training of Probabilistic Neural Nets by Importance Sampling](http://www.iro.umontreal.ca/~lisa/pointeurs/submit_aistats2003.pdf)[C]// 2002.
2. Mnih A, Kavukcuoglu K. [Learning word embeddings efficiently with noise-contrastive estimation](https://papers.nips.cc/paper/5165-learning-word-embeddings-efficiently-with-noise-contrastive-estimation.pdf)[C]//Advances in neural information processing systems. 2013: 2265-2273.
3. Morin, F., & Bengio, Y. (2005, January). [Hierarchical Probabilistic Neural Network Language Model](http://www.iro.umontreal.ca/~lisa/pointeurs/hierarchical-nnlm-aistats05.pdf). In Aistats (Vol. 5, pp. 246-252).
2. Morin, F., & Bengio, Y. (2005, January). [Hierarchical Probabilistic Neural Network Language Model](http://www.iro.umontreal.ca/~lisa/pointeurs/hierarchical-nnlm-aistats05.pdf). In Aistats (Vol. 5, pp. 246-252).
4. Mnih A, Teh Y W. [A Fast and Simple Algorithm for Training Neural Probabilistic Language Models](http://xueshu.baidu.com/s?wd=paperuri%3A%280735b97df93976efb333ac8c266a1eb2%29&filter=sc_long_sign&tn=SE_xueshusource_2kduw22v&sc_vurl=http%3A%2F%2Farxiv.org%2Fabs%2F1206.6426&ie=utf-8&sc_us=5770715420073315630)[J]. Computer Science, 2012:1751-1758.