Softmax for a vector x is defined as
Value
softmax
returns the softmax of x
; rowwise if x
is a matrix.
dsoftmax
returns the Jacobi-matrix of softmax(x)
at x
. x
must be a vector.
Details
\(s_i = \exp(x_i) / \sum_k \exp(x_k)\)
It satisfies sum(s) == 1.0
and can be used to smoothly enforce a sum
constraint.
Examples
softmax(c(5, 5))
#> [1] 0.5 0.5
softmax(diag(nrow = 5, ncol = 6))
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 0.3521874 0.1295625 0.1295625 0.1295625 0.1295625 0.1295625
#> [2,] 0.1295625 0.3521874 0.1295625 0.1295625 0.1295625 0.1295625
#> [3,] 0.1295625 0.1295625 0.3521874 0.1295625 0.1295625 0.1295625
#> [4,] 0.1295625 0.1295625 0.1295625 0.3521874 0.1295625 0.1295625
#> [5,] 0.1295625 0.1295625 0.1295625 0.1295625 0.3521874 0.1295625