Vector dot product — from projection to multiplication
Vector dot product -- from projection to multiplication
The dot product is the most frequently used vector operation in AI.
The calculations in fully connected layers and the similarity matching in attention mechanisms—their core is the dot product.
The dot product has two equivalent perspectives, and understanding the bridge between them is the core takeaway of this chapter.
** Perspective One: Algebraic Calculation
Multiply the elements at corresponding positions, then sum them up.
( \mathbf{a} \cdot \mathbf{b} = \sum a_i b_i )
For example: (2,3) · (4,1) = 2×4 + 3×1 = 11
Perspective Two: Geometric Intuition
Projecting one vector onto another, and then multiplying the two lengths.
( \mathbf{a} \cdot \mathbf{b} = |\mathbf{a}| |\mathbf{b}| \cos\theta )
The smaller the angle $\theta$, the larger the dot product
Both algorithms yield the same value—this is an extremely elegant equation in mathematics.
The sign of the dot product reveals the directional relationship
<90°
Dot product > 0
Directions are roughly the same
=90°
Dot product = 0
Directions are perpendicular (orthogonal)
90°
Dot product < 0
Directions are roughly opposite
Real-life examples
**Shopping bill: quantity × unit price
You bought 2 bottles of coke (3 yuan per bottle), 3 bags of chips (5 yuan per bag), and 1 bucket of instant noodles (4 yuan per bucket).
The dot product of the quantity vector (2, 3, 1) and the price vector (3, 5, 4) is the total price: 2×3 + 3×5 + 1×4 = 25 yuan.
**Pushing a shopping cart: force × displacement
The force you apply to push the shopping cart is a vector (with magnitude and direction), and the cart's displacement is another vector.
In physics, work = component of force in the direction of displacement × magnitude of displacement = dot product of force vector and displacement vector.
If you push sideways (perpendicularly), no work is done—the dot product is zero.
Mathematical definition
Algebraic definition
[ \mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i b_i = a_1 b_1 + a_2 b_2 + \cdots + a_n b_n ]
Geometric definition
[ \mathbf{a} \cdot \mathbf{b} = |\mathbf{a}| |\mathbf{b}| \cos\theta ]
Bridge between the two definitions: finding the angle
[ \cos\theta = \frac{\mathbf{a} \cdot \mathbf{b}}{|\mathbf{a}| |\mathbf{b}|} ]
This means: given the coordinates of two vectors, you can calculate the angle between them without a protractor.
Properties of operation
| Property | Formula | Description |
|---|---|---|
| Commutative law | ( \mathbf{a} \cdot \mathbf{b} = \mathbf{b} \cdot \mathbf{a} ) | Order does not affect |
| Distributive law | ( \mathbf{a} \cdot (\mathbf{b} + \mathbf{c}) = \mathbf{a}\cdot\mathbf{b} + \mathbf{a}\cdot\mathbf{c} ) | Addition before dot product is possible |
| Self dot product | ( \mathbf{a} \cdot \mathbf{a} = \ | \mathbf{a} |
Python hands-on practice
Instance
import
numpy
as
np
a_runoob
=
np.
array
(
[
2
,
3
,
1
]
)
b_runoob
=
np.
array
(
[
4
,
1
,
2
]
)
Method 1: np.dot()
dot1
=
np.
dot
(
a_runoob
,
b_runoob
)
Method 2: @ operator (Recommended)
dot2
=
a_runoob
@
b_runoob
Method 3: Manual verification
dot3
=
sum
(
a_runoob
[
i
]
- b_runoob
[
i
]
for
i
in
range
(
len
(
a_runoob
)
)
)
(
"a · b ="
,
dot1
)
13
(
"The results of the three methods are consistent:"
,
dot1
==
dot2
==
dot3
)
Use geometry to deduce the angle
norm_a
=
np.
linalg
.
norm
(
a_runoob
)
norm_b
=
np.
linalg
.
norm
(
b_runoob
)
cos_theta
=
dot1 /
(
norm_a * norm_b
)
theta_deg
=
np.
degrees
(
np.
arccos
(
cos_theta
)
)
(
f
"||a||={norm_a:.2f}, ||b||={norm_b:.2f}, cosθ={cos_theta:.4f}"
)
(
f
"Angle = {theta_deg:.1f}°"
)
Verify geometric formula
verify
=
norm_a * norm_b * cos_theta
(
f
"||a||·||b||·cosθ = {verify:.4f} (consistent with dot product)"
)
Demonstration with different angles
(
"\n[1,0] · [0.8,0.6] ="
,
np.
array
(
[
1
,
0
]
)
@
np.
array
(
[
0.8
,
0.6
]
)
,
" (sharp angle, positive)"
)
(
"[1,0] · [0,1] ="
,
np.
array
(
[
1
,
0
]
)
@
np.
array
(
[
0
,
1
]
)
,
" (right angle, zero)"
)
(
"[1,0] · [-1,0] ="
,
np.
array
(
[
1
,
0
]
)
@
np.
array
(
[
1
,
0
]
)
,
" (obtuse angle, negative)"
)
Original prompt (Chinese, left untranslated)
a · b = 13
三种方法结果一致: True
||a||=3.74, ||b||=4.58, cosθ=0.7580
夹角 = 40.7°
[1,0] · [0.8,0.6] = 0.8 (锐角,正)
[1,0] · [0,1] = 0 (直角,零)
[1,0] · [-1,0] = -1.0 (钝角,负)
Interactive dot product demonstration
Drag the endpoints of vectors a and b and observe how the dot product value and angle change. The sign of the dot product determines whether the angle is acute, obtuse, or right:
Application scenarios in AI
Fully connected layer = batch calculation of dot products
A neuron calculation ( y = w_1x_1 + w_2x_2 + \cdots + w_nx_n + b ). The summation part is the dot product of the weight vector w and the input vector x.
When you see a fully connected layernn.Linear(784, 256), mathematically it does the following: 256 neurons, each neuron takes its own weight vector (784 dimensions) and performs a dot product with the input vector, plus a bias. 256 parallel dot product calculations = matrix multiplication.
Attention mechanism = dot product + Softmax
The core operation of Transformer: performing a dot product between the Query vector and the Key vector, where a larger result indicates greater "relevance" between the two.
[ \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V ]
( QK^T ) is the multiplication of a seq_len × d_k matrix and a d_k × seq_len matrix, and the value at position (i, j) in the resulting matrix = the dot product of the i-th Query and the j-th Key. A larger value indicates higher "attention" of the i-th position to the j-th position.
Dividing by ( \sqrt{d_k} ) is to prevent the dot product value from being too large, which could cause the Softmax gradient to disappear. All modern large language models such as GPT, BERT, and Claude are based on this dot-product attention mechanism.
Similarity calculation in recommendation systems
The dot product of the user vector and the item vector yields the user's "preference score" for that item. This is the core of matrix factorization recommendation algorithms—the rating matrix $\approx$ User latent vector matrix $\times$ Transpose of Item latent vector matrix.