class
Hardtanh
extends
ModuleHardtanh(min_val: float = -1.0, max_val: float = 1.0, inplace: bool = False)Hardtanh activation function.
Applies element-wise:
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.0Lower bound of the clamping range. Default:
-1.0.max_valfloat= 1.0Upper bound of the clamping range. Default:
1.0.inplacebool= FalseAccepted for API compatibility; currently unused. Default:
False.Notes
- Input: — any shape.
- Output: — same shape as input, values in .
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)Initialise the Hardtanh module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor)Apply the activation function element-wise.
Parameters
inputTensorInput tensor of arbitrary shape.
Returns
TensorOutput tensor of the same shape as input.
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.