hardtanh(input: Tensor, min_val: float = -1.0, max_val: float = 1.0, inplace: bool = False)Hardtanh — element-wise clamp to .
A cheap piecewise-linear surrogate for tanh whose forward and
backward passes require no transcendental functions. Standard
activation in compact / quantised RNN cells.
Parameters
inputTensorInput tensor of any shape.
min_ valfloat= -1.0Lower clamp. Default
-1.0.max_ valfloat= 1.0Upper clamp. Default
1.0.inplacebool= FalseIf
True, write the result into input and return input.Returns
TensorClamped tensor with the same shape as input.
Notes
Derivative is inside the linear region and in the saturated regions — gradients stop flowing through saturated units, a drawback compared to smooth tanh but acceptable when the linear region covers the bulk of activations.
Examples
>>> import lucid
>>> from lucid.nn.functional import hardtanh
>>> x = lucid.tensor([-2.0, -0.5, 0.0, 0.5, 2.0])
>>> hardtanh(x)
tensor([-1., -0.5, 0., 0.5, 1.])