{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Approximate Training\n",
"\n",
"Recall content of the last section. The core feature of the skip-gram model is the use of softmax operations to compute the conditional probability of generating context word $w_o$ based on the given central target word $w_c$.\n",
"\n",
"$$\\mathbb{P}(w_o \\mid w_c) = \\frac{\\text{exp}(\\mathbf{u}_o^\\top \\mathbf{v}_c)}{ \\sum_{i \\in \\mathcal{V}} \\text{exp}(\\mathbf{u}_i^\\top \\mathbf{v}_c)}.$$\n",
"\n",
"The logarithmic loss corresponding to the conditional probability is given as\n",
"\n",
"$$-\\log \\mathbb{P}(w_o \\mid w_c) =\n",
"-\\mathbf{u}_o^\\top \\mathbf{v}_c + \\log\\left(\\sum_{i \\in \\mathcal{V}} \\text{exp}(\\mathbf{u}_i^\\top \\mathbf{v}_c)\\right).$$\n",
"\n",
"\n",
"Because the softmax operation has considered that the context word could be any word in the dictionary $\\mathcal{V}$, the loss mentioned above actually includes the sum of the number of items in the dictionary size. From the last section, we know that for both the skip-gram model and CBOW model, because they both get the conditional probability using a softmax operation, the gradient computation for each step contains the sum of the number of items in the dictionary size. For larger dictionaries with hundreds of thousands or even millions of words, the overhead for computing each gradient may be too high. In order to reduce such computational complexity, we will introduce two approximate training methods in this section: negative sampling and hierarchical softmax. Since there is no major difference between the skip-gram model and the CBOW model, we will only use the skip-gram model as an example to introduce these two training methods in this section.\n",
"\n",
"\n",
"\n",
"## Negative Sampling\n",
"\n",
"Negative sampling modifies the original objective function. Given a context window for the central target word $w_c$, we will treat it as an event for context word $w_o$ to appear in the context window and compute the probability of this event from\n",
"\n",
"$$\\mathbb{P}(D=1\\mid w_c, w_o) = \\sigma(\\mathbf{u}_o^\\top \\mathbf{v}_c),$$\n",
"\n",
"Here, the $\\sigma$ function has the same definition as the sigmoid activation function:\n",
"\n",
"$$\\sigma(x) = \\frac{1}{1+\\exp(-x)}.$$\n",
"\n",
"We will first consider training the word vector by maximizing the joint probability of all events in the text sequence. Given a text sequence of length $T$, we assume that the word at time step $t$ is $w^{(t)}$ and the context window size is $m$. Now we consider maximizing the joint probability\n",
"\n",
"$$ \\prod_{t=1}^{T} \\prod_{-m \\leq j \\leq m,\\ j \\neq 0} \\mathbb{P}(D=1\\mid w^{(t)}, w^{(t+j)}).$$\n",
"\n",
"However, the events included in the model only consider positive examples. In this case, only when all the word vectors are equal and their values approach infinity can the joint probability above be maximized to 1. Obviously, such word vectors are meaningless. Negative sampling makes the objective function more meaningful by sampling with an addition of negative examples. Assume that event $P$ occurs when context word $w_o$ to appear in the context window of central target word $w_c$, and we sample $K$ words that do not appear in the context window according to the distribution $\\mathbb{P}(w)$ to act as noise words. We assume the event for noise word $w_k$($k=1, \\ldots, K$) to not appear in the context window of central target word $w_c$ is $N_k$. Suppose that events $P and N_1, \\ldots, N_K$ for both positive and negative examples are independent of each other. By considering negative sampling, we can rewrite the joint probability above, which only considers the positive examples, as\n",
"\n",
"\n",
"$$ \\prod_{t=1}^{T} \\prod_{-m \\leq j \\leq m,\\ j \\neq 0} \\mathbb{P}(w^{(t+j)} \\mid w^{(t)}),$$\n",
"\n",
"Here, the conditional probability is approximated to be\n",
"$$ \\mathbb{P}(w^{(t+j)} \\mid w^{(t)}) =\\mathbb{P}(D=1\\mid w^{(t)}, w^{(t+j)})\\prod_{k=1,\\ w_k \\sim \\mathbb{P}(w)}^K \\mathbb{P}(D=0\\mid w^{(t)}, w_k).$$\n",
"\n",
"\n",
"Let the text sequence index of word $w^{(t)}$ at time step $t$ be $i_t$ and $h_k$ for noise word $w_k$ in the dictionary. The logarithmic loss for the conditional probability above is\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
"-\\log\\mathbb{P}(w^{(t+j)} \\mid w^{(t)})\n",
"=& -\\log\\mathbb{P}(D=1\\mid w^{(t)}, w^{(t+j)}) - \\sum_{k=1,\\ w_k \\sim \\mathbb{P}(w)}^K \\log\\mathbb{P}(D=0\\mid w^{(t)}, w_k)\\\\\n",
"=&- \\log\\, \\sigma\\left(\\mathbf{u}_{i_{t+j}}^\\top \\mathbf{v}_{i_t}\\right) - \\sum_{k=1,\\ w_k \\sim \\mathbb{P}(w)}^K \\log\\left(1-\\sigma\\left(\\mathbf{u}_{h_k}^\\top \\mathbf{v}_{i_t}\\right)\\right)\\\\\n",
"=&- \\log\\, \\sigma\\left(\\mathbf{u}_{i_{t+j}}^\\top \\mathbf{v}_{i_t}\\right) - \\sum_{k=1,\\ w_k \\sim \\mathbb{P}(w)}^K \\log\\sigma\\left(-\\mathbf{u}_{h_k}^\\top \\mathbf{v}_{i_t}\\right).\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"Here, the gradient computation in each step of the training is no longer related to the dictionary size, but linearly related to $K$. When $K$ takes a smaller constant, the negative sampling has a lower computational overhead for each step.\n",
"\n",
"\n",
"## Hierarchical Softmax\n",
"\n",
"Hierarchical softmax is another type of approximate training method. It uses a binary tree for data structure, with the leaf nodes of the tree representing every word in the dictionary $\\mathcal{V}$.\n",
"\n",
"![Hierarchical Softmax. Each leaf node of the tree represents a word in the dictionary. ](../img/hi-softmax.svg)\n",
"\n",
"\n",
"We assume that $L(w)$ is the number of nodes on the path (including the root and leaf nodes) from the root node of the binary tree to the leaf node of word $w$. Let $n(w,j)$ be the $j$th node on this path, with the context word vector $\\mathbf{u}_{n(w,j)}$. We use Figure 12.3 as an example, so $L(w_3) = 4$. Hierarchical softmax will approximate the conditional probability in the skip-gram model as\n",
"\n",
"$$\\mathbb{P}(w_o \\mid w_c) = \\prod_{j=1}^{L(w_o)-1} \\sigma\\left( [\\![ n(w_o, j+1) = \\text{leftChild}(n(w_o,j)) ]\\!] \\cdot \\mathbf{u}_{n(w_o,j)}^\\top \\mathbf{v}_c\\right),$$\n",
"\n",
"Here the $\\sigma$ function has the same definition as the sigmoid activation function, and $\\text{leftChild}(n)$ is the left child node of node $n$. If $x$ is true, $[\\![x]\\!] = 1$; otherwise $[\\![x]\\!] = -1$.\n",
"Now, we will compute the conditional probability of generating word $w_3$ based on the given word $w_c$ in Figure 12.3. We need to find the inner product of word vector $\\mathbf{v}_c$ (for word $w_c$) and each non-leaf node vector on the path from the root node to $w_3$. Because, in the binary tree, the path from the root node to leaf node $w_3$ needs to be traversed left, right, and left again (the path with the bold line in Figure 12.3), we get\n",
"\n",
"$$\\mathbb{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).$$\n",
"\n",
"Because $\\sigma(x)+\\sigma(-x) = 1$, the condition that the sum of the conditional probability of any word generated based on the given central target word $w_c$ in dictionary $\\mathcal{V}$ be 1 will also suffice:\n",
"\n",
"$$\\sum_{w \\in \\mathcal{V}} \\mathbb{P}(w \\mid w_c) = 1.$$\n",
"\n",
"In addition, because the order of magnitude for $L(w_o)-1$ is $\\mathcal{O}(\\text{log}_2|\\mathcal{V}|)$, when the size of dictionary $\\mathcal{V}$ is large, the computational overhead for each step in the hierarchical softmax training is greatly reduced compared to situations where we do not use approximate training.\n",
"\n",
"## Summary\n",
"\n",
"* Negative sampling constructs the loss function by considering independent events that contain both positive and negative examples. The gradient computational overhead for each step in the training process is linearly related to the number of noise words we sample.\n",
"* Hierarchical softmax uses a binary tree and constructs the loss function based on the path from the root node to the leaf node. The gradient computational overhead for each step in the training process is related to the logarithm of the dictionary size.\n",
"\n",
"## Exercises\n",
"\n",
"* Before reading the next section, think about how we should sample noise words in negative sampling.\n",
"* What makes the last formula in this section hold?\n",
"* How can we apply negative sampling and hierarchical softmax in the skip-gram model?\n",
"\n",
"## Scan the QR Code to [Discuss](https://discuss.mxnet.io/t/2386)\n",
"\n",
"![](../img/qr_approx-training.svg)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}