Skip to content

adding softmax function #1267

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 2 commits into from
Oct 3, 2019
Merged
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
56 changes: 56 additions & 0 deletions maths/softmax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
This script demonstrates the implementation of the Softmax function.

Its a function that takes as input a vector of K real numbers, and normalizes
it into a probability distribution consisting of K probabilities proportional
to the exponentials of the input numbers. After softmax, the elements of the
vector always sum up to 1.

Script inspired from its corresponding Uncyclopedia article
https://en.wikipedia.org/wiki/Softmax_function
"""

import numpy as np


def softmax(vector):
"""
Implements the softmax function

Parameters:
vector (np.array,list,tuple): A numpy array of shape (1,n)
consisting of real values or a similar list,tuple


Returns:
softmax_vec (np.array): The input numpy array after applying
softmax.

The softmax vector adds up to one. We need to ceil to mitigate for
precision
>>> np.ceil(np.sum(softmax([1,2,3,4])))
1.0

>>> vec = np.array([5,5])
>>> softmax(vec)
array([0.5, 0.5])

>>> softmax([0])
array([1.])
"""

# Calculate e^x for each x in your vector where e is Euler's
# number (approximately 2.718)
exponentVector = np.exp(vector)

# Add up the all the exponentials
sumOfExponents = np.sum(exponentVector)

# Divide every exponent by the sum of all exponents
softmax_vector = exponentVector / sumOfExponents

return softmax_vector


if __name__ == "__main__":
print(softmax((0,)))