Alex Minnaar's Blog

Word2Vec Tutorial Part I: The Skip-Gram Model

12 Apr 2015

In many natural language processing tasks, words are often represented by their tf-idf scores. While these scores give us some idea of a word’s relative importance in a document, they do not give us any insight into its semantic meaning. Word2Vec is the name given to a class of neural network models that, given an unlabelled training corpus, produce a vector for each word in the corpus that encodes its semantic information. These vectors are usefull for two main reasons.

  1. We can measure the semantic similarity between two words are by calculating the cosine similarity between their corresponding word vectors.
  2. We can use these word vectors as features for various supervised NLP tasks such as document classification, named entity recognition, and sentiment analysis. The semantic information that is contained in these vectors make them powerful features for these tasks.

You may ask “how do we know that these vectors effectively capture the semantic meanings of the words?”. The answer is because the vectors adhere surprisingly well to our intuition. For instance, words that we know to be synonyms tend to have similar vectors in terms of cosine similarity and antonyms tend to have dissimilar vectors. Even more surprisingly, word vectors tend to obey the laws of analogy. For example, consider the analogy “Woman is to queen as man is to king”. It turns out that

where ,,, and are the word vectors for , , , and respectively. These observations strongly suggest that word vectors encode valuable semantic information about the words that they represent.

In this series of blog posts I will describe the two main Word2Vec models - the skip-gram model and the continuous bag-of-words model.

Both of these models are simple neural networks with one hidden layer. The word vectors are learned via backpropagation and stochastic gradient descent both of which I descibed in my previous Deep Learning Basics blog post.

The Skip-Gram Model

Before we define the skip-gram model, it would be instructive to understand the format of the training data that it accepts. The input of the skip-gram model is a single word and the output is the words in ’s context defined by a word window of size . For example, consider the sentence “I drove my car to the store”. A potential training instance could be the word “car” as an input and the words {“I”,”drove”,”my”,”to”,”the”,”store”} as outputs. All of these words are one-hot encoded meaning they are vectors of length $V$ (the size of the vocabulary) with a value of $1$ at the index corresponding to the word and zeros in all other indexes. As you can see, we are essentially creating training examples from plain text which means that we can have a virtually unlimited number of training examples at our disposal.

Forward Propagation

Now let’s define the skip-gram nerual network model as follows.

In the above model represents the one-hot encoded vector corresponding to the input word in the training instance and are the one-hot encoded vectors corresponding to the output words in the training instance. The matrix is the weight matrix between the input layer and hidden layer whose row represents the weights corresponding to the word in the vocabulary. This weight matrix is what we are interested in learning because it contains the vector encodings of all of the words in our vocabulary (as its rows). Each output word vector also has an associated output matrix . There is also a hidden layer consisting of nodes (the exact size of is a training parameter).

From my previous blog post, we know that the input to a unit in the hidden layer is simply the weighted sum of its inputs. Since the input vector is one-hot encoded, the weights coming from the nonzero element will be the only ones contributing to the hidden layer. Therefore, for the input with and for all the outputs of the hidden layer will be equivalent to the row of . Or mathematically,

Notice that there is no activation function used here. This is presumably because the inputs are bounded by the one-hot encoding. In the same way, the inputs to each of the output nodes is computed by the weighted sum of its inputs. Therefore, the input to the node of the output word is

However we can also observe that the output layers for each output word share the same weights therefore . We can finally compute the output of the node of the output word via the softmax function which produces a multinomial distribution

In plain english, this value is the probability that the output of the node of the output word is equal to the actual value of the index of the output vector (which is one-hot encoded).

Learning the Weights with Backpropagation and Stochastic Gradient Descent

Now that we know how inputs are propograted forward through the network to produce outputs, we can derive the error gradients necessary for the backpropagation algorithm which we will use to learn both $\mathbf{W}$ and . The first step in deriving the gradients is defining a loss function. This loss function will be

which is simply the probability of the output words (the words in the input word’s context) given the input word. Here, is the index of the output word. If we take the derivative with respect to we get

where if the node of the true output word is equal to (from its one-hot encoding), otherwise . This is the prediction error for node (or the node of the output word).

Now that we have the error derivative with respect to inputs of the final layer, we can derive the derivative with respect to the output matrix . Here we use the chain rule

Therefore the gradient descent update equation for the output matrix is

Now we can derive the update equation for the input-hidden layer weights in . Let’s start by computing the error derivative with respect to the hidden layer.

Now we are able to compute the derivative with respect to

and finally we arrive at our gradient descent equation for our input weights

As you can see, each gradient descent update requires a sum over the entire vocabulary which is computationally expensive. In practice, computation techniques such as hierarchical softmax and negative sampling are used to make this computation more efficient.

References