1. Home
  2. Explore
  3. Computer Science
  4. Word Embeddings in NLP

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Word Embeddings in NLP

Transcript

[SQUEAKING] [RUSTLING] [CLICKING] RAMA RAMAKRISHNAN: We'll continue our journey with natural language processing. We looked at the bag of words model, one-hot embeddings, and so on and so forth. And today, we will talk about embeddings, or to be more precise, standalone embeddings. And then that will tee us up for something called contextual embeddings, which is where the transformer really comes into play. All right. So let's get going. So FR, we have encoded input text with one-hot vector.

So to just to refresh your memories from Monday, if this is the phrase that's coming into the system, we run it through the STIE process. And when we do that, what happens is that we first of all, we standardize, then we split on whitespace to get individual words. Then we assign words to integers, and then we take each integer and essentially create a one-hot version of that integer. And when we do that, basically, we have a vocabulary, right? And in this example, we just have 100 words.

And you will note that this vocabulary which you arrive at once you standardize and tokenize has words like the because we decided not to remove stop words like a, and, the, and so on. So just to be clear, standardization-- here, standardization-- while it has historically been all about stripping punctuation, lowercasing everything, removing stopwords, and stemming-- while that has been true historically, if you look at modern practice, people essentially

strip punctuation, maybe, and then lowercase, and they often don't even bother to do stemming and things like that, or to remove stop words. And that's why in Keras, the default standardization is only lowercasing. And punctuation stripping. This detail may actually be handy for homework too, perhaps. That's why I'm pointing it out. OK, so that's what we have. And so for each word that's coming in, we have a one-hot vector. The one-hot vector is just onto the vocabulary.

And we can either add them up and get a multi-hot encoding, or we can-- sorry-- get a count encoding, or we can just do or. Look for just any ones in a column, and get multi-hot encoding. So that's what we saw last class. But this scheme, while it's quite effective for simple problems, it has some very serious shortcomings. And so we will delve into those shortcomings and then step back and say, all right, is there a solution to fix these things? OK. Explain the problem with one-hot vectors.

There are lots of problems. Any volunteers? AUDIENCE: So similar words are understood differently. So for example, if you have this house is great and this house is awesome, great and awesome is encoded differently. And obviously, differently, even though this [INAUDIBLE]. RAMA RAMAKRISHNAN: Absolutely. So what he's pointing out is that if you have two words which are synonyms-- let's say great and awesome-- we would hope that the way we represent them using these vectors would have some connection to what the words actually mean.

In particular, we would hope that if they mean similar things, that they are close by. If they mean very different things, we would hope that they are very far away. Things like that, common sensical expectations of what you want the vectors to have. So it clearly won't have that, and we'll look into it in more detail in a bit. But before we do that, there is also a computational issue, which we covered last class. Which is that if the vocabulary is really long, then each token, each word that's coming in here will have a one-hot vector that's as long as the size of the vocabulary. If you have 500,000 words in your vocabulary, every little word that comes in has a vector which is 500,000 long, which feels like a gross waste of stuff.

Now, you can mitigate it somewhat by choosing only the most frequent words. But it does increase the number of weights the model has to learn, and increase the need for compute, and data, and so on and so forth. Now, let's say that we have created a vocabulary from a training corpus. We have a bunch of text that's coming in. We have done the SD. The standardization, tokenization, we've created vocabulary from it. And let's say we get the words movie and film. So the question is-- and O's observation gets to this immediately. If you look at the words movie and film, are these two vectors close to each other or not? So if you have two vectors, how would we measure closeness? What's the simplest way to think about closeness?

It's not a trick question. Distance. Yeah exactly. So if they're really close, distance-wise, you would hope the similar words should be close by. So here, let's just imagine that the vector for movie-- let's say your vocabulary is, I don't know, 100,000 long. So your vector is 100,000 long, and the word for movie is the position. So this has a 1, everything else is 0. Right?

