mlpack  2.0.1
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  private:
135  DecomposableFunctionType& function;
136 
138  double stepSize;
139 
142 
144  double tolerance;
145 
148  bool shuffle;
149 };
150 
151 } // namespace optimization
152 } // namespace mlpack
153 
154 // Include implementation.
155 #include "sgd_impl.hpp"
156 
157 #endif
double tolerance
The tolerance for termination.
Definition: sgd.hpp:144
double & Tolerance()
Modify the tolerance for termination.
Definition: sgd.hpp:126
Linear algebra utility functions, generally performed on matrices or vectors.
double stepSize
The step size for each example.
Definition: sgd.hpp:138
size_t maxIterations
The maximum number of allowed iterations.
Definition: sgd.hpp:141
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.
bool & Shuffle()
Modify whether or not the individual functions are shuffled.
Definition: sgd.hpp:131
double Tolerance() const
Get the tolerance for termination.
Definition: sgd.hpp:124
double & StepSize()
Modify the step size.
Definition: sgd.hpp:116
bool Shuffle() const
Get whether or not the individual functions are shuffled.
Definition: sgd.hpp:129
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
size_t & MaxIterations()
Modify the maximum number of iterations (0 indicates no limit).
Definition: sgd.hpp:121
double StepSize() const
Get the step size.
Definition: sgd.hpp:114
bool shuffle
Controls whether or not the individual functions are shuffled when iterating.
Definition: sgd.hpp:148
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.
size_t MaxIterations() const
Get the maximum number of iterations (0 indicates no limit).
Definition: sgd.hpp:119
const DecomposableFunctionType & Function() const
Get the instantiated function to be optimized.
Definition: sgd.hpp:109