Logo ROOT   6.08/07
Reference Guide
TestDataLoader.h
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 12/07/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 // Generic test for DataLoader implementations. //
14 //////////////////////////////////////////////////
15 
16 #include "TMVA/DNN/Net.h"
17 #include "TMVA/DNN/DataLoader.h"
18 #include "Utility.h"
19 
20 namespace TMVA
21 {
22 namespace DNN
23 {
24 
25 /** Test that the data loader loads all data in the data set by summing
26  * up all elements batch wise and comparing to the result over the complete
27  * data set. */
28 //______________________________________________________________________________
29 template <typename Architecture_t>
30 auto testSum()
31  -> typename Architecture_t::Scalar_t
32 {
33  using Scalar_t = typename Architecture_t::Scalar_t;
34  using Matrix_t = typename Architecture_t::Matrix_t;
35  using DataLoader_t = TDataLoader<MatrixInput_t, Architecture_t>;
36 
37  size_t nSamples = 10000;
38  TMatrixT<Double_t> X(nSamples,1);
39  randomMatrix(X);
40  for (size_t i = 0; i < 10000; i++) {
41  X(i,0) = i;
42  }
43  MatrixInput_t input(X, X);
44  DataLoader_t loader(input, nSamples, 5, 1, 1);
45 
46  Matrix_t XArch(X), Sum(1,1), SumTotal(1,1);
47  Scalar_t sum = 0.0, sumTotal = 0.0;
48 
49  for (auto b : loader) {
50  Architecture_t::SumColumns(Sum, b.GetInput());
51  sum += Sum(0, 0);
52  }
53 
54  Architecture_t::SumColumns(SumTotal, XArch);
55  sumTotal = SumTotal(0,0);
56 
57  return fabs(sumTotal - sum) / sumTotal;
58 }
59 
60 /** Test the data loader by loading identical input and output data, running it
61  * through an identity neural network and computing the the mean squared error.
62  * Should obviously be zero. */
63 //______________________________________________________________________________
64 template <typename Architecture_t>
66  -> typename Architecture_t::Scalar_t
67 {
68  using Scalar_t = typename Architecture_t::Scalar_t;
69  using Net_t = TNet<Architecture_t>;
70  using DataLoader_t = TDataLoader<MatrixInput_t, Architecture_t>;
71 
72  TMatrixT<Double_t> X(2000, 100); randomMatrix(X);
73  MatrixInput_t input(X, X);
74  DataLoader_t loader(input, 2000, 20, 100, 100);
75 
76  Net_t net(20, 100, ELossFunction::kMeanSquaredError);
77  net.AddLayer(100, EActivationFunction::kIdentity);
78  net.AddLayer(100, EActivationFunction::kIdentity);
79  net.Initialize(EInitialization::kIdentity);
80 
81  Scalar_t maximumError = 0.0;
82  for (auto b : loader) {
83  auto inputMatrix = b.GetInput();
84  auto outputMatrix = b.GetOutput();
85  Scalar_t error = net.Loss(inputMatrix, outputMatrix);
86  maximumError = std::max(error, maximumError);
87  }
88 
89  return maximumError;
90 }
91 
92 } // namespace DNN
93 } // namespace TMVA
static long int sum(long int i)
Definition: Factory.cxx:1786
void randomMatrix(AMatrix &X)
Fill matrix with random, Gaussian-distributed values.
Definition: Utility.h:60
auto testSum() -> typename Architecture_t::Scalar_t
Test that the data loader loads all data in the data set by summing up all elements batch wise and co...
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
TDataLoader.
Definition: DataLoader.h:72
Double_t Sum(const double *x, const double *p)
Abstract ClassifierFactory template that handles arbitrary types.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
auto testIdentity() -> typename Architecture_t::Scalar_t
Test the data loader by loading identical input and output data, running it through an identity neura...
std::pair< const TMatrixT< Double_t > &, const TMatrixT< Double_t > & > MatrixInput_t
Definition: DataLoader.h:34