Skip to main content

softmax

Function softmax 

Source
pub fn softmax(xs: &[f64]) -> Vec<f64>
Expand description

Compute the softmax function

Pure Rust translation of scipy.special.softmax.

§Definition

The formula for the softmax function $\sigma(\bm{x})$ for a vector $\bm{x} = \begin{bmatrix} x_0 & x_1 & \cdots & x_{n-1} \end{bmatrix}$ is

$$ \sigma(\bm{x})_j = {e^{x_j} \over \sum_k e^{x_k}} $$

The softmax function is the gradient of logsumexp.

The implementation uses shifting to improve numerical stability.

§See also