Skip to content
Glacius
OptimizationConcept reference

L2 penalties

A λ||w||²/2 penalty contributes λw; a λ||w||² penalty contributes 2λw.

On this page 7 sections
  1. Overview
  2. An L2 penalty with λ≥0 adds squared coefficient size
  3. Differentiate each squared coordinate separately
  4. The penalty gradient adds to the data-loss gradient
  5. Key takeaway
  6. Sources & further reading
  7. Concept connections

01An L2 penalty with λ≥0 adds squared coefficient size#

An L2 penalty with λ≥0 adds squared coefficient size. Its gradient depends on the written coefficient:

P(w)=λ2w22P(w)=λw\begin{gathered}P(w)=\tfrac\lambda2\|w\|_2^2\\\nabla P(w)=\lambda w\end{gathered}

For λ=3 and w=(2,−1), the penalty gradient is (6,−3). The half cancels the derivative’s factor of two.

Penalty P(w)=3||w||²/2 has gradient 3w. At w=(2,−1), that gradient is (6,−3), three times the weight vector. The factor one-half cancels the derivative’s factor two; it is not 6w.Penalty P(w)=3||w||²/2 has gradient 3w. At w=(2,−1), that gradient is (6,−3), three times the weight vector. The factor one-half cancels the derivative’s factor two; it is not 6w.
Figure 1Penalty P(w)=3||w||²/2 has gradient 3w. At w=(2,−1), that gradient is (6,−3), three times the weight vector. The factor one-half cancels the derivative’s factor two; it is not 6w.
Link to this figure ↗Download SVGDownload PNG
Check your reasoning

P(w)=6w²/2. At w=-2, find P′(w).

Show answer and explanation
-12

Gradient factor 6; 6×(-2)=-12.

02Differentiate each squared coordinate separately#

Differentiate each squared coordinate separately. A negative weight contributes a negative gradient component; zero contributes zero.

Without the half, P(w)=λ||w||² has gradient 2λw. Read the written coefficient before computing each component.

Check your reasoning

P(w)=2||w||₂², w=(2,3)(2, -3). Find the full penalty gradient.

  1. A(8,11)(8, -11)
  2. B(8,12)(8, -12)
  3. C(9,12)(9, -12)
Show answer and explanation
(8,12)(8, -12)

The gradient is 4w=(8,12)(8, -12).

03The penalty gradient adds to the data-loss gradient#

The penalty gradient adds to the data-loss gradient. It is a vector contribution, not the scalar penalty value.

Read the actual objective before borrowing a memorized regularization factor.

Check your reasoning

P(w)=7w², w=3. Draft gradient 21 uses the other half-factor convention. Correct it.

Show answer and explanation
42

Gradient factor 14; 14×3=42.

Key takeaway

Check the half-factor and apply the resulting coefficient to every weight coordinate.

  • Compute the gradient contribution of a stated quadratic penalty.

Sources & further reading

  1. [1]
    D2L §3.7.1 equations 3.7.2–3.7.3D2L §3.7.1 equations 3.7.2–3.7.3 · Article

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “L2 penalties.” Math behind ML. /learn/o-ltwo-penalty