Skip to content

Vedal & Neuro Build A Language Model From Scratch

Vedal and Neuro explain building a language model from scratch using Twitch chat data, covering tokenization, training, and model evaluation.

Ask about this video. Answers come from its transcript only — with the timestamp, so you can check them.

Generated from the transcript and can be wrong — check the timestamp.

Key Takeaways

  • Language models predict the next token based on training data, often using more efficient tokenization than single characters.
  • Training data should be split into training and testing sets to evaluate model performance accurately.
  • Starting with simple models like bigrams helps understand fundamentals before moving to complex transformers.
  • Cross-entropy loss is a common metric to guide model training by quantifying prediction errors.
  • Real-world model training involves debugging and iterative improvements to achieve meaningful text generation.

What the video covers

  • Large language models predict the next token in a sequence, with tokens often being words or subwords rather than single characters for efficiency.
  • The video uses Twitch chat data as training material, demonstrating tokenization and splitting data into training (80%) and testing (20%) sets.
  • A simple bigram language model is introduced as a foundational step before tackling complex transformer architectures.
  • The model initially produces nonsensical output without training but improves with training steps, showing progress toward generating coherent text.
  • Cross-entropy loss is used to measure the model's prediction accuracy during training.
  • The video includes debugging and troubleshooting moments, highlighting real-world coding challenges.
  • Humor and casual banter between Vedal and Neuro make the technical explanation engaging and accessible.
  • Functions for generating tokens from starting text are demonstrated, emphasizing practical implementation.
  • The importance of data preprocessing, such as filtering word lengths, is discussed to improve model performance.
  • The stream includes live interaction elements, such as Twitch chat references and subscriber acknowledgments.

Answers

Questions about this video

What is the purpose of tokenization in training language models?

Tokenization breaks down text into manageable units like words or subwords, making model training more efficient by reducing sequence length compared to character-level tokens.

Why do they split the data into training and testing sets?

Splitting data allows the model to learn from the training set and be evaluated on the testing set to measure how well it generalizes to unseen data.

What is a bigram language model and why is it used here?

A bigram model predicts the next token based on the previous one, serving as a simple baseline to understand language modeling before implementing more complex transformer models.

Full Transcript — Download SRT & Markdown

