Logo ROOT   6.10/09
Reference Guide
TestMinimization.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Simon Pfreundschuh
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 // Test Standard Minimizer //
14 // //
15 // This test trains a linear neural network on a linear function //
16 // F(x) = W * x and computes the relative error between the matrix //
17 // W' representing the linear function learned by the net to the //
18 // orignal matrix W. //
19 /////////////////////////////////////////////////////////////////////
20 
21 #include "TMatrix.h"
22 #include "TMVA/DNN/Minimizers.h"
23 #include "TMVA/DNN/Net.h"
24 #include "Utility.h"
25 
26 using namespace TMVA::DNN;
27 
28 /** Train a linear neural network on a randomly generated linear mapping
29  * from a 20-dimensional input space to a 1-dimensional output space.
30  * Returns the error of the response of the network to the input containing
31  * only ones to the 1x20 matrix generating the mapping.
32  */
33 template <typename Architecture>
35  -> typename Architecture::Scalar_t
36 {
37  using Matrix_t = typename Architecture::Matrix_t;
38  using Net_t = TNet<Architecture>;
39 
40  size_t nSamples = 10000;
41  size_t nFeatures = 20;
42  size_t batchSize = 256;
43 
44  TMatrixT<Double_t> XTrain(nSamples, nFeatures), YTrain(nSamples, 1),
45  XTest(batchSize, nFeatures), YTest(batchSize, 1), W(nFeatures, 1);
46 
47  randomMatrix(W);
48  randomMatrix(XTrain);
49  randomMatrix(XTest);
50  YTrain.Mult(XTrain, W);
51  YTest.Mult(XTest, W);
52 
53  Net_t net(batchSize, nFeatures, ELossFunction::kMeanSquaredError);
54  net.AddLayer(64, EActivationFunction::kIdentity);
55  net.AddLayer(64, EActivationFunction::kIdentity);
56  net.AddLayer(64, EActivationFunction::kIdentity);
57  net.AddLayer(1, EActivationFunction::kIdentity);
58  net.Initialize(EInitialization::kGauss);
59 
60  TGradientDescent<Architecture> minimizer(0.0001, 5, 1);
61  MatrixInput_t trainingData(XTrain, YTrain);
62  MatrixInput_t testData(XTest, YTest);
63  minimizer.TrainMomentum(trainingData, nSamples, testData, batchSize, net, 0.8, 1);
64 
65  TMatrixT<Double_t> I(nFeatures, nFeatures);
66  for (size_t i = 0; i < nFeatures; i++) {
67  I(i, i) = 1.0;
68  }
69  Matrix_t Id(I);
70  auto clone = net.CreateClone(nFeatures);
71  clone.Forward(Id);
72  TMatrixT<Double_t> Y(clone.GetOutput());
73 
74  return maximumRelativeError(Y, W);
75 }
76 
77 /** Similar to testMinimization() as the function above except that
78  * it uses momentum for the training */
79 template <typename Architecture>
81  -> typename Architecture::Scalar_t
82 {
83  using Matrix_t = typename Architecture::Matrix_t;
84  using Net_t = TNet<Architecture>;
85 
86  size_t nSamples = 10000;
87  size_t nFeatures = 20;
88  size_t batchSize = 256;
89 
90  TMatrixT<Double_t> XTrain(nSamples, nFeatures), YTrain(nSamples, 1),
91  XTest(batchSize, nFeatures), YTest(batchSize, 1), W(nFeatures, 1);
92 
93  randomMatrix(W);
94  randomMatrix(XTrain);
95  randomMatrix(XTest);
96  YTrain.Mult(XTrain, W);
97  YTest.Mult(XTest, W);
98 
99  Net_t net(batchSize, nFeatures, ELossFunction::kMeanSquaredError);
100  net.AddLayer(64, EActivationFunction::kIdentity);
101  net.AddLayer(64, EActivationFunction::kIdentity);
102  net.AddLayer(64, EActivationFunction::kIdentity);
103  net.AddLayer(1, EActivationFunction::kIdentity);
104  net.Initialize(EInitialization::kGauss);
105 
106  TGradientDescent<Architecture> minimizer(0.0001, 5, 5);
107  MatrixInput_t trainingData(XTrain, YTrain);
108  MatrixInput_t testData(XTest, YTest);
109  minimizer.TrainMomentum(trainingData, nSamples, testData, batchSize, net, 0.9, 1);
110 
111  TMatrixT<Double_t> I(nFeatures, nFeatures);
112  for (size_t i = 0; i < nFeatures; i++) {
113  I(i, i) = 1.0;
114  }
115  Matrix_t Id(I);
116  auto clone = net.CreateClone(nFeatures);
117  clone.Forward(Id);
118  TMatrixT<Double_t> Y(clone.GetOutput());
119 
120  return maximumRelativeError(Y, W);
121 }
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
Scalar_t TrainMomentum(const Data_t &TrainingDataIn, size_t nTrainingSamples, const Data_t &TestDataIn, size_t nTestSamples, Net_t &net, Scalar_t momentum, size_t nThreads=1)
Same as Train(...) but uses the given momentum.
Definition: Blas.h:58
Generic neural network class.
Definition: Net.h:49
auto testMinimization() -> typename Architecture::Scalar_t
Train a linear neural network on a randomly generated linear mapping from a 20-dimensional input spac...
auto testMinimizationMomentum() -> typename Architecture::Scalar_t
Similar to testMinimization() as the function above except that it uses momentum for the training...
#define I(x, y, z)
std::pair< const TMatrixT< Double_t > &, const TMatrixT< Double_t > & > MatrixInput_t
Definition: DataLoader.h:35