Text Embeddings and Natural Language Processing

From Words to Vectors

Understanding how machines process and understand human language

Core Challenge: How do we convert human language into numerical representations that machines can understand and process?
"The quick brown fox" → [0.2, -0.1, 0.8, 0.3, ...]

The Text Representation Problem

Why We Need Embeddings

Fundamental Issues:
Traditional approach: "cat" = 1, "dog" = 2, "car" = 3
Problem: No notion of similarity between words

Solution: Map words/text to dense vector spaces where similarity has meaning

Bag of Words Representation

The Simplest Approach

Bag of Words: Represent text as a vector of word counts
Vocabulary: ["the", "cat", "sat", "on", "mat", "dog", "ran"]

"The cat sat on the mat" → [2, 1, 1, 1, 1, 0, 0]
"The dog ran" → [1, 0, 0, 0, 0, 1, 1]

Advantages:

Disadvantages:

TF-IDF: Weighted Word Importance

Term Frequency × Inverse Document Frequency

TF-IDF Formula: $$\text{TF-IDF}(t,d,D) = \text{TF}(t,d) \times \text{IDF}(t,D)$$ $$\text{TF}(t,d) = \frac{\text{count of } t \text{ in } d}{\text{total words in } d}$$ $$\text{IDF}(t,D) = \log\left(\frac{|D|}{|\{d \in D : t \in d\}|}\right)$$

Key Idea: Weight words by importance

"the" → Low IDF (appears everywhere)
"quantum" → High IDF (appears rarely)
"machine learning" → High TF in ML papers, High IDF overall

Word Embeddings

Dense Vector Representations

Key Insight: Map words to dense, low-dimensional vectors where semantic similarity is preserved
"king" → [0.2, -0.1, 0.8, 0.3, -0.5, ...]
"queen" → [0.3, -0.2, 0.7, 0.4, -0.4, ...]
"man" → [0.1, -0.3, 0.2, 0.8, -0.1, ...]
"woman" → [0.2, -0.4, 0.1, 0.9, -0.2, ...]

Properties of Good Embeddings:

Word2Vec

Learning Word Representations

Two Architectures:
Sentence: "The cat sat on the mat"

Skip-gram: "cat" → predict ["The", "sat"]
CBOW: ["The", "sat"] → predict "cat"
Training Objective: Maximize probability of observing context words $$P(w_{context} | w_{target}) = \frac{\exp(\mathbf{v}_{context} \cdot \mathbf{v}_{target})}{\sum_{w \in V} \exp(\mathbf{v}_w \cdot \mathbf{v}_{target})}$$

Key Innovations:

GloVe: Global Vectors

Combining Global and Local Statistics

Core Idea: Use global word co-occurrence statistics to learn embeddings
GloVe Objective: $$J = \sum_{i,j=1}^V f(X_{ij})(\mathbf{w}_i^T \mathbf{w}_j + b_i + b_j - \log X_{ij})^2$$

Where $X_{ij}$ is the co-occurrence count of words $i$ and $j$

Advantages over Word2Vec:

Co-occurrence matrix captures how often words appear together:
X["ice", "steam"] = low (rarely co-occur)
X["ice", "cold"] = high (frequently co-occur)

Contextual Embeddings

Beyond Static Word Representations

Problem with Static Embeddings: One vector per word type
"I deposited money in the bank" (financial institution)
"I sat by the river bank" (side of river)

Static embedding: "bank" always gets the same vector
Solution: Generate different embeddings based on context

Key Models:

Context 1: "bank" → [0.1, 0.8, -0.2, ...] (financial)
Context 2: "bank" → [0.7, -0.1, 0.5, ...] (river)

BERT and Transformer Embeddings

Attention-Based Representations

BERT: Bidirectional Encoder Representations from Transformers

Key Innovations:

BERT Training Tasks:
Input: "The [MASK] sat on the mat"
BERT learns to predict: "cat", "dog", "mouse", etc.

Sentence and Document Embeddings

Beyond Word-Level Representations

Challenge: How to represent entire sentences or documents?

Approaches:

"Machine learning is powerful" → [0.3, -0.1, 0.7, ...]
"AI can solve many problems" → [0.4, -0.2, 0.6, ...]
Similarity: cos(v1, v2) = 0.85 (high semantic similarity)
Applications: Semantic search, document clustering, paraphrase detection

Modern Language Model Embeddings

GPT, T5, and Beyond

Evolution: From task-specific to general-purpose embeddings

Modern Approaches:

OpenAI text-embedding-ada-002:
Input: "Find me documents about climate change"
Embedding: Captures intent, semantics, and domain

Applications of Text Embeddings

Real-World Use Cases

Core Applications:
Example: Semantic Search
Query: "How to bake bread?"
Matches: "Bread making tutorial", "Baking recipes", "Yeast fermentation"
(Even without exact keyword matches)

Choosing the Right Embedding

Trade-offs and Considerations

Key Factors:
Decision Framework:

Key Takeaways

Text Embeddings Landscape

Practical Advice:
"Understanding text embeddings opens the door to modern NLP applications"
Slide 1 of 14