Logo ROOT   6.16/01
Reference Guide
MethodDL.h
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn:$Id$
2// Author: Vladimir Ilievski, Saurav Shekhar
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : MethodDL *
8 * Web : http://tmva.sourceforge.net *
9 * *
10 * Description: *
11 * Deep Neural Network Method *
12 * *
13 * Authors (alphabetical): *
14 * Vladimir Ilievski <ilievski.vladimir@live.com> - CERN, Switzerland *
15 * Saurav Shekhar <sauravshekhar01@gmail.com> - ETH Zurich, Switzerland *
16 * *
17 * Copyright (c) 2005-2015: *
18 * CERN, Switzerland *
19 * U. of Victoria, Canada *
20 * MPI-K Heidelberg, Germany *
21 * U. of Bonn, Germany *
22 * *
23 * Redistribution and use in source and binary forms, with or without *
24 * modification, are permitted according to the terms listed in LICENSE *
25 * (http://tmva.sourceforge.net/LICENSE) *
26 **********************************************************************************/
27
28#ifndef ROOT_TMVA_MethodDL
29#define ROOT_TMVA_MethodDL
30
31//////////////////////////////////////////////////////////////////////////
32// //
33// MethodDL //
34// //
35// Method class for all Deep Learning Networks //
36// //
37//////////////////////////////////////////////////////////////////////////
38
39#include "TString.h"
40
41#include "TMVA/MethodBase.h"
42#include "TMVA/Types.h"
43
45
46#ifdef R__HAS_TMVACPU
48#endif
49
50#ifdef R__HAS_TMVAGPU
52#endif
53
54#include "TMVA/DNN/Functions.h"
55#include "TMVA/DNN/DeepNet.h"
56
57#include <vector>
58
59namespace TMVA {
60
61/*! All of the options that can be specified in the training string */
63 size_t batchSize;
66 size_t maxEpochs;
72 std::vector<Double_t> dropoutProbabilities;
74};
75
76
77class MethodDL : public MethodBase {
78
79private:
80 // Key-Value vector type, contining the values for the training options
81 using KeyValueVector_t = std::vector<std::map<TString, TString>>;
82// #ifdef R__HAS_TMVAGPU
83// using ArchitectureImpl_t = TMVA::DNN::TCuda<Double_t>;
84// #else
85// do not use arch GPU for evaluation. It is too slow for batch size=1
86#ifdef R__HAS_TMVACPU
88#else
90#endif
91//#endif
95
96 std::vector<MatrixImpl_t> fXInput; // input tensor used to evaluate fNet
97 std::unique_ptr<MatrixImpl_t> fYHat; // output prediction matrix of fNet
98 std::unique_ptr<DeepNetImpl_t> fNet;
99
100
101 /*! The option handling methods */
102 void DeclareOptions();
103 void ProcessOptions();
104
105 void Init();
106
107 // Function to parse the layout of the input
108 void ParseInputLayout();
109 void ParseBatchLayout();
110
111 /*! After calling the ProcesOptions(), all of the options are parsed,
112 * so using the parsed options, and given the architecture and the
113 * type of the layers, we build the Deep Network passed as
114 * a reference in the function. */
115 template <typename Architecture_t, typename Layer_t>
117 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets);
118
119 template <typename Architecture_t, typename Layer_t>
121 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
122
123 template <typename Architecture_t, typename Layer_t>
125 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
126
127 template <typename Architecture_t, typename Layer_t>
129 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
130 TString delim);
131
132 template <typename Architecture_t, typename Layer_t>
134 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
135 TString delim);
136
137 template <typename Architecture_t, typename Layer_t>
139 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
140
141 template <typename Architecture_t, typename Layer_t>
143 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
144
145 /// train of deep neural network using the defined architecture
146 template <typename Architecture_t>
147 void TrainDeepNet();
148
149 /// perform prediction of the deep neural network
150 /// using batches (called by GetMvaValues)
151 template <typename Architecture_t>
152 std::vector<Double_t> PredictDeepNet(Long64_t firstEvt, Long64_t lastEvt, size_t batchSize, Bool_t logProgress);
153
154 /// parce the validation string and return the number of event data used for validation
156
157
158 size_t fInputDepth; ///< The depth of the input.
159 size_t fInputHeight; ///< The height of the input.
160 size_t fInputWidth; ///< The width of the input.
161
162 size_t fBatchDepth; ///< The depth of the batch used to train the deep net.
163 size_t fBatchHeight; ///< The height of the batch used to train the deep net.
164 size_t fBatchWidth; ///< The width of the batch used to train the deep net.
165
166 size_t fRandomSeed; ///<The random seed used to initialize the weights and shuffling batches (default is zero)
167
168 DNN::EInitialization fWeightInitialization; ///< The initialization method
169 DNN::EOutputFunction fOutputFunction; ///< The output function for making the predictions
170 DNN::ELossFunction fLossFunction; ///< The loss function
171
172 TString fInputLayoutString; ///< The string defining the layout of the input
173 TString fBatchLayoutString; ///< The string defining the layout of the batch
174 TString fLayoutString; ///< The string defining the layout of the deep net
175 TString fErrorStrategy; ///< The string defining the error strategy for training
176 TString fTrainingStrategyString; ///< The string defining the training strategy
177 TString fWeightInitializationString; ///< The string defining the weight initialization method
178 TString fArchitectureString; ///< The string defining the architecure: CPU or GPU
179 TString fNumValidationString; ///< The string defining the number (or percentage) of training data used for validation
181 bool fBuildNet; ///< Flag to control whether to build fNet, the stored network used for the evaluation
182
183 KeyValueVector_t fSettings; ///< Map for the training strategy
184 std::vector<TTrainingSettings> fTrainingSettings; ///< The vector defining each training strategy
185
186 ClassDef(MethodDL, 0);
187
188protected:
189 // provide a help message
190 void GetHelpMessage() const;
191
192 virtual std::vector<Double_t> GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress);
193
194
195public:
196 /*! Constructor */
197 MethodDL(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption);
198
199 /*! Constructor */
200 MethodDL(DataSetInfo &theData, const TString &theWeightFile);
201
202 /*! Virtual Destructor */
203 virtual ~MethodDL();
204
205 /*! Function for parsing the training settings, provided as a string
206 * in a key-value form. */
207 KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim);
208
209 /*! Check the type of analysis the deep learning network can do */
210 Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets);
211
212 /*! Methods for training the deep learning network */
213 void Train();
214
215 Double_t GetMvaValue(Double_t *err = 0, Double_t *errUpper = 0);
216 virtual const std::vector<Float_t>& GetRegressionValues();
217 virtual const std::vector<Float_t>& GetMulticlassValues();
218
219 /*! Methods for writing and reading weights */
221 void AddWeightsXMLTo(void *parent) const;
222 void ReadWeightsFromXML(void *wghtnode);
223 void ReadWeightsFromStream(std::istream &);
224
225 /* Create ranking */
226 const Ranking *CreateRanking();
227
228 /* Getters */
229 size_t GetInputDepth() const { return fInputDepth; }
230 size_t GetInputHeight() const { return fInputHeight; }
231 size_t GetInputWidth() const { return fInputWidth; }
232
233 size_t GetBatchDepth() const { return fBatchDepth; }
234 size_t GetBatchHeight() const { return fBatchHeight; }
235 size_t GetBatchWidth() const { return fBatchWidth; }
236
237 const DeepNetImpl_t & GetDeepNet() const { return *fNet; }
238
242
250
251 const std::vector<TTrainingSettings> &GetTrainingSettings() const { return fTrainingSettings; }
252 std::vector<TTrainingSettings> &GetTrainingSettings() { return fTrainingSettings; }
255
256 /** Setters */
257 void SetInputDepth(size_t inputDepth) { fInputDepth = inputDepth; }
258 void SetInputHeight(size_t inputHeight) { fInputHeight = inputHeight; }
259 void SetInputWidth(size_t inputWidth) { fInputWidth = inputWidth; }
260
261 void SetBatchDepth(size_t batchDepth) { fBatchDepth = batchDepth; }
262 void SetBatchHeight(size_t batchHeight) { fBatchHeight = batchHeight; }
263 void SetBatchWidth(size_t batchWidth) { fBatchWidth = batchWidth; }
264
266 {
267 fWeightInitialization = weightInitialization;
268 }
269 void SetOutputFunction(DNN::EOutputFunction outputFunction) { fOutputFunction = outputFunction; }
270 void SetErrorStrategyString(TString errorStrategy) { fErrorStrategy = errorStrategy; }
271 void SetTrainingStrategyString(TString trainingStrategyString) { fTrainingStrategyString = trainingStrategyString; }
272 void SetWeightInitializationString(TString weightInitializationString)
273 {
274 fWeightInitializationString = weightInitializationString;
275 }
276 void SetArchitectureString(TString architectureString) { fArchitectureString = architectureString; }
277 void SetLayoutString(TString layoutString) { fLayoutString = layoutString; }
278};
279
280} // namespace TMVA
281
282#endif
unsigned int UInt_t
Definition: RtypesCore.h:42
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
long long Long64_t
Definition: RtypesCore.h:69
#define ClassDef(name, id)
Definition: Rtypes.h:324
int type
Definition: TGX11.cxx:120
The TCpu architecture class.
Definition: Cpu.h:45
Generic Deep Neural Network class.
Definition: DeepNet.h:74
The reference architecture class.
Definition: Reference.h:45
TMatrixT< AReal > Matrix_t
Definition: Reference.h:51
Class that contains all the data information.
Definition: DataSetInfo.h:60
Virtual base Class for all MVA method.
Definition: MethodBase.h:109
virtual void ReadWeightsFromStream(std::istream &)=0
void SetInputWidth(size_t inputWidth)
Definition: MethodDL.h:259
KeyValueVector_t & GetKeyValueSettings()
Definition: MethodDL.h:254
size_t fBatchHeight
The height of the batch used to train the deep net.
Definition: MethodDL.h:163
void GetHelpMessage() const
Definition: MethodDL.cxx:2068
DNN::ELossFunction fLossFunction
The loss function.
Definition: MethodDL.h:170
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodDL.cxx:1767
size_t fInputHeight
The height of the input.
Definition: MethodDL.h:159
TString GetErrorStrategyString() const
Definition: MethodDL.h:246
void SetErrorStrategyString(TString errorStrategy)
Definition: MethodDL.h:270
TString fLayoutString
The string defining the layout of the deep net.
Definition: MethodDL.h:174
std::vector< TTrainingSettings > & GetTrainingSettings()
Definition: MethodDL.h:252
std::unique_ptr< MatrixImpl_t > fYHat
Definition: MethodDL.h:97
void Train()
Methods for training the deep learning network.
Definition: MethodDL.cxx:1453
size_t GetBatchHeight() const
Definition: MethodDL.h:234
TString GetTrainingStrategyString() const
Definition: MethodDL.h:247
virtual std::vector< Double_t > GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress)
Evaluate the DeepNet on a vector of input values stored in the TMVA Event class.
Definition: MethodDL.cxx:1793
void ParseRnnLayer(DNN::TDeepNet< Architecture_t, Layer_t > &deepNet, std::vector< DNN::TDeepNet< Architecture_t, Layer_t > > &nets, TString layerString, TString delim)
Pases the layer string and creates the appropriate rnn layer.
Definition: MethodDL.cxx:837
TString fWeightInitializationString
The string defining the weight initialization method.
Definition: MethodDL.h:177
void ParseMaxPoolLayer(DNN::TDeepNet< Architecture_t, Layer_t > &deepNet, std::vector< DNN::TDeepNet< Architecture_t, Layer_t > > &nets, TString layerString, TString delim)
Pases the layer string and creates the appropriate max pool layer.
Definition: MethodDL.cxx:715
size_t fRandomSeed
The random seed used to initialize the weights and shuffling batches (default is zero)
Definition: MethodDL.h:166
TString fArchitectureString
The string defining the architecure: CPU or GPU.
Definition: MethodDL.h:178
void Init()
default initializations
Definition: MethodDL.cxx:392
MethodDL(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption)
Constructor.
Definition: MethodDL.cxx:917
void TrainDeepNet()
train of deep neural network using the defined architecture
Definition: MethodDL.cxx:1058
const std::vector< TTrainingSettings > & GetTrainingSettings() const
Definition: MethodDL.h:251
DNN::EOutputFunction GetOutputFunction() const
Definition: MethodDL.h:240
void ParseLstmLayer(DNN::TDeepNet< Architecture_t, Layer_t > &deepNet, std::vector< DNN::TDeepNet< Architecture_t, Layer_t > > &nets, TString layerString, TString delim)
Pases the layer string and creates the appropriate lstm layer.
Definition: MethodDL.cxx:898
void SetInputDepth(size_t inputDepth)
Setters.
Definition: MethodDL.h:257
void ParseDenseLayer(DNN::TDeepNet< Architecture_t, Layer_t > &deepNet, std::vector< DNN::TDeepNet< Architecture_t, Layer_t > > &nets, TString layerString, TString delim)
Pases the layer string and creates the appropriate dense layer.
Definition: MethodDL.cxx:539
UInt_t GetNumValidationSamples()
parce the validation string and return the number of event data used for validation
TString GetBatchLayoutString() const
Definition: MethodDL.h:244
void SetArchitectureString(TString architectureString)
Definition: MethodDL.h:276
void ProcessOptions()
Definition: MethodDL.cxx:219
size_t fBatchWidth
The width of the batch used to train the deep net.
Definition: MethodDL.h:164
size_t GetInputDepth() const
Definition: MethodDL.h:229
virtual const std::vector< Float_t > & GetRegressionValues()
Definition: MethodDL.cxx:1730
std::unique_ptr< DeepNetImpl_t > fNet
Definition: MethodDL.h:98
TString GetWeightInitializationString() const
Definition: MethodDL.h:248
TString GetInputLayoutString() const
Definition: MethodDL.h:243
void SetBatchHeight(size_t batchHeight)
Definition: MethodDL.h:262
std::vector< MatrixImpl_t > fXInput
Definition: MethodDL.h:96
size_t GetInputHeight() const
Definition: MethodDL.h:230
TString GetArchitectureString() const
Definition: MethodDL.h:249
void ParseBatchLayout()
Parse the input layout.
Definition: MethodDL.cxx:445
void ReadWeightsFromStream(std::istream &)
Definition: MethodDL.cxx:2056
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodDL.cxx:1884
TString fNumValidationString
The string defining the number (or percentage) of training data used for validation.
Definition: MethodDL.h:179
const KeyValueVector_t & GetKeyValueSettings() const
Definition: MethodDL.h:253
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDL.h:81
DNN::EOutputFunction fOutputFunction
The output function for making the predictions.
Definition: MethodDL.h:169
DNN::EInitialization fWeightInitialization
The initialization method.
Definition: MethodDL.h:168
void SetOutputFunction(DNN::EOutputFunction outputFunction)
Definition: MethodDL.h:269
size_t GetBatchDepth() const
Definition: MethodDL.h:233
std::vector< TTrainingSettings > fTrainingSettings
The vector defining each training strategy.
Definition: MethodDL.h:184
size_t GetInputWidth() const
Definition: MethodDL.h:231
DNN::ELossFunction GetLossFunction() const
Definition: MethodDL.h:241
TString fBatchLayoutString
The string defining the layout of the batch.
Definition: MethodDL.h:173
void SetWeightInitializationString(TString weightInitializationString)
Definition: MethodDL.h:272
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
Check the type of analysis the deep learning network can do.
Definition: MethodDL.cxx:985
void ParseConvLayer(DNN::TDeepNet< Architecture_t, Layer_t > &deepNet, std::vector< DNN::TDeepNet< Architecture_t, Layer_t > > &nets, TString layerString, TString delim)
Pases the layer string and creates the appropriate convolutional layer.
Definition: MethodDL.cxx:616
void ParseReshapeLayer(DNN::TDeepNet< Architecture_t, Layer_t > &deepNet, std::vector< DNN::TDeepNet< Architecture_t, Layer_t > > &nets, TString layerString, TString delim)
Pases the layer string and creates the appropriate reshape layer.
Definition: MethodDL.cxx:776
TString fTrainingStrategyString
The string defining the training strategy.
Definition: MethodDL.h:176
void SetTrainingStrategyString(TString trainingStrategyString)
Definition: MethodDL.h:271
const Ranking * CreateRanking()
Definition: MethodDL.cxx:2061
void SetLayoutString(TString layoutString)
Definition: MethodDL.h:277
const DeepNetImpl_t & GetDeepNet() const
Definition: MethodDL.h:237
void SetInputHeight(size_t inputHeight)
Definition: MethodDL.h:258
void SetBatchDepth(size_t batchDepth)
Definition: MethodDL.h:261
KeyValueVector_t fSettings
Map for the training strategy.
Definition: MethodDL.h:183
size_t fInputWidth
The width of the input.
Definition: MethodDL.h:160
KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim)
Function for parsing the training settings, provided as a string in a key-value form.
Definition: MethodDL.cxx:946
size_t fInputDepth
The depth of the input.
Definition: MethodDL.h:158
void SetBatchWidth(size_t batchWidth)
Definition: MethodDL.h:263
std::vector< Double_t > PredictDeepNet(Long64_t firstEvt, Long64_t lastEvt, size_t batchSize, Bool_t logProgress)
perform prediction of the deep neural network using batches (called by GetMvaValues)
Definition: MethodDL.cxx:1601
DNN::EInitialization GetWeightInitialization() const
Definition: MethodDL.h:239
void SetWeightInitialization(DNN::EInitialization weightInitialization)
Definition: MethodDL.h:265
TString GetLayoutString() const
Definition: MethodDL.h:245
size_t fBatchDepth
The depth of the batch used to train the deep net.
Definition: MethodDL.h:162
size_t GetBatchWidth() const
Definition: MethodDL.h:235
void AddWeightsXMLTo(void *parent) const
Definition: MethodDL.cxx:1823
typename ArchitectureImpl_t::Matrix_t MatrixImpl_t
Definition: MethodDL.h:93
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Definition: MethodDL.cxx:1510
virtual ~MethodDL()
Virtual Destructor.
Definition: MethodDL.cxx:939
typename ArchitectureImpl_t::Scalar_t ScalarImpl_t
Definition: MethodDL.h:94
void ParseInputLayout()
Parse the input layout.
Definition: MethodDL.cxx:399
bool fBuildNet
Flag to control whether to build fNet, the stored network used for the evaluation.
Definition: MethodDL.h:181
void CreateDeepNet(DNN::TDeepNet< Architecture_t, Layer_t > &deepNet, std::vector< DNN::TDeepNet< Architecture_t, Layer_t > > &nets)
After calling the ProcesOptions(), all of the options are parsed, so using the parsed options,...
Definition: MethodDL.cxx:492
TString fErrorStrategy
The string defining the error strategy for training.
Definition: MethodDL.h:175
void DeclareOptions()
The option handling methods.
Definition: MethodDL.cxx:159
TString fInputLayoutString
The string defining the layout of the input.
Definition: MethodDL.h:172
Ranking for variables in method (implementation)
Definition: Ranking.h:48
EAnalysisType
Definition: Types.h:127
Basic string class.
Definition: TString.h:131
EInitialization
Definition: Functions.h:70
EOptimizer
Enum representing the optimizer used for training.
Definition: Functions.h:80
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:44
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:63
ELossFunction
Enum that represents objective functions for the net, i.e.
Definition: Functions.h:55
Abstract ClassifierFactory template that handles arbitrary types.
All of the options that can be specified in the training string.
Definition: MethodDL.h:62
DNN::EOptimizer optimizer
Definition: MethodDL.h:68
DNN::ERegularization regularization
Definition: MethodDL.h:67
std::vector< Double_t > dropoutProbabilities
Definition: MethodDL.h:72