Gradient Clipping › Worked Example: Gradient Clipping in a Two-Parameter Network

Michael BrenndoerferUpdated April 24, 20253 min read
Language AI HandbookAll chapters
Part 10 of 67Neural Network Foundations
Chapter 73Gradient Clipping › Worked Example: Gradient Clipping in a Two-Parameter Network
Reading section 4 of 14 · Worked Example: Gradient Clipping in a Two-Parameter Network · 3 minSection 4 of 14 · 3 min

Book contents

Section 4 of 14 · 3 min read

Gradient Clipping › Worked Example: Gradient Clipping in a Two-Parameter NetworkOne section · Sections in this chapter4 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To solidify the mechanics, let's work through a concrete example with a tiny network where we can track every number. Consider a network with exactly two scalar parameters, w1w_1 and w2w_2, and a simple quadratic loss. This stripped-down scenario lets us see exactly how each clipping method transforms the gradients and what effect that has on the parameter update.

Suppose after a forward and backward pass, we compute:

∂L∂w1=8.0and∂L∂w2=6.0\frac{\partial L}{\partial w_1} = 8.0 \quad \text{and} \quad \frac{\partial L}{\partial w_2} = 6.0

The gradient vector is g=[8.0,6.0]\mathbf{g} = [8.0, 6.0], with global norm:

∥g∥2=82+62=64+36=100=10.0\|\mathbf{g}\|_2 = \sqrt{8^2 + 6^2} = \sqrt{64 + 36} = \sqrt{100} = 10.0

We will apply both clipping methods with threshold τ=5.0\tau = 5.0 and compare the results.

Clip by value with τ=5.0\tau = 5.0: Each component is clamped independently to [−5,5][-5, 5].

g1clipped=min⁡(5,8)=5.0andg2clipped=min⁡(5,6)=5.0g_1^{\text{clipped}} = \min(5, 8) = 5.0 \quad \text{and} \quad g_2^{\text{clipped}} = \min(5, 6) = 5.0

The clipped gradient is [5.0,5.0][5.0, 5.0] with norm 25+25=52≈7.07\sqrt{25 + 25} = 5\sqrt{2} \approx 7.07. Notice what happened: the original gradient had g1>g2g_1 > g_2, suggesting that w1w_1 should receive a larger update. After clipping, both parameters receive the same update magnitude of 5.0. The information that w1w_1 needed a bigger adjustment than w2w_2 has been discarded.

Clip by norm with τ=5.0\tau = 5.0: The entire gradient is scaled by 5.010.0=0.5\frac{5.0}{10.0} = 0.5.

g1clipped=8.0×0.5=4.0andg2clipped=6.0×0.5=3.0g_1^{\text{clipped}} = 8.0 \times 0.5 = 4.0 \quad \text{and} \quad g_2^{\text{clipped}} = 6.0 \times 0.5 = 3.0

The clipped gradient is [4.0,3.0][4.0, 3.0] with norm 16+9=25=5.0\sqrt{16 + 9} = \sqrt{25} = 5.0. The ratio g1/g2=4.0/3.0≈1.33g_1 / g_2 = 4.0 / 3.0 \approx 1.33 is the same as the original ratio 8.0/6.0≈1.338.0 / 6.0 \approx 1.33. The direction is perfectly preserved, and the magnitude is exactly at the threshold.

In[13]:
Code
import torch

# Two-parameter gradient example
g = torch.tensor([8.0, 6.0])
tau = 5.0

# Clip by value
g_value = torch.clamp(g, -tau, tau)

# Clip by norm
g_norm_val = g.norm(p=2).item()
if g_norm_val > tau:
    g_norm_clipped = g * (tau / g_norm_val)
else:
    g_norm_clipped = g.clone()
Out[14]:
Console
Worked Example: Two-Parameter Gradient Clipping
=======================================================

Original gradient:   [8.0, 6.0]
Original norm:       10.0000
Threshold tau:       5.0

Clip by Value:
  Clipped gradient:  [5.0, 5.0]
  Resulting norm:    7.0711
  Ratio g1/g2 before: 1.3333
  Ratio g1/g2 after:  1.0000
  Direction preserved? No

Clip by Norm:
  Scale factor:      0.5000
  Clipped gradient:  [4.0, 3.0]
  Resulting norm:    5.0000
  Ratio g1/g2 before: 1.3333
  Ratio g1/g2 after:  1.3333
  Direction preserved? Yes

The output confirms the theoretical analysis. Clip by value modifies the gradient ratio from 1.33 to 1.00, erasing the relative information. Clip by norm maintains the exact ratio, preserving the directional intent of the original gradient while reducing its magnitude to the threshold.

This example, while small, captures the essential behavior in networks with millions of parameters. The gradient vector in a real network encodes the relative importance of updating every weight, and norm clipping respects that encoding while imposing a safe step size limit.