Our FREE site is mostly funded by the Amazon ads ⇓below⇓.
Support us by clicking ⇑above⇑ next time you plan to buy a product on Amazon.
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 Wikipedia article
import numpy as np
Implements the softmax function
vector (np.array,list,tuple): A numpy array of shape (1,n)
consisting of real values or a similar list,tuple
softmax_vec (np.array): The input numpy array after applying
The softmax vector adds up to one. We need to ceil to mitigate for
>>> vec = np.array([5,5])
# 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
if __name__ == "__main__":