In [2]:
%matplotlib inline
cf. Quiz: Softmax
In [1]:
"""Softmax"""
Out[1]:
In [3]:
scores = [3.0, 1.0, 0.2]
In [4]:
import numpy as np
In [5]:
def softmax(x):
"""Compute softmax values for x."""
y = np.exp(x)/np.sum(np.exp(x), axis=0)
return y
In [6]:
print(softmax(scores))
In [7]:
# Plot softmax curves
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)])
In [8]:
plt.plot(x, softmax(scores).T, linewidth=2)
Out[8]:
In [9]:
plt.show()
In [13]:
# Multiply the scores by 10. What happens?
scores = np.array([3.0, 1.0, 0.2])
print(softmax(scores* 10))
In [14]:
# Divide the scores by 10. What happens?
scores = np.array([3.0, 1.0, 0.2])
print(softmax(scores/ 10))
EY : 20170126 Numerical Stability!!!
cf. Quiz: Numerical Stability
In [15]:
N = 1000000000
a = 0.000001
ITER_MAX = 1000000
In [16]:
for iter in range(ITER_MAX):
N += a
In [17]:
print( N - 1000000000 )
In [18]:
from decimal import Decimal
In [19]:
N_Decimal = Decimal(N)
a_Decimal = Decimal(a)
for iter in range(ITER_MAX):
N_Decimal += a_Decimal
In [20]:
print( N_Decimal - Decimal( N))
In [ ]: