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 {
78 return fLearningRate;
79 }
80 size_t GetGlobalStep() const { return fGlobalStep; }
81 std::vector<Layer_t *> &GetLayers() { return fDeepNet.GetLayers(); }
82 Layer_t *GetLayerAt(size_t i) { return fDeepNet.GetLayerAt(i); }
83
84 /*! Setters */
85 void SetLearningRate(size_t learningRate) { fLearningRate = learningRate; }
86};
87
88//
89//
90// The General Optimizer Class - Implementation
91//_________________________________________________________________________________________________
92template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
94 : fLearningRate(learningRate), fGlobalStep(0), fDeepNet(deepNet)
95{
96}
97
98//_________________________________________________________________________________________________
99template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
101{
102 for (size_t i = 0; i < this->GetLayers().size(); i++) {
103 this->UpdateWeights(i, this->GetLayerAt(i)->GetWeights(), this->GetLayerAt(i)->GetWeightGradients());
104 this->UpdateBiases(i, this->GetLayerAt(i)->GetBiases(), this->GetLayerAt(i)->GetBiasGradients());
105 }
106}
107
108} // namespace DNN
109} // namespace TMVA
110
111#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:82
std::vector< Layer_t * > & GetLayers()
Definition: Optimizer.h:81
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:85
Scalar_t GetLearningRate() const
Getters.
Definition: Optimizer.h:76
void Step()
Performs one step of optimization.
Definition: Optimizer.h:100
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:80
DeepNet_t & fDeepNet
The reference to the deep net.
Definition: Optimizer.h:52
VOptimizer(Scalar_t learningRate, DeepNet_t &deepNet)
Constructor.
Definition: Optimizer.h:93
typename Architecture_t::Matrix_t Matrix_t
Definition: Optimizer.h:46
create variable transformations