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 {
22 softmax_elt_vari(
double val,
24 const double* softmax_alpha,
34 for (
int m = 0; m <
size_; ++m) {
58 inline Eigen::Matrix<var, Eigen::Dynamic, 1>
59 softmax(
const Eigen::Matrix<var, Eigen::Dynamic, 1>& alpha) {
68 for (
int i = 0; i < alpha.size(); ++i)
69 alpha_vi_array[i] = alpha(i).vi_;
71 Matrix<double, Dynamic, 1> alpha_d(alpha.size());
72 for (
int i = 0; i < alpha_d.size(); ++i)
73 alpha_d(i) = alpha(i).val();
75 Matrix<double, Dynamic, 1> softmax_alpha_d
78 double* softmax_alpha_d_array
80 .
alloc(
sizeof(
double) * alpha_d.size()));
81 for (
int i = 0; i < alpha_d.size(); ++i)
82 softmax_alpha_d_array[i] = softmax_alpha_d(i);
84 Matrix<var, Dynamic, 1> softmax_alpha(alpha.size());
85 for (
int k = 0; k < softmax_alpha.size(); ++k)
86 softmax_alpha(k) =
var(
new softmax_elt_vari(softmax_alpha_d[k],
88 softmax_alpha_d_array,
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.
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
const double * softmax_alpha_
int size(const std::vector< T > &x)
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...