The Operation You'll See Everywhere
Pick any two vectors of the same length. The dot product is: multiply element-wise, then sum. . One scalar out. That's it. That's the whole formula. And yet every modern neural network is roughly "do a few billion of these and call it inference."
Why is it so central? Because the dot product secretly measures similarity. Specifically: , where is the angle between them. Same direction → big positive. Opposite direction → big negative. Perpendicular (no shared direction) → zero. It's a numerical answer to "how aligned are these two things?"
Three Lenses on the Same Operation
| Lens | What means |
|---|---|
| Geometric | Length of the projection of one vector onto the other, weighted by length |
| Statistical | Unnormalized correlation between two feature lists |
| AI / ML | How much one embedding "agrees with" another — used in attention, retrieval, classification |
Where You'll Meet It
- Attention scores: a query vector dotted with each key vector → which tokens to focus on.
- Cosine similarity: dot product divided by the product of norms → "how similar are these two embeddings?" Used in semantic search, recommendation systems, clustering.
- Logits → softmax: the network's final layer is a stack of dot products between the last hidden state and a class-embedding matrix.
- Backprop chain: every weight update involves dot products with gradients.
import numpy as np
np.random.seed(42)
128차원 임의 벡터 두 개
a = np.random.randn(128) b = np.random.randn(128)
코사인 유사도 함수
def cosine_similarity(v1, v2): return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
첫 번째 비교
sim_ab = cosine_similarity(a, b) print(f"a와 b의 코사인 유사도: {sim_ab:.6f}")
a를 100배한 세 번째 벡터
c = a * 100
c와 b의 유사도
sim_cb = cosine_similarity(c, b) print(f"c와 b의 코사인 유사도: {sim_cb:.6f}")
두 결과 비교
print(f"두 유사도가 같은가? {np.isclose(sim_ab, sim_cb)}")
크기는 어떻게 변했나
print(f"\n||a|| = {np.linalg.norm(a):.4f}") print(f"||c|| = {np.linalg.norm(c):.4f}") print(f"c의 크기는 a의 {np.linalg.norm(c) / np.linalg.norm(a):.0f}배")
a와 b의 코사인 유사도: 0.029607 c와 b의 코사인 유사도: 0.029607 두 유사도가 같은가? True
크기만 변했지(스칼라) 방향(벡터)는 변하지 않았기 때문에 코사인 유사도는 같음, 두 백터에 어떤 값을 곱해도 동일하다