fn

threshold

Tensor
threshold(x: Tensor, threshold: float, value: float, inplace: bool = False)
source

Threshold activation — gate elements by a scalar cutoff.

Replaces every element of x that is not strictly greater than threshold with the constant value. A generalisation of relu (recovered by threshold=0, value=0) and a common component of dead-zone non-linearities.

Parameters

xTensor
Input tensor.
thresholdfloat
Cutoff tt. Elements satisfying x>tx > t are kept.
valuefloat
Replacement constant for elements not above the threshold.
inplacebool= False
Accepted for API compatibility; currently ignored.

Returns

Tensor

Thresholded tensor with the same shape as x.

Notes

threshold(x;t,v)={xx>tvotherwise\text{threshold}(x; t, v) = \begin{cases} x & x > t \\ v & \text{otherwise} \end{cases}

Discontinuous at x=tx = t when vtv \ne t; the derivative is 11 above the threshold and 00 below. Useful as a deterministic alternative to relu when a non-zero "off" value is needed (e.g. setting masked positions to a sentinel).

Examples

>>> import lucid
>>> from lucid.nn.functional import threshold
>>> x = lucid.tensor([-1.0, 0.0, 0.5, 1.0])
>>> threshold(x, threshold=0.5, value=-99.0)
Tensor([-99.0000, -99.0000, -99.0000,   1.0000])