Automatic Differentiation
 
Loading...
Searching...
No Matches
softmax.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_SOFTMAX_HPP
2#define STAN_MATH_PRIM_FUN_SOFTMAX_HPP
3
9#include <utility>
10
11namespace stan {
12namespace math {
13
48template <typename Container, require_st_arithmetic<Container>* = nullptr,
49 require_container_t<Container>* = nullptr,
50 require_not_t<bool_constant<
51 is_eigen<std::decay_t<Container>>::value
52 && !is_eigen_vector<std::decay_t<Container>>::value>>* = nullptr>
53inline auto softmax(Container&& x) {
54 return make_holder(
55 [](auto&& a) {
57 std::forward<decltype(a)>(a),
58 [](auto&& v) -> plain_type_t<decltype(v)> {
59 if (v.size() == 0) {
60 return std::forward<decltype(v)>(v);
61 }
62 const auto theta = (v.array() - v.maxCoeff()).exp();
63 return (theta / theta.sum()).matrix();
64 });
65 },
66 to_ref(std::forward<Container>(x)));
67}
68
69} // namespace math
70} // namespace stan
71#endif
auto softmax(T &&x)
Return the softmax of each vector in a container of fvar values.
Definition softmax.hpp:23
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:488
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
typename plain_type< std::decay_t< T > >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...