Skip to content

Add Maths / Sigmoid Function #3880

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

Merged
merged 7 commits into from
Nov 15, 2020
Merged
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions maths/sigmoid.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def sigmoid(vector: float):
def sigmoid(vector: np.array):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xcodz-dot Thank you for your advice. I revised the code and uploaded it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 thanks

"""
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]