Skip to content

Commit

Permalink
reformatting activations sigmoid ivy-llc#754 ivy-llc#10446 (ivy-llc#1…
Browse files Browse the repository at this point in the history
…0485)

* reformatting activations sigmoid ivy-llc#754 ivy-llc#10446

* more modification to sigmoid function
  • Loading branch information
Monsurat-Onabajo authored Feb 17, 2023
1 parent e44891b commit f2bbd95
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
27 changes: 24 additions & 3 deletions ivy/array/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,33 @@ def gelu(
"""
return ivy.gelu(self._data, approximate=approximate, out=out)

def sigmoid(self: ivy.Array, /, *, out: Optional[ivy.Array] = None) -> ivy.Array:
def sigmoid(
self: ivy.Array,
/,
*,
out: Optional[ivy.Array] = None
) -> ivy.Array:

"""
ivy.Array instance method variant of ivy.sigmoid. This method simply wraps the
function, and so the docstring for ivy.sigmoid also applies to this method
ivy.Array instance method variant of ivy.sigmoid.
This method simply wraps the function, and so the docstring for ivy.sigmoid also applies to this method
with minimal changes.
Parameters
----------
self
Input array
out
optional output array for writing the result to. It must have the same shape the input broadcast to
default: None
Returns
-------
ret
an array with the sigmoid activation function applied element-wise.
Examples
--------
>>> x = ivy.array([-1., 1., 2.])
Expand Down
7 changes: 6 additions & 1 deletion ivy/functional/backends/jax/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ def relu(x: JaxArray, /, *, out: Optional[JaxArray] = None) -> JaxArray:
return jnp.maximum(x, 0)


def sigmoid(x: JaxArray, /, *, out: Optional[JaxArray] = None) -> JaxArray:
def sigmoid(
x: JaxArray,
/,
*,
out: Optional[JaxArray] = None
) -> JaxArray:
return 1 / (1 + jnp.exp(-x))


Expand Down
7 changes: 6 additions & 1 deletion ivy/functional/backends/numpy/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ def gelu(
return ivy.astype(ret, x.dtype, copy=False)


def sigmoid(x: np.ndarray, /, *, out: Optional[np.ndarray] = None) -> np.ndarray:
def sigmoid(
x: np.ndarray,
/,
*,
out: Optional[np.ndarray] = None
) -> np.ndarray:
if not ivy.is_array(x):
return np.asarray(1 / (1 + np.exp(-x)))
return np.asarray(1 / (1 + np.exp(-x))).astype(x.dtype)
Expand Down
7 changes: 6 additions & 1 deletion ivy/functional/backends/tensorflow/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def relu(x: Tensor, /, *, out: Optional[Tensor] = None) -> Tensor:
return tf.nn.relu(x)


def sigmoid(x: Tensor, /, *, out: Optional[Tensor] = None) -> Tensor:
def sigmoid(
x: Tensor,
/,
*,
out: Optional[Tensor] = None
) -> Tensor:
if not ivy.is_array(x):
x = float(x)
return tf.nn.sigmoid(x)
Expand Down
7 changes: 6 additions & 1 deletion ivy/functional/backends/torch/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ def gelu(


@with_unsupported_dtypes({"1.11.0 and below": ("float16",)}, backend_version)
def sigmoid(x: torch.Tensor, /, *, out: Optional[torch.Tensor] = None) -> torch.Tensor:
def sigmoid(
x: torch.Tensor,
/,
*,
out: Optional[torch.Tensor] = None
) -> torch.Tensor:
if not ivy.is_array(x):
x = torch.tensor(x)
return torch.sigmoid(x, out=out)
Expand Down
25 changes: 19 additions & 6 deletions ivy/functional/ivy/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,23 @@ def relu(
@handle_array_like_without_promotion
@handle_array_function
def sigmoid(
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 sigmoid function element-wise.

"""
Applies the sigmoid function element-wise.
Parameters
----------
x
input array.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
input broadcast to.
default: None
Returns
-------
Expand All @@ -422,16 +428,23 @@ def sigmoid(
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([-1., 1., 2.])
>>> x = ivy.array([-1.0, 1.0, 2.0])
>>> y = ivy.sigmoid(x)
>>> print(y)
ivy.array([0.269, 0.731, 0.881])
or
>>> x = ivy.array([-1.0, 1.0, 2.0])
>>> y = x.sigmoid()
>>> print(y)
ivy.array([0.269, 0.731, 0.881])
>>> x = ivy.array([-1.3, 3.8, 2.1])
>>> x = ivy.array([[-1.3, 3.8, 2.1], [1.7, 4.2, -6.6]])
>>> y = ivy.sigmoid(x)
>>> print(y)
ivy.array([0.214, 0.978, 0.891])
ivy.array([[0.214, 0.978, 0.891], [0.846,0.985,0.001]] )
"""
return current_backend(x).sigmoid(x, out=out)

Expand Down

0 comments on commit f2bbd95

Please sign in to comment.