Skip to main content

Subsection 1.2 Word Embeddings

To represent words in a way that captures their meaning and relationships, we might try to define different attributes or features for each word. For example, we could define features such as "masculine" or "feminine" for the word "king", or "fast" or "slow" for the word "car". But doing this by hand is impractical for a large vocabulary.
So to create embeddings for words, we can turn to machine learning algorithms to automatically define the traits we will use to represent words and assign values for each particular word.
This section introduces how those embeddings are created and how they capture the semantic relationships between words.

Subsubsection 1.2.1 Word Embeddings and Meaning

This is a word embedding for the word "king" (using the GloVe algorithm trained on Wikipedia):
[ 0.50451 ,  0.68607 , -0.59517 , -0.022801,  0.60046 , -0.13498 ,
 -0.08813 ,  0.47377 , -0.61798 , -0.31012 , -0.076666,  1.493   ,
 -0.034189, -0.98173 ,  0.68229 ,  0.81722 , -0.51874 , -0.31503 ,
 -0.55809 ,  0.66421 ,  0.1961  , -0.13495 , -0.11476 , -0.30344 ,
  0.41177 , -2.223   , -1.0756  , -1.0783  , -0.34354 ,  0.33505 ,
  1.9927  , -0.04234 , -0.64319 ,  0.71125 ,  0.49159 ,  0.16754 ,
  0.34344 , -0.25663 , -0.8523  ,  0.1661  ,  0.40102 ,  1.1685  ,
 -1.0137  , -0.21585 , -0.15155 ,  0.78321 , -0.91241 , -1.6106  ,
 -0.64426 , -0.51042 ]
The embedding is a list of 50 numbers that range from -2 to 2. A visual representation makes comparison with other word vectors easier. The values can be arranged in a single row:
The 50 values of the word embedding for king arranged in a single row of numbered cells, with dimensions indexed from 0 to 49.
To make it easier to visualize the embedding, the cells can be color-coded by value: red near 2, white near 0, and blue near -2:
The same 50 values for king displayed as a heatmap: positive values are red, values near zero are white, and negative values are blue.
The colors indicate the values without displaying the numbers. This makes it easier to contrast "king" with other words:
Aligned heatmap rows for king, man, and woman. Man and woman have similar patterns across many dimensions, while king has a more distinct pattern.
See how "Man" and "Woman" are much more similar to each other than either of them is to "king"? This tells you something. The concepts of β€œMan” and β€œWoman”, despite being β€œopposites” in gender, are very similar overall. A β€œking” is a very different concept from β€œMan” and β€œWoman” despite being more closely associated with one of them.

Note 1.7.

We will rely on colors to make these comparisons. But keep in mind that a computer algorithm could use cosine_similarity to measure the similarity between two word vectors, and reach similar conclusions.
Here’s another list of examples (compare by vertically scanning the columns looking for columns with similar colors):
Aligned heatmaps for queen, woman, girl, boy, man, king, queen again, and water. Related pairs share color patterns; a blue column shared by the people words changes to red for water.
We can see some interesting patterns emerge from these visualizations that might tell us something about the words.
  1. A red column runs through all of these words, indicating similarity along that dimension, although the meaning of the dimension is unknown.
  2. You can see how "woman" and "girl" are similar to each other in a lot of places. The same with "man" and "boy"
  3. "boy" and "girl" also have places where they are similar to each other, but different from "woman" or "man". Could these be coding for a vague conception of youth?
  4. All but the last word represent people. The object "water" illustrates differences between categories. For example, a blue column runs through the person-related words but stops before "water".
  5. There are clear places where "king" and "queen" are similar to each other and distinct from all the others. Could these be coding for a vague concept of royalty?
Although it is tempting to interpret the meaning of each dimension, we must be cautious about over-interpreting the results. There is no dimension that maps directly to β€œgender” or β€œage”. A human didn’t design the 50 dimensions; they are just what a machine learning algorithm learned worked best to capture the relationships in the data it was trained on.
Despite the limitations of interpreting individual dimensions, word embeddings clearly capture some of the relationships between words. This can be seen in the way that adding and subtracting word vectors can reveal semantic relationships. A well-known example is "king" - "man" + "woman". If we take the 50 element vector for "king", subtract the 50 element vector for "man", and add the 50 element vector for "woman", we get a new 50 element vector. If we ask the question, "what word is most similar to the resulting vector?", we find that it is very similar to the vector for "queen".
Gensim results for king minus man plus woman rank queen first with cosine similarity 0.852, followed by throne at 0.766 and prince at 0.759.
Figure 1.8. The Gensim library in Python supports addition and subtraction of word vectors and identifies the words most similar to the resulting vector. The image lists these words with their cosine similarities.
The analogy can also be visualized using the same color representation:
Heatmap rows for king, man, woman, king minus man plus woman, and queen. The calculated vector has a color pattern similar to the queen vector.
Figure 1.9. The vector resulting from "king-man+woman" does not exactly equal "queen", but "queen" is the closest word among the 400,000 embeddings in this collection.