# 2.7 负采样（Negative Sampling）

算法要做的是构造一个新的监督学习问题：给定一对单词，比如**orange**和**juice**，去预测这是否是一对上下文词-目标词（**context-target**）

在这个例子中**orange**和**juice**就是个正样本，用1作为标记，**orange**和**king**就是个负样本，标为0。要做的就是采样得到一个上下文词和一个目标词，中间列叫做词（**word**）。然后：

* 生成一个正样本，先抽取一个context，在一定词距内比如说正负10个词距内选一个target，生成这个表的第一行，即**orange– juice -1**的过程
* 生成一个负样本，用相同的context，再在字典中随机选一个词，如**king、book、the、of**，标记为0。因为如果随机选一个词，它很可能跟**orange**没关联

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/54beb302688f6a298b63178534281575.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/54beb302688f6a298b63178534281575.png)

如果从字典中随机选到的词，正好出现在了词距内，比如说在上下文词**orange**正负10个词之内，也没关系，如**of**被标记为0，即使**of**的确出现在**orange**词的前面

接下来将构造一个监督学习问题，学习算法输入$$x$$，即输入这对词（编号7），要去预测目标的标签（编号8），即预测输出$$y$$

如何选取$$K$$：

* 小数据集的话，$$K$$从5到20，数据集越小$$K$$就越大
* 如果数据集很大，$$K$$就选的小一点。对于更大的数据集$$K$$就从2到5

学习从$$x$$映射到$$y$$的监督学习模型：

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/f36df292b7444e9b7379fa7c14626fa2.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/f36df292b7444e9b7379fa7c14626fa2.png)

编号2是新的输入$$x$$，编号3是要预测的值$$y$$。记号$$c$$表示context，记号$$t$$表示可能的target，$$y$$表示0和1，即是否是一对context-target。要做的是定义一个逻辑回归模型，给定输入的$$c$$，$$t$$对的条件下，$$y=1$$的概率，即：

$$
P\left( y = 1 \middle| c,t \right) = \sigma(\theta\_{t}^{T}e\_{c})
$$

如果输入词是**orange**，即词6257，要做的就是输入**one-hot**向量，和$$E$$相乘获得嵌入向量$$e\_{6257}$$，最后得到10,000个可能的逻辑回归分类问题，其中一个（编号4）将会是用来判断目标词是否是**juice**的分类器，其他的词比如下面的某个分类器（编号5）是用来预测**king**是否是目标词

negative sampling中某个固定的正样本对应$$k$$个负样本，即模型总共包含了$$k+1$$个**binary classification**。对比之前10000个输出单元的softmax分类，negative sampling转化为$$k+1$$个二分类问题，每次迭代并不是训练10000个，而仅训练其中$$k+1$$个，计算量要小很多，大大提高了模型运算速度

这种方法就叫做**负采样（Negative Sampling）:** 选择一个正样本，随机采样$$k$$个负样本

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/b05dd0362a19496bb0ad91b8494e374c.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/b05dd0362a19496bb0ad91b8494e374c.png)

选取了context **orange**之后，如何选取负样本：

* 通过单词出现的频率进行采样：导致一些类似a、the、of等词的频率较高
* 均匀随机地抽取负样本：没有很好的代表性

**（推荐）**：

$$
P(w\_{i}) = \frac{f( w\_{i})^{\frac{3}{4}}}{\sum\_{j = 1}^{10,000}{f( w\_{j} )^{\frac{3}{4}}}}
$$

这种方法处于上面两种极端采样方法之间，即不用频率分布，也不用均匀分布，而采用的是对词频的$$\frac{3}{4}$$除以词频$$\frac{3}{4}$$整体的和进行采样的。其中，$$f(w\_j)$$是语料库中观察到的某个词的词频


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/neural-networks-and-deep-learning/di-wu-men-ke-xu-lie-mo-xing-sequence-models/di-wu-men-kexulie-mo-578b28-sequence-models/natural-language-processing-and-word-embeddings/27-fu-cai-yang-ff08-negative-sampling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
