Logo ROOT   6.12/07
Reference Guide
MethodDNN.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Peter Speckmayer
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : MethodDNN *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * NeuralNetwork *
12  * *
13  * Authors (alphabetical): *
14  * Peter Speckmayer <peter.speckmayer@gmx.at> - CERN, Switzerland *
15  * Simon Pfreundschuh <s.pfreundschuh@gmail.com> - CERN, 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 //#pragma once
29 
30 #ifndef ROOT_TMVA_MethodDNN
31 #define ROOT_TMVA_MethodDNN
32 
33 //////////////////////////////////////////////////////////////////////////
34 // //
35 // MethodDNN //
36 // //
37 // Neural Network implementation //
38 // //
39 //////////////////////////////////////////////////////////////////////////
40 
41 #include <vector>
42 #include "TString.h"
43 #include "TTree.h"
44 #include "TRandom3.h"
45 #include "TH1F.h"
46 #include "TMVA/MethodBase.h"
47 #include "TMVA/NeuralNet.h"
48 
49 #include "TMVA/Tools.h"
50 
51 #include "TMVA/DNN/Net.h"
52 #include "TMVA/DNN/Minimizers.h"
54 
55 #ifdef DNNCPU
57 #endif
58 
59 #ifdef DNNCUDA
61 #endif
62 
63 namespace TMVA {
64 
65 class MethodDNN : public MethodBase
66 {
68 
72 
73 private:
74  using LayoutVector_t = std::vector<std::pair<int, DNN::EActivationFunction>>;
75  using KeyValueVector_t = std::vector<std::map<TString, TString>>;
76 
78  {
79  size_t batchSize;
80  size_t testInterval;
86  std::vector<Double_t> dropoutProbabilities;
88  };
89 
90  // the option handling methods
91  void DeclareOptions();
92  void ProcessOptions();
93 
95 
96  // general helper functions
97  void Init();
98 
102 
110  std::vector<TTrainingSettings> fTrainingSettings;
111  bool fResume;
112 
114 
115  ClassDef(MethodDNN,0); // neural network
116 
117  static inline void WriteMatrixXML(void *parent, const char *name,
118  const TMatrixT<Double_t> &X);
119  static inline void ReadMatrixXML(void *xml, const char *name,
120  TMatrixT<Double_t> &X);
121 protected:
122 
123  void MakeClassSpecific( std::ostream&, const TString& ) const;
124  void GetHelpMessage() const;
125 
126 public:
127 
128  // Standard Constructors
129  MethodDNN(const TString& jobName,
130  const TString& methodTitle,
131  DataSetInfo& theData,
132  const TString& theOption);
133  MethodDNN(DataSetInfo& theData,
134  const TString& theWeightFile);
135  virtual ~MethodDNN();
136 
138  UInt_t numberClasses,
139  UInt_t numberTargets );
142  TString blockDelim,
143  TString tokenDelim);
144  void Train();
145  void TrainGpu();
146  void TrainCpu();
147 
148  virtual Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
149  virtual const std::vector<Float_t>& GetRegressionValues();
150  virtual const std::vector<Float_t>& GetMulticlassValues();
151 
153 
154  // write weights to stream
155  void AddWeightsXMLTo ( void* parent ) const;
156 
157  // read weights from stream
158  void ReadWeightsFromStream( std::istream & i );
159  void ReadWeightsFromXML ( void* wghtnode );
160 
161  // ranking of input variables
162  const Ranking* CreateRanking();
163 
164 };
165 
166 inline void MethodDNN::WriteMatrixXML(void *parent,
167  const char *name,
168  const TMatrixT<Double_t> &X)
169 {
170  std::stringstream matrixStringStream("");
171  matrixStringStream.precision( 16 );
172 
173  for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
174  {
175  for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
176  {
177  matrixStringStream << std::scientific << X(i,j) << " ";
178  }
179  }
180  std::string s = matrixStringStream.str();
181  void* matxml = gTools().xmlengine().NewChild(parent, 0, name);
182  gTools().xmlengine().NewAttr(matxml, 0, "rows",
183  gTools().StringFromInt((int)X.GetNrows()));
184  gTools().xmlengine().NewAttr(matxml, 0, "cols",
185  gTools().StringFromInt((int)X.GetNcols()));
186  gTools().xmlengine().AddRawLine (matxml, s.c_str());
187 }
188 
189 inline void MethodDNN::ReadMatrixXML(void *xml,
190  const char *name,
192 {
193  void *matrixXML = gTools().GetChild(xml, name);
194  size_t rows, cols;
195  gTools().ReadAttr(matrixXML, "rows", rows);
196  gTools().ReadAttr(matrixXML, "cols", cols);
197 
198  const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
199  std::stringstream matrixStringStream(matrixString);
200 
201  for (size_t i = 0; i < rows; i++)
202  {
203  for (size_t j = 0; j < cols; j++)
204  {
205  matrixStringStream >> X(i,j);
206  }
207  }
208 }
209 } // namespace TMVA
210 
211 #endif
void GetHelpMessage() const
Definition: MethodDNN.cxx:1431
TXMLEngine & xmlengine()
Definition: Tools.h:270
LayoutVector_t ParseLayoutString(TString layerSpec)
DNN::EOutputFunction fOutputFunction
Definition: MethodDNN.h:101
void ProcessOptions()
Definition: MethodDNN.cxx:412
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDNN.h:75
Int_t GetNcols() const
Definition: TMatrixTBase.h:125
void MakeClassSpecific(std::ostream &, const TString &) const
Definition: MethodDNN.cxx:1424
EAnalysisType
Definition: Types.h:125
Virtual base Class for all MVA method.
Definition: MethodBase.h:109
Basic string class.
Definition: TString.h:125
typename Architecture_t::Matrix_t Matrix_t
Definition: MethodDNN.h:71
Ranking for variables in method (implementation)
Definition: Ranking.h:48
bool Bool_t
Definition: RtypesCore.h:59
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
friend struct TestMethodDNNValidationSize
Definition: MethodDNN.h:67
LayoutVector_t fLayout
Definition: MethodDNN.h:109
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
EInitialization
Definition: Functions.h:70
void ReadWeightsFromStream(std::istream &i)
Definition: MethodDNN.cxx:1407
#define ClassDef(name, id)
Definition: Rtypes.h:320
The reference architecture class.
Definition: DataLoader.h:30
std::vector< std::pair< int, DNN::EActivationFunction > > LayoutVector_t
Definition: MethodDNN.h:74
std::vector< Double_t > dropoutProbabilities
Definition: MethodDNN.h:86
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodDNN.cxx:1355
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1161
TString fErrorStrategy
Definition: MethodDNN.h:104
TString fArchitectureString
Definition: MethodDNN.h:107
Class that contains all the data information.
Definition: DataSetInfo.h:60
static void ReadMatrixXML(void *xml, const char *name, TMatrixT< Double_t > &X)
Definition: MethodDNN.h:189
KeyValueVector_t fSettings
Definition: MethodDNN.h:113
KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim)
Bool_t AddRawLine(XMLNodePointer_t parent, const char *line)
Add just line into xml file Line should has correct xml syntax that later it can be decoded by xml pa...
Definition: TXMLEngine.cxx:867
TString fLayoutString
Definition: MethodDNN.h:103
const Ranking * CreateRanking()
Definition: MethodDNN.cxx:1413
unsigned int UInt_t
Definition: RtypesCore.h:42
static void WriteMatrixXML(void *parent, const char *name, const TMatrixT< Double_t > &X)
Definition: MethodDNN.h:166
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:290
Tools & gTools()
DNN::EInitialization fWeightInitialization
Definition: MethodDNN.h:100
Int_t GetNrows() const
Definition: TMatrixTBase.h:122
XMLAttrPointer_t NewAttr(XMLNodePointer_t xmlnode, XMLNsPointer_t, const char *name, const char *value)
creates new attribute for xmlnode, namespaces are not supported for attributes
Definition: TXMLEngine.cxx:578
UInt_t GetNumValidationSamples()
double Double_t
Definition: RtypesCore.h:55
Deep Neural Network Implementation.
Definition: MethodDNN.h:65
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:43
int type
Definition: TGX11.cxx:120
static constexpr double s
DNN::ERegularization regularization
Definition: MethodDNN.h:82
TString fTrainingStrategyString
Definition: MethodDNN.h:105
TString fWeightInitializationString
Definition: MethodDNN.h:106
void DeclareOptions()
std::vector< TTrainingSettings > fTrainingSettings
Definition: MethodDNN.h:110
virtual ~MethodDNN()
Abstract ClassifierFactory template that handles arbitrary types.
virtual Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Definition: MethodDNN.cxx:1251
MethodDNN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption)
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
Definition: TXMLEngine.cxx:707
TString fValidationSize
Definition: MethodDNN.h:108
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:62
void AddWeightsXMLTo(void *parent) const
Definition: MethodDNN.cxx:1329
virtual void ReadWeightsFromStream(std::istream &)=0
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodDNN.cxx:1306
virtual const std::vector< Float_t > & GetRegressionValues()
Definition: MethodDNN.cxx:1268
char name[80]
Definition: TGX11.cxx:109