プリンキピア

第15章自然言語処理:事前学習

15.1.4. The Continuous Bag of Words (CBOW) Model15.1.4 連続バッグオブワーズ(CBOW)モデル

The continuous bag of words (CBOW) model is similar to the skip-gram model. The major difference from the skip-gram model is that the continuous bag of words model assumes that a center word is generated based on its surrounding context words in the text sequence. For example, in the same text sequence “the”, “man”, “loves”, “his”, and “son”, with “loves” as the center word and the context window size being 2, the continuous bag of words model considers the conditional probability of generating the center word “loves” based on the context words “the”, “man”, “his” and “son” (as shown in Fig. 15.1.2), which is
英語のヒント
連続バッグオブワーズ(CBOW)モデルはスキップグラムモデルに似ている。スキップグラムモデルとの大きな違いは、連続バッグオブワーズモデルでは、テキストの系列の中で、中心語がその周りの文脈語にもとづいて生成されると仮定することである。たとえば、同じテキストの系列「the」「man」「loves」「his」「son」で、「loves」を中心語とし、文脈窓の大きさを2とすると、連続バッグオブワーズモデルは、文脈語「the」「man」「his」「son」にもとづいて中心語「loves」を生成する条件付き確率を考える(図15.1.2に示すとおり)。それは次のとおりである。
P("loves"∣"the","man","his","son").(15.1.9)
Fig. 15.1.2図15.1.2
The continuous bag of words model considers the conditional probability of generating the center word given its surrounding context words.
英語のヒント
連続バッグオブワーズモデルは、周りの文脈語が与えられたときに中心語を生成する条件付き確率を考える。
Since there are multiple context words in the continuous bag of words model, these context word vectors are averaged in the calculation of the conditional probability. Specifically, for any word with index i in the dictionary, denote by 𝐯i∈ℝd and 𝐮i∈ℝd its two vectors when used as a context word and a center word (meanings are switched in the skip-gram model), respectively. The conditional probability of generating any center word wc (with index c in the dictionary) given its surrounding context words wo1,…,wo2m (with index o1,…,o2m in the dictionary) can be modeled by
英語のヒント
連続バッグオブワーズモデルでは文脈語が複数あるので、条件付き確率の計算では、これらの文脈語ベクトルを平均する。具体的には、辞書でのインデックスがiの任意の単語について、それが文脈語として使われるときと中心語として使われるときの二つのベクトルを、それぞれ𝐯i∈ℝdと𝐮i∈ℝdで表す(スキップグラムモデルとは意味が入れ替わっている)。周りの文脈語wo1,…,wo2m(辞書でのインデックスはo1,…,o2m)が与えられたときに任意の中心語wc(辞書でのインデックスはc)を生成する条件付き確率は、次のようにモデル化できる。
P(wc∣wo1,…,wo2m)=exp⁡(12m𝐮c⊤(𝐯o1+…+𝐯o2m))∑i∈𝒱exp⁡(12m𝐮i⊤(𝐯o1+…+𝐯o2m)).(15.1.10)
For brevity, let 𝒲o={wo1,…,wo2m} and 𝐯¯o=(𝐯o1+…+𝐯o2m)/(2m). Then (15.1.10) can be simplified as
英語のヒント
簡潔にするため、𝒲o={wo1,…,wo2m}、𝐯¯o=(𝐯o1+…+𝐯o2m)/(2m)とおく。すると、(15.1.10)は次のように簡単になる。
P(wc∣𝒲o)=exp⁡(𝐮c⊤𝐯¯o)∑i∈𝒱exp⁡(𝐮i⊤𝐯¯o).(15.1.11)
Given a text sequence of length T, where the word at time step t is denoted as w(t). For context window size m, the likelihood function of the continuous bag of words model is the probability of generating all center words given their context words:
英語のヒント
長さTのテキストの系列が与えられ、時間ステップtの単語をw(t)で表すとする。文脈窓の大きさがmのとき、連続バッグオブワーズモデルの尤度関数は、それぞれの文脈語が与えられたときにすべての中心語を生成する確率である。
∏t=1TP(w(t)∣w(t−m),…,w(t−1),w(t+1),…,w(t+m)).(15.1.12)

15.1.4.1. Training15.1.4.1 訓練

Training continuous bag of words models is almost the same as training skip-gram models. The maximum likelihood estimation of the continuous bag of words model is equivalent to minimizing the following loss function:
英語のヒント
連続バッグオブワーズモデルの訓練は、スキップグラムモデルの訓練とほとんど同じである。連続バッグオブワーズモデルの最尤推定は、次の損失関数を最小化することと同値である。
−∑t=1TlogP(w(t)∣w(t−m),…,w(t−1),w(t+1),…,w(t+m)).(15.1.13)
Notice that
英語のヒント
次に注意しよう。
logP(wc∣𝒲o)=𝐮c⊤𝐯¯o−log(∑i∈𝒱exp⁡(𝐮i⊤𝐯¯o)).(15.1.14)
Through differentiation, we can obtain its gradient with respect to any context word vector 𝐯oi(i=1,…,2m) as
英語のヒント
微分によって、任意の文脈語ベクトル𝐯oi(i=1,…,2m)についてのその勾配は次のように得られる。
∂logP(wc∣𝒲o)∂𝐯oi=12m(𝐮c−∑j∈𝒱exp⁡(𝐮j⊤𝐯¯o)𝐮j∑i∈𝒱exp⁡(𝐮i⊤𝐯¯o))=12m(𝐮c−∑j∈𝒱P(wj∣𝒲o)𝐮j).(15.1.15)
The gradients for the other word vectors can be obtained in the same way. Unlike the skip-gram model, the continuous bag of words model typically uses context word vectors as the word representations.
英語のヒント
ほかの単語ベクトルについての勾配も同じやり方で得られる。スキップグラムモデルとは違って、連続バッグオブワーズモデルではふつう文脈語ベクトルを単語の表現として使う。