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)
| 983 | |
| 984 | |
| 985 | def 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 | |
| 1027 | def make_nonnegative(X, min_value=0): |
searching dependent graphs…