1. Home
  2. Explore
  3. Computer Science
  4. Transformers and Large Language Models

Study methods

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

Transformers and Large Language Models

Transcript

Hello everyone and welcome to CME 295 transformers and large language models. My name is Afin and I'll be teaching this class with Shervin who's in the back. So before we start, I just want to say a few words about ourselves. So Shervin and I are twin brothers and we have very similar backgrounds. So we both went to a school in France called Central Pari and then we came to the US for grad school. So on my end I went to MIT and then Shervin came here at Stanford in the ICM department and

then after that we both went to the tech industry. So we first both went to Uber and then we went to Google and now we're both at Netflix. So Shervin and I, we like LLMs. So we've been working in the field of LLM, I would say since the early 2020s. And so that's one of the reasons why we're so lucky to be teaching this class. We've been teaching it since 2021 under various formats. But as attention gained towards LLM, we have actually made it an actual Stanford

class since last year. And this session is the third instance. And so if you're here wondering if this class is good for you. So the goal of this class is to tell you how LLM's work and in particular understand the underlying mechanism of its architecture. So we're going to see the transformer but also how LLM are trained, how we can use them, where what they're good at, what they're not good at. So hopefully you'll be able to get a sense of all of that in this class.

And in terms of the target audience, I would say it's a pretty wide audience. So in case you want to go and be a research scientist, I would say it could be a great class to just open up the set of things that you are able to see in terms of you know what are the different techniques that are being used, what are the different uh applications and and so on. So I think that's one. The second one is if you want to do a personal project. So nowadays coding agents are

everywhere and so if you want to just take one tool and just do something, you should probably know what the coding agent is good at and not good at. And third, I would say no matter your role, there's a growing need of being literate in AI. So there's this term called AI literacy. So no matter where you work and your role, I would say just knowing how these tools work are be is becoming increasingly important. So if you feel like you're in one of these categories, this class may be good

for you. And in terms of the prerequisites, I would say if you have some foundations in linear algebra, if you know what a matrix is, how we do multiplications, then that would be great along with some basics in ML. So for instance, what is a loss function? What is a neural network? What is an embedding? Although we will be seeing that uh in the class, but it's good to have those I would say beforehand. Make sense? Okay, cool. Uh, so you may or may not know that uh, this class is being

recorded. So you can see the camera in the back. It was actually recorded last year. So you may be wondering, well, what is the point in attending the lecture if it was already recorded? So the field is moving so quickly that there's been so many new things that have come up since last year. And actually one of the goals of this class is to incorporate all the advances that have happened. So I'll name a few. So there has been uh some progress on the post-raining side.

So for instance on policy distillation if you've heard of it uh of course AI agents so coding agents and so on and uh new paradigms of LLM. So traditionally it was predicting tokens one at a time but nowadays there's been an increasing number of models that have approached things differently. So from the diffusion perspective uh among other things. So long story short if you're attending this class and you're preparing for exams please look at this year's lectures because they will be quite

different uh in terms of the logistics. So I'm so happy to see you know so many of you on a Friday afternoon. So it will be uh every Friday from 3:30 to 5:20. It will be in this class and then uh it is a twounit class. Uh you can either choose it to take it as letter or credit nonredit. So again all the lectures are recorded. We'll make sure to post the recordings at most two days after the lecture has happened. So feel free to watch it remotely if you want or attend the class

uh in person. Actually we'd love to see in person. So uh feel free to choose either way. And this class has no homeworks but it has two exams. So one is the midterm and one is the final. So the midterm will be about the first four lectures and it will be one month from now I guess. Yeah, October 23rd and the final exam will be on the last five lectures. So there will be nine lectures in total and it will be accounting for 50% of the total. So given that it's not the first time

that we're giving this class, we have already posted online what a midterm looks like, what a final looks like. So if you go on the website, you can actually see in the tab 2025 what we would expect from this exam. So it's a multi multi-choice questions. We also have some free form questions. So this is the kind of question we would ask. Any questions on the logistics so far? Yep. So, uh I'm going to always repeat by the way questions just for uh people who are