Sorry, this is the vector for film, and maybe this is the position for film. So that has a 1. Everything else here-- 0. What is the distance between these two vectors? You just use the Euclidean distance. So the Euclidean distance, you will recall, you literally just take the difference of these values, square them, add them up, take the square root. So which means that all the zeros will obviously give you 0. This 1 is going to give you a 1. This comparison is going to give you another 1.

1 plus 1-- 2. Root 2. That's the answer. So this is between these two vectors, is root 2. Now, so the distance between them is root 2. What about the one-hot encoded vectors for good and bad? Clearly, good and bad mean opposite things. What is the distance between the good and bad 0, 1 vectors? Still root 2. Because the zeros don't mean anything, the ones are not in the same place. So when you subtract the 1 and the 0, you'll get ones and ones. Add them up to root 2. In fact, you take any two words in your vocabulary. What's the distance between the two one-hot vectors for those words? It's root 2. So if any two words have the same distance, does this even have a notion of distance? It doesn't. There's no notion of distance from one-hot vectors.

It has no connection to the actual meanings of these words. It's just a way of representing them. So that is the big problem with one-hot vectors. So the distance between them is the same, regardless of the words. It's got nothing to do with the meaning of the words. And this is a huge problem, which we'll have to solve. So to summarize where we are, if the vocabulary is very long, each token will have a one-hot vector that's long as vocabulary. That's a computational and training problem.

And then this is a deeper problem where there's no connection between the meaning of a word and its vector. So wouldn't it be nice if vectors that represent synonyms-- movie and film, apple-banana-- hopefully, they are close to each other. Wouldn't it be nice if the vectors for things that mean very different things were far from each other? So let's take a look at a particular example. OK? Let's assume that we have been magically given these vectors so that they actually have some notion of meaning. And for convenience, let's say that we take just the first two dimensions of these vectors-- the first two dimensions-- so that we can do a scatter plot on them. So we plot the first dimension of these vectors, the second dimension. And what we have in this little cartoon is we have plotted the word for factory, for home, for building, and they all happen to be clustered here. Clearly, this representation is capturing some notion

of what the thing is, some sort of building And here, we have, a bicycle, truck, and car. Clearly, this is the automobile cluster, transportation cluster. And here, we have a fruit cluster. And here, we have sports balls cluster. OK? Because it's a cartoon, things are all nice and cleanly separated. So now, if you take the word apple, where do you think it's going to go? It's going to go into A, C, D, or B? C right? It makes eminent sense it's going to go to C. Good.

Now wouldn't it be nice if, more generally, if the geometric relationship between word vectors represents a semantic relationship between the underlying objects that the words represent? And I say relationship and not distance because it's not just distance. It's actually more than that. So let's look another one. Here, we have-- this is the vector plotted for puppy and dog, and this is calf. We have plotted the word for calf. And let's say that we need to figure out where would the embedding-- the word vector for cow appear? Where is the most logical? Should it be A? Should it be C? Should it be B? Where should it be? AUDIENCE: This is C. RAMA RAMAKRISHNAN: C? OK, what's the logic? Any volunteers? Just put your hand up. Yes?

AUDIENCE: A calf is a baby animal, whereas the cow is probably the adult. So it'd be closer to dog, which is B-- the adult version of the dog. RAMA RAMAKRISHNAN: Got it. So you're basically saying, go from the puppy version to the grown up version. That's what you're getting at, right? And that's a totally valid way to think about it. But there are a couple of ways to think about this-- which, this is one of those two ways. So what you can do is you can actually look at it and say, well, OK. If this is bringing you bad memories of GMAT and GRE and stuff like that, I apologize.

So a puppy is to a dog like a calf is to a cow, right? Which means that there's exactly what Jay is pointing out. You can go from the baby version to the full grown version if you go in the horizontal direction. But maybe, if you go in the vertical direction, you're essentially going up and down the young entities of animals. So here, you're growing with-- you're still across the same dimension of animals. You're just going from the age level. That is the band here.

