All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
softmax.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_MAT_FUN_SOFTMAX_HPP
#define STAN_MATH_REV_MAT_FUN_SOFTMAX_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/fun/softmax.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/prim/scal/err/check_nonzero_size.hpp>
#include <vector>
namespace stan {
namespace math {
namespace {
class softmax_elt_vari : public vari {
private:
vari** alpha_;
const double* softmax_alpha_;
const int size_; // array sizes
const int idx_; // in in softmax output
public:
softmax_elt_vari(double val,
vari** alpha,
const double* softmax_alpha,
int size,
int idx)
: vari(val),
alpha_(alpha),
softmax_alpha_(softmax_alpha),
size_(size),
idx_(idx) {
}
void chain() {
for (int m = 0; m < size_; ++m) {
if (m == idx_) {
alpha_[m]->adj_
+= adj_ * softmax_alpha_[idx_] * (1 - softmax_alpha_[m]);
} else {
alpha_[m]->adj_
-= adj_ * softmax_alpha_[idx_] * softmax_alpha_[m];
}
}
}
};
}
inline Eigen::Matrix<var, Eigen::Dynamic, 1>
softmax(const Eigen::Matrix<var, Eigen::Dynamic, 1>& alpha) {
using Eigen::Matrix;
using Eigen::Dynamic;
stan::math::check_nonzero_size("softmax", "alpha", alpha);
vari** alpha_vi_array
= reinterpret_cast< vari**>(ChainableStack::memalloc_
.alloc(sizeof(vari*) * alpha.size()));
for (int i = 0; i < alpha.size(); ++i)
alpha_vi_array[i] = alpha(i).vi_;
Matrix<double, Dynamic, 1> alpha_d(alpha.size());
for (int i = 0; i < alpha_d.size(); ++i)
alpha_d(i) = alpha(i).val();
Matrix<double, Dynamic, 1> softmax_alpha_d
= stan::math::softmax(alpha_d);
double* softmax_alpha_d_array
= reinterpret_cast<double*>(ChainableStack::memalloc_
.alloc(sizeof(double) * alpha_d.size()));
for (int i = 0; i < alpha_d.size(); ++i)
softmax_alpha_d_array[i] = softmax_alpha_d(i);
Matrix<var, Dynamic, 1> softmax_alpha(alpha.size());
for (int k = 0; k < softmax_alpha.size(); ++k)
softmax_alpha(k) = var(new softmax_elt_vari(softmax_alpha_d[k],
alpha_vi_array,
softmax_alpha_d_array,
alpha.size(),
k));
return softmax_alpha;
}
}
}
#endif
size_
const int size_
Definition: softmax.hpp:18
core.hpp
stan::math::softmax
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: softmax.hpp:14
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:28
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
stan::math::check_nonzero_size
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
Definition: check_nonzero_size.hpp:31
softmax.hpp
softmax_alpha_
const double * softmax_alpha_
Definition: softmax.hpp:17
idx_
const int idx_
Definition: softmax.hpp:19
Eigen.hpp
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
alpha_
vari ** alpha_
Definition: softmax.hpp:16
check_nonzero_size.hpp
stan::math::stack_alloc::alloc
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Definition: stack_alloc.hpp:170

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