class

Hardtanh

extendsModule
Hardtanh(min_val: float = -1.0, max_val: float = 1.0, inplace: bool = False)
source

Hardtanh activation function.

Applies element-wise:

Hardtanh(x)=clamp(x,  min_val,  max_val)={min_valif x<min_valxif min_valxmax_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
Accepted for API compatibility; currently unused. 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)

Methods (3)

dunder

__init__

None
__init__(min_val: float = -1.0, max_val: float = 1.0, inplace: bool = False)
source

Initialise the Hardtanh module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor)
source

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.