So this is the grown up version of a whole bunch of animals, the puppy version of a whole bunch of animals. So the vertical dimension measures some variation across animal species of the same, roughly, maturity stage. So these directions also matter. It's not just the distance. That's what I mean when I say semantic relationship and geometric relationship. The relationship is distance and direction. Both have to be involved. So now, word embeddings-- as we will learn soon-- are word vectors designed to achieve exactly these requirements. They will achieve these requirements, and they will fix both these problems very elegantly.

So let's say that we have word embeddings that achieve both these problems. Are we basically done? Can we declare victory? Or is there anything that even words which actually capture the meaning of the underlying thing don't fully address? Is there any remaining problem we have to worry about? Yes? AUDIENCE: Context. RAMA RAMAKRISHNAN: Context? Yes. Context right? The fact is a word's meaning-- sure, every word has a meaning. But we know that some words have multiple meanings,

and that meaning is really inferrential or you can make sense of it-- only if you know the surrounding context. If you see the word bank, B-A-N-K-- sure, it could be a financial institution. It could be the side of a river. It could be the act of a plane turning in one direction. It could be someone hoping for something, banking on something. The list of possible meanings of the word bank is basically enormous, and you cannot figure out what it means unless you

know what else is going on around that word. So context is super, super important. And these word embeddings just tell you what the meaning of the word is. And basically, what's going to happen when you have a word which could mean many different things, it's going to give you some average version of that meaning. The average version is not going to be very good. There are some words which only mean one thing, and you'll be OK there. But for the rest of it, it's going to be tough. So what we need is some way--

you need to find a way to make word embeddings contextual. Meaning, we need to somehow consider the other words in the sentence. So if we can do that, then we will be in great shape to solve all sorts of NLP problems. So as it turns out, contextual word embeddings or word vectors or word embeddings will achieve both these requirements. They capture the semantic geometric relationship thing I talked about, and they are contextual. They're really fantastic.

And the key to calculating contextual word embeddings is the transformer. That is why transformers are justifiably famous. That's the lay of the land here. So today, we're going to look at how to calculate standalone or uncontextual word embeddings. And then starting Monday, we will take these un-standalone embeddings and make them contextual using transformers. That is the plan. Any questions so far? So now, let's think about how we can learn these standalone embeddings from data. Now, the naive way to think about it would be, hey, why don't we manually collect a whole bunch of synonyms, antonyms, related words, et cetera and try to assign embedding vectors to them that satisfy our requirements? Now as you can imagine, there's going to be a long, painful, and never quite complete exercise.

And given that we are machine learning people, question is, can we do it in a better way? Can we just learn it from the data without doing any of this manual stuff? And the key insight that makes it all happen is this humble looking line on the screen by John Firth, who was a linguist. "You shall know a word by the company it keeps." I wish I could deliver this in a British accent. "You shall know a word by the company it keeps." It's a very profound statement.

And here is the key intuition behind this. It says, let's say that you have a sentence like, "the acting in the dash was superb." What are some words that you folks think are likely to appear in the sentence? Shout it out. Play movie show musical. Those are all some great candidates. The acting in the movie, the film, musical, and so on and so forth. Now, let's say that I ask you, what are some words that are unlikely to appear in the sentence? And I think we could all be here for days listing them out. I just listed these out. I love the word tensor, so I have to find a way to use it somewhere. All right. So the acting of the banana was superb. Clearly nonsensical.

So what we are seeing here is that if certain words are interchangeable in a sentence-- meaning, you change them, and the sentence still makes sense-- if they appear in the same context very often, i.e. if they're interchangeable, they are probably related. Sort of like, we don't even have to know what the word is. All we have to know is that this word and this word, you can drop them into a particular sentence. You can fill in the blank of that sentence with that word, and it actually makes sense. Then you're like-- oh, wow. OK. These words are related then. You're inferring their relatedness, not by looking at them directly, but by seeing where they live. It's a very, very clever idea, and it'll slowly sink into you. So that's the first observation. If they appear in the same context very often, they are likely to be related. Generally, related words appear in related contexts.

