StochasticGradientDescent Class Reference

This concrete class represents the stochastic gradient descent optimization algorithm[1] for a loss index of a neural network. More...

#include <stochastic_gradient_descent.h>

Inheritance diagram for StochasticGradientDescent:
OptimizationAlgorithm

Public Member Functions

 StochasticGradientDescent ()
 
 StochasticGradientDescent (LossIndex *)
 
virtual ~StochasticGradientDescent ()
 Destructor. More...
 
const type & get_initial_learning_rate () const
 Returns the initial learning rate. More...
 
const type & get_initial_decay () const
 Returns the initial decay. More...
 
const type & get_momentum () const
 Returns the momentum. More...
 
const bool & get_nesterov () const
 Returns true if nesterov is active, and false otherwise. More...
 
const type & get_loss_goal () const
 
const type & get_maximum_time () const
 Returns the maximum training time. More...
 
void set_loss_index_pointer (LossIndex *)
 
void set_default ()
 Sets the members of the optimization algorithm object to their default values. More...
 
void set_batch_samples_number (const Index &new_batch_samples_number)
 
Index get_batch_samples_number () const
 
void set_initial_learning_rate (const type &)
 
void set_initial_decay (const type &)
 
void set_momentum (const type &)
 
void set_nesterov (const bool &)
 
void set_maximum_epochs_number (const Index &)
 
void set_loss_goal (const type &)
 
void set_maximum_time (const type &)
 
void update_parameters (LossIndexBackPropagation &back_propagation, StochasticGradientDescentData &optimization_data)
 Set hardware to use. Default: Multi-core. More...
 
TrainingResults perform_training ()
 
string write_optimization_algorithm_type () const
 
Tensor< string, 2 > to_string_matrix () const
 Writes as matrix of strings the most representative atributes. More...
 
void from_XML (const tinyxml2::XMLDocument &)
 
void write_XML (tinyxml2::XMLPrinter &) const
 
- Public Member Functions inherited from OptimizationAlgorithm
 OptimizationAlgorithm ()
 
 OptimizationAlgorithm (LossIndex *)
 
virtual ~OptimizationAlgorithm ()
 Destructor. More...
 
LossIndexget_loss_index_pointer () const
 
string get_hardware_use () const
 Hardware use. More...
 
void set_hardware_use (const string &)
 Set hardware to use. Default: Multi-core. More...
 
bool has_loss_index () const
 
const bool & get_display () const
 
const Index & get_display_period () const
 Returns the number of iterations between the training showing progress. More...
 
const Index & get_save_period () const
 Returns the number of iterations between the training saving progress. More...
 
const string & get_neural_network_file_name () const
 Returns the file name where the neural network will be saved. More...
 
const string write_time (const type &) const
 Writes the time from seconds in format HH:mm:ss. More...
 
void set ()
 
virtual void set_threads_number (const int &)
 
virtual void set_display (const bool &)
 
void set_display_period (const Index &)
 
void set_save_period (const Index &)
 
void set_neural_network_file_name (const string &)
 
virtual void check () const
 
virtual void print () const
 Prints to the screen the XML-type representation of the optimization algorithm object. More...
 
void save (const string &) const
 
void load (const string &)
 

Private Attributes

type initial_learning_rate
 Initial learning rate. More...
 
type initial_decay
 Learning rate decay over each update. More...
 
type momentum
 Parameter that accelerates SGD in the relevant direction and dampens oscillations. More...
 
bool nesterov
 Boolean. Whether to apply Nesterov momentum. More...
 
Index batch_samples_number = 1000
 Number of samples per training batch. More...
 
type training_loss_goal = type(0)
 Goal value for the loss. It is used as a stopping criterion. More...
 
Index maximum_selection_failures = numeric_limits<Index>::max()
 Maximum selection error allowed. More...
 
Index maximum_epochs_number = 10000
 Maximum epochs number. More...
 
type maximum_time = type(3600)
 Maximum training time. It is used as a stopping criterion. More...
 

Additional Inherited Members

- Public Types inherited from OptimizationAlgorithm
enum class  StoppingCondition {
  MinimumLossDecrease , LossGoal , MaximumSelectionErrorIncreases , MaximumEpochsNumber ,
  MaximumTime
}
 Enumeration of all possibles condition of stop for the algorithms. More...
 
- Protected Attributes inherited from OptimizationAlgorithm
NonBlockingThreadPool * non_blocking_thread_pool = nullptr
 
ThreadPoolDevice * thread_pool_device
 
LossIndexloss_index_pointer = nullptr
 Pointer to a loss index for a neural network object. More...
 
Index epochs_number = 10000
 Number of training epochs in the neural network. More...
 
