Multiple Ways to Be Big
For a scalar, "size" is unambiguous — . For a vector you have choices, and each choice has a name and a use case:
- L2 norm (Euclidean): — the straight-line length you'd measure with a ruler. Default norm when nobody specifies.
- L1 norm (Manhattan): — the taxi-cab distance, summing absolute values. Encourages sparsity in optimization (some components go all the way to zero).
- L∞ norm (max): — just the biggest absolute value. Used when "the worst element" matters most.
Where Each Shows Up
- L2 regularization (weight decay) — penalize . Common in deep learning. Encourages small, distributed weights.
- L1 regularization (LASSO) — penalize . Drives many weights exactly to zero — automatic feature selection.
- Gradient clipping — if exceeds a threshold, scale it down. Keeps training stable when gradients explode.
Normalizing a Vector
"Normalize" a vector usually means: divide by its L2 norm so the result has length 1. The direction is preserved; the magnitude becomes exactly 1. Then you can compare directions cleanly.
Norms are the rulers you choose. Different rulers measure different kinds of "big." Pick the one that matches what you're trying to constrain.
import numpy as np
1000 길이 임의 벡터 생성
np.random.seed(42) v = np.random.randn(1000)
L1, L2 norm 계산
l1 = np.linalg.norm(v, ord=1) l2 = np.linalg.norm(v, ord=2)
print(f"L1 norm: {l1:.4f}") print(f"L2 norm: {l2:.4f}")
L2 norm으로 나눠서 단위 벡터 만들기
unit = v / l2
결과 벡터의 L2 norm 확인
new_l2 = np.linalg.norm(unit, ord=2) print(f"\nunit vector의 L2 norm: {new_l2}") print(f"정확히 1인가? {np.isclose(new_l2, 1.0)}")
#결과 L1 norm: 779.4946 L2 norm: 30.9561 unit vector의 L2 norm: 0.9999999999999999 정확히 1인가? True