Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#if 0
51#ifdef R__HAS_TMVAGPU
53#ifdef R__HAS_CUDNN
55#endif
56#endif
57#endif
58
59#include "TMVA/DNN/Functions.h"
60#include "TMVA/DNN/DeepNet.h"
61
62#include <vector>
63#include <map>
64
65#ifdef R__HAS_TMVAGPU
66//#define USE_GPU_INFERENCE
67#endif
68
69namespace TMVA {
70
71/*! All of the options that can be specified in the training string */
73 size_t batchSize;
76 size_t maxEpochs;
83 std::vector<Double_t> dropoutProbabilities;
84 std::map<TString,double> optimizerParams;
86};
87
88
89class MethodDL : public MethodBase {
90
91private:
92 // Key-Value vector type, contining the values for the training options
93 using KeyValueVector_t = std::vector<std::map<TString, TString>>;
94
95// #ifdef R__HAS_TMVAGPU
96// #ifdef R__HAS_CUDNN
97// using ArchitectureImpl_t = TMVA::DNN::TCudnn<Float_t>;
98// #else
99// using ArchitectureImpl_t = TMVA::DNN::TCuda<Float_t>;
100// #endif
101// #else
102// do not use arch GPU for evaluation. It is too slow for batch size=1
104// #endif
105
111
112 /*! The option handling methods */
113 void DeclareOptions();
114 void ProcessOptions();
115
116 void Init();
117
118 // Function to parse the layout of the input
119 void ParseInputLayout();
120 void ParseBatchLayout();
121
122 /*! After calling the ProcesOptions(), all of the options are parsed,
123 * so using the parsed options, and given the architecture and the
124 * type of the layers, we build the Deep Network passed as
125 * a reference in the function. */
126 template <typename Architecture_t, typename Layer_t>
128 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets);
129
130 template <typename Architecture_t, typename Layer_t>
132 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
133
134 template <typename Architecture_t, typename Layer_t>
136 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
137
138 template <typename Architecture_t, typename Layer_t>
140 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
141 TString delim);
142
143 template <typename Architecture_t, typename Layer_t>
145 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
146 TString delim);
147
148 template <typename Architecture_t, typename Layer_t>
150 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
151 TString delim);
152
154 template <typename Architecture_t, typename Layer_t>
156 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
157
158
159 /// train of deep neural network using the defined architecture
160 template <typename Architecture_t>
161 void TrainDeepNet();
162
163 /// perform prediction of the deep neural network
164 /// using batches (called by GetMvaValues)
165 template <typename Architecture_t>
166 std::vector<Double_t> PredictDeepNet(Long64_t firstEvt, Long64_t lastEvt, size_t batchSize, Bool_t logProgress);
167
168 /// parce the validation string and return the number of event data used for validation
170
171 // cudnn implementation needs this format
172 /** Contains the batch size (no. of images in the batch), input depth (no. channels)
173 * and furhter input dimensios of the data (image height, width ...)*/
174 std::vector<size_t> fInputShape;
175
176 // The size of the batch, i.e. the number of images that are contained in the batch, is either set to be the depth
177 // or the height of the batch
178 size_t fBatchDepth; ///< The depth of the batch used to train the deep net.
179 size_t fBatchHeight; ///< The height of the batch used to train the deep net.
180 size_t fBatchWidth; ///< The width of the batch used to train the deep net.
181
182 size_t fRandomSeed; ///<The random seed used to initialize the weights and shuffling batches (default is zero)
183
184 DNN::EInitialization fWeightInitialization; ///< The initialization method
185 DNN::EOutputFunction fOutputFunction; ///< The output function for making the predictions
186 DNN::ELossFunction fLossFunction; ///< The loss function
187
188 TString fInputLayoutString; ///< The string defining the layout of the input
189 TString fBatchLayoutString; ///< The string defining the layout of the batch
190 TString fLayoutString; ///< The string defining the layout of the deep net
191 TString fErrorStrategy; ///< The string defining the error strategy for training
192 TString fTrainingStrategyString; ///< The string defining the training strategy
193 TString fWeightInitializationString; ///< The string defining the weight initialization method
194 TString fArchitectureString; ///< The string defining the architecure: CPU or GPU
195 TString fNumValidationString; ///< The string defining the number (or percentage) of training data used for validation
197 bool fBuildNet; ///< Flag to control whether to build fNet, the stored network used for the evaluation
198
199 KeyValueVector_t fSettings; ///< Map for the training strategy
200 std::vector<TTrainingSettings> fTrainingSettings; ///< The vector defining each training strategy
201
202 TensorImpl_t fXInput; // input tensor used to evaluate fNet
203 HostBufferImpl_t fXInputBuffer; // input hist buffer corresponding to X (needed for GPU implementation)
204 std::unique_ptr<MatrixImpl_t> fYHat; // output prediction matrix of fNet
205 std::unique_ptr<DeepNetImpl_t> fNet;
206
207
208 ClassDef(MethodDL, 0);
209
210protected:
211 // provide a help message
212 void GetHelpMessage() const;
213
214 virtual std::vector<Double_t> GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress);
215
216
217public:
218 /*! Constructor */
219 MethodDL(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption);
220
221 /*! Constructor */
222 MethodDL(DataSetInfo &theData, const TString &theWeightFile);
223
224 /*! Virtual Destructor */
225 virtual ~MethodDL();
226
227 /*! Function for parsing the training settings, provided as a string
228 * in a key-value form. */
229 KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim);
230
231 /*! Check the type of analysis the deep learning network can do */
232 Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets);
233
234 /*! Methods for training the deep learning network */
235 void Train();
236
237 Double_t GetMvaValue(Double_t *err = 0, Double_t *errUpper = 0);
238 virtual const std::vector<Float_t>& GetRegressionValues();
239 virtual const std::vector<Float_t>& GetMulticlassValues();
240
241 /*! Methods for writing and reading weights */
243 void AddWeightsXMLTo(void *parent) const;
244 void ReadWeightsFromXML(void *wghtnode);
245 void ReadWeightsFromStream(std::istream &);
246
247 /* Create ranking */
248 const Ranking *CreateRanking();
249
250 /* Getters */
251 size_t GetInputDepth() const { return fInputShape[1]; } //< no. of channels for an image
252 size_t GetInputHeight() const { return fInputShape[2]; }
253 size_t GetInputWidth() const { return fInputShape[3]; }
254 size_t GetInputDim() const { return fInputShape.size() - 2; }
255 std::vector<size_t> GetInputShape() const { return fInputShape; }
256
257 size_t GetBatchSize() const { return fInputShape[0]; }
258 size_t GetBatchDepth() const { return fBatchDepth; }
259 size_t GetBatchHeight() const { return fBatchHeight; }
260 size_t GetBatchWidth() const { return fBatchWidth; }
261
262 const DeepNetImpl_t & GetDeepNet() const { return *fNet; }
263
267
275
276 const std::vector<TTrainingSettings> &GetTrainingSettings() const { return fTrainingSettings; }
277 std::vector<TTrainingSettings> &GetTrainingSettings() { return fTrainingSettings; }
280
281 /** Setters */
282 void SetInputDepth (int inputDepth) { fInputShape[1] = inputDepth; }
283 void SetInputHeight(int inputHeight) { fInputShape[2] = inputHeight; }
284 void SetInputWidth (int inputWidth) { fInputShape[3] = inputWidth; }
285 void SetInputShape (std::vector<size_t> inputShape) { fInputShape = std::move(inputShape); }
286
287 void SetBatchSize (size_t batchSize) { fInputShape[0] = batchSize; }
288 void SetBatchDepth (size_t batchDepth) { fBatchDepth = batchDepth; }
289 void SetBatchHeight(size_t batchHeight) { fBatchHeight = batchHeight; }
290 void SetBatchWidth (size_t batchWidth) { fBatchWidth = batchWidth; }
291
293 {
294 fWeightInitialization = weightInitialization;
295 }
296 void SetOutputFunction (DNN::EOutputFunction outputFunction) { fOutputFunction = outputFunction; }
297 void SetErrorStrategyString (TString errorStrategy) { fErrorStrategy = errorStrategy; }
298 void SetTrainingStrategyString (TString trainingStrategyString) { fTrainingStrategyString = trainingStrategyString; }
299 void SetWeightInitializationString(TString weightInitializationString)
300 {
301 fWeightInitializationString = weightInitializationString;
302 }
303 void SetArchitectureString (TString architectureString) { fArchitectureString = architectureString; }
304 void SetLayoutString (TString layoutString) { fLayoutString = layoutString; }
305};
306
307} // namespace TMVA
308
309#endif
bool Bool_t
Definition RtypesCore.h:63
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:73
#define ClassDef(name, id)
Definition Rtypes.h:325
int type
Definition TGX11.cxx:121
The TCpu architecture class.
Definition Cpu.h:65
AReal Scalar_t
Definition Cpu.h:69
TCpuTensor< AReal > Tensor_t
Definition Cpu.h:70
TCpuBuffer< AReal > HostBuffer_t
Definition Cpu.h:72
TCpuMatrix< AReal > Matrix_t
Definition Cpu.h:71
Generic Deep Neural Network class.
Definition DeepNet.h:73
Class that contains all the data information.
Definition DataSetInfo.h:62
Virtual base Class for all MVA method.
Definition MethodBase.h:111
virtual void ReadWeightsFromStream(std::istream &)=0
KeyValueVector_t & GetKeyValueSettings()
Definition MethodDL.h:279
typename ArchitectureImpl_t::Tensor_t TensorImpl_t
Definition MethodDL.h:108
size_t fBatchHeight
The height of the batch used to train the deep net.
Definition MethodDL.h:179
void GetHelpMessage() const
DNN::ELossFunction fLossFunction
The loss function.
Definition MethodDL.h:186
size_t GetInputDim() const
Definition MethodDL.h:254
virtual const std::vector< Float_t > & GetMulticlassValues()
TString GetErrorStrategyString() const
Definition MethodDL.h:271
std::vector< size_t > fInputShape
Contains the batch size (no.
Definition MethodDL.h:174
void SetErrorStrategyString(TString errorStrategy)
Definition MethodDL.h:297
TString fLayoutString
The string defining the layout of the deep net.
Definition MethodDL.h:190
std::vector< TTrainingSettings > & GetTrainingSettings()
Definition MethodDL.h:277
void SetInputDepth(int inputDepth)
Setters.
Definition MethodDL.h:282
std::vector< size_t > GetInputShape() const
Definition MethodDL.h:255
std::unique_ptr< MatrixImpl_t > fYHat
Definition MethodDL.h:204
void Train()
Methods for training the deep learning network.
size_t GetBatchHeight() const
Definition MethodDL.h:259
TString GetTrainingStrategyString() const
Definition MethodDL.h:272
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.
TString fWeightInitializationString
The string defining the weight initialization method.
Definition MethodDL.h:193
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:770
TensorImpl_t fXInput
Definition MethodDL.h:202
size_t fRandomSeed
The random seed used to initialize the weights and shuffling batches (default is zero)
Definition MethodDL.h:182
TString fArchitectureString
The string defining the architecure: CPU or GPU.
Definition MethodDL.h:194
void Init()
default initializations
Definition MethodDL.cxx:432
void TrainDeepNet()
train of deep neural network using the defined architecture
const std::vector< TTrainingSettings > & GetTrainingSettings() const
Definition MethodDL.h:276
DNN::EOutputFunction GetOutputFunction() const
Definition MethodDL.h:265
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:583
UInt_t GetNumValidationSamples()
parce the validation string and return the number of event data used for validation
TString GetBatchLayoutString() const
Definition MethodDL.h:269
void SetInputWidth(int inputWidth)
Definition MethodDL.h:284
void SetArchitectureString(TString architectureString)
Definition MethodDL.h:303
void ProcessOptions()
Definition MethodDL.cxx:219
HostBufferImpl_t fXInputBuffer
Definition MethodDL.h:203
size_t fBatchWidth
The width of the batch used to train the deep net.
Definition MethodDL.h:180
size_t GetInputDepth() const
Definition MethodDL.h:251
virtual const std::vector< Float_t > & GetRegressionValues()
std::unique_ptr< DeepNetImpl_t > fNet
Definition MethodDL.h:205
TString GetWeightInitializationString() const
Definition MethodDL.h:273
TString GetInputLayoutString() const
Definition MethodDL.h:268
void SetBatchHeight(size_t batchHeight)
Definition MethodDL.h:289
size_t GetInputHeight() const
Definition MethodDL.h:252
TString GetArchitectureString() const
Definition MethodDL.h:274
void ParseBatchLayout()
Parse the input layout.
Definition MethodDL.cxx:482
void ParseBatchNormLayer(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:892
void ReadWeightsFromStream(std::istream &)
void ReadWeightsFromXML(void *wghtnode)
TString fNumValidationString
The string defining the number (or percentage) of training data used for validation.
Definition MethodDL.h:195
const KeyValueVector_t & GetKeyValueSettings() const
Definition MethodDL.h:278
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition MethodDL.h:93
DNN::EOutputFunction fOutputFunction
The output function for making the predictions.
Definition MethodDL.h:185
DNN::EInitialization fWeightInitialization
The initialization method.
Definition MethodDL.h:184
void SetOutputFunction(DNN::EOutputFunction outputFunction)
Definition MethodDL.h:296
size_t GetBatchDepth() const
Definition MethodDL.h:258
void ParseRecurrentLayer(ERecurrentLayerType type, 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:933
std::vector< TTrainingSettings > fTrainingSettings
The vector defining each training strategy.
Definition MethodDL.h:200
size_t GetInputWidth() const
Definition MethodDL.h:253
void SetInputShape(std::vector< size_t > inputShape)
Definition MethodDL.h:285
DNN::ELossFunction GetLossFunction() const
Definition MethodDL.h:266
TString fBatchLayoutString
The string defining the layout of the batch.
Definition MethodDL.h:189
void SetWeightInitializationString(TString weightInitializationString)
Definition MethodDL.h:299
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
Check the type of analysis the deep learning network can do.
size_t GetBatchSize() const
Definition MethodDL.h:257
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:671
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:831
TString fTrainingStrategyString
The string defining the training strategy.
Definition MethodDL.h:192
void SetTrainingStrategyString(TString trainingStrategyString)
Definition MethodDL.h:298
const Ranking * CreateRanking()
void SetLayoutString(TString layoutString)
Definition MethodDL.h:304
const DeepNetImpl_t & GetDeepNet() const
Definition MethodDL.h:262
typename ArchitectureImpl_t::HostBuffer_t HostBufferImpl_t
Definition MethodDL.h:110
void SetBatchDepth(size_t batchDepth)
Definition MethodDL.h:288
KeyValueVector_t fSettings
Map for the training strategy.
Definition MethodDL.h:199
KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim)
Function for parsing the training settings, provided as a string in a key-value form.
void SetBatchWidth(size_t batchWidth)
Definition MethodDL.h:290
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)
DNN::EInitialization GetWeightInitialization() const
Definition MethodDL.h:264
void SetBatchSize(size_t batchSize)
Definition MethodDL.h:287
void SetWeightInitialization(DNN::EInitialization weightInitialization)
Definition MethodDL.h:292
TString GetLayoutString() const
Definition MethodDL.h:270
size_t fBatchDepth
The depth of the batch used to train the deep net.
Definition MethodDL.h:178
size_t GetBatchWidth() const
Definition MethodDL.h:260
void AddWeightsXMLTo(void *parent) const
typename ArchitectureImpl_t::Matrix_t MatrixImpl_t
Definition MethodDL.h:107
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
virtual ~MethodDL()
Virtual Destructor.
typename ArchitectureImpl_t::Scalar_t ScalarImpl_t
Definition MethodDL.h:109
void ParseInputLayout()
Parse the input layout.
Definition MethodDL.cxx:439
bool fBuildNet
Flag to control whether to build fNet, the stored network used for the evaluation.
Definition MethodDL.h:197
void SetInputHeight(int inputHeight)
Definition MethodDL.h:283
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:529
TString fErrorStrategy
The string defining the error strategy for training.
Definition MethodDL.h:191
void DeclareOptions()
The option handling methods.
Definition MethodDL.cxx:167
TString fInputLayoutString
The string defining the layout of the input.
Definition MethodDL.h:188
Ranking for variables in method (implementation)
Definition Ranking.h:48
Basic string class.
Definition TString.h:136
EOptimizer
Enum representing the optimizer used for training.
Definition Functions.h:82
EOutputFunction
Enum that represents output functions.
Definition Functions.h:46
ERegularization
Enum representing the regularization type applied for a given layer.
Definition Functions.h:65
ELossFunction
Enum that represents objective functions for the net, i.e.
Definition Functions.h:57
create variable transformations
All of the options that can be specified in the training string.
Definition MethodDL.h:72
std::map< TString, double > optimizerParams
Definition MethodDL.h:84
DNN::EOptimizer optimizer
Definition MethodDL.h:78
DNN::ERegularization regularization
Definition MethodDL.h:77
std::vector< Double_t > dropoutProbabilities
Definition MethodDL.h:83