watching remotely. So, question is are there office hours? So, we don't have official office hours. However, Shervin and I will be staying after each class in case you want to ask any questions and uh we're going to see it actually here in case you have any uh so we're going to see it later if you have any questions there is also ED so platform where we can ask questions um so yeah love to answer there cool okay so in terms of the material so I was talking about the the midterms

and and so on. So they're all uh so the ones from last year they're on the website. Um the website al also contains the syllabus and uh the recordings will be posted there. One thing that we're doing is that we're posting the slides one day before the lecture because last year some people came to us and said okay we'd love to be able to annotate on the slides. So now we took the habit of posting them one day before the lecture. So if you go on the website you can retrieve them.

So in terms of the material we do have a textbook which is this one the super study guide transformers and LLMs. Uh I believe the library has a few copies so you can uh borrow from there. Uh you can also get it online. We got the website as well. And along with that, we also have a cheat sheet which aims at being the most upto-date. So Sharon and I actually updated it just uh a couple of weeks ago and that one aims at covering the whole content in a concise way. So maybe it's

a good companion for studying right before each exam. And also I know a lot of us are international people. In case you don't see your language being translated, feel free to come to us. We'd love to partner with you. Uh so right now it's available in I believe 15 languages including English. So yeah, love to see more languages as well. Cool. Um, in terms of how we communicate, so we will post announcements on Canvas and you can also access ED through Canvas, but if you have any questions that you want to ask us in private, we can also um, you know, answer through the mailing list that is on the slides along with our Stanford emails. I think I'm done for the logistics. Is there any questions?

Is it scary? Because we have we had only one question. Uh just wondering if there's any [snorts] that you may worry. Is everything clear? Super clear. Yeah. So the question is uh will we see how this ties back to what is practiced in the industry? The answer is yes. So one of the things we'll be seeing especially in the second part of this class is how we can apply those LLMs in different settings and so just as an anecdote uh so last year I remember there were some things that came up that were released as we were teaching the class and so we updated the content as we went so I'm sure we'll do the same thing this cycle as well. So yeah the answer is yes. Yeah. So the question is what is the recommendation for preparation given that there's no homework and it's 50/50 midterm exam uh final. I would say depending on how you usually study I think just studying regularly I think works best. Um I would say just paying attention to things that we're covering

in the class during the class I think is most important. So there may be topics that are in the textbook that we're not covering and I would say those are not expected in the in either exams. So roughly speaking anything we say can be in the exam. So question is do you have any practice problems? So yes. So last year we said no but now yes you have the 2025 ones. So yeah but the questions will be different. So yeah. Okay great. So, one thing is we only have two hours

or 1 hour and 50 minutes per week. So, we will have nowhere near the time to cover everything. So one thing that we'll be seeing in uh each of the slides that we'll be talking over is always I mean most of the time a source at the very bottom of the slides that is meant for you to check in case you're interested to learn more details. So feel free to refer to the slides in case you're interested in a topic that maybe we've covered for a few minutes but you want to learn more.

Cool. Um, another thing, I mean, something that I personally have felt when I first started in the NLP field like a few years ago, there's so many abbreviations all over the place and it can be intimidating. So our hope is that in case you're feeling overwhelmed today by these abbreviations that by the end of this class you'll have a mental mapping of which abbreviation correspond to what concept. So that is our goal and with that we're going to start by asking ourselves how we got here.

So 10 15 years ago in the 2010s so I remember uh the field was completely different. We didn't have one model that did everything. We had typically one model per use case. So the kind of model that was dominating the field was was RNN's recurrent neural networks which we'll briefly see in this lecture. And you had these mo use case specific models such as uh for sentiment extraction. So you asked yourself if a sentence a review is positive, negative or neutral. If you had a text you wanted

to translate, you had a dedicated model for that. And if you had, I don't know, text you wanted to identify names, locations, and so on, you would also have a dedicated model for that. So this was the 2010s performance was okay but the thing that really made a true difference that was a turning point was 2017. So 2017 there's a paper that came out that we're going to talk about at length today that's called attention is all you need that introduced a new architecture

that relied on a paradigm that proved to be very scalable by scalable I mean if you give your model more data more compute and more parameters you're actually able to see performance gains that are far greater compared to what you used to see before and it's something that we're going to see today. So this architecture is called the transformer and on the right you have an illustration of how it's uh illustrated in the original paper. We're going to see how it works.

