Several Rulers for Vector Size
A scalar's size is its absolute value, but vectors admit several useful norms:
- L2 norm: , the Euclidean distance from the origin.
- L1 norm: , connected to distance along an axis-aligned grid.
- L∞ norm: , determined by the largest component.
Unqualified “norm” often means L2, but APIs differ. Changing the norm changes the geometry of what counts as large or nearby.
Normalization and Regularization Are Different Operations
- Vector normalization divides by a norm, as in , to produce unit length. It is undefined for the zero vector without an explicit convention.
- L1 regularization adds to an objective and tends to favor sparse solutions; LASSO is the standard example.
- L2 regularization adds a penalty such as to discourage large weights. Traditional SGD weight decay is closely related, while optimizers such as AdamW decouple weight decay from the gradient-based penalty.
- Gradient clipping rescales a gradient when its norm exceeds a threshold to reduce exploding updates.
The same norm can be used in distinct operations. State whether you are measuring, dividing by the measurement, adding a penalty, or clipping an update.
A norm is a ruler. The operation is not fully specified until you say what you do with that measurement.
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