YouTube2Text

1.2 Dot product and distance — Transcript

by KAUST Academy · 2,529 words · 372 segments · language en · Watch on YouTube

Full transcript

  1. 0:01Whenever, after we finish talking about
  2. 0:03linear combinations or the span of
  3. 0:06vectors, please do remember this
  4. 0:08terminology that I'm going to use very
  5. 0:11often. The span of vectors.
  6. 0:15After talking about these two concepts,
  7. 0:19we are going to discuss the dot
  8. 0:21products.
  9. 0:22What is a dot product? A dot product is
  10. 0:25a number that comes from two vectors
  11. 0:28multiplied with each other.
  12. 0:30So, when I'm multiplying u dot v, I'm
  13. 0:33multiplying the components together and
  14. 0:36I'm adding them.
  15. 0:38Let's look at the example that we see
  16. 0:40over here. So, we have the u is equal to
  17. 0:441 2. We have the two vectors 1 2 and 3
  18. 0:46-1. We do the dot product u dot with v.
  19. 0:51Both vectors are represented as column
  20. 0:54vectors.
  21. 0:56So, u dot v, the operation, the dot
  22. 0:58product, uses column vectors.
  23. 1:01And how is this performed? By
  24. 1:04multiplying the components, the
  25. 1:06corresponding components together, and
  26. 1:07then adding the results, which means 1 *
  27. 1:113 added to 2 * -1.
  28. 1:15And the answer would be what?
  29. 1:17You see also another notation, u
  30. 1:19transpose v. What is this notation is
  31. 1:22about? So, this notation is about taking
  32. 1:25the column vector and transposing it.
  33. 1:28What I mean by that? You are going to
  34. 1:29transform it into a row vector.
  35. 1:33So, a row vector multiplied by a column
  36. 1:37vector, now I don't have The operation
  37. 1:39is not a dot product, but it's a
  38. 1:41multiplication of vectors can happen.
  39. 1:45And how does it happen? By simply doing
  40. 1:48the dot product between both vectors.
  41. 1:51So, this notation is a very important
  42. 1:54notation. The transpose The transpose of
  43. 1:57a vector is uh an operation that will
  44. 2:01transform the row vectors into column
  45. 2:04vectors and the the the column vectors
  46. 2:07into row vectors.
  47. 2:09And when I'm doing U dot with V, I'm
  48. 2:12representing both vectors in column
  49. 2:16vectors. This is the same as U transpose
  50. 2:21multiplied by V. If you see, I do not
  51. 2:24I'm not putting a dot product anymore.
  52. 2:27What is U transpose times V? Taking the
  53. 2:31vector U and representing it as a row
  54. 2:34vector and I'm multiplying it by V. They
  55. 2:37are all these are just notations. These
  56. 2:40are operations.
  57. 2:42These operations are between the two
  58. 2:44vectors and this is an operation between
  59. 2:47matrices. It's multiplication of
  60. 2:49matrices. We will see that
  61. 2:52in details later on. But so far, you
  62. 2:55need to know that the dot product is
  63. 2:57performed on two column vectors
  64. 3:00and the product of the matrices is
  65. 3:03performed on a row vector with a column
  66. 3:05vector. They both produce the same
  67. 3:08result.
  68. 3:12So, the output is one number. Both
  69. 3:14vectors need also to be of the same
  70. 3:16dimension because you're doing a dot
  71. 3:17product. So, you cannot have a vector of
  72. 3:21a different dimension dotted with a
  73. 3:24vector for instance of a longer
  74. 3:26dimension because you need to produce
  75. 3:30the product of the corresponding
  76. 3:32component together and add them. So,
  77. 3:34these guys will not have corresponding
  78. 3:37components. So, when the vectors are in
  79. 3:41different spaces or have different sizes
  80. 3:44or dimensions, the dot product cannot be
  81. 3:47performed.
  82. 3:50Prediction as weighted sum. So, this is
  83. 3:53an example of a simple prediction that
  84. 3:56can be written written down as Y hat is
  85. 3:59W transpose X plus B.
  86. 4:02What do I mean by w transpose x? It's a
  87. 4:04dot product between w and x, but
  88. 4:07represented as a product of matrices.
  89. 4:09One row with one column vector, which we
  90. 4:12are going to see also in details in the
  91. 4:14next module.
  92. 4:15>> [sighs and gasps]
  93. 4:15>> So, in our example, every single vector
  94. 4:18is represented in column vector. When
  95. 4:20I'm transposing w, I'm represented in
  96. 4:24I'm representing it in a row vector.
  97. 4:26And
  98. 4:27uh applying w transpose x + b. If you're
  99. 4:32looking at this expression, so the
  100. 4:34transpose is w transpose is a row vector
  101. 4:38multiplied by the column vector is
  102. 4:40exactly performing a dot product between
  103. 4:44x and w. So, I multiply 4 by 2, then I
  104. 4:48multiply 3 by -1, and then I add them
  105. 4:51together.
  106. 4:52And then finally, the b is what's called
  107. 4:55the bias term. I have to add it to the
  108. 4:58result of the dot product in order to
  109. 5:00get to my final result.
  110. 5:03Each feature is multiplied by weight,
  111. 5:05and the results are added together. This
  112. 5:08is These are big terminologies.
  113. 5:10And big terminologies that you are going
  114. 5:12to understand more and more and see more
  115. 5:15and more during your journey.
  116. 5:19The norm of a vector. The norm of a
  117. 5:21vector is a way to measure how large
  118. 5:26your vector is.
  119. 5:28So, the norm of a vector in R2, for
  120. 5:30instance, what I mean by R2 is a vector
  121. 5:33with two components, is given by the
  122. 5:36norm of u, which is the square root of
  123. 5:39u1 squared + u2 squared.
  124. 5:42So, this is called the Euclidean norm,
  125. 5:45and we can extend this Euclidean norm
  126. 5:47for an n vector u, where the norm of u
  127. 5:50is given as the square root of u1
  128. 5:53squared + u2 squared all the way to un
  129. 5:57squared. And this is effectively the
  130. 5:59square root of u dot with u.
  131. 6:03So, the norm of a vector, as I
  132. 6:04mentioned, is to use to measure the
  133. 6:06length of a vector.
  134. 6:08So, how large this vector is, how long
  135. 6:10it is.
  136. 6:12A unit vector is a vector whose norm is
  137. 6:15equal to one. You can transform any
  138. 6:18vector into a unit vector, so you are
  139. 6:21removing the length.
  140. 6:23So, for instance, you're taking U
  141. 6:25divided by the norm of U is the unit
  142. 6:28vector in the direction of U.
  143. 6:34Some properties of unit vectors.
  144. 6:37So, for any N vectors U and V and scalar
  145. 6:41C C,
  146. 6:42the norm of C multiplied by U is the
  147. 6:46absolute value of the norm of C
  148. 6:48multiplied by the norm of U.
  149. 6:51The norm of a vector is always positive.
  150. 6:54So, if you multiply
  151. 6:56U by a negative scalar, for instance,
  152. 7:00multiply by -2, the norm of -2
  153. 7:04multiplied by U, how can you compare it
  154. 7:07to the norm of U? You have to take the
  155. 7:10absolute value of -2 and multiply it by
  156. 7:14the norm of U.
  157. 7:16So, this is going to be equal to two
  158. 7:18times the norm of U.
  159. 7:21Why is that? Mathematically, the reason
  160. 7:23is very simple. You have the square root
  161. 7:26of the C U dot with C U, so the square
  162. 7:30root of C U dot with C U, which is the
  163. 7:34square root of C squared U dot with U.
  164. 7:37When you pull this guy outside of the
  165. 7:39square root, you have to take its
  166. 7:42absolute value
  167. 7:43times the square root of U dot with U.
  168. 7:46So, bottom line, a norm is always
  169. 7:48positive. Even if you scale with a
  170. 7:50negative number, you cannot have the
  171. 7:53norm to be negative. So, the scale has
  172. 7:55to be when you're computing the norm of
  173. 7:58C U, the scale has to be a positive
  174. 8:00value.
  175. 8:02Triangle inequality. The triangle
  176. 8:04inequality tells you that the norm of U
  177. 8:06plus V is less or equal than the norm of
  178. 8:09U plus the norm of V. So, in other word,
  179. 8:12if you're going from one place to
  180. 8:14another one, going through [laughter]
  181. 8:18U and V in this case, going to
  182. 8:22from point A to point B via U, from
  183. 8:26point B to point C is definitely longer
  184. 8:31It's a longer way to go going from A to
  185. 8:34C directly.
  186. 8:35Okay? So, the distance between A and C
  187. 8:39is less or equal than the distance
  188. 8:41between A and B and B and C. You have
  189. 8:43equality whenever B is on the way
  190. 8:48between A and C.
  191. 8:51Of course, as we mentioned, the norm of
  192. 8:53U is always positive and it's equal to
  193. 8:56zero if and only if U is equal to zero.
  194. 9:01Distance. The Euclidean distance between
  195. 9:04two vectors U and V is given by the norm
  196. 9:06of U minus V.
  197. 9:08If we're looking at this example, let's
  198. 9:10see how did we get the distance. So, we
  199. 9:12have two operations, right? What do you
  200. 9:14think? What kind of operations do we
  201. 9:16have? Number one
  202. 9:18is subtracting
  203. 9:20V from U or U from V. It's the same
  204. 9:23thing, right? The norm of U minus V is
  205. 9:25the same as the norm of V minus U.
  206. 9:28Then, the second thing is performing the
  207. 9:31dot product
  208. 9:33between
  209. 9:34the answer.
  210. 9:35So, if U is 1 2 and V is 4 6, you can do
  211. 9:41You can subtract V minus U or U minus V.
  212. 9:43It's up to you. So, the reason why we
  213. 9:46did V minus U is just to keep those
  214. 9:48positive components because they are
  215. 9:50nice. It's always nice to work with
  216. 9:51positive components, but it doesn't
  217. 9:53really matter.
  218. 9:54So, U V minus U would be 4 minus 1 is 3,
  219. 9:586 minus 2 is 4. And therefore, the
  220. 10:01distance between U and V is going to be
  221. 10:04the norm of the answer. So, the answer
  222. 10:08is 3 4, so it's going to be the norm of
  223. 10:103, 4, which is the square root of U V
  224. 10:14minus U dot with V minus U. So, I'm
  225. 10:17going to take every single component,
  226. 10:19and I'm going to square them and add
  227. 10:21them. So, it's the square root of 3
  228. 10:23squared plus 4 squared, which is 5.
  229. 10:26So, the distance between two points is
  230. 10:28the length of the vector connecting
  231. 10:30them. How far apart are they?
  232. 10:35The angles and cosine similarity
  233. 10:38So, the cosine of the angle is given by
  234. 10:42U transpose V or U dot with V. Again,
  235. 10:46those are notations which you need to be
  236. 10:48familiar with.
  237. 10:49So, the cosine of theta is given by U
  238. 10:52dot V over the norm of U multiplied by
  239. 10:56the norm of V.
  240. 10:57Or cosine of theta is given by U
  241. 11:01transpose V. This is a notation divided
  242. 11:04by the norm of U times the norm of V.
  243. 11:07Or you can just write this equation as U
  244. 11:12transpose V is equal to the cosine of
  245. 11:15theta multiplied by the norm of U times
  246. 11:18the norm of V.
  247. 11:20In machine learning, this cosine of
  248. 11:22theta we use it a lot in cosine
  249. 11:24similarities. We call it cosine
  250. 11:26similarity between U and V.
  251. 11:28Which is U transpose V over the norm of
  252. 11:30U and the norm of V.
  253. 11:32It tells us if U and V are in the same
  254. 11:35directions, in opposite directions, how
  255. 11:37close in direction they are.
  256. 11:41So, the cosine, as we all know, ranges
  257. 11:44from -1 to +1.
  258. 11:47So, if the cosine of the angle is equal
  259. 11:49to -1, what does it mean? It means the
  260. 11:53angle is 180 degrees. So, both vectors,
  261. 11:57U and V, are going to be in opposite
  262. 12:00directions.
  263. 12:02If the cosine is equal to 1, it means
  264. 12:05that U and V are in the same directions.
  265. 12:07They do not have to have the same
  266. 12:11magnitude. Right? They can be just They
  267. 12:14don't have to have the same magnitude.
  268. 12:16That's the reason why we divide by the
  269. 12:17norm of U and the norm of V.
  270. 12:20Having U and V, if the cosine is equal
  271. 12:25to zero, then U and V are perpendicular,
  272. 12:28but our terminology we use we use
  273. 12:31orthogonal. So, orthogonal is the
  274. 12:35terminology that is used in linear
  275. 12:37algebra.
  276. 12:39Cosine similarity is undefined if either
  277. 12:42vector is zero.
  278. 12:45Normalization, this is something we've
  279. 12:48mentioned before.
  280. 12:49Normalization in machine learning or
  281. 12:51unit vector in linear algebra. This is
  282. 12:54where you take the vector and you
  283. 12:56normalize it. You divide by its norm to
  284. 12:58make it a unit vector. Meaning, what
  285. 13:01does it mean exactly? What do you think
  286. 13:03it means uh dividing by the norm? It
  287. 13:05means that we are making the norm of our
  288. 13:08new vector equals to one.
  289. 13:11So, X hat is X over the norm of X when X
  290. 13:13is not zero. So, if you're taking the
  291. 13:16vector 3 4, we already have seen that
  292. 13:18the norm of 3 4 is equal to five because
  293. 13:22it's the square root of 9 + 16.
  294. 13:26So, 9 + 16 is the square root of 25,
  295. 13:29which is five. When you divide three and
  296. 13:32four by five,
  297. 13:33then you are going to get a new vector,
  298. 13:36which is 0.6 and 0.8.
  299. 13:39What is the norm of this new vector?
  300. 13:42It is exactly one.
  301. 13:45Unit normalization removes the length.
  302. 13:47This is what it means.
  303. 13:50Okay, another example of cosine
  304. 13:52similarity and the use of dot product is
  305. 13:55the nearest direction.
  306. 13:57So, we consider the query vector Q,
  307. 13:59which is equal to 1 1, and two
  308. 14:01candidates vectors. And we would like to
  309. 14:03know
  310. 14:05uh
  311. 14:06where is how much or
  312. 14:08is Q
  313. 14:10in what direction is it closer to A or
  314. 14:13to B? What is its direction with respect
  315. 14:16to A and B?
  316. 14:17So, we have the two candidates vectors A
  317. 14:19which is equal to 2 0, B equals to 3 3.
  318. 14:23So, how do we compute the cosine
  319. 14:25similarity? The cosine similarities are
  320. 14:27based on the dot product as we
  321. 14:29mentioned.
  322. 14:30So, the cosine similarity between Q and
  323. 14:33A, do you remember the formula?
  324. 14:35So, this is going to give you the dot
  325. 14:38product of Q and A over the norm of Q
  326. 14:43times the norm of A. And this is a times
  327. 14:47and not a dot product. So, be careful on
  328. 14:50the operations. A dot product I'm going
  329. 14:53to just put it slightly bigger. You can
  330. 14:56use both notations Q dot with A or Q
  331. 14:59transpose A. What is the dot product
  332. 15:02between Q and A? So, I'm going to take
  333. 15:04the corresponding components and
  334. 15:06multiply them together. 2 * 1 is 2 + 2 +
  335. 15:090 * 1 is just 0, so I'll be left with
  336. 15:132 divided by What about What about the
  337. 15:17norm of Q? Let me just do it on the
  338. 15:20side. This is going to be the square
  339. 15:22root of 1 squared + 1 squared, which is
  340. 15:25the square root of 1 + 1, which is 2.
  341. 15:28What is the norm of A? This is equal to
  342. 15:31the square root of 2 squared + 0
  343. 15:34squared, and this is the square root of
  344. 15:364, which is 2.
  345. 15:38Okay? So, the norm of Q is the square
  346. 15:42root of 2 and multiplied by 2. We just
  347. 15:46do the simplification. 2 is 2 and I'll
  348. 15:48be left with 1 over square root of 2.
  349. 15:52The same thing happens, the same
  350. 15:53computations,
  351. 15:56will be done to be able to find the
  352. 15:59cosine similarity between Q and B, which
  353. 16:02I highly recommend for you guys to do.
  354. 16:05Then, what can we say? That the cosine
  355. 16:07similarity between Q and A is 0.707,
  356. 16:11the cosine similarity between Q and B is
  357. 16:131, then candidate B has the same
  358. 16:16direction as the query.
  359. 16:21So, distance versus similarity, choose
  360. 16:23the geometry.
  361. 16:25The Euclidean distance asks, "How close
  362. 16:27are the points between each other?"
  363. 16:30The cosine similarity asks, "How aligned
  364. 16:33are they directions?"
  365. 16:351 1 and 10 10, take this example. What
  366. 16:38do you think? They have cosine
  367. 16:40similarity one, so they're both going in
  368. 16:43the same direction, but they are far
  369. 16:45apart in Euclidean distance.
  370. 16:48So, Euclidean distance compares
  371. 16:50location, but cosine similarity compares
  372. 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.