Skip to content
fn

clamp

→Tensor
clamp(input: Tensor, min: TensorOrScalar | None = ..., max: TensorOrScalar | None = ...)

Element-wise clamping to a closed interval.

Constrains each entry of input to lie within [min, max]. Either bound may be None to leave that side unbounded.

Parameters

inputTensor
Input tensor.
minscalar or None= ...
Lower bound. None disables the lower clip.
maxscalar or None= ...
Upper bound. None disables the upper clip.

Returns

Tensor

Tensor with the same shape and dtype as input, with values constrained to [min, max].

Notes

Mathematical definition:

outi=min⁡(max⁡(inputi,min),max)\text{out}_i = \min(\max(\text{input}_i, \text{min}), \text{max})

Gradient flows through where the input lies strictly inside the interval and is zero at the clipped boundaries. If min > max every element is set to max. Equivalent to clip.

Examples

>>> import lucid
>>> x = lucid.tensor([-3.0, 0.0, 3.0])
>>> lucid.clamp(x, -1.0, 1.0)
Tensor([-1., 0., 1.])