string hardware_use = "Multi-core"
 Hardware use. More...
 
Index display_period = 10
 Number of iterations between the training showing progress. More...
 
Index save_period = numeric_limits<Index>::max()
 Number of iterations between the training saving progress. More...
 
string neural_network_file_name = "neural_network.xml"
 Path where the neural network is saved. More...
 
bool display = true
 Display messages to screen. More...
 
const Eigen::array< IndexPair< Index >, 1 > AT_B = {IndexPair<Index>(0, 0)}
 
const Eigen::array< IndexPair< Index >, 1 > product_vector_matrix = {IndexPair<Index>(0, 1)}
 
const Eigen::array< IndexPair< Index >, 1 > A_B = {IndexPair<Index>(1, 0)}
 

Detailed Description

This concrete class represents the stochastic gradient descent optimization algorithm[1] for a loss index of a neural network.

It supports momentum, learning rate decay, and Nesterov momentum.

[1] Neural Designer "5 Algorithms to Train a Neural Network." https://www.neuraldesigner.com/blog/5_algorithms_to_train_a_neural_network

Definition at line 43 of file stochastic_gradient_descent.h.

Constructor & Destructor Documentation

◆ StochasticGradientDescent() [1/2]

Default constructor. It creates a stochastic gradient descent optimization algorithm not associated to any loss index object. It also initializes the class members to their default values.

Definition at line 18 of file stochastic_gradient_descent.cpp.

◆ StochasticGradientDescent() [2/2]

StochasticGradientDescent ( LossIndex new_loss_index_pointer)
explicit

Loss index constructor. It creates a stochastic gradient descent optimization algorithm associated to a loss index. It also initializes the class members to their default values.

Parameters
new_loss_index_pointerPointer to a loss index object.

Definition at line 30 of file stochastic_gradient_descent.cpp.

◆ ~StochasticGradientDescent()

Destructor.

Definition at line 39 of file stochastic_gradient_descent.cpp.

Member Function Documentation

◆ from_XML()

void from_XML ( const tinyxml2::XMLDocument document)
virtual

Loads a default optimization algorithm from a XML document.

Parameters
documentTinyXML document containing the error term members.

Reimplemented from OptimizationAlgorithm.

Definition at line 723 of file stochastic_gradient_descent.cpp.

◆ get_batch_samples_number()

Index get_batch_samples_number ( ) const

Definition at line 124 of file stochastic_gradient_descent.cpp.

◆ get_initial_decay()

const type & get_initial_decay ( ) const

Returns the initial decay.

Definition at line 54 of file stochastic_gradient_descent.cpp.

◆ get_initial_learning_rate()

const type & get_initial_learning_rate ( ) const

Returns the initial learning rate.

Definition at line 46 of file stochastic_gradient_descent.cpp.

◆ get_loss_goal()

const type & get_loss_goal ( ) const

Returns the goal value for the loss. This is used as a stopping criterion when training a neural network

Definition at line 79 of file stochastic_gradient_descent.cpp.

◆ get_maximum_time()

const type & get_maximum_time ( ) const

Returns the maximum training time.

Definition at line 87 of file stochastic_gradient_descent.cpp.

◆ get_momentum()

const type & get_momentum ( ) const

Returns the momentum.

Definition at line 62 of file stochastic_gradient_descent.cpp.

◆ get_nesterov()

const bool & get_nesterov ( ) const

Returns true if nesterov is active, and false otherwise.

Definition at line 70 of file stochastic_gradient_descent.cpp.

◆ perform_training()

TrainingResults perform_training ( )
virtual

Trains a neural network with an associated loss index, according to the stochastic gradient descent method. Training occurs according to the training parameters and stopping criteria. It returns a results structure with the history and the final values of the reserved variables.

Implements OptimizationAlgorithm.

Definition at line 328 of file stochastic_gradient_descent.cpp.

◆ set_batch_samples_number()

void set_batch_samples_number ( const Index &  new_batch_samples_number)
inline

Definition at line 76 of file stochastic_gradient_descent.h.

◆ set_default()

void set_default ( )
virtual

Sets the members of the optimization algorithm object to their default values.

Reimplemented from OptimizationAlgorithm.

Definition at line 103 of file stochastic_gradient_descent.cpp.

◆ set_initial_decay()

void set_initial_decay ( const type &  new_dacay)

Set the initial value for the decay.

Parameters
new_initial_learning_rateinitial value for the decay.

Definition at line 160 of file stochastic_gradient_descent.cpp.

◆ set_initial_learning_rate()

void set_initial_learning_rate ( const type &  new_learning_rate)

