Logo ROOT   6.12/07
Reference Guide
Functions.h
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 20/06/16
3 
4 /*************************************************************************
5  * Copyright (C) 2016, Simon Pfreundschuh *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /////////////////////////////////////////////////////////////////////
13 // Contains function enums for activation and output functions, as //
14 // well as generic evaluation functions, that delegate the call to //
15 // the corresponding evaluation kernel. //
16 /////////////////////////////////////////////////////////////////////
17 
18 #ifndef TMVA_DNN_FUNCTIONS
19 #define TMVA_DNN_FUNCTIONS
20 
21 namespace TMVA
22 {
23 namespace DNN
24 {
25 //______________________________________________________________________________
26 //
27 // Enum Definitions
28 //______________________________________________________________________________
29 
30 /*! Enum that represents layer activation functions. */
32 {
33  kIdentity = 0,
34  kRelu = 1,
35  kSigmoid = 2,
36  kTanh = 3,
37  kSymmRelu = 4,
38  kSoftSign = 5,
39  kGauss = 6
40 };
41 
42 /*! Enum that represents output functions */
43 enum class EOutputFunction
44 {
45  kIdentity = 'I',
46  kSigmoid = 'S',
47  kSoftmax = 'M'
48 };
49 
50 /*! Enum that represents objective functions for the net, i.e. functions
51 * that take the output from the last layer in the net together with the
52 * truths and return the objective function values that is to be minimized
53 * in the training process. */
54 enum class ELossFunction
55 {
56  kCrossEntropy = 'C',
57  kMeanSquaredError = 'R',
59 };
60 
61 /*! Enum representing the regularization type applied for a given layer */
62 enum class ERegularization
63 {
64  kNone = '0',
65  kL1 = '1',
66  kL2 = '2'
67  };
68 
69 /* Enum represnting the initialization method used for this layer. */
70 enum class EInitialization {
71  kGauss = 'G',
72  kUniform = 'U',
73  kIdentity = 'I',
74  kZero = 'Z'
75 };
76 
77 //______________________________________________________________________________
78 //
79 // Activation Functions
80 //______________________________________________________________________________
81 
82 /*! Apply the given activation function to each value in the given
83 * matrix A. */
84 template<typename Architecture_t>
85 inline void evaluate(typename Architecture_t::Matrix_t &A,
87 {
88  switch(f)
89  {
90  case EActivationFunction::kIdentity : break;
91  case EActivationFunction::kRelu : Architecture_t::Relu(A);
92  break;
94  break;
96  break;
97  case EActivationFunction::kSymmRelu : Architecture_t::SymmetricRelu(A);
98  break;
100  break;
102  break;
103  }
104 }
105 
106 
107 /*! Compute the first partial derivative of the activation function for
108 * the values given in matrix A and write the results into B. */
109 //______________________________________________________________________________
110 template<typename Architecture_t>
111 inline void evaluateDerivative(typename Architecture_t::Matrix_t & B,
113  const typename Architecture_t::Matrix_t & A)
114 {
115  switch(f)
116  {
117  case EActivationFunction::kIdentity : Architecture_t::IdentityDerivative(B, A);
118  break;
119  case EActivationFunction::kRelu : Architecture_t::ReluDerivative(B, A);
120  break;
121  case EActivationFunction::kSigmoid : Architecture_t::SigmoidDerivative(B, A);
122  break;
123  case EActivationFunction::kTanh : Architecture_t::TanhDerivative(B, A);
124  break;
125  case EActivationFunction::kSymmRelu : Architecture_t::SymmetricReluDerivative(B, A);
126  break;
127  case EActivationFunction::kSoftSign : Architecture_t::SoftSignDerivative(B, A);
128  break;
129  case EActivationFunction::kGauss : Architecture_t::GaussDerivative(B, A);
130  break;
131  }
132 }
133 
134 //______________________________________________________________________________
135 //
136 // Output Functions
137 //______________________________________________________________________________
138 
139 /*! Apply the given output function to each value in the given
140 * matrix A. */
141 template<typename Architecture_t>
142 inline void evaluate(typename Architecture_t::Matrix_t &A,
143  EOutputFunction f,
144  const typename Architecture_t::Matrix_t &X)
145 {
146  switch(f)
147  {
149  break;
151  break;
152  case EOutputFunction::kSoftmax : Architecture_t::Softmax(A, X);
153  break;
154  }
155 }
156 
157 //______________________________________________________________________________
158 //
159 // Loss Functions
160 //______________________________________________________________________________
161 
162 /*! Compute the value of the objective function f for given activations
163 * of the ouput layer and the truth Y. */
164 template <typename Architecture_t>
165 inline auto evaluate(ELossFunction f, const typename Architecture_t::Matrix_t &Y,
166  const typename Architecture_t::Matrix_t &output, const typename Architecture_t::Matrix_t &weights)
167  -> decltype(Architecture_t::CrossEntropy(Y, output, weights))
168 {
169  switch(f)
170  {
171  case ELossFunction::kCrossEntropy: return Architecture_t::CrossEntropy(Y, output, weights);
172  case ELossFunction::kMeanSquaredError: return Architecture_t::MeanSquaredError(Y, output, weights);
173  case ELossFunction::kSoftmaxCrossEntropy: return Architecture_t::SoftmaxCrossEntropy(Y, output, weights);
174  }
175  return 0.0;
176 }
177 
178 /*! Compute the gradient of the given output function f for given activations
179 * output of the output layer and truth Y and write the results into dY. */
180 //______________________________________________________________________________
181 template <typename Architecture_t>
182 inline void evaluateGradients(typename Architecture_t::Matrix_t &dY, ELossFunction f,
183  const typename Architecture_t::Matrix_t &Y,
184  const typename Architecture_t::Matrix_t &output,
185  const typename Architecture_t::Matrix_t &weights)
186 {
187  switch(f)
188  {
189  case ELossFunction::kCrossEntropy: Architecture_t::CrossEntropyGradients(dY, Y, output, weights); break;
190  case ELossFunction::kMeanSquaredError: Architecture_t::MeanSquaredErrorGradients(dY, Y, output, weights); break;
192  Architecture_t::SoftmaxCrossEntropyGradients(dY, Y, output, weights);
193  break;
194  }
195 }
196 
197 
198 //______________________________________________________________________________
199 //
200 // Regularization
201 //______________________________________________________________________________
202 
203 /*! Evaluate the regularization functional for a given weight matrix. */
204 template<typename Architecture_t>
205 inline auto regularization(const typename Architecture_t::Matrix_t &A,
207 -> decltype(Architecture_t::L1Regularization(A))
208 {
209  switch(R)
210  {
212  return 0.0;
213  case ERegularization::kL1 :
214  return Architecture_t::L1Regularization(A);
215  case ERegularization::kL2 :
216  return Architecture_t::L2Regularization(A);
217  }
218  return 0.0;
219 }
220 
221 /*! Add the regularization gradient corresponding to weight matrix W, to
222 * the matrix A. */
223 //______________________________________________________________________________
224 template<typename Architecture_t>
225 inline void addRegularizationGradients(typename Architecture_t::Matrix_t &A,
226  const typename Architecture_t::Matrix_t &W,
227  typename Architecture_t::Scalar_t weightDecay,
229 {
230  switch(R)
231  {
233  break;
234  case ERegularization::kL1 :
235  Architecture_t::AddL1RegularizationGradients(A, W, weightDecay);
236  break;
237  case ERegularization::kL2 :
238  Architecture_t::AddL2RegularizationGradients(A, W, weightDecay);
239  break;
240  }
241 }
242 
243 //______________________________________________________________________________
244 //
245 // Initialization
246 //______________________________________________________________________________
247 
248 template<typename Architecture_t>
249 inline void initialize(typename Architecture_t::Matrix_t & A,
251 {
252  switch(m) {
253  case EInitialization::kGauss : Architecture_t::InitializeGauss(A);
254  break;
255  case EInitialization::kUniform : Architecture_t::InitializeUniform(A);
256  break;
257  case EInitialization::kIdentity : Architecture_t::InitializeIdentity(A);
258  break;
259  case EInitialization::kZero : Architecture_t::InitializeZero(A);
260  break;
261  }
262 }
263 
264 } // namespace DNN
265 } // namespace TMVA
266 
267 #endif
static double B[]
void evaluateDerivative(typename Architecture_t::Matrix_t &B, EActivationFunction f, const typename Architecture_t::Matrix_t &A)
Compute the first partial derivative of the activation function for the values given in matrix A and ...
Definition: Functions.h:111
auto * m
Definition: textangle.C:8
static std::shared_ptr< std::function< double(double)> > Tanh
Definition: NeuralNet.icc:56
static double A[]
void evaluate(typename Architecture_t::Matrix_t &A, EActivationFunction f)
Apply the given activation function to each value in the given matrix A.
Definition: Functions.h:85
EInitialization
Definition: Functions.h:70
static std::shared_ptr< std::function< double(double)> > Sigmoid
Definition: NeuralNet.icc:53
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
Definition: NeuralNet.icc:497
void evaluateGradients(typename Architecture_t::Matrix_t &dY, ELossFunction f, const typename Architecture_t::Matrix_t &Y, const typename Architecture_t::Matrix_t &output, const typename Architecture_t::Matrix_t &weights)
Compute the gradient of the given output function f for given activations output of the output layer ...
Definition: Functions.h:182
auto regularization(const typename Architecture_t::Matrix_t &A, ERegularization R) -> decltype(Architecture_t::L1Regularization(A))
Evaluate the regularization functional for a given weight matrix.
Definition: Functions.h:205
static std::shared_ptr< std::function< double(double)> > SoftSign
Definition: NeuralNet.icc:74
void Copy(void *source, void *dest)
void addRegularizationGradients(typename Architecture_t::Matrix_t &A, const typename Architecture_t::Matrix_t &W, typename Architecture_t::Scalar_t weightDecay, ERegularization R)
Add the regularization gradient corresponding to weight matrix W, to the matrix A.
Definition: Functions.h:225
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:43
ELossFunction
Enum that represents objective functions for the net, i.e.
Definition: Functions.h:54
static std::shared_ptr< std::function< double(double)> > Gauss
Definition: NeuralNet.icc:77
Abstract ClassifierFactory template that handles arbitrary types.
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:62
EActivationFunction
Enum that represents layer activation functions.
Definition: Functions.h:31
constexpr Double_t R()
Definition: TMath.h:213
void initialize(typename Architecture_t::Matrix_t &A, EInitialization m)
Definition: Functions.h:249