【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors

【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors

我们如何表达一个词的意思?

  • 早期方法比如WordNet:建立包含同义词synonym和上位词hypernym(即“is a"的关系)的词库
    • 缺少细微差别,一词多义
    • 缺少单词新的含义,不能及时更新
    • 构建过程过于主观
    • 人工构造
    • 无法计算准确的单词相似度
  • 离散符号-OneHot:【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors
    • 词汇表太大
    • 无法有效的表示两个向量间的相似度
  • 词汇的含义可能与上下文相关,尤其是多义词。
    分布式语义( Distributional semantics):一个词的含义经常由出现在它附近的词所决定。
    【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors
    【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors

word vector

是dense vector(即大多数元素不为零且维度较小的向量),也被称为word embedding 或word representation。它们是分布式表示。
维度:通常不低于50。降为二维进行可视化。

papers

Efficient Estimation of Word Representations in Vector Space(original word2vec paper)
Distributed Representations of Words and Phrases and their Compositionality (negative sampling paper)
Word2vec是一个学习单词向量的框架

idea

  • 固定词汇表中的每个单词都由一个向量表示
  • 遍历文本中的每个位置t,其中有一个中心词c和上下文(外部)单词o
  • 使用向量c和o的相似度来计算给定c的o的概率(反之亦然)
  • 不断调整单词向量,以使得上下文词出现概率最大(Skip-Gram
    如图是窗口大小为2,中心词为into的context words的概率表示(预测每个单词周围的单词):
    【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors
    扫描到下一个位置:
    【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors

objective function

  • 极大似然估计:(最大化):这些条件概率的累乘积
    对于每个位置t=1,...,Tt=1, ..., T,给定中心单词wjw_j,预测固定大小mm的窗口内的概率最大上下文单词
    【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors
    objective function也叫cost or loss function。
    取对数和负平均
  • 目标函数为(){\color{Red} (平均)负对数似然}(最小化)
    【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors
    最小化目标函数 \Leftrightarrow 最大化似然函数 \Leftrightarrow 最大化准确率。
  • 如何计算每个条件概率?
    每个词w用两种向量表示:
    • vwv_w:是中心词的词向量;
    • uwu_w是上下文的词向量.
      因为同一个词,在不同时刻可能作为中心词也可能作为上下文的词。
      prediction function:
      【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors
  • 利用梯度下降算法优化参数训练模型
    这里的θ\thetavcv_c,对vcv_c求偏导:
    【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors
    【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors

Gensim本身不支持Glove Vector,需要将Glove文件格式转换为word2vec文件格式
【2019斯坦福CS224n-NLP with DL】Lecture 01 Introduction and Word Vectors