1.2 Dot product and distance — Transcript
Full transcript
- 0:01Whenever, after we finish talking about
- 0:03linear combinations or the span of
- 0:06vectors, please do remember this
- 0:08terminology that I'm going to use very
- 0:11often. The span of vectors.
- 0:15After talking about these two concepts,
- 0:19we are going to discuss the dot
- 0:21products.
- 0:22What is a dot product? A dot product is
- 0:25a number that comes from two vectors
- 0:28multiplied with each other.
- 0:30So, when I'm multiplying u dot v, I'm
- 0:33multiplying the components together and
- 0:36I'm adding them.
- 0:38Let's look at the example that we see
- 0:40over here. So, we have the u is equal to
- 0:441 2. We have the two vectors 1 2 and 3
- 0:46-1. We do the dot product u dot with v.
- 0:51Both vectors are represented as column
- 0:54vectors.
- 0:56So, u dot v, the operation, the dot
- 0:58product, uses column vectors.
- 1:01And how is this performed? By
- 1:04multiplying the components, the
- 1:06corresponding components together, and
- 1:07then adding the results, which means 1 *
- 1:113 added to 2 * -1.
- 1:15And the answer would be what?
- 1:17You see also another notation, u
- 1:19transpose v. What is this notation is
- 1:22about? So, this notation is about taking
- 1:25the column vector and transposing it.
- 1:28What I mean by that? You are going to
- 1:29transform it into a row vector.
- 1:33So, a row vector multiplied by a column
- 1:37vector, now I don't have The operation
- 1:39is not a dot product, but it's a
- 1:41multiplication of vectors can happen.
- 1:45And how does it happen? By simply doing
- 1:48the dot product between both vectors.
- 1:51So, this notation is a very important
- 1:54notation. The transpose The transpose of
- 1:57a vector is uh an operation that will
- 2:01transform the row vectors into column
- 2:04vectors and the the the column vectors
- 2:07into row vectors.
- 2:09And when I'm doing U dot with V, I'm
- 2:12representing both vectors in column
- 2:16vectors. This is the same as U transpose
- 2:21multiplied by V. If you see, I do not
- 2:24I'm not putting a dot product anymore.
- 2:27What is U transpose times V? Taking the
- 2:31vector U and representing it as a row
- 2:34vector and I'm multiplying it by V. They
- 2:37are all these are just notations. These
- 2:40are operations.
- 2:42These operations are between the two
- 2:44vectors and this is an operation between
- 2:47matrices. It's multiplication of
- 2:49matrices. We will see that
- 2:52in details later on. But so far, you
- 2:55need to know that the dot product is
- 2:57performed on two column vectors
- 3:00and the product of the matrices is
- 3:03performed on a row vector with a column
- 3:05vector. They both produce the same
- 3:08result.
- 3:12So, the output is one number. Both
- 3:14vectors need also to be of the same
- 3:16dimension because you're doing a dot
- 3:17product. So, you cannot have a vector of
- 3:21a different dimension dotted with a
- 3:24vector for instance of a longer
- 3:26dimension because you need to produce
- 3:30the product of the corresponding
- 3:32component together and add them. So,
- 3:34these guys will not have corresponding
- 3:37components. So, when the vectors are in
- 3:41different spaces or have different sizes
- 3:44or dimensions, the dot product cannot be
- 3:47performed.
- 3:50Prediction as weighted sum. So, this is
- 3:53an example of a simple prediction that
- 3:56can be written written down as Y hat is
- 3:59W transpose X plus B.
- 4:02What do I mean by w transpose x? It's a
- 4:04dot product between w and x, but
- 4:07represented as a product of matrices.
- 4:09One row with one column vector, which we
- 4:12are going to see also in details in the
- 4:14next module.
- 4:15>> [sighs and gasps]
- 4:15>> So, in our example, every single vector
- 4:18is represented in column vector. When
- 4:20I'm transposing w, I'm represented in
- 4:24I'm representing it in a row vector.
- 4:26And
- 4:27uh applying w transpose x + b. If you're
- 4:32looking at this expression, so the
- 4:34transpose is w transpose is a row vector
- 4:38multiplied by the column vector is
- 4:40exactly performing a dot product between
- 4:44x and w. So, I multiply 4 by 2, then I
- 4:48multiply 3 by -1, and then I add them
- 4:51together.
- 4:52And then finally, the b is what's called
- 4:55the bias term. I have to add it to the
- 4:58result of the dot product in order to
- 5:00get to my final result.
- 5:03Each feature is multiplied by weight,
- 5:05and the results are added together. This
- 5:08is These are big terminologies.
- 5:10And big terminologies that you are going
- 5:12to understand more and more and see more
- 5:15and more during your journey.
- 5:19The norm of a vector. The norm of a
- 5:21vector is a way to measure how large
- 5:26your vector is.
- 5:28So, the norm of a vector in R2, for
- 5:30instance, what I mean by R2 is a vector
- 5:33with two components, is given by the
- 5:36norm of u, which is the square root of
- 5:39u1 squared + u2 squared.
- 5:42So, this is called the Euclidean norm,
- 5:45and we can extend this Euclidean norm
- 5:47for an n vector u, where the norm of u
- 5:50is given as the square root of u1
- 5:53squared + u2 squared all the way to un
- 5:57squared. And this is effectively the
- 5:59square root of u dot with u.
- 6:03So, the norm of a vector, as I
- 6:04mentioned, is to use to measure the
- 6:06length of a vector.
- 6:08So, how large this vector is, how long
- 6:10it is.
- 6:12A unit vector is a vector whose norm is
- 6:15equal to one. You can transform any
- 6:18vector into a unit vector, so you are
- 6:21removing the length.
- 6:23So, for instance, you're taking U
- 6:25divided by the norm of U is the unit
- 6:28vector in the direction of U.
- 6:34Some properties of unit vectors.
- 6:37So, for any N vectors U and V and scalar
- 6:41C C,
- 6:42the norm of C multiplied by U is the
- 6:46absolute value of the norm of C
- 6:48multiplied by the norm of U.
- 6:51The norm of a vector is always positive.
- 6:54So, if you multiply
- 6:56U by a negative scalar, for instance,
- 7:00multiply by -2, the norm of -2
- 7:04multiplied by U, how can you compare it
- 7:07to the norm of U? You have to take the
- 7:10absolute value of -2 and multiply it by
- 7:14the norm of U.
- 7:16So, this is going to be equal to two
- 7:18times the norm of U.
- 7:21Why is that? Mathematically, the reason
- 7:23is very simple. You have the square root
- 7:26of the C U dot with C U, so the square
- 7:30root of C U dot with C U, which is the
- 7:34square root of C squared U dot with U.
- 7:37When you pull this guy outside of the
- 7:39square root, you have to take its
- 7:42absolute value
- 7:43times the square root of U dot with U.
- 7:46So, bottom line, a norm is always
- 7:48positive. Even if you scale with a
- 7:50negative number, you cannot have the
- 7:53norm to be negative. So, the scale has
- 7:55to be when you're computing the norm of
- 7:58C U, the scale has to be a positive
- 8:00value.
- 8:02Triangle inequality. The triangle
- 8:04inequality tells you that the norm of U
- 8:06plus V is less or equal than the norm of
- 8:09U plus the norm of V. So, in other word,
- 8:12if you're going from one place to
- 8:14another one, going through [laughter]
- 8:18U and V in this case, going to
- 8:22from point A to point B via U, from
- 8:26point B to point C is definitely longer
- 8:31It's a longer way to go going from A to
- 8:34C directly.
- 8:35Okay? So, the distance between A and C
- 8:39is less or equal than the distance
- 8:41between A and B and B and C. You have
- 8:43equality whenever B is on the way
- 8:48between A and C.
- 8:51Of course, as we mentioned, the norm of
- 8:53U is always positive and it's equal to
- 8:56zero if and only if U is equal to zero.
- 9:01Distance. The Euclidean distance between
- 9:04two vectors U and V is given by the norm
- 9:06of U minus V.
- 9:08If we're looking at this example, let's
- 9:10see how did we get the distance. So, we
- 9:12have two operations, right? What do you
- 9:14think? What kind of operations do we
- 9:16have? Number one
- 9:18is subtracting
- 9:20V from U or U from V. It's the same
- 9:23thing, right? The norm of U minus V is
- 9:25the same as the norm of V minus U.
- 9:28Then, the second thing is performing the
- 9:31dot product
- 9:33between
- 9:34the answer.
- 9:35So, if U is 1 2 and V is 4 6, you can do
- 9:41You can subtract V minus U or U minus V.
- 9:43It's up to you. So, the reason why we
- 9:46did V minus U is just to keep those
- 9:48positive components because they are
- 9:50nice. It's always nice to work with
- 9:51positive components, but it doesn't
- 9:53really matter.
- 9:54So, U V minus U would be 4 minus 1 is 3,
- 9:586 minus 2 is 4. And therefore, the
- 10:01distance between U and V is going to be
- 10:04the norm of the answer. So, the answer
- 10:08is 3 4, so it's going to be the norm of
- 10:103, 4, which is the square root of U V
- 10:14minus U dot with V minus U. So, I'm
- 10:17going to take every single component,
- 10:19and I'm going to square them and add
- 10:21them. So, it's the square root of 3
- 10:23squared plus 4 squared, which is 5.
- 10:26So, the distance between two points is
- 10:28the length of the vector connecting
- 10:30them. How far apart are they?
- 10:35The angles and cosine similarity
- 10:38So, the cosine of the angle is given by
- 10:42U transpose V or U dot with V. Again,
- 10:46those are notations which you need to be
- 10:48familiar with.
- 10:49So, the cosine of theta is given by U
- 10:52dot V over the norm of U multiplied by
- 10:56the norm of V.
- 10:57Or cosine of theta is given by U
- 11:01transpose V. This is a notation divided
- 11:04by the norm of U times the norm of V.
- 11:07Or you can just write this equation as U
- 11:12transpose V is equal to the cosine of
- 11:15theta multiplied by the norm of U times
- 11:18the norm of V.
- 11:20In machine learning, this cosine of
- 11:22theta we use it a lot in cosine
- 11:24similarities. We call it cosine
- 11:26similarity between U and V.
- 11:28Which is U transpose V over the norm of
- 11:30U and the norm of V.
- 11:32It tells us if U and V are in the same
- 11:35directions, in opposite directions, how
- 11:37close in direction they are.
- 11:41So, the cosine, as we all know, ranges
- 11:44from -1 to +1.
- 11:47So, if the cosine of the angle is equal
- 11:49to -1, what does it mean? It means the
- 11:53angle is 180 degrees. So, both vectors,
- 11:57U and V, are going to be in opposite
- 12:00directions.
- 12:02If the cosine is equal to 1, it means
- 12:05that U and V are in the same directions.
- 12:07They do not have to have the same
- 12:11magnitude. Right? They can be just They
- 12:14don't have to have the same magnitude.
- 12:16That's the reason why we divide by the
- 12:17norm of U and the norm of V.
- 12:20Having U and V, if the cosine is equal
- 12:25to zero, then U and V are perpendicular,
- 12:28but our terminology we use we use
- 12:31orthogonal. So, orthogonal is the
- 12:35terminology that is used in linear
- 12:37algebra.
- 12:39Cosine similarity is undefined if either
- 12:42vector is zero.
- 12:45Normalization, this is something we've
- 12:48mentioned before.
- 12:49Normalization in machine learning or
- 12:51unit vector in linear algebra. This is
- 12:54where you take the vector and you
- 12:56normalize it. You divide by its norm to
- 12:58make it a unit vector. Meaning, what
- 13:01does it mean exactly? What do you think
- 13:03it means uh dividing by the norm? It
- 13:05means that we are making the norm of our
- 13:08new vector equals to one.
- 13:11So, X hat is X over the norm of X when X
- 13:13is not zero. So, if you're taking the
- 13:16vector 3 4, we already have seen that
- 13:18the norm of 3 4 is equal to five because
- 13:22it's the square root of 9 + 16.
- 13:26So, 9 + 16 is the square root of 25,
- 13:29which is five. When you divide three and
- 13:32four by five,
- 13:33then you are going to get a new vector,
- 13:36which is 0.6 and 0.8.
- 13:39What is the norm of this new vector?
- 13:42It is exactly one.
- 13:45Unit normalization removes the length.
- 13:47This is what it means.
- 13:50Okay, another example of cosine
- 13:52similarity and the use of dot product is
- 13:55the nearest direction.
- 13:57So, we consider the query vector Q,
- 13:59which is equal to 1 1, and two
- 14:01candidates vectors. And we would like to
- 14:03know
- 14:05uh
- 14:06where is how much or
- 14:08is Q
- 14:10in what direction is it closer to A or
- 14:13to B? What is its direction with respect
- 14:16to A and B?
- 14:17So, we have the two candidates vectors A
- 14:19which is equal to 2 0, B equals to 3 3.
- 14:23So, how do we compute the cosine
- 14:25similarity? The cosine similarities are
- 14:27based on the dot product as we
- 14:29mentioned.
- 14:30So, the cosine similarity between Q and
- 14:33A, do you remember the formula?
- 14:35So, this is going to give you the dot
- 14:38product of Q and A over the norm of Q
- 14:43times the norm of A. And this is a times
- 14:47and not a dot product. So, be careful on
- 14:50the operations. A dot product I'm going
- 14:53to just put it slightly bigger. You can
- 14:56use both notations Q dot with A or Q
- 14:59transpose A. What is the dot product
- 15:02between Q and A? So, I'm going to take
- 15:04the corresponding components and
- 15:06multiply them together. 2 * 1 is 2 + 2 +
- 15:090 * 1 is just 0, so I'll be left with
- 15:132 divided by What about What about the
- 15:17norm of Q? Let me just do it on the
- 15:20side. This is going to be the square
- 15:22root of 1 squared + 1 squared, which is
- 15:25the square root of 1 + 1, which is 2.
- 15:28What is the norm of A? This is equal to
- 15:31the square root of 2 squared + 0
- 15:34squared, and this is the square root of
- 15:364, which is 2.
- 15:38Okay? So, the norm of Q is the square
- 15:42root of 2 and multiplied by 2. We just
- 15:46do the simplification. 2 is 2 and I'll
- 15:48be left with 1 over square root of 2.
- 15:52The same thing happens, the same
- 15:53computations,
- 15:56will be done to be able to find the
- 15:59cosine similarity between Q and B, which
- 16:02I highly recommend for you guys to do.
- 16:05Then, what can we say? That the cosine
- 16:07similarity between Q and A is 0.707,
- 16:11the cosine similarity between Q and B is
- 16:131, then candidate B has the same
- 16:16direction as the query.
- 16:21So, distance versus similarity, choose
- 16:23the geometry.
- 16:25The Euclidean distance asks, "How close
- 16:27are the points between each other?"
- 16:30The cosine similarity asks, "How aligned
- 16:33are they directions?"
- 16:351 1 and 10 10, take this example. What
- 16:38do you think? They have cosine
- 16:40similarity one, so they're both going in
- 16:43the same direction, but they are far
- 16:45apart in Euclidean distance.
- 16:48So, Euclidean distance compares
- 16:50location, but cosine similarity compares
- 16:54direction.
About this transcript
This page contains the full transcript of 1.2 Dot product and distance by KAUST Academy, generated from the public captions YouTube serves with the video. The transcript has 2,529 words across 372 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.