What are embeddings in machine learning?
An embedding is a learned vector of numbers that represents a discrete item, such as a word, a product, or a user, so that similar items get similar vectors. Instead of a sparse one-hot code with one slot per item, each item gets a short dense vector, usually with a few hundred dimensions. Distances and directions between these vectors carry meaning that the model learned from data.
Embeddings are the input layer of almost every language model, where each token is looked up as a vector before any other processing. They also power semantic search and retrieval-augmented generation, recommendation systems that match users to products, and image search that compares photos with text. Word2vec (2013) made word embeddings widely used, and the same idea now applies to sentences, images, code, and graph nodes.
Key ideas
- An embedding maps each discrete item, such as a word, to a short dense vector of learned numbers.
- One-hot vectors are sparse and treat every pair of words as equally unrelated.
- An embedding layer is a weight matrix, and looking up a row equals multiplying by a one-hot vector.
- Words used in similar contexts get similar vectors, so cosine similarity tracks relatedness.
- Directions in the space can encode relationships, as in king − man + woman ≈ queen.
- The same idea embeds products, users, graph nodes, sentences, and images.
Common questions
What is the difference between word2vec and transformer embeddings?
Word2vec gives each word one fixed vector, so "bank" has the same embedding in "river bank" and "bank account". A Transformer starts from a fixed token embedding but then mixes in the surrounding words, so its output vectors are contextual. Models such as BERT give each occurrence of a word its own vector.
How many dimensions should an embedding have?
Classic word vectors use 100 to 300 dimensions. Language models use 768 to several thousand, and sentence embedding models commonly output 384 to 3,072. More dimensions can store finer distinctions but cost memory and need more data to train well.
Should embeddings be trained from scratch or pretrained?
With little data, starting from pretrained vectors such as GloVe or a pretrained language model usually works better. The embeddings can then be frozen or fine-tuned with the rest of the network. Training from scratch makes sense for items with no pretrained vectors, such as product IDs in a recommender.
What is an embedding model?
An embedding model is a network trained to turn a whole input, such as a sentence or an image, into one vector for comparison. These vectors are stored in a vector database for semantic search and retrieval-augmented generation. They are usually trained with contrastive learning, which pulls matching pairs together and pushes unrelated pairs apart.
Related explainers
- Word2Vec & Word Embeddings — Learn dense vector representations where similar words live near each other in space
- CBOW (Continuous Bag of Words) — Predict the center word from its context — the averaging architecture behind Word2Vec
- Skip-gram — Predict context words from a center word — the architecture that powers Word2Vec with negative sampling
- Norms & Distance Metrics — L1, L2, cosine similarity — measuring closeness in vector spaces
- Retrieval-Augmented Generation — Ground language model outputs in retrieved documents to reduce hallucination
- Tokenization & BPE — How text becomes numbers — from characters to subword tokens via byte-pair encoding
References
- Mikolov et al. (2013), "Efficient Estimation of Word Representations in Vector Space"
- Mikolov et al. (2013), "Distributed Representations of Words and Phrases and their Compositionality"
- Pennington, Socher & Manning (2014), "GloVe: Global Vectors for Word Representation", EMNLP
- Grover & Leskovec (2016), "node2vec: Scalable Feature Learning for Networks"
- Bolukbasi et al. (2016), "Man is to Computer Programmer as Woman is to Homemaker? Debiasing Word Embeddings"
Last updated