Softmax function
softmax.Rd
Efficient implementation (via Fortran) of the softmax (aka multinomial logistic) function converting a set of numerical values to probabilities summing to 1.
Arguments
- x
a matrix of dimension \(n \times k\) of numerical values. If a vector is provided, it is converted to a single-row matrix.
- v
an optional vector of length \(k\) of numerical values to be added to each row of
x
matrix. If not provided, a vector of zeros is used.
Details
Given the matrix x
, for each row \(x_{[i]} = [x_1, \dots, x_k]\) (with \(i=1,\dots,n\)), the softmax function calculates
$$
\text{softmax}(x_{[i]})_j =
\dfrac{\exp{x_j + v_j}}{\sum_{l=1}^k \exp(x_l + v_l)}
\qquad \text{for } j = 1,\dots,k
$$
Value
Returns a matrix of the same dimension as x
with values in the range \((0,1)\) that sum to 1 along the rows.
Examples
x = matrix(rnorm(15), 5, 3)
v = log(c(0.5, 0.3, 0.2))
(z = softmax(x, v))
#> [,1] [,2] [,3]
#> [1,] 0.4039612 0.04453269 0.55150615
#> [2,] 0.5363225 0.34888841 0.11478912
#> [3,] 0.7237697 0.18876193 0.08746842
#> [4,] 0.4567840 0.52021530 0.02300074
#> [5,] 0.3457171 0.24608575 0.40819713
rowSums(z)
#> [1] 1 1 1 1 1