4.15 Vectorization (optional) | Matrix multiplication-- [Machine Learning - Andrew Ng]
 So you know that a matrix is just a block or 2D array of numbers. What does it mean to multiply two matrices? Let's take a look. In order to build up to multiplying matrices, let's start by looking at how we take dot products between vectors. Let's use the example of taking the dot product between this vector 1, 2, and this vector 3, 4. If z is the dot product between these two vectors, then you compute z by multiplying the first element by this first element here, so it's 1 times 3, plus the second element times the second element, plus 2 times 4, and so that's just 3 plus 8, which is equal to 11. In the more general case, if z is the dot product between a vector a and a vector w, then you compute z by multiplying the first element together and then the second element together and the third and so on, and then adding up all of these products. So that's the vector vector dot product. It turns out there's another equivalent way of writing a dot product, which is given a vector a, that is 1, 2 written as a column, you can turn this into a row, that is, you can turn it from what's called a column vector to a row vector by taking the transpose of a. So the transpose of a vector a means you take this vector and lay its elements on the side like this. And it turns out that if you multiply a transpose, this is a row vector, or you can think of this as a 1 by 2 matrix, with w, which you can now think of as a 2 by 1 matrix, then z equals a transpose times w, and this is the same as taking the dot product between a and w. So to recap, z equals the dot product between a and w is the same as z equals a transpose, that is a laid on the side, multiplied by w. And this will be useful for understanding matrix multiplication, that these are just two ways of writing the exact same computation to arrive at z. Now let's look at vector matrix multiplication, which is when you take a vector and you multiply a vector by a matrix. Here again is the vector a, 1, 2, and a transpose is a laid on the side, so rather than just kind of think of this as a 2 by 1 matrix, it becomes a 1 by 2 matrix. And let me now create a 2 by 2 matrix w with these four elements, v4, v5, v6. If you want to compute capital Z as a transpose times w, so let's see how you would go about doing so. It turns out that z is going to be a 2 by 1 matrix, and to compute the first value of z, we're going to take a transpose, 1, 2 here, and multiply that by the first column of w, so that's 3, 4. And so to compute the first element of z, you end up with 1 times 3 plus 2 times 4, which we saw earlier is equal to 11, and so the first element of z is 11. Let's figure out what's the second element of z. Turns out you just repeat this process, but now multiply a transpose by the second column of w. And so to do that computation, you have 1 times 5 plus 2 times 6, which is equal to 5 plus 12, which is 17, so that's equal to 17. So z is equal to this 1 by 2 matrix, 11 and 17. Now just one last thing, and then that'll take us to the end of this video, which is how to take vector matrix multiplication and generalize it to matrix matrix multiplication. I have a matrix A with these four elements, the first column is 1, 2, and the second column is negative 1, negative 2. And I want to know how to compute a transpose times w. Unlike the previous slide, A now is a matrix rather than just a vector, but the matrix is just a set of different vectors stacked together in columns. So first, let's figure out what is A transpose. In order to compute A transpose, we're going to take the columns of A, and similar to what happened when you transpose a vector, we're going to take the columns and lay them on the side one column at a time. So the first column, 1, 2, becomes the first row, 1, 2, because it's just laid on the side, and this second column, negative 1, negative 2, becomes laid on the side, negative 1, negative 2, like this. So the way you transpose a matrix is you take the columns and you just lay the columns on the side one column at a time. So you end up with this being A transpose. Next, we have this matrix, w, which we're going to write as 3, 4, 5, 6. So there's a column 3, 4, and a column 5, 6. One way I encourage you to think of matrices, at least that's useful for neural network implementations is if you see a matrix, think of the columns of the matrix, and if you see the transpose of a matrix, think of the rows of that matrix as being grouped together, as illustrated here with A and A transpose, as well as w. And now, let me show you how to multiply A transpose and w. In order to carry out this computation, let me call the columns of A, A1 and A2, and that means that A1 transpose is the first row of A transpose, and A2 transpose is the second row of A transpose. And then, same as before, let me call the columns of w to be w1 and w2. So it turns out that to compute A transpose w, the first thing we need to do is let's just ignore the second row of A, and let's just pay attention to the first row of A, and let's take this row 1, 2, that is A1 transpose, and multiply that with w. So you already know how to do that from the previous slide. The first element is 1, 2, inner product or dot product with 3, 4, so that ends up with 3 times 1 plus 2 times 4, which is 11. And then the second element is 1, 2, A transpose, inner product with 5, 6, so that's 5 times 1 plus 6 times 2, which is 5 plus 12, which is 17. So that gives you the first row of z equals A transpose w. So all we've done is take A1 transpose and multiply that by w. That's exactly what we did on the previous slide. Next, let's forget A1 for now, and let's just look at A2 and take A2 transpose and multiply that by w. So now we have A2 transpose times w, and to compute that, first we take negative 1 and negative 2 and dot product that with 3, 4, so that's negative 1 times 3 plus negative 2 times 4, and that turns out to be negative 11. And then we have to compute A2 transpose times the second column, and that's negative 1 times 5 plus negative 2 times 6, and that turns out to be negative 17. So you end up with A transpose times w is equal to this 2 by 2 matrix over here. Let's talk about the general form of matrix-matrix multiplication. So this was an example of how you multiply a vector with a matrix or a matrix with a matrix. There's a lot of dot products between vectors, but ordered in a certain way to construct the elements of the output z one element at a time. I know this was a lot, but in the next video, let's look at the general form of how a matrix-matrix multiplication is defined, and I hope that that will make all this clear as well. Let's go on to the next video.
