softmax | R Documentation |
Efficient implementation (via Fortran) of the softmax (aka multinomial logistic) function converting a set of numerical values to probabilities summing to 1.
softmax(x, v = NULL)
x |
a matrix of dimension |
v |
an optional vector of length |
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
Returns a matrix of the same dimension as x
with values in the range (0,1)
that sum to 1 along the rows.
Luca Scrucca
Blanchard P., Higham D. J., Higham N. J. (2021). Accurately computing the log-sum-exp and softmax functions. IMA Journal of Numerical Analysis, 41/4:2311–2330. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1093/imanum/draa038")}
logsumexp
x = matrix(rnorm(15), 5, 3)
v = log(c(0.5, 0.3, 0.2))
(z = softmax(x, v))
rowSums(z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.