And from there people have taken this architecture and just scaled it from a model size perspective but also from the number of tokens it's been trained on. And this is how modern day LLMs have come into the picture. LLMs that actually know I mean that actually generate text pretty well that generate code pretty well. And in particular in 2022, chat GPT got released. So I remember in November I believe. Yeah. And again it was a turning point from more of a product perspective because

before that date people were not really used to interacting with the chatbot. So I believe that release was pretty the first time that people got introduced to that aspect and I think even the person or the company who launched it probably didn't even realize how big it would get. I mean I'm assuming because in their tweets that released the the model I believe they had like a typo which uh I find funny because nowadays whenever there is a model release you know there's 10 times review of what

the wording is so I'm assuming that this was not the case for for the announcement here but yeah so that is also another turning point and then here we are 2026 I'm assuming a lot of you or all of you are interacting with coding agents almost on a daily basis. So we're going to see how we got to that stage. How we went from a conversational LLM conversational assistant to actually agentic behavior. So we're going to see that especially in the second part of this class.

So far so good. Cool. So with that, we're going to start with one foundational concept which is tokenization. So what is tokenization? So when we talk about text, text is re not really something that models can understand. models understand numbers. They don't understand text. So the first goal is to find a way to represent that text in forms of something that models can understand. So in forms of numbers. So the way that we do that is we first try to divide the text into entities

that we can then represent. And so the process of dividing the text into indivisible entities is what is called tokenization. So in this example we have a cute teddy bear is reading and one possible tokenization is to say that a is one indivisible unit, cute is another unit. Teddy bear can be another unit and so on and so forth. So each of these elements is called a token and our goal is to represent these tokens with mathematical representations that are meaningful.

So in this case we want each token to be represented by a vector, a vector of numbers. So, in order for you to tokenize an input text, you need to know what are the tokens that are allowed for you to be using. And so, we have something that's called a vocabulary, which includes all possible tokens that you can use. And depending on the vocabulary that you choose, you may end up with different sequence length. So here by sequence lengths I mean the number of tokens by which you divided your your text. So for instance in this instance we have 1 2 3 4 5 six six tokens.

We're going to see in a second that there's a tight relationship between vocabulary size and sequence length. And that's something that will inform which tokenizer we should be choosing. So far so good. So now you may be wondering well all of that is great but how do I know how I tokenize my text? Like this is just a theoretical uh concept but how do I do that? So one natural way is for you to say okay so I'll just tokenize the text with respect to words. So I'm going to consider space

as being a delimiter between tokens. So here I would say a cute teddy bear is reading. So every word in that text would actually represent a token. So what are the pros and cons of this approach? So pros very simple especially for the English language. You map a token to its words. Very simple. It is also interpretable as in you know when you get a representation of a word that it corresponds or representation of a token that it corresponds to a given word. However at least in English

you have many variation of words. So for instance a noun like you can have it is singular in plural you can have the masculine form the feminine form you can have many many variations so your vocabulary size may get very large as a result of that. So that's one con. The second con is we're going to see in a second that when we learn the representation of each token, there is nothing that guarantees us that two tokens that represent the same thing would get the same representation. So

for instance, if you consider the word bear and the word bears, these are the singular and plural of the same animal which is a bear. And so you wouldn't want the representation of these two tokens to be too far from one another because they represent the same thing. Well, with this way of tokenizing your text, you would have a hard time first of all leveraging the knowledge that you have from the similar token and second just guaranteeing that these representations are similar.

And then the third one is well given that you need to learn the representation of each of these words. So one word is one token. You may run into a case where if you train your tokenizer on a given corpus of text that you actually do not have a given word that you then encounter at inference time. So you may actually be missing representations that you could not have had the chance to learn just because you did not encounter that word at training time. And therefore you can run into the risk of

out of vocabulary meaning that a token is actually not something that you have seen at training time and you don't have the representation for that one. So long story short, word level tokenization is simple but it comes with a few cons and one of the cons is really the fact that you are not able to leverage words of similar roots and this leads to vocabulary being large and risk of out of vocabulary.

