fn
hardtanh
→Tensorhardtanh(x: 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
xTensorInput tensor of any shape.
min_valfloat= -1.0Lower clamp. Default
-1.0.max_valfloat= 1.0Upper clamp. Default
1.0.inplacebool= FalseAccepted for API compatibility; currently ignored.
Returns
TensorClamped tensor with the same shape as x.
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.0000, -0.5000, 0.0000, 0.5000, 1.0000])