So all we have to do is to figure out a way to calculate context, and then use that to understand what the words are that happen to live in these contexts. And there are some beautiful ways to do these things, and we'll and we'll really dive deep into one such way to do it. So what we're going to do in this approach is that since words that appear in related contexts mean same similar things, first of all, I have to define what do you mean by context? There are many ways to define context.

We're going to go with a very simple explanation, a simple definition. Which is that if words happen to appear in the same sentence a lot, let me think that they are in the same context. So context here means sentence. So what we can do is we can actually take a whole bunch of text-- maybe all of Wikipedia-- and then break it up into sentences. We'll have billions of sentences. And then for all these billion sentences, we can literally go and count for every pair of words how many times are both these words showing up in the same sentence.

And we call this co-occurrence. The words are co-occurring in the sentence. And it doesn't have to be next to each other. We know that in complicated words, a word at the very end of the sentence could actually alter the meaning Its meaning could be altered by a word that happened in the very beginning of the sentence, and it could be a really long sentence. So we take the whole sentence and say, are two words co-occurring in the sentence? Yes or no? And we just count them up. And when we do that, we will get something like this. So this just captures what I've been talking about. Identify all the words that occur, let's say, in Wikipedia. And then for every sentence, you look at every word pair and count the number of times they appear in the same sentence across all those sentences. This is a word-word co-occurrence matrix. So for example, let's assume that you took all of Wikipedia, looked at all the words, distinct words, and you found there were 500,000 words.

So there are 500,000 words here in the columns. 500,000 words on the rows. So columns and rows. And then you go, and each cell of this table basically has a number that you calculate which is the number of times the word in the row and the word in the column happened to show up in the same sentence. That's it. So for instance, if you look at deep and learning-- the word deep and the word learning-- maybe those two words occurred in the same sentence, maybe 3,025 times.

3,025 sentences across all of Wikipedia. You put 3,025 right in that cell. Many words are unlikely to appear in the same sentence. So much of this matrix is going to be 0. But we fundamentally form this co-occurrence matrix. This matrix essentially embodies all the context information that we can work with in a very compact, beautiful, elegant way.

And using this, we're going to try to figure out what the word embeddings actually are going to be. So by the way, the approach I'm describing here to calculate standalone embeddings is called GloVe. It's called GloVe. And standalone embeddings first came on to the NLP deep learning scene-- there were two ways of doing it. One was called word2vec. Word2vec. The other one is GloVe. And they are both comparable. They use slightly different mechanisms of doing this. We went with word for this lecture because I think it's actually a little easier to understand, and equally effective. So this is what we have. And so what we want to do is we want to learn these embedding vectors that

can be used to essentially approximate this matrix. If we can find vectors that can actually approximate this matrix, then hopefully those vectors do, in fact, capture some notion of what the words actually mean. So let me put it differently. You come to me with this matrix. And you say, OK. Rama, do you have embeddings for me. And I'm like, yeah. I reach into my bag, and I'm like, OK. Every one of those 500,000 words, I have an embedding. Let's ignore for a moment how I actually calculated the embeddings. I have the embeddings. How will you know if my embeddings are any good? How will you know? How can you actually assess if the embeddings are any good? Well, you can certainly say, OK, give me the embeddings for movie and film, and you can see if they're really close by. If you look at the embedding for movie and tensor-- and hopefully they'll far away. But you'll never get done, right? How can you systematically evaluate this?

