1#ifndef STAN_MATH_REV_FUN_LOG_SOFTMAX_HPP
2#define STAN_MATH_REV_FUN_LOG_SOFTMAX_HPP
37 for (
int m = 0; m <
size_; ++m) {
56template <
typename T, require_eigen_st<is_var, T>* =
nullptr>
58 const int a_size = x.size();
62 const auto& x_ref =
to_ref(x);
66 Eigen::Map<vector_vi>(x_vi_array, a_size) = x_ref.vi();
73 vector_d diff = (x_d.array() - x_d.maxCoeff());
74 vector_d softmax_x_d = diff.array().exp();
75 double sum = softmax_x_d.sum();
76 vector_d log_softmax_x_d = diff.array() - std::log(
sum);
79 double* softmax_x_d_array
81 Eigen::Map<vector_d>(softmax_x_d_array, a_size) = softmax_x_d.array() /
sum;
84 for (
int k = 0; k < a_size; ++k) {
85 log_softmax_x(k) =
var(
new internal::log_softmax_elt_vari(
86 log_softmax_x_d[k], x_vi_array, softmax_x_d_array, a_size, k));
99template <
typename T, require_var_matrix_t<T>* =
nullptr>
103 const auto& theta = (x.val().array() - x.val().maxCoeff()).
eval();
106 (theta.array() -
log(theta.exp().sum())).matrix(),
107 [x](
const auto& res)
mutable {
109 += res.adj() - (res.adj().sum() * res.val().array().exp()).matrix();
122template <
typename T, require_std_vector_st<is_var, T>* =
nullptr>
124 return apply_vector_unary<T>::apply(
125 x, [](
const auto& alpha) {
return log_softmax(alpha); });
const double * softmax_alpha_
log_softmax_elt_vari(double val, vari **alpha, const double *softmax_alpha, int size, int idx)
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Type for (column) vector of double values.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
fvar< T > log(const fvar< T > &x)
vari_value< double > vari
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
auto sum(const std::vector< 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.
auto log_softmax(const T &x)
Return the log softmax of the specified vector or container of vectors.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
static thread_local AutodiffStackStorage * instance_