add_decomposable_evaluate_with_gradient_const.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_OPTIMIZERS_ADD_DECOMPOSABLE_EVALUATE_WITH_GRADIENT_CONST_HPP
14 #define MLPACK_CORE_OPTIMIZERS_ADD_DECOMPOSABLE_EVALUATE_WITH_GRADIENT_CONST_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 #include "traits.hpp"
18 
19 namespace mlpack {
20 namespace optimization {
21 
22 namespace aux {
23 
24 template<typename FunctionType>
25 using DecomposableEvaluateForm = double(FunctionType::*)(const arma::mat&,
26  const size_t,
27  const size_t);
28 
29 template<typename FunctionType>
31  double(FunctionType::*)(const arma::mat&, const size_t, const size_t) const;
32 
33 template<typename FunctionType>
34 using DecomposableEvaluateStaticForm = double(*)(const arma::mat&,
35  size_t,
36  size_t);
37 
38 template<typename FunctionType>
39 using DecomposableGradientForm = void(FunctionType::*)(const arma::mat&,
40  const size_t,
41  arma::mat&,
42  const size_t);
43 
44 template<typename FunctionType>
46  void(FunctionType::*)(const arma::mat&,
47  const size_t,
48  arma::mat&,
49  const size_t) const;
50 
51 template<typename FunctionType, typename... Ts>
52 using DecomposableGradientStaticForm = void(*)(const arma::mat&,
53  const size_t,
54  arma::mat&);
55 
56 template<typename FunctionType>
58  double(FunctionType::*)(const arma::mat&,
59  const size_t,
60  arma::mat&,
61  const size_t);
62 
63 template<typename FunctionType>
65  void(FunctionType::*)(const arma::mat&,
66  const size_t,
67  arma::mat&,
68  const size_t) const;
69 
70 template<typename FunctionType>
72  double(*)(const arma::mat&, const size_t, arma::mat&, const size_t);
73 
74 } // namespace aux
75 
83 template<typename FunctionType,
84  bool HasDecomposableEvaluate =
85  aux::HasEvaluate<FunctionType,
87  aux::HasEvaluate<FunctionType,
89  aux::HasEvaluate<FunctionType,
91  bool HasGradient =
92  aux::HasGradient<FunctionType,
94  aux::HasGradient<FunctionType,
96  aux::HasGradient<FunctionType,
98  bool HasEvaluateWithGradient =
99  aux::HasEvaluateWithGradient<FunctionType,
101  aux::HasEvaluateWithGradient<FunctionType,
103  aux::HasEvaluateWithGradient<FunctionType,
105 class AddDecomposableEvaluateWithGradient : public FunctionType { };
106 
111 template<typename FunctionType>
112 class AddDecomposableEvaluateWithGradient<FunctionType, true, true, false> :
113  public FunctionType
114 {
115  public:
123  double EvaluateWithGradient(const arma::mat& coordinates,
124  const size_t begin,
125  arma::mat& gradient,
126  const size_t batchSize)
127  {
128  const double objective = FunctionType::Evaluate(coordinates, begin,
129  batchSize);
130  FunctionType::Gradient(coordinates, begin, gradient, batchSize);
131  return objective;
132  }
133 };
134 
139 template<typename FunctionType>
140 class AddDecomposableEvaluateWithGradient<FunctionType, false, true, true> :
141  public FunctionType
142 {
143  public:
149  double Evaluate(const arma::mat& coordinates,
150  const size_t begin,
151  const size_t batchSize)
152  {
153  arma::mat gradient; // This will be ignored.
154  return FunctionType::EvaluateWithGradient(coordinates, begin, gradient,
155  batchSize);
156  }
157 };
158 
163 template<typename FunctionType>
164 class AddDecomposableEvaluateWithGradient<FunctionType, true, false, true> :
165  public FunctionType
166 {
167  public:
174  void Gradient(const arma::mat& coordinates,
175  const size_t begin,
176  arma::mat& gradient,
177  const size_t batchSize)
178  {
179  // The returned objective value will be ignored.
180  (void) FunctionType::EvaluateWithGradient(coordinates, begin, gradient,
181  batchSize);
182  }
183 };
184 
185 } // namespace optimization
186 } // namespace mlpack
187 
188 #endif
void Gradient(const arma::mat &coordinates, const size_t begin, arma::mat &gradient, const size_t batchSize)
Calculate the gradient and store it in the given matrix.
void(FunctionType::*)(const arma::mat &, const size_t, arma::mat &, const size_t) const DecomposableGradientConstForm
double(FunctionType::*)(const arma::mat &, const size_t, arma::mat &, const size_t) DecomposableEvaluateWithGradientForm
.hpp
Definition: add_to_po.hpp:21
The AddDecomposableEvaluateWithGradient mixin class will add a decomposable EvaluateWithGradient() me...
double(*)(const arma::mat &, size_t, size_t) DecomposableEvaluateStaticForm
The core includes that mlpack expects; standard C++ includes and Armadillo.
double EvaluateWithGradient(const arma::mat &coordinates, const size_t begin, arma::mat &gradient, const size_t batchSize)
Return both the evaluated objective function and its gradient, storing the gradient in the given matr...
void(FunctionType::*)(const arma::mat &, const size_t, arma::mat &, const size_t) DecomposableGradientForm
double(FunctionType::*)(const arma::mat &, const size_t, const size_t) DecomposableEvaluateForm
void(*)(const arma::mat &, const size_t, arma::mat &) DecomposableGradientStaticForm
double(FunctionType::*)(const arma::mat &, const size_t, const size_t) const DecomposableEvaluateConstForm
double(*)(const arma::mat &, const size_t, arma::mat &, const size_t) DecomposableEvaluateWithGradientStaticForm
double Evaluate(const arma::mat &coordinates, const size_t begin, const size_t batchSize)
Return the objective function for the given coordinates.
void(FunctionType::*)(const arma::mat &, const size_t, arma::mat &, const size_t) const DecomposableEvaluateWithGradientConstForm