ArticleslgStudy

computer science

Word2Vec

Word2Vec is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Word2Vec rather than just read about it. In short: In natural language processing, Word2Vec is a technique for obtaining vector representations of words as word embeddings. These vectors capture information about the meaning of a word based on its surrounding words in a piece of text, following the principles of distributional semantics.

Word2Vec — main illustration
Word2Vec — illustration

Key takeaways

  • Word2Vec belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Word2Vec to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Word2Vec from memory before moving on to harder problems.

Reference excerpt

In natural language processing, Word2Vec is a technique for obtaining vector representations of words as word embeddings. These vectors capture information about the meaning of a word based on its surrounding words in a piece of text, following the principles of distributional semantics. Once trained, the model can be used to find words with similar meanings or usage, while its embeddings can serve as inputs to systems for search and classification. Word2Vec was developed by Tomáš Mikolov, Kai Chen, Greg Corrado, Ilya Sutskever and Jeff Dean at Google, published in preprints and presented at ICLR in 2013. Its computational efficiency made it practical to learn high-quality word embeddings from very large text corpora and contributed to their widespread adoption in natural language processing.

Approach Word2Vec is a group of related models that are used to produce word embeddings. These models are shallow, two-layer neural networks that are trained to reconstruct linguistic contexts of words. Word2Vec takes as its input a large corpus of text and produces a mapping of the set of words to a vector space, typically of several hundred dimensions, with each unique word in the corpus being assigned a vector in the space. Word2Vec can use either of two model architectures to produce these distributed representations of words: continuous bag of words (CBOW) or continuously sliding skip-gram. In both architectures, Word2Vec considers both individual words and a sliding context window as it iterates over the corpus. The CBOW can be viewed as a "fill in the blank" task, where the word embedding represents the way the word influences the relative probabilities of other words in the context window. Words which are semantically similar should influence these probabilities in similar ways, because semantically similar words should be used in similar contexts. The order of context words does not influence prediction (bag of words assumption). In the continuous skip-gram architecture, the model uses the current word to predict the surrounding window of context words. The skip-gram architecture weighs nearby context words more heavily than more distant context words. According to the authors' note, CBOW is faster while skip-gram does a better job for infrequent words. After the model is trained, the learned word embeddings are positioned in the vector space such that words that share common contexts in the corpus — that is, words that are semantically and syntactically similar — are located close to one another in the space. More dissimilar words are located farther from one another in the space.

Mathematical details This section is based on expositions. A corpus is a sequence of words. Both CBOW and skip-gram are methods to learn one vector per word appearing in the corpus. Let V ("vocabulary") be the set of all words appearing in the corpus C. Our goal is to learn one vector vw∈ℝd for each word w∈V. The idea of skip-gram is that the vector of a word should be close to the vector of each of its neighbors. The idea of CBOW is that the vector-sum of a word's neighbors should be close to the vector of the word.

Continuous bag-of-words (CBOW)

The idea of CBOW is to represent each word with a vector, such that it is possible to predict a word using the sum of the vectors of its neighbors. Specifically, for each word wi in the corpus, the one-hot encoding of the word is used as the input to the neural network. The output of the neural network is a probability distribution over the dictionary, representing a prediction of individual words in the neighborhood of wi. The objective of training is to maximize ∑iln⁡Pr⁡(wi∣wi+j:j∈N) where N is a set of (non-zero) indices representing the relative locations of nearby words considered to be in wi's neighborhood. For example, if we want each word in the corpus to be predicted by every other word in a small span of 4 words. The set of relative indexes of neighbor words will be: N={−2,−1,+1,+2}, and the objective is to maximize∑iln⁡Pr⁡(wi∣wi−2,wi−1,wi+1,wi+2). In standard bag-of-words, a word's context is represented by a word-count (aka a word histogram) of its neighboring words. For example, the "sat" in "the cat sat on the mat" is represented as {"the": 2, "cat": 1, "on": 1}. Note that the last word "mat" is not used to represent "sat", because it is outside the neighborhood N={−2,−1,+1,+2}. In continuous bag-of-words, the histogram is multiplied by a matrix V to obtain a continuous representation of the word's context. The matrix V is also called a dictionary. Its columns are the word vectors. It has D columns, where D is the size of the dictionary. Let d be the length of each word vector. We have V∈ℝd×D. For example, multiplying the word histogram {"the": 2, "cat": 1, "on": 1} with V, we obtain 2vthe+vcat+von. This is then multiplied with another matrix V′ of shape ℝD×d. Each row of it is a word vector v′. This results in a vector of length D, one entry per dictionary entry. Then, apply the softmax to obtain a probability distribution over the dictionary. This system can be visualized as a neural network, similar in spirit to an autoencoder, of architecture linear-linear-softmax, as depicted in the diagram. The system is trained by gradient descent to minimize the cross-entropy loss. In full formula, the cross-entropy loss is:−∑iln⁡evwi′⋅(∑j∈Nvwj+i)∑w′evw′′⋅(∑j∈Nvw′j+i)where the outer summation ∑i is over the words in a corpus, the quantity ∑j∈Nvwj+i is the sum of a word's neighbors' vectors, etc. Once such a system is trained, we have two trained matrices V,V′. Either the column vectors of V or the row vectors of V′ can serve as the dictionary. For example, the word "sat" can be represented as either the "sat"-th column of V or the "sat"-th row of V′. It is also possible to simply define V′=V⊤, in which case there would no longer be a choice.

Skip-gram

… excerpt ends here. Continue reading the full article.

Illustrations

Word2Vec: Illustration of CBOW as a neural network
Illustration of CBOW as a neural network
Word2Vec: Skip-gram model for the sentence, "the man loves his son." The word "loves" is the target word used to predict similarity to its surrounding words.
Skip-gram model for the sentence, "the man loves his son." The word "loves" is the target word used to predict similarity to its surrounding words.
Word2Vec: Visual illustration of word embeddings
Visual illustration of word embeddings

Worked examples

Example 1 — a first encounter with Word2Vec

Start with the simplest possible case. Write down what Word2Vec claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Word2Vec before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Word2Vec ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Word2Vec

In research
Word2Vec appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Word2Vec in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Word2Vec is common in secondary-school and first-year university syllabi. It links to neighbouring topics 2013 in artificial intelligence, 2013 software, Artificial neural networks, so understanding it makes those chapters shorter.
In everyday life
Look for Word2Vec outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Word2Vec” →

How to study Word2Vec in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Word2Vec means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Word2Vec out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Word2Vec in simple terms?

In natural language processing, Word2Vec is a technique for obtaining vector representations of words as word embeddings. These vectors capture information about the meaning of a word based on its surrounding words in a piece of text, following the principles of distributional semantics.

Why does Word2Vec matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Word2Vec?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Word2Vec.

Tags

  • 2013 in artificial intelligence
  • 2013 software
  • Artificial neural networks
  • Free science software
  • Natural language processing toolkits
  • Semantic relations

Keep exploring