logsumexp | R Documentation |
Efficient implementation (via Rcpp) of log-sum-exp and softmax functions.
logsumexp(x, v = NULL) softmax(x, v = NULL)
x |
a matrix of dimension (n x k). |
v |
an optional vector of length k. If not provided a vector of zeros is used. |
Given the matrix x logsumexp()
calculates for each row
x[i,] the log-sum-exp function computed as
m[i] + log(sum(exp(x[i,] + v) - m[i]))
where m[i] = max(x[i,] + v) .
softmax()
calculates for each row x[i,] the softmax (aka
multinomial logistic) function
exp( x_[i,c] + v_[c]) / (sum_{j=1}^k exp( x_[i,j] + v_[j]) )
logsumexp()
returns a vector of values of length equal to the number of rows of x.
softmax()
returns a matrix of values of the same dimension as x.
Luca Scrucca
x = matrix(rnorm(15), 5, 3) v = log(c(0.5, 0.3, 0.2)) logsumexp(x, v) (z = softmax(x, v)) rowSums(z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.