[{"start": 0.0, "end": 8.0, "text": " So you know that a matrix is just a block or 2D array of numbers."}, {"start": 8.0, "end": 11.200000000000001, "text": " What does it mean to multiply two matrices?"}, {"start": 11.200000000000001, "end": 12.72, "text": " Let's take a look."}, {"start": 12.72, "end": 19.76, "text": " In order to build up to multiplying matrices, let's start by looking at how we take dot"}, {"start": 19.76, "end": 22.28, "text": " products between vectors."}, {"start": 22.28, "end": 27.8, "text": " Let's use the example of taking the dot product between this vector 1, 2, and this vector"}, {"start": 27.8, "end": 30.2, "text": " 3, 4."}, {"start": 30.2, "end": 35.96, "text": " If z is the dot product between these two vectors, then you compute z by multiplying"}, {"start": 35.96, "end": 42.32, "text": " the first element by this first element here, so it's 1 times 3, plus the second element"}, {"start": 42.32, "end": 47.96, "text": " times the second element, plus 2 times 4, and so that's just 3 plus 8, which is equal"}, {"start": 47.96, "end": 50.64, "text": " to 11."}, {"start": 50.64, "end": 59.0, "text": " In the more general case, if z is the dot product between a vector a and a vector w,"}, {"start": 59.0, "end": 63.52, "text": " then you compute z by multiplying the first element together and then the second element"}, {"start": 63.52, "end": 67.03999999999999, "text": " together and the third and so on, and then adding up all of these products."}, {"start": 67.03999999999999, "end": 70.72, "text": " So that's the vector vector dot product."}, {"start": 70.72, "end": 77.0, "text": " It turns out there's another equivalent way of writing a dot product, which is given a"}, {"start": 77.0, "end": 85.44, "text": " vector a, that is 1, 2 written as a column, you can turn this into a row, that is, you"}, {"start": 85.44, "end": 92.6, "text": " can turn it from what's called a column vector to a row vector by taking the transpose of"}, {"start": 92.6, "end": 93.72, "text": " a."}, {"start": 93.72, "end": 101.24000000000001, "text": " So the transpose of a vector a means you take this vector and lay its elements on the side"}, {"start": 101.24000000000001, "end": 103.6, "text": " like this."}, {"start": 103.6, "end": 110.72, "text": " And it turns out that if you multiply a transpose, this is a row vector, or you can think of"}, {"start": 110.72, "end": 120.39999999999999, "text": " this as a 1 by 2 matrix, with w, which you can now think of as a 2 by 1 matrix, then"}, {"start": 120.39999999999999, "end": 128.0, "text": " z equals a transpose times w, and this is the same as taking the dot product between"}, {"start": 128.0, "end": 131.04, "text": " a and w."}, {"start": 131.04, "end": 140.0, "text": " So to recap, z equals the dot product between a and w is the same as z equals a transpose,"}, {"start": 140.0, "end": 145.76, "text": " that is a laid on the side, multiplied by w."}, {"start": 145.76, "end": 150.32, "text": " And this will be useful for understanding matrix multiplication, that these are just"}, {"start": 150.32, "end": 156.35999999999999, "text": " two ways of writing the exact same computation to arrive at z."}, {"start": 156.36, "end": 162.32000000000002, "text": " Now let's look at vector matrix multiplication, which is when you take a vector and you multiply"}, {"start": 162.32000000000002, "end": 165.44000000000003, "text": " a vector by a matrix."}, {"start": 165.44000000000003, "end": 173.96, "text": " Here again is the vector a, 1, 2, and a transpose is a laid on the side, so rather than just"}, {"start": 173.96, "end": 180.4, "text": " kind of think of this as a 2 by 1 matrix, it becomes a 1 by 2 matrix."}, {"start": 180.4, "end": 187.92000000000002, "text": " And let me now create a 2 by 2 matrix w with these four elements, v4, v5, v6."}, {"start": 187.92000000000002, "end": 198.28, "text": " If you want to compute capital Z as a transpose times w, so let's see how you would go about"}, {"start": 198.28, "end": 199.88, "text": " doing so."}, {"start": 199.88, "end": 210.44, "text": " It turns out that z is going to be a 2 by 1 matrix, and to compute the first value of"}, {"start": 210.44, "end": 218.72, "text": " z, we're going to take a transpose, 1, 2 here, and multiply that by the first column of w,"}, {"start": 218.72, "end": 221.12, "text": " so that's 3, 4."}, {"start": 221.12, "end": 229.56, "text": " And so to compute the first element of z, you end up with 1 times 3 plus 2 times 4,"}, {"start": 229.56, "end": 236.72, "text": " which we saw earlier is equal to 11, and so the first element of z is 11."}, {"start": 236.72, "end": 239.84, "text": " Let's figure out what's the second element of z."}, {"start": 239.84, "end": 246.52, "text": " Turns out you just repeat this process, but now multiply a transpose by the second column"}, {"start": 246.52, "end": 247.76, "text": " of w."}, {"start": 247.76, "end": 256.44, "text": " And so to do that computation, you have 1 times 5 plus 2 times 6, which is equal to"}, {"start": 256.44, "end": 263.4, "text": " 5 plus 12, which is 17, so that's equal to 17."}, {"start": 263.4, "end": 269.84, "text": " So z is equal to this 1 by 2 matrix, 11 and 17."}, {"start": 269.84, "end": 275.64, "text": " Now just one last thing, and then that'll take us to the end of this video, which is"}, {"start": 275.64, "end": 282.88, "text": " how to take vector matrix multiplication and generalize it to matrix matrix multiplication."}, {"start": 282.88, "end": 288.71999999999997, "text": " I have a matrix A with these four elements, the first column is 1, 2, and the second column"}, {"start": 288.71999999999997, "end": 292.08, "text": " is negative 1, negative 2."}, {"start": 292.08, "end": 298.52, "text": " And I want to know how to compute a transpose times w."}, {"start": 298.52, "end": 304.44, "text": " Unlike the previous slide, A now is a matrix rather than just a vector, but the matrix"}, {"start": 304.44, "end": 309.8, "text": " is just a set of different vectors stacked together in columns."}, {"start": 309.8, "end": 313.52000000000004, "text": " So first, let's figure out what is A transpose."}, {"start": 313.52000000000004, "end": 318.84000000000003, "text": " In order to compute A transpose, we're going to take the columns of A, and similar to what"}, {"start": 318.84000000000003, "end": 323.44, "text": " happened when you transpose a vector, we're going to take the columns and lay them on"}, {"start": 323.44, "end": 326.2, "text": " the side one column at a time."}, {"start": 326.2, "end": 331.2, "text": " So the first column, 1, 2, becomes the first row, 1, 2, because it's just laid on the"}, {"start": 331.2, "end": 338.0, "text": " side, and this second column, negative 1, negative 2, becomes laid on the side, negative"}, {"start": 338.0, "end": 339.92, "text": " 1, negative 2, like this."}, {"start": 339.92, "end": 344.28, "text": " So the way you transpose a matrix is you take the columns and you just lay the columns on"}, {"start": 344.28, "end": 346.08, "text": " the side one column at a time."}, {"start": 346.08, "end": 349.72, "text": " So you end up with this being A transpose."}, {"start": 349.72, "end": 356.32, "text": " Next, we have this matrix, w, which we're going to write as 3, 4, 5, 6."}, {"start": 356.32, "end": 361.28, "text": " So there's a column 3, 4, and a column 5, 6."}, {"start": 361.28, "end": 366.56, "text": " One way I encourage you to think of matrices, at least that's useful for neural network"}, {"start": 366.56, "end": 373.84, "text": " implementations is if you see a matrix, think of the columns of the matrix, and if you see"}, {"start": 373.84, "end": 378.48, "text": " the transpose of a matrix, think of the rows of that matrix as being grouped together,"}, {"start": 378.48, "end": 383.6, "text": " as illustrated here with A and A transpose, as well as w."}, {"start": 383.6, "end": 390.68, "text": " And now, let me show you how to multiply A transpose and w."}, {"start": 390.68, "end": 399.04, "text": " In order to carry out this computation, let me call the columns of A, A1 and A2, and that"}, {"start": 399.04, "end": 408.16, "text": " means that A1 transpose is the first row of A transpose, and A2 transpose is the second"}, {"start": 408.16, "end": 410.6, "text": " row of A transpose."}, {"start": 410.6, "end": 418.2, "text": " And then, same as before, let me call the columns of w to be w1 and w2."}, {"start": 418.2, "end": 425.03999999999996, "text": " So it turns out that to compute A transpose w, the first thing we need to do is let's"}, {"start": 425.03999999999996, "end": 432.68, "text": " just ignore the second row of A, and let's just pay attention to the first row of A,"}, {"start": 432.68, "end": 439.03999999999996, "text": " and let's take this row 1, 2, that is A1 transpose, and multiply that with w."}, {"start": 439.03999999999996, "end": 443.44, "text": " So you already know how to do that from the previous slide."}, {"start": 443.44, "end": 449.4, "text": " The first element is 1, 2, inner product or dot product with 3, 4, so that ends up with"}, {"start": 449.4, "end": 453.56, "text": " 3 times 1 plus 2 times 4, which is 11."}, {"start": 453.56, "end": 461.24, "text": " And then the second element is 1, 2, A transpose, inner product with 5, 6, so that's 5 times"}, {"start": 461.24, "end": 467.15999999999997, "text": " 1 plus 6 times 2, which is 5 plus 12, which is 17."}, {"start": 467.15999999999997, "end": 472.24, "text": " So that gives you the first row of z equals A transpose w."}, {"start": 472.24, "end": 477.72, "text": " So all we've done is take A1 transpose and multiply that by w."}, {"start": 477.72, "end": 481.12, "text": " That's exactly what we did on the previous slide."}, {"start": 481.12, "end": 489.2, "text": " Next, let's forget A1 for now, and let's just look at A2 and take A2 transpose and multiply"}, {"start": 489.2, "end": 491.28000000000003, "text": " that by w."}, {"start": 491.28000000000003, "end": 498.76, "text": " So now we have A2 transpose times w, and to compute that, first we take negative 1 and"}, {"start": 498.76, "end": 505.44, "text": " negative 2 and dot product that with 3, 4, so that's negative 1 times 3 plus negative"}, {"start": 505.44, "end": 510.76, "text": " 2 times 4, and that turns out to be negative 11."}, {"start": 510.76, "end": 517.64, "text": " And then we have to compute A2 transpose times the second column, and that's negative 1 times"}, {"start": 517.64, "end": 524.08, "text": " 5 plus negative 2 times 6, and that turns out to be negative 17."}, {"start": 524.08, "end": 531.96, "text": " So you end up with A transpose times w is equal to this 2 by 2 matrix over here."}, {"start": 531.96, "end": 537.84, "text": " Let's talk about the general form of matrix-matrix multiplication."}, {"start": 537.84, "end": 543.5600000000001, "text": " So this was an example of how you multiply a vector with a matrix or a matrix with a"}, {"start": 543.5600000000001, "end": 544.5600000000001, "text": " matrix."}, {"start": 544.5600000000001, "end": 549.48, "text": " There's a lot of dot products between vectors, but ordered in a certain way to construct"}, {"start": 549.48, "end": 553.76, "text": " the elements of the output z one element at a time."}, {"start": 553.76, "end": 560.16, "text": " I know this was a lot, but in the next video, let's look at the general form of how a matrix-matrix"}, {"start": 560.16, "end": 565.48, "text": " multiplication is defined, and I hope that that will make all this clear as well."}, {"start": 565.48, "end": 585.88, "text": " Let's go on to the next video."}]