hard_tanh

Contents

hard_tanh#

class brainunit.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 (Quantity | Array | ndarray | bool | number | bool | int | float | complex) – Input array. Must be unitless if a Quantity.

  • unit_to_scale (Unit | None) – 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)