MCPcopy Index your code
hub / github.com/scikit-learn/scikit-learn / softmax

Function softmax

sklearn/utils/extmath.py:985–1024  ·  view source on GitHub ↗

Calculate the softmax function. The softmax function is calculated by np.exp(X) / np.sum(np.exp(X), axis=1) This will cause overflow when large values are exponentiated. Hence the largest value in each row is subtracted from each data point to prevent this. Parameters

(X, copy=True)

Source from the content-addressed store, hash-verified

983
984
985def softmax(X, copy=True):
986 """
987 Calculate the softmax function.
988
989 The softmax function is calculated by
990 np.exp(X) / np.sum(np.exp(X), axis=1)
991
992 This will cause overflow when large values are exponentiated.
993 Hence the largest value in each row is subtracted from each data
994 point to prevent this.
995
996 Parameters
997 ----------
998 X : array-like of float of shape (M, N)
999 Argument to the logistic function.
1000
1001 copy : bool, default=True
1002 Copy X or not.
1003
1004 Returns
1005 -------
1006 out : ndarray of shape (M, N)
1007 Softmax function evaluated at every point in x.
1008 """
1009 xp, is_array_api_compliant = get_namespace(X)
1010 if copy:
1011 X = xp.asarray(X, copy=True)
1012 max_prob = xp.reshape(xp.max(X, axis=1), (-1, 1))
1013 X -= max_prob
1014
1015 if _is_numpy_namespace(xp):
1016 # optimization for NumPy arrays
1017 np.exp(X, out=np.asarray(X))
1018 else:
1019 # array_api does not have `out=`
1020 X = xp.exp(X)
1021
1022 sum_prob = xp.reshape(xp.sum(X, axis=1), (-1, 1))
1023 X /= sum_prob
1024 return X
1025
1026
1027def make_nonnegative(X, min_value=0):

Callers 11

predictMethod · 0.90
predict_probaMethod · 0.90
_loss_grad_lbfgsMethod · 0.90
test_softmaxFunction · 0.90
predict_probaMethod · 0.90
_compute_gradientMethod · 0.90
inverseMethod · 0.90
_predict_probaFunction · 0.90

Calls 3

get_namespaceFunction · 0.90
_is_numpy_namespaceFunction · 0.90
maxMethod · 0.80

Tested by 3

test_softmaxFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…