bundles / scipy 1.17.1 / scipy / special / _logsumexp / softmax
function
scipy.special._logsumexp:softmax
Signature
def softmax ( x , axis = None ) Summary
Compute the softmax function.
Extended Summary
The softmax function transforms each element of a collection by computing the exponential of each element divided by the sum of the exponentials of all the elements. That is, if x is a one-dimensional numpy array
softmax(x) = np.exp(x)/sum(np.exp(x))Parameters
x: array_likeInput array.
axis: int or tuple of ints, optionalAxis to compute values along. Default is None and softmax will be computed over the entire array
x.
Returns
s: ndarrayAn array the same shape as
x. The result will sum to 1 along the specified axis.
Notes
The formula for the softmax function for a vector is
The softmax function is the gradient of logsumexp.
The implementation uses shifting to avoid overflow. See [1] for more details.
Array API Standard Support
softmax has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ✅ JAX ✅ ✅ Dask ✅ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
import numpy as np from scipy.special import softmax np.set_printoptions(precision=5)✓
x = np.array([[1, 0.5, 0.2, 3], [1, -1, 7, 3], [2, 12, 13, 3]])✓
m = softmax(x)
✓m
✗m.sum()
✗m = softmax(x, axis=0)
✓m
✗m.sum(axis=0)
✗m = softmax(x, axis=1)
✓m
✗m.sum(axis=1)
✗Aliases
-
scipy.special.softmax