mlpack  1.0.12
sgd.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_CORE_OPTIMIZERS_SGD_SGD_HPP
15 #define __MLPACK_CORE_OPTIMIZERS_SGD_SGD_HPP
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace optimization {
21 
77 template<typename DecomposableFunctionType>
78 class SGD
79 {
80  public:
92  SGD(DecomposableFunctionType& function,
93  const double stepSize = 0.01,
94  const size_t maxIterations = 100000,
95  const double tolerance = 1e-5,
96  const bool shuffle = true);
97 
106  double Optimize(arma::mat& iterate);
107 
109  const DecomposableFunctionType& Function() const { return function; }
111  DecomposableFunctionType& Function() { return function; }
112 
114  double StepSize() const { return stepSize; }
116  double& StepSize() { return stepSize; }
117 
119  size_t MaxIterations() const { return maxIterations; }
121  size_t& MaxIterations() { return maxIterations; }
122 
124  double Tolerance() const { return tolerance; }
126  double& Tolerance() { return tolerance; }
127 
129  bool Shuffle() const { return shuffle; }
131  bool& Shuffle() { return shuffle; }
132 
133  // convert the obkect into a string
134  std::string ToString() const;
135 
136  private:
138  DecomposableFunctionType& function;
139 
141  double stepSize;
142 
145 
147  double tolerance;
148 
151  bool shuffle;
152 };
153 
154 }; // namespace optimization
155 }; // namespace mlpack
156 
157 // Include implementation.
158 #include "sgd_impl.hpp"
159 
160 #endif
bool Shuffle() const
Get whether or not the individual functions are shuffled.
Definition: sgd.hpp:129
double tolerance
The tolerance for termination.
Definition: sgd.hpp:147
double & Tolerance()
Modify the tolerance for termination.
Definition: sgd.hpp:126
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
std::string ToString() const
double Tolerance() const
Get the tolerance for termination.
Definition: sgd.hpp:124
double stepSize
The step size for each example.
Definition: sgd.hpp:141
size_t maxIterations
The maximum number of allowed iterations.
Definition: sgd.hpp:144
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:114
const DecomposableFunctionType & Function() const
Get the instantiated function to be optimized.
Definition: sgd.hpp:109
bool & Shuffle()
Modify whether or not the individual functions are shuffled.
Definition: sgd.hpp:131
double & StepSize()
Modify the step size.
Definition: sgd.hpp:116
size_t & MaxIterations()
Modify the maximum number of iterations (0 indicates no limit).
Definition: sgd.hpp:121
size_t MaxIterations() const
Get the maximum number of iterations (0 indicates no limit).
Definition: sgd.hpp:119
bool shuffle
Controls whether or not the individual functions are shuffled when iterating.
Definition: sgd.hpp:151
DecomposableFunctionType & Function()
Modify the instantiated function.
Definition: sgd.hpp:111
Stochastic Gradient Descent is a technique for minimizing a function which can be expressed as a sum ...
Definition: sgd.hpp:78
double Optimize(arma::mat &iterate)
Optimize the given function using stochastic gradient descent.