Logo ROOT   6.08/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 #ifndef ROOT_TString
43 #include "TString.h"
44 #endif
45 #ifndef ROOT_TTree
46 #include "TTree.h"
47 #endif
48 #ifndef ROOT_TRandom3
49 #include "TRandom3.h"
50 #endif
51 #ifndef ROOT_TH1F
52 #include "TH1F.h"
53 #endif
54 #ifndef ROOT_TMVA_MethodBase
55 #include "TMVA/MethodBase.h"
56 #endif
57 #ifndef TMVA_NEURAL_NET
58 #include "TMVA/NeuralNet.h"
59 #endif
60 
61 #include "TMVA/Tools.h"
62 
63 #include "TMVA/DNN/Net.h"
64 #include "TMVA/DNN/Minimizers.h"
66 
67 #ifdef DNNCPU
69 #endif
70 
71 #ifdef DNNCUDA
73 #endif
74 
75 namespace TMVA {
76 
77 class MethodDNN : public MethodBase
78 {
82 
83 private:
84 
85  using LayoutVector_t = std::vector<std::pair<int, DNN::EActivationFunction>>;
86  using KeyValueVector_t = std::vector<std::map<TString, TString>>;
87 
89  {
90  size_t batchSize;
91  size_t testInterval;
97  std::vector<Double_t> dropoutProbabilities;
99  };
100 
101  // the option handling methods
102  void DeclareOptions();
103  void ProcessOptions();
104 
105  // general helper functions
106  void Init();
107 
111 
118  std::vector<TTrainingSettings> fTrainingSettings;
119  bool fResume;
120 
122 
123  ClassDef(MethodDNN,0); // neural network
124 
125  static inline void WriteMatrixXML(void *parent, const char *name,
126  const TMatrixT<Double_t> &X);
127  static inline void ReadMatrixXML(void *xml, const char *name,
129 protected:
130 
131  void MakeClassSpecific( std::ostream&, const TString& ) const;
132  void GetHelpMessage() const;
133 
134 public:
135 
136  // Standard Constructors
137  MethodDNN(const TString& jobName,
138  const TString& methodTitle,
139  DataSetInfo& theData,
140  const TString& theOption);
141  MethodDNN(DataSetInfo& theData,
142  const TString& theWeightFile);
143  virtual ~MethodDNN();
144 
146  UInt_t numberClasses,
147  UInt_t numberTargets );
150  TString blockDelim,
151  TString tokenDelim);
152  void Train();
153  void TrainGpu();
154  void TrainCpu();
155 
156  virtual Double_t GetMvaValue( Double_t* err=0, Double_t* errUpper=0 );
157  virtual const std::vector<Float_t>& GetRegressionValues();
158  virtual const std::vector<Float_t>& GetMulticlassValues();
159 
161 
162  // write weights to stream
163  void AddWeightsXMLTo ( void* parent ) const;
164 
165  // read weights from stream
166  void ReadWeightsFromStream( std::istream & i );
167  void ReadWeightsFromXML ( void* wghtnode );
168 
169  // ranking of input variables
170  const Ranking* CreateRanking();
171 
172 };
173 
174 inline void MethodDNN::WriteMatrixXML(void *parent,
175  const char *name,
176  const TMatrixT<Double_t> &X)
177 {
178  std::stringstream matrixStringStream("");
179  matrixStringStream.precision( 16 );
180 
181  for (size_t i = 0; i < (size_t) X.GetNrows(); i++)
182  {
183  for (size_t j = 0; j < (size_t) X.GetNcols(); j++)
184  {
185  matrixStringStream << std::scientific << X(i,j) << " ";
186  }
187  }
188  std::string s = matrixStringStream.str();
189  void* matxml = gTools().xmlengine().NewChild(parent, 0, name);
190  gTools().xmlengine().NewAttr(matxml, 0, "rows",
191  gTools().StringFromInt((int)X.GetNrows()));
192  gTools().xmlengine().NewAttr(matxml, 0, "cols",
193  gTools().StringFromInt((int)X.GetNcols()));
194  gTools().xmlengine().AddRawLine (matxml, s.c_str());
195 }
196 
197 inline void MethodDNN::ReadMatrixXML(void *xml,
198  const char *name,
200 {
201  void *matrixXML = gTools().GetChild(xml, name);
202  size_t rows, cols;
203  gTools().ReadAttr(matrixXML, "rows", rows);
204  gTools().ReadAttr(matrixXML, "cols", cols);
205 
206  const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
207  std::stringstream matrixStringStream(matrixString);
208 
209  for (size_t i = 0; i < rows; i++)
210  {
211  for (size_t j = 0; j < cols; j++)
212  {
213  matrixStringStream >> X(i,j);
214  }
215  }
216 }
217 } // namespace TMVA
218 
219 #endif
void GetHelpMessage() const
Definition: MethodDNN.cxx:1243
TXMLEngine & xmlengine()
Definition: Tools.h:278
DNN::EOutputFunction fOutputFunction
Definition: MethodDNN.h:110
void ProcessOptions()
Definition: MethodDNN.cxx:386
std::vector< std::map< TString, TString > > KeyValueVector_t
Definition: MethodDNN.h:86
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
void MakeClassSpecific(std::ostream &, const TString &) const
Definition: MethodDNN.cxx:1237
EAnalysisType
Definition: Types.h:129
Basic string class.
Definition: TString.h:137
typename Architecture_t::Matrix_t Matrix_t
Definition: MethodDNN.h:81
bool Bool_t
Definition: RtypesCore.h:59
LayoutVector_t fLayout
Definition: MethodDNN.h:117
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xml node
Definition: TXMLEngine.cxx:938
EInitialization
Definition: Functions.h:70
Tools & gTools()
Definition: Tools.cxx:79
void ReadWeightsFromStream(std::istream &i)
Definition: MethodDNN.cxx:1222
#define ClassDef(name, id)
Definition: Rtypes.h:254
The reference architecture class.
Definition: Reference.h:37
std::vector< std::pair< int, DNN::EActivationFunction > > LayoutVector_t
Definition: MethodDNN.h:85
TMatrixT< AReal > Matrix_t
Definition: Reference.h:42
std::vector< Double_t > dropoutProbabilities
Definition: MethodDNN.h:97
void ReadWeightsFromXML(void *wghtnode)
Definition: MethodDNN.cxx:1171
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1158
TString fErrorStrategy
Definition: MethodDNN.h:113
TString fArchitectureString
Definition: MethodDNN.h:116
static void ReadMatrixXML(void *xml, const char *name, TMatrixT< Double_t > &X)
Definition: MethodDNN.h:197
KeyValueVector_t fSettings
Definition: MethodDNN.h:121
virtual ~MethodDNN()
Definition: MethodDNN.cxx:94
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:769
TString fLayoutString
Definition: MethodDNN.h:112
const Ranking * CreateRanking()
Definition: MethodDNN.cxx:1227
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
Definition: MethodDNN.cxx:101
static void WriteMatrixXML(void *parent, const char *name, const TMatrixT< Double_t > &X)
Definition: MethodDNN.h:174
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:296
DNN::EInitialization fWeightInitialization
Definition: MethodDNN.h:109
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
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:488
KeyValueVector_t ParseKeyValueString(TString parseString, TString blockDelim, TString tokenDelim)
Definition: MethodDNN.cxx:247
double Double_t
Definition: RtypesCore.h:55
EOutputFunction
Enum that represents output functions.
Definition: Functions.h:43
int type
Definition: TGX11.cxx:120
DNN::ERegularization regularization
Definition: MethodDNN.h:93
TString fTrainingStrategyString
Definition: MethodDNN.h:114
TString fWeightInitializationString
Definition: MethodDNN.h:115
void DeclareOptions()
Definition: MethodDNN.cxx:121
std::vector< TTrainingSettings > fTrainingSettings
Definition: MethodDNN.h:118
Abstract ClassifierFactory template that handles arbitrary types.
virtual Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
Definition: MethodDNN.cxx:1070
MethodDNN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption)
Definition: MethodDNN.cxx:70
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:614
LayoutVector_t ParseLayoutString(TString layerSpec)
Definition: MethodDNN.cxx:181
ERegularization
Enum representing the regularization type applied for a given layer.
Definition: Functions.h:62
void AddWeightsXMLTo(void *parent) const
Definition: MethodDNN.cxx:1146
virtual void ReadWeightsFromStream(std::istream &)=0
virtual const std::vector< Float_t > & GetMulticlassValues()
Definition: MethodDNN.cxx:1124
virtual const std::vector< Float_t > & GetRegressionValues()
Definition: MethodDNN.cxx:1086
char name[80]
Definition: TGX11.cxx:109