Logo ROOT   6.07/09
Reference Guide
Utility.h
Go to the documentation of this file.
1 #ifndef TMVA_TEST_DNN_UTILITY
2 #define TMVA_TEST_DNN_UTILITY
3 
4 #include <iostream>
5 #include <sstream>
6 #include <type_traits>
7 #include "stdlib.h"
8 #include "TRandom.h"
10 #include "TMVA/DNN/Functions.h"
11 #include "TMVA/DNN/Net.h"
12 
13 namespace TMVA
14 {
15 namespace DNN
16 {
17 
18 /** Construct a random linear neural network with up to five layers.*/
19 //______________________________________________________________________________
20 template <typename AArchitecture>
22 {
23  int nlayers = rand() % 5 + 1;
24 
25  std::vector<EActivationFunction> ActivationFunctions
27 
28  for (int i = 0; i < nlayers; i++) {
29  int width = rand() % 20 + 1;
31  ActivationFunctions[rand() % ActivationFunctions.size()];
32  net.AddLayer(width, f);
33  }
34 }
35 
36 /*! Set matrix to the identity matrix */
37 //______________________________________________________________________________
38 template <typename AMatrix>
39 void identityMatrix(AMatrix &X)
40 {
41  size_t m, n;
42  m = X.GetNrows();
43  n = X.GetNcols();
44 
45 
46  for (size_t i = 0; i < m; i++) {
47  for (size_t j = 0; j < n; j++) {
48  X(i,j) = 0.0;
49  }
50  if (i < n) {
51  X(i,i) = 1.0;
52  }
53  }
54 }
55 
56 /*! Fill matrix with random, Gaussian-distributed values. */
57 //______________________________________________________________________________
58 template <typename AMatrix>
59 void randomMatrix(AMatrix &X)
60 {
61  size_t m,n;
62  m = X.GetNrows();
63  n = X.GetNcols();
64 
65  TRandom rand(clock());
66 
67  Double_t sigma = sqrt(10.0);
68 
69  for (size_t i = 0; i < m; i++) {
70  for (size_t j = 0; j < n; j++) {
71  X(i,j) = rand.Gaus(0.0, sigma);
72  }
73  }
74 }
75 
76 /*! Generate a random batch as input for a neural net. */
77 //______________________________________________________________________________
78 template <typename AMatrix>
79 void randomBatch(AMatrix &X)
80 {
81  randomMatrix(X);
82 }
83 
84 /*! Generate a random batch as input for a neural net. */
85 //______________________________________________________________________________
86 template <typename AMatrix>
87 void copyMatrix(AMatrix &X, const AMatrix &Y)
88 {
89  size_t m,n;
90  m = X.GetNrows();
91  n = X.GetNcols();
92 
93  for (size_t i = 0; i < m; i++) {
94  for (size_t j = 0; j < n; j++) {
95  X(i,j) = Y(i,j);
96  }
97  }
98 }
99 
100 /*! Apply functional to each element in the matrix. */
101 //______________________________________________________________________________
102 template <typename AMatrix, typename F>
103 void applyMatrix(AMatrix &X, F f)
104 {
105  size_t m,n;
106  m = X.GetNrows();
107  n = X.GetNcols();
108 
109  for (size_t i = 0; i < m; i++) {
110  for (size_t j = 0; j < n; j++) {
111  X(i,j) = f(X(i,j));
112  }
113  }
114 }
115 
116 /*! Combine elements of two given matrices into a single matrix using
117  * the given function f. */
118 //______________________________________________________________________________
119 template <typename AMatrix, typename F>
120 void zipWithMatrix(AMatrix &Z,
121  F f,
122  const AMatrix &X,
123  const AMatrix &Y)
124 {
125  size_t m,n;
126  m = X.GetNrows();
127  n = X.GetNcols();
128 
129  for (size_t i = 0; i < m; i++) {
130  for (size_t j = 0; j < n; j++) {
131  Z(i,j) = f(X(i,j), Y(i,j));
132  }
133  }
134 }
135 
136 /** Generate a random batch as input for a neural net. */
137 //______________________________________________________________________________
138 template <typename AMatrix, typename AFloat, typename F>
139 AFloat reduce(F f, AFloat start, const AMatrix &X)
140 {
141  size_t m,n;
142  m = X.GetNrows();
143  n = X.GetNcols();
144 
145  AFloat result = start;
146 
147  for (size_t i = 0; i < m; i++) {
148  for (size_t j = 0; j < n; j++) {
149  result = f(result, X(i,j));
150  }
151  }
152  return result;
153 }
154 
155 /** Apply function to matrix element-wise and compute the mean of the resulting
156  * element values */
157 //______________________________________________________________________________
158 template <typename AMatrix, typename AFloat, typename F>
159 AFloat reduceMean(F f, AFloat start, const AMatrix &X)
160 {
161  size_t m,n;
162  m = X.GetNrows();
163  n = X.GetNcols();
164 
165  AFloat result = start;
166 
167  for (size_t i = 0; i < m; i++) {
168  for (size_t j = 0; j < n; j++) {
169  result = f(result, X(i,j));
170  }
171  }
172  return result / (AFloat) (m * n);
173 }
174 
175 /** Compute the relative error of x and y normalized by y. Specialized for
176  * float and double to make sure both arguments are above expected machine
177  * precision (1e-5 and 1e-10). */
178 //______________________________________________________________________________
179 template <typename AFloat>
180 inline AFloat relativeError(const AFloat &x,
181  const AFloat &y);
182 
183 
184 //______________________________________________________________________________
185 template <>
187  const Double_t &y)
188 {
189  if ((std::abs(x) > 1e-10) && (std::abs(y) > 1e-10)) {
190  return std::fabs((x - y) / y);
191  } else {
192  return std::fabs(x - y);
193  }
194 }
195 
196 //______________________________________________________________________________
197 template <>
198 inline Real_t relativeError(const Real_t &x,
199  const Real_t &y)
200 {
201  if ((std::abs(x) > 1e-5) && (std::abs(y) > 1e-5)) {
202  return std::fabs((x - y) / y);
203  } else {
204  return std::fabs(x - y);
205  }
206 }
207 
208 /*! Compute the maximum, element-wise relative error of the matrices
209 * X and Y normalized by the element of Y. Protected against division
210 * by zero. */
211 //______________________________________________________________________________
212 template <typename AMatrix>
213 auto maximumRelativeError(const AMatrix &X,
214  const AMatrix &Y)
215 -> decltype(X(0,0))
216 {
217 
218  using AFloat = decltype(X(0,0));
219 
220  size_t m,n;
221  m = X.GetNrows();
222  n = X.GetNcols();
223 
224  AFloat maximumError = 0.0;
225 
226  for (size_t i = 0; i < m; i++) {
227  for (size_t j = 0; j < n; j++) {
228  AFloat error = relativeError(X(i,j), Y(i,j));
229  maximumError = std::max(error, maximumError);
230  }
231  }
232  return maximumError;
233 }
234 
235 /*! Numerically compute the derivative of the functional f using finite
236 * differences. */
237 //______________________________________________________________________________
238 template <typename F, typename AFloat>
239 inline AFloat finiteDifference(F f, AFloat dx)
240 {
241  return f(dx) - f(0.0 - dx);
242 }
243 
244 /*! Color code error. */
245 //______________________________________________________________________________
246 template <typename AFloat>
247 std::string print_error(AFloat &e)
248 {
249  std::ostringstream out{};
250 
251  out << ("\e[");
252 
253  if (e > 1e-5)
254  out << "31m";
255  else if (e > 1e-9)
256  out << "33m";
257  else
258  out << "32m";
259 
260  out << e;
261  out << "\e[39m";
262 
263  return out.str();
264 }
265 
266 }
267 }
268 
269 #endif
void randomMatrix(AMatrix &X)
Fill matrix with random, Gaussian-distributed values.
Definition: Utility.h:59
auto maximumRelativeError(const AMatrix &X, const AMatrix &Y) -> decltype(X(0, 0))
Compute the maximum, element-wise relative error of the matrices X and Y normalized by the element of...
Definition: Utility.h:213
void applyMatrix(AMatrix &X, F f)
Apply functional to each element in the matrix.
Definition: Utility.h:103
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
void constructRandomLinearNet(TNet< AArchitecture > &net)
Construct a random linear neural network with up to five layers.
Definition: Utility.h:21
void identityMatrix(AMatrix &X)
Set matrix to the identity matrix.
Definition: Utility.h:39
void AddLayer(size_t width, EActivationFunction f, Scalar_t dropoutProbability=1.0)
Add a layer of the given size to the neural net.
Definition: Net.h:224
double sqrt(double)
AFloat reduceMean(F f, AFloat start, const AMatrix &X)
Apply function to matrix element-wise and compute the mean of the resulting element values...
Definition: Utility.h:159
Double_t x[n]
Definition: legend1.C:17
std::string print_error(AFloat &e)
Color code error.
Definition: Utility.h:247
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:31
AFloat finiteDifference(F f, AFloat dx)
Numerically compute the derivative of the functional f using finite differences.
Definition: Utility.h:239
void zipWithMatrix(AMatrix &Z, F f, const AMatrix &X, const AMatrix &Y)
Combine elements of two given matrices into a single matrix using the given function f...
Definition: Utility.h:120
Generic neural network class.
Definition: Net.h:49
const Double_t sigma
#define F(x, y, z)
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
void randomBatch(AMatrix &X)
Generate a random batch as input for a neural net.
Definition: Utility.h:79
TMarker * m
Definition: textangle.C:8
void copyMatrix(AMatrix &X, const AMatrix &Y)
Generate a random batch as input for a neural net.
Definition: Utility.h:87
double f(double x)
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
float Real_t
Definition: RtypesCore.h:64
Abstract ClassifierFactory template that handles arbitrary types.
AFloat relativeError(const AFloat &x, const AFloat &y)
Compute the relative error of x and y normalized by y.
EActivationFunction
Enum that represents layer activation functions.
Definition: Functions.h:31
double result[121]
AFloat reduce(F f, AFloat start, const AMatrix &X)
Generate a random batch as input for a neural net.
Definition: Utility.h:139
const Int_t n
Definition: legend1.C:16