diff --git a/ivy/array/activations.py b/ivy/array/activations.py index 466225feed42d..449811143adb6 100644 --- a/ivy/array/activations.py +++ b/ivy/array/activations.py @@ -71,12 +71,28 @@ def tanh(self: ivy.Array, *, out: Optional[ivy.Array] = None) -> ivy.Array: function, and so the docstring for ivy.tanh also applies to this method with minimal changes. + Parameters + ---------- + self + input array whose elements each represent a hyperbolic angle. + Should have a real-valued floating-point data type. + out + optional output, for writing the result to. It must have a shape that the + inputs broadcast to. + + Returns + ------- + ret + an array containing the hyperbolic tangent of each element in ``self``. + The returned array must have a real-valued floating-point data type + determined by :ref:`type-promotion`. + Examples -------- - >>> x = ivy.array([0.55 , -0.55]) + >>> x = ivy.array([0., 1., 2.]) >>> y = x.tanh() >>> print(y) - ivy.array([ 0.501, -0.501]) + ivy.array([0., 0.762, 0.964]) """ return ivy.tanh(self._data, out=out) diff --git a/ivy/container/activations.py b/ivy/container/activations.py index bd774c8b260c4..189ae95d21ea1 100644 --- a/ivy/container/activations.py +++ b/ivy/container/activations.py @@ -360,7 +360,7 @@ def gelu( @staticmethod def static_tanh( - x: Union[ivy.Array, ivy.NativeArray, ivy.Container], + x: ivy.Container, key_chains: Optional[Union[List[str], Dict[str, str]]] = None, to_apply: bool = True, prune_unapplied: bool = False, @@ -370,13 +370,14 @@ def static_tanh( ) -> ivy.Container: """ ivy.Container static method variant of ivy.tanh. - This method simply wraps the function, and so the docstring - for ivy.tanh also applies to this method with minimal changes. + This method simply wraps the function, and so the docstring for + ivy.tanh also applies to this method with minimal changes. Parameters ---------- x - input container. + input container whose elements each represent a hyperbolic angle. + Should have a real-valued floating-point data type. key_chains The key-chains to apply or not apply the method to. Default is None. to_apply @@ -394,18 +395,19 @@ def static_tanh( Returns ------- ret - a container with Hyperbolic tangent activation function - applied element-wise. + an container containing the hyperbolic tangent of each element in ``x``. + The returned array must have a real-valued floating-point data type + determined by :ref:`type-promotion`. Examples -------- - >>> x = ivy.Container(a=ivy.array([[0.55 , -0.55]])) + >>> x = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([3., 4., 5.])) >>> y = ivy.Container.static_tanh(x) >>> print(y) { - a: ivy.array([[0.501, -0.501]]) + a: ivy.array([0., 0.76, 0.96]), + b: ivy.array([0.995, 0.999, 0.9999]) } - """ return ContainerBase.multi_map_in_static_method( "tanh", @@ -428,13 +430,14 @@ def tanh( ) -> ivy.Container: """ ivy.Container instance method variant of ivy.tanh. - This method simply wraps the function, and so the docstring - for ivy.tanh also applies to this method with minimal changes. + This method simply wraps the function, and so the docstring for + ivy.tanh also applies to this method with minimal changes. Parameters ---------- self - input container. + input container whose elements each represent a hyperbolic angle. + Should have a real-valued floating-point data type. key_chains The key-chains to apply or not apply the method to. Default is None. to_apply @@ -452,18 +455,20 @@ def tanh( Returns ------- ret - a container with Hyperbolic tangent activation function - applied element-wise. + an container containing the hyperbolic tangent of each element in + ``self``. The returned container must have a real-valued floating-point + data type determined by :ref:`type-promotion`. Examples -------- - >>> x = ivy.Container(a=ivy.array([[0.55 , -0.55]])) + >>> x = ivy.Container(a=ivy.array([0., 1., 2.]),\ + b=ivy.array([3., 4., 5.])) >>> y = x.tanh() >>> print(y) { - a: ivy.array([[0.501, -0.501]]) + a:ivy.array([0., 0.762, 0.964]), + b:ivy.array([0.995, 0.999, 1.]) } - """ return self.static_tanh( self, key_chains, to_apply, prune_unapplied, map_sequences, out=out diff --git a/ivy/functional/ivy/activations.py b/ivy/functional/ivy/activations.py index a35df15981cb8..e00529f7e5271 100644 --- a/ivy/functional/ivy/activations.py +++ b/ivy/functional/ivy/activations.py @@ -188,53 +188,87 @@ def gelu( @handle_out_argument @handle_nestable def tanh( - x: Union[ivy.Array, ivy.NativeArray], *, out: Optional[ivy.Array] = None + x: Union[ivy.Array, ivy.NativeArray], + *, + out: Optional[ivy.Array] = None, ) -> ivy.Array: - """Applies the Hyperbolic tangent activation function element-wise. + """ + Calculates an implementation-dependent approximation to the hyperbolic tangent, having domain ``[-infinity, +infinity]`` and codomain ``[-1, +1]``, for each element ``x_i`` of the input array ``x``. + + **Special cases** + + For floating-point operands, + + - If ``x_i`` is ``NaN``, the result is ``NaN``. + - If ``x_i`` is ``+0``, the result is ``+0``. + - If ``x_i`` is ``-0``, the result is ``-0``. + - If ``x_i`` is ``+infinity``, the result is ``+1``. + - If ``x_i`` is ``-infinity``, the result is ``-1``. Parameters ---------- x - input array + input array whose elements each represent a hyperbolic angle. Should have a real-valued floating-point data + type. out - optional output array, for writing the result to. It must have a shape that the - inputs broadcast to. + optional output, for writing the result to. It must have a shape that the inputs + broadcast to. Returns ------- ret - The input array with Hyperbolic tangent activation applied element-wise. + an array containing the hyperbolic tangent of each element in ``x``. The returned array must have a real-valued + floating-point data type determined by :ref:`type-promotion`. - Functional Examples - ------------------- + This method conforms to the `Array API Standard + `_. This docstring is an extension of the `docstring `_ # noqa in the standard. The descriptions above assume an array input for simplicity, but + the method also accepts :code:`ivy.Container` instances in place of + :code:`ivy.Array` or :code:`ivy.NativeArray` instances, as shown in the type hints + and also the examples below. - With :code: `ivy.Array` input: - >>> x = ivy.array([0.55 , -0.55]) + Examples + -------- + With :code:`ivy.Array` input: + + >>> x = ivy.array([0., 1., 2.]) >>> y = ivy.tanh(x) >>> print(y) - ivy.array([0.501, -0.501]) - - With :code: `ivy.NativeArray` input: + ivy.array([0., 0.762, 0.964]) - >>> x = ivy.native_array([0., -1., 2.]) - >>> y = ivy.tanh(x) + >>> x = ivy.array([0.5, -0.7, 2.4]) + >>> y = ivy.zeros(3) + >>> ivy.tanh(x, out=y) >>> print(y) - ivy.array([0., -0.762, 0.964]) + ivy.array([0.462, -0.604, 0.984]) - Instance Method Example - ----------------------- + >>> x = ivy.array([[1.1, 2.2, 3.3],\ + [-4.4, -5.5, -6.6]]) + >>> ivy.tanh(x, out=x) + >>> print(x) + ivy.array([[0.8, 0.976, 0.997], + [-1., -1., -1.]]) - Using :code: `ivy.Array` instance method: + With :code:`ivy.NativeArray` input: - >>> x = ivy.array([0.55 , -0.55]) - >>> y = x.tanh() + >>> x = ivy.native_array([0., 1., 2.]) + >>> y = ivy.tanh(x) >>> print(y) - ivy.array([0.501, -0.501]) + ivy.array([0., 0.762, 0.964]) + With :code:`ivy.Container` input: + + >>> x = ivy.Container(a=ivy.array([0., 1., 2.]),\ + b=ivy.array([3., 4., 5.])) + >>> y = ivy.tanh(x) + >>> print(y) + { + a: ivy.array([0., 0.762, 0.964]), + b: ivy.array([0.995, 0.999, 1.]) + } """ - return current_backend(x).tanh(x, out=out) + return ivy.current_backend(x).tanh(x, out=out) @to_native_arrays_and_back