A generalization of the logistic function that takes a K-dimensional vector of arbitrary values and converts it to a K-dimensional vector of real values in the range (0,1) that sum to 1. The function is also known as the normalized exponential.

softmax(x)

Arguments

x

A vector of values from -Inf to Inf.

Value

A vector of values from 0 to 1 that sum to 1.

Details

The function can take either a vector or a matrix of values. If a matrix is passed in, the function is applied to each row of the matrix.

Examples

set.seed(3902)
ex <- softmax(rnorm(5))
sum(ex) # Should equal 1
#> [1] 1
mat <- matrix(rnorm(9), 3, 3)
ex <- softmax(mat)
rowSums(ex) # Each row should sum to 1
#> [1] 1 1 1