Set the initial value for the learning rate. If dacay is not active learning rate will be constant otherwise learning rate will decay over each update.

Parameters
new_initial_learning_rateinitial learning rate value.

Definition at line 134 of file stochastic_gradient_descent.cpp.

◆ set_loss_goal()

void set_loss_goal ( const type &  new_loss_goal)

Sets a new goal value for the loss. This is used as a stopping criterion when training a neural network

Parameters
new_loss_goalGoal value for the loss.

Definition at line 249 of file stochastic_gradient_descent.cpp.

◆ set_loss_index_pointer()

void set_loss_index_pointer ( LossIndex new_loss_index_pointer)
virtual

Sets a pointer to a loss index object to be associated to the gradient descent object. It also sets that loss index to the learning rate algorithm.

Parameters
new_loss_index_pointerPointer to a loss index object.

Reimplemented from OptimizationAlgorithm.

Definition at line 97 of file stochastic_gradient_descent.cpp.

◆ set_maximum_epochs_number()

void set_maximum_epochs_number ( const Index &  new_maximum_epochs_number)

Set the a new maximum for the epochs number.

Parameters
new_maximum_epochsnumber New maximum epochs number.

Definition at line 222 of file stochastic_gradient_descent.cpp.

◆ set_maximum_time()

void set_maximum_time ( const type &  new_maximum_time)

Sets a new maximum training time.

Parameters
new_maximum_timeMaximum training time.

Definition at line 258 of file stochastic_gradient_descent.cpp.

◆ set_momentum()

void set_momentum ( const type &  new_momentum)

Set a new value for momentum, this parameter accelerates SGD in the relevant direction and dampens oscillations.

Parameters
new_momentuminitial value for the mometum.

Definition at line 187 of file stochastic_gradient_descent.cpp.

◆ set_nesterov()

void set_nesterov ( const bool &  new_nesterov_momentum)

Set nesterov, boolean. Whether to apply Nesterov momentum.

Parameters
new_momentuminitial value for the mometum.

Definition at line 213 of file stochastic_gradient_descent.cpp.

◆ to_string_matrix()

Tensor< string, 2 > to_string_matrix ( ) const
virtual

Writes as matrix of strings the most representative atributes.

Reimplemented from OptimizationAlgorithm.

Definition at line 599 of file stochastic_gradient_descent.cpp.

◆ update_parameters()

void update_parameters ( LossIndexBackPropagation back_propagation,
StochasticGradientDescentData optimization_data 
)

Set hardware to use. Default: Multi-core.

Definition at line 283 of file stochastic_gradient_descent.cpp.

◆ write_optimization_algorithm_type()

string write_optimization_algorithm_type ( ) const
virtual

Reimplemented from OptimizationAlgorithm.

Definition at line 591 of file stochastic_gradient_descent.cpp.

◆ write_XML()

void write_XML ( tinyxml2::XMLPrinter file_stream) const
virtual

Serializes the gradient descent object into a XML document of the TinyXML library without keep the DOM tree in memory. See the OpenNN manual for more information about the format of this document.

Reimplemented from OptimizationAlgorithm.

Definition at line 645 of file stochastic_gradient_descent.cpp.

Member Data Documentation

◆ batch_samples_number

Index batch_samples_number = 1000
private

Number of samples per training batch.

Definition at line 138 of file stochastic_gradient_descent.h.

◆ initial_decay

type initial_decay
private

Learning rate decay over each update.

Definition at line 126 of file stochastic_gradient_descent.h.

◆ initial_learning_rate

type initial_learning_rate
private

Initial learning rate.

Definition at line 122 of file stochastic_gradient_descent.h.

◆ maximum_epochs_number

Index maximum_epochs_number = 10000
private

Maximum epochs number.

Definition at line 152 of file stochastic_gradient_descent.h.

◆ maximum_selection_failures

Index maximum_selection_failures = numeric_limits<Index>::max()
private

Maximum selection error allowed.

Definition at line 148 of file stochastic_gradient_descent.h.

◆ maximum_time

type maximum_time = type(3600)
private

Maximum training time. It is used as a stopping criterion.

Definition at line 156 of file stochastic_gradient_descent.h.

◆ momentum

type momentum
private

Parameter that accelerates SGD in the relevant direction and dampens oscillations.

Definition at line 130 of file stochastic_gradient_descent.h.

◆ nesterov

bool nesterov
private

Boolean. Whether to apply Nesterov momentum.

Definition at line 134 of file stochastic_gradient_descent.h.

◆ training_loss_goal

type training_loss_goal = type(0)
private

Goal value for the loss. It is used as a stopping criterion.

Definition at line 144 of file stochastic_gradient_descent.h.


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