プリンキピア

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

15.1.3. The Skip-Gram Model15.1.3 スキップグラムモデル

The skip-gram model assumes that a word can be used to generate its surrounding words in a text sequence. Take the text sequence “the”, “man”, “loves”, “his”, “son” as an example. Let’s choose “loves” as the center word and set the context window size to 2. As shown in Fig. 15.1.1, given the center word “loves”, the skip-gram model considers the conditional probability for generating the context words: “the”, “man”, “his”, and “son”, which are no more than 2 words away from the center word:
英語のヒント
スキップグラムモデルは、テキストの系列の中で、ある単語を使ってその周りの単語を生成できると仮定する。テキストの系列「the」「man」「loves」「his」「son」を例にとろう。「loves」を中心語に選び、文脈窓の大きさを2にする。図15.1.1に示すように、中心語「loves」が与えられたとき、スキップグラムモデルは、中心語から2語以内の距離にある文脈語「the」「man」「his」「son」を生成する、次の条件付き確率を考える。
P("the","man","his","son"∣"loves").(15.1.2)
Assume that the context words are independently generated given the center word (i.e., conditional independence). In this case, the above conditional probability can be rewritten as
英語のヒント
中心語が与えられたとき、文脈語は独立に生成されると仮定する(つまり条件付き独立性)。この場合、上の条件付き確率は次のように書き直せる。
P("the"∣"loves")⋅P("man"∣"loves")⋅P("his"∣"loves")⋅P("son"∣"loves").(15.1.3)
Fig. 15.1.1図15.1.1
The skip-gram model considers the conditional probability of generating the surrounding context words given a center word.
英語のヒント
スキップグラムモデルは、中心語が与えられたときに周りの文脈語を生成する条件付き確率を考える。
In the skip-gram model, each word has two d-dimensional-vector representations for calculating conditional probabilities. More concretely, for any word with index i in the dictionary, denote by 𝐯i∈ℝd and 𝐮i∈ℝd its two vectors when used as a center word and a context word, respectively. The conditional probability of generating any context word wo (with index o in the dictionary) given the center word wc (with index c in the dictionary) can be modeled by a softmax operation on vector dot products:
英語のヒント
スキップグラムモデルでは、各単語は、条件付き確率を計算するためのd次元のベクトル表現を二つ持つ。もっと具体的には、辞書でのインデックスがiの任意の単語について、それが中心語として使われるときと文脈語として使われるときの二つのベクトルを、それぞれ𝐯i∈ℝdと𝐮i∈ℝdで表す。中心語wc(辞書でのインデックスはc)が与えられたときに任意の文脈語wo(辞書でのインデックスはo)を生成する条件付き確率は、ベクトルの内積へのソフトマックス演算でモデル化できる。
P(wo∣wc)=exp⁡(𝐮o⊤𝐯c)∑i∈𝒱exp⁡(𝐮i⊤𝐯c),(15.1.4)
where the vocabulary index set 𝒱={0,1,…,|𝒱|−1}. Given a text sequence of length T, where the word at time step t is denoted as w(t). Assume that context words are independently generated given any center word. For context window size m, the likelihood function of the skip-gram model is the probability of generating all context words given any center word:
英語のヒント
ここで、語彙のインデックスの集合は𝒱={0,1,…,|𝒱|−1}である。長さTのテキストの系列が与えられ、時間ステップtの単語をw(t)で表すとする。任意の中心語が与えられたとき、文脈語は独立に生成されると仮定する。文脈窓の大きさがmのとき、スキップグラムモデルの尤度関数は、任意の中心語が与えられたときにすべての文脈語を生成する確率である。
∏t=1T∏−m≤j≤m, j≠0P(w(t+j)∣w(t)),(15.1.5)
where any time step that is less than 1 or greater than T can be omitted.
英語のヒント
ここで、1より小さいかTより大きい時間ステップはどれも省いてよい。

15.1.3.1. Training15.1.3.1 訓練

The skip-gram model parameters are the center word vector and context word vector for each word in the vocabulary. In training, we learn the model parameters by maximizing the likelihood function (i.e., maximum likelihood estimation). This is equivalent to minimizing the following loss function:
英語のヒント
スキップグラムモデルのパラメータは、語彙の各単語の中心語ベクトルと文脈語ベクトルである。訓練では、尤度関数を最大化すること(つまり最尤推定)によってモデルのパラメータを学習する。これは、次の損失関数を最小化することと同値である。
−∑t=1T∑−m≤j≤m, j≠0logP(w(t+j)∣w(t)).(15.1.6)
When using stochastic gradient descent to minimize the loss, in each iteration we can randomly sample a shorter subsequence to calculate the (stochastic) gradient for this subsequence to update the model parameters. To calculate this (stochastic) gradient, we need to obtain the gradients of the log conditional probability with respect to the center word vector and the context word vector. In general, according to (15.1.4) the log conditional probability involving any pair of the center word wc and the context word wo is
英語のヒント
確率的勾配降下法を使って損失を最小化するときは、各反復で、もっと短い部分系列をランダムに抜き出し、この部分系列についての(確率的な)勾配を計算して、モデルのパラメータを更新できる。この(確率的な)勾配を計算するには、中心語ベクトルと文脈語ベクトルについての対数条件付き確率の勾配を得る必要がある。一般に、(15.1.4)によれば、中心語wcと文脈語woの任意の組を含む対数条件付き確率は次のとおりである。
log⁡P(wo∣wc)=𝐮o⊤𝐯c−log⁡(∑i∈𝒱exp⁡(𝐮i⊤𝐯c)).(15.1.7)
Through differentiation, we can obtain its gradient with respect to the center word vector 𝐯c as
英語のヒント
微分によって、中心語ベクトル𝐯cについてのその勾配は次のように得られる。
∂logP(wo∣wc)∂𝐯c=𝐮o−∑j∈𝒱exp⁡(𝐮j⊤𝐯c)𝐮j∑i∈𝒱exp⁡(𝐮i⊤𝐯c)=𝐮o−∑j∈𝒱(exp⁡(𝐮j⊤𝐯c)∑i∈𝒱exp⁡(𝐮i⊤𝐯c))𝐮j=𝐮o−∑j∈𝒱P(wj∣wc)𝐮j.(15.1.8)
Note that the calculation in (15.1.8) requires the conditional probabilities of all words in the dictionary with wc as the center word. The gradients for the other word vectors can be obtained in the same way.
英語のヒント
(15.1.8)の計算には、wcを中心語とする辞書のすべての単語の条件付き確率が必要であることに注意しよう。ほかの単語ベクトルについての勾配も同じやり方で得られる。
After training, for any word with index i in the dictionary, we obtain both word vectors 𝐯i (as the center word) and 𝐮i (as the context word). In natural language processing applications, the center word vectors of the skip-gram model are typically used as the word representations.
英語のヒント
訓練の後、辞書でのインデックスがiの任意の単語について、二つの単語ベクトル𝐯i(中心語として)と𝐮i(文脈語として)が得られる。自然言語処理の応用では、ふつう、スキップグラムモデルの中心語ベクトルが単語の表現として使われる。