1 #ifndef STAN__AGRAD__REV__MATRIX__SOFTMAX_HPP
2 #define STAN__AGRAD__REV__MATRIX__SOFTMAX_HPP
14 class softmax_elt_vari :
public vari {
21 softmax_elt_vari(
double val,
23 const double* softmax_alpha,
33 for (
int m = 0; m <
size_; ++m) {
57 inline Eigen::Matrix<var,Eigen::Dynamic,1>
58 softmax(
const Eigen::Matrix<var,Eigen::Dynamic,1>& alpha) {
66 for (
int i = 0; i < alpha.size(); ++i)
67 alpha_vi_array[i] = alpha(i).vi_;
69 Matrix<double,Dynamic,1> alpha_d(alpha.size());
70 for (
int i = 0; i < alpha_d.size(); ++i)
71 alpha_d(i) = alpha(i).val();
73 Matrix<double,Dynamic,1> softmax_alpha_d
76 double* softmax_alpha_d_array
78 for (
int i = 0; i < alpha_d.size(); ++i)
79 softmax_alpha_d_array[i] = softmax_alpha_d(i);
81 Matrix<var,Dynamic,1> softmax_alpha(alpha.size());
82 for (
int k = 0; k < softmax_alpha.size(); ++k)
83 softmax_alpha(k) =
var(
new softmax_elt_vari(softmax_alpha_d[k],
85 softmax_alpha_d_array,
const double * softmax_alpha_
Eigen::Matrix< T, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v)
Return the softmax of the specified vector.
memory::stack_alloc memalloc_
The variable implementation base class.
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Independent (input) and dependent (output) variables for gradients.
int size(const std::vector< T > &x)
bool check_nonzero_size(const char *function, const T_y &y, const char *name, T_result *result)
Return true if the specified matrix/vector is of non-zero size.