Skip to content
class

Hardtanh

extendsModule
Hardtanh(min_val: float = -1.0, max_val: float = 1.0, inplace: bool = False, min_value: float | None = None, max_value: float | None = None)

Hardtanh activation function.

Applies element-wise:

Hardtanh(x)=clamp(x,  min_val,  max_val)={min_valif x<min_valxif min_val≤x≤max_valmax_valif x>max_val\text{Hardtanh}(x) = \text{clamp}(x,\; \text{min\_val},\; \text{max\_val}) = \begin{cases} \text{min\_val} & \text{if } x < \text{min\_val} \\ x & \text{if } \text{min\_val} \leq x \leq \text{max\_val} \\ \text{max\_val} & \text{if } x > \text{max\_val} \end{cases}

A piecewise-linear activation that clips the input to a fixed interval. With the default [-1, 1] range it approximates the tanh function using only comparisons and clamps, making it suitable for quantised models.

Parameters

min_valfloat= -1.0
Lower bound of the clamping range. Default: -1.0.
max_valfloat= 1.0
Upper bound of the clamping range. Default: 1.0.
inplacebool= False
If True, modifies the input tensor in-place. Default: False.

Notes

  • Input: (∗)(*) — any shape.
  • Output: (∗)(*) — same shape as input, values in [min_val,  max_val][\text{min\_val},\; \text{max\_val}].

ReLU6 is a special case of Hardtanh with min_val=0 and max_val=6.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.Hardtanh(min_val=-1.0, max_val=1.0)
>>> x = lucid.tensor([-3.0, -0.5, 0.0, 0.5, 3.0])
>>> m(x)
tensor([-1., -0.5, 0., 0.5, 1.])
>>> # Custom range for output normalisation
>>> m = nn.Hardtanh(min_val=0.0, max_val=6.0)
>>> x = lucid.randn(4, 64)
>>> out = m(x)
>>> out.shape
(4, 64)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(min_val: float = -1.0, max_val: float = 1.0, inplace: bool = False, min_value: float | None = None, max_value: float | None = None)

Initialise the Hardtanh 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.