๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #16

Dot Product

The dot product combines two vectors of equal length into a single scalar. It measures how much two vectors "point in the same direction" โ€” and it is the exact operation behind cosine similarity and the attention mechanism you'll meet later in this hub.

Formula

\[ \mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i b_i = \|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta \]

The first form sums element-wise products directly. The second form connects it to geometry: \(\|\mathbf{a}\|\) and \(\|\mathbf{b}\|\) are the vectors' lengths (see Vector Norms), and \(\theta\) is the angle between them. When two vectors point in the same direction (\(\theta = 0\)), \(\cos\theta = 1\) and the dot product is maximal for their lengths; when they're perpendicular (\(\theta = 90ยฐ\)), the dot product is exactly 0.

Numerical Example

\[ \mathbf{a} = [1, 2, 3], \quad \mathbf{b} = [4, 5, 6] \] \[ \mathbf{a} \cdot \mathbf{b} = (1)(4) + (2)(5) + (3)(6) = 4 + 10 + 18 = 32 \]

Geometric Intuition

a b θ

A small angle ฮธ between two vectors gives a large, positive dot product โ€” they point in a similar direction.

Relationship to Matrix Multiplication

Matrix multiplication is literally a table of dot products: entry \(C_{ij}\) in Matrix Multiplication is the dot product of row \(i\) of \(\mathbf{A}\) and column \(j\) of \(\mathbf{B}\). Understanding the dot product is what makes matrix multiplication's mechanics click.

Code

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))   # 32
print(a @ b)            # 32 -- same result, matrix-multiply operator on 1D arrays
import torch
a = torch.tensor([1., 2., 3.])
b = torch.tensor([4., 5., 6.])
print(torch.dot(a, b))   # tensor(32.)

Where This Shows Up in Deep Learning

  • Cosine similarity (comparing word/document embeddings) is a normalized dot product: \(\cos\theta = \dfrac{\mathbf{a}\cdot\mathbf{b}}{\|\mathbf{a}\|\|\mathbf{b}\|}\).
  • Attention scores: the core of the Transformer computes \(\mathbf{Q}\mathbf{K}^\top\) โ€” a matrix of dot products between every query vector and every key vector, measuring how relevant each token is to every other token (see Scaled Dot-Product Attention).
  • A single neuron's weighted sum, \(\mathbf{w}\cdot\mathbf{x}\), is exactly a dot product between the weight vector and the input vector.

Common Mistakes

  • Forgetting the dot product requires equal-length vectors โ€” unlike matrix multiplication with its more flexible (but still strict) inner-dimension rule.
  • Interpreting a large dot product as "similarity" without normalizing for vector length โ€” two long vectors pointing in slightly different directions can have a larger raw dot product than two short, perfectly aligned vectors. Cosine similarity fixes this by dividing out the norms.

Interview Relevance

Q: "Why does the Transformer's attention mechanism use a dot product between queries and keys?" The dot product measures directional similarity โ€” a query vector will have a large dot product with key vectors pointing in a similar direction in the learned embedding space, which the model uses as a relevance score for how much attention to pay to each token.

Practice Question

Compute the dot product of \([2, 0, -1]\) and \([3, 4, 2]\) by hand. Is the result positive, negative, or zero โ€” and what does that tell you about the angle between them?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Dot Product โ€“ FAQs

Quick answers about learning Dot Product in Deep Learning.

This free note from CodingNow 2.0 explains Dot Product in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Dot Product, is 100% free with no signup required.
With focused practice, most students grasp Dot Product in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now