So the next question is okay can we do better? Is there another way of tokenizing the text that could make more sense? So a natural thought could be well you don't divide by words you divide by character. You have a fixed number of allowed characters at least in in English or in any languages. And so you are not running the risk of out of vocabulary anymore because you will encounter the all the letters in the alphabet in your training set or you can try to do that

and by doing so you will also have a small vocabulary and you would also be robust to casing and misspellings that people usually make in when I mean I'm not sure if you recognize yourself. I do. So when I type uh I don't know on uh my computer, I sometimes misspell things and I wouldn't want the representation of a word I meant to write to be different from the actual word. And so here we would not be going into the case of not recognizing a word just because it is misspelled.

But we will see that if you do that the sequence length is going to get much longer and model complexity is a function of sequence length. So if the sequence is very long then your model will take a long time to process the sequence. So it will make computations much slower and the embeddings themselves they're much less interpretable as in you know if we tell you the letter A has this representation it it's maybe much more abstract than talking about word representation and we will see that in a second.

So far does that make sense? Yeah. So I hope I am motivating a trade-off enough that the next natural thought in your mind is to actually try something in between. So not word, not character, but in between. So subwords. So, how about we tokenize the text by leveraging knowledge that we have about words that we can learn from some training corpus and we divide common occurrences of letters and we represent them with a token. So for instance for reading we would represent read with a

token and ink with another token. So how about we would do that. So the good thing here is that we would leverage the root of words. So in my previous example bear and bears would have the for instance bear token in common. And then the second thing is it would be actually learned from the data as in the way you tokenize text is informed by how words are actually written. But then the con is well you need to do that extra work. So in the word level case you just need to produce tokens by

just divi dividing based on space. So here you do you need to do some extra work and then the way you obtain these tokens will be highly dependent on the type of data that you have when you do your tokenizer training. So that's another thing another con. So we need to make sure that the data that you use to learn how to tokenize things is actually aligned with your end goal.

So um I just want to uh say something here. So I talked about three ways of tokenizing text. So word level, character level, subword level. Among the three, subword level is by far the most popular nowadays for all the reasons that I mentioned. And you'll hear the word or the acronym BPE a lot. So BPE is a type of subword tokenizer that stands for bite pair encoding. So the paper is linked at the bottom of this slide in case you're interested to learn more, but I'll give the just the

gist of how it works. So, BPE learns what tokens should be in your vocabulary by starting from an initial set of tokens and identifying the most frequent pairs in your in your training data and by coming up with dedicated tokens for merge tokens of these pairs. So for instance, I'll give an example. Let's imagine your initial vocabulary is all the letters in the alphabet and you have I don't know the word un like an idea and an an amazing course. So un that occurs

very frequently. So here it will say okay a and n appear very frequently together. So let's actually add a token that merges the two. it will add that new token in the vocabulary and then repeat the process until reaching a desired final vocabulary size and that will be your final vocabulary. So the nice thing is that these tokens are learned from the data. So things that appear together are actually identified and added to your vocabulary. So this will lead to your sequence length

being shorter. So you will not have to decompose your text into that many tokens. So your model will not take that much time to process your text. And so that's where the efficiency come from comes from. So if you have a training corpus that has let's say a lot of the words that you encounter in practice then the sequence length of the tokens you will get from a piece of text that uses the same words as what you had in your training corpus will be much more much shorter.

So in case you're doing multilingual work, then make sure that your tokenizer is trained on a corpus that's represents all the languages that you're interested in. That's one example. Okay, so that was a lot. So I'll just pause for a second. Is there any questions so far on this part? Yes. So uh the question is what is the the relationship between BP and engram? So engram is actually something separate and this one yes to your point it will learn the tokens from your corpus by

merging the most frequent pairs in an iterative way until reaching a final desired vocabulary size. So next natural question is okay you have your tokens then now how do you know how to represent them? Maybe you're asking yourself this and we will see that actually in about five minutes. So, more on that in a bit. Actually, I'm looking at the time. I don't think I'm on time. So, I'll probably move on. But if you have any questions, please feel free to interrupt me.

