
Softmax cross-entropy gradient (morie.fn grxeng)
Source:R/geron_train_native.R
morie_geron_softmax_cost_gradient.Rdgrad = (1/m) X^T (P_hat - Y); its columns sum to zero.
Examples
set.seed(7)
X <- cbind(1, rnorm(20))
Y <- diag(3)[sample(1:3, 20, replace = TRUE), ]
theta <- matrix(0, 2, 3)
morie_geron_softmax_cost_gradient(X, Y, theta)
#> $gradient
#> [,1] [,2] [,3]
#> [1,] -1.166667e-01 0.08333333 0.03333333
#> [2,] 8.146778e-05 0.09308831 -0.09316977
#>
#> $probabilities
#> [,1] [,2] [,3]
#> [1,] 0.3333333 0.3333333 0.3333333
#> [2,] 0.3333333 0.3333333 0.3333333
#> [3,] 0.3333333 0.3333333 0.3333333
#> [4,] 0.3333333 0.3333333 0.3333333
#> [5,] 0.3333333 0.3333333 0.3333333
#> [6,] 0.3333333 0.3333333 0.3333333
#> [7,] 0.3333333 0.3333333 0.3333333
#> [8,] 0.3333333 0.3333333 0.3333333
#> [9,] 0.3333333 0.3333333 0.3333333
#> [10,] 0.3333333 0.3333333 0.3333333
#> [11,] 0.3333333 0.3333333 0.3333333
#> [12,] 0.3333333 0.3333333 0.3333333
#> [13,] 0.3333333 0.3333333 0.3333333
#> [14,] 0.3333333 0.3333333 0.3333333
#> [15,] 0.3333333 0.3333333 0.3333333
#> [16,] 0.3333333 0.3333333 0.3333333
#> [17,] 0.3333333 0.3333333 0.3333333
#> [18,] 0.3333333 0.3333333 0.3333333
#> [19,] 0.3333333 0.3333333 0.3333333
#> [20,] 0.3333333 0.3333333 0.3333333
#>
#> $gradient_norm
#> [1] 0.1975164
#>
#> $estimate
#> [,1] [,2] [,3]
#> [1,] -1.166667e-01 0.08333333 0.03333333
#> [2,] 8.146778e-05 0.09308831 -0.09316977
#>
#> $n
#> [1] 20
#>