mlpack  master
Public Member Functions | Private Attributes | List of all members
mlpack::optimization::MiniBatchSGD< DecomposableFunctionType > Class Template Reference

Mini-batch Stochastic Gradient Descent is a technique for minimizing a function which can be expressed as a sum of other functions. More...

Public Member Functions

 MiniBatchSGD (DecomposableFunctionType &function, const size_t batchSize=1000, const double stepSize=0.01, const size_t maxIterations=100000, const double tolerance=1e-5, const bool shuffle=true)
 Construct the MiniBatchSGD optimizer with the given function and parameters. More...
 
size_t BatchSize () const
 Get the batch size. More...
 
size_t & BatchSize ()
 Modify the batch size. 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 mini-batch SGD. 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

size_t batchSize
 The size of each mini-batch. More...
 
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...
 

Detailed Description

template<typename DecomposableFunctionType>
class mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >

Mini-batch 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

\[ f(A) = \sum_{i = 0}^{n} f_i(A) \]

and our task is to minimize $ A $. Mini-batch SGD iterates over batches of functions $ \{ f_{i0}(A), f_{i1}(A), \ldots, f_{i(m - 1)}(A) $ for some batch size $ m $, producing the following update scheme:

\[ A_{j + 1} = A_j + \alpha \left(\sum_{k = 0}^{m - 1} \nabla f_{ik}(A) \right) \]

where $ \alpha $ is a parameter which specifies the step size. Each mini-batch is passed through either sequentially or randomly. The algorithm continues until $ j $ reaches the maximum number of iterations—or when a full sequence of updates through each of the mini-batches produces an improvement within a certain tolerance $ \epsilon $.

The parameter $ \epsilon $ is specified by the tolerance parameter tot he constructor, as is the maximum number of iterations 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, mini-batch SGD considers the gradient of the objective function operation on an individual mini-batch of points in its update of $ A $.

For mini-batch 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).

Template Parameters
DecomposableFunctionTypeDecomposable objective function type to be minimized.

Definition at line 74 of file minibatch_sgd.hpp.

Constructor & Destructor Documentation

template<typename DecomposableFunctionType >
mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::MiniBatchSGD ( DecomposableFunctionType &  function,
const size_t  batchSize = 1000,
const double  stepSize = 0.01,
const size_t  maxIterations = 100000,
const double  tolerance = 1e-5,
const bool  shuffle = true 
)

Construct the MiniBatchSGD 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 for the task at hand. The maximum number of iterations refers to the maximum number of mini-batches that are processed.

Parameters
functionFunction to be optimized (minimized).
batchSizeSize of each mini-batch.
stepSizeStep size for each iteration.
maxIterationsMaximum number of iterations allowed (0 means no limit).
toleranceMaximum absolute tolerance to terminate algorithm.
shuffleIf true, the mini-batch order is shuffled; otherwise, each mini-batch is visited in linear order.

Member Function Documentation

template<typename DecomposableFunctionType >
size_t mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::BatchSize ( ) const
inline

Get the batch size.

Definition at line 116 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::batchSize.

template<typename DecomposableFunctionType >
size_t& mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::BatchSize ( )
inline

Modify the batch size.

Definition at line 118 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::batchSize.

template<typename DecomposableFunctionType >
const DecomposableFunctionType& mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Function ( ) const
inline

Get the instantiated function to be optimized.

Definition at line 111 of file minibatch_sgd.hpp.

template<typename DecomposableFunctionType >
DecomposableFunctionType& mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Function ( )
inline

Modify the instantiated function.

Definition at line 113 of file minibatch_sgd.hpp.

template<typename DecomposableFunctionType >
size_t mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::MaxIterations ( ) const
inline

Get the maximum number of iterations (0 indicates no limit).

Definition at line 126 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::maxIterations.

template<typename DecomposableFunctionType >
size_t& mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::MaxIterations ( )
inline

Modify the maximum number of iterations (0 indicates no limit).

Definition at line 128 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::maxIterations.

template<typename DecomposableFunctionType >
double mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Optimize ( arma::mat &  iterate)

Optimize the given function using mini-batch SGD.

The given starting point will be modified to store the finishing point of the algorithm, and the final objective value is returned.

Parameters
iterateStarting point (will be modified).
Returns
Objective value of the final point.
template<typename DecomposableFunctionType >
bool mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Shuffle ( ) const
inline

Get whether or not the individual functions are shuffled.

Definition at line 136 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::shuffle.

template<typename DecomposableFunctionType >
bool& mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Shuffle ( )
inline

Modify whether or not the individual functions are shuffled.

Definition at line 138 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::shuffle.

template<typename DecomposableFunctionType >
double mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::StepSize ( ) const
inline

Get the step size.

Definition at line 121 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::stepSize.

template<typename DecomposableFunctionType >
double& mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::StepSize ( )
inline

Modify the step size.

Definition at line 123 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::stepSize.

template<typename DecomposableFunctionType >
double mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Tolerance ( ) const
inline

Get the tolerance for termination.

Definition at line 131 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::tolerance.

template<typename DecomposableFunctionType >
double& mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Tolerance ( )
inline

Modify the tolerance for termination.

Definition at line 133 of file minibatch_sgd.hpp.

References mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::tolerance.

Member Data Documentation

template<typename DecomposableFunctionType >
size_t mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::batchSize
private

The size of each mini-batch.

Definition at line 145 of file minibatch_sgd.hpp.

Referenced by mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::BatchSize().

template<typename DecomposableFunctionType >
DecomposableFunctionType& mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::function
private

The instantiated function.

Definition at line 142 of file minibatch_sgd.hpp.

template<typename DecomposableFunctionType >
size_t mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::maxIterations
private

The maximum number of allowed iterations.

Definition at line 151 of file minibatch_sgd.hpp.

Referenced by mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::MaxIterations().

template<typename DecomposableFunctionType >
bool mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::shuffle
private

Controls whether or not the individual functions are shuffled when iterating.

Definition at line 158 of file minibatch_sgd.hpp.

Referenced by mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Shuffle().

template<typename DecomposableFunctionType >
double mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::stepSize
private

The step size for each example.

Definition at line 148 of file minibatch_sgd.hpp.

Referenced by mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::StepSize().

template<typename DecomposableFunctionType >
double mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::tolerance
private

The tolerance for termination.

Definition at line 154 of file minibatch_sgd.hpp.

Referenced by mlpack::optimization::MiniBatchSGD< DecomposableFunctionType >::Tolerance().


The documentation for this class was generated from the following file: