Created
February 28, 2017 13:25
-
-
Save diegomrodz/0f7f2ba22300ab42893966ad04a12221 to your computer and use it in GitHub Desktop.
Computing the softmax function given an array of number of n-dimension. And how to plot it and see that all probabilities sum to one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def softmax(x): | |
"""Compute softmax values for each sets of scores in x.""" | |
return np.array([np.exp(e) / np.sum(np.exp(x), axis=0) for e in x]) | |
import matplotlib.pyplot as plt | |
x = np.arange(-2.0, 6.0, 0.1) | |
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)]) | |
plt.plot(x, softmax(scores).T, linewidth=2) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment