MLPACK  1.0.11
sgd.hpp
Go to the documentation of this file.
1 
22 #ifndef __MLPACK_CORE_OPTIMIZERS_SGD_SGD_HPP
23 #define __MLPACK_CORE_OPTIMIZERS_SGD_SGD_HPP
24 
25 #include <mlpack/core.hpp>
26 
27 namespace mlpack {
28 namespace optimization {
29 
85 template<typename DecomposableFunctionType>
86 class SGD
87 {
88  public:
100  SGD(DecomposableFunctionType& function,
101  const double stepSize = 0.01,
102  const size_t maxIterations = 100000,
103  const double tolerance = 1e-5,
104  const bool shuffle = true);
105 
114  double Optimize(arma::mat& iterate);
115 
117  const DecomposableFunctionType& Function() const { return function; }
119  DecomposableFunctionType& Function() { return function; }
120 
122  double StepSize() const { return stepSize; }
124  double& StepSize() { return stepSize; }
125 
127  size_t MaxIterations() const { return maxIterations; }
129  size_t& MaxIterations() { return maxIterations; }
130 
132  double Tolerance() const { return tolerance; }
134  double& Tolerance() { return tolerance; }
135 
137  bool Shuffle() const { return shuffle; }
139  bool& Shuffle() { return shuffle; }
140 
141  // convert the obkect into a string
142  std::string ToString() const;
143 
144  private:
146  DecomposableFunctionType& function;
147 
149  double stepSize;
150 
153 
155  double tolerance;
156 
159  bool shuffle;
160 };
161 
162 }; // namespace optimization
163 }; // namespace mlpack
164 
165 // Include implementation.
166 #include "sgd_impl.hpp"
167 
168 #endif
bool Shuffle() const
Get whether or not the individual functions are shuffled.
Definition: sgd.hpp:137
double tolerance
The tolerance for termination.
Definition: sgd.hpp:155
double & Tolerance()
Modify the tolerance for termination.
Definition: sgd.hpp:134
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:31
std::string ToString() const
double Tolerance() const
Get the tolerance for termination.
Definition: sgd.hpp:132
double stepSize
The step size for each example.
Definition: sgd.hpp:149
size_t maxIterations
The maximum number of allowed iterations.
Definition: sgd.hpp:152
SGD(DecomposableFunctionType &function, const double stepSize=0.01, const size_t maxIterations=100000, const double tolerance=1e-5, const bool shuffle=true)
Construct the SGD optimizer with the given function and parameters.
double StepSize() const
Get the step size.
Definition: sgd.hpp:122
const DecomposableFunctionType & Function() const
Get the instantiated function to be optimized.
Definition: sgd.hpp:117
bool & Shuffle()
Modify whether or not the individual functions are shuffled.
Definition: sgd.hpp:139
double & StepSize()
Modify the step size.
Definition: sgd.hpp:124
size_t & MaxIterations()
Modify the maximum number of iterations (0 indicates no limit).
Definition: sgd.hpp:129
size_t MaxIterations() const
Get the maximum number of iterations (0 indicates no limit).
Definition: sgd.hpp:127
bool shuffle
Controls whether or not the individual functions are shuffled when iterating.
Definition: sgd.hpp:159
DecomposableFunctionType & Function()
Modify the instantiated function.
Definition: sgd.hpp:119
Stochastic Gradient Descent is a technique for minimizing a function which can be expressed as a sum ...
Definition: sgd.hpp:86
double Optimize(arma::mat &iterate)
Optimize the given function using stochastic gradient descent.