Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include <vector>
33
34namespace TMVA {
35namespace DNN {
36
37/** \class VOptimizer
38 Generic Optimizer class
39
40 This class represents the general class for all optimizers in the Deep Learning
41 Module.
42 */
43template <typename Architecture_t, typename Layer_t = VGeneralLayer<Architecture_t>,
44 typename DeepNet_t = TDeepNet<Architecture_t, Layer_t>>
46public:
47 using Matrix_t = typename Architecture_t::Matrix_t;
48 using Scalar_t = typename Architecture_t::Scalar_t;
49
50protected:
51 Scalar_t fLearningRate; ///< The learning rate used for training.
52 size_t fGlobalStep; ///< The current global step count during training.
53 DeepNet_t &fDeepNet; ///< The reference to the deep net.
54
55 /*! Update the weights, given the current weight gradients. */
56 virtual void
57 UpdateWeights(size_t layerIndex, std::vector<Matrix_t> &weights, const std::vector<Matrix_t> &weightGradients) = 0;
58
59 /*! Update the biases, given the current bias gradients. */
60 virtual void
61 UpdateBiases(size_t layerIndex, std::vector<Matrix_t> &biases, const std::vector<Matrix_t> &biasGradients) = 0;
62
63public:
64 /*! Constructor. */
65 VOptimizer(Scalar_t learningRate, DeepNet_t &deepNet);
66
67 /*! Performs one step of optimization. */
68 void Step();
69
70 /*! Virtual Destructor. */
71 virtual ~VOptimizer() = default;
72
73 /*! Increments the global step. */
74 void IncrementGlobalStep() { this->fGlobalStep++; }
75
76 /*! Getters */
78 {
79 return fLearningRate;
80 }
81 size_t GetGlobalStep() const { return fGlobalStep; }
82 std::vector<Layer_t *> &GetLayers() { return fDeepNet.GetLayers(); }
83 Layer_t *GetLayerAt(size_t i) { return fDeepNet.GetLayerAt(i); }
84
85 /*! Setters */
86 void SetLearningRate(size_t learningRate) { fLearningRate = learningRate; }
87};
88
89//
90//
91// The General Optimizer Class - Implementation
92//_________________________________________________________________________________________________
93template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
95 : fLearningRate(learningRate), fGlobalStep(0), fDeepNet(deepNet)
96{
97}
98
99//_________________________________________________________________________________________________
100template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
102{
103 for (size_t i = 0; i < this->GetLayers().size(); i++) {
104 this->UpdateWeights(i, this->GetLayerAt(i)->GetWeights(), this->GetLayerAt(i)->GetWeightGradients());
105 this->UpdateBiases(i, this->GetLayerAt(i)->GetBiases(), this->GetLayerAt(i)->GetBiasGradients());
106 }
107}
108
109} // namespace DNN
110} // namespace TMVA
111
112#endif
Generic Optimizer class.
Definition Optimizer.h:45
size_t fGlobalStep
The current global step count during training.
Definition Optimizer.h:52
Layer_t * GetLayerAt(size_t i)
Definition Optimizer.h:83
std::vector< Layer_t * > & GetLayers()
Definition Optimizer.h:82
void IncrementGlobalStep()
Increments the global step.
Definition Optimizer.h:74
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:86
Scalar_t GetLearningRate() const
Getters.
Definition Optimizer.h:77
void Step()
Performs one step of optimization.
Definition Optimizer.h:101
Scalar_t fLearningRate
The learning rate used for training.
Definition Optimizer.h:51
typename Architecture_t::Scalar_t Scalar_t
Definition Optimizer.h:48
size_t GetGlobalStep() const
Definition Optimizer.h:81
DeepNet_t & fDeepNet
The reference to the deep net.
Definition Optimizer.h:53
VOptimizer(Scalar_t learningRate, DeepNet_t &deepNet)
Constructor.
Definition Optimizer.h:94
typename Architecture_t::Matrix_t Matrix_t
Definition Optimizer.h:47
create variable transformations