# 2.3 词嵌入的特性（Properties of Word Embeddings）

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

该例中，假设用的是四维的嵌入向量，假如向量$$e\_{\text{man}}$$和$$e\_{\text{woman}}$$、$$e\_{\text{king}}$$和$$e\_{\text{queen}}$$ 分别进行减法运算，相减结果表明，“Man”与“Woman”的主要区别是性别，“King”与“Queen”也是一样

所以当算法被问及**man**对**woman**相当于**king**对什么时，算法所做的就是计算$$e\_{\text{man}}-e\_{\text{woman}}$$，然后找出一个向量也就是找出一个词，使得：

$$
e\_{\text{man}}-e\_{\text{woman}}\approx e\_{\text{king}} - e\_{?}
$$

即当这个新词是**queen**时，式子的左边会近似地等于右边

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

在图中，词嵌入向量在一个可能有300维的空间里，箭头代表的是向量在**gender**（**性别**）这一维的差，为了得出类比推理，计算当**man**对于**woman**，**king**对于什么，要做的就是找到单词**w**来使得

$$
e\_{\text{man}}-e\_{\text{woman}}\approx e\_{\text{king}} - e\_{w}
$$

等式成立，即找到单词**w**来最大化$$e\_{w}$$与$$e\_{\text{king}} - e\_{\text{man}} + e\_{\text{woman}}$$的相似度，即

$$
Find\ word\ w:argmax\ Sim(e\_{w},e\_{\text{king}} - e\_{\text{man}} + e\_{\text{woman}})
$$

即把$$e\_{w}$$全部放到等式的一边，另一边是$$e\_{\text{king}}- e\_{\text{man}} + e\_{\text{woman}}$$。应用相似度函数，通过方程找到一个使得相似度最大的单词，如果结果理想的话会得到单词**queen**

**t-SNE算法**所做的就是把这些300维的数据用一种非线性的方式映射到2维平面上，可以得知**t-SNE**中这种映射很复杂而且很非线性。在大多数情况下，由于**t-SNE**的非线性映射，不能总是期望使等式成立的关系会像左边那样成一个平行四边形

关于相似函数，比较常用的是余弦相似度，假如在向量$$u$$和$$v$$之间定义相似度：

$$
Sim(u,v)=\frac{u^Tv}{||u||\cdot ||v||}
$$

分子是$$u$$和$$v$$的内积。如果$$u$$和$$v$$非常相似，那么它们的内积将会很大，把整个式子叫做余弦相似度，是因为该式是$$u$$和$$v$$的夹角的余弦值

**参考资料：** 给定两个向量$$u$$和$$v$$，余弦相似度定义如下：

$$
{CosineSimilarity(u, v)} = \frac {u . v} {||u||\_2 ||v||\_2} = cos(\theta)
$$

$$u.v$$ 是两个向量的点积（或内积），$$||u||\_2$$是向量$$u$$的范数（或长度）， $$\theta$$ 是向量$$u$$和$$v$$之间的角度。这种相似性取决于角度在向量$$u$$和$$v$$之间。如果向量$$u$$和$$v$$非常相似，它们的余弦相似性将接近1; 如果它们不相似，则余弦相似性将取较小的值

[![osine\_si](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/cosine_sim.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/cosine_sim.png)

> 两个向量之间角度的余弦是衡量它们有多相似的指标，角度越小，两个向量越相似

还可以计算Euclidian distance来比较相似性，即$$||u-v||^2$$。距离越大，相似性越小


---

# 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/23-ci-qian-ru-de-texing-ff08-propertiesof-word-embeddings.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.