Tokenization is really the building block of everything we'll see from here. So yeah. Okay. So, so far we've seen tokens that represent text that we want to tokenize. But the truth is we also have some special tokens. And these special tokens are meant to represent things that we want to express in a sequence. So one such token is whenever we encounter a term or a piece that is actually not something that is part of our vocabulary. So the convention is to have

a dedicated token that represents this out of vocabulary piece of text and it's called an unknown token. That's one special token. So give you an example teddy bear. Imagine if I write it teddy d bear and in the training corpus we've never seen such a spelling. Then if your vocabulary is only composed of these four tokens, so teddy, cute, bear, and unknown, then teddy bear will be tokenized as unknown token and then bear. One example, another set of special tokens. So a lot

of the times you need to indicate when the sequence is starting, when it is ending. And we'll see how we do that. So we have two set of special tokens. One is beginning of se sequence bos and then end of sequence EOS. Those are mostly used when we want to generate text. So for instance when when you want to start the generation the convention is to feed your model with the beginning of sequence token just to indicate to your token to your model that you you want to

generate text and your model will then at the end of its generation just output the end of sequence token to just tell you that this generation is finished. So this is how it's used. So what a cute teddy bear would be beginning of sequence with a cute teddy bear and end of sequence as an example. And then the last one that you may encounter is the padding token and that one is meant to make your sequences be of a certain size. So this is sometimes used for efficiency perspectives

if you want all your sequences to be of the same length because we will see that we can represent that with vectors, matrices, tensors and hardware loves things that have the same dimension then padding can also be used. But I want to say that this list is by no means exhaustive and that you also have a lot of different conventions that people take. So this way of writing BOS, EOS, this way of writing padding, it's not universal. So just don't don't be surprised if you

encounter different variations. And nowadays you have things like assistance. So you have a user, assistant and those are other things that people represent with special tokens. So again this is not an exhaustive list but just know that such tokens exist and they are meant to convey something about the text that the model should understand.

Cool. That that's it for tokenization. Any questions? Yes. The question is what is the order of magnitude of the vocabulary size for lens right now? So it's a great question. It is on the order of hundreds of thousands. So you will see something like hundreds of thousand 200,000 something like this. And these are tokenizers that are able to handle both English but also other languages. And as I mentioned, the most common type of tokenizer nowadays is the BP one. So

yeah, so these are the things that are good to have in mind. Yeah. So the question is having multiple languages be handled by your tokenizer. Does that make your tokenizer a bit less of an expert in a given language? I think it is hard to say. I would say because there may be some synergies between languages. So I would say it's hard to say in general, but that being said, the vocabulary size that you fix when you when you run your BP algorithm will be filled with tokens from

different languages. So you may get a vocabulary that may lead you to longer sequence length if you're only interested in one particular language. So from an efficiency perspective maybe you'll you'll see something but then from a representation perspective it's not super clear. So one example is um acronyms of organizations that may be something that people talk about in a bunch of languages. So you may benefit from learning that representation from different text of different languages.

So pros and cons. Great. Cool. So now what we know what we have learned is given an input text we're able to generate tokens or represent that text with tokens. So the next natural question is how to represent those tokens. So this is what we're going to see now. So the most naive way to do that is to say that each token is different. So it's extremely obvious but you can represent your tokens by what we call one hot encodings. So here uh let's say I have the the token soft I can say okay it's the one 0 0 vector

and if you do that for all your tokens well the bad thing is they're all going to be orthogonal to each other if you take the dot products. So, is it a good thing? Right? No, it's not a good thing because it doesn't tell you anything about what these tokens are and how they're related to each other. So, it's not what you want. What you want is to represent these tokens in a way that reflect how similar they are to one another. That is what you want. So for instance, you want teddy bear to be similar to soft because teddy bears are soft, but maybe book to be maybe independent. This is what you want to learn. You want to learn meaningful representations.

And by the way, why do you want to learn meaningful representations? Well, it's because you want to do something with your text. You need to have it as input into your model in order to do something as output. So for instance generating the next words predicting some class. So you need to understand what is in your text which is the reason why what you want is meaningful token representations. So this is what an earlier method called v word tovec try tried to achieve. So here the idea is

