プリンキピア

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

15.2.1. Negative Sampling15.2.1 負例サンプリング

Negative sampling modifies the original objective function. Given the context window of a center word wc, the fact that any (context) word wo comes from this context window is considered as an event with the probability modeled by
英語のヒント
負例サンプリングは元の目的関数を変更する。中心語wcの文脈窓が与えられたとき、任意の(文脈)語woがこの文脈窓から来るという事実を、次のようにモデル化した確率をもつ事象とみなす。
P(D=1∣wc,wo)=σ(𝐮o⊤𝐯c),(15.2.1)
where σ uses the definition of the sigmoid activation function:
英語のヒント
ここで、σにはシグモイド活性化関数の定義を使う。
σ(x)=11+exp⁡(−x).(15.2.2)
Let’s begin by maximizing the joint probability of all such events in text sequences to train word embeddings. Specifically, given a text sequence of length T, denote by w(t) the word at time step t and let the context window size be m, consider maximizing the joint probability
英語のヒント
単語埋め込みを訓練するため、まずテキストの系列の中のそうした事象すべての同時確率を最大化することから始めよう。具体的には、長さTのテキストの系列が与えられたとき、時間ステップtの単語をw(t)で表し、文脈窓の大きさをmとして、次の同時確率を最大化することを考える。
∏t=1T∏−m≤j≤m, j≠0P(D=1∣w(t),w(t+j)).(15.2.3)
However, (15.2.3) only considers those events that involve positive examples. As a result, the joint probability in (15.2.3) is maximized to 1 only if all the word vectors are equal to infinity. Of course, such results are meaningless. To make the objective function more meaningful, negative sampling adds negative examples sampled from a predefined distribution.
英語のヒント
しかし、(15.2.3)は正例を含む事象しか考えていない。その結果、(15.2.3)の同時確率が最大値の1になるのは、すべての単語ベクトルが無限大に等しいときに限られる。もちろん、そのような結果には意味がない。目的関数をもっと意味のあるものにするため、負例サンプリングは、あらかじめ決めた分布から抜き出した負例を加える。
Denote by S the event that a context word wo comes from the context window of a center word wc. For this event involving wo, from a predefined distribution P(w) sample K noise words that are not from this context window. Denote by Nk the event that a noise word wk (k=1,…,K) does not come from the context window of wc. Assume that these events involving both the positive example and negative examples S,N1,…,NK are mutually independent. Negative sampling rewrites the joint probability (involving only positive examples) in (15.2.3) as
英語のヒント
文脈語woが中心語wcの文脈窓から来るという事象をSで表す。woを含むこの事象について、あらかじめ決めた分布P(w)から、この文脈窓から来たのではないノイズ語をK個抜き出す。ノイズ語wk(k=1,…,K)がwcの文脈窓から来ないという事象をNkで表す。正例と負例の両方を含むこれらの事象S,N1,…,NKは互いに独立であると仮定する。負例サンプリングは、(15.2.3)の(正例だけを含む)同時確率を次のように書き直す。
∏t=1T∏−m≤j≤m, j≠0P(w(t+j)∣w(t)),(15.2.4)
where the conditional probability is approximated through events S,N1,…,NK:
英語のヒント
ここで、条件付き確率は事象S,N1,…,NKを通じて近似される。
P(w(t+j)∣w(t))=P(D=1∣w(t),w(t+j))∏k=1, wk∼P(w)KP(D=0∣w(t),wk).(15.2.5)
Denote by it and hk the indices of a word w(t) at time step t of a text sequence and a noise word wk, respectively. The logarithmic loss with respect to the conditional probabilities in (15.2.5) is
英語のヒント
テキストの系列の時間ステップtにある単語w(t)のインデックスと、ノイズ語wkのインデックスを、それぞれitとhkで表す。(15.2.5)の条件付き確率についての対数損失は次のとおりである。
−log⁡P(w(t+j)∣w(t))=−log⁡P(D=1∣w(t),w(t+j))−∑k=1, wk∼P(w)Klog⁡P(D=0∣w(t),wk)=−logσ(𝐮it+j⊤𝐯it)−∑k=1, wk∼P(w)Klog⁡(1−σ(𝐮hk⊤𝐯it))=−logσ(𝐮it+j⊤𝐯it)−∑k=1, wk∼P(w)Klog⁡σ(−𝐮hk⊤𝐯it).(15.2.6)
We can see that now the computational cost for gradients at each training step has nothing to do with the dictionary size, but linearly depends on K. When setting the hyperparameter K to a smaller value, the computational cost for gradients at each training step with negative sampling is smaller.
英語のヒント
これで、各訓練ステップでの勾配の計算コストは辞書の大きさとは関係がなく、Kに線形に依存することがわかる。ハイパーパラメータKをもっと小さな値に設定すると、負例サンプリングでの各訓練ステップの勾配の計算コストは小さくなる。