← 深度学习导航
CORE PATH · 05

词表示与 Word2Vec

本页说明怎样把词转换为机器可计算、同时包含语义信息的向量。从 N 元语法与共现统计出发,再介绍 SVD、Word2Vec、CBOW、Skip-Gram 和训练加速方法。

统计语言处理(Statistical Language Processing)

术语:词嵌入(Word Embedding)把词映射为低维稠密向量;连续词袋模型(Continuous Bag of Words, CBOW)由上下文预测中心词;跳字模型(Skip-Gram)由中心词预测上下文;负采样(Negative Sampling)只更新少量噪声词以降低计算成本。

从统计规律学习 Language

ORIGINAL NOTES

不是 Static。Statistical Language Processing = 从大量文本的统计规律学习 language。

不用人工写“cat 是 animal”,而是统计 cat 平时和哪些 words 一起出现。

Synonym

同义词。

Antonym

反义词。

Taxonomy

分类层级,例如 penguin → bird → vertebrate。

传统方法的问题

需要大量人工,而且 nuances 容易丢失。

Word Frequency / Document Classification

COUNT VECTOR

统计每个 word 在 document 出现几次,然后 document 可以表示成:

[count(word1), …, count(wordV)]

某些 words 经常出现,可以帮助判断 document 类型。例如 cat / mouse / house → children’s text。

4. N-gram:研究词按什么顺序出现

N-gram 定义

ORDER

N-gram = 一次考虑连续的 n 个 words。如果用于预测,就用前 n−1 个 words 预测下一个 word。

Bigram

P(wt|wt−1)

看前 1 个 word。

Trigram

P(wt|wt−2, wt−1)

看前 2 个 words。

优点:n 大时 context 更多。
缺点:vocabulary 大时组合数量和 memory 会快速爆炸。
口诀:Bigram 看 1,Trigram 看 2,N-gram 看 n−1。

5–6. Co-occurrence Matrix:研究附近经常是谁

一个 Word 的“朋友圈统计表”

NEIGHBORHOOD

Co-occurrence Matrix 统计一个 word 附近经常出现哪些 words,不一定要求 consecutive。

  • row = 当前 word
  • column = context word
  • value = 一起出现在附近的次数
Similar context → Similar meaning如果两个词拥有相似的“朋友圈”,它们往往具有相似意义。
方法研究的问题是否强调顺序
N-gram谁按顺序跟在谁后面强调连续顺序
Co-occurrence谁经常和谁出现在附近附近即可,不一定连续

词嵌入(Word Embedding)与 SVD

PROBLEM

原始 Vector 太大

Vocabulary 几万,原始 vector 就可能有几万维;co-occurrence representation 高维而且 sparse。

GOAL

低维 Dense Vector

Word Embedding 把每个 word 变成低维 dense vector,使 similar context 的 words 拥有 similar vectors,并适合 neural network。

Singular Value Decomposition

SVD
X = UΣVT主要用途:降维(Dimensionality Reduction)。

SVD 把巨大的 co-occurrence matrix 压成更低维的 word representations。截断到前 N 个重要 dimensions 后,可以得到 N-dimensional word vector。

口诀:Co-occurrence = 大朋友圈表;SVD = 压成短特征卡。

问题:SVD 对整个巨大矩阵做 decomposition 很贵。

9–12. Word2Vec:用 Prediction 直接学习 Vector

Skip不先制造完整 co-occurrence matrix
Task直接设计 surrounding-word prediction task
Train通过训练更新 weight matrix
KeepPrediction 是训练手段,embedding/vector 才是最终目标
模型类型核心信息
Word2VecPredictive model通过 surrounding words 的 prediction 学 vector
GloVeCount-based model利用全局 co-occurrence statistics

Word Vector 在哪里?

WEIGHT MATRIX W

模型有 weight matrix W。W 的第 k 行 vk 可以作为 word k 的 representation。所以:训练 weights = 学 embeddings。

Softmax in Word2Vec

SCORE → PROBABILITY

先得到每个 candidate word 的 score uj,再用 Softmax 形成 probability distribution:

P(j|k) = euj / Σj′ euj′训练目标:真实 context word 的 probability ↑。

CBOW 与 Skip-Gram

CONTEXT → CENTER

连续词袋模型(Continuous Bag of Words, CBOW)

上下文词 → 中心词

例如 I really [love] deep learning:输入是 I、really、deep、learning,目标词是 love。口诀:大家猜中间。

CENTER → CONTEXT

跳字模型(Skip-Gram)

中心词 → 上下文词

同一个例子中,输入是 love,目标词是 I、really、deep、learning。口诀:中间猜大家。

必须秒答:CBOW: Context → Center;Skip-Gram: Center → Context。

16–19. Vocabulary 很大时怎样加速?

Full Softmax 的问题

V = 60,000

如果 vocabulary V = 60,000,Full Softmax 每次都要计算所有 60,000 个 outputs,代价太高。常见解决方案是 Hierarchical Softmax 和 Negative Sampling。

Hierarchical Softmax

把 vocabulary 放进 binary tree。预测 target 时从 root 沿 tree path 找过去,平均只访问 O(log V) 个 nodes。口诀:几万个选一个,变成连续二选一。

负采样(Negative Sampling)

不更新全部负例词,只使用 1 个正确目标词和少量抽样负例;提高正例分数并降低负例分数。口诀:一个真的 + 几个假的。

Subsampling Frequent Words

the / a / is / and 出现太多,所以高频 words 更容易被 discard 或少训练,避免无信息的高频词 dominate training。

20. Word Analogy / Semantic Relationships

King + Woman − Man ≈ QueenWord vectors 不只表示 similarity,也能把 gender / semantic relationship 表示为 vector-space direction。
扩展理解:类比公式描述的是向量空间中关系方向的近似规律,不意味着每个训练出的 embedding 都会完美满足所有类比。

Word Vectors 必背 10 句

  1. N-gram = 前 n−1 个词预测下一个。
  2. Co-occurrence = 看哪些 words 经常在附近一起出现。
  3. Similar context → similar meaning。
  4. Embedding = low-dimensional dense word vector。
  5. SVD = 高维 co-occurrence → 低维 representation。
  6. Word2Vec = 用 prediction task 直接学习 embedding。
  7. Word2Vec = predictive;GloVe = count-based。
  8. CBOW = context → center;Skip-Gram = center → context。
  9. Negative Sampling = positive + few negatives。
  10. Embedding 能编码关系:King + Woman − Man ≈ Queen。

两条完整学习路线

Sequence Models Sliding Window → RNN → BPTT → Long-range problem → LSTM / GRU → Bidirectional → Attention Word Vectors N-gram → Co-occurrence → Embedding → SVD → Word2Vec → CBOW / Skip-Gram → Negative Sampling → Analogy
最后压缩:先统计词怎么出现 → 用附近 context 表示 meaning → 大矩阵用 SVD 压缩 → Word2Vec 用预测直接学 vector → CBOW / Skip-Gram 决定预测方向 → Negative Sampling 加速 → 最终 vector 能表示 semantic relationships。

词表示与 Word2Vec 测验(Quiz)

40 道选择题:30 道简单、5 道中等、5 道困难;选择后立即显示答案与解析。

开始 40 题测验 →