Skip to content

Instantly share code, notes, and snippets.

@diegomrodz
Created February 28, 2017 13:25
Show Gist options
  • Save diegomrodz/0f7f2ba22300ab42893966ad04a12221 to your computer and use it in GitHub Desktop.
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.
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