Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1574 reformating and docstring examples for tanh(), activations.py #2304

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions ivy/array/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
39 changes: 22 additions & 17 deletions ivy/container/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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
Expand Down
80 changes: 57 additions & 23 deletions ivy/functional/ivy/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://data-apis.org/array-api/latest/>`_. This docstring is an extension of the `docstring <https://data-apis.org/array-api/latest/API_specification/generated/signatures.elementwise_functions.tanh.html>`_ # 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
Expand Down