torch.heaviside
function heaviside(input: unknown, values: unknown, options: unknown): TensorComputes the Heaviside step function element-wise.
Returns 1 where input > 0, 0 where input < 0, and values at input == 0. Essential for:
- Discontinuous functions and indicator variables
- Conditional logic in differentiable models (gate mechanisms)
- Unit step responses in control theory
- Activation threshold functions
- On-off switches and binary conditions
- Zero handling: Parameter v controls behavior at exactly x = 0
- Discontinuous: Has jump discontinuity at x = 0
- ReLU relation: ReLU(x) = x * Heaviside(x)
Non-differentiable: Not differentiable at x = 0
Parameters
inputunknown- The input tensor
valuesunknown- The value to use at input == 0 (can be scalar or tensor)
optionsunknown- Optional settings including
outparameter
Returns
Tensor– A new tensor with Heaviside(x) valuesExamples
// Basic usage - step function with custom zero value
const x = torch.tensor([-1, 0, 1]);
torch.heaviside(x, torch.tensor([0.5])); // [0, 0.5, 1]
// Standard Heaviside: zero value is 0
const x = torch.tensor([-5, -1, 0, 1, 5]);
torch.heaviside(x, torch.tensor([0])); // [0, 0, 0, 1, 1]
// Indicator: mask elements above threshold
const signal = torch.tensor([1.5, -0.5, 2.3]);
torch.heaviside(signal, torch.tensor([0])); // [1, 0, 1]See Also
- PyTorch torch.heaviside()
- relu - Related: ReLU(x) = x * Heaviside(x)
- where - Conditional selection