Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
30 #include "TMVA/DNN/GeneralLayer.h"
31 #include "TMVA/DNN/DeepNet.h"
32 
33 namespace TMVA {
34 namespace 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  */
42 template <typename Architecture_t, typename Layer_t = VGeneralLayer<Architecture_t>,
43  typename DeepNet_t = TDeepNet<Architecture_t, Layer_t>>
44 class VOptimizer {
45 public:
46  using Matrix_t = typename Architecture_t::Matrix_t;
47  using Scalar_t = typename Architecture_t::Scalar_t;
48 
49 protected:
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 
62 public:
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 */
76  Scalar_t GetLearningRate() const { return fLearningRate; }
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 //_________________________________________________________________________________________________
89 template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
90 VOptimizer<Architecture_t, Layer_t, DeepNet_t>::VOptimizer(Scalar_t learningRate, DeepNet_t &deepNet)
91  : fLearningRate(learningRate), fGlobalStep(0), fDeepNet(deepNet)
92 {
93 }
94 
95 //_________________________________________________________________________________________________
96 template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
97 auto VOptimizer<Architecture_t, Layer_t, DeepNet_t>::Step() -> void
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