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