Well, what if you could actually-- what if I come to you and say, not only am I going to give you an embedding, here is a procedure which you can use with these embeddings to validate how good they are. And here is the procedure. What you can do is you can use the embedding to recreate the co-occurrence matrix. And if the recreated co-occurrence matrix actually matches the real matrix, well, these embeddings probably are pretty good. Remember, the whole point of the co-occurrence is to handle this context information. So if the embeddings can actually recreate them, reconstruct them pretty close, right? It will never be perfect. But if it comes pretty close, they'll be like, wow, OK. These embeddings do mean something. So if it turns out, for instance, that the matrix has 3,000 possible value of 3,000 for deep end learning and values of, say, 50 for extreme learning-- under embedding comes in and says 3,002 for the first one and 48 for the second one, we'll be like,

we'll be pretty impressed. [INAUDIBLE] needed to be that close unless it was actually capturing something. So that's what we're going to do. And so we're going to take this logic of saying find embeddings that can approximate what we actually see in Wikipedia. And we're going to use that idea to actually build a model and learn from it using nothing more than basically linear regression. And here, you are thinking the linear regression is useless now that you've graduated to machine learning, right?

So we can think of the embedding vectors that we want to figure out as just the weights in a model in a little regression. We can think of the co-occurrence matrix as just the data we're going to use in this model to estimate these weights. And the model we're going to use is something like this. So first, I have to inflict some notation on you. We will denote the co-occurrence matrix of, say, words I and J as Xij. Xij is just data. It's just data, OK? It's not a variable, it's data.

And then we'll denote an embedding vector for each word. So we're going to calculate a vector for each word. So we'll call it wi. wi is the embedding vector for each word. And we will also assume that some words are just inherently very popular. They're going to show up all the time, like the word the. So we'll assume that every word has some natural frequency of occurring, like movie versus flick, the versus tensor. So we want the vectors to capture the co-occurrence patterns independent of how naturally frequent the words are. And so to capture this natural frequency, we will assign a bias, or bi, to each word that we're going to calculate. And all this will become clear in just a moment. So with this setup, basically what we're saying is something very simple. We're saying, look, this co-occurrence matrix that we have that we're able to compute-- it came about because in truth, in reality, in nature,

there are these embedding vectors for every word. There are these biases, bi, by for every word. And every co-occurrence number that you see just came about because under the hood, mother nature grabbed the bias number for the word i, the bias number for the word j, took the two embedding vectors which only mother nature knows at this point, did the dot product of them, added them, and that's how we get this number. So it basically says the number you see is the sum of the inherent popularity of the first word, plus the inherent popularity of the second word, plus the way in which these two words connect to each other. That's it. And you will agree with me that it literally can't get simpler than this. If I tell you, hey, here are two things. I want you to tell me how connected they are. You'll be like, well, let's take the first one. Figure out how inherently popular it is, inherent popularity.

And then, of course, you to worry about the connection, so do a dot product. That's it. Those three things. So this is what we have. Now you may have seen from your good old linear regression that whenever your dependent variable happens to be positive-- guaranteed to be positive-- and it ends up having a big range, we always advise your folks to take the logarithmic transformation to squash it into a narrow range because that will make these models much more well-behaved.

Regression if the y value is like a huge range. The canonical example is that if you are trying to model the net worth of people, it's going to have a long right tail with people like Elon and Jeff and so on the right side, and the rest of us on the left. So to model this big, long tail distribution, you just take the logarithm, just squash everything to a very narrow range. And that will make regression much better behaved. Here, most of the counts are going to be 0, but some of the counts could be very high. And therefore, we wanted to-- if you take the logarithm, it makes it much better behaved. So take the logarithm here. So this is actually our model. That's it. And I know that many of the numbers are 0, and log of 0 is not defined, so we can just add the number 1 to all the numbers to avoid that kind of technical arithmetic problems. But this, conceptually, is what's going on. This is the model we want to calculate. So given that we have essentially

postulated this model and we have this data, this co-occurrence matrix, how can we actually find the weights? How can we actually find the b's and the w's? What will what should we do? Go back to the fundamentals of regression. Think about it conceptually. You have some model, which has some weights. There's some data you can use to train the model. And you need to find the best set of weights. What does the best mean here? AUDIENCE: Probably the lowest error. RAMA RAMAKRISHNAN: The lowest error, exactly. There are many ways to measure error. What is the simplest thing we could use? So what you would do is you would actually do mean squared error, which is what you're getting at. You could take the actual thing, you could take the predicted thing, take the difference and square it, and minimize the sum of it. If your model exactly nails every number in the co-occurrence matrix, the rest is going to be 0. So what we do is we literally just do that.

