jax.numpy.tanh#

jax.numpy.tanh(x, /)[原始碼]#

計算輸入的逐元素雙曲正切值。

numpy.tanh 的 JAX 實作。

雙曲正切的定義為

\[tanh(x) = \frac{sinh(x)}{cosh(x)} = \frac{e^x - e^{-x}}{e^x + e^{-x}}\]
參數:

x (ArrayLike) – 輸入陣列或純量。

返回:

一個陣列,包含 x 每個元素的雙曲正切值,並提升為非精確 dtype。

返回類型:

Array

注意

jnp.tanh 等同於計算 -1j * jnp.tan(1j * x)

另請參閱

範例

>>> x = jnp.array([[-1, 0, 1],
...                [3, -2, 5]])
>>> with jnp.printoptions(precision=3, suppress=True):
...   jnp.tanh(x)
Array([[-0.762,  0.   ,  0.762],
       [ 0.995, -0.964,  1.   ]], dtype=float32)
>>> with jnp.printoptions(precision=3, suppress=True):
...   -1j * jnp.tan(1j * x)
Array([[-0.762+0.j,  0.   -0.j,  0.762-0.j],
       [ 0.995-0.j, -0.964+0.j,  1.   -0.j]],      dtype=complex64, weak_type=True)

對於複數值輸入

>>> with jnp.printoptions(precision=3, suppress=True):
...   jnp.tanh(2-5j)
Array(1.031+0.021j, dtype=complex64, weak_type=True)
>>> with jnp.printoptions(precision=3, suppress=True):
...   -1j * jnp.tan(1j * (2-5j))
Array(1.031+0.021j, dtype=complex64, weak_type=True)