{ } Raw JSON

bundles / scipy latest / scipy / special / _logsumexp / softmax

function

scipy.special._logsumexp:softmax

source: /scipy/special/_logsumexp.py :251

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_like

Input array.

axis : int or tuple of ints, optional

Axis to compute values along. Default is None and softmax will be computed over the entire array x.

Returns

s : ndarray

An 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-arrayapi for 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]])
Compute the softmax transformation over the entire array.
m = softmax(x)
m
m.sum()
Compute the softmax transformation along the first axis (i.e., the columns).
m = softmax(x, axis=0)
m
m.sum(axis=0)
Compute the softmax transformation along the second axis (i.e., the rows).
m = softmax(x, axis=1)
m
m.sum(axis=1)

Aliases

  • scipy.special.softmax

Referenced by

This package