Automatic Differentiation
 
Loading...
Searching...
No Matches
log_softmax.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_LOG_SOFTMAX_HPP
2#define STAN_MATH_PRIM_FUN_LOG_SOFTMAX_HPP
3
10
11namespace stan {
12namespace math {
13
44template <typename Container, require_st_arithmetic<Container>* = nullptr,
45 require_container_t<Container>* = nullptr,
46 require_not_t<bool_constant<
47 is_eigen<std::decay_t<Container>>::value
48 && !is_eigen_vector<std::decay_t<Container>>::value>>* = nullptr>
49inline auto log_softmax(Container&& x) {
50 check_nonzero_size("log_softmax", "x", x);
51 return make_holder(
52 [](auto&& a) {
54 std::forward<decltype(a)>(a),
55 [](auto&& v) { return v.array() - log_sum_exp(v); });
56 },
57 to_ref(std::forward<Container>(x)));
58}
59
60} // namespace math
61} // namespace stan
62#endif
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:437
auto log_softmax(T &&x)
Return the log softmax of each vector in a container of fvar values.
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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:51
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...