fn

clip_grad_value_

None
clip_grad_value_(parameters: Iterable[Parameter], clip_value: float)
source

Clamp every gradient element to [clip_value,clip_value][-\text{clip\_value}, \text{clip\_value}] in place.

Unlike clip_grad_norm_, which preserves the direction of the full gradient vector, this operates element-wise — each scalar entry is independently clipped to the symmetric interval. Cheap, and a useful safety net when only a handful of weights tend to blow up (e.g. embedding tables on rare tokens).

Parameters

parametersiterable of Parameter
Parameters whose .grad should be clipped. Entries with grad is None are skipped.
clip_valuefloat
Symmetric magnitude bound. Must be non-negative; gradients are clamped to [-clip_value, +clip_value].

Returns

None

The clipping happens in place via Parameter.grad.

Notes

The element-wise update is

gi    clip(gi,c,+c),g_i \;\mapsto\; \mathrm{clip}(g_i,\, -c,\, +c),

with c=clip_valuec = \text{clip\_value}. Because each component is treated independently the direction of the gradient vector is not preserved — large coordinates are flattened toward zero while small ones pass through unchanged.

Examples

>>> from lucid.nn.utils import clip_grad_value_
>>> # after loss.backward() ...
>>> clip_grad_value_(model.parameters(), clip_value=0.5)