to learn these representations through what we call a proxy task. So a proxy task is a task that you're trying to learn but it's not the task that you're trying to learn as an end goal. It is is a task that you try to learn in order to achieve something else. And here the proxy task is something like predicting the word that is in between some context words. So this is the continuous bag of words version of vorttovec or predicting words that are surrounding a given word that is the skip gram

version. So we'll go through an example so that it becomes clear. So does everyone know what a neural network is? Yeah. Okay, so I represented a very basic neural network. So you can think of it as an input matrix multiplication to get a hidden representation of size D and then again matrix multiplication to go back to size V your vocabulary size. So let's assume that you have the the sentence a cute teddy bear is reading. And let's assume for the sake of simplicity that your

goal is to predict cute from the word a. So let's imagine that your proxy task is to predict the next word. So here a your very naive representation of a is the one hot encoding. So let's assume that you have a vocabulary size of six and a is your first token. So you would represent a with 1 0 0 0. You pass it through your network. So think of it as matrix multiplication with this vector. you would have a hidden state of size two. Let's assume two numbers 02 and 0.9

and then you pass it to your network. So again another matrix multiplication and then you have predictions that sum to one. So you can use something like a softmax to normalize them. And your goal is to have these probabilities be as close to the label that you have which is predicting the word cute. And so cute let's suppose that it's the second word of your vocabulary. So it is represented by 0 1 0 0. So your goal is like on the slide for the 0.4 to be as close as one as possible.

So the idea is to have a loss function that penalizes the fact of having your probability of a given word being too far from the word that you're trying to predict. So in case you know the cross entropy loss and then updates the weights of your of your model in order to reflect what you want. So just so that we're clear I will just walk through all the quantities that we mentioned. So here the one hand encoding that represents the token is of size the vocabulary size.

The hidden state or also called latent representation is of size D and D is actually a parameter that you can choose typically much smaller than the vocabulary size. And then the predicted word probabilities well you have a probability per token. So it needs to match the vocabulary size. So it's of size V. And that also matches the size of the onehood encoding. And this is where you're able to try to penalize for deviations from the word that you're trying to predict. So let's assume you do that

for a trying to gen to predict cute. You repeat the same thing for cute for teddy bear. So you go cute, you represent it with the 100 encoding. You pass it through your network. You have a latent representation and again probabilities. You compare that with the actual label that is representing teddy bear. Compute the loss. Update the weights given how far you are from the from the predict from the label. And you repeat that through your whole corpus. Well, at the end of this,

what the authors realize is that the latent representation that you learn in the middle of the network actually is making sense. As in, you can compute similarity measures between the learned representations. And you see that representations of similar tokens are actually similar. So on this example, so teddy bear is soft. What is Persian poetry to arts? Another example is Paris is to France, what Berlin is to Germany. So you have these nice associations that are actually something that the

model learns. So that gives you meaningful word representations. So that's a method that appeared I believe in the early 2010s. I believe it was 2013 if I remember correctly. So who can tell me what is a a limitation of this approach? So here the representation of the words that we learn we saw that they end up being meaningful but is there is there a limitation to this approach? Yeah. So one answer is that you would have trouble if you actually are not encountering something that you have seen during um your training process. So yes, that is right and I think that's right in actually most cases beyond that case in particular. Um so that's one. Is there another yes another limitation? So the answer here is uh the dimension

scales up when the the vocabulary size gets bigger. So actually this is a limitation of your tokenizer because the tokenizer is the one that determines your vocabulary size. And but you're right that um that this is a limitation but it's not specific to that approach as in you will always need to find a representation of each element in your vocabulary. So I mean it's a limitation but limitation in methods in general. Yes. Oh yes. So answer is words can have

multiple meanings. So exactly. So um one limitation here is if the word let's say cute is in this sentence versus in another sentence they will both get the same representation. So if you say a cute teddy bear in a nice way versus someone who sarcastically says oh how cute. They may mean different things between the word behind the word cute. So I think a very classic example is bank. So river bank versus going to the bank. These are the same words but different meaning. So the

problem with this approach is that the representation of this word would actually be the same regardless of the sentence that it is in. So great point. Another limitation here is that the word order does not matter. So let's assume we're saying the child is hugging the teddy bear and the teddy bear is hugging the child. These are the exact same words, but they mean different things. We would get the same embeddings with this approach in both cases. So these are all limitations

