From d5f02bc9250eec91162b9c88798f374b729e2512 Mon Sep 17 00:00:00 2001 From: sukyung99 Date: Thu, 12 Nov 2020 02:26:30 +0900 Subject: [PATCH 1/7] Add Maths / Sigmoid Function --- maths/sigmoid.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 maths/sigmoid.py diff --git a/maths/sigmoid.py b/maths/sigmoid.py new file mode 100644 index 000000000000..ffecac22f2b4 --- /dev/null +++ b/maths/sigmoid.py @@ -0,0 +1,38 @@ +""" +This script demonstrates the implementation of the Sigmoid function. + +The function takes a vector of K real numbers as input and then 1 / (1 + exp(-x)). +After through Sigmoid, the element of the vector mostly 0 between 1. or 1 between -1. + +Script inspired from its corresponding Wikipedia article +https://en.wikipedia.org/wiki/Sigmoid_function +""" + +import numpy as np + + +def sigmoid(vector: float): + """ + Implements the sigmoid function + + Parameters: + vector (np.array): A numpy array of shape (1,n) + consisting of real values + + + Returns: + sigmoid_vec (np.array): The input numpy array, after applying + sigmoid. + + >>> vec = np.array([-1.0, 1.0, 2.0]) + >>> sigmoid(vec) + array([0.26894142, 0.73105858, 0.88079708]) + """ + + return 1 / (1 + np.exp(-vector)) + + +if __name__ == "__main__": + print( + sigmoid(np.array([-1.0, 1.0, 2.0])) + ) # --> [0.26894142, 0.73105858, 0.88079708] From 24b6ea95028be19873a7bcaa4f931b9b68cc2e9f Mon Sep 17 00:00:00 2001 From: sukyung99 Date: Fri, 13 Nov 2020 20:20:45 +0900 Subject: [PATCH 2/7] Update Sigmoid Function --- maths/sigmoid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index ffecac22f2b4..654f0d0fb342 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -11,7 +11,7 @@ import numpy as np -def sigmoid(vector: float): +def sigmoid(vector: np.array): """ Implements the sigmoid function From 6572b057049c27b0414e74063a17eb611b43504d Mon Sep 17 00:00:00 2001 From: sukyung99 Date: Sat, 14 Nov 2020 01:03:50 +0900 Subject: [PATCH 3/7] Add doctest and type hints --- maths/sigmoid.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index 654f0d0fb342..f6e81ec3c93b 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -11,7 +11,7 @@ import numpy as np -def sigmoid(vector: np.array): +def sigmoid(vector: np.array) -> np.array: """ Implements the sigmoid function @@ -24,15 +24,19 @@ def sigmoid(vector: np.array): sigmoid_vec (np.array): The input numpy array, after applying sigmoid. - >>> vec = np.array([-1.0, 1.0, 2.0]) - >>> sigmoid(vec) + + Examples: + >>> sigmoid(np.array([-1.0, 1.0, 2.0])) array([0.26894142, 0.73105858, 0.88079708]) + + >>> sigmoid(np.array([0.0])) + array([0.5]) """ return 1 / (1 + np.exp(-vector)) if __name__ == "__main__": - print( - sigmoid(np.array([-1.0, 1.0, 2.0])) - ) # --> [0.26894142, 0.73105858, 0.88079708] + import doctest + + doctest.testmod() From bb7b64025993c35a60c76c35986b5a6d5c754d5e Mon Sep 17 00:00:00 2001 From: sukyung99 Date: Sat, 14 Nov 2020 01:18:07 +0900 Subject: [PATCH 4/7] Fix Trim Trailing Whitespace --- maths/sigmoid.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index f6e81ec3c93b..b07e95a757a5 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -19,12 +19,10 @@ def sigmoid(vector: np.array) -> np.array: vector (np.array): A numpy array of shape (1,n) consisting of real values - Returns: sigmoid_vec (np.array): The input numpy array, after applying sigmoid. - Examples: >>> sigmoid(np.array([-1.0, 1.0, 2.0])) array([0.26894142, 0.73105858, 0.88079708]) From bfdcabe3e969a7954563911537a0d164bf655efe Mon Sep 17 00:00:00 2001 From: sukyung99 Date: Sat, 14 Nov 2020 14:27:08 +0900 Subject: [PATCH 5/7] Fix Error --- maths/sigmoid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index b07e95a757a5..68492f14ecaf 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -36,5 +36,4 @@ def sigmoid(vector: np.array) -> np.array: if __name__ == "__main__": import doctest - doctest.testmod() From d2455e4db2c821ef866dcadb62f92bf95bf6df8e Mon Sep 17 00:00:00 2001 From: sukyung99 Date: Sat, 14 Nov 2020 14:39:34 +0900 Subject: [PATCH 6/7] Modified Black --- maths/sigmoid.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index 68492f14ecaf..1bb19540891b 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -36,4 +36,5 @@ def sigmoid(vector: np.array) -> np.array: if __name__ == "__main__": import doctest + doctest.testmod() From 5365237cb8575e72ba7e4dc8b531507f7d2f34cd Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 14 Nov 2020 09:18:25 +0100 Subject: [PATCH 7/7] Update sigmoid.py --- maths/sigmoid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index 1bb19540891b..147588e8871f 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -30,7 +30,6 @@ def sigmoid(vector: np.array) -> np.array: >>> sigmoid(np.array([0.0])) array([0.5]) """ - return 1 / (1 + np.exp(-vector))