Become an AI Researcher Course – LLM, Math, PyTorch, Neural Networks, Transformers — Transcript
Full transcript
- 0:00Welcome to this full course on becoming
- 0:01an AI researcher. This course will guide
- 0:03you step-by-step starting with the
- 0:05foundational mathematics essential for
- 0:08understanding modern AI. Before diving
- 0:11into PyTorch fundamentals, you will then
- 0:13learn about the building blocks of AI
- 0:16from simple neural networks to the
- 0:18complexities of multi-layer
- 0:20architectures. The course ends with an
- 0:22in-depth module on transformers, the
- 0:25critical technology underpinning today's
- 0:27large language models and generative AI.
- 0:30By the end, you will possess all the
- 0:32fundamental knowledge required to
- 0:34critically read, understand, and even
- 0:37begin contributing to AI research
- 0:40papers. Vuk Rosić developed this course.
- 0:43Welcome to the full course of becoming
- 0:45AI researcher. I will guide you
- 0:48step-by-step through mathematics and
- 0:51then PyTorch fundamentals, neural
- 0:53networks,
- 0:55and lastly, transformers. After
- 0:58finishing this course, you will have all
- 1:00of the fundamental knowledge necessary
- 1:02to start doing your own AI research, to
- 1:05start understanding others' AI research,
- 1:07and publishing your own papers.
- 1:09First, we will start from basics,
- 1:11talking about functions in mathematics,
- 1:14different types of functions, and what
- 1:16each term in a function determines. Then
- 1:19we will talk about derivatives, how
- 1:21derivatives show a rate of change of a
- 1:23function, and how they are used later in
- 1:27neural networks.
- 1:28After that, we will learn about vectors.
- 1:30Vectors are present everywhere in large
- 1:32language models, reinforcement learning,
- 1:34robotics, and neural networks, and
- 1:37anywhere else you can imagine pretty
- 1:39much. They're used for everything.
- 1:42Then we will combine our knowledge of
- 1:44vectors and derivatives to figure out
- 1:46gradients. Gradients are used for
- 1:49learning of the neural network. Gradient
- 1:51is how neural networks learn. So, you
- 1:52will understand what they are, how they
- 1:55show the direction of steepest ascent,
- 1:57how to use them to make neural network
- 2:01errors lower, how to make how to use
- 2:04them to make neural networks learn.
- 2:07And by the way, right now I'm also
- 2:08explaining to you your road map, your
- 2:11path to become AI researcher, what you
- 2:13need to learn. So, keep track of what
- 2:16I'm explaining. This is not just an
- 2:17intro. This is your road map to becoming
- 2:19AI researcher, so you can self-study as
- 2:22well. Next are matrices and matrix
- 2:25multiplications.
- 2:26These are maybe the core of neural
- 2:29networks besides gradients. They are
- 2:31done on GPUs. Neural networks are all
- 2:33about calculating matrices and doing it
- 2:37quickly and efficiently.
- 2:39And lastly, in the math module, we have
- 2:41a probability. This is used also
- 2:43absolutely everywhere. Neural networks,
- 2:45large language models, reinforcement
- 2:46learning, robotics, everywhere. Video
- 2:49generation, diffusion, image generation.
- 2:51Then there is PyTorch module. The first
- 2:54lesson includes creating tensors,
- 2:56understanding tensors, understanding
- 2:58data types.
- 2:59Then matrix multiplication, different
- 3:02operators, and how matrix multiplication
- 3:05works in PyTorch.
- 3:07Then transposing, flattening, reshaping,
- 3:09viewing, squeezing, unsqueezing tensors.
- 3:11All of these things are going to be used
- 3:13very frequently in AI research, so you
- 3:15need to understand and master them. And
- 3:17I have a lot of examples for you to play
- 3:19around. You can find this GitHub
- 3:21repository below the video, the first
- 3:24link in the description.
- 3:26You will also learn tensor indexing and
- 3:28slicing. So, if I tell you I want last
- 3:30two rows, and out of those last two
- 3:33rows, I want um
- 3:35second to fifth columns. And so, you
- 3:38need to be able to quickly uh write
- 3:40indexing logic. Then we will talk about
- 3:43special matrices and arrays like
- 3:45identity matrix, random matrix,
- 3:48linspace, etc. After PyTorch module, we
- 3:51will work with neural networks. First,
- 3:54we will create a single neuron,
- 3:55understand what weights and biases are,
- 3:57how they influence the output, how they
- 4:00are calculated. Then activation
- 4:03functions, how they turn the straight
- 4:06line of weights and bias and input
- 4:09multiplication into a curved line that
- 4:11can encircle data.
- 4:13We will talk about different activation
- 4:15functions and what they are useful for.
- 4:18We will also talk about multi-layer
- 4:20neural networks and how to have entire
- 4:23layer of weights in a single matrix and
- 4:25how that matrix multiplies inputs and
- 4:27how
- 4:28the results get transformed. So, we will
- 4:31be building neural network layers from
- 4:34scratch, coding it step-by-step with all
- 4:36of the functions explained step-by-step
- 4:38in this course.
- 4:40As well as backpropagation and how
- 4:42neural networks learn.
- 4:43And finally, in the last module, we will
- 4:45talk about transformers. Transformers
- 4:48are base of today's large language
- 4:50models, as well as image generation,
- 4:52video generation, audio generation, and
- 4:55almost everything.
- 4:56You will understand the attention
- 4:58mechanism, and then on top of that,
- 5:00self-attention and multi-head attention,
- 5:02and then the feedforward networks and
- 5:04layers of the transformer, and how all
- 5:06of that works, as well as tokenization.
- 5:09And this will be a good base for
- 5:11building your own large language model.
- 5:14As well as video generation, image
- 5:15generation, and other models. So, that's
- 5:18the road map that we will follow. You
- 5:20can also follow this road map by
- 5:22searching other YouTube videos and
- 5:24self-studying.
- 5:26And if you are ready, we will begin.
- 5:28Hello, guys. This is mathematics for AI
- 5:31research. If you understand and learn
- 5:33the math I'm going to explain in this
- 5:35video, you will be able to take any AI
- 5:38research paper and understand math
- 5:40within it as well. And you will be able
- 5:42to continue doing research in any part
- 5:45of AI that you wish. So, is it large
- 5:48language models, transformers, video
- 5:50generation, diffusion, image generation,
- 5:52reinforcement learning, robotics?
- 5:54Whichever part of AI you are interested
- 5:56in, this is fundamental math that
- 5:58appears in everywhere. This is going to
- 6:01be part of my course on creating and
- 6:03training large language model like
- 6:05Kimiko thinking from scratch, but this
- 6:08math applies to the entire field. But
- 6:11whichever AI research you are doing,
- 6:12you're going to have to be learning this
- 6:14math as well. If you click the first
- 6:16link below this video, you're going to
- 6:17get to this repository, become elite AI
- 6:19researcher, and the first module is
- 6:22math, which what I'm going to which is
- 6:23what I'm going to explain now. We have
- 6:25all of these Jupyter notebooks. So,
- 6:28requirements here is that you understand
- 6:30basic Python. So, here we have some
- 6:33array, we have some for loop, we have
- 6:36some variables. So, this is all basic
- 6:38Python.
- 6:39So, if you are not familiar with basic
- 6:41Python, you can just type a Python
- 6:43tutorial on YouTube, and you can check
- 6:46whichever one you like. Maybe this one,
- 6:48or actually Harvard classes are actually
- 6:50very good.
- 6:51So, I want you to know how to execute
- 6:53Python code. Uh it can be on your local
- 6:56machine, or it can be in Google Colab.
- 6:59So, now I also want you to say Google
- 7:02Colab tutorial, if you want if you want
- 7:04to use Google Colab, but also you can
- 7:06just run it on your local machine. So,
- 7:08whichever one you prefer.
- 7:10Also, if you're using VS Code or Cursor
- 7:12or some fork of VS Code, you can install
- 7:15this brand new Google Colab extension.
- 7:18So, if you just search for Colab, it's
- 7:19by Google, install it, and then you can
- 7:21connect to Google Colab GPU, CPU, TPU
- 7:25from your local environment, from your
- 7:27local Jupyter notebook.
- 7:30So, now on my GitHub repository, in the
- 7:32math folder, I want you to open this
- 7:35first Jupyter notebook. So, you can copy
- 7:38this and open it in Google Colab. As I
- 7:41explained, you can ask ChatGPT how to do
- 7:43it, or open it in your local, or however
- 7:46you want to open this.
- 7:48And then
- 7:49I have it here on my local, and we're
- 7:51going to go through this.
- 7:53So, if you install
- 7:54Google Colab extension, you can click
- 7:56here Google Colab, and we can say open
- 8:00Google Colab web.
- 8:02But I don't want to open this on web. I
- 8:03want to say um
- 8:06select kernel
- 8:08with which to activate this and select
- 8:10another kernel, and then Colab.
- 8:13And then I can create new Colab server.
- 8:16And then I can choose CPU in this case,
- 8:19because I don't need to
- 8:23run this on GPU. But keep in mind, uh
- 8:25now you can write a name.
- 8:29I can just use the default name, like
- 8:30this. Press enter. But you don't
- 8:32actually need to do this if you want to
- 8:34run the Python code on your local
- 8:35computer. So, I'm I'm showing you how to
- 8:37run it from your local using Google
- 8:40Colab, Google's GPUs or CPUs.
- 8:43But you can just run it locally as well.
- 8:46And so, if I click here Colab,
- 8:49I can click remove server if I want to
- 8:52uh shut down the server. But anyways,
- 8:57first, depending on which kernel you are
- 8:59running here, uh if it's Google Colab,
- 9:02it will have these NumPy, Matplotlib
- 9:04already pre-installed.
- 9:06But uh you may also want to create your
- 9:09own environment.
- 9:12And this is something you would learn
- 9:13how to do on YouTube or from ChatGPT,
- 9:15and you need to install these
- 9:17requirements.
- 9:18So, if you try to run this, but you get
- 9:21error like uh NumPy or NP is not
- 9:24defined, then you would don't have these
- 9:27requirements installed. So, make sure
- 9:29you enter your console,
- 9:32and make sure you are in the folder
- 9:34where the in which the requirements are.
- 9:38So, this is the outside folder in which
- 9:40requirements file is.
- 9:43And then I'm going to say pip install -r
- 9:45requirements.txt,
- 9:47but make sure to create your
- 9:48environment, install this in a Python
- 9:50environment.
- 9:53And then just press this.
- 9:55Enter.
- 9:56But actually, I don't want to install
- 9:58PyTorch because uh for the PyTorch
- 10:01tutorials, I'm going to use Google
- 10:02Colab. So, I'm just going to install
- 10:04NumPy and Matplotlib, and then I'm going
- 10:06to comment out PyTorch.
- 10:08So, let me do the installation again.
- 10:10Okay, now this is already installed.
- 10:13So, let's do Let's start with the maths.
- 10:16Now that you have everything set up,
- 10:18everything installed, I will show you
- 10:19the curriculum. First, we're going to
- 10:21learn the functions. So, these are math
- 10:23functions, not Python functions, but
- 10:25math functions. Then, derivatives.
- 10:29Then, vectors, gradients, matrices, and
- 10:32probability.
- 10:33A lot of you may already be familiar
- 10:36with some of these, especially maybe
- 10:37with functions. But, uh you can use uh
- 10:40timestamps below or chapters to skip,
- 10:43but maybe you can also uh watch it
- 10:45anyways. So, uh
- 10:48function will map
- 10:50from one value or from one set of
- 10:53values, but let let's make it simple.
- 10:55You have some value X
- 10:57equals one. Your function will transform
- 11:00it into some other Y, like Y equals two,
- 11:03based on some rules. For example,
- 11:06if we have function that Y is equal to 2
- 11:09* X,
- 11:10then if X is one, Y will be two. If X is
- 11:15five, Y will be 2 * 5 is 10.
- 11:19And
- 11:21I want to draw this, so
- 11:23uh as X goes 0 1 2 3 4,
- 11:29the Y is going to be the double. So,
- 11:31here at For example, at five, Y will be
- 11:3410.
- 11:36So then, for every X, we're going to
- 11:40take and draw corresponding Y.
- 11:43So, for example, for uh 2.5 right here,
- 11:46Y will be five.
- 11:49For two, Y will be four, right here.
- 11:53For five, Y will be 10. So, if we then
- 11:56just connect all of these uh dots we
- 11:58draw all of these dots, we get this
- 12:00line. So, this is a linear function.
- 12:03And this is how we drew This is where
- 12:05the plot drawing happens. So, you don't
- 12:08need to understand uh this part at all.
- 12:09This is just to show this plot. So,
- 12:12let's just understand this part. This is
- 12:13our function.
- 12:15So,
- 12:16we will create
- 12:19an array of X values between zero and
- 12:2210.
- 12:23And we're going to create 100 X values
- 12:26between zero and 10,
- 12:28which is including zero and 10. So, we
- 12:30You see, we have X from zero to 10, and
- 12:32in between we have 100 small dots.
- 12:36And for each of these, we're going to
- 12:38just calculate the Y correspond
- 12:40corresponding Y values. So, 2 *
- 12:442 * uh these X values.
- 12:48So, Y will also be an array of 100
- 12:50numbers, but each element will be two
- 12:55times the corresponding element in the
- 12:57X. So, if it's three, it's going to be
- 12:59six here. Four will be eight, etc.
- 13:03And then, we just use this to draw.
- 13:07And that's how we draw the linear
- 13:08function here.
- 13:09And let's see in if instead of having
- 13:12number two here, we have some other
- 13:14numbers.
- 13:15So,
- 13:17Y is equal to 1 X, 2 X, 3 X, 0.5 X, -1
- 13:21X. So, this will This number will
- 13:25determine the slope of this linear
- 13:27function.
- 13:29The 3 X, which is the highest number
- 13:32here, will also have the highest slope.
- 13:35The lowest number, minus one, will have
- 13:38kind of highest negative slope in
- 13:41negative
- 13:42uh direction on negative X.
- 13:46For example, if we look at Y equals X,
- 13:49here where X is four,
- 13:53Y will also be four.
- 13:56So then, that's the slope as well.
- 14:00And the way this is coded is very
- 14:01simple. There is a list of K values.
- 14:06And then, we just plot for each
- 14:09uh of the K values, we plot the
- 14:10function.
- 14:13I recommend using uh AI, ChatGPT, or
- 14:16Cursor to explain any of these things
- 14:19for you if you don't understand it.
- 14:21But, I want to focus on the theoretical
- 14:23part of the math for now. So, this
- 14:26coefficient determines the slope of the
- 14:28function. Then, there is quadratic
- 14:30function,
- 14:31where Y is equal to X squared.
- 14:34So, as we go on
- 14:38X slowly, the Y will become a lot
- 14:41bigger. We'll start to accelerate.
- 14:44So, in the beginning, Y doesn't grow so
- 14:47quickly, but later, uh when we go from
- 14:50two squared, three squared, four
- 14:52squared, five squared, the Y starts to
- 14:55become a lot bigger, a lot faster.
- 14:58Here,
- 14:59like zero squared is zero, one squared
- 15:02is one, two squared, four, three
- 15:04squared, nine, four squared, 16. So, Y
- 15:07starts to grow very fast. You can play
- 15:09around with this code
- 15:11to see and to modify to see uh what the
- 15:14drawing will look like.
- 15:16Now, notice that uh Y is always positive
- 15:19here. So, Y never goes down below zero
- 15:22on Y axis. We never go down. Uh because
- 15:25uh square function, quadratic function,
- 15:27will always make everything positive.
- 15:29Because if you multiply
- 15:31two negative numbers or negative number
- 15:33with itself, it's going to become a
- 15:34positive number. So, if you square a
- 15:36negative number, like minus two here, it
- 15:38will become a positive number.
- 15:41But, that's not true for the cubic
- 15:43function,
- 15:45where
- 15:46uh putting uh negative number cubed will
- 15:48give you negative number.
- 15:51So, this is what cubic function looks
- 15:53like. And
- 15:54it grows even faster
- 15:56than
- 15:58square function. Here at three, we are
- 16:01already at 27
- 16:03uh on Y.
- 16:06And three was just nine
- 16:08in the square
- 16:10here.
- 16:12So, cubic grows even faster, but there
- 16:14is also negative part of it. So, this is
- 16:15the shape of the cubic
- 16:17function. This is the shape of the
- 16:19square function. So, knowing these
- 16:21shapes is good.
- 16:23Square root function. So, Y is equal to
- 16:26square root of X.
- 16:28Uh I'm going to have the same
- 16:31code here. So, I put some numbers.
- 16:34Uh you can only find roots of positive
- 16:37numbers, by the way. You cannot find
- 16:38roots of negative numbers. It's not
- 16:40defined or it's not real, I should say.
- 16:44And so,
- 16:45this is what
- 16:47uh Y equals
- 16:49square root of X looks like.
- 16:52This is also good to know, this kind of
- 16:54function.
- 16:56It's growing very fast in the beginning,
- 16:59but then it's starting to grow slower
- 17:01and slower.
- 17:04Although, this will grow to infinity as
- 17:06well.
- 17:09So, there is no limit here. It will also
- 17:12grow forever.
- 17:13Uh as X grows forever, Y will also grow
- 17:15forever.
- 17:17Then, we have exponential function,
- 17:19where Y is equal to two power X or some
- 17:24some number
- 17:25some constant power X.
- 17:28And this grows very fast.
- 17:31This grows faster than the quadratic.
- 17:33So, two power X grows faster than X
- 17:36power two.
- 17:38It's good to know that.
- 17:39And this is the shape of exponential
- 17:41function.
- 17:42The code is very similar. This part is
- 17:45just different. So, Y is equal to two
- 17:47power X. This is how you can denote
- 17:50power in
- 17:51uh Jupyter
- 17:53or Python.
- 17:54We also have sine function. So,
- 17:57uh Y is equal to sine X, and I'll show
- 18:00you first that this is a periodic
- 18:01function.
- 18:04And this function is important. It's a
- 18:06trigonometric function. It's used
- 18:07everywhere. It's used so often.
- 18:10And in Python, you can just write Y is
- 18:12equal to math.sin of X.
- 18:16Now, let's go back to the linear
- 18:17function. So, this is also a linear
- 18:19function.
- 18:20So, we have some uh 2 * X, which is
- 18:24coefficient and the variable plus some
- 18:26constant or minus some constant that
- 18:28doesn't have * X in it or
- 18:32So, if you change this number three,
- 18:36make it four, five, six, you will just
- 18:38move this function left or right. So,
- 18:41the slope will be same. So,
- 18:44if you change this number two, then you
- 18:46will change the slope of the function.
- 18:48But, if you change this number three,
- 18:50you will just move it left or right.
- 18:53This is also important to know.
- 18:55And now, we may take a look at these
- 18:58functions.
- 19:01And so, that's going to be math
- 19:02functions. I recommend you play with
- 19:04this. Make sure you understand uh this,
- 19:07and we can go on to the next lesson.
- 19:10Next, we have derivatives. I'm going to
- 19:12try to explain the main points of
- 19:15derivatives.
- 19:16So, if you have a So, if you have a
- 19:18function, you can find the derivative of
- 19:21that function
- 19:24that shows the rate of change of the
- 19:27function, how quickly it grows at each
- 19:30point. So, you see, this linear function
- 19:34is always growing at the same rate. So,
- 19:36this function is not like quadratic,
- 19:38where it's growing a bit slower here, a
- 19:41bit faster here, etc.
- 19:43This linear function is always growing
- 19:45at the same rate.
- 19:47In this case, the derivative is going to
- 19:49be equal to this number two.
- 19:53So, if we have 2x, the way I'll show you
- 19:56later how to calculate derivatives, but
- 19:58the derivative will be this two. So, at
- 20:01any point
- 20:03when you increase x by a little bit,
- 20:05when you go in the x forward by a little
- 20:07bit, the y will go by two times that
- 20:11amount you went on x.
- 20:14So, you move x a little bit, the y will
- 20:16go up by two times. So, that's
- 20:19So, here you can see the derivative is
- 20:21two at any x. So, at any x here, the
- 20:25derivative is always two.
- 20:27Because it's a linear function, the
- 20:28growth is always the same.
- 20:31Can you try to explain yourself what
- 20:34derivative is? Can you try to explain
- 20:35what I just explained uh yourself?
- 20:38So, derivative shows rate of change.
- 20:41And it's linear function, so rate of
- 20:42change is always the same, and it's two
- 20:44in this case. It's equal to this number
- 20:46two of 2x.
- 20:49So, in my code, I have the function f of
- 20:52x, and the derivative is just hardcoded
- 20:55here. It's always two. Because
- 20:58I'll show you later how to calculate it,
- 21:00but this will be derivative two.
- 21:03Let's see derivative of this quadratic
- 21:05function, y is equal x squared.
- 21:08So, the derivative here will be 2x.
- 21:13And I'm going to explain how it's
- 21:15calculated, but we can see
- 21:19that here the function doesn't grow
- 21:22equally everywhere. So,
- 21:24actually, as we increase or go further
- 21:28into negative
- 21:29uh on x, the y will start growing a lot
- 21:33faster. Here around zero, x is growing,
- 21:36but y is not growing up so much.
- 21:39But as we uh grow x, the y grows a lot
- 21:42faster.
- 21:44So, now we don't have a constant
- 21:46derivative that's always the same, like
- 21:48two, the constant growth.
- 21:50Here we have
- 21:52uh derivative looks like a linear
- 21:54function. So, derivative here is 2x.
- 21:57So, the derivative itself can be drawn
- 21:59as a function, by the way. So, we have
- 22:02one
- 22:03and two
- 22:05for this spot.
- 22:08So, funnily enough, you can also take
- 22:10derivative of this derivative. So, that
- 22:12would be second derivative. So,
- 22:14here the growth will be just two.
- 22:17Derivative of 2x will be just two.
- 22:19But let's not get confused. So, here we
- 22:21have the function x squared, the
- 22:23derivative is 2x. Because the formula
- 22:26is
- 22:28you
- 22:29put this So, if you have x to n, you put
- 22:32this number two, number n in front of x,
- 22:35so you get 2x.
- 22:37You will just put this number in front
- 22:38of x, so you get 2x.
- 22:40And you subtract one from this number up
- 22:43here. So, 2 - 1 will be one. So,
- 22:46you will have x to power one. So, that's
- 22:49just x.
- 22:51So, the derivative of this is 2 * x
- 22:53power one, which is just 2x.
- 22:57That's the formula for calculating
- 22:58derivatives. You can also check other
- 23:00tutorials. Now, this can go quite deep.
- 23:04Um
- 23:05I think it would be good if you knew it,
- 23:08but it's not absolutely essential to
- 23:09master this. I'm just explaining These
- 23:12are the things you need to understand.
- 23:14Uh you also learn this in high school.
- 23:18So, you will encounter this in some
- 23:19research papers, and it's good to know
- 23:22uh this stuff. Now, do you want to dive
- 23:23deeper right now or later? It's up to
- 23:25you.
- 23:27So, what this derivative means is that
- 23:30um
- 23:31any of these points, wherever you pick
- 23:34x, for example, if you look at x = 2,
- 23:37then that means here on this original
- 23:39function, if you if at x2, you increase
- 23:43x by just a little bit,
- 23:46then the y is will increase by
- 23:50four.
- 23:51You see here, it's number four. So,
- 23:54if you increase x by a bit, the y will
- 23:57increase four times.
- 24:00Because at this particular
- 24:04uh place, let's calculate derivative.
- 24:07Derivative is 2 * x, so 2 * 2 is four.
- 24:12So, at this particular place, if you
- 24:14increase x by a little bit, the y will
- 24:17increase four times that little bit you
- 24:19of your increase. Here,
- 24:21at three, derivative is six.
- 24:24So, at here at three, if you increase x
- 24:27by a little bit,
- 24:28the y will increase six times that
- 24:31little bit.
- 24:32And so, you can see that y is increasing
- 24:34faster and faster as you increase x.
- 24:38So, this may require a bit of practice,
- 24:40a bit of maybe pen and paper. You can uh
- 24:43try to think about this yourself. Maybe
- 24:45watch some specific tutorials on
- 24:47derivation if you
- 24:48uh wish, so now or maybe even later.
- 24:52So, we have uh this quadratic function,
- 24:54and I want to show you at each of these
- 24:57points, whichever points we pick, 0 1 2
- 25:01uh on the x-axis,
- 25:04each point has its slope.
- 25:08So, uh because it's quadratic function,
- 25:11we can see that there is no slope at
- 25:13zero. X = 0, there is no slope, because
- 25:15y is zero, x is zero here.
- 25:18Uh at one, there is a slope. At x = 2,
- 25:23there is even higher slope.
- 25:26>> [snorts]
- 25:26>> So, this slope is basically the
- 25:29derivative. I told you that
- 25:32at number at x = 2, if you increase x by
- 25:35a little bit, by 0.001, for example,
- 25:38the y will increase by four times that
- 25:40amount.
- 25:43Now, I'm not going to tell you that if
- 25:45you increase
- 25:47here x by one, so you go from two to
- 25:50three, the y will increase by four.
- 25:52Because
- 25:53I'm not going to tell you that, because
- 25:55as you move towards three, the y rate of
- 25:58change will also be increasing.
- 26:02Keep in mind that the slope will change.
- 26:06That's why I say if you increase So,
- 26:09just around this two, a little bit
- 26:11around this number two, the slope is
- 26:14four. Okay? As you move towards three,
- 26:17the slope will become six. It will
- 26:19increase.
- 26:21So, that's why I say
- 26:23if you just increase
- 26:25uh x by a little bit here, the slope
- 26:27will increase by this amount, four times
- 26:30or six times. But if you increase x by a
- 26:32bit, by by a lot more,
- 26:35then the slope will also change.
- 26:38So, in summary, the derivative will show
- 26:40you the slope. It will show you rate of
- 26:42change around this uh point.
- 26:46And for this particular function, the
- 26:48slope or the derivative will be equal to
- 26:502x.
- 26:53So,
- 26:54uh here if x is one, the slope is two.
- 26:58If x is two, slope is four. If x is
- 27:00three, slope is six, etc.
- 27:03Now, let's see some derivation rules.
- 27:06So, if you have
- 27:08x power three, you're going to put this
- 27:10three in front of the x, so you get So,
- 27:13the derivative will be 3 * x.
- 27:16And also,
- 27:17you need to subtract one from this
- 27:19exponent. So,
- 27:213 - 1 is two, so the final derivative
- 27:24will be
- 27:253 * x power two. And it's just x power
- 27:29two. So, it's x power two times three.
- 27:32And so, you can see these examples, like
- 27:34uh
- 27:35derivative of x power five is going to
- 27:37be 5 *
- 27:39x power four.
- 27:41You just subtract one here, so it's x
- 27:43power four times five.
- 27:45And this four is not like it's not the
- 27:47whole thing. It's just x power four.
- 27:52And so, if we have a function y equals
- 27:54uh x cubed or x power three, then
- 27:58the
- 28:03according to the rules.
- 28:04So, this is the derivative function.
- 28:06It's 3 * x squared.
- 28:09And so, we already know that
- 28:11x squared
- 28:13it's going to be like this U shape. We
- 28:16discussed that earlier.
- 28:18And then 3 * x squared, so it will just
- 28:23make the y's even larger. It will maybe
- 28:27uh squish it like So, this coefficient
- 28:30will just change this uh how wide or
- 28:32thin
- 28:33the U shape is.
- 28:36But basically, because we have x
- 28:39squared, we're going to have U shape.
- 28:42So, that's a derivative. It shows how
- 28:44the function is changing at any of these
- 28:48points.
- 28:50Some other derivation rule. So, if you
- 28:52have
- 28:53a function 2x + 3, you can find
- 28:56derivatives of each of these
- 28:58uh separately. So, around plus or minus,
- 29:01so you can find derivatives of 2x and
- 29:04derivative of
- 29:05constant three.
- 29:07And uh the rule is that
- 29:09a constant three, any constant, becomes
- 29:12zero.
- 29:13So, it will just be plus zero. So, we
- 29:15can omit that.
- 29:17And derivative of 2x
- 29:19will be
- 29:20So, uh the rule is the following.
- 29:23If you have constant times variable, the
- 29:26constant will remain the same. So, two
- 29:29will remain the same, because it's times
- 29:31variable, okay? Here,
- 29:33this constant two is not separate. So,
- 29:35here three doesn't have any variable
- 29:37attached to it. Three is not multiplying
- 29:39a variable, so three disappears. It
- 29:40becomes zero.
- 29:42But here, constant two is multiplying a
- 29:44variable. So, it remains two. In the
- 29:47derivative, this constant two will
- 29:48remain
- 29:49two.
- 29:51It's explained well in ChatGPT. So, as I
- 29:53said, derivative of a constant, it's a
- 29:55alone constant that doesn't doesn't
- 29:57multiply
- 29:58variable. Like here, two, constant two
- 30:01multiplies variable. So, this is not a
- 30:02alone constant. So, if you have a alone
- 30:05constant doesn't multiply variable, then
- 30:07derivative is zero. So, this part will
- 30:09be completely
- 30:11deleted or omitted.
- 30:12And then, derivative of 2x is two. Let's
- 30:16see how.
- 30:18So, 2x is equal to 2 * x ^ 1.
- 30:22And we know that I explained earlier
- 30:26uh that if we find derivative So, first
- 30:29of all,
- 30:30because we have constant times variable,
- 30:32we will just
- 30:33This constant will remain the same. So,
- 30:35in the derivative, this constant will
- 30:37not change. So, in the derivative,
- 30:39this will still be two times something,
- 30:41okay? So, now let's see what that
- 30:43something is.
- 30:45I told you the rule is
- 30:47whatever power you have here, you put it
- 30:49in front of the x.
- 30:51So, one will go in front of the x.
- 30:55And then you will subtract one from
- 30:57whatever power we have here. So, we have
- 30:591 - 1. So,
- 31:021 * 1 - 1. So, that's 1 * x ^ 0.
- 31:08x ^ 0 is just 1.
- 31:10So, this is equal to one. Any number to
- 31:11zero is equal to one.
- 31:13So, that's going to be one. Okay?
- 31:16So, then I told you
- 31:18we will just get 2 * 1 because
- 31:22as I said, two multiplies variable, so
- 31:25it remains the same in the derivative.
- 31:28And then we apply this rule x to the x
- 31:30that I explained, so it will be just 2 *
- 31:321.
- 31:36And so, this derivative of 2x is two.
- 31:40So, at the end, derivative of this is
- 31:42two, derivative of this is zero, so we
- 31:44get 2 + 0.
- 31:46We get derivative of this whole thing is
- 31:50two.
- 31:51So, you can practice this. This is done
- 31:53in high school, university. You can
- 31:54practice this yourself if you want uh
- 31:57with ChatGPT or some other AI or
- 31:59YouTube videos.
- 32:01And I can show it to you here. So, the
- 32:03function is 2x + 3. The derivative is
- 32:06two. It's always two for any x.
- 32:09Uh when whenever you increase x, the y
- 32:11will increase by two times
- 32:14whatever the increase in x was
- 32:17in this function
- 32:19because the derivative here is always
- 32:21two.
- 32:22So, this means that +3 constant doesn't
- 32:24affect the slope of the function. As I
- 32:27said,
- 32:28the slope is determined by this number
- 32:29two.
- 32:31And so, if I change this number two, the
- 32:33slope of the function will change. But
- 32:35this constant +3, this will just move
- 32:38the function left or right. So, this
- 32:41constant +3 will not change the slope.
- 32:43So, it So,
- 32:46it makes sense that it disappears in the
- 32:48derivative as well. It becomes zero. It
- 32:49doesn't affect the slope.
- 32:52And now I just want to summarize.
- 32:54Derivative, so we have linear function,
- 32:56the derivative is constant.
- 32:58We have quadratic function, so x ^ 2,
- 33:02the uh derivative is linear, so x ^ 1 is
- 33:05the derivative.
- 33:07We have cubic function, x ^ 3,
- 33:10the derivative will be x ^ 2. It will be
- 33:12quadratic. So, the derivative is kind of
- 33:14one degree less
- 33:17than the function.
- 33:19So, that's going to be the core
- 33:20knowledge you need to know about
- 33:22derivation. Uh you will eventually learn
- 33:24more as you read papers, but for now,
- 33:27this is enough.
- 33:29Third lesson, let's learn about vectors.
- 33:32So, this is ex- This is maybe the most
- 33:33important one besides matrices as well.
- 33:37Uh everything is in vectors in machine
- 33:39learning, neural networks.
- 33:41First, we will look at a vector as just
- 33:43an array of numbers.
- 33:45So, here we're going to create a NumPy
- 33:48array. Hopefully, uh you understand what
- 33:51NumPy is. You can watch YouTube if you
- 33:53don't understand this Python. So, these
- 33:55are fundamentals. You can
- 33:57uh learn that first.
- 33:59So, array of just two numbers in this
- 34:01case, three and four. Uh vector can be
- 34:05uh array of any
- 34:07amount of numbers.
- 34:10And
- 34:11we can also look at these numbers as
- 34:14coordinates on a coordinate system.
- 34:17So, x is three, y is four.
- 34:20We can look at that this way if we want.
- 34:23So, this blue example, it's going to be
- 34:27x is three here, three, and y is four.
- 34:31So, that's the vector, the spot. So, we
- 34:34can draw vector by
- 34:36going from zero to that spot.
- 34:39And you can see that vector is like an
- 34:41arrow.
- 34:42So, vector has a direction and length.
- 34:46Direction and length.
- 34:49We can also look at a vector as going
- 34:51like
- 34:52uh three units this way and then four
- 34:55units this way to end up at this spot.
- 34:59And here we have two other examples of
- 35:01minus So, minus two, one.
- 35:04So, going minus two on x and one on y.
- 35:08Minus two, one. And zero, five. So, zero
- 35:12on x, five on y. So, this vector will be
- 35:16on the x Sorry, on the y axis.
- 35:19Then let's look at vector addition. So,
- 35:21if you have vector three, two
- 35:24and vector one, three, if you add them,
- 35:26you're going to say you add the first
- 35:28elements, so you add 3 + 1, the first
- 35:31elements
- 35:33will be four. And the second elements,
- 35:362 + 3, will be five.
- 35:39And uh we can also add vectors visually.
- 35:44Starting from the first vector, three,
- 35:46two,
- 35:47going from zero to three, two,
- 35:49and the second vector, one, three. Now,
- 35:53this vector would go here from zero,
- 35:54zero to
- 35:56one, three like this here. So, this
- 35:58would be this second vector.
- 36:02Uh and then we will just move this
- 36:04second vector to the end of this first
- 36:06vector if we want to add it visually.
- 36:09And so, wherever it ends up,
- 36:11that's going to be our resulting vector.
- 36:14So, the resulting vector will be four,
- 36:17five.
- 36:18We can draw it from origin to four, five
- 36:21if we want.
- 36:22So, that's how you can add vectors
- 36:24visually as well.
- 36:26Let's see multiplying vector with a
- 36:28scalar. So, if we have vector two, one,
- 36:31what happens if we say
- 36:33two times that vector, 0.5 times that
- 36:36vector, minus one times that vector. So,
- 36:39this blue is our original vector.
- 36:42Um
- 36:43So, it goes from zero to two, one. Now,
- 36:46multiplying with a scalar will uh
- 36:48stretch or squish or inverse, so make it
- 36:52go in the other direction.
- 36:55So, two times
- 36:57vector two, one will give us vector
- 36:59four, two. So,
- 37:01you multiply each of the elements.
- 37:03So, two times
- 37:05v is going to be 2 * 2, it's four, and 2
- 37:07* 1, it's two.
- 37:10So, the resulting vector will actually
- 37:13be two times
- 37:15uh longer and it will have same
- 37:17direction.
- 37:20And multiplying with 0.5, it will just
- 37:23make it two times shorter instead.
- 37:26>> [snorts]
- 37:26>> Multiplying it with minus one
- 37:29will have the same length, just the
- 37:31opposite direction.
- 37:34So, it will be minus two, minus one.
- 37:37How do we calculate uh
- 37:39length or magnitude of vector? How long
- 37:42is this vector?
- 37:43So, it's actually similar to Pythagorean
- 37:47So, you can just say x ^ 2 + y ^ 2.
- 37:51You add them up.
- 37:52And then you get the square root of the
- 37:55result.
- 37:57Uh but in Pythagorean theorem, you just
- 37:59have two numbers, but here you can have
- 38:01any amount of dimensions. So, you can
- 38:03have x ^ 2 + y ^ 2 + z ^ 2 + a ^ 2. So,
- 38:08you just add all of the squares of
- 38:10every dimension.
- 38:12And at the end, you take
- 38:15the square root of
- 38:17of that sum.
- 38:19So, let's say we have a vector that has
- 38:21four dimensions.
- 38:24Let's say this is the vector.
- 38:27Uh and then we just square each
- 38:29dimension, sum them up.
- 38:31Let's say it's 30, the square of the sum
- 38:34of all the squares, and then take the
- 38:36root of 30, and that's the length, the
- 38:38magnitude of your vector.
- 38:41So, it's always the square root. It's
- 38:43the It's not like fourth root here, it's
- 38:45square root always.
- 38:48Here are some examples. If we have
- 38:49vector one, one,
- 38:51the magnitude will be 1.41. So, that's
- 38:55the length, the magnitude of the vector.
- 38:57So,
- 38:58you can see here
- 39:00that this is a bit longer than this
- 39:05uh one unit.
- 39:07So, it's about 1.41.
- 39:10Here, the vector zero, five, the
- 39:12magnitude will be five here.
- 39:15You can already see that the length of
- 39:16this is five.
- 39:19So, okay. And then vector three, four,
- 39:22um
- 39:23three, four, the magnitude, the length
- 39:27of the vector will be five units.
- 39:30And if you remember from Pythagorean
- 39:32Pythagorean theorem, if you have
- 39:34triangle with sides three, four, the
- 39:37hypotenuse will be
- 39:39uh five.
- 39:41But what's going to happen often is if
- 39:43you have some vector that has some
- 39:45length, you will want to squish it back
- 39:48so it has unit length, length of one.
- 39:52>> [snorts]
- 39:53>> Uh this is to make vectors in neural
- 39:55networks stable. They don't because you
- 39:57are multiplying a bunch of them so
- 39:59they're going to start exploding. The
- 40:01resulting vectors will become
- 40:02exponentially or quadratically or a lot
- 40:06bigger and bigger and bigger as you
- 40:07multiply them or they will become a lot
- 40:09smaller and smaller and smaller and
- 40:11vanish.
- 40:12So you will want to normalize vectors.
- 40:14You will want to make vectors of unit
- 40:17length, of length one.
- 40:19Let's say we have a vector 3 1.
- 40:23You will find its
- 40:25length, which is going to be 3.16 in
- 40:27this case.
- 40:31And you will just divide this vector
- 40:34with its length, with its magnitude.
- 40:38And when you divide, so
- 40:40dividing it means you will divide each
- 40:42dimension with the magnitude separately.
- 40:45>> [snorts]
- 40:46>> And then this new vector it's going to
- 40:49be unit length so it will have same
- 40:51direction, same direction. It will just
- 40:53have unit length. It will not be
- 40:56it will have be long one, one unit.
- 40:59Whatever the unit is.
- 41:03So neural networks like numbers around
- 41:05one because when you multiply a bunch of
- 41:07those numbers, it will not start to the
- 41:10result will not become very huge or very
- 41:13small, which is not good for graphic
- 41:15cards. They cannot represent very huge
- 41:17numbers or very small uh very big
- 41:19numbers or very small numbers. So you
- 41:21want to keep your numbers around one,
- 41:23zero, minus one, something like that.
- 41:26Now let's understand dot product. So dot
- 41:29product shows you how similar two
- 41:31vectors are.
- 41:33Here on this image, you can see that we
- 41:35have a vector A and vector B.
- 41:38And uh these two vectors are closer
- 41:40together so which means which means they
- 41:42are more similar
- 41:44than here where there is 90° angle
- 41:47between them
- 41:48or here where they are very separate.
- 41:52So these two vectors are most similar.
- 41:54These are less similar. These are least
- 41:56similar.
- 41:57Maybe I should say aligned instead of
- 42:00similar, but uh word similar is also
- 42:02used very frequently, even maybe even
- 42:04more frequently.
- 42:06So it's very simple.
- 42:08We will multiply
- 42:10>> [snorts]
- 42:10>> first dimensions. So here
- 42:13we have a vector, let's say, 3 4 and the
- 42:16second vector is 1 2.
- 42:18To find dot product or similarity
- 42:21uh be- between them,
- 42:23we multiply 3 * 1. So we multiply the
- 42:25first dimensions and then we say plus
- 42:29multiplying second dimensions 4 * 2.
- 42:32Okay? So
- 42:333 * 1 is 3
- 42:35plus
- 42:374 * 2 is 8.
- 42:39So 3 + 8 is 11. So dot product between
- 42:42these two is 11.
- 42:46So if our number is positive, like 11,
- 42:50then they are pointing in the same
- 42:51direction. Okay? If the two vectors are
- 42:54perpendicular, 90° angle between them,
- 42:58then the dot product will be zero. Okay?
- 43:00So if you get the dot product zero, you
- 43:02know that these vectors are
- 43:03perpendicular and this is
- 43:05useful in neural optimizer. Uh this is
- 43:08this may be the new way to make uh
- 43:11neural networks learn a lot faster,
- 43:13better. I'm going to talk about this uh
- 43:15in a different part of the course.
- 43:19So just know that
- 43:21if vectors are 90° between each other,
- 43:24they are they will have dot product
- 43:26zero. And if you have dot product zero,
- 43:28that means the vectors are 90°.
- 43:31And if dot product is negative, then the
- 43:34vectors are even further away than 90°
- 43:37between them.
- 43:39So they are even more different or
- 43:41dissimilar. In Python, we can calculate
- 43:43dot product by just calling np.
- 43:47dot
- 43:48v1 v2, vector one, vector two.
- 43:52So uh vectors
- 43:54v1 and v2 that we calculated manually
- 43:57have dot product of 11.
- 44:00Now these two vectors, 3 4 and minus 4
- 44:023, have dot product of zero, which means
- 44:06they are perpendicular. So
- 44:08v1 and v3, you can see that the angle
- 44:11between them is 90°.
- 44:13So they are perpendicular so dot product
- 44:15will be zero.
- 44:18And then
- 44:20uh v1 v4, so v4 is actually minus 3
- 44:24minus 4, so
- 44:26the blue and the yellow, they are very
- 44:29different
- 44:30because they point in very different
- 44:33directions, opposite directions.
- 44:36So their dot product will be like
- 44:38negative number, minus 25. It's going to
- 44:40be large negative number number here
- 44:42because they are very different.
- 44:46Subtracting vectors is very easy. If you
- 44:49have vector 4 3, you want to subtract
- 44:52vector 1 2, you will just subtract each
- 44:55element
- 44:57from each so 4 minus 1,
- 45:00that's going to be your first element,
- 45:01which is going to be now three.
- 45:03And the second element will be result of
- 45:063 minus 2, which is going to be now one.
- 45:09So the new resulting vector is
- 45:123 1.
- 45:15We can also show that visually.
- 45:18So if we have
- 45:20uh this blue vector, which is 4 3
- 45:24and we have red vector, which is 1 2,
- 45:29uh we can
- 45:32draw a vector from the end of this one
- 45:35to the end of the first one
- 45:37and then move it back to the origin.
- 45:40And that's going to be our new vector,
- 45:42which is going to be uh 3 1,
- 45:45our resulting vector.
- 45:47So you will start from the second vector
- 45:50that you are subtracting uh moving going
- 45:53to the end of first vector from which
- 45:55you are subtracting, that's your new
- 45:57vector. So then you can move it to the
- 45:59origin to see its real coordinates.
- 46:02And that will be it for vectors and see
- 46:05you in the next lesson.
- 46:06Let's talk about gradients. Let's say we
- 46:10have some surface like this.
- 46:12So at any point of this surface,
- 46:15you would want to know
- 46:17where is in which direction to move to
- 46:20go up, in which direction to move to go
- 46:22down.
- 46:23This is used in neural networks to know
- 46:28uh if for example, this surface would
- 46:30show error. So you want to make errors
- 46:33small.
- 46:35So you want to go down on this surface.
- 46:37So how do you
- 46:39in which direction to move to go down on
- 46:41this surface? Now I know if you've never
- 46:43heard of loss function
- 46:45uh and in neural networks, then this is
- 46:46confusing like how is this showing an
- 46:49error, but we will talk about this
- 46:51later. But just know that
- 46:53uh
- 46:54if you have some surface, you're going
- 46:55to use gradients to know which way is
- 46:59you need to move to go up, which way you
- 47:01need to move to go down.
- 47:02Because the gradient will show you the
- 47:05direction of the steepest ascent. So in
- 47:08this direction, if you want to go up
- 47:10fastest.
- 47:12So that means that in the opposite
- 47:14direction, you're going to go down
- 47:15fastest
- 47:16for this particular for this point where
- 47:18you are standing, so very near around
- 47:21that point.
- 47:23So that's what gradients are used for.
- 47:25For every point on the surface, they
- 47:27show you in which direction
- 47:30uh it's ascending, increasing. And so in
- 47:33neural network, we will just go in the
- 47:34opposite direction to decrease because
- 47:36in neural network, it will be used
- 47:39to represent this surface will be used
- 47:41to represent our error,
- 47:43which we want to minimize the error.
- 47:46And gradients are closely related to
- 47:48derivatives. You know that derivatives
- 47:50are showing you the slope.
- 47:52So in gradients, you will also use
- 47:54derivatives to see the slope.
- 47:58But we will use partial derivatives. So
- 48:00let's see what those are.
- 48:02In this case, we have a 2D surface, so
- 48:04two dimensions. We have X dimension and
- 48:06Y dimension.
- 48:08And we want to take partial derivative
- 48:10with respect to X and partial derivative
- 48:13with respect to Y. So what does that
- 48:14mean? So
- 48:16if we stand at any point,
- 48:19if we just move, let's say,
- 48:21on uh on X direction, positive X
- 48:23direction. We increase X 1 2 3 4. Do we
- 48:26go up or down on this surface?
- 48:30So we want to examine if we move
- 48:33on X, so how when we change X,
- 48:36do we go up or down? Are we ascending or
- 48:38descending? So
- 48:40that's going to depend on where we stand
- 48:42at the surface.
- 48:44So we will take partial derivative of
- 48:46this function, this loss function or
- 48:48this surface
- 48:50with respect to X
- 48:53at that particular So we will know So
- 48:55when we plug in our particular X
- 48:58coordinate
- 48:59in which we are currently standing into
- 49:01that partial derivative
- 49:04with respect to X, we will know if
- 49:07for example, if we and when we increase
- 49:09X, if
- 49:11the function will increase or decrease.
- 49:14So that's the partial derivative with
- 49:15respect to X. And it's partial because
- 49:19there is also Y. So we need So we have
- 49:22two partial derivatives with respect to
- 49:23X and with respect to Y.
- 49:25So then for that same position where we
- 49:28calculate
- 49:29if we increase X, does the loss increase
- 49:32or decrease,
- 49:33we will also calculate for Y
- 49:36if we increase Y, does the loss increase
- 49:38or decrease?
- 49:41So, if we find that
- 49:43as we increase Y coordinate, the loss
- 49:46will increase, then we want to
- 49:49go in the opposite direction. We want to
- 49:50move in the Y decreasing direction. So,
- 49:54if let's say this is Y increasing, we
- 49:55want to move in the Y decreasing, so the
- 49:58loss will also
- 49:59decrease as we move.
- 50:02So, that's in the case if we find that
- 50:05partial derivative with respect to Y
- 50:09is such that when we increase Y, the
- 50:10loss is increasing, okay?
- 50:13And it works the other way around.
- 50:16If we find that if we increase Y, the
- 50:19loss is decreasing, then we just want to
- 50:21keep moving in that Y direction.
- 50:25And so
- 50:26the X direction and the Y direction will
- 50:28give us
- 50:30um well, the gradient at that spot will
- 50:34show us
- 50:36the direction of the steepest ascent,
- 50:38steepest increase. So, we will just go
- 50:40in the direction of the steepest
- 50:41decrease, the opposite direction.
- 50:44So, these are gradients. Uh we're going
- 50:45to see some examples to learn more about
- 50:47this. You can also check some YouTube
- 50:49videos. This may take some time, a few
- 50:52iterations, a few
- 50:53times of new understanding this to
- 50:55finally grasp it.
- 50:57Gradient will will just be an array of
- 51:00numbers. And the first number in this
- 51:02array will be partial derivative of the
- 51:04function with respect to X. So, it's
- 51:07just a derivative. So,
- 51:09if we increase X,
- 51:11what's the change in the function?
- 51:13What's the slope? So, it can be 1 2 3 -1
- 51:16or -1.5
- 51:18or whatever the number is.
- 51:21So,
- 51:23we have derivative of the function with
- 51:25respect to X and derivative of the
- 51:26function with respect to Y. And I should
- 51:29say partial derivative.
- 51:32So, that's the gradient.
- 51:35So, if we have a function X squared plus
- 51:38Y squared,
- 51:40uh here these arrows will show you
- 51:45the direction in which it's increasing
- 51:47most from that particular point where
- 51:50you are standing. So,
- 51:52um X squared plus Y squared do look like
- 51:54this. It will look like a ball, like a
- 51:56ball.
- 51:58And so here at the bottom, there will
- 52:00not be too much of a slope. So, these
- 52:03arrows will be small.
- 52:05The magnitude, the length will be small
- 52:07because there is not too much slope. But
- 52:09as you go to the sides, the slope will
- 52:13be higher, larger.
- 52:15And so the magnitude, so each of these
- 52:18vectors will show you how um steep it
- 52:22is. And it will also show you the
- 52:24direction in which it's steep.
- 52:27The longer the vector, the longer the
- 52:29gradient, the steeper it is. And it
- 52:31shows you the direction in which it's
- 52:33steep.
- 52:34So, if you want to move
- 52:37down, then you would go in the opposite
- 52:39direction of this arrow. If you are
- 52:41standing at this place,
- 52:43you want to go down, you want to go
- 52:44opposite.
- 52:47This is why it's called gradient descent
- 52:49in neural networks. You want to go
- 52:51opposite of the gradient.
- 52:53Because gradient shows steepest ascent,
- 52:54you want to go down the loss function.
- 52:58This is what X squared plus Y squared
- 53:00looks like. It's like a ball.
- 53:02Now, let's see how to calculate gradient
- 53:05of a simple linear function 2X plus 3Y.
- 53:08We have two variables. So, first
- 53:12uh in our gradient, the first
- 53:14uh element will be partial derivative of
- 53:16X
- 53:17with respect to the function. So, we
- 53:20will treat Y as a constant.
- 53:23If you remember in derivatives,
- 53:25uh when we treat Y as a constant, then
- 53:27this will be this whole thing will be a
- 53:29constant, so it it will become zero.
- 53:32It doesn't multiply X because right now
- 53:35partial derivative with respect to X,
- 53:38uh
- 53:39will just treat X as the variable.
- 53:41So, now we will just have 2X as the
- 53:44derivative of this.
- 53:45Oh, sorry, sorry, two as a derivative of
- 53:47this.
- 53:49And then
- 53:50when we do partial derivative of the
- 53:52function with respect to Y, then we just
- 53:54treat Y as the variable and X as a
- 53:56constant. So, we have 2X, that's a
- 53:58constant. That whole thing is a
- 53:59constant, it becomes zero.
- 54:01So, derivative of 3Y will just become
- 54:04three.
- 54:06So, here we have partial derivative
- 54:09of a function with respect to X is going
- 54:12to be two, and partial derivative of the
- 54:13function with respect to Y is going to
- 54:15be three.
- 54:17So, you just gradients
- 54:18and derivatives.
- 54:20In this case, we don't have any
- 54:22variables in the gradient. So, we just
- 54:25have constant two and three, which means
- 54:27for this function, the gradients will
- 54:29always be same.
- 54:31It doesn't depend on X or Y. The
- 54:33gradient is always the same. So, these
- 54:35arrows are always going to be the same.
- 54:36They will always This function will
- 54:38always rise in this way. It's like a
- 54:42plane.
- 54:43It's always the same, always rising in
- 54:45this way for the same amount. It's like
- 54:47a plane.
- 54:49But if we have maybe a curvy surface
- 54:51like this, so this surface is X squared
- 54:54plus Y squared, we will notice that at
- 54:57the bottom,
- 54:58uh it's a bit flat, so there is no so
- 55:00much
- 55:01a
- 55:02rising or ascending or ascent.
- 55:05So, the vectors,
- 55:08gradient vectors will be smaller.
- 55:10But as we
- 55:11go up,
- 55:12these values will grow quadratically.
- 55:15They will grow faster and faster. So,
- 55:17as we increase X and Y values,
- 55:21the slope will be
- 55:23uh bigger and bigger and bigger.
- 55:26So, these gradient arrows will become
- 55:28larger and larger.
- 55:30As I said, the gradient will uh show the
- 55:34direction of steepest ascent. So, if we
- 55:36have this function X squared plus Y
- 55:38squared,
- 55:40if we find
- 55:42uh first of all, the gradient,
- 55:44there is going to be 2X,
- 55:47{comma} 2Y.
- 55:49So, gradient partial derivative with
- 55:51respect to X will be 2X. Partial
- 55:53derivative with respect to Y will be 2Y.
- 55:56So, our gradient
- 55:57for this function will be 2X, {comma}
- 56:002Y.
- 56:01I can show you here. The function is X
- 56:04squared plus Y squared. This is how you
- 56:07uh can write squared in Python.
- 56:10The gradient will be just 2X and 2Y.
- 56:13So,
- 56:15for
- 56:16let's uh
- 56:17see some point like point 2 1. So, X is
- 56:21two, Y is one.
- 56:23We're going to get 2 * X is four. Uh 2 *
- 56:26Y is
- 56:27uh two. 2 * 1 is two, so
- 56:31our gradient at point 2 1 will be 4 2.
- 56:37Uh 4 2 will be our arrow right here, but
- 56:40it's not shown fully.
- 56:43But anyways, that's it points to the
- 56:45steepest ascent, so we want to take the
- 56:47opposite arrow
- 56:48to point it at the descent, where how to
- 56:51go down the function.
- 56:53Now, let's learn about matrices. This is
- 56:55extremely important. This is how neural
- 56:57networks work. This is the
- 57:00main thing about AI neural networks. So,
- 57:02this is a matrix.
- 57:04It's an array of arrays, okay? So, you
- 57:06can look at this first row as one array.
- 57:10So, 1 2. This is the one array.
- 57:12And then second array is 3 5. So, that's
- 57:15one matrix.
- 57:17So, each of these numbers has its index.
- 57:20So,
- 57:22starting from zero. So, this is the
- 57:23zeroth row.
- 57:25So, the first row is actually has has
- 57:27index zero, just like in normal
- 57:28programming.
- 57:29>> [snorts]
- 57:30>> This is a row with index one, okay?
- 57:32Column with index zero, column with
- 57:34index one.
- 57:36And so this uh element has index zero
- 57:39zero.
- 57:42And then
- 57:43uh this has zero one, one zero,
- 57:46because this is
- 57:47a row. So, first is row, row one, and
- 57:49then column zero. So, one zero, and then
- 57:52one one.
- 57:53So, if you want to add two matrices,
- 57:55you just add each element, corresponding
- 57:58elements at the same
- 58:00s- index. So, 1 + 2 is three.
- 58:042 + 5 is seven.
- 58:07Et cetera. This is how addition works.
- 58:10Matrix multiplication is the most
- 58:12important operation in neural networks
- 58:13in uh machine learning in AI.
- 58:16So, you have two matrices. First of all,
- 58:20this number of columns here, three, must
- 58:22be same as the number of rows
- 58:25here, three. So, this three and this
- 58:26three must be same.
- 58:28So, we have one two three columns, one
- 58:30two three rows.
- 58:31If you swap these places,
- 58:34uh you will get a completely different
- 58:36resulting matrix. So, you cannot swap
- 58:39places of matrices and get same result.
- 58:40You will not. Those are That's a
- 58:42different operation.
- 58:44Uh this is how you multiply two
- 58:46matrices. So, you say you do a dot
- 58:49product between this row and this
- 58:51column. And they must be same length.
- 58:54So, 3 * 0
- 58:57plus
- 58:581 * 2. This is the classic dot product.
- 59:00We talked about this earlier in the
- 59:02video.
- 59:03Plus 0 * 0. Okay? That's the formula
- 59:07here, two. So, that's
- 59:09that number two goes at the
- 59:11intersection.
- 59:13So, it's going to be
- 59:15uh first uh row and first column. So,
- 59:18first row, first column here.
- 59:20That's this dot product result.
- 59:23And then you will go for the second one,
- 59:25you will go so go first uh column,
- 59:29sorry, first row and second column here.
- 59:33So, you go 3 * 4 + 1 * 2 + 0 * 1 and
- 59:39result goes here.
- 59:41So, you just put the result at the
- 59:43intersection. So, first row, second
- 59:46column. First row, second column.
- 59:49And at the end, you will get a matrix
- 59:51that's that's
- 59:53size of these outside dimensions. So, 2
- 59:56* 2 because
- 59:58this middle dimension will cancel out.
- 1:00:01And this middle dimension must be same,
- 1:00:03equal. And these outside dimension
- 1:00:06outside doesn't need to doesn't have to
- 1:00:08be same.
- 1:00:11So, if this was 2 * 3
- 1:00:13this matrix and this matrix was
- 1:00:15uh three rows and four columns
- 1:00:18then
- 1:00:20it's possible to multiply because three
- 1:00:22and three inner dimensions are equal and
- 1:00:25the resulting would be 2 * 4 if this was
- 1:00:28four.
- 1:00:30In Python, you would define matrix as
- 1:00:34array of arrays.
- 1:00:35So, this is the first row. This is the
- 1:00:38second row.
- 1:00:41Uh here it's being indexed as 1 1. So,
- 1:00:44here indices are starting from one.
- 1:00:46Maybe to show like example to for you to
- 1:00:49understand it easily, but usually
- 1:00:51indices start from 0 0.
- 1:00:54First row, second row.
- 1:00:57Just like here. So, this is the first
- 1:00:59row.
- 1:01:00This is the second row.
- 1:01:01And this has two columns as well. So,
- 1:01:03two rows, two columns.
- 1:01:05And so, that's how we can define matrix
- 1:01:07in
- 1:01:08Python with for example NumPy. We can
- 1:01:10also do it with PyTorch and stuff.
- 1:01:14And this is example of 3 by 2 matrix.
- 1:01:16So, three rows, 1 2 3 and two columns.
- 1:01:22Now, these rows and columns are going to
- 1:01:24take some time for you to understand
- 1:01:25intuitively quickly
- 1:01:27what's happening. Like 2 by 3 matrix.
- 1:01:30Two rows
- 1:01:32and three columns.
- 1:01:34So, we can print shape of the matrix.
- 1:01:38So, A matrix
- 1:01:40is this one. So, shape here is 2 by 2.
- 1:01:45And then we can print shape of the first
- 1:01:46row and shape of the first
- 1:01:49column.
- 1:01:53So, let's see.
- 1:01:54Uh shape is 2 by 2. Rows two and columns
- 1:01:57two.
- 1:01:58Uh it's maybe easier to understand on
- 1:02:00this example because here rows and
- 1:02:02columns are different uh shapes. So,
- 1:02:05shape is 2 by 3. Rows two, columns
- 1:02:07three.
- 1:02:10So, if you say uh print
- 1:02:14A.shape
- 1:02:160, that's going to be
- 1:02:19the first number in your shape. So, it's
- 1:02:21going to be this number, two.
- 1:02:24And shape one of one with index one will
- 1:02:27be the second number, three.
- 1:02:32Uh maybe you want to play with this a
- 1:02:33little bit to see what happens. So, this
- 1:02:36is going to show the number of columns.
- 1:02:37This is going to show number of rows.
- 1:02:41And so, if you have a matrix 1 2 3 4 5
- 1:02:446, you can access individual elements by
- 1:02:47just calling the index. So, this index
- 1:02:49of this will be 0 0.
- 1:02:52So, if you say A
- 1:02:54of 0 0, that will give you this value.
- 1:02:58Which is in this case, the value is one.
- 1:03:00If I put it like this, it's going to be
- 1:03:0210.
- 1:03:03So, this value
- 1:03:05has index 0 0.
- 1:03:10And that's how you can access
- 1:03:12uh the elements.
- 1:03:15I'm actually going to talk more about
- 1:03:16this indexing and slicing later. It's
- 1:03:19more for maybe PyTorch tutorial.
- 1:03:23So, let's go on to matrix addition. I
- 1:03:25already explained this. So, here you
- 1:03:28would just add each element with on
- 1:03:30corresponding on the same index. So, 1 +
- 1:03:325 is going to be six. The resulting
- 1:03:36will be six.
- 1:03:38And then 2 + 6 result will be eight. So,
- 1:03:41same index.
- 1:03:44Uh scalar multiplication, you just
- 1:03:46multiply.
- 1:03:48So, if you say 2 * matrix A, you will
- 1:03:50just multiply each of these elements
- 1:03:52with two.
- 1:03:54So, this will become 2 4 6 8.
- 1:03:59So, let's see the result here.
- 1:04:012 * A, you will just multiply with two
- 1:04:03or 0.5 * A, you will just multiply each
- 1:04:07element with 0.5.
- 1:04:110.5 1 1.5 2.
- 1:04:14-1 same. Just each element with -1.
- 1:04:17Matrix vector vector multiplication is
- 1:04:20just like matrix multiplication.
- 1:04:23So, you first need to make sure that the
- 1:04:24inner dimension
- 1:04:26is same. So, 2 * 3. So, this three must
- 1:04:29be same as this three, 3 * 1.
- 1:04:32In this case, you can multiply
- 1:04:34A matrix multiplied vector V, but you
- 1:04:38cannot multiply vector V matrix multiply
- 1:04:40vector A because then this inner
- 1:04:42dimension would not be same.
- 1:04:47So, you can first put this first and
- 1:04:49then this second and then that's how you
- 1:04:52can matrix multiply it.
- 1:04:54And then the result will be
- 1:04:562 * 1 because the inner same dimension
- 1:04:58will cancel out.
- 1:05:01And the step-by-step calculation goes
- 1:05:03like this.
- 1:05:041 * 2 + 2 * 3 + 3 * 4.
- 1:05:10And then that's going to be
- 1:05:1220. And then 4 * 2 + 5 * 3 + 6 * 4
- 1:05:18that's going to be 47. So, that's the
- 1:05:19resulting vector.
- 1:05:21So, you multiply this row with this row.
- 1:05:24Now, this could be a bit confusing
- 1:05:26because I was showing you that you
- 1:05:28multiply row and a column.
- 1:05:31Uh but depending on how you do it, how
- 1:05:34you code it, how you imagine it
- 1:05:37this can also be done this way.
- 1:05:39Because this is just a vector.
- 1:05:41So, in matrix multiplication, the way we
- 1:05:43imagine it conceptually is row * column.
- 1:05:46But since this is just a vector
- 1:05:49we can also imagine this as a column
- 1:05:51vector being 2 3 4. And then this this
- 1:05:54row and this column vector.
- 1:05:57So, this is a bit confusing. You're
- 1:05:58going to have to do this by yourself by
- 1:06:00hand. Try to multiply. Try to figure
- 1:06:02out. Try to get intuition.
- 1:06:05Try multiplying this. Try multiplying
- 1:06:07others maybe
- 1:06:08uh get some other examples from AI from
- 1:06:10ChatGPT.
- 1:06:12So, the matrix multiplication example
- 1:06:14here
- 1:06:15I explained it. So, you can use this at
- 1:06:17as
- 1:06:18uh matmul or you can use np.dot which is
- 1:06:22a bit confusing because this implies dot
- 1:06:24product, but this is actually
- 1:06:26this is matmul.
- 1:06:30And so, here you have step-by-step on
- 1:06:31how to multiply two matrices, but this
- 1:06:33is just something you would need to
- 1:06:35understand. And I saw Sam Altman uh
- 1:06:38explaining something about matrices and
- 1:06:40he was showing this with his uh arms or
- 1:06:44hands. So, that's how I know that he
- 1:06:46also knows how to multiply matrices. I
- 1:06:48was wondering if it was in his job
- 1:06:50description as CEO of OpenAI to be able
- 1:06:54to multiply matrices. I was always
- 1:06:56wondering that.
- 1:06:58This is uh transpose matrix. So, we have
- 1:07:011 2 3 4 5 6. We transpose that, it
- 1:07:03becomes So, we swap rows and columns.
- 1:07:06So, it's now this is this column now
- 1:07:09became the row.
- 1:07:11So, 1 4 now became the row. 2 5 now
- 1:07:13became the row.
- 1:07:15And the shape is swapped now.
- 1:07:18Then we have identity matrix
- 1:07:21where it's one on the diagonal
- 1:07:24and others are zero.
- 1:07:26And it's a square matrix. It has equal
- 1:07:29number of rows and columns.
- 1:07:32And when you multiply any matrix with
- 1:07:34identity matrix
- 1:07:35it will remain the same matrix. So, it
- 1:07:39the result will be the same as this
- 1:07:41matrix that you multiplied with the
- 1:07:43identity matrix.
- 1:07:45And in Python, you can create identity
- 1:07:47matrix by saying np.i
- 1:07:50and then how many rows and columns. So,
- 1:07:53two or three.
- 1:07:54Or whatever the number is. So, this is
- 1:07:56the two. This is 3 by 3. It can be any
- 1:07:59number.
- 1:08:01So, identity matrix acts like the number
- 1:08:04one in multiplication. So, it doesn't
- 1:08:05change the thing it's multiplying.
- 1:08:08Here are some properties. You can
- 1:08:11analyze them if you want. I just want
- 1:08:13you to understand that in matrix
- 1:08:15multiplication, you cannot swap the
- 1:08:18order of matrices. This is not a
- 1:08:20generalizable. It's not true in general.
- 1:08:22There might be specific cases when it's
- 1:08:24true, but in general, it's not true.
- 1:08:27That's going to be it on matrices. If
- 1:08:29you understand this, you understand
- 1:08:32um everything you need to know about
- 1:08:33matrices as your fundamentals. So, let's
- 1:08:36uh check out the last lesson as well.
- 1:08:39Lastly, let's learn about probability.
- 1:08:41This is extremely important. It's used
- 1:08:43in large language models, reinforcement
- 1:08:44learning,
- 1:08:45uh computer vision, natural language
- 1:08:47processing, everywhere.
- 1:08:49>> [snorts]
- 1:08:49>> So, you have a coin.
- 1:08:51Let's assume it's a fair coin. You flip
- 1:08:54it, you have 50% chance to get heads,
- 1:08:5650% chance to get tails.
- 1:08:59Or you roll a dice, six sides. So, you
- 1:09:01have 1 over 6
- 1:09:04chance
- 1:09:05for any particular side
- 1:09:07to appear, to be rolled.
- 1:09:10Probability measures how likely an event
- 1:09:12is to occur. Zero means impossible, 0%
- 1:09:16chance probability. One means certain,
- 1:09:19100% chance of probability, always
- 1:09:21happens.
- 1:09:23And 0.5 means 50/50.
- 1:09:26So, let's calculate the probability of
- 1:09:28flipping a coin and getting heads.
- 1:09:31So, we have two possible outcomes, heads
- 1:09:33and tails.
- 1:09:35And but we are calculating probability
- 1:09:38for just one of those outcome, heads.
- 1:09:41So, we say
- 1:09:44uh
- 1:09:44heads outcomes can be just one of those
- 1:09:47two divided by the total outcomes, which
- 1:09:49is two.
- 1:09:51Which is going to give us
- 1:09:52uh 1/2 or 0.5 or 50%.
- 1:09:57So, for rolling a dice, it's same. We
- 1:09:59just have 1/6 now because one side out
- 1:10:03of six possible.
- 1:10:06But in this case, we are assuming that
- 1:10:08each of these six sides has equal
- 1:10:11probability to happen.
- 1:10:13So, then you can divide
- 1:10:16uh 1/6
- 1:10:18to get your probability.
- 1:10:20Same goes for heads. If each of the uh
- 1:10:23heads or tails has equal probability,
- 1:10:26then you can divide.
- 1:10:29Here, if we flip the coin 10 times, we
- 1:10:32get seven times head, uh three times
- 1:10:35tails. So,
- 1:10:37theoretical probability is 0.5, but
- 1:10:39actual probability measured, I should
- 1:10:41say, is 0.7 or 70%. But as we flip more
- 1:10:46coins,
- 1:10:47the measured probability will get closer
- 1:10:49and closer to theoretical. So, for 100
- 1:10:52coins, we get 0.39.
- 1:10:56And for 10,000 coins, we get 0. uh 5024.
- 1:11:01So, this one experimental is very close
- 1:11:04to the theoretical.
- 1:11:07So, the more flips or more samples,
- 1:11:10uh the experiment probability measured
- 1:11:13probability will get closer to the
- 1:11:15theoretical.
- 1:11:16And so, if we try that with dices, we
- 1:11:18will get uh over with 1,000 rolls, we
- 1:11:21will get
- 1:11:22close to the theoretical of 0.1667.
- 1:11:28And here, the orange one shows
- 1:11:30theoretical probability of each uh dice
- 1:11:34face, and it's always the same. But the
- 1:11:37measured one,
- 1:11:38it's going to be a bit different.
- 1:11:42But if we do a lot more measurings, then
- 1:11:45uh this measured one will get closer and
- 1:11:46closer to the theoretical.
- 1:11:49Now, what is the chance that if we roll
- 1:11:51two dices, one dice
- 1:11:54has uh gets give us one, and the second
- 1:11:57dice gives us
- 1:11:59six. So, it doesn't matter which dice
- 1:12:01gives which number. We just need to get
- 1:12:03one and six.
- 1:12:04And the probability there would be by
- 1:12:06multiplying those two probabilities.
- 1:12:08So, it's going to be a lot smaller
- 1:12:10probability to get both of them.
- 1:12:14Now, what is the chance that in those
- 1:12:16two dices, we get one or six, but not
- 1:12:21both of them, just either one or six.
- 1:12:24So, it can be one on the first dice or
- 1:12:27one on the second dice or six on the
- 1:12:30first dice or six on the second dice.
- 1:12:32So, there are four possible ways to
- 1:12:34satisfy this, but if we get one and six,
- 1:12:37that's not what we want. So,
- 1:12:39So,
- 1:12:41we will subtract the probability of
- 1:12:44getting one and six because that's not
- 1:12:47what we're looking for.
- 1:12:49And so, the probability of getting one
- 1:12:51is this, and probability of getting six
- 1:12:53is the same. And then subtract if we get
- 1:12:56both one and six. And this is the
- 1:12:58probability that we get one or six if we
- 1:13:01roll two dices.
- 1:13:04Here, we have probability distribution.
- 1:13:06So,
- 1:13:07this is the task. If we roll two dices,
- 1:13:11uh what is the probability that the sum
- 1:13:14of the numbers of each dices, like we
- 1:13:17get one and six,
- 1:13:19so the sum is seven.
- 1:13:21So, what is the probability of each of
- 1:13:23these sums?
- 1:13:25For example,
- 1:13:27if the sum is two, it means we got one
- 1:13:29and one.
- 1:13:31If the sum is seven, it means we got
- 1:13:34three and four and four and three and
- 1:13:36two five, five two, one six, six one.
- 1:13:39So, you can see that
- 1:13:41we can get seven in a lot more different
- 1:13:43ways
- 1:13:45than for example two because we can get
- 1:13:46two as a sum if only if
- 1:13:49both of the
- 1:13:50uh both of the dices are one.
- 1:13:53But seven, we can get
- 1:13:55in a lot more different ways. So,
- 1:13:57probability of the sum being seven is a
- 1:13:59lot higher.
- 1:14:01And that's the probability distribution
- 1:14:03over all of these uh sums.
- 1:14:07So, what is the expected value of a dice
- 1:14:11roll?
- 1:14:12Uh we know that dice can show us numbers
- 1:14:161 2 3 4 5 6.
- 1:14:19And each of these numbers has 1/6 chance
- 1:14:22to happen.
- 1:14:23So, what is the average or expected
- 1:14:26number we will get by just rolling a
- 1:14:28bunch of times?
- 1:14:31You can maybe intuitively understand
- 1:14:33that the average number will be 3.5
- 1:14:36because this is exactly in the middle of
- 1:14:38one and six.
- 1:14:41Uh but we can calculate expected value
- 1:14:46uh by multiplying the value we will get,
- 1:14:49so one two three four, times its
- 1:14:52probability.
- 1:14:541/6 is the probability for each of these
- 1:14:56values.
- 1:14:58And then sum up.
- 1:15:00So, sum up and divide by the number
- 1:15:05to make to do the to find the average or
- 1:15:08mean.
- 1:15:09But this is very important. This summing
- 1:15:12up of all of the values and dividing by
- 1:15:14the number
- 1:15:16is just because every value has the
- 1:15:18equal probability to happen.
- 1:15:21That's why we can pull them into a
- 1:15:23bracket and then divide by this common
- 1:15:25denominator is the same for every one.
- 1:15:28But [snorts] if these probabilities were
- 1:15:30different,
- 1:15:32uh not all are like same 1/6, then we
- 1:15:34would not be able to like sum up the
- 1:15:36values and divide by six.
- 1:15:38So, summing up and dividing only works
- 1:15:40if the probabilities are same.
- 1:15:43So, that's like a shortcut you can do.
- 1:15:47But this is more general formula.
- 1:15:49You just do each value times its
- 1:15:51probability.
- 1:15:52And that's
- 1:15:54but this is uh more specific. This works
- 1:15:57if probabilities are same.
- 1:15:59Then we have conditional probability.
- 1:16:01So,
- 1:16:02probability of A happening given that B
- 1:16:06happened.
- 1:16:08Uh you can calculate this by this
- 1:16:11formula. So, probability of A and B
- 1:16:14divided by probability of B.
- 1:16:16It's easier to understand this in an
- 1:16:18example.
- 1:16:20So, given that we roll an even number,
- 1:16:24what's the probability that that number
- 1:16:26is six?
- 1:16:29So, first of all, probability to roll an
- 1:16:31even number is 0.5 because we have three
- 1:16:34odd, three even numbers on a dice.
- 1:16:38And probability
- 1:16:40of rolling six and even. So, you need to
- 1:16:43think a little bit. So, if you roll six,
- 1:16:45it will also be even.
- 1:16:48So, here we're just going to use the
- 1:16:49probability of rolling six because if we
- 1:16:52roll six, it will also be even. So,
- 1:16:55that's the probability.
- 1:16:57And so, the formula,
- 1:17:00uh we intuitively know that
- 1:17:05let's not let's first look at intuition.
- 1:17:07So, we have three of these. So, if we
- 1:17:10roll a even number,
- 1:17:12then because each of these even numbers
- 1:17:14has same probability,
- 1:17:17then
- 1:17:18it's 1/3, 1/3, and 1/3 to be each of
- 1:17:21these numbers.
- 1:17:23Because they have same probability.
- 1:17:25So, that's what we will get in the
- 1:17:27result, 1/3 or 0.03 0.3333.
- 1:17:32So, and if we check the formula, it will
- 1:17:35actually
- 1:17:36come out as 1/3.
- 1:17:39So, the prob so,
- 1:17:41the probability of getting six given
- 1:17:44that we got even number is 1/3.
- 1:17:49And this is the law of large numbers.
- 1:17:52So, uh if you have probability
- 1:17:54theoretical probability is 0.5, you're
- 1:17:57flipping the coin.
- 1:17:58In the beginning where you just flip
- 1:18:00maybe 10 times, maybe you will actually
- 1:18:03get seven heads and three tails.
- 1:18:05But as you flip more more more more more
- 1:18:07more,
- 1:18:08the measured probability will converge
- 1:18:11towards the theoretical probability. So,
- 1:18:14that's the law of large numbers. If you
- 1:18:16have large numbers, they will converge
- 1:18:17towards theoretical probability.
- 1:18:20Now, this is also interesting. Let's say
- 1:18:22you are tossing a coin 10 times.
- 1:18:26What's the probability that out of those
- 1:18:2910 times,
- 1:18:32you get five heads?
- 1:18:35I actually recommend you go to my
- 1:18:37channel, and here in playlists, you
- 1:18:40check out this uh become AI researcher
- 1:18:42probability because this is a playlist
- 1:18:44of going into probability a lot more in
- 1:18:47a lot more details. And you can check
- 1:18:49just first video or the first and sec
- 1:18:51and second. Those contain most important
- 1:18:53things.
- 1:18:55So, random variable, standard deviation,
- 1:18:57variance.
- 1:18:59And then continuous random variable
- 1:19:00probability density function. So, maybe
- 1:19:03uh these two first videos you can check.
- 1:19:06That's going to be it on this part on
- 1:19:08maths. You can join my school to become
- 1:19:11AI researcher uh link below the video or
- 1:19:14you can find it on my channel.
- 1:19:17Here, on my channel.
- 1:19:19Get a 7-day a trial if you join right
- 1:19:21now.
- 1:19:22In this lesson, we will learn about
- 1:19:24PyTorch necessary for AI research. After
- 1:19:28this short course, you will be able to
- 1:19:30understand and code any neural network,
- 1:19:32any AI research in PyTorch.
- 1:19:35So, I presume basic understanding of
- 1:19:37Python. You can learn that on YouTube if
- 1:19:39you never use Python.
- 1:19:42So, here I'm going to go into
- 1:19:44extensions. I am on VS Code or Cursor,
- 1:19:47and I'm just going to say Colab, and I'm
- 1:19:49going to install this Google Colab. If I
- 1:19:52want to run my code on Google Colab from
- 1:19:56this local environment.
- 1:19:57You can also open these notebooks on
- 1:20:00Google Colab or
- 1:20:02you can just
- 1:20:03run them locally, but here I'm going to
- 1:20:06run on Google Colab from my local
- 1:20:08environment, but you can have whatever
- 1:20:09setup you want.
- 1:20:11So, I will choose select another kernel
- 1:20:14here, so I can click here Colab or
- 1:20:17select another kernel and Colab.
- 1:20:20And then I can start a new Colab server.
- 1:20:23I'm going to choose
- 1:20:25CPU and just press enter for the So,
- 1:20:27here I'm just going to use Google's CPU.
- 1:20:32And then I'm going to choose this Python
- 1:20:34kernel.
- 1:20:36And let's see, it should work.
- 1:20:40And it works. I chose CPU because I can
- 1:20:43use it for longer than GPU.
- 1:20:45And if you don't have PyTorch installed,
- 1:20:48like you run this and says no module
- 1:20:50named torch, then you would just install
- 1:20:54So, you would pip install torch in your
- 1:20:57console, in your CMD, right here.
- 1:21:03Pip install torch or in the Google Colab
- 1:21:06or wherever.
- 1:21:07So, let's learn about tensors.
- 1:21:10So, you can create a tensor.
- 1:21:13Tensor is like an array. It can also be
- 1:21:17array of arrays, or it can be array of
- 1:21:19arrays of arrays. So, any number of
- 1:21:22nested arrays within arrays.
- 1:21:25So, here we have a 2D tensor.
- 1:21:30I'm going to zoom in. So,
- 1:21:32it's like a matrix. So, this is the
- 1:21:34first row. This is the second row. And
- 1:21:37the data type is float 32 here.
- 1:21:41So, this is from Python.
- 1:21:43You can self-study what this is. This is
- 1:21:45from Python. So, this is kind of
- 1:21:48prerequisite, but if you don't know, you
- 1:21:49can ask ChatGPT all of these things that
- 1:21:52I skip.
- 1:21:53So,
- 1:21:55if I go ahead and print all of this,
- 1:21:58this is the tensor that we have. This is
- 1:21:59its shape. So, we have two rows,
- 1:22:03and
- 1:22:04each row has
- 1:22:05three columns or So, 1 2 3. 1 2 3. So,
- 1:22:09there is three columns in total.
- 1:22:12So, two rows, three columns.
- 1:22:15We can flatten this tensor, which means
- 1:22:17we will just convert it into a single
- 1:22:22And so, it's going to It's going to go
- 1:22:23like this. 1 2 3 4 5 6. So, it will just
- 1:22:25put rows next to each other, and it's
- 1:22:27going to be a single array. It will not
- 1:22:29be two arrays like here.
- 1:22:32So, the shape is now just six because
- 1:22:34there is six numbers in this one array.
- 1:22:39And this flattening is done with
- 1:22:41tensor.flatten, and T
- 1:22:43is our tensor, remember. So, T is our
- 1:22:45tensor.
- 1:22:48And then we can also specify this
- 1:22:52start dimension to be zero.
- 1:22:55In this case, this will look the same as
- 1:22:59without specifying this start dimension
- 1:23:01zero.
- 1:23:03For me to explain what this start
- 1:23:05dimension does, let's look at a
- 1:23:08different tensor. It's a 3D tensor, so
- 1:23:10there are It's 2 2 2. So,
- 1:23:13inner arrays have two elements. There is
- 1:23:15two of these.
- 1:23:17And then there is two of these outside.
- 1:23:19So, it's 2 2 2.
- 1:23:21So, if I say flatten, but
- 1:23:25start dimension is equal to one.
- 1:23:28So, we will actually preserve the
- 1:23:30dimension zero. We will preserve
- 1:23:31dimension zero, and we will start
- 1:23:33flattening from dimension one. So,
- 1:23:35So, what that means is
- 1:23:38we will not have one big single array.
- 1:23:42Instead, we will preserve these outside
- 1:23:45these most like outer
- 1:23:48two elements.
- 1:23:50And then we will flatten within them.
- 1:23:52So, we will flatten within these outer
- 1:23:54elements. So, it will be 1 2 3 4.
- 1:23:58Because we are preserving dimension
- 1:23:59zero, and we are starting from dimension
- 1:24:01one.
- 1:24:02So, you can play around with this and
- 1:24:04try to
- 1:24:06change it and understand what's
- 1:24:07happening here.
- 1:24:09Try to make bigger tensors. Try to
- 1:24:11put some other dimensions.
- 1:24:13Now, let's see reshaping. So, we have a
- 1:24:15tensor 1 2 3 4 5 6 7 8.
- 1:24:19And the shape here is going to be two
- 1:24:21rows
- 1:24:23and four
- 1:24:24columns. So, 2 4.
- 1:24:27And so, we want to reshape for it to be
- 1:24:304 2 instead.
- 1:24:33So, let's see
- 1:24:34what it's going to do. It's going to
- 1:24:35group like this. 1 2
- 1:24:373 4 5 6. So, now we're going to have
- 1:24:40four rows
- 1:24:42and two columns.
- 1:24:44And it's going to just start from the
- 1:24:45beginning.
- 1:24:49So, it's going to group these two, these
- 1:24:50two,
- 1:24:51these two, these two.
- 1:24:55Also, if we have a tensor 2 by 3 by 4.
- 1:24:58Now, I want you to understand what here
- 1:25:01is two, what here is three with what
- 1:25:03here is four.
- 1:25:04So, you need to like think about it.
- 1:25:07So, this is four, and then there is
- 1:25:09three of them,
- 1:25:10and then there is one two of these big
- 1:25:13ones. So, you need to understand this.
- 1:25:16And we want to reshape this tensor to 6
- 1:25:184.
- 1:25:19What is that going to do? It's going to
- 1:25:21create six
- 1:25:23rows,
- 1:25:25each row having four columns or four
- 1:25:27elements. So, four columns.
- 1:25:31So, in that case,
- 1:25:33this four will be preserved,
- 1:25:36and these two and three will be merged
- 1:25:39into six. And you see that
- 1:25:42you have to have equal number of
- 1:25:44elements at the end. So, 2 * 3 * 4 must
- 1:25:47be equal to
- 1:25:486 * 4.
- 1:25:51So, you can play with this and try to
- 1:25:54understand what's happening, and how it
- 1:25:55works if you change it a little bit.
- 1:25:58Now, there is also view, which is
- 1:26:00similar to reshape,
- 1:26:02but I'll show the difference. So, if we
- 1:26:05have a tensor like this,
- 1:26:07we say we want to reshape that to have
- 1:26:09four rows and two columns instead.
- 1:26:12So, this will group 1 2, and then 3 4,
- 1:26:16and then 5 6, and 7 8.
- 1:26:20Like this.
- 1:26:22So, this is interesting. So, we created
- 1:26:24a new tensor that's going to be T view.
- 1:26:28So, we have original tensor T, and T
- 1:26:30view is the new tensor.
- 1:26:32But if we change T view, it will also
- 1:26:34change original tensor because
- 1:26:37T view is actually using the same
- 1:26:38elements, the same memory slots in
- 1:26:40memory
- 1:26:41as this original tensor. So, when we
- 1:26:44change T view, we will also change this
- 1:26:46original one. So, keep that in mind.
- 1:26:48That can be useful for some situations.
- 1:26:50So,
- 1:26:53we see that the first element is one of
- 1:26:55both of them.
- 1:26:58And then we're going to just modify T
- 1:27:00view at 99.
- 1:27:02And then the original first element now
- 1:27:05also becomes 99. So, now all of them are
- 1:27:0899.
- 1:27:12So, view requires tensor to be
- 1:27:15contiguous in memory means all of the
- 1:27:18elements need to be next to each other
- 1:27:20in memory.
- 1:27:21Reshape will work even if the tensor is
- 1:27:24not contiguous, whether or not it's
- 1:27:26contiguous. So, whether or not
- 1:27:28it's all of the elements are next to
- 1:27:29each other in memory or not.
- 1:27:32And it will create a copy of the tensor
- 1:27:34if necessary
- 1:27:36if it's not next to each other.
- 1:27:39Join my school to become AI researcher.
- 1:27:42We have all of these courses that will
- 1:27:43lead you step by step. Get 7-day free
- 1:27:45trial if you join right now. Let's check
- 1:27:48squeezing.
- 1:27:50So, we have a tensor
- 1:27:52that has elements 1 2 3, but there is
- 1:27:54another dimension around it, and another
- 1:27:56dimension around it.
- 1:27:58So, the shape of this tensor it's going
- 1:28:01to be
- 1:28:021 1 3 because the inner array has three
- 1:28:06elements, and then there is and then
- 1:28:07that array is an element of the outer
- 1:28:10array, and then that array is an element
- 1:28:12of the outer outer array.
- 1:28:15You see here.
- 1:28:17And so, we can use this command
- 1:28:20T.squeeze.
- 1:28:23So, now we're going to create a new
- 1:28:25tensor T squeeze is equal to T.squeeze.
- 1:28:31And this will remove all of these outer
- 1:28:34dimensions that have just dimension one.
- 1:28:39So, now we will just be left with this
- 1:28:41tensor, or this array I should say,
- 1:28:45or whatever. It can be also tensor. But
- 1:28:47the outer dimensions that
- 1:28:50have one element will be removed.
- 1:28:54That's what squeezing does.
- 1:28:56We can also specify at which dimension
- 1:28:58we want to squeeze. So,
- 1:29:01if we look at this tensor, and can you
- 1:29:03tell me what the shape of this tensor
- 1:29:05is? Because
- 1:29:07there is three, and then but three is an
- 1:29:09element within an array.
- 1:29:11And then there is three of those
- 1:29:13elements, or I should say three of those
- 1:29:15arrays.
- 1:29:18And then there is this outside.
- 1:29:23So, the shape of this is 1 3 1.
- 1:29:26Because uh
- 1:29:28there is three of these and each of them
- 1:29:30has one element inside and there is like
- 1:29:32one outside dimension.
- 1:29:35So, if I [snorts] say I want to squeeze
- 1:29:37a dimension zero, this will remove this
- 1:29:39outermost dimension.
- 1:29:41So, let's see what happens.
- 1:29:46Now,
- 1:29:47this has removed this outermost
- 1:29:49dimension
- 1:29:51and so we just have one array
- 1:29:53of three of these arrays, small arrays,
- 1:29:56one element arrays.
- 1:29:59So, you can play with this and
- 1:30:00understand how this works.
- 1:30:02I can also squeeze a dimension two. So,
- 1:30:04t2.squeeze and remember t2 is our
- 1:30:07original array. So, this is not our
- 1:30:10squeeze. I'm not squeezing already
- 1:30:12squeezed array. I'm squeezing now the
- 1:30:14original array again or I should say
- 1:30:16tensor.
- 1:30:19So, squeeze the dimension two here.
- 1:30:22So, this will remove
- 1:30:24you see here.
- 1:30:26So, every element was originally in its
- 1:30:28in its own array.
- 1:30:30But,
- 1:30:31I'm going to remove that dimension two
- 1:30:34here. So, now these elements will no
- 1:30:35longer be in its own array.
- 1:30:38And so,
- 1:30:40we will just have these elements
- 1:30:43in this So, now it's 1 3.
- 1:30:46So, because we removed this
- 1:30:48and now we just 1 3.
- 1:30:51And unsqueeze does the opposite. It will
- 1:30:53add the dimension of size one.
- 1:30:57So, if we have some tensor 1 2 3 4,
- 1:31:01we say unsqueeze a dimension zero.
- 1:31:05So,
- 1:31:07the previous shape was four and then we
- 1:31:09unsqueeze a dimension zero. So, it will
- 1:31:11add
- 1:31:12uh
- 1:31:13one dimension at position zero and it
- 1:31:17will have size one.
- 1:31:19So, now that means we have another array
- 1:31:23outside of this original
- 1:31:25tensor.
- 1:31:29And we can also unsqueeze a dimension
- 1:31:31one. So, if we take this unsqueeze a
- 1:31:33dimension one,
- 1:31:35it will be 4 1 now.
- 1:31:37Because it will add
- 1:31:39dimension uh
- 1:31:41after this.
- 1:31:43And that means that each of these will
- 1:31:44be in its own brackets.
- 1:31:47Or I should say in its own array of size
- 1:31:49one.
- 1:31:52So, this is the difference between
- 1:31:53unsqueeze at zero, unsqueeze at uh one.
- 1:31:56Uh you can play with this and understand
- 1:31:58like how it works and why it works and
- 1:31:59what it does.
- 1:32:01You can also unsqueeze at minus one,
- 1:32:03which will add
- 1:32:05uh dimension at the end.
- 1:32:08So, whatever your So, if you don't know
- 1:32:12the actual shape of your
- 1:32:14tensor, but you just want to put every
- 1:32:16element in its own array,
- 1:32:20then you can say unsqueeze a dimension
- 1:32:23minus one and this will put every
- 1:32:24element in its own array. So, it will
- 1:32:26add another. So, even if every element
- 1:32:29was already in its own array, it will
- 1:32:31add one more
- 1:32:33array within that array. So, every
- 1:32:35element will just get brackets around it
- 1:32:38if you unsqueeze a dimension minus one.
- 1:32:41This is good if you want to separate if
- 1:32:43you have some array and you need to
- 1:32:44separate it for something, do some
- 1:32:46operations on every single element.
- 1:32:49So, that's how it works.
- 1:32:51So, we are now at the first lesson here,
- 1:32:54first notebook.
- 1:32:56Uh I'm going to show you how to create
- 1:32:58different types of tensors. So, we can
- 1:33:00do torch.zeros five.
- 1:33:04That's going to just create a
- 1:33:06tensor of five zeros, but you can
- 1:33:09But, whatever shape you put here like 3
- 1:33:12{comma} 4,
- 1:33:13it will create a tensor with this shape
- 1:33:16with all zeros.
- 1:33:18So, three rows, each row has four
- 1:33:20elements or four columns. So, three
- 1:33:24rows, each row has four elements.
- 1:33:28Uh we have ones
- 1:33:32and this full,
- 1:33:35which we can specify the value. So,
- 1:33:39uh ones five, this will create an array
- 1:33:41of five ones or this matrix of ones.
- 1:33:45Let's see what it what it looks like.
- 1:33:46So, array of five ones or matrix with
- 1:33:49this shape or
- 1:33:51uh matrix where each value is five.
- 1:33:54You can also convert NumPy array to a
- 1:33:57tensor. So, if you have NumPy array, you
- 1:33:59just say torch.from_numpy
- 1:34:02to convert it into a tensor.
- 1:34:05And you can take that tensor and say dot
- 1:34:08numpy to convert it back to NumPy array.
- 1:34:11So, if you check the classes, it's
- 1:34:13either going to be torch tensor if it's
- 1:34:15torch tensor or numpy.ndarray.
- 1:34:19And when you convert them, so when you
- 1:34:21use torch.from_numpy, it shares the
- 1:34:23memory with the NumPy array. So,
- 1:34:26when you change this new uh
- 1:34:29torch tensor, it will also change the
- 1:34:31original NumPy array variable.
- 1:34:34Uh you can also if you clone, so that
- 1:34:37will create a separate
- 1:34:38uh copy. So, then it will be separated,
- 1:34:42the NumPy array and
- 1:34:44the tensor.
- 1:34:46You can also create tensors with random
- 1:34:48values. This is used in neural networks
- 1:34:50when initializing weights.
- 1:34:52So, you just say torch.rand and then the
- 1:34:54shape.
- 1:34:57And you can also specify you can also
- 1:34:59use randint to make it integer between
- 1:35:02zero and 10, but
- 1:35:05zero is included, 10 is not included.
- 1:35:07So, zero is inclusive, 10 is not
- 1:35:09inclusive.
- 1:35:11Or maybe exclusive.
- 1:35:14Uh this is our first So, this is example
- 1:35:17of the first thing we did here is
- 1:35:19torch.rand 3 4.
- 1:35:22So, that's going to give us this these
- 1:35:23numbers between zero and one.
- 1:35:27And so, three arrays or three rows and
- 1:35:30each row has four columns.
- 1:35:33And then the second randint will give us
- 1:35:36integers like this between zero and
- 1:35:38nine.
- 1:35:40We can also uh create random values from
- 1:35:43normal distribution where mean is zero
- 1:35:45and standard deviation is one.
- 1:35:48So, if we use torch.randn,
- 1:35:52so uh this should be
- 1:35:56uh bunched up around zero. These numbers
- 1:35:58should be uh very close to zero and then
- 1:36:01the further away you go from zero in
- 1:36:03positive or negative,
- 1:36:05uh there is less probability, although
- 1:36:07you can get any number here.
- 1:36:09But, the further away from zero you go,
- 1:36:12uh the probability decays exponentially.
- 1:36:15So, you're usually not going to get
- 1:36:17a number that are too far away. But, you
- 1:36:19see we get even numbers larger than one
- 1:36:21or
- 1:36:23So, we also have um
- 1:36:27You can also use this trick because
- 1:36:30torch.rand will give you values between
- 1:36:32zero and one,
- 1:36:33but you can multiply all of those values
- 1:36:35with 10. So, then they will be between
- 1:36:37zero and 10. Or yeah, zero and 10.
- 1:36:41So, here we can see that values are
- 1:36:43between zero and 10.
- 1:36:46You can also print data type of the
- 1:36:49tensor. So, it's going to be float
- 1:36:52if you initialize it like this, but you
- 1:36:54can also specify data type to be int64
- 1:36:57or float32.
- 1:37:00Because originally it's float32.
- 1:37:05You can also convert. So,
- 1:37:07if you have uh int64, you say
- 1:37:11dot So, that tensor.float,
- 1:37:13it will convert to float uh 32.
- 1:37:16For the next one, you would need to know
- 1:37:18how matrix multiplication works in
- 1:37:21mathematics. So, you can just watch my
- 1:37:23video here, math for AI research, or you
- 1:37:26can just uh type here like how to
- 1:37:29multiply matrices, matrix multiplication
- 1:37:32on YouTube.
- 1:37:33So, if you know how matrices are
- 1:37:35multiplied, dot product between this row
- 1:37:37and this column. So, you need to
- 1:37:38understand that.
- 1:37:40Uh then we can go further, go on and
- 1:37:44uh this is the operators to multiply
- 1:37:46matrices.
- 1:37:47So,
- 1:37:49uh you can also use torch.matmul by the
- 1:37:51way.
- 1:37:54So, if you have this matrix and this
- 1:37:55matrix, the result will be this matrix.
- 1:37:58Of course, the same result
- 1:38:01depending on which you use.
- 1:38:03At sign or torch.matmul, the result will
- 1:38:05be the same.
- 1:38:07One thing you need to know. So, when you
- 1:38:10have matrix and vector, you multiply
- 1:38:12them
- 1:38:13the same way.
- 1:38:16Uh you're going to get some other
- 1:38:17vector. Now,
- 1:38:19you can imagine this vector as being
- 1:38:21some vector and this resulting vector as
- 1:38:24being some other vector.
- 1:38:26So, the rule in maths is when you
- 1:38:28multiply vector with a matrix, that
- 1:38:31matrix will rotate
- 1:38:34and squish or stretch the vector.
- 1:38:36So, that's what matrices does and this
- 1:38:38is important in neural networks. Uh if
- 1:38:40you want to do this theoretical or
- 1:38:42research and stuff. So, just know that
- 1:38:44when you have a vector you multiply it
- 1:38:45with a matrix, it's going to stretch or
- 1:38:47squish or and rotate the vector.
- 1:38:51Possibly rotate. It may not rotate by
- 1:38:52the way.
- 1:38:54I will go to third lesson, third
- 1:38:55notebook here in PyTorch. So, I'm going
- 1:38:59to skip some of the matrix stuff. That's
- 1:39:01not so important.
- 1:39:04If we have a tensor,
- 1:39:06in this case it's a matrix, we can
- 1:39:08transpose it by
- 1:39:09uh using a.t and I'll show you what
- 1:39:12transposing means.
- 1:39:14And
- 1:39:16uh we can use a.t with these brackets,
- 1:39:19but only for 2D tensors.
- 1:39:22So, this is what's going to do. It's
- 1:39:23going to swap rows and columns.
- 1:39:25So, this was a column.
- 1:39:27Now, it's going to become row. 1 4 was a
- 1:39:30column. It's going to become a row now.
- 1:39:322 5 was a column. It's going to become
- 1:39:34row. And you see that
- 1:39:361 2 3 was a row. Now, 1 2 3 is a column.
- 1:39:39So, it's swapping rows and columns.
- 1:39:41Transposing.
- 1:39:43Let's see if we have a 3D matrix.
- 1:39:461 2 3 4 and then that's one matrix. And
- 1:39:49then there are two matrices.
- 1:39:52And we want to transpose
- 1:39:54uh
- 1:39:55the
- 1:39:56zeroth. So,
- 1:39:58so that's going to be this outer
- 1:40:00dimension and the last dimension,
- 1:40:03which has index two.
- 1:40:05So, that's going to be a little bit
- 1:40:06weird. So, now
- 1:40:09we do this. We say 1 and 5 now becomes
- 1:40:15uh this row. So, now
- 1:40:18the first element of the
- 1:40:20the first elements of the outer most
- 1:40:23dimensions, which are these two,
- 1:40:25now become elements
- 1:40:27of the inner most dimension because we
- 1:40:29transposed them. We swapped them.
- 1:40:32And then the inner most dimension
- 1:40:33elements in the previous become outer
- 1:40:37most dimension first elements. So, 1
- 1:40:40and 2. So, this only This is going to
- 1:40:42require some intuition, some playing
- 1:40:43around, some understanding like how this
- 1:40:45transposing works.
- 1:40:47We can also rearrange dimensions.
- 1:40:50So, now we are In this case, we are
- 1:40:54reversing the order, but you can put any
- 1:40:56order you want.
- 1:40:58So, let's see what happens when you
- 1:40:59reverse the order of dimensions.
- 1:41:02So, the original one is 1 2 3 4 5 6 7 8.
- 1:41:06But then
- 1:41:081 5 3 7. Let's see.
- 1:41:111 5 3 7.
- 1:41:18It's a little weird. 2 6 4 8.
- 1:41:21So, this is something
- 1:41:23that you just need to kind of look at
- 1:41:25this and get the intuition why these
- 1:41:27numbers, how what's swapped,
- 1:41:31etc.
- 1:41:32So, indexing and slicing.
- 1:41:35If we have a tensor 1 2 3 4 5 6 7 8 9,
- 1:41:41uh then uh we can
- 1:41:44index. So, this is the uh this element
- 1:41:46has indices 0 0 because it's zero row
- 1:41:49and zero column.
- 1:41:51Uh it starts from zero, not from one.
- 1:41:53So, then this element would have 1 1
- 1:41:56because it's
- 1:41:57So, this is zeroth first row, second
- 1:42:00row.
- 1:42:01Zero column, one column, two column.
- 1:42:04So, this has 1 1. This has uh two
- 1:42:09So, this has No, this is 1 2 because row
- 1:42:12one and column two.
- 1:42:16So,
- 1:42:18we see that
- 1:42:20uh t of 0 1 is
- 1:42:22two. So, zeroth column
- 1:42:24zeroth row, first column.
- 1:42:27That's going to be zeroth row
- 1:42:30and then column one, that's going to be
- 1:42:31two.
- 1:42:33You can also access entire row. So, this
- 1:42:36will give you entire row.
- 1:42:39And
- 1:42:40let's see.
- 1:42:421 2 3, entire row.
- 1:42:44And you can also get the first column or
- 1:42:46entire column not the entire column.
- 1:42:49But you need to put uh this sign here.
- 1:42:53And this means get all of the rows
- 1:42:56in the zeroth column. All of the rows in
- 1:42:59the zeroth column.
- 1:43:02So, all of the rows, but only in the
- 1:43:05zeroth column. So, 1 4 7.
- 1:43:09So, 1 4 7 is the It's going to be our
- 1:43:12new tensor.
- 1:43:14And you say all of the rows in the
- 1:43:17column with index one.
- 1:43:20So, that's going to be all of the rows,
- 1:43:22but in this column with index one. 2 5
- 1:43:258.
- 1:43:272 5 8.
- 1:43:29Now, let's see slicing. So, if you say t
- 1:43:33and then you say 0 to 2,
- 1:43:36this will give you all of the rows
- 1:43:40uh starting from the zero including row
- 1:43:42zero
- 1:43:43until two, but excluding two. So, this
- 1:43:47is excluded. This is inclusive. This is
- 1:43:49exclusive. So,
- 1:43:50starting from here until here, but
- 1:43:53excluding this one. So, it will just
- 1:43:54give you these two.
- 1:43:561 2 3 4 5 6 7 8.
- 1:44:00Let's see. 1 2 3 4 5 6 7 8.
- 1:44:05And then uh I'm going to now go a bit
- 1:44:08quicker. So, this will go
- 1:44:09from index from row one to the end. So,
- 1:44:13from row one including until the end.
- 1:44:15So, it will give you these two.
- 1:44:17You can check below. So, this will give
- 1:44:19you from the beginning until two, but
- 1:44:21excluding two.
- 1:44:23So, from the beginning, so it will give
- 1:44:25this one and this one, and it will not
- 1:44:27give you two. This is index two. So,
- 1:44:28this is index 0 1 2. It will not give
- 1:44:31you two.
- 1:44:32From the beginning until two, but not
- 1:44:34two.
- 1:44:37Okay, and then you can also say columns.
- 1:44:40So, because you have comma here,
- 1:44:42this is where you select rows, and here
- 1:44:44this is all rows. This means all rows.
- 1:44:46So, from the beginning to the end.
- 1:44:49And in this case, end is also included.
- 1:44:52So, when you have this, you it's from
- 1:44:53the beginning to the end in rows, and
- 1:44:55everything is included.
- 1:44:57So, from zero to two columns, but two is
- 1:45:00not included.
- 1:45:02Okay, so we got all of the rows, and
- 1:45:04then 0 1.
- 1:45:08But
- 1:45:09two is not included. So, it should just
- 1:45:10give you these two. 1 2 5 6 9 10. Let's
- 1:45:14see.
- 1:45:151 2 5 6 9 10.
- 1:45:19Okay.
- 1:45:20Uh we can also get all of the rows and
- 1:45:22from two until the end. So, that's going
- 1:45:25to
- 1:45:25say
- 1:45:29Let me see here.
- 1:45:31Uh 3 4 7 Yeah, that's it. So, from two
- 1:45:33including two to to end columns.
- 1:45:37And you can also pick like this. So,
- 1:45:39uh from zero to two excluding two rows
- 1:45:43and from one to three columns. So, from
- 1:45:46zero to two and from one is going to be
- 1:45:49this one to three, but excluding three.
- 1:45:52So, this will just give you
- 1:45:54this middle
- 1:45:55What was the
- 1:45:57columns?
- 1:45:59So, it will give you this middle matrix.
- 1:46:02You can also create a Boolean mask. So,
- 1:46:04let me show you what that is. So, if you
- 1:46:06have a tensor 1 2 3 4, etc.
- 1:46:10You can say mask is equal to tensor t
- 1:46:13greater than five.
- 1:46:16So, what that's going to do is
- 1:46:19replace everything that's greater than
- 1:46:21five with true.
- 1:46:24All of the where the value is greater
- 1:46:26than five. So, this is
- 1:46:27uh 1 2 3 4 5, okay? And then this is
- 1:46:31greater than five. 6 7 8 These these
- 1:46:34elements will be replaced with true
- 1:46:35because these are the values.
- 1:46:37So, if I was to put number seven here
- 1:46:41instead of the number one that was
- 1:46:42originally, then this would also be true
- 1:46:44because this element would also be
- 1:46:46greater than five. So, all of the
- 1:46:47elements
- 1:46:49values that are greater than five, they
- 1:46:50get
- 1:46:51replaced with true.
- 1:46:55And then you can use that mask that you
- 1:46:57just created.
- 1:46:59So, you can say t or original tensor t
- 1:47:03of mask,
- 1:47:06it will give you all of the elements
- 1:47:08that have the true
- 1:47:10uh assigned to them. Like
- 1:47:12So, 6 7 8 9.
- 1:47:16You also can do this conditional
- 1:47:18indexing. So, t where the value is
- 1:47:21greater than five
- 1:47:24or t where the number is even. So, value
- 1:47:28divided by two gives remain remainder
- 1:47:31zero.
- 1:47:32So, these will give us
- 1:47:34greater than five and even numbers.
- 1:47:38You can also modify values like this.
- 1:47:41So, t
- 1:47:43where the value is greater than 50, you
- 1:47:45want to replace all with zero. So,
- 1:47:48everywhere in this tensor t where the
- 1:47:51value is greater than 50, it will all be
- 1:47:53replaced with zero.
- 1:47:56So, let's say we have these values here.
- 1:47:59100 200 300 400. And then
- 1:48:03it's going to get replaced with zero
- 1:48:04because if we put put it this way. If we
- 1:48:09Now, let's learn about concatenating
- 1:48:10tensors. So, if you have two tensors,
- 1:48:131 2 3 4 and 5 6 7 8,
- 1:48:17you can just concatenate them
- 1:48:19uh in dimension zero or concatenate them
- 1:48:23or join them in dimension one.
- 1:48:26Let's see what both of those
- 1:48:29uh do.
- 1:48:31So, concatenating at dimension zero will
- 1:48:34just append
- 1:48:37all of these rows. Now, it's all going
- 1:48:38to become like a bunch of these rows.
- 1:48:40You see?
- 1:48:411 2 So, this guy this guy then just
- 1:48:44continue with this row and this row.
- 1:48:48So, now they're concatenated like that.
- 1:48:49So, it's 4 2. The new shape is 4 2. This
- 1:48:53dimension got concatenated. The first
- 1:48:55dimension, dimension zero.
- 1:48:58And if we concatenate on dimension one
- 1:49:00or second dimension, then
- 1:49:03it will go like this. 1 2 5 6. So, the
- 1:49:07first element here and the first element
- 1:49:09here
- 1:49:11will get here.
- 1:49:13And then 3 4 7 8. And now the shape is 2
- 1:49:164. So [snorts] this also requires some
- 1:49:19playing around, testing. You need to see
- 1:49:21like what happens. Try to understand it
- 1:49:23yourself. This is something that's um
- 1:49:26that you just need to
- 1:49:27play around with and understand.
- 1:49:30You can also concatenate three tensors
- 1:49:32and it's done with torch.cat. I forgot
- 1:49:35to say.
- 1:49:36So three tensors, it's going to do the
- 1:49:38same thing.
- 1:49:40So you're just going to say 1 2 3 4 5 6
- 1:49:42etc. So the same thing. Now it's 6 2.
- 1:49:46You can also torch.stack.
- 1:49:49If you have two matrices
- 1:49:522 by 2 and 2 by 2
- 1:49:54you can kind of just put them into
- 1:49:57another array, another tensor.
- 1:50:00So this is this first matrix will be the
- 1:50:03first element.
- 1:50:04And the second matrix will be the second
- 1:50:06element
- 1:50:07of this. So now the shape is 2 2 2
- 1:50:09because
- 1:50:11you join these two these two matrices
- 1:50:15into like another array now.
- 1:50:18So that's what you can do with this
- 1:50:20torch.stack
- 1:50:22uh tensor one tensor two along
- 1:50:25a dimension zero.
- 1:50:26Now I'm in the last seventh lesson.
- 1:50:30Let's see some more tricks. So we have
- 1:50:32identity matrix. If you say torch.eye 3
- 1:50:39uh let's see. This will give you
- 1:50:42uh this identity matrix where it's ones
- 1:50:45on the diagonal and zeros outside. If
- 1:50:47you check my math course, you will see
- 1:50:49that multiplying
- 1:50:51any matrix with this identity matrix
- 1:50:54will give you that same matrix. This is
- 1:50:56like multiplying with one.
- 1:50:59This identity matrix.
- 1:51:02So if you have any matrix
- 1:51:04you multiply that with identity matrix.
- 1:51:07So matrix multiply it will give you the
- 1:51:09same matrix.
- 1:51:12torch.arange
- 1:51:1310 will give you an array from 0 to 9
- 1:51:17because this is excluded. So integers
- 1:51:19from 0 to 9.
- 1:51:22Let's check here. 0 to 9.
- 1:51:25arange uh torch.arange 2 to 8 will give
- 1:51:29you integers 2 to 7 because this last is
- 1:51:33excluded.
- 1:51:362 to 7.
- 1:51:38You can also say arrange 0 to 10 with
- 1:51:40step 2.
- 1:51:42This means it will go 0 2 4 6 8 and 10.
- 1:51:46The last one is excluded.
- 1:51:48You can say step 0.5.
- 1:51:51So it will go 0 0.5 1 1.5 until 4.5.
- 1:51:58You can also set data type to be float
- 1:52:0132. Then it will give you this like 0.1.
- 1:52:03So these are the floats.
- 1:52:06And this is what you will often do is
- 1:52:08you will arrange
- 1:52:09and then reshape.
- 1:52:11So this will give you numbers 0 to 11.
- 1:52:13So there will be 12 numbers.
- 1:52:15And then reshape to 3 4, three rows four
- 1:52:18columns.
- 1:52:20And this is what it's going to look
- 1:52:21like. You're going to do this often.
- 1:52:24linspace
- 1:52:26will give you
- 1:52:27the following. So if you say 0 to 1 and
- 1:52:30five values, so it will
- 1:52:32give you five values in between these
- 1:52:35numbers
- 1:52:37equally spaced including these two
- 1:52:39numbers.
- 1:52:42So
- 1:52:43starting from 0 to 1
- 1:52:46and in between and it's evenly spaced.
- 1:52:480.25 0.25.
- 1:52:52So then you can also say from 0 to 10
- 1:52:55and then 10 evenly spaced numbers.
- 1:52:58And you see here in linspace this 10 is
- 1:53:01also included. So previously it was
- 1:53:03excluded, but now it's included. You
- 1:53:05need to just memorize that. Those are
- 1:53:07That's how it works.
- 1:53:10From 5 to minus from minus 5 to 5, 11
- 1:53:14numbers. So in this case it will just be
- 1:53:17the integers.
- 1:53:19And this is interesting. If you say
- 1:53:20torch.empty
- 1:53:22and then the shape, so
- 1:53:24this will create a tensor or in memory,
- 1:53:27but it will not change the values.
- 1:53:30It will not set any values. So it will
- 1:53:32just allocate space in memory. So
- 1:53:34whatever numbers or values were in the
- 1:53:36memory, it will have
- 1:53:38uh these values.
- 1:53:42You see we have some garbage values here
- 1:53:44because we did not change them. We just
- 1:53:45took the memory
- 1:53:47but we didn't change anything.
- 1:53:50So that's going to be it for this video.
- 1:53:52Check out the GitHub. Check out the
- 1:53:54previous course on math. Uh join my
- 1:53:56school and see you in the next video.
- 1:54:00Let's learn about neural networks. After
- 1:54:02this lesson you will be able to code
- 1:54:04your own neural network from scratch.
- 1:54:06You will understand how this works and
- 1:54:08you will be able to then further build
- 1:54:11it into projects and AI research etc.
- 1:54:14Make sure to have PyTorch installed,
- 1:54:16NumPy, Matplotlib as well. You can find
- 1:54:19this repository uh at the GitHub below
- 1:54:21this video.
- 1:54:22So this is a neural network, but let's
- 1:54:25just first focus on a single neuron. So
- 1:54:28let's just look at this first blue
- 1:54:30circle and the first white circle and
- 1:54:32this output. And let's imagine none of
- 1:54:35these other exist.
- 1:54:37So input will be some number.
- 1:54:40And then it's multiplied with some
- 1:54:42weight. This weight is learned by the
- 1:54:43neural network. So this weight will also
- 1:54:46be some number.
- 1:54:47And then input is multiplied with this
- 1:54:50weight, it will give some output.
- 1:54:52So the output is going to trans So the
- 1:54:56neural network will transform this input
- 1:54:58into output. It will predict something.
- 1:55:00And the weight that's multiplying the
- 1:55:03input and transforming it into output is
- 1:55:05going to be learned during training.
- 1:55:08So what is the best weight that's going
- 1:55:09to give us correct output based on any
- 1:55:11input?
- 1:55:12That's how neural network learns and
- 1:55:14then generates
- 1:55:15or predicts.
- 1:55:17So let's check code example.
- 1:55:19Let's say we have inputs two and three.
- 1:55:22And then these would be weights.
- 1:55:26Uh these would be neural network weights
- 1:55:27like 0.5 and 0.3 that are learned.
- 1:55:31That are already learned. So we'll see
- 1:55:32later how they are learned.
- 1:55:34Now we could also have a bias. It's
- 1:55:38just a single number that's added
- 1:55:41for the entire layer, entire set of
- 1:55:44weights. Entire set of weights has a
- 1:55:45single bias number and you see here.
- 1:55:49The output, the weighted sum
- 1:55:51it's going to be
- 1:55:53this weight
- 1:55:54times this input
- 1:55:56plus this weight times this input.
- 1:55:59And then at the end we have some bias.
- 1:56:00So bias is also learned. It also
- 1:56:03helps. If you know functions, bias will
- 1:56:05help
- 1:56:06uh this function, whatever it is, to
- 1:56:08move left and right.
- 1:56:09And these weights are going to determine
- 1:56:11the slope.
- 1:56:13So this is in my math course uh for
- 1:56:15machine learning that I published
- 1:56:17earlier.
- 1:56:18In this case this is just a dot product
- 1:56:21right here. It's just a dot product
- 1:56:22between input and weights.
- 1:56:24And so
- 1:56:27uh weighted sum is two in this case.
- 1:56:31And then
- 1:56:32uh because this is going to be some
- 1:56:34linear function. If we want to make it
- 1:56:36maybe curved we need to actually pass
- 1:56:39this through some activation function.
- 1:56:40For example, sigmoid.
- 1:56:43Now what When we pass this output
- 1:56:45weighted sum through
- 1:56:47some function like sigmoid, it looks
- 1:56:49kind of weird, but let's just ignore
- 1:56:52like weights weights like this. Let's
- 1:56:54just imagine that sigmoid is going to
- 1:56:56make this function like curved.
- 1:56:58So
- 1:56:59if you have some data points here, data
- 1:57:01points here, you want to actually
- 1:57:03separate them with a curved function.
- 1:57:06If you have a straight line, then it
- 1:57:08will not separate these like messy data
- 1:57:10points. So you need to be able to curve
- 1:57:12the function to separate the data
- 1:57:13points.
- 1:57:14That's what activation function will
- 1:57:17It will take this weighted sum, which is
- 1:57:18a line
- 1:57:19and make it curved.
- 1:57:24And so at the end for these specific
- 1:57:26numbers we get this activation or
- 1:57:30output.
- 1:57:32So this here is sigmoid function.
- 1:57:35We also have other activation functions
- 1:57:37like like ReLU and their purpose in
- 1:57:39neural networks is to curve the lines
- 1:57:42that are because uh lines we get we just
- 1:57:44multiplying weights and that bias are
- 1:57:46straight lines.
- 1:57:48Because here uh let's say we want to
- 1:57:51separate
- 1:57:53uh maybe this red this red data set. We
- 1:57:55cannot do it with a straight line
- 1:57:57because it will it can just like cross
- 1:58:00over it. We need a curved line that goes
- 1:58:02around this like this.
- 1:58:04So that's why
- 1:58:05we have activation functions to curve
- 1:58:07the lines.
- 1:58:10So that's how neural network will be
- 1:58:11able to learn how to separate these data
- 1:58:14sets, but let's see on some examples
- 1:58:15more. Join my school community to become
- 1:58:18AI researcher. Link below or you can
- 1:58:21find link on my channel here in the
- 1:58:22about section.
- 1:58:24So usually you would first initialize uh
- 1:58:27a neuron. So weights would be
- 1:58:30initialized as random numbers from a
- 1:58:32normal distribution. I explained that in
- 1:58:34previous PyTorch lesson on my channel.
- 1:58:37And [snorts] then make it very small. So
- 1:58:40uh maybe these would be around like 0 to
- 1:58:431, a bit more maybe.
- 1:58:45But we won't make them all very small,
- 1:58:47so they're around 0.1.
- 1:58:50Bias goes at 0 because bias is being
- 1:58:53added. So in the beginning we add 0.
- 1:58:58But you don't want to initialize weights
- 1:58:59at 0 because weights are multiplying. So
- 1:59:02whatever then they will just turn
- 1:59:04everything to zero, so that's not good.
- 1:59:05You want to initialize weight with some
- 1:59:07number.
- 1:59:08And here we would instantiate this class
- 1:59:11neuron, and let's say number of inputs
- 1:59:13is equal to two.
- 1:59:15So, then
- 1:59:16this will create two weights and one
- 1:59:19bias. So, this number of input just
- 1:59:20influence the the amount of weights.
- 1:59:24So, this is example.
- 1:59:26Number of inputs two, and these are the
- 1:59:27random weights that we generated.
- 1:59:29They're really very small because we
- 1:59:310.1.
- 1:59:35So, then
- 1:59:36uh to our neuron class, we want to add
- 1:59:38this weighted sum function.
- 1:59:41So, uh it will take inputs, the numbers.
- 1:59:46So, this is different from number of
- 1:59:47input. This is the the input, the actual
- 1:59:48inputs numbers.
- 1:59:50And this will do dot product between
- 1:59:52weights and inputs. Now, this is also
- 1:59:55matrix multiplication. It says here dot,
- 1:59:57it's a bit confusingly named, but this
- 1:59:59is matrix multiplication between weights
- 2:00:01and inputs, and then add bias at the
- 2:00:03end.
- 2:00:06And then we're also going to add this
- 2:00:07part to our code. So, after we
- 2:00:09initialize, we already we already seen
- 2:00:11this part of initialization.
- 2:00:14We want to add this part. So, I set some
- 2:00:17inputs here.
- 2:00:18And then I call neuron.weighted_sum.
- 2:00:21I call our new function to do matrix
- 2:00:22multiplication.
- 2:00:26So, the weighted sum here is
- 2:00:28uh
- 2:00:29two.
- 2:00:31Which also includes this bias, by the
- 2:00:33way.
- 2:00:35Now, we will add third uh function to
- 2:00:37our neuron class, which is going to be
- 2:00:39this sigmoid function.
- 2:00:41And so, it makes it non-linear, so it
- 2:00:44curves our line. Because when we just
- 2:00:46multiply with inputs with weights, and
- 2:00:48that bias is going to be a straight
- 2:00:49line. The function is going to be a
- 2:00:51straight line. The function of weights
- 2:00:53times inputs plus bias.
- 2:00:56But sigmoid make makes it curve, or
- 2:00:58activation function makes it curved. And
- 2:01:00we will use sigmoid because it has like
- 2:01:02a nice curvy shape that neural network
- 2:01:05can use well.
- 2:01:07So, this is the formula for the sigmoid
- 2:01:08function. We need to
- 2:01:10pass in this number.
- 2:01:12And then the formula is one over one
- 2:01:15plus e to
- 2:01:17-x.
- 2:01:21And then we will just pass our
- 2:01:24activation,
- 2:01:25or I should say the output of the
- 2:01:27weighted sum,
- 2:01:28through sigmoid.
- 2:01:30And we will get some
- 2:01:32uh activation value.
- 2:01:36Sigmoid will be between zero and one
- 2:01:37like this. It's going to be curved. So,
- 2:01:39if neural network needs to make this uh
- 2:01:42maybe left curve, it will make weights
- 2:01:45and biases such that we get some number
- 2:01:48around minus two and minus one or
- 2:01:50something like that.
- 2:01:51And then after activation, it will get
- 2:01:53this left curve to maybe encircle some
- 2:01:55data.
- 2:01:56If it needs this other right curve,
- 2:01:58then it will uh
- 2:02:01make its learn its weights and biases
- 2:02:03to give a number around one, two.
- 2:02:06So, it will get this curve here to
- 2:02:08encircle some other data in this way.
- 2:02:12So, this activation function will never
- 2:02:13change. It's just the weights and
- 2:02:14biases, but the neural network will
- 2:02:16learn to adjust weights and biases, so
- 2:02:18when
- 2:02:19the output is passed through activation
- 2:02:22function, they will properly encircle
- 2:02:24data.
- 2:02:26Or be able to predict or generate data.
- 2:02:29That is it will have proper
- 2:02:30understanding of the data.
- 2:02:32Now, we can test uh this with different
- 2:02:35different input, sorry. So, 0 0 1 0 2 3.
- 2:02:39And you will see that the output is
- 2:02:41always some number between zero and one.
- 2:02:46We can also try different activation
- 2:02:47functions. So, sigmoid will give 0.8.
- 2:02:51Uh ReLU will give two.
- 2:02:54This tanh will give 0.96. So, depending
- 2:02:57on the activation function, you can uh
- 2:02:59you need to find one that works well for
- 2:03:01your data set. For example, ReLU is very
- 2:03:03easy and fast to compute, but it's not
- 2:03:06as powerful. So, maybe if you need some
- 2:03:08quick and easy task.
- 2:03:10Now, let's see how to combine multiple
- 2:03:12of these neurons, not just one, into a
- 2:03:15neural network layer.
- 2:03:17So, again we will have input two, three,
- 2:03:19let's say. But then we will have two
- 2:03:21neurons. The first neuron is this one,
- 2:03:230.5 0.3.
- 2:03:25And the second neuron is 0.2 0.4. So,
- 2:03:28you see that it has weights for each of
- 2:03:30the inputs.
- 2:03:33And we will have two biases, so one for
- 2:03:35each of the neurons.
- 2:03:38And the forward pass is the matrix
- 2:03:40multiplication between inputs and
- 2:03:42weights transposed. So, uh I explained
- 2:03:45matrix multiplication in math and
- 2:03:46PyTorch fundamentals,
- 2:03:48but uh you can also check how it works,
- 2:03:50but you need to transpose weights here.
- 2:03:52Now,
- 2:03:53uh some people can also write like
- 2:03:55weights
- 2:03:57uh
- 2:03:58mat matrix multiply inputs or inputs
- 2:04:00transposed. Now, this just depends on
- 2:04:02how you save your weights in memory.
- 2:04:04You know that matrix multiplication,
- 2:04:06there are some rules.
- 2:04:08But for now, let's just take it as is.
- 2:04:10So, inputs at uh
- 2:04:12weights transposed plus biases, so we
- 2:04:14have
- 2:04:15input tensor
- 2:04:17at weights transposed.
- 2:04:20At the end, we get this output two, 1.8.
- 2:04:24And then if we pass it through sigmoid
- 2:04:26activation, each of them will be uh some
- 2:04:29number between zero and one, depending
- 2:04:32on the input here.
- 2:04:34So, [clears throat] here we would define
- 2:04:36a class layer,
- 2:04:38and then it would uh have number of
- 2:04:40inputs and number of neurons. So, these
- 2:04:42are like hidden neurons. They It can be
- 2:04:45different number. You see here we have
- 2:04:47three inputs, but five neurons, five
- 2:04:49hidden neurons, I should say.
- 2:04:52And then one output at the end.
- 2:04:54So, in our linear layer that we
- 2:04:56initialize, we have number of inputs and
- 2:04:57number of neurons.
- 2:04:59And weights are going to be
- 2:05:02uh
- 2:05:02number of neurons {comma} number of
- 2:05:04inputs.
- 2:05:06This is one of those weird things that
- 2:05:08you just need to kind of get the
- 2:05:09intuition by uh trying and
- 2:05:11understanding. So,
- 2:05:12number of neurons is three means we have
- 2:05:15we have three rows.
- 2:05:16One, two, three rows.
- 2:05:18And number of input is two.
- 2:05:21And that's the second dimension here.
- 2:05:22So, we have
- 2:05:23two columns. So, in each row there is
- 2:05:26two. So, two two two. So, three rows,
- 2:05:29two columns.
- 2:05:32And that's why we also need to transpose
- 2:05:33because we have two inputs.
- 2:05:35We need to transpose this matrix so we
- 2:05:37because of matrix multiplication, we
- 2:05:39will
- 2:05:39multiply these two and these two, and
- 2:05:41then these two these two, these two
- 2:05:42these two. So, we need to transpose,
- 2:05:44flip the matrix
- 2:05:46of weights.
- 2:05:47But I recommend you just try to
- 2:05:49understand this yourself, or uh you try
- 2:05:51like playing around, see what happens,
- 2:05:52try to draw.
- 2:05:54Understand how matrix multiplication
- 2:05:55works first. You need to practice this.
- 2:05:58These are the example weights that are
- 2:06:00initialized.
- 2:06:02And the input is two, so when you
- 2:06:05multiply input matrix and weights
- 2:06:08matrix, you need to transpose this
- 2:06:11because you are
- 2:06:12multiplying
- 2:06:14uh row of the input
- 2:06:15with the column of the weights.
- 2:06:18So, you want to multiply want to
- 2:06:20multiply it with this row, but you
- 2:06:22cannot multiply row and row. So, you
- 2:06:23need to transpose this row to become a
- 2:06:25column.
- 2:06:27So, now it's just two two two, like
- 2:06:29this.
- 2:06:31So, then after initialization, we will
- 2:06:33add this weighted sum. And here we just
- 2:06:36do
- 2:06:37inputs matrix multiply weights
- 2:06:39transposed. We need to transpose them as
- 2:06:41I explained plus biases.
- 2:06:46And at the end, you will get something
- 2:06:48like this.
- 2:06:50Um we will get three numbers because
- 2:06:53the hidden number of neurons is three.
- 2:06:56You know, we are multiplying
- 2:06:581 * 2 matrix of inputs
- 2:07:00times
- 2:07:02uh
- 2:07:022 * 3
- 2:07:04matrix of weights, so the result will be
- 2:07:061 * 3. So, this is matrix
- 2:07:08multiplication. So, you have these three
- 2:07:10neurons
- 2:07:12uh that you get
- 2:07:14as as a result.
- 2:07:15So, again I recommend you try this out
- 2:07:17yourself, change some numbers, try to
- 2:07:19understand how this matrix
- 2:07:20multiplication works.
- 2:07:22And then it's very easy to add this
- 2:07:24activation function. So, torch.sigmoid
- 2:07:28and pass in the neurons.
- 2:07:31So, before the activation, the numbers
- 2:07:33that we got
- 2:07:35of the neurons are these, and then after
- 2:07:37activation, it's going to be this
- 2:07:39uh tensor or vector.
- 2:07:41So, after we uh have this sigmoid, we
- 2:07:44would add that to this forward function.
- 2:07:47So, in the forward function, we first
- 2:07:50do inputs times weights plus biases, and
- 2:07:53then pass it pass the result through
- 2:07:56sigmoid activation, and then that's the
- 2:07:57output. Those are the neurons that we
- 2:07:59get.
- 2:08:00And so, we can say outputs is equal to
- 2:08:03layer.forward then pass in inputs. This
- 2:08:05is how you would usually do it
- 2:08:08in
- 2:08:09uh neural networks, large language
- 2:08:11models, reinforcement learning,
- 2:08:12everywhere.
- 2:08:13Here we can take a look at some
- 2:08:15activation functions. So, I already
- 2:08:17explained sigmoid, it puts any value on
- 2:08:20x between uh zero and one. Output is
- 2:08:23always between zero and one.
- 2:08:25Uh we also have ReLU. This is very easy
- 2:08:27and quick to compute. So,
- 2:08:29if the input is lower than zero,
- 2:08:32or zero, it will just become zero. The
- 2:08:35output will just become zero.
- 2:08:37And if the input is larger than zero, it
- 2:08:39will just remain that same number,
- 2:08:40whatever it is.
- 2:08:42So,
- 2:08:43R of Z, Z is our number, is going to be
- 2:08:46max maximum between zero and Z.
- 2:08:49So, that's the ReLU function. You just
- 2:08:51find uh set zero or Z, whichever is
- 2:08:54bigger.
- 2:08:55This is very fast to compute. You would
- 2:08:57use this if you need some quick
- 2:08:59computation with that's not so maybe
- 2:09:01powerful or precise, but you just need
- 2:09:02some
- 2:09:04easy task for the neural network and
- 2:09:05maybe it can do it with this activation
- 2:09:08function. And you see here
- 2:09:10it would just if you have a line it will
- 2:09:12it will maybe [clears throat]
- 2:09:14make that line like this. It's not going
- 2:09:15to be curved. It's going to be like this
- 2:09:17just like cut or
- 2:09:19broken like this.
- 2:09:21But it will be a continuous line. It
- 2:09:23will not be like separated.
- 2:09:25But for some tasks it's not good if you
- 2:09:27make outputs that are lower than zero
- 2:09:30exactly zero always. So you kind of
- 2:09:31erase all of that information. So there
- 2:09:34is a leaky ReLU.
- 2:09:37So
- 2:09:38outputs that are lower than zero it will
- 2:09:40just make them a lot
- 2:09:42closer to zero but not zero.
- 2:09:45So it will just squish them towards zero
- 2:09:48but they will still not be zero.
- 2:09:50So they will be 0.1 times X if they are
- 2:09:53lower than zero.
- 2:09:55Or they will just remain X if they are
- 2:09:58higher than zero.
- 2:10:00So this doesn't erase completely
- 2:10:01information like the ReLU function.
- 2:10:05While it still provides non-linearity
- 2:10:06this bending. So the neural network can
- 2:10:09use this bending to
- 2:10:11encircle some data.
- 2:10:13Here we have some examples. You see this
- 2:10:15is the zero. So ReLU is just
- 2:10:17zero.
- 2:10:19And then GELU
- 2:10:21is actually
- 2:10:22more curved around zero here. So it
- 2:10:25preserves information if the number is
- 2:10:27around minus two.
- 2:10:30So and then we have ELU that's kind of
- 2:10:34going down even more.
- 2:10:37So we have these different activations.
- 2:10:38You would test which ones work best for
- 2:10:41your data.
- 2:10:42Here is another example. Sigmoid, ReLU,
- 2:10:47SiLU,
- 2:10:49Swish, ELU.
- 2:10:52They're all kind of similar.
- 2:10:53But they do a bit different things.
- 2:10:56But they will all curve your line of
- 2:10:59weights and outputs and neurons. You see
- 2:11:02with just multiplying weights and inputs
- 2:11:04and biases you would get a straight
- 2:11:06line, but then passing that through
- 2:11:08activation function you would get a
- 2:11:10curvy line.
- 2:11:12This is a good example as well.
- 2:11:14You want to fit this data with a
- 2:11:16straight line. It's going to be tough.
- 2:11:18You're going to have to leave out a
- 2:11:19bunch of this red data
- 2:11:21on the other side which is wrong. But
- 2:11:23with a curvy line maybe this is a
- 2:11:25sigmoid function here. You can fit data
- 2:11:28properly.
- 2:11:29Now to understand how neural networks
- 2:11:31learn I have three good videos. So you
- 2:11:34can watch this one backpropagation from
- 2:11:36scratch, torch.backward, and this one
- 2:11:38where I train a neural network. I'm
- 2:11:40going to leave all
- 2:11:42these links in the description.
- 2:11:44Or you can just search for them.
- 2:11:48I also put these three videos here in
- 2:11:50the third lesson after
- 2:11:52these neural network and layer and
- 2:11:54stuff. So you can just copy these URLs.
- 2:11:57That's going to be it for this video and
- 2:11:59see you in the next one.
- 2:12:01Welcome to attention mechanism and
- 2:12:04self-attention in transformers in large
- 2:12:06language models.
- 2:12:07So let's say we have a sentence the cat
- 2:12:10sat on the mat.
- 2:12:12Let's say we are now we have just these
- 2:12:14words in front of the mat and we're
- 2:12:16trying to predict the next word
- 2:12:19that is mat.
- 2:12:21So how do we know
- 2:12:24what's the next word? Well, we're going
- 2:12:26to take some information from the
- 2:12:27previous words.
- 2:12:29So we have two challenges here.
- 2:12:32How do we know from which words to take
- 2:12:34information? For example here cat sat
- 2:12:37cat sat. That would give us most clue
- 2:12:41that this could be like a mat.
- 2:12:44>> [snorts]
- 2:12:44>> So how to select previous words and how
- 2:12:47to actually take information? How do we
- 2:12:48actually like take information? What
- 2:12:50does that mean? How is that done?
- 2:12:53So in this example we see that cat is
- 2:12:55selected to have 60%
- 2:12:58relevance,
- 2:12:59sat 20%, the 10%, on the these are have
- 2:13:04smaller. Okay.
- 2:13:06And so you can ignore this last imagine
- 2:13:09this doesn't exist. So I'm explaining
- 2:13:11like how to generate this in this case.
- 2:13:15Attention weights will sum up to 100.
- 2:13:18So do they sum up? I think they do.
- 2:13:21Okay? But if we didn't have this 3% this
- 2:13:24one then this 3% would actually be like
- 2:13:27within these as well. So these would sum
- 2:13:29up to 100 if we didn't have this last
- 2:13:32word that we are trying to predict.
- 2:13:35So model learns
- 2:13:36to which word to focus on.
- 2:13:39The way LLM works is
- 2:13:42if you are predicting this word mat it's
- 2:13:44going to
- 2:13:45look take this word previous word the
- 2:13:48and combine
- 2:13:50add all of the irrelevant information
- 2:13:52into this word. So now
- 2:13:54this word will have not only this word
- 2:13:56but all of the context before it as
- 2:13:58well.
- 2:13:59And then after processing processing
- 2:14:01processing
- 2:14:02move this this is done like many times
- 2:14:06over and over again.
- 2:14:08>> [snorts]
- 2:14:08>> It will just convert the final vector
- 2:14:10that's juiced up with all of the
- 2:14:12previous information
- 2:14:14into this next word.
- 2:14:17So how do we juice up this vector with
- 2:14:19all of the previous words all of the
- 2:14:21previous information? So
- 2:14:23every of the tokens including this one
- 2:14:27will have
- 2:14:29will create three things.
- 2:14:31Query,
- 2:14:33key,
- 2:14:34and value.
- 2:14:35This is confusing but bear with me.
- 2:14:38So each of these words or tokens will be
- 2:14:42like a vector embedding. It's a vector.
- 2:14:44You can check my previous lessons math
- 2:14:46PyTorch and neural networks to
- 2:14:49understand this.
- 2:14:50So it's a vector.
- 2:14:52And so that vector will transform into
- 2:14:54three vectors for each of the token
- 2:14:57vectors. So each of the tokens here each
- 2:14:59of them will get three.
- 2:15:01Query, key, value.
- 2:15:03And usually these query key and value
- 2:15:04are a lot smaller five 10 times smaller
- 2:15:07than this big token embedding. So
- 2:15:10this token vector token embedding
- 2:15:12represents the meaning
- 2:15:14of these
- 2:15:16words.
- 2:15:18So just these numbers represent meaning.
- 2:15:20So for example
- 2:15:22large language model learns
- 2:15:24what each of the numbers in the vector
- 2:15:27each of the dimensions represents. For
- 2:15:29example maybe the first number in the
- 2:15:31vector represents if something is green.
- 2:15:36So the higher the number the more this
- 2:15:38token has this green characteristics.
- 2:15:42So for example if you have like some
- 2:15:45green apple
- 2:15:47the first dimension will have a lot
- 2:15:49higher value
- 2:15:50than a word heart that's associated with
- 2:15:54red for example.
- 2:15:55So the first
- 2:15:58number first dimension in the vector of
- 2:16:00word heart will have a lot lower value.
- 2:16:03So neural network will learn what these
- 2:16:05features are and
- 2:16:07in this vector this vector is same size
- 2:16:10for every token. It represents the token
- 2:16:12it's same size.
- 2:16:14And
- 2:16:15the position the dimension first second
- 2:16:19it's always the same. So zero's
- 2:16:22dimension always represents the same
- 2:16:24thing for every token.
- 2:16:26Like how this how green is this.
- 2:16:30So each token will have query, key, and
- 2:16:33value.
- 2:16:34Query says which information am I
- 2:16:37looking for?
- 2:16:40And then
- 2:16:42key says which information I have.
- 2:16:47And the value is the information that it
- 2:16:50gives. Now keep in mind there is a
- 2:16:53difference between explaining which
- 2:16:55information you have and that
- 2:16:58information.
- 2:17:00So that's important difference.
- 2:17:02So
- 2:17:04if we are on this word the
- 2:17:06its query
- 2:17:08will dot product
- 2:17:10with all of the keys individually.
- 2:17:13Okay?
- 2:17:14So if this words this tokens query
- 2:17:18dot product this tokens key as I said
- 2:17:22key is explaining what this token holds
- 2:17:24what's information.
- 2:17:26If the dot product is a high number we
- 2:17:28call that high affinity.
- 2:17:31Then
- 2:17:33you will just multiply the value
- 2:17:35of this token the value
- 2:17:38which is the actual information it gives
- 2:17:41with that number
- 2:17:43with that dot product of the query and
- 2:17:45key
- 2:17:46and add to this to value of this token.
- 2:17:50Okay? In the end we are just adding
- 2:17:52values.
- 2:17:54But
- 2:17:55but
- 2:17:57the query and key have two functions.
- 2:17:59How much to weight the value? How much
- 2:18:02so it do you want to reduce? So if you
- 2:18:04multiply the value of this with a small
- 2:18:06number then when you add that small
- 2:18:08number to this value
- 2:18:10it will not influence a lot
- 2:18:12because
- 2:18:14this value will be multiplied with like
- 2:18:160.01.
- 2:18:17So when you add that it's a very small
- 2:18:20number in each of the dimensions it will
- 2:18:21not change too much this.
- 2:18:24And
- 2:18:26if that 0.01 is like the dot product
- 2:18:30between
- 2:18:31this query and this key. It means like
- 2:18:34we don't care about I don't care about
- 2:18:35this information too much. There is no
- 2:18:36like affinity there.
- 2:18:38But if that product is high
- 2:18:41it can be
- 2:18:43larger close to one for example.
- 2:18:46Let's say 0.9.
- 2:18:48Um
- 2:18:49then when you multiply this value with
- 2:18:510.9 and add this value will have a lot
- 2:18:54stronger influence because now it's a
- 2:18:56lot larger number than when you multiply
- 2:18:58it with like 0.01.
- 2:19:01So, basically, to repeat,
- 2:19:04if you are predicting this token, then
- 2:19:07you look at this token
- 2:19:09and you add all of the values to the
- 2:19:12value of this token. Now, remember, I
- 2:19:13said we're not adding the
- 2:19:16vector embedding of this token of these
- 2:19:19tokens. We are adding their their
- 2:19:21We are transforming that vector
- 2:19:23embedding using a small neural network,
- 2:19:25one layer neural network, into a value
- 2:19:28vector.
- 2:19:29And value vector is a lot smaller than
- 2:19:32the big vector embedding.
- 2:19:34So, we don't actually need
- 2:19:37to have uh so much processing. We don't
- 2:19:39need to work with the huge vectors. We
- 2:19:41can just transform it to small vectors
- 2:19:44so for faster processing and it's
- 2:19:46sufficient and enough to do this.
- 2:19:49So,
- 2:19:50we take We just add values of each of
- 2:19:53them to this last known token.
- 2:19:55Add those vectors at the vectors, but
- 2:19:58how much of the vectors we add
- 2:20:00will be decided by the
- 2:20:02query times key. So,
- 2:20:04query or what I'm looking for, what I
- 2:20:06give. If that number is low, then it
- 2:20:08means like, "Okay, I don't really want
- 2:20:10what you give." Or I just want a little
- 2:20:12bit of what you give.
- 2:20:15So, then the value will be multiplied.
- 2:20:18The value vector will multiply by with
- 2:20:20that small number, so you will just get
- 2:20:21like a little bit of influence here when
- 2:20:23you add that weighted value
- 2:20:26to this value.
- 2:20:28In the end,
- 2:20:29all of the values will be added to this
- 2:20:32value, and maybe this cat value will
- 2:20:35have a high multiplication number.
- 2:20:39High weight.
- 2:20:40And so, it will employ like give a lot
- 2:20:42of context, influence this value.
- 2:20:45And then set can also give a lot of
- 2:20:47context so we have large number as well.
- 2:20:50So, this is the self-attention formula.
- 2:20:53In this case, so this is where we start.
- 2:20:55So,
- 2:20:56query, this is a query vector of this
- 2:20:59token, current token.
- 2:21:01Actually, in this formula, this is meant
- 2:21:03like for matrix of all of the queries of
- 2:21:05all of tokens, but
- 2:21:07not to complicate, let's just focus on a
- 2:21:10single token. So, query
- 2:21:12of the current token times
- 2:21:15all of the keys of every previous token.
- 2:21:18So, instead of calculating keys one by
- 2:21:20one, so query times key, query times
- 2:21:22key, query times key, we put all of this
- 2:21:24into matrix.
- 2:21:26And we calculate this vector
- 2:21:28with this key matrix. And transpose is
- 2:21:31just because
- 2:21:33that's how you do matrix multiplication.
- 2:21:36So, that's why it's like here it's it's
- 2:21:37like to indicate it's a matrix
- 2:21:39multiplication. Need to take care like
- 2:21:41which dimensions, what's transpose,
- 2:21:43what's not, which dimensions.
- 2:21:45But
- 2:21:46basically, you multiply so this going to
- 2:21:48be your query, this going to be your
- 2:21:49keys, for example. Or you can have query
- 2:21:52like this and keys like this.
- 2:21:54Uh it's just a matrix multiplication.
- 2:21:57And then
- 2:21:59this is dividing
- 2:22:02>> [snorts]
- 2:22:02>> because here as I said, you just have
- 2:22:05one query. So, the result will just be
- 2:22:07also one vector.
- 2:22:11And
- 2:22:12you want to kind of normalize that
- 2:22:14vector. If it has some large numbers,
- 2:22:16neural networks don't like large
- 2:22:18numbers.
- 2:22:19Uh it's [snorts] a bit tough to explain,
- 2:22:22but
- 2:22:23mere multiplication of large numbers,
- 2:22:27it behaves differently and it can
- 2:22:29influence
- 2:22:30so
- 2:22:32not to overcomplicate, it will explain
- 2:22:34that later.
- 2:22:35Uh neural network learns better if the
- 2:22:37numbers are small, between zero and one
- 2:22:39or a bit maybe a bit bigger than one,
- 2:22:42but not too much bigger.
- 2:22:45So, that's why we just multiply by the
- 2:22:47square root
- 2:22:49of the number of dimensions of these
- 2:22:52key or query. So, for example, if
- 2:22:54queries has 500 dimensions, it's a
- 2:22:57vector 500 numbers long, you you divide
- 2:22:59by the square root of 500. And
- 2:23:02key and query will have same
- 2:23:04dimension.
- 2:23:08Then comes this softmax. So, remember,
- 2:23:11this is result of this is just an array
- 2:23:15and it has some numbers.
- 2:23:18So, maybe biggest number will be here.
- 2:23:20It just dot products. So, they don't yet
- 2:23:23add up to one or to 100%. They are just
- 2:23:26like big number here, a bit smaller
- 2:23:28number here, smaller. So, you need to
- 2:23:30make them add up to one or 100%. That's
- 2:23:33why you do softmax. You pass them
- 2:23:34through softmax so they all add up to
- 2:23:37one or 200%.
- 2:23:39While retaining their relative position.
- 2:23:42So, this
- 2:23:43guy that had the biggest dot product
- 2:23:45will still be the highest. Will still
- 2:23:47get the highest percentage.
- 2:23:49Let's see this on an example. So, these
- 2:23:52could be our values. So, uh this is a
- 2:23:56value vector for the
- 2:23:58cat set. And these are our weights.
- 2:24:02So, it means like to generate the next
- 2:24:06uh word,
- 2:24:08this is 10% important, uh 30% important,
- 2:24:12and 60% important.
- 2:24:16And so, we multiply 0.1 times this first
- 2:24:21and second
- 2:24:2230% times this
- 2:24:25and this times this, and then we add up
- 2:24:28all of the vectors.
- 2:24:30We add them up so we so we add first
- 2:24:34dimension here
- 2:24:35after the multiplication. So, first
- 2:24:37dimension, second dimension. So, we add
- 2:24:39element-wise vectors. We get just one
- 2:24:42vector, so four values.
- 2:24:45It's called like a weighted value
- 2:24:47vector.
- 2:24:49This is the example. So, multiply each
- 2:24:52vector.
- 2:24:53So, you get these numbers.
- 2:24:56And then you add
- 2:24:58uh this guy goes here. This guy goes
- 2:25:02here. This guy So, you add all of the
- 2:25:04third elements into this, all of the
- 2:25:06fourth elements into this.
- 2:25:08So, this is the weighted value vector
- 2:25:10that you get in the end.
- 2:25:12And now, this output will be kind of
- 2:25:14dominated by this
- 2:25:16uh third vector because you see this
- 2:25:18these values are very small because our
- 2:25:21weight is 0.1.
- 2:25:24And this these values are going to be
- 2:25:26biggest because our weight is 0.6. So,
- 2:25:30the output will
- 2:25:31uh don't be dominated by this
- 2:25:34because it has large weight.
- 2:25:38Here, I explained uh query key value
- 2:25:40again.
- 2:25:41So, um
- 2:25:44example. So, query is what I'm looking
- 2:25:46for.
- 2:25:49If processing the word set, the query
- 2:25:51might encode, "I need to find the
- 2:25:54subject of this action."
- 2:25:56So, then it will fire when it sees a
- 2:25:58cat, it will fire. So, when you have
- 2:26:01a query of the word set and it sees word
- 2:26:04cat, it will have high
- 2:26:07dot product between the query of the
- 2:26:10word set
- 2:26:11and key of the word cat because
- 2:26:14this query learned to adjust its weights
- 2:26:17to look for nouns or subjects.
- 2:26:21The key,
- 2:26:23for example, of the word cat might say,
- 2:26:25"I am a noun, an animal, a potential
- 2:26:28subject."
- 2:26:30So,
- 2:26:32uh as I said, it's going to have this
- 2:26:33key vector. And key vector will just
- 2:26:36have a high dot product with this query
- 2:26:38vector.
- 2:26:39That's how neural network just learns
- 2:26:41those weights to be to take to get a
- 2:26:43high product
- 2:26:45between set and cat.
- 2:26:48And value is the actual vector that gets
- 2:26:51added up, the actual information it
- 2:26:53gives. So, it's different from
- 2:26:55describing what it has. The key The key
- 2:26:58describes what value has.
- 2:27:02Or what value will give.
- 2:27:05So, if you go down in my school, uh here
- 2:27:08we have learn to code and build LLMs
- 2:27:10from scratch, you can check a few of
- 2:27:13these. Uh I'm ac- actually explaining
- 2:27:16all of this over and over again in all
- 2:27:17of these.
- 2:27:19So, it will just require like a lot of
- 2:27:21practice, explanations. I'm going to add
- 2:27:23more
- 2:27:24exercises videos.
- 2:27:26So, that's going to be it for this video
- 2:27:28and uh see you in the next lesson.
- 2:27:30Welcome to self-attention lesson.
- 2:27:33Here, we will focus more on code so you
- 2:27:35can check the first lesson, attention
- 2:27:37mechanism explained.
- 2:27:39And here, it's like code implementation.
- 2:27:41So,
- 2:27:42uh key, query, and value, which I
- 2:27:45explained, are all linear layers. So, I
- 2:27:48I mean,
- 2:27:49they are created with linear layers.
- 2:27:52You pass in embedding dimension, which
- 2:27:53is the vector dimension, the model LM
- 2:27:57dimension.
- 2:27:59And
- 2:28:00in this case, we will have the same
- 2:28:02dimension for the
- 2:28:03query and key and value, okay?
- 2:28:05Sometimes, you can have smaller
- 2:28:08dimensions here.
- 2:28:09But So, this will just go from embedding
- 2:28:12dimension to embedding, but it will
- 2:28:14transform. So, it will have some weights
- 2:28:16and transform this vector embedding into
- 2:28:19its query.
- 2:28:20So, this query will say what information
- 2:28:24is this token looking for. I explained
- 2:28:26this in the last video.
- 2:28:28Key is describing the information and
- 2:28:31value is the actual information. So, key
- 2:28:33is describing what value is.
- 2:28:36And all of them are
- 2:28:39uh linear layers that generate this key,
- 2:28:43query, and value and same input.
- 2:28:45If we scroll down a bit, we can see a
- 2:28:48classic example in a large language
- 2:28:50model where you have
- 2:28:52sequence length, which are just like
- 2:28:55your conversation, your words.
- 2:28:58So, your context window when you talk to
- 2:29:00chat GPT, okay?
- 2:29:01And batch means there is like a multiple
- 2:29:03context windows, multiple independent
- 2:29:05conversations. Okay? And every token
- 2:29:10in that sequence is represented with a
- 2:29:12vector.
- 2:29:14So, that's how we have
- 2:29:15vector or this is token.
- 2:29:18Sequence length and batch,
- 2:29:20which can be this.
- 2:29:2232
- 2:29:23um
- 2:29:24conversations.
- 2:29:28I changed it to conversations.
- 2:29:31Each conversation has 100 tokens and 512
- 2:29:35dimensions, for example, for each token.
- 2:29:38So, we pass
- 2:29:40X
- 2:29:41all of this this entire thing.
- 2:29:44We generate the queries for every token,
- 2:29:46okay? So, there is 32 * 100 tokens.
- 2:29:50Generate query for each,
- 2:29:52key for each, value for each. That's how
- 2:29:54we code it.
- 2:29:56And so, we have
- 2:29:57>> [clears throat]
- 2:29:58>> query matrix all queries key value.
- 2:30:01So, the scores are calculated with
- 2:30:03queries at queries
- 2:30:06matrix multiply
- 2:30:08key transpose where we transpose the
- 2:30:09last and second to last because
- 2:30:12that's just how matrix multiplication
- 2:30:14works. This is This is for the reasons
- 2:30:16of matrix multiplication. You need to
- 2:30:18have this transposed and that's it it.
- 2:30:21So, the shape will be batch size
- 2:30:24sequence length sequence length. So,
- 2:30:26So, here
- 2:30:28every token
- 2:30:30will have score with every other token.
- 2:30:34So, there will be score between every
- 2:30:36single pair of tokens. So, it's like a
- 2:30:38square.
- 2:30:40That's why they say attention grows
- 2:30:42quadratically. For every token, you need
- 2:30:44to add all of the previous tokens to the
- 2:30:46score list.
- 2:30:49So, the scores again you can imagine a
- 2:30:51similarity between these two tokens or I
- 2:30:55should say affinity or how much this
- 2:30:58token cares for this other token, how
- 2:31:00much of its information it wants to take
- 2:31:02its context.
- 2:31:05And then we will just scale them back
- 2:31:07for stability reasons. So, this this
- 2:31:11this means like square root.
- 2:31:14So, this is the square root of the
- 2:31:16embedding dimension of the
- 2:31:18of the dimension of the queries or or
- 2:31:20keys.
- 2:31:22And then we apply softmax to scores to
- 2:31:25put them between like add up to 100%.
- 2:31:28So, we have like percentages.
- 2:31:31And then
- 2:31:32those are weights. So, now
- 2:31:36for every token
- 2:31:39I explained this in the previous lesson,
- 2:31:41but here
- 2:31:43we want to
- 2:31:45for every token, we take its value and
- 2:31:47add all of the
- 2:31:49values of other tokens, but weighted.
- 2:31:53So,
- 2:31:54if for this token like this other token
- 2:31:56is very important, then
- 2:31:58this attention weights will actually
- 2:32:00multiply its value with a high number
- 2:32:02and then we add just like add
- 2:32:05element-wise this vector.
- 2:32:07But if some
- 2:32:08uh value is not so important, it will
- 2:32:11get weighted by a smaller multiply by a
- 2:32:13smaller number, smaller attention
- 2:32:15weight.
- 2:32:17And now this this
- 2:32:18uh condensed or squished uh numbers that
- 2:32:22are around maybe zero will all get added
- 2:32:24to this value.
- 2:32:26So, it will not influence too much this
- 2:32:28value that it's added to. So, it didn't
- 2:32:30take too much context from this token.
- 2:32:34I explained this in previous lesson you
- 2:32:35can check also.
- 2:32:37So, then you would be able to just
- 2:32:39define self-attention with the
- 2:32:42dimension you want for keys, queries,
- 2:32:44values.
- 2:32:45And then you would
- 2:32:47just pass in some X through the
- 2:32:49attention
- 2:32:51and you should get the output
- 2:32:52immediately,
- 2:32:54which is a list of values for each token
- 2:32:57that's just stuck with the
- 2:32:59context
- 2:33:01from other tokens.
- 2:33:03So, here we don't have causal
- 2:33:05self-attention. So, here
- 2:33:08every token is taking
- 2:33:10attention from all of the previous and
- 2:33:12forward tokens.
- 2:33:13But when we're training neural network,
- 2:33:15we shouldn't be able to see forward
- 2:33:16tokens because then because then that
- 2:33:18would be easy to predict forward tokens
- 2:33:20if we see them.
- 2:33:22So, but it's not implemented here, but
- 2:33:23I'm telling you we will learn this. So,
- 2:33:25we will actually just for each value we
- 2:33:26will
- 2:33:28uh take context or do multiplications
- 2:33:31with just previous tokens.
- 2:33:35So, that's going to be a short lesson in
- 2:33:36implementing attention in code.
- 2:33:39Let's learn to build and code a
- 2:33:41GPT-style LLM. So, this is like chat
- 2:33:43GPT.
- 2:33:45I recommend you watch these two previous
- 2:33:46lessons on attention mechanism explained
- 2:33:49and self-attention from scratch.
- 2:33:51So, if you've seen transformer
- 2:33:52architecture, there are two parts and
- 2:33:54this is good for sequence to sequence.
- 2:33:57When you have encoder and decoder. So,
- 2:33:59classic transformer architecture is good
- 2:34:02when you have like some sequence of
- 2:34:04words in one language, you want to
- 2:34:06translate it into different language,
- 2:34:08but
- 2:34:10GPT has only this part, only decoder.
- 2:34:13And that's good when you just want to
- 2:34:14keep generating text.
- 2:34:18And as it turns out, maybe this
- 2:34:20decoder-only transformer is also better
- 2:34:22for translation and sequence to
- 2:34:24sequence. So, that's probably also
- 2:34:27it works better in practice even though
- 2:34:28the idea here is cool to
- 2:34:31have like two parts for two
- 2:34:33languages.
- 2:34:35But anyways, in our GPT architecture, we
- 2:34:37just have decoder and we'll explain more
- 2:34:40what that is.
- 2:34:42We know that large language model will
- 2:34:44predict the next word.
- 2:34:46So, if we have once upon a time
- 2:34:49very far away there, it's going to
- 2:34:52predict lived as the next likely word.
- 2:34:56So, the four parts of transformer, we
- 2:34:59have causal masked multi-head
- 2:35:02self-attention,
- 2:35:04which is actually explained here in
- 2:35:06self-attention. I will explain what
- 2:35:08multiple heads mean.
- 2:35:11And then add a normalization. So,
- 2:35:15when you have input, you pass input
- 2:35:17token or tokens through self-attention.
- 2:35:21And after that's processed,
- 2:35:23you actually want to add same input
- 2:35:25and add this processed
- 2:35:28tokens with self-attention and add to
- 2:35:30the input that's not processed. So, add
- 2:35:32them together.
- 2:35:33And when adding vectors,
- 2:35:36uh then neural network will actually you
- 2:35:38give both information, both processed
- 2:35:40information and raw information for the
- 2:35:42neural network.
- 2:35:43So,
- 2:35:45it has both both of the information and
- 2:35:47then it can easily use both.
- 2:35:49If you remove this residual connection
- 2:35:52that just adds unprocessed information,
- 2:35:55then neural network or language model
- 2:35:57will learn worse and slower because
- 2:36:01this initial tokens also contain
- 2:36:03important information. So, we we don't
- 2:36:05just want to process and just have that,
- 2:36:07but also the initial. So, you want to
- 2:36:09add those vectors.
- 2:36:12And this normalization this can happen
- 2:36:16usually before self-attention
- 2:36:18and it's just to keep the vectors, the
- 2:36:21numbers in a nice range between around
- 2:36:24zero and one or minus one
- 2:36:26because big numbers or very small
- 2:36:27numbers can cause problems like gradient
- 2:36:31vanishing gradient exploding.
- 2:36:33Because when you multiply numbers near
- 2:36:35zero near sorry near one, they're going
- 2:36:37to stay result is also going to stay
- 2:36:39near one.
- 2:36:40But if you multiply numbers like
- 2:36:42thousands and millions,
- 2:36:43the other numbers next numbers results
- 2:36:46will explode. And maybe
- 2:36:48such big numbers cannot be represented
- 2:36:50in computers.
- 2:36:52So, we want to keep our numbers around
- 2:36:54one zero.
- 2:36:57Feed forward network, it's just a single
- 2:36:59multi-layer perceptron.
- 2:37:01So, it takes as input this processed and
- 2:37:04normalized. So, usually you would also
- 2:37:06put normalization before feed forward
- 2:37:08and this mask before feed forward.
- 2:37:11In the original transformer, it was
- 2:37:13after, but now we would put like before.
- 2:37:17So,
- 2:37:19uh
- 2:37:20at the output of self-attention and the
- 2:37:24residual network, when we add those two,
- 2:37:26so this add and norm.
- 2:37:29So, I guess norm would go before, but
- 2:37:31add would go after.
- 2:37:36So,
- 2:37:37uh with that added vector result, we can
- 2:37:40normalize that again
- 2:37:42with this norm that would that we would
- 2:37:44put put
- 2:37:46before feed forward.
- 2:37:49And then put that into feed forward. So,
- 2:37:50that's input. Now, we expand into the
- 2:37:53middle layer, hidden layer. It's going
- 2:37:55to be like four times bigger usually
- 2:37:59uh than this input. And so, in this
- 2:38:02middle layer of our feed forward
- 2:38:03network,
- 2:38:05uh there there are a lot of facts like
- 2:38:08about basketball players, like all of
- 2:38:10the knowledge that neural network
- 2:38:11learned.
- 2:38:13It's like a data storage. And also it
- 2:38:16also transforms
- 2:38:19uh
- 2:38:20this these tokens
- 2:38:22in different ways. So, it has multiple
- 2:38:25purposes.
- 2:38:27And then in the hidden layer that's four
- 2:38:28times bigger than the token size in the
- 2:38:30input, it shrinks back to the token size
- 2:38:34at the output.
- 2:38:36And that's our one pass through the
- 2:38:38transformer. Now, that can go back into
- 2:38:40attention into the next pass, next layer
- 2:38:44back again. And then we can repeat this
- 2:38:46cycle 10 20 30 maybe 60 times in big uh
- 2:38:50neural big large language models.
- 2:38:52So, we'll explain all of this again,
- 2:38:54don't worry.
- 2:38:56So, these would be example hyper
- 2:38:59parameters here.
- 2:39:01So, uh number of heads in
- 2:39:05uh self attention. So,
- 2:39:08we will just divide each token vector in
- 2:39:11self attention into different parts,
- 2:39:13independent parts.
- 2:39:14And so, we will then do attention on
- 2:39:17those independent parts, which are like
- 2:39:19independent key query value, independent
- 2:39:21key query value, all coming from the
- 2:39:23same token.
- 2:39:25But we will divide again into multiple
- 2:39:27because now each head will learn
- 2:39:29different things. Maybe some head will
- 2:39:31learn to like do mathematics, some head
- 2:39:34will learn about medicine, etc.
- 2:39:38Uh
- 2:39:39This is the dimension of the feed
- 2:39:40forward hidden layer that I was
- 2:39:42explaining that was four times bigger,
- 2:39:45and here it is four times bigger because
- 2:39:47D model is the token embedding size, and
- 2:39:52feed forward hidden dimension is four
- 2:39:54times bigger.
- 2:39:56Dropout
- 2:39:57just to prevent overfitting. Vocabulary
- 2:40:00size, usually this is like 50,000 to
- 2:40:02200,000 on big models.
- 2:40:05And this is the maximum context window
- 2:40:06sequence, like maximum of 512 tokens
- 2:40:09that you can have in your
- 2:40:11language model.
- 2:40:13And number of blocks can also be called
- 2:40:16number of layers. So, how many times do
- 2:40:19we repeat this process? So, self
- 2:40:22attention norms feed forward norm. So,
- 2:40:24that's one, okay. Then we go output
- 2:40:27here, we go back in.
- 2:40:29And then back in again, so three times
- 2:40:31in this case, three blocks or three
- 2:40:32layers.
- 2:40:34And these earlier layers
- 2:40:37will learn something.
- 2:40:40Maybe to find out some basic features in
- 2:40:43text, and then later layers will learn
- 2:40:45maybe more complex things to generate
- 2:40:47next tokens or to give information
- 2:40:50information necessary to generate next
- 2:40:52tokens.
- 2:40:54Then we have rotary positional
- 2:40:56embeddings. This is how we encode
- 2:40:59which token comes after which token.
- 2:41:02This is the order of tokens.
- 2:41:04So, is cat chasing the dog or dog
- 2:41:06chasing the cat?
- 2:41:08We use rope to encode the position of
- 2:41:10the words, of the tokens.
- 2:41:12And
- 2:41:14uh let's go all the way down. This is
- 2:41:16where it happens.
- 2:41:17So, we have our
- 2:41:20um token
- 2:41:22our vector embedding for the token, and
- 2:41:25we split it into two.
- 2:41:29And
- 2:41:30the first So, we have
- 2:41:32X1 is the first half of the token, X2 is
- 2:41:34the second half of the token.
- 2:41:36And then we apply this rotation matrix.
- 2:41:39So, rotation matrix is like a term in
- 2:41:42mathematics.
- 2:41:43When you have a vector,
- 2:41:45you can rotate the vector by multiplying
- 2:41:48it with this rotation matrix.
- 2:41:52So, the idea here is that
- 2:41:56um
- 2:41:58it's a bit more complex, but I'll
- 2:41:59explain. You want to rotate closer
- 2:42:02tokens or early tokens in the sentence
- 2:42:06maybe less,
- 2:42:08and later tokens in the sentence more.
- 2:42:11So, the later the token, the more you
- 2:42:12want to rotate its vector.
- 2:42:15That's how by this rotation
- 2:42:18neural network will learn
- 2:42:20like the order of the tokens, how much
- 2:42:23it will just see how much
- 2:42:25uh every token is rotated to understand
- 2:42:27their orders.
- 2:42:28And it's also good because
- 2:42:31it can also compare relatively tokens
- 2:42:34So, third token and fifth token, it can
- 2:42:37see that there is like two tokens in
- 2:42:39between
- 2:42:41uh relative to each other.
- 2:42:43So, we don't need absolute positions
- 2:42:45like
- 2:42:46uh this is absolutely what first,
- 2:42:48second, third. There is also a relative
- 2:42:50besides the absolute.
- 2:42:53So, this rotation matrix, the matrix
- 2:42:55that rotates the tokens, can be
- 2:42:57expressed with uh sine and cosine.
- 2:43:00And this is how we can This is one way
- 2:43:01to do it.
- 2:43:02So, we multiply first half
- 2:43:05with cosine minus second half with sine,
- 2:43:09that's going to be our rotated X1. This
- 2:43:11is our first half now.
- 2:43:13And the second half
- 2:43:15uh to get the new second half, we're
- 2:43:16going to rotate the previous the
- 2:43:18original first half with sine plus
- 2:43:21second half with cosine, so that's going
- 2:43:23to be our new second half.
- 2:43:25And then we concatenate the first and
- 2:43:27second half.
- 2:43:29And that's our new vector that now token
- 2:43:32embedding that now encodes
- 2:43:34positional information.
- 2:43:38And this code above is just how we
- 2:43:40generate uh this sine and cosine, and
- 2:43:43how we decide
- 2:43:45how much
- 2:43:47for each position we should rotate, what
- 2:43:50the sine and cosine should be for each
- 2:43:51position.
- 2:43:52But this is a bit complex, so we'll
- 2:43:54leave this for another time. This is not
- 2:43:57so important now.
- 2:43:59This is a very good example here of the
- 2:44:02code. So,
- 2:44:03uh this is in Andrej Karpathy's nanoGPT.
- 2:44:07And
- 2:44:08apply rotary embeddings, we just get the
- 2:44:10X, our token embedding cosine and sine.
- 2:44:15And
- 2:44:17uh we will get we will split
- 2:44:21into X1 and X2 all of the X's. So, X is
- 2:44:24like not just in this case, this is not
- 2:44:26just one vector, it's all of the
- 2:44:27vectors.
- 2:44:29So, we will split every vector in
- 2:44:31halves, and then
- 2:44:33we will apply the same rotation that I
- 2:44:35just explained,
- 2:44:37and then concatenate.
- 2:44:39Uh this is the dimension because
- 2:44:42uh now we don't just have one vector, we
- 2:44:44have like a whole batch of sequences of
- 2:44:47vectors.
- 2:44:49But the logic is same as I explained.
- 2:44:53Then uh let's look at some code in
- 2:44:55implementing causal self attention with
- 2:44:57rope.
- 2:44:58Uh we have a class causal self attention
- 2:45:01and that inherits from NN.Module.
- 2:45:04And we need to make sure that
- 2:45:08the token embedding is divided into
- 2:45:11number of heads
- 2:45:12uh without like it should be divisible
- 2:45:15by number of heads because every head
- 2:45:17should have equal number of uh numbers
- 2:45:21dimensions.
- 2:45:23And then head dimension will be just
- 2:45:24dividing the token dimension with number
- 2:45:27of heads, that's
- 2:45:28how many numbers or elements each head
- 2:45:30has.
- 2:45:33And then we have uh query key value.
- 2:45:37So, in this case, we do
- 2:45:39all the query key value in one go.
- 2:45:43So, we go from D model to D model times
- 2:45:46three because D model is the token
- 2:45:49dimension.
- 2:45:51And
- 2:45:53our query key value will also be the
- 2:45:56token dimension, same size.
- 2:46:00And you can learn more about query key
- 2:46:02value in the previous video, previous
- 2:46:04lessons about self attention.
- 2:46:07So, we want to generate three of them.
- 2:46:08We want to generate three uh query key
- 2:46:10value, and each is
- 2:46:13uh each has size of the token, so we
- 2:46:15need three times size the token.
- 2:46:18So, we go from we go we have a linear
- 2:46:20layer, it's like some learned network
- 2:46:22weights that's going to generate our key
- 2:46:25query and value.
- 2:46:26And we input the token,
- 2:46:28and we output three times the token
- 2:46:30size, which is going to be now different
- 2:46:32numbers.
- 2:46:36And so, later we will just split this
- 2:46:38into three. Like take the first third as
- 2:46:41uh query, second third as key, uh last
- 2:46:44third as value.
- 2:46:46We also have output head.
- 2:46:48So, after we combine all of the tokens,
- 2:46:51so after we do a attention with tokens,
- 2:46:54uh this will just convert because
- 2:46:57at the end of attention, we just added
- 2:46:59values from each attention, each token
- 2:47:02to the last one, we just added values.
- 2:47:05Now we need to convert from this
- 2:47:06mixed-up value vector to the actual
- 2:47:10token embedding
- 2:47:11to exit attention mechanism.
- 2:47:15And keep in mind that in this case,
- 2:47:17value key query value are same dimension
- 2:47:19as token, but it doesn't have to be.
- 2:47:21Actually, in big LLMs,
- 2:47:24key query value are smaller dimension.
- 2:47:27So, at the end of attention mechanism
- 2:47:29where we added all of the weighted
- 2:47:31values, we need to convert that mixed-up
- 2:47:34value vector into the token embedding
- 2:47:36vector.
- 2:47:38And do that for every token if we are
- 2:47:40training, or just for one token if we
- 2:47:42are inferencing. But we'll talk about
- 2:47:44that more later.
- 2:47:46And we initialize rope here as well,
- 2:47:49passing self head dimension. In this
- 2:47:51case, this is how we defined it, so we
- 2:47:53need to pass in
- 2:47:54uh dimension of the head.
- 2:47:57And then
- 2:47:58uh in this case, you want to get
- 2:48:00uh the batch size, how many independent
- 2:48:02conversations, sequence length length,
- 2:48:05what is the maximum uh number of tokens
- 2:48:08in the sequence.
- 2:48:09And I guess we don't need the token
- 2:48:11dimension here.
- 2:48:13And so, we generate
- 2:48:14key query values, so by just
- 2:48:18passing X through our linear layer that
- 2:48:20generates key query values.
- 2:48:22And then split. So, this will get us
- 2:48:25like one big vector, three times
- 2:48:29uh
- 2:48:30model dimension.
- 2:48:31And then we split it into three parts
- 2:48:33for query key value.
- 2:48:38And then we need to split query, key,
- 2:48:40and value into heads.
- 2:48:42As I said, we will split query, key,
- 2:48:45value into heads, so each head will
- 2:48:46learn different things. Like maybe one
- 2:48:48will be about math or calculation or
- 2:48:50nouns or verbs. The other will do
- 2:48:52something else.
- 2:48:54So,
- 2:48:57uh we will use query.view batch size
- 2:49:00sequence length that already exists.
- 2:49:03But,
- 2:49:04that
- 2:49:05where previously was one dimension here.
- 2:49:07This was model dimension. Now we split
- 2:49:09into two dimensions.
- 2:49:11So, number of heads and head dimension.
- 2:49:14Number of heads and head dimension for
- 2:49:16each head.
- 2:49:19So, now we do that for all of these.
- 2:49:22And we can apply rope
- 2:49:25then to query and key. We don't need to
- 2:49:27apply rope for the value. Just query and
- 2:49:30key.
- 2:49:32So, next in attention calculation,
- 2:49:35we want to do heads independently.
- 2:49:38So, we will transpose this to put heads
- 2:49:41first. This is the batch size and heads
- 2:49:43are independent dimensions.
- 2:49:46And then
- 2:49:48in every sequence length, we will do
- 2:49:51heads of the key, query, value. So,
- 2:49:54sequence length is
- 2:49:56For the sequence length, we will do
- 2:49:58same head. So, this is separate. Here we
- 2:50:01will separate them. Here we will we will
- 2:50:02also separate them here.
- 2:50:04But, now here and here they're together.
- 2:50:05They're grouped up by by head.
- 2:50:09And so, we do whole sequence length
- 2:50:12of the query, key, value together
- 2:50:15for this particular head and for the
- 2:50:17next head, etc.
- 2:50:20And then this is the formula for
- 2:50:21attention.
- 2:50:23And we talked about this formula in the
- 2:50:25previous lessons. This is how you do it.
- 2:50:29And then we want to apply mask so that
- 2:50:33every token in a sequence can only look
- 2:50:36at the previous tokens
- 2:50:38and not the future tokens.
- 2:50:41So, it can only pay attention to that
- 2:50:42this is during the training, by the way.
- 2:50:45So, in inference, we don't need masks
- 2:50:46because we just have a sequence. We just
- 2:50:49do the next token once. We don't like
- 2:50:52need to calculate every every token with
- 2:50:55every other previous token in inference
- 2:50:58when you are generating, okay? But, when
- 2:50:59we are training, we do need to calculate
- 2:51:01every token with every previous token
- 2:51:04to train better.
- 2:51:06To train on the whole thing.
- 2:51:08So, this mask again will just make sure
- 2:51:11that tokens cannot look at so, query of
- 2:51:14the token cannot look at the keys of the
- 2:51:16following tokens because
- 2:51:18it's learning to predict those.
- 2:51:19So, query can only look at the keys of
- 2:51:21the previous tokens
- 2:51:23for every token.
- 2:51:26And then we calculate softmax, which
- 2:51:30previously these scores are just
- 2:51:32numbers, logits. They don't add up to
- 2:51:35one. They don't add up to 100%. So, they
- 2:51:38can be like two, one, 0.5, whatever. And
- 2:51:42we need to make them add add up to 100%
- 2:51:45because
- 2:51:46let's take example this this token.
- 2:51:49It needs to know how much to take of the
- 2:51:52information of context of every other
- 2:51:54previous token.
- 2:51:56So, it's a lot easier to do that if you
- 2:51:58just have them add up to 100%. So, it
- 2:52:01knows I should take 20% from this token
- 2:52:05in from 20% of its value,
- 2:52:07uh 10% of this value, 5% of this value.
- 2:52:10So, that's what softmax will do. It will
- 2:52:12just make the scores
- 2:52:15for every So, for every token, all of
- 2:52:17the scores
- 2:52:18with every other token add up to one,
- 2:52:21200%. So, it knows how much.
- 2:52:24And then we can drop out for like ignore
- 2:52:26some
- 2:52:28of the scores
- 2:52:30to stabilize training
- 2:52:32to
- 2:52:33make it not so dependent on some
- 2:52:35particular score scores or some
- 2:52:37particular positions.
- 2:52:39So, this part outputs is where we
- 2:52:41perform the weighted sum of the values.
- 2:52:44So, now this is where it takes context
- 2:52:46from every previous token
- 2:52:48based on the score, the importance.
- 2:52:53Uh I explained here
- 2:52:55V is the matrix of values for every
- 2:52:58token in the sequence.
- 2:52:59For example, V of the the, V of the cat,
- 2:53:03V of the set.
- 2:53:06So, when we multiply
- 2:53:08uh attention scores and values,
- 2:53:12this is what it looks like. For example,
- 2:53:14output for the word set,
- 2:53:17it's going to do 0.1 or 10% times value
- 2:53:20of the plus 0.6
- 2:53:24times value of cat plus
- 2:53:260.3 times value of set. And usually you
- 2:53:29will see that
- 2:53:30these tokens usually take most of their
- 2:53:33own value. So, maybe I should put 0.6
- 2:53:36here
- 2:53:38and 0.6 there.
- 2:53:42So, that's how we will get weighted sum
- 2:53:45of the values for
- 2:53:47for each token
- 2:53:49of the every previous token into that
- 2:53:51token. So, that's how we do the context.
- 2:53:56And then after we do that, we need to
- 2:53:58now go back with the swapping heads and
- 2:54:01sequence length. So, now we need to put
- 2:54:03sequence left length after batch.
- 2:54:06And so, we have sequence length and then
- 2:54:10we will combine head number of heads and
- 2:54:12head dimension to get
- 2:54:14model D because
- 2:54:16now we want to transform from the key,
- 2:54:19query, value space into the token space.
- 2:54:21So,
- 2:54:22we will use this this uh weight. So,
- 2:54:25this output layer.
- 2:54:27So, output is just our still it's our
- 2:54:30values, value space.
- 2:54:32And we pass that through this linear
- 2:54:34layer to get actual token space, token
- 2:54:36embedding.
- 2:54:38But, before we pass our values, we need
- 2:54:40to
- 2:54:41we need to
- 2:54:43convert them uh to the proper shape. So,
- 2:54:47combine heads into one single vector.
- 2:54:52Then we have the transformer block where
- 2:54:54we combine all of that.
- 2:54:56So, here we want to initialize feed
- 2:54:58forward, which we didn't make yet, by
- 2:55:00the way.
- 2:55:02So, this is very simple. It has
- 2:55:06It goes from model from token embedding
- 2:55:08into the hidden four times bigger.
- 2:55:11And then go back to token embedding. And
- 2:55:13in this hidden four times bigger, you
- 2:55:15have all of the facts living there, all
- 2:55:17of the knowledge, and some ways to
- 2:55:19transform tokens
- 2:55:21as well. Because imagine attention as
- 2:55:23just collecting information into the
- 2:55:25token, last token,
- 2:55:27in order to predict the next token.
- 2:55:30And then this feed forward will process
- 2:55:32that information, mix it up, do
- 2:55:33something with it,
- 2:55:35uh create new insights.
- 2:55:38So, I also have dropout between those
- 2:55:41expansion and
- 2:55:42contraction layers.
- 2:55:45So, this is the whole formula. So, we
- 2:55:47pass our token into first linear,
- 2:55:50use activation like ReLU, although
- 2:55:52this could be maybe SiLU, SwiGLU,
- 2:55:56but let's now use ReLU. ReLU is simple
- 2:55:58and fast to compute.
- 2:56:00And dropout to not to make so, to
- 2:56:02prevent overfitting.
- 2:56:03And then scale back into the token size.
- 2:56:08In our transformer block, we will
- 2:56:10initialize causal self-attention,
- 2:56:14feed forward, and then two
- 2:56:15normalizations. In this case, it's layer
- 2:56:17norm, but I should actually change this
- 2:56:19to RMS norm.
- 2:56:21RMS norm is just used more these days.
- 2:56:25And the dropout as well. And this is the
- 2:56:27forward of the transformer. Okay? So, we
- 2:56:30pass in first through the attention.
- 2:56:34I guess we get input and mask here.
- 2:56:38Although, we could probably generate
- 2:56:39mask within attention as well.
- 2:56:42Pass into the attention and then
- 2:56:44normalize.
- 2:56:47Although, we could also put
- 2:56:48normalization before attention, but
- 2:56:51let's keep it this way now.
- 2:56:54And then uh feed forward, pass through
- 2:56:55feed forward, normalize, and that's it.
- 2:56:57That's our transformer block.
- 2:57:00And then we have multiple of those
- 2:57:02blocks and output head and token. And
- 2:57:05so, we need to now combine everything
- 2:57:06into one.
- 2:57:08Here we need the entire vocabulary
- 2:57:11of tokens. So, token embedding
- 2:57:13vocabulary will just be
- 2:57:16all of the tokens, vocab size. And each
- 2:57:19of the tokens has its own embedding.
- 2:57:22So, vocab size times
- 2:57:24uh D model.
- 2:57:27Then we also define these uh list of
- 2:57:29blocks of transformer blocks that I was
- 2:57:31just explaining. We will just do
- 2:57:32multiple. Number of blocks is going to
- 2:57:34be three in our case, if you remember
- 2:57:36from the beginning of the video.
- 2:57:39And this is I guess final norm
- 2:57:41normalization that we get. And this is
- 2:57:43the output head that it generates the
- 2:57:45next token. So, this is after all of the
- 2:57:47blocks, after everything.
- 2:57:49Output head will generate next token.
- 2:57:51And it has
- 2:57:52D model times vocab size. So,
- 2:57:56so it's basically going to
- 2:57:59for every
- 2:58:00token in the vocabulary,
- 2:58:03it will generate probability that this
- 2:58:05is the next token.
- 2:58:07So, it converts our vector embedding
- 2:58:10from the transformer into set of
- 2:58:13probabilities,
- 2:58:1450,000 probabilities if we have 50,000
- 2:58:17tokens.
- 2:58:19It's just a linear layer.
- 2:58:22Vector embed token embedding into set of
- 2:58:25probabilities over entire vocabulary.
- 2:58:29So, this can actually be huge, and you
- 2:58:30need some you can So, this can be 1/3 of
- 2:58:33the entire language model
- 2:58:36for small models. But, there are some
- 2:58:38tricks.
- 2:58:39For example, Apple's cut cross
- 2:58:41attention.
- 2:58:43Maybe writing some kernels or GPU code.
- 2:58:47And let's see the forward pass through
- 2:58:49our GPT GPT style decoder only
- 2:58:53transformer.
- 2:58:55Uh we get our batch size sequence length
- 2:58:57and shapes. We get a device, which will
- 2:59:00be GPU.
- 2:59:02We can create masks here, or we can do
- 2:59:04it in the attention. So,
- 2:59:07uh this is how we create mask. As I
- 2:59:08said,
- 2:59:10I talked more about unsqueeze in my N
- 2:59:13torch once, and
- 2:59:16uh these
- 2:59:17uh
- 2:59:17PyTorch in my PyTorch fundamentals
- 2:59:19course in this school.
- 2:59:22So, we just want to make sure that we
- 2:59:24have a triangular matrix where
- 2:59:28um
- 2:59:29we have like a mask such that when we
- 2:59:32apply this to the attention,
- 2:59:35for every single query,
- 2:59:37it will only
- 2:59:39be able it will only have unmasked the
- 2:59:41previous keys, and it will have
- 2:59:43masked the following keys.
- 2:59:46So, this mask will, for example, be
- 2:59:48for the first
- 2:59:50token, it will have all of the masks for
- 2:59:52the following
- 2:59:54tokens, it will just have one unmask for
- 2:59:56itself to pay the attention
- 2:59:58to itself, to do uh query and key of
- 3:00:00itself. For the second token,
- 3:00:03it will have unmasked itself and
- 3:00:05previous token, but masked all of the
- 3:00:08next tokens.
- 3:00:09Third token will have first three. So,
- 3:00:11you you see how you have 1 2 3 4. So,
- 3:00:14it's like a triangle
- 3:00:15of masks, and this is triangle here is
- 3:00:17also masked, and this is unmasked.
- 3:00:20So, that's why we need this triangular
- 3:00:23matrix.
- 3:00:24And uh in the future, maybe uh we will
- 3:00:27talk more about this as well.
- 3:00:30Then, we will pass all of our tokens
- 3:00:32through our token embedding
- 3:00:35layer to replace
- 3:00:37all of the tokens with their embeddings.
- 3:00:39So, this will give us like list of
- 3:00:41embeddings instead of the list of
- 3:00:43tokens.
- 3:00:45And this is good for stabilization,
- 3:00:47dividing
- 3:00:49uh these tokens
- 3:00:51with the square root these embeddings
- 3:00:53with the square root of the
- 3:00:55model dimension.
- 3:00:59As I said, you want to keep like your
- 3:01:00numbers around one, around zero.
- 3:01:03So, that's what we build This will do.
- 3:01:04It will push them towards one.
- 3:01:07Then, we pass our X through our blocks.
- 3:01:11So, for block in self blocks, so we have
- 3:01:13three block we have
- 3:01:15We pass X through every transformer
- 3:01:18block, and what block includes attention
- 3:01:20and feed forward.
- 3:01:22So, we do that three times here in this
- 3:01:24loop.
- 3:01:26And then, we normalize at the end and
- 3:01:28generate
- 3:01:30the probability over all of the possible
- 3:01:33tokens
- 3:01:36uh to be the next token.
- 3:01:38But, these are logits. These are not
- 3:01:39probabilities. These are just like
- 3:01:40numbers. So, we can later turn them into
- 3:01:44probabilities with softmax.
- 3:01:46But, basically, these logits are also
- 3:01:48like numbers.
- 3:01:50Higher number means like it will later
- 3:01:51become higher probability for this token
- 3:01:53to be
- 3:01:54chosen.
- 3:01:56Okay, and then this is how we use and
- 3:01:58train this GPT.
- 3:02:00So, we initialize it.
- 3:02:03We select a device,
- 3:02:05which is going to be CUDA, which means
- 3:02:07GPU, and Nvidia GPU,
- 3:02:10if it's available.
- 3:02:11You better not train on CPU. That's I
- 3:02:13don't want to be funny. It's going to be
- 3:02:14so long.
- 3:02:15And then, put the model to device, too,
- 3:02:18to GPU. So, because you initialize it
- 3:02:19here, it can be initialized on CPU, you
- 3:02:22want to move it to GPU.
- 3:02:26And then, we can have like
- 3:02:29this uh some dummy data.
- 3:02:32So, this uh source, it's just going to
- 3:02:36be
- 3:02:37our random tokens, example tokens. So,
- 3:02:40this is let's say we have like some text
- 3:02:42here, but it's now randomly generated.
- 3:02:45Some tokens, some sequences, and stuff.
- 3:02:48And this is for the model training. We
- 3:02:49need cross entropy loss
- 3:02:52to
- 3:02:53uh check the model's prediction versus
- 3:02:55the real next token.
- 3:02:58And we will use Adam to update weights
- 3:03:01to make the model learn
- 3:03:04uh based on this loss
- 3:03:07to next time generate better loss.
- 3:03:09And loss in large language models should
- 3:03:12be around 1 to 2 to 3. If you have 0.001
- 3:03:17or 10,
- 3:03:18your loss,
- 3:03:20then that's there is some bug. I had
- 3:03:210.001 loss
- 3:03:23because I had some bug. And then, I
- 3:03:25discovered it, and so loss should be
- 3:03:27around 1 to 3.
- 3:03:30And then, model.train This will train
- 3:03:32because
- 3:03:33we are inheriting all of these like
- 3:03:35train and stuff from PyTorch. This is
- 3:03:38done by PyTorch.
- 3:03:41So, this is how you train large language
- 3:03:42model.
- 3:03:43You have a sequence.
- 3:03:45So, your inputs are going to be entire
- 3:03:47sequence except the last
- 3:03:50last token.
- 3:03:52And your target is going to be entire
- 3:03:54sequence except the first token.
- 3:03:58And I'll explain why, but you see here
- 3:04:00we take all of the batches, all of the
- 3:04:01conversations,
- 3:04:03all of the sequences, I should say.
- 3:04:06And everything inputs is everything
- 3:04:08except last,
- 3:04:09and target is everything except the
- 3:04:11first token. So, from first, so index
- 3:04:14zero, token zero is not because
- 3:04:18uh here in the inputs, we will start
- 3:04:20with just one token, and we want to
- 3:04:22predict the next token.
- 3:04:24So, the target cannot be the first token
- 3:04:26because we can start from the first. We
- 3:04:28cannot start from like nothing in line
- 3:04:30with it. So, we are not training model
- 3:04:32to start from nothing.
- 3:04:34We are training the model to start from
- 3:04:35at least one token.
- 3:04:37So, that's why target cannot be that
- 3:04:39that first token.
- 3:04:40Okay?
- 3:04:42And
- 3:04:43inputs will go until last, but not last
- 3:04:46because
- 3:04:47during the training,
- 3:04:49we can have
- 3:04:51it's possible to have all of the tokens,
- 3:04:54all of the sequence as input except last
- 3:04:57when we are predicting the last. Okay?
- 3:04:59So,
- 3:05:00all of these combinations are possible.
- 3:05:02So, you can have like half
- 3:05:04of the tokens
- 3:05:06uh given, and you need to predict the
- 3:05:08next half.
- 3:05:09Or you need to Okay, so you wouldn't
- 3:05:11predict You would just predict the next
- 3:05:12token.
- 3:05:13So, let let me simplify.
- 3:05:15All of the possible combinations when
- 3:05:18training large language model are
- 3:05:21You can have one token and predict next.
- 3:05:23You can have two tokens predict next.
- 3:05:24You can have three tokens until you have
- 3:05:28almost all and predict the last.
- 3:05:31So, that's why inputs can be from zero
- 3:05:33until last, but not last.
- 3:05:36And targets can be
- 3:05:38from the second token until last.
- 3:05:41So, all of the possible combinations
- 3:05:43where you have at least one to predict
- 3:05:44the next, or until all except last to
- 3:05:47predict last.
- 3:05:50So, as I said, we will get logits, like
- 3:05:52numbers,
- 3:05:53uh when we pass this through the model.
- 3:05:56And
- 3:05:59So, in this case, it looks like we're
- 3:06:00not using softmax. That's okay, too.
- 3:06:03Um to put them into percentages,
- 3:06:07we can also calculate
- 3:06:09loss
- 3:06:10uh by just
- 3:06:12comparing the current logits to the
- 3:06:13target
- 3:06:15uh tokens.
- 3:06:17So, the highest logit should be
- 3:06:21same as the target, the correct token.
- 3:06:24And if it's not,
- 3:06:25then we calculate the loss, update
- 3:06:27weights, do optimization steps. So,
- 3:06:31we empty out gradients from the last
- 3:06:33back propagation.
- 3:06:35You calculate all of the
- 3:06:37uh chain rule, the gradients, and then
- 3:06:39you update weights to reduce the loss.
- 3:06:44So, that's [snorts] going to be it for
- 3:06:45our uh GPT style transformer.
- 3:06:48I'm going to
- 3:06:49put this video here, and see you in the
- 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.