fn
clip_grad_value_
→Noneclip_grad_value_(parameters: Iterable[Parameter], clip_value: float)Clamp every gradient element to 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 ParameterParameters whose
.grad should be clipped. Entries with
grad is None are skipped.clip_valuefloatSymmetric magnitude bound. Must be non-negative; gradients are
clamped to
[-clip_value, +clip_value].Returns
NoneThe clipping happens in place via Parameter.grad.
Notes
The element-wise update is
with . 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)