File indexing completed on 2025-01-18 10:10:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #ifndef TMVA_DNN_OPTIMIZER
0028 #define TMVA_DNN_OPTIMIZER
0029
0030 #include "TMVA/DNN/GeneralLayer.h"
0031 #include "TMVA/DNN/DeepNet.h"
0032 #include <vector>
0033
0034 namespace TMVA {
0035 namespace DNN {
0036
0037
0038
0039
0040
0041
0042
0043 template <typename Architecture_t, typename Layer_t = VGeneralLayer<Architecture_t>,
0044 typename DeepNet_t = TDeepNet<Architecture_t, Layer_t>>
0045 class VOptimizer {
0046 public:
0047 using Matrix_t = typename Architecture_t::Matrix_t;
0048 using Scalar_t = typename Architecture_t::Scalar_t;
0049
0050 protected:
0051 Scalar_t fLearningRate;
0052 size_t fGlobalStep;
0053 DeepNet_t &fDeepNet;
0054
0055
0056 virtual void
0057 UpdateWeights(size_t layerIndex, std::vector<Matrix_t> &weights, const std::vector<Matrix_t> &weightGradients) = 0;
0058
0059
0060 virtual void
0061 UpdateBiases(size_t layerIndex, std::vector<Matrix_t> &biases, const std::vector<Matrix_t> &biasGradients) = 0;
0062
0063 public:
0064
0065 VOptimizer(Scalar_t learningRate, DeepNet_t &deepNet);
0066
0067
0068 void Step();
0069
0070
0071 virtual ~VOptimizer() = default;
0072
0073
0074 void IncrementGlobalStep() { this->fGlobalStep++; }
0075
0076
0077 Scalar_t GetLearningRate() const
0078 {
0079 return fLearningRate;
0080 }
0081 size_t GetGlobalStep() const { return fGlobalStep; }
0082 std::vector<Layer_t *> &GetLayers() { return fDeepNet.GetLayers(); }
0083 Layer_t *GetLayerAt(size_t i) { return fDeepNet.GetLayerAt(i); }
0084
0085
0086 void SetLearningRate(size_t learningRate) { fLearningRate = learningRate; }
0087 };
0088
0089
0090
0091
0092
0093 template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
0094 VOptimizer<Architecture_t, Layer_t, DeepNet_t>::VOptimizer(Scalar_t learningRate, DeepNet_t &deepNet)
0095 : fLearningRate(learningRate), fGlobalStep(0), fDeepNet(deepNet)
0096 {
0097 }
0098
0099
0100 template <typename Architecture_t, typename Layer_t, typename DeepNet_t>
0101 auto VOptimizer<Architecture_t, Layer_t, DeepNet_t>::Step() -> void
0102 {
0103 for (size_t i = 0; i < this->GetLayers().size(); i++) {
0104 this->UpdateWeights(i, this->GetLayerAt(i)->GetWeights(), this->GetLayerAt(i)->GetWeightGradients());
0105 this->UpdateBiases(i, this->GetLayerAt(i)->GetBiases(), this->GetLayerAt(i)->GetBiasGradients());
0106 }
0107 }
0108
0109 }
0110 }
0111
0112 #endif