This is the data. This is the actual predicted value. Predicted value, actual value, difference squared, add them all up, minimize. OK? Yes? AUDIENCE: I'm kind of lost where how is this capturing the context? Because unless my input data is having that context, how will this actually differentiate based on where the particular word is used? RAMA RAMAKRISHNAN: The way the word is-- so let's take two words, like deep and learning. AUDIENCE: No, let's take one word and change it according to the final. RAMA RAMAKRISHNAN: OK. Sorry, go ahead. AUDIENCE: Yeah, so basically let's say I have a banana. So it's a fruit in some context. And I could be saying, he's going bananas. So whatever, right? So now these are two different contexts in my understanding. And my same model needs to be able to tell me that banana is the right word in this context, but wrong word in this context, or correct in both contexts. RAMA RAMAKRISHNAN: Yeah. Very good question.

So let's actually spend a minute on that. Good question. I'm going to swap to my iPad. So let's assume that this is our co-occurrence matrix. And then we have words going from all the way to-- let's say zebra. These are all the words in the vocabulary, and we have a through zebra here. And now what we have is we have apple and banana.

So basically what's going on at this point is that here, every number here measures for every word here, how many times that word and apple show up in the same sentence. It is not measuring, to your point, how many times apple and banana are showing up. It's measuring how many times apple is showing up in each sentence. Now, if apple and banana are interchangeable, what do we expect these two rows of numbers to look like? Let's assume that apple and banana are perfect synonyms,

just for argument. Let's say it's a perfect synonyms. What do we expect these two numbers to look like? Very similar. So if two words are related, their entry row vectors, the co-occurrence matrix are going to be very, very similar. So that is how the context comes into the co-occurrence matrix. So what we want to do is we want to find if embeddings can recreate the same pattern of numbers in these two rows, it's actually capturing the underlying context.

So words which are similar will zig and zag together the same way through the co-occurrence matrix, and that's where it comes in. yeah AUDIENCE: What's on the diagonal of the co-occurrence matrix? Like, if apple shows up twice? RAMA RAMAKRISHNAN: Oh, I see. So here, you can just ignore the diagonal, typically, because all the action is off diagonal entries. So that's basically the idea. And if words which are very similar will have a very similar pattern of numbers, and then any embeddings that can actually recreate the same pattern of numbers is capturing the underlying reality of what's going on. If words are unrelated, those two vectors-- let's say that the word you have is--

let's assume the word is-- you know what I'm going to say. Tensor. These two vectors won't have any connection to each other. Which means if you look at something like the correlation of those two vectors, it's going to be around zero. But words which are interchangeable will have a very high correlation. Words which are antonyms and never show up in the same place together may have a highly negative correlation, close to minus 1, for instance. So that's the intuition behind what's going on on these row vectors. And so the point is given this co-occurrence matrix is capturing all these word-word correlational structure, any embedding that can recreate it must have captured the structure as well because you can't recreate something like this with great fidelity unless you have some notion of what's going on under the hood. That's the basic idea. Yeah? AUDIENCE: So just connecting just from this question-- so in that example then,

banana is a fruit and apple is a fruit as well. Banana and apple are synonyms. And you are going mad or you are going bananas, and that comes together? Is that how we read it? RAMA RAMAKRISHNAN: Oh, I see. You're going mad. You're going bananas. Yeah, so those will also have some correlation structure to it, which the embeddings will hopefully catch. But words like banana, which are very-- the thing is it's called polysemy, where the word looks one way. It looks the same. It's like the word bank, right? It can mean very different things in many different contexts. So the embedding is going to be some average representation of it. But we're not happy with that average, and we will get around that average next week when we do contextual stuff. All right. So that's what we have here. So to go back to this thing--

