YouTube2Text

Become an AI Researcher Course – LLM, Math, PyTorch, Neural Networks, Transformers — Transcript

by freeCodeCamp.org · 27,531 words · 4,735 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Welcome to this full course on becoming
  2. 0:01an AI researcher. This course will guide
  3. 0:03you step-by-step starting with the
  4. 0:05foundational mathematics essential for
  5. 0:08understanding modern AI. Before diving
  6. 0:11into PyTorch fundamentals, you will then
  7. 0:13learn about the building blocks of AI
  8. 0:16from simple neural networks to the
  9. 0:18complexities of multi-layer
  10. 0:20architectures. The course ends with an
  11. 0:22in-depth module on transformers, the
  12. 0:25critical technology underpinning today's
  13. 0:27large language models and generative AI.
  14. 0:30By the end, you will possess all the
  15. 0:32fundamental knowledge required to
  16. 0:34critically read, understand, and even
  17. 0:37begin contributing to AI research
  18. 0:40papers. Vuk Rosić developed this course.
  19. 0:43Welcome to the full course of becoming
  20. 0:45AI researcher. I will guide you
  21. 0:48step-by-step through mathematics and
  22. 0:51then PyTorch fundamentals, neural
  23. 0:53networks,
  24. 0:55and lastly, transformers. After
  25. 0:58finishing this course, you will have all
  26. 1:00of the fundamental knowledge necessary
  27. 1:02to start doing your own AI research, to
  28. 1:05start understanding others' AI research,
  29. 1:07and publishing your own papers.
  30. 1:09First, we will start from basics,
  31. 1:11talking about functions in mathematics,
  32. 1:14different types of functions, and what
  33. 1:16each term in a function determines. Then
  34. 1:19we will talk about derivatives, how
  35. 1:21derivatives show a rate of change of a
  36. 1:23function, and how they are used later in
  37. 1:27neural networks.
  38. 1:28After that, we will learn about vectors.
  39. 1:30Vectors are present everywhere in large
  40. 1:32language models, reinforcement learning,
  41. 1:34robotics, and neural networks, and
  42. 1:37anywhere else you can imagine pretty
  43. 1:39much. They're used for everything.
  44. 1:42Then we will combine our knowledge of
  45. 1:44vectors and derivatives to figure out
  46. 1:46gradients. Gradients are used for
  47. 1:49learning of the neural network. Gradient
  48. 1:51is how neural networks learn. So, you
  49. 1:52will understand what they are, how they
  50. 1:55show the direction of steepest ascent,
  51. 1:57how to use them to make neural network
  52. 2:01errors lower, how to make how to use
  53. 2:04them to make neural networks learn.
  54. 2:07And by the way, right now I'm also
  55. 2:08explaining to you your road map, your
  56. 2:11path to become AI researcher, what you
  57. 2:13need to learn. So, keep track of what
  58. 2:16I'm explaining. This is not just an
  59. 2:17intro. This is your road map to becoming
  60. 2:19AI researcher, so you can self-study as
  61. 2:22well. Next are matrices and matrix
  62. 2:25multiplications.
  63. 2:26These are maybe the core of neural
  64. 2:29networks besides gradients. They are
  65. 2:31done on GPUs. Neural networks are all
  66. 2:33about calculating matrices and doing it
  67. 2:37quickly and efficiently.
  68. 2:39And lastly, in the math module, we have
  69. 2:41a probability. This is used also
  70. 2:43absolutely everywhere. Neural networks,
  71. 2:45large language models, reinforcement
  72. 2:46learning, robotics, everywhere. Video
  73. 2:49generation, diffusion, image generation.
  74. 2:51Then there is PyTorch module. The first
  75. 2:54lesson includes creating tensors,
  76. 2:56understanding tensors, understanding
  77. 2:58data types.
  78. 2:59Then matrix multiplication, different
  79. 3:02operators, and how matrix multiplication
  80. 3:05works in PyTorch.
  81. 3:07Then transposing, flattening, reshaping,
  82. 3:09viewing, squeezing, unsqueezing tensors.
  83. 3:11All of these things are going to be used
  84. 3:13very frequently in AI research, so you
  85. 3:15need to understand and master them. And
  86. 3:17I have a lot of examples for you to play
  87. 3:19around. You can find this GitHub
  88. 3:21repository below the video, the first
  89. 3:24link in the description.
  90. 3:26You will also learn tensor indexing and
  91. 3:28slicing. So, if I tell you I want last
  92. 3:30two rows, and out of those last two
  93. 3:33rows, I want um
  94. 3:35second to fifth columns. And so, you
  95. 3:38need to be able to quickly uh write
  96. 3:40indexing logic. Then we will talk about
  97. 3:43special matrices and arrays like
  98. 3:45identity matrix, random matrix,
  99. 3:48linspace, etc. After PyTorch module, we
  100. 3:51will work with neural networks. First,
  101. 3:54we will create a single neuron,
  102. 3:55understand what weights and biases are,
  103. 3:57how they influence the output, how they
  104. 4:00are calculated. Then activation
  105. 4:03functions, how they turn the straight
  106. 4:06line of weights and bias and input
  107. 4:09multiplication into a curved line that
  108. 4:11can encircle data.
  109. 4:13We will talk about different activation
  110. 4:15functions and what they are useful for.
  111. 4:18We will also talk about multi-layer
  112. 4:20neural networks and how to have entire
  113. 4:23layer of weights in a single matrix and
  114. 4:25how that matrix multiplies inputs and
  115. 4:27how
  116. 4:28the results get transformed. So, we will
  117. 4:31be building neural network layers from
  118. 4:34scratch, coding it step-by-step with all
  119. 4:36of the functions explained step-by-step
  120. 4:38in this course.
  121. 4:40As well as backpropagation and how
  122. 4:42neural networks learn.
  123. 4:43And finally, in the last module, we will
  124. 4:45talk about transformers. Transformers
  125. 4:48are base of today's large language
  126. 4:50models, as well as image generation,
  127. 4:52video generation, audio generation, and
  128. 4:55almost everything.
  129. 4:56You will understand the attention
  130. 4:58mechanism, and then on top of that,
  131. 5:00self-attention and multi-head attention,
  132. 5:02and then the feedforward networks and
  133. 5:04layers of the transformer, and how all
  134. 5:06of that works, as well as tokenization.
  135. 5:09And this will be a good base for
  136. 5:11building your own large language model.
  137. 5:14As well as video generation, image
  138. 5:15generation, and other models. So, that's
  139. 5:18the road map that we will follow. You
  140. 5:20can also follow this road map by
  141. 5:22searching other YouTube videos and
  142. 5:24self-studying.
  143. 5:26And if you are ready, we will begin.
  144. 5:28Hello, guys. This is mathematics for AI
  145. 5:31research. If you understand and learn
  146. 5:33the math I'm going to explain in this
  147. 5:35video, you will be able to take any AI
  148. 5:38research paper and understand math
  149. 5:40within it as well. And you will be able
  150. 5:42to continue doing research in any part
  151. 5:45of AI that you wish. So, is it large
  152. 5:48language models, transformers, video
  153. 5:50generation, diffusion, image generation,
  154. 5:52reinforcement learning, robotics?
  155. 5:54Whichever part of AI you are interested
  156. 5:56in, this is fundamental math that
  157. 5:58appears in everywhere. This is going to
  158. 6:01be part of my course on creating and
  159. 6:03training large language model like
  160. 6:05Kimiko thinking from scratch, but this
  161. 6:08math applies to the entire field. But
  162. 6:11whichever AI research you are doing,
  163. 6:12you're going to have to be learning this
  164. 6:14math as well. If you click the first
  165. 6:16link below this video, you're going to
  166. 6:17get to this repository, become elite AI
  167. 6:19researcher, and the first module is
  168. 6:22math, which what I'm going to which is
  169. 6:23what I'm going to explain now. We have
  170. 6:25all of these Jupyter notebooks. So,
  171. 6:28requirements here is that you understand
  172. 6:30basic Python. So, here we have some
  173. 6:33array, we have some for loop, we have
  174. 6:36some variables. So, this is all basic
  175. 6:38Python.
  176. 6:39So, if you are not familiar with basic
  177. 6:41Python, you can just type a Python
  178. 6:43tutorial on YouTube, and you can check
  179. 6:46whichever one you like. Maybe this one,
  180. 6:48or actually Harvard classes are actually
  181. 6:50very good.
  182. 6:51So, I want you to know how to execute
  183. 6:53Python code. Uh it can be on your local
  184. 6:56machine, or it can be in Google Colab.
  185. 6:59So, now I also want you to say Google
  186. 7:02Colab tutorial, if you want if you want
  187. 7:04to use Google Colab, but also you can
  188. 7:06just run it on your local machine. So,
  189. 7:08whichever one you prefer.
  190. 7:10Also, if you're using VS Code or Cursor
  191. 7:12or some fork of VS Code, you can install
  192. 7:15this brand new Google Colab extension.
  193. 7:18So, if you just search for Colab, it's
  194. 7:19by Google, install it, and then you can
  195. 7:21connect to Google Colab GPU, CPU, TPU
  196. 7:25from your local environment, from your
  197. 7:27local Jupyter notebook.
  198. 7:30So, now on my GitHub repository, in the
  199. 7:32math folder, I want you to open this
  200. 7:35first Jupyter notebook. So, you can copy
  201. 7:38this and open it in Google Colab. As I
  202. 7:41explained, you can ask ChatGPT how to do
  203. 7:43it, or open it in your local, or however
  204. 7:46you want to open this.
  205. 7:48And then
  206. 7:49I have it here on my local, and we're
  207. 7:51going to go through this.
  208. 7:53So, if you install
  209. 7:54Google Colab extension, you can click
  210. 7:56here Google Colab, and we can say open
  211. 8:00Google Colab web.
  212. 8:02But I don't want to open this on web. I
  213. 8:03want to say um
  214. 8:06select kernel
  215. 8:08with which to activate this and select
  216. 8:10another kernel, and then Colab.
  217. 8:13And then I can create new Colab server.
  218. 8:16And then I can choose CPU in this case,
  219. 8:19because I don't need to
  220. 8:23run this on GPU. But keep in mind, uh
  221. 8:25now you can write a name.
  222. 8:29I can just use the default name, like
  223. 8:30this. Press enter. But you don't
  224. 8:32actually need to do this if you want to
  225. 8:34run the Python code on your local
  226. 8:35computer. So, I'm I'm showing you how to
  227. 8:37run it from your local using Google
  228. 8:40Colab, Google's GPUs or CPUs.
  229. 8:43But you can just run it locally as well.
  230. 8:46And so, if I click here Colab,
  231. 8:49I can click remove server if I want to
  232. 8:52uh shut down the server. But anyways,
  233. 8:57first, depending on which kernel you are
  234. 8:59running here, uh if it's Google Colab,
  235. 9:02it will have these NumPy, Matplotlib
  236. 9:04already pre-installed.
  237. 9:06But uh you may also want to create your
  238. 9:09own environment.
  239. 9:12And this is something you would learn
  240. 9:13how to do on YouTube or from ChatGPT,
  241. 9:15and you need to install these
  242. 9:17requirements.
  243. 9:18So, if you try to run this, but you get
  244. 9:21error like uh NumPy or NP is not
  245. 9:24defined, then you would don't have these
  246. 9:27requirements installed. So, make sure
  247. 9:29you enter your console,
  248. 9:32and make sure you are in the folder
  249. 9:34where the in which the requirements are.
  250. 9:38So, this is the outside folder in which
  251. 9:40requirements file is.
  252. 9:43And then I'm going to say pip install -r
  253. 9:45requirements.txt,
  254. 9:47but make sure to create your
  255. 9:48environment, install this in a Python
  256. 9:50environment.
  257. 9:53And then just press this.
  258. 9:55Enter.
  259. 9:56But actually, I don't want to install
  260. 9:58PyTorch because uh for the PyTorch
  261. 10:01tutorials, I'm going to use Google
  262. 10:02Colab. So, I'm just going to install
  263. 10:04NumPy and Matplotlib, and then I'm going
  264. 10:06to comment out PyTorch.
  265. 10:08So, let me do the installation again.
  266. 10:10Okay, now this is already installed.
  267. 10:13So, let's do Let's start with the maths.
  268. 10:16Now that you have everything set up,
  269. 10:18everything installed, I will show you
  270. 10:19the curriculum. First, we're going to
  271. 10:21learn the functions. So, these are math
  272. 10:23functions, not Python functions, but
  273. 10:25math functions. Then, derivatives.
  274. 10:29Then, vectors, gradients, matrices, and
  275. 10:32probability.
  276. 10:33A lot of you may already be familiar
  277. 10:36with some of these, especially maybe
  278. 10:37with functions. But, uh you can use uh
  279. 10:40timestamps below or chapters to skip,
  280. 10:43but maybe you can also uh watch it
  281. 10:45anyways. So, uh
  282. 10:48function will map
  283. 10:50from one value or from one set of
  284. 10:53values, but let let's make it simple.
  285. 10:55You have some value X
  286. 10:57equals one. Your function will transform
  287. 11:00it into some other Y, like Y equals two,
  288. 11:03based on some rules. For example,
  289. 11:06if we have function that Y is equal to 2
  290. 11:09* X,
  291. 11:10then if X is one, Y will be two. If X is
  292. 11:15five, Y will be 2 * 5 is 10.
  293. 11:19And
  294. 11:21I want to draw this, so
  295. 11:23uh as X goes 0 1 2 3 4,
  296. 11:29the Y is going to be the double. So,
  297. 11:31here at For example, at five, Y will be
  298. 11:3410.
  299. 11:36So then, for every X, we're going to
  300. 11:40take and draw corresponding Y.
  301. 11:43So, for example, for uh 2.5 right here,
  302. 11:46Y will be five.
  303. 11:49For two, Y will be four, right here.
  304. 11:53For five, Y will be 10. So, if we then
  305. 11:56just connect all of these uh dots we
  306. 11:58draw all of these dots, we get this
  307. 12:00line. So, this is a linear function.
  308. 12:03And this is how we drew This is where
  309. 12:05the plot drawing happens. So, you don't
  310. 12:08need to understand uh this part at all.
  311. 12:09This is just to show this plot. So,
  312. 12:12let's just understand this part. This is
  313. 12:13our function.
  314. 12:15So,
  315. 12:16we will create
  316. 12:19an array of X values between zero and
  317. 12:2210.
  318. 12:23And we're going to create 100 X values
  319. 12:26between zero and 10,
  320. 12:28which is including zero and 10. So, we
  321. 12:30You see, we have X from zero to 10, and
  322. 12:32in between we have 100 small dots.
  323. 12:36And for each of these, we're going to
  324. 12:38just calculate the Y correspond
  325. 12:40corresponding Y values. So, 2 *
  326. 12:442 * uh these X values.
  327. 12:48So, Y will also be an array of 100
  328. 12:50numbers, but each element will be two
  329. 12:55times the corresponding element in the
  330. 12:57X. So, if it's three, it's going to be
  331. 12:59six here. Four will be eight, etc.
  332. 13:03And then, we just use this to draw.
  333. 13:07And that's how we draw the linear
  334. 13:08function here.
  335. 13:09And let's see in if instead of having
  336. 13:12number two here, we have some other
  337. 13:14numbers.
  338. 13:15So,
  339. 13:17Y is equal to 1 X, 2 X, 3 X, 0.5 X, -1
  340. 13:21X. So, this will This number will
  341. 13:25determine the slope of this linear
  342. 13:27function.
  343. 13:29The 3 X, which is the highest number
  344. 13:32here, will also have the highest slope.
  345. 13:35The lowest number, minus one, will have
  346. 13:38kind of highest negative slope in
  347. 13:41negative
  348. 13:42uh direction on negative X.
  349. 13:46For example, if we look at Y equals X,
  350. 13:49here where X is four,
  351. 13:53Y will also be four.
  352. 13:56So then, that's the slope as well.
  353. 14:00And the way this is coded is very
  354. 14:01simple. There is a list of K values.
  355. 14:06And then, we just plot for each
  356. 14:09uh of the K values, we plot the
  357. 14:10function.
  358. 14:13I recommend using uh AI, ChatGPT, or
  359. 14:16Cursor to explain any of these things
  360. 14:19for you if you don't understand it.
  361. 14:21But, I want to focus on the theoretical
  362. 14:23part of the math for now. So, this
  363. 14:26coefficient determines the slope of the
  364. 14:28function. Then, there is quadratic
  365. 14:30function,
  366. 14:31where Y is equal to X squared.
  367. 14:34So, as we go on
  368. 14:38X slowly, the Y will become a lot
  369. 14:41bigger. We'll start to accelerate.
  370. 14:44So, in the beginning, Y doesn't grow so
  371. 14:47quickly, but later, uh when we go from
  372. 14:50two squared, three squared, four
  373. 14:52squared, five squared, the Y starts to
  374. 14:55become a lot bigger, a lot faster.
  375. 14:58Here,
  376. 14:59like zero squared is zero, one squared
  377. 15:02is one, two squared, four, three
  378. 15:04squared, nine, four squared, 16. So, Y
  379. 15:07starts to grow very fast. You can play
  380. 15:09around with this code
  381. 15:11to see and to modify to see uh what the
  382. 15:14drawing will look like.
  383. 15:16Now, notice that uh Y is always positive
  384. 15:19here. So, Y never goes down below zero
  385. 15:22on Y axis. We never go down. Uh because
  386. 15:25uh square function, quadratic function,
  387. 15:27will always make everything positive.
  388. 15:29Because if you multiply
  389. 15:31two negative numbers or negative number
  390. 15:33with itself, it's going to become a
  391. 15:34positive number. So, if you square a
  392. 15:36negative number, like minus two here, it
  393. 15:38will become a positive number.
  394. 15:41But, that's not true for the cubic
  395. 15:43function,
  396. 15:45where
  397. 15:46uh putting uh negative number cubed will
  398. 15:48give you negative number.
  399. 15:51So, this is what cubic function looks
  400. 15:53like. And
  401. 15:54it grows even faster
  402. 15:56than
  403. 15:58square function. Here at three, we are
  404. 16:01already at 27
  405. 16:03uh on Y.
  406. 16:06And three was just nine
  407. 16:08in the square
  408. 16:10here.
  409. 16:12So, cubic grows even faster, but there
  410. 16:14is also negative part of it. So, this is
  411. 16:15the shape of the cubic
  412. 16:17function. This is the shape of the
  413. 16:19square function. So, knowing these
  414. 16:21shapes is good.
  415. 16:23Square root function. So, Y is equal to
  416. 16:26square root of X.
  417. 16:28Uh I'm going to have the same
  418. 16:31code here. So, I put some numbers.
  419. 16:34Uh you can only find roots of positive
  420. 16:37numbers, by the way. You cannot find
  421. 16:38roots of negative numbers. It's not
  422. 16:40defined or it's not real, I should say.
  423. 16:44And so,
  424. 16:45this is what
  425. 16:47uh Y equals
  426. 16:49square root of X looks like.
  427. 16:52This is also good to know, this kind of
  428. 16:54function.
  429. 16:56It's growing very fast in the beginning,
  430. 16:59but then it's starting to grow slower
  431. 17:01and slower.
  432. 17:04Although, this will grow to infinity as
  433. 17:06well.
  434. 17:09So, there is no limit here. It will also
  435. 17:12grow forever.
  436. 17:13Uh as X grows forever, Y will also grow
  437. 17:15forever.
  438. 17:17Then, we have exponential function,
  439. 17:19where Y is equal to two power X or some
  440. 17:24some number
  441. 17:25some constant power X.
  442. 17:28And this grows very fast.
  443. 17:31This grows faster than the quadratic.
  444. 17:33So, two power X grows faster than X
  445. 17:36power two.
  446. 17:38It's good to know that.
  447. 17:39And this is the shape of exponential
  448. 17:41function.
  449. 17:42The code is very similar. This part is
  450. 17:45just different. So, Y is equal to two
  451. 17:47power X. This is how you can denote
  452. 17:50power in
  453. 17:51uh Jupyter
  454. 17:53or Python.
  455. 17:54We also have sine function. So,
  456. 17:57uh Y is equal to sine X, and I'll show
  457. 18:00you first that this is a periodic
  458. 18:01function.
  459. 18:04And this function is important. It's a
  460. 18:06trigonometric function. It's used
  461. 18:07everywhere. It's used so often.
  462. 18:10And in Python, you can just write Y is
  463. 18:12equal to math.sin of X.
  464. 18:16Now, let's go back to the linear
  465. 18:17function. So, this is also a linear
  466. 18:19function.
  467. 18:20So, we have some uh 2 * X, which is
  468. 18:24coefficient and the variable plus some
  469. 18:26constant or minus some constant that
  470. 18:28doesn't have * X in it or
  471. 18:32So, if you change this number three,
  472. 18:36make it four, five, six, you will just
  473. 18:38move this function left or right. So,
  474. 18:41the slope will be same. So,
  475. 18:44if you change this number two, then you
  476. 18:46will change the slope of the function.
  477. 18:48But, if you change this number three,
  478. 18:50you will just move it left or right.
  479. 18:53This is also important to know.
  480. 18:55And now, we may take a look at these
  481. 18:58functions.
  482. 19:01And so, that's going to be math
  483. 19:02functions. I recommend you play with
  484. 19:04this. Make sure you understand uh this,
  485. 19:07and we can go on to the next lesson.
  486. 19:10Next, we have derivatives. I'm going to
  487. 19:12try to explain the main points of
  488. 19:15derivatives.
  489. 19:16So, if you have a So, if you have a
  490. 19:18function, you can find the derivative of
  491. 19:21that function
  492. 19:24that shows the rate of change of the
  493. 19:27function, how quickly it grows at each
  494. 19:30point. So, you see, this linear function
  495. 19:34is always growing at the same rate. So,
  496. 19:36this function is not like quadratic,
  497. 19:38where it's growing a bit slower here, a
  498. 19:41bit faster here, etc.
  499. 19:43This linear function is always growing
  500. 19:45at the same rate.
  501. 19:47In this case, the derivative is going to
  502. 19:49be equal to this number two.
  503. 19:53So, if we have 2x, the way I'll show you
  504. 19:56later how to calculate derivatives, but
  505. 19:58the derivative will be this two. So, at
  506. 20:01any point
  507. 20:03when you increase x by a little bit,
  508. 20:05when you go in the x forward by a little
  509. 20:07bit, the y will go by two times that
  510. 20:11amount you went on x.
  511. 20:14So, you move x a little bit, the y will
  512. 20:16go up by two times. So, that's
  513. 20:19So, here you can see the derivative is
  514. 20:21two at any x. So, at any x here, the
  515. 20:25derivative is always two.
  516. 20:27Because it's a linear function, the
  517. 20:28growth is always the same.
  518. 20:31Can you try to explain yourself what
  519. 20:34derivative is? Can you try to explain
  520. 20:35what I just explained uh yourself?
  521. 20:38So, derivative shows rate of change.
  522. 20:41And it's linear function, so rate of
  523. 20:42change is always the same, and it's two
  524. 20:44in this case. It's equal to this number
  525. 20:46two of 2x.
  526. 20:49So, in my code, I have the function f of
  527. 20:52x, and the derivative is just hardcoded
  528. 20:55here. It's always two. Because
  529. 20:58I'll show you later how to calculate it,
  530. 21:00but this will be derivative two.
  531. 21:03Let's see derivative of this quadratic
  532. 21:05function, y is equal x squared.
  533. 21:08So, the derivative here will be 2x.
  534. 21:13And I'm going to explain how it's
  535. 21:15calculated, but we can see
  536. 21:19that here the function doesn't grow
  537. 21:22equally everywhere. So,
  538. 21:24actually, as we increase or go further
  539. 21:28into negative
  540. 21:29uh on x, the y will start growing a lot
  541. 21:33faster. Here around zero, x is growing,
  542. 21:36but y is not growing up so much.
  543. 21:39But as we uh grow x, the y grows a lot
  544. 21:42faster.
  545. 21:44So, now we don't have a constant
  546. 21:46derivative that's always the same, like
  547. 21:48two, the constant growth.
  548. 21:50Here we have
  549. 21:52uh derivative looks like a linear
  550. 21:54function. So, derivative here is 2x.
  551. 21:57So, the derivative itself can be drawn
  552. 21:59as a function, by the way. So, we have
  553. 22:02one
  554. 22:03and two
  555. 22:05for this spot.
  556. 22:08So, funnily enough, you can also take
  557. 22:10derivative of this derivative. So, that
  558. 22:12would be second derivative. So,
  559. 22:14here the growth will be just two.
  560. 22:17Derivative of 2x will be just two.
  561. 22:19But let's not get confused. So, here we
  562. 22:21have the function x squared, the
  563. 22:23derivative is 2x. Because the formula
  564. 22:26is
  565. 22:28you
  566. 22:29put this So, if you have x to n, you put
  567. 22:32this number two, number n in front of x,
  568. 22:35so you get 2x.
  569. 22:37You will just put this number in front
  570. 22:38of x, so you get 2x.
  571. 22:40And you subtract one from this number up
  572. 22:43here. So, 2 - 1 will be one. So,
  573. 22:46you will have x to power one. So, that's
  574. 22:49just x.
  575. 22:51So, the derivative of this is 2 * x
  576. 22:53power one, which is just 2x.
  577. 22:57That's the formula for calculating
  578. 22:58derivatives. You can also check other
  579. 23:00tutorials. Now, this can go quite deep.
  580. 23:04Um
  581. 23:05I think it would be good if you knew it,
  582. 23:08but it's not absolutely essential to
  583. 23:09master this. I'm just explaining These
  584. 23:12are the things you need to understand.
  585. 23:14Uh you also learn this in high school.
  586. 23:18So, you will encounter this in some
  587. 23:19research papers, and it's good to know
  588. 23:22uh this stuff. Now, do you want to dive
  589. 23:23deeper right now or later? It's up to
  590. 23:25you.
  591. 23:27So, what this derivative means is that
  592. 23:30um
  593. 23:31any of these points, wherever you pick
  594. 23:34x, for example, if you look at x = 2,
  595. 23:37then that means here on this original
  596. 23:39function, if you if at x2, you increase
  597. 23:43x by just a little bit,
  598. 23:46then the y is will increase by
  599. 23:50four.
  600. 23:51You see here, it's number four. So,
  601. 23:54if you increase x by a bit, the y will
  602. 23:57increase four times.
  603. 24:00Because at this particular
  604. 24:04uh place, let's calculate derivative.
  605. 24:07Derivative is 2 * x, so 2 * 2 is four.
  606. 24:12So, at this particular place, if you
  607. 24:14increase x by a little bit, the y will
  608. 24:17increase four times that little bit you
  609. 24:19of your increase. Here,
  610. 24:21at three, derivative is six.
  611. 24:24So, at here at three, if you increase x
  612. 24:27by a little bit,
  613. 24:28the y will increase six times that
  614. 24:31little bit.
  615. 24:32And so, you can see that y is increasing
  616. 24:34faster and faster as you increase x.
  617. 24:38So, this may require a bit of practice,
  618. 24:40a bit of maybe pen and paper. You can uh
  619. 24:43try to think about this yourself. Maybe
  620. 24:45watch some specific tutorials on
  621. 24:47derivation if you
  622. 24:48uh wish, so now or maybe even later.
  623. 24:52So, we have uh this quadratic function,
  624. 24:54and I want to show you at each of these
  625. 24:57points, whichever points we pick, 0 1 2
  626. 25:01uh on the x-axis,
  627. 25:04each point has its slope.
  628. 25:08So, uh because it's quadratic function,
  629. 25:11we can see that there is no slope at
  630. 25:13zero. X = 0, there is no slope, because
  631. 25:15y is zero, x is zero here.
  632. 25:18Uh at one, there is a slope. At x = 2,
  633. 25:23there is even higher slope.
  634. 25:26>> [snorts]
  635. 25:26>> So, this slope is basically the
  636. 25:29derivative. I told you that
  637. 25:32at number at x = 2, if you increase x by
  638. 25:35a little bit, by 0.001, for example,
  639. 25:38the y will increase by four times that
  640. 25:40amount.
  641. 25:43Now, I'm not going to tell you that if
  642. 25:45you increase
  643. 25:47here x by one, so you go from two to
  644. 25:50three, the y will increase by four.
  645. 25:52Because
  646. 25:53I'm not going to tell you that, because
  647. 25:55as you move towards three, the y rate of
  648. 25:58change will also be increasing.
  649. 26:02Keep in mind that the slope will change.
  650. 26:06That's why I say if you increase So,
  651. 26:09just around this two, a little bit
  652. 26:11around this number two, the slope is
  653. 26:14four. Okay? As you move towards three,
  654. 26:17the slope will become six. It will
  655. 26:19increase.
  656. 26:21So, that's why I say
  657. 26:23if you just increase
  658. 26:25uh x by a little bit here, the slope
  659. 26:27will increase by this amount, four times
  660. 26:30or six times. But if you increase x by a
  661. 26:32bit, by by a lot more,
  662. 26:35then the slope will also change.
  663. 26:38So, in summary, the derivative will show
  664. 26:40you the slope. It will show you rate of
  665. 26:42change around this uh point.
  666. 26:46And for this particular function, the
  667. 26:48slope or the derivative will be equal to
  668. 26:502x.
  669. 26:53So,
  670. 26:54uh here if x is one, the slope is two.
  671. 26:58If x is two, slope is four. If x is
  672. 27:00three, slope is six, etc.
  673. 27:03Now, let's see some derivation rules.
  674. 27:06So, if you have
  675. 27:08x power three, you're going to put this
  676. 27:10three in front of the x, so you get So,
  677. 27:13the derivative will be 3 * x.
  678. 27:16And also,
  679. 27:17you need to subtract one from this
  680. 27:19exponent. So,
  681. 27:213 - 1 is two, so the final derivative
  682. 27:24will be
  683. 27:253 * x power two. And it's just x power
  684. 27:29two. So, it's x power two times three.
  685. 27:32And so, you can see these examples, like
  686. 27:34uh
  687. 27:35derivative of x power five is going to
  688. 27:37be 5 *
  689. 27:39x power four.
  690. 27:41You just subtract one here, so it's x
  691. 27:43power four times five.
  692. 27:45And this four is not like it's not the
  693. 27:47whole thing. It's just x power four.
  694. 27:52And so, if we have a function y equals
  695. 27:54uh x cubed or x power three, then
  696. 27:58the
  697. 28:03according to the rules.
  698. 28:04So, this is the derivative function.
  699. 28:06It's 3 * x squared.
  700. 28:09And so, we already know that
  701. 28:11x squared
  702. 28:13it's going to be like this U shape. We
  703. 28:16discussed that earlier.
  704. 28:18And then 3 * x squared, so it will just
  705. 28:23make the y's even larger. It will maybe
  706. 28:27uh squish it like So, this coefficient
  707. 28:30will just change this uh how wide or
  708. 28:32thin
  709. 28:33the U shape is.
  710. 28:36But basically, because we have x
  711. 28:39squared, we're going to have U shape.
  712. 28:42So, that's a derivative. It shows how
  713. 28:44the function is changing at any of these
  714. 28:48points.
  715. 28:50Some other derivation rule. So, if you
  716. 28:52have
  717. 28:53a function 2x + 3, you can find
  718. 28:56derivatives of each of these
  719. 28:58uh separately. So, around plus or minus,
  720. 29:01so you can find derivatives of 2x and
  721. 29:04derivative of
  722. 29:05constant three.
  723. 29:07And uh the rule is that
  724. 29:09a constant three, any constant, becomes
  725. 29:12zero.
  726. 29:13So, it will just be plus zero. So, we
  727. 29:15can omit that.
  728. 29:17And derivative of 2x
  729. 29:19will be
  730. 29:20So, uh the rule is the following.
  731. 29:23If you have constant times variable, the
  732. 29:26constant will remain the same. So, two
  733. 29:29will remain the same, because it's times
  734. 29:31variable, okay? Here,
  735. 29:33this constant two is not separate. So,
  736. 29:35here three doesn't have any variable
  737. 29:37attached to it. Three is not multiplying
  738. 29:39a variable, so three disappears. It
  739. 29:40becomes zero.
  740. 29:42But here, constant two is multiplying a
  741. 29:44variable. So, it remains two. In the
  742. 29:47derivative, this constant two will
  743. 29:48remain
  744. 29:49two.
  745. 29:51It's explained well in ChatGPT. So, as I
  746. 29:53said, derivative of a constant, it's a
  747. 29:55alone constant that doesn't doesn't
  748. 29:57multiply
  749. 29:58variable. Like here, two, constant two
  750. 30:01multiplies variable. So, this is not a
  751. 30:02alone constant. So, if you have a alone
  752. 30:05constant doesn't multiply variable, then
  753. 30:07derivative is zero. So, this part will
  754. 30:09be completely
  755. 30:11deleted or omitted.
  756. 30:12And then, derivative of 2x is two. Let's
  757. 30:16see how.
  758. 30:18So, 2x is equal to 2 * x ^ 1.
  759. 30:22And we know that I explained earlier
  760. 30:26uh that if we find derivative So, first
  761. 30:29of all,
  762. 30:30because we have constant times variable,
  763. 30:32we will just
  764. 30:33This constant will remain the same. So,
  765. 30:35in the derivative, this constant will
  766. 30:37not change. So, in the derivative,
  767. 30:39this will still be two times something,
  768. 30:41okay? So, now let's see what that
  769. 30:43something is.
  770. 30:45I told you the rule is
  771. 30:47whatever power you have here, you put it
  772. 30:49in front of the x.
  773. 30:51So, one will go in front of the x.
  774. 30:55And then you will subtract one from
  775. 30:57whatever power we have here. So, we have
  776. 30:591 - 1. So,
  777. 31:021 * 1 - 1. So, that's 1 * x ^ 0.
  778. 31:08x ^ 0 is just 1.
  779. 31:10So, this is equal to one. Any number to
  780. 31:11zero is equal to one.
  781. 31:13So, that's going to be one. Okay?
  782. 31:16So, then I told you
  783. 31:18we will just get 2 * 1 because
  784. 31:22as I said, two multiplies variable, so
  785. 31:25it remains the same in the derivative.
  786. 31:28And then we apply this rule x to the x
  787. 31:30that I explained, so it will be just 2 *
  788. 31:321.
  789. 31:36And so, this derivative of 2x is two.
  790. 31:40So, at the end, derivative of this is
  791. 31:42two, derivative of this is zero, so we
  792. 31:44get 2 + 0.
  793. 31:46We get derivative of this whole thing is
  794. 31:50two.
  795. 31:51So, you can practice this. This is done
  796. 31:53in high school, university. You can
  797. 31:54practice this yourself if you want uh
  798. 31:57with ChatGPT or some other AI or
  799. 31:59YouTube videos.
  800. 32:01And I can show it to you here. So, the
  801. 32:03function is 2x + 3. The derivative is
  802. 32:06two. It's always two for any x.
  803. 32:09Uh when whenever you increase x, the y
  804. 32:11will increase by two times
  805. 32:14whatever the increase in x was
  806. 32:17in this function
  807. 32:19because the derivative here is always
  808. 32:21two.
  809. 32:22So, this means that +3 constant doesn't
  810. 32:24affect the slope of the function. As I
  811. 32:27said,
  812. 32:28the slope is determined by this number
  813. 32:29two.
  814. 32:31And so, if I change this number two, the
  815. 32:33slope of the function will change. But
  816. 32:35this constant +3, this will just move
  817. 32:38the function left or right. So, this
  818. 32:41constant +3 will not change the slope.
  819. 32:43So, it So,
  820. 32:46it makes sense that it disappears in the
  821. 32:48derivative as well. It becomes zero. It
  822. 32:49doesn't affect the slope.
  823. 32:52And now I just want to summarize.
  824. 32:54Derivative, so we have linear function,
  825. 32:56the derivative is constant.
  826. 32:58We have quadratic function, so x ^ 2,
  827. 33:02the uh derivative is linear, so x ^ 1 is
  828. 33:05the derivative.
  829. 33:07We have cubic function, x ^ 3,
  830. 33:10the derivative will be x ^ 2. It will be
  831. 33:12quadratic. So, the derivative is kind of
  832. 33:14one degree less
  833. 33:17than the function.
  834. 33:19So, that's going to be the core
  835. 33:20knowledge you need to know about
  836. 33:22derivation. Uh you will eventually learn
  837. 33:24more as you read papers, but for now,
  838. 33:27this is enough.
  839. 33:29Third lesson, let's learn about vectors.
  840. 33:32So, this is ex- This is maybe the most
  841. 33:33important one besides matrices as well.
  842. 33:37Uh everything is in vectors in machine
  843. 33:39learning, neural networks.
  844. 33:41First, we will look at a vector as just
  845. 33:43an array of numbers.
  846. 33:45So, here we're going to create a NumPy
  847. 33:48array. Hopefully, uh you understand what
  848. 33:51NumPy is. You can watch YouTube if you
  849. 33:53don't understand this Python. So, these
  850. 33:55are fundamentals. You can
  851. 33:57uh learn that first.
  852. 33:59So, array of just two numbers in this
  853. 34:01case, three and four. Uh vector can be
  854. 34:05uh array of any
  855. 34:07amount of numbers.
  856. 34:10And
  857. 34:11we can also look at these numbers as
  858. 34:14coordinates on a coordinate system.
  859. 34:17So, x is three, y is four.
  860. 34:20We can look at that this way if we want.
  861. 34:23So, this blue example, it's going to be
  862. 34:27x is three here, three, and y is four.
  863. 34:31So, that's the vector, the spot. So, we
  864. 34:34can draw vector by
  865. 34:36going from zero to that spot.
  866. 34:39And you can see that vector is like an
  867. 34:41arrow.
  868. 34:42So, vector has a direction and length.
  869. 34:46Direction and length.
  870. 34:49We can also look at a vector as going
  871. 34:51like
  872. 34:52uh three units this way and then four
  873. 34:55units this way to end up at this spot.
  874. 34:59And here we have two other examples of
  875. 35:01minus So, minus two, one.
  876. 35:04So, going minus two on x and one on y.
  877. 35:08Minus two, one. And zero, five. So, zero
  878. 35:12on x, five on y. So, this vector will be
  879. 35:16on the x Sorry, on the y axis.
  880. 35:19Then let's look at vector addition. So,
  881. 35:21if you have vector three, two
  882. 35:24and vector one, three, if you add them,
  883. 35:26you're going to say you add the first
  884. 35:28elements, so you add 3 + 1, the first
  885. 35:31elements
  886. 35:33will be four. And the second elements,
  887. 35:362 + 3, will be five.
  888. 35:39And uh we can also add vectors visually.
  889. 35:44Starting from the first vector, three,
  890. 35:46two,
  891. 35:47going from zero to three, two,
  892. 35:49and the second vector, one, three. Now,
  893. 35:53this vector would go here from zero,
  894. 35:54zero to
  895. 35:56one, three like this here. So, this
  896. 35:58would be this second vector.
  897. 36:02Uh and then we will just move this
  898. 36:04second vector to the end of this first
  899. 36:06vector if we want to add it visually.
  900. 36:09And so, wherever it ends up,
  901. 36:11that's going to be our resulting vector.
  902. 36:14So, the resulting vector will be four,
  903. 36:17five.
  904. 36:18We can draw it from origin to four, five
  905. 36:21if we want.
  906. 36:22So, that's how you can add vectors
  907. 36:24visually as well.
  908. 36:26Let's see multiplying vector with a
  909. 36:28scalar. So, if we have vector two, one,
  910. 36:31what happens if we say
  911. 36:33two times that vector, 0.5 times that
  912. 36:36vector, minus one times that vector. So,
  913. 36:39this blue is our original vector.
  914. 36:42Um
  915. 36:43So, it goes from zero to two, one. Now,
  916. 36:46multiplying with a scalar will uh
  917. 36:48stretch or squish or inverse, so make it
  918. 36:52go in the other direction.
  919. 36:55So, two times
  920. 36:57vector two, one will give us vector
  921. 36:59four, two. So,
  922. 37:01you multiply each of the elements.
  923. 37:03So, two times
  924. 37:05v is going to be 2 * 2, it's four, and 2
  925. 37:07* 1, it's two.
  926. 37:10So, the resulting vector will actually
  927. 37:13be two times
  928. 37:15uh longer and it will have same
  929. 37:17direction.
  930. 37:20And multiplying with 0.5, it will just
  931. 37:23make it two times shorter instead.
  932. 37:26>> [snorts]
  933. 37:26>> Multiplying it with minus one
  934. 37:29will have the same length, just the
  935. 37:31opposite direction.
  936. 37:34So, it will be minus two, minus one.
  937. 37:37How do we calculate uh
  938. 37:39length or magnitude of vector? How long
  939. 37:42is this vector?
  940. 37:43So, it's actually similar to Pythagorean
  941. 37:47So, you can just say x ^ 2 + y ^ 2.
  942. 37:51You add them up.
  943. 37:52And then you get the square root of the
  944. 37:55result.
  945. 37:57Uh but in Pythagorean theorem, you just
  946. 37:59have two numbers, but here you can have
  947. 38:01any amount of dimensions. So, you can
  948. 38:03have x ^ 2 + y ^ 2 + z ^ 2 + a ^ 2. So,
  949. 38:08you just add all of the squares of
  950. 38:10every dimension.
  951. 38:12And at the end, you take
  952. 38:15the square root of
  953. 38:17of that sum.
  954. 38:19So, let's say we have a vector that has
  955. 38:21four dimensions.
  956. 38:24Let's say this is the vector.
  957. 38:27Uh and then we just square each
  958. 38:29dimension, sum them up.
  959. 38:31Let's say it's 30, the square of the sum
  960. 38:34of all the squares, and then take the
  961. 38:36root of 30, and that's the length, the
  962. 38:38magnitude of your vector.
  963. 38:41So, it's always the square root. It's
  964. 38:43the It's not like fourth root here, it's
  965. 38:45square root always.
  966. 38:48Here are some examples. If we have
  967. 38:49vector one, one,
  968. 38:51the magnitude will be 1.41. So, that's
  969. 38:55the length, the magnitude of the vector.
  970. 38:57So,
  971. 38:58you can see here
  972. 39:00that this is a bit longer than this
  973. 39:05uh one unit.
  974. 39:07So, it's about 1.41.
  975. 39:10Here, the vector zero, five, the
  976. 39:12magnitude will be five here.
  977. 39:15You can already see that the length of
  978. 39:16this is five.
  979. 39:19So, okay. And then vector three, four,
  980. 39:22um
  981. 39:23three, four, the magnitude, the length
  982. 39:27of the vector will be five units.
  983. 39:30And if you remember from Pythagorean
  984. 39:32Pythagorean theorem, if you have
  985. 39:34triangle with sides three, four, the
  986. 39:37hypotenuse will be
  987. 39:39uh five.
  988. 39:41But what's going to happen often is if
  989. 39:43you have some vector that has some
  990. 39:45length, you will want to squish it back
  991. 39:48so it has unit length, length of one.
  992. 39:52>> [snorts]
  993. 39:53>> Uh this is to make vectors in neural
  994. 39:55networks stable. They don't because you
  995. 39:57are multiplying a bunch of them so
  996. 39:59they're going to start exploding. The
  997. 40:01resulting vectors will become
  998. 40:02exponentially or quadratically or a lot
  999. 40:06bigger and bigger and bigger as you
  1000. 40:07multiply them or they will become a lot
  1001. 40:09smaller and smaller and smaller and
  1002. 40:11vanish.
  1003. 40:12So you will want to normalize vectors.
  1004. 40:14You will want to make vectors of unit
  1005. 40:17length, of length one.
  1006. 40:19Let's say we have a vector 3 1.
  1007. 40:23You will find its
  1008. 40:25length, which is going to be 3.16 in
  1009. 40:27this case.
  1010. 40:31And you will just divide this vector
  1011. 40:34with its length, with its magnitude.
  1012. 40:38And when you divide, so
  1013. 40:40dividing it means you will divide each
  1014. 40:42dimension with the magnitude separately.
  1015. 40:45>> [snorts]
  1016. 40:46>> And then this new vector it's going to
  1017. 40:49be unit length so it will have same
  1018. 40:51direction, same direction. It will just
  1019. 40:53have unit length. It will not be
  1020. 40:56it will have be long one, one unit.
  1021. 40:59Whatever the unit is.
  1022. 41:03So neural networks like numbers around
  1023. 41:05one because when you multiply a bunch of
  1024. 41:07those numbers, it will not start to the
  1025. 41:10result will not become very huge or very
  1026. 41:13small, which is not good for graphic
  1027. 41:15cards. They cannot represent very huge
  1028. 41:17numbers or very small uh very big
  1029. 41:19numbers or very small numbers. So you
  1030. 41:21want to keep your numbers around one,
  1031. 41:23zero, minus one, something like that.
  1032. 41:26Now let's understand dot product. So dot
  1033. 41:29product shows you how similar two
  1034. 41:31vectors are.
  1035. 41:33Here on this image, you can see that we
  1036. 41:35have a vector A and vector B.
  1037. 41:38And uh these two vectors are closer
  1038. 41:40together so which means which means they
  1039. 41:42are more similar
  1040. 41:44than here where there is 90° angle
  1041. 41:47between them
  1042. 41:48or here where they are very separate.
  1043. 41:52So these two vectors are most similar.
  1044. 41:54These are less similar. These are least
  1045. 41:56similar.
  1046. 41:57Maybe I should say aligned instead of
  1047. 42:00similar, but uh word similar is also
  1048. 42:02used very frequently, even maybe even
  1049. 42:04more frequently.
  1050. 42:06So it's very simple.
  1051. 42:08We will multiply
  1052. 42:10>> [snorts]
  1053. 42:10>> first dimensions. So here
  1054. 42:13we have a vector, let's say, 3 4 and the
  1055. 42:16second vector is 1 2.
  1056. 42:18To find dot product or similarity
  1057. 42:21uh be- between them,
  1058. 42:23we multiply 3 * 1. So we multiply the
  1059. 42:25first dimensions and then we say plus
  1060. 42:29multiplying second dimensions 4 * 2.
  1061. 42:32Okay? So
  1062. 42:333 * 1 is 3
  1063. 42:35plus
  1064. 42:374 * 2 is 8.
  1065. 42:39So 3 + 8 is 11. So dot product between
  1066. 42:42these two is 11.
  1067. 42:46So if our number is positive, like 11,
  1068. 42:50then they are pointing in the same
  1069. 42:51direction. Okay? If the two vectors are
  1070. 42:54perpendicular, 90° angle between them,
  1071. 42:58then the dot product will be zero. Okay?
  1072. 43:00So if you get the dot product zero, you
  1073. 43:02know that these vectors are
  1074. 43:03perpendicular and this is
  1075. 43:05useful in neural optimizer. Uh this is
  1076. 43:08this may be the new way to make uh
  1077. 43:11neural networks learn a lot faster,
  1078. 43:13better. I'm going to talk about this uh
  1079. 43:15in a different part of the course.
  1080. 43:19So just know that
  1081. 43:21if vectors are 90° between each other,
  1082. 43:24they are they will have dot product
  1083. 43:26zero. And if you have dot product zero,
  1084. 43:28that means the vectors are 90°.
  1085. 43:31And if dot product is negative, then the
  1086. 43:34vectors are even further away than 90°
  1087. 43:37between them.
  1088. 43:39So they are even more different or
  1089. 43:41dissimilar. In Python, we can calculate
  1090. 43:43dot product by just calling np.
  1091. 43:47dot
  1092. 43:48v1 v2, vector one, vector two.
  1093. 43:52So uh vectors
  1094. 43:54v1 and v2 that we calculated manually
  1095. 43:57have dot product of 11.
  1096. 44:00Now these two vectors, 3 4 and minus 4
  1097. 44:023, have dot product of zero, which means
  1098. 44:06they are perpendicular. So
  1099. 44:08v1 and v3, you can see that the angle
  1100. 44:11between them is 90°.
  1101. 44:13So they are perpendicular so dot product
  1102. 44:15will be zero.
  1103. 44:18And then
  1104. 44:20uh v1 v4, so v4 is actually minus 3
  1105. 44:24minus 4, so
  1106. 44:26the blue and the yellow, they are very
  1107. 44:29different
  1108. 44:30because they point in very different
  1109. 44:33directions, opposite directions.
  1110. 44:36So their dot product will be like
  1111. 44:38negative number, minus 25. It's going to
  1112. 44:40be large negative number number here
  1113. 44:42because they are very different.
  1114. 44:46Subtracting vectors is very easy. If you
  1115. 44:49have vector 4 3, you want to subtract
  1116. 44:52vector 1 2, you will just subtract each
  1117. 44:55element
  1118. 44:57from each so 4 minus 1,
  1119. 45:00that's going to be your first element,
  1120. 45:01which is going to be now three.
  1121. 45:03And the second element will be result of
  1122. 45:063 minus 2, which is going to be now one.
  1123. 45:09So the new resulting vector is
  1124. 45:123 1.
  1125. 45:15We can also show that visually.
  1126. 45:18So if we have
  1127. 45:20uh this blue vector, which is 4 3
  1128. 45:24and we have red vector, which is 1 2,
  1129. 45:29uh we can
  1130. 45:32draw a vector from the end of this one
  1131. 45:35to the end of the first one
  1132. 45:37and then move it back to the origin.
  1133. 45:40And that's going to be our new vector,
  1134. 45:42which is going to be uh 3 1,
  1135. 45:45our resulting vector.
  1136. 45:47So you will start from the second vector
  1137. 45:50that you are subtracting uh moving going
  1138. 45:53to the end of first vector from which
  1139. 45:55you are subtracting, that's your new
  1140. 45:57vector. So then you can move it to the
  1141. 45:59origin to see its real coordinates.
  1142. 46:02And that will be it for vectors and see
  1143. 46:05you in the next lesson.
  1144. 46:06Let's talk about gradients. Let's say we
  1145. 46:10have some surface like this.
  1146. 46:12So at any point of this surface,
  1147. 46:15you would want to know
  1148. 46:17where is in which direction to move to
  1149. 46:20go up, in which direction to move to go
  1150. 46:22down.
  1151. 46:23This is used in neural networks to know
  1152. 46:28uh if for example, this surface would
  1153. 46:30show error. So you want to make errors
  1154. 46:33small.
  1155. 46:35So you want to go down on this surface.
  1156. 46:37So how do you
  1157. 46:39in which direction to move to go down on
  1158. 46:41this surface? Now I know if you've never
  1159. 46:43heard of loss function
  1160. 46:45uh and in neural networks, then this is
  1161. 46:46confusing like how is this showing an
  1162. 46:49error, but we will talk about this
  1163. 46:51later. But just know that
  1164. 46:53uh
  1165. 46:54if you have some surface, you're going
  1166. 46:55to use gradients to know which way is
  1167. 46:59you need to move to go up, which way you
  1168. 47:01need to move to go down.
  1169. 47:02Because the gradient will show you the
  1170. 47:05direction of the steepest ascent. So in
  1171. 47:08this direction, if you want to go up
  1172. 47:10fastest.
  1173. 47:12So that means that in the opposite
  1174. 47:14direction, you're going to go down
  1175. 47:15fastest
  1176. 47:16for this particular for this point where
  1177. 47:18you are standing, so very near around
  1178. 47:21that point.
  1179. 47:23So that's what gradients are used for.
  1180. 47:25For every point on the surface, they
  1181. 47:27show you in which direction
  1182. 47:30uh it's ascending, increasing. And so in
  1183. 47:33neural network, we will just go in the
  1184. 47:34opposite direction to decrease because
  1185. 47:36in neural network, it will be used
  1186. 47:39to represent this surface will be used
  1187. 47:41to represent our error,
  1188. 47:43which we want to minimize the error.
  1189. 47:46And gradients are closely related to
  1190. 47:48derivatives. You know that derivatives
  1191. 47:50are showing you the slope.
  1192. 47:52So in gradients, you will also use
  1193. 47:54derivatives to see the slope.
  1194. 47:58But we will use partial derivatives. So
  1195. 48:00let's see what those are.
  1196. 48:02In this case, we have a 2D surface, so
  1197. 48:04two dimensions. We have X dimension and
  1198. 48:06Y dimension.
  1199. 48:08And we want to take partial derivative
  1200. 48:10with respect to X and partial derivative
  1201. 48:13with respect to Y. So what does that
  1202. 48:14mean? So
  1203. 48:16if we stand at any point,
  1204. 48:19if we just move, let's say,
  1205. 48:21on uh on X direction, positive X
  1206. 48:23direction. We increase X 1 2 3 4. Do we
  1207. 48:26go up or down on this surface?
  1208. 48:30So we want to examine if we move
  1209. 48:33on X, so how when we change X,
  1210. 48:36do we go up or down? Are we ascending or
  1211. 48:38descending? So
  1212. 48:40that's going to depend on where we stand
  1213. 48:42at the surface.
  1214. 48:44So we will take partial derivative of
  1215. 48:46this function, this loss function or
  1216. 48:48this surface
  1217. 48:50with respect to X
  1218. 48:53at that particular So we will know So
  1219. 48:55when we plug in our particular X
  1220. 48:58coordinate
  1221. 48:59in which we are currently standing into
  1222. 49:01that partial derivative
  1223. 49:04with respect to X, we will know if
  1224. 49:07for example, if we and when we increase
  1225. 49:09X, if
  1226. 49:11the function will increase or decrease.
  1227. 49:14So that's the partial derivative with
  1228. 49:15respect to X. And it's partial because
  1229. 49:19there is also Y. So we need So we have
  1230. 49:22two partial derivatives with respect to
  1231. 49:23X and with respect to Y.
  1232. 49:25So then for that same position where we
  1233. 49:28calculate
  1234. 49:29if we increase X, does the loss increase
  1235. 49:32or decrease,
  1236. 49:33we will also calculate for Y
  1237. 49:36if we increase Y, does the loss increase
  1238. 49:38or decrease?
  1239. 49:41So, if we find that
  1240. 49:43as we increase Y coordinate, the loss
  1241. 49:46will increase, then we want to
  1242. 49:49go in the opposite direction. We want to
  1243. 49:50move in the Y decreasing direction. So,
  1244. 49:54if let's say this is Y increasing, we
  1245. 49:55want to move in the Y decreasing, so the
  1246. 49:58loss will also
  1247. 49:59decrease as we move.
  1248. 50:02So, that's in the case if we find that
  1249. 50:05partial derivative with respect to Y
  1250. 50:09is such that when we increase Y, the
  1251. 50:10loss is increasing, okay?
  1252. 50:13And it works the other way around.
  1253. 50:16If we find that if we increase Y, the
  1254. 50:19loss is decreasing, then we just want to
  1255. 50:21keep moving in that Y direction.
  1256. 50:25And so
  1257. 50:26the X direction and the Y direction will
  1258. 50:28give us
  1259. 50:30um well, the gradient at that spot will
  1260. 50:34show us
  1261. 50:36the direction of the steepest ascent,
  1262. 50:38steepest increase. So, we will just go
  1263. 50:40in the direction of the steepest
  1264. 50:41decrease, the opposite direction.
  1265. 50:44So, these are gradients. Uh we're going
  1266. 50:45to see some examples to learn more about
  1267. 50:47this. You can also check some YouTube
  1268. 50:49videos. This may take some time, a few
  1269. 50:52iterations, a few
  1270. 50:53times of new understanding this to
  1271. 50:55finally grasp it.
  1272. 50:57Gradient will will just be an array of
  1273. 51:00numbers. And the first number in this
  1274. 51:02array will be partial derivative of the
  1275. 51:04function with respect to X. So, it's
  1276. 51:07just a derivative. So,
  1277. 51:09if we increase X,
  1278. 51:11what's the change in the function?
  1279. 51:13What's the slope? So, it can be 1 2 3 -1
  1280. 51:16or -1.5
  1281. 51:18or whatever the number is.
  1282. 51:21So,
  1283. 51:23we have derivative of the function with
  1284. 51:25respect to X and derivative of the
  1285. 51:26function with respect to Y. And I should
  1286. 51:29say partial derivative.
  1287. 51:32So, that's the gradient.
  1288. 51:35So, if we have a function X squared plus
  1289. 51:38Y squared,
  1290. 51:40uh here these arrows will show you
  1291. 51:45the direction in which it's increasing
  1292. 51:47most from that particular point where
  1293. 51:50you are standing. So,
  1294. 51:52um X squared plus Y squared do look like
  1295. 51:54this. It will look like a ball, like a
  1296. 51:56ball.
  1297. 51:58And so here at the bottom, there will
  1298. 52:00not be too much of a slope. So, these
  1299. 52:03arrows will be small.
  1300. 52:05The magnitude, the length will be small
  1301. 52:07because there is not too much slope. But
  1302. 52:09as you go to the sides, the slope will
  1303. 52:13be higher, larger.
  1304. 52:15And so the magnitude, so each of these
  1305. 52:18vectors will show you how um steep it
  1306. 52:22is. And it will also show you the
  1307. 52:24direction in which it's steep.
  1308. 52:27The longer the vector, the longer the
  1309. 52:29gradient, the steeper it is. And it
  1310. 52:31shows you the direction in which it's
  1311. 52:33steep.
  1312. 52:34So, if you want to move
  1313. 52:37down, then you would go in the opposite
  1314. 52:39direction of this arrow. If you are
  1315. 52:41standing at this place,
  1316. 52:43you want to go down, you want to go
  1317. 52:44opposite.
  1318. 52:47This is why it's called gradient descent
  1319. 52:49in neural networks. You want to go
  1320. 52:51opposite of the gradient.
  1321. 52:53Because gradient shows steepest ascent,
  1322. 52:54you want to go down the loss function.
  1323. 52:58This is what X squared plus Y squared
  1324. 53:00looks like. It's like a ball.
  1325. 53:02Now, let's see how to calculate gradient
  1326. 53:05of a simple linear function 2X plus 3Y.
  1327. 53:08We have two variables. So, first
  1328. 53:12uh in our gradient, the first
  1329. 53:14uh element will be partial derivative of
  1330. 53:16X
  1331. 53:17with respect to the function. So, we
  1332. 53:20will treat Y as a constant.
  1333. 53:23If you remember in derivatives,
  1334. 53:25uh when we treat Y as a constant, then
  1335. 53:27this will be this whole thing will be a
  1336. 53:29constant, so it it will become zero.
  1337. 53:32It doesn't multiply X because right now
  1338. 53:35partial derivative with respect to X,
  1339. 53:38uh
  1340. 53:39will just treat X as the variable.
  1341. 53:41So, now we will just have 2X as the
  1342. 53:44derivative of this.
  1343. 53:45Oh, sorry, sorry, two as a derivative of
  1344. 53:47this.
  1345. 53:49And then
  1346. 53:50when we do partial derivative of the
  1347. 53:52function with respect to Y, then we just
  1348. 53:54treat Y as the variable and X as a
  1349. 53:56constant. So, we have 2X, that's a
  1350. 53:58constant. That whole thing is a
  1351. 53:59constant, it becomes zero.
  1352. 54:01So, derivative of 3Y will just become
  1353. 54:04three.
  1354. 54:06So, here we have partial derivative
  1355. 54:09of a function with respect to X is going
  1356. 54:12to be two, and partial derivative of the
  1357. 54:13function with respect to Y is going to
  1358. 54:15be three.
  1359. 54:17So, you just gradients
  1360. 54:18and derivatives.
  1361. 54:20In this case, we don't have any
  1362. 54:22variables in the gradient. So, we just
  1363. 54:25have constant two and three, which means
  1364. 54:27for this function, the gradients will
  1365. 54:29always be same.
  1366. 54:31It doesn't depend on X or Y. The
  1367. 54:33gradient is always the same. So, these
  1368. 54:35arrows are always going to be the same.
  1369. 54:36They will always This function will
  1370. 54:38always rise in this way. It's like a
  1371. 54:42plane.
  1372. 54:43It's always the same, always rising in
  1373. 54:45this way for the same amount. It's like
  1374. 54:47a plane.
  1375. 54:49But if we have maybe a curvy surface
  1376. 54:51like this, so this surface is X squared
  1377. 54:54plus Y squared, we will notice that at
  1378. 54:57the bottom,
  1379. 54:58uh it's a bit flat, so there is no so
  1380. 55:00much
  1381. 55:01a
  1382. 55:02rising or ascending or ascent.
  1383. 55:05So, the vectors,
  1384. 55:08gradient vectors will be smaller.
  1385. 55:10But as we
  1386. 55:11go up,
  1387. 55:12these values will grow quadratically.
  1388. 55:15They will grow faster and faster. So,
  1389. 55:17as we increase X and Y values,
  1390. 55:21the slope will be
  1391. 55:23uh bigger and bigger and bigger.
  1392. 55:26So, these gradient arrows will become
  1393. 55:28larger and larger.
  1394. 55:30As I said, the gradient will uh show the
  1395. 55:34direction of steepest ascent. So, if we
  1396. 55:36have this function X squared plus Y
  1397. 55:38squared,
  1398. 55:40if we find
  1399. 55:42uh first of all, the gradient,
  1400. 55:44there is going to be 2X,
  1401. 55:47{comma} 2Y.
  1402. 55:49So, gradient partial derivative with
  1403. 55:51respect to X will be 2X. Partial
  1404. 55:53derivative with respect to Y will be 2Y.
  1405. 55:56So, our gradient
  1406. 55:57for this function will be 2X, {comma}
  1407. 56:002Y.
  1408. 56:01I can show you here. The function is X
  1409. 56:04squared plus Y squared. This is how you
  1410. 56:07uh can write squared in Python.
  1411. 56:10The gradient will be just 2X and 2Y.
  1412. 56:13So,
  1413. 56:15for
  1414. 56:16let's uh
  1415. 56:17see some point like point 2 1. So, X is
  1416. 56:21two, Y is one.
  1417. 56:23We're going to get 2 * X is four. Uh 2 *
  1418. 56:26Y is
  1419. 56:27uh two. 2 * 1 is two, so
  1420. 56:31our gradient at point 2 1 will be 4 2.
  1421. 56:37Uh 4 2 will be our arrow right here, but
  1422. 56:40it's not shown fully.
  1423. 56:43But anyways, that's it points to the
  1424. 56:45steepest ascent, so we want to take the
  1425. 56:47opposite arrow
  1426. 56:48to point it at the descent, where how to
  1427. 56:51go down the function.
  1428. 56:53Now, let's learn about matrices. This is
  1429. 56:55extremely important. This is how neural
  1430. 56:57networks work. This is the
  1431. 57:00main thing about AI neural networks. So,
  1432. 57:02this is a matrix.
  1433. 57:04It's an array of arrays, okay? So, you
  1434. 57:06can look at this first row as one array.
  1435. 57:10So, 1 2. This is the one array.
  1436. 57:12And then second array is 3 5. So, that's
  1437. 57:15one matrix.
  1438. 57:17So, each of these numbers has its index.
  1439. 57:20So,
  1440. 57:22starting from zero. So, this is the
  1441. 57:23zeroth row.
  1442. 57:25So, the first row is actually has has
  1443. 57:27index zero, just like in normal
  1444. 57:28programming.
  1445. 57:29>> [snorts]
  1446. 57:30>> This is a row with index one, okay?
  1447. 57:32Column with index zero, column with
  1448. 57:34index one.
  1449. 57:36And so this uh element has index zero
  1450. 57:39zero.
  1451. 57:42And then
  1452. 57:43uh this has zero one, one zero,
  1453. 57:46because this is
  1454. 57:47a row. So, first is row, row one, and
  1455. 57:49then column zero. So, one zero, and then
  1456. 57:52one one.
  1457. 57:53So, if you want to add two matrices,
  1458. 57:55you just add each element, corresponding
  1459. 57:58elements at the same
  1460. 58:00s- index. So, 1 + 2 is three.
  1461. 58:042 + 5 is seven.
  1462. 58:07Et cetera. This is how addition works.
  1463. 58:10Matrix multiplication is the most
  1464. 58:12important operation in neural networks
  1465. 58:13in uh machine learning in AI.
  1466. 58:16So, you have two matrices. First of all,
  1467. 58:20this number of columns here, three, must
  1468. 58:22be same as the number of rows
  1469. 58:25here, three. So, this three and this
  1470. 58:26three must be same.
  1471. 58:28So, we have one two three columns, one
  1472. 58:30two three rows.
  1473. 58:31If you swap these places,
  1474. 58:34uh you will get a completely different
  1475. 58:36resulting matrix. So, you cannot swap
  1476. 58:39places of matrices and get same result.
  1477. 58:40You will not. Those are That's a
  1478. 58:42different operation.
  1479. 58:44Uh this is how you multiply two
  1480. 58:46matrices. So, you say you do a dot
  1481. 58:49product between this row and this
  1482. 58:51column. And they must be same length.
  1483. 58:54So, 3 * 0
  1484. 58:57plus
  1485. 58:581 * 2. This is the classic dot product.
  1486. 59:00We talked about this earlier in the
  1487. 59:02video.
  1488. 59:03Plus 0 * 0. Okay? That's the formula
  1489. 59:07here, two. So, that's
  1490. 59:09that number two goes at the
  1491. 59:11intersection.
  1492. 59:13So, it's going to be
  1493. 59:15uh first uh row and first column. So,
  1494. 59:18first row, first column here.
  1495. 59:20That's this dot product result.
  1496. 59:23And then you will go for the second one,
  1497. 59:25you will go so go first uh column,
  1498. 59:29sorry, first row and second column here.
  1499. 59:33So, you go 3 * 4 + 1 * 2 + 0 * 1 and
  1500. 59:39result goes here.
  1501. 59:41So, you just put the result at the
  1502. 59:43intersection. So, first row, second
  1503. 59:46column. First row, second column.
  1504. 59:49And at the end, you will get a matrix
  1505. 59:51that's that's
  1506. 59:53size of these outside dimensions. So, 2
  1507. 59:56* 2 because
  1508. 59:58this middle dimension will cancel out.
  1509. 1:00:01And this middle dimension must be same,
  1510. 1:00:03equal. And these outside dimension
  1511. 1:00:06outside doesn't need to doesn't have to
  1512. 1:00:08be same.
  1513. 1:00:11So, if this was 2 * 3
  1514. 1:00:13this matrix and this matrix was
  1515. 1:00:15uh three rows and four columns
  1516. 1:00:18then
  1517. 1:00:20it's possible to multiply because three
  1518. 1:00:22and three inner dimensions are equal and
  1519. 1:00:25the resulting would be 2 * 4 if this was
  1520. 1:00:28four.
  1521. 1:00:30In Python, you would define matrix as
  1522. 1:00:34array of arrays.
  1523. 1:00:35So, this is the first row. This is the
  1524. 1:00:38second row.
  1525. 1:00:41Uh here it's being indexed as 1 1. So,
  1526. 1:00:44here indices are starting from one.
  1527. 1:00:46Maybe to show like example to for you to
  1528. 1:00:49understand it easily, but usually
  1529. 1:00:51indices start from 0 0.
  1530. 1:00:54First row, second row.
  1531. 1:00:57Just like here. So, this is the first
  1532. 1:00:59row.
  1533. 1:01:00This is the second row.
  1534. 1:01:01And this has two columns as well. So,
  1535. 1:01:03two rows, two columns.
  1536. 1:01:05And so, that's how we can define matrix
  1537. 1:01:07in
  1538. 1:01:08Python with for example NumPy. We can
  1539. 1:01:10also do it with PyTorch and stuff.
  1540. 1:01:14And this is example of 3 by 2 matrix.
  1541. 1:01:16So, three rows, 1 2 3 and two columns.
  1542. 1:01:22Now, these rows and columns are going to
  1543. 1:01:24take some time for you to understand
  1544. 1:01:25intuitively quickly
  1545. 1:01:27what's happening. Like 2 by 3 matrix.
  1546. 1:01:30Two rows
  1547. 1:01:32and three columns.
  1548. 1:01:34So, we can print shape of the matrix.
  1549. 1:01:38So, A matrix
  1550. 1:01:40is this one. So, shape here is 2 by 2.
  1551. 1:01:45And then we can print shape of the first
  1552. 1:01:46row and shape of the first
  1553. 1:01:49column.
  1554. 1:01:53So, let's see.
  1555. 1:01:54Uh shape is 2 by 2. Rows two and columns
  1556. 1:01:57two.
  1557. 1:01:58Uh it's maybe easier to understand on
  1558. 1:02:00this example because here rows and
  1559. 1:02:02columns are different uh shapes. So,
  1560. 1:02:05shape is 2 by 3. Rows two, columns
  1561. 1:02:07three.
  1562. 1:02:10So, if you say uh print
  1563. 1:02:14A.shape
  1564. 1:02:160, that's going to be
  1565. 1:02:19the first number in your shape. So, it's
  1566. 1:02:21going to be this number, two.
  1567. 1:02:24And shape one of one with index one will
  1568. 1:02:27be the second number, three.
  1569. 1:02:32Uh maybe you want to play with this a
  1570. 1:02:33little bit to see what happens. So, this
  1571. 1:02:36is going to show the number of columns.
  1572. 1:02:37This is going to show number of rows.
  1573. 1:02:41And so, if you have a matrix 1 2 3 4 5
  1574. 1:02:446, you can access individual elements by
  1575. 1:02:47just calling the index. So, this index
  1576. 1:02:49of this will be 0 0.
  1577. 1:02:52So, if you say A
  1578. 1:02:54of 0 0, that will give you this value.
  1579. 1:02:58Which is in this case, the value is one.
  1580. 1:03:00If I put it like this, it's going to be
  1581. 1:03:0210.
  1582. 1:03:03So, this value
  1583. 1:03:05has index 0 0.
  1584. 1:03:10And that's how you can access
  1585. 1:03:12uh the elements.
  1586. 1:03:15I'm actually going to talk more about
  1587. 1:03:16this indexing and slicing later. It's
  1588. 1:03:19more for maybe PyTorch tutorial.
  1589. 1:03:23So, let's go on to matrix addition. I
  1590. 1:03:25already explained this. So, here you
  1591. 1:03:28would just add each element with on
  1592. 1:03:30corresponding on the same index. So, 1 +
  1593. 1:03:325 is going to be six. The resulting
  1594. 1:03:36will be six.
  1595. 1:03:38And then 2 + 6 result will be eight. So,
  1596. 1:03:41same index.
  1597. 1:03:44Uh scalar multiplication, you just
  1598. 1:03:46multiply.
  1599. 1:03:48So, if you say 2 * matrix A, you will
  1600. 1:03:50just multiply each of these elements
  1601. 1:03:52with two.
  1602. 1:03:54So, this will become 2 4 6 8.
  1603. 1:03:59So, let's see the result here.
  1604. 1:04:012 * A, you will just multiply with two
  1605. 1:04:03or 0.5 * A, you will just multiply each
  1606. 1:04:07element with 0.5.
  1607. 1:04:110.5 1 1.5 2.
  1608. 1:04:14-1 same. Just each element with -1.
  1609. 1:04:17Matrix vector vector multiplication is
  1610. 1:04:20just like matrix multiplication.
  1611. 1:04:23So, you first need to make sure that the
  1612. 1:04:24inner dimension
  1613. 1:04:26is same. So, 2 * 3. So, this three must
  1614. 1:04:29be same as this three, 3 * 1.
  1615. 1:04:32In this case, you can multiply
  1616. 1:04:34A matrix multiplied vector V, but you
  1617. 1:04:38cannot multiply vector V matrix multiply
  1618. 1:04:40vector A because then this inner
  1619. 1:04:42dimension would not be same.
  1620. 1:04:47So, you can first put this first and
  1621. 1:04:49then this second and then that's how you
  1622. 1:04:52can matrix multiply it.
  1623. 1:04:54And then the result will be
  1624. 1:04:562 * 1 because the inner same dimension
  1625. 1:04:58will cancel out.
  1626. 1:05:01And the step-by-step calculation goes
  1627. 1:05:03like this.
  1628. 1:05:041 * 2 + 2 * 3 + 3 * 4.
  1629. 1:05:10And then that's going to be
  1630. 1:05:1220. And then 4 * 2 + 5 * 3 + 6 * 4
  1631. 1:05:18that's going to be 47. So, that's the
  1632. 1:05:19resulting vector.
  1633. 1:05:21So, you multiply this row with this row.
  1634. 1:05:24Now, this could be a bit confusing
  1635. 1:05:26because I was showing you that you
  1636. 1:05:28multiply row and a column.
  1637. 1:05:31Uh but depending on how you do it, how
  1638. 1:05:34you code it, how you imagine it
  1639. 1:05:37this can also be done this way.
  1640. 1:05:39Because this is just a vector.
  1641. 1:05:41So, in matrix multiplication, the way we
  1642. 1:05:43imagine it conceptually is row * column.
  1643. 1:05:46But since this is just a vector
  1644. 1:05:49we can also imagine this as a column
  1645. 1:05:51vector being 2 3 4. And then this this
  1646. 1:05:54row and this column vector.
  1647. 1:05:57So, this is a bit confusing. You're
  1648. 1:05:58going to have to do this by yourself by
  1649. 1:06:00hand. Try to multiply. Try to figure
  1650. 1:06:02out. Try to get intuition.
  1651. 1:06:05Try multiplying this. Try multiplying
  1652. 1:06:07others maybe
  1653. 1:06:08uh get some other examples from AI from
  1654. 1:06:10ChatGPT.
  1655. 1:06:12So, the matrix multiplication example
  1656. 1:06:14here
  1657. 1:06:15I explained it. So, you can use this at
  1658. 1:06:17as
  1659. 1:06:18uh matmul or you can use np.dot which is
  1660. 1:06:22a bit confusing because this implies dot
  1661. 1:06:24product, but this is actually
  1662. 1:06:26this is matmul.
  1663. 1:06:30And so, here you have step-by-step on
  1664. 1:06:31how to multiply two matrices, but this
  1665. 1:06:33is just something you would need to
  1666. 1:06:35understand. And I saw Sam Altman uh
  1667. 1:06:38explaining something about matrices and
  1668. 1:06:40he was showing this with his uh arms or
  1669. 1:06:44hands. So, that's how I know that he
  1670. 1:06:46also knows how to multiply matrices. I
  1671. 1:06:48was wondering if it was in his job
  1672. 1:06:50description as CEO of OpenAI to be able
  1673. 1:06:54to multiply matrices. I was always
  1674. 1:06:56wondering that.
  1675. 1:06:58This is uh transpose matrix. So, we have
  1676. 1:07:011 2 3 4 5 6. We transpose that, it
  1677. 1:07:03becomes So, we swap rows and columns.
  1678. 1:07:06So, it's now this is this column now
  1679. 1:07:09became the row.
  1680. 1:07:11So, 1 4 now became the row. 2 5 now
  1681. 1:07:13became the row.
  1682. 1:07:15And the shape is swapped now.
  1683. 1:07:18Then we have identity matrix
  1684. 1:07:21where it's one on the diagonal
  1685. 1:07:24and others are zero.
  1686. 1:07:26And it's a square matrix. It has equal
  1687. 1:07:29number of rows and columns.
  1688. 1:07:32And when you multiply any matrix with
  1689. 1:07:34identity matrix
  1690. 1:07:35it will remain the same matrix. So, it
  1691. 1:07:39the result will be the same as this
  1692. 1:07:41matrix that you multiplied with the
  1693. 1:07:43identity matrix.
  1694. 1:07:45And in Python, you can create identity
  1695. 1:07:47matrix by saying np.i
  1696. 1:07:50and then how many rows and columns. So,
  1697. 1:07:53two or three.
  1698. 1:07:54Or whatever the number is. So, this is
  1699. 1:07:56the two. This is 3 by 3. It can be any
  1700. 1:07:59number.
  1701. 1:08:01So, identity matrix acts like the number
  1702. 1:08:04one in multiplication. So, it doesn't
  1703. 1:08:05change the thing it's multiplying.
  1704. 1:08:08Here are some properties. You can
  1705. 1:08:11analyze them if you want. I just want
  1706. 1:08:13you to understand that in matrix
  1707. 1:08:15multiplication, you cannot swap the
  1708. 1:08:18order of matrices. This is not a
  1709. 1:08:20generalizable. It's not true in general.
  1710. 1:08:22There might be specific cases when it's
  1711. 1:08:24true, but in general, it's not true.
  1712. 1:08:27That's going to be it on matrices. If
  1713. 1:08:29you understand this, you understand
  1714. 1:08:32um everything you need to know about
  1715. 1:08:33matrices as your fundamentals. So, let's
  1716. 1:08:36uh check out the last lesson as well.
  1717. 1:08:39Lastly, let's learn about probability.
  1718. 1:08:41This is extremely important. It's used
  1719. 1:08:43in large language models, reinforcement
  1720. 1:08:44learning,
  1721. 1:08:45uh computer vision, natural language
  1722. 1:08:47processing, everywhere.
  1723. 1:08:49>> [snorts]
  1724. 1:08:49>> So, you have a coin.
  1725. 1:08:51Let's assume it's a fair coin. You flip
  1726. 1:08:54it, you have 50% chance to get heads,
  1727. 1:08:5650% chance to get tails.
  1728. 1:08:59Or you roll a dice, six sides. So, you
  1729. 1:09:01have 1 over 6
  1730. 1:09:04chance
  1731. 1:09:05for any particular side
  1732. 1:09:07to appear, to be rolled.
  1733. 1:09:10Probability measures how likely an event
  1734. 1:09:12is to occur. Zero means impossible, 0%
  1735. 1:09:16chance probability. One means certain,
  1736. 1:09:19100% chance of probability, always
  1737. 1:09:21happens.
  1738. 1:09:23And 0.5 means 50/50.
  1739. 1:09:26So, let's calculate the probability of
  1740. 1:09:28flipping a coin and getting heads.
  1741. 1:09:31So, we have two possible outcomes, heads
  1742. 1:09:33and tails.
  1743. 1:09:35And but we are calculating probability
  1744. 1:09:38for just one of those outcome, heads.
  1745. 1:09:41So, we say
  1746. 1:09:44uh
  1747. 1:09:44heads outcomes can be just one of those
  1748. 1:09:47two divided by the total outcomes, which
  1749. 1:09:49is two.
  1750. 1:09:51Which is going to give us
  1751. 1:09:52uh 1/2 or 0.5 or 50%.
  1752. 1:09:57So, for rolling a dice, it's same. We
  1753. 1:09:59just have 1/6 now because one side out
  1754. 1:10:03of six possible.
  1755. 1:10:06But in this case, we are assuming that
  1756. 1:10:08each of these six sides has equal
  1757. 1:10:11probability to happen.
  1758. 1:10:13So, then you can divide
  1759. 1:10:16uh 1/6
  1760. 1:10:18to get your probability.
  1761. 1:10:20Same goes for heads. If each of the uh
  1762. 1:10:23heads or tails has equal probability,
  1763. 1:10:26then you can divide.
  1764. 1:10:29Here, if we flip the coin 10 times, we
  1765. 1:10:32get seven times head, uh three times
  1766. 1:10:35tails. So,
  1767. 1:10:37theoretical probability is 0.5, but
  1768. 1:10:39actual probability measured, I should
  1769. 1:10:41say, is 0.7 or 70%. But as we flip more
  1770. 1:10:46coins,
  1771. 1:10:47the measured probability will get closer
  1772. 1:10:49and closer to theoretical. So, for 100
  1773. 1:10:52coins, we get 0.39.
  1774. 1:10:56And for 10,000 coins, we get 0. uh 5024.
  1775. 1:11:01So, this one experimental is very close
  1776. 1:11:04to the theoretical.
  1777. 1:11:07So, the more flips or more samples,
  1778. 1:11:10uh the experiment probability measured
  1779. 1:11:13probability will get closer to the
  1780. 1:11:15theoretical.
  1781. 1:11:16And so, if we try that with dices, we
  1782. 1:11:18will get uh over with 1,000 rolls, we
  1783. 1:11:21will get
  1784. 1:11:22close to the theoretical of 0.1667.
  1785. 1:11:28And here, the orange one shows
  1786. 1:11:30theoretical probability of each uh dice
  1787. 1:11:34face, and it's always the same. But the
  1788. 1:11:37measured one,
  1789. 1:11:38it's going to be a bit different.
  1790. 1:11:42But if we do a lot more measurings, then
  1791. 1:11:45uh this measured one will get closer and
  1792. 1:11:46closer to the theoretical.
  1793. 1:11:49Now, what is the chance that if we roll
  1794. 1:11:51two dices, one dice
  1795. 1:11:54has uh gets give us one, and the second
  1796. 1:11:57dice gives us
  1797. 1:11:59six. So, it doesn't matter which dice
  1798. 1:12:01gives which number. We just need to get
  1799. 1:12:03one and six.
  1800. 1:12:04And the probability there would be by
  1801. 1:12:06multiplying those two probabilities.
  1802. 1:12:08So, it's going to be a lot smaller
  1803. 1:12:10probability to get both of them.
  1804. 1:12:14Now, what is the chance that in those
  1805. 1:12:16two dices, we get one or six, but not
  1806. 1:12:21both of them, just either one or six.
  1807. 1:12:24So, it can be one on the first dice or
  1808. 1:12:27one on the second dice or six on the
  1809. 1:12:30first dice or six on the second dice.
  1810. 1:12:32So, there are four possible ways to
  1811. 1:12:34satisfy this, but if we get one and six,
  1812. 1:12:37that's not what we want. So,
  1813. 1:12:39So,
  1814. 1:12:41we will subtract the probability of
  1815. 1:12:44getting one and six because that's not
  1816. 1:12:47what we're looking for.
  1817. 1:12:49And so, the probability of getting one
  1818. 1:12:51is this, and probability of getting six
  1819. 1:12:53is the same. And then subtract if we get
  1820. 1:12:56both one and six. And this is the
  1821. 1:12:58probability that we get one or six if we
  1822. 1:13:01roll two dices.
  1823. 1:13:04Here, we have probability distribution.
  1824. 1:13:06So,
  1825. 1:13:07this is the task. If we roll two dices,
  1826. 1:13:11uh what is the probability that the sum
  1827. 1:13:14of the numbers of each dices, like we
  1828. 1:13:17get one and six,
  1829. 1:13:19so the sum is seven.
  1830. 1:13:21So, what is the probability of each of
  1831. 1:13:23these sums?
  1832. 1:13:25For example,
  1833. 1:13:27if the sum is two, it means we got one
  1834. 1:13:29and one.
  1835. 1:13:31If the sum is seven, it means we got
  1836. 1:13:34three and four and four and three and
  1837. 1:13:36two five, five two, one six, six one.
  1838. 1:13:39So, you can see that
  1839. 1:13:41we can get seven in a lot more different
  1840. 1:13:43ways
  1841. 1:13:45than for example two because we can get
  1842. 1:13:46two as a sum if only if
  1843. 1:13:49both of the
  1844. 1:13:50uh both of the dices are one.
  1845. 1:13:53But seven, we can get
  1846. 1:13:55in a lot more different ways. So,
  1847. 1:13:57probability of the sum being seven is a
  1848. 1:13:59lot higher.
  1849. 1:14:01And that's the probability distribution
  1850. 1:14:03over all of these uh sums.
  1851. 1:14:07So, what is the expected value of a dice
  1852. 1:14:11roll?
  1853. 1:14:12Uh we know that dice can show us numbers
  1854. 1:14:161 2 3 4 5 6.
  1855. 1:14:19And each of these numbers has 1/6 chance
  1856. 1:14:22to happen.
  1857. 1:14:23So, what is the average or expected
  1858. 1:14:26number we will get by just rolling a
  1859. 1:14:28bunch of times?
  1860. 1:14:31You can maybe intuitively understand
  1861. 1:14:33that the average number will be 3.5
  1862. 1:14:36because this is exactly in the middle of
  1863. 1:14:38one and six.
  1864. 1:14:41Uh but we can calculate expected value
  1865. 1:14:46uh by multiplying the value we will get,
  1866. 1:14:49so one two three four, times its
  1867. 1:14:52probability.
  1868. 1:14:541/6 is the probability for each of these
  1869. 1:14:56values.
  1870. 1:14:58And then sum up.
  1871. 1:15:00So, sum up and divide by the number
  1872. 1:15:05to make to do the to find the average or
  1873. 1:15:08mean.
  1874. 1:15:09But this is very important. This summing
  1875. 1:15:12up of all of the values and dividing by
  1876. 1:15:14the number
  1877. 1:15:16is just because every value has the
  1878. 1:15:18equal probability to happen.
  1879. 1:15:21That's why we can pull them into a
  1880. 1:15:23bracket and then divide by this common
  1881. 1:15:25denominator is the same for every one.
  1882. 1:15:28But [snorts] if these probabilities were
  1883. 1:15:30different,
  1884. 1:15:32uh not all are like same 1/6, then we
  1885. 1:15:34would not be able to like sum up the
  1886. 1:15:36values and divide by six.
  1887. 1:15:38So, summing up and dividing only works
  1888. 1:15:40if the probabilities are same.
  1889. 1:15:43So, that's like a shortcut you can do.
  1890. 1:15:47But this is more general formula.
  1891. 1:15:49You just do each value times its
  1892. 1:15:51probability.
  1893. 1:15:52And that's
  1894. 1:15:54but this is uh more specific. This works
  1895. 1:15:57if probabilities are same.
  1896. 1:15:59Then we have conditional probability.
  1897. 1:16:01So,
  1898. 1:16:02probability of A happening given that B
  1899. 1:16:06happened.
  1900. 1:16:08Uh you can calculate this by this
  1901. 1:16:11formula. So, probability of A and B
  1902. 1:16:14divided by probability of B.
  1903. 1:16:16It's easier to understand this in an
  1904. 1:16:18example.
  1905. 1:16:20So, given that we roll an even number,
  1906. 1:16:24what's the probability that that number
  1907. 1:16:26is six?
  1908. 1:16:29So, first of all, probability to roll an
  1909. 1:16:31even number is 0.5 because we have three
  1910. 1:16:34odd, three even numbers on a dice.
  1911. 1:16:38And probability
  1912. 1:16:40of rolling six and even. So, you need to
  1913. 1:16:43think a little bit. So, if you roll six,
  1914. 1:16:45it will also be even.
  1915. 1:16:48So, here we're just going to use the
  1916. 1:16:49probability of rolling six because if we
  1917. 1:16:52roll six, it will also be even. So,
  1918. 1:16:55that's the probability.
  1919. 1:16:57And so, the formula,
  1920. 1:17:00uh we intuitively know that
  1921. 1:17:05let's not let's first look at intuition.
  1922. 1:17:07So, we have three of these. So, if we
  1923. 1:17:10roll a even number,
  1924. 1:17:12then because each of these even numbers
  1925. 1:17:14has same probability,
  1926. 1:17:17then
  1927. 1:17:18it's 1/3, 1/3, and 1/3 to be each of
  1928. 1:17:21these numbers.
  1929. 1:17:23Because they have same probability.
  1930. 1:17:25So, that's what we will get in the
  1931. 1:17:27result, 1/3 or 0.03 0.3333.
  1932. 1:17:32So, and if we check the formula, it will
  1933. 1:17:35actually
  1934. 1:17:36come out as 1/3.
  1935. 1:17:39So, the prob so,
  1936. 1:17:41the probability of getting six given
  1937. 1:17:44that we got even number is 1/3.
  1938. 1:17:49And this is the law of large numbers.
  1939. 1:17:52So, uh if you have probability
  1940. 1:17:54theoretical probability is 0.5, you're
  1941. 1:17:57flipping the coin.
  1942. 1:17:58In the beginning where you just flip
  1943. 1:18:00maybe 10 times, maybe you will actually
  1944. 1:18:03get seven heads and three tails.
  1945. 1:18:05But as you flip more more more more more
  1946. 1:18:07more,
  1947. 1:18:08the measured probability will converge
  1948. 1:18:11towards the theoretical probability. So,
  1949. 1:18:14that's the law of large numbers. If you
  1950. 1:18:16have large numbers, they will converge
  1951. 1:18:17towards theoretical probability.
  1952. 1:18:20Now, this is also interesting. Let's say
  1953. 1:18:22you are tossing a coin 10 times.
  1954. 1:18:26What's the probability that out of those
  1955. 1:18:2910 times,
  1956. 1:18:32you get five heads?
  1957. 1:18:35I actually recommend you go to my
  1958. 1:18:37channel, and here in playlists, you
  1959. 1:18:40check out this uh become AI researcher
  1960. 1:18:42probability because this is a playlist
  1961. 1:18:44of going into probability a lot more in
  1962. 1:18:47a lot more details. And you can check
  1963. 1:18:49just first video or the first and sec
  1964. 1:18:51and second. Those contain most important
  1965. 1:18:53things.
  1966. 1:18:55So, random variable, standard deviation,
  1967. 1:18:57variance.
  1968. 1:18:59And then continuous random variable
  1969. 1:19:00probability density function. So, maybe
  1970. 1:19:03uh these two first videos you can check.
  1971. 1:19:06That's going to be it on this part on
  1972. 1:19:08maths. You can join my school to become
  1973. 1:19:11AI researcher uh link below the video or
  1974. 1:19:14you can find it on my channel.
  1975. 1:19:17Here, on my channel.
  1976. 1:19:19Get a 7-day a trial if you join right
  1977. 1:19:21now.
  1978. 1:19:22In this lesson, we will learn about
  1979. 1:19:24PyTorch necessary for AI research. After
  1980. 1:19:28this short course, you will be able to
  1981. 1:19:30understand and code any neural network,
  1982. 1:19:32any AI research in PyTorch.
  1983. 1:19:35So, I presume basic understanding of
  1984. 1:19:37Python. You can learn that on YouTube if
  1985. 1:19:39you never use Python.
  1986. 1:19:42So, here I'm going to go into
  1987. 1:19:44extensions. I am on VS Code or Cursor,
  1988. 1:19:47and I'm just going to say Colab, and I'm
  1989. 1:19:49going to install this Google Colab. If I
  1990. 1:19:52want to run my code on Google Colab from
  1991. 1:19:56this local environment.
  1992. 1:19:57You can also open these notebooks on
  1993. 1:20:00Google Colab or
  1994. 1:20:02you can just
  1995. 1:20:03run them locally, but here I'm going to
  1996. 1:20:06run on Google Colab from my local
  1997. 1:20:08environment, but you can have whatever
  1998. 1:20:09setup you want.
  1999. 1:20:11So, I will choose select another kernel
  2000. 1:20:14here, so I can click here Colab or
  2001. 1:20:17select another kernel and Colab.
  2002. 1:20:20And then I can start a new Colab server.
  2003. 1:20:23I'm going to choose
  2004. 1:20:25CPU and just press enter for the So,
  2005. 1:20:27here I'm just going to use Google's CPU.
  2006. 1:20:32And then I'm going to choose this Python
  2007. 1:20:34kernel.
  2008. 1:20:36And let's see, it should work.
  2009. 1:20:40And it works. I chose CPU because I can
  2010. 1:20:43use it for longer than GPU.
  2011. 1:20:45And if you don't have PyTorch installed,
  2012. 1:20:48like you run this and says no module
  2013. 1:20:50named torch, then you would just install
  2014. 1:20:54So, you would pip install torch in your
  2015. 1:20:57console, in your CMD, right here.
  2016. 1:21:03Pip install torch or in the Google Colab
  2017. 1:21:06or wherever.
  2018. 1:21:07So, let's learn about tensors.
  2019. 1:21:10So, you can create a tensor.
  2020. 1:21:13Tensor is like an array. It can also be
  2021. 1:21:17array of arrays, or it can be array of
  2022. 1:21:19arrays of arrays. So, any number of
  2023. 1:21:22nested arrays within arrays.
  2024. 1:21:25So, here we have a 2D tensor.
  2025. 1:21:30I'm going to zoom in. So,
  2026. 1:21:32it's like a matrix. So, this is the
  2027. 1:21:34first row. This is the second row. And
  2028. 1:21:37the data type is float 32 here.
  2029. 1:21:41So, this is from Python.
  2030. 1:21:43You can self-study what this is. This is
  2031. 1:21:45from Python. So, this is kind of
  2032. 1:21:48prerequisite, but if you don't know, you
  2033. 1:21:49can ask ChatGPT all of these things that
  2034. 1:21:52I skip.
  2035. 1:21:53So,
  2036. 1:21:55if I go ahead and print all of this,
  2037. 1:21:58this is the tensor that we have. This is
  2038. 1:21:59its shape. So, we have two rows,
  2039. 1:22:03and
  2040. 1:22:04each row has
  2041. 1:22:05three columns or So, 1 2 3. 1 2 3. So,
  2042. 1:22:09there is three columns in total.
  2043. 1:22:12So, two rows, three columns.
  2044. 1:22:15We can flatten this tensor, which means
  2045. 1:22:17we will just convert it into a single
  2046. 1:22:22And so, it's going to It's going to go
  2047. 1:22:23like this. 1 2 3 4 5 6. So, it will just
  2048. 1:22:25put rows next to each other, and it's
  2049. 1:22:27going to be a single array. It will not
  2050. 1:22:29be two arrays like here.
  2051. 1:22:32So, the shape is now just six because
  2052. 1:22:34there is six numbers in this one array.
  2053. 1:22:39And this flattening is done with
  2054. 1:22:41tensor.flatten, and T
  2055. 1:22:43is our tensor, remember. So, T is our
  2056. 1:22:45tensor.
  2057. 1:22:48And then we can also specify this
  2058. 1:22:52start dimension to be zero.
  2059. 1:22:55In this case, this will look the same as
  2060. 1:22:59without specifying this start dimension
  2061. 1:23:01zero.
  2062. 1:23:03For me to explain what this start
  2063. 1:23:05dimension does, let's look at a
  2064. 1:23:08different tensor. It's a 3D tensor, so
  2065. 1:23:10there are It's 2 2 2. So,
  2066. 1:23:13inner arrays have two elements. There is
  2067. 1:23:15two of these.
  2068. 1:23:17And then there is two of these outside.
  2069. 1:23:19So, it's 2 2 2.
  2070. 1:23:21So, if I say flatten, but
  2071. 1:23:25start dimension is equal to one.
  2072. 1:23:28So, we will actually preserve the
  2073. 1:23:30dimension zero. We will preserve
  2074. 1:23:31dimension zero, and we will start
  2075. 1:23:33flattening from dimension one. So,
  2076. 1:23:35So, what that means is
  2077. 1:23:38we will not have one big single array.
  2078. 1:23:42Instead, we will preserve these outside
  2079. 1:23:45these most like outer
  2080. 1:23:48two elements.
  2081. 1:23:50And then we will flatten within them.
  2082. 1:23:52So, we will flatten within these outer
  2083. 1:23:54elements. So, it will be 1 2 3 4.
  2084. 1:23:58Because we are preserving dimension
  2085. 1:23:59zero, and we are starting from dimension
  2086. 1:24:01one.
  2087. 1:24:02So, you can play around with this and
  2088. 1:24:04try to
  2089. 1:24:06change it and understand what's
  2090. 1:24:07happening here.
  2091. 1:24:09Try to make bigger tensors. Try to
  2092. 1:24:11put some other dimensions.
  2093. 1:24:13Now, let's see reshaping. So, we have a
  2094. 1:24:15tensor 1 2 3 4 5 6 7 8.
  2095. 1:24:19And the shape here is going to be two
  2096. 1:24:21rows
  2097. 1:24:23and four
  2098. 1:24:24columns. So, 2 4.
  2099. 1:24:27And so, we want to reshape for it to be
  2100. 1:24:304 2 instead.
  2101. 1:24:33So, let's see
  2102. 1:24:34what it's going to do. It's going to
  2103. 1:24:35group like this. 1 2
  2104. 1:24:373 4 5 6. So, now we're going to have
  2105. 1:24:40four rows
  2106. 1:24:42and two columns.
  2107. 1:24:44And it's going to just start from the
  2108. 1:24:45beginning.
  2109. 1:24:49So, it's going to group these two, these
  2110. 1:24:50two,
  2111. 1:24:51these two, these two.
  2112. 1:24:55Also, if we have a tensor 2 by 3 by 4.
  2113. 1:24:58Now, I want you to understand what here
  2114. 1:25:01is two, what here is three with what
  2115. 1:25:03here is four.
  2116. 1:25:04So, you need to like think about it.
  2117. 1:25:07So, this is four, and then there is
  2118. 1:25:09three of them,
  2119. 1:25:10and then there is one two of these big
  2120. 1:25:13ones. So, you need to understand this.
  2121. 1:25:16And we want to reshape this tensor to 6
  2122. 1:25:184.
  2123. 1:25:19What is that going to do? It's going to
  2124. 1:25:21create six
  2125. 1:25:23rows,
  2126. 1:25:25each row having four columns or four
  2127. 1:25:27elements. So, four columns.
  2128. 1:25:31So, in that case,
  2129. 1:25:33this four will be preserved,
  2130. 1:25:36and these two and three will be merged
  2131. 1:25:39into six. And you see that
  2132. 1:25:42you have to have equal number of
  2133. 1:25:44elements at the end. So, 2 * 3 * 4 must
  2134. 1:25:47be equal to
  2135. 1:25:486 * 4.
  2136. 1:25:51So, you can play with this and try to
  2137. 1:25:54understand what's happening, and how it
  2138. 1:25:55works if you change it a little bit.
  2139. 1:25:58Now, there is also view, which is
  2140. 1:26:00similar to reshape,
  2141. 1:26:02but I'll show the difference. So, if we
  2142. 1:26:05have a tensor like this,
  2143. 1:26:07we say we want to reshape that to have
  2144. 1:26:09four rows and two columns instead.
  2145. 1:26:12So, this will group 1 2, and then 3 4,
  2146. 1:26:16and then 5 6, and 7 8.
  2147. 1:26:20Like this.
  2148. 1:26:22So, this is interesting. So, we created
  2149. 1:26:24a new tensor that's going to be T view.
  2150. 1:26:28So, we have original tensor T, and T
  2151. 1:26:30view is the new tensor.
  2152. 1:26:32But if we change T view, it will also
  2153. 1:26:34change original tensor because
  2154. 1:26:37T view is actually using the same
  2155. 1:26:38elements, the same memory slots in
  2156. 1:26:40memory
  2157. 1:26:41as this original tensor. So, when we
  2158. 1:26:44change T view, we will also change this
  2159. 1:26:46original one. So, keep that in mind.
  2160. 1:26:48That can be useful for some situations.
  2161. 1:26:50So,
  2162. 1:26:53we see that the first element is one of
  2163. 1:26:55both of them.
  2164. 1:26:58And then we're going to just modify T
  2165. 1:27:00view at 99.
  2166. 1:27:02And then the original first element now
  2167. 1:27:05also becomes 99. So, now all of them are
  2168. 1:27:0899.
  2169. 1:27:12So, view requires tensor to be
  2170. 1:27:15contiguous in memory means all of the
  2171. 1:27:18elements need to be next to each other
  2172. 1:27:20in memory.
  2173. 1:27:21Reshape will work even if the tensor is
  2174. 1:27:24not contiguous, whether or not it's
  2175. 1:27:26contiguous. So, whether or not
  2176. 1:27:28it's all of the elements are next to
  2177. 1:27:29each other in memory or not.
  2178. 1:27:32And it will create a copy of the tensor
  2179. 1:27:34if necessary
  2180. 1:27:36if it's not next to each other.
  2181. 1:27:39Join my school to become AI researcher.
  2182. 1:27:42We have all of these courses that will
  2183. 1:27:43lead you step by step. Get 7-day free
  2184. 1:27:45trial if you join right now. Let's check
  2185. 1:27:48squeezing.
  2186. 1:27:50So, we have a tensor
  2187. 1:27:52that has elements 1 2 3, but there is
  2188. 1:27:54another dimension around it, and another
  2189. 1:27:56dimension around it.
  2190. 1:27:58So, the shape of this tensor it's going
  2191. 1:28:01to be
  2192. 1:28:021 1 3 because the inner array has three
  2193. 1:28:06elements, and then there is and then
  2194. 1:28:07that array is an element of the outer
  2195. 1:28:10array, and then that array is an element
  2196. 1:28:12of the outer outer array.
  2197. 1:28:15You see here.
  2198. 1:28:17And so, we can use this command
  2199. 1:28:20T.squeeze.
  2200. 1:28:23So, now we're going to create a new
  2201. 1:28:25tensor T squeeze is equal to T.squeeze.
  2202. 1:28:31And this will remove all of these outer
  2203. 1:28:34dimensions that have just dimension one.
  2204. 1:28:39So, now we will just be left with this
  2205. 1:28:41tensor, or this array I should say,
  2206. 1:28:45or whatever. It can be also tensor. But
  2207. 1:28:47the outer dimensions that
  2208. 1:28:50have one element will be removed.
  2209. 1:28:54That's what squeezing does.
  2210. 1:28:56We can also specify at which dimension
  2211. 1:28:58we want to squeeze. So,
  2212. 1:29:01if we look at this tensor, and can you
  2213. 1:29:03tell me what the shape of this tensor
  2214. 1:29:05is? Because
  2215. 1:29:07there is three, and then but three is an
  2216. 1:29:09element within an array.
  2217. 1:29:11And then there is three of those
  2218. 1:29:13elements, or I should say three of those
  2219. 1:29:15arrays.
  2220. 1:29:18And then there is this outside.
  2221. 1:29:23So, the shape of this is 1 3 1.
  2222. 1:29:26Because uh
  2223. 1:29:28there is three of these and each of them
  2224. 1:29:30has one element inside and there is like
  2225. 1:29:32one outside dimension.
  2226. 1:29:35So, if I [snorts] say I want to squeeze
  2227. 1:29:37a dimension zero, this will remove this
  2228. 1:29:39outermost dimension.
  2229. 1:29:41So, let's see what happens.
  2230. 1:29:46Now,
  2231. 1:29:47this has removed this outermost
  2232. 1:29:49dimension
  2233. 1:29:51and so we just have one array
  2234. 1:29:53of three of these arrays, small arrays,
  2235. 1:29:56one element arrays.
  2236. 1:29:59So, you can play with this and
  2237. 1:30:00understand how this works.
  2238. 1:30:02I can also squeeze a dimension two. So,
  2239. 1:30:04t2.squeeze and remember t2 is our
  2240. 1:30:07original array. So, this is not our
  2241. 1:30:10squeeze. I'm not squeezing already
  2242. 1:30:12squeezed array. I'm squeezing now the
  2243. 1:30:14original array again or I should say
  2244. 1:30:16tensor.
  2245. 1:30:19So, squeeze the dimension two here.
  2246. 1:30:22So, this will remove
  2247. 1:30:24you see here.
  2248. 1:30:26So, every element was originally in its
  2249. 1:30:28in its own array.
  2250. 1:30:30But,
  2251. 1:30:31I'm going to remove that dimension two
  2252. 1:30:34here. So, now these elements will no
  2253. 1:30:35longer be in its own array.
  2254. 1:30:38And so,
  2255. 1:30:40we will just have these elements
  2256. 1:30:43in this So, now it's 1 3.
  2257. 1:30:46So, because we removed this
  2258. 1:30:48and now we just 1 3.
  2259. 1:30:51And unsqueeze does the opposite. It will
  2260. 1:30:53add the dimension of size one.
  2261. 1:30:57So, if we have some tensor 1 2 3 4,
  2262. 1:31:01we say unsqueeze a dimension zero.
  2263. 1:31:05So,
  2264. 1:31:07the previous shape was four and then we
  2265. 1:31:09unsqueeze a dimension zero. So, it will
  2266. 1:31:11add
  2267. 1:31:12uh
  2268. 1:31:13one dimension at position zero and it
  2269. 1:31:17will have size one.
  2270. 1:31:19So, now that means we have another array
  2271. 1:31:23outside of this original
  2272. 1:31:25tensor.
  2273. 1:31:29And we can also unsqueeze a dimension
  2274. 1:31:31one. So, if we take this unsqueeze a
  2275. 1:31:33dimension one,
  2276. 1:31:35it will be 4 1 now.
  2277. 1:31:37Because it will add
  2278. 1:31:39dimension uh
  2279. 1:31:41after this.
  2280. 1:31:43And that means that each of these will
  2281. 1:31:44be in its own brackets.
  2282. 1:31:47Or I should say in its own array of size
  2283. 1:31:49one.
  2284. 1:31:52So, this is the difference between
  2285. 1:31:53unsqueeze at zero, unsqueeze at uh one.
  2286. 1:31:56Uh you can play with this and understand
  2287. 1:31:58like how it works and why it works and
  2288. 1:31:59what it does.
  2289. 1:32:01You can also unsqueeze at minus one,
  2290. 1:32:03which will add
  2291. 1:32:05uh dimension at the end.
  2292. 1:32:08So, whatever your So, if you don't know
  2293. 1:32:12the actual shape of your
  2294. 1:32:14tensor, but you just want to put every
  2295. 1:32:16element in its own array,
  2296. 1:32:20then you can say unsqueeze a dimension
  2297. 1:32:23minus one and this will put every
  2298. 1:32:24element in its own array. So, it will
  2299. 1:32:26add another. So, even if every element
  2300. 1:32:29was already in its own array, it will
  2301. 1:32:31add one more
  2302. 1:32:33array within that array. So, every
  2303. 1:32:35element will just get brackets around it
  2304. 1:32:38if you unsqueeze a dimension minus one.
  2305. 1:32:41This is good if you want to separate if
  2306. 1:32:43you have some array and you need to
  2307. 1:32:44separate it for something, do some
  2308. 1:32:46operations on every single element.
  2309. 1:32:49So, that's how it works.
  2310. 1:32:51So, we are now at the first lesson here,
  2311. 1:32:54first notebook.
  2312. 1:32:56Uh I'm going to show you how to create
  2313. 1:32:58different types of tensors. So, we can
  2314. 1:33:00do torch.zeros five.
  2315. 1:33:04That's going to just create a
  2316. 1:33:06tensor of five zeros, but you can
  2317. 1:33:09But, whatever shape you put here like 3
  2318. 1:33:12{comma} 4,
  2319. 1:33:13it will create a tensor with this shape
  2320. 1:33:16with all zeros.
  2321. 1:33:18So, three rows, each row has four
  2322. 1:33:20elements or four columns. So, three
  2323. 1:33:24rows, each row has four elements.
  2324. 1:33:28Uh we have ones
  2325. 1:33:32and this full,
  2326. 1:33:35which we can specify the value. So,
  2327. 1:33:39uh ones five, this will create an array
  2328. 1:33:41of five ones or this matrix of ones.
  2329. 1:33:45Let's see what it what it looks like.
  2330. 1:33:46So, array of five ones or matrix with
  2331. 1:33:49this shape or
  2332. 1:33:51uh matrix where each value is five.
  2333. 1:33:54You can also convert NumPy array to a
  2334. 1:33:57tensor. So, if you have NumPy array, you
  2335. 1:33:59just say torch.from_numpy
  2336. 1:34:02to convert it into a tensor.
  2337. 1:34:05And you can take that tensor and say dot
  2338. 1:34:08numpy to convert it back to NumPy array.
  2339. 1:34:11So, if you check the classes, it's
  2340. 1:34:13either going to be torch tensor if it's
  2341. 1:34:15torch tensor or numpy.ndarray.
  2342. 1:34:19And when you convert them, so when you
  2343. 1:34:21use torch.from_numpy, it shares the
  2344. 1:34:23memory with the NumPy array. So,
  2345. 1:34:26when you change this new uh
  2346. 1:34:29torch tensor, it will also change the
  2347. 1:34:31original NumPy array variable.
  2348. 1:34:34Uh you can also if you clone, so that
  2349. 1:34:37will create a separate
  2350. 1:34:38uh copy. So, then it will be separated,
  2351. 1:34:42the NumPy array and
  2352. 1:34:44the tensor.
  2353. 1:34:46You can also create tensors with random
  2354. 1:34:48values. This is used in neural networks
  2355. 1:34:50when initializing weights.
  2356. 1:34:52So, you just say torch.rand and then the
  2357. 1:34:54shape.
  2358. 1:34:57And you can also specify you can also
  2359. 1:34:59use randint to make it integer between
  2360. 1:35:02zero and 10, but
  2361. 1:35:05zero is included, 10 is not included.
  2362. 1:35:07So, zero is inclusive, 10 is not
  2363. 1:35:09inclusive.
  2364. 1:35:11Or maybe exclusive.
  2365. 1:35:14Uh this is our first So, this is example
  2366. 1:35:17of the first thing we did here is
  2367. 1:35:19torch.rand 3 4.
  2368. 1:35:22So, that's going to give us this these
  2369. 1:35:23numbers between zero and one.
  2370. 1:35:27And so, three arrays or three rows and
  2371. 1:35:30each row has four columns.
  2372. 1:35:33And then the second randint will give us
  2373. 1:35:36integers like this between zero and
  2374. 1:35:38nine.
  2375. 1:35:40We can also uh create random values from
  2376. 1:35:43normal distribution where mean is zero
  2377. 1:35:45and standard deviation is one.
  2378. 1:35:48So, if we use torch.randn,
  2379. 1:35:52so uh this should be
  2380. 1:35:56uh bunched up around zero. These numbers
  2381. 1:35:58should be uh very close to zero and then
  2382. 1:36:01the further away you go from zero in
  2383. 1:36:03positive or negative,
  2384. 1:36:05uh there is less probability, although
  2385. 1:36:07you can get any number here.
  2386. 1:36:09But, the further away from zero you go,
  2387. 1:36:12uh the probability decays exponentially.
  2388. 1:36:15So, you're usually not going to get
  2389. 1:36:17a number that are too far away. But, you
  2390. 1:36:19see we get even numbers larger than one
  2391. 1:36:21or
  2392. 1:36:23So, we also have um
  2393. 1:36:27You can also use this trick because
  2394. 1:36:30torch.rand will give you values between
  2395. 1:36:32zero and one,
  2396. 1:36:33but you can multiply all of those values
  2397. 1:36:35with 10. So, then they will be between
  2398. 1:36:37zero and 10. Or yeah, zero and 10.
  2399. 1:36:41So, here we can see that values are
  2400. 1:36:43between zero and 10.
  2401. 1:36:46You can also print data type of the
  2402. 1:36:49tensor. So, it's going to be float
  2403. 1:36:52if you initialize it like this, but you
  2404. 1:36:54can also specify data type to be int64
  2405. 1:36:57or float32.
  2406. 1:37:00Because originally it's float32.
  2407. 1:37:05You can also convert. So,
  2408. 1:37:07if you have uh int64, you say
  2409. 1:37:11dot So, that tensor.float,
  2410. 1:37:13it will convert to float uh 32.
  2411. 1:37:16For the next one, you would need to know
  2412. 1:37:18how matrix multiplication works in
  2413. 1:37:21mathematics. So, you can just watch my
  2414. 1:37:23video here, math for AI research, or you
  2415. 1:37:26can just uh type here like how to
  2416. 1:37:29multiply matrices, matrix multiplication
  2417. 1:37:32on YouTube.
  2418. 1:37:33So, if you know how matrices are
  2419. 1:37:35multiplied, dot product between this row
  2420. 1:37:37and this column. So, you need to
  2421. 1:37:38understand that.
  2422. 1:37:40Uh then we can go further, go on and
  2423. 1:37:44uh this is the operators to multiply
  2424. 1:37:46matrices.
  2425. 1:37:47So,
  2426. 1:37:49uh you can also use torch.matmul by the
  2427. 1:37:51way.
  2428. 1:37:54So, if you have this matrix and this
  2429. 1:37:55matrix, the result will be this matrix.
  2430. 1:37:58Of course, the same result
  2431. 1:38:01depending on which you use.
  2432. 1:38:03At sign or torch.matmul, the result will
  2433. 1:38:05be the same.
  2434. 1:38:07One thing you need to know. So, when you
  2435. 1:38:10have matrix and vector, you multiply
  2436. 1:38:12them
  2437. 1:38:13the same way.
  2438. 1:38:16Uh you're going to get some other
  2439. 1:38:17vector. Now,
  2440. 1:38:19you can imagine this vector as being
  2441. 1:38:21some vector and this resulting vector as
  2442. 1:38:24being some other vector.
  2443. 1:38:26So, the rule in maths is when you
  2444. 1:38:28multiply vector with a matrix, that
  2445. 1:38:31matrix will rotate
  2446. 1:38:34and squish or stretch the vector.
  2447. 1:38:36So, that's what matrices does and this
  2448. 1:38:38is important in neural networks. Uh if
  2449. 1:38:40you want to do this theoretical or
  2450. 1:38:42research and stuff. So, just know that
  2451. 1:38:44when you have a vector you multiply it
  2452. 1:38:45with a matrix, it's going to stretch or
  2453. 1:38:47squish or and rotate the vector.
  2454. 1:38:51Possibly rotate. It may not rotate by
  2455. 1:38:52the way.
  2456. 1:38:54I will go to third lesson, third
  2457. 1:38:55notebook here in PyTorch. So, I'm going
  2458. 1:38:59to skip some of the matrix stuff. That's
  2459. 1:39:01not so important.
  2460. 1:39:04If we have a tensor,
  2461. 1:39:06in this case it's a matrix, we can
  2462. 1:39:08transpose it by
  2463. 1:39:09uh using a.t and I'll show you what
  2464. 1:39:12transposing means.
  2465. 1:39:14And
  2466. 1:39:16uh we can use a.t with these brackets,
  2467. 1:39:19but only for 2D tensors.
  2468. 1:39:22So, this is what's going to do. It's
  2469. 1:39:23going to swap rows and columns.
  2470. 1:39:25So, this was a column.
  2471. 1:39:27Now, it's going to become row. 1 4 was a
  2472. 1:39:30column. It's going to become a row now.
  2473. 1:39:322 5 was a column. It's going to become
  2474. 1:39:34row. And you see that
  2475. 1:39:361 2 3 was a row. Now, 1 2 3 is a column.
  2476. 1:39:39So, it's swapping rows and columns.
  2477. 1:39:41Transposing.
  2478. 1:39:43Let's see if we have a 3D matrix.
  2479. 1:39:461 2 3 4 and then that's one matrix. And
  2480. 1:39:49then there are two matrices.
  2481. 1:39:52And we want to transpose
  2482. 1:39:54uh
  2483. 1:39:55the
  2484. 1:39:56zeroth. So,
  2485. 1:39:58so that's going to be this outer
  2486. 1:40:00dimension and the last dimension,
  2487. 1:40:03which has index two.
  2488. 1:40:05So, that's going to be a little bit
  2489. 1:40:06weird. So, now
  2490. 1:40:09we do this. We say 1 and 5 now becomes
  2491. 1:40:15uh this row. So, now
  2492. 1:40:18the first element of the
  2493. 1:40:20the first elements of the outer most
  2494. 1:40:23dimensions, which are these two,
  2495. 1:40:25now become elements
  2496. 1:40:27of the inner most dimension because we
  2497. 1:40:29transposed them. We swapped them.
  2498. 1:40:32And then the inner most dimension
  2499. 1:40:33elements in the previous become outer
  2500. 1:40:37most dimension first elements. So, 1
  2501. 1:40:40and 2. So, this only This is going to
  2502. 1:40:42require some intuition, some playing
  2503. 1:40:43around, some understanding like how this
  2504. 1:40:45transposing works.
  2505. 1:40:47We can also rearrange dimensions.
  2506. 1:40:50So, now we are In this case, we are
  2507. 1:40:54reversing the order, but you can put any
  2508. 1:40:56order you want.
  2509. 1:40:58So, let's see what happens when you
  2510. 1:40:59reverse the order of dimensions.
  2511. 1:41:02So, the original one is 1 2 3 4 5 6 7 8.
  2512. 1:41:06But then
  2513. 1:41:081 5 3 7. Let's see.
  2514. 1:41:111 5 3 7.
  2515. 1:41:18It's a little weird. 2 6 4 8.
  2516. 1:41:21So, this is something
  2517. 1:41:23that you just need to kind of look at
  2518. 1:41:25this and get the intuition why these
  2519. 1:41:27numbers, how what's swapped,
  2520. 1:41:31etc.
  2521. 1:41:32So, indexing and slicing.
  2522. 1:41:35If we have a tensor 1 2 3 4 5 6 7 8 9,
  2523. 1:41:41uh then uh we can
  2524. 1:41:44index. So, this is the uh this element
  2525. 1:41:46has indices 0 0 because it's zero row
  2526. 1:41:49and zero column.
  2527. 1:41:51Uh it starts from zero, not from one.
  2528. 1:41:53So, then this element would have 1 1
  2529. 1:41:56because it's
  2530. 1:41:57So, this is zeroth first row, second
  2531. 1:42:00row.
  2532. 1:42:01Zero column, one column, two column.
  2533. 1:42:04So, this has 1 1. This has uh two
  2534. 1:42:09So, this has No, this is 1 2 because row
  2535. 1:42:12one and column two.
  2536. 1:42:16So,
  2537. 1:42:18we see that
  2538. 1:42:20uh t of 0 1 is
  2539. 1:42:22two. So, zeroth column
  2540. 1:42:24zeroth row, first column.
  2541. 1:42:27That's going to be zeroth row
  2542. 1:42:30and then column one, that's going to be
  2543. 1:42:31two.
  2544. 1:42:33You can also access entire row. So, this
  2545. 1:42:36will give you entire row.
  2546. 1:42:39And
  2547. 1:42:40let's see.
  2548. 1:42:421 2 3, entire row.
  2549. 1:42:44And you can also get the first column or
  2550. 1:42:46entire column not the entire column.
  2551. 1:42:49But you need to put uh this sign here.
  2552. 1:42:53And this means get all of the rows
  2553. 1:42:56in the zeroth column. All of the rows in
  2554. 1:42:59the zeroth column.
  2555. 1:43:02So, all of the rows, but only in the
  2556. 1:43:05zeroth column. So, 1 4 7.
  2557. 1:43:09So, 1 4 7 is the It's going to be our
  2558. 1:43:12new tensor.
  2559. 1:43:14And you say all of the rows in the
  2560. 1:43:17column with index one.
  2561. 1:43:20So, that's going to be all of the rows,
  2562. 1:43:22but in this column with index one. 2 5
  2563. 1:43:258.
  2564. 1:43:272 5 8.
  2565. 1:43:29Now, let's see slicing. So, if you say t
  2566. 1:43:33and then you say 0 to 2,
  2567. 1:43:36this will give you all of the rows
  2568. 1:43:40uh starting from the zero including row
  2569. 1:43:42zero
  2570. 1:43:43until two, but excluding two. So, this
  2571. 1:43:47is excluded. This is inclusive. This is
  2572. 1:43:49exclusive. So,
  2573. 1:43:50starting from here until here, but
  2574. 1:43:53excluding this one. So, it will just
  2575. 1:43:54give you these two.
  2576. 1:43:561 2 3 4 5 6 7 8.
  2577. 1:44:00Let's see. 1 2 3 4 5 6 7 8.
  2578. 1:44:05And then uh I'm going to now go a bit
  2579. 1:44:08quicker. So, this will go
  2580. 1:44:09from index from row one to the end. So,
  2581. 1:44:13from row one including until the end.
  2582. 1:44:15So, it will give you these two.
  2583. 1:44:17You can check below. So, this will give
  2584. 1:44:19you from the beginning until two, but
  2585. 1:44:21excluding two.
  2586. 1:44:23So, from the beginning, so it will give
  2587. 1:44:25this one and this one, and it will not
  2588. 1:44:27give you two. This is index two. So,
  2589. 1:44:28this is index 0 1 2. It will not give
  2590. 1:44:31you two.
  2591. 1:44:32From the beginning until two, but not
  2592. 1:44:34two.
  2593. 1:44:37Okay, and then you can also say columns.
  2594. 1:44:40So, because you have comma here,
  2595. 1:44:42this is where you select rows, and here
  2596. 1:44:44this is all rows. This means all rows.
  2597. 1:44:46So, from the beginning to the end.
  2598. 1:44:49And in this case, end is also included.
  2599. 1:44:52So, when you have this, you it's from
  2600. 1:44:53the beginning to the end in rows, and
  2601. 1:44:55everything is included.
  2602. 1:44:57So, from zero to two columns, but two is
  2603. 1:45:00not included.
  2604. 1:45:02Okay, so we got all of the rows, and
  2605. 1:45:04then 0 1.
  2606. 1:45:08But
  2607. 1:45:09two is not included. So, it should just
  2608. 1:45:10give you these two. 1 2 5 6 9 10. Let's
  2609. 1:45:14see.
  2610. 1:45:151 2 5 6 9 10.
  2611. 1:45:19Okay.
  2612. 1:45:20Uh we can also get all of the rows and
  2613. 1:45:22from two until the end. So, that's going
  2614. 1:45:25to
  2615. 1:45:25say
  2616. 1:45:29Let me see here.
  2617. 1:45:31Uh 3 4 7 Yeah, that's it. So, from two
  2618. 1:45:33including two to to end columns.
  2619. 1:45:37And you can also pick like this. So,
  2620. 1:45:39uh from zero to two excluding two rows
  2621. 1:45:43and from one to three columns. So, from
  2622. 1:45:46zero to two and from one is going to be
  2623. 1:45:49this one to three, but excluding three.
  2624. 1:45:52So, this will just give you
  2625. 1:45:54this middle
  2626. 1:45:55What was the
  2627. 1:45:57columns?
  2628. 1:45:59So, it will give you this middle matrix.
  2629. 1:46:02You can also create a Boolean mask. So,
  2630. 1:46:04let me show you what that is. So, if you
  2631. 1:46:06have a tensor 1 2 3 4, etc.
  2632. 1:46:10You can say mask is equal to tensor t
  2633. 1:46:13greater than five.
  2634. 1:46:16So, what that's going to do is
  2635. 1:46:19replace everything that's greater than
  2636. 1:46:21five with true.
  2637. 1:46:24All of the where the value is greater
  2638. 1:46:26than five. So, this is
  2639. 1:46:27uh 1 2 3 4 5, okay? And then this is
  2640. 1:46:31greater than five. 6 7 8 These these
  2641. 1:46:34elements will be replaced with true
  2642. 1:46:35because these are the values.
  2643. 1:46:37So, if I was to put number seven here
  2644. 1:46:41instead of the number one that was
  2645. 1:46:42originally, then this would also be true
  2646. 1:46:44because this element would also be
  2647. 1:46:46greater than five. So, all of the
  2648. 1:46:47elements
  2649. 1:46:49values that are greater than five, they
  2650. 1:46:50get
  2651. 1:46:51replaced with true.
  2652. 1:46:55And then you can use that mask that you
  2653. 1:46:57just created.
  2654. 1:46:59So, you can say t or original tensor t
  2655. 1:47:03of mask,
  2656. 1:47:06it will give you all of the elements
  2657. 1:47:08that have the true
  2658. 1:47:10uh assigned to them. Like
  2659. 1:47:12So, 6 7 8 9.
  2660. 1:47:16You also can do this conditional
  2661. 1:47:18indexing. So, t where the value is
  2662. 1:47:21greater than five
  2663. 1:47:24or t where the number is even. So, value
  2664. 1:47:28divided by two gives remain remainder
  2665. 1:47:31zero.
  2666. 1:47:32So, these will give us
  2667. 1:47:34greater than five and even numbers.
  2668. 1:47:38You can also modify values like this.
  2669. 1:47:41So, t
  2670. 1:47:43where the value is greater than 50, you
  2671. 1:47:45want to replace all with zero. So,
  2672. 1:47:48everywhere in this tensor t where the
  2673. 1:47:51value is greater than 50, it will all be
  2674. 1:47:53replaced with zero.
  2675. 1:47:56So, let's say we have these values here.
  2676. 1:47:59100 200 300 400. And then
  2677. 1:48:03it's going to get replaced with zero
  2678. 1:48:04because if we put put it this way. If we
  2679. 1:48:09Now, let's learn about concatenating
  2680. 1:48:10tensors. So, if you have two tensors,
  2681. 1:48:131 2 3 4 and 5 6 7 8,
  2682. 1:48:17you can just concatenate them
  2683. 1:48:19uh in dimension zero or concatenate them
  2684. 1:48:23or join them in dimension one.
  2685. 1:48:26Let's see what both of those
  2686. 1:48:29uh do.
  2687. 1:48:31So, concatenating at dimension zero will
  2688. 1:48:34just append
  2689. 1:48:37all of these rows. Now, it's all going
  2690. 1:48:38to become like a bunch of these rows.
  2691. 1:48:40You see?
  2692. 1:48:411 2 So, this guy this guy then just
  2693. 1:48:44continue with this row and this row.
  2694. 1:48:48So, now they're concatenated like that.
  2695. 1:48:49So, it's 4 2. The new shape is 4 2. This
  2696. 1:48:53dimension got concatenated. The first
  2697. 1:48:55dimension, dimension zero.
  2698. 1:48:58And if we concatenate on dimension one
  2699. 1:49:00or second dimension, then
  2700. 1:49:03it will go like this. 1 2 5 6. So, the
  2701. 1:49:07first element here and the first element
  2702. 1:49:09here
  2703. 1:49:11will get here.
  2704. 1:49:13And then 3 4 7 8. And now the shape is 2
  2705. 1:49:164. So [snorts] this also requires some
  2706. 1:49:19playing around, testing. You need to see
  2707. 1:49:21like what happens. Try to understand it
  2708. 1:49:23yourself. This is something that's um
  2709. 1:49:26that you just need to
  2710. 1:49:27play around with and understand.
  2711. 1:49:30You can also concatenate three tensors
  2712. 1:49:32and it's done with torch.cat. I forgot
  2713. 1:49:35to say.
  2714. 1:49:36So three tensors, it's going to do the
  2715. 1:49:38same thing.
  2716. 1:49:40So you're just going to say 1 2 3 4 5 6
  2717. 1:49:42etc. So the same thing. Now it's 6 2.
  2718. 1:49:46You can also torch.stack.
  2719. 1:49:49If you have two matrices
  2720. 1:49:522 by 2 and 2 by 2
  2721. 1:49:54you can kind of just put them into
  2722. 1:49:57another array, another tensor.
  2723. 1:50:00So this is this first matrix will be the
  2724. 1:50:03first element.
  2725. 1:50:04And the second matrix will be the second
  2726. 1:50:06element
  2727. 1:50:07of this. So now the shape is 2 2 2
  2728. 1:50:09because
  2729. 1:50:11you join these two these two matrices
  2730. 1:50:15into like another array now.
  2731. 1:50:18So that's what you can do with this
  2732. 1:50:20torch.stack
  2733. 1:50:22uh tensor one tensor two along
  2734. 1:50:25a dimension zero.
  2735. 1:50:26Now I'm in the last seventh lesson.
  2736. 1:50:30Let's see some more tricks. So we have
  2737. 1:50:32identity matrix. If you say torch.eye 3
  2738. 1:50:39uh let's see. This will give you
  2739. 1:50:42uh this identity matrix where it's ones
  2740. 1:50:45on the diagonal and zeros outside. If
  2741. 1:50:47you check my math course, you will see
  2742. 1:50:49that multiplying
  2743. 1:50:51any matrix with this identity matrix
  2744. 1:50:54will give you that same matrix. This is
  2745. 1:50:56like multiplying with one.
  2746. 1:50:59This identity matrix.
  2747. 1:51:02So if you have any matrix
  2748. 1:51:04you multiply that with identity matrix.
  2749. 1:51:07So matrix multiply it will give you the
  2750. 1:51:09same matrix.
  2751. 1:51:12torch.arange
  2752. 1:51:1310 will give you an array from 0 to 9
  2753. 1:51:17because this is excluded. So integers
  2754. 1:51:19from 0 to 9.
  2755. 1:51:22Let's check here. 0 to 9.
  2756. 1:51:25arange uh torch.arange 2 to 8 will give
  2757. 1:51:29you integers 2 to 7 because this last is
  2758. 1:51:33excluded.
  2759. 1:51:362 to 7.
  2760. 1:51:38You can also say arrange 0 to 10 with
  2761. 1:51:40step 2.
  2762. 1:51:42This means it will go 0 2 4 6 8 and 10.
  2763. 1:51:46The last one is excluded.
  2764. 1:51:48You can say step 0.5.
  2765. 1:51:51So it will go 0 0.5 1 1.5 until 4.5.
  2766. 1:51:58You can also set data type to be float
  2767. 1:52:0132. Then it will give you this like 0.1.
  2768. 1:52:03So these are the floats.
  2769. 1:52:06And this is what you will often do is
  2770. 1:52:08you will arrange
  2771. 1:52:09and then reshape.
  2772. 1:52:11So this will give you numbers 0 to 11.
  2773. 1:52:13So there will be 12 numbers.
  2774. 1:52:15And then reshape to 3 4, three rows four
  2775. 1:52:18columns.
  2776. 1:52:20And this is what it's going to look
  2777. 1:52:21like. You're going to do this often.
  2778. 1:52:24linspace
  2779. 1:52:26will give you
  2780. 1:52:27the following. So if you say 0 to 1 and
  2781. 1:52:30five values, so it will
  2782. 1:52:32give you five values in between these
  2783. 1:52:35numbers
  2784. 1:52:37equally spaced including these two
  2785. 1:52:39numbers.
  2786. 1:52:42So
  2787. 1:52:43starting from 0 to 1
  2788. 1:52:46and in between and it's evenly spaced.
  2789. 1:52:480.25 0.25.
  2790. 1:52:52So then you can also say from 0 to 10
  2791. 1:52:55and then 10 evenly spaced numbers.
  2792. 1:52:58And you see here in linspace this 10 is
  2793. 1:53:01also included. So previously it was
  2794. 1:53:03excluded, but now it's included. You
  2795. 1:53:05need to just memorize that. Those are
  2796. 1:53:07That's how it works.
  2797. 1:53:10From 5 to minus from minus 5 to 5, 11
  2798. 1:53:14numbers. So in this case it will just be
  2799. 1:53:17the integers.
  2800. 1:53:19And this is interesting. If you say
  2801. 1:53:20torch.empty
  2802. 1:53:22and then the shape, so
  2803. 1:53:24this will create a tensor or in memory,
  2804. 1:53:27but it will not change the values.
  2805. 1:53:30It will not set any values. So it will
  2806. 1:53:32just allocate space in memory. So
  2807. 1:53:34whatever numbers or values were in the
  2808. 1:53:36memory, it will have
  2809. 1:53:38uh these values.
  2810. 1:53:42You see we have some garbage values here
  2811. 1:53:44because we did not change them. We just
  2812. 1:53:45took the memory
  2813. 1:53:47but we didn't change anything.
  2814. 1:53:50So that's going to be it for this video.
  2815. 1:53:52Check out the GitHub. Check out the
  2816. 1:53:54previous course on math. Uh join my
  2817. 1:53:56school and see you in the next video.
  2818. 1:54:00Let's learn about neural networks. After
  2819. 1:54:02this lesson you will be able to code
  2820. 1:54:04your own neural network from scratch.
  2821. 1:54:06You will understand how this works and
  2822. 1:54:08you will be able to then further build
  2823. 1:54:11it into projects and AI research etc.
  2824. 1:54:14Make sure to have PyTorch installed,
  2825. 1:54:16NumPy, Matplotlib as well. You can find
  2826. 1:54:19this repository uh at the GitHub below
  2827. 1:54:21this video.
  2828. 1:54:22So this is a neural network, but let's
  2829. 1:54:25just first focus on a single neuron. So
  2830. 1:54:28let's just look at this first blue
  2831. 1:54:30circle and the first white circle and
  2832. 1:54:32this output. And let's imagine none of
  2833. 1:54:35these other exist.
  2834. 1:54:37So input will be some number.
  2835. 1:54:40And then it's multiplied with some
  2836. 1:54:42weight. This weight is learned by the
  2837. 1:54:43neural network. So this weight will also
  2838. 1:54:46be some number.
  2839. 1:54:47And then input is multiplied with this
  2840. 1:54:50weight, it will give some output.
  2841. 1:54:52So the output is going to trans So the
  2842. 1:54:56neural network will transform this input
  2843. 1:54:58into output. It will predict something.
  2844. 1:55:00And the weight that's multiplying the
  2845. 1:55:03input and transforming it into output is
  2846. 1:55:05going to be learned during training.
  2847. 1:55:08So what is the best weight that's going
  2848. 1:55:09to give us correct output based on any
  2849. 1:55:11input?
  2850. 1:55:12That's how neural network learns and
  2851. 1:55:14then generates
  2852. 1:55:15or predicts.
  2853. 1:55:17So let's check code example.
  2854. 1:55:19Let's say we have inputs two and three.
  2855. 1:55:22And then these would be weights.
  2856. 1:55:26Uh these would be neural network weights
  2857. 1:55:27like 0.5 and 0.3 that are learned.
  2858. 1:55:31That are already learned. So we'll see
  2859. 1:55:32later how they are learned.
  2860. 1:55:34Now we could also have a bias. It's
  2861. 1:55:38just a single number that's added
  2862. 1:55:41for the entire layer, entire set of
  2863. 1:55:44weights. Entire set of weights has a
  2864. 1:55:45single bias number and you see here.
  2865. 1:55:49The output, the weighted sum
  2866. 1:55:51it's going to be
  2867. 1:55:53this weight
  2868. 1:55:54times this input
  2869. 1:55:56plus this weight times this input.
  2870. 1:55:59And then at the end we have some bias.
  2871. 1:56:00So bias is also learned. It also
  2872. 1:56:03helps. If you know functions, bias will
  2873. 1:56:05help
  2874. 1:56:06uh this function, whatever it is, to
  2875. 1:56:08move left and right.
  2876. 1:56:09And these weights are going to determine
  2877. 1:56:11the slope.
  2878. 1:56:13So this is in my math course uh for
  2879. 1:56:15machine learning that I published
  2880. 1:56:17earlier.
  2881. 1:56:18In this case this is just a dot product
  2882. 1:56:21right here. It's just a dot product
  2883. 1:56:22between input and weights.
  2884. 1:56:24And so
  2885. 1:56:27uh weighted sum is two in this case.
  2886. 1:56:31And then
  2887. 1:56:32uh because this is going to be some
  2888. 1:56:34linear function. If we want to make it
  2889. 1:56:36maybe curved we need to actually pass
  2890. 1:56:39this through some activation function.
  2891. 1:56:40For example, sigmoid.
  2892. 1:56:43Now what When we pass this output
  2893. 1:56:45weighted sum through
  2894. 1:56:47some function like sigmoid, it looks
  2895. 1:56:49kind of weird, but let's just ignore
  2896. 1:56:52like weights weights like this. Let's
  2897. 1:56:54just imagine that sigmoid is going to
  2898. 1:56:56make this function like curved.
  2899. 1:56:58So
  2900. 1:56:59if you have some data points here, data
  2901. 1:57:01points here, you want to actually
  2902. 1:57:03separate them with a curved function.
  2903. 1:57:06If you have a straight line, then it
  2904. 1:57:08will not separate these like messy data
  2905. 1:57:10points. So you need to be able to curve
  2906. 1:57:12the function to separate the data
  2907. 1:57:13points.
  2908. 1:57:14That's what activation function will
  2909. 1:57:17It will take this weighted sum, which is
  2910. 1:57:18a line
  2911. 1:57:19and make it curved.
  2912. 1:57:24And so at the end for these specific
  2913. 1:57:26numbers we get this activation or
  2914. 1:57:30output.
  2915. 1:57:32So this here is sigmoid function.
  2916. 1:57:35We also have other activation functions
  2917. 1:57:37like like ReLU and their purpose in
  2918. 1:57:39neural networks is to curve the lines
  2919. 1:57:42that are because uh lines we get we just
  2920. 1:57:44multiplying weights and that bias are
  2921. 1:57:46straight lines.
  2922. 1:57:48Because here uh let's say we want to
  2923. 1:57:51separate
  2924. 1:57:53uh maybe this red this red data set. We
  2925. 1:57:55cannot do it with a straight line
  2926. 1:57:57because it will it can just like cross
  2927. 1:58:00over it. We need a curved line that goes
  2928. 1:58:02around this like this.
  2929. 1:58:04So that's why
  2930. 1:58:05we have activation functions to curve
  2931. 1:58:07the lines.
  2932. 1:58:10So that's how neural network will be
  2933. 1:58:11able to learn how to separate these data
  2934. 1:58:14sets, but let's see on some examples
  2935. 1:58:15more. Join my school community to become
  2936. 1:58:18AI researcher. Link below or you can
  2937. 1:58:21find link on my channel here in the
  2938. 1:58:22about section.
  2939. 1:58:24So usually you would first initialize uh
  2940. 1:58:27a neuron. So weights would be
  2941. 1:58:30initialized as random numbers from a
  2942. 1:58:32normal distribution. I explained that in
  2943. 1:58:34previous PyTorch lesson on my channel.
  2944. 1:58:37And [snorts] then make it very small. So
  2945. 1:58:40uh maybe these would be around like 0 to
  2946. 1:58:431, a bit more maybe.
  2947. 1:58:45But we won't make them all very small,
  2948. 1:58:47so they're around 0.1.
  2949. 1:58:50Bias goes at 0 because bias is being
  2950. 1:58:53added. So in the beginning we add 0.
  2951. 1:58:58But you don't want to initialize weights
  2952. 1:58:59at 0 because weights are multiplying. So
  2953. 1:59:02whatever then they will just turn
  2954. 1:59:04everything to zero, so that's not good.
  2955. 1:59:05You want to initialize weight with some
  2956. 1:59:07number.
  2957. 1:59:08And here we would instantiate this class
  2958. 1:59:11neuron, and let's say number of inputs
  2959. 1:59:13is equal to two.
  2960. 1:59:15So, then
  2961. 1:59:16this will create two weights and one
  2962. 1:59:19bias. So, this number of input just
  2963. 1:59:20influence the the amount of weights.
  2964. 1:59:24So, this is example.
  2965. 1:59:26Number of inputs two, and these are the
  2966. 1:59:27random weights that we generated.
  2967. 1:59:29They're really very small because we
  2968. 1:59:310.1.
  2969. 1:59:35So, then
  2970. 1:59:36uh to our neuron class, we want to add
  2971. 1:59:38this weighted sum function.
  2972. 1:59:41So, uh it will take inputs, the numbers.
  2973. 1:59:46So, this is different from number of
  2974. 1:59:47input. This is the the input, the actual
  2975. 1:59:48inputs numbers.
  2976. 1:59:50And this will do dot product between
  2977. 1:59:52weights and inputs. Now, this is also
  2978. 1:59:55matrix multiplication. It says here dot,
  2979. 1:59:57it's a bit confusingly named, but this
  2980. 1:59:59is matrix multiplication between weights
  2981. 2:00:01and inputs, and then add bias at the
  2982. 2:00:03end.
  2983. 2:00:06And then we're also going to add this
  2984. 2:00:07part to our code. So, after we
  2985. 2:00:09initialize, we already we already seen
  2986. 2:00:11this part of initialization.
  2987. 2:00:14We want to add this part. So, I set some
  2988. 2:00:17inputs here.
  2989. 2:00:18And then I call neuron.weighted_sum.
  2990. 2:00:21I call our new function to do matrix
  2991. 2:00:22multiplication.
  2992. 2:00:26So, the weighted sum here is
  2993. 2:00:28uh
  2994. 2:00:29two.
  2995. 2:00:31Which also includes this bias, by the
  2996. 2:00:33way.
  2997. 2:00:35Now, we will add third uh function to
  2998. 2:00:37our neuron class, which is going to be
  2999. 2:00:39this sigmoid function.
  3000. 2:00:41And so, it makes it non-linear, so it
  3001. 2:00:44curves our line. Because when we just
  3002. 2:00:46multiply with inputs with weights, and
  3003. 2:00:48that bias is going to be a straight
  3004. 2:00:49line. The function is going to be a
  3005. 2:00:51straight line. The function of weights
  3006. 2:00:53times inputs plus bias.
  3007. 2:00:56But sigmoid make makes it curve, or
  3008. 2:00:58activation function makes it curved. And
  3009. 2:01:00we will use sigmoid because it has like
  3010. 2:01:02a nice curvy shape that neural network
  3011. 2:01:05can use well.
  3012. 2:01:07So, this is the formula for the sigmoid
  3013. 2:01:08function. We need to
  3014. 2:01:10pass in this number.
  3015. 2:01:12And then the formula is one over one
  3016. 2:01:15plus e to
  3017. 2:01:17-x.
  3018. 2:01:21And then we will just pass our
  3019. 2:01:24activation,
  3020. 2:01:25or I should say the output of the
  3021. 2:01:27weighted sum,
  3022. 2:01:28through sigmoid.
  3023. 2:01:30And we will get some
  3024. 2:01:32uh activation value.
  3025. 2:01:36Sigmoid will be between zero and one
  3026. 2:01:37like this. It's going to be curved. So,
  3027. 2:01:39if neural network needs to make this uh
  3028. 2:01:42maybe left curve, it will make weights
  3029. 2:01:45and biases such that we get some number
  3030. 2:01:48around minus two and minus one or
  3031. 2:01:50something like that.
  3032. 2:01:51And then after activation, it will get
  3033. 2:01:53this left curve to maybe encircle some
  3034. 2:01:55data.
  3035. 2:01:56If it needs this other right curve,
  3036. 2:01:58then it will uh
  3037. 2:02:01make its learn its weights and biases
  3038. 2:02:03to give a number around one, two.
  3039. 2:02:06So, it will get this curve here to
  3040. 2:02:08encircle some other data in this way.
  3041. 2:02:12So, this activation function will never
  3042. 2:02:13change. It's just the weights and
  3043. 2:02:14biases, but the neural network will
  3044. 2:02:16learn to adjust weights and biases, so
  3045. 2:02:18when
  3046. 2:02:19the output is passed through activation
  3047. 2:02:22function, they will properly encircle
  3048. 2:02:24data.
  3049. 2:02:26Or be able to predict or generate data.
  3050. 2:02:29That is it will have proper
  3051. 2:02:30understanding of the data.
  3052. 2:02:32Now, we can test uh this with different
  3053. 2:02:35different input, sorry. So, 0 0 1 0 2 3.
  3054. 2:02:39And you will see that the output is
  3055. 2:02:41always some number between zero and one.
  3056. 2:02:46We can also try different activation
  3057. 2:02:47functions. So, sigmoid will give 0.8.
  3058. 2:02:51Uh ReLU will give two.
  3059. 2:02:54This tanh will give 0.96. So, depending
  3060. 2:02:57on the activation function, you can uh
  3061. 2:02:59you need to find one that works well for
  3062. 2:03:01your data set. For example, ReLU is very
  3063. 2:03:03easy and fast to compute, but it's not
  3064. 2:03:06as powerful. So, maybe if you need some
  3065. 2:03:08quick and easy task.
  3066. 2:03:10Now, let's see how to combine multiple
  3067. 2:03:12of these neurons, not just one, into a
  3068. 2:03:15neural network layer.
  3069. 2:03:17So, again we will have input two, three,
  3070. 2:03:19let's say. But then we will have two
  3071. 2:03:21neurons. The first neuron is this one,
  3072. 2:03:230.5 0.3.
  3073. 2:03:25And the second neuron is 0.2 0.4. So,
  3074. 2:03:28you see that it has weights for each of
  3075. 2:03:30the inputs.
  3076. 2:03:33And we will have two biases, so one for
  3077. 2:03:35each of the neurons.
  3078. 2:03:38And the forward pass is the matrix
  3079. 2:03:40multiplication between inputs and
  3080. 2:03:42weights transposed. So, uh I explained
  3081. 2:03:45matrix multiplication in math and
  3082. 2:03:46PyTorch fundamentals,
  3083. 2:03:48but uh you can also check how it works,
  3084. 2:03:50but you need to transpose weights here.
  3085. 2:03:52Now,
  3086. 2:03:53uh some people can also write like
  3087. 2:03:55weights
  3088. 2:03:57uh
  3089. 2:03:58mat matrix multiply inputs or inputs
  3090. 2:04:00transposed. Now, this just depends on
  3091. 2:04:02how you save your weights in memory.
  3092. 2:04:04You know that matrix multiplication,
  3093. 2:04:06there are some rules.
  3094. 2:04:08But for now, let's just take it as is.
  3095. 2:04:10So, inputs at uh
  3096. 2:04:12weights transposed plus biases, so we
  3097. 2:04:14have
  3098. 2:04:15input tensor
  3099. 2:04:17at weights transposed.
  3100. 2:04:20At the end, we get this output two, 1.8.
  3101. 2:04:24And then if we pass it through sigmoid
  3102. 2:04:26activation, each of them will be uh some
  3103. 2:04:29number between zero and one, depending
  3104. 2:04:32on the input here.
  3105. 2:04:34So, [clears throat] here we would define
  3106. 2:04:36a class layer,
  3107. 2:04:38and then it would uh have number of
  3108. 2:04:40inputs and number of neurons. So, these
  3109. 2:04:42are like hidden neurons. They It can be
  3110. 2:04:45different number. You see here we have
  3111. 2:04:47three inputs, but five neurons, five
  3112. 2:04:49hidden neurons, I should say.
  3113. 2:04:52And then one output at the end.
  3114. 2:04:54So, in our linear layer that we
  3115. 2:04:56initialize, we have number of inputs and
  3116. 2:04:57number of neurons.
  3117. 2:04:59And weights are going to be
  3118. 2:05:02uh
  3119. 2:05:02number of neurons {comma} number of
  3120. 2:05:04inputs.
  3121. 2:05:06This is one of those weird things that
  3122. 2:05:08you just need to kind of get the
  3123. 2:05:09intuition by uh trying and
  3124. 2:05:11understanding. So,
  3125. 2:05:12number of neurons is three means we have
  3126. 2:05:15we have three rows.
  3127. 2:05:16One, two, three rows.
  3128. 2:05:18And number of input is two.
  3129. 2:05:21And that's the second dimension here.
  3130. 2:05:22So, we have
  3131. 2:05:23two columns. So, in each row there is
  3132. 2:05:26two. So, two two two. So, three rows,
  3133. 2:05:29two columns.
  3134. 2:05:32And that's why we also need to transpose
  3135. 2:05:33because we have two inputs.
  3136. 2:05:35We need to transpose this matrix so we
  3137. 2:05:37because of matrix multiplication, we
  3138. 2:05:39will
  3139. 2:05:39multiply these two and these two, and
  3140. 2:05:41then these two these two, these two
  3141. 2:05:42these two. So, we need to transpose,
  3142. 2:05:44flip the matrix
  3143. 2:05:46of weights.
  3144. 2:05:47But I recommend you just try to
  3145. 2:05:49understand this yourself, or uh you try
  3146. 2:05:51like playing around, see what happens,
  3147. 2:05:52try to draw.
  3148. 2:05:54Understand how matrix multiplication
  3149. 2:05:55works first. You need to practice this.
  3150. 2:05:58These are the example weights that are
  3151. 2:06:00initialized.
  3152. 2:06:02And the input is two, so when you
  3153. 2:06:05multiply input matrix and weights
  3154. 2:06:08matrix, you need to transpose this
  3155. 2:06:11because you are
  3156. 2:06:12multiplying
  3157. 2:06:14uh row of the input
  3158. 2:06:15with the column of the weights.
  3159. 2:06:18So, you want to multiply want to
  3160. 2:06:20multiply it with this row, but you
  3161. 2:06:22cannot multiply row and row. So, you
  3162. 2:06:23need to transpose this row to become a
  3163. 2:06:25column.
  3164. 2:06:27So, now it's just two two two, like
  3165. 2:06:29this.
  3166. 2:06:31So, then after initialization, we will
  3167. 2:06:33add this weighted sum. And here we just
  3168. 2:06:36do
  3169. 2:06:37inputs matrix multiply weights
  3170. 2:06:39transposed. We need to transpose them as
  3171. 2:06:41I explained plus biases.
  3172. 2:06:46And at the end, you will get something
  3173. 2:06:48like this.
  3174. 2:06:50Um we will get three numbers because
  3175. 2:06:53the hidden number of neurons is three.
  3176. 2:06:56You know, we are multiplying
  3177. 2:06:581 * 2 matrix of inputs
  3178. 2:07:00times
  3179. 2:07:02uh
  3180. 2:07:022 * 3
  3181. 2:07:04matrix of weights, so the result will be
  3182. 2:07:061 * 3. So, this is matrix
  3183. 2:07:08multiplication. So, you have these three
  3184. 2:07:10neurons
  3185. 2:07:12uh that you get
  3186. 2:07:14as as a result.
  3187. 2:07:15So, again I recommend you try this out
  3188. 2:07:17yourself, change some numbers, try to
  3189. 2:07:19understand how this matrix
  3190. 2:07:20multiplication works.
  3191. 2:07:22And then it's very easy to add this
  3192. 2:07:24activation function. So, torch.sigmoid
  3193. 2:07:28and pass in the neurons.
  3194. 2:07:31So, before the activation, the numbers
  3195. 2:07:33that we got
  3196. 2:07:35of the neurons are these, and then after
  3197. 2:07:37activation, it's going to be this
  3198. 2:07:39uh tensor or vector.
  3199. 2:07:41So, after we uh have this sigmoid, we
  3200. 2:07:44would add that to this forward function.
  3201. 2:07:47So, in the forward function, we first
  3202. 2:07:50do inputs times weights plus biases, and
  3203. 2:07:53then pass it pass the result through
  3204. 2:07:56sigmoid activation, and then that's the
  3205. 2:07:57output. Those are the neurons that we
  3206. 2:07:59get.
  3207. 2:08:00And so, we can say outputs is equal to
  3208. 2:08:03layer.forward then pass in inputs. This
  3209. 2:08:05is how you would usually do it
  3210. 2:08:08in
  3211. 2:08:09uh neural networks, large language
  3212. 2:08:11models, reinforcement learning,
  3213. 2:08:12everywhere.
  3214. 2:08:13Here we can take a look at some
  3215. 2:08:15activation functions. So, I already
  3216. 2:08:17explained sigmoid, it puts any value on
  3217. 2:08:20x between uh zero and one. Output is
  3218. 2:08:23always between zero and one.
  3219. 2:08:25Uh we also have ReLU. This is very easy
  3220. 2:08:27and quick to compute. So,
  3221. 2:08:29if the input is lower than zero,
  3222. 2:08:32or zero, it will just become zero. The
  3223. 2:08:35output will just become zero.
  3224. 2:08:37And if the input is larger than zero, it
  3225. 2:08:39will just remain that same number,
  3226. 2:08:40whatever it is.
  3227. 2:08:42So,
  3228. 2:08:43R of Z, Z is our number, is going to be
  3229. 2:08:46max maximum between zero and Z.
  3230. 2:08:49So, that's the ReLU function. You just
  3231. 2:08:51find uh set zero or Z, whichever is
  3232. 2:08:54bigger.
  3233. 2:08:55This is very fast to compute. You would
  3234. 2:08:57use this if you need some quick
  3235. 2:08:59computation with that's not so maybe
  3236. 2:09:01powerful or precise, but you just need
  3237. 2:09:02some
  3238. 2:09:04easy task for the neural network and
  3239. 2:09:05maybe it can do it with this activation
  3240. 2:09:08function. And you see here
  3241. 2:09:10it would just if you have a line it will
  3242. 2:09:12it will maybe [clears throat]
  3243. 2:09:14make that line like this. It's not going
  3244. 2:09:15to be curved. It's going to be like this
  3245. 2:09:17just like cut or
  3246. 2:09:19broken like this.
  3247. 2:09:21But it will be a continuous line. It
  3248. 2:09:23will not be like separated.
  3249. 2:09:25But for some tasks it's not good if you
  3250. 2:09:27make outputs that are lower than zero
  3251. 2:09:30exactly zero always. So you kind of
  3252. 2:09:31erase all of that information. So there
  3253. 2:09:34is a leaky ReLU.
  3254. 2:09:37So
  3255. 2:09:38outputs that are lower than zero it will
  3256. 2:09:40just make them a lot
  3257. 2:09:42closer to zero but not zero.
  3258. 2:09:45So it will just squish them towards zero
  3259. 2:09:48but they will still not be zero.
  3260. 2:09:50So they will be 0.1 times X if they are
  3261. 2:09:53lower than zero.
  3262. 2:09:55Or they will just remain X if they are
  3263. 2:09:58higher than zero.
  3264. 2:10:00So this doesn't erase completely
  3265. 2:10:01information like the ReLU function.
  3266. 2:10:05While it still provides non-linearity
  3267. 2:10:06this bending. So the neural network can
  3268. 2:10:09use this bending to
  3269. 2:10:11encircle some data.
  3270. 2:10:13Here we have some examples. You see this
  3271. 2:10:15is the zero. So ReLU is just
  3272. 2:10:17zero.
  3273. 2:10:19And then GELU
  3274. 2:10:21is actually
  3275. 2:10:22more curved around zero here. So it
  3276. 2:10:25preserves information if the number is
  3277. 2:10:27around minus two.
  3278. 2:10:30So and then we have ELU that's kind of
  3279. 2:10:34going down even more.
  3280. 2:10:37So we have these different activations.
  3281. 2:10:38You would test which ones work best for
  3282. 2:10:41your data.
  3283. 2:10:42Here is another example. Sigmoid, ReLU,
  3284. 2:10:47SiLU,
  3285. 2:10:49Swish, ELU.
  3286. 2:10:52They're all kind of similar.
  3287. 2:10:53But they do a bit different things.
  3288. 2:10:56But they will all curve your line of
  3289. 2:10:59weights and outputs and neurons. You see
  3290. 2:11:02with just multiplying weights and inputs
  3291. 2:11:04and biases you would get a straight
  3292. 2:11:06line, but then passing that through
  3293. 2:11:08activation function you would get a
  3294. 2:11:10curvy line.
  3295. 2:11:12This is a good example as well.
  3296. 2:11:14You want to fit this data with a
  3297. 2:11:16straight line. It's going to be tough.
  3298. 2:11:18You're going to have to leave out a
  3299. 2:11:19bunch of this red data
  3300. 2:11:21on the other side which is wrong. But
  3301. 2:11:23with a curvy line maybe this is a
  3302. 2:11:25sigmoid function here. You can fit data
  3303. 2:11:28properly.
  3304. 2:11:29Now to understand how neural networks
  3305. 2:11:31learn I have three good videos. So you
  3306. 2:11:34can watch this one backpropagation from
  3307. 2:11:36scratch, torch.backward, and this one
  3308. 2:11:38where I train a neural network. I'm
  3309. 2:11:40going to leave all
  3310. 2:11:42these links in the description.
  3311. 2:11:44Or you can just search for them.
  3312. 2:11:48I also put these three videos here in
  3313. 2:11:50the third lesson after
  3314. 2:11:52these neural network and layer and
  3315. 2:11:54stuff. So you can just copy these URLs.
  3316. 2:11:57That's going to be it for this video and
  3317. 2:11:59see you in the next one.
  3318. 2:12:01Welcome to attention mechanism and
  3319. 2:12:04self-attention in transformers in large
  3320. 2:12:06language models.
  3321. 2:12:07So let's say we have a sentence the cat
  3322. 2:12:10sat on the mat.
  3323. 2:12:12Let's say we are now we have just these
  3324. 2:12:14words in front of the mat and we're
  3325. 2:12:16trying to predict the next word
  3326. 2:12:19that is mat.
  3327. 2:12:21So how do we know
  3328. 2:12:24what's the next word? Well, we're going
  3329. 2:12:26to take some information from the
  3330. 2:12:27previous words.
  3331. 2:12:29So we have two challenges here.
  3332. 2:12:32How do we know from which words to take
  3333. 2:12:34information? For example here cat sat
  3334. 2:12:37cat sat. That would give us most clue
  3335. 2:12:41that this could be like a mat.
  3336. 2:12:44>> [snorts]
  3337. 2:12:44>> So how to select previous words and how
  3338. 2:12:47to actually take information? How do we
  3339. 2:12:48actually like take information? What
  3340. 2:12:50does that mean? How is that done?
  3341. 2:12:53So in this example we see that cat is
  3342. 2:12:55selected to have 60%
  3343. 2:12:58relevance,
  3344. 2:12:59sat 20%, the 10%, on the these are have
  3345. 2:13:04smaller. Okay.
  3346. 2:13:06And so you can ignore this last imagine
  3347. 2:13:09this doesn't exist. So I'm explaining
  3348. 2:13:11like how to generate this in this case.
  3349. 2:13:15Attention weights will sum up to 100.
  3350. 2:13:18So do they sum up? I think they do.
  3351. 2:13:21Okay? But if we didn't have this 3% this
  3352. 2:13:24one then this 3% would actually be like
  3353. 2:13:27within these as well. So these would sum
  3354. 2:13:29up to 100 if we didn't have this last
  3355. 2:13:32word that we are trying to predict.
  3356. 2:13:35So model learns
  3357. 2:13:36to which word to focus on.
  3358. 2:13:39The way LLM works is
  3359. 2:13:42if you are predicting this word mat it's
  3360. 2:13:44going to
  3361. 2:13:45look take this word previous word the
  3362. 2:13:48and combine
  3363. 2:13:50add all of the irrelevant information
  3364. 2:13:52into this word. So now
  3365. 2:13:54this word will have not only this word
  3366. 2:13:56but all of the context before it as
  3367. 2:13:58well.
  3368. 2:13:59And then after processing processing
  3369. 2:14:01processing
  3370. 2:14:02move this this is done like many times
  3371. 2:14:06over and over again.
  3372. 2:14:08>> [snorts]
  3373. 2:14:08>> It will just convert the final vector
  3374. 2:14:10that's juiced up with all of the
  3375. 2:14:12previous information
  3376. 2:14:14into this next word.
  3377. 2:14:17So how do we juice up this vector with
  3378. 2:14:19all of the previous words all of the
  3379. 2:14:21previous information? So
  3380. 2:14:23every of the tokens including this one
  3381. 2:14:27will have
  3382. 2:14:29will create three things.
  3383. 2:14:31Query,
  3384. 2:14:33key,
  3385. 2:14:34and value.
  3386. 2:14:35This is confusing but bear with me.
  3387. 2:14:38So each of these words or tokens will be
  3388. 2:14:42like a vector embedding. It's a vector.
  3389. 2:14:44You can check my previous lessons math
  3390. 2:14:46PyTorch and neural networks to
  3391. 2:14:49understand this.
  3392. 2:14:50So it's a vector.
  3393. 2:14:52And so that vector will transform into
  3394. 2:14:54three vectors for each of the token
  3395. 2:14:57vectors. So each of the tokens here each
  3396. 2:14:59of them will get three.
  3397. 2:15:01Query, key, value.
  3398. 2:15:03And usually these query key and value
  3399. 2:15:04are a lot smaller five 10 times smaller
  3400. 2:15:07than this big token embedding. So
  3401. 2:15:10this token vector token embedding
  3402. 2:15:12represents the meaning
  3403. 2:15:14of these
  3404. 2:15:16words.
  3405. 2:15:18So just these numbers represent meaning.
  3406. 2:15:20So for example
  3407. 2:15:22large language model learns
  3408. 2:15:24what each of the numbers in the vector
  3409. 2:15:27each of the dimensions represents. For
  3410. 2:15:29example maybe the first number in the
  3411. 2:15:31vector represents if something is green.
  3412. 2:15:36So the higher the number the more this
  3413. 2:15:38token has this green characteristics.
  3414. 2:15:42So for example if you have like some
  3415. 2:15:45green apple
  3416. 2:15:47the first dimension will have a lot
  3417. 2:15:49higher value
  3418. 2:15:50than a word heart that's associated with
  3419. 2:15:54red for example.
  3420. 2:15:55So the first
  3421. 2:15:58number first dimension in the vector of
  3422. 2:16:00word heart will have a lot lower value.
  3423. 2:16:03So neural network will learn what these
  3424. 2:16:05features are and
  3425. 2:16:07in this vector this vector is same size
  3426. 2:16:10for every token. It represents the token
  3427. 2:16:12it's same size.
  3428. 2:16:14And
  3429. 2:16:15the position the dimension first second
  3430. 2:16:19it's always the same. So zero's
  3431. 2:16:22dimension always represents the same
  3432. 2:16:24thing for every token.
  3433. 2:16:26Like how this how green is this.
  3434. 2:16:30So each token will have query, key, and
  3435. 2:16:33value.
  3436. 2:16:34Query says which information am I
  3437. 2:16:37looking for?
  3438. 2:16:40And then
  3439. 2:16:42key says which information I have.
  3440. 2:16:47And the value is the information that it
  3441. 2:16:50gives. Now keep in mind there is a
  3442. 2:16:53difference between explaining which
  3443. 2:16:55information you have and that
  3444. 2:16:58information.
  3445. 2:17:00So that's important difference.
  3446. 2:17:02So
  3447. 2:17:04if we are on this word the
  3448. 2:17:06its query
  3449. 2:17:08will dot product
  3450. 2:17:10with all of the keys individually.
  3451. 2:17:13Okay?
  3452. 2:17:14So if this words this tokens query
  3453. 2:17:18dot product this tokens key as I said
  3454. 2:17:22key is explaining what this token holds
  3455. 2:17:24what's information.
  3456. 2:17:26If the dot product is a high number we
  3457. 2:17:28call that high affinity.
  3458. 2:17:31Then
  3459. 2:17:33you will just multiply the value
  3460. 2:17:35of this token the value
  3461. 2:17:38which is the actual information it gives
  3462. 2:17:41with that number
  3463. 2:17:43with that dot product of the query and
  3464. 2:17:45key
  3465. 2:17:46and add to this to value of this token.
  3466. 2:17:50Okay? In the end we are just adding
  3467. 2:17:52values.
  3468. 2:17:54But
  3469. 2:17:55but
  3470. 2:17:57the query and key have two functions.
  3471. 2:17:59How much to weight the value? How much
  3472. 2:18:02so it do you want to reduce? So if you
  3473. 2:18:04multiply the value of this with a small
  3474. 2:18:06number then when you add that small
  3475. 2:18:08number to this value
  3476. 2:18:10it will not influence a lot
  3477. 2:18:12because
  3478. 2:18:14this value will be multiplied with like
  3479. 2:18:160.01.
  3480. 2:18:17So when you add that it's a very small
  3481. 2:18:20number in each of the dimensions it will
  3482. 2:18:21not change too much this.
  3483. 2:18:24And
  3484. 2:18:26if that 0.01 is like the dot product
  3485. 2:18:30between
  3486. 2:18:31this query and this key. It means like
  3487. 2:18:34we don't care about I don't care about
  3488. 2:18:35this information too much. There is no
  3489. 2:18:36like affinity there.
  3490. 2:18:38But if that product is high
  3491. 2:18:41it can be
  3492. 2:18:43larger close to one for example.
  3493. 2:18:46Let's say 0.9.
  3494. 2:18:48Um
  3495. 2:18:49then when you multiply this value with
  3496. 2:18:510.9 and add this value will have a lot
  3497. 2:18:54stronger influence because now it's a
  3498. 2:18:56lot larger number than when you multiply
  3499. 2:18:58it with like 0.01.
  3500. 2:19:01So, basically, to repeat,
  3501. 2:19:04if you are predicting this token, then
  3502. 2:19:07you look at this token
  3503. 2:19:09and you add all of the values to the
  3504. 2:19:12value of this token. Now, remember, I
  3505. 2:19:13said we're not adding the
  3506. 2:19:16vector embedding of this token of these
  3507. 2:19:19tokens. We are adding their their
  3508. 2:19:21We are transforming that vector
  3509. 2:19:23embedding using a small neural network,
  3510. 2:19:25one layer neural network, into a value
  3511. 2:19:28vector.
  3512. 2:19:29And value vector is a lot smaller than
  3513. 2:19:32the big vector embedding.
  3514. 2:19:34So, we don't actually need
  3515. 2:19:37to have uh so much processing. We don't
  3516. 2:19:39need to work with the huge vectors. We
  3517. 2:19:41can just transform it to small vectors
  3518. 2:19:44so for faster processing and it's
  3519. 2:19:46sufficient and enough to do this.
  3520. 2:19:49So,
  3521. 2:19:50we take We just add values of each of
  3522. 2:19:53them to this last known token.
  3523. 2:19:55Add those vectors at the vectors, but
  3524. 2:19:58how much of the vectors we add
  3525. 2:20:00will be decided by the
  3526. 2:20:02query times key. So,
  3527. 2:20:04query or what I'm looking for, what I
  3528. 2:20:06give. If that number is low, then it
  3529. 2:20:08means like, "Okay, I don't really want
  3530. 2:20:10what you give." Or I just want a little
  3531. 2:20:12bit of what you give.
  3532. 2:20:15So, then the value will be multiplied.
  3533. 2:20:18The value vector will multiply by with
  3534. 2:20:20that small number, so you will just get
  3535. 2:20:21like a little bit of influence here when
  3536. 2:20:23you add that weighted value
  3537. 2:20:26to this value.
  3538. 2:20:28In the end,
  3539. 2:20:29all of the values will be added to this
  3540. 2:20:32value, and maybe this cat value will
  3541. 2:20:35have a high multiplication number.
  3542. 2:20:39High weight.
  3543. 2:20:40And so, it will employ like give a lot
  3544. 2:20:42of context, influence this value.
  3545. 2:20:45And then set can also give a lot of
  3546. 2:20:47context so we have large number as well.
  3547. 2:20:50So, this is the self-attention formula.
  3548. 2:20:53In this case, so this is where we start.
  3549. 2:20:55So,
  3550. 2:20:56query, this is a query vector of this
  3551. 2:20:59token, current token.
  3552. 2:21:01Actually, in this formula, this is meant
  3553. 2:21:03like for matrix of all of the queries of
  3554. 2:21:05all of tokens, but
  3555. 2:21:07not to complicate, let's just focus on a
  3556. 2:21:10single token. So, query
  3557. 2:21:12of the current token times
  3558. 2:21:15all of the keys of every previous token.
  3559. 2:21:18So, instead of calculating keys one by
  3560. 2:21:20one, so query times key, query times
  3561. 2:21:22key, query times key, we put all of this
  3562. 2:21:24into matrix.
  3563. 2:21:26And we calculate this vector
  3564. 2:21:28with this key matrix. And transpose is
  3565. 2:21:31just because
  3566. 2:21:33that's how you do matrix multiplication.
  3567. 2:21:36So, that's why it's like here it's it's
  3568. 2:21:37like to indicate it's a matrix
  3569. 2:21:39multiplication. Need to take care like
  3570. 2:21:41which dimensions, what's transpose,
  3571. 2:21:43what's not, which dimensions.
  3572. 2:21:45But
  3573. 2:21:46basically, you multiply so this going to
  3574. 2:21:48be your query, this going to be your
  3575. 2:21:49keys, for example. Or you can have query
  3576. 2:21:52like this and keys like this.
  3577. 2:21:54Uh it's just a matrix multiplication.
  3578. 2:21:57And then
  3579. 2:21:59this is dividing
  3580. 2:22:02>> [snorts]
  3581. 2:22:02>> because here as I said, you just have
  3582. 2:22:05one query. So, the result will just be
  3583. 2:22:07also one vector.
  3584. 2:22:11And
  3585. 2:22:12you want to kind of normalize that
  3586. 2:22:14vector. If it has some large numbers,
  3587. 2:22:16neural networks don't like large
  3588. 2:22:18numbers.
  3589. 2:22:19Uh it's [snorts] a bit tough to explain,
  3590. 2:22:22but
  3591. 2:22:23mere multiplication of large numbers,
  3592. 2:22:27it behaves differently and it can
  3593. 2:22:29influence
  3594. 2:22:30so
  3595. 2:22:32not to overcomplicate, it will explain
  3596. 2:22:34that later.
  3597. 2:22:35Uh neural network learns better if the
  3598. 2:22:37numbers are small, between zero and one
  3599. 2:22:39or a bit maybe a bit bigger than one,
  3600. 2:22:42but not too much bigger.
  3601. 2:22:45So, that's why we just multiply by the
  3602. 2:22:47square root
  3603. 2:22:49of the number of dimensions of these
  3604. 2:22:52key or query. So, for example, if
  3605. 2:22:54queries has 500 dimensions, it's a
  3606. 2:22:57vector 500 numbers long, you you divide
  3607. 2:22:59by the square root of 500. And
  3608. 2:23:02key and query will have same
  3609. 2:23:04dimension.
  3610. 2:23:08Then comes this softmax. So, remember,
  3611. 2:23:11this is result of this is just an array
  3612. 2:23:15and it has some numbers.
  3613. 2:23:18So, maybe biggest number will be here.
  3614. 2:23:20It just dot products. So, they don't yet
  3615. 2:23:23add up to one or to 100%. They are just
  3616. 2:23:26like big number here, a bit smaller
  3617. 2:23:28number here, smaller. So, you need to
  3618. 2:23:30make them add up to one or 100%. That's
  3619. 2:23:33why you do softmax. You pass them
  3620. 2:23:34through softmax so they all add up to
  3621. 2:23:37one or 200%.
  3622. 2:23:39While retaining their relative position.
  3623. 2:23:42So, this
  3624. 2:23:43guy that had the biggest dot product
  3625. 2:23:45will still be the highest. Will still
  3626. 2:23:47get the highest percentage.
  3627. 2:23:49Let's see this on an example. So, these
  3628. 2:23:52could be our values. So, uh this is a
  3629. 2:23:56value vector for the
  3630. 2:23:58cat set. And these are our weights.
  3631. 2:24:02So, it means like to generate the next
  3632. 2:24:06uh word,
  3633. 2:24:08this is 10% important, uh 30% important,
  3634. 2:24:12and 60% important.
  3635. 2:24:16And so, we multiply 0.1 times this first
  3636. 2:24:21and second
  3637. 2:24:2230% times this
  3638. 2:24:25and this times this, and then we add up
  3639. 2:24:28all of the vectors.
  3640. 2:24:30We add them up so we so we add first
  3641. 2:24:34dimension here
  3642. 2:24:35after the multiplication. So, first
  3643. 2:24:37dimension, second dimension. So, we add
  3644. 2:24:39element-wise vectors. We get just one
  3645. 2:24:42vector, so four values.
  3646. 2:24:45It's called like a weighted value
  3647. 2:24:47vector.
  3648. 2:24:49This is the example. So, multiply each
  3649. 2:24:52vector.
  3650. 2:24:53So, you get these numbers.
  3651. 2:24:56And then you add
  3652. 2:24:58uh this guy goes here. This guy goes
  3653. 2:25:02here. This guy So, you add all of the
  3654. 2:25:04third elements into this, all of the
  3655. 2:25:06fourth elements into this.
  3656. 2:25:08So, this is the weighted value vector
  3657. 2:25:10that you get in the end.
  3658. 2:25:12And now, this output will be kind of
  3659. 2:25:14dominated by this
  3660. 2:25:16uh third vector because you see this
  3661. 2:25:18these values are very small because our
  3662. 2:25:21weight is 0.1.
  3663. 2:25:24And this these values are going to be
  3664. 2:25:26biggest because our weight is 0.6. So,
  3665. 2:25:30the output will
  3666. 2:25:31uh don't be dominated by this
  3667. 2:25:34because it has large weight.
  3668. 2:25:38Here, I explained uh query key value
  3669. 2:25:40again.
  3670. 2:25:41So, um
  3671. 2:25:44example. So, query is what I'm looking
  3672. 2:25:46for.
  3673. 2:25:49If processing the word set, the query
  3674. 2:25:51might encode, "I need to find the
  3675. 2:25:54subject of this action."
  3676. 2:25:56So, then it will fire when it sees a
  3677. 2:25:58cat, it will fire. So, when you have
  3678. 2:26:01a query of the word set and it sees word
  3679. 2:26:04cat, it will have high
  3680. 2:26:07dot product between the query of the
  3681. 2:26:10word set
  3682. 2:26:11and key of the word cat because
  3683. 2:26:14this query learned to adjust its weights
  3684. 2:26:17to look for nouns or subjects.
  3685. 2:26:21The key,
  3686. 2:26:23for example, of the word cat might say,
  3687. 2:26:25"I am a noun, an animal, a potential
  3688. 2:26:28subject."
  3689. 2:26:30So,
  3690. 2:26:32uh as I said, it's going to have this
  3691. 2:26:33key vector. And key vector will just
  3692. 2:26:36have a high dot product with this query
  3693. 2:26:38vector.
  3694. 2:26:39That's how neural network just learns
  3695. 2:26:41those weights to be to take to get a
  3696. 2:26:43high product
  3697. 2:26:45between set and cat.
  3698. 2:26:48And value is the actual vector that gets
  3699. 2:26:51added up, the actual information it
  3700. 2:26:53gives. So, it's different from
  3701. 2:26:55describing what it has. The key The key
  3702. 2:26:58describes what value has.
  3703. 2:27:02Or what value will give.
  3704. 2:27:05So, if you go down in my school, uh here
  3705. 2:27:08we have learn to code and build LLMs
  3706. 2:27:10from scratch, you can check a few of
  3707. 2:27:13these. Uh I'm ac- actually explaining
  3708. 2:27:16all of this over and over again in all
  3709. 2:27:17of these.
  3710. 2:27:19So, it will just require like a lot of
  3711. 2:27:21practice, explanations. I'm going to add
  3712. 2:27:23more
  3713. 2:27:24exercises videos.
  3714. 2:27:26So, that's going to be it for this video
  3715. 2:27:28and uh see you in the next lesson.
  3716. 2:27:30Welcome to self-attention lesson.
  3717. 2:27:33Here, we will focus more on code so you
  3718. 2:27:35can check the first lesson, attention
  3719. 2:27:37mechanism explained.
  3720. 2:27:39And here, it's like code implementation.
  3721. 2:27:41So,
  3722. 2:27:42uh key, query, and value, which I
  3723. 2:27:45explained, are all linear layers. So, I
  3724. 2:27:48I mean,
  3725. 2:27:49they are created with linear layers.
  3726. 2:27:52You pass in embedding dimension, which
  3727. 2:27:53is the vector dimension, the model LM
  3728. 2:27:57dimension.
  3729. 2:27:59And
  3730. 2:28:00in this case, we will have the same
  3731. 2:28:02dimension for the
  3732. 2:28:03query and key and value, okay?
  3733. 2:28:05Sometimes, you can have smaller
  3734. 2:28:08dimensions here.
  3735. 2:28:09But So, this will just go from embedding
  3736. 2:28:12dimension to embedding, but it will
  3737. 2:28:14transform. So, it will have some weights
  3738. 2:28:16and transform this vector embedding into
  3739. 2:28:19its query.
  3740. 2:28:20So, this query will say what information
  3741. 2:28:24is this token looking for. I explained
  3742. 2:28:26this in the last video.
  3743. 2:28:28Key is describing the information and
  3744. 2:28:31value is the actual information. So, key
  3745. 2:28:33is describing what value is.
  3746. 2:28:36And all of them are
  3747. 2:28:39uh linear layers that generate this key,
  3748. 2:28:43query, and value and same input.
  3749. 2:28:45If we scroll down a bit, we can see a
  3750. 2:28:48classic example in a large language
  3751. 2:28:50model where you have
  3752. 2:28:52sequence length, which are just like
  3753. 2:28:55your conversation, your words.
  3754. 2:28:58So, your context window when you talk to
  3755. 2:29:00chat GPT, okay?
  3756. 2:29:01And batch means there is like a multiple
  3757. 2:29:03context windows, multiple independent
  3758. 2:29:05conversations. Okay? And every token
  3759. 2:29:10in that sequence is represented with a
  3760. 2:29:12vector.
  3761. 2:29:14So, that's how we have
  3762. 2:29:15vector or this is token.
  3763. 2:29:18Sequence length and batch,
  3764. 2:29:20which can be this.
  3765. 2:29:2232
  3766. 2:29:23um
  3767. 2:29:24conversations.
  3768. 2:29:28I changed it to conversations.
  3769. 2:29:31Each conversation has 100 tokens and 512
  3770. 2:29:35dimensions, for example, for each token.
  3771. 2:29:38So, we pass
  3772. 2:29:40X
  3773. 2:29:41all of this this entire thing.
  3774. 2:29:44We generate the queries for every token,
  3775. 2:29:46okay? So, there is 32 * 100 tokens.
  3776. 2:29:50Generate query for each,
  3777. 2:29:52key for each, value for each. That's how
  3778. 2:29:54we code it.
  3779. 2:29:56And so, we have
  3780. 2:29:57>> [clears throat]
  3781. 2:29:58>> query matrix all queries key value.
  3782. 2:30:01So, the scores are calculated with
  3783. 2:30:03queries at queries
  3784. 2:30:06matrix multiply
  3785. 2:30:08key transpose where we transpose the
  3786. 2:30:09last and second to last because
  3787. 2:30:12that's just how matrix multiplication
  3788. 2:30:14works. This is This is for the reasons
  3789. 2:30:16of matrix multiplication. You need to
  3790. 2:30:18have this transposed and that's it it.
  3791. 2:30:21So, the shape will be batch size
  3792. 2:30:24sequence length sequence length. So,
  3793. 2:30:26So, here
  3794. 2:30:28every token
  3795. 2:30:30will have score with every other token.
  3796. 2:30:34So, there will be score between every
  3797. 2:30:36single pair of tokens. So, it's like a
  3798. 2:30:38square.
  3799. 2:30:40That's why they say attention grows
  3800. 2:30:42quadratically. For every token, you need
  3801. 2:30:44to add all of the previous tokens to the
  3802. 2:30:46score list.
  3803. 2:30:49So, the scores again you can imagine a
  3804. 2:30:51similarity between these two tokens or I
  3805. 2:30:55should say affinity or how much this
  3806. 2:30:58token cares for this other token, how
  3807. 2:31:00much of its information it wants to take
  3808. 2:31:02its context.
  3809. 2:31:05And then we will just scale them back
  3810. 2:31:07for stability reasons. So, this this
  3811. 2:31:11this means like square root.
  3812. 2:31:14So, this is the square root of the
  3813. 2:31:16embedding dimension of the
  3814. 2:31:18of the dimension of the queries or or
  3815. 2:31:20keys.
  3816. 2:31:22And then we apply softmax to scores to
  3817. 2:31:25put them between like add up to 100%.
  3818. 2:31:28So, we have like percentages.
  3819. 2:31:31And then
  3820. 2:31:32those are weights. So, now
  3821. 2:31:36for every token
  3822. 2:31:39I explained this in the previous lesson,
  3823. 2:31:41but here
  3824. 2:31:43we want to
  3825. 2:31:45for every token, we take its value and
  3826. 2:31:47add all of the
  3827. 2:31:49values of other tokens, but weighted.
  3828. 2:31:53So,
  3829. 2:31:54if for this token like this other token
  3830. 2:31:56is very important, then
  3831. 2:31:58this attention weights will actually
  3832. 2:32:00multiply its value with a high number
  3833. 2:32:02and then we add just like add
  3834. 2:32:05element-wise this vector.
  3835. 2:32:07But if some
  3836. 2:32:08uh value is not so important, it will
  3837. 2:32:11get weighted by a smaller multiply by a
  3838. 2:32:13smaller number, smaller attention
  3839. 2:32:15weight.
  3840. 2:32:17And now this this
  3841. 2:32:18uh condensed or squished uh numbers that
  3842. 2:32:22are around maybe zero will all get added
  3843. 2:32:24to this value.
  3844. 2:32:26So, it will not influence too much this
  3845. 2:32:28value that it's added to. So, it didn't
  3846. 2:32:30take too much context from this token.
  3847. 2:32:34I explained this in previous lesson you
  3848. 2:32:35can check also.
  3849. 2:32:37So, then you would be able to just
  3850. 2:32:39define self-attention with the
  3851. 2:32:42dimension you want for keys, queries,
  3852. 2:32:44values.
  3853. 2:32:45And then you would
  3854. 2:32:47just pass in some X through the
  3855. 2:32:49attention
  3856. 2:32:51and you should get the output
  3857. 2:32:52immediately,
  3858. 2:32:54which is a list of values for each token
  3859. 2:32:57that's just stuck with the
  3860. 2:32:59context
  3861. 2:33:01from other tokens.
  3862. 2:33:03So, here we don't have causal
  3863. 2:33:05self-attention. So, here
  3864. 2:33:08every token is taking
  3865. 2:33:10attention from all of the previous and
  3866. 2:33:12forward tokens.
  3867. 2:33:13But when we're training neural network,
  3868. 2:33:15we shouldn't be able to see forward
  3869. 2:33:16tokens because then because then that
  3870. 2:33:18would be easy to predict forward tokens
  3871. 2:33:20if we see them.
  3872. 2:33:22So, but it's not implemented here, but
  3873. 2:33:23I'm telling you we will learn this. So,
  3874. 2:33:25we will actually just for each value we
  3875. 2:33:26will
  3876. 2:33:28uh take context or do multiplications
  3877. 2:33:31with just previous tokens.
  3878. 2:33:35So, that's going to be a short lesson in
  3879. 2:33:36implementing attention in code.
  3880. 2:33:39Let's learn to build and code a
  3881. 2:33:41GPT-style LLM. So, this is like chat
  3882. 2:33:43GPT.
  3883. 2:33:45I recommend you watch these two previous
  3884. 2:33:46lessons on attention mechanism explained
  3885. 2:33:49and self-attention from scratch.
  3886. 2:33:51So, if you've seen transformer
  3887. 2:33:52architecture, there are two parts and
  3888. 2:33:54this is good for sequence to sequence.
  3889. 2:33:57When you have encoder and decoder. So,
  3890. 2:33:59classic transformer architecture is good
  3891. 2:34:02when you have like some sequence of
  3892. 2:34:04words in one language, you want to
  3893. 2:34:06translate it into different language,
  3894. 2:34:08but
  3895. 2:34:10GPT has only this part, only decoder.
  3896. 2:34:13And that's good when you just want to
  3897. 2:34:14keep generating text.
  3898. 2:34:18And as it turns out, maybe this
  3899. 2:34:20decoder-only transformer is also better
  3900. 2:34:22for translation and sequence to
  3901. 2:34:24sequence. So, that's probably also
  3902. 2:34:27it works better in practice even though
  3903. 2:34:28the idea here is cool to
  3904. 2:34:31have like two parts for two
  3905. 2:34:33languages.
  3906. 2:34:35But anyways, in our GPT architecture, we
  3907. 2:34:37just have decoder and we'll explain more
  3908. 2:34:40what that is.
  3909. 2:34:42We know that large language model will
  3910. 2:34:44predict the next word.
  3911. 2:34:46So, if we have once upon a time
  3912. 2:34:49very far away there, it's going to
  3913. 2:34:52predict lived as the next likely word.
  3914. 2:34:56So, the four parts of transformer, we
  3915. 2:34:59have causal masked multi-head
  3916. 2:35:02self-attention,
  3917. 2:35:04which is actually explained here in
  3918. 2:35:06self-attention. I will explain what
  3919. 2:35:08multiple heads mean.
  3920. 2:35:11And then add a normalization. So,
  3921. 2:35:15when you have input, you pass input
  3922. 2:35:17token or tokens through self-attention.
  3923. 2:35:21And after that's processed,
  3924. 2:35:23you actually want to add same input
  3925. 2:35:25and add this processed
  3926. 2:35:28tokens with self-attention and add to
  3927. 2:35:30the input that's not processed. So, add
  3928. 2:35:32them together.
  3929. 2:35:33And when adding vectors,
  3930. 2:35:36uh then neural network will actually you
  3931. 2:35:38give both information, both processed
  3932. 2:35:40information and raw information for the
  3933. 2:35:42neural network.
  3934. 2:35:43So,
  3935. 2:35:45it has both both of the information and
  3936. 2:35:47then it can easily use both.
  3937. 2:35:49If you remove this residual connection
  3938. 2:35:52that just adds unprocessed information,
  3939. 2:35:55then neural network or language model
  3940. 2:35:57will learn worse and slower because
  3941. 2:36:01this initial tokens also contain
  3942. 2:36:03important information. So, we we don't
  3943. 2:36:05just want to process and just have that,
  3944. 2:36:07but also the initial. So, you want to
  3945. 2:36:09add those vectors.
  3946. 2:36:12And this normalization this can happen
  3947. 2:36:16usually before self-attention
  3948. 2:36:18and it's just to keep the vectors, the
  3949. 2:36:21numbers in a nice range between around
  3950. 2:36:24zero and one or minus one
  3951. 2:36:26because big numbers or very small
  3952. 2:36:27numbers can cause problems like gradient
  3953. 2:36:31vanishing gradient exploding.
  3954. 2:36:33Because when you multiply numbers near
  3955. 2:36:35zero near sorry near one, they're going
  3956. 2:36:37to stay result is also going to stay
  3957. 2:36:39near one.
  3958. 2:36:40But if you multiply numbers like
  3959. 2:36:42thousands and millions,
  3960. 2:36:43the other numbers next numbers results
  3961. 2:36:46will explode. And maybe
  3962. 2:36:48such big numbers cannot be represented
  3963. 2:36:50in computers.
  3964. 2:36:52So, we want to keep our numbers around
  3965. 2:36:54one zero.
  3966. 2:36:57Feed forward network, it's just a single
  3967. 2:36:59multi-layer perceptron.
  3968. 2:37:01So, it takes as input this processed and
  3969. 2:37:04normalized. So, usually you would also
  3970. 2:37:06put normalization before feed forward
  3971. 2:37:08and this mask before feed forward.
  3972. 2:37:11In the original transformer, it was
  3973. 2:37:13after, but now we would put like before.
  3974. 2:37:17So,
  3975. 2:37:19uh
  3976. 2:37:20at the output of self-attention and the
  3977. 2:37:24residual network, when we add those two,
  3978. 2:37:26so this add and norm.
  3979. 2:37:29So, I guess norm would go before, but
  3980. 2:37:31add would go after.
  3981. 2:37:36So,
  3982. 2:37:37uh with that added vector result, we can
  3983. 2:37:40normalize that again
  3984. 2:37:42with this norm that would that we would
  3985. 2:37:44put put
  3986. 2:37:46before feed forward.
  3987. 2:37:49And then put that into feed forward. So,
  3988. 2:37:50that's input. Now, we expand into the
  3989. 2:37:53middle layer, hidden layer. It's going
  3990. 2:37:55to be like four times bigger usually
  3991. 2:37:59uh than this input. And so, in this
  3992. 2:38:02middle layer of our feed forward
  3993. 2:38:03network,
  3994. 2:38:05uh there there are a lot of facts like
  3995. 2:38:08about basketball players, like all of
  3996. 2:38:10the knowledge that neural network
  3997. 2:38:11learned.
  3998. 2:38:13It's like a data storage. And also it
  3999. 2:38:16also transforms
  4000. 2:38:19uh
  4001. 2:38:20this these tokens
  4002. 2:38:22in different ways. So, it has multiple
  4003. 2:38:25purposes.
  4004. 2:38:27And then in the hidden layer that's four
  4005. 2:38:28times bigger than the token size in the
  4006. 2:38:30input, it shrinks back to the token size
  4007. 2:38:34at the output.
  4008. 2:38:36And that's our one pass through the
  4009. 2:38:38transformer. Now, that can go back into
  4010. 2:38:40attention into the next pass, next layer
  4011. 2:38:44back again. And then we can repeat this
  4012. 2:38:46cycle 10 20 30 maybe 60 times in big uh
  4013. 2:38:50neural big large language models.
  4014. 2:38:52So, we'll explain all of this again,
  4015. 2:38:54don't worry.
  4016. 2:38:56So, these would be example hyper
  4017. 2:38:59parameters here.
  4018. 2:39:01So, uh number of heads in
  4019. 2:39:05uh self attention. So,
  4020. 2:39:08we will just divide each token vector in
  4021. 2:39:11self attention into different parts,
  4022. 2:39:13independent parts.
  4023. 2:39:14And so, we will then do attention on
  4024. 2:39:17those independent parts, which are like
  4025. 2:39:19independent key query value, independent
  4026. 2:39:21key query value, all coming from the
  4027. 2:39:23same token.
  4028. 2:39:25But we will divide again into multiple
  4029. 2:39:27because now each head will learn
  4030. 2:39:29different things. Maybe some head will
  4031. 2:39:31learn to like do mathematics, some head
  4032. 2:39:34will learn about medicine, etc.
  4033. 2:39:38Uh
  4034. 2:39:39This is the dimension of the feed
  4035. 2:39:40forward hidden layer that I was
  4036. 2:39:42explaining that was four times bigger,
  4037. 2:39:45and here it is four times bigger because
  4038. 2:39:47D model is the token embedding size, and
  4039. 2:39:52feed forward hidden dimension is four
  4040. 2:39:54times bigger.
  4041. 2:39:56Dropout
  4042. 2:39:57just to prevent overfitting. Vocabulary
  4043. 2:40:00size, usually this is like 50,000 to
  4044. 2:40:02200,000 on big models.
  4045. 2:40:05And this is the maximum context window
  4046. 2:40:06sequence, like maximum of 512 tokens
  4047. 2:40:09that you can have in your
  4048. 2:40:11language model.
  4049. 2:40:13And number of blocks can also be called
  4050. 2:40:16number of layers. So, how many times do
  4051. 2:40:19we repeat this process? So, self
  4052. 2:40:22attention norms feed forward norm. So,
  4053. 2:40:24that's one, okay. Then we go output
  4054. 2:40:27here, we go back in.
  4055. 2:40:29And then back in again, so three times
  4056. 2:40:31in this case, three blocks or three
  4057. 2:40:32layers.
  4058. 2:40:34And these earlier layers
  4059. 2:40:37will learn something.
  4060. 2:40:40Maybe to find out some basic features in
  4061. 2:40:43text, and then later layers will learn
  4062. 2:40:45maybe more complex things to generate
  4063. 2:40:47next tokens or to give information
  4064. 2:40:50information necessary to generate next
  4065. 2:40:52tokens.
  4066. 2:40:54Then we have rotary positional
  4067. 2:40:56embeddings. This is how we encode
  4068. 2:40:59which token comes after which token.
  4069. 2:41:02This is the order of tokens.
  4070. 2:41:04So, is cat chasing the dog or dog
  4071. 2:41:06chasing the cat?
  4072. 2:41:08We use rope to encode the position of
  4073. 2:41:10the words, of the tokens.
  4074. 2:41:12And
  4075. 2:41:14uh let's go all the way down. This is
  4076. 2:41:16where it happens.
  4077. 2:41:17So, we have our
  4078. 2:41:20um token
  4079. 2:41:22our vector embedding for the token, and
  4080. 2:41:25we split it into two.
  4081. 2:41:29And
  4082. 2:41:30the first So, we have
  4083. 2:41:32X1 is the first half of the token, X2 is
  4084. 2:41:34the second half of the token.
  4085. 2:41:36And then we apply this rotation matrix.
  4086. 2:41:39So, rotation matrix is like a term in
  4087. 2:41:42mathematics.
  4088. 2:41:43When you have a vector,
  4089. 2:41:45you can rotate the vector by multiplying
  4090. 2:41:48it with this rotation matrix.
  4091. 2:41:52So, the idea here is that
  4092. 2:41:56um
  4093. 2:41:58it's a bit more complex, but I'll
  4094. 2:41:59explain. You want to rotate closer
  4095. 2:42:02tokens or early tokens in the sentence
  4096. 2:42:06maybe less,
  4097. 2:42:08and later tokens in the sentence more.
  4098. 2:42:11So, the later the token, the more you
  4099. 2:42:12want to rotate its vector.
  4100. 2:42:15That's how by this rotation
  4101. 2:42:18neural network will learn
  4102. 2:42:20like the order of the tokens, how much
  4103. 2:42:23it will just see how much
  4104. 2:42:25uh every token is rotated to understand
  4105. 2:42:27their orders.
  4106. 2:42:28And it's also good because
  4107. 2:42:31it can also compare relatively tokens
  4108. 2:42:34So, third token and fifth token, it can
  4109. 2:42:37see that there is like two tokens in
  4110. 2:42:39between
  4111. 2:42:41uh relative to each other.
  4112. 2:42:43So, we don't need absolute positions
  4113. 2:42:45like
  4114. 2:42:46uh this is absolutely what first,
  4115. 2:42:48second, third. There is also a relative
  4116. 2:42:50besides the absolute.
  4117. 2:42:53So, this rotation matrix, the matrix
  4118. 2:42:55that rotates the tokens, can be
  4119. 2:42:57expressed with uh sine and cosine.
  4120. 2:43:00And this is how we can This is one way
  4121. 2:43:01to do it.
  4122. 2:43:02So, we multiply first half
  4123. 2:43:05with cosine minus second half with sine,
  4124. 2:43:09that's going to be our rotated X1. This
  4125. 2:43:11is our first half now.
  4126. 2:43:13And the second half
  4127. 2:43:15uh to get the new second half, we're
  4128. 2:43:16going to rotate the previous the
  4129. 2:43:18original first half with sine plus
  4130. 2:43:21second half with cosine, so that's going
  4131. 2:43:23to be our new second half.
  4132. 2:43:25And then we concatenate the first and
  4133. 2:43:27second half.
  4134. 2:43:29And that's our new vector that now token
  4135. 2:43:32embedding that now encodes
  4136. 2:43:34positional information.
  4137. 2:43:38And this code above is just how we
  4138. 2:43:40generate uh this sine and cosine, and
  4139. 2:43:43how we decide
  4140. 2:43:45how much
  4141. 2:43:47for each position we should rotate, what
  4142. 2:43:50the sine and cosine should be for each
  4143. 2:43:51position.
  4144. 2:43:52But this is a bit complex, so we'll
  4145. 2:43:54leave this for another time. This is not
  4146. 2:43:57so important now.
  4147. 2:43:59This is a very good example here of the
  4148. 2:44:02code. So,
  4149. 2:44:03uh this is in Andrej Karpathy's nanoGPT.
  4150. 2:44:07And
  4151. 2:44:08apply rotary embeddings, we just get the
  4152. 2:44:10X, our token embedding cosine and sine.
  4153. 2:44:15And
  4154. 2:44:17uh we will get we will split
  4155. 2:44:21into X1 and X2 all of the X's. So, X is
  4156. 2:44:24like not just in this case, this is not
  4157. 2:44:26just one vector, it's all of the
  4158. 2:44:27vectors.
  4159. 2:44:29So, we will split every vector in
  4160. 2:44:31halves, and then
  4161. 2:44:33we will apply the same rotation that I
  4162. 2:44:35just explained,
  4163. 2:44:37and then concatenate.
  4164. 2:44:39Uh this is the dimension because
  4165. 2:44:42uh now we don't just have one vector, we
  4166. 2:44:44have like a whole batch of sequences of
  4167. 2:44:47vectors.
  4168. 2:44:49But the logic is same as I explained.
  4169. 2:44:53Then uh let's look at some code in
  4170. 2:44:55implementing causal self attention with
  4171. 2:44:57rope.
  4172. 2:44:58Uh we have a class causal self attention
  4173. 2:45:01and that inherits from NN.Module.
  4174. 2:45:04And we need to make sure that
  4175. 2:45:08the token embedding is divided into
  4176. 2:45:11number of heads
  4177. 2:45:12uh without like it should be divisible
  4178. 2:45:15by number of heads because every head
  4179. 2:45:17should have equal number of uh numbers
  4180. 2:45:21dimensions.
  4181. 2:45:23And then head dimension will be just
  4182. 2:45:24dividing the token dimension with number
  4183. 2:45:27of heads, that's
  4184. 2:45:28how many numbers or elements each head
  4185. 2:45:30has.
  4186. 2:45:33And then we have uh query key value.
  4187. 2:45:37So, in this case, we do
  4188. 2:45:39all the query key value in one go.
  4189. 2:45:43So, we go from D model to D model times
  4190. 2:45:46three because D model is the token
  4191. 2:45:49dimension.
  4192. 2:45:51And
  4193. 2:45:53our query key value will also be the
  4194. 2:45:56token dimension, same size.
  4195. 2:46:00And you can learn more about query key
  4196. 2:46:02value in the previous video, previous
  4197. 2:46:04lessons about self attention.
  4198. 2:46:07So, we want to generate three of them.
  4199. 2:46:08We want to generate three uh query key
  4200. 2:46:10value, and each is
  4201. 2:46:13uh each has size of the token, so we
  4202. 2:46:15need three times size the token.
  4203. 2:46:18So, we go from we go we have a linear
  4204. 2:46:20layer, it's like some learned network
  4205. 2:46:22weights that's going to generate our key
  4206. 2:46:25query and value.
  4207. 2:46:26And we input the token,
  4208. 2:46:28and we output three times the token
  4209. 2:46:30size, which is going to be now different
  4210. 2:46:32numbers.
  4211. 2:46:36And so, later we will just split this
  4212. 2:46:38into three. Like take the first third as
  4213. 2:46:41uh query, second third as key, uh last
  4214. 2:46:44third as value.
  4215. 2:46:46We also have output head.
  4216. 2:46:48So, after we combine all of the tokens,
  4217. 2:46:51so after we do a attention with tokens,
  4218. 2:46:54uh this will just convert because
  4219. 2:46:57at the end of attention, we just added
  4220. 2:46:59values from each attention, each token
  4221. 2:47:02to the last one, we just added values.
  4222. 2:47:05Now we need to convert from this
  4223. 2:47:06mixed-up value vector to the actual
  4224. 2:47:10token embedding
  4225. 2:47:11to exit attention mechanism.
  4226. 2:47:15And keep in mind that in this case,
  4227. 2:47:17value key query value are same dimension
  4228. 2:47:19as token, but it doesn't have to be.
  4229. 2:47:21Actually, in big LLMs,
  4230. 2:47:24key query value are smaller dimension.
  4231. 2:47:27So, at the end of attention mechanism
  4232. 2:47:29where we added all of the weighted
  4233. 2:47:31values, we need to convert that mixed-up
  4234. 2:47:34value vector into the token embedding
  4235. 2:47:36vector.
  4236. 2:47:38And do that for every token if we are
  4237. 2:47:40training, or just for one token if we
  4238. 2:47:42are inferencing. But we'll talk about
  4239. 2:47:44that more later.
  4240. 2:47:46And we initialize rope here as well,
  4241. 2:47:49passing self head dimension. In this
  4242. 2:47:51case, this is how we defined it, so we
  4243. 2:47:53need to pass in
  4244. 2:47:54uh dimension of the head.
  4245. 2:47:57And then
  4246. 2:47:58uh in this case, you want to get
  4247. 2:48:00uh the batch size, how many independent
  4248. 2:48:02conversations, sequence length length,
  4249. 2:48:05what is the maximum uh number of tokens
  4250. 2:48:08in the sequence.
  4251. 2:48:09And I guess we don't need the token
  4252. 2:48:11dimension here.
  4253. 2:48:13And so, we generate
  4254. 2:48:14key query values, so by just
  4255. 2:48:18passing X through our linear layer that
  4256. 2:48:20generates key query values.
  4257. 2:48:22And then split. So, this will get us
  4258. 2:48:25like one big vector, three times
  4259. 2:48:29uh
  4260. 2:48:30model dimension.
  4261. 2:48:31And then we split it into three parts
  4262. 2:48:33for query key value.
  4263. 2:48:38And then we need to split query, key,
  4264. 2:48:40and value into heads.
  4265. 2:48:42As I said, we will split query, key,
  4266. 2:48:45value into heads, so each head will
  4267. 2:48:46learn different things. Like maybe one
  4268. 2:48:48will be about math or calculation or
  4269. 2:48:50nouns or verbs. The other will do
  4270. 2:48:52something else.
  4271. 2:48:54So,
  4272. 2:48:57uh we will use query.view batch size
  4273. 2:49:00sequence length that already exists.
  4274. 2:49:03But,
  4275. 2:49:04that
  4276. 2:49:05where previously was one dimension here.
  4277. 2:49:07This was model dimension. Now we split
  4278. 2:49:09into two dimensions.
  4279. 2:49:11So, number of heads and head dimension.
  4280. 2:49:14Number of heads and head dimension for
  4281. 2:49:16each head.
  4282. 2:49:19So, now we do that for all of these.
  4283. 2:49:22And we can apply rope
  4284. 2:49:25then to query and key. We don't need to
  4285. 2:49:27apply rope for the value. Just query and
  4286. 2:49:30key.
  4287. 2:49:32So, next in attention calculation,
  4288. 2:49:35we want to do heads independently.
  4289. 2:49:38So, we will transpose this to put heads
  4290. 2:49:41first. This is the batch size and heads
  4291. 2:49:43are independent dimensions.
  4292. 2:49:46And then
  4293. 2:49:48in every sequence length, we will do
  4294. 2:49:51heads of the key, query, value. So,
  4295. 2:49:54sequence length is
  4296. 2:49:56For the sequence length, we will do
  4297. 2:49:58same head. So, this is separate. Here we
  4298. 2:50:01will separate them. Here we will we will
  4299. 2:50:02also separate them here.
  4300. 2:50:04But, now here and here they're together.
  4301. 2:50:05They're grouped up by by head.
  4302. 2:50:09And so, we do whole sequence length
  4303. 2:50:12of the query, key, value together
  4304. 2:50:15for this particular head and for the
  4305. 2:50:17next head, etc.
  4306. 2:50:20And then this is the formula for
  4307. 2:50:21attention.
  4308. 2:50:23And we talked about this formula in the
  4309. 2:50:25previous lessons. This is how you do it.
  4310. 2:50:29And then we want to apply mask so that
  4311. 2:50:33every token in a sequence can only look
  4312. 2:50:36at the previous tokens
  4313. 2:50:38and not the future tokens.
  4314. 2:50:41So, it can only pay attention to that
  4315. 2:50:42this is during the training, by the way.
  4316. 2:50:45So, in inference, we don't need masks
  4317. 2:50:46because we just have a sequence. We just
  4318. 2:50:49do the next token once. We don't like
  4319. 2:50:52need to calculate every every token with
  4320. 2:50:55every other previous token in inference
  4321. 2:50:58when you are generating, okay? But, when
  4322. 2:50:59we are training, we do need to calculate
  4323. 2:51:01every token with every previous token
  4324. 2:51:04to train better.
  4325. 2:51:06To train on the whole thing.
  4326. 2:51:08So, this mask again will just make sure
  4327. 2:51:11that tokens cannot look at so, query of
  4328. 2:51:14the token cannot look at the keys of the
  4329. 2:51:16following tokens because
  4330. 2:51:18it's learning to predict those.
  4331. 2:51:19So, query can only look at the keys of
  4332. 2:51:21the previous tokens
  4333. 2:51:23for every token.
  4334. 2:51:26And then we calculate softmax, which
  4335. 2:51:30previously these scores are just
  4336. 2:51:32numbers, logits. They don't add up to
  4337. 2:51:35one. They don't add up to 100%. So, they
  4338. 2:51:38can be like two, one, 0.5, whatever. And
  4339. 2:51:42we need to make them add add up to 100%
  4340. 2:51:45because
  4341. 2:51:46let's take example this this token.
  4342. 2:51:49It needs to know how much to take of the
  4343. 2:51:52information of context of every other
  4344. 2:51:54previous token.
  4345. 2:51:56So, it's a lot easier to do that if you
  4346. 2:51:58just have them add up to 100%. So, it
  4347. 2:52:01knows I should take 20% from this token
  4348. 2:52:05in from 20% of its value,
  4349. 2:52:07uh 10% of this value, 5% of this value.
  4350. 2:52:10So, that's what softmax will do. It will
  4351. 2:52:12just make the scores
  4352. 2:52:15for every So, for every token, all of
  4353. 2:52:17the scores
  4354. 2:52:18with every other token add up to one,
  4355. 2:52:21200%. So, it knows how much.
  4356. 2:52:24And then we can drop out for like ignore
  4357. 2:52:26some
  4358. 2:52:28of the scores
  4359. 2:52:30to stabilize training
  4360. 2:52:32to
  4361. 2:52:33make it not so dependent on some
  4362. 2:52:35particular score scores or some
  4363. 2:52:37particular positions.
  4364. 2:52:39So, this part outputs is where we
  4365. 2:52:41perform the weighted sum of the values.
  4366. 2:52:44So, now this is where it takes context
  4367. 2:52:46from every previous token
  4368. 2:52:48based on the score, the importance.
  4369. 2:52:53Uh I explained here
  4370. 2:52:55V is the matrix of values for every
  4371. 2:52:58token in the sequence.
  4372. 2:52:59For example, V of the the, V of the cat,
  4373. 2:53:03V of the set.
  4374. 2:53:06So, when we multiply
  4375. 2:53:08uh attention scores and values,
  4376. 2:53:12this is what it looks like. For example,
  4377. 2:53:14output for the word set,
  4378. 2:53:17it's going to do 0.1 or 10% times value
  4379. 2:53:20of the plus 0.6
  4380. 2:53:24times value of cat plus
  4381. 2:53:260.3 times value of set. And usually you
  4382. 2:53:29will see that
  4383. 2:53:30these tokens usually take most of their
  4384. 2:53:33own value. So, maybe I should put 0.6
  4385. 2:53:36here
  4386. 2:53:38and 0.6 there.
  4387. 2:53:42So, that's how we will get weighted sum
  4388. 2:53:45of the values for
  4389. 2:53:47for each token
  4390. 2:53:49of the every previous token into that
  4391. 2:53:51token. So, that's how we do the context.
  4392. 2:53:56And then after we do that, we need to
  4393. 2:53:58now go back with the swapping heads and
  4394. 2:54:01sequence length. So, now we need to put
  4395. 2:54:03sequence left length after batch.
  4396. 2:54:06And so, we have sequence length and then
  4397. 2:54:10we will combine head number of heads and
  4398. 2:54:12head dimension to get
  4399. 2:54:14model D because
  4400. 2:54:16now we want to transform from the key,
  4401. 2:54:19query, value space into the token space.
  4402. 2:54:21So,
  4403. 2:54:22we will use this this uh weight. So,
  4404. 2:54:25this output layer.
  4405. 2:54:27So, output is just our still it's our
  4406. 2:54:30values, value space.
  4407. 2:54:32And we pass that through this linear
  4408. 2:54:34layer to get actual token space, token
  4409. 2:54:36embedding.
  4410. 2:54:38But, before we pass our values, we need
  4411. 2:54:40to
  4412. 2:54:41we need to
  4413. 2:54:43convert them uh to the proper shape. So,
  4414. 2:54:47combine heads into one single vector.
  4415. 2:54:52Then we have the transformer block where
  4416. 2:54:54we combine all of that.
  4417. 2:54:56So, here we want to initialize feed
  4418. 2:54:58forward, which we didn't make yet, by
  4419. 2:55:00the way.
  4420. 2:55:02So, this is very simple. It has
  4421. 2:55:06It goes from model from token embedding
  4422. 2:55:08into the hidden four times bigger.
  4423. 2:55:11And then go back to token embedding. And
  4424. 2:55:13in this hidden four times bigger, you
  4425. 2:55:15have all of the facts living there, all
  4426. 2:55:17of the knowledge, and some ways to
  4427. 2:55:19transform tokens
  4428. 2:55:21as well. Because imagine attention as
  4429. 2:55:23just collecting information into the
  4430. 2:55:25token, last token,
  4431. 2:55:27in order to predict the next token.
  4432. 2:55:30And then this feed forward will process
  4433. 2:55:32that information, mix it up, do
  4434. 2:55:33something with it,
  4435. 2:55:35uh create new insights.
  4436. 2:55:38So, I also have dropout between those
  4437. 2:55:41expansion and
  4438. 2:55:42contraction layers.
  4439. 2:55:45So, this is the whole formula. So, we
  4440. 2:55:47pass our token into first linear,
  4441. 2:55:50use activation like ReLU, although
  4442. 2:55:52this could be maybe SiLU, SwiGLU,
  4443. 2:55:56but let's now use ReLU. ReLU is simple
  4444. 2:55:58and fast to compute.
  4445. 2:56:00And dropout to not to make so, to
  4446. 2:56:02prevent overfitting.
  4447. 2:56:03And then scale back into the token size.
  4448. 2:56:08In our transformer block, we will
  4449. 2:56:10initialize causal self-attention,
  4450. 2:56:14feed forward, and then two
  4451. 2:56:15normalizations. In this case, it's layer
  4452. 2:56:17norm, but I should actually change this
  4453. 2:56:19to RMS norm.
  4454. 2:56:21RMS norm is just used more these days.
  4455. 2:56:25And the dropout as well. And this is the
  4456. 2:56:27forward of the transformer. Okay? So, we
  4457. 2:56:30pass in first through the attention.
  4458. 2:56:34I guess we get input and mask here.
  4459. 2:56:38Although, we could probably generate
  4460. 2:56:39mask within attention as well.
  4461. 2:56:42Pass into the attention and then
  4462. 2:56:44normalize.
  4463. 2:56:47Although, we could also put
  4464. 2:56:48normalization before attention, but
  4465. 2:56:51let's keep it this way now.
  4466. 2:56:54And then uh feed forward, pass through
  4467. 2:56:55feed forward, normalize, and that's it.
  4468. 2:56:57That's our transformer block.
  4469. 2:57:00And then we have multiple of those
  4470. 2:57:02blocks and output head and token. And
  4471. 2:57:05so, we need to now combine everything
  4472. 2:57:06into one.
  4473. 2:57:08Here we need the entire vocabulary
  4474. 2:57:11of tokens. So, token embedding
  4475. 2:57:13vocabulary will just be
  4476. 2:57:16all of the tokens, vocab size. And each
  4477. 2:57:19of the tokens has its own embedding.
  4478. 2:57:22So, vocab size times
  4479. 2:57:24uh D model.
  4480. 2:57:27Then we also define these uh list of
  4481. 2:57:29blocks of transformer blocks that I was
  4482. 2:57:31just explaining. We will just do
  4483. 2:57:32multiple. Number of blocks is going to
  4484. 2:57:34be three in our case, if you remember
  4485. 2:57:36from the beginning of the video.
  4486. 2:57:39And this is I guess final norm
  4487. 2:57:41normalization that we get. And this is
  4488. 2:57:43the output head that it generates the
  4489. 2:57:45next token. So, this is after all of the
  4490. 2:57:47blocks, after everything.
  4491. 2:57:49Output head will generate next token.
  4492. 2:57:51And it has
  4493. 2:57:52D model times vocab size. So,
  4494. 2:57:56so it's basically going to
  4495. 2:57:59for every
  4496. 2:58:00token in the vocabulary,
  4497. 2:58:03it will generate probability that this
  4498. 2:58:05is the next token.
  4499. 2:58:07So, it converts our vector embedding
  4500. 2:58:10from the transformer into set of
  4501. 2:58:13probabilities,
  4502. 2:58:1450,000 probabilities if we have 50,000
  4503. 2:58:17tokens.
  4504. 2:58:19It's just a linear layer.
  4505. 2:58:22Vector embed token embedding into set of
  4506. 2:58:25probabilities over entire vocabulary.
  4507. 2:58:29So, this can actually be huge, and you
  4508. 2:58:30need some you can So, this can be 1/3 of
  4509. 2:58:33the entire language model
  4510. 2:58:36for small models. But, there are some
  4511. 2:58:38tricks.
  4512. 2:58:39For example, Apple's cut cross
  4513. 2:58:41attention.
  4514. 2:58:43Maybe writing some kernels or GPU code.
  4515. 2:58:47And let's see the forward pass through
  4516. 2:58:49our GPT GPT style decoder only
  4517. 2:58:53transformer.
  4518. 2:58:55Uh we get our batch size sequence length
  4519. 2:58:57and shapes. We get a device, which will
  4520. 2:59:00be GPU.
  4521. 2:59:02We can create masks here, or we can do
  4522. 2:59:04it in the attention. So,
  4523. 2:59:07uh this is how we create mask. As I
  4524. 2:59:08said,
  4525. 2:59:10I talked more about unsqueeze in my N
  4526. 2:59:13torch once, and
  4527. 2:59:16uh these
  4528. 2:59:17uh
  4529. 2:59:17PyTorch in my PyTorch fundamentals
  4530. 2:59:19course in this school.
  4531. 2:59:22So, we just want to make sure that we
  4532. 2:59:24have a triangular matrix where
  4533. 2:59:28um
  4534. 2:59:29we have like a mask such that when we
  4535. 2:59:32apply this to the attention,
  4536. 2:59:35for every single query,
  4537. 2:59:37it will only
  4538. 2:59:39be able it will only have unmasked the
  4539. 2:59:41previous keys, and it will have
  4540. 2:59:43masked the following keys.
  4541. 2:59:46So, this mask will, for example, be
  4542. 2:59:48for the first
  4543. 2:59:50token, it will have all of the masks for
  4544. 2:59:52the following
  4545. 2:59:54tokens, it will just have one unmask for
  4546. 2:59:56itself to pay the attention
  4547. 2:59:58to itself, to do uh query and key of
  4548. 3:00:00itself. For the second token,
  4549. 3:00:03it will have unmasked itself and
  4550. 3:00:05previous token, but masked all of the
  4551. 3:00:08next tokens.
  4552. 3:00:09Third token will have first three. So,
  4553. 3:00:11you you see how you have 1 2 3 4. So,
  4554. 3:00:14it's like a triangle
  4555. 3:00:15of masks, and this is triangle here is
  4556. 3:00:17also masked, and this is unmasked.
  4557. 3:00:20So, that's why we need this triangular
  4558. 3:00:23matrix.
  4559. 3:00:24And uh in the future, maybe uh we will
  4560. 3:00:27talk more about this as well.
  4561. 3:00:30Then, we will pass all of our tokens
  4562. 3:00:32through our token embedding
  4563. 3:00:35layer to replace
  4564. 3:00:37all of the tokens with their embeddings.
  4565. 3:00:39So, this will give us like list of
  4566. 3:00:41embeddings instead of the list of
  4567. 3:00:43tokens.
  4568. 3:00:45And this is good for stabilization,
  4569. 3:00:47dividing
  4570. 3:00:49uh these tokens
  4571. 3:00:51with the square root these embeddings
  4572. 3:00:53with the square root of the
  4573. 3:00:55model dimension.
  4574. 3:00:59As I said, you want to keep like your
  4575. 3:01:00numbers around one, around zero.
  4576. 3:01:03So, that's what we build This will do.
  4577. 3:01:04It will push them towards one.
  4578. 3:01:07Then, we pass our X through our blocks.
  4579. 3:01:11So, for block in self blocks, so we have
  4580. 3:01:13three block we have
  4581. 3:01:15We pass X through every transformer
  4582. 3:01:18block, and what block includes attention
  4583. 3:01:20and feed forward.
  4584. 3:01:22So, we do that three times here in this
  4585. 3:01:24loop.
  4586. 3:01:26And then, we normalize at the end and
  4587. 3:01:28generate
  4588. 3:01:30the probability over all of the possible
  4589. 3:01:33tokens
  4590. 3:01:36uh to be the next token.
  4591. 3:01:38But, these are logits. These are not
  4592. 3:01:39probabilities. These are just like
  4593. 3:01:40numbers. So, we can later turn them into
  4594. 3:01:44probabilities with softmax.
  4595. 3:01:46But, basically, these logits are also
  4596. 3:01:48like numbers.
  4597. 3:01:50Higher number means like it will later
  4598. 3:01:51become higher probability for this token
  4599. 3:01:53to be
  4600. 3:01:54chosen.
  4601. 3:01:56Okay, and then this is how we use and
  4602. 3:01:58train this GPT.
  4603. 3:02:00So, we initialize it.
  4604. 3:02:03We select a device,
  4605. 3:02:05which is going to be CUDA, which means
  4606. 3:02:07GPU, and Nvidia GPU,
  4607. 3:02:10if it's available.
  4608. 3:02:11You better not train on CPU. That's I
  4609. 3:02:13don't want to be funny. It's going to be
  4610. 3:02:14so long.
  4611. 3:02:15And then, put the model to device, too,
  4612. 3:02:18to GPU. So, because you initialize it
  4613. 3:02:19here, it can be initialized on CPU, you
  4614. 3:02:22want to move it to GPU.
  4615. 3:02:26And then, we can have like
  4616. 3:02:29this uh some dummy data.
  4617. 3:02:32So, this uh source, it's just going to
  4618. 3:02:36be
  4619. 3:02:37our random tokens, example tokens. So,
  4620. 3:02:40this is let's say we have like some text
  4621. 3:02:42here, but it's now randomly generated.
  4622. 3:02:45Some tokens, some sequences, and stuff.
  4623. 3:02:48And this is for the model training. We
  4624. 3:02:49need cross entropy loss
  4625. 3:02:52to
  4626. 3:02:53uh check the model's prediction versus
  4627. 3:02:55the real next token.
  4628. 3:02:58And we will use Adam to update weights
  4629. 3:03:01to make the model learn
  4630. 3:03:04uh based on this loss
  4631. 3:03:07to next time generate better loss.
  4632. 3:03:09And loss in large language models should
  4633. 3:03:12be around 1 to 2 to 3. If you have 0.001
  4634. 3:03:17or 10,
  4635. 3:03:18your loss,
  4636. 3:03:20then that's there is some bug. I had
  4637. 3:03:210.001 loss
  4638. 3:03:23because I had some bug. And then, I
  4639. 3:03:25discovered it, and so loss should be
  4640. 3:03:27around 1 to 3.
  4641. 3:03:30And then, model.train This will train
  4642. 3:03:32because
  4643. 3:03:33we are inheriting all of these like
  4644. 3:03:35train and stuff from PyTorch. This is
  4645. 3:03:38done by PyTorch.
  4646. 3:03:41So, this is how you train large language
  4647. 3:03:42model.
  4648. 3:03:43You have a sequence.
  4649. 3:03:45So, your inputs are going to be entire
  4650. 3:03:47sequence except the last
  4651. 3:03:50last token.
  4652. 3:03:52And your target is going to be entire
  4653. 3:03:54sequence except the first token.
  4654. 3:03:58And I'll explain why, but you see here
  4655. 3:04:00we take all of the batches, all of the
  4656. 3:04:01conversations,
  4657. 3:04:03all of the sequences, I should say.
  4658. 3:04:06And everything inputs is everything
  4659. 3:04:08except last,
  4660. 3:04:09and target is everything except the
  4661. 3:04:11first token. So, from first, so index
  4662. 3:04:14zero, token zero is not because
  4663. 3:04:18uh here in the inputs, we will start
  4664. 3:04:20with just one token, and we want to
  4665. 3:04:22predict the next token.
  4666. 3:04:24So, the target cannot be the first token
  4667. 3:04:26because we can start from the first. We
  4668. 3:04:28cannot start from like nothing in line
  4669. 3:04:30with it. So, we are not training model
  4670. 3:04:32to start from nothing.
  4671. 3:04:34We are training the model to start from
  4672. 3:04:35at least one token.
  4673. 3:04:37So, that's why target cannot be that
  4674. 3:04:39that first token.
  4675. 3:04:40Okay?
  4676. 3:04:42And
  4677. 3:04:43inputs will go until last, but not last
  4678. 3:04:46because
  4679. 3:04:47during the training,
  4680. 3:04:49we can have
  4681. 3:04:51it's possible to have all of the tokens,
  4682. 3:04:54all of the sequence as input except last
  4683. 3:04:57when we are predicting the last. Okay?
  4684. 3:04:59So,
  4685. 3:05:00all of these combinations are possible.
  4686. 3:05:02So, you can have like half
  4687. 3:05:04of the tokens
  4688. 3:05:06uh given, and you need to predict the
  4689. 3:05:08next half.
  4690. 3:05:09Or you need to Okay, so you wouldn't
  4691. 3:05:11predict You would just predict the next
  4692. 3:05:12token.
  4693. 3:05:13So, let let me simplify.
  4694. 3:05:15All of the possible combinations when
  4695. 3:05:18training large language model are
  4696. 3:05:21You can have one token and predict next.
  4697. 3:05:23You can have two tokens predict next.
  4698. 3:05:24You can have three tokens until you have
  4699. 3:05:28almost all and predict the last.
  4700. 3:05:31So, that's why inputs can be from zero
  4701. 3:05:33until last, but not last.
  4702. 3:05:36And targets can be
  4703. 3:05:38from the second token until last.
  4704. 3:05:41So, all of the possible combinations
  4705. 3:05:43where you have at least one to predict
  4706. 3:05:44the next, or until all except last to
  4707. 3:05:47predict last.
  4708. 3:05:50So, as I said, we will get logits, like
  4709. 3:05:52numbers,
  4710. 3:05:53uh when we pass this through the model.
  4711. 3:05:56And
  4712. 3:05:59So, in this case, it looks like we're
  4713. 3:06:00not using softmax. That's okay, too.
  4714. 3:06:03Um to put them into percentages,
  4715. 3:06:07we can also calculate
  4716. 3:06:09loss
  4717. 3:06:10uh by just
  4718. 3:06:12comparing the current logits to the
  4719. 3:06:13target
  4720. 3:06:15uh tokens.
  4721. 3:06:17So, the highest logit should be
  4722. 3:06:21same as the target, the correct token.
  4723. 3:06:24And if it's not,
  4724. 3:06:25then we calculate the loss, update
  4725. 3:06:27weights, do optimization steps. So,
  4726. 3:06:31we empty out gradients from the last
  4727. 3:06:33back propagation.
  4728. 3:06:35You calculate all of the
  4729. 3:06:37uh chain rule, the gradients, and then
  4730. 3:06:39you update weights to reduce the loss.
  4731. 3:06:44So, that's [snorts] going to be it for
  4732. 3:06:45our uh GPT style transformer.
  4733. 3:06:48I'm going to
  4734. 3:06:49put this video here, and see you in the
  4735. 3:06:51next lesson.

About this transcript

This page contains the full transcript of Become an AI Researcher Course – LLM, Math, PyTorch, Neural Networks, Transformers by freeCodeCamp.org, generated from the public captions YouTube serves with the video. The transcript has 27,531 words across 4,735 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 translate the transcript into 100+ languages.

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, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.