Word embedding

A word embedding represents a word as a vector, a list of numbers, placed so that words used in similar contexts end up close together, letting software compare meanings mathematically.

Computers cannot compare words as words, but they can compare numbers. Google's machine learning glossary describes a word embedding as representing each word in a vocabulary as an embedding vector, with words of similar meaning given more similar vectors. A typical word embedding has somewhere between 100 and 1,000 dimensions, and closeness between two vectors stands in for closeness in meaning.

The vectors are learned from text using an old linguistic idea, often traced to J. R. Firth: words that appear in similar contexts tend to have similar meanings. In January 2013 Tomas Mikolov, Kai Chen, Greg Corrado and Jeffrey Dean at Google described two architectures that became known as word2vec. One, continuous bag of words, predicts a word from its neighbours; the other, skip-gram, predicts the neighbours from the word. They reported learning high-quality vectors from a 1.6 billion word dataset in under a day. GloVe, from Stanford's Jeffrey Pennington, Richard Socher and Christopher Manning, followed at the EMNLP conference in October 2014; it trains on counts of how often pairs of words occur together across a whole corpus and scored 75% on a word analogy test.

The famous party trick is arithmetic on meaning. Take the vector for brother, subtract man and add woman, and the nearest word to the result is sister. Relationships such as gender or a country and its capital show up as rough directions in the space, which made embeddings useful for search, recommendations and document classification.

The same property carries social bias. A July 2016 paper by Tolga Bolukbasi and colleagues, titled Man is to Computer Programmer as Woman is to Homemaker?, showed that embeddings trained on Google News articles encoded gender stereotypes, for instance linking receptionist with women, and that the bias lay along an identifiable direction in the space. The authors proposed ways to reduce it while keeping useful associations such as queen with female.

Classic word embeddings give each word one fixed vector, whatever the context, so bank gets the same vector in a riverside walk as in a loan application. Contextual models fixed that: ELMo, built on recurrent networks, and the transformer-based BERT produce a different vector for each use of a word. Today's language models learn their own token embeddings as the first step of processing and then reshape them through many layers, and embedding models produce vectors for whole sentences and documents. Word2vec-style embeddings are now seen as dated, but they remain cheap, fast and easy to inspect, and the idea behind them underpins modern semantic search.

Common questions

What is a word embedding?

A word embedding is a list of numbers that represents a word, learned so that words used in similar ways get similar lists. This lets software measure how related two words are by comparing their vectors. Word2vec and GloVe are the best-known methods for learning them.

What is the difference between word2vec and GloVe?

Both produce one fixed vector per word. Word2vec, published by Google researchers in 2013, learns by predicting words from their neighbours or neighbours from a word as it moves through the text. GloVe, presented at EMNLP in 2014, learns from statistics of how often words appear together across the whole corpus.

What is the difference between word embeddings and contextual embeddings?

Word embeddings give each word a single vector regardless of the sentence it appears in. Contextual embeddings, produced by models such as ELMo and BERT, give a different vector each time a word is used, so the two meanings of a word like bank are told apart. Modern language models work with contextual representations.

Can word embeddings be biased?

Yes. Because they learn from human-written text, they absorb the associations in that text, including stereotypes. A 2016 study found embeddings trained on Google News linked occupations such as receptionist with women and proposed methods to reduce that bias.

Finding more

Search for "word2vec", "GloVe word vectors", "distributional hypothesis" or "bias in word embeddings" to explore further. The original papers on arXiv and in the ACL Anthology are the primary sources, and natural language processing course notes explain how the training works. For current practice, documentation from embedding model providers covers sentence and document embeddings, which have largely replaced word-level vectors.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.