so what we can do is-- yeah? AUDIENCE: After getting that initial, how did we get the mean square error in this? Because we split the embedding you put in the data and then we get... RAMA RAMAKRISHNAN: We haven't calculated the embeddings. We are trying to calculate them. Those are just-- it's like in regression, you have beta 1 times x1 plus beta 2 times x2 kind of thing. The betas are what the regression produces for us. The embeddings are exactly that. They're just coefficients that we're trying to figure out. The data is only the x's, the Xij. And so this is what we're trying to calculate. And so what you can do is you can actually start with some random values for these things and then keep on trying to improve to minimize the error starting from these random values. Are you aware of any algorithm that allows us to take random values starting point and then minimize some notion of error?

AUDIENCE: Who takes the random? How can we order it random if it has to be random? RAMA RAMAKRISHNAN: How do you know it's actually random? Oh. So that's actually a very deep question. And so it's actually a tough question because ultimately, the random number is coming from a computer. And we know how the computer runs. It's deterministic, at the end of the day. So we actually use something called pseudorandom numbers. And there's a whole specialized field of math which essentially says, look, how can I get random numbers that are sufficiently random, even though they come from a non-random computer deterministic process? So we can talk offline about it. But fundamentally, all these systems have some random number generators built in. We just cross our fingers and hope for the best and just use them. So come back to this. We can start with random values for these weights, and then we can try to minimize the squared error.

Are you folks aware of any algorithm that can help us do that? Yes? AUDIENCE: Gradient descent. RAMA RAMAKRISHNAN: Yes, gradient descent. Again, comes to the rescue. And since we are cool, we'll do stochastic gradient descent. So that's it. So gradient descent actually doesn't care what the function is, as long as you can calculate a derivative from it. As long as you calculate the gradient, you're good. So we can just run gradient descent on this thing. One key point here is that stochastic gradient descent work for any models. As long as you can calculate good gradients from them, it doesn't have to be a neural network. Any mathematical function, as long as it's differentiable and gives you a good gradient. So here, this is not a neural network, per se, but we can use gradient descent for it. So we do that, and when we are done, we would have calculated some nice embeddings. We can also calculate all these biases.

But we don't need the biases anymore. We can just throw out the biases because we only care about the embeddings and how they connect to each other. Yeah? AUDIENCE: So when you're doing the regression, are you predicting the co-occurrence matrix? RAMA RAMAKRISHNAN: Mm-hmm. AUDIENCE: OK. RAMA RAMAKRISHNAN: Exactly. So actually, let me just show a very quick numerical example here. So let's say, for example, that-- you know what? Deep learning. So this is, say, w1 and this is w2. And this is the vector. And let's assume for a moment that it has two dimensions. Two dimensions. And we also need to calculate b1 and b2, which is just a number.

And let's say the number for deep learning in the co-occurrence matrix, let's say it has occurred 104 times. So all we are doing is to say log of 104-- that is the actual value-- minus b1, which we don't know, plus B2, which we don't know. And then this thing here, let's just call it w1 one, w1 two, w2 one, w2 two. And then we're just doing the dot product, which is w1 one times w1 two plus w2 one, w2 two. So this is our prediction. Where is that cool laser pointer?

Yeah, so this is our prediction. This is the actual. So all we do is to say, OK, this thing-- the difference, we're going to square it. And then we're going to do the same exact thing for every other word pair. And when we are done with all of that thing, we just take this whole thing and say gradient descent, minimize. So then it has to find the b's and the w's and everything for every pair, every word. So that's actually what's going on. Make sense?

All right. So by the way, here, I said let's assume that the embeddings are just vectors which are dimension two. Well, that's an arbitrary decision that I made just to show you how it works because I was doing it by hand. But more generally, we get to choose how long these vectors are. And the longer the vector, the more interesting ways it can actually reproduce the co-occurrence matrix. It has more flexibility. But the longer the vector, what is the risk that you run? Overfitting.

