Word Embeddings in NLP


Word Embeddings in NLP

Anna
So we've been talking about bag-of-words and one-hot vectors, but they've got some serious flaws. Let's break down why.

Michael
Right. First, with one-hot vectors, every word is represented by a vector as long as the vocabulary. If you have 500,000 words, that's a huge vector for every single word—computationally wasteful.

Anna
And even worse, the distance between any two one-hot vectors is always root 2. So 'movie' and 'film' are just as far apart as 'good' and 'bad'. There's no notion of semantic similarity at all.

Michael
Exactly. That's the big motivation for word embeddings. We want vectors where similar words are close together and different words are far apart. Ideally, the geometry reflects meaning.

Anna
So how do we get those vectors? The key insight comes from the linguist John Firth: 'You shall know a word by the company it keeps.' Words that appear in similar contexts tend to be related.

Michael
That leads to the idea of a co-occurrence matrix. You take a huge corpus—say all of Wikipedia—and for every pair of words, count how often they appear in the same sentence. That captures context.

Anna
Then you want to learn embeddings that can reconstruct that co-occurrence matrix. That's the GloVe approach. The model is simple: the log of the co-occurrence count for words i and j is approximated by the sum of their bias terms plus the dot product of their embedding vectors.

Michael
So it's like a regression problem. You minimize the squared error between the actual log co-occurrence and your prediction. You can use gradient descent to learn the embeddings and biases.

Anna
And once you train those embeddings, they capture amazing relationships. For example, the vector for 'brother' minus 'man' plus 'woman' ends up close to 'sister'. That's word vector algebra.

Michael
But standalone embeddings have a limitation: they give one fixed vector per word. Words like 'bank' have multiple meanings—financial institution, river bank, to bank on something. The embedding averages all those uses, which can be muddy.

Anna
That's why we need contextual embeddings, where the representation of a word changes depending on the surrounding words. That's where transformers come in. But for now, GloVe is a great starting point.

Michael
And in practice, you can use pre-trained GloVe vectors if you have a small dataset. They capture general language knowledge. But if you have enough data, training your own embeddings from scratch for your specific task often works better.

Anna
Exactly. In the lecture demo, they tried three approaches: using frozen GloVe, fine-tuning GloVe, and learning embeddings from scratch. The last one gave the best accuracy for genre classification of song lyrics.

Michael
So the takeaway is: embeddings fix the flaws of one-hot vectors by capturing semantic relationships, but they still need context to handle polysemy. Next up, we'll see how transformers make embeddings truly contextual.

Ask anything
We've read this whole study set, so questions about it get the best answers.