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 based on the given center word 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 , 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 , the fact that any (context) word comes from this context window is considered as an event with the probability modeled by
where uses the definition of the sigmoid activation function:
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 , denote by the word at time step and let the context window size be , consider maximizing the joint probability
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 the event that a context word comes from the context window of a center word . For this event involving , from a predefined distribution sample noise words that are not from this context window. Denote by the event that a noise word () does not come from the context window of . Assume that these events involving both the positive example and negative examples are mutually independent. Negative sampling rewrites the joint probability (involving only positive examples) in (18.2.3) as
where the conditional probability is approximated through events :
Denote by and the indices of a word at time step of a text sequence and a noise word , respectively. The logarithmic loss with respect to the conditional probabilities in (18.2.5) is
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 . When setting the hyperparameter 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 .
Hierarchical softmax for approximate training, where each leaf node of the tree represents a word in the dictionary.
Denote by the number of nodes (including both ends) on the path from the root node to the leaf node representing word in the binary tree. Let be the node on this path, with its context word vector being . For example, in Figure 18.2.1. Hierarchical softmax approximates the conditional probability in (18.1.4) as
where function is defined in (18.2.2), and is the left child node of node : if is true, ; otherwise .
To illustrate, let's calculate the conditional probability of generating word given word in Figure 18.2.1. This requires dot products between the word vector of and non-leaf node vectors on the path (the path in bold in Figure 18.2.1) from the root to , which is traversed left, right, then left:
Since , it holds that the conditional probabilities of generating all the words in dictionary based on any word sum up to one:
Fortunately, since is on the order of due to the binary tree structure, when the dictionary size 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#
-
How can we sample noise words in negative sampling?
-
Verify that (18.2.8) holds.
-
How to train the continuous bag of words model using negative sampling and hierarchical softmax, respectively?
-
For a center word , a context word , and noise words , the negative sampling loss is .
- Show that and that the derivative of is .
- Assume that the noise words are distinct and differ from . Derive , , and . How many word vectors receive a nonzero gradient, and how many operations does computing all of these gradients take, in terms of and the dimension ?
- Now let a word be drawn several times among the noise words. Derive .
Adapted from Stanford CS224N, Assignment 2, Problems 1(e)–1(g).
-
Negative sampling does not fit the probabilities of (18.1.4). To find what it fits instead, let be the number of times word occurs in a context window of center word in the corpus, let and , and let be the number of (center word, context word) pairs. Treat as a free parameter for every pair of words.
- Show that the terms of the expected loss over the corpus that depend on are , and that they are minimized at .
- Let the noise distribution be . Show that is the pointwise mutual information minus (Levy and Goldberg, 2014).
- What is for two words that never co-occur? Explain why the trained dot products stay finite nonetheless.