Because these are all parameters, at the end of the day. More parameters you have, the more risk of overfitting. So you get to choose how big these things can be. Yes? AUDIENCE: Don't you find it surprising that we're able to fit a model where we have a lot more parameters than we have data? Because usually when it comes to machine learning, we like sparsity. We like to not have a lot of parameters. But here, we're going to have, as you said, the number of dimensions times more parameters than we have of data points. RAMA RAMAKRISHNAN: Well, in this particular case, as it turns out-- let's assume that you only have 10 words. And for each word, let's assume that you have-- just to keep the math simple, you have a two dimensional vector. So 10 words times 2, that's 20. Plus you have 10 biases for the words, so there's another 10. That's 30. But 10 by 10, the matrix has 100 entries. So because of the matrix being order n squared matrix,

you'll have a lot more numbers than parameters. In this particular case, you have more data than parameters. So that particular problem doesn't apply in this case, but that does show up in other cases. And there is some very interesting research in neural networks which suggests that oftentimes, the traditional assumptions of data and overfitting and all can all be called into question under some situations. I'm happy to tell you more offline, but if you're curious, just Google something called double descent to know what I mean. But in this case, it's not a problem. OK. So what that means is that we can choose how big these things are. So if you look at one-hot vectors, where there's a 1 and everything else is 0, depending on the position of the word-- these are vectors as long as the vocabulary, as we saw earlier. Word embeddings, on the other hand, they can be very dense. The numbers that make up these embeddings,

we actually want to figure out from the data what they are. So it can be anything. So the first dimension may stand for some combination of brightness plus speed plus animalness or something. We have no idea what it means. All we know is that it's able to reproduce the co-occurrence matrix really well, so it probably has figured something out. And so we can keep it really short. So the word embeddings tend to be very dense, meaning not zeros and ones, but some arbitrary numbers. It's very low dimensional, and it's of course learned from data. So once you do this, once you actually run GloVe on this data and do gradient descent and so on and so forth, you actually come up with embeddings. And then you can actually plot the embeddings. You can take these embeddings and just plot them. Here, they are not literally plotting the first two dimensions. They're using a particular technique called t-SNE, which is a way to take long vectors and project them to 2D

space for visualization purposes. And you can see here, some very interesting things are showing up. So they're basically, they plotted the embedding for brother-- nephew, uncle, sister, niece, aunt and so on and so forth. It's all showing up here. This is the embedding for man, embedding for woman, sir madam, heiress, heir, duke, emperor, king. You get the idea. So clearly, there are patterns here where things which are similar in their nature are all hanging out together in the same part

of the space, which is comforting, which is good to know. But as I mentioned earlier, it's not just about the fact that similar things happen to be near each other. The direction also actually matters. And beautiful things happen when you look at directions. So for instance, let's say that you have man, and you want to go from man to brother. So to go from man to brother, you have to start with man and then travel along this arrow to get to brother.

So this arrow has some notion of a person becoming a sibling. So you would hope that if you take that same arrow and then start here with that arrow, hopefully the woman will become a sister. Sure enough, it is. So this is called word vector algebra. Embedding algebra. And these relationships are actually showing up in the data. We didn't tell it any of these things. We just literally gave it the co-occurrence matrix and asked her to reproduce it. So I find it pretty shocking that these things are actually true. And it gives us evidence and comfort

that whatever has been learned does have some deep connection to describing the underlying nature of what's going on. It's not some statistically fluky artifact. Yeah? AUDIENCE: So we can find synonyms by context or by adjacency to other words, and not by replacing the same word, right? Because they won't appear in the same sentence if you have two words-- RAMA RAMAKRISHNAN: Right. They won't appear in the same sentence, but the pattern of co-occurrence will be the same for them, which is what we've been able to reproduce with these embeddings. So that's the key idea.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent