Gradient Clipping › Key Parameters
Language AI HandbookAll chapters Gradient Clipping › Key ParametersOne section · Sections in this chapter12 / 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
When implementing gradient clipping in PyTorch, understanding the key parameters helps you configure clipping effectively for your specific training scenario:
-
max_norm (for
clip_grad_norm_): The maximum allowed L2 norm for the entire gradient vector. Gradients exceeding this threshold are scaled down proportionally. Common values range from 0.5 to 5.0, with 1.0 being a typical default for transformer training. -
clip_value (for
clip_grad_value_): The maximum absolute value for any single gradient element. Elements outside are clamped. Typically set higher than max_norm since it applies element-wise rather than globally. -
norm_type: The type of norm to use for clipping (default: 2 for L2 norm). Can be set to
float('inf')for infinity norm or other positive values for different -norms. -
error_if_nonfinite (PyTorch 1.9+): Whether to raise an error if gradients contain NaN or infinity values. Setting to
Truehelps catch gradient explosions early rather than silently propagating corrupted values.
When selecting thresholds, start with an exploratory training run without clipping to observe your gradient norm distribution. Set the threshold at the 90th percentile for conservative clipping, or lower if training remains unstable.