Skip to content
class

Threshold

extendsModule
Threshold(threshold: float, value: float, inplace: bool = False)

Threshold activation function.

Applies element-wise:

Threshold(x)={xif x>thresholdvalueotherwise\text{Threshold}(x) = \begin{cases} x & \text{if } x > \text{threshold} \\ \text{value} & \text{otherwise} \end{cases}

Values that fall at or below the threshold are replaced with a constant value. This generalises the standard ReLU (threshold=0, value=0) to arbitrary cut-off points and fill values.

Parameters

thresholdfloat
The cut-off value. Elements strictly greater than this pass through unchanged.
valuefloat
The replacement constant for elements that do not exceed the threshold.
inplacebool= False
If True, modifies the input tensor in-place. Default: False.

Notes

  • Input: (∗)(*) — any shape.
  • Output: (∗)(*) — same shape as input.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.Threshold(threshold=1.0, value=-1.0)
>>> x = lucid.tensor([0.5, 1.0, 1.5, 2.0])
>>> m(x)
tensor([-1., -1., 1.5, 2.])
>>> # Use as a generalised ReLU with a non-zero floor
>>> m = nn.Threshold(threshold=0.0, value=0.0)
>>> x = lucid.randn(8, 32)
>>> out = m(x)
>>> out.shape
(8, 32)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(threshold: float, value: float, inplace: bool = False)

Initialise the Threshold module. See the class docstring for parameter semantics.

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.