Loading...
Implement the "Hard Tanh" function which clamps values to a range.
Hard Tanh is a computationally cheaper approximation of the tanh function:
It's essentially a piecewise linear function that clips values outside [-1, 1].
Write a function hard_tanh(values) that applies Hard Tanh element-wise to a list.
Return a list of floats with Hard Tanh applied. Round each value to 4 decimal places.
values=[-1.5, 0.5, 2.0]
[-1.0, 0.5, 1.0]
-1.5 < -1 → clamped to -1.0; 0.5 is in [-1,1] → stays 0.5; 2.0 > 1 → clamped to 1.0.