Logo ROOT  
Reference Guide
Optimizer.h
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn:$Id$
2// Author: Ravi Kiran S
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : VOptimizer *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * General Optimizer Class *
12 * *
13 * Authors (alphabetical): *
14 * Ravi Kiran S <sravikiran0606@gmail.com> - CERN, Switzerland *
15 * *
16 * Copyright (c) 2005-2018 : *
17 * CERN, Switzerland *
18 * U. of Victoria, Canada *
19 * MPI-K Heidelberg, Germany *
20 * U. of Bonn, Germany *
21 * *
22 * Redistribution and use in source and binary forms, with or without *
23 * modification, are permitted according to the terms listed in LICENSE *
24 * (http://tmva.sourceforge.net/LICENSE) *
25 **********************************************************************************/
26
27#ifndef TMVA_DNN_OPTIMIZER
28#define TMVA_DNN_OPTIMIZER
29
31#include "TMVA/DNN/DeepNet.h"
32
33namespace TMVA {
34namespace DNN {
35
36/** \class VOptimizer
37 Generic Optimizer class
38
39 This class represents the general class for all optimizers in the Deep Learning
40 Module.
41 */
42template <typename Architecture_t, typename Layer_t = VGeneralLayer<Architecture_t>,
43 typename DeepNet_t = TDeepNet<Architecture_t, Layer_t>>
45public:
46 using Matrix_t = typename Architecture_t::Matrix_t;
47 using Scalar_t = typename Architecture_t::Scalar_t;
48
49protected:
50 Scalar_t fLearningRate; ///< The learning rate used for training.
51 size_t fGlobalStep; ///< The current global step count during training.
52 DeepNet_t &fDeepNet; ///< The reference to the deep net.
53
54 /*! Update the weights, given the current weight gradients. */
55 virtual void
56 UpdateWeights(size_t layerIndex, std::vector<Matrix_t> &weights, const std::vector<Matrix_t> &weightGradients) = 0;
57
58 /*! Update the biases, given the current bias gradients. */
59 virtual void
60 UpdateBiases(size_t layerIndex, std::vector<Matrix_t> &biases, const std::vector<Matrix_t> &biasGradients) = 0;
61
62public:
63 /*! Constructor. */
64 VOptimizer(Scalar_t learningRate, DeepNet_t &deepNet);
65
66 /*! Performs one step of optimization. */
67 void Step();
68
69 /*! Virtual Destructor. */
70 virtual ~VOptimizer() = default;
71
72 /*! Increments the global step. */
73 void IncrementGlobalStep() { this->fGlobalStep++; }
74
75 /*! Getters */
77 size_t GetGlobalStep() const { return fGlobalStep; }
78 std::vector<Layer_t *> &GetLayers() { return fDeepNet.GetLayers(); }
79 Layer_t *GetLayerAt(size_t i) { return fDeepNet.GetLayerAt(i); }
80
81 /*! Setters */
82 void SetLearningRate(size_t learningRate) { fLearningRate = learningRate; }
83};
84
85//
86//
87// The General Optimizer Class - Implementation
88//_________________________________________________________________________________________________
89template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
91 : fLearningRate(learningRate), fGlobalStep(0), fDeepNet(deepNet)
92{
93}
94
95//_________________________________________________________________________________________________
96template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
98{
99 for (size_t i = 0; i < this->GetLayers().size(); i++) {
100 this->UpdateWeights(i, this->GetLayerAt(i)->GetWeights(), this->GetLayerAt(i)->GetWeightGradients());
101 this->UpdateBiases(i, this->GetLayerAt(i)->GetBiases(), this->GetLayerAt(i)->GetBiasGradients());
102 }
103}
104
105} // namespace DNN
106} // namespace TMVA
107
108#endif
Generic Optimizer class.
Definition: Optimizer.h:44
size_t fGlobalStep
The current global step count during training.
Definition: Optimizer.h:51
Layer_t * GetLayerAt(size_t i)
Definition: Optimizer.h:79
std::vector< Layer_t * > & GetLayers()
Definition: Optimizer.h:78
void IncrementGlobalStep()
Increments the global step.
Definition: Optimizer.h:73
virtual ~VOptimizer()=default
Virtual Destructor.
virtual void UpdateBiases(size_t layerIndex, std::vector< Matrix_t > &biases, const std::vector< Matrix_t > &biasGradients)=0
Update the biases, given the current bias gradients.
virtual void UpdateWeights(size_t layerIndex, std::vector< Matrix_t > &weights, const std::vector< Matrix_t > &weightGradients)=0
Update the weights, given the current weight gradients.
void SetLearningRate(size_t learningRate)
Setters.
Definition: Optimizer.h:82
Scalar_t GetLearningRate() const
Getters.
Definition: Optimizer.h:76
void Step()
Performs one step of optimization.
Definition: Optimizer.h:97
Scalar_t fLearningRate
The learning rate used for training.
Definition: Optimizer.h:50
typename Architecture_t::Scalar_t Scalar_t
Definition: Optimizer.h:47
size_t GetGlobalStep() const
Definition: Optimizer.h:77
DeepNet_t & fDeepNet
The reference to the deep net.
Definition: Optimizer.h:52
VOptimizer(Scalar_t learningRate, DeepNet_t &deepNet)
Constructor.
Definition: Optimizer.h:90
typename Architecture_t::Matrix_t Matrix_t
Definition: Optimizer.h:46
create variable transformations