import numpy as np
from numpy.core._rational_tests import denominator
def softmax(x):
#implementation one
""" Compute the softmax in a numerically stable way."""
x = x- np.max(x)
exp_x = np.exp(x)
softmax_x = exp_x / np.sum(exp_x)
return softmax_x
def softmax_2(x):
#implementation 2
"""
Compute the softmax function for each row in matrix or vectors
Arguemnts:
"""
orig_shape = x.shape
if len(x.shape) >1:
# Matrix
exp_minmax = lambda x: np.exp(x-np.max)
demon = lambda x: 1.0 / np.sum(x)
x = np.apply_along_axis(exp_minmax, 1, x)
denominator = np.apply_along_axis(demon,1, x)
if len( denominator.shape) == 1:
denominator = denominator.reshape((denominator.shape[0], 1) )
x = x*denominator
else:
#Vector
x_max = np.max(x)
x = x - x_max
numerator = np.exp(x)
denominator = 1.0 / np.sum(numerator)
x= numerator.dot(denominator)
assert x.shape == orig_shape
return x