Skip to content
fn

threshold

→Tensor
threshold(input: Tensor, threshold: float, value: float, inplace: bool = False)

Threshold activation — gate elements by a scalar cutoff.

Replaces every element of input 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

inputTensor
Input tensor.
thresholdfloat
Cutoff tt. Elements satisfying x>tx > t are kept.
valuefloat
Replacement constant for elements not above the threshold.
inplacebool= False
If True, write the result into input and return input.

Returns

Tensor

Thresholded tensor with the same shape as input.

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 v≠tv \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., -99., -99., 1.])

Used by 2