18.2 Approximate Training

Recall our discussions in Section 18.1. The main idea of the skip-gram model is using softmax operations to calculate the conditional probability of generating a context word wow_o based on the given center word wcw_c in (18.1.4), whose corresponding logarithmic loss is given by the opposite of (18.1.7).

Due to the nature of the softmax operation, since a context word may be any word in the dictionary V\mathcal{V}, the opposite of (18.1.7) contains the summation of items as many as the entire size of the vocabulary. Consequently, the gradient calculation for the skip-gram model in (18.1.8) and that for the continuous bag-of-words model in (18.1.15) both contain the summation. Unfortunately, the computational cost for such gradients that sum over a large dictionary (often with hundreds of thousands or millions of words) is huge!

In order to reduce the aforementioned computational complexity, this section will introduce two approximate training methods: negative sampling and hierarchical softmax. Due to the similarity between the skip-gram model and the continuous bag of words model, we will just take the skip-gram model as an example to describe these two approximate training methods.

18.2.1 Negative Sampling#

Negative sampling modifies the original objective function. Given the context window of a center word wcw_c, the fact that any (context) word wow_o comes from this context window is considered as an event with the probability modeled by

P(D=1∣wc,wo)=σ(uo⊤vc),(18.2.1)P(D=1\mid w_c, w_o) = \sigma(\mathbf{u}_o^\top \mathbf{v}_c), \tag{18.2.1}

where σ\sigma uses the definition of the sigmoid activation function:

