fn

clip

Tensor
clip(input: Tensor, min: Scalar | None, max: Scalar | None)
source

Element-wise clamping to a closed interval.

Clips each entry of input so that it lies within [min, max]. Either bound may be None to leave that side unbounded. Alias of clamp.

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 min < x < max; at the clipped boundaries it is treated as zero. If min > max the result is implementation-defined.

Examples

>>> import lucid
>>> x = lucid.tensor([-2.0, -0.5, 0.5, 2.0])
>>> lucid.clip(x, -1.0, 1.0)
Tensor([-1., -0.5, 0.5, 1.])