I Trained an LLM from Scratch — Transcript
Full transcript
- 0:00This is ChatGPT, and this is my own GPT
- 0:02that I'm coding and training from
- 0:03absolute scratch. I'm calling it Anton.
- 0:06And over this series, I'm spending
- 0:07$5,000 of compute credits to make Anton
- 0:10giga brain and see how good of a GPT
- 0:12model I can build. But, I have a massive
- 0:14problem. You see, in the last episode,
- 0:16we built a whole GPT from scratch only
- 0:18using NumPy, which was really hard. I'm
- 0:20not going to lie. But, when I tried to
- 0:22make Anton speak, its first ever words
- 0:24were these.
- 0:25>> Green code videos are tethered footprint
- 0:27pool pool pool STNR Otis Otis Otis
- 0:30disability disability entrepreneurship
- 0:33entrepreneurship timer timer timer timer
- 0:36>> Which is really interesting, but not
- 0:38very useful. And the reason for this
- 0:39abomination of a text is that I didn't
- 0:42train Anton on anything. So, in this
- 0:44video, we're going to teach Anton how to
- 0:46speak by pre-training an entire GPT from
- 0:49scratch only using NumPy. So, here's the
- 0:51game plan. In fact, here's the
- 0:53Shakespeare game plan. First, download
- 0:56every single line of text Shakespeare
- 0:58ever wrote, which comes out to 167,205
- 1:02lines of Shakespeare. Second, tokenize
- 1:04the entire data set. And third, teach
- 1:06Anton to speak like Shakespeare. And
- 1:08this is going to make Anton so giga
- 1:10brain that he's going to come up with
- 1:11the best literary masterpiece of the
- 1:1321st century.
- 1:15Let's take a look at the data, shall we?
- 1:17Check this out. We got Romeo and Juliet
- 1:19over here. We got Macbeth and even
- 1:20Hamlet and everything Shakespeare ever
- 1:23wrote. Anton is going to be so smart.
- 1:24Cool, but now we have another problem. A
- 1:26GPT doesn't take in text. It takes in
- 1:28numbers. So, we have to convert this
- 1:30text to numbers through tokenization.
- 1:32And we need to be smart about how we do
- 1:34this. We could just assign a number to
- 1:36every single letter and we're done. But,
- 1:38this is mega brain dead because the
- 1:40input is going to be super long. And if
- 1:41we want Anton to say hello, for example,
- 1:43it will have to predict every single
- 1:45letter one at a time instead of just
- 1:47predicting the whole word at once. So,
- 1:49instead, we use byte pair encoding. And
- 1:52we merge common character sequences like
- 1:54the into single tokens. It works by
- 1:57repeating three simple steps. One, find
- 1:59the most frequent pair of adjacent
- 2:01tokens in the text. Two, assign that
- 2:03pair a new token ID. And three, replace
- 2:05all instances of that pair with the new
- 2:07token. Then you repeat this 10,000 times
- 2:09and boom, you get a vocabulary of around
- 2:1110,000 tokens. And this is super cool
- 2:14because not only are we compressing the
- 2:15text, but we're also making it easier
- 2:17for Anton to speak. Obviously, there's a
- 2:19lot of details that I'm leaving out. I
- 2:21actually wrote like 300 lines of code
- 2:24just to make this work. And if we do
- 2:25this with the entire Shakespeare data
- 2:27set, we get tokens like observance,
- 2:29Romeo, philosophy, subdue, subscribe. Oh
- 2:33wow, that's crazy. Shakespeare is
- 2:34telling you to subscribe, I guess.
- 2:36Anyways, once we have this vocabulary,
- 2:38check it out. We can encode pretty long
- 2:39sentences in just a few tokens. In fact,
- 2:42we can tokenize everything Shakespeare
- 2:44ever wrote and it comes out to 318,741
- 2:48tokens. Cool. Now we have tokens and we
- 2:50have Anton and we're trying to cram all
- 2:52these tokens into Anton. But before we
- 2:55do that, you might be wondering why I'm
- 2:56trying to build my own GPT from scratch.
- 2:58And there's three reasons. One, because
- 3:00it's cool. Two, because ever since all
- 3:02these AIs came out, I've always wondered
- 3:04like how hard would it actually be to do
- 3:07this myself. Not some genius coder like
- 3:09the goat Andre Karpathy, like myself, an
- 3:12average coder at best. And three, to
- 3:14raise money for cancer research. This is
- 3:16the first of three versions of Anton
- 3:17that I'm building. And while I show you
- 3:19everything I do, my goal is to raise as
- 3:21much money as possible for cancer
- 3:23research. So far we've raised almost
- 3:25$20,000 from you guys donating and also
- 3:28from some very cool sponsors, which is
- 3:30so awesome. And if we reach these
- 3:32donation milestones, I'll publish all
- 3:34the code I wrote for free. Oh, and if we
- 3:35raise 100k, I'll go bald. But first, we
- 3:38need to make Anton get a brain. And to
- 3:40do that, we need to cram as many tokens
- 3:42as possible into Anton's little brain.
- 3:44But how do we do that? Well, here's a
- 3:4630-second crash course on how to train
- 3:48your own GPT from scratch. We have data,
- 3:50tokens. We copy data and slide it one
- 3:52position. Now we have target outputs. We
- 3:55feed input to Anton and ask Anton to
- 3:57predict next token. We compare that with
- 4:00target outputs. We realize Anton is
- 4:02still really freaking dumb. But how dumb
- 4:05is it? We use cross-entropy loss to
- 4:07calculate the difference between what
- 4:09it's predicting and what it should be
- 4:11predicting. And we quantify exactly how
- 4:13dumb Anton is. Now we want to make Anton
- 4:16less dumb. From loss we calculate what
- 4:19changes would make Anton more dumb. We
- 4:21do the opposite. Now Anton less dumb. We
- 4:25do this thousand times. Now Anton smart.
- 4:28In more technical terms, we quantify the
- 4:30difference between the token probability
- 4:32distribution of the target outputs and
- 4:34what Anton is predicting, which is
- 4:35pretty much why we use cross-entropy
- 4:37loss. From that we calculate the
- 4:39gradient, which is the direction of the
- 4:40parameters that would make the loss
- 4:42increase. But since we want to decrease
- 4:44the loss and make Anton less dumb, we
- 4:46take a little step in the opposite
- 4:48direction of the gradients. This whole
- 4:49thing is called gradient descent. And if
- 4:51we do this like a bunch of times, Anton
- 4:53will become giga brain. But there's also
- 4:55a really big catch. We also have to stop
- 4:57Anton from cheating. When we train a GPT
- 5:00on a sentence, not only are we training
- 5:02it to predict the next token after the
- 5:04sentence, but also every single sentence
- 5:07within that sentence, if that makes
- 5:08sense. And if we aren't careful in the
- 5:10attention mechanism, when Anton tries to
- 5:13predict the third word, for example, it
- 5:14can just peek ahead and look at the
- 5:17answer. That's why we use an attention
- 5:19mask, which basically blocks out all
- 5:21future tokens. This is called causal
- 5:23attention. All right, end of mumbo
- 5:24jumbo. Let's teach Anton how to speak.
- 5:26Let's train Anton with everything
- 5:28Shakespeare ever wrote until it becomes
- 5:30our little literary genius. 3 2 1 go.
- 5:33And after some time, boom, it was done.
- 5:36Okay, let's see the result.
- 5:42Well, that didn't go too well. I tried
- 5:44looking around to see if I had any bugs,
- 5:46but I didn't see any. I even
- 5:47double-checked my beautifully written
- 5:49gradient code. But after looking around
- 5:51on the internet, I realized that my code
- 5:53was fine. The problem was the optimizer.
- 5:56You see, training an AI is just an
- 5:58optimization problem. You're trying to
- 6:00find the exact parameters that will make
- 6:02our cross-entropy loss go to zero. And
- 6:05remember, to do that, we first calculate
- 6:07all the gradients, and then we slightly
- 6:08change our parameters. But here's the
- 6:10catch. How exactly do we change the
- 6:13parameters based on the gradients? So
- 6:15far to train Anton, we've been using
- 6:17stochastic gradient descent, which is
- 6:19really easy. We calculate the gradient,
- 6:21and we take a tiny step in the opposite
- 6:23direction. That's it. It's so simple. It
- 6:26has zero memory about past iterations,
- 6:28and it treats every single parameter
- 6:30exactly the same. Which is fine if
- 6:31you're training like a little silly
- 6:33neural net, but this is a GPT we're
- 6:35talking about. Anton is a complex dude.
- 6:38In fact, GPTs and transformers are
- 6:40notoriously hard to train. There's like
- 6:42dozens of research papers just trying to
- 6:45figure this out. Which means we can get
- 6:46inspired by them. For the past few
- 6:49years, almost everyone has been using
- 6:51AdamW optimizer. And AdamW is kind of
- 6:55similar to SGD, except it's way more
- 6:57confusing. And to explain AdamW, I have
- 7:00a question. Have you ever gone skiing? I
- 7:02have, and I'm not very good at it. In
- 7:04fact, this is me skiing. I stumble all
- 7:07over the place, and I do all these weird
- 7:09S shapes. And this is Candide, one of
- 7:11the best skiers in the world. And I have
- 7:13another question for you. Can you tell
- 7:15the difference? Because if you can, you
- 7:17already understand the difference
- 7:19between SGD and the AdamW optimizer.
- 7:21Imagine this mountain is the loss
- 7:23function we're trying to optimize, and
- 7:25the goal is to ski to the bottom as fast
- 7:27as possible by only changing these
- 7:29parameters. SGD only looks at the
- 7:31gradient of the very last step, so it
- 7:33has no big picture view of the mountain
- 7:35and makes a lot of sharp turns. It's
- 7:37kind of like me when I ski. I don't
- 7:39really know what I'm doing. I'm just
- 7:41trying not to die. AdamW is like
- 7:43Kandide. It just skis straight down to
- 7:45the bottom. But how does he go so fast?
- 7:48Momentum. Instead of only using the
- 7:50information from the last step, AdamW
- 7:52remembers previous updates and uses them
- 7:54to calculate the overall trajectory of
- 7:56the optimization. And it's this momentum
- 7:58that lets him ski straight down the
- 8:00mountain really, really fast, just like
- 8:02Kandide. Now, if we're optimizing
- 8:04something simple like a bunny slope, aka
- 8:07a simple neural net, I'll get to the
- 8:08bottom, right? Like I'm a bad skier, but
- 8:11I'm not that bad. Will it take me longer
- 8:13than Kandide? Yes. But will I eventually
- 8:15reach the bottom? Also, yes. But if
- 8:17we're skiing down Everest, aka
- 8:19optimizing a GPT, I'm dying 100%. Like
- 8:23there's no chance I make it. Kandide on
- 8:25the other hand, will have some trouble,
- 8:27sure, but he'll probably make it to the
- 8:29bottom. And if not, he'll get pretty
- 8:31close to it. So yeah, that's why we
- 8:32train a GPT using the AdamW optimizer.
- 8:36And AdamW also does something really
- 8:38smart. It scales each parameter's
- 8:40learning rate, or the step we take at
- 8:42every iteration, based on how much that
- 8:45parameter changes. So from the really
- 8:46complicated formula, this part is the
- 8:48momentum, and this is the adaptive
- 8:51learning rate part. And I have good
- 8:52news, coding Adam is easy peasy. Check
- 8:55it out. You literally just follow the
- 8:57formula. That's it. Cool, let's use
- 8:58AdamW to train Anton so he speaks just
- 9:02like Shakespeare. Boom, training is
- 9:04done. And check it out, it's not too
- 9:05bad, right? Let's take a listen.
- 9:07>> Madam is well, fellow, sir. What? Ho!
- 9:10Madam, go within. I'll go. Whilst please
- 9:13your goodwill, the weed my prosperer of
- 9:15fellow. Aroundly, sir, he willing him he
- 9:18dissemble. I should not trouble you.
- 9:21>> Okay, so it's definitely better than
- 9:22this, but we can improve this way more.
- 9:25Cuz low-key, my bro Anton is kind of
- 9:27struggling with grammar. Like what the
- 9:29hell is this? Sist take you a shapoon as
- 9:32she loves. This makes zero sense.
- 9:34So, how are we going to force Anton to
- 9:37improve? We're going to do
- 9:38hyperparameter tuning. You see, there's
- 9:40so many variables that affect how a GBD
- 9:43is trained. The vocabulary size, the
- 9:45embedding dimension, the number of
- 9:46heads, the number of transformer blocks,
- 9:48beta 1, beta 2, weight decay, batch
- 9:49size, number of epochs. It's a mess. So,
- 9:52let's just train a bunch of Antons and
- 9:54see what works and what doesn't. And
- 9:55while this trains, I forgot to mention a
- 9:57quick detail in these first two
- 9:58episodes. By default, Anton has zero
- 10:01clue what order words are in. Like, he
- 10:04doesn't know the difference between
- 10:05Anton is better than ChatGPT and ChatGPT
- 10:08is better than Anton. These two phrases
- 10:10look exactly the same to him. And this
- 10:12is because in the attention layer,
- 10:14everything gets scrambled and Anton
- 10:16looks at every word at the exact same
- 10:18time. So, to fix this, we add positional
- 10:20embeddings, which is a tiny layer that
- 10:22sits at the beginning of our GBD and
- 10:24gives each token information about where
- 10:27it is located within that sentence. It's
- 10:29a small detail and it's barely 25 lines
- 10:31of code, but it makes a huge difference.
- 10:33Cool. Now that we have positional
- 10:35embeddings and we tuned our
- 10:36hyperparameters a bit, let's freaking
- 10:38train mega Anton. I'm talking 10K book
- 10:41of size, five blocks, five heads, weight
- 10:43decay, 128 sequence length, embedding
- 10:46dimension, a humble 200. Let's not go
- 10:48too crazy, all right? In total, Anton
- 10:49has 6 million parameters. He's a thick
- 10:52boy. And you might wonder, are we going
- 10:54to overfit the out of Shakespeare?
- 10:56So, guys, are we ready to welcome into
- 10:59the world Anton version one, the first
- 11:02of three GBDs we'll create during this
- 11:04series? The goat, the machine, the
- 11:06undisputed champion, Anton GBD number
- 11:09edition. All right, here we go.
- 11:12Let's do this. 3 2 1
- 11:17Oh, yeah, right. I I forgot. I'm
- 11:19training on my own computer, so it's
- 11:21going to take like 11 hours. Also, if
- 11:23you're interested in learning more, I'm
- 11:25posting explainer videos on my second
- 11:27channel. You guys always ask me to like
- 11:30make longer videos or teach more stuff.
- 11:32So, yeah, for each episode of the
- 11:33series, I'm making like a 1-hour video
- 11:36where I explain everything in way more
- 11:38detail. It's also like free. I'm not I'm
- 11:40not trying to sell you a course or
- 11:41something. All I ask though is that if
- 11:43you like these videos or find them
- 11:45useful, consider donating to the
- 11:47fundraiser. The link is below. And if we
- 11:49reach 20,000 euros, which we're really
- 11:51close, I'll release all the code for
- 11:53Anton version 1, NumPy version, for free
- 11:56on GitHub. Oh, and if you donate $50 or
- 11:58more, I put your name at the end of
- 12:00every video for the entire series. Oh,
- 12:02I'm I'm running out of time. I
- 12:04think Anton is about to be
- 12:05>> Is the day so young? But new struck
- 12:07nine.
- 12:08>> I me, sad hours seem long.
- 12:11>> Why no, I am not well acquainted with
- 12:14all. A man was not like a man to be
- 12:16worth, but now is the first council.
- 12:18Where is thy face? Why does thou know
- 12:20me? Why do I, Marcus, look you? And
- 12:22wherefore not her fair love I will make,
- 12:24but I do as a subject to her love. If
- 12:26anything he were as fearing means, and I
- 12:29have given his love, and for no means by
- 12:31her, and nay, it is a place and grace.
- 12:35Let's be the blame. This is the truth of
- 12:37life.
- 12:37>> Honestly, I'm so happy with that. Yes,
- 12:39it's not perfect. It's obviously not as
- 12:41good as Shakespeare, and some of the
- 12:43phrases don't make much sense, but
- 12:45that's not the point. We trained the
- 12:46whole GPT from scratch only using NumPy.
- 12:49We taught Anton how to speak. We
- 12:51literally went from this to this in a
- 12:53single video. And let me tell you a
- 12:55secret, you can't really train a GPT on
- 12:58only Shakespeare data and expect it to
- 13:00be the next AGI. We need more data. Now,
- 13:03imagine if instead of Shakespeare, we
- 13:05train on the entire internet. Instead of
- 13:07training on the CPU, we train on GPUs.
- 13:10And instead of training with my own
- 13:11computer, we spend $5,000 of compute
- 13:14credits. Well, you won't have to imagine
- 13:16cuz that's exactly what we're doing in
- 13:18this series. In the next episode, I'm
- 13:19going to convert all this NumPy code
- 13:21into PyTorch. I'm also finally going to
- 13:24start using some sweet, sweet GPUs for
- 13:26training. We're going to make Anton even
- 13:28more giga brain by implementing flash
- 13:30attention from scratch and making a lot
- 13:33of other improvements. It's going to be
- 13:34awesome. Thank you to everyone who
- 13:36already donated to the fundraiser. Thank
- 13:38you Deep ML for donating a thousand
- 13:40dollars and matching donations from the
- 13:42last episode. Thank you to these two
- 13:44anonymous goats right here. Muchas
- 13:46muchas gracias Oscar. Thank you so much
- 13:47Gold Juan. Go subscribe. Thank you to
- 13:49Metality for the hundred dollars. Thank
- 13:51you to this other anonymous goat. Thank
- 13:53you Tamado. Thank you Luke. Gracias
- 13:55Carlos. Gracias Rodrigo. Thank you to
- 13:56this other two anonymous goats. Thank
- 13:58you Alfonso, Nichita, Shelfico, Yuval,
- 14:01Patrick, Jared, Rodrigo, Benjamin. Thank
- 14:03you so much for watching and I'll see
- 14:05you in episode three.
About this transcript
This page contains the full transcript of I Trained an LLM from Scratch by Green Code, generated from the public captions YouTube serves with the video. The transcript has 2,760 words across 397 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.