14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Word2vecのCBOWについて

Last updated at Posted at 2018-04-16

はじめに

前回の続きとして、Word2Vecのもう一つのモデルCBOWについて説明する。

CBOW

Continuous Bag-of-Word Model(CBOW)はSkip-Gramと逆に、周囲の単語が与えられたとき、中心語となる単語を予測する。そのモデルは以下のように定式化できる。

\max\prod_{t=1}^{T}\mathbb{P}(w^{t}|w^{(t-m)},...,w^{(t-1)},w^{(t+1)},...,w^{(t+m)})

対数を取ることで、次のように定式化できる。

\min-\sum_{t=1}^{T}\log \mathbb{P}(w^{t}|w^{(t-m)},...,w^{(t-1)},w^{(t+1)},...,w^{(t+m)})

ここで、周囲の単語$w_{o_{1}},...,w_{o_{2m}}$から中心語$w_{c}$を生成する確率は以下のように与える。ここでは、逆に背景語を$\textbf{v}$、中心語を$\textbf{u}$と表す。

\mathbb{P}(w_{c}|w_{o_{1}},...,w_{o_{2m}})=\dfrac{\exp[\textbf{u}_{c}^{T}
(\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}})/(2m)]}
{\sum_{i \in \textit{V}}\exp[\textbf{u}_{i}^{T}(\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}})/(2m)]}

パラメータに関する勾配の計算は次のようになる。

\dfrac{\partial \log \mathbb{P}(w_{c}|w_{o_{1}},...,w_{o_{2m}})}{\partial \textbf{v}_{o_{i}}}= \dfrac{1}{2m}(\textbf{u}_{c} - 
\sum_{j \in \textit{V}}\dfrac{\exp[\textbf{u}_{j}^{T}(\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}})/(2m)]}{\sum_{i \in \textit{V}}\exp([\textbf{u}_{i}^{T}\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}})/(2m)]}\textbf{u}_{j})
\dfrac{\partial \log \mathbb{P}(w_{c}|w_{o_{1}},...,w_{o_{2m}})}{\partial \textbf{u}_{c}}=
\dfrac{\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}}}{2m} - \dfrac{\exp[\textbf{u}_{c}^{T}(\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}})/(2m)]}{\sum_{i \in \textit{V}}\exp[\textbf{u}_{i}^{T}(\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}})/(2m)]}\cdot\dfrac{\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}}}{2m}

また、上の式はそれぞれ以下のように変形できる。

\dfrac{\partial \log \mathbb{P}(w_{c}|w_{o_{1}},...,w_{o_{2m}})}{\partial \textbf{v}_{o_{i}}}=
\dfrac{1}{2m}(\textbf{u}_{c} - \sum_{j \in \textit{V}}\mathbb{P}(w_{j}|w_{o_{1}},...,w_{o_{2m}})\textbf{u}_{j})
\dfrac{\partial \log \mathbb{P}(w_{c}|w_{o_{1}},...,w_{o_{2m}})}{\partial \textbf{u}_{c}}=
\dfrac{\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}}}{2m} - \mathbb{P}(w_{c}|w_{o_{1}},...,w_{o_{2m}})\dfrac{\textbf{v}_{o_{1}}+...+\textbf{v}_{o_{2m}}}{2m}

ここで、上の式から分かるように、勾配を計算するとき、Skip-Gramと同様に計算量が大きいという問題が生じる。したがって、CBOWの学習には、通常Hierarchical Softmax(階層ソフトマックス)という手法が使われることが多い。これによって、計算量がO($|V|$)からO($\log|V|$)まで大きく減少される。

14
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?