hard_tanh

Contents

hard_tanh#

class saiunit.math.hard_tanh(x, unit_to_scale=None)#

Hard \(\mathrm{tanh}\) activation function.

Computes the element-wise function:

\[\begin{split}\mathrm{hard\_tanh}(x) = \begin{cases} -1, & x < -1\\ x, & -1 \le x \le 1\\ 1, & 1 < x \end{cases}\end{split}\]
Parameters:
  • x (Union[saiunit.Quantity, Array, ndarray, number, bool]) – Input array. Must be unitless if a Quantity.

  • unit_to_scale (Optional[saiunit.Unit]) – Unit used to convert x to a dimensionless number before applying the activation.

Returns:

out – An array with values clipped to the range [-1, 1].

Return type:

Array

Examples

>>> import jax.numpy as jnp
>>> import saiunit.math as sumath
>>> sumath.hard_tanh(jnp.array([-2., -0.5, 0., 0.5, 2.]))
Array([-1. , -0.5,  0. ,  0.5,  1. ], dtype=float32)