1 #ifndef STAN_MATH_REV_MAT_FUN_SOFTMAX_HPP 2 #define STAN_MATH_REV_MAT_FUN_SOFTMAX_HPP 14 class softmax_elt_vari :
public vari {
17 const double* softmax_alpha_;
22 softmax_elt_vari(
double val,
24 const double* softmax_alpha,
29 softmax_alpha_(softmax_alpha),
34 for (
int m = 0; m <
size_; ++m) {
37 += adj_ * softmax_alpha_[idx_] * (1 - softmax_alpha_[m]);
40 -= adj_ * softmax_alpha_[idx_] * softmax_alpha_[m];
57 inline Eigen::Matrix<var, Eigen::Dynamic, 1>
58 softmax(
const Eigen::Matrix<var, Eigen::Dynamic, 1>& alpha) {
67 for (
int i = 0; i < alpha.size(); ++i)
68 alpha_vi_array[i] = alpha(i).vi_;
70 Matrix<double, Dynamic, 1> alpha_d(alpha.size());
71 for (
int i = 0; i < alpha_d.size(); ++i)
72 alpha_d(i) = alpha(i).val();
74 Matrix<double, Dynamic, 1> softmax_alpha_d
77 double* softmax_alpha_d_array
79 .
alloc(
sizeof(
double) * alpha_d.size()));
80 for (
int i = 0; i < alpha_d.size(); ++i)
81 softmax_alpha_d_array[i] = softmax_alpha_d(i);
83 Matrix<var, Dynamic, 1> softmax_alpha(alpha.size());
84 for (
int k = 0; k < softmax_alpha.size(); ++k)
85 softmax_alpha(k) =
var(
new softmax_elt_vari(softmax_alpha_d[k],
87 softmax_alpha_d_array,
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.
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
static stack_alloc memalloc_
The variable implementation base class.
Independent (input) and dependent (output) variables for gradients.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...