In [2]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

In [3]:
"""Softmax."""

scores = [3.0, 1.0, 0.2]

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    #pass  # TODO: Compute and return softmax(x)
    #if np.ndim(x) == 1:
    return np.exp(x) / np.sum(np.exp(x),axis=0)
    #elif np.ndim(x) == 2:
        #return np.exp(x) / np.reshape(np.sum(np.exp(x),axis=0),(x.shape[0],1))

print(softmax(scores))

# Plot softmax curves

x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])
#print x.shape
#print softmax(scores).T.shape
#print scores.shape
plt.plot(x, softmax(scores).T, linewidth=2)
plt.show()


[ 0.8360188   0.11314284  0.05083836]

In [59]:
a = np.array([1,2,3])
b = np.array([11,12,13])
AB = np.vstack([a,b])
print AB


[[ 1  2  3]
 [11 12 13]]

In [51]:
softmax(AB)


Out[51]:
array([[ 0.09003057,  0.24472847,  0.66524096],
       [ 0.09003057,  0.24472847,  0.66524096]])

In [60]:
np.ndim(AB)


Out[60]:
2

In [61]:
np.exp(AB) / np.sum(np.exp(AB),axis=0)


Out[61]:
array([[  4.53978687e-05,   4.53978687e-05,   4.53978687e-05],
       [  9.99954602e-01,   9.99954602e-01,   9.99954602e-01]])

In [12]:
A = np.arange(-2,6,1)
A


Out[12]:
array([-2, -1,  0,  1,  2,  3,  4,  5])

In [57]:
np.exp(A)


Out[57]:
array([ 20.08553692,   2.71828183,   1.22140276])

In [58]:
np.exp(A) / np.sum(np.exp(A))


Out[58]:
array([ 0.8360188 ,  0.11314284,  0.05083836])

In [ ]: