mlpack
master
|
Stochastic Gradient Descent is a technique for minimizing a function which can be expressed as a sum of other functions. More...
Public Member Functions | |
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. More... | |
const DecomposableFunctionType & | Function () const |
Get the instantiated function to be optimized. More... | |
DecomposableFunctionType & | Function () |
Modify the instantiated function. More... | |
size_t | MaxIterations () const |
Get the maximum number of iterations (0 indicates no limit). More... | |
size_t & | MaxIterations () |
Modify the maximum number of iterations (0 indicates no limit). More... | |
double | Optimize (arma::mat &iterate) |
Optimize the given function using stochastic gradient descent. More... | |
template<> | |
double | Optimize (arma::mat ¶meters) |
Template specialization for SGD optimizer. More... | |
bool | Shuffle () const |
Get whether or not the individual functions are shuffled. More... | |
bool & | Shuffle () |
Modify whether or not the individual functions are shuffled. More... | |
double | StepSize () const |
Get the step size. More... | |
double & | StepSize () |
Modify the step size. More... | |
double | Tolerance () const |
Get the tolerance for termination. More... | |
double & | Tolerance () |
Modify the tolerance for termination. More... | |
Private Attributes | |
DecomposableFunctionType & | function |
The instantiated function. More... | |
size_t | maxIterations |
The maximum number of allowed iterations. More... | |
bool | shuffle |
Controls whether or not the individual functions are shuffled when iterating. More... | |
double | stepSize |
The step size for each example. More... | |
double | tolerance |
The tolerance for termination. More... | |
Stochastic Gradient Descent is a technique for minimizing a function which can be expressed as a sum of other functions.
That is, suppose we have
and our task is to minimize . Stochastic gradient descent iterates over each function
, producing the following update scheme:
where is a parameter which specifies the step size.
is chosen according to
(the iteration number). The SGD class supports either scanning through each of the
functions
linearly, or in a random sequence. The algorithm continues until
reaches the maximum number of iterations—or when a full sequence of updates through each of the
functions
produces an improvement within a certain tolerance
. That is,
The parameter is specified by the tolerance parameter to the constructor;
is specified by the maxIterations parameter.
This class is useful for data-dependent functions whose objective function can be expressed as a sum of objective functions operating on an individual point. Then, SGD considers the gradient of the objective function operating on an individual point in its update of .
For SGD to work, a DecomposableFunctionType template parameter is required. This class must implement the following function:
size_t NumFunctions(); double Evaluate(const arma::mat& coordinates, const size_t i); void Gradient(const arma::mat& coordinates, const size_t i, arma::mat& gradient);
NumFunctions() should return the number of functions ( ), and in the other two functions, the parameter i refers to which individual function (or gradient) is being evaluated. So, for the case of a data-dependent function, such as NCA (see mlpack::nca::NCA), NumFunctions() should return the number of points in the dataset, and Evaluate(coordinates, 0) will evaluate the objective function on the first point in the dataset (presumably, the dataset is held internally in the DecomposableFunctionType).
DecomposableFunctionType | Decomposable objective function type to be minimized. |
mlpack::optimization::SGD< DecomposableFunctionType >::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.
The defaults here are not necessarily good for the given problem, so it is suggested that the values used be tailored to the task at hand. The maximum number of iterations refers to the maximum number of points that are processed (i.e., one iteration equals one point; one iteration does not equal one pass over the dataset).
function | Function to be optimized (minimized). |
stepSize | Step size for each iteration. |
maxIterations | Maximum number of iterations allowed (0 means no limit). |
tolerance | Maximum absolute tolerance to terminate algorithm. |
shuffle | If true, the function order is shuffled; otherwise, each function is visited in linear order. |
|
inline |
|
inline |
|
inline |
Get the maximum number of iterations (0 indicates no limit).
Definition at line 122 of file sgd.hpp.
References mlpack::optimization::SGD< DecomposableFunctionType >::maxIterations.
|
inline |
Modify the maximum number of iterations (0 indicates no limit).
Definition at line 124 of file sgd.hpp.
References mlpack::optimization::SGD< DecomposableFunctionType >::maxIterations.
double mlpack::optimization::SGD< DecomposableFunctionType >::Optimize | ( | arma::mat & | iterate | ) |
Optimize the given function using stochastic gradient descent.
The given starting point will be modified to store the finishing point of the algorithm, and the final objective value is returned.
iterate | Starting point (will be modified). |
double mlpack::optimization::SGD< mlpack::svd::RegularizedSVDFunction >::Optimize | ( | arma::mat & | parameters | ) |
Template specialization for SGD optimizer.
Used because the gradient affects only a small number of parameters per example, and thus the normal abstraction does not work as fast as we might like it to.
|
inline |
Get whether or not the individual functions are shuffled.
Definition at line 132 of file sgd.hpp.
References mlpack::optimization::SGD< DecomposableFunctionType >::shuffle.
|
inline |
Modify whether or not the individual functions are shuffled.
Definition at line 134 of file sgd.hpp.
References mlpack::optimization::SGD< DecomposableFunctionType >::shuffle.
|
inline |
Get the step size.
Definition at line 117 of file sgd.hpp.
References mlpack::optimization::SGD< DecomposableFunctionType >::stepSize.
|
inline |
Modify the step size.
Definition at line 119 of file sgd.hpp.
References mlpack::optimization::SGD< DecomposableFunctionType >::stepSize.
|
inline |
Get the tolerance for termination.
Definition at line 127 of file sgd.hpp.
References mlpack::optimization::SGD< DecomposableFunctionType >::tolerance.
|
inline |
Modify the tolerance for termination.
Definition at line 129 of file sgd.hpp.
References mlpack::optimization::SGD< DecomposableFunctionType >::tolerance.
|
private |
|
private |
The maximum number of allowed iterations.
Definition at line 144 of file sgd.hpp.
Referenced by mlpack::optimization::SGD< DecomposableFunctionType >::MaxIterations().
|
private |
Controls whether or not the individual functions are shuffled when iterating.
Definition at line 151 of file sgd.hpp.
Referenced by mlpack::optimization::SGD< DecomposableFunctionType >::Shuffle().
|
private |
The step size for each example.
Definition at line 141 of file sgd.hpp.
Referenced by mlpack::optimization::SGD< DecomposableFunctionType >::StepSize().
|
private |
The tolerance for termination.
Definition at line 147 of file sgd.hpp.
Referenced by mlpack::optimization::SGD< DecomposableFunctionType >::Tolerance().