Logo ROOT   6.14/05
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 
59 namespace TMVA {
60 
61 /*! All of the options that can be specified in the training string */
63  size_t batchSize;
64  size_t testInterval;
66  size_t maxEpochs;
71  std::vector<Double_t> dropoutProbabilities;
73 };
74 
75 class MethodDL : public MethodBase {
76 
77 private:
78  // Key-Value vector type, contining the values for the training options
79  using KeyValueVector_t = std::vector<std::map<TString, TString>>;
80 // #ifdef R__HAS_TMVAGPU
81 // using ArchitectureImpl_t = TMVA::DNN::TCuda<Double_t>;
82 // #else
83 // do not use arch GPU for evaluation. It is too slow for batch size=1
84 #ifdef R__HAS_TMVACPU
86 #else
88 #endif
89 //#endif
91  std::unique_ptr<DeepNetImpl_t> fNet;
92 
93  /*! The option handling methods */
94  void DeclareOptions();
95  void ProcessOptions();
96 
97  void Init();
98 
99  // Function to parse the layout of the input
100  void ParseInputLayout();
101  void ParseBatchLayout();
102 
103  /*! After calling the ProcesOptions(), all of the options are parsed,
104  * so using the parsed options, and given the architecture and the
105  * type of the layers, we build the Deep Network passed as
106  * a reference in the function. */
107  template <typename Architecture_t, typename Layer_t>
108  void CreateDeepNet(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
109  std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets);
110 
111  template <typename Architecture_t, typename Layer_t>
112  void ParseDenseLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
113  std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
114 
115  template <typename Architecture_t, typename Layer_t>
116  void ParseConvLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
117  std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
118 
119  template <typename Architecture_t, typename Layer_t>
120  void ParseMaxPoolLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
121  std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
122  TString delim);
123 
124  template <typename Architecture_t, typename Layer_t>
125  void ParseReshapeLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
126  std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString,
127  TString delim);
128 
129  template <typename Architecture_t, typename Layer_t>
130  void ParseRnnLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
131  std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
132 
133  template <typename Architecture_t, typename Layer_t>
134  void ParseLstmLayer(DNN::TDeepNet<Architecture_t, Layer_t> &deepNet,
135  std::vector<DNN::TDeepNet<Architecture_t, Layer_t>> &nets, TString layerString, TString delim);
136 
137  template <typename Architecture_t>
138  void TrainDeepNet();
139 
140  size_t fInputDepth; ///< The depth of the input.
141  size_t fInputHeight; ///< The height of the input.
142  size_t fInputWidth; ///< The width of the input.
143 
144  size_t fBatchDepth; ///< The depth of the batch used to train the deep net.
145  size_t fBatchHeight; ///< The height of the batch used to train the deep net.
146  size_t fBatchWidth; ///< The width of the batch used to train the deep net.
147 
148  size_t fRandomSeed; ///<The random seed used to initialize the weights and shuffling batches (default is zero)
149 
150  DNN::EInitialization fWeightInitialization; ///< The initialization method
151  DNN::EOutputFunction fOutputFunction; ///< The output function for making the predictions
152  DNN::ELossFunction fLossFunction; ///< The loss function
153 
154  TString fInputLayoutString; ///< The string defining the layout of the input
155  TString fBatchLayoutString; ///< The string defining the layout of the batch
156  TString fLayoutString; ///< The string defining the layout of the deep net
157  TString fErrorStrategy; ///< The string defining the error strategy for training
158  TString fTrainingStrategyString; ///< The string defining the training strategy
159  TString fWeightInitializationString; ///< The string defining the weight initialization method
160  TString fArchitectureString; ///< The string defining the architecure: CPU or GPU
161  bool fResume;
162  bool fBuildNet; ///< Flag to control whether to build fNet, the stored network used for the evaluation
163 
164  KeyValueVector_t fSettings; ///< Map for the training strategy
165  std::vector<TTrainingSettings> fTrainingSettings; ///< The vector defining each training strategy
166 
167  ClassDef(MethodDL, 0);
168 
169 protected:
170  // provide a help message
171  void GetHelpMessage() const;
172 
173 public:
174  /*! Constructor */
175  MethodDL(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption);
176 
177  /*! Constructor */
178  MethodDL(DataSetInfo &theData, const TString &theWeightFile);
179 
180  /*! Virtual Destructor */
181  virtual ~MethodDL();
182 
183  /*! Function for parsing the training settings, provided as a string
184  * in a key-value form. */
185  KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim);
186 
187  /*! Check the type of analysis the deep learning network can do */
188  Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets);
189 
190  /*! Methods for training the deep learning network */
191  void Train();
192 
193  Double_t GetMvaValue(Double_t *err = 0, Double_t *errUpper = 0);
194 
195  /*! Methods for writing and reading weights */
197  void AddWeightsXMLTo(void *parent) const;
198  void ReadWeightsFromXML(void *wghtnode);
199  void ReadWeightsFromStream(std::istream &);
200 
201  /* Create ranking */
202  const Ranking *CreateRanking();
203 
204  /* Getters */
205  size_t GetInputDepth() const { return fInputDepth; }
206  size_t GetInputHeight() const { return fInputHeight; }
207  size_t GetInputWidth() const { return fInputWidth; }
208 
209  size_t GetBatchDepth() const { return fBatchDepth; }
210  size_t GetBatchHeight() const { return fBatchHeight; }
211  size_t GetBatchWidth() const { return fBatchWidth; }
212 
213  const DeepNetImpl_t & GetDeepNet() const { return *fNet; }
214 
215  DNN::EInitialization GetWeightInitialization() const { return fWeightInitialization; }
216  DNN::EOutputFunction GetOutputFunction() const { return fOutputFunction; }
217  DNN::ELossFunction GetLossFunction() const { return fLossFunction; }
218 
219  TString GetInputLayoutString() const { return fInputLayoutString; }
220  TString GetBatchLayoutString() const { return fBatchLayoutString; }
221  TString GetLayoutString() const { return fLayoutString; }
222  TString GetErrorStrategyString() const { return fErrorStrategy; }
223  TString GetTrainingStrategyString() const { return fTrainingStrategyString; }
224  TString GetWeightInitializationString() const { return fWeightInitializationString; }
225  TString GetArchitectureString() const { return fArchitectureString; }
226 
227  const std::vector<TTrainingSettings> &GetTrainingSettings() const { return fTrainingSettings; }
228  std::vector<TTrainingSettings> &GetTrainingSettings() { return fTrainingSettings; }
229  const KeyValueVector_t &GetKeyValueSettings() const { return fSettings; }
230  KeyValueVector_t &GetKeyValueSettings() { return fSettings; }
231 
232  /** Setters */
233  void SetInputDepth(size_t inputDepth) { fInputDepth = inputDepth; }
234  void SetInputHeight(size_t inputHeight) { fInputHeight = inputHeight; }
235  void SetInputWidth(size_t inputWidth) { fInputWidth = inputWidth; }
236 
237  void SetBatchDepth(size_t batchDepth) { fBatchDepth = batchDepth; }
238  void SetBatchHeight(size_t batchHeight) { fBatchHeight = batchHeight; }
239  void SetBatchWidth(size_t batchWidth) { fBatchWidth = batchWidth; }
240 
242  {
243  fWeightInitialization = weightInitialization;
244  }
245  void SetOutputFunction(DNN::EOutputFunction outputFunction) { fOutputFunction = outputFunction; }
246  void SetErrorStrategyString(TString errorStrategy) { fErrorStrategy = errorStrategy; }
247  void SetTrainingStrategyString(TString trainingStrategyString) { fTrainingStrategyString = trainingStrategyString; }
248  void SetWeightInitializationString(TString weightInitializationString)
249  {
250  fWeightInitializationString = weightInitializationString;
251  }
252  void SetArchitectureString(TString architectureString) { fArchitectureString = architectureString; }
253  void SetLayoutString(TString layoutString) { fLayoutString = layoutString; }
254 };
255 
256 } // namespace TMVA
257 
258 #endif
void SetBatchHeight(size_t batchHeight)
Definition: MethodDL.h:238
DNN::ELossFunction GetLossFunction() const
Definition: MethodDL.h:217
void SetArchitectureString(TString architectureString)
Definition: MethodDL.h:252
TString fLayoutString
The string defining the layout of the deep net.
Definition: MethodDL.h:156
The TCpu architecture class.
Definition: Cpu.h:43
void SetInputDepth(size_t inputDepth)
Setters.
Definition: MethodDL.h:233
void SetWeightInitializationString(TString weightInitializationString)
Definition: MethodDL.h:248
std::vector< TTrainingSettings > & GetTrainingSettings()
Definition: MethodDL.h:228
void SetBatchWidth(size_t batchWidth)
Definition: MethodDL.h:239
DNN::EInitialization GetWeightInitialization() const
Definition: MethodDL.h:215
EAnalysisType
Definition: Types.h:127
Virtual base Class for all MVA method.
Definition: MethodBase.h:109
DNN::EOutputFunction fOutputFunction
The output function for making the predictions.
Definition: MethodDL.h:151
DNN::EInitialization fWeightInitialization
The initialization method.
Definition: MethodDL.h:150
size_t GetBatchWidth() const
Definition: MethodDL.h:211
TString GetLayoutString() const
Definition: MethodDL.h:221
Basic string class.
Definition: TString.h:131
Ranking for variables in method (implementation)
Definition: Ranking.h:48
bool Bool_t
Definition: RtypesCore.h:59
void SetOutputFunction(DNN::EOutputFunction outputFunction)
Definition: MethodDL.h:245
size_t fRandomSeed
The random seed used to initialize the weights and shuffling batches (default is zero) ...
Definition: MethodDL.h:148
void SetErrorStrategyString(TString errorStrategy)
Definition: MethodDL.h:246
TString fArchitectureString
The string defining the architecure: CPU or GPU.
Definition: MethodDL.h:160
void SetLayoutString(TString layoutString)
Definition: MethodDL.h:253
EInitialization
Definition: Functions.h:70
void SetWeightInitialization(DNN::EInitialization weightInitialization)
Definition: MethodDL.h:241
size_t fInputDepth
The depth of the input.
Definition: MethodDL.h:140
#define ClassDef(name, id)
Definition: Rtypes.h:320
The reference architecture class.
Definition: DataLoader.h:30
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:121
const DeepNetImpl_t & GetDeepNet() const
Definition: MethodDL.h:213
TString fTrainingStrategyString
The string defining the training strategy.
Definition: MethodDL.h:158
bool fBuildNet
Flag to control whether to build fNet, the stored network used for the evaluation.
Definition: MethodDL.h:162
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDL.h:79
void SetBatchDepth(size_t batchDepth)
Definition: MethodDL.h:237
TString fBatchLayoutString
The string defining the layout of the batch.
Definition: MethodDL.h:155
Class that contains all the data information.
Definition: DataSetInfo.h:60
TString GetBatchLayoutString() const
Definition: MethodDL.h:220
TString GetArchitectureString() const
Definition: MethodDL.h:225
void SetTrainingStrategyString(TString trainingStrategyString)
Definition: MethodDL.h:247
void SetInputWidth(size_t inputWidth)
Definition: MethodDL.h:235
All of the options that can be specified in the training string.
Definition: MethodDL.h:62
size_t fInputWidth
The width of the input.
Definition: MethodDL.h:142
TString GetWeightInitializationString() const
Definition: MethodDL.h:224
DNN::ELossFunction fLossFunction
The loss function.
Definition: MethodDL.h:152
TMVA::DNN::TDeepNet< ArchitectureImpl_t > DeepNetImpl_t
Definition: MethodDL.h:90
TString GetErrorStrategyString() const
Definition: MethodDL.h:222
std::vector< TTrainingSettings > fTrainingSettings
The vector defining each training strategy.
Definition: MethodDL.h:165
const std::vector< TTrainingSettings > & GetTrainingSettings() const
Definition: MethodDL.h:227
size_t GetBatchDepth() const
Definition: MethodDL.h:209
TString GetInputLayoutString() const
Definition: MethodDL.h:219
size_t GetInputDepth() const
Definition: MethodDL.h:205
unsigned int UInt_t
Definition: RtypesCore.h:42
size_t fBatchDepth
The depth of the batch used to train the deep net.
Definition: MethodDL.h:144
KeyValueVector_t fSettings
Map for the training strategy.
Definition: MethodDL.h:164
TString fErrorStrategy
The string defining the error strategy for training.
Definition: MethodDL.h:157
DNN::ERegularization regularization
Definition: MethodDL.h:67
size_t fInputHeight
The height of the input.
Definition: MethodDL.h:141
double Double_t
Definition: RtypesCore.h:55
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:43
int type
Definition: TGX11.cxx:120
ELossFunction
Enum that represents objective functions for the net, i.e.
Definition: Functions.h:54
void SetInputHeight(size_t inputHeight)
Definition: MethodDL.h:234
TString GetTrainingStrategyString() const
Definition: MethodDL.h:223
const KeyValueVector_t & GetKeyValueSettings() const
Definition: MethodDL.h:229
KeyValueVector_t & GetKeyValueSettings()
Definition: MethodDL.h:230
Abstract ClassifierFactory template that handles arbitrary types.
DNN::EOutputFunction GetOutputFunction() const
Definition: MethodDL.h:216
size_t fBatchHeight
The height of the batch used to train the deep net.
Definition: MethodDL.h:145
TMVA::DNN::TReference< Double_t > ArchitectureImpl_t
Definition: MethodDL.h:87
std::vector< Double_t > dropoutProbabilities
Definition: MethodDL.h:71
TString fInputLayoutString
The string defining the layout of the input.
Definition: MethodDL.h:154
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:62
virtual void ReadWeightsFromStream(std::istream &)=0
size_t GetInputHeight() const
Definition: MethodDL.h:206
size_t GetBatchHeight() const
Definition: MethodDL.h:210
size_t GetInputWidth() const
Definition: MethodDL.h:207
size_t fBatchWidth
The width of the batch used to train the deep net.
Definition: MethodDL.h:146
TString fWeightInitializationString
The string defining the weight initialization method.
Definition: MethodDL.h:159
std::unique_ptr< DeepNetImpl_t > fNet
Definition: MethodDL.h:91
Generic Deep Neural Network class.
Definition: DeepNet.h:74