Stan Math Library  2.15.0
reverse mode automatic differentiation
softmax.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_SOFTMAX_HPP
2 #define STAN_MATH_REV_MAT_FUN_SOFTMAX_HPP
3 
7 #include <stan/math/rev/core.hpp>
8 #include <vector>
9 
10 namespace stan {
11  namespace math {
12 
13  namespace {
14  class softmax_elt_vari : public vari {
15  private:
16  vari** alpha_;
17  const double* softmax_alpha_;
18  const int size_; // array sizes
19  const int idx_; // in in softmax output
20 
21  public:
22  softmax_elt_vari(double val,
23  vari** alpha,
24  const double* softmax_alpha,
25  int size,
26  int idx)
27  : vari(val),
28  alpha_(alpha),
29  softmax_alpha_(softmax_alpha),
30  size_(size),
31  idx_(idx) {
32  }
33  void chain() {
34  for (int m = 0; m < size_; ++m) {
35  if (m == idx_) {
36  alpha_[m]->adj_
37  += adj_ * softmax_alpha_[idx_] * (1 - softmax_alpha_[m]);
38  } else {
39  alpha_[m]->adj_
40  -= adj_ * softmax_alpha_[idx_] * softmax_alpha_[m];
41  }
42  }
43  }
44  };
45  }
46 
57  inline Eigen::Matrix<var, Eigen::Dynamic, 1>
58  softmax(const Eigen::Matrix<var, Eigen::Dynamic, 1>& alpha) {
59  using Eigen::Matrix;
60  using Eigen::Dynamic;
61 
62  check_nonzero_size("softmax", "alpha", alpha);
63 
64  vari** alpha_vi_array
65  = reinterpret_cast<vari**>(ChainableStack::memalloc_
66  .alloc(sizeof(vari*) * alpha.size()));
67  for (int i = 0; i < alpha.size(); ++i)
68  alpha_vi_array[i] = alpha(i).vi_;
69 
70  Matrix<double, Dynamic, 1> alpha_d(alpha.size());
71  for (int i = 0; i < alpha_d.size(); ++i)
72  alpha_d(i) = alpha(i).val();
73 
74  Matrix<double, Dynamic, 1> softmax_alpha_d
75  = softmax(alpha_d);
76 
77  double* softmax_alpha_d_array
78  = reinterpret_cast<double*>(ChainableStack::memalloc_
79  .alloc(sizeof(double) * alpha_d.size()));
80  for (int i = 0; i < alpha_d.size(); ++i)
81  softmax_alpha_d_array[i] = softmax_alpha_d(i);
82 
83  Matrix<var, Dynamic, 1> softmax_alpha(alpha.size());
84  for (int k = 0; k < softmax_alpha.size(); ++k)
85  softmax_alpha(k) = var(new softmax_elt_vari(softmax_alpha_d[k],
86  alpha_vi_array,
87  softmax_alpha_d_array,
88  alpha.size(),
89  k));
90  return softmax_alpha;
91  }
92 
93  }
94 }
95 #endif
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.
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:14
The variable implementation base class.
Definition: vari.hpp:30
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
size_t size_
Definition: dot_self.hpp:18
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...

     [ Stan Home Page ] © 2011–2016, Stan Development Team.