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 * *
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 * (see tmva/doc/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 */
87
88
89class R__DEPRECATED(6, 46,
90 "Please train neural networks with a dedicated machine learning library instead, and use SOFIE "
91 "for inference in C++.") MethodDL : public MethodBase {
92
93private:
94 // Key-Value vector type, contining the values for the training options
95 using KeyValueVector_t = std::vector<std::map<TString, TString>>;
96
97// #ifdef R__HAS_TMVAGPU
98// #ifdef R__HAS_CUDNN
99// using ArchitectureImpl_t = TMVA::DNN::TCudnn<Float_t>;
100// #else
101// using ArchitectureImpl_t = TMVA::DNN::TCuda<Float_t>;
102// #endif
103// #else
104// do not use GPU architecture for evaluation. It is too slow for batch size=1
105 using ArchitectureImpl_t = TMVA::DNN::TCpu<Float_t>;
106// #endif
107
108 using DeepNetImpl_t = TMVA::DNN::TDeepNet<ArchitectureImpl_t>;
109 using MatrixImpl_t = typename ArchitectureImpl_t::Matrix_t;
110 using TensorImpl_t = typename ArchitectureImpl_t::Tensor_t;
111 using ScalarImpl_t = typename ArchitectureImpl_t::Scalar_t;
112 using HostBufferImpl_t = typename ArchitectureImpl_t::HostBuffer_t;
113
114 /*! The option handling methods */
115 void DeclareOptions() override;
116 void ProcessOptions() override;
117
118 void Init() override;
119
120 // Function to parse the layout of the input
121 void ParseInputLayout();
122 void ParseBatchLayout();
123
124 /*! After calling the ProcesOptions(), all of the options are parsed,
125 * so using the parsed options, and given the architecture and the
126 * type of the layers, we build the Deep Network passed as
127 * a reference in the function. */
128 template <typename Architecture_t, typename Layer_t>
129 void CreateDeepNet(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
130 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets);
131
132 template <typename Architecture_t, typename Layer_t>
133 void ParseDenseLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
134 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
135
136 template <typename Architecture_t, typename Layer_t>
137 void ParseConvLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
138 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
139
140 template <typename Architecture_t, typename Layer_t>
141 void ParseMaxPoolLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
142 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
143 TString delim);
144
145 template <typename Architecture_t, typename Layer_t>
146 void ParseReshapeLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
147 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
148 TString delim);
149
150 template <typename Architecture_t, typename Layer_t>
151 void ParseBatchNormLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
152 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
153 TString delim);
154
155 enum ERecurrentLayerType { kLayerRNN = 0, kLayerLSTM = 1, kLayerGRU = 2 };
156 template <typename Architecture_t, typename Layer_t>
157 void ParseRecurrentLayer(ERecurrentLayerType type, DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
158 std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
159
160
161 /// train of deep neural network using the defined architecture
162 template <typename Architecture_t>
163 void TrainDeepNet();
164
165 /// perform prediction of the deep neural network
166 /// using batches (called by GetMvaValues)
167 template <typename Architecture_t>
168 std::vector<Double_t> PredictDeepNet(Long64_t firstEvt, Long64_t lastEvt, size_t batchSize, Bool_t logProgress);
169
170 /// Get the input event tensor for evaluation
171 /// Internal function to fill the fXInput tensor with the correct shape from TMVA current Event class
172 void FillInputTensor();
173
174 /// parce the validation string and return the number of event data used for validation
175 UInt_t GetNumValidationSamples();
176
177 // cudnn implementation needs this format
178 /** Contains the batch size (no. of images in the batch), input depth (no. channels)
179 * and further input dimensions of the data (image height, width ...)*/
180 std::vector<size_t> fInputShape;
181
182 // The size of the batch, i.e. the number of images that are contained in the batch, is either set to be the depth
183 // or the height of the batch
184 size_t fBatchDepth; ///< The depth of the batch used to train the deep net.
185 size_t fBatchHeight; ///< The height of the batch used to train the deep net.
186 size_t fBatchWidth; ///< The width of the batch used to train the deep net.
187
188 size_t fRandomSeed; ///<The random seed used to initialize the weights and shuffling batches (default is zero)
189
190 DNN::EInitialization fWeightInitialization; ///< The initialization method
191 DNN::EOutputFunction fOutputFunction; ///< The output function for making the predictions
192 DNN::ELossFunction fLossFunction; ///< The loss function
193
194 TString fInputLayoutString; ///< The string defining the layout of the input
195 TString fBatchLayoutString; ///< The string defining the layout of the batch
196 TString fLayoutString; ///< The string defining the layout of the deep net
197 TString fErrorStrategy; ///< The string defining the error strategy for training
198 TString fTrainingStrategyString; ///< The string defining the training strategy
199 TString fWeightInitializationString; ///< The string defining the weight initialization method
200 TString fArchitectureString; ///< The string defining the architecture: CPU or GPU
201 TString fNumValidationString; ///< The string defining the number (or percentage) of training data used for validation
202 bool fResume;
203 bool fBuildNet; ///< Flag to control whether to build fNet, the stored network used for the evaluation
204
205 KeyValueVector_t fSettings; ///< Map for the training strategy
206 std::vector<TTrainingSettings> fTrainingSettings; ///< The vector defining each training strategy
207
208 TensorImpl_t fXInput; // input tensor used to evaluate fNet
209 HostBufferImpl_t fXInputBuffer; // input host buffer corresponding to X (needed for GPU implementation)
210 std::unique_ptr<MatrixImpl_t> fYHat; // output prediction matrix of fNet
211 std::unique_ptr<DeepNetImpl_t> fNet;
212
213
215
216protected:
217 // provide a help message
218 void GetHelpMessage() const override;
219
220 std::vector<Double_t> GetMvaValues(Long64_t firstEvt, Long64_t lastEvt, Bool_t logProgress) override;
221
222
223public:
224 /*! Constructor */
225 MethodDL(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption);
226
227 /*! Constructor */
228 MethodDL(DataSetInfo &theData, const TString &theWeightFile);
229
230 /*! Virtual Destructor */
231 virtual ~MethodDL();
232
233 /*! Function for parsing the training settings, provided as a string
234 * in a key-value form. */
235 KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim);
236
237 /*! Check the type of analysis the deep learning network can do */
238 Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets) override;
239
240 /*! Methods for training the deep learning network */
241 void Train() override;
242
243 Double_t GetMvaValue(Double_t *err = nullptr, Double_t *errUpper = nullptr) override;
244 const std::vector<Float_t>& GetRegressionValues() override;
245 const std::vector<Float_t>& GetMulticlassValues() override;
246
247 /*! Methods for writing and reading weights */
248 using MethodBase::ReadWeightsFromStream;
249 void AddWeightsXMLTo(void *parent) const override;
250 void ReadWeightsFromXML(void *wghtnode) override;
251 void ReadWeightsFromStream(std::istream &) override;
252
253 /* Create ranking */
254 const Ranking *CreateRanking() override;
255
256 /* Getters */
257 size_t GetInputDepth() const { return fInputShape[1]; } //< no. of channels for an image
258 size_t GetInputHeight() const { return fInputShape[2]; }
259 size_t GetInputWidth() const { return fInputShape[3]; }
260 size_t GetInputDim() const { return fInputShape.size() - 2; }
261 std::vector<size_t> GetInputShape() const { return fInputShape; }
262
263 size_t GetBatchSize() const { return fInputShape[0]; }
264 size_t GetBatchDepth() const { return fBatchDepth; }
265 size_t GetBatchHeight() const { return fBatchHeight; }
266 size_t GetBatchWidth() const { return fBatchWidth; }
267
268 const DeepNetImpl_t & GetDeepNet() const { return *fNet; }
269
270 DNN::EInitialization GetWeightInitialization() const { return fWeightInitialization; }
271 DNN::EOutputFunction GetOutputFunction() const { return fOutputFunction; }
272 DNN::ELossFunction GetLossFunction() const { return fLossFunction; }
273
274 TString GetInputLayoutString() const { return fInputLayoutString; }
275 TString GetBatchLayoutString() const { return fBatchLayoutString; }
276 TString GetLayoutString() const { return fLayoutString; }
277 TString GetErrorStrategyString() const { return fErrorStrategy; }
278 TString GetTrainingStrategyString() const { return fTrainingStrategyString; }
279 TString GetWeightInitializationString() const { return fWeightInitializationString; }
280 TString GetArchitectureString() const { return fArchitectureString; }
281
282 const std::vector<TTrainingSettings> &GetTrainingSettings() const { return fTrainingSettings; }
283 std::vector<TTrainingSettings> &GetTrainingSettings() { return fTrainingSettings; }
284 const KeyValueVector_t &GetKeyValueSettings() const { return fSettings; }
285 KeyValueVector_t &GetKeyValueSettings() { return fSettings; }
286
287 /** Setters */
288 void SetInputDepth (int inputDepth) { fInputShape[1] = inputDepth; }
289 void SetInputHeight(int inputHeight) { fInputShape[2] = inputHeight; }
290 void SetInputWidth (int inputWidth) { fInputShape[3] = inputWidth; }
291 void SetInputShape (std::vector<size_t> inputShape) { fInputShape = std::move(inputShape); }
292
293 void SetBatchSize (size_t batchSize) { fInputShape[0] = batchSize; }
294 void SetBatchDepth (size_t batchDepth) { fBatchDepth = batchDepth; }
295 void SetBatchHeight(size_t batchHeight) { fBatchHeight = batchHeight; }
296 void SetBatchWidth (size_t batchWidth) { fBatchWidth = batchWidth; }
297
298 void SetWeightInitialization(DNN::EInitialization weightInitialization)
299 {
300 fWeightInitialization = weightInitialization;
301 }
302 void SetOutputFunction (DNN::EOutputFunction outputFunction) { fOutputFunction = outputFunction; }
303 void SetErrorStrategyString (TString errorStrategy) { fErrorStrategy = errorStrategy; }
304 void SetTrainingStrategyString (TString trainingStrategyString) { fTrainingStrategyString = trainingStrategyString; }
305 void SetWeightInitializationString(TString weightInitializationString)
306 {
307 fWeightInitializationString = weightInitializationString;
308 }
309 void SetArchitectureString (TString architectureString) { fArchitectureString = architectureString; }
310 void SetLayoutString (TString layoutString) { fLayoutString = layoutString; }
311};
312
313} // namespace TMVA
314
315#endif
#define R__DEPRECATED(MAJOR, MINOR, REASON)
Definition RConfig.hxx:486
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:78
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
#define ClassDefOverride(name, id)
Definition Rtypes.h:347
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
The TCpu architecture class.
Definition Cpu.h:65
Generic Deep Neural Network class.
Definition DeepNet.h:73
Virtual base Class for all MVA method.
Definition MethodBase.h:79
Deep neural network method, with dense, convolutional and recurrent layers.
Basic string class.
Definition TString.h:137
EOptimizer
Enum representing the optimizer used for training.
Definition Functions.h:82
ERegularization
Enum representing the regularization type applied for a given layer.
Definition Functions.h:65
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