No history yet

Introduction to Word Embeddings

Words as Numbers

Computers don't understand language the way we do. For a machine, text is just a sequence of characters. To perform tasks like translation, sentiment analysis, or even spell-checking, a computer needs to process words in a way it understands: with numbers.

This is where word embeddings come in. They are a way to represent words as numerical vectors. Instead of seeing the word "cat," a machine learning model sees a list of numbers, like [0.2, -0.4, 0.7, ...]. This vector captures the word's meaning, context, and relationships to other words.

Word embeddings are dense vector representations of words in a continuous vector space, where semantically similar words are mapped closer together.

Think of it like giving each word a set of coordinates on a map. Words with similar meanings are placed close to each other. On this map, "dog" would be near "puppy" and "cat," but far from "airplane" or "democracy."

The Old Way

Before word embeddings, a common method for converting words to numbers was one-hot encoding. It's a simple and straightforward approach. First, you build a vocabulary of all the unique words you have. Let's say our vocabulary is just four words: "king," "queen," "prince," and "princess."

To represent a word, you create a vector that's the same size as your vocabulary. This vector is all zeros, except for a single '1' at the position corresponding to that word.

For example:

  • king: [1, 0, 0, 0]
  • queen: [0, 1, 0, 0]
  • prince: [0, 0, 1, 0]
  • princess: [0, 0, 0, 1]

One-hot encoding is simple, but it has two major drawbacks. First, the vectors become enormous for large vocabularies. The English language has over 170,000 words in current use! Second, it treats every word as completely independent. The vector for "king" is no more similar to "queen" than it is to "car."

A Smarter Vector Space

Word embeddings solve these problems. Instead of huge, sparse vectors, they are dense, meaning most of the values are non-zero. They are also much smaller, typically having between 50 and 300 dimensions, regardless of vocabulary size.

Most importantly, they place words in a vector space where location and direction have meaning. The distance between two word vectors indicates how similar they are. Even more, the relationships between words can be captured through simple vector math.

The classic example of this is the relationship between royalty and gender. If you take the vector for "king," subtract the vector for "man," and add the vector for "woman," you'll get a vector that is very close to the one for "queen."

vec(king)vec(man)+vec(woman)vec(queen)\text{vec}(\text{king}) - \text{vec}(\text{man}) + \text{vec}(\text{woman}) \approx \text{vec}(\text{queen})

This ability to capture complex semantic relationships is what makes word embeddings so powerful. They allow machine learning models to work with language in a much more nuanced and sophisticated way, forming the foundation of modern natural language processing.

These meaningful representations are what allow models to understand that "Can you recommend a good book?" is more similar to "What should I read next?" than to "Can you book a flight?" It’s a foundational step towards machines truly understanding human language.

Quiz Questions 1/5

What is the primary purpose of a word embedding in Natural Language Processing?

Quiz Questions 2/5

Compared to one-hot encoded vectors, word embedding vectors are typically: