1 #ifndef STAN_MATH_REV_MAT_FUN_LOG_SOFTMAX_HPP 2 #define STAN_MATH_REV_MAT_FUN_LOG_SOFTMAX_HPP 21 const double* softmax_alpha_;
30 softmax_alpha_(softmax_alpha),
34 for (
int m = 0; m < size_; ++m) {
36 alpha_[m]->
adj_ +=
adj_ * (1 - softmax_alpha_[m]);
38 alpha_[m]->
adj_ -=
adj_ * softmax_alpha_[m];
56 const Eigen::Matrix<var, Eigen::Dynamic, 1>& alpha) {
63 vari** alpha_vi_array =
reinterpret_cast<vari**
>(
64 vari::operator
new(
sizeof(
vari*) * alpha.size()));
65 for (
int i = 0; i < alpha.size(); ++i)
66 alpha_vi_array[i] = alpha(i).vi_;
68 Matrix<double, Dynamic, 1> alpha_d(alpha.size());
69 for (
int i = 0; i < alpha_d.size(); ++i)
70 alpha_d(i) = alpha(i).val();
75 Matrix<double, Dynamic, 1> softmax_alpha_d(alpha_d.size());
76 Matrix<double, Dynamic, 1> log_softmax_alpha_d(alpha_d.size());
78 double max_v = alpha_d.maxCoeff();
81 for (
int i = 0; i < alpha_d.size(); ++i) {
82 softmax_alpha_d(i) =
std::exp(alpha_d(i) - max_v);
83 sum += softmax_alpha_d(i);
86 for (
int i = 0; i < alpha_d.size(); ++i)
87 softmax_alpha_d(i) /=
sum;
90 for (
int i = 0; i < alpha_d.size(); ++i)
91 log_softmax_alpha_d(i) = (alpha_d(i) - max_v) - log_sum;
95 double* softmax_alpha_d_array =
reinterpret_cast<double*
>(
96 vari::operator
new(
sizeof(double) * alpha_d.size()));
98 for (
int i = 0; i < alpha_d.size(); ++i)
99 softmax_alpha_d_array[i] = softmax_alpha_d(i);
101 Matrix<var, Dynamic, 1> log_softmax_alpha(alpha.size());
102 for (
int k = 0; k < log_softmax_alpha.size(); ++k)
104 log_softmax_alpha_d[k], alpha_vi_array, softmax_alpha_d_array,
106 return log_softmax_alpha;
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
fvar< T > log(const fvar< T > &x)
The variable implementation base class.
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > log_softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
fvar< T > exp(const fvar< T > &x)
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
log_softmax_elt_vari(double val, vari **alpha, const double *softmax_alpha, int size, int idx)