Chapter 167
Approximate Training
Approximate Training
Recall our discussions in sec_word2vec. 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 eq_skip-gram-softmax, whose corresponding logarithmic loss is given by the opposite of eq_skip-gram-log.
Due to the nature of the softmax operation, since a context word may be anyone in the dictionary , the opposite of eq_skip-gram-log contains the summation of items as many as the entire size of the vocabulary. Consequently, the gradient calculation for the skip-gram model in eq_skip-gram-grad and that for the continuous bag-of-words model in eq_cbow-gradient 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.
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 us 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, eq-negative-sample-pos only considers those events that involve positive examples. As a result, the joint probability in eq-negative-sample-pos 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 eq-negative-sample-pos 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 eq-negative-sample-conditional-prob 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.
Hierarchical Softmax
As an alternative approximate training method, hierarchical softmax uses the binary tree, a data structure illustrated in fig_hi_softmax, where each leaf node of the tree represents a word in 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 fig_hi_softmax. Hierarchical softmax approximates the conditional probability in eq_skip-gram-softmax as
where function is defined in eq_sigma-f, and is the left child node of node : if is true, ; otherwise .
To illustrate, let us calculate the conditional probability of generating word given word in fig_hi_softmax. This requires dot products between the word vector of and non-leaf node vectors on the path (the path in bold in fig_hi_softmax) 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.
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.
Exercises
- How can we sample noise words in negative sampling?
- Verify that eq_hi-softmax-sum-one holds.
- How to train the continuous bag of words model using negative sampling and hierarchical softmax, respectively?
