Loading [MathJax]/jax/output/HTML-CSS/config.js
Logo ROOT   6.18/05
Reference Guide
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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;
73 std::vector<Double_t> dropoutProbabilities;
75};
76
77
78class MethodDL : public MethodBase {
79
80private:
81 // Key-Value vector type, contining the values for the training options
82 using KeyValueVector_t = std::vector<std::map<TString, TString>>;
83// #ifdef R__HAS_TMVAGPU
84// using ArchitectureImpl_t = TMVA::DNN::TCuda<Double_t>;
85// #else
86// do not use arch GPU for evaluation. It is too slow for batch size=1
87#ifdef R__HAS_TMVACPU
89#else
91#endif
92//#endif
96
97 std::vector<MatrixImpl_t> fXInput; // input tensor used to evaluate fNet
98 std::unique_ptr<MatrixImpl_t> fYHat; // output prediction matrix of fNet
99 std::unique_ptr<DeepNetImpl_t> fNet;
100
101
102 /*! The option handling methods */
103 void DeclareOptions();
104 void ProcessOptions();
105
106 void Init();
107
108 // Function to parse the layout of the input
109 void ParseInputLayout();
110 void ParseBatchLayout();
111
112 /*! After calling the ProcesOptions(), all of the options are parsed,
113 * so using the parsed options, and given the architecture and the
114 * type of the layers, we build the Deep Network passed as
115 * a reference in the function. */
116 template <typename Architecture_t, typename Layer_t>
118 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets);
119
120 template <typename Architecture_t, typename Layer_t>
122 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
123
124 template <typename Architecture_t, typename Layer_t>
126 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
127
128 template <typename Architecture_t, typename Layer_t>
130 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
131 TString delim);
132
133 template <typename Architecture_t, typename Layer_t>
135 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
136 TString delim);
137
138 template <typename Architecture_t, typename Layer_t>
140 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
141
142 template <typename Architecture_t, typename Layer_t>
144 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
145
146 /// train of deep neural network using the defined architecture
147 template <typename Architecture_t>
148 void TrainDeepNet();
149
150 /// perform prediction of the deep neural network
151 /// using batches (called by GetMvaValues)
152 template <typename Architecture_t>
153 std::vector<Double_t> PredictDeepNet(Long64_t firstEvt, Long64_t lastEvt, size_t batchSize, Bool_t logProgress);
154
155 /// parce the validation string and return the number of event data used for validation
157
158
159 size_t fInputDepth; ///< The depth of the input.
160 size_t fInputHeight; ///< The height of the input.
161 size_t fInputWidth; ///< The width of the input.
162
163 size_t fBatchDepth; ///< The depth of the batch used to train the deep net.
164 size_t fBatchHeight; ///< The height of the batch used to train the deep net.
165 size_t fBatchWidth; ///< The width of the batch used to train the deep net.
166
167 size_t fRandomSeed; ///<The random seed used to initialize the weights and shuffling batches (default is zero)
168
169 DNN::EInitialization fWeightInitialization; ///< The initialization method
170 DNN::EOutputFunction fOutputFunction; ///< The output function for making the predictions
171 DNN::ELossFunction fLossFunction; ///< The loss function
172
173 TString fInputLayoutString; ///< The string defining the layout of the input
174 TString fBatchLayoutString; ///< The string defining the layout of the batch
175 TString fLayoutString; ///< The string defining the layout of the deep net
176 TString fErrorStrategy; ///< The string defining the error strategy for training
177 TString fTrainingStrategyString; ///< The string defining the training strategy
178 TString fWeightInitializationString; ///< The string defining the weight initialization method
179 TString fArchitectureString; ///< The string defining the architecure: CPU or GPU
180 TString fNumValidationString; ///< The string defining the number (or percentage) of training data used for validation
182 bool fBuildNet; ///< Flag to control whether to build fNet, the stored network used for the evaluation
183
184 KeyValueVector_t fSettings; ///< Map for the training strategy
185 std::vector<TTrainingSettings> fTrainingSettings; ///< The vector defining each training strategy
186
187 ClassDef(MethodDL, 0);
188
189protected:
190 // provide a help message
191 void GetHelpMessage() const;
192
193 virtual std::vector<Double_t> GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress);
194
195
196public:
197 /*! Constructor */
198 MethodDL(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption);
199
200 /*! Constructor */
201 MethodDL(DataSetInfo &theData, const TString &theWeightFile);
202
203 /*! Virtual Destructor */
204 virtual ~MethodDL();
205
206 /*! Function for parsing the training settings, provided as a string
207 * in a key-value form. */
208 KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim);
209
210 /*! Check the type of analysis the deep learning network can do */
211 Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets);
212
213 /*! Methods for training the deep learning network */
214 void Train();
215
216 Double_t GetMvaValue(Double_t *err = 0, Double_t *errUpper = 0);
217 virtual const std::vector<Float_t>& GetRegressionValues();
218 virtual const std::vector<Float_t>& GetMulticlassValues();
219
220 /*! Methods for writing and reading weights */
222 void AddWeightsXMLTo(void *parent) const;
223 void ReadWeightsFromXML(void *wghtnode);
224 void ReadWeightsFromStream(std::istream &);
225
226 /* Create ranking */
227 const Ranking *CreateRanking();
228
229 /* Getters */
230 size_t GetInputDepth() const { return fInputDepth; }
231 size_t GetInputHeight() const { return fInputHeight; }
232 size_t GetInputWidth() const { return fInputWidth; }
233
234 size_t GetBatchDepth() const { return fBatchDepth; }
235 size_t GetBatchHeight() const { return fBatchHeight; }
236 size_t GetBatchWidth() const { return fBatchWidth; }
237
238 const DeepNetImpl_t & GetDeepNet() const { return *fNet; }
239
243
251
252 const std::vector<TTrainingSettings> &GetTrainingSettings() const { return fTrainingSettings; }
253 std::vector<TTrainingSettings> &GetTrainingSettings() { return fTrainingSettings; }
256
257 /** Setters */
258 void SetInputDepth(size_t inputDepth) { fInputDepth = inputDepth; }
259 void SetInputHeight(size_t inputHeight) { fInputHeight = inputHeight; }
260 void SetInputWidth(size_t inputWidth) { fInputWidth = inputWidth; }
261
262 void SetBatchDepth(size_t batchDepth) { fBatchDepth = batchDepth; }
263 void SetBatchHeight(size_t batchHeight) { fBatchHeight = batchHeight; }
264 void SetBatchWidth(size_t batchWidth) { fBatchWidth = batchWidth; }
265
267 {
268 fWeightInitialization = weightInitialization;
269 }
270 void SetOutputFunction(DNN::EOutputFunction outputFunction) { fOutputFunction = outputFunction; }
271 void SetErrorStrategyString(TString errorStrategy) { fErrorStrategy = errorStrategy; }
272 void SetTrainingStrategyString(TString trainingStrategyString) { fTrainingStrategyString = trainingStrategyString; }
273 void SetWeightInitializationString(TString weightInitializationString)
274 {
275 fWeightInitializationString = weightInitializationString;
276 }
277 void SetArchitectureString(TString architectureString) { fArchitectureString = architectureString; }
278 void SetLayoutString(TString layoutString) { fLayoutString = layoutString; }
279};
280
281} // namespace TMVA
282
283#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:326
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:260
KeyValueVector_t & GetKeyValueSettings()
Definition: MethodDL.h:255
size_t fBatchHeight
The height of the batch used to train the deep net.
Definition: MethodDL.h:164
void GetHelpMessage() const
Definition: MethodDL.cxx:2114
DNN::ELossFunction fLossFunction
The loss function.
Definition: MethodDL.h:171
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodDL.cxx:1813
size_t fInputHeight
The height of the input.
Definition: MethodDL.h:160
TString GetErrorStrategyString() const
Definition: MethodDL.h:247
void SetErrorStrategyString(TString errorStrategy)
Definition: MethodDL.h:271
TString fLayoutString
The string defining the layout of the deep net.
Definition: MethodDL.h:175
std::vector< TTrainingSettings > & GetTrainingSettings()
Definition: MethodDL.h:253
std::unique_ptr< MatrixImpl_t > fYHat
Definition: MethodDL.h:98
void Train()
Methods for training the deep learning network.
Definition: MethodDL.cxx:1496
size_t GetBatchHeight() const
Definition: MethodDL.h:235
TString GetTrainingStrategyString() const
Definition: MethodDL.h:248
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:1839
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:871
TString fWeightInitializationString
The string defining the weight initialization method.
Definition: MethodDL.h:178
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:749
size_t fRandomSeed
The random seed used to initialize the weights and shuffling batches (default is zero)
Definition: MethodDL.h:167
TString fArchitectureString
The string defining the architecure: CPU or GPU.
Definition: MethodDL.h:179
void Init()
default initializations
Definition: MethodDL.cxx:418
MethodDL(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption)
Constructor.
Definition: MethodDL.cxx:951
void TrainDeepNet()
train of deep neural network using the defined architecture
Definition: MethodDL.cxx:1092
const std::vector< TTrainingSettings > & GetTrainingSettings() const
Definition: MethodDL.h:252
DNN::EOutputFunction GetOutputFunction() const
Definition: MethodDL.h:241
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:932
void SetInputDepth(size_t inputDepth)
Setters.
Definition: MethodDL.h:258
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:570
UInt_t GetNumValidationSamples()
parce the validation string and return the number of event data used for validation
TString GetBatchLayoutString() const
Definition: MethodDL.h:245
void SetArchitectureString(TString architectureString)
Definition: MethodDL.h:277
void ProcessOptions()
Definition: MethodDL.cxx:221
size_t fBatchWidth
The width of the batch used to train the deep net.
Definition: MethodDL.h:165
size_t GetInputDepth() const
Definition: MethodDL.h:230
virtual const std::vector< Float_t > & GetRegressionValues()
Definition: MethodDL.cxx:1776
std::unique_ptr< DeepNetImpl_t > fNet
Definition: MethodDL.h:99
TString GetWeightInitializationString() const
Definition: MethodDL.h:249
TString GetInputLayoutString() const
Definition: MethodDL.h:244
void SetBatchHeight(size_t batchHeight)
Definition: MethodDL.h:263
std::vector< MatrixImpl_t > fXInput
Definition: MethodDL.h:97
size_t GetInputHeight() const
Definition: MethodDL.h:231
TString GetArchitectureString() const
Definition: MethodDL.h:250
void ParseBatchLayout()
Parse the input layout.
Definition: MethodDL.cxx:471
void ReadWeightsFromStream(std::istream &)
Definition: MethodDL.cxx:2102
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodDL.cxx:1930
TString fNumValidationString
The string defining the number (or percentage) of training data used for validation.
Definition: MethodDL.h:180
const KeyValueVector_t & GetKeyValueSettings() const
Definition: MethodDL.h:254
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDL.h:82
DNN::EOutputFunction fOutputFunction
The output function for making the predictions.
Definition: MethodDL.h:170
DNN::EInitialization fWeightInitialization
The initialization method.
Definition: MethodDL.h:169
void SetOutputFunction(DNN::EOutputFunction outputFunction)
Definition: MethodDL.h:270
size_t GetBatchDepth() const
Definition: MethodDL.h:234
std::vector< TTrainingSettings > fTrainingSettings
The vector defining each training strategy.
Definition: MethodDL.h:185
size_t GetInputWidth() const
Definition: MethodDL.h:232
DNN::ELossFunction GetLossFunction() const
Definition: MethodDL.h:242
TString fBatchLayoutString
The string defining the layout of the batch.
Definition: MethodDL.h:174
void SetWeightInitializationString(TString weightInitializationString)
Definition: MethodDL.h:273
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:1019
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:650
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:810
TString fTrainingStrategyString
The string defining the training strategy.
Definition: MethodDL.h:177
void SetTrainingStrategyString(TString trainingStrategyString)
Definition: MethodDL.h:272
const Ranking * CreateRanking()
Definition: MethodDL.cxx:2107
void SetLayoutString(TString layoutString)
Definition: MethodDL.h:278
const DeepNetImpl_t & GetDeepNet() const
Definition: MethodDL.h:238
void SetInputHeight(size_t inputHeight)
Definition: MethodDL.h:259
void SetBatchDepth(size_t batchDepth)
Definition: MethodDL.h:262
KeyValueVector_t fSettings
Map for the training strategy.
Definition: MethodDL.h:184
size_t fInputWidth
The width of the input.
Definition: MethodDL.h:161
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:980
size_t fInputDepth
The depth of the input.
Definition: MethodDL.h:159
void SetBatchWidth(size_t batchWidth)
Definition: MethodDL.h:264
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:1647
DNN::EInitialization GetWeightInitialization() const
Definition: MethodDL.h:240
void SetWeightInitialization(DNN::EInitialization weightInitialization)
Definition: MethodDL.h:266
TString GetLayoutString() const
Definition: MethodDL.h:246
size_t fBatchDepth
The depth of the batch used to train the deep net.
Definition: MethodDL.h:163
size_t GetBatchWidth() const
Definition: MethodDL.h:236
void AddWeightsXMLTo(void *parent) const
Definition: MethodDL.cxx:1869
typename ArchitectureImpl_t::Matrix_t MatrixImpl_t
Definition: MethodDL.h:94
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Definition: MethodDL.cxx:1556
virtual ~MethodDL()
Virtual Destructor.
Definition: MethodDL.cxx:973
typename ArchitectureImpl_t::Scalar_t ScalarImpl_t
Definition: MethodDL.h:95
void ParseInputLayout()
Parse the input layout.
Definition: MethodDL.cxx:425
bool fBuildNet
Flag to control whether to build fNet, the stored network used for the evaluation.
Definition: MethodDL.h:182
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:518
TString fErrorStrategy
The string defining the error strategy for training.
Definition: MethodDL.h:176
void DeclareOptions()
The option handling methods.
Definition: MethodDL.cxx:161
TString fInputLayoutString
The string defining the layout of the input.
Definition: MethodDL.h:173
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
create variable transformations
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:73