σ(x)=11+exp⁡(−x).(18.2.2)\sigma(x) = \frac{1}{1+\exp(-x)}. \tag{18.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 TT, denote by w(t)w^{(t)} the word at time step tt and let the context window size be mm, consider maximizing the joint probability

∏t=1T∏−m≤j≤m, j≠0P(D=1∣w(t),w(t+j)).(18.2.3)\prod_{t=1}^{T} \prod_{-m \leq j \leq m,\ j \neq 0} P(D=1\mid w^{(t)}, w^{(t+j)}). \tag{18.2.3}

However, (18.2.3) only considers those events that involve positive examples. As a result, the joint probability in (18.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.

Denote by SS the event that a context word wow_o comes from the context window of a center word wcw_c. For this event involving wow_o, from a predefined distribution P(w)P(w) sample KK noise words that are not from this context window. Denote by NkN_k the event that a noise word wkw_k (k=1,…,Kk=1, \ldots, K) does not come from the context window of wcw_c. Assume that these events involving both the positive example and negative examples S,N1,…,NKS, N_1, \ldots, N_K are mutually independent. Negative sampling rewrites the joint probability (involving only positive examples) in (18.2.3) as

∏t=1T∏−m≤j≤m, j≠0P(w(t+j)∣w(t)),(18.2.4)\prod_{t=1}^{T} \prod_{-m \leq j \leq m,\ j \neq 0} P(w^{(t+j)} \mid w^{(t)}), \tag{18.2.4}

where the conditional probability is approximated through events S,N1,…,NKS, N_1, \ldots, N_K:

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).(18.2.5)P(w^{(t+j)} \mid w^{(t)}) =P(D=1\mid w^{(t)}, w^{(t+j)})\prod_{k=1,\ w_k \sim P(w)}^K P(D=0\mid w^{(t)}, w_k). \tag{18.2.5}

Denote by iti_t and hkh_k the indices of a word w(t)w^{(t)} at time step tt of a text sequence and a noise word wkw_k, respectively. The logarithmic loss with respect to the conditional probabilities in (18.2.5) is

−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⁡ σ(uit+j⊤vit)−∑k=1, wk∼P(w)Klog⁡(1−σ(uhk⊤vit))=−log⁡ σ(uit+j⊤vit)−∑k=1, wk∼P(w)Klog⁡σ(−uhk⊤vit).\begin{aligned} -\log P(w^{(t+j)} \mid w^{(t)}) =& -\log P(D=1\mid w^{(t)}, w^{(t+j)}) \\ &\quad - \sum_{k=1,\ w_k \sim P(w)}^K \log P(D=0\mid w^{(t)}, w_k)\\ =&- \log\, \sigma\left(\mathbf{u}_{i_{t+j}}^\top \mathbf{v}_{i_t}\right) - \sum_{k=1,\ w_k \sim P(w)}^K \log\left(1-\sigma\left(\mathbf{u}_{h_k}^\top \mathbf{v}_{i_t}\right)\right)\\ =&- \log\, \sigma\left(\mathbf{u}_{i_{t+j}}^\top \mathbf{v}_{i_t}\right) - \sum_{k=1,\ w_k \sim P(w)}^K \log\sigma\left(-\mathbf{u}_{h_k}^\top \mathbf{v}_{i_t}\right). \end{aligned}

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 KK. When setting the hyperparameter KK to a smaller value, the computational cost for gradients at each training step with negative sampling is smaller.

18.2.2 Hierarchical Softmax#

As an alternative approximate training method, hierarchical softmax uses the binary tree, a data structure illustrated in Figure 18.2.1, where each leaf node of the tree represents a word in dictionary V\mathcal{V}.

Figure 18.2.1:

Hierarchical softmax for approximate training, where each leaf node of the tree represents a word in the dictionary.

Denote by L(w)L(w) the number of nodes (including both ends) on the path from the root node to the leaf node representing word ww in the binary tree. Let n(w,j)n(w,j) be the jthj^\textrm{th} node on this path, with its context word vector being un(w,j)\mathbf{u}_{n(w, j)}. For example, L(w3)=4L(w_3) = 4 in Figure 18.2.1. Hierarchical softmax approximates the conditional probability in (18.1.4) as

P(wo∣wc)=∏j=1L(wo)−1σ([ ⁣[n(wo,j+1)=leftChild(n(wo,j))] ⁣]⋅un(wo,j)⊤vc),(18.2.6)P(w_o \mid w_c) = \prod_{j=1}^{L(w_o)-1} \sigma\left( [\![ n(w_o, j+1) = \textrm{leftChild}(n(w_o, j)) ]\!] \cdot \mathbf{u}_{n(w_o, j)}^\top \mathbf{v}_c\right), \tag{18.2.6}

where function σ\sigma is defined in (18.2.2), and leftChild(n)\textrm{leftChild}(n) is the left child node of node nn: if xx is true, [ ⁣[x] ⁣]=1[\![x]\!] = 1; otherwise [ ⁣[x] ⁣]=−1[\![x]\!] = -1.

To illustrate, let's calculate the conditional probability of generating word w3w_3 given word wcw_c in Figure 18.2.1. This requires dot products between the word vector vc\mathbf{v}_c of wcw_c and non-leaf node vectors on the path (the path in bold in Figure 18.2.1) from the root to w3w_3, which is traversed left, right, then left:

P(w3∣wc)=σ(un(w3,1)⊤vc)⋅σ(−un(w3,2)⊤vc)⋅σ(un(w3,3)⊤vc).(18.2.7)P(w_3 \mid w_c) = \sigma(\mathbf{u}_{n(w_3, 1)}^\top \mathbf{v}_c) \cdot \sigma(-\mathbf{u}_{n(w_3, 2)}^\top \mathbf{v}_c) \cdot \sigma(\mathbf{u}_{n(w_3, 3)}^\top \mathbf{v}_c). \tag{18.2.7}

Since σ(x)+σ(−x)=1\sigma(x)+\sigma(-x) = 1, it holds that the conditional probabilities of generating all the words in dictionary V\mathcal{V} based on any word wcw_c sum up to one:

∑w∈VP(w∣wc)=1.(18.2.8)\sum_{w \in \mathcal{V}} P(w \mid w_c) = 1. \tag{18.2.8}

Fortunately, since L(wo)−1L(w_o)-1 is on the order of O(log2∣V∣)O(\textrm{log}_2|\mathcal{V}|) due to the binary tree structure, when the dictionary size V\mathcal{V} is huge, the computational cost for each training step using hierarchical softmax is significantly reduced compared with that without approximate training.

18.2.3 Summary#

  • Negative sampling constructs the loss function by considering mutually independent events that involve both positive and negative examples. The computational cost for training is linearly dependent on the number of noise words at each step.
  • Hierarchical softmax constructs the loss function using the path from the root node to the leaf node in the binary tree. The computational cost for training is dependent on the logarithm of the dictionary size at each step.

18.2.4 Exercises#

  1. How can we sample noise words in negative sampling?

  2. Verify that (18.2.8) holds.

  3. How to train the continuous bag of words model using negative sampling and hierarchical softmax, respectively?

  4. For a center word wcw_c, a context word wow_o, and noise words wh1,…,whKw_{h_1}, \ldots, w_{h_K}, the negative sampling loss is ℓ=−log⁡σ(uo⊤vc)−∑k=1Klog⁡σ(−uhk⊤vc)\ell = -\log \sigma(\mathbf{u}_o^\top \mathbf{v}_c) - \sum_{k=1}^K \log \sigma(-\mathbf{u}_{h_k}^\top \mathbf{v}_c).

    1. Show that σ′(x)=σ(x)(1−σ(x))\sigma'(x) = \sigma(x)(1 - \sigma(x)) and that the derivative of log⁡σ(x)\log \sigma(x) is 1−σ(x)1 - \sigma(x).
    2. Assume that the KK noise words are distinct and differ from wow_o. Derive ∂ℓ/∂vc\partial \ell / \partial \mathbf{v}_c, ∂ℓ/∂uo\partial \ell / \partial \mathbf{u}_o, and ∂ℓ/∂uhk\partial \ell / \partial \mathbf{u}_{h_k}. How many word vectors receive a nonzero gradient, and how many operations does computing all of these gradients take, in terms of KK and the dimension dd?
    3. Now let a word ww be drawn several times among the noise words. Derive ∂ℓ/∂uw\partial \ell / \partial \mathbf{u}_w.

    Adapted from Stanford CS224N, Assignment 2, Problems 1(e)–1(g).

  5. Negative sampling does not fit the probabilities of (18.1.4). To find what it fits instead, let ncon_{co} be the number of times word wow_o occurs in a context window of center word wcw_c in the corpus, let nc=∑oncon_c = \sum_o n_{co} and no=∑cncon_o = \sum_c n_{co}, and let D=∑c,oncoD = \sum_{c, o} n_{co} be the number of (center word, context word) pairs. Treat x=uo⊤vcx = \mathbf{u}_o^\top \mathbf{v}_c as a free parameter for every pair of words.

    1. Show that the terms of the expected loss over the corpus that depend on xx are ncolog⁡(1+e−x)+KncP(wo)log⁡(1+ex)n_{co} \log(1 + e^{-x}) + K n_c P(w_o) \log(1 + e^{x}), and that they are minimized at x⋆=log⁡(nco/(KncP(wo)))x^\star = \log \left(n_{co} / (K n_c P(w_o))\right).
    2. Let the noise distribution be P(wo)=no/DP(w_o) = n_o / D. Show that x⋆x^\star is the pointwise mutual information log⁡(ncoD/(ncno))\log \left(n_{co} D / (n_c n_o)\right) minus log⁡K\log K (Levy and Goldberg, 2014).
    3. What is x⋆x^\star for two words that never co-occur? Explain why the trained dot products stay finite nonetheless.

Discussions