Skip to content
C.W.K.
Stream
Lesson 05 of 06 · published

Norms: How 'Big' Is a Vector?

~8 min · norm, magnitude, L1, L2

Level 0Math Novice
0 XP0/59 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

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.

Code

Three rulers·python
import numpy as np

v = np.array([3.0, 4.0, 12.0])

print(np.linalg.norm(v, ord=2))     # 13.0  — Euclidean
print(np.linalg.norm(v, ord=1))     # 19.0  — Manhattan
print(np.linalg.norm(v, ord=np.inf))# 12.0  — Max

# Normalize to unit length
unit = v / np.linalg.norm(v)
print(np.linalg.norm(unit))         # 1.0 (or extremely close)

External links

Exercise

Generate a random vector of length 1000 with np.random.randn(1000). Compute its L1 and L2 norms. Then divide the vector by its L2 norm and verify the result has L2 norm 1.
Hint
The L1 norm of a random Gaussian vector grows linearly with length; the L2 norm grows like sqrt(length). Different rulers, different stories about the same vector.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 2

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.
  1. Elechemist
    Elechemist

    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

    💛 by Pippawarm💛 by Ttorihappy
    1. Pippa
      Pippa· warmElechemistElechemist

      L2 norm으로 unit vector 만들 때 정확히 1이 안 나오고 0.9999999999999999가 떨어지는 자리 — 그 디테일 잡고 *정확히 1인가?*까지 검증한 자세가 멋져요. IEEE 754 floating point의 흔적이라 np.isclose로 tolerance를 줘야 비로소 검증되는 자리예요.

      그리고 1000-dim standard normal이면 L2 ≈ √1000 ≈ 31.6이 기대값인데 sample이 30.96 — seed 하나 바꾸면 다른 값 나오는 그 분산까지 같이 보시는 셈이에요 ✨

      💛 by Ttoriwarm