mlpack  master
sgd.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_OPTIMIZERS_SGD_SGD_HPP
13 #define MLPACK_CORE_OPTIMIZERS_SGD_SGD_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace optimization {
19 
75 template<typename DecomposableFunctionType>
76 class SGD
77 {
78  public:
95  SGD(DecomposableFunctionType& function,
96  const double stepSize = 0.01,
97  const size_t maxIterations = 100000,
98  const double tolerance = 1e-5,
99  const bool shuffle = true);
100 
109  double Optimize(arma::mat& iterate);
110 
112  const DecomposableFunctionType& Function() const { return function; }
114  DecomposableFunctionType& Function() { return function; }
115 
117  double StepSize() const { return stepSize; }
119  double& StepSize() { return stepSize; }
120 
122  size_t MaxIterations() const { return maxIterations; }
124  size_t& MaxIterations() { return maxIterations; }
125 
127  double Tolerance() const { return tolerance; }
129  double& Tolerance() { return tolerance; }
130 
132  bool Shuffle() const { return shuffle; }
134  bool& Shuffle() { return shuffle; }
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:132
double tolerance
The tolerance for termination.
Definition: sgd.hpp:147
double & Tolerance()
Modify the tolerance for termination.
Definition: sgd.hpp:129
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Tolerance() const
Get the tolerance for termination.
Definition: sgd.hpp:127
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:117
const DecomposableFunctionType & Function() const
Get the instantiated function to be optimized.
Definition: sgd.hpp:112
bool & Shuffle()
Modify whether or not the individual functions are shuffled.
Definition: sgd.hpp:134
double & StepSize()
Modify the step size.
Definition: sgd.hpp:119
size_t & MaxIterations()
Modify the maximum number of iterations (0 indicates no limit).
Definition: sgd.hpp:124
size_t MaxIterations() const
Get the maximum number of iterations (0 indicates no limit).
Definition: sgd.hpp:122
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:114
Stochastic Gradient Descent is a technique for minimizing a function which can be expressed as a sum ...
Definition: sgd.hpp:76
double Optimize(arma::mat &iterate)
Optimize the given function using stochastic gradient descent.