# 2.8 GloVe 词向量（GloVe Word Vectors）

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

**GloVe**代表用词表示的全局变量（**global vectors for word representation**）

假定$$X\_{ij}$$是单词$$i$$在单词$$j$$上下文中出现的次数，$$i$$和$$j$$ 与$$t$$和$$c$$的功能一样，可以认为$$X\_{ij}$$等同于$$X\_{tc}$$。根据context和target的定义，会得出$$X\_{ij}$$等于$$X\_{ji}$$

* 如果将context和target的范围定义为出现于左右各10词以内的话，就有对称关系$$X\_{ij}=X\_{ji}$$
* 如果对context的选择是context总是目target前一个单词，那么$$X\_{ij}\neq X\_{ji}$$

对于**GloVe**算法，可以定义context和target为任意两个位置相近的单词，假设是左右各10词的距离，那么$$X\_{ij}$$就是一个能够获取单词$$i$$和单词$$j$$彼此接近的频率计数器

**GloVe**模型做的就是进行优化，将差距进行最小化处理：

$$
\text{mini}\text{mize}\sum\_{i = 1}^{10,000}{\sum\_{j = 1}^{10,000}}{f\left( X\_{ij}
\right)\left( \theta\_{i}^{T}e\_{j} + b\_{i} + b\_{j}^{'} - \log X\_{ij} \right)^{2}}
$$

$$\theta\_{i}^{T}e\_{j}$$即$$\theta\_{t}^{T}e\_{c}$$。对于$$\theta\_{t}^{T}e\_{c}$$，这两个单词同时出现的频率是多少受$$X\_{ij}$$影响，若两个词的embedding vector越相近，同时出现的次数越多，则对应的loss越小

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

当$$X\_{ij}=0$$时，权重因子$$f(X\_{ij})=0$$。这种做法直接忽略了无任何相关性的context和target，只考虑$$X\_{ij}>0$$的情况

出现频率较大的单词相应的权重因子$$f(X\_{ij})$$较大，出现频率较小的单词相应的权重因子$$f(X\_{ij})$$较小一些

因为$$\theta$$和$$e$$是完全对称的，所以$$\theta\_{i}$$和$$e\_{j}$$是对称的。因此训练算法的方法是一致地初始化$$\theta$$和$$e$$，然后使用梯度下降来最小化输出，当每个词都处理完之后取平均值：

$$
e\_{w}^{(final)}= \frac{e\_{w} +\theta\_{w}}{2}
$$

**GloVe**算法不能保证嵌入向量的独立组成部分：

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

通过上面的很多算法得到的词嵌入向量，无法保证词嵌入向量的每个独立分量是能够理解的。但能够确定是每个分量和所想的一些特征是有关联的，可能是一些我们能够理解的特征的组合而构成的一个组合分量

使用上面的GloVe模型，从线性代数的角度解释如下：

$$
\Theta\_{i}^{T}e\_{j} = \Theta\_{i}^{T}A^{T}A^{-T}e\_{j}=(A\Theta\_{i})^{T}(A^{-T}e\_{j})
$$

加入的$$A$$项，可能构成任意的分量组合


---

# 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/28-glove-ci-xiang-liang-ff08-glove-word-vectors.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.
