Hardtanh — PyTorch 2.7 documentation (original) (raw)

class torch.nn.Hardtanh(min_val=-1.0, max_val=1.0, inplace=False, min_value=None, max_value=None)[source][source]

Applies the HardTanh function element-wise.

HardTanh is defined as:

HardTanh(x)={max_val if x> max_val min_val if x< min_val x otherwise \text{HardTanh}(x) = \begin{cases} \text{max\_val} & \text{ if } x > \text{ max\_val } \\ \text{min\_val} & \text{ if } x < \text{ min\_val } \\ x & \text{ otherwise } \\ \end{cases}

Parameters

Keyword arguments min_value and max_valuehave been deprecated in favor of min_val and max_val.

Shape:

../_images/Hardtanh.png

Examples:

m = nn.Hardtanh(-2, 2) input = torch.randn(2) output = m(input)