Embeddings

Nlp tutorial · PySpark.in

Embeddings

Embeddings are dense numerical representations of text (words, sentences, or documents) as vectors in a continuous high-dimensional space, where semantic similarity is captured by geometric proximity. Raw text can't be fed directly into machine learning models it needs to be converted to numbers. Embeddings solve this by mapping words/tokens to vectors such that similar meanings to similar vectors.

For example:

Why Not Just One-Hot Encoding?

One-hot encoding gives each word a sparse binary vector (e.g., 50,000 dimensions for a vocabulary of 50k words). Problems:

Embeddings fix this by being dense (e.g., 300 dimensions) and meaningful.

Types of Embeddings

Word-level (static)

Contextual embeddings (same word, different vector based on context)

Sentence/Document embeddings

How Word2Vec Works (Intuition)

Two main training approaches:

The model is trained on massive text corpora, and the weight matrices learned during training become the embeddings.

Practical Uses

Modern Context

In today's LLMs (like GPT-4, Claude), embeddings are generated internally at every layer of the transformer. The input tokens are first converted to embeddings via an embedding lookup table, then refined through attention layers to become rich contextual representations.

More Nlp tutorials

All tutorials · Try the free PySpark compiler · Practice challenges