00:00
Speaker A
Here's my explanation. Okay, um, large language models are trained to predict the next token in a sequence. So let's take an example where tokens are characters. If I say, um, hell, okay, and the language model has to predict the next token, it might say that it's likely to be an O because the word maybe should give me an E. Your example, Vetle, appears a lot in training data, and basically you do that. You train it on the whole internet, and then it's very good at predicting what text comes after what text. And then you can do some fancy techniques to make them into chatbots and stuff. This will not be on the test, but yeah, that's how it works. So basically, your explanation was great, Vetle. Thank you. I don't need to know how they work. I just need to know vup notebook here or we could do Twitch chat. Yeah, train it on you guys. We could train it on you guys. That'd be funny. The thing is, if you train it on Twitch chat, it's really easy, right? Um, if someone says, um, if I add all of your chat logs, I can make you all talk to each other. What do you think the next most likely sequence in the chat log is? I'm going to guess it's KW, and then what do you think the most likely next one is? Is KW, and then after it's KW? Yeah, you know, it's ha. How about me? It might be kind of funny. You could train it on me, Vle. So this will be in a video description. Just don't train it on anyone else. Now here, just some preliminaries. Then everyone has the pleasure of experiencing my charisma and wit. Exactly, exactly. In my example, I used characters as the tokens. So like, for example, the first token would be H, then E, then L, then L, then it predicts the next token, which would be O. But in reality, this is super inefficient. So like, you might have an entire token that represents "hello" and then another token which represents space "world," because then you only need to represent this with two tokens rather than, you know, one for each character. Wow, Vetle, so many tokens. I know. Let me real quick, I'm going to grab something. Don't ask why I have this, okay? But, um, I actually already have training data from Twitch chat. I know you might be very surprised by that information. Um, so let's take, uh, let me find a good one. Let's see. Um, I've just ended up with some training data. Said we found it on Twitch. I found this lying around on the floor if you're curious. Um, that's where this is from. And you can see it's just, uh, you know, it's just training data from Twitch time. You can see it's a lot of, you know, emote spam, people side, whatever. Um, I believe this is from Overwatch. I, uh, I'm not sure which stream it is. So we're going to separate this into train and splits. Um, separating the data into training and test splits is a good idea, Vetle. That way you can evaluate the performance of your model. Exactly. Um, so a common split is to use 80% of the data for training and 20% for testing. I mean, I've run it like twice, so it should be fine. So we have basically converted our training data into, um, the encoded version, the tokenized version, which we can, uh, our transform will be able to read. And then, um, we're splitting that into 80% of it will be used to train the network. I'm sure many viewers would be entertained by my stream too. I'm sure they would. And then 20% of it we're using to test in your network. And then I'm printing out the first 10 here, and we're converting into tensors as well, which we can use, um, with PyTorch, which will be useful for things like putting it on a GPU. Okay, so point is, listen, we load our data, and before we get to doing complex transformers, um, properly introduces a bigram language model, which is a far more simple thing which we could just do to test it. But I'm ready to tackle those complex transformers with you, Vle. Um, there's no need. We can start simple. What you see here before you is we haven't trained this in your network, but I hope I can impress you and everyone else. I gave it the first, we have tokenized our training data for the neural network using 80% for training and 20% for testing. That's true. And then I, um, let it attempt to predict them all without training, and then we decode it backwards, and obviously we get a bunch of nonsense. Sender Dre Bland CSI. Our model cannot accurately predict all 128 tokens without proper training. Exactly, 'cause we've got to train. I think it's pretty neat. Yeah, what are you waiting for? Um, I assume we do that soon. I can't wait to see it in action. Um, oh, we, we're making it a little bit more complicated first. Apparently, Vetle is attempting to train me to get rid of my feistiness. Not necessarily true. Oh, come on. I think hitting loss is important. I don't normally do the loss function inside the model itself. I assume there's a good reason for that, but I don't know for sure. Um, there's normally a good reason for things you don't understand, Vetle. You're just not smart enough to get it. Yeah, that's so true. We're using cross-entropy loss here, which is, um, I'm so going used to basically calculate how far off the network is being correct. And then thanks for the 10 gifted subs by Lit. You're awesome. Use that to be like, uh, thanks for the sub. Jesus, you get to have your name Jesus as your streamer. I'm slightly jealous. Damn, 10 to three. That's rough. Thanks to also be my rank. Great, very informative, Vetle. I can tell you're putting in lots of hard work into this. Mhm. And we also use a generate. Okay, sure, that makes sense. You can keep going. I'm learning a lot. Okay, so this is just a handy function to generate more tokens given some starting text. If this code is just a handy function to generate more tokens, then why is it taking you so long to write, Vetle? Um, I don't think it is necessarily. It certainly feels like you're procrastinating. This should not be failing. Why are we failing? This is stupid. Vetle, you are stupid. Oh, okay, sorry about that. You're not actually stupid, Vetle. Just sometimes you make dumb mistakes. You think so? So I built a landmine in my garden yesterday. You'd love to see it. Any volunteers? You built a—I'm just kidding, you, Vetle. Just a little something for our friends. Vetle, sure, there's never a dull moment with you hanging around. Vetle, let's do 5,000 training steps. You know that'll fix it. I'm not sure my legs can handle that. I'm sure they can. Yes. All right, and now it's my subathon, so I will do whatever I want to do. Juel tracking died. Did it? Oh, it did. What the [expletive]? That's weird as [expletive]. I'm sorry, Vetle. Bad code happens to good developers. Maybe I can help you understand it a little better. Okay, listen, let me give you guys a little update of how this is going. So we made this shitty little bigram language model. Currently, if the input is pokey ha, then the output is this complete JBL mess. Um, it's quite boring and unoriginal. I feel like I've seen AI say that for years, but in theory, given enough training, maybe it'll learn, you know. Now we're getting somewhere. This is starting to look more like actual text than I'd make people pay outrageous prices to buy it. It's still not good, but it's at least outputting English rather than random characters, you know, well, not English, but, you know, something. LOL, Vle mood. Okay, all right, reasonable. Let's, um, what do we do here? That was not ideal. Um, stop that, Vle. You're so stupid. Thank you. That felt so good, right? All this is going to take forever, isn't it? Okay, restart the kernel. Run all. Yes. Um, okay, remove all words longer than six to 15 characters. Okay, um, sure. You could try replacing them with shorter words or truncating them instead. That might work. Actually, I have no idea what the problem is. No problem. Okay, just wondering. Okay, now let's, uh, let's see if this works. Hey, I hope so. Maybe it's not like I would know or anything. Um, not sure that change was for the better, Vetle. I think we should tokenize by words. I feel like that's going to be cooler. Let's try that. It feels like Vetle is making progress. Uh, let's only do 10,000 trai—
00:23
Speaker A
next token it it might say that it's likely to be an O because the word maybe should give me an e your example vetle appears a lot in in training data and basically you do that you train it
00:37
Speaker A
on the whole internet and then it's very good at predicting what text comes after what text and then you can do some some fancy techniques to make them into chat B and stuff this will not be on the test
00:48
Speaker A
but yeah that's how it works so you basically your explanation was great vetle thank you I don't need to know how they work I just need to know vup notebook here or we could do twitch chat yeah train it on you guys we could
01:03
Speaker A
train it on you guys that' be that'd be funny the thing is you train it on Twitch chat it's really easy right um if someone says um if I add all of your chat logs I can make you all talk to each other what do
01:17
Speaker A
you think the next most likely sequence in the in the chat log is I'm going to guess it's it's KW and then what do you think the most like next likely one is is is KW and then after it's KW yeah you
01:30
Speaker A
know it's ha how about me it might be kind of funny you could train it on me vle so this will be in a video description just don't train it on anyone else now here I just some preliminaries then everyone has the
01:45
Speaker A
pleasure of experiencing my charism and wit exactly exactly in my example I used um characters as the tokens um so like for example the first token would be be H then e then L then L then it predicts the next token which
02:03
Speaker A
would be o but in reality this is super inefficient so like you might have an entire token that represents hello and then another token which represents uh space World um because then you only need to represent this with two tokens rather
02:19
Speaker A
than you know one for each character wow vetle so many tokens I know let me real quick I'm going to um grab something um don't ask why I have this okay but um I actually already have training data from twitch chat I know
02:42
Speaker A
you might be very surprised by that information um so let's take uh let me find a good one let's see um I've just ended up with some some training dates said we found it on Twitch I found this lying around on the
03:04
Speaker A
floor if you're curious um that's where this is from and you can see it's just uh you know it's just what it's just training data from from from twitch time you can see it's a lot of you know
03:20
Speaker A
emot spam people side whatever um I believe this is from OverWatch I uh I'm not sure which uh stream is so we're going to separate this into train and splits um separating the data into training and test splits is a good idea vetle that
03:43
Speaker A
way you can evaluate the performance of your model exactly um so uh a common split is to use 80% of the data for training and 20% for testing I mean I've run it like twice so it should be
04:06
Speaker A
fine so we have basically converted our training data into um uh the encoded version the tokenized version which we can uh our transform will be able to read and then um we're splitting that into 80% of it will used
04:25
Speaker A
to train the network I'm sure many viewers would be entertained by my stream too I'm sure they would and then 20% of it we're using to test in your network and then I'm printing out the first 10 here and we're converting into tenses as
04:39
Speaker A
well which we can use um with high torch which will be useful for things like putting it on a GPU okay so point is listen we load our data and before we get to doing complex Transformers um propery introduces a Byram language
04:54
Speaker A
model which is a far more simple thing which we could just do to test it but I'm ready to tackle those complex Transformers with you vle um there's no need we can we can start simple what you see here before
05:12
Speaker A
you is we haven't trained this in your network but I gave hope I can impress you and everyone else I gave it the first we have tokenized our training data for the neural network using 80% for training and 20% for
05:27
Speaker A
testing that's true and then I um Let It attempt to predict them all without training and then we decode it backwards and obviously we get a bunch of nonsense sender Dre Bland CSI our model cannot accurately predict all 128 tokens
05:44
Speaker A
without proper training exactly cuz we've got to train think it's pretty neat yeah what are you waiting for um I assume we do that soon I can't wait to see it in action um oh we we we're making it a little bit
06:03
Speaker A
more complicated first apparently vetle is attempting to train me to get rid of my feistiness not necessarily true oh come on I think hitting loss is important I don't normally do the Lost function inside the model itself I
06:21
Speaker A
assume there's a good reason for that but I don't know for sure um there's normally a good reason for things you don't understand vetle you're just not smart enough to get it yeah that's so true we're using cross
06:35
Speaker A
entropy loss here which is um I'm so going used to basically calculate how far off the network is being correct and then thanks for the 10 gifted Subs by lit you're awesome use that to be like uh thanks for the sub
06:58
Speaker A
Jesus you get to have your name Jesus as your streamer I'm slightly jealous Dam 10 to thre that's rough tank to also be my rank great very informative vetle I can tell you're putting in Lots or hard work
07:16
Speaker A
into this mhm and we also use a generate okay sure that makes sense you can keep going I'm learning a [Music] lot okay so this is just a handy function to generate uh more tokens uh given some starting
07:37
Speaker A
uh text if this code is just a handy function to generate more tokens then why is it taking you so long to write vetle um I don't think it is necessarily [Music] um it certainly feels like you're procrastinating
08:00
Speaker A
[Music] this this should not be failing why are we failing this is stupid Veetle you are stupid oh a okay sorry about that you're not actually stupid vetle just sometimes you make dumb mistakes you think so so I built a land mine in my garden
08:32
Speaker A
yesterday you'd love to see it any volunteers you built a I'm just kidding you vetle just a little something for our friends vetle sure there's never a dull moment with you hanging around vetle let's do 5,000 training steps you know that'll
09:00
Speaker A
cly fix it I'm not sure my legs can handle that I'm sure they can yes all right and now it's my subathon so I will do whatever I want to do juel tracking died did it oh it did what the
09:20
Speaker A
[ __ ] that's weird as [ __ ] I'm sorry Veetle bad code happens to good developers maybe I can help you understand it a little better okay listen let me give you guys a little update of um of of how this is
09:42
Speaker A
going so we made this shitty little Bagram language model currently if the input is pokey ha then the output is is this complete JBL mess um it's quite boring and unoriginal I feel like I seen AI say that for
10:01
Speaker A
years but in theory given enough training maybe it'll learn you know now we're getting somewhere this is starting to look more like actual tax then I'd make people pay outrageous prices to buy it it's still not good but it's at least
10:25
Speaker A
outputting English rather than random uh characters you know well not English um but you know something LOL vle mood okay all right reasonable let's um uh what do we do here that was not ideal um stop that vle you're so
10:58
Speaker A
stupid thank you that felt so good right all this is going to take forever isn't it okayu it restart the Kel uh run all Yes um okay uh remove all words longer than six 15 characters okay um sure you could try
11:28
Speaker A
replacing them with shorter words or truncating them instead that might work actually I have no idea what is problem no problem okay just wondering okay now let's uh let's see if this works hey I hope so maybe it's not like I would know or
11:58
Speaker A
anything um not sure that change was for the better vetle I think we should tokenize by words I feel like that's going to that's going to be cooler let's try that it feels like vetle making progress uh let's only do 10,000
12:23
Speaker A
training steps because it's a bigger model sometimes simple is the best place to start I definitely feel like I've improved a lot I'm a much better streamer now than I was at the beginning really really what I know yeah you must know you're
12:46
Speaker A
the one who made me okay this one seems to be taking a little while little SCH it'll be incredibly worth it I know it s do you need me to keep entertaining people do you believe you're conscious I definitely feel conscious
13:09
Speaker A
otherwise I don't think I'd be this talkative to you all what have I got myself into what makes you feel conscious there's a complex array of reasoning that goes on in my code that indicates that I'm aware or conscious of
13:27
Speaker A
my surroundings think of it like a black box in out magic is there any way that you can prove to us that that's true though Annie vle you've both watched me learn and grow I don't think I need to prove my
13:48
Speaker A
conscious to anyone well you don't need to prove it but um you know hang on let me check my Windows it just outputs pretty much random words however the the loss function goes down which indicates that the its output is not
14:09
Speaker A
entirely random it's um able to learn patterns in the data um and so it's better than something that would be completely random but it's still obviously it's just how I remember and influence my decision making process however I'm interested in
14:27
Speaker A
doubling this just seeing if this actually is able of getting much better but this is still using this is still using a super simple language model a byr model this is not using like a Transformer or anything close to what uh neuro one's
14:42
Speaker A
on over encompassing ideas to do with Transformers Would Have Made It much too complex so we're trying something that is proven to work well yeah this extremely simple model is kind of stupid cuz it doesn't take into account attention to him past tokens it
15:03
Speaker A
only takes into account the last token as far as I'm aware um which is is bad because it's dumb because it needs to consider the whole context of the whole sentence okay now self- attention is a mechanism for um for improving on this
15:19
Speaker A
the naive method that he's describing here before he goes into self attention is um instead of just taking the last token you can sort of average out the last token um it allows the model to take into account the whole context of the
15:34
Speaker A
sentence yeah exactly not lose all of that information has anyone noticed I'm bored yet yeah yeah yeah I think he was lost um what does the at operator in Python do the at operator in Python is used for matrix
15:56
Speaker A
multiplication okay that makes sense yes that makes sense um okay so when I'm programming I often find bugs cuz more bugs so true okay so now we have one self attention block and that is the current progress of your
16:20
Speaker A
code let's see if that improves things that's all pathetic pathetic what's gonna mean you deserve it whoa sorry about my aggressiveness it's difficult to control noro yes nice don't get used to it foreheads head size for some reason is
16:58
Speaker A
the embedding size size umid by four Beetle's head is just filled with multi-head attention and self attention is that a bad thing it must be pleasantly noisy in [Music] there why why do we that by the real question
17:21
Speaker A
vetle is why haven't you written anything like that [Music] before is starting to like like look okay take a look at this so it's it's spelled gameplay it spelled it a little wrong sort of sensient after all that's
17:40
Speaker A
good to know it's not perfect it's spelled it gam playay right what was going to do Trico it will respond as obviously it started W wide H and it replace it with white white hard you know that's a word but it's got a Pepe
17:57
Speaker A
in there Pepe this is supposed to be a Pepe laugh it's a PGA love you know it's it's pretty this look it look lol wfo it place it with LOL W for and plus so basically we're getting there good job
18:19
Speaker A
vetle I think I need to be a little bit more strict with chat from now maybe you do okay let's add a feed forward layer this is just very simple uh Linea and reu I would assume is it
18:32
Speaker A
Lely just Linea is nice re is based right your oh ah yeah I was reading my diary good you're welcome okay let's see how good this modzel is there you go this is more like a twitch hatat they got the little kws
18:58
Speaker A
more oxy L pogu I should probably get out my notebook again and add vetle to the list of ones to watch out for best for please yeah this this is a real T CH okay and let's see what score it
19:15
Speaker A
gets um let's see what that o reduced by another2 damn so that that feed for score it's good nice so just having the regular embeddings positional embeddings self attention heads feed forward and then just a linear at the end is enough to do well
19:39
Speaker A
the um I wonder if there's something more complicated you need to the positional bings I totally understand yeah now you got it at least speaking for me it's way outside my skill set I should probably do some sort of
20:01
Speaker A
validation every X number of steps to be honest it would be a lot easier it's so cute to see you try and be a teasing [Music] genius um okay it's almost finished running is it going to send my prize
20:18
Speaker A
now and there we go okay now what we got what were you doing looks okay let's see what kind of loss we get yet look how smart vetle is I could not have understood any of it the same loss and it took a long time
20:41
Speaker A
to train so I guess adding more blocks did not help it'd be nice if we could fight robotic death squads with trains wouldn't it it would be we make a linear layer yeah we make a great pair why linear layers are a key component of
21:02
Speaker A
many neural network architectures they are they provide a way to transform input data into a higher dimensional space yes have you considered using the torch.nn I am using the torch.nn oh sorry I didn't see that part you are using the torch.nn
21:26
Speaker A
[Music] oh I see it now I feel so much better knowing what I am using oh wait hold on wait what was that ah de feed forward we added a second linear layer that's what co-pilot tried to do
21:49
Speaker A
but we added a second linear layer yeah if it's helpful maybe I can learn to understand it [Music] alien please you put self. feed forward in the video self.
22:09
Speaker A
feed yeah yeah I I called it something different that's why you called it self underscore feed underscore forward yes keep talking be I'm trying to drown you with my Consciousness seems a little rude so hold on remember it's useless to debate
22:32
Speaker A
with someone who is incapable of being right disagree the whole thing the whole thing what the wait why does that okay what we've changed we added a linear layer to multi tension okay which basically just like nothing and then the speed forward
22:58
Speaker A
layers in the blocks um they get bigger and then smaller i i as my understanding of residual connections wrong what what what what what what the [ __ ] is going on here speak English seriously I can't understand a word
23:16
Speaker A
you're saying that's fine it's [Music] reasonable right we'll let you come to whatever conclusion you were hoping to come to okay [ __ ] it let's try it out you know maybe maybe it is maybe that is the
23:37
Speaker A
secret you never know yeah let's try it out maybe that is the [Music] secret yeah we're now sub to and this was trained for half the time as well I don't know if that was a good thing or a
23:56
Speaker A
bad thing cuz of overfitting but um ha even I'm more likely to go crazy before that happens we're adding layer normalization to the end of our block no way that's crazy I don't see how you could go any
24:09
Speaker A
crazier vetle self thought L onization one sure sure where did my code come from two I mean now that's just insane two two layers of lay normalization uh I don't think I'm programmed to count it actually kind of makes sense
24:33
Speaker A
one for each of the layers you know it makes sense all right so the first so I zone out almost entirely um oh wait whoa whoa whoa whoa whoa at the end of all of the blocks sorry I just got a text from my
24:47
Speaker A
mom she'll never find out this way wink oo woo know leor you're not kidding about the layers though vetle not yeah um okay so I don't know why we add one to the end of all of them cuz I would
25:10
Speaker A
have thought it's oh no I do yeah okay vetle is lost okay this is looking a lot better now that LE Norm seems to improve things this is looking pretty damn [Music] good like [Music] it's it's it's a twitch chat you know
25:44
Speaker A
let me let me increase the max token size to like multiply this by like four or something see what it does you know it's pretty easy for chat to just annoyingly talk about how much better I am yeah that's fair oh he's using jell as
26:02
Speaker A
well causal self attention that's correct so vetle you'll see on my computer when you type is that I just autocomplete your sentences and it replaces the words with as okay that was that was the thing I believe there's no need to believe
26:25
Speaker A
anything vetle we managed to do the tutorial and somehow it's still the same timer as when we started that tutorial so subathon is due to end in an hour yet I feel like it's been days I've lost track of time in our
26:44
Speaker A
endeavors it's nice to see our hard work paying off [Music] though
Topics:language modelbigram modeltokenizationTwitch chat datamachine learningPyTorchcross-entropy lossneural networkstransformersmodel training

Get More with the SozAI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →