This is a pre-release version (latest).Go to latest (1.18.0)
{ } Raw JSON

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

function

scipy.special._logsumexp:softmax

source: build-install/usr/lib/python3.14/site-packages/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.

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
✗
array([[0.     , 0.     , 0.     , 0.00003],
       [0.     , 0.     , 0.00181, 0.00003],
       [0.00001, 0.26842, 0.72964, 0.00003]])
m.sum()
✗
np.float64(1.0)
Compute the softmax transformation along the first axis (i.e., the columns).
m = softmax(x, axis=0)
✓
m
✗
array([[0.21194, 0.00001, 0.     , 0.33333],
       [0.21194, 0.     , 0.00247, 0.33333],
       [0.57612, 0.99999, 0.99752, 0.33333]])
m.sum(axis=0)
✗
array([1., 1., 1., 1.])
Compute the softmax transformation along the second axis (i.e., the rows).
m = softmax(x, axis=1)
✓
m
✗
array([[0.10588, 0.06422, 0.04757, 0.78233],
       [0.00243, 0.00033, 0.97931, 0.01794],
       [0.00001, 0.26893, 0.73103, 0.00003]])
m.sum(axis=1)
✗
array([1., 1., 1.])

Aliases

  • scipy.special.softmax

Referenced by