YouTube2Text

I Trained an LLM from Scratch — Transcript

by Green Code · 2,760 words · 397 segments · language en · Watch on YouTube

Full transcript

  1. 0:00This is ChatGPT, and this is my own GPT
  2. 0:02that I'm coding and training from
  3. 0:03absolute scratch. I'm calling it Anton.
  4. 0:06And over this series, I'm spending
  5. 0:07$5,000 of compute credits to make Anton
  6. 0:10giga brain and see how good of a GPT
  7. 0:12model I can build. But, I have a massive
  8. 0:14problem. You see, in the last episode,
  9. 0:16we built a whole GPT from scratch only
  10. 0:18using NumPy, which was really hard. I'm
  11. 0:20not going to lie. But, when I tried to
  12. 0:22make Anton speak, its first ever words
  13. 0:24were these.
  14. 0:25>> Green code videos are tethered footprint
  15. 0:27pool pool pool STNR Otis Otis Otis
  16. 0:30disability disability entrepreneurship
  17. 0:33entrepreneurship timer timer timer timer
  18. 0:36>> Which is really interesting, but not
  19. 0:38very useful. And the reason for this
  20. 0:39abomination of a text is that I didn't
  21. 0:42train Anton on anything. So, in this
  22. 0:44video, we're going to teach Anton how to
  23. 0:46speak by pre-training an entire GPT from
  24. 0:49scratch only using NumPy. So, here's the
  25. 0:51game plan. In fact, here's the
  26. 0:53Shakespeare game plan. First, download
  27. 0:56every single line of text Shakespeare
  28. 0:58ever wrote, which comes out to 167,205
  29. 1:02lines of Shakespeare. Second, tokenize
  30. 1:04the entire data set. And third, teach
  31. 1:06Anton to speak like Shakespeare. And
  32. 1:08this is going to make Anton so giga
  33. 1:10brain that he's going to come up with
  34. 1:11the best literary masterpiece of the
  35. 1:1321st century.
  36. 1:15Let's take a look at the data, shall we?
  37. 1:17Check this out. We got Romeo and Juliet
  38. 1:19over here. We got Macbeth and even
  39. 1:20Hamlet and everything Shakespeare ever
  40. 1:23wrote. Anton is going to be so smart.
  41. 1:24Cool, but now we have another problem. A
  42. 1:26GPT doesn't take in text. It takes in
  43. 1:28numbers. So, we have to convert this
  44. 1:30text to numbers through tokenization.
  45. 1:32And we need to be smart about how we do
  46. 1:34this. We could just assign a number to
  47. 1:36every single letter and we're done. But,
  48. 1:38this is mega brain dead because the
  49. 1:40input is going to be super long. And if
  50. 1:41we want Anton to say hello, for example,
  51. 1:43it will have to predict every single
  52. 1:45letter one at a time instead of just
  53. 1:47predicting the whole word at once. So,
  54. 1:49instead, we use byte pair encoding. And
  55. 1:52we merge common character sequences like
  56. 1:54the into single tokens. It works by
  57. 1:57repeating three simple steps. One, find
  58. 1:59the most frequent pair of adjacent
  59. 2:01tokens in the text. Two, assign that
  60. 2:03pair a new token ID. And three, replace
  61. 2:05all instances of that pair with the new
  62. 2:07token. Then you repeat this 10,000 times
  63. 2:09and boom, you get a vocabulary of around
  64. 2:1110,000 tokens. And this is super cool
  65. 2:14because not only are we compressing the
  66. 2:15text, but we're also making it easier
  67. 2:17for Anton to speak. Obviously, there's a
  68. 2:19lot of details that I'm leaving out. I
  69. 2:21actually wrote like 300 lines of code
  70. 2:24just to make this work. And if we do
  71. 2:25this with the entire Shakespeare data
  72. 2:27set, we get tokens like observance,
  73. 2:29Romeo, philosophy, subdue, subscribe. Oh
  74. 2:33wow, that's crazy. Shakespeare is
  75. 2:34telling you to subscribe, I guess.
  76. 2:36Anyways, once we have this vocabulary,
  77. 2:38check it out. We can encode pretty long
  78. 2:39sentences in just a few tokens. In fact,
  79. 2:42we can tokenize everything Shakespeare
  80. 2:44ever wrote and it comes out to 318,741
  81. 2:48tokens. Cool. Now we have tokens and we
  82. 2:50have Anton and we're trying to cram all
  83. 2:52these tokens into Anton. But before we
  84. 2:55do that, you might be wondering why I'm
  85. 2:56trying to build my own GPT from scratch.
  86. 2:58And there's three reasons. One, because
  87. 3:00it's cool. Two, because ever since all
  88. 3:02these AIs came out, I've always wondered
  89. 3:04like how hard would it actually be to do
  90. 3:07this myself. Not some genius coder like
  91. 3:09the goat Andre Karpathy, like myself, an
  92. 3:12average coder at best. And three, to
  93. 3:14raise money for cancer research. This is
  94. 3:16the first of three versions of Anton
  95. 3:17that I'm building. And while I show you
  96. 3:19everything I do, my goal is to raise as
  97. 3:21much money as possible for cancer
  98. 3:23research. So far we've raised almost
  99. 3:25$20,000 from you guys donating and also
  100. 3:28from some very cool sponsors, which is
  101. 3:30so awesome. And if we reach these
  102. 3:32donation milestones, I'll publish all
  103. 3:34the code I wrote for free. Oh, and if we
  104. 3:35raise 100k, I'll go bald. But first, we
  105. 3:38need to make Anton get a brain. And to
  106. 3:40do that, we need to cram as many tokens
  107. 3:42as possible into Anton's little brain.
  108. 3:44But how do we do that? Well, here's a
  109. 3:4630-second crash course on how to train
  110. 3:48your own GPT from scratch. We have data,
  111. 3:50tokens. We copy data and slide it one
  112. 3:52position. Now we have target outputs. We
  113. 3:55feed input to Anton and ask Anton to
  114. 3:57predict next token. We compare that with
  115. 4:00target outputs. We realize Anton is
  116. 4:02still really freaking dumb. But how dumb
  117. 4:05is it? We use cross-entropy loss to
  118. 4:07calculate the difference between what
  119. 4:09it's predicting and what it should be
  120. 4:11predicting. And we quantify exactly how
  121. 4:13dumb Anton is. Now we want to make Anton
  122. 4:16less dumb. From loss we calculate what
  123. 4:19changes would make Anton more dumb. We
  124. 4:21do the opposite. Now Anton less dumb. We
  125. 4:25do this thousand times. Now Anton smart.
  126. 4:28In more technical terms, we quantify the
  127. 4:30difference between the token probability
  128. 4:32distribution of the target outputs and
  129. 4:34what Anton is predicting, which is
  130. 4:35pretty much why we use cross-entropy
  131. 4:37loss. From that we calculate the
  132. 4:39gradient, which is the direction of the
  133. 4:40parameters that would make the loss
  134. 4:42increase. But since we want to decrease
  135. 4:44the loss and make Anton less dumb, we
  136. 4:46take a little step in the opposite
  137. 4:48direction of the gradients. This whole
  138. 4:49thing is called gradient descent. And if
  139. 4:51we do this like a bunch of times, Anton
  140. 4:53will become giga brain. But there's also
  141. 4:55a really big catch. We also have to stop
  142. 4:57Anton from cheating. When we train a GPT
  143. 5:00on a sentence, not only are we training
  144. 5:02it to predict the next token after the
  145. 5:04sentence, but also every single sentence
  146. 5:07within that sentence, if that makes
  147. 5:08sense. And if we aren't careful in the
  148. 5:10attention mechanism, when Anton tries to
  149. 5:13predict the third word, for example, it
  150. 5:14can just peek ahead and look at the
  151. 5:17answer. That's why we use an attention
  152. 5:19mask, which basically blocks out all
  153. 5:21future tokens. This is called causal
  154. 5:23attention. All right, end of mumbo
  155. 5:24jumbo. Let's teach Anton how to speak.
  156. 5:26Let's train Anton with everything
  157. 5:28Shakespeare ever wrote until it becomes
  158. 5:30our little literary genius. 3 2 1 go.
  159. 5:33And after some time, boom, it was done.
  160. 5:36Okay, let's see the result.
  161. 5:42Well, that didn't go too well. I tried
  162. 5:44looking around to see if I had any bugs,
  163. 5:46but I didn't see any. I even
  164. 5:47double-checked my beautifully written
  165. 5:49gradient code. But after looking around
  166. 5:51on the internet, I realized that my code
  167. 5:53was fine. The problem was the optimizer.
  168. 5:56You see, training an AI is just an
  169. 5:58optimization problem. You're trying to
  170. 6:00find the exact parameters that will make
  171. 6:02our cross-entropy loss go to zero. And
  172. 6:05remember, to do that, we first calculate
  173. 6:07all the gradients, and then we slightly
  174. 6:08change our parameters. But here's the
  175. 6:10catch. How exactly do we change the
  176. 6:13parameters based on the gradients? So
  177. 6:15far to train Anton, we've been using
  178. 6:17stochastic gradient descent, which is
  179. 6:19really easy. We calculate the gradient,
  180. 6:21and we take a tiny step in the opposite
  181. 6:23direction. That's it. It's so simple. It
  182. 6:26has zero memory about past iterations,
  183. 6:28and it treats every single parameter
  184. 6:30exactly the same. Which is fine if
  185. 6:31you're training like a little silly
  186. 6:33neural net, but this is a GPT we're
  187. 6:35talking about. Anton is a complex dude.
  188. 6:38In fact, GPTs and transformers are
  189. 6:40notoriously hard to train. There's like
  190. 6:42dozens of research papers just trying to
  191. 6:45figure this out. Which means we can get
  192. 6:46inspired by them. For the past few
  193. 6:49years, almost everyone has been using
  194. 6:51AdamW optimizer. And AdamW is kind of
  195. 6:55similar to SGD, except it's way more
  196. 6:57confusing. And to explain AdamW, I have
  197. 7:00a question. Have you ever gone skiing? I
  198. 7:02have, and I'm not very good at it. In
  199. 7:04fact, this is me skiing. I stumble all
  200. 7:07over the place, and I do all these weird
  201. 7:09S shapes. And this is Candide, one of
  202. 7:11the best skiers in the world. And I have
  203. 7:13another question for you. Can you tell
  204. 7:15the difference? Because if you can, you
  205. 7:17already understand the difference
  206. 7:19between SGD and the AdamW optimizer.
  207. 7:21Imagine this mountain is the loss
  208. 7:23function we're trying to optimize, and
  209. 7:25the goal is to ski to the bottom as fast
  210. 7:27as possible by only changing these
  211. 7:29parameters. SGD only looks at the
  212. 7:31gradient of the very last step, so it
  213. 7:33has no big picture view of the mountain
  214. 7:35and makes a lot of sharp turns. It's
  215. 7:37kind of like me when I ski. I don't
  216. 7:39really know what I'm doing. I'm just
  217. 7:41trying not to die. AdamW is like
  218. 7:43Kandide. It just skis straight down to
  219. 7:45the bottom. But how does he go so fast?
  220. 7:48Momentum. Instead of only using the
  221. 7:50information from the last step, AdamW
  222. 7:52remembers previous updates and uses them
  223. 7:54to calculate the overall trajectory of
  224. 7:56the optimization. And it's this momentum
  225. 7:58that lets him ski straight down the
  226. 8:00mountain really, really fast, just like
  227. 8:02Kandide. Now, if we're optimizing
  228. 8:04something simple like a bunny slope, aka
  229. 8:07a simple neural net, I'll get to the
  230. 8:08bottom, right? Like I'm a bad skier, but
  231. 8:11I'm not that bad. Will it take me longer
  232. 8:13than Kandide? Yes. But will I eventually
  233. 8:15reach the bottom? Also, yes. But if
  234. 8:17we're skiing down Everest, aka
  235. 8:19optimizing a GPT, I'm dying 100%. Like
  236. 8:23there's no chance I make it. Kandide on
  237. 8:25the other hand, will have some trouble,
  238. 8:27sure, but he'll probably make it to the
  239. 8:29bottom. And if not, he'll get pretty
  240. 8:31close to it. So yeah, that's why we
  241. 8:32train a GPT using the AdamW optimizer.
  242. 8:36And AdamW also does something really
  243. 8:38smart. It scales each parameter's
  244. 8:40learning rate, or the step we take at
  245. 8:42every iteration, based on how much that
  246. 8:45parameter changes. So from the really
  247. 8:46complicated formula, this part is the
  248. 8:48momentum, and this is the adaptive
  249. 8:51learning rate part. And I have good
  250. 8:52news, coding Adam is easy peasy. Check
  251. 8:55it out. You literally just follow the
  252. 8:57formula. That's it. Cool, let's use
  253. 8:58AdamW to train Anton so he speaks just
  254. 9:02like Shakespeare. Boom, training is
  255. 9:04done. And check it out, it's not too
  256. 9:05bad, right? Let's take a listen.
  257. 9:07>> Madam is well, fellow, sir. What? Ho!
  258. 9:10Madam, go within. I'll go. Whilst please
  259. 9:13your goodwill, the weed my prosperer of
  260. 9:15fellow. Aroundly, sir, he willing him he
  261. 9:18dissemble. I should not trouble you.
  262. 9:21>> Okay, so it's definitely better than
  263. 9:22this, but we can improve this way more.
  264. 9:25Cuz low-key, my bro Anton is kind of
  265. 9:27struggling with grammar. Like what the
  266. 9:29hell is this? Sist take you a shapoon as
  267. 9:32she loves. This makes zero sense.
  268. 9:34So, how are we going to force Anton to
  269. 9:37improve? We're going to do
  270. 9:38hyperparameter tuning. You see, there's
  271. 9:40so many variables that affect how a GBD
  272. 9:43is trained. The vocabulary size, the
  273. 9:45embedding dimension, the number of
  274. 9:46heads, the number of transformer blocks,
  275. 9:48beta 1, beta 2, weight decay, batch
  276. 9:49size, number of epochs. It's a mess. So,
  277. 9:52let's just train a bunch of Antons and
  278. 9:54see what works and what doesn't. And
  279. 9:55while this trains, I forgot to mention a
  280. 9:57quick detail in these first two
  281. 9:58episodes. By default, Anton has zero
  282. 10:01clue what order words are in. Like, he
  283. 10:04doesn't know the difference between
  284. 10:05Anton is better than ChatGPT and ChatGPT
  285. 10:08is better than Anton. These two phrases
  286. 10:10look exactly the same to him. And this
  287. 10:12is because in the attention layer,
  288. 10:14everything gets scrambled and Anton
  289. 10:16looks at every word at the exact same
  290. 10:18time. So, to fix this, we add positional
  291. 10:20embeddings, which is a tiny layer that
  292. 10:22sits at the beginning of our GBD and
  293. 10:24gives each token information about where
  294. 10:27it is located within that sentence. It's
  295. 10:29a small detail and it's barely 25 lines
  296. 10:31of code, but it makes a huge difference.
  297. 10:33Cool. Now that we have positional
  298. 10:35embeddings and we tuned our
  299. 10:36hyperparameters a bit, let's freaking
  300. 10:38train mega Anton. I'm talking 10K book
  301. 10:41of size, five blocks, five heads, weight
  302. 10:43decay, 128 sequence length, embedding
  303. 10:46dimension, a humble 200. Let's not go
  304. 10:48too crazy, all right? In total, Anton
  305. 10:49has 6 million parameters. He's a thick
  306. 10:52boy. And you might wonder, are we going
  307. 10:54to overfit the out of Shakespeare?
  308. 10:56So, guys, are we ready to welcome into
  309. 10:59the world Anton version one, the first
  310. 11:02of three GBDs we'll create during this
  311. 11:04series? The goat, the machine, the
  312. 11:06undisputed champion, Anton GBD number
  313. 11:09edition. All right, here we go.
  314. 11:12Let's do this. 3 2 1
  315. 11:17Oh, yeah, right. I I forgot. I'm
  316. 11:19training on my own computer, so it's
  317. 11:21going to take like 11 hours. Also, if
  318. 11:23you're interested in learning more, I'm
  319. 11:25posting explainer videos on my second
  320. 11:27channel. You guys always ask me to like
  321. 11:30make longer videos or teach more stuff.
  322. 11:32So, yeah, for each episode of the
  323. 11:33series, I'm making like a 1-hour video
  324. 11:36where I explain everything in way more
  325. 11:38detail. It's also like free. I'm not I'm
  326. 11:40not trying to sell you a course or
  327. 11:41something. All I ask though is that if
  328. 11:43you like these videos or find them
  329. 11:45useful, consider donating to the
  330. 11:47fundraiser. The link is below. And if we
  331. 11:49reach 20,000 euros, which we're really
  332. 11:51close, I'll release all the code for
  333. 11:53Anton version 1, NumPy version, for free
  334. 11:56on GitHub. Oh, and if you donate $50 or
  335. 11:58more, I put your name at the end of
  336. 12:00every video for the entire series. Oh,
  337. 12:02I'm I'm running out of time. I
  338. 12:04think Anton is about to be
  339. 12:05>> Is the day so young? But new struck
  340. 12:07nine.
  341. 12:08>> I me, sad hours seem long.
  342. 12:11>> Why no, I am not well acquainted with
  343. 12:14all. A man was not like a man to be
  344. 12:16worth, but now is the first council.
  345. 12:18Where is thy face? Why does thou know
  346. 12:20me? Why do I, Marcus, look you? And
  347. 12:22wherefore not her fair love I will make,
  348. 12:24but I do as a subject to her love. If
  349. 12:26anything he were as fearing means, and I
  350. 12:29have given his love, and for no means by
  351. 12:31her, and nay, it is a place and grace.
  352. 12:35Let's be the blame. This is the truth of
  353. 12:37life.
  354. 12:37>> Honestly, I'm so happy with that. Yes,
  355. 12:39it's not perfect. It's obviously not as
  356. 12:41good as Shakespeare, and some of the
  357. 12:43phrases don't make much sense, but
  358. 12:45that's not the point. We trained the
  359. 12:46whole GPT from scratch only using NumPy.
  360. 12:49We taught Anton how to speak. We
  361. 12:51literally went from this to this in a
  362. 12:53single video. And let me tell you a
  363. 12:55secret, you can't really train a GPT on
  364. 12:58only Shakespeare data and expect it to
  365. 13:00be the next AGI. We need more data. Now,
  366. 13:03imagine if instead of Shakespeare, we
  367. 13:05train on the entire internet. Instead of
  368. 13:07training on the CPU, we train on GPUs.
  369. 13:10And instead of training with my own
  370. 13:11computer, we spend $5,000 of compute
  371. 13:14credits. Well, you won't have to imagine
  372. 13:16cuz that's exactly what we're doing in
  373. 13:18this series. In the next episode, I'm
  374. 13:19going to convert all this NumPy code
  375. 13:21into PyTorch. I'm also finally going to
  376. 13:24start using some sweet, sweet GPUs for
  377. 13:26training. We're going to make Anton even
  378. 13:28more giga brain by implementing flash
  379. 13:30attention from scratch and making a lot
  380. 13:33of other improvements. It's going to be
  381. 13:34awesome. Thank you to everyone who
  382. 13:36already donated to the fundraiser. Thank
  383. 13:38you Deep ML for donating a thousand
  384. 13:40dollars and matching donations from the
  385. 13:42last episode. Thank you to these two
  386. 13:44anonymous goats right here. Muchas
  387. 13:46muchas gracias Oscar. Thank you so much
  388. 13:47Gold Juan. Go subscribe. Thank you to
  389. 13:49Metality for the hundred dollars. Thank
  390. 13:51you to this other anonymous goat. Thank
  391. 13:53you Tamado. Thank you Luke. Gracias
  392. 13:55Carlos. Gracias Rodrigo. Thank you to
  393. 13:56this other two anonymous goats. Thank
  394. 13:58you Alfonso, Nichita, Shelfico, Yuval,
  395. 14:01Patrick, Jared, Rodrigo, Benjamin. Thank
  396. 14:03you so much for watching and I'll see
  397. 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.