fn

heaviside

Tensor
heaviside(x: Tensor, values: Tensor | Scalar)
source

Heaviside step function with a user-selectable value at x=0x = 0.

Produces 0 for negative inputs and 1 for positive inputs; the value at exactly zero is taken from values (typically 0, 0.5, or 1 depending on convention).

Parameters

xTensor
Input tensor.
valuesTensor | Scalar
Value(s) used wherever x == 0. If a scalar, it is broadcast to the shape of x; if a tensor, it must broadcast against x.

Returns

Tensor

Element-wise Heaviside step.

Notes

Mathematical definition:

H(xi)={0,xi<0,valuesi,xi=0,1,xi>0.H(x_i) = \begin{cases} 0, & x_i < 0, \\ \text{values}_i, & x_i = 0, \\ 1, & x_i > 0. \end{cases}

The gradient is zero almost everywhere — the derivative is a Dirac delta in the distributional sense, which autograd does not propagate.

Examples

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