Logo ROOT   6.07/09
Reference Guide
Reference.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 // Declaration of the TReference architecture, which provides a //
14 // reference implementation of the low-level interface for the DNN //
15 // implementation based on ROOT's TMatrixT matrix type. //
16 ///////////////////////////////////////////////////////////////////////
17 
18 #ifndef TMVA_DNN_ARCHITECTURES_REFERENCE
19 #define TMVA_DNN_ARCHITECTURES_REFERENCE
20 
21 #include "TMatrix.h"
22 
23 namespace TMVA
24 {
25 namespace DNN
26 {
27 
28 /*! The reference architecture class.
29 *
30 * Class template that contains the reference implementation of the low-level
31 * interface for the DNN implementation. The reference implementation uses the
32 * TMatrixT class template to represent matrices.
33 *
34 * \tparam Real_t The floating point type used to represent scalars.
35 */
36 template<typename Real_t>
38 {
39 public:
40 
41  using Scalar_t = Real_t;
43 
44  //____________________________________________________________________________
45  //
46  // Propagation
47  //____________________________________________________________________________
48 
49  /** @name Forward Propagation
50  * Low-level functions required for the forward propagation of activations
51  * through the network.
52  */
53  ///@{
54  /** Matrix-multiply \p input with the transpose of \pweights and
55  * write the results into \p output. */
57  const TMatrixT<Scalar_t> &input,
58  const TMatrixT<Scalar_t> &weights);
59  /** Add the vectors biases row-wise to the matrix output */
61  const TMatrixT<Scalar_t> &biases);
62  ///@}
63 
64  /** @name Backward Propagation
65  * Low-level functions required for the forward propagation of activations
66  * through the network.
67  */
68  ///@{
69  /** Perform the complete backward propagation step. If the provided
70  * \p activationGradientsBackward matrix is not empty, compute the
71  * gradients of the objective function with respect to the activations
72  * of the previous layer (backward direction).
73  * Also compute the weight and the bias gradients. Modifies the values
74  * in \p df and thus produces only a valid result, if it is applied the
75  * first time after the corresponding forward propagation has been per-
76  * formed. */
77  static void Backward(TMatrixT<Scalar_t> & activationGradientsBackward,
78  TMatrixT<Scalar_t> & weightGradients,
79  TMatrixT<Scalar_t> & biasGradients,
80  TMatrixT<Scalar_t> & df,
81  const TMatrixT<Scalar_t> & activationGradients,
82  const TMatrixT<Scalar_t> & weights,
83  const TMatrixT<Scalar_t> & activationBackward);
84  /** Adds a the elements in matrix B scaled by c to the elements in
85  * the matrix A. This is required for the weight update in the gradient
86  * descent step.*/
87  static void ScaleAdd(TMatrixT<Scalar_t> & A,
88  const TMatrixT<Scalar_t> & B,
89  Scalar_t beta = 1.0);
90 
91  static void Copy(TMatrixT<Scalar_t> & A,
92  const TMatrixT<Scalar_t> & B);
93  ///@}
94 
95  //____________________________________________________________________________
96  //
97  // Activation Functions
98  //____________________________________________________________________________
99 
100  /** @name Activation Functions
101  * For each activation function, the low-level interface contains two routines.
102  * One that applies the acitvation function to a matrix and one that evaluate
103  * the derivatives of the activation function at the elements of a given matrix
104  * and writes the results into the result matrix.
105  */
106  ///@{
107  static void Identity(TMatrixT<Real_t> & B);
108  static void IdentityDerivative(TMatrixT<Real_t> & B,
109  const TMatrixT<Real_t> & A);
110 
111  static void Relu(TMatrixT<Real_t> & B);
112  static void ReluDerivative(TMatrixT<Real_t> & B,
113  const TMatrixT<Real_t> & A);
114 
115  static void Sigmoid(TMatrixT<Real_t> & B);
116  static void SigmoidDerivative(TMatrixT<Real_t> & B,
117  const TMatrixT<Real_t> & A);
118 
119  static void Tanh(TMatrixT<Real_t> & B);
120  static void TanhDerivative(TMatrixT<Real_t> & B,
121  const TMatrixT<Real_t> & A);
122 
123  static void SymmetricRelu(TMatrixT<Real_t> & B);
125  const TMatrixT<Real_t> & A);
126 
127  static void SoftSign(TMatrixT<Real_t> & B);
128  static void SoftSignDerivative(TMatrixT<Real_t> & B,
129  const TMatrixT<Real_t> & A);
130 
131  static void Gauss(TMatrixT<Real_t> & B);
132  static void GaussDerivative(TMatrixT<Real_t> & B,
133  const TMatrixT<Real_t> & A);
134 
135  ///@}
136 
137  //____________________________________________________________________________
138  //
139  // Loss Functions
140  //____________________________________________________________________________
141 
142  /** @name Loss Functions
143  * Loss functions compute a scalar value given the \p output of the network
144  * for a given training input and the expected network prediction \p Y that
145  * quantifies the quality of the prediction. For each function also a routing
146  * that computes the gradients (suffixed by Gradients) must be provided for
147  * the starting of the backpropagation algorithm.
148  */
149  ///@{
150 
152  const TMatrixT<Real_t> &output);
154  const TMatrixT<Real_t> &Y,
155  const TMatrixT<Real_t> &output);
156 
157  /** Sigmoid transformation is implicitly applied, thus \p output should
158  * hold the linear activations of the last layer in the net. */
159  static Real_t CrossEntropy(const TMatrixT<Real_t> &Y,
160  const TMatrixT<Real_t> &output);
161 
162  static void CrossEntropyGradients(TMatrixT<Real_t> & dY,
163  const TMatrixT<Real_t> & Y,
164  const TMatrixT<Real_t> & output);
165  ///@}
166 
167  //____________________________________________________________________________
168  //
169  // Output Functions
170  //____________________________________________________________________________
171 
172  /** @name Output Functions
173  * Output functions transform the activations \p output of the
174  * output layer in the network to a valid prediction \p YHat for
175  * the desired usage of the network, e.g. the identity function
176  * for regression or the sigmoid transformation for two-class
177  * classification.
178  */
179  ///@{
180  static void Sigmoid(TMatrixT<Real_t> &YHat,
181  const TMatrixT<Real_t> & );
182  ///@}
183 
184  //____________________________________________________________________________
185  //
186  // Regularization
187  //____________________________________________________________________________
188 
189  /** @name Regularization
190  * For each regularization type two functions are required, one named
191  * <tt><Type>Regularization</tt> that evaluates the corresponding
192  * regularization functional for a given weight matrix and the
193  * <tt>Add<Type>RegularizationGradients</tt>, that adds the regularization
194  * component in the gradients to the provided matrix.
195  */
196  ///@{
197 
198  static Real_t L1Regularization(const TMatrixT<Real_t> & W);
200  const TMatrixT<Real_t> & W,
202 
203  static Real_t L2Regularization(const TMatrixT<Real_t> & W);
205  const TMatrixT<Real_t> & W,
207  ///@}
208 
209  //____________________________________________________________________________
210  //
211  // Initialization
212  //____________________________________________________________________________
213 
214  /** @name Initialization
215  * For each initialization method, one function in the low-level interface
216  * is provided. The naming scheme is <p>Initialize<Type></p> for a given
217  * initialization method Type.
218  */
219  ///@{
220 
221  static void InitializeGauss(TMatrixT<Real_t> & A);
222 
223  static void InitializeUniform(TMatrixT<Real_t> & A);
224 
225  static void InitializeIdentity(TMatrixT<Real_t> & A);
226 
227  static void InitializeZero(TMatrixT<Real_t> & A);
228 
229  ///@}
230 
231  //____________________________________________________________________________
232  //
233  // Dropout
234  //____________________________________________________________________________
235 
236  /** @name Dropout
237  */
238  ///@{
239 
240  /** Apply dropout with activation probability \p p to the given
241  * matrix \p A and scale the result by reciprocal of \p p. */
242  static void Dropout(TMatrixT<Real_t> & A, Real_t dropoutProbability);
243 
244  ///@}
245 };
246 
247 } // namespace DNN
248 } // namespace TMVA
249 
250 #endif
static double B[]
static Real_t CrossEntropy(const TMatrixT< Real_t > &Y, const TMatrixT< Real_t > &output)
Sigmoid transformation is implicitly applied, thus output should hold the linear activations of the l...
static void MultiplyTranspose(TMatrixT< Scalar_t > &output, const TMatrixT< Scalar_t > &input, const TMatrixT< Scalar_t > &weights)
Matrix-multiply input with the transpose of and write the results into output.
Definition: Propagation.cxx:25
static void Tanh(TMatrixT< Real_t > &B)
static Real_t MeanSquaredError(const TMatrixT< Real_t > &Y, const TMatrixT< Real_t > &output)
static void MeanSquaredErrorGradients(TMatrixT< Real_t > &dY, const TMatrixT< Real_t > &Y, const TMatrixT< Real_t > &output)
static void SigmoidDerivative(TMatrixT< Real_t > &B, const TMatrixT< Real_t > &A)
static void InitializeUniform(TMatrixT< Real_t > &A)
static void AddL2RegularizationGradients(TMatrixT< Real_t > &A, const TMatrixT< Real_t > &W, Real_t weightDecay)
static void SoftSignDerivative(TMatrixT< Real_t > &B, const TMatrixT< Real_t > &A)
static Real_t L2Regularization(const TMatrixT< Real_t > &W)
static void Backward(TMatrixT< Scalar_t > &activationGradientsBackward, TMatrixT< Scalar_t > &weightGradients, TMatrixT< Scalar_t > &biasGradients, TMatrixT< Scalar_t > &df, const TMatrixT< Scalar_t > &activationGradients, const TMatrixT< Scalar_t > &weights, const TMatrixT< Scalar_t > &activationBackward)
Perform the complete backward propagation step.
Definition: Propagation.cxx:44
static void ReluDerivative(TMatrixT< Real_t > &B, const TMatrixT< Real_t > &A)
static void AddRowWise(TMatrixT< Scalar_t > &output, const TMatrixT< Scalar_t > &biases)
Add the vectors biases row-wise to the matrix output.
Definition: Propagation.cxx:33
static double A[]
double beta(double x, double y)
Calculates the beta function.
TMatrixT.
Definition: TMatrixDfwd.h:24
static void CrossEntropyGradients(TMatrixT< Real_t > &dY, const TMatrixT< Real_t > &Y, const TMatrixT< Real_t > &output)
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
Definition: NeuralNet.icc:491
The reference architecture class.
Definition: Reference.h:37
static void TanhDerivative(TMatrixT< Real_t > &B, const TMatrixT< Real_t > &A)
static void SymmetricReluDerivative(TMatrixT< Real_t > &B, const TMatrixT< Real_t > &A)
static void ScaleAdd(TMatrixT< Scalar_t > &A, const TMatrixT< Scalar_t > &B, Scalar_t beta=1.0)
Adds a the elements in matrix B scaled by c to the elements in the matrix A.
Definition: Propagation.cxx:83
static void AddL1RegularizationGradients(TMatrixT< Real_t > &A, const TMatrixT< Real_t > &W, Real_t weightDecay)
static Real_t L1Regularization(const TMatrixT< Real_t > &W)
static void Gauss(TMatrixT< Real_t > &B)
static void InitializeZero(TMatrixT< Real_t > &A)
static void IdentityDerivative(TMatrixT< Real_t > &B, const TMatrixT< Real_t > &A)
static void GaussDerivative(TMatrixT< Real_t > &B, const TMatrixT< Real_t > &A)
static void InitializeIdentity(TMatrixT< Real_t > &A)
static void Copy(TMatrixT< Scalar_t > &A, const TMatrixT< Scalar_t > &B)
Definition: Propagation.cxx:95
float Real_t
Definition: RtypesCore.h:64
Abstract ClassifierFactory template that handles arbitrary types.
static void Dropout(TMatrixT< Real_t > &A, Real_t dropoutProbability)
Apply dropout with activation probability p to the given matrix A and scale the result by reciprocal ...
Definition: Dropout.cxx:29
static void Sigmoid(TMatrixT< Real_t > &B)
static void Identity(TMatrixT< Real_t > &B)
static void InitializeGauss(TMatrixT< Real_t > &A)
static void SymmetricRelu(TMatrixT< Real_t > &B)
static void output(int code)
Definition: gifencode.c:226
static void Relu(TMatrixT< Real_t > &B)
static void SoftSign(TMatrixT< Real_t > &B)