which is the reason why this is something that people have tried to resolve through different means. One such means is RNN which as we previously saw was the type of model that was dominating the models back in the 2010s. And so here RNN's what they do is that they actually have an extra quantity that they compute which here is represented with a that is also called the hidden state that tries to encapsulate the meaning of the sentence seen so far. So we're going to see that as an

example. So let's assume we go with the same sentence. So a cute teddy bear is reading. So here you would actually put the word a into the RNN. It would try to predict the word cute. But then what it will do is keep what we call the hidden state that encodes the meaning of the sequence seen so far. That will be an input to the next prediction. So we'll have cute as an input and then these hidden states as an input as well in order to inform the next representation

and so on and so forth. So here what you're doing is you're not just considering embeddings of tokens on their on their own. You're also considering the embedding of the sequence decoded so far. And that allows you to take into consideration the order of the words in the sentence. And RNN's are actually models that were used in a bunch of tasks back in the day. So for instance, for classifications, what you would do is process tokens one at a time and then keep a selfmutating state that encodes

the meaning of the sequence seen so far and it would try to predict let's say a sentiment in the sentiment extraction case. In the multi-classification case, for instance, if you wanted to determine if a word was a noun or a verb, then you would actually leverage the representations corresponding to each input, which again would take into consideration the hidden states or just generation. But one limitation of RNN's is that okay it was great you would consider the order at

which the words would appear in a sequence but it would have trouble remembering words that happened far in the past because what it used to do was to encode the meaning of the sequence in one single vector. that kept on being modified. So you had this issue of what we call long range dependency where if you end up two sentences after something that you have talked about prior to that, you would have trouble teasing that out just using the hidden state at that point.

And that can be problematic. For instance, when you refer to something that happened in the past. So for instance, you can say, "Oh, my teddy bear is so cute. Uh, it is 3 ft tall." So it refers to something that is in the past. So now let's assume that the it is referring to something that is far in the past. then we have this problem of actually having the right set of information in the hidden state that we're considering. So this is the reason why there were some variations of RNN.

One such variation was called the LSTM long short-term memory which tried to address this problem by not only keeping a hidden state but also another state called the cell state that was meant to carry information for longer. But the truth is even such variations had their limitations which again had the same issue. So if we were to just recap the state of the ways we could obtain our embedding so far we saw vortac which is a very simple approach to compute representations of words. But the

problem is that if a word is let's say in a different sequence then the representation is the same. So that is a limitation and also as mentioned previously the same word can actually mean different things and so that is also not conveyed and in the case of RNN's we are actually addressing the word order order problem by having a hidden state that encapsulate the sequence that is seen so far. But the problem is that we have this long range dependency problem that is

actually coming from a deeper problem come called the vanishing gradient problem which is a a problem when you want to back propagate that just introduces some limitation and another thing that we have not mentioned but RNN's the problem is every time you want to predict the next token you need to have the hidden state that encapsulates all the things that you have seen so far. So you always have to just go through tokens one at a time in order to make the next prediction. And

when it comes to training such a model, it is just extremely slow and it had great results at the time but not amazing either. So for all these reasons in the 2010s people tried different techniques and there is one technique that really stood out now from hindsight but that is called attention and the idea is as follows. Instead of solely relying on the hidden state to encapsulate the meaning of the sequence that you have seen so far, the idea is to let your model have direct connections

from tokens that you have seen in the past. So in other words, we're not saying only rely on the hidden state to know what has happened in the past. We're saying you can also have direct connections to tokens that you have seen in the past in order to predict the next token. So in the translation example, so let's assume you want to translate in French a cute teddy bear is reading and let's assume you say and you want to predict the next words. So intuitively in order to predict the next words,

it would be amazing to know what the word that you want to translate is. So in the traditional RNN case, you would not have access to that direct access to that word. You would only have access to it via the hidden state. So here the idea is to have a direct connection to previous tokens and let the model learn which ones matter and here for the prediction to rely on the ones that matter in order to make the prediction. So this is the concept of attention.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent