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()
In [59]:
a = np.array([1,2,3])
b = np.array([11,12,13])
AB = np.vstack([a,b])
print AB
In [51]:
softmax(AB)
Out[51]:
In [60]:
np.ndim(AB)
Out[60]:
In [61]:
np.exp(AB) / np.sum(np.exp(AB),axis=0)
Out[61]:
In [12]:
A = np.arange(-2,6,1)
A
Out[12]:
In [57]:
np.exp(A)
Out[57]:
In [58]:
np.exp(A) / np.sum(np.exp(A))
Out[58]:
In [ ]: