Lesson 1



In [21]:
# import the modules
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

Problem 1


Implement the softmax function


In [9]:
x = np.asmatrix([scores,scores])

print x, np.exp(x)/np.sum(np.exp(x),axis=1)


[[ 3.   1.   0.2]
 [ 3.   1.   0.2]] [[ 0.8360188   0.11314284  0.05083836]
 [ 0.8360188   0.11314284  0.05083836]]

In [18]:
def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    x = np.asarray(x)
    return np.exp(x)/np.sum(np.exp(x),axis=0)

In [19]:
# analysis 1
scores = [3.0, 1.0, 0.2]
print(softmax(scores))


[ 0.8360188   0.11314284  0.05083836]

In [23]:
# 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)])

plt.plot(x, softmax(scores).T, linewidth=2)
plt.legend(['x','1','0.2'])
plt.show()



In [ ]: