sparse_test_function.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_OPTIMIZERS_PARALLEL_SGD_SPARSE_TEST_FUNCTION_HPP
13 #define MLPACK_CORE_OPTIMIZERS_PARALLEL_SGD_SPARSE_TEST_FUNCTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace optimization {
19 namespace test {
20 
21 // A simple test function. Each dimension has a parabola with a
22 // distinct minimum. Each update is guaranteed to be sparse(only a single
23 // dimension is updated in the decision variable by each thread). At the end of
24 // a reasonable number of iterations, each value in the decision variable should
25 // be at the vertex of the parabola in that dimension.
27 {
28  public:
31  {
32  intercepts = arma::vec("20 12 15 100");
33  bi = arma::vec("-4 -2 -3 -8");
34  }
35 
37  size_t NumFunctions() const { return 4; }
38 
40  size_t NumFeatures() const { return 4; }
41 
43  arma::mat GetInitialPoint() const { return arma::mat("0 0 0 0;"); }
44 
46  double Evaluate(const arma::mat& coordinates,
47  const size_t i,
48  const size_t batchSize = 1) const
49  {
50  double result = 0.0;
51  for (size_t j = i; j < i + batchSize; ++j)
52  {
53  result += coordinates[j] * coordinates[j] + bi[j] * coordinates[j] +
54  intercepts[j];
55  }
56 
57  return result;
58  }
59 
61  double Evaluate(const arma::mat& coordinates) const
62  {
63  double objective = 0.0;
64  for (size_t i = 0; i < NumFunctions(); ++i)
65  {
66  objective += coordinates[i] * coordinates[i] + bi[i] * coordinates[i] +
67  intercepts[i];
68  }
69 
70  return objective;
71  }
72 
74  void Gradient(const arma::mat& coordinates,
75  const size_t i,
76  arma::sp_mat& gradient,
77  const size_t batchSize = 1) const
78  {
79  gradient.zeros(arma::size(coordinates));
80  for (size_t j = i; j < i + batchSize; ++j)
81  gradient[j] = 2 * coordinates[j] + bi[j];
82  }
83 
85  void PartialGradient(const arma::mat& coordinates,
86  const size_t j,
87  arma::sp_mat& gradient) const
88  {
89  gradient.zeros(arma::size(coordinates));
90  gradient[j] = 2 * coordinates[j] + bi[j];
91  }
92 
93  private:
94  // Each quadratic polynomial is monic. The intercept and coefficient of the
95  // first order term is stored.
96 
98  arma::vec intercepts;
99 
101  arma::vec bi;
102 };
103 
104 } // namespace test
105 } // namespace optimization
106 } // namespace mlpack
107 
108 #endif
void PartialGradient(const arma::mat &coordinates, const size_t j, arma::sp_mat &gradient) const
Evaluate the gradient of a feature function.
SparseTestFunction()
Set members in the default constructor.
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Evaluate(const arma::mat &coordinates, const size_t i, const size_t batchSize=1) const
Evaluate a function.
size_t NumFunctions() const
Return 4 (the number of functions).
double Evaluate(const arma::mat &coordinates) const
Evaluate all the functions.
arma::mat GetInitialPoint() const
Get the starting point.
size_t NumFeatures() const
Return 4 (the number of features).
void Gradient(const arma::mat &coordinates, const size_t i, arma::sp_mat &gradient, const size_t batchSize=1